A metronome using the FRDM K64F board

Committer:
ram54288
Date:
Sun May 14 18:40:18 2017 +0000
Revision:
0:a7a43371b306
Initial commit

Who changed what in which revision?

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