Doug Anson / mbedEndpointNetwork_BLE

Dependencies:   libnsdl_m0 BLE_API Base64 nRF51822 SplitterAssembler

Committer:
ansond
Date:
Fri Sep 04 21:04:08 2015 +0000
Revision:
35:71eb3663ecbd
Parent:
33:4f6929e123f2
Child:
36:aa73681951ad
update for android 5.x. A wait() call is necessary for 5.x prior to write().

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 5:9233e88b9c83 34 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 5:9233e88b9c83 43 uint8_t response[2];
ansond 6:98af441fd960 44 memset(response,0,2);
ansond 35:71eb3663ecbd 45 //DBG("ble_rpc_open_udp_socket: UDP Socket open() dispatching...\r\n");
ansond 11:d601b867b297 46 if (__rpc->dispatch(SOCKET_OPEN_FN,(void *)cb,response,2,"%s %d",ip_address,port) > 0) {
ansond 35:71eb3663ecbd 47 //DBG("ble_rpc_open_udp_socket: UDP Socket open() dispatched SUCCESSFULLY\r\n");
ansond 6:98af441fd960 48 return true;
ansond 5:9233e88b9c83 49 }
ansond 6:98af441fd960 50
ansond 6:98af441fd960 51 // failure
ansond 35:71eb3663ecbd 52 //DBG("ble_rpc_open_udp_socket: UDP Socket open() dispatch failed\r\n");
ansond 6:98af441fd960 53 return false;
ansond 5:9233e88b9c83 54 }
ansond 5:9233e88b9c83 55
ansond 5:9233e88b9c83 56 bool ble_rpc_close_udp_socket(void)
ansond 5:9233e88b9c83 57 {
ansond 5:9233e88b9c83 58 uint8_t response[2];
ansond 6:98af441fd960 59 memset(response,0,2);
ansond 35:71eb3663ecbd 60 //DBG("ble_rpc_open_udp_socket: UDP Socket close() dispatched successfully. Waiting on response...\r\n");
ansond 33:4f6929e123f2 61 if (__rpc->dispatch(SOCKET_CLOSE_FN,NULL,response,2,"%s","loc") > 0) {
ansond 35:71eb3663ecbd 62 //DBG("ble_rpc_close_udp_socket: UDP Socket close() dispatched SUCCESSFULLY\r\n");
ansond 6:98af441fd960 63 return true;
ansond 5:9233e88b9c83 64 }
ansond 6:98af441fd960 65
ansond 6:98af441fd960 66 // failure
ansond 35:71eb3663ecbd 67 //DBG("ble_rpc_close_udp_socket: UDP Socket close() dispatch failed\r\n");
ansond 6:98af441fd960 68 return false;
ansond 5:9233e88b9c83 69 }
ansond 5:9233e88b9c83 70
ansond 6:98af441fd960 71 int ble_rpc_send_data(uint8_t *data,int data_length)
ansond 5:9233e88b9c83 72 {
ansond 6:98af441fd960 73 Base64 b64;
ansond 5:9233e88b9c83 74 uint8_t response[2];
ansond 6:98af441fd960 75 memset(response,0,2);
ansond 5:9233e88b9c83 76 int base64_data_length = MAX_ARGUMENT_LENGTH;
ansond 16:fb9c3f2af2df 77 //DBG("ble_rpc_send_data: base64 encoding data...\r\n");
ansond 5:9233e88b9c83 78 char *base64_data = b64.Encode((char *)data,data_length,(std::size_t *)&base64_data_length);
ansond 11:d601b867b297 79 //DBG("ble_rpc_send_data: sending data=[%s] length=%d...\r\n",base64_data,strlen((char *)base64_data));
ansond 11:d601b867b297 80 int sent_length = __rpc->dispatch(SEND_DATA_FN,NULL,response,2,"%s",base64_data);
ansond 16:fb9c3f2af2df 81 //DBG("ble_rpc_send_data: dispatched %d bytes\r\n",sent_length);
ansond 5:9233e88b9c83 82 if (base64_data != NULL) free(base64_data);
ansond 6:98af441fd960 83 return sent_length;
ansond 5:9233e88b9c83 84 }
ansond 5:9233e88b9c83 85
ansond 33:4f6929e123f2 86 int ble_rpc_get_location(BLELocation *location)
ansond 33:4f6929e123f2 87 {
ansond 33:4f6929e123f2 88 uint8_t response[2];
ansond 33:4f6929e123f2 89 memset(response,0,2);
ansond 33:4f6929e123f2 90 __rpc->setLocationInstance(location);
ansond 33:4f6929e123f2 91 return __rpc->dispatch(GET_LOCATION_FN,NULL,response,2,"%s","");
ansond 33:4f6929e123f2 92 }
ansond 33:4f6929e123f2 93
ansond 9:bf0cf5828378 94 int ble_rpc_recv_packet(uint8_t *buffer,int buffer_length)
ansond 9:bf0cf5828378 95 {
ansond 15:3c021e52addd 96 //DBG("ble_rpc_recv_packet: checking for a local dispatch...\r\n");
ansond 9:bf0cf5828378 97 if (__rpc->localDispatchAvailable()) {
ansond 16:fb9c3f2af2df 98 //DBG("ble_rpc_recv_packet: local dispatch is ready. Retrieving...\r\n");
ansond 9:bf0cf5828378 99 return __rpc->retrieveLocalDispatch(buffer,buffer_length);
ansond 9:bf0cf5828378 100 }
ansond 9:bf0cf5828378 101 return 0;
ansond 9:bf0cf5828378 102 }
ansond 9:bf0cf5828378 103
ansond 5:9233e88b9c83 104 int ble_rpc_recv_data(uint8_t *buffer,int buffer_length)
ansond 5:9233e88b9c83 105 {
ansond 16:fb9c3f2af2df 106 //DBG("ble_rpc_recv_data: got data...\r\n");
ansond 9:bf0cf5828378 107 if (__rpc->accumulate(buffer,buffer_length)) {
ansond 16:fb9c3f2af2df 108 //DBG("ble_rpc_recv_data: received entire packet.. dispatching locally...\r\n");
ansond 9:bf0cf5828378 109 __rpc->dispatch();
ansond 5:9233e88b9c83 110 }
ansond 9:bf0cf5828378 111 return buffer_length;
ansond 5:9233e88b9c83 112 }
ansond 5:9233e88b9c83 113