6LowPAN mesh-based network support for mbedConnectorInterface. The Atmel-based mbed 6LowPAN shield is the assumed network hardware.

Dependencies:   libnsdl Nanostack_lib

Committer:
ansond
Date:
Tue Nov 03 17:07:01 2015 +0000
Revision:
13:17948fd0fe32
Parent:
8:635a826f1de9
updated buffer sizes, updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:2a5a48a8b4d4 1 // NSDL library support functions
ansond 0:2a5a48a8b4d4 2
ansond 0:2a5a48a8b4d4 3 #include "configuration.h"
ansond 0:2a5a48a8b4d4 4 #include "mbedConnectorInterface.h"
ansond 0:2a5a48a8b4d4 5 #include "debug.h"
ansond 0:2a5a48a8b4d4 6 #include "nsdl_support.h"
ansond 0:2a5a48a8b4d4 7 #include "socket_api.h"
ansond 0:2a5a48a8b4d4 8
ansond 0:2a5a48a8b4d4 9 // we have to redefine DBG as its used differently here...
ansond 0:2a5a48a8b4d4 10 #ifdef DBG
ansond 0:2a5a48a8b4d4 11 #undef DBG
ansond 0:2a5a48a8b4d4 12 #endif
ansond 0:2a5a48a8b4d4 13 #define DBG printf
ansond 0:2a5a48a8b4d4 14
ansond 0:2a5a48a8b4d4 15 static ns_address_t app_dest;
ansond 0:2a5a48a8b4d4 16 char null_ep_name[] = "";
ansond 0:2a5a48a8b4d4 17 char null_ep_domain[] = "";
ansond 0:2a5a48a8b4d4 18 uint8_t null_ep_type[] = "";
ansond 0:2a5a48a8b4d4 19 uint8_t null_lifetime_ptr[] = "";
ansond 0:2a5a48a8b4d4 20 bool nsdl_reg_update_needed = false;
ansond 7:0e80a25afabc 21 bool endpoint_registered = false;
ansond 0:2a5a48a8b4d4 22
ansond 0:2a5a48a8b4d4 23 extern int8_t coap_udp_socket;
ansond 0:2a5a48a8b4d4 24
ansond 0:2a5a48a8b4d4 25 void *nsdl_alloc(uint16_t size)
ansond 0:2a5a48a8b4d4 26 {
ansond 0:2a5a48a8b4d4 27 void *buf = malloc(size);
ansond 0:2a5a48a8b4d4 28 return buf;
ansond 0:2a5a48a8b4d4 29 }
ansond 0:2a5a48a8b4d4 30
ansond 0:2a5a48a8b4d4 31 void nsdl_free(void* ptr_to_free)
ansond 0:2a5a48a8b4d4 32 {
ansond 0:2a5a48a8b4d4 33 free(ptr_to_free);
ansond 0:2a5a48a8b4d4 34 }
ansond 0:2a5a48a8b4d4 35
ansond 0:2a5a48a8b4d4 36 /*
ansond 0:2a5a48a8b4d4 37 * Create a static resoure
ansond 0:2a5a48a8b4d4 38 * Only get is allowed
ansond 0:2a5a48a8b4d4 39 */
ansond 0:2a5a48a8b4d4 40 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:2a5a48a8b4d4 41 {
ansond 0:2a5a48a8b4d4 42 resource_structure->access = SN_GRS_GET_ALLOWED;
ansond 0:2a5a48a8b4d4 43 resource_structure->mode = SN_GRS_STATIC;
ansond 0:2a5a48a8b4d4 44 resource_structure->pathlen = pt_len;
ansond 0:2a5a48a8b4d4 45 resource_structure->path = pt;
ansond 0:2a5a48a8b4d4 46 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
ansond 0:2a5a48a8b4d4 47 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
ansond 0:2a5a48a8b4d4 48 resource_structure->resource = rsc;
ansond 0:2a5a48a8b4d4 49 resource_structure->resourcelen = rsc_len;
ansond 0:2a5a48a8b4d4 50 sn_nsdl_create_resource(resource_structure);
ansond 0:2a5a48a8b4d4 51 }
ansond 0:2a5a48a8b4d4 52
ansond 0:2a5a48a8b4d4 53 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:2a5a48a8b4d4 54 {
ansond 0:2a5a48a8b4d4 55 resource_structure->access = (sn_grs_resource_acl_e)access_right;
ansond 0:2a5a48a8b4d4 56 resource_structure->resource = 0;
ansond 0:2a5a48a8b4d4 57 resource_structure->resourcelen = 0;
ansond 0:2a5a48a8b4d4 58 resource_structure->sn_grs_dyn_res_callback = callback_ptr;
ansond 0:2a5a48a8b4d4 59 resource_structure->mode = SN_GRS_DYNAMIC;
ansond 0:2a5a48a8b4d4 60 resource_structure->pathlen = pt_len;
ansond 0:2a5a48a8b4d4 61 resource_structure->path = pt;
ansond 0:2a5a48a8b4d4 62 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
ansond 0:2a5a48a8b4d4 63 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
ansond 0:2a5a48a8b4d4 64 resource_structure->resource_parameters_ptr->observable = is_observable;
ansond 0:2a5a48a8b4d4 65 sn_nsdl_create_resource(resource_structure);
ansond 0:2a5a48a8b4d4 66 }
ansond 0:2a5a48a8b4d4 67
ansond 0:2a5a48a8b4d4 68 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:2a5a48a8b4d4 69 {
ansond 0:2a5a48a8b4d4 70 if (endpoint_structure == NULL)
ansond 0:2a5a48a8b4d4 71 {
ansond 0:2a5a48a8b4d4 72 endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
ansond 0:2a5a48a8b4d4 73 }
ansond 0:2a5a48a8b4d4 74 if (endpoint_structure != NULL)
ansond 0:2a5a48a8b4d4 75 {
ansond 0:2a5a48a8b4d4 76 memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s));
ansond 0:2a5a48a8b4d4 77 endpoint_structure->endpoint_name_ptr = name;
ansond 0:2a5a48a8b4d4 78 endpoint_structure->endpoint_name_len = strlen((char*)name);
ansond 0:2a5a48a8b4d4 79 endpoint_structure->domain_name_ptr = domain;
ansond 0:2a5a48a8b4d4 80 endpoint_structure->domain_name_len = strlen((char*)domain);
ansond 0:2a5a48a8b4d4 81 endpoint_structure->type_ptr = typename_ptr;
ansond 0:2a5a48a8b4d4 82 endpoint_structure->type_len = strlen((char*)typename_ptr);
ansond 0:2a5a48a8b4d4 83 endpoint_structure->lifetime_ptr = lifetime_ptr;
ansond 0:2a5a48a8b4d4 84 endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr);
ansond 0:2a5a48a8b4d4 85 }
ansond 0:2a5a48a8b4d4 86 return endpoint_structure;
ansond 0:2a5a48a8b4d4 87 }
ansond 0:2a5a48a8b4d4 88
ansond 0:2a5a48a8b4d4 89 void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure)
ansond 0:2a5a48a8b4d4 90 {
ansond 0:2a5a48a8b4d4 91 if (*endpoint_structure)
ansond 0:2a5a48a8b4d4 92 {
ansond 0:2a5a48a8b4d4 93 nsdl_free(*endpoint_structure);
ansond 0:2a5a48a8b4d4 94 *endpoint_structure = NULL;
ansond 0:2a5a48a8b4d4 95 }
ansond 0:2a5a48a8b4d4 96 }
ansond 0:2a5a48a8b4d4 97
ansond 0:2a5a48a8b4d4 98 static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t datalen, sn_nsdl_addr_s *address_prt)
ansond 0:2a5a48a8b4d4 99 {
ansond 0:2a5a48a8b4d4 100 DBG("TX callback!\n\rSending %d bytes...", datalen);
ansond 0:2a5a48a8b4d4 101 //DBG("Payload:'");
ansond 0:2a5a48a8b4d4 102 //for (int i=0; i < datalen; i++)
ansond 0:2a5a48a8b4d4 103 // DBG("%c", *(data_ptr + i));
ansond 0:2a5a48a8b4d4 104 //DBG("' \r\n");
ansond 0:2a5a48a8b4d4 105
ansond 0:2a5a48a8b4d4 106 int8_t socket_ret_value = 0;
ansond 0:2a5a48a8b4d4 107 memcpy(app_dest.address, address_prt->addr_ptr, 16);
ansond 0:2a5a48a8b4d4 108 app_dest.identifier = address_prt->port;
ansond 0:2a5a48a8b4d4 109 app_dest.type = ADDRESS_IPV6;
ansond 0:2a5a48a8b4d4 110
ansond 0:2a5a48a8b4d4 111 socket_ret_value = socket_sendto(coap_udp_socket, &app_dest, data_ptr, datalen);
ansond 0:2a5a48a8b4d4 112 if(socket_ret_value != 0)
ansond 0:2a5a48a8b4d4 113 {
ansond 0:2a5a48a8b4d4 114 DBG("Failed\r\n");
ansond 0:2a5a48a8b4d4 115 return 0;//failed
ansond 0:2a5a48a8b4d4 116 }
ansond 0:2a5a48a8b4d4 117 else
ansond 0:2a5a48a8b4d4 118 {
ansond 0:2a5a48a8b4d4 119 DBG("Ok\r\n");
ansond 0:2a5a48a8b4d4 120 return 1; // success
ansond 0:2a5a48a8b4d4 121 }
ansond 0:2a5a48a8b4d4 122 }
ansond 0:2a5a48a8b4d4 123
ansond 0:2a5a48a8b4d4 124 static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
ansond 0:2a5a48a8b4d4 125 {
ansond 0:2a5a48a8b4d4 126 DBG("RX callback!\r\n");
ansond 0:2a5a48a8b4d4 127 DBG("msg_code: %d \r\n", coap_packet_ptr->msg_code);
ansond 0:2a5a48a8b4d4 128 DBG("Payload length: %d bytes\r\n", coap_packet_ptr->payload_len);
ansond 0:2a5a48a8b4d4 129 int i;
ansond 0:2a5a48a8b4d4 130 DBG("Payload:'");
ansond 0:2a5a48a8b4d4 131 for (i=0; i < coap_packet_ptr->payload_len; i++)
ansond 0:2a5a48a8b4d4 132 DBG("%c", *(coap_packet_ptr->payload_ptr + i));
ansond 0:2a5a48a8b4d4 133 DBG("' \r\n");
ansond 0:2a5a48a8b4d4 134 if (coap_packet_ptr->options_list_ptr && coap_packet_ptr->options_list_ptr->location_path_ptr)
ansond 0:2a5a48a8b4d4 135 {
ansond 0:2a5a48a8b4d4 136 DBG("Location: /");
ansond 0:2a5a48a8b4d4 137 int i;
ansond 0:2a5a48a8b4d4 138 for (i=0; i < coap_packet_ptr->options_list_ptr->location_path_len; i++) DBG("%c", (char)(coap_packet_ptr->options_list_ptr->location_path_ptr[i]));
ansond 0:2a5a48a8b4d4 139 DBG(" \r\n");
ansond 0:2a5a48a8b4d4 140 }
ansond 0:2a5a48a8b4d4 141 //sn_coap_packet_debug(coap_packet_ptr);
ansond 0:2a5a48a8b4d4 142 return 0;
ansond 0:2a5a48a8b4d4 143 }
ansond 0:2a5a48a8b4d4 144
ansond 0:2a5a48a8b4d4 145
ansond 0:2a5a48a8b4d4 146 void NSP_registration( ) {
ansond 0:2a5a48a8b4d4 147 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
ansond 0:2a5a48a8b4d4 148
ansond 0:2a5a48a8b4d4 149 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t *)domain_name, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
ansond 7:0e80a25afabc 150 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) {
ansond 0:2a5a48a8b4d4 151 DBG("NSP re-registering failed\r\n");
ansond 7:0e80a25afabc 152 endpoint_registered = false;
ansond 7:0e80a25afabc 153 }
ansond 7:0e80a25afabc 154 else {
ansond 0:2a5a48a8b4d4 155 DBG("NSP re-registering OK\r\n");
ansond 7:0e80a25afabc 156 endpoint_registered = true;
ansond 7:0e80a25afabc 157 }
ansond 0:2a5a48a8b4d4 158 nsdl_clean_register_endpoint(&endpoint_ptr);
ansond 0:2a5a48a8b4d4 159 }
ansond 0:2a5a48a8b4d4 160
ansond 7:0e80a25afabc 161 bool nsdl_endpoint_is_registered(void) {
ansond 7:0e80a25afabc 162 return endpoint_registered;
ansond 7:0e80a25afabc 163 }
ansond 0:2a5a48a8b4d4 164
ansond 0:2a5a48a8b4d4 165 void nsdl_init()
ansond 0:2a5a48a8b4d4 166 {
ansond 0:2a5a48a8b4d4 167 //uint8_t nsp_addr[4];
ansond 0:2a5a48a8b4d4 168 sn_nsdl_mem_s memory_cbs;
ansond 0:2a5a48a8b4d4 169
ansond 0:2a5a48a8b4d4 170 /* Initialize libNsdl */
ansond 0:2a5a48a8b4d4 171 memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
ansond 0:2a5a48a8b4d4 172 memory_cbs.sn_nsdl_free = &nsdl_free;
ansond 0:2a5a48a8b4d4 173 if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1)
ansond 0:2a5a48a8b4d4 174 DBG("libNsdl init failed\r\n");
ansond 0:2a5a48a8b4d4 175 else
ansond 0:2a5a48a8b4d4 176 DBG("libNsdl init done\r\n");
ansond 0:2a5a48a8b4d4 177 /* Set nsp address for library */
ansond 0:2a5a48a8b4d4 178 set_NSP_address(NSP_address_bytes, NSP_COAP_UDP_PORT, SN_NSDL_ADDRESS_TYPE_IPV6);
ansond 0:2a5a48a8b4d4 179 }
ansond 0:2a5a48a8b4d4 180
ansond 0:2a5a48a8b4d4 181 void nsdl_reg_update()
ansond 8:635a826f1de9 182 {
ansond 0:2a5a48a8b4d4 183 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
ansond 0:2a5a48a8b4d4 184 // reg update should be invoked with null parameters if nothing is changing
ansond 0:2a5a48a8b4d4 185 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t *)null_ep_domain, (uint8_t*)null_ep_name, null_ep_type, null_lifetime_ptr);
ansond 8:635a826f1de9 186 if(sn_nsdl_update_registration(endpoint_ptr) != 0) {
ansond 0:2a5a48a8b4d4 187 DBG("NSP re-registering failed\r\n");
ansond 8:635a826f1de9 188 endpoint_registered = false;
ansond 8:635a826f1de9 189 }
ansond 8:635a826f1de9 190 else {
ansond 0:2a5a48a8b4d4 191 DBG("NSP re-registering OK\r\n");
ansond 8:635a826f1de9 192 endpoint_registered = true;
ansond 0:2a5a48a8b4d4 193 }
ansond 0:2a5a48a8b4d4 194
ansond 8:635a826f1de9 195 nsdl_clean_register_endpoint(&endpoint_ptr);
ansond 8:635a826f1de9 196 }
ansond 8:635a826f1de9 197
ansond 0:2a5a48a8b4d4 198 void nsdl_reg_update_timeout()
ansond 8:635a826f1de9 199 {
ansond 0:2a5a48a8b4d4 200 nsdl_reg_update_needed = true;
ansond 8:635a826f1de9 201 }
ansond 0:2a5a48a8b4d4 202
ansond 0:2a5a48a8b4d4 203 void nsdl_event_loop()
ansond 0:2a5a48a8b4d4 204 {
ansond 0:2a5a48a8b4d4 205
ansond 0:2a5a48a8b4d4 206 sn_nsdl_addr_s received_packet_address;
ansond 0:2a5a48a8b4d4 207 uint8_t received_address[4];
ansond 0:2a5a48a8b4d4 208
ansond 0:2a5a48a8b4d4 209 memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
ansond 0:2a5a48a8b4d4 210 received_packet_address.addr_ptr = received_address;
ansond 0:2a5a48a8b4d4 211
ansond 0:2a5a48a8b4d4 212 while(1)
ansond 0:2a5a48a8b4d4 213 {
ansond 8:635a826f1de9 214 //pc.DBG("checking reg timeout\r\n");
ansond 0:2a5a48a8b4d4 215 if (nsdl_reg_update_needed)
ansond 0:2a5a48a8b4d4 216 {
ansond 0:2a5a48a8b4d4 217 nsdl_reg_update_needed = false;
ansond 8:635a826f1de9 218
ansond 8:635a826f1de9 219 if (nsdl_endpoint_is_registered())
ansond 8:635a826f1de9 220 nsdl_reg_update();
ansond 8:635a826f1de9 221 else
ansond 8:635a826f1de9 222 NSP_registration();
ansond 8:635a826f1de9 223
ansond 8:635a826f1de9 224 // wait just a bit...
ansond 8:635a826f1de9 225 //wait_ms(1000);
ansond 0:2a5a48a8b4d4 226 }
ansond 0:2a5a48a8b4d4 227 }
ansond 0:2a5a48a8b4d4 228 }
ansond 0:2a5a48a8b4d4 229
ansond 4:2c8eeaf31699 230 void nsdl_set_nsp_address(void) {
ansond 4:2c8eeaf31699 231 // not used in 6LowPAN version of nsdl_support.cpp
ansond 4:2c8eeaf31699 232 ;
ansond 0:2a5a48a8b4d4 233 }