Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: libnsdl_m0 BLE_API Base64 nRF51822 SplitterAssembler
bt_network/BleUartRPC/UartRPCFunctions.cpp@6:98af441fd960, 2015-02-17 (annotated)
- Committer:
- ansond
- Date:
- Tue Feb 17 02:56:36 2015 +0000
- Revision:
- 6:98af441fd960
- Parent:
- 5:9233e88b9c83
- Child:
- 9:bf0cf5828378
updates still not working
Who changed what in which revision?
User | Revision | Line number | New 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 | typedef enum { |
ansond | 5:9233e88b9c83 | 35 | SOCKET_OPEN_FN = 0x01, |
ansond | 5:9233e88b9c83 | 36 | SOCKET_CLOSE_FN = 0x02, |
ansond | 5:9233e88b9c83 | 37 | SEND_DATA_FN = 0x04, |
ansond | 5:9233e88b9c83 | 38 | RECV_DATA_FN = 0x08, |
ansond | 5:9233e88b9c83 | 39 | NUM_FNs = 4 |
ansond | 5:9233e88b9c83 | 40 | } FunctionIDs; |
ansond | 5:9233e88b9c83 | 41 | |
ansond | 5:9233e88b9c83 | 42 | UartRPC *__rpc = NULL; |
ansond | 5:9233e88b9c83 | 43 | |
ansond | 5:9233e88b9c83 | 44 | void ble_rpc_init(BLEDevice &ble) |
ansond | 5:9233e88b9c83 | 45 | { |
ansond | 5:9233e88b9c83 | 46 | if (__rpc == NULL) __rpc = new UartRPC(ble); |
ansond | 5:9233e88b9c83 | 47 | } |
ansond | 5:9233e88b9c83 | 48 | |
ansond | 5:9233e88b9c83 | 49 | bool ble_rpc_open_udp_socket(char *ip_address,int port) |
ansond | 5:9233e88b9c83 | 50 | { |
ansond | 5:9233e88b9c83 | 51 | uint8_t response[2]; |
ansond | 6:98af441fd960 | 52 | memset(response,0,2); |
ansond | 6:98af441fd960 | 53 | if (__rpc->dispatch(SOCKET_OPEN_FN,response,2,"%s %d",ip_address,port) > 0) { |
ansond | 6:98af441fd960 | 54 | DBG("ble_rpc_open_udp_socket: success...\r\n"); |
ansond | 6:98af441fd960 | 55 | return true; |
ansond | 5:9233e88b9c83 | 56 | } |
ansond | 6:98af441fd960 | 57 | |
ansond | 6:98af441fd960 | 58 | // failure |
ansond | 6:98af441fd960 | 59 | DBG("ble_rpc_open_udp_socket: dispatch() failed\r\n"); |
ansond | 6:98af441fd960 | 60 | return false; |
ansond | 5:9233e88b9c83 | 61 | } |
ansond | 5:9233e88b9c83 | 62 | |
ansond | 5:9233e88b9c83 | 63 | bool ble_rpc_close_udp_socket(void) |
ansond | 5:9233e88b9c83 | 64 | { |
ansond | 5:9233e88b9c83 | 65 | uint8_t response[2]; |
ansond | 6:98af441fd960 | 66 | memset(response,0,2); |
ansond | 6:98af441fd960 | 67 | if (__rpc->dispatch(SOCKET_CLOSE_FN,response,2,"%s","") > 0) { |
ansond | 6:98af441fd960 | 68 | DBG("ble_rpc_close_udp_socket: success...\r\n"); |
ansond | 6:98af441fd960 | 69 | return true; |
ansond | 5:9233e88b9c83 | 70 | } |
ansond | 6:98af441fd960 | 71 | |
ansond | 6:98af441fd960 | 72 | // failure |
ansond | 6:98af441fd960 | 73 | DBG("ble_rpc_close_udp_socket: dispatch() failed\r\n"); |
ansond | 6:98af441fd960 | 74 | return false; |
ansond | 5:9233e88b9c83 | 75 | } |
ansond | 5:9233e88b9c83 | 76 | |
ansond | 6:98af441fd960 | 77 | int ble_rpc_send_data(uint8_t *data,int data_length) |
ansond | 5:9233e88b9c83 | 78 | { |
ansond | 6:98af441fd960 | 79 | Base64 b64; |
ansond | 5:9233e88b9c83 | 80 | uint8_t response[2]; |
ansond | 6:98af441fd960 | 81 | memset(response,0,2); |
ansond | 5:9233e88b9c83 | 82 | int base64_data_length = MAX_ARGUMENT_LENGTH; |
ansond | 6:98af441fd960 | 83 | DBG("ble_rpc_send_data: base64 encoding data...\r\n"); |
ansond | 5:9233e88b9c83 | 84 | char *base64_data = b64.Encode((char *)data,data_length,(std::size_t *)&base64_data_length); |
ansond | 6:98af441fd960 | 85 | DBG("ble_rpc_send_data: sending data=[%s] length=%d...\r\n",base64_data,strlen((char *)base64_data)); |
ansond | 6:98af441fd960 | 86 | int sent_length = __rpc->dispatch(SEND_DATA_FN,response,2,"%s",base64_data); |
ansond | 6:98af441fd960 | 87 | DBG("ble_rpc_send_data: dispatched %d bytes\r\n",sent_length); |
ansond | 5:9233e88b9c83 | 88 | if (base64_data != NULL) free(base64_data); |
ansond | 6:98af441fd960 | 89 | return sent_length; |
ansond | 5:9233e88b9c83 | 90 | } |
ansond | 5:9233e88b9c83 | 91 | |
ansond | 5:9233e88b9c83 | 92 | int ble_rpc_recv_data(uint8_t *buffer,int buffer_length) |
ansond | 5:9233e88b9c83 | 93 | { |
ansond | 6:98af441fd960 | 94 | /* |
ansond | 5:9233e88b9c83 | 95 | uint8_t base64_buffer[MAX_RESULT_LENGTH+1]; |
ansond | 6:98af441fd960 | 96 | memset(base64_buffer,0,MAX_RESULT_LENGTH+1); |
ansond | 6:98af441fd960 | 97 | DBG("ble_rpc_recv_data: seeing if any data is available...\r\n"); |
ansond | 6:98af441fd960 | 98 | int recv_length =__rpc->dispatch(RECV_DATA_FN,base64_buffer,MAX_RESULT_LENGTH,"%s",""); |
ansond | 6:98af441fd960 | 99 | DBG("ble_rpc_recv_data: received %d bytes of data...\r\n",recv_length); |
ansond | 6:98af441fd960 | 100 | if (recv_length > 0) { |
ansond | 5:9233e88b9c83 | 101 | // success |
ansond | 5:9233e88b9c83 | 102 | Base64 b64; |
ansond | 5:9233e88b9c83 | 103 | int base64_length = buffer_length; |
ansond | 5:9233e88b9c83 | 104 | char *raw_buffer = b64.Decode((char *)base64_buffer,strlen((char *)base64_buffer),(std::size_t *)&base64_length); |
ansond | 5:9233e88b9c83 | 105 | memcpy(buffer,raw_buffer,base64_length); |
ansond | 5:9233e88b9c83 | 106 | if (raw_buffer != NULL) free(raw_buffer); |
ansond | 5:9233e88b9c83 | 107 | return base64_length; |
ansond | 5:9233e88b9c83 | 108 | } |
ansond | 5:9233e88b9c83 | 109 | else { |
ansond | 5:9233e88b9c83 | 110 | // failure |
ansond | 5:9233e88b9c83 | 111 | DBG("ble_rpc_recv_data: dispatch() failed\r\n"); |
ansond | 5:9233e88b9c83 | 112 | } |
ansond | 6:98af441fd960 | 113 | */ |
ansond | 5:9233e88b9c83 | 114 | return -1; |
ansond | 5:9233e88b9c83 | 115 | } |
ansond | 5:9233e88b9c83 | 116 |