ble nano hid over gatt

Dependencies:   BLE_API mbed-dev nRF51822

Revision:
79:0095bfb18c57
Parent:
75:351d7ffe81d1
Child:
82:af52d37b1946
--- a/HIDServiceBase.cpp	Fri Sep 02 23:19:19 2016 +0900
+++ b/HIDServiceBase.cpp	Sat Sep 03 20:36:40 2016 +0900
@@ -15,6 +15,7 @@
  */
 
 #include "mbed.h"
+#include "config.h"
 #include "HIDServiceBase.h"
 
 static const report_reference_t inputReportReferenceData = { 0, INPUT_REPORT };
@@ -104,67 +105,67 @@
     reportTickerDelay(inputReportTickerDelay),
     reportTickerIsActive(false)
 {
-    static GattCharacteristic *characteristics[] = {
-        &HIDInformationCharacteristic,
-        &reportMapCharacteristic,
-        &protocolModeCharacteristic,
-        &HIDControlPointCharacteristic,
-        NULL,
-        NULL,
-        NULL,
-        NULL,
-        NULL
-    };
+}
 
-    unsigned int charIndex = 4;
-    /*
-     * Report characteristics are optional, and depend on the reportMap descriptor
-     * Note: at least one should be present, but we don't check that at the moment.
-     */
-    if (inputReportLength)
-        characteristics[charIndex++] = &inputReportCharacteristic;
-    if (outputReportLength)
-        characteristics[charIndex++] = &outputReportCharacteristic;
-    if (featureReportLength)
-        characteristics[charIndex++] = &featureReportCharacteristic;
+void HIDServiceBase::init(void) {
+	static GattCharacteristic *characteristics[] = {
+		&HIDInformationCharacteristic,
+		&reportMapCharacteristic,
+		&protocolModeCharacteristic,
+		&HIDControlPointCharacteristic,
+		NULL,
+		NULL,
+		NULL,
+		NULL,
+		NULL
+	};
 
-    /* TODO: let children add some more characteristics, namely boot keyboard and mouse (They are
-     * mandatory as per HIDS spec.) Ex:
-     *
-     * addExtraCharacteristics(characteristics, int& charIndex);
-     */
+	uint8_t charIndex = 4;
+	/*
+	 * Report characteristics are optional, and depend on the reportMap descriptor
+	 * Note: at least one should be present, but we don't check that at the moment.
+	 */
+	if (inputReportLength)
+		characteristics[charIndex++] = &inputReportCharacteristic;
+	if (outputReportLength)
+		characteristics[charIndex++] = &outputReportCharacteristic;
+	if (featureReportLength)
+		characteristics[charIndex++] = &featureReportCharacteristic;
+
+	addExtraCharacteristics(characteristics, charIndex);
 
-    GattService service(GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE,
-                        characteristics, charIndex);
+	DEBUG_PRINTF_BLE("new GattService %d\r\n", charIndex);
+	GattService service(GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE, characteristics, charIndex);
 
-    ble.gattServer().addService(service);
+	ble.gattServer().addService(service);
 
-    ble.gap().onConnection(this, &HIDServiceBase::onConnection);
-    ble.gap().onDisconnection(this, &HIDServiceBase::onDisconnection);
+	ble.gap().onConnection(this, &HIDServiceBase::onConnection);
+	ble.gap().onDisconnection(this, &HIDServiceBase::onDisconnection);
 
-    ble.gattServer().onDataSent(this, &HIDServiceBase::onDataSent);
+	ble.gattServer().onDataSent(this, &HIDServiceBase::onDataSent);
+	ble.gattServer().onDataWritten(this, &HIDServiceBase::onDataWritten);
 
-    /*
-     * Change preferred connection params, in order to optimize the notification frequency. Most
-     * OSes seem to respect this, even though they are not required to.
-     *
-     * Some OSes don't handle reconnection well, at the moment, so we set the maximum possible
-     * timeout, 32 seconds
-     */
-    uint16_t minInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(reportTickerDelay / 2);
-    if (minInterval < 6)
-        minInterval = 6;
-    uint16_t maxInterval = minInterval * 2;
-    Gap::ConnectionParams_t params = {minInterval, maxInterval, 0, 3200};
+	/*
+	 * Change preferred connection params, in order to optimize the notification frequency. Most
+	 * OSes seem to respect this, even though they are not required to.
+	 *
+	 * Some OSes don't handle reconnection well, at the moment, so we set the maximum possible
+	 * timeout, 32 seconds
+	 */
+	uint16_t minInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(reportTickerDelay / 2);
+	if (minInterval < 6)
+		minInterval = 6;
+	uint16_t maxInterval = minInterval * 2;
+	Gap::ConnectionParams_t params = {minInterval, maxInterval, 0, 3200};
 
-    ble.gap().setPreferredConnectionParams(&params);
+	ble.gap().setPreferredConnectionParams(&params);
 
-    SecurityManager::SecurityMode_t securityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM;
-    protocolModeCharacteristic.requireSecurity(securityMode);
-    reportMapCharacteristic.requireSecurity(securityMode);
-    inputReportCharacteristic.requireSecurity(securityMode);
-    outputReportCharacteristic.requireSecurity(securityMode);
-    featureReportCharacteristic.requireSecurity(securityMode);
+	SecurityManager::SecurityMode_t securityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM;
+	protocolModeCharacteristic.requireSecurity(securityMode);
+	reportMapCharacteristic.requireSecurity(securityMode);
+	inputReportCharacteristic.requireSecurity(securityMode);
+	outputReportCharacteristic.requireSecurity(securityMode);
+	featureReportCharacteristic.requireSecurity(securityMode);
 }
 
 void HIDServiceBase::startReportTicker(void) {
@@ -183,6 +184,11 @@
     startReportTicker();
 }
 
+void HIDServiceBase::onDataWritten(const GattWriteCallbackParams *params) {
+}
+
+void HIDServiceBase::addExtraCharacteristics(GattCharacteristic** characteristics, uint8_t& charIndex) {
+}
 
 ble_error_t HIDServiceBase::send(const report_t report) {
     return ble.gattServer().write(inputReportCharacteristic.getValueHandle(),