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