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:
Sun Jan 08 23:15:53 2017 +0000
Revision:
23:2e73c391bb12
A cute tiny piece of code implementing an Iot NAND device, demonstrating how to setup and advertise a cute GATT (NAND) service.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hux 23:2e73c391bb12 1 // advertise.h - begin peripheral role and start advertising
hux 23:2e73c391bb12 2 //
hux 23:2e73c391bb12 3 // Synopsis:
hux 23:2e73c391bb12 4 //
hux 23:2e73c391bb12 5 // advertise(O&o);
hux 23:2e73c391bb12 6 // advertise(O&o, const char *mode, int ms = 100);
hux 23:2e73c391bb12 7 //
hux 23:2e73c391bb12 8 // payload(O&o,const char *mode) // set advertising payload
hux 23:2e73c391bb12 9 //
hux 23:2e73c391bb12 10 // Arguments:
hux 23:2e73c391bb12 11 //
hux 23:2e73c391bb12 12 // o: Blob object (to avoid name clashes)
hux 23:2e73c391bb12 13 // mode: String containing a sequence of mode characters defining both
hux 23:2e73c391bb12 14 // advertising type (upper case characters) and advertising flags
hux 23:2e73c391bb12 15 // (lower case characters), some of which have meaning, all others
hux 23:2e73c391bb12 16 // are ignored and can be used for spacing (e.g. ':', ' ', '-', ...)
hux 23:2e73c391bb12 17 // ms: Advertising interval in mili seconds (default 100 ms)
hux 23:2e73c391bb12 18 //
hux 23:2e73c391bb12 19 // Advertising Type
hux 23:2e73c391bb12 20 //
hux 23:2e73c391bb12 21 // C: ADV_CONNECTABLE_UNDIRECTED
hux 23:2e73c391bb12 22 // D: ADV_CONNECTABLE_DIRECTED
hux 23:2e73c391bb12 23 // S: ADV_SCANNABLE_UNDIRECTED
hux 23:2e73c391bb12 24 // N: ADV_NON_CONNECTABLE_UNDIRECTED
hux 23:2e73c391bb12 25 //
hux 23:2e73c391bb12 26 // Advertising Flags:
hux 23:2e73c391bb12 27 //
hux 23:2e73c391bb12 28 // l: LE Limited Discoverable Mode (value: 0x01)
hux 23:2e73c391bb12 29 // g: LE General Discoverable Mode (value: 0x02)
hux 23:2e73c391bb12 30 // n: BR/EDR Not Supported (value: 0x04)
hux 23:2e73c391bb12 31 // c: Controller: simultaneous LE & BR/EDR (value: 0x08)
hux 23:2e73c391bb12 32 // h: Host: Simultaneous LE & BR/EDR (value: 0x10)
hux 23:2e73c391bb12 33 //
hux 23:2e73c391bb12 34 // Examples 1: Start advertising in peripheral role with type 'connectable, uni-
hux 23:2e73c391bb12 35 // directed, and flags 'BR/EDR Not Supported', 'LE General Discoverable Mode'
hux 23:2e73c391bb12 36 //
hux 23:2e73c391bb12 37 // advertise(o,"C:ng",100); // start advertising @ 100 msec interval
hux 23:2e73c391bb12 38 // advertise(o,"Cng",100); // same as above
hux 23:2e73c391bb12 39 // advertise(o,"gCn",100); // same as above
hux 23:2e73c391bb12 40 //
hux 23:2e73c391bb12 41 // Examples 2: Start advertising in peripheral role with type 'connectable,
hux 23:2e73c391bb12 42 // directed, and flags 'LE Limited Discoverable Mode'
hux 23:2e73c391bb12 43 //
hux 23:2e73c391bb12 44 // advertise("D:l",100) // Connectable Directed, limited discoverable
hux 23:2e73c391bb12 45 //
hux 23:2e73c391bb12 46 // Examples 3: A typical peripheral advertising session starts with setup of
hux 23:2e73c391bb12 47 // device name, advertising name and advertising data.
hux 23:2e73c391bb12 48 //
hux 23:2e73c391bb12 49 // device(o,"Smart Button"); // setup device name
hux 23:2e73c391bb12 50 // name(o,"Smart Button #1"); // setup advertising name
hux 23:2e73c391bb12 51 // data(o,"My Home"); // setup advertising data
hux 23:2e73c391bb12 52 // advertise(o,"C:ng",100); // start advertising @ 100 msec interval
hux 23:2e73c391bb12 53 //
hux 23:2e73c391bb12 54 // Example 4: restart advertising based on pre-defined advertising payload
hux 23:2e73c391bb12 55 //
hux 23:2e73c391bb12 56 //
hux 23:2e73c391bb12 57 // See also: SERVICE
hux 23:2e73c391bb12 58 //
hux 23:2e73c391bb12 59 #ifndef _ADVERTISE_H_
hux 23:2e73c391bb12 60 #define _ADVERTISE_H_
hux 23:2e73c391bb12 61
hux 23:2e73c391bb12 62 #include "bricks/blob.h"
hux 23:2e73c391bb12 63 #include "bricks/trace.h"
hux 23:2e73c391bb12 64
hux 23:2e73c391bb12 65 #define _GADAT GapAdvertisingData
hux 23:2e73c391bb12 66 #define _GAPAR GapAdvertisingParams
hux 23:2e73c391bb12 67
hux 23:2e73c391bb12 68
hux 23:2e73c391bb12 69 inline void payload(O&o,const char *p) // Set advertising type and flags
hux 23:2e73c391bb12 70 {
hux 23:2e73c391bb12 71 _GAPAR::AdvertisingType_t typ = _GAPAR::ADV_CONNECTABLE_UNDIRECTED;
hux 23:2e73c391bb12 72 uint8_t mask = 0x00;
hux 23:2e73c391bb12 73
hux 23:2e73c391bb12 74 for (;*p; p++)
hux 23:2e73c391bb12 75 {
hux 23:2e73c391bb12 76 switch (*p)
hux 23:2e73c391bb12 77 {
hux 23:2e73c391bb12 78 case 'l': mask = mask | _GADAT::LE_LIMITED_DISCOVERABLE; break;
hux 23:2e73c391bb12 79 case 'g': mask = mask | _GADAT::LE_GENERAL_DISCOVERABLE; break;
hux 23:2e73c391bb12 80 case 'n': mask = mask | _GADAT::BREDR_NOT_SUPPORTED; break;
hux 23:2e73c391bb12 81 case 'c': mask = mask | _GADAT::SIMULTANEOUS_LE_BREDR_C; break;
hux 23:2e73c391bb12 82 case 'h': mask = mask | _GADAT::SIMULTANEOUS_LE_BREDR_H; break;
hux 23:2e73c391bb12 83
hux 23:2e73c391bb12 84 case 'C': typ = _GAPAR::ADV_CONNECTABLE_UNDIRECTED; break;
hux 23:2e73c391bb12 85 case 'D': typ = _GAPAR::ADV_CONNECTABLE_DIRECTED; break;
hux 23:2e73c391bb12 86 case 'S': typ = _GAPAR::ADV_SCANNABLE_UNDIRECTED; break;
hux 23:2e73c391bb12 87 case 'N': typ = _GAPAR::ADV_NON_CONNECTABLE_UNDIRECTED; break;
hux 23:2e73c391bb12 88 }
hux 23:2e73c391bb12 89 }
hux 23:2e73c391bb12 90
hux 23:2e73c391bb12 91 o.gap().setAdvertisingType(typ);
hux 23:2e73c391bb12 92 o.gap().accumulateAdvertisingPayload(mask);
hux 23:2e73c391bb12 93 }
hux 23:2e73c391bb12 94
hux 23:2e73c391bb12 95
hux 23:2e73c391bb12 96 inline void advertise(O&o) // start advertising
hux 23:2e73c391bb12 97 {
hux 23:2e73c391bb12 98 o.gap().startAdvertising(); // start advertising
hux 23:2e73c391bb12 99 trace(o,2,"<advertising>\n");
hux 23:2e73c391bb12 100 }
hux 23:2e73c391bb12 101
hux 23:2e73c391bb12 102 inline void advertise(O&o, int ms) // start advertising (msec: periode)
hux 23:2e73c391bb12 103 {
hux 23:2e73c391bb12 104 o.gap().setAdvertisingInterval(ms);
hux 23:2e73c391bb12 105 advertise(o); // start advertising
hux 23:2e73c391bb12 106 }
hux 23:2e73c391bb12 107
hux 23:2e73c391bb12 108 inline void advertise(Blob &o, const char *mode, int ms = 100)
hux 23:2e73c391bb12 109 {
hux 23:2e73c391bb12 110 payload(o,mode); // set advertising payload type & mask
hux 23:2e73c391bb12 111 advertise(o,ms); // start advertising
hux 23:2e73c391bb12 112 }
hux 23:2e73c391bb12 113
hux 23:2e73c391bb12 114 #endif // _ADVERTISE_H_