Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 0:6c56fb4bc5f0 1 /*
nexpaq 0:6c56fb4bc5f0 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
nexpaq 0:6c56fb4bc5f0 3 * SPDX-License-Identifier: Apache-2.0
nexpaq 0:6c56fb4bc5f0 4 * Licensed under the Apache License, Version 2.0 (the License); you may
nexpaq 0:6c56fb4bc5f0 5 * not use this file except in compliance with the License.
nexpaq 0:6c56fb4bc5f0 6 * You may obtain a copy of the License at
nexpaq 0:6c56fb4bc5f0 7 *
nexpaq 0:6c56fb4bc5f0 8 * http://www.apache.org/licenses/LICENSE-2.0
nexpaq 0:6c56fb4bc5f0 9 *
nexpaq 0:6c56fb4bc5f0 10 * Unless required by applicable law or agreed to in writing, software
nexpaq 0:6c56fb4bc5f0 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
nexpaq 0:6c56fb4bc5f0 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nexpaq 0:6c56fb4bc5f0 13 * See the License for the specific language governing permissions and
nexpaq 0:6c56fb4bc5f0 14 * limitations under the License.
nexpaq 0:6c56fb4bc5f0 15 */
nexpaq 0:6c56fb4bc5f0 16 #include "include/nsdlaccesshelper.h"
nexpaq 0:6c56fb4bc5f0 17 #include "include/m2mnsdlinterface.h"
nexpaq 0:6c56fb4bc5f0 18
nexpaq 0:6c56fb4bc5f0 19 M2MNsdlInterfaceList __nsdl_interface_list;
nexpaq 0:6c56fb4bc5f0 20
nexpaq 0:6c56fb4bc5f0 21 // callback function for NSDL library to call into
nexpaq 0:6c56fb4bc5f0 22
nexpaq 0:6c56fb4bc5f0 23 M2MConnectionHandler *__connection_handler = NULL;
nexpaq 0:6c56fb4bc5f0 24
nexpaq 0:6c56fb4bc5f0 25
nexpaq 0:6c56fb4bc5f0 26 uint8_t __nsdl_c_callback(struct nsdl_s *nsdl_handle,
nexpaq 0:6c56fb4bc5f0 27 sn_coap_hdr_s *received_coap_ptr,
nexpaq 0:6c56fb4bc5f0 28 sn_nsdl_addr_s *address,
nexpaq 0:6c56fb4bc5f0 29 sn_nsdl_capab_e nsdl_capab)
nexpaq 0:6c56fb4bc5f0 30 {
nexpaq 0:6c56fb4bc5f0 31 uint8_t status = 0;
nexpaq 0:6c56fb4bc5f0 32 M2MNsdlInterface *interface = get_interface(nsdl_handle);
nexpaq 0:6c56fb4bc5f0 33 if(interface) {
nexpaq 0:6c56fb4bc5f0 34 status = interface->resource_callback(nsdl_handle,received_coap_ptr,
nexpaq 0:6c56fb4bc5f0 35 address, nsdl_capab);
nexpaq 0:6c56fb4bc5f0 36 // Payload freeing must be done in app level if blockwise message
nexpaq 0:6c56fb4bc5f0 37 if (received_coap_ptr &&
nexpaq 0:6c56fb4bc5f0 38 received_coap_ptr->options_list_ptr &&
nexpaq 0:6c56fb4bc5f0 39 received_coap_ptr->options_list_ptr->block1_len > 0) {
nexpaq 0:6c56fb4bc5f0 40 free(received_coap_ptr->payload_ptr);
nexpaq 0:6c56fb4bc5f0 41 received_coap_ptr->payload_ptr = NULL;
nexpaq 0:6c56fb4bc5f0 42 }
nexpaq 0:6c56fb4bc5f0 43 }
nexpaq 0:6c56fb4bc5f0 44 return status;
nexpaq 0:6c56fb4bc5f0 45 }
nexpaq 0:6c56fb4bc5f0 46
nexpaq 0:6c56fb4bc5f0 47 void* __nsdl_c_memory_alloc(uint16_t size)
nexpaq 0:6c56fb4bc5f0 48 {
nexpaq 0:6c56fb4bc5f0 49 if(size)
nexpaq 0:6c56fb4bc5f0 50 return malloc(size);
nexpaq 0:6c56fb4bc5f0 51 else
nexpaq 0:6c56fb4bc5f0 52 return 0;
nexpaq 0:6c56fb4bc5f0 53 }
nexpaq 0:6c56fb4bc5f0 54
nexpaq 0:6c56fb4bc5f0 55 void __nsdl_c_memory_free(void *ptr)
nexpaq 0:6c56fb4bc5f0 56 {
nexpaq 0:6c56fb4bc5f0 57 if(ptr)
nexpaq 0:6c56fb4bc5f0 58 free(ptr);
nexpaq 0:6c56fb4bc5f0 59 }
nexpaq 0:6c56fb4bc5f0 60
nexpaq 0:6c56fb4bc5f0 61 uint8_t __nsdl_c_send_to_server(struct nsdl_s * nsdl_handle,
nexpaq 0:6c56fb4bc5f0 62 sn_nsdl_capab_e protocol,
nexpaq 0:6c56fb4bc5f0 63 uint8_t *data_ptr,
nexpaq 0:6c56fb4bc5f0 64 uint16_t data_len,
nexpaq 0:6c56fb4bc5f0 65 sn_nsdl_addr_s *address_ptr)
nexpaq 0:6c56fb4bc5f0 66 {
nexpaq 0:6c56fb4bc5f0 67 uint8_t status = 0;
nexpaq 0:6c56fb4bc5f0 68 M2MNsdlInterface *interface = get_interface(nsdl_handle);
nexpaq 0:6c56fb4bc5f0 69 if(interface) {
nexpaq 0:6c56fb4bc5f0 70 status = interface->send_to_server_callback(nsdl_handle,
nexpaq 0:6c56fb4bc5f0 71 protocol, data_ptr,
nexpaq 0:6c56fb4bc5f0 72 data_len, address_ptr);
nexpaq 0:6c56fb4bc5f0 73 }
nexpaq 0:6c56fb4bc5f0 74 return status;
nexpaq 0:6c56fb4bc5f0 75 }
nexpaq 0:6c56fb4bc5f0 76
nexpaq 0:6c56fb4bc5f0 77 uint8_t __nsdl_c_received_from_server(struct nsdl_s * nsdl_handle,
nexpaq 0:6c56fb4bc5f0 78 sn_coap_hdr_s *coap_header,
nexpaq 0:6c56fb4bc5f0 79 sn_nsdl_addr_s *address_ptr)
nexpaq 0:6c56fb4bc5f0 80 {
nexpaq 0:6c56fb4bc5f0 81 uint8_t status = 0;
nexpaq 0:6c56fb4bc5f0 82 M2MNsdlInterface *interface = get_interface(nsdl_handle);
nexpaq 0:6c56fb4bc5f0 83 if(interface) {
nexpaq 0:6c56fb4bc5f0 84 status = interface->received_from_server_callback(nsdl_handle,
nexpaq 0:6c56fb4bc5f0 85 coap_header,
nexpaq 0:6c56fb4bc5f0 86 address_ptr);
nexpaq 0:6c56fb4bc5f0 87 // Payload freeing must be done in app level if blockwise message
nexpaq 0:6c56fb4bc5f0 88 if (coap_header &&
nexpaq 0:6c56fb4bc5f0 89 coap_header->options_list_ptr &&
nexpaq 0:6c56fb4bc5f0 90 coap_header->options_list_ptr->block1_len > 0) {
nexpaq 0:6c56fb4bc5f0 91 free(coap_header->payload_ptr);
nexpaq 0:6c56fb4bc5f0 92 coap_header->payload_ptr = NULL;
nexpaq 0:6c56fb4bc5f0 93 }
nexpaq 0:6c56fb4bc5f0 94 }
nexpaq 0:6c56fb4bc5f0 95 return status;
nexpaq 0:6c56fb4bc5f0 96 }
nexpaq 0:6c56fb4bc5f0 97
nexpaq 0:6c56fb4bc5f0 98 void* __socket_malloc( void * context, size_t size)
nexpaq 0:6c56fb4bc5f0 99 {
nexpaq 0:6c56fb4bc5f0 100 (void) context;
nexpaq 0:6c56fb4bc5f0 101 return malloc(size);
nexpaq 0:6c56fb4bc5f0 102 }
nexpaq 0:6c56fb4bc5f0 103
nexpaq 0:6c56fb4bc5f0 104 void __socket_free(void * context, void * ptr)
nexpaq 0:6c56fb4bc5f0 105 {
nexpaq 0:6c56fb4bc5f0 106 (void) context;
nexpaq 0:6c56fb4bc5f0 107 free(ptr);
nexpaq 0:6c56fb4bc5f0 108 }
nexpaq 0:6c56fb4bc5f0 109
nexpaq 0:6c56fb4bc5f0 110 M2MNsdlInterface* get_interface(struct nsdl_s* nsdl_handle)
nexpaq 0:6c56fb4bc5f0 111 {
nexpaq 0:6c56fb4bc5f0 112 M2MNsdlInterfaceList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 113 it = __nsdl_interface_list.begin();
nexpaq 0:6c56fb4bc5f0 114 M2MNsdlInterface* obj = NULL;
nexpaq 0:6c56fb4bc5f0 115 if (nsdl_handle) {
nexpaq 0:6c56fb4bc5f0 116 for (; it!=__nsdl_interface_list.end(); it++) {
nexpaq 0:6c56fb4bc5f0 117 if ((*it)->get_nsdl_handle() == nsdl_handle) {
nexpaq 0:6c56fb4bc5f0 118 obj = *it;
nexpaq 0:6c56fb4bc5f0 119 break;
nexpaq 0:6c56fb4bc5f0 120 }
nexpaq 0:6c56fb4bc5f0 121 }
nexpaq 0:6c56fb4bc5f0 122 }
nexpaq 0:6c56fb4bc5f0 123 return obj;
nexpaq 0:6c56fb4bc5f0 124 }
nexpaq 0:6c56fb4bc5f0 125
nexpaq 0:6c56fb4bc5f0 126 void __mutex_claim()
nexpaq 0:6c56fb4bc5f0 127 {
nexpaq 0:6c56fb4bc5f0 128 if(__connection_handler) {
nexpaq 0:6c56fb4bc5f0 129 __connection_handler->claim_mutex();
nexpaq 0:6c56fb4bc5f0 130 }
nexpaq 0:6c56fb4bc5f0 131 }
nexpaq 0:6c56fb4bc5f0 132
nexpaq 0:6c56fb4bc5f0 133 void __mutex_release()
nexpaq 0:6c56fb4bc5f0 134 {
nexpaq 0:6c56fb4bc5f0 135 if(__connection_handler) {
nexpaq 0:6c56fb4bc5f0 136 __connection_handler->release_mutex();
nexpaq 0:6c56fb4bc5f0 137 }
nexpaq 0:6c56fb4bc5f0 138 }