NanoService Example for u-blox Cellular modems

Dependencies:   Beep LM75B MMA7660 mbed nsdl_lib

Fork of NSDL_HelloWorld by Sensinode

Committer:
zdshelby
Date:
Wed Oct 30 00:32:48 2013 +0000
Revision:
9:ccb9e53d5471
Parent:
8:8452c1eaa690
Child:
10:4cb556c7845e
- Attempting to solve debugging issues, no debug available even using plain printf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 2:7e489126fe7a 1 // NSDL library support functions
bogdanm 2:7e489126fe7a 2
bogdanm 2:7e489126fe7a 3 #include "mbed.h"
bogdanm 2:7e489126fe7a 4 #include "nsdl_support.h"
bogdanm 2:7e489126fe7a 5 #include "mbed.h"
bogdanm 2:7e489126fe7a 6 #include "rtos.h"
zdshelby 8:8452c1eaa690 7 #include "UbloxUSBGSMModem.h"
zdshelby 8:8452c1eaa690 8 #include "UbloxUSBCDMAModem.h"
zdshelby 8:8452c1eaa690 9 #include "CellularModem.h"
zdshelby 8:8452c1eaa690 10 #include "Socket.h"
zdshelby 8:8452c1eaa690 11 #include "Endpoint.h"
zdshelby 8:8452c1eaa690 12 #include "UDPSocket.h"
bogdanm 2:7e489126fe7a 13
bogdanm 2:7e489126fe7a 14 extern Serial pc;
zdshelby 8:8452c1eaa690 15 extern UbloxUSBGSMModem modem;
bogdanm 2:7e489126fe7a 16 extern Endpoint nsp;
bogdanm 2:7e489126fe7a 17 extern UDPSocket server;
bogdanm 3:52c1b649eb04 18 extern char endpoint_name[16];
bogdanm 2:7e489126fe7a 19 extern uint8_t ep_type[];
bogdanm 2:7e489126fe7a 20 extern uint8_t lifetime_ptr[];
bogdanm 2:7e489126fe7a 21
bogdanm 2:7e489126fe7a 22 /* The number of seconds between NSP registration messages */
bogdanm 2:7e489126fe7a 23 #define RD_UPDATE_PERIOD 60
bogdanm 2:7e489126fe7a 24
bogdanm 2:7e489126fe7a 25 void *nsdl_alloc(uint16_t size)
bogdanm 2:7e489126fe7a 26 {
bogdanm 2:7e489126fe7a 27 return malloc(size);
bogdanm 2:7e489126fe7a 28 }
bogdanm 2:7e489126fe7a 29
bogdanm 2:7e489126fe7a 30 void nsdl_free(void* ptr_to_free)
bogdanm 2:7e489126fe7a 31 {
bogdanm 2:7e489126fe7a 32 free(ptr_to_free);
bogdanm 2:7e489126fe7a 33 }
bogdanm 2:7e489126fe7a 34
bogdanm 2:7e489126fe7a 35 /*
bogdanm 2:7e489126fe7a 36 * Create a static resoure
bogdanm 2:7e489126fe7a 37 * Only get is allowed
bogdanm 2:7e489126fe7a 38 */
bogdanm 2:7e489126fe7a 39 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)
bogdanm 2:7e489126fe7a 40 {
bogdanm 2:7e489126fe7a 41 resource_structure->access = SN_GRS_GET_ALLOWED;
bogdanm 2:7e489126fe7a 42 resource_structure->mode = SN_GRS_STATIC;
bogdanm 2:7e489126fe7a 43 resource_structure->pathlen = pt_len;
bogdanm 2:7e489126fe7a 44 resource_structure->path = pt;
bogdanm 2:7e489126fe7a 45 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
bogdanm 2:7e489126fe7a 46 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
bogdanm 2:7e489126fe7a 47 resource_structure->resource = rsc;
bogdanm 2:7e489126fe7a 48 resource_structure->resourcelen = rsc_len;
bogdanm 2:7e489126fe7a 49 sn_nsdl_create_resource(resource_structure);
bogdanm 2:7e489126fe7a 50 }
bogdanm 2:7e489126fe7a 51
bogdanm 2:7e489126fe7a 52 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)
bogdanm 2:7e489126fe7a 53 {
bogdanm 2:7e489126fe7a 54 resource_structure->access = (sn_grs_resource_acl_e)access_right;
bogdanm 2:7e489126fe7a 55 resource_structure->resource = 0;
bogdanm 2:7e489126fe7a 56 resource_structure->resourcelen = 0;
bogdanm 2:7e489126fe7a 57 resource_structure->sn_grs_dyn_res_callback = callback_ptr;
bogdanm 2:7e489126fe7a 58 resource_structure->mode = SN_GRS_DYNAMIC;
bogdanm 2:7e489126fe7a 59 resource_structure->pathlen = pt_len;
bogdanm 2:7e489126fe7a 60 resource_structure->path = pt;
bogdanm 2:7e489126fe7a 61 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
bogdanm 2:7e489126fe7a 62 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
bogdanm 2:7e489126fe7a 63 resource_structure->resource_parameters_ptr->observable = is_observable;
bogdanm 2:7e489126fe7a 64 sn_nsdl_create_resource(resource_structure);
bogdanm 2:7e489126fe7a 65 }
bogdanm 2:7e489126fe7a 66
bogdanm 2:7e489126fe7a 67 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)
bogdanm 2:7e489126fe7a 68 {
bogdanm 2:7e489126fe7a 69 if (NULL == endpoint_structure)
bogdanm 2:7e489126fe7a 70 {
bogdanm 2:7e489126fe7a 71 endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
bogdanm 2:7e489126fe7a 72 }
bogdanm 2:7e489126fe7a 73 if (endpoint_structure)
bogdanm 2:7e489126fe7a 74 {
bogdanm 2:7e489126fe7a 75 memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s));
bogdanm 2:7e489126fe7a 76 endpoint_structure->endpoint_name_ptr = name;
bogdanm 7:6b068978be9a 77 endpoint_structure->endpoint_name_len = strlen((char*)name);
bogdanm 2:7e489126fe7a 78 endpoint_structure->type_ptr = typename_ptr;
bogdanm 3:52c1b649eb04 79 endpoint_structure->type_len = strlen((char*)typename_ptr);
bogdanm 2:7e489126fe7a 80 endpoint_structure->lifetime_ptr = lifetime_ptr;
bogdanm 3:52c1b649eb04 81 endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr);
bogdanm 2:7e489126fe7a 82 }
bogdanm 2:7e489126fe7a 83 return endpoint_structure;
bogdanm 2:7e489126fe7a 84 }
bogdanm 2:7e489126fe7a 85
bogdanm 2:7e489126fe7a 86 void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure)
bogdanm 2:7e489126fe7a 87 {
bogdanm 2:7e489126fe7a 88 if (*endpoint_structure)
bogdanm 2:7e489126fe7a 89 {
bogdanm 2:7e489126fe7a 90 nsdl_free(*endpoint_structure);
bogdanm 2:7e489126fe7a 91 *endpoint_structure = NULL;
bogdanm 2:7e489126fe7a 92 }
bogdanm 2:7e489126fe7a 93 }
bogdanm 2:7e489126fe7a 94
bogdanm 2:7e489126fe7a 95 static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr)
bogdanm 2:7e489126fe7a 96 {
zdshelby 9:ccb9e53d5471 97 printf("TX callback!\n\rSending %d bytes\r\n", data_len);
bogdanm 2:7e489126fe7a 98
bogdanm 2:7e489126fe7a 99 if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len)
zdshelby 9:ccb9e53d5471 100 printf("sending failed\n\r");
bogdanm 2:7e489126fe7a 101
bogdanm 2:7e489126fe7a 102 return 1;
bogdanm 2:7e489126fe7a 103 }
bogdanm 2:7e489126fe7a 104
bogdanm 2:7e489126fe7a 105 static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
bogdanm 2:7e489126fe7a 106 {
zdshelby 9:ccb9e53d5471 107 printf("RX callback!\r\n");
bogdanm 2:7e489126fe7a 108 return 0;
bogdanm 2:7e489126fe7a 109 }
bogdanm 2:7e489126fe7a 110
bogdanm 2:7e489126fe7a 111 static void registration_update_thread(void const *args)
bogdanm 2:7e489126fe7a 112 {
bogdanm 2:7e489126fe7a 113 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
bogdanm 2:7e489126fe7a 114
bogdanm 2:7e489126fe7a 115 while(true)
bogdanm 2:7e489126fe7a 116 {
bogdanm 2:7e489126fe7a 117 wait(RD_UPDATE_PERIOD);
bogdanm 2:7e489126fe7a 118 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
bogdanm 2:7e489126fe7a 119 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
zdshelby 9:ccb9e53d5471 120 printf("NSP re-registering failed\r\n");
bogdanm 2:7e489126fe7a 121 else
zdshelby 9:ccb9e53d5471 122 printf("NSP re-registering OK\r\n");
bogdanm 2:7e489126fe7a 123 nsdl_clean_register_endpoint(&endpoint_ptr);
bogdanm 2:7e489126fe7a 124 }
bogdanm 2:7e489126fe7a 125 }
bogdanm 2:7e489126fe7a 126
bogdanm 2:7e489126fe7a 127 void nsdl_init()
bogdanm 2:7e489126fe7a 128 {
bogdanm 2:7e489126fe7a 129 uint8_t nsp_addr[4];
bogdanm 2:7e489126fe7a 130 sn_nsdl_mem_s memory_cbs;
bogdanm 2:7e489126fe7a 131
bogdanm 2:7e489126fe7a 132 /* Initialize libNsdl */
bogdanm 2:7e489126fe7a 133 memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
bogdanm 2:7e489126fe7a 134 memory_cbs.sn_nsdl_free = &nsdl_free;
bogdanm 2:7e489126fe7a 135 if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1)
zdshelby 9:ccb9e53d5471 136 printf("libNsdl init failed\r\n");
bogdanm 2:7e489126fe7a 137 else
zdshelby 9:ccb9e53d5471 138 printf("libNsdl init done\r\n");
bogdanm 2:7e489126fe7a 139
bogdanm 2:7e489126fe7a 140 /* Set nsp address for library */
bogdanm 2:7e489126fe7a 141 set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4);
bogdanm 2:7e489126fe7a 142 }
bogdanm 2:7e489126fe7a 143
bogdanm 2:7e489126fe7a 144 void nsdl_event_loop()
bogdanm 2:7e489126fe7a 145 {
bogdanm 2:7e489126fe7a 146 Thread registration_thread(registration_update_thread);
bogdanm 2:7e489126fe7a 147
bogdanm 2:7e489126fe7a 148 sn_nsdl_addr_s received_packet_address;
bogdanm 2:7e489126fe7a 149 uint8_t received_address[4];
bogdanm 2:7e489126fe7a 150 char buffer[1024];
bogdanm 2:7e489126fe7a 151 Endpoint from;
bogdanm 2:7e489126fe7a 152
bogdanm 2:7e489126fe7a 153 memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
bogdanm 2:7e489126fe7a 154 received_packet_address.addr_ptr = received_address;
bogdanm 2:7e489126fe7a 155
bogdanm 2:7e489126fe7a 156 while(1)
bogdanm 2:7e489126fe7a 157 {
bogdanm 2:7e489126fe7a 158 int n = server.receiveFrom(from, buffer, sizeof(buffer));
bogdanm 2:7e489126fe7a 159 if (n < 0)
bogdanm 2:7e489126fe7a 160 {
zdshelby 9:ccb9e53d5471 161 printf("Socket error\n\r");
bogdanm 2:7e489126fe7a 162 }
bogdanm 2:7e489126fe7a 163 else
bogdanm 2:7e489126fe7a 164 {
zdshelby 9:ccb9e53d5471 165 printf("Received %d bytes\r\n", n);
bogdanm 2:7e489126fe7a 166 sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address);
bogdanm 2:7e489126fe7a 167 }
bogdanm 2:7e489126fe7a 168 }
bogdanm 2:7e489126fe7a 169 }
bogdanm 2:7e489126fe7a 170
bogdanm 2:7e489126fe7a 171