Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: cc3000_hostdriver_mbedsocket mbed nsdl_lib
nsdl_support.cpp
00001 // NSDL library support functions 00002 00003 #include "mbed.h" 00004 #include "nsdl_support.h" 00005 #include "mbed.h" 00006 #include "cc3000.h" 00007 #include "UDPSocket.h" 00008 #include "Endpoint.h" 00009 00010 /* __heapvalid((__heapprt)fprintf,stdout, 1);\*/ 00011 #define MEM_VALID(x) \ 00012 int s##x=0;\ 00013 int *h##x = new int [1];\ 00014 std::printf("[stack]0x%08x\t[heap]0x%08x\t[memory avail]%d bytes \tLine: %d %s\r\n", &s##x, h##x, &s##x-h##x, __LINE__, __FILE__);\ 00015 if (h##x > &s##x)\ 00016 printf("collision\n");\ 00017 else\ 00018 delete [] h##x;\ 00019 __nop() 00020 00021 extern Serial pc; 00022 extern Endpoint nsp; 00023 extern UDPSocket server; 00024 extern char endpoint_name[24]; 00025 extern uint8_t ep_type[]; 00026 extern uint8_t lifetime_ptr[]; 00027 char null_ep_name[] = ""; 00028 uint8_t null_ep_type[] = ""; 00029 uint8_t null_lifetime_ptr[] = "60"; 00030 bool nsdl_reg_update_needed = false; 00031 00032 /* The number of seconds between NSP registration messages */ 00033 #define RD_UPDATE_PERIOD 30 00034 00035 void *nsdl_alloc(uint16_t size) 00036 { 00037 void *buf = malloc(size); 00038 // printf("alloc\r\n"); 00039 // MEM_VALID(0); 00040 return buf; 00041 } 00042 00043 void nsdl_free(void* ptr_to_free) 00044 { 00045 free(ptr_to_free); 00046 // printf("de-alloc\r\n"); 00047 // MEM_VALID(0); 00048 } 00049 00050 /* 00051 * Create a static resoure 00052 * Only get is allowed 00053 */ 00054 void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len) 00055 { 00056 resource_structure->access = SN_GRS_GET_ALLOWED; 00057 resource_structure->mode = SN_GRS_STATIC; 00058 resource_structure->pathlen = pt_len; 00059 resource_structure->path = pt; 00060 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; 00061 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; 00062 resource_structure->resource = rsc; 00063 resource_structure->resourcelen = rsc_len; 00064 sn_nsdl_create_resource(resource_structure); 00065 } 00066 00067 void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right) 00068 { 00069 resource_structure->access = (sn_grs_resource_acl_e)access_right; 00070 resource_structure->resource = 0; 00071 resource_structure->resourcelen = 0; 00072 resource_structure->sn_grs_dyn_res_callback = callback_ptr; 00073 resource_structure->mode = SN_GRS_DYNAMIC; 00074 resource_structure->pathlen = pt_len; 00075 resource_structure->path = pt; 00076 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; 00077 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; 00078 resource_structure->resource_parameters_ptr->observable = is_observable; 00079 sn_nsdl_create_resource(resource_structure); 00080 } 00081 00082 sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t* name, uint8_t* typename_ptr, uint8_t *lifetime_ptr) 00083 { 00084 if (NULL == endpoint_structure) 00085 { 00086 endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s)); 00087 } 00088 if (endpoint_structure) 00089 { 00090 memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s)); 00091 endpoint_structure->endpoint_name_ptr = name; 00092 endpoint_structure->endpoint_name_len = strlen((char*)name); 00093 endpoint_structure->type_ptr = typename_ptr; 00094 endpoint_structure->type_len = strlen((char*)typename_ptr); 00095 endpoint_structure->lifetime_ptr = lifetime_ptr; 00096 endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr); 00097 } 00098 return endpoint_structure; 00099 } 00100 00101 void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure) 00102 { 00103 if (*endpoint_structure) 00104 { 00105 nsdl_free(*endpoint_structure); 00106 *endpoint_structure = NULL; 00107 } 00108 } 00109 00110 static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr) 00111 { 00112 pc.printf("TX callback!\n\rSending %d bytes\r\n", data_len); 00113 00114 if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len) 00115 pc.printf("sending failed\n\r"); 00116 00117 return 1; 00118 } 00119 00120 static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) 00121 { 00122 pc.printf("RX callback!\r\n"); 00123 pc.printf("msg_code: %d \r\n", coap_packet_ptr->msg_code); 00124 pc.printf("Payload length: %d bytes\r\n", coap_packet_ptr->payload_len); 00125 int i; 00126 pc.printf("Payload:'"); 00127 for (i=0; i < coap_packet_ptr->payload_len; i++) 00128 pc.printf("%c", *(coap_packet_ptr->payload_ptr + i)); 00129 pc.printf("' \r\n"); 00130 if (coap_packet_ptr->options_list_ptr && coap_packet_ptr->options_list_ptr->location_path_ptr) 00131 { 00132 pc.printf("Location: /"); 00133 int i; 00134 for (i=0; i < coap_packet_ptr->options_list_ptr->location_path_len; i++) pc.printf("%c", (char)(coap_packet_ptr->options_list_ptr->location_path_ptr[i])); 00135 pc.printf(" \r\n"); 00136 } 00137 //sn_coap_packet_debug(coap_packet_ptr); 00138 return 0; 00139 } 00140 00141 void nsdl_init() 00142 { 00143 uint8_t nsp_addr[4]; 00144 sn_nsdl_mem_s memory_cbs; 00145 00146 /* Initialize libNsdl */ 00147 memory_cbs.sn_nsdl_alloc = &nsdl_alloc; 00148 memory_cbs.sn_nsdl_free = &nsdl_free; 00149 if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) 00150 pc.printf("libNsdl init failed\r\n"); 00151 else 00152 pc.printf("libNsdl init done\r\n"); 00153 00154 /* Set nsp address for library */ 00155 set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4); 00156 } 00157 00158 void nsdl_reg_update() 00159 { 00160 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; 00161 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)null_ep_name, null_ep_type, null_lifetime_ptr); 00162 if(sn_nsdl_update_registration(endpoint_ptr) != 0) 00163 pc.printf("NSP registration update failed\r\n"); 00164 else 00165 pc.printf("NSP registration update OK\r\n"); 00166 nsdl_clean_register_endpoint(&endpoint_ptr); 00167 } 00168 00169 void nsdl_reg_update_timeout() 00170 { 00171 nsdl_reg_update_needed = true; 00172 } 00173 00174 Ticker regUpdateTicker; 00175 00176 void nsdl_event_loop() 00177 { 00178 regUpdateTicker.attach(&nsdl_reg_update_timeout, RD_UPDATE_PERIOD); 00179 00180 sn_nsdl_addr_s received_packet_address; 00181 uint8_t received_address[4]; 00182 char buffer[1024]; 00183 Endpoint from; 00184 00185 memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); 00186 received_packet_address.addr_ptr = received_address; 00187 00188 while(1) 00189 { 00190 int n = server.receiveFrom(from, buffer, sizeof(buffer)); 00191 if (n < 0) 00192 { 00193 pc.printf("Socket error\n\r"); 00194 } 00195 else if (n > 0) 00196 { 00197 pc.printf("Received %d bytes\r\n", n); 00198 sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address); 00199 } 00200 if (nsdl_reg_update_needed) 00201 { 00202 nsdl_reg_update_needed = false; 00203 nsdl_reg_update(); 00204 } 00205 } 00206 } 00207 00208
Generated on Thu Jul 14 2022 00:07:15 by
1.7.2