Doug Anson / mbedEndpointNetwork_BLE

Dependencies:   libnsdl_m0 BLE_API Base64 nRF51822 SplitterAssembler

bt_network/BleUartRPC/UartRPCFunctions.cpp

Committer:
ansond
Date:
2015-02-17
Revision:
6:98af441fd960
Parent:
5:9233e88b9c83
Child:
9:bf0cf5828378

File content as of revision 6:98af441fd960:

/**
 * @file    UartRPCFunctions.cpp
 * @brief   BLE UART RPC stubs implementation
 * @author  Doug Anson
 * @version 1.0
 * @see     
 *
 * Copyright (c) 2014
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
 #include "UartRPCFunctions.h"
 #include "UartRPC.h"
 
 
 #include "Base64.h"
 
 #ifdef DBG
    #undef DBG
 #endif
 #define DBG  std::printf
 
 typedef enum {
     SOCKET_OPEN_FN  = 0x01,
     SOCKET_CLOSE_FN = 0x02,
     SEND_DATA_FN    = 0x04,
     RECV_DATA_FN    = 0x08,
     NUM_FNs         = 4
 } FunctionIDs;
 
 UartRPC *__rpc = NULL;
 
 void ble_rpc_init(BLEDevice &ble) 
 {
     if (__rpc == NULL) __rpc = new UartRPC(ble);
 }
 
 bool ble_rpc_open_udp_socket(char *ip_address,int port) 
 {
     uint8_t response[2];
     memset(response,0,2);
     if (__rpc->dispatch(SOCKET_OPEN_FN,response,2,"%s %d",ip_address,port) > 0) {
         DBG("ble_rpc_open_udp_socket: success...\r\n");
         return true;
     }
     
     // failure
     DBG("ble_rpc_open_udp_socket: dispatch() failed\r\n");
     return false;
 }
 
 bool ble_rpc_close_udp_socket(void) 
 {
     uint8_t response[2];
     memset(response,0,2);
     if (__rpc->dispatch(SOCKET_CLOSE_FN,response,2,"%s","") > 0) {
         DBG("ble_rpc_close_udp_socket: success...\r\n");
         return true;
     }
     
     // failure
     DBG("ble_rpc_close_udp_socket: dispatch() failed\r\n");
     return false;
 }
 
 int ble_rpc_send_data(uint8_t *data,int data_length) 
 {
     Base64 b64;
     uint8_t response[2];
     memset(response,0,2);
     int base64_data_length = MAX_ARGUMENT_LENGTH;
     DBG("ble_rpc_send_data: base64 encoding data...\r\n");
     char *base64_data = b64.Encode((char *)data,data_length,(std::size_t *)&base64_data_length);
     DBG("ble_rpc_send_data: sending data=[%s] length=%d...\r\n",base64_data,strlen((char *)base64_data));
     int sent_length = __rpc->dispatch(SEND_DATA_FN,response,2,"%s",base64_data);
     DBG("ble_rpc_send_data: dispatched %d bytes\r\n",sent_length); 
     if (base64_data != NULL) free(base64_data);
     return sent_length;
 }
 
 int ble_rpc_recv_data(uint8_t *buffer,int buffer_length)
 {
     /*
     uint8_t base64_buffer[MAX_RESULT_LENGTH+1];
     memset(base64_buffer,0,MAX_RESULT_LENGTH+1);
     DBG("ble_rpc_recv_data: seeing if any data is available...\r\n");
     int recv_length =__rpc->dispatch(RECV_DATA_FN,base64_buffer,MAX_RESULT_LENGTH,"%s","");
     DBG("ble_rpc_recv_data: received %d bytes of data...\r\n",recv_length);
     if (recv_length > 0) {
         // success
         Base64 b64;
         int base64_length = buffer_length;
         char *raw_buffer = b64.Decode((char *)base64_buffer,strlen((char *)base64_buffer),(std::size_t *)&base64_length);
         memcpy(buffer,raw_buffer,base64_length);
         if (raw_buffer != NULL) free(raw_buffer);
         return base64_length;
     }
     else {
         // failure
         DBG("ble_rpc_recv_data: dispatch() failed\r\n");
     }
     */
     return -1;
 }