Bluetooth Low Energy template with prewritten functions and callbacks for BLE events.
Revision 0:dbe0ce913311, committed 2018-06-22
- Comitter:
- jurica238814
- Date:
- Fri Jun 22 15:05:42 2018 +0200
- Child:
- 1:46333f67c87a
- Commit message:
- init commit
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/aconnoBLE/BLEConfig.h Fri Jun 22 15:05:42 2018 +0200 @@ -0,0 +1,16 @@ +/* + * Made by Jurica @ aconno + * All rights reserved + * + */ + +#ifndef BLE_CONFIG_H +#define BLE_CONFIG_H + +#define A_ADV_INTERVAL_MS (100) +/* BLE event queue size */ +#define EVENT_COUNT (4) + +#define TX_POWER_dB (0) + +#endif // BLE_CONFIG_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/aconnoBLE/BLEData.h Fri Jun 22 15:05:42 2018 +0200
@@ -0,0 +1,51 @@
+/*
+ * Made by Jurica @ aconno
+ * All rights reserved
+ *
+ */
+
+#ifndef BLE_DATA_H
+#define BLE_DATA_H
+
+typedef struct __attribute__((packed, aligned(1))) _timestamp
+{
+ uint64_t time : 48;
+} timestampFormat;
+
+typedef struct __attribute__((packed, aligned(1))) _headerFormat
+{
+ uint16_t ID;
+} headerFormat;
+
+typedef struct __attribute__((packed, aligned(1))) _diagnoseFlagsFormat
+{
+ uint8_t powerSupplyConnected : 1;
+ uint8_t valveSwitchedOn : 1;
+ uint8_t valveSwitchedOff : 1;
+ uint8_t shortCircuitDetected : 1;
+ uint8_t warningLife : 1;
+ uint8_t failureLife : 1;
+ uint8_t internSupplyVoltageLow : 1;
+} diagnoseFlagsFormat;
+
+typedef struct __attribute__((packed, aligned(1))) _systemState
+{
+ unsigned int switchCounter;
+ uint8_t operatingHours[3];
+ int8_t electronicsTemp;
+ int16_t coilTemp;
+ __attribute__((packed)) int8_t powerSuppyVoltage;
+ __attribute__((packed)) int8_t internPowerSupplyVoltage;
+ __attribute__((packed)) int16_t lifeTime;
+ __attribute__((packed)) uint16_t coilResistance;
+ __attribute__((packed)) uint8_t magnetoCurrent;
+} systemStateFormat;
+
+typedef struct __attribute__((packed, aligned(1))) _advertisingFormat{
+ __attribute__((packed, aligned(1))) headerFormat header;
+ __attribute__((packed, aligned(1))) diagnoseFlagsFormat diagnoseFlags;
+ __attribute__((packed, aligned(1))) timestampFormat timestamp;
+ __attribute__((packed, aligned(1))) systemStateFormat systemState;
+} advertisingFormat;
+
+#endif // BLE_DATA_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/aconnoBLE/aconnoBLE.cpp Fri Jun 22 15:05:42 2018 +0200
@@ -0,0 +1,70 @@
+/*
+* Made by Jurica @ aconno
+* All rights reserved
+*
+*/
+
+#include "aconnoBLE.h"
+#include "BLEConfig.h"
+#include "ble/BLE.h"
+#include "GapAdvertisingData.h"
+#include "BLEData.h"
+#include "service.h"
+
+extern advertisingFormat manufacturerSpecificData;
+static EventQueue eventQueue(EVENT_COUNT * EVENTS_EVENT_SIZE);
+Service *service;
+
+
+void onBleInitError(BLE &ble, ble_error_t error)
+{
+ /* Avoid compiler warnings */
+ (void) ble;
+ (void) error;
+ /* Initialization error handling should go here */
+}
+
+void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context)
+{
+ BLE &ble = context->ble;
+ eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
+}
+
+EventQueue *getBLEEventQueue(void)
+{
+ return &eventQueue;
+}
+
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
+ BLE &ble = params->ble;
+
+ /* setup NTP service and characteristic */
+ service = new Service(ble);
+ ble.gap().onDisconnection(disconnectionCallback);
+ ble.gap().onConnection(onConnectionCallback);
+ ble.gattServer().onDataWritten(onDataCallback);
+
+ /* setup event handling */
+ ble.onEventsToProcess(scheduleBleEventsProcessing);
+
+ /* setup advertising */
+ ble.gap().accumulateAdvertisingPayload(
+ GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
+ (uint8_t *)&manufacturerSpecificData, sizeof(advertisingFormat));
+ ble.gap().setAdvertisingInterval(A_ADV_INTERVAL_MS);
+ ble.gap().startAdvertising();
+}
+
+void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){
+ //printf("Device is connected.\n");
+}
+
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
+ //printf("Device is disconnected.\n");
+ BLE::Instance().gap().startAdvertising();
+}
+
+void onDataCallback(const GattWriteCallbackParams *params){
+ if(params->handle == service->getTimeCharacteristicHandle()){
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/aconnoBLE/aconnoBLE.h Fri Jun 22 15:05:42 2018 +0200 @@ -0,0 +1,59 @@ +/* + * Aconno BLE template + * Made by Jurica @ aconno + * All rights reserved + * + */ + +#ifndef ACONNO_BLE_H +#define ACONNO_BLE_H + +#include "mbed.h" +#include "ble/BLE.h" +#include "GapAdvertisingData.h" + +/** + * Ble init error callback + * @param ble referance to BLE object + * @param error Error code + */ +void onBleInitError(BLE &ble, ble_error_t error); + +/** + * BLE event callback. + * @param context + */ +void scheduleBleEventsProcessing( + BLE::OnEventsToProcessCallbackContext* context); + +/** + * Get pointer to BLE event queue. + * @return BLE event queue. + */ +EventQueue *getBLEEventQueue(void); + +/** + * ble init complete callback + * @param params + */ +void bleInitComplete(BLE::InitializationCompleteCallbackContext *params); + +/** + * onConnection callback + * @param params ConnectionCallbackParams_t + */ +void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params); + +/** + * Disconnection callback + * @param params DisconnectionCallbackParams_t + */ +void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params); + +/** + * onDataWritten callback + * @param params GattWriteCallbackParams + */ +void onDataCallback(const GattWriteCallbackParams *params); + +#endif // ACONNO_BLE_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/aconnoBLE/service.h Fri Jun 22 15:05:42 2018 +0200
@@ -0,0 +1,33 @@
+/*
+ * Made by Jurica @ aconno
+ * All rights reserved
+ *
+ */
+
+#ifndef SERVICE_H
+#define SERVICE_H
+
+#define TIMESTAMP_SIZE_B (6)
+
+static const uint16_t SERVICE_UUID = 0xA3B6;
+static const uint16_t NTP_CHARACTERISTIC_UUID = 0x33CC;
+
+class Service{
+ public:
+ Service(BLEDevice &ble) : ble(ble), time(NTP_CHARACTERISTIC_UUID, 0){
+ // Add characteristics to the table
+ GattCharacteristic *characteristics[] = {&time};
+ GattService service(SERVICE_UUID, characteristics,
+ sizeof(characteristics)/sizeof(*characteristics));
+ ble.addService(service); // Add service in the BLE
+ }
+ inline GattAttribute::Handle_t getTimeCharacteristicHandle()
+ {
+ return time.getValueHandle();
+ }
+ private:
+ BLEDevice &ble;
+ // New characteristics names time
+ WriteOnlyArrayGattCharacteristic<uint8_t, TIMESTAMP_SIZE_B> time;
+};
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Fri Jun 22 15:05:42 2018 +0200 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#bfb43799afe571b26895d021f9932c274cff58c6
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/source/main.cpp Fri Jun 22 15:05:42 2018 +0200
@@ -0,0 +1,51 @@
+/*
+ * aconno.de
+ * Simple program for aconno ble template demonstration
+ *
+ * Made by Jurica Resetar @ aconno
+ * ResetarJurica@gmail.com
+ *
+ */
+
+#include "mbed.h"
+#include "aconnoBLE.h"
+#include "ble/BLE.h"
+#include "BLEData.h"
+#include "GapAdvertisingData.h"
+#include "BLEConfig.h"
+#include <list>
+
+advertisingFormat manufacturerSpecificData;
+headerFormat header;
+systemStateFormat systemState;
+timestampFormat timestamp;
+diagnoseFlagsFormat diagnoseFlags;
+
+int (*callback_)(int, int) = NULL; // pointer to a function
+
+int callbackFunction(int param1, int param2)
+{
+ // Do some job
+ return 1;
+}
+
+void primjer(int(*callback)(int, int))
+{
+ callback_(1, 2);
+}
+
+int main()
+{
+ //primjer(functionCallback);
+
+ callback_ = callbackFunction;
+ primjer(callback_);
+
+ BLE &ble = BLE::Instance();
+ ble.init(bleInitComplete);
+ ble.gap().setTxPower(TX_POWER_dB); // Set TX power to TX_POWER
+ while (ble.hasInitialized() == false) { }
+
+ while(1)
+ {}
+}