Lightly modified version of the BLE stack, that doesn't bring up a DFUService by default... as we have our own.

Fork of BLE_API by Bluetooth Low Energy

Revision:
250:6862d374e613
Parent:
242:0e9201b67e2f
Child:
251:6ce809c972bb
--- a/services/URIBeaconConfigService.h	Fri Dec 12 13:32:24 2014 +0000
+++ b/services/URIBeaconConfigService.h	Fri Dec 12 13:32:24 2014 +0000
@@ -106,6 +106,13 @@
 
         configureGAP();
 
+        uriDataChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::uriDataWriteAuthorizationCallback);
+        flagsChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::falgsAuthorizationCallback);
+        txPowerLevelsChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked);
+        txPowerModeChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked);
+        beaconPeriodChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked);
+        resetChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked);
+
         GattCharacteristic *charTable[] = {&lockedStateChar, &uriDataChar, &flagsChar, &txPowerLevelsChar, &beaconPeriodChar, &resetChar};
         GattService         beaconControlService(URIBeacon2ControlServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
 
@@ -300,58 +307,22 @@
 
     /*
      * This callback is invoked when a GATT client attempts to modify any of the
-     * characteristics of this service. Attempts to do so must be rolled back if
-     * the config service is locked; else they are also applied to the internal
-     * state of this service object.
+     * characteristics of this service. Attempts to do so are also applied to
+     * the internal state of this service object.
      */
     void onDataWritten(const GattCharacteristicWriteCBParams *params) {
         uint16_t handle = params->charHandle;
         if (handle == uriDataChar.getValueHandle()) {
-            if (lockedState) { /* When locked, the device isn't allowed to update the uriData characteristic. */
-                /* Restore GATT database with previous value. */
-                updateURIDataCharacteristic();
-                return;
-            }
-
-            /* We don't handle very large writes at the moment. */
-            if ((params->offset != 0) || (params->len > MAX_SIZE_URI_DATA_CHAR_VALUE)) {
-                return;
-            }
-
             uriDataLength = params->len;
             memcpy(uriData, params->data, uriDataLength);
         } else if (handle == flagsChar.getValueHandle()) {
-            if (lockedState) { /* When locked, the device isn't allowed to update the characteristic. */
-                /* Restore GATT database with previous value. */
-                updateFlagsCharacteristic();
-                return;
-            } else {
-                flags = *(params->data);
-            }
+            flags = *(params->data);
         } else if (handle == txPowerLevelsChar.getValueHandle()) {
-            if (lockedState) { /* When locked, the device isn't allowed to update the characteristic. */
-                /* Restore GATT database with previous value. */
-                updateTxPowerLevelsCharacteristic();
-                return;
-            } else {
-                memcpy(powerLevels, params->data, NUM_POWER_MODES * sizeof(int8_t));
-            }
+            memcpy(powerLevels, params->data, NUM_POWER_MODES * sizeof(int8_t));
         } else if (handle == txPowerModeChar.getValueHandle()) {
-            if (lockedState) { /* When locked, the device isn't allowed to update the characteristic. */
-                /* Restore GATT database with previous value. */
-                updateTxPowerModeCharacteristic();
-                return;
-            } else {
-                txPowerMode = *reinterpret_cast<const TXPowerModes_t *>(params->data);
-            }
+            txPowerMode = *reinterpret_cast<const TXPowerModes_t *>(params->data);
         } else if (handle == beaconPeriodChar.getValueHandle()) {
-            if (lockedState) { /* When locked, the device isn't allowed to update the characteristic. */
-                /* Restore GATT database with previous value. */
-                updateBeaconPeriodCharacteristic();
-                return;
-            } else {
-                beaconPeriod = *((uint16_t *)(params->data));
-            }
+            beaconPeriod = *((uint16_t *)(params->data));
         } else if (handle == resetChar.getValueHandle()) {
             resetDefaults();
         }
@@ -412,6 +383,25 @@
     }
 
 private:
+    void uriDataWriteAuthorizationCallback(GattCharacteristicWriteAuthCBParams *params) {
+        if (lockedState || (params->offset != 0) || (params->len > MAX_SIZE_URI_DATA_CHAR_VALUE)) {
+            params->authorizationReply = false;
+        }
+    }
+
+    void falgsAuthorizationCallback(GattCharacteristicWriteAuthCBParams *params) {
+        if (lockedState || ((*(params->data) & 0xFE) != 0)) {
+            params->authorizationReply = false;
+        }
+    }
+
+    void denyGATTWritesIfLocked(GattCharacteristicWriteAuthCBParams *params) {
+        if (lockedState) {
+            params->authorizationReply = false;
+        }
+    }
+
+private:
     /**
      * For debugging only. Print Hex representation of ServiceDataPayload to the console.
      */