NanoService Example for u-blox Cellular modems

Dependencies:   Beep LM75B MMA7660 mbed nsdl_lib

Fork of NSDL_HelloWorld by Sensinode

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nsdl_support.cpp Source File

nsdl_support.cpp

00001 // NSDL library support functions
00002 
00003 #include "mbed.h"
00004 #include "nsdl_support.h"
00005 #include "mbed.h"
00006 #include "rtos.h"
00007 //#include "UbloxUSBGSMModem.h"
00008 //#include "UbloxUSBCDMAModem.h"
00009 //#include "CellularModem.h"
00010 #include "Socket.h"
00011 #include "Endpoint.h"
00012 #include "UDPSocket.h"
00013 
00014 //extern UbloxUSBGSMModem modem;
00015 extern Endpoint nsp;
00016 extern UDPSocket server;
00017 extern char endpoint_name[16];
00018 extern uint8_t ep_type[];
00019 extern uint8_t lifetime_ptr[];
00020 
00021 /* The number of seconds between NSP registration messages */
00022 #define RD_UPDATE_PERIOD  60
00023 
00024 void *nsdl_alloc(uint16_t size)
00025 {
00026     return malloc(size);
00027 }
00028 
00029 void nsdl_free(void* ptr_to_free)
00030 {
00031     free(ptr_to_free);
00032 }
00033 
00034 /*
00035  * Create a static resoure
00036  * Only get is allowed
00037  */
00038 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)
00039 {
00040     resource_structure->access = SN_GRS_GET_ALLOWED;
00041     resource_structure->mode = SN_GRS_STATIC;
00042     resource_structure->pathlen = pt_len;
00043     resource_structure->path = pt;
00044     resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
00045     resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
00046     resource_structure->resource = rsc;
00047     resource_structure->resourcelen = rsc_len;
00048     sn_nsdl_create_resource(resource_structure);
00049 }
00050 
00051 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)
00052 {
00053     resource_structure->access = (sn_grs_resource_acl_e)access_right;
00054     resource_structure->resource = 0;
00055     resource_structure->resourcelen = 0;
00056     resource_structure->sn_grs_dyn_res_callback = callback_ptr;
00057     resource_structure->mode = SN_GRS_DYNAMIC;
00058     resource_structure->pathlen = pt_len;
00059     resource_structure->path = pt;
00060     resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
00061     resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
00062     resource_structure->resource_parameters_ptr->observable = is_observable;
00063     sn_nsdl_create_resource(resource_structure);
00064 }
00065 
00066 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)
00067 {
00068     if (NULL == endpoint_structure)
00069     {   
00070         endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
00071     }
00072     if (endpoint_structure)
00073     {
00074         memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s));
00075         endpoint_structure->endpoint_name_ptr = name;
00076         endpoint_structure->endpoint_name_len = strlen((char*)name);
00077         endpoint_structure->type_ptr = typename_ptr;
00078         endpoint_structure->type_len =  strlen((char*)typename_ptr);
00079         endpoint_structure->lifetime_ptr = lifetime_ptr;
00080         endpoint_structure->lifetime_len =  strlen((char*)lifetime_ptr);
00081     }
00082     return endpoint_structure;
00083 }
00084 
00085 void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure)
00086 {
00087     if (*endpoint_structure)
00088     {
00089         nsdl_free(*endpoint_structure);
00090         *endpoint_structure = NULL;
00091     }
00092 }
00093 
00094 static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr)
00095 {
00096     printf("TX callback!\n\rSending %d bytes\r\n", data_len);
00097 
00098     if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len)
00099         printf("sending failed\n\r");
00100 
00101     return 1;
00102 }
00103 
00104 static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
00105 {
00106     printf("RX callback!\r\n");
00107     return 0;
00108 }
00109 
00110 static void registration_update_thread(void const *args)
00111 {
00112     sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
00113 
00114     while(true)
00115     {
00116         wait(RD_UPDATE_PERIOD);
00117         endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
00118         if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
00119             printf("NSP re-registering failed\r\n");
00120         else
00121             printf("NSP re-registering OK\r\n");
00122         nsdl_clean_register_endpoint(&endpoint_ptr);
00123     }
00124 }
00125 
00126 void nsdl_init()
00127 {
00128     uint8_t nsp_addr[4];
00129     sn_nsdl_mem_s memory_cbs;
00130 
00131     /* Initialize libNsdl */
00132     memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
00133     memory_cbs.sn_nsdl_free = &nsdl_free;
00134     if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1)
00135         printf("libNsdl init failed\r\n");
00136     else
00137         printf("libNsdl init done\r\n");
00138 
00139     /* Set nsp address for library */
00140     set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4);
00141 }
00142 
00143 void nsdl_event_loop()
00144 {
00145     Thread registration_thread(registration_update_thread);
00146 
00147     sn_nsdl_addr_s received_packet_address;
00148     uint8_t received_address[4];
00149     char buffer[1024];
00150     Endpoint from;
00151 
00152     memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
00153     received_packet_address.addr_ptr = received_address;
00154 
00155     while(1)
00156     {
00157         int n = server.receiveFrom(from, buffer, sizeof(buffer));
00158         if (n < 0)
00159         {
00160             printf("Socket error\n\r");
00161         }
00162         else
00163         {   
00164             printf("Received %d bytes\r\n", n);
00165             sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address);
00166         }
00167     }
00168 }
00169 
00170