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
Revision 6:98af441fd960, committed 2015-02-17
- Comitter:
- ansond
- Date:
- Tue Feb 17 02:56:36 2015 +0000
- Parent:
- 5:9233e88b9c83
- Child:
- 7:203c348ccc66
- Commit message:
- updates still not working
Changed in this revision
--- a/NSDL/nsdl_support.cpp Mon Feb 16 06:37:35 2015 +0000
+++ b/NSDL/nsdl_support.cpp Tue Feb 17 02:56:36 2015 +0000
@@ -99,14 +99,14 @@
}
static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr) {
- //DBG("NSP: sending %d bytes...\r\n",data_len);
- int sent = server.sendTo(nsp, (char*)data_ptr, data_len);
+ DBG("NSP: tx_cb(): sending %d bytes...\r\n",data_len);
+ int sent = server.sendTo(nsp,(char*)data_ptr, data_len);
return 1;
}
static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) {
// Rx callback process it...
- //DBG("NSP: received data. processing...\r\n");
+ DBG("NSP: rx_cb(): received data. processing...\r\n");
return 0;
}
@@ -192,7 +192,7 @@
// only process if we are registered and thus connected... otherwise ignore
if (__registered == true) {
- //DBG("NSP: waiting for data...\r\n");
+ DBG("NSP: waiting for data...\r\n");
int n = server.receiveFrom(from,nsp_buffer,sizeof(nsp_buffer));
if (n >= 0) {
DBG("NSP: received %d bytes... processing..\r\n.",n);
--- a/bt_network/BLE_API.lib Mon Feb 16 06:37:35 2015 +0000 +++ b/bt_network/BLE_API.lib Tue Feb 17 02:56:36 2015 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/users/ansond/code/BLE_API/#c605f4abee83 +http://developer.mbed.org/users/ansond/code/BLE_API/#3dab35584efe
--- a/bt_network/BleUartRPC/Buffer.lib Mon Feb 16 06:37:35 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://mbed.org/users/sam_grove/code/Buffer/#7b754354b99c
--- a/bt_network/BleUartRPC/Dispatcher.cpp Mon Feb 16 06:37:35 2015 +0000
+++ b/bt_network/BleUartRPC/Dispatcher.cpp Tue Feb 17 02:56:36 2015 +0000
@@ -22,10 +22,97 @@
#include "Dispatcher.h"
+ #include "SplitterAssembler.h"
+ SplitterAssembler __splitter;
+
+ #ifdef DBG
+ #undef DBG
+ #endif
+ #define DBG std::printf
+
// constructor
- Dispatcher::Dispatcher(BLEDevice &ble,int rxsize,int txsize) : m_uart(ble), m_rxbuf(rxsize), m_txbuf(txsize) {
+ Dispatcher::Dispatcher(BLEDevice &ble) : m_uart(ble) {
+ }
+
+ int Dispatcher::uart_write(uint8_t *data,int data_length) {
+ return this->m_uart.write(data,data_length);
}
// dispatch
- bool Dispatcher::dispatch(uint8_t fn_id,uint8_t *args,int args_length,uint8_t *response,int response_length) {
+ int Dispatcher::dispatch(uint8_t fn_id,uint8_t *args,int args_length,uint8_t *response,int response_length) {
+ DBG("dispatcher(): fn_id=0x%.2x args=[%s] args_length=%d response_length=%d\r\n",fn_id,args,args_length,response_length);
+
+ // prepare the send packet
+ DBG("dispatcher(): preparing the send packet...\r\n");
+ int len = this->prepare_send_packet(fn_id,args,args_length);
+
+ // Split into chunks and pad the last one
+ DBG("dispatcher(): splitting into segments for UART transmission...\r\n");
+ __splitter.reset();
+ int split_len = __splitter.split(this->m_send_packet,len);
+
+ // MULTIPLE write/putc
+ char buf[20];
+ memset(buf,0,20);
+ sprintf(buf,"\n");
+ int num_written = 1;
+ for(int k=0;k<15 && num_written > 0 ;++k) {
+ sprintf(buf,"%2d\n",k);
+ DBG("dispatcher(): sending %d...\r\n",k);
+ //for(int d=0;d<3;++d) this->m_uart._putc(buf[d]);
+ this->uart_write((uint8_t *)buf,3);
+ wait_ms(500);
+ }
+ if (num_written <= 0) DBG("dispatcher(): UART gave up...\r\n");
+
+ /* LONG
+ char buf[1024];
+ memset(buf,0,1024);
+ DBG("dispatcher(): sending initial...\r\n");
+ sprintf(buf,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz\n");
+ this->m_uart.write(buf,strlen(buf));
+ for(int k=0;k<10;++k) {
+ DBG("dispatcher(): sending %d...\r\n",k);
+ this->m_uart.write(buf,strlen(buf));
+ wait_ms(1000);
+ }
+ */
+
+ return -1;
+
+ // send the split segments over the UART
+ for(int i=0;i<split_len;++i) {
+ if (i < (split_len-1)) {
+ // wont be partial length...
+ DBG("dispatcher(): sending %dth segment: [%s]\r\n",i,__splitter.get(i));
+ this->uart_write(__splitter.get(i),UART_SEGMENT_LENGTH);
+ }
+ else {
+ // partial length so we pad with spaces...
+ uint8_t *tmp = __splitter.get(i);
+ int padded_length = strlen((char *)tmp);
+ int diff = UART_SEGMENT_LENGTH-padded_length;
+ for(int j=0;j<diff;++j) memcpy((tmp+padded_length+j)," ",1);
+ padded_length = strlen((char *)tmp);
+
+ // send the last segment which may be a partial length one...
+ DBG("dispatcher(): sending %dth segment: [%s] length: %d (padded %d)...\r\n",i,tmp,padded_length,diff);
+ this->uart_write(tmp,padded_length);
+
+ // update sent amount
+ len += diff;
+ }
+ }
+
+ // return the number of bytes sent...
+ DBG("dispatcher(): send completed. sent %d bytes...\r\n",len);
+ return len;
+ }
+
+ // prepare the packet to send
+ int Dispatcher::prepare_send_packet(uint8_t fn_id,uint8_t *args,int args_length) {
+ // clear the packet
+ memset(this->m_send_packet,0,MAX_PACKET_LENGTH+1);
+ sprintf((char *)this->m_send_packet,"[%d|%s]",fn_id,args);
+ return strlen((char *)this->m_send_packet);
}
\ No newline at end of file
--- a/bt_network/BleUartRPC/Dispatcher.h Mon Feb 16 06:37:35 2015 +0000
+++ b/bt_network/BleUartRPC/Dispatcher.h Tue Feb 17 02:56:36 2015 +0000
@@ -25,18 +25,19 @@
#include "BLEDevice.h"
#include "UARTService.h"
-
- #include "Buffer.h"
+
+ #define MAX_PACKET_LENGTH 512 // longest packet we will send
+ #define MAX_ARGUMENT_LENGTH 512 // longest argument sent as parameter list
+ #define MAX_RESULT_LENGTH 512 // longest result received
+ #define UART_SEGMENT_LENGTH 20 // BLE: max UART MTU (will trigger send immediately...)
class Dispatcher {
public:
/**
Default constructor
@param ble the BLEDevice instance
- @param rxsize the size of the receive ring buffer (default is 1024)
- @param txsize the size of the transmit ring buffer (default is 1024)
*/
- Dispatcher(BLEDevice &ble,int rxsize = 1024,int txsize = 1024);
+ Dispatcher(BLEDevice &ble);
/**
Dispatch function request with arguments (already serialized and prepared for transport)
@@ -45,14 +46,17 @@
@param args_length the length of the serialized function argument list
@param response the received response buffer
@param response_length the recieved response buffer length
- @returns true - dispatch succeeded, false - otherwise
+ @returns number of bytes sent in dispatch() or -1 if in failure
*/
- bool dispatch(uint8_t fn_id,uint8_t *args,int args_length,uint8_t *response,int response_length);
+ int dispatch(uint8_t fn_id,uint8_t *args,int args_length,uint8_t *response,int response_length);
private:
+ int uart_write(uint8_t *data,int data_length);
+ int prepare_send_packet(uint8_t fn_id,uint8_t *args,int args_length);
+
+ uint8_t m_send_packet[MAX_PACKET_LENGTH+1];
+
UARTService m_uart;
- Buffer<char> m_rxbuf;
- Buffer<char> m_txbuf;
};
#endif // __RPC_DISPATCH_H__
\ No newline at end of file
--- a/bt_network/BleUartRPC/UartRPC.cpp Mon Feb 16 06:37:35 2015 +0000
+++ b/bt_network/BleUartRPC/UartRPC.cpp Tue Feb 17 02:56:36 2015 +0000
@@ -34,7 +34,7 @@
}
// dispatch RPC
- bool UartRPC::dispatch(uint8_t fn_id,uint8_t *response,int response_length,const char *format,...)
+ int UartRPC::dispatch(uint8_t fn_id,uint8_t *response,int response_length,const char *format,...)
{
// serialize the variable arguments into a long string...
va_list args;
--- a/bt_network/BleUartRPC/UartRPC.h Mon Feb 16 06:37:35 2015 +0000
+++ b/bt_network/BleUartRPC/UartRPC.h Tue Feb 17 02:56:36 2015 +0000
@@ -27,14 +27,11 @@
#include <stdarg.h>
#include "BLEDevice.h"
#include "Dispatcher.h"
-
- #define MAX_ARGUMENT_LENGTH 1024 // longest argument sent as parameter list
- #define MAX_RESULT_LENGTH 1024 // longest result received
-
+
class UartRPC {
public:
UartRPC(BLEDevice &ble);
- bool dispatch(uint8_t fn_id,uint8_t *response,int response_length,const char *format,...);
+ int dispatch(uint8_t fn_id,uint8_t *response,int response_length,const char *format,...);
private:
Dispatcher m_dispatcher;
--- a/bt_network/BleUartRPC/UartRPCFunctions.cpp Mon Feb 16 06:37:35 2015 +0000
+++ b/bt_network/BleUartRPC/UartRPCFunctions.cpp Tue Feb 17 02:56:36 2015 +0000
@@ -48,67 +48,56 @@
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;
+ 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;
}
- else {
- // failure
- DBG("ble_rpc_open_udp_socket: dispatch() failed\r\n");
- }
- return success;
+
+ // failure
+ DBG("ble_rpc_open_udp_socket: dispatch() failed\r\n");
+ return false;
}
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;
+ 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;
}
- else {
- // failure
- DBG("ble_rpc_close_udp_socket: dispatch() failed\r\n");
- }
- return success;
+
+ // failure
+ DBG("ble_rpc_close_udp_socket: dispatch() failed\r\n");
+ return false;
}
- bool ble_rpc_send_data(uint8_t *data,int data_length)
+ int ble_rpc_send_data(uint8_t *data,int data_length)
{
- bool success = false;
+ Base64 b64;
uint8_t response[2];
- memset(response,0,sizeof(response));
- Base64 b64;
+ 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);
- 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");
- }
+ 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 success;
+ 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,sizeof(base64_buffer));
- if (__rpc->dispatch(RECV_DATA_FN,base64_buffer,MAX_RESULT_LENGTH,"%s","")) {
+ 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;
@@ -121,6 +110,7 @@
// failure
DBG("ble_rpc_recv_data: dispatch() failed\r\n");
}
+ */
return -1;
}
\ No newline at end of file
--- a/bt_network/BleUartRPC/UartRPCFunctions.h Mon Feb 16 06:37:35 2015 +0000 +++ b/bt_network/BleUartRPC/UartRPCFunctions.h Tue Feb 17 02:56:36 2015 +0000 @@ -28,7 +28,7 @@ extern "C" void ble_rpc_init(BLEDevice &ble); extern "C" bool ble_rpc_open_udp_socket(char *ip_address,int port); extern "C" bool ble_rpc_close_udp_socket(void); - extern "C" bool ble_rpc_send_data(uint8_t *data,int data_length); + extern "C" int ble_rpc_send_data(uint8_t *data,int data_length); extern "C" int ble_rpc_recv_data(uint8_t *buffer,int buffer_length); #endif // __UART_RPC_FUNCTIONS_H__ \ No newline at end of file
--- a/bt_network/Socket/Socket.cpp Mon Feb 16 06:37:35 2015 +0000
+++ b/bt_network/Socket/Socket.cpp Tue Feb 17 02:56:36 2015 +0000
@@ -20,8 +20,7 @@
using std::memset;
-extern "C" void ble_build_disconnection_record();
-extern "C" void ble_send_disconnection_record();
+#include "UartRPCFunctions.h"
Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) {
@@ -51,8 +50,7 @@
}
int Socket::close(bool shutdown) {
- ble_build_disconnection_record();
- ble_send_disconnection_record();
+ ble_rpc_close_udp_socket();
return 0;
}
--- a/bt_network/Socket/UDPSocket.cpp Mon Feb 16 06:37:35 2015 +0000
+++ b/bt_network/Socket/UDPSocket.cpp Tue Feb 17 02:56:36 2015 +0000
@@ -20,10 +20,9 @@
#include <cstring>
+#include "UartRPCFunctions.h"
+
// BLE network_stubs.cpp linkage
-extern "C" int ble_recv_data(uint8_t *buffer,int buffer_len);
-extern "C" int ble_send_data(uint8_t *data,int data_len,bool sendHeader);
-extern "C" void ble_set_remote_ip_address(const char *ipAddress,const int ipAddressLength);
extern "C" void ble_set_remote_ip_port(const int ipPort);
using std::memset;
@@ -43,10 +42,10 @@
// -1 if unsuccessful, else number of bytes written
int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) {
- return ble_send_data((uint8_t *)packet,length,true);
+ return ble_rpc_send_data((uint8_t *)packet,length);
}
// -1 if unsuccessful, else number of bytes received
int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) {
- return ble_recv_data((uint8_t *)buffer,length);
+ return ble_rpc_recv_data((uint8_t *)buffer,length);
}
--- a/bt_network/SplitterAssembler.lib Mon Feb 16 06:37:35 2015 +0000 +++ b/bt_network/SplitterAssembler.lib Tue Feb 17 02:56:36 2015 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/users/ansond/code/SplitterAssembler/#00f7a99862a3 +http://developer.mbed.org/users/ansond/code/SplitterAssembler/#221c8a56a80e
--- a/bt_network/nRF51822.lib Mon Feb 16 06:37:35 2015 +0000 +++ b/bt_network/nRF51822.lib Tue Feb 17 02:56:36 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#0e7a9efee6d7 +http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#9a820b110c29
--- a/network_stubs/network_stubs.cpp Mon Feb 16 06:37:35 2015 +0000
+++ b/network_stubs/network_stubs.cpp Tue Feb 17 02:56:36 2015 +0000
@@ -22,30 +22,18 @@
#include "network_stubs.h"
-#include "BLEDevice.h"
-#include "UARTService.h"
-
-#include "Base64.h"
-#include "SplitterAssembler.h"
-
-// forward declarations
-extern "C" int ble_recv_data(uint8_t *buffer,int buffer_len);
-extern "C" int ble_send_data(uint8_t *data,int data_len,bool sendHeader);
+#include "UartRPCFunctions.h"
extern "C" {
// Globals
-BLEDevice ble;
-UARTService *uart = NULL;
+BLEDevice ble;
volatile bool __registered = false;
-SplitterAssembler splitter;
-Base64 b64;
static const uint16_t deviceInfoServiceUUID[] = {GattService::UUID_DEVICE_INFORMATION_SERVICE};
char _ipAddress[IP_ADDRESS_LENGTH];
int _ipPort;
-char _connectionRecord[MAX_CONNECTION_RECORD_LENGTH+1];
// record the remote UDPSocket endpoint IP address
void ble_set_remote_ip_address(const char *ipAddress,const int ipAddressLength)
@@ -62,22 +50,6 @@
_ipPort = ipPort;
}
-// build connection record
-void ble_build_connection_record()
-{
- memset(_connectionRecord,0,MAX_CONNECTION_RECORD_LENGTH+1);
- sprintf(_connectionRecord,"1|%s|%d",_ipAddress,_ipPort);
- DBG("ble_build_connection_record: Connect Record: %s\r\n",_connectionRecord);
-}
-
-// build disconnection record
-void ble_build_disconnection_record()
-{
- memset(_connectionRecord,0,MAX_CONNECTION_RECORD_LENGTH+1);
- printf(_connectionRecord,"0|%s|%d",_ipAddress,_ipPort);
- DBG("ble_build_disconnection_record: Disconnect Record: %s\r\n",_connectionRecord);
-}
-
// BLE Disconnection callback
void ble_disconnect_callback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
@@ -90,33 +62,28 @@
// BLE Connection callback
void ble_connect_callback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *params)
{
+ DBG("ble_connect_callback: BLE connected! waiting for 3 seconds...\r\n");
wait_ms(3000); // wait for 3 seconds... let the gateway register the device completely and get ready to accept data...
if (__registered == false) {
- DBG("ble_connect_callback: BLE connected! Creating connection record...\r\n");
- ble_build_connection_record();
- DBG("ble_connect_callback: Sending connection record...\r\n");
- ble_send_data((uint8_t *)_connectionRecord,strlen(_connectionRecord),false);
-
- wait_ms(3000); // wait for 3 seconds... let the gateway register the device completely and get ready to accept data...
-
- // NSP registration
- DBG("ble_connect_callback: calling NSP registration...\r\n");
- register_endpoint(true);
- DBG("ble_connect_callback: NSP registration completed\r\n");
-
- // registration completed
- __registered = true;
+ DBG("ble_connect_callback: Opening remote UDP socket to: %s@%d...\r\n",_ipAddress,_ipPort);
+ if (ble_rpc_open_udp_socket(_ipAddress,_ipPort)) {
+ // NSP registration
+ DBG("ble_connect_callback: calling NSP registration...\r\n");
+ register_endpoint(true);
+ DBG("ble_connect_callback: NSP registration completed\r\n");
+
+ // registration completed
+ __registered = true;
+ }
+ else {
+ DBG("ble_connect_callback: remote UDP socket open FAILED...\r\n");
+ __registered = false;
+ }
}
-}
-
-// send the disconnection record
-void ble_send_disconnection_record()
-{
- DBG("Building disconnection record...\r\n");
- ble_build_disconnection_record();
- DBG("Sending disconnection record...\r\n");
- ble_send_data((uint8_t *)_connectionRecord,strlen(_connectionRecord),false);
+ else {
+ DBG("ble_connect_callback: Already registered and connected...\r\n");
+ }
}
// BLE Init
@@ -147,7 +114,7 @@
}
// BLE send data
-int ble_send_data(uint8_t *data,int data_len,bool sendHeader)
+int ble_send_data(uint8_t *data,int data_len)
{
int sent = 0;
DBG("in ble_send_data() data_len=%d data: [",data_len);
@@ -156,45 +123,8 @@
if (i < (data_len-1)) DBG(" ");
}
DBG("] sent: ");
-
- if (sendHeader) {
- char header[10];
- uint8_t buffer[256];
-
- memset(header,0,sizeof(header));
- memset(buffer,0,sizeof(buffer));
-
- // Base64 Encode + Add Length Header...
- int b64_data_len = sizeof(buffer); // initial sizing for malloc() in Base64...
- char *b64_data = b64.Encode((char *)data,data_len,(std::size_t *)&b64_data_len);
- sprintf(header,"%d|",b64_data_len);
- int header_length = strlen(header);
- memcpy(buffer,header,header_length);
- memcpy((buffer+header_length),b64_data,b64_data_len);
-
- // write if we have a UART...
- if (uart != NULL) {
- // Split into fragments...
- int num_fragments = splitter.split(buffer,header_length+b64_data_len);
- sent = 0;
- for(int i=0;i<num_fragments;++i) {
- int i_sent = uart->write((const void *)splitter.get(i),strlen((char *)splitter.get(i)));
- int diff = splitter.getSplitLength() - i_sent;
- sent += i_sent;
- for(int j=0;j<diff;++j) uart->write((const void *)"\0",1);
- }
- }
- if (b64_data != NULL) free(b64_data);
- }
- else {
- // plumbing headers are simple and dont need the data length header...
- if (uart != NULL) {
- sent = uart->write((const void *)data,data_len);
- int diff = splitter.getSplitLength() - sent;
- for(int j=0;j<diff;++j) uart->write((const void *)"\0",1);
- }
- }
-
+
+ sent = ble_rpc_send_data(data,data_len);
DBG("%d bytes\r\n",sent);
return sent;
}
@@ -202,84 +132,36 @@
// BLE recv data
int ble_recv_data(uint8_t *buffer,int buffer_len)
{
- // initialize...
- int received = 0;
- uint8_t encoded_buffer[1024];
- memset(encoded_buffer,0,sizeof(encoded_buffer));
+ int recv = ble_rpc_recv_data(buffer,buffer_len);
- // receive
- if (uart != NULL) received = uart->read(encoded_buffer,buffer_len);
-
- // look for the length header first
- for(int i=0;i<received;++i) {
- if (encoded_buffer[i] == '|') encoded_buffer[i] = ' ';
+ DBG("in ble_recv_data() recv=%d data: [",recv);
+ for(int i=0;i<recv;++i) {
+ DBG("%.2x",buffer[i]);
+ if (i < (recv-1)) DBG(" ");
}
- uint8_t tmp_buffer[1024];
- memset((char *)tmp_buffer,0,sizeof(tmp_buffer));
- int num_bytes = 0;
- sscanf((char *)encoded_buffer,"%d %s",&num_bytes,tmp_buffer);
- if (num_bytes > 0) {
- DBG("in ble_recv_data(): need to read a total of %d bytes...\r\n",num_bytes);
-
- // read more...
- num_bytes -= strlen((char *)tmp_buffer);
- int index = strlen((char *)tmp_buffer);
- DBG("in ble_recv_data(): Initial buffer: [%s] index=%d, num_bytes=%d\r\n",tmp_buffer,index,num_bytes);
- for(int i=0;i<num_bytes;++i) {
- DBG("in ble_recv_data() reading byte %d...\r\n",i);
- tmp_buffer[index] = uart->_getc();
- ++index;
- }
-
- buffer_len = strlen((char *)tmp_buffer);
- memset(encoded_buffer,0,sizeof(encoded_buffer));
- memcpy(encoded_buffer,tmp_buffer,buffer_len);
- received = buffer_len;
-
- // DEBUG
- DBG("in ble_recv_data() buffer_len=%d ",buffer_len);
- DBG("encoded_buffer: [%s](%d) ",encoded_buffer,strlen((char *)encoded_buffer));
- DBG("received %d bytes\r\n",received);
-
- // decode and copy to the input buffer
- int raw_data_len = buffer_len; // initial sizing for malloc() in Base64...
- char *raw_data = b64.Decode((char *)encoded_buffer,received,(std::size_t *)&raw_data_len);
-
- // copy...
- memset(buffer,0,buffer_len);
- int length = raw_data_len;
- if (length > buffer_len) length = buffer_len;
- memcpy(buffer,raw_data,length);
-
- // clean up
- if (raw_data != NULL) free(raw_data);
-
- // return the recieved number of bytes
- return received;
- }
- else {
- DBG("in ble_recv_data() no bytes to read...\r\n");
- return 0;
- }
+ DBG("] buffer_len=%d\r\n",buffer_len);
+
+ return recv;
}
// plumb out the network
void net_stubs_pre_plumb_network(bool canActAsRouterNode)
{
// BLE initialize...
- DBG("Initializing BLE layer...\r\n");
+ DBG("Initializing BLE...\r\n");
ble_init();
}
// called after the endpoint is configured...
void net_stubs_post_plumb_network(void)
{
+ // Initialize the BLE RPC layer
+ DBG("Initializing BLE UART RPC layer...\r\n");
+ ble_rpc_init(ble);
+
// BLE advertise...
DBG("Beginning BLE advertising...\r\n");
ble_begin_advertising();
-
- // Attach the BLE UART Service
- if (uart == NULL) uart = new UARTService(ble);
}
// create a suitable main event loop for this specific network
--- a/network_stubs/network_stubs.h Mon Feb 16 06:37:35 2015 +0000
+++ b/network_stubs/network_stubs.h Tue Feb 17 02:56:36 2015 +0000
@@ -39,8 +39,7 @@
#include "mbed.h"
-#define MAX_CONNECTION_RECORD_LENGTH 64
-#define IP_ADDRESS_LENGTH 130
+#define IP_ADDRESS_LENGTH 64
// timeval
struct timeval {