BLE mbed Endpoint network stack for mbedConnectorInterface. The stack makes use of a special BLE Socket abstraction to create socket() semantics over BLE.

Dependencies:   libnsdl_m0 BLE_API Base64 nRF51822 SplitterAssembler

Committer:
ansond
Date:
Sun Apr 12 03:48:48 2015 +0000
Revision:
25:51cc8937022a
Parent:
24:0be5b09a207e
Child:
26:6c1ee1a58f8e
updates for nsdl lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:7809547930d9 1 // NSDL library support functions
ansond 0:7809547930d9 2
ansond 0:7809547930d9 3 #include "mbed.h"
ansond 0:7809547930d9 4 #include "nsdl_support.h"
ansond 0:7809547930d9 5
ansond 0:7809547930d9 6 #include "mbedConnectorInterface.h"
ansond 0:7809547930d9 7
ansond 0:7809547930d9 8 // we have to redefine DBG as its used differently here...
ansond 0:7809547930d9 9 #ifdef DBG
ansond 0:7809547930d9 10 #undef DBG
ansond 0:7809547930d9 11 #endif
ansond 0:7809547930d9 12 #define DBG std::printf
ansond 0:7809547930d9 13
ansond 0:7809547930d9 14 #include "UDPSocket.h"
ansond 0:7809547930d9 15 #include "Endpoint.h"
ansond 0:7809547930d9 16
ansond 2:30f4a0dab604 17 #include "BLEDevice.h"
ansond 2:30f4a0dab604 18
ansond 7:203c348ccc66 19 #define NSDL_BUFFER_LENGTH 1024
ansond 7:203c348ccc66 20
ansond 2:30f4a0dab604 21 extern BLEDevice ble;
ansond 2:30f4a0dab604 22
ansond 0:7809547930d9 23 Endpoint nsp;
ansond 0:7809547930d9 24 UDPSocket server;
ansond 11:d601b867b297 25 Endpoint from;
ansond 11:d601b867b297 26
ansond 11:d601b867b297 27 uint8_t nsp_received_address[4];
ansond 11:d601b867b297 28 char nsp_buffer[NSDL_BUFFER_LENGTH];
ansond 0:7809547930d9 29
ansond 0:7809547930d9 30 char null_endpoint_name[] = "";
ansond 0:7809547930d9 31 char null_domain[] = "";
ansond 0:7809547930d9 32 uint8_t null_ep_type[] = "";
ansond 0:7809547930d9 33 uint8_t null_lifetime_ptr[] = "";
ansond 20:5bbbdc2d8ac4 34 bool endpoint_registered = false;
ansond 0:7809547930d9 35
ansond 0:7809547930d9 36 void *nsdl_alloc(uint16_t size) {
ansond 0:7809547930d9 37 void *chunk = NULL;
ansond 0:7809547930d9 38 if (size > 0) chunk = malloc(size);
ansond 0:7809547930d9 39 if (chunk != NULL && size > 0) memset(chunk,0,size);
ansond 0:7809547930d9 40 return chunk;
ansond 0:7809547930d9 41 }
ansond 0:7809547930d9 42
ansond 0:7809547930d9 43 void nsdl_free(void* ptr_to_free) {
ansond 0:7809547930d9 44 if (ptr_to_free != NULL) free(ptr_to_free);
ansond 0:7809547930d9 45 }
ansond 0:7809547930d9 46
ansond 0:7809547930d9 47 /*
ansond 0:7809547930d9 48 * Create a static resoure
ansond 0:7809547930d9 49 * Only get is allowed
ansond 0:7809547930d9 50 */
ansond 0:7809547930d9 51 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)
ansond 0:7809547930d9 52 {
ansond 0:7809547930d9 53 resource_structure->access = SN_GRS_GET_ALLOWED;
ansond 0:7809547930d9 54 resource_structure->mode = SN_GRS_STATIC;
ansond 0:7809547930d9 55 resource_structure->pathlen = pt_len;
ansond 0:7809547930d9 56 resource_structure->path = pt;
ansond 0:7809547930d9 57 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
ansond 0:7809547930d9 58 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
ansond 0:7809547930d9 59 resource_structure->resource = rsc;
ansond 0:7809547930d9 60 resource_structure->resourcelen = rsc_len;
ansond 0:7809547930d9 61 sn_nsdl_create_resource(resource_structure);
ansond 0:7809547930d9 62 }
ansond 0:7809547930d9 63
ansond 0:7809547930d9 64 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)
ansond 0:7809547930d9 65 {
ansond 0:7809547930d9 66 resource_structure->access = (sn_grs_resource_acl_e)access_right;
ansond 0:7809547930d9 67 resource_structure->resource = 0;
ansond 0:7809547930d9 68 resource_structure->resourcelen = 0;
ansond 0:7809547930d9 69 resource_structure->sn_grs_dyn_res_callback = callback_ptr;
ansond 0:7809547930d9 70 resource_structure->mode = SN_GRS_DYNAMIC;
ansond 0:7809547930d9 71 resource_structure->pathlen = pt_len;
ansond 0:7809547930d9 72 resource_structure->path = pt;
ansond 0:7809547930d9 73 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
ansond 0:7809547930d9 74 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
ansond 0:7809547930d9 75 resource_structure->resource_parameters_ptr->observable = is_observable;
ansond 0:7809547930d9 76 sn_nsdl_create_resource(resource_structure);
ansond 0:7809547930d9 77 }
ansond 0:7809547930d9 78
ansond 0:7809547930d9 79 sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t *domain, uint8_t* name, uint8_t* typename_ptr, uint8_t *lifetime_ptr) {
ansond 0:7809547930d9 80 if (endpoint_structure == NULL) {
ansond 0:7809547930d9 81 endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
ansond 0:7809547930d9 82 }
ansond 0:7809547930d9 83
ansond 0:7809547930d9 84 if (endpoint_structure != NULL) {
ansond 0:7809547930d9 85 memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s));
ansond 0:7809547930d9 86 endpoint_structure->endpoint_name_ptr = name;
ansond 0:7809547930d9 87 endpoint_structure->endpoint_name_len = strlen((char*)name);
ansond 0:7809547930d9 88 endpoint_structure->domain_name_ptr = domain;
ansond 0:7809547930d9 89 endpoint_structure->domain_name_len = strlen((char *)domain);
ansond 0:7809547930d9 90 endpoint_structure->type_ptr = typename_ptr;
ansond 0:7809547930d9 91 endpoint_structure->type_len = strlen((char*)typename_ptr);
ansond 0:7809547930d9 92 endpoint_structure->lifetime_ptr = lifetime_ptr;
ansond 0:7809547930d9 93 endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr);
ansond 0:7809547930d9 94 }
ansond 0:7809547930d9 95
ansond 0:7809547930d9 96 return endpoint_structure;
ansond 0:7809547930d9 97 }
ansond 0:7809547930d9 98
ansond 0:7809547930d9 99 void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure) {
ansond 0:7809547930d9 100 if (endpoint_structure != NULL) {
ansond 0:7809547930d9 101 if (*endpoint_structure != NULL) {
ansond 0:7809547930d9 102 nsdl_free(*endpoint_structure);
ansond 0:7809547930d9 103 *endpoint_structure = NULL;
ansond 0:7809547930d9 104 }
ansond 0:7809547930d9 105 }
ansond 0:7809547930d9 106 }
ansond 0:7809547930d9 107
ansond 0:7809547930d9 108 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 21:828ca4f1da3d 109 //DBG("NSP: tx_cb(): sending %d bytes...\r\n",data_len);
ansond 6:98af441fd960 110 int sent = server.sendTo(nsp,(char*)data_ptr, data_len);
ansond 0:7809547930d9 111 return 1;
ansond 0:7809547930d9 112 }
ansond 0:7809547930d9 113
ansond 0:7809547930d9 114 static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) {
ansond 0:7809547930d9 115 // Rx callback process it...
ansond 16:fb9c3f2af2df 116 //DBG("NSP: rx_cb(): received data. processing...\r\n");
ansond 0:7809547930d9 117 return 0;
ansond 0:7809547930d9 118 }
ansond 0:7809547930d9 119
ansond 0:7809547930d9 120 void register_endpoint(bool init) {
ansond 0:7809547930d9 121 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
ansond 0:7809547930d9 122 if (init) {
ansond 0:7809547930d9 123 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t *)domain_name, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
ansond 20:5bbbdc2d8ac4 124 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) {
ansond 0:7809547930d9 125 DBG("NSP initial registration failed\r\n");
ansond 20:5bbbdc2d8ac4 126 endpoint_registered = false;
ansond 20:5bbbdc2d8ac4 127 }
ansond 20:5bbbdc2d8ac4 128 else {
ansond 22:8f6d03b2f844 129 //DBG("NSP initial registration OK\r\n");
ansond 20:5bbbdc2d8ac4 130 endpoint_registered = true;
ansond 20:5bbbdc2d8ac4 131 }
ansond 0:7809547930d9 132 }
ansond 0:7809547930d9 133 else {
ansond 23:9aeda6113295 134 endpoint_registered = false;
ansond 22:8f6d03b2f844 135 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t *)null_domain, (uint8_t*)null_endpoint_name, null_ep_type, null_lifetime_ptr);
ansond 22:8f6d03b2f844 136 if(sn_nsdl_update_registration(endpoint_ptr) != 0) {
ansond 22:8f6d03b2f844 137 DBG("NSP re-registration failed\r\n");
ansond 22:8f6d03b2f844 138 }
ansond 22:8f6d03b2f844 139 else {
ansond 22:8f6d03b2f844 140 //DBG("NSP re-registration OK\r\n");
ansond 23:9aeda6113295 141 endpoint_registered = true;
ansond 21:828ca4f1da3d 142 }
ansond 0:7809547930d9 143 }
ansond 0:7809547930d9 144 nsdl_clean_register_endpoint(&endpoint_ptr);
ansond 0:7809547930d9 145 }
ansond 0:7809547930d9 146
ansond 20:5bbbdc2d8ac4 147 bool nsdl_endpoint_is_registered(void) {
ansond 20:5bbbdc2d8ac4 148 return endpoint_registered;
ansond 20:5bbbdc2d8ac4 149 }
ansond 20:5bbbdc2d8ac4 150
ansond 2:30f4a0dab604 151 // Simple Ticker-based re-registration updating...
ansond 2:30f4a0dab604 152 void registration_update_tick(void) {
ansond 22:8f6d03b2f844 153 register_endpoint(!endpoint_registered);
ansond 0:7809547930d9 154 }
ansond 0:7809547930d9 155
ansond 0:7809547930d9 156 void nsdl_init() {
ansond 0:7809547930d9 157 sn_nsdl_mem_s memory_cbs;
ansond 0:7809547930d9 158
ansond 0:7809547930d9 159 // initilize the UDP channel
ansond 0:7809547930d9 160 server.init();
ansond 0:7809547930d9 161 server.bind(nsp_port);
ansond 0:7809547930d9 162
ansond 0:7809547930d9 163 /* Initialize libNsdl */
ansond 0:7809547930d9 164 memset(&memory_cbs,0,sizeof(memory_cbs));
ansond 0:7809547930d9 165 memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
ansond 0:7809547930d9 166 memory_cbs.sn_nsdl_free = &nsdl_free;
ansond 0:7809547930d9 167 if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) {
ansond 0:7809547930d9 168 DBG("NSP: libNsdl init failed.\r\n");
ansond 0:7809547930d9 169 }
ansond 0:7809547930d9 170 else {
ansond 0:7809547930d9 171 DBG("NSP: libNsdl init successful.\r\n");
ansond 0:7809547930d9 172 }
ansond 0:7809547930d9 173 }
ansond 0:7809547930d9 174
ansond 0:7809547930d9 175 void nsdl_set_nsp_address(void) {
ansond 0:7809547930d9 176 char NSP_address_str[16];
ansond 0:7809547930d9 177
ansond 0:7809547930d9 178 /* Set nsp address for library */
ansond 0:7809547930d9 179 sprintf(NSP_address_str,"%d.%d.%d.%d",NSP_address_bytes[0],NSP_address_bytes[1],NSP_address_bytes[2],NSP_address_bytes[3]);
ansond 0:7809547930d9 180 DBG("NSP: libNsdl NSP_ADDRESS: %s port: %d\r\n",NSP_address_str,nsp_port);
ansond 0:7809547930d9 181 set_NSP_address(NSP_address_bytes, nsp_port, SN_NSDL_ADDRESS_TYPE_IPV4);
ansond 0:7809547930d9 182 nsp.set_address(NSP_address_str,nsp_port);
ansond 0:7809547930d9 183 }
ansond 0:7809547930d9 184
ansond 2:30f4a0dab604 185 extern "C" volatile bool __registered;
ansond 2:30f4a0dab604 186
ansond 0:7809547930d9 187 // NSP event loop - spawn a re-registration thread AFTER we have initially registered and begun event processing...
ansond 0:7809547930d9 188 void nsdl_event_loop() {
ansond 0:7809547930d9 189 sn_nsdl_addr_s received_packet_address;
ansond 2:30f4a0dab604 190
ansond 0:7809547930d9 191 memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
ansond 0:7809547930d9 192 memset(nsp_received_address, 0, sizeof(nsp_received_address));
ansond 25:51cc8937022a 193 received_packet_address.addr_ptr = nsp_received_address;
ansond 0:7809547930d9 194
ansond 2:30f4a0dab604 195 // re-registration update ticker...
ansond 2:30f4a0dab604 196 Ticker ticker;
ansond 24:0be5b09a207e 197 ticker.attach(registration_update_tick,NSP_RD_UPDATE_PERIOD);
ansond 0:7809547930d9 198
ansond 0:7809547930d9 199 // FOREVER: main loop for event processing
ansond 2:30f4a0dab604 200 while(true) {
ansond 2:30f4a0dab604 201 // wait for BLE events...
ansond 2:30f4a0dab604 202 ble.waitForEvent();
ansond 2:30f4a0dab604 203
ansond 2:30f4a0dab604 204 // only process if we are registered and thus connected... otherwise ignore
ansond 25:51cc8937022a 205 if (endpoint_registered == true) {
ansond 15:3c021e52addd 206 //DBG("NSP: waiting for data...\r\n");
ansond 9:bf0cf5828378 207 memset(nsp_buffer,0,NSDL_BUFFER_LENGTH);
ansond 7:203c348ccc66 208 int n = server.receiveFrom(from,nsp_buffer,NSDL_BUFFER_LENGTH);
ansond 9:bf0cf5828378 209 if (n > 0) {
ansond 21:828ca4f1da3d 210 //DBG("NSP: received %d bytes... processing..\r\n.",n);
ansond 5:9233e88b9c83 211 sn_nsdl_process_coap((uint8_t*)nsp_buffer,n,&received_packet_address);
ansond 5:9233e88b9c83 212 }
ansond 2:30f4a0dab604 213 }
ansond 0:7809547930d9 214 }
ansond 0:7809547930d9 215 }