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:
Mon Feb 16 06:37:35 2015 +0000
Revision:
5:9233e88b9c83
Parent:
2:30f4a0dab604
Child:
6:98af441fd960
updates - pre-RPC

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