static int do_deregister_reply_cb(const struct zoap_packet *response, struct zoap_reply *reply, const struct sockaddr *from) { u8_t code; int index; code = zoap_header_get_code(response); SYS_LOG_DBG("Deregister callback (code:%u.%u)", ZOAP_RESPONSE_CODE_CLASS(code), ZOAP_RESPONSE_CODE_DETAIL(code)); index = find_clients_index(from, false); if (index < 0) { SYS_LOG_ERR("Registration clients index not found."); return 0; } if (code == ZOAP_RESPONSE_CODE_DELETED) { clients[index].registered = 0; SYS_LOG_DBG("Deregistration success"); set_sm_state(index, ENGINE_DEREGISTERED); } else { SYS_LOG_ERR("failed with code %u.%u", ZOAP_RESPONSE_CODE_CLASS(code), ZOAP_RESPONSE_CODE_DETAIL(code)); if (get_sm_state(index) == ENGINE_DEREGISTER_SENT) { set_sm_state(index, ENGINE_DEREGISTER_FAILED); } } return 0; }
static int send_response(struct zoap_packet *request, u8_t response_code) { struct net_pkt *pkt; struct net_buf *frag; struct zoap_packet response; u8_t code, type; u16_t id; int r; code = zoap_header_get_code(request); type = zoap_header_get_type(request); id = zoap_header_get_id(request); printk("*******\n"); printk("type: %u code %u id %u\n", type, code, id); printk("*******\n"); pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); if (!pkt) { return -ENOMEM; } frag = net_buf_alloc(&zoap_data_pool, K_NO_WAIT); if (!frag) { return -ENOMEM; } net_pkt_frag_add(pkt, frag); r = zoap_packet_init(&response, pkt); if (r < 0) { return -EINVAL; } zoap_header_set_version(&response, 1); zoap_header_set_type(&response, ZOAP_TYPE_ACK); zoap_header_set_code(&response, response_code); zoap_header_set_id(&response, id); do { r = mbedtls_ssl_write(curr_ctx, frag->data, frag->len); } while (r == MBEDTLS_ERR_SSL_WANT_READ || r == MBEDTLS_ERR_SSL_WANT_WRITE); if (r >= 0) { r = 0; } net_pkt_unref(pkt); return r; }
static int do_bootstrap_reply_cb(const struct zoap_packet *response, struct zoap_reply *reply, const struct sockaddr *from) { u8_t code; int index; code = zoap_header_get_code(response); SYS_LOG_DBG("Bootstrap callback (code:%u.%u)", ZOAP_RESPONSE_CODE_CLASS(code), ZOAP_RESPONSE_CODE_DETAIL(code)); index = find_clients_index(from, true); if (index < 0) { SYS_LOG_ERR("Bootstrap client index not found."); return 0; } if (code == ZOAP_RESPONSE_CODE_CHANGED) { SYS_LOG_DBG("Considered done!"); set_sm_state(index, ENGINE_BOOTSTRAP_DONE); } else if (code == ZOAP_RESPONSE_CODE_NOT_FOUND) { SYS_LOG_ERR("Failed: NOT_FOUND. Not Retrying."); set_sm_state(index, ENGINE_DO_REGISTRATION); } else if (code == ZOAP_RESPONSE_CODE_FORBIDDEN) { SYS_LOG_ERR("Failed: 4.03 - Forbidden. Not Retrying."); set_sm_state(index, ENGINE_DO_REGISTRATION); } else { /* TODO: Read payload for error message? */ SYS_LOG_ERR("Failed with code %u.%u. Retrying ...", ZOAP_RESPONSE_CODE_CLASS(code), ZOAP_RESPONSE_CODE_DETAIL(code)); set_sm_state(index, ENGINE_INIT); } return 0; }
static int do_update_reply_cb(const struct zoap_packet *response, struct zoap_reply *reply, const struct sockaddr *from) { u8_t code; int index; code = zoap_header_get_code(response); SYS_LOG_DBG("Update callback (code:%u.%u)", ZOAP_RESPONSE_CODE_CLASS(code), ZOAP_RESPONSE_CODE_DETAIL(code)); index = find_clients_index(from, false); if (index < 0) { SYS_LOG_ERR("Registration client index not found."); return 0; } /* If NOT_FOUND just continue on */ if ((code == ZOAP_RESPONSE_CODE_CHANGED) || (code == ZOAP_RESPONSE_CODE_CREATED)) { set_sm_state(index, ENGINE_REGISTRATION_DONE); SYS_LOG_DBG("Update Done"); return 0; } /* TODO: Read payload for error message? */ /* Possible error response codes: 4.00 Bad request & 4.04 Not Found */ SYS_LOG_ERR("Failed with code %u.%u. Retrying registration", ZOAP_RESPONSE_CODE_CLASS(code), ZOAP_RESPONSE_CODE_DETAIL(code)); clients[index].registered = 0; set_sm_state(index, ENGINE_DO_REGISTRATION); return 0; }
static int do_registration_reply_cb(const struct zoap_packet *response, struct zoap_reply *reply, const struct sockaddr *from) { struct zoap_option options[2]; u8_t code; int ret, index; code = zoap_header_get_code(response); SYS_LOG_DBG("Registration callback (code:%u.%u)", ZOAP_RESPONSE_CODE_CLASS(code), ZOAP_RESPONSE_CODE_DETAIL(code)); index = find_clients_index(from, false); if (index < 0) { SYS_LOG_ERR("Registration client index not found."); return 0; } /* check state and possibly set registration to done */ if (code == ZOAP_RESPONSE_CODE_CREATED) { ret = zoap_find_options(response, ZOAP_OPTION_LOCATION_PATH, options, 2); if (ret < 0) { return ret; } if (ret < 2) { SYS_LOG_ERR("Unexpected endpoint data returned."); return -EINVAL; } /* option[0] should be "rd" */ if (options[1].len + 1 > sizeof(clients[index].server_ep)) { SYS_LOG_ERR("Unexpected length of query: " "%u (expected %zu)\n", options[1].len, sizeof(clients[index].server_ep)); return -EINVAL; } memcpy(clients[index].server_ep, options[1].value, options[1].len); clients[index].server_ep[options[1].len] = '\0'; set_sm_state(index, ENGINE_REGISTRATION_DONE); clients[index].registered = 1; SYS_LOG_INF("Registration Done (EP='%s')", clients[index].server_ep); return 0; } else if (code == ZOAP_RESPONSE_CODE_NOT_FOUND) { SYS_LOG_ERR("Failed: NOT_FOUND. Not Retrying."); set_sm_state(index, ENGINE_REGISTRATION_DONE); return 0; } else if (code == ZOAP_RESPONSE_CODE_FORBIDDEN) { SYS_LOG_ERR("Failed: 4.03 - Forbidden. Not Retrying."); set_sm_state(index, ENGINE_REGISTRATION_DONE); return 0; } /* TODO: Read payload for error message? */ /* Possible error response codes: 4.00 Bad request */ SYS_LOG_ERR("failed with code %u.%u. Re-init network", ZOAP_RESPONSE_CODE_CLASS(code), ZOAP_RESPONSE_CODE_DETAIL(code)); set_sm_state(index, ENGINE_INIT); return 0; }
static int query_get(struct zoap_resource *resource, struct zoap_packet *request, const struct sockaddr *from) { struct zoap_option options[4]; struct net_pkt *pkt; struct net_buf *frag; struct zoap_packet response; u8_t *payload, code, type; u16_t len, id; int i, r; code = zoap_header_get_code(request); type = zoap_header_get_type(request); id = zoap_header_get_id(request); r = zoap_find_options(request, ZOAP_OPTION_URI_QUERY, options, 4); if (r <= 0) { return -EINVAL; } printk("*******\n"); printk("type: %u code %u id %u\n", type, code, id); printk("num queries: %d\n", r); for (i = 0; i < r; i++) { char str[16]; if (options[i].len + 1 > sizeof(str)) { printk("Unexpected length of query: " "%d (expected %zu)\n", options[i].len, sizeof(str)); break; } memcpy(str, options[i].value, options[i].len); str[options[i].len] = '\0'; printk("query[%d]: %s\n", i + 1, str); } printk("*******\n"); pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); if (!pkt) { return -ENOMEM; } frag = net_buf_alloc(&zoap_data_pool, K_NO_WAIT); if (!frag) { return -ENOMEM; } net_pkt_frag_add(pkt, frag); r = zoap_packet_init(&response, pkt); if (r < 0) { return -EINVAL; } /* FIXME: Could be that zoap_packet_init() sets some defaults */ zoap_header_set_version(&response, 1); zoap_header_set_type(&response, ZOAP_TYPE_ACK); zoap_header_set_code(&response, ZOAP_RESPONSE_CODE_CONTENT); zoap_header_set_id(&response, id); payload = zoap_packet_get_payload(&response, &len); if (!payload) { return -EINVAL; } /* The response that coap-client expects */ r = snprintk((char *)payload, len, "Type: %u\nCode: %u\nMID: %u\n", type, code, id); if (r < 0 || r > len) { return -EINVAL; } r = zoap_packet_set_used(&response, r); if (r) { return -EINVAL; } do { r = mbedtls_ssl_write(curr_ctx, frag->data, frag->len); } while (r == MBEDTLS_ERR_SSL_WANT_READ || r == MBEDTLS_ERR_SSL_WANT_WRITE); if (r >= 0) { r = 0; } net_pkt_unref(pkt); return r; }
static int piggyback_get(struct zoap_resource *resource, struct zoap_packet *request, const struct sockaddr *from) { struct net_pkt *pkt; struct net_buf *frag; struct zoap_packet response; u8_t *payload, code, type; u16_t len, id; int r; code = zoap_header_get_code(request); type = zoap_header_get_type(request); id = zoap_header_get_id(request); printk("*******\n"); printk("type: %u code %u id %u\n", type, code, id); printk("*******\n"); pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); if (!pkt) { return -ENOMEM; } frag = net_buf_alloc(&zoap_data_pool, K_NO_WAIT); if (!frag) { return -ENOMEM; } net_pkt_frag_add(pkt, frag); r = zoap_packet_init(&response, pkt); if (r < 0) { return -EINVAL; } zoap_header_set_version(&response, 1); zoap_header_set_type(&response, ZOAP_TYPE_ACK); zoap_header_set_code(&response, ZOAP_RESPONSE_CODE_CONTENT); zoap_header_set_id(&response, id); payload = zoap_packet_get_payload(&response, &len); if (!payload) { return -EINVAL; } /* The response that coap-client expects */ r = snprintk((char *)payload, len, "Type: %u\nCode: %u\nMID: %u\n", type, code, id); if (r < 0 || r > len) { return -EINVAL; } r = zoap_packet_set_used(&response, r); if (r) { return -EINVAL; } do { r = mbedtls_ssl_write(curr_ctx, frag->data, frag->len); } while (r == MBEDTLS_ERR_SSL_WANT_READ || r == MBEDTLS_ERR_SSL_WANT_WRITE); if (r >= 0) { r = 0; } net_pkt_unref(pkt); return r; }