NanoService Example for u-blox Cellular modems

Dependencies:   Beep LM75B MMA7660 mbed nsdl_lib

Fork of NSDL_HelloWorld by Sensinode

Committer:
sam_grove
Date:
Thu Oct 31 20:29:30 2013 +0000
Revision:
11:3b7ae478dcd1
Parent:
10:4cb556c7845e
Tested and working with ublox LISA C200 CDMA modem

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