![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Dependencies: AccelSensor BLE_API mbed nRF51822
InternalValuesService.h@0:027b7829b46a, 2018-01-19 (annotated)
- Committer:
- fjaviercifuentes
- Date:
- Fri Jan 19 02:38:38 2018 +0000
- Revision:
- 0:027b7829b46a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fjaviercifuentes | 0:027b7829b46a | 1 | #ifndef __BLE_INTERNAL_VALUES_SERVICE_H__ |
fjaviercifuentes | 0:027b7829b46a | 2 | #define __BLE_INTERNAL_VALUES_SERVICE_H__ |
fjaviercifuentes | 0:027b7829b46a | 3 | |
fjaviercifuentes | 0:027b7829b46a | 4 | #include "mbed.h" |
fjaviercifuentes | 0:027b7829b46a | 5 | #include "ble/BLE.h" |
fjaviercifuentes | 0:027b7829b46a | 6 | #include "ble/Gap.h" |
fjaviercifuentes | 0:027b7829b46a | 7 | #include "ImobStateService.h" |
fjaviercifuentes | 0:027b7829b46a | 8 | |
fjaviercifuentes | 0:027b7829b46a | 9 | class InternalValuesService { |
fjaviercifuentes | 0:027b7829b46a | 10 | public: |
fjaviercifuentes | 0:027b7829b46a | 11 | const static uint16_t INTERNAL_VALUES_SERVICE_UUID = 0xB000; |
fjaviercifuentes | 0:027b7829b46a | 12 | const static uint16_t LIPOCHARGER_STATE_CHARACTERISTIC_UUID = 0xB001; |
fjaviercifuentes | 0:027b7829b46a | 13 | const static uint16_t CONTACT_STATE_CHARACTERISTIC_UUID = 0xB002; |
fjaviercifuentes | 0:027b7829b46a | 14 | const static uint16_t ID1_CHARACTERISTIC_UUID = 0xB003; |
fjaviercifuentes | 0:027b7829b46a | 15 | const static uint16_t ID2_CHARACTERISTIC_UUID = 0xB004; |
fjaviercifuentes | 0:027b7829b46a | 16 | const static uint16_t CPC_CHARACTERISTIC_UUID = 0xB005; |
fjaviercifuentes | 0:027b7829b46a | 17 | const static uint16_t DPC_CHARACTERISTIC_UUID = 0xB006; |
fjaviercifuentes | 0:027b7829b46a | 18 | |
fjaviercifuentes | 0:027b7829b46a | 19 | InternalValuesService(BLEDevice &_ble, ImobStateService * imobStateServicePtr) : |
fjaviercifuentes | 0:027b7829b46a | 20 | ble(_ble), |
fjaviercifuentes | 0:027b7829b46a | 21 | lipoChargerState(2), |
fjaviercifuentes | 0:027b7829b46a | 22 | contactState(0), |
fjaviercifuentes | 0:027b7829b46a | 23 | id1(NRF_FICR->DEVICEID[0]), |
fjaviercifuentes | 0:027b7829b46a | 24 | id2(NRF_FICR->DEVICEID[1]), |
fjaviercifuentes | 0:027b7829b46a | 25 | chargeProgramCycles(0), |
fjaviercifuentes | 0:027b7829b46a | 26 | dischargeProgramCycles(0), |
fjaviercifuentes | 0:027b7829b46a | 27 | ISS(imobStateServicePtr), |
fjaviercifuentes | 0:027b7829b46a | 28 | LipoChargerCharacteristic(LIPOCHARGER_STATE_CHARACTERISTIC_UUID, &lipoChargerState, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
fjaviercifuentes | 0:027b7829b46a | 29 | ContactCharacteristic(CONTACT_STATE_CHARACTERISTIC_UUID, &contactState, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
fjaviercifuentes | 0:027b7829b46a | 30 | Id1Characteristic(ID1_CHARACTERISTIC_UUID, &id1), |
fjaviercifuentes | 0:027b7829b46a | 31 | Id2Characteristic(ID2_CHARACTERISTIC_UUID, &id2), |
fjaviercifuentes | 0:027b7829b46a | 32 | ChargeProgramCyclesCharacteristic(CPC_CHARACTERISTIC_UUID, &chargeProgramCycles), |
fjaviercifuentes | 0:027b7829b46a | 33 | DischargeProgramCyclesCharacteristic(DPC_CHARACTERISTIC_UUID, &dischargeProgramCycles) |
fjaviercifuentes | 0:027b7829b46a | 34 | { |
fjaviercifuentes | 0:027b7829b46a | 35 | GattCharacteristic *charTable[] = {&LipoChargerCharacteristic, &ContactCharacteristic, &Id1Characteristic, &Id2Characteristic, &ChargeProgramCyclesCharacteristic, &DischargeProgramCyclesCharacteristic}; |
fjaviercifuentes | 0:027b7829b46a | 36 | GattService internalValuesService(INTERNAL_VALUES_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
fjaviercifuentes | 0:027b7829b46a | 37 | |
fjaviercifuentes | 0:027b7829b46a | 38 | ble.addService(internalValuesService); |
fjaviercifuentes | 0:027b7829b46a | 39 | |
fjaviercifuentes | 0:027b7829b46a | 40 | uint8_t auxPass[PASSLEN]; |
fjaviercifuentes | 0:027b7829b46a | 41 | uint8_t auxKey[KEYLEN]; |
fjaviercifuentes | 0:027b7829b46a | 42 | |
fjaviercifuentes | 0:027b7829b46a | 43 | id1Array[0] = id1 >> 24; |
fjaviercifuentes | 0:027b7829b46a | 44 | id1Array[1] = id1 >> 16; |
fjaviercifuentes | 0:027b7829b46a | 45 | id1Array[2] = id1 >> 8; |
fjaviercifuentes | 0:027b7829b46a | 46 | id1Array[3] = id1; |
fjaviercifuentes | 0:027b7829b46a | 47 | |
fjaviercifuentes | 0:027b7829b46a | 48 | id2Array[0] = id2 >> 24; |
fjaviercifuentes | 0:027b7829b46a | 49 | id2Array[1] = id2 >> 16; |
fjaviercifuentes | 0:027b7829b46a | 50 | id2Array[2] = id2 >> 8; |
fjaviercifuentes | 0:027b7829b46a | 51 | id2Array[3] = id2; |
fjaviercifuentes | 0:027b7829b46a | 52 | |
fjaviercifuentes | 0:027b7829b46a | 53 | |
fjaviercifuentes | 0:027b7829b46a | 54 | for(uint8_t i = 0; i < 8; i++) |
fjaviercifuentes | 0:027b7829b46a | 55 | auxPass[i] = id1Array[i%4]; |
fjaviercifuentes | 0:027b7829b46a | 56 | |
fjaviercifuentes | 0:027b7829b46a | 57 | for(uint8_t i = 8; i < PASSLEN; i++) |
fjaviercifuentes | 0:027b7829b46a | 58 | auxPass[i] = id2Array[i%4]; |
fjaviercifuentes | 0:027b7829b46a | 59 | |
fjaviercifuentes | 0:027b7829b46a | 60 | for(uint8_t i = 0; i < KEYLEN; i++) |
fjaviercifuentes | 0:027b7829b46a | 61 | auxKey[i] = id2Array[i%4]; |
fjaviercifuentes | 0:027b7829b46a | 62 | |
fjaviercifuentes | 0:027b7829b46a | 63 | ISS->setCorrectPass(auxPass); |
fjaviercifuentes | 0:027b7829b46a | 64 | ISS->setCryptKey(auxKey); |
fjaviercifuentes | 0:027b7829b46a | 65 | |
fjaviercifuentes | 0:027b7829b46a | 66 | ble.gattServer().write(Id1Characteristic.getValueHandle(), id1Array, 4); |
fjaviercifuentes | 0:027b7829b46a | 67 | ble.gattServer().write(Id2Characteristic.getValueHandle(), id2Array, 4); |
fjaviercifuentes | 0:027b7829b46a | 68 | } |
fjaviercifuentes | 0:027b7829b46a | 69 | |
fjaviercifuentes | 0:027b7829b46a | 70 | void updateLipoChargerState(uint8_t newLipoChargerState) |
fjaviercifuentes | 0:027b7829b46a | 71 | { |
fjaviercifuentes | 0:027b7829b46a | 72 | lipoChargerState = newLipoChargerState; |
fjaviercifuentes | 0:027b7829b46a | 73 | ble.gattServer().write(LipoChargerCharacteristic.getValueHandle(), &lipoChargerState, 1); |
fjaviercifuentes | 0:027b7829b46a | 74 | } |
fjaviercifuentes | 0:027b7829b46a | 75 | |
fjaviercifuentes | 0:027b7829b46a | 76 | uint8_t getLipoChargerState() |
fjaviercifuentes | 0:027b7829b46a | 77 | { |
fjaviercifuentes | 0:027b7829b46a | 78 | return lipoChargerState; |
fjaviercifuentes | 0:027b7829b46a | 79 | } |
fjaviercifuentes | 0:027b7829b46a | 80 | |
fjaviercifuentes | 0:027b7829b46a | 81 | void updateContactState(uint8_t newContactState) |
fjaviercifuentes | 0:027b7829b46a | 82 | { |
fjaviercifuentes | 0:027b7829b46a | 83 | contactState = newContactState; |
fjaviercifuentes | 0:027b7829b46a | 84 | ble.gattServer().write(ContactCharacteristic.getValueHandle(), &contactState, 1); |
fjaviercifuentes | 0:027b7829b46a | 85 | } |
fjaviercifuentes | 0:027b7829b46a | 86 | |
fjaviercifuentes | 0:027b7829b46a | 87 | void updateDischargeProgramCyclesCharacteristic() |
fjaviercifuentes | 0:027b7829b46a | 88 | { |
fjaviercifuentes | 0:027b7829b46a | 89 | dischargeProgramCyclesArray[0] = dischargeProgramCycles >> 24; |
fjaviercifuentes | 0:027b7829b46a | 90 | dischargeProgramCyclesArray[1] = dischargeProgramCycles >> 16; |
fjaviercifuentes | 0:027b7829b46a | 91 | dischargeProgramCyclesArray[2] = dischargeProgramCycles >> 8; |
fjaviercifuentes | 0:027b7829b46a | 92 | dischargeProgramCyclesArray[3] = dischargeProgramCycles; |
fjaviercifuentes | 0:027b7829b46a | 93 | |
fjaviercifuentes | 0:027b7829b46a | 94 | ble.gattServer().write(DischargeProgramCyclesCharacteristic.getValueHandle(), dischargeProgramCyclesArray, 4); |
fjaviercifuentes | 0:027b7829b46a | 95 | dischargeProgramCycles = 0; |
fjaviercifuentes | 0:027b7829b46a | 96 | } |
fjaviercifuentes | 0:027b7829b46a | 97 | |
fjaviercifuentes | 0:027b7829b46a | 98 | void incrementDischargeProgramCycles() |
fjaviercifuentes | 0:027b7829b46a | 99 | { |
fjaviercifuentes | 0:027b7829b46a | 100 | dischargeProgramCycles++; |
fjaviercifuentes | 0:027b7829b46a | 101 | } |
fjaviercifuentes | 0:027b7829b46a | 102 | |
fjaviercifuentes | 0:027b7829b46a | 103 | void updateChargeProgramCyclesCharacteristic() |
fjaviercifuentes | 0:027b7829b46a | 104 | { |
fjaviercifuentes | 0:027b7829b46a | 105 | chargeProgramCyclesArray[0] = chargeProgramCycles >> 24; |
fjaviercifuentes | 0:027b7829b46a | 106 | chargeProgramCyclesArray[1] = chargeProgramCycles >> 16; |
fjaviercifuentes | 0:027b7829b46a | 107 | chargeProgramCyclesArray[2] = chargeProgramCycles >> 8; |
fjaviercifuentes | 0:027b7829b46a | 108 | chargeProgramCyclesArray[3] = chargeProgramCycles; |
fjaviercifuentes | 0:027b7829b46a | 109 | |
fjaviercifuentes | 0:027b7829b46a | 110 | ble.gattServer().write(ChargeProgramCyclesCharacteristic.getValueHandle(), chargeProgramCyclesArray, 4); |
fjaviercifuentes | 0:027b7829b46a | 111 | chargeProgramCycles = 0; |
fjaviercifuentes | 0:027b7829b46a | 112 | } |
fjaviercifuentes | 0:027b7829b46a | 113 | |
fjaviercifuentes | 0:027b7829b46a | 114 | void incrementChargeProgramCycles() |
fjaviercifuentes | 0:027b7829b46a | 115 | { |
fjaviercifuentes | 0:027b7829b46a | 116 | chargeProgramCycles++; |
fjaviercifuentes | 0:027b7829b46a | 117 | } |
fjaviercifuentes | 0:027b7829b46a | 118 | |
fjaviercifuentes | 0:027b7829b46a | 119 | private: |
fjaviercifuentes | 0:027b7829b46a | 120 | BLEDevice &ble; |
fjaviercifuentes | 0:027b7829b46a | 121 | uint8_t lipoChargerState; |
fjaviercifuentes | 0:027b7829b46a | 122 | uint8_t contactState; |
fjaviercifuentes | 0:027b7829b46a | 123 | |
fjaviercifuentes | 0:027b7829b46a | 124 | uint32_t id1; |
fjaviercifuentes | 0:027b7829b46a | 125 | uint32_t id2; |
fjaviercifuentes | 0:027b7829b46a | 126 | uint32_t chargeProgramCycles; |
fjaviercifuentes | 0:027b7829b46a | 127 | uint32_t dischargeProgramCycles; |
fjaviercifuentes | 0:027b7829b46a | 128 | |
fjaviercifuentes | 0:027b7829b46a | 129 | uint8_t id1Array[4]; |
fjaviercifuentes | 0:027b7829b46a | 130 | uint8_t id2Array[4]; |
fjaviercifuentes | 0:027b7829b46a | 131 | uint8_t chargeProgramCyclesArray[4]; |
fjaviercifuentes | 0:027b7829b46a | 132 | uint8_t dischargeProgramCyclesArray[4]; |
fjaviercifuentes | 0:027b7829b46a | 133 | |
fjaviercifuentes | 0:027b7829b46a | 134 | ImobStateService * ISS; |
fjaviercifuentes | 0:027b7829b46a | 135 | |
fjaviercifuentes | 0:027b7829b46a | 136 | ReadOnlyGattCharacteristic < uint8_t > LipoChargerCharacteristic; |
fjaviercifuentes | 0:027b7829b46a | 137 | ReadOnlyGattCharacteristic < uint8_t > ContactCharacteristic; |
fjaviercifuentes | 0:027b7829b46a | 138 | |
fjaviercifuentes | 0:027b7829b46a | 139 | ReadOnlyGattCharacteristic < uint32_t > Id1Characteristic; |
fjaviercifuentes | 0:027b7829b46a | 140 | ReadOnlyGattCharacteristic < uint32_t > Id2Characteristic; |
fjaviercifuentes | 0:027b7829b46a | 141 | ReadOnlyGattCharacteristic < uint32_t > ChargeProgramCyclesCharacteristic; |
fjaviercifuentes | 0:027b7829b46a | 142 | ReadOnlyGattCharacteristic < uint32_t > DischargeProgramCyclesCharacteristic; |
fjaviercifuentes | 0:027b7829b46a | 143 | |
fjaviercifuentes | 0:027b7829b46a | 144 | }; |
fjaviercifuentes | 0:027b7829b46a | 145 | |
fjaviercifuentes | 0:027b7829b46a | 146 | #endif /* #ifndef __BLE_INTERNAL_VALUES_SERVICE_H__ */ |