summaryrefslogtreecommitdiff
path: root/src/Components/Ble/NimbleController.cpp
diff options
context:
space:
mode:
authorJF <jf@codingfield.com>2020-04-22 20:19:36 +0200
committerJF <jf@codingfield.com>2020-04-22 20:19:36 +0200
commita9254ee90e835b6a187d1fcd6f8850decca5bc89 (patch)
treeab6e63c48c0b9efe1dd8f2e8d5ba1137211df207 /src/Components/Ble/NimbleController.cpp
parent2c9ce1cfc7d4c733b1b35f51a1f6f5da332cf3fa (diff)
NimbleController : support CTS
Diffstat (limited to 'src/Components/Ble/NimbleController.cpp')
-rw-r--r--src/Components/Ble/NimbleController.cpp79
1 files changed, 78 insertions, 1 deletions
diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp
index eaad9077..f3add36d 100644
--- a/src/Components/Ble/NimbleController.cpp
+++ b/src/Components/Ble/NimbleController.cpp
@@ -1,3 +1,5 @@
+#include <Components/DateTime/DateTimeController.h>
+
#include "NimbleController.h"
#include <services/gatt/ble_svc_gatt.h>
#include <services/gap/ble_svc_gap.h>
@@ -5,15 +7,41 @@
#include <host/ble_hs_id.h>
#include <host/ble_hs.h>
#include <host/ble_gap.h>
-
+#include <hal/nrf_rtc.h>
using namespace Pinetime::Controllers;
+NimbleController::NimbleController(DateTime& datetimeController) : dateTimeController{datetimeController} {
+ ctsUuid.u.type = BLE_UUID_TYPE_16;
+ ctsUuid.value = BleGatServiceCts;
+
+ ctsCurrentTimeUuid.u.type = BLE_UUID_TYPE_16;
+ ctsCurrentTimeUuid.value = bleGattCharacteristicCurrentTime;
+}
+
int GAPEventCallback(struct ble_gap_event *event, void *arg) {
auto nimbleController = static_cast<NimbleController*>(arg);
return nimbleController->OnGAPEvent(event);
}
+int DiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
+ const struct ble_gatt_svc *service, void *arg) {
+ auto nimbleController = static_cast<NimbleController*>(arg);
+ return nimbleController->OnDiscoveryEvent(conn_handle, error, service);
+}
+
+int CharacteristicDiscoveredCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
+ const struct ble_gatt_chr *chr, void *arg) {
+ auto nimbleController = static_cast<NimbleController*>(arg);
+ return nimbleController->OnCharacteristicDiscoveryEvent(conn_handle, error, chr);
+}
+
+static int CurrentTimeReadCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
+ struct ble_gatt_attr *attr, void *arg) {
+ auto nimbleController = static_cast<NimbleController*>(arg);
+ return nimbleController->OnCurrentTimeReadResult(conn_handle, error, attr);
+}
+
void NimbleController::Init() {
while (!ble_hs_synced()) {}
@@ -87,6 +115,10 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) {
if (event->connect.status != 0) {
/* Connection failed; resume advertising. */
StartAdvertising();
+ } else {
+ connectionHandle = event->connect.conn_handle;
+
+ ble_gattc_disc_svc_by_uuid(connectionHandle, ((ble_uuid_t*)&ctsUuid), DiscoveryEventCallback, this);
}
}
break;
@@ -146,3 +178,48 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) {
return 0;
}
+int NimbleController::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service) {
+ if(service == nullptr && error->status == BLE_HS_EDONE)
+ NRF_LOG_INFO("Discovery complete");
+
+ if(service != nullptr && ble_uuid_cmp(((ble_uuid_t*)&ctsUuid), &service->uuid.u) == 0) {
+ NRF_LOG_INFO("CTS discovered : 0x%x", service->start_handle);
+ ble_gattc_disc_chrs_by_uuid(connectionHandle, service->start_handle, service->end_handle, ((ble_uuid_t*)&ctsCurrentTimeUuid), CharacteristicDiscoveredCallback, this);
+ }
+
+ return 0;
+}
+
+int NimbleController::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error,
+ const ble_gatt_chr *characteristic) {
+ if(characteristic == nullptr && error->status == BLE_HS_EDONE)
+ NRF_LOG_INFO("Characteristic discovery complete");
+
+ if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&ctsCurrentTimeUuid), &characteristic->uuid.u) == 0) {
+ NRF_LOG_INFO("CTS Characteristic discovered : 0x%x", characteristic->val_handle);
+
+ ble_gattc_read(conn_handle, characteristic->val_handle, CurrentTimeReadCallback, this);
+ }
+ return 0;
+}
+
+int NimbleController::OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_attr *attribute) {
+ if(error->status == 0) {
+ // TODO check that attribute->handle equals the handle discovered in OnCharacteristicDiscoveryEvent
+ CtsData result;
+ os_mbuf_copydata(attribute->om, 0, sizeof(CtsData), &result);
+ NRF_LOG_INFO("Received data: %d-%d-%d %d:%d:%d", result.year,
+ result.month, result.dayofmonth,
+ result.hour, result.minute, result.second);
+ dateTimeController.SetTime(result.year, result.month, result.dayofmonth,
+ 0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG));
+ } else {
+ NRF_LOG_INFO("Error retrieving current time: %d", error->status);
+ }
+ return 0;
+}
+
+
+
+
+