nsp resources for the mbed nsp lighting endpoint

Dependencies:   nsdl_lib

Dependents:   mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet mbed_nsp_endpoint_nxp

Committer:
ansond
Date:
Wed Sep 17 15:24:11 2014 +0000
Revision:
0:b26c3526ee89
Child:
1:9824f99868a3
initial checkin

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:b26c3526ee89 1 // NSDL library support functions
ansond 0:b26c3526ee89 2
ansond 0:b26c3526ee89 3 #include "mbed.h"
ansond 0:b26c3526ee89 4 #include "nsdl_support.h"
ansond 0:b26c3526ee89 5 #include "ErrorHandler.h"
ansond 0:b26c3526ee89 6
ansond 0:b26c3526ee89 7 extern ErrorHandler *error_handler;
ansond 0:b26c3526ee89 8 #ifdef NETWORK_MUTEX
ansond 0:b26c3526ee89 9 extern Mutex *network_mutex;
ansond 0:b26c3526ee89 10 #endif
ansond 0:b26c3526ee89 11
ansond 0:b26c3526ee89 12 Endpoint nsp;
ansond 0:b26c3526ee89 13 UDPSocket server;
ansond 0:b26c3526ee89 14 char _endpoint_name[PERSONALITY_NAME_LEN+1];
ansond 0:b26c3526ee89 15 uint8_t _ep_type[] = { NSP_NODE_TYPE };
ansond 0:b26c3526ee89 16 uint8_t _lifetime_ptr[] = { NSP_NODE_LIFETIME };
ansond 0:b26c3526ee89 17 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
ansond 0:b26c3526ee89 18
ansond 0:b26c3526ee89 19 #include "MBEDEndpoint.h"
ansond 0:b26c3526ee89 20 extern MBEDEndpoint *endpoint;
ansond 0:b26c3526ee89 21
ansond 0:b26c3526ee89 22 void *nsdl_alloc(uint16_t size) {
ansond 0:b26c3526ee89 23 void *chunk = NULL;
ansond 0:b26c3526ee89 24 if (size > 0) chunk = malloc(size);
ansond 0:b26c3526ee89 25 if (chunk != NULL && size > 0) memset(chunk,0,size);
ansond 0:b26c3526ee89 26 return chunk;
ansond 0:b26c3526ee89 27 }
ansond 0:b26c3526ee89 28
ansond 0:b26c3526ee89 29 void nsdl_free(void* ptr_to_free) {
ansond 0:b26c3526ee89 30 if (ptr_to_free != NULL) free(ptr_to_free);
ansond 0:b26c3526ee89 31 }
ansond 0:b26c3526ee89 32
ansond 0:b26c3526ee89 33 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, uint16_t if_len, uint8_t *if_ptr, uint8_t is_observable, uint8_t is_registered, uint8_t *rsc, uint16_t rsc_len) {
ansond 0:b26c3526ee89 34 if (resource_structure != NULL) {
ansond 0:b26c3526ee89 35 resource_structure->access = SN_GRS_GET_ALLOWED;
ansond 0:b26c3526ee89 36 resource_structure->mode = SN_GRS_STATIC;
ansond 0:b26c3526ee89 37 resource_structure->pathlen = pt_len;
ansond 0:b26c3526ee89 38 resource_structure->path = pt;
ansond 0:b26c3526ee89 39 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
ansond 0:b26c3526ee89 40 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
ansond 0:b26c3526ee89 41 resource_structure->resource_parameters_ptr->interface_description_ptr = if_ptr;
ansond 0:b26c3526ee89 42 resource_structure->resource_parameters_ptr->interface_description_len = if_len;
ansond 0:b26c3526ee89 43 resource_structure->resource_parameters_ptr->observable = is_observable;
ansond 0:b26c3526ee89 44 resource_structure->resource_parameters_ptr->registered = is_registered;
ansond 0:b26c3526ee89 45 resource_structure->resource = rsc;
ansond 0:b26c3526ee89 46 resource_structure->resourcelen = rsc_len;
ansond 0:b26c3526ee89 47 sn_nsdl_create_resource(resource_structure);
ansond 0:b26c3526ee89 48 }
ansond 0:b26c3526ee89 49 }
ansond 0:b26c3526ee89 50
ansond 0:b26c3526ee89 51 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, uint16_t if_len, uint8_t *if_ptr, uint8_t is_observable, uint8_t is_registered, sn_grs_dyn_res_callback_t callback_ptr, int access_right) {
ansond 0:b26c3526ee89 52 if (resource_structure != NULL) {
ansond 0:b26c3526ee89 53 resource_structure->access = (sn_grs_resource_acl_e)access_right;
ansond 0:b26c3526ee89 54 resource_structure->resource = 0;
ansond 0:b26c3526ee89 55 resource_structure->resourcelen = 0;
ansond 0:b26c3526ee89 56 resource_structure->sn_grs_dyn_res_callback = callback_ptr;
ansond 0:b26c3526ee89 57 resource_structure->mode = SN_GRS_DYNAMIC;
ansond 0:b26c3526ee89 58 resource_structure->pathlen = pt_len;
ansond 0:b26c3526ee89 59 resource_structure->path = pt;
ansond 0:b26c3526ee89 60 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
ansond 0:b26c3526ee89 61 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
ansond 0:b26c3526ee89 62 resource_structure->resource_parameters_ptr->interface_description_ptr = if_ptr;
ansond 0:b26c3526ee89 63 resource_structure->resource_parameters_ptr->interface_description_len = if_len;
ansond 0:b26c3526ee89 64 resource_structure->resource_parameters_ptr->observable = is_observable;
ansond 0:b26c3526ee89 65 resource_structure->resource_parameters_ptr->registered = is_registered;
ansond 0:b26c3526ee89 66 sn_nsdl_create_resource(resource_structure);
ansond 0:b26c3526ee89 67 }
ansond 0:b26c3526ee89 68 }
ansond 0:b26c3526ee89 69
ansond 0:b26c3526ee89 70 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) {
ansond 0:b26c3526ee89 71 if (endpoint_structure == NULL) {
ansond 0:b26c3526ee89 72 endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
ansond 0:b26c3526ee89 73 }
ansond 0:b26c3526ee89 74
ansond 0:b26c3526ee89 75 if (endpoint_structure != NULL) {
ansond 0:b26c3526ee89 76 memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s));
ansond 0:b26c3526ee89 77 endpoint_structure->endpoint_name_ptr = name;
ansond 0:b26c3526ee89 78 endpoint_structure->endpoint_name_len = strlen((char*)name);
ansond 0:b26c3526ee89 79 endpoint_structure->type_ptr = typename_ptr;
ansond 0:b26c3526ee89 80 endpoint_structure->type_len = strlen((char*)typename_ptr);
ansond 0:b26c3526ee89 81 endpoint_structure->lifetime_ptr = lifetime_ptr;
ansond 0:b26c3526ee89 82 endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr);
ansond 0:b26c3526ee89 83 }
ansond 0:b26c3526ee89 84
ansond 0:b26c3526ee89 85 return endpoint_structure;
ansond 0:b26c3526ee89 86 }
ansond 0:b26c3526ee89 87
ansond 0:b26c3526ee89 88 void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure) {
ansond 0:b26c3526ee89 89 if (endpoint_structure != NULL) {
ansond 0:b26c3526ee89 90 if (*endpoint_structure != NULL) {
ansond 0:b26c3526ee89 91 nsdl_free(*endpoint_structure);
ansond 0:b26c3526ee89 92 *endpoint_structure = NULL;
ansond 0:b26c3526ee89 93 }
ansond 0:b26c3526ee89 94 }
ansond 0:b26c3526ee89 95 }
ansond 0:b26c3526ee89 96
ansond 0:b26c3526ee89 97 static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr) {
ansond 0:b26c3526ee89 98 //if (error_handler != NULL) error_handler->log("NSP: sending %d bytes...",data_len);
ansond 0:b26c3526ee89 99 //#ifdef NETWORK_MUTEX
ansond 0:b26c3526ee89 100 // if (network_mutex != NULL) network_mutex->lock();
ansond 0:b26c3526ee89 101 //#endif
ansond 0:b26c3526ee89 102 int sent = server.sendTo(nsp, (char*)data_ptr, data_len);
ansond 0:b26c3526ee89 103 //#ifdef NETWORK_MUTEX
ansond 0:b26c3526ee89 104 // if (network_mutex != NULL) network_mutex->unlock();
ansond 0:b26c3526ee89 105 //#endif
ansond 0:b26c3526ee89 106 //if (error_handler != NULL) error_handler->log("NSP: send done. sent %d bytes...",sent);
ansond 0:b26c3526ee89 107 if (sent != data_len) {
ansond 0:b26c3526ee89 108 if (error_handler != NULL) error_handler->log("NSP: send failed!! Attempted: %d Sent: %d", data_len, sent);
ansond 0:b26c3526ee89 109 }
ansond 0:b26c3526ee89 110 else {
ansond 0:b26c3526ee89 111 if (error_handler != NULL) error_handler->blinkTransportTxLED();
ansond 0:b26c3526ee89 112 }
ansond 0:b26c3526ee89 113
ansond 0:b26c3526ee89 114 return 1;
ansond 0:b26c3526ee89 115 }
ansond 0:b26c3526ee89 116
ansond 0:b26c3526ee89 117 static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) {
ansond 0:b26c3526ee89 118 // Rx callback process it...
ansond 0:b26c3526ee89 119 if (error_handler != NULL) error_handler->blinkTransportRxLED();
ansond 0:b26c3526ee89 120 return 0;
ansond 0:b26c3526ee89 121 }
ansond 0:b26c3526ee89 122
ansond 0:b26c3526ee89 123 static void register_endpoint(bool init) {
ansond 0:b26c3526ee89 124 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)_endpoint_name, _ep_type, _lifetime_ptr);
ansond 0:b26c3526ee89 125 sn_nsdl_update_registration(endpoint_ptr);
ansond 0:b26c3526ee89 126 nsdl_clean_register_endpoint(&endpoint_ptr);
ansond 0:b26c3526ee89 127 }
ansond 0:b26c3526ee89 128
ansond 0:b26c3526ee89 129 static void registration_update_thread(void const *args) {
ansond 0:b26c3526ee89 130 int count = 0;
ansond 0:b26c3526ee89 131 int registration_time = 240; // about 1 minute assuming 4 iterations/second RD_UPDATE_PERIOD;
ansond 0:b26c3526ee89 132
ansond 0:b26c3526ee89 133 // first we want to wait a bit... let the endpoint crank up...
ansond 0:b26c3526ee89 134 Thread::wait(RD_UPDATE_PERIOD); // wait about 150 seconds, then go ahead and start the re-registration interval...
ansond 0:b26c3526ee89 135
ansond 0:b26c3526ee89 136 // now loop this thread forever sleeping and re-registering at the right iteration.
ansond 0:b26c3526ee89 137 while(true) {
ansond 0:b26c3526ee89 138 Thread::wait(MAIN_LOOP_SLEEP);
ansond 0:b26c3526ee89 139 ++count;
ansond 0:b26c3526ee89 140 if (count%registration_time == 0) {
ansond 0:b26c3526ee89 141 // re-registration time!
ansond 0:b26c3526ee89 142 count = 0;
ansond 0:b26c3526ee89 143 if (error_handler != NULL) error_handler->log("NSP: (re)registering...");
ansond 0:b26c3526ee89 144 register_endpoint(false);
ansond 0:b26c3526ee89 145 if (error_handler != NULL) error_handler->log("NSP: (re)registering complete.");
ansond 0:b26c3526ee89 146 }
ansond 0:b26c3526ee89 147 else {
ansond 0:b26c3526ee89 148 if (count%RD_UPDATE_PERIOD == 0) {
ansond 0:b26c3526ee89 149 if (endpoint != NULL) endpoint->sendObservations();
ansond 0:b26c3526ee89 150 }
ansond 0:b26c3526ee89 151 if (error_handler != NULL) {
ansond 0:b26c3526ee89 152 error_handler->checkForExit();
ansond 0:b26c3526ee89 153 }
ansond 0:b26c3526ee89 154 }
ansond 0:b26c3526ee89 155 if (error_handler != NULL) error_handler->blinkTransportRxLED();
ansond 0:b26c3526ee89 156 }
ansond 0:b26c3526ee89 157 }
ansond 0:b26c3526ee89 158
ansond 0:b26c3526ee89 159 void nsdl_init() {
ansond 0:b26c3526ee89 160 uint8_t nsp_addr[4];
ansond 0:b26c3526ee89 161 sn_nsdl_mem_s memory_cbs;
ansond 0:b26c3526ee89 162
ansond 0:b26c3526ee89 163 /* Initialize libNsdl */
ansond 0:b26c3526ee89 164 memset(&memory_cbs,0,sizeof(memory_cbs));
ansond 0:b26c3526ee89 165 memset(nsp_addr,0,sizeof(nsp_addr));
ansond 0:b26c3526ee89 166 memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
ansond 0:b26c3526ee89 167 memory_cbs.sn_nsdl_free = &nsdl_free;
ansond 0:b26c3526ee89 168 if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) {
ansond 0:b26c3526ee89 169 if (error_handler != NULL) error_handler->log("NSP: libNsdl init failed");
ansond 0:b26c3526ee89 170 }
ansond 0:b26c3526ee89 171 else {
ansond 0:b26c3526ee89 172 if (error_handler != NULL) error_handler->log("NSP: libNsdl init successful");
ansond 0:b26c3526ee89 173 }
ansond 0:b26c3526ee89 174
ansond 0:b26c3526ee89 175 /* Set nsp address for library */
ansond 0:b26c3526ee89 176 set_NSP_address(nsp_addr, NSP_PORT, SN_NSDL_ADDRESS_TYPE_IPV4);
ansond 0:b26c3526ee89 177 }
ansond 0:b26c3526ee89 178
ansond 0:b26c3526ee89 179 // NSP event loop - spawn a re-registration thread AFTER we have initially registered and begun event processing...
ansond 0:b26c3526ee89 180 void nsdl_event_loop() {
ansond 0:b26c3526ee89 181 sn_nsdl_addr_s received_packet_address;
ansond 0:b26c3526ee89 182 Endpoint from;
ansond 0:b26c3526ee89 183 uint8_t nsp_received_address[4];
ansond 0:b26c3526ee89 184 char nsp_buffer[1024];
ansond 0:b26c3526ee89 185
ansond 0:b26c3526ee89 186 memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
ansond 0:b26c3526ee89 187 memset(nsp_received_address, 0, sizeof(nsp_received_address));
ansond 0:b26c3526ee89 188 received_packet_address.addr_ptr = nsp_received_address;
ansond 0:b26c3526ee89 189
ansond 0:b26c3526ee89 190 #ifdef ENABLE_THREADS
ansond 0:b26c3526ee89 191 // start the registration update thread.. it will wait a bit while the endpoint gins up...
ansond 0:b26c3526ee89 192 Thread registration_thread(registration_update_thread);
ansond 0:b26c3526ee89 193 #endif
ansond 0:b26c3526ee89 194
ansond 0:b26c3526ee89 195 // FOREVER: main loop for event processing
ansond 0:b26c3526ee89 196 while(true) {
ansond 0:b26c3526ee89 197 //if (error_handler != NULL) error_handler->log("NSP: waiting for data...");
ansond 0:b26c3526ee89 198 //#ifdef NETWORK_MUTEX
ansond 0:b26c3526ee89 199 // if (network_mutex != NULL) network_mutex->lock();
ansond 0:b26c3526ee89 200 //#endif
ansond 0:b26c3526ee89 201 int n = server.receiveFrom(from,nsp_buffer,sizeof(nsp_buffer));
ansond 0:b26c3526ee89 202 //#ifdef NETWORK_MUTEX
ansond 0:b26c3526ee89 203 // if (network_mutex != NULL) network_mutex->unlock();
ansond 0:b26c3526ee89 204 //#endif
ansond 0:b26c3526ee89 205 //if (error_handler != NULL) error_handler->log("NSP: received %d bytes... processing...",n);
ansond 0:b26c3526ee89 206 if (n >= 0) sn_nsdl_process_coap((uint8_t*)nsp_buffer,n,&received_packet_address);
ansond 0:b26c3526ee89 207
ansond 0:b26c3526ee89 208 //if (error_handler != NULL) error_handler->log("NSP: done processing %d bytes...",n);
ansond 0:b26c3526ee89 209 if (error_handler != NULL) error_handler->blinkTransportRxLED();
ansond 0:b26c3526ee89 210
ansond 0:b26c3526ee89 211 // perform any extra event loop work
ansond 0:b26c3526ee89 212 if (endpoint != NULL) endpoint->extraEventLoopWork();
ansond 0:b26c3526ee89 213 }
ansond 0:b26c3526ee89 214 }