Red Hat Summit NanoService Demo for LPC1768 App Board using OMA Lightweight Objects

Dependencies:   Beep C12832_lcd EthernetInterface LM75B MMA7660 mbed-rtos mbed nsdl_lib

Fork of LWM2M_NanoService_Ethernet by MBED_DEMOS

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