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