adding resources firmware and 1/0/8

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