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

Committer:
pnysten
Date:
Tue Oct 27 07:18:16 2015 +0000
Revision:
22:2fab757f9c2a
Parent:
21:978281bfb26e
Firmware and 1/0/8 resource

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