Doug Anson / mbedEndpointNetwork_BLE

Dependencies:   libnsdl_m0 BLE_API Base64 nRF51822 SplitterAssembler

Committer:
ansond
Date:
Fri Sep 04 21:04:08 2015 +0000
Revision:
35:71eb3663ecbd
Parent:
33:4f6929e123f2
Child:
36:aa73681951ad
update for android 5.x. A wait() call is necessary for 5.x prior to write().

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 5:9233e88b9c83 1 /**
ansond 5:9233e88b9c83 2 * @file Dispatcher.h
ansond 5:9233e88b9c83 3 * @brief BLE UART RPC dispatcher header
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 #ifndef __RPC_DISPATCH_H__
ansond 5:9233e88b9c83 24 #define __RPC_DISPATCH_H__
ansond 5:9233e88b9c83 25
ansond 5:9233e88b9c83 26 #include "BLEDevice.h"
ansond 5:9233e88b9c83 27 #include "UARTService.h"
ansond 9:bf0cf5828378 28 #include "SplitterAssembler.h"
ansond 11:d601b867b297 29
ansond 11:d601b867b297 30 // These are aligned with MAX_VALUE_BUFFER_LENGTH in mbedConnectorInterface.h
ansond 33:4f6929e123f2 31 #define MAX_PACKET_LENGTH 340 // longest packet we will send (aligns with 15x20 in SplitterAssembler)
ansond 33:4f6929e123f2 32 #define MAX_ARGUMENT_LENGTH 340 // longest argument sent as parameter list (aligns with 15x20 in SplitterAssembler)
ansond 33:4f6929e123f2 33 #define MAX_RESULT_LENGTH 340 // longest result received (aligns with 15x20 in SplitterAssembler)
ansond 11:d601b867b297 34
ansond 11:d601b867b297 35 // This is aligned with the value of DEF_FRAGMENT_LENGTH in SplitterAssembler.h and is also the max UART MTU for BLE
ansond 6:98af441fd960 36 #define UART_SEGMENT_LENGTH 20 // BLE: max UART MTU (will trigger send immediately...)
ansond 5:9233e88b9c83 37
ansond 35:71eb3663ecbd 38 // 9/4/15: Android 5.x appears to have some sort of poor-mans DDoS detector in its BLE stack so we have to slow things down a bit..
ansond 35:71eb3663ecbd 39 #define ANDROID_BLE_DELAY true // true to enable slow down, false to disable
ansond 35:71eb3663ecbd 40 #define ANDROID_BLE_DELAY_MS 500 // delay time (in ms), if enabled, to allow android 5.x to catch up...
ansond 35:71eb3663ecbd 41
ansond 5:9233e88b9c83 42 class Dispatcher {
ansond 5:9233e88b9c83 43 public:
ansond 5:9233e88b9c83 44 /**
ansond 5:9233e88b9c83 45 Default constructor
ansond 5:9233e88b9c83 46 @param ble the BLEDevice instance
ansond 5:9233e88b9c83 47 */
ansond 6:98af441fd960 48 Dispatcher(BLEDevice &ble);
ansond 5:9233e88b9c83 49
ansond 5:9233e88b9c83 50 /**
ansond 5:9233e88b9c83 51 Dispatch function request with arguments (already serialized and prepared for transport)
ansond 5:9233e88b9c83 52 @param fn_id the identifier for the target remote function to invoke
ansond 5:9233e88b9c83 53 @param args the serialized function argument array list
ansond 5:9233e88b9c83 54 @param args_length the length of the serialized function argument list
ansond 5:9233e88b9c83 55 @param response the received response buffer
ansond 5:9233e88b9c83 56 @param response_length the recieved response buffer length
ansond 6:98af441fd960 57 @returns number of bytes sent in dispatch() or -1 if in failure
ansond 5:9233e88b9c83 58 */
ansond 6:98af441fd960 59 int dispatch(uint8_t fn_id,uint8_t *args,int args_length,uint8_t *response,int response_length);
ansond 5:9233e88b9c83 60
ansond 5:9233e88b9c83 61 private:
ansond 11:d601b867b297 62 void onDataWritten(const GattCharacteristicWriteCBParams *params);
ansond 6:98af441fd960 63
ansond 11:d601b867b297 64 int uart_write(bool send_ack = false);
ansond 11:d601b867b297 65 int uart_write(uint8_t *data,int data_length);
ansond 11:d601b867b297 66 int prepare_send_packet(uint8_t fn_id,uint8_t *args,int args_length);
ansond 9:bf0cf5828378 67
ansond 11:d601b867b297 68 uint8_t m_send_packet[MAX_PACKET_LENGTH+1];
ansond 11:d601b867b297 69 UARTService m_uart;
ansond 11:d601b867b297 70 SplitterAssembler m_splitter;
ansond 11:d601b867b297 71 int m_splitter_count;
ansond 11:d601b867b297 72 int m_splitter_index;
ansond 5:9233e88b9c83 73 };
ansond 5:9233e88b9c83 74
ansond 5:9233e88b9c83 75 #endif // __RPC_DISPATCH_H__