|
| 1 | +#include "HomeService.h" |
| 2 | + |
| 3 | +#include <libraries/log/nrf_log.h> |
| 4 | +#include "components/ble/NimbleController.h" |
| 5 | + |
| 6 | +namespace { |
| 7 | + // 0006yyxx-78fc-48fe-8e23-433b3a1942d0 |
| 8 | + constexpr ble_uuid128_t CharUuid(uint8_t x, uint8_t y) { |
| 9 | + return ble_uuid128_t {.u = {.type = BLE_UUID_TYPE_128}, |
| 10 | + .value = {0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, x, y, 0x06, 0x00}}; |
| 11 | + } |
| 12 | + |
| 13 | + // 00060000-78fc-48fe-8e23-433b3a1942d0 |
| 14 | + constexpr ble_uuid128_t BaseUuid() { |
| 15 | + return CharUuid(0x00, 0x00); |
| 16 | + } |
| 17 | + |
| 18 | + constexpr ble_uuid128_t homeUuid {BaseUuid()}; |
| 19 | + |
| 20 | + constexpr ble_uuid128_t homeOpenUuid {CharUuid(0x01, 0x00)}; |
| 21 | + constexpr ble_uuid128_t homeLayoutUuid {CharUuid(0x02, 0x00)}; |
| 22 | + constexpr ble_uuid128_t homePressUuid {CharUuid(0x03, 0x00)}; |
| 23 | + |
| 24 | + int HomeCallback(uint16_t /*conn_handle*/, uint16_t /*attr_handle*/, struct ble_gatt_access_ctxt* ctxt, void* arg) { |
| 25 | + return static_cast<Pinetime::Controllers::HomeService*>(arg)->OnCommand(ctxt); |
| 26 | + } |
| 27 | +} // namespace |
| 28 | + |
| 29 | +Pinetime::Controllers::HomeService::HomeService(NimbleController& nimble) : nimble(nimble) { |
| 30 | + characteristicDefinition[0] = {.uuid = &homeLayoutUuid.u, .access_cb = HomeCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE}; |
| 31 | + characteristicDefinition[1] = {.uuid = &homeOpenUuid.u, |
| 32 | + .access_cb = HomeCallback, |
| 33 | + .arg = this, |
| 34 | + .flags = BLE_GATT_CHR_F_NOTIFY, |
| 35 | + .val_handle = &eventOpenedHandle}; |
| 36 | + characteristicDefinition[2] = {.uuid = &homePressUuid.u, |
| 37 | + .access_cb = HomeCallback, |
| 38 | + .arg = this, |
| 39 | + .flags = BLE_GATT_CHR_F_NOTIFY, |
| 40 | + .val_handle = &eventPressedHandle}; |
| 41 | + characteristicDefinition[3] = {0}; |
| 42 | + |
| 43 | + serviceDefinition[0] = {.type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = &homeUuid.u, .characteristics = characteristicDefinition}; |
| 44 | + serviceDefinition[1] = {0}; |
| 45 | +} |
| 46 | + |
| 47 | +void Pinetime::Controllers::HomeService::Init() { |
| 48 | + uint8_t res = 0; |
| 49 | + res = ble_gatts_count_cfg(serviceDefinition); |
| 50 | + ASSERT(res == 0); |
| 51 | + |
| 52 | + res = ble_gatts_add_svcs(serviceDefinition); |
| 53 | + ASSERT(res == 0); |
| 54 | +} |
| 55 | + |
| 56 | +int Pinetime::Controllers::HomeService::OnCommand(ble_gatt_access_ctxt* ctxt) { |
| 57 | + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { |
| 58 | + size_t bufferSize = OS_MBUF_PKTLEN(ctxt->om); |
| 59 | + |
| 60 | + uint8_t data[bufferSize]; |
| 61 | + os_mbuf_copydata(ctxt->om, 0, bufferSize, data); |
| 62 | + |
| 63 | + if (ble_uuid_cmp(ctxt->chr->uuid, &homeLayoutUuid.u) == 0) { |
| 64 | + auto screen = std::make_unique<Screen>(); |
| 65 | + uint8_t* ptr = &data[0]; |
| 66 | + |
| 67 | + numScreens = *ptr++; |
| 68 | + screen->index = *ptr++; |
| 69 | + |
| 70 | + screen->cols = *ptr >> 4; |
| 71 | + screen->rows = *ptr & 0x0F; |
| 72 | + ptr++; |
| 73 | + uint8_t num_comps = *(ptr++); |
| 74 | + |
| 75 | + for (size_t j = 0; j < num_comps; j++) { |
| 76 | + Component comp; |
| 77 | + comp.type = (ComponentType) (*(ptr++)); |
| 78 | + |
| 79 | + comp.x = *ptr >> 4; |
| 80 | + comp.y = *ptr & 0x0F; |
| 81 | + ptr++; |
| 82 | + comp.w = *ptr >> 4; |
| 83 | + comp.h = *ptr & 0x0F; |
| 84 | + ptr++; |
| 85 | + |
| 86 | + uint8_t label_len = *(ptr++); |
| 87 | + auto label = std::make_unique<char[]>(label_len + 1); |
| 88 | + memcpy(label.get(), ptr, label_len); |
| 89 | + label.get()[label_len] = 0; |
| 90 | + ptr += label_len; |
| 91 | + |
| 92 | + comp.label = std::move(label); |
| 93 | + |
| 94 | + screen->components.emplace_back(std::move(comp)); |
| 95 | + } |
| 96 | + |
| 97 | + currentScreen = std::move(screen); |
| 98 | + dataUpdateTime = xTaskGetTickCount(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return 0; |
| 103 | +} |
| 104 | + |
| 105 | +bool Pinetime::Controllers::HomeService::NotifyOpened(int8_t screenIndex) { |
| 106 | + uint16_t connectionHandle = nimble.connHandle(); |
| 107 | + |
| 108 | + if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + auto* om = ble_hs_mbuf_from_flat(&screenIndex, sizeof(screenIndex)); |
| 113 | + ble_gattc_notify_custom(connectionHandle, eventOpenedHandle, om); |
| 114 | + |
| 115 | + return true; |
| 116 | +} |
| 117 | + |
| 118 | +bool Pinetime::Controllers::HomeService::OnOpened() { |
| 119 | + return NotifyOpened(0); |
| 120 | +} |
| 121 | + |
| 122 | +void Pinetime::Controllers::HomeService::OnViewScreen(uint8_t n) { |
| 123 | + NotifyOpened(n); |
| 124 | +} |
| 125 | + |
| 126 | +void Pinetime::Controllers::HomeService::OnClosed() { |
| 127 | + dataUpdateTime = 0; |
| 128 | + numScreens = 0; |
| 129 | + currentScreen.reset(); |
| 130 | + |
| 131 | + NotifyOpened(-1); |
| 132 | +} |
| 133 | + |
| 134 | +void Pinetime::Controllers::HomeService::OnPressed(uint8_t screen, uint8_t componentId) { |
| 135 | + uint16_t connectionHandle = nimble.connHandle(); |
| 136 | + |
| 137 | + if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + uint8_t v[] = {screen, componentId}; |
| 142 | + auto* om = ble_hs_mbuf_from_flat(v, sizeof(v)); |
| 143 | + ble_gattc_notify_custom(connectionHandle, eventPressedHandle, om); |
| 144 | +} |
0 commit comments