Demo program for LSM303 based boards
Dependencies: mbed AES BLE_API nRF51822 smallAES
BLEservice.h@8:682b575fd7b3, 2020-11-02 (annotated)
- Committer:
- f3d
- Date:
- Mon Nov 02 08:48:54 2020 +0000
- Revision:
- 8:682b575fd7b3
First commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
f3d | 8:682b575fd7b3 | 1 | //BLEservice.h |
f3d | 8:682b575fd7b3 | 2 | #ifndef __BLESERVICE_H |
f3d | 8:682b575fd7b3 | 3 | #define __BLESERVICE_H |
f3d | 8:682b575fd7b3 | 4 | #define MAX_CHARACTERISTICS 16 |
f3d | 8:682b575fd7b3 | 5 | extern Serial pc; |
f3d | 8:682b575fd7b3 | 6 | typedef void (*fptr)(void); |
f3d | 8:682b575fd7b3 | 7 | |
f3d | 8:682b575fd7b3 | 8 | class BLEservice { |
f3d | 8:682b575fd7b3 | 9 | public: |
f3d | 8:682b575fd7b3 | 10 | uint16_t Service_UUID; |
f3d | 8:682b575fd7b3 | 11 | uint32_t Characteristic_Count; |
f3d | 8:682b575fd7b3 | 12 | uint16_t Characteristic_UUID_Array[MAX_CHARACTERISTICS]; // This will contain the list of characterisic UUID's |
f3d | 8:682b575fd7b3 | 13 | BLEservice(BLEDevice &_ble,uint16_t Service_UUID,uint32_t Characteristic_Count, uint16_t *Characteristic_UUID_Array, fptr init, fptr poll,int Notify=0): ble(_ble) |
f3d | 8:682b575fd7b3 | 14 | { |
f3d | 8:682b575fd7b3 | 15 | |
f3d | 8:682b575fd7b3 | 16 | pc.printf("Entering constructor\r\n"); |
f3d | 8:682b575fd7b3 | 17 | this->Service_UUID = Service_UUID; |
f3d | 8:682b575fd7b3 | 18 | this->Characteristic_Count = Characteristic_Count; |
f3d | 8:682b575fd7b3 | 19 | pc.printf("Array of %d chars created\r\n",Characteristic_Count); |
f3d | 8:682b575fd7b3 | 20 | for (int i=0;i<Characteristic_Count;i++) |
f3d | 8:682b575fd7b3 | 21 | { |
f3d | 8:682b575fd7b3 | 22 | pc.printf("UUID %d = %d\r\n",i,Characteristic_UUID_Array[i]); |
f3d | 8:682b575fd7b3 | 23 | printf("Address of Characteristic_UUID_Array = %p\r\n",this->Characteristic_UUID_Array); |
f3d | 8:682b575fd7b3 | 24 | this->Characteristic_UUID_Array[i]=Characteristic_UUID_Array[i]; |
f3d | 8:682b575fd7b3 | 25 | } |
f3d | 8:682b575fd7b3 | 26 | pc.printf("chars copied\r\n"); |
f3d | 8:682b575fd7b3 | 27 | this->init = init; |
f3d | 8:682b575fd7b3 | 28 | this->poll = poll; |
f3d | 8:682b575fd7b3 | 29 | Value = 0; |
f3d | 8:682b575fd7b3 | 30 | GattCharacteristic *charTable[MAX_CHARACTERISTICS]; |
f3d | 8:682b575fd7b3 | 31 | for (int i=0; i < Characteristic_Count; i++) |
f3d | 8:682b575fd7b3 | 32 | { |
f3d | 8:682b575fd7b3 | 33 | if (Notify == 1) |
f3d | 8:682b575fd7b3 | 34 | { |
f3d | 8:682b575fd7b3 | 35 | Characteristics[i] = new ReadWriteGattCharacteristic<uint16_t>(this->Characteristic_UUID_Array[i],&Value,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
f3d | 8:682b575fd7b3 | 36 | } |
f3d | 8:682b575fd7b3 | 37 | else |
f3d | 8:682b575fd7b3 | 38 | { |
f3d | 8:682b575fd7b3 | 39 | Characteristics[i] = new ReadWriteGattCharacteristic<uint16_t>(this->Characteristic_UUID_Array[i],&Value); |
f3d | 8:682b575fd7b3 | 40 | } |
f3d | 8:682b575fd7b3 | 41 | charTable[i] = Characteristics[i]; |
f3d | 8:682b575fd7b3 | 42 | printf("Characteristic %d added\r\n",this->Characteristic_UUID_Array[i]); |
f3d | 8:682b575fd7b3 | 43 | } |
f3d | 8:682b575fd7b3 | 44 | GattService Service(this->Service_UUID, charTable, Characteristic_Count); |
f3d | 8:682b575fd7b3 | 45 | pc.printf("Service created\r\n"); |
f3d | 8:682b575fd7b3 | 46 | ble.addService(Service); |
f3d | 8:682b575fd7b3 | 47 | pc.printf("Service added\r\n"); |
f3d | 8:682b575fd7b3 | 48 | } |
f3d | 8:682b575fd7b3 | 49 | ~BLEservice() |
f3d | 8:682b575fd7b3 | 50 | { |
f3d | 8:682b575fd7b3 | 51 | |
f3d | 8:682b575fd7b3 | 52 | } |
f3d | 8:682b575fd7b3 | 53 | fptr init; |
f3d | 8:682b575fd7b3 | 54 | fptr poll; |
f3d | 8:682b575fd7b3 | 55 | uint16_t readCharacteristic(uint16_t index) |
f3d | 8:682b575fd7b3 | 56 | { |
f3d | 8:682b575fd7b3 | 57 | uint16_t Value,Len; |
f3d | 8:682b575fd7b3 | 58 | Len = sizeof(Value); |
f3d | 8:682b575fd7b3 | 59 | ble.gattServer().read(this->Characteristics[index]->getValueHandle(),(uint8_t *)&Value,&Len); |
f3d | 8:682b575fd7b3 | 60 | return Value; |
f3d | 8:682b575fd7b3 | 61 | } |
f3d | 8:682b575fd7b3 | 62 | void writeCharacteristic(uint16_t index, uint16_t Value) |
f3d | 8:682b575fd7b3 | 63 | { |
f3d | 8:682b575fd7b3 | 64 | pc.printf("Writing %d to handle %d\r\n",Value,this->Characteristics[index]->getValueHandle()); |
f3d | 8:682b575fd7b3 | 65 | |
f3d | 8:682b575fd7b3 | 66 | ble.gattServer().write(this->Characteristics[index]->getValueHandle(),(uint8_t *)&Value,sizeof(uint16_t)); |
f3d | 8:682b575fd7b3 | 67 | } |
f3d | 8:682b575fd7b3 | 68 | BLEDevice &ble; // Keep track of the Bluetoot device we are attached to. |
f3d | 8:682b575fd7b3 | 69 | ReadWriteGattCharacteristic<uint16_t> * Characteristics[MAX_CHARACTERISTICS]; |
f3d | 8:682b575fd7b3 | 70 | uint16_t Value; |
f3d | 8:682b575fd7b3 | 71 | }; |
f3d | 8:682b575fd7b3 | 72 | #endif |