static void test_coap_find_options(void) { uint8_t pdu[] = { 0x55, 0xA5, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0x00, 0xC1, 0x00, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0x00 }; struct sol_str_slice options[16]; struct sol_coap_packet *pkt; struct sol_buffer *buf; int count = 16; size_t offset; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset)); ASSERT(!sol_buffer_remove_data(buf, 0, offset)); ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu))); ASSERT(!coap_packet_parse(pkt)); count = sol_coap_find_options(pkt, SOL_COAP_OPTION_CONTENT_FORMAT, options, count); ASSERT_INT_EQ(count, 1); ASSERT_INT_EQ(options[0].len, 1); ASSERT_INT_EQ((uint8_t)options[0].data[0], 0); count = sol_coap_find_options(pkt, SOL_COAP_OPTION_IF_MATCH, options, count); ASSERT_INT_EQ(count, 0); sol_coap_packet_unref(pkt); }
static void test_coap_find_options(void) { uint8_t pdu[] = { 0x55, 0xA5, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0x00, 0xC1, 0x00, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0x00 }; struct sol_coap_packet *pkt; struct sol_str_slice options[16]; int count = 16; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); memcpy(pkt->buf, pdu, sizeof(pdu)); pkt->payload.size = sizeof(pdu); ASSERT(!coap_packet_parse(pkt)); count = sol_coap_find_options(pkt, SOL_COAP_OPTION_CONTENT_FORMAT, options, count); ASSERT_INT_EQ(count, 1); ASSERT_INT_EQ(options[0].len, 1); ASSERT_INT_EQ((uint8_t)options[0].data[0], 0); count = sol_coap_find_options(pkt, SOL_COAP_OPTION_IF_MATCH, options, count); ASSERT_INT_EQ(count, 0); sol_coap_packet_unref(pkt); }
static void test_coap_parse_simple_pdu(void) { uint8_t pdu[] = { 0x55, 0xA5, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0x00, 0xc1, 0x00, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0x00 }; struct sol_str_slice options[16]; struct sol_coap_packet *pkt; struct sol_buffer *buf; uint8_t *token; int count = 16; size_t offset; uint8_t tkl, code, ver, type; uint16_t id; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset)); ASSERT(!sol_buffer_remove_data(buf, 0, offset)); ASSERT(!sol_buffer_insert_bytes(buf, 0, pdu, sizeof(pdu))); ASSERT(!coap_packet_parse(pkt)); sol_coap_header_get_version(pkt, &ver); sol_coap_header_get_type(pkt, &type); ASSERT_INT_EQ(ver, 1); ASSERT_INT_EQ(type, SOL_COAP_MESSAGE_TYPE_NON_CON); token = sol_coap_header_get_token(pkt, &tkl); ASSERT(token); ASSERT_INT_EQ(tkl, 5); ASSERT(!strcmp((char *)token, "token")); sol_coap_header_get_code(pkt, &code); sol_coap_header_get_id(pkt, &id); ASSERT_INT_EQ(code, SOL_COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED); ASSERT_INT_EQ(id, 0x1234); count = sol_coap_find_options(pkt, SOL_COAP_OPTION_CONTENT_FORMAT, options, count); ASSERT_INT_EQ(count, 1); ASSERT_INT_EQ(options[0].len, 1); ASSERT_INT_EQ((uint8_t)options[0].data[0], 0); /* Not existent. */ count = sol_coap_find_options(pkt, SOL_COAP_OPTION_ETAG, options, count); ASSERT_INT_EQ(count, 0); ASSERT(!sol_coap_packet_get_payload(pkt, &buf, &offset)); ASSERT_INT_EQ(offset + sizeof("payload"), buf->used); ASSERT(!strcmp((char *)buf->data + offset, "payload")); sol_coap_packet_unref(pkt); }
static void test_coap_parse_simple_pdu(void) { uint8_t pdu[] = { 0x55, 0xA5, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0x00, 0xc1, 0x00, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0x00 }; struct sol_coap_packet *pkt; struct sol_str_slice options[16]; uint8_t *payload, *token; uint16_t len; int count = 16; uint8_t tkl; pkt = sol_coap_packet_new(NULL); ASSERT(pkt); memcpy(pkt->buf, pdu, sizeof(pdu)); pkt->payload.size = sizeof(pdu); ASSERT(!coap_packet_parse(pkt)); ASSERT_INT_EQ(sol_coap_header_get_ver(pkt), 1); ASSERT_INT_EQ(sol_coap_header_get_type(pkt), SOL_COAP_TYPE_NONCON); token = sol_coap_header_get_token(pkt, &tkl); ASSERT(token); ASSERT_INT_EQ(tkl, 5); ASSERT(!strcmp((char *)token, "token")); ASSERT_INT_EQ(sol_coap_header_get_code(pkt), SOL_COAP_RSPCODE_PROXYING_NOT_SUPPORTED); ASSERT_INT_EQ(sol_coap_header_get_id(pkt), 0x1234); count = sol_coap_find_options(pkt, SOL_COAP_OPTION_CONTENT_FORMAT, options, count); ASSERT_INT_EQ(count, 1); ASSERT_INT_EQ(options[0].len, 1); ASSERT_INT_EQ((uint8_t)options[0].data[0], 0); /* Not existent. */ count = sol_coap_find_options(pkt, SOL_COAP_OPTION_ETAG, options, count); ASSERT_INT_EQ(count, 0); ASSERT(!sol_coap_packet_get_payload(pkt, &payload, &len)); ASSERT_INT_EQ(len, sizeof("payload")); ASSERT(!strcmp((char *)payload, "payload")); sol_coap_packet_unref(pkt); }
static int extract_bootstrap_client_info(struct sol_coap_packet *req, struct sol_str_slice *client_name) { struct sol_str_slice query; int r; r = sol_coap_find_options(req, SOL_COAP_OPTION_URI_QUERY, &query, LWM2M_BOOTSTRAP_QUERY_PARAMS); SOL_INT_CHECK(r, < 0, r); struct sol_str_slice key, value; const char *sep; SOL_DBG("Query:%.*s", SOL_STR_SLICE_PRINT(query)); sep = memchr(query.data, '=', query.len); if (!sep) { SOL_WRN("Could not find the separator '=' at: %.*s", SOL_STR_SLICE_PRINT(query)); return -EINVAL; } key.data = query.data; key.len = sep - query.data; value.data = sep + 1; value.len = query.len - key.len - 1; if (sol_str_slice_str_eq(key, "ep")) { //Required info *client_name = value; } else { SOL_WRN("The client did not provide its name!"); return -EINVAL; } return 0; }