static void disable_observing(struct sol_coap_packet *req, struct sol_coap_server *server, struct sol_str_slice path[], const struct sol_network_link_addr *cliaddr) { struct sol_coap_packet *pkt = sol_coap_packet_new(req); uint8_t observe = 1; int i, r; if (!pkt) return; r = sol_coap_header_set_code(pkt, SOL_COAP_METHOD_GET); SOL_INT_CHECK_GOTO(r, < 0, err); r = sol_coap_header_set_type(pkt, SOL_COAP_TYPE_CON); SOL_INT_CHECK_GOTO(r, < 0, err); r = sol_coap_add_option(pkt, SOL_COAP_OPTION_OBSERVE, &observe, sizeof(observe)); SOL_INT_CHECK_GOTO(r, < 0, err); for (i = 0; path[i].data; i++) sol_coap_add_option(pkt, SOL_COAP_OPTION_URI_PATH, path[i].data, path[i].len); sol_coap_send_packet(server, pkt, cliaddr); SOL_INF("Disabled observing"); return; err: sol_coap_packet_unref(pkt); }
static int light_method_get(struct sol_coap_server *server, const struct sol_coap_resource *resource, struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr, void *data) { struct sol_coap_packet *resp; struct sol_buffer *buf; int r; resp = sol_coap_packet_new(req); if (!resp) { SOL_WRN("Could not build response packet"); return -1; } r = sol_coap_header_set_type(resp, SOL_COAP_TYPE_ACK); SOL_INT_CHECK_GOTO(r, < 0, err); r = sol_coap_header_set_code(resp, SOL_COAP_RSPCODE_CONTENT); SOL_INT_CHECK_GOTO(r, < 0, err); r = sol_coap_packet_get_payload(resp, &buf, NULL); SOL_INT_CHECK_GOTO(r, < 0, err); r = light_resource_to_rep(resource, get_scrolllock_led(), buf); SOL_INT_CHECK_GOTO(r, < 0, err); return sol_coap_send_packet(server, resp, cliaddr); err: sol_coap_packet_unref(resp); return r; }
static bool update_light(void *data) { struct light_context *context = data; struct sol_coap_server *server = context->server; struct sol_coap_resource *resource = context->resource; struct sol_coap_packet *pkt; struct sol_buffer *buf; int r; SOL_INF("Emitting notification"); pkt = sol_coap_packet_notification_new(server, resource); SOL_NULL_CHECK(pkt, false); r = sol_coap_header_set_code(pkt, SOL_COAP_RSPCODE_CONTENT); SOL_INT_CHECK_GOTO(r, < 0, err); r = sol_coap_packet_get_payload(pkt, &buf, NULL); SOL_INT_CHECK_GOTO(r, < 0, err); r = light_resource_to_rep(resource, get_scrolllock_led(), buf); SOL_INT_CHECK_GOTO(r, < 0, err); return !sol_coap_packet_send_notification(server, resource, pkt); err: sol_coap_packet_unref(pkt); return false; }
static int light_method_put(struct sol_coap_server *server, const struct sol_coap_resource *resource, struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr, void *data) { sol_coap_responsecode_t code = SOL_COAP_RSPCODE_CONTENT; struct sol_coap_packet *resp; struct sol_buffer *buf; char *sub = NULL; size_t offset; bool value; int r; sol_coap_packet_get_payload(req, &buf, &offset); if (buf) sub = strstr((char *)sol_buffer_at(buf, offset), "state\":"); if (!sub) { code = SOL_COAP_RSPCODE_BAD_REQUEST; goto done; } value = !memcmp(sub + strlen("state\":"), "true", sizeof("true") - 1); SOL_INF("Changing light state to %s", value ? "on" : "off"); set_scrolllock_led(value); done: resp = sol_coap_packet_new(req); if (!resp) { SOL_WRN("Could not build response packet"); return -1; } r = sol_coap_header_set_type(resp, SOL_COAP_TYPE_ACK); SOL_INT_CHECK_GOTO(r, < 0, err); r = sol_coap_header_set_code(resp, code); SOL_INT_CHECK_GOTO(r, < 0, err); return sol_coap_send_packet(server, resp, cliaddr); err: sol_coap_packet_unref(resp); return r; }
static int light_method_put(const struct sol_coap_resource *resource, struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr, void *data) { struct sol_coap_server *server = (void *)data; struct sol_coap_packet *resp; char *sub = NULL; uint8_t *p; uint16_t len; bool value; sol_coap_responsecode_t code = SOL_COAP_RSPCODE_CONTENT; sol_coap_packet_get_payload(req, &p, &len); if (p) sub = strstr((char *)p, "state\":"); if (!sub) { code = SOL_COAP_RSPCODE_BAD_REQUEST; goto done; } value = !memcmp(sub + strlen("state\":"), "true", sizeof("true") - 1); SOL_INF("Changing light state to %s", value ? "on" : "off"); set_scrolllock_led(value); done: resp = sol_coap_packet_new(req); if (!resp) { SOL_WRN("Could not build response packet"); return -1; } sol_coap_header_set_type(resp, SOL_COAP_TYPE_ACK); sol_coap_header_set_code(resp, code); return sol_coap_send_packet(server, resp, cliaddr); }
static int light_method_get(const struct sol_coap_resource *resource, struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr, void *data) { struct sol_coap_server *server = (void *)data; struct sol_coap_packet *resp; uint8_t *payload; uint16_t len; resp = sol_coap_packet_new(req); if (!resp) { SOL_WRN("Could not build response packet"); return -1; } sol_coap_header_set_type(resp, SOL_COAP_TYPE_ACK); sol_coap_header_set_code(resp, SOL_COAP_RSPCODE_CONTENT); sol_coap_packet_get_payload(resp, &payload, &len); len = light_resource_to_rep(resource, get_scrolllock_led(), (char *)payload, len); sol_coap_packet_set_payload_used(resp, len); return sol_coap_send_packet(server, resp, cliaddr); }
static bool update_light(void *data) { struct light_context *context = data; struct sol_coap_server *server = context->server; struct sol_coap_resource *resource = context->resource; struct sol_coap_packet *pkt; uint8_t *payload; uint16_t len; SOL_INF("Emitting notification"); pkt = sol_coap_packet_notification_new(server, resource); SOL_NULL_CHECK(pkt, false); sol_coap_header_set_code(pkt, SOL_COAP_RSPCODE_CONTENT); sol_coap_packet_get_payload(pkt, &payload, &len); len = light_resource_to_rep(resource, get_scrolllock_led(), (char *)payload, len); sol_coap_packet_set_payload_used(pkt, len); return !sol_coap_packet_send_notification(server, resource, pkt); }
static int bootstrap_request(void *data, struct sol_coap_server *coap, const struct sol_coap_resource *resource, struct sol_coap_packet *req, const struct sol_network_link_addr *cliaddr) { struct sol_lwm2m_bootstrap_client_info *bs_cinfo; struct sol_lwm2m_bootstrap_server *server = data; struct sol_coap_packet *response; struct sol_str_slice client_name = SOL_STR_SLICE_EMPTY; int r; size_t i; bool know_client = false; SOL_DBG("Client Bootstrap Request received"); response = sol_coap_packet_new(req); SOL_NULL_CHECK(response, -ENOMEM); r = extract_bootstrap_client_info(req, &client_name); SOL_INT_CHECK_GOTO(r, < 0, err_exit); for (i = 0; server->known_clients[i]; i++) { if (sol_str_slice_str_eq(client_name, server->known_clients[i])) know_client = true; } if (!know_client) { SOL_WRN("Client %.*s bootstrap request received, but this Bootstrap Server" " doesn't have Bootstrap Information for this client.", SOL_STR_SLICE_PRINT(client_name)); goto err_exit; } r = new_bootstrap_client_info(&bs_cinfo, cliaddr, client_name); SOL_INT_CHECK_GOTO(r, < 0, err_exit); r = sol_ptr_vector_append(&server->clients, bs_cinfo); SOL_INT_CHECK_GOTO(r, < 0, err_exit_del_client); r = sol_coap_header_set_code(response, SOL_COAP_RESPONSE_CODE_CHANGED); SOL_INT_CHECK_GOTO(r, < 0, err_exit_del_client_list); SOL_DBG("Client %s bootstrap request received." " Bootstrap Process will start now.", bs_cinfo->name); r = sol_coap_send_packet(coap, response, cliaddr); dispatch_bootstrap_event_to_server(server, bs_cinfo); return r; err_exit_del_client_list: sol_ptr_vector_remove(&server->clients, bs_cinfo); err_exit_del_client: bootstrap_client_info_del(bs_cinfo); err_exit: sol_coap_header_set_code(response, SOL_COAP_RESPONSE_CODE_BAD_REQUEST); sol_coap_send_packet(coap, response, cliaddr); return r; }