MicroBit as BLE gamepad

Dependencies:   nrf51-sdk

Dependents:   microbit-dal

Fork of nRF51822 by Lancaster University

Revision:
616:a8f9b022d8fd
Parent:
615:65ea2acfc6a2
diff -r 65ea2acfc6a2 -r a8f9b022d8fd source/nRF5xGattServer.cpp
--- a/source/nRF5xGattServer.cpp	Wed Apr 06 22:38:43 2016 +0100
+++ b/source/nRF5xGattServer.cpp	Wed Apr 06 22:39:17 2016 +0100
@@ -15,17 +15,16 @@
  */
 
 #include "nRF5xGattServer.h"
-#include "mbed.h"
+#ifdef YOTTA_CFG_MBED_OS
+    #include "mbed-drivers/mbed.h"
+#else
+    #include "mbed.h"
+#endif
 
 #include "common/common.h"
 #include "btle/custom/custom_helper.h"
 
-#include "nRF5xGap.h"
-
-nRF5xGattServer &nRF5xGattServer::getInstance(void) {
-    static nRF5xGattServer m_instance;
-    return m_instance;
-}
+#include "nRF5xn.h"
 
 /**************************************************************************/
 /*!
@@ -69,7 +68,7 @@
 
         /* Skip any incompletely defined, read-only characteristics. */
         if ((p_char->getValueAttribute().getValuePtr() == NULL) &&
-            (p_char->getValueAttribute().getInitialLength() == 0) &&
+            (p_char->getValueAttribute().getLength() == 0) &&
             (p_char->getProperties() == GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)) {
             continue;
         }
@@ -95,8 +94,9 @@
                                               p_char->getProperties(),
                                               p_char->getRequiredSecurity(),
                                               p_char->getValueAttribute().getValuePtr(),
-                                              p_char->getValueAttribute().getInitialLength(),
+                                              p_char->getValueAttribute().getLength(),
                                               p_char->getValueAttribute().getMaxLength(),
+                                              p_char->getValueAttribute().hasVariableLength(),
                                               userDescriptionDescriptorValuePtr,
                                               userDescriptionDescriptorValueLen,
                                               p_char->isReadAuthorizationEnabled(),
@@ -127,8 +127,9 @@
                    custom_add_in_descriptor(BLE_GATT_HANDLE_INVALID,
                                             &nordicUUID,
                                             p_desc->getValuePtr(),
-                                            p_desc->getInitialLength(),
+                                            p_desc->getLength(),
                                             p_desc->getMaxLength(),
+                                            p_desc->hasVariableLength(),
                                             &nrfDescriptorHandles[descriptorCount]),
                 BLE_ERROR_PARAM_OUT_OF_RANGE);
 
@@ -239,7 +240,8 @@
         hvx_params.p_len  = &len;
 
         if (connectionHandle == BLE_CONN_HANDLE_INVALID) { /* use the default connection handle if the caller hasn't specified a valid connectionHandle. */
-            connectionHandle = nRF5xGap::getInstance().getConnectionHandle();
+            nRF5xGap &gap = (nRF5xGap &) nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getGap();
+            connectionHandle = gap.getConnectionHandle();
         }
         error_t error = (error_t) sd_ble_gatts_hvx(connectionHandle, &hvx_params);
         if (error != ERROR_NONE) {
@@ -275,10 +277,43 @@
     return returnValue;
 }
 
+/**
+ * Perform an explicit BLE notification of a given attribute.
+ *
+ * @param[in] attributeHandle
+ *              Handle for the value attribute of the Characteristic.
+ * @param[in] value
+ *              A pointer to a buffer holding the new value
+ * @param[in] size
+ *              Size of the new value (in bytes).
+ *
+ * @return BLE_ERROR_NONE if we have successfully set the value of the attribute.
+ */
+ble_error_t nRF5xGattServer::notify(GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len)
+{
+    uint16_t gapConnectionHandle = ((nRF5xGap &)nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getGap()).getConnectionHandle();
+    ble_gatts_hvx_params_t hvx_params;
+
+    hvx_params.handle = attributeHandle;
+    hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
+    hvx_params.offset = 0;
+    hvx_params.p_data = const_cast<uint8_t *>(buffer);
+    hvx_params.p_len  = &len;
+
+    error_t error = (error_t) sd_ble_gatts_hvx(gapConnectionHandle, &hvx_params);
+
+    if (error == ERROR_NONE)  
+	    return BLE_ERROR_NONE;
+    else
+	    return BLE_STACK_BUSY;
+}
+
+
 ble_error_t nRF5xGattServer::areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP)
 {
     /* Forward the call with the default connection handle. */
-    return areUpdatesEnabled(nRF5xGap::getInstance().getConnectionHandle(), characteristic, enabledP);
+    nRF5xGap &gap = (nRF5xGap &) nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getGap();
+    return areUpdatesEnabled(gap.getConnectionHandle(), characteristic, enabledP);
 }
 
 ble_error_t nRF5xGattServer::areUpdatesEnabled(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP)
@@ -310,6 +345,33 @@
 
 /**************************************************************************/
 /*!
+    @brief  Clear nRF5xGattServer's state.
+
+    @returns    ble_error_t
+
+    @retval     BLE_ERROR_NONE
+                Everything executed properly
+*/
+/**************************************************************************/
+ble_error_t nRF5xGattServer::reset(void)
+{
+    /* Clear all state that is from the parent, including private members */
+    if (GattServer::reset() != BLE_ERROR_NONE) {
+        return BLE_ERROR_INVALID_STATE;
+    }
+
+    /* Clear derived class members */
+    memset(p_characteristics,        0, sizeof(p_characteristics));
+    memset(p_descriptors,            0, sizeof(p_descriptors));
+    memset(nrfCharacteristicHandles, 0, sizeof(ble_gatts_char_handles_t));
+    memset(nrfDescriptorHandles,     0, sizeof(nrfDescriptorHandles));
+    descriptorCount = 0;
+
+    return BLE_ERROR_NONE;
+}
+
+/**************************************************************************/
+/*!
     @brief  Callback handler for events getting pushed up from the SD
 */
 /**************************************************************************/
@@ -359,9 +421,13 @@
             return;
         }
 
-        case BLE_GATTS_EVT_SYS_ATTR_MISSING:
-            sd_ble_gatts_sys_attr_set(gattsEventP->conn_handle, NULL, 0, 0);
+        case BLE_GATTS_EVT_SYS_ATTR_MISSING: 
+        case BLE_GAP_EVT_CONN_SEC_UPDATE:
+        {
+            GattSysAttrMissingCallbackParams cbParams = {gattsEventP->conn_handle};
+            handleSysAttrMissingEvent(&cbParams);
             return;
+        }
 
         case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
             switch (gattsEventP->params.authorize_request.type) {