static void test_map(void) { { /* serialization */ TEST_ASSERT(cbor_serialize_map(&stream, 2)); TEST_ASSERT(cbor_serialize_int(&stream, 1)); TEST_ASSERT(cbor_serialize_byte_string(&stream, "1")); TEST_ASSERT(cbor_serialize_int(&stream, 2)); TEST_ASSERT(cbor_serialize_byte_string(&stream, "2")); unsigned char data[] = {0xa2, 0x01, 0x41, 0x31, /* kv-pair 1 */ 0x02, 0x41, 0x32, /* kv-pair 2 */ }; CBOR_CHECK_SERIALIZED(stream, data, sizeof(data)); /* deserialization */ size_t map_length; size_t offset = cbor_deserialize_map(&stream, 0, &map_length); TEST_ASSERT_EQUAL_INT(2, map_length); int key; char value[8]; offset += cbor_deserialize_int(&stream, offset, &key); TEST_ASSERT_EQUAL_INT(1, key); offset += cbor_deserialize_byte_string(&stream, offset, value, sizeof(value)); TEST_ASSERT_EQUAL_STRING("1", &(value[0])); offset += cbor_deserialize_int(&stream, offset, &key); TEST_ASSERT_EQUAL_INT(2, key); offset += cbor_deserialize_byte_string(&stream, offset, value, sizeof(value)); TEST_ASSERT_EQUAL_STRING("2", &(value[0])); TEST_ASSERT_EQUAL_INT(sizeof(data), offset); } }
int handleConfig(coap_rw_buffer_t* scratch, const coap_packet_t* inpkt, coap_packet_t* outpkt, uint8_t id_hi, uint8_t id_lo) { (void)scratch; (void)outpkt; (void)id_hi; (void)id_lo; coap_buffer_t payload = inpkt->payload; unsigned char* data = (unsigned char*) payload.p; // create cbor stream cbor_stream_t stream = {data, sizeof(data), 0}; size_t map_length; // read out length of the map size_t offset = cbor_deserialize_map(&stream, 0, &map_length); int key1, key2, key3; char medtemp[8], hightemp[8], windowsize[8]; // read key 1 offset += cbor_deserialize_int(&stream, offset, &key1); // read medtemp offset += cbor_deserialize_byte_string(&stream, offset, medtemp, sizeof(medtemp)); // read key 2 offset += cbor_deserialize_int(&stream, offset, &key2); // read hightemp offset += cbor_deserialize_byte_string(&stream, offset, hightemp, sizeof(hightemp)); // read key 3 offset += cbor_deserialize_int(&stream, offset, &key3); // read windowsize offset += cbor_deserialize_byte_string(&stream, offset, windowsize, sizeof(windowsize)); cbor_destroy(&stream); puts("NEW CONFIGURATION RECEIVED"); YELLOW_TEMP = atoi(medtemp); RED_TEMP = atoi(hightemp); printf("YELLOW_TEMP set to: %d\n", YELLOW_TEMP); printf("RED_TEMP set to: %d\n", RED_TEMP); if (strcmp(windowsize, "-") != 0) { printf("Set windowsize to: %d\n", atoi(windowsize)); setWindowSize(atoi(windowsize)); } return 0; }
static void test_map_invalid(void) { { /* check writing to stream that is not large enough */ cbor_stream_t stream; cbor_init(&stream, 0, 0); TEST_ASSERT_EQUAL_INT(0, cbor_serialize_map(&stream, 1)); cbor_destroy(&stream); } { /* check reading from stream that contains other type of data */ unsigned char data[] = {0x40}; /* empty string encoded in CBOR */ cbor_stream_t stream = {data, 1, 1}; size_t map_length; TEST_ASSERT_EQUAL_INT(0, cbor_deserialize_map(&stream, 0, &map_length)); } }