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