Doug Anson / mbedEndpointNetwork_BLE

Dependencies:   libnsdl_m0 BLE_API Base64 nRF51822 SplitterAssembler

Committer:
ansond
Date:
Mon Mar 16 16:10:55 2015 +0000
Revision:
16:fb9c3f2af2df
Parent:
15:3c021e52addd
Child:
33:4f6929e123f2
updates

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