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:
Fri Sep 26 05:55:55 2014 +0000
Revision:
3:30c96bd77160
Parent:
1:9824f99868a3
updates for new logger

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