Project with example IPSO resources for LED bar, Gas Sensor, and Light Sensor

Dependencies:   EthernetInterface LED_Bar mbed-rtos mbed nsdl_lib

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 #include "UDPSocket.h"
00009 #include "Endpoint.h"
00010 
00011 /*    __heapvalid((__heapprt)fprintf,stdout, 1);\*/
00012 #define MEM_VALID(x) \
00013     int s##x=0;\
00014     int *h##x = new int [1];\
00015     std::printf("[stack]0x%08x\t[heap]0x%08x\t[memory avail]%d bytes \tLine: %d %s\r\n", &s##x, h##x, &s##x-h##x, __LINE__, __FILE__);\
00016     if (h##x > &s##x)\
00017     printf("collision\n");\
00018     else\
00019     delete [] h##x;\
00020     __nop()
00021 
00022 extern Serial pc;
00023 extern Endpoint nsp;
00024 extern UDPSocket server;
00025 extern char endpoint_name[24];
00026 extern uint8_t ep_type[];
00027 extern uint8_t lifetime_ptr[];
00028 char null_ep_name[] = "";
00029 uint8_t null_ep_type[] = "";
00030 uint8_t null_lifetime_ptr[] = "60";
00031 bool nsdl_reg_update_needed = false;
00032 
00033 /* The number of seconds between NSP registration messages */
00034 #define RD_UPDATE_PERIOD  30
00035 
00036 void *nsdl_alloc(uint16_t size)
00037 {
00038     void *buf = malloc(size);
00039 //    printf("alloc\r\n");
00040 //    MEM_VALID(0);
00041     return buf;
00042 }
00043 
00044 void nsdl_free(void* ptr_to_free)
00045 {
00046     free(ptr_to_free);
00047 //    printf("de-alloc\r\n");
00048 //    MEM_VALID(0);
00049 }
00050 
00051 /*
00052  * Create a static resoure
00053  * Only get is allowed
00054  */
00055 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)
00056 {
00057     resource_structure->access = SN_GRS_GET_ALLOWED;
00058     resource_structure->mode = SN_GRS_STATIC;
00059     resource_structure->pathlen = pt_len;
00060     resource_structure->path = pt;
00061     resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
00062     resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
00063     resource_structure->resource = rsc;
00064     resource_structure->resourcelen = rsc_len;
00065     sn_nsdl_create_resource(resource_structure);
00066 }
00067 
00068 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)
00069 {
00070     resource_structure->access = (sn_grs_resource_acl_e)access_right;
00071     resource_structure->resource = 0;
00072     resource_structure->resourcelen = 0;
00073     resource_structure->sn_grs_dyn_res_callback = callback_ptr;
00074     resource_structure->mode = SN_GRS_DYNAMIC;
00075     resource_structure->pathlen = pt_len;
00076     resource_structure->path = pt;
00077     resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
00078     resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
00079     resource_structure->resource_parameters_ptr->observable = is_observable;
00080     sn_nsdl_create_resource(resource_structure);
00081 }
00082 
00083 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)
00084 {
00085     if (NULL == endpoint_structure)
00086     {   
00087         endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
00088     }
00089     if (endpoint_structure)
00090     {
00091         memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s));
00092         endpoint_structure->endpoint_name_ptr = name;
00093         endpoint_structure->endpoint_name_len = strlen((char*)name);
00094         endpoint_structure->type_ptr = typename_ptr;
00095         endpoint_structure->type_len =  strlen((char*)typename_ptr);
00096         endpoint_structure->lifetime_ptr = lifetime_ptr;
00097         endpoint_structure->lifetime_len =  strlen((char*)lifetime_ptr); 
00098     }
00099     return endpoint_structure;
00100 }
00101 
00102 void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure)
00103 {
00104     if (*endpoint_structure)
00105     {
00106         nsdl_free(*endpoint_structure);
00107         *endpoint_structure = NULL;
00108     }
00109 }
00110 
00111 static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr)
00112 {
00113     pc.printf("TX callback!\n\rSending %d bytes\r\n", data_len);
00114 
00115     if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len)
00116         pc.printf("sending failed\n\r");
00117 
00118     return 1;
00119 }
00120 
00121 static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
00122 {
00123     pc.printf("RX callback!\r\n");
00124     pc.printf("msg_code: %d \r\n", coap_packet_ptr->msg_code);
00125     pc.printf("Payload length: %d bytes\r\n", coap_packet_ptr->payload_len);
00126     int i;
00127     pc.printf("Payload:'");
00128     for (i=0; i < coap_packet_ptr->payload_len; i++)
00129         pc.printf("%c", *(coap_packet_ptr->payload_ptr + i));
00130     pc.printf("' \r\n");
00131     if (coap_packet_ptr->options_list_ptr && coap_packet_ptr->options_list_ptr->location_path_ptr)
00132     {
00133         pc.printf("Location: /");
00134         int i;
00135         for (i=0; i < coap_packet_ptr->options_list_ptr->location_path_len; i++) pc.printf("%c", (char)(coap_packet_ptr->options_list_ptr->location_path_ptr[i]));
00136         pc.printf(" \r\n");
00137     }
00138     //sn_coap_packet_debug(coap_packet_ptr);
00139     return 0;
00140 }
00141 
00142 static void registration_update_thread(void const *args)
00143 {
00144     sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
00145 
00146     while(true)
00147     {
00148         wait(RD_UPDATE_PERIOD);
00149         endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)null_ep_name, null_ep_type, null_lifetime_ptr);
00150         if(sn_nsdl_update_registration(endpoint_ptr) != 0)            
00151             pc.printf("NSP re-registering failed\r\n");
00152         else
00153             pc.printf("NSP re-registering OK\r\n");
00154         nsdl_clean_register_endpoint(&endpoint_ptr);
00155     }
00156 }
00157 
00158 void nsdl_init()
00159 {
00160     uint8_t nsp_addr[4];
00161     sn_nsdl_mem_s memory_cbs;
00162 
00163     /* Initialize libNsdl */
00164     memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
00165     memory_cbs.sn_nsdl_free = &nsdl_free;
00166     if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1)
00167         pc.printf("libNsdl init failed\r\n");
00168     else
00169         pc.printf("libNsdl init done\r\n");
00170 
00171     /* Set nsp address for library */
00172     set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4);
00173 }   
00174 
00175 void nsdl_event_loop()
00176 {
00177     Thread registration_thread(registration_update_thread);
00178 
00179     sn_nsdl_addr_s received_packet_address;
00180     uint8_t received_address[4];
00181     char buffer[1024];
00182     Endpoint from;
00183 
00184     memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
00185     received_packet_address.addr_ptr = received_address;
00186 
00187     while(1)
00188     {
00189         int n = server.receiveFrom(from, buffer, sizeof(buffer));
00190         if (n < 0)
00191         {
00192             pc.printf("Socket error\n\r");
00193         }
00194         else if (n > 0)
00195         {   
00196             pc.printf("Received %d bytes\r\n", n);
00197             sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address);
00198         }
00199     }
00200 }
00201 
00202