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: EthernetInterface-1 MaxbotixDriver Presence HTU21D_TEMP_HUMID_SENSOR_SAMPLE Resources SHARPIR mbed-rtos mbed-src WDT_K64F nsdl_lib
Fork of Trenton_Switch_LPC1768_ETH by
nsdl_support.cpp
00001 // NSDL library support functions 00002 00003 #include "mbed.h" 00004 #include "nsdl_support.h" 00005 #include "EthernetInterface.h" 00006 #include "node_cfg.h" 00007 00008 00009 #define COAP_UDP_PORT 5683 00010 extern uint8_t NSP_address_bytes[]; 00011 //static ns_address_t app_dest; 00012 00013 extern UDPSocket server; 00014 extern Endpoint nsp; 00015 00016 #define MEM_VALID(x) \ 00017 int s##x=0;\ 00018 int *h##x = new int [1];\ 00019 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__);\ 00020 if (h##x > &s##x)\ 00021 printf("collision\n");\ 00022 else\ 00023 delete [] h##x;\ 00024 __nop() 00025 00026 extern char endpoint_name[24]; 00027 extern uint8_t ep_type[]; 00028 extern uint8_t lifetime_ptr[]; 00029 char null_ep_name[] = ""; 00030 uint8_t null_ep_type[] = ""; 00031 uint8_t null_lifetime_ptr[] = ""; 00032 bool nsdl_reg_update_needed = false; 00033 00034 extern int8_t coap_udp_socket; 00035 00036 void *nsdl_alloc(uint16_t size) 00037 { 00038 void *buf = malloc(size); 00039 return buf; 00040 } 00041 00042 void nsdl_free(void* ptr_to_free) 00043 { 00044 free(ptr_to_free); 00045 } 00046 00047 /* 00048 * Create a static resoure 00049 * Only get is allowed 00050 */ 00051 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) 00052 { 00053 resource_structure->access = SN_GRS_GET_ALLOWED; 00054 resource_structure->mode = SN_GRS_STATIC; 00055 resource_structure->pathlen = pt_len; 00056 resource_structure->path = pt; 00057 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; 00058 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; 00059 resource_structure->resource = rsc; 00060 resource_structure->resourcelen = rsc_len; 00061 sn_nsdl_create_resource(resource_structure); 00062 } 00063 00064 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) 00065 { 00066 resource_structure->access = (sn_grs_resource_acl_e)access_right; 00067 resource_structure->resource = 0; 00068 resource_structure->resourcelen = 0; 00069 resource_structure->sn_grs_dyn_res_callback = callback_ptr; 00070 resource_structure->mode = SN_GRS_DYNAMIC; 00071 resource_structure->pathlen = pt_len; 00072 resource_structure->path = pt; 00073 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; 00074 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; 00075 resource_structure->resource_parameters_ptr->observable = is_observable; 00076 sn_nsdl_create_resource(resource_structure); 00077 } 00078 00079 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) 00080 { 00081 if (NULL == endpoint_structure) 00082 { 00083 endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s)); 00084 } 00085 if (endpoint_structure) 00086 { 00087 memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s)); 00088 endpoint_structure->endpoint_name_ptr = name; 00089 endpoint_structure->endpoint_name_len = strlen((char*)name); 00090 endpoint_structure->type_ptr = typename_ptr; 00091 endpoint_structure->type_len = strlen((char*)typename_ptr); 00092 endpoint_structure->lifetime_ptr = lifetime_ptr; 00093 endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr); 00094 } 00095 return endpoint_structure; 00096 } 00097 00098 void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure){ 00099 if (*endpoint_structure) 00100 { 00101 nsdl_free(*endpoint_structure); 00102 *endpoint_structure = NULL; 00103 } 00104 } 00105 00106 static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr) { 00107 //UDP 00108 printf("TX callback!\n\rSending %d bytes\r\n", data_len); 00109 if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len) 00110 printf("sending failed\n\r"); 00111 return 1; 00112 } 00113 00114 static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr){ 00115 printf("RX callback!\r\n"); 00116 printf("msg_code: %d \r\n", coap_packet_ptr->msg_code); 00117 printf("Payload length: %d bytes\r\n", coap_packet_ptr->payload_len); 00118 int i; 00119 printf("Payload:'"); 00120 for (i=0; i < coap_packet_ptr->payload_len; i++) 00121 printf("%c", *(coap_packet_ptr->payload_ptr + i)); 00122 printf("' \r\n"); 00123 if (coap_packet_ptr->options_list_ptr && coap_packet_ptr->options_list_ptr->location_path_ptr) 00124 { 00125 printf("Location: /"); 00126 int i; 00127 for (i=0; i < coap_packet_ptr->options_list_ptr->location_path_len; i++) printf("%c", (char)(coap_packet_ptr->options_list_ptr->location_path_ptr[i])); 00128 printf(" \r\n"); 00129 } 00130 //sn_coap_packet_debug(coap_packet_ptr); 00131 return 0; 00132 } 00133 00134 00135 void NSP_registration( ) { 00136 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; 00137 00138 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); 00139 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) { 00140 printf("NSP re-registering failed\r\n"); 00141 } else { 00142 printf("NSP re-registering OK\r\n"); 00143 sn_nsdl_update_registration(endpoint_ptr); 00144 } 00145 nsdl_clean_register_endpoint(&endpoint_ptr); 00146 } 00147 00148 00149 void nsdl_init(){ 00150 //uint8_t nsp_addr[4]; 00151 sn_nsdl_mem_s memory_cbs; 00152 00153 /* Initialize libNsdl */ 00154 memory_cbs.sn_nsdl_alloc = &nsdl_alloc; 00155 memory_cbs.sn_nsdl_free = &nsdl_free; 00156 if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) 00157 printf("libNsdl init failed\r\n"); 00158 else 00159 printf("libNsdl init done\r\n"); 00160 server.init(); 00161 server.bind(COAP_UDP_PORT); 00162 nsp.set_address(NSP_IP_ADDRESS, COAP_UDP_PORT); 00163 /* Set nsp address for library */ 00164 //set_NSP_address(NSP_address_bytes, COAP_UDP_PORT, SN_NSDL_ADDRESS_TYPE_IPV6); 00165 set_NSP_address(NSP_address_bytes, COAP_UDP_PORT, SN_NSDL_ADDRESS_TYPE_IPV4); 00166 } 00167 00168 void nsdl_reg_update() 00169 { 00170 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; 00171 // endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); 00172 // reg update should be invoked with null parameters if nothing is changing 00173 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)null_ep_name, null_ep_type, null_lifetime_ptr); 00174 if(sn_nsdl_update_registration(endpoint_ptr) != 0) 00175 printf("NSP re-registering failed\r\n"); 00176 else 00177 printf("NSP re-registering OK\r\n"); 00178 nsdl_clean_register_endpoint(&endpoint_ptr); 00179 } 00180 00181 void nsdl_reg_update_timeout() 00182 { 00183 nsdl_reg_update_needed = true; 00184 } 00185 00186 void nsdl_event_loop() 00187 { 00188 00189 sn_nsdl_addr_s received_packet_address; 00190 uint8_t received_address[4]; 00191 00192 memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); 00193 received_packet_address.addr_ptr = received_address; 00194 00195 while(1) 00196 { 00197 // pc.printf("checking reg timeout\r\n"); 00198 if (nsdl_reg_update_needed) 00199 { 00200 nsdl_reg_update_needed = false; 00201 nsdl_reg_update(); 00202 } 00203 } 00204 }
Generated on Wed Jul 13 2022 06:03:53 by
1.7.2
