Doug Anson / mbedEndpointNetwork_BLE

Dependencies:   libnsdl_m0 BLE_API Base64 nRF51822 SplitterAssembler

Committer:
ansond
Date:
Mon Sep 07 04:51:43 2015 +0000
Revision:
36:aa73681951ad
Parent:
35:71eb3663ecbd
tweaks for android 5.x and not breaking iOS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 5:9233e88b9c83 1 /**
ansond 5:9233e88b9c83 2 * @file UartRPCFunctions.cpp
ansond 5:9233e88b9c83 3 * @brief BLE UART RPC stubs implementation
ansond 5:9233e88b9c83 4 * @author Doug Anson
ansond 5:9233e88b9c83 5 * @version 1.0
ansond 5:9233e88b9c83 6 * @see
ansond 5:9233e88b9c83 7 *
ansond 5:9233e88b9c83 8 * Copyright (c) 2014
ansond 5:9233e88b9c83 9 *
ansond 5:9233e88b9c83 10 * Licensed under the Apache License, Version 2.0 (the "License");
ansond 5:9233e88b9c83 11 * you may not use this file except in compliance with the License.
ansond 5:9233e88b9c83 12 * You may obtain a copy of the License at
ansond 5:9233e88b9c83 13 *
ansond 5:9233e88b9c83 14 * http://www.apache.org/licenses/LICENSE-2.0
ansond 5:9233e88b9c83 15 *
ansond 5:9233e88b9c83 16 * Unless required by applicable law or agreed to in writing, software
ansond 5:9233e88b9c83 17 * distributed under the License is distributed on an "AS IS" BASIS,
ansond 5:9233e88b9c83 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ansond 5:9233e88b9c83 19 * See the License for the specific language governing permissions and
ansond 5:9233e88b9c83 20 * limitations under the License.
ansond 5:9233e88b9c83 21 */
ansond 5:9233e88b9c83 22
ansond 5:9233e88b9c83 23 #include "UartRPCFunctions.h"
ansond 5:9233e88b9c83 24 #include "UartRPC.h"
ansond 5:9233e88b9c83 25
ansond 5:9233e88b9c83 26
ansond 5:9233e88b9c83 27 #include "Base64.h"
ansond 5:9233e88b9c83 28
ansond 5:9233e88b9c83 29 #ifdef DBG
ansond 5:9233e88b9c83 30 #undef DBG
ansond 5:9233e88b9c83 31 #endif
ansond 5:9233e88b9c83 32 #define DBG std::printf
ansond 5:9233e88b9c83 33
ansond 36:aa73681951ad 34 static UartRPC *__rpc = NULL;
ansond 5:9233e88b9c83 35
ansond 5:9233e88b9c83 36 void ble_rpc_init(BLEDevice &ble)
ansond 5:9233e88b9c83 37 {
ansond 5:9233e88b9c83 38 if (__rpc == NULL) __rpc = new UartRPC(ble);
ansond 5:9233e88b9c83 39 }
ansond 5:9233e88b9c83 40
ansond 11:d601b867b297 41 bool ble_rpc_open_udp_socket(char *ip_address,int port,ble_dispatch_callback_fn cb)
ansond 5:9233e88b9c83 42 {
ansond 36:aa73681951ad 43 bool status = false;
ansond 5:9233e88b9c83 44 uint8_t response[2];
ansond 6:98af441fd960 45 memset(response,0,2);
ansond 36:aa73681951ad 46
ansond 35:71eb3663ecbd 47 //DBG("ble_rpc_open_udp_socket: UDP Socket open() dispatching...\r\n");
ansond 11:d601b867b297 48 if (__rpc->dispatch(SOCKET_OPEN_FN,(void *)cb,response,2,"%s %d",ip_address,port) > 0) {
ansond 35:71eb3663ecbd 49 //DBG("ble_rpc_open_udp_socket: UDP Socket open() dispatched SUCCESSFULLY\r\n");
ansond 36:aa73681951ad 50 status = true;
ansond 5:9233e88b9c83 51 }
ansond 6:98af441fd960 52
ansond 6:98af441fd960 53 // failure
ansond 35:71eb3663ecbd 54 //DBG("ble_rpc_open_udp_socket: UDP Socket open() dispatch failed\r\n");
ansond 36:aa73681951ad 55 return status;
ansond 5:9233e88b9c83 56 }
ansond 5:9233e88b9c83 57
ansond 5:9233e88b9c83 58 bool ble_rpc_close_udp_socket(void)
ansond 5:9233e88b9c83 59 {
ansond 36:aa73681951ad 60 bool status = false;
ansond 5:9233e88b9c83 61 uint8_t response[2];
ansond 6:98af441fd960 62 memset(response,0,2);
ansond 36:aa73681951ad 63
ansond 35:71eb3663ecbd 64 //DBG("ble_rpc_open_udp_socket: UDP Socket close() dispatched successfully. Waiting on response...\r\n");
ansond 33:4f6929e123f2 65 if (__rpc->dispatch(SOCKET_CLOSE_FN,NULL,response,2,"%s","loc") > 0) {
ansond 35:71eb3663ecbd 66 //DBG("ble_rpc_close_udp_socket: UDP Socket close() dispatched SUCCESSFULLY\r\n");
ansond 36:aa73681951ad 67 status = true;
ansond 5:9233e88b9c83 68 }
ansond 6:98af441fd960 69
ansond 6:98af441fd960 70 // failure
ansond 35:71eb3663ecbd 71 //DBG("ble_rpc_close_udp_socket: UDP Socket close() dispatch failed\r\n");
ansond 36:aa73681951ad 72 return status;
ansond 5:9233e88b9c83 73 }
ansond 5:9233e88b9c83 74
ansond 6:98af441fd960 75 int ble_rpc_send_data(uint8_t *data,int data_length)
ansond 5:9233e88b9c83 76 {
ansond 6:98af441fd960 77 Base64 b64;
ansond 5:9233e88b9c83 78 uint8_t response[2];
ansond 6:98af441fd960 79 memset(response,0,2);
ansond 5:9233e88b9c83 80 int base64_data_length = MAX_ARGUMENT_LENGTH;
ansond 16:fb9c3f2af2df 81 //DBG("ble_rpc_send_data: base64 encoding data...\r\n");
ansond 5:9233e88b9c83 82 char *base64_data = b64.Encode((char *)data,data_length,(std::size_t *)&base64_data_length);
ansond 11:d601b867b297 83 //DBG("ble_rpc_send_data: sending data=[%s] length=%d...\r\n",base64_data,strlen((char *)base64_data));
ansond 11:d601b867b297 84 int sent_length = __rpc->dispatch(SEND_DATA_FN,NULL,response,2,"%s",base64_data);
ansond 16:fb9c3f2af2df 85 //DBG("ble_rpc_send_data: dispatched %d bytes\r\n",sent_length);
ansond 5:9233e88b9c83 86 if (base64_data != NULL) free(base64_data);
ansond 6:98af441fd960 87 return sent_length;
ansond 5:9233e88b9c83 88 }
ansond 5:9233e88b9c83 89
ansond 33:4f6929e123f2 90 int ble_rpc_get_location(BLELocation *location)
ansond 33:4f6929e123f2 91 {
ansond 33:4f6929e123f2 92 uint8_t response[2];
ansond 33:4f6929e123f2 93 memset(response,0,2);
ansond 33:4f6929e123f2 94 __rpc->setLocationInstance(location);
ansond 33:4f6929e123f2 95 return __rpc->dispatch(GET_LOCATION_FN,NULL,response,2,"%s","");
ansond 33:4f6929e123f2 96 }
ansond 33:4f6929e123f2 97
ansond 9:bf0cf5828378 98 int ble_rpc_recv_packet(uint8_t *buffer,int buffer_length)
ansond 36:aa73681951ad 99 {
ansond 36:aa73681951ad 100 int n = 0;
ansond 15:3c021e52addd 101 //DBG("ble_rpc_recv_packet: checking for a local dispatch...\r\n");
ansond 9:bf0cf5828378 102 if (__rpc->localDispatchAvailable()) {
ansond 16:fb9c3f2af2df 103 //DBG("ble_rpc_recv_packet: local dispatch is ready. Retrieving...\r\n");
ansond 36:aa73681951ad 104 n = __rpc->retrieveLocalDispatch(buffer,buffer_length);
ansond 9:bf0cf5828378 105 }
ansond 36:aa73681951ad 106 return n;
ansond 9:bf0cf5828378 107 }
ansond 9:bf0cf5828378 108
ansond 5:9233e88b9c83 109 int ble_rpc_recv_data(uint8_t *buffer,int buffer_length)
ansond 5:9233e88b9c83 110 {
ansond 16:fb9c3f2af2df 111 //DBG("ble_rpc_recv_data: got data...\r\n");
ansond 9:bf0cf5828378 112 if (__rpc->accumulate(buffer,buffer_length)) {
ansond 16:fb9c3f2af2df 113 //DBG("ble_rpc_recv_data: received entire packet.. dispatching locally...\r\n");
ansond 9:bf0cf5828378 114 __rpc->dispatch();
ansond 5:9233e88b9c83 115 }
ansond 9:bf0cf5828378 116 return buffer_length;
ansond 5:9233e88b9c83 117 }
ansond 5:9233e88b9c83 118