Doug Anson / mbedEndpointNetwork_BLE

Dependencies:   libnsdl_m0 BLE_API Base64 nRF51822 SplitterAssembler

bt_network/BleUartRPC/UartRPCFunctions.cpp

Committer:
ansond
Date:
2015-02-16
Revision:
5:9233e88b9c83
Child:
6:98af441fd960

File content as of revision 5:9233e88b9c83:

/**
 * @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) 
 {
     bool success = false;
     uint8_t response[2];
     memset(response,0,sizeof(response));
     if (__rpc->dispatch(SOCKET_OPEN_FN,response,sizeof((char *)response),"%s %d",ip_address,port)) {
         // success
         int reply = 0;
         scanf((char *)response,"%d",&reply);
         if (reply == 1) success = true;
     }
     else {
         // failure
         DBG("ble_rpc_open_udp_socket: dispatch() failed\r\n");
     }
     return success;
 }
 
 bool ble_rpc_close_udp_socket(void) 
 {
     bool success = false;
     uint8_t response[2];
     memset(response,0,sizeof(response));
     if (__rpc->dispatch(SOCKET_CLOSE_FN,response,sizeof((char *)response),"%s","")) {
         // success
         int reply = 0;
         scanf((char *)response,"%d",&reply);
         if (reply == 1) success = true;
     }
     else {
         // failure
         DBG("ble_rpc_close_udp_socket: dispatch() failed\r\n");
     }
     return success;
 }
 
 bool ble_rpc_send_data(uint8_t *data,int data_length) 
 {
     bool success = false;
     uint8_t response[2];
     memset(response,0,sizeof(response));
     Base64 b64;
     int base64_data_length = MAX_ARGUMENT_LENGTH;
     char *base64_data = b64.Encode((char *)data,data_length,(std::size_t *)&base64_data_length);
     if (__rpc->dispatch(SEND_DATA_FN,response,sizeof((char *)response),"%s",base64_data)) {
         // success
         int reply = 0;
         scanf((char *)response,"%d",&reply);
         if (reply == 1) success = true;
     }
     else {
         // failure
         DBG("ble_rpc_send_data: dispatch() failed\r\n");
     }
     if (base64_data != NULL) free(base64_data);
     return success;
 }
 
 int ble_rpc_recv_data(uint8_t *buffer,int buffer_length)
 {
     uint8_t base64_buffer[MAX_RESULT_LENGTH+1];
     memset(base64_buffer,0,sizeof(base64_buffer));
     if (__rpc->dispatch(RECV_DATA_FN,base64_buffer,MAX_RESULT_LENGTH,"%s","")) {
         // 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;
 }