A cute tiny piece of code implementing an IoT NAND device, demonstrating how to setup and advertise a cute GATT (NAND) service. The code has been tested on a Nordic nRF51822-DK.

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_HeartRate_IDB0XA1 by ST

Committer:
hux
Date:
Sat May 19 14:10:17 2018 +0000
Revision:
26:dce30a5341bb
Parent:
23:2e73c391bb12
Published

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hux 23:2e73c391bb12 1 // collection.h - used to setup a collection of characteristics in order to
hux 23:2e73c391bb12 2 // setup a service.
hux 23:2e73c391bb12 3 //
hux 23:2e73c391bb12 4 // Description:
hux 23:2e73c391bb12 5 // Collections are used to setup a service comprising a collection of
hux 23:2e73c391bb12 6 // characteristics. The setup sequence starts with declaring a collection
hux 23:2e73c391bb12 7 // (this constructs an empty collection) while passing the collection in-
hux 23:2e73c391bb12 8 // stance repeatedly to a constructor of a characteristic, which auto-adds
hux 23:2e73c391bb12 9 // the new constructed characteristics to the collection. Finally, when the
hux 23:2e73c391bb12 10 // collection instance is passed to a service constructor, the service con-
hux 23:2e73c391bb12 11 // structor is able to access all characteristics which have been collected
hux 23:2e73c391bb12 12 // previously.
hux 23:2e73c391bb12 13 //
hux 23:2e73c391bb12 14 // Example 1: A protocol might be declared as a class as follows
hux 23:2e73c391bb12 15 //
hux 23:2e73c391bb12 16 // Collection col; // collection used during setup
hux 23:2e73c391bb12 17 //
hux 23:2e73c391bb12 18 // Characteristic<ObjectId> id(col,0x2AC3,"rw","ID");
hux 23:2e73c391bb12 19 // Characteristic<ObjectName> name(col,0x2ABE,"rw","Name");
hux 23:2e73c391bb12 20 // Characteristic<Digital> presence(col,0x2A56,"r","Presence");
hux 23:2e73c391bb12 21 //
hux 23:2e73c391bb12 22 // Service presenceDetection(col,0xA001); // instantiate service
hux 23:2e73c391bb12 23 //
hux 23:2e73c391bb12 24 // onSetup(Blob &blue)
hux 23:2e73c391bb12 25 // {
hux 23:2e73c391bb12 26 // blue.service(presenceDetection); // add service
hux 23:2e73c391bb12 27 // }
hux 23:2e73c391bb12 28 //
hux 23:2e73c391bb12 29 // Example 2: service definition by means of a service definition class
hux 23:2e73c391bb12 30 //
hux 23:2e73c391bb12 31 // class PresenceDetector
hux 23:2e73c391bb12 32 // {
hux 23:2e73c391bb12 33 // public:
hux 23:2e73c391bb12 34 // Collection col; // collection used during setup
hux 23:2e73c391bb12 35 //
hux 23:2e73c391bb12 36 // Characteristic<ObjectId> id; // ID of presence detector
hux 23:2e73c391bb12 37 // Characteristic<ObjectName> name; // name of presence detector
hux 23:2e73c391bb12 38 // Characteristic<Digital> presence; // digital presence value
hux 23:2e73c391bb12 39 // Characteristic<DateTime> timestamp; // last detection change's time
hux 23:2e73c391bb12 40 // Characteristic<ObjectName> layout; // name of model railway layout
hux 23:2e73c391bb12 41 //
hux 23:2e73c391bb12 42 // Service presenceDetection; // the service
hux 23:2e73c391bb12 43 //
hux 23:2e73c391bb12 44 // public:
hux 23:2e73c391bb12 45 // PresenceDetector(Blob &blue, cost UUID uuid) :
hux 23:2e73c391bb12 46 // list; // init service list
hux 23:2e73c391bb12 47 // id(list,0x2AC3,"rw","ID"), // instantiate characteristic
hux 23:2e73c391bb12 48 // name(list,0x2ABE,"rw","Name"), // instantiate characteristic
hux 23:2e73c391bb12 49 // presence(list,0x2A56,"r","Presence"),// instantiate characteristic
hux 23:2e73c391bb12 50 // timestamp(list,0x2A08,"r","Timestamp"),// instantiate characteristic
hux 23:2e73c391bb12 51 // layout(list,0x2ABE,"rw","Layout"), // instantiate characteristic
hux 23:2e73c391bb12 52 // presenceDetection(list,uuid) // instantiate service
hux 23:2e73c391bb12 53 // {
hux 23:2e73c391bb12 54 // blue.service(presenceDetection); // add service
hux 23:2e73c391bb12 55 // }
hux 23:2e73c391bb12 56 // };
hux 23:2e73c391bb12 57 //
hux 23:2e73c391bb12 58 #ifndef _COLLECTION_H_
hux 23:2e73c391bb12 59 #define _COLLECTION_H_
hux 23:2e73c391bb12 60
hux 23:2e73c391bb12 61 #include "ble/BLE.h"
hux 23:2e73c391bb12 62 #include "ble/Gap.h"
hux 23:2e73c391bb12 63
hux 23:2e73c391bb12 64 class Collection
hux 23:2e73c391bb12 65 {
hux 23:2e73c391bb12 66 private:
hux 23:2e73c391bb12 67 typedef GattCharacteristic *GattCharPtr;
hux 23:2e73c391bb12 68
hux 23:2e73c391bb12 69 public:
hux 23:2e73c391bb12 70 GattCharacteristic **plist;
hux 23:2e73c391bb12 71 int count;
hux 23:2e73c391bb12 72
hux 23:2e73c391bb12 73 Collection() // construct empty collection
hux 23:2e73c391bb12 74 {
hux 23:2e73c391bb12 75 plist = 0; count = 0; // init properties
hux 23:2e73c391bb12 76 }
hux 23:2e73c391bb12 77
hux 23:2e73c391bb12 78 void add(GattCharPtr p)
hux 23:2e73c391bb12 79 {
hux 23:2e73c391bb12 80 GattCharPtr *pnew = new GattCharPtr[count+1];
hux 23:2e73c391bb12 81 pnew[count] = p; // add new characteristic
hux 23:2e73c391bb12 82
hux 23:2e73c391bb12 83 if (count > 0)
hux 23:2e73c391bb12 84 { for (int i=0; i < count; i++)
hux 23:2e73c391bb12 85 pnew[i] = plist[i];
hux 23:2e73c391bb12 86 delete plist;
hux 23:2e73c391bb12 87 }
hux 23:2e73c391bb12 88 plist = pnew; // update list pointer
hux 23:2e73c391bb12 89 count++;
hux 23:2e73c391bb12 90 }
hux 23:2e73c391bb12 91 };
hux 23:2e73c391bb12 92
hux 23:2e73c391bb12 93 #endif // _COLLECTION_H_