bool test_sn_coap_parser_release_allocated_coap_msg_mem() { struct coap_s* coap = (struct coap_s*)malloc(sizeof(struct coap_s)); coap->sn_coap_protocol_malloc = myMalloc; coap->sn_coap_protocol_free = myFree; retCounter = 99; sn_coap_parser_release_allocated_coap_msg_mem( NULL, NULL ); sn_coap_hdr_s* ptr = (sn_coap_hdr_s*)myMalloc(sizeof(sn_coap_hdr_s)); ptr->uri_path_ptr = (uint8_t*)malloc(sizeof(uint8_t)); ptr->token_ptr = (uint8_t*)malloc(sizeof(uint8_t)); //ptr->payload_ptr = (uint8_t*)malloc(sizeof(uint8_t)); ptr->options_list_ptr = (sn_coap_options_list_s*)myMalloc(sizeof(sn_coap_options_list_s)); ptr->options_list_ptr->max_age = 1; ptr->options_list_ptr->proxy_uri_ptr = (uint8_t*)malloc(sizeof(uint8_t)); ptr->options_list_ptr->etag_ptr = (uint8_t*)malloc(sizeof(uint8_t)); ptr->options_list_ptr->uri_host_ptr = (uint8_t*)malloc(sizeof(uint8_t)); ptr->options_list_ptr->location_path_ptr = (uint8_t*)malloc(sizeof(uint8_t)); ptr->options_list_ptr->uri_port = 8; ptr->options_list_ptr->location_query_ptr = (uint8_t*)malloc(sizeof(uint8_t)); ptr->options_list_ptr->observe = 0; ptr->options_list_ptr->uri_query_ptr = (uint8_t*)malloc(sizeof(uint8_t)); sn_coap_parser_release_allocated_coap_msg_mem( coap, ptr ); free(coap); return true; //this is a memory leak check, so that will pass/fail }
/** * \fn test_libcoap_parser_parse_message_with_multiple_options(void) * * \brief call coap protocol parser with message with multiple options (ACK - changed - token - max age - URI path - URI host) * */ void test_libcoap_parser_parse_message_with_multiple_options(void) { coap_address.addr_ptr = address; sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, sizeof(coap_message_multiple_options), coap_message_multiple_options, NULL); TEST_ASSERT_NOT_NULL(coap_header_ptr); TEST_ASSERT_NOT_NULL(coap_header_ptr->options_list_ptr); TEST_ASSERT_EQUAL(COAP_MSG_TYPE_ACKNOWLEDGEMENT, coap_header_ptr->msg_type); TEST_ASSERT_EQUAL(COAP_MSG_CODE_RESPONSE_CHANGED, coap_header_ptr->msg_code); TEST_ASSERT_EQUAL(0x1234, coap_header_ptr->msg_id); TEST_ASSERT_EQUAL(sizeof(option_short), coap_header_ptr->options_list_ptr->uri_host_len); TEST_ASSERT_EQUAL_INT8_ARRAY(option_short, coap_header_ptr->options_list_ptr->uri_host_ptr, sizeof(option_short)); TEST_ASSERT_EQUAL(sizeof(option_path), coap_header_ptr->uri_path_len); TEST_ASSERT_EQUAL_INT8_ARRAY(option_path, coap_header_ptr->uri_path_ptr, sizeof(option_path)); TEST_ASSERT_EQUAL(option_short_value, coap_header_ptr->options_list_ptr->max_age); TEST_ASSERT_EQUAL(sizeof(option_short), coap_header_ptr->token_len); TEST_ASSERT_EQUAL_INT8_ARRAY(option_short, coap_header_ptr->token_ptr, sizeof(option_short)); sn_coap_parser_release_allocated_coap_msg_mem(handle, coap_header_ptr); }
/* Only GET method allowed */ static uint8_t door_trip_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) { sn_coap_hdr_s *coap_res_ptr = 0; snprintf(door_trip_val,2,"%d" ,current_door_trip_value); pc.printf("door_trip callback\r\n"); pc.printf("door_trip: %s\r\n", door_trip_val); if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) { coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); coap_res_ptr->payload_len = strlen(door_trip_val); coap_res_ptr->payload_ptr = (uint8_t*)door_trip_val; coap_res_ptr->content_type_ptr = &content_type; coap_res_ptr->content_type_len = sizeof(content_type); if(received_coap_ptr->token_ptr){ pc.printf(" Token included\r\n"); if(obs_token_ptr) { free(obs_token_ptr); obs_token_ptr = 0; } obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); if(obs_token_ptr){ memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); obs_token_len = received_coap_ptr->token_len; } } if(received_coap_ptr->options_list_ptr->observe){ coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); coap_res_ptr->options_list_ptr->observe_ptr = &obs_number; coap_res_ptr->options_list_ptr->observe_len = 1; obs_number++; } pc.printf(" Send observation %d... \r\n", obs_number); sn_nsdl_send_coap_message(address, coap_res_ptr); nsdl_free(coap_res_ptr->options_list_ptr); coap_res_ptr->options_list_ptr = NULL; coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory } sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); return 0; }
bool test_sn_coap_parser() { if( sn_coap_parser_alloc_message(NULL) ) { return false; } if( sn_coap_parser_alloc_options(NULL, NULL) ) { return false; } retCounter = 0; bool ret = true; // use zero-initialized buffer for tests uint8_t* ptr = (uint8_t*)calloc(20, 1); assert(ptr); sn_coap_hdr_s * hdr = sn_coap_parser(NULL, 8, ptr, NULL); if( hdr != NULL ){ free(hdr); ret = false; } if( ret ){ struct coap_s* coap = (struct coap_s*)malloc(sizeof(struct coap_s)); coap->sn_coap_protocol_malloc = myMalloc; coap->sn_coap_protocol_free = myFree; retCounter = 0; coap_version_e* ver = (coap_version_e*)malloc(sizeof(coap_version_e)); hdr = sn_coap_parser(coap, 8, ptr, ver); if( hdr != NULL ){ free(hdr); ret = false; } if( ret ){ retCounter = 1; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ free(hdr); ret = false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); } free(ver); free(coap); } free(ptr); return ret; }
/** * \fn test_libcoap_parser_parse_message_without_options(void) * * \brief call coap protocol parser with message without options (ACK - changed) * */ void test_libcoap_parser_parse_message_without_options(void) { coap_address.addr_ptr = address; sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, sizeof(coap_message_no_options), coap_message_no_options, NULL); TEST_ASSERT_NOT_NULL(coap_header_ptr); TEST_ASSERT_EQUAL(COAP_MSG_TYPE_ACKNOWLEDGEMENT, coap_header_ptr->msg_type); TEST_ASSERT_EQUAL(COAP_MSG_CODE_RESPONSE_CHANGED, coap_header_ptr->msg_code); TEST_ASSERT_EQUAL(0x1234, coap_header_ptr->msg_id); sn_coap_parser_release_allocated_coap_msg_mem(handle, coap_header_ptr); }
/** * \fn test_libcoap_parser_parse_message_with_payload_and_token(void) * * \brief call coap protocol parser with message with token option and small payload (ACK - changed) * */ void test_libcoap_parser_parse_message_with_payload_and_token(void) { coap_address.addr_ptr = address; sn_coap_hdr_s *coap_header_ptr = sn_coap_protocol_parse(handle, &coap_address, sizeof(coap_message_token_payload), coap_message_token_payload, NULL); TEST_ASSERT_NOT_NULL(coap_header_ptr); TEST_ASSERT_EQUAL(COAP_MSG_TYPE_ACKNOWLEDGEMENT, coap_header_ptr->msg_type); TEST_ASSERT_EQUAL(COAP_MSG_CODE_RESPONSE_CHANGED, coap_header_ptr->msg_code); TEST_ASSERT_EQUAL(0x1234, coap_header_ptr->msg_id); TEST_ASSERT_EQUAL(3, coap_header_ptr->token_len); TEST_ASSERT_EQUAL_INT8_ARRAY(option_short, coap_header_ptr->token_ptr, sizeof(option_short)); TEST_ASSERT_EQUAL(3, coap_header_ptr->payload_len); TEST_ASSERT_EQUAL_INT8_ARRAY(option_short, coap_header_ptr->payload_ptr, sizeof(option_short)); sn_coap_parser_release_allocated_coap_msg_mem(handle, coap_header_ptr); }
bool test_sn_coap_parser_parsing() { bool ret = true; uint8_t* ptr = (uint8_t*)malloc(33); memset(ptr, 0, 33); struct coap_s* coap = (struct coap_s*)malloc(sizeof(struct coap_s)); coap->sn_coap_protocol_malloc = myMalloc; coap->sn_coap_protocol_free = myFree; coap_version_e* ver = (coap_version_e*)malloc(sizeof(coap_version_e)); ptr[0] = 0x60; ptr[4] = 0x42; ptr[5] = 0x00; ptr[6] = 0x00; retCounter = 4; //TODO: add sn_coap_parser_parsing related stuff ptr[7] = 0x20; ptr[8] = 0x00; sn_coap_hdr_s *hdr = NULL; //this should test parsing without payload marker, but it is not possible // hdr = sn_coap_parser(coap, 9, ptr, ver); // if( hdr != NULL ){ // ret = false; // goto end3; // } // sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[0] = 0x60; ptr[4] = 0x42; ptr[5] = 0x00; ptr[6] = 0x00; ptr[7] = 0x06; ptr[8] = 0x00; ptr[9] = 0x6f; ptr[10] = 0x6d; ptr[11] = 0x61; ptr[12] = 0x69; ptr[13] = 0x6e; ptr[14] = 0xff; ptr[15] = 0x1; retCounter = 4; hdr = sn_coap_parser(coap, 16, ptr, ver); if( hdr == NULL ){ ret = false; goto end3; } sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); end3: free(ver); free(coap); free(ptr); return ret; }
bool test_sn_coap_parser_options_parse_multiple_options() { bool ret = true; uint8_t* ptr = (uint8_t*)malloc(33); memset(ptr, 0, 33); struct coap_s* coap = (struct coap_s*)malloc(sizeof(struct coap_s)); coap->sn_coap_protocol_malloc = myMalloc; coap->sn_coap_protocol_free = myFree; coap_version_e* ver = (coap_version_e*)malloc(sizeof(coap_version_e)); ptr[0] = 0x60; ptr[4] = 0x82; //opt 8 & len 2 ptr[5] = 0x00; ptr[6] = 0x00; ptr[7] = 0x0d; ptr[8] = 0x00; ptr[9] = 0x6f; ptr[10] = 0x6d; ptr[11] = 0x61; ptr[12] = 0x69; ptr[13] = 0x6e; retCounter = 4; sn_coap_hdr_s *hdr = sn_coap_parser(coap, 14, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; goto end2; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[7] = 0x0e; ptr[8] = 0xff; ptr[9] = 0x00; retCounter = 4; hdr = sn_coap_parser(coap, 14, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; goto end2; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[0] = 0x60; ptr[4] = 0x42; ptr[5] = 0x00; ptr[6] = 0x00; ptr[7] = 0x06; ptr[8] = 0x00; ptr[9] = 0x6f; ptr[10] = 0x6d; ptr[11] = 0x61; ptr[12] = 0x69; ptr[13] = 0x6e; retCounter = 4; hdr = sn_coap_parser(coap, 14, ptr, ver); if( hdr == NULL ){ ret = false; goto end2; }else{ sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); } free(ptr); ptr = (uint8_t*)malloc(10); memset(ptr, 0, 10); //this should be checked after bug http://jira.arm.com/browse/IOTCLT-244 is fixed //This is illegal options message, but gets parsed though //this test passes because parsing of data still fails (expected) ptr[0] = 0x60; ptr[4] = 0x82; ptr[5] = 0x00; ptr[6] = 0x00; ptr[7] = 0x06; ptr[8] = 0x00; ptr[9] = 0x6f; retCounter = 4; hdr = sn_coap_parser(coap, 10, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; goto end2; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); end2: free(ver); free(coap); free(ptr); return ret; }
bool test_sn_coap_parser_options_parsing() { bool ret = true; uint8_t* ptr = (uint8_t*)malloc(20); memset(ptr, 0, 20); ptr[0] = 9; struct coap_s* coap = (struct coap_s*)malloc(sizeof(struct coap_s)); coap->sn_coap_protocol_malloc = myMalloc; coap->sn_coap_protocol_free = myFree; retCounter = 1; coap_version_e* ver = (coap_version_e*)malloc(sizeof(coap_version_e)); sn_coap_hdr_s * hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; }else{ if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[0] = 1; retCounter = 1; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; }else{ if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 255; //this point is sufficient to test parsing of payload retCounter = 2; hdr = sn_coap_parser(coap, 6, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; }else{ if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 239; retCounter = 2; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; }else{ if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 254; //15 | 14 retCounter = 2; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; }else{ if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 238; //14 | 14 ptr[6] = 6; ptr[7] = 7; retCounter = 2; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; }else{ if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 221; //13 | 13 ptr[6] = 6; retCounter = 2; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; } else { if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); } } } } } } } free(ver); free(coap); free(ptr); return ret; }
bool test_sn_coap_parser_options_count_needed_memory_multiple_option() { bool ret = true; uint8_t* ptr = (uint8_t*)malloc(65635); memset(ptr, 0, 65635); struct coap_s* coap = (struct coap_s*)malloc(sizeof(struct coap_s)); coap->sn_coap_protocol_malloc = myMalloc; coap->sn_coap_protocol_free = myFree; coap_version_e* ver = (coap_version_e*)malloc(sizeof(coap_version_e)); //Some of these should be tested: //These 6 will test sn_coap_parser_options_count_needed_memory_multiple_option overflows ptr[0] = 1; ptr[5] = 0x4d; //4 | 13 ptr[6] = 254; retCounter = 4; //This should test if (ret_status >= 0) {} sn_coap_hdr_s * hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; goto end; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[0] = 1; ptr[5] = 0x8d; //4 | 13 ptr[6] = 254; retCounter = 4; //This should test if (ret_status >= 0) {} hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; goto end; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[0] = 1; ptr[5] = 0xbd; ptr[6] = 254; retCounter = 4; //This should test if (ret_status >= 0) {} hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; goto end; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[0] = 1; ptr[5] = 0xdd; ptr[6] = 2; ptr[7] = 254; retCounter = 4; //This should test if (ret_status >= 0) {} hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; goto end; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[0] = 1; ptr[5] = 0xdd; ptr[6] = 4; ptr[7] = 254; retCounter = 4; //This should test if (ret_status >= 0) {} hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; goto end; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[0] = 1; ptr[5] = 0xdd; ptr[6] = 7; ptr[7] = 254; retCounter = 4; //This should test if (ret_status >= 0) {} hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; goto end; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[0] = 1; ptr[5] = 0x81; ptr[6] = 0x00; ptr[7] = 0x20; retCounter = 4; //This should test if (ret_status >= 0) {} hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; goto end; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[0] = 1; ptr[5] = 0x81; ptr[6] = 0x00; ptr[7] = 0x00; retCounter = 4; //This should test if (ret_status >= 0) {} hdr = sn_coap_parser(coap, 6, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; goto end; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[0] = 1; ptr[5] = 0x81; ptr[6] = 0x00; ptr[7] = 0x0d; retCounter = 4; //This should test if (ret_status >= 0) {} hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; goto end; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[0] = 1; ptr[5] = 0x81; ptr[6] = 0x00; ptr[7] = 0x0e; retCounter = 4; //This should test if (ret_status >= 0) {} hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; goto end; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[0] = 1; ptr[5] = 0x81; ptr[6] = 0x00; ptr[7] = 0x0f; retCounter = 4; //This should test if (ret_status >= 0) {} hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ ret = false; goto end; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); end: free(ver); free(coap); free(ptr); return ret; }
bool test_sn_coap_parser_options_parsing_switches() { bool ret = true; uint8_t* ptr = (uint8_t*)malloc(20); memset(ptr, 0, 20); struct coap_s* coap = (struct coap_s*)malloc(sizeof(struct coap_s)); coap->sn_coap_protocol_malloc = myMalloc; coap->sn_coap_protocol_free = myFree; coap_version_e* ver = (coap_version_e*)malloc(sizeof(coap_version_e)); //These should be tested: /* COAP_OPTION_IF_MATCH = 1, COAP_OPTION_URI_HOST = 3, COAP_OPTION_ETAG = 4, COAP_OPTION_IF_NONE_MATCH = 5, COAP_OPTION_OBSERVE = 6, COAP_OPTION_URI_PORT = 7, COAP_OPTION_LOCATION_PATH = 8, COAP_OPTION_URI_PATH = 11, COAP_OPTION_CONTENT_FORMAT = 12, COAP_OPTION_MAX_AGE = 14, COAP_OPTION_URI_QUERY = 15, COAP_OPTION_ACCEPT = 17, COAP_OPTION_LOCATION_QUERY = 20, COAP_OPTION_BLOCK2 = 23, COAP_OPTION_BLOCK1 = 27, COAP_OPTION_SIZE2 = 28, COAP_OPTION_PROXY_URI = 35, COAP_OPTION_PROXY_SCHEME = 39, COAP_OPTION_SIZE1 = 60 */ ptr[0] = 1; retCounter = 2; ptr[5] = 17; //1 | 1 (number | length) sn_coap_hdr_s * hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 51; //1 | 3 retCounter = 2; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 48; //3 | 0 retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 51; //3 | 3 retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 51; //3 | 3 retCounter = 4; //overflows, so not valid data hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 68; //4 | 4 retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 68; //4 | 4 retCounter = 4; //This should test if (ret_status >= 0) {} hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 85; //5 | 5 retCounter = 2; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 102; //6 | 6 retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 97; //6 | 1 retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 97; //6 | 1 retCounter = 4; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 119; //7 | 7 retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 113; //7 | 1 retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 113; //7 | 1 retCounter = 4; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 128; //8 | 8 retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 136; //8 | 8 retCounter = 4; //This should test if (ret_status >= 0) {} hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 187; //11 | 11 retCounter = 2; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 187; //11 | 11 retCounter = 3; //This should test if (ret_status >= 0) {} hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 204; //12 | 12 retCounter = 2; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 193; //12 | 1 retCounter = 2; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 193; //12 | 1 retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 216; //13 | 8 ptr[6] = 1; //1 -> 14 retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 209; //13 | 1 ptr[6] = 1; //1 -> 14 retCounter = 2; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 210; //13 | 2 ptr[6] = 1; //1 -> 14 retCounter = 3; hdr = sn_coap_parser(coap, 6, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 208; //13 | 0 ptr[6] = 2; //2 -> 15 ??? retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 209; //13 | 1 ptr[6] = 2; //2 -> 15 ??? retCounter = 5; //This should test if (ret_status >= 0) {} hdr = sn_coap_parser(coap, 7, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 208; //13 | 0 ptr[6] = 4; retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 209; //13 | 1 ptr[6] = 4; retCounter = 5; //This should test if (ret_status >= 0) {} hdr = sn_coap_parser(coap, 7, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 208; //13 | 0 ptr[6] = 7; retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 209; //13 | 1 ptr[6] = 7; retCounter = 5; //This should test if (ret_status >= 0) {} hdr = sn_coap_parser(coap, 7, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 216; //13 | 8 ptr[6] = 10; retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 209; //13 | 1 ptr[6] = 10; retCounter = 2; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 210; //13 | 2 ptr[6] = 10; retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 216; //13 | 8 ptr[6] = 14; retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 209; //13 | 1 ptr[6] = 14; retCounter = 2; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 210; //13 | 2 ptr[6] = 14; retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 208; //13 | 0 ptr[6] = 22; retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 209; //13 | 1 ptr[6] = 22; retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 209; //13 | 1 ptr[6] = 22; retCounter = 4; hdr = sn_coap_parser(coap, 7, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 208; //13 | 0 ptr[6] = 26; retCounter = 2; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 208; //13 | 0 ptr[6] = 47; retCounter = 2; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 216; //13 | 8 ptr[6] = 47; retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 216; //13 | 8 ptr[6] = 47; retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 210; //13 | 2 ptr[6] = 47; retCounter = 4; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 216; //13 | 8 ptr[6] = 15; retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 210; //13 | 2 ptr[6] = 15; retCounter = 4; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 216; //13 | 8 ptr[6] = 15; retCounter = 3; hdr = sn_coap_parser(coap, 8, ptr, ver); if( !hdr || (hdr && hdr->coap_status != COAP_STATUS_PARSER_ERROR_IN_HEADER) ){ return false; } if (hdr) sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); ptr[5] = 209; //13 | 1 ptr[6] = 10; retCounter = 4; hdr = sn_coap_parser(coap, 8, ptr, ver); if( hdr == NULL ){ return false; } sn_coap_parser_release_allocated_coap_msg_mem(coap, hdr); free(ver); free(coap); free(ptr); return ret; }
/* Callback for LWM2M (CoAP REST) operations allowed on the resosurce GET and PUT methods allowed */ static uint8_t LWM2M_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) { sn_coap_hdr_s *coap_res_ptr = 0; if(COAP_MSG_CODE_REQUEST_GET == received_coap_ptr->msg_code){ coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); current_sample = LWM2M_Sensor.read() * (float) 100; sprintf(LWM2M_value_string,"%3.1f", current_sample); pc.printf("LWM2M resource callback\r\n"); pc.printf("LWM2M resource state %s\r\n", LWM2M_value_string); coap_res_ptr->payload_len = strlen(LWM2M_value_string); coap_res_ptr->payload_ptr = (uint8_t*)LWM2M_value_string; coap_res_ptr->content_type_ptr = &LWM2M_content_type; coap_res_ptr->content_type_len = sizeof(LWM2M_content_type); coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s)); if(!coap_res_ptr->options_list_ptr){ pc.printf("cant alloc option list\r\n"); coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover } else{ memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); coap_res_ptr->options_list_ptr->max_age_ptr = &LWM2M_max_age; coap_res_ptr->options_list_ptr->max_age_len = sizeof(LWM2M_max_age); } if(received_coap_ptr->token_ptr){ pc.printf("Token included\r\n"); // replace any existing token if(LWM2M_obs_token_ptr){ free(LWM2M_obs_token_ptr); LWM2M_obs_token_ptr = 0; } //with new token LWM2M_obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); if(LWM2M_obs_token_ptr){ memcpy(LWM2M_obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); LWM2M_obs_token_len = received_coap_ptr->token_len; } } if(received_coap_ptr->options_list_ptr->observe) { // get observe start/stop value from received GET LWM2M_obs_option = * received_coap_ptr->options_list_ptr->observe_ptr; // start or stop based on option value // ref. draft-ietf-core-observe-16 if (START_OBS == LWM2M_obs_option){ coap_res_ptr->options_list_ptr->observe_ptr = &LWM2M_obs_number; coap_res_ptr->options_list_ptr->observe_len = sizeof(LWM2M_obs_number); LWM2M_start_notification(); } else if (STOP_OBS == LWM2M_obs_option){ LWM2M_stop_notification(); } } sn_nsdl_send_coap_message(address, coap_res_ptr); nsdl_free(coap_res_ptr->options_list_ptr); coap_res_ptr->options_list_ptr = NULL; coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory } /* PUT needs to be enabled for the write attributes operation which is empty payload + query options */ else if(COAP_MSG_CODE_REQUEST_PUT == received_coap_ptr->msg_code){ //pc.printf("PUT: %d bytes\r\n", received_coap_ptr->payload_len); if((received_coap_ptr->payload_len > 0) && (received_coap_ptr->payload_len <= 5)){ memcpy(LWM2M_update_string, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len); LWM2M_update_string[received_coap_ptr->payload_len] = '\0'; pc.printf("PUT: %s\r\n", LWM2M_update_string); sscanf( LWM2M_update_string, "%f3.1", ¤t_sample); //update for read-back test, observe will clobber coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); sn_nsdl_send_coap_message(address, coap_res_ptr); } // see if there are query options and scan for write attributes, allow payload and query options // PUT without query ffrom web client reads some query string, wireshark it... if(received_coap_ptr->options_list_ptr->uri_query_ptr != NULL){ LWM2M_query_string_ptr = (char*)nsdl_alloc(received_coap_ptr->options_list_ptr->uri_query_len+1); if (LWM2M_query_string_ptr){ memcpy(LWM2M_query_string_ptr, received_coap_ptr->options_list_ptr->uri_query_ptr, received_coap_ptr->options_list_ptr->uri_query_len); memset(LWM2M_query_string_ptr + received_coap_ptr->options_list_ptr->uri_query_len,'\0',1);//string terminator // diagnostic // pc.printf("query string received: %s\r\n", LWM2M_query_string_ptr); attribute_update = false; // extract query options from string query_option = strtok(LWM2M_query_string_ptr, "&");// split the string num_options = 0; while (query_option != NULL){ strcpy(query_options[num_options++], query_option); // pc.printf("query part: %s\r\n", query_option); query_option = strtok(NULL, "&");// next query option } // pc.printf("setting attributes\r\n"); for (int option = 0; option < num_options; option++) set_notification_attribute(query_options[option]); nsdl_free(LWM2M_query_string_ptr); // if anything was updated, re-initialize the stored notification attributes if (attribute_update){ // initializes and sends an update if observing is on, don't change observing state // allows cancel to turn off observing and updte state without sending a notification LWM2M_notification_init(); coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); // 2.04 } else // if query options are sent but no notification attribute names were found, it's an error coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_BAD_REQUEST);// 4.00 sn_nsdl_send_coap_message(address, coap_res_ptr); } else pc.printf("cant alloc query string\r\n"); } } sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); return 0; }