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