summaryrefslogtreecommitdiff
path: root/src/components/ble
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/ble')
-rw-r--r--src/components/ble/AlertNotificationClient.cpp1
-rw-r--r--src/components/ble/BleController.cpp16
-rw-r--r--src/components/ble/BleController.h9
-rw-r--r--src/components/ble/DfuService.cpp1
-rw-r--r--src/components/ble/HeartRateService.cpp1
-rw-r--r--src/components/ble/MotionService.cpp1
-rw-r--r--src/components/ble/MusicService.cpp21
-rw-r--r--src/components/ble/NimbleController.cpp43
-rw-r--r--src/components/ble/NimbleController.h35
9 files changed, 91 insertions, 37 deletions
diff --git a/src/components/ble/AlertNotificationClient.cpp b/src/components/ble/AlertNotificationClient.cpp
index 0f850874..335845e3 100644
--- a/src/components/ble/AlertNotificationClient.cpp
+++ b/src/components/ble/AlertNotificationClient.cpp
@@ -2,6 +2,7 @@
#include <algorithm>
#include "components/ble/NotificationManager.h"
#include "systemtask/SystemTask.h"
+#include <nrf_log.h>
using namespace Pinetime::Controllers;
constexpr ble_uuid16_t AlertNotificationClient::ansServiceUuid;
diff --git a/src/components/ble/BleController.cpp b/src/components/ble/BleController.cpp
index a80c9719..b6b7383a 100644
--- a/src/components/ble/BleController.cpp
+++ b/src/components/ble/BleController.cpp
@@ -2,6 +2,10 @@
using namespace Pinetime::Controllers;
+bool Ble::IsConnected() const {
+ return isConnected;
+}
+
void Ble::Connect() {
isConnected = true;
}
@@ -10,6 +14,18 @@ void Ble::Disconnect() {
isConnected = false;
}
+bool Ble::IsRadioEnabled() const {
+ return isRadioEnabled;
+}
+
+void Ble::EnableRadio() {
+ isRadioEnabled = true;
+}
+
+void Ble::DisableRadio() {
+ isRadioEnabled = false;
+}
+
void Ble::StartFirmwareUpdate() {
isFirmwareUpdating = true;
}
diff --git a/src/components/ble/BleController.h b/src/components/ble/BleController.h
index 72b87663..675ede2d 100644
--- a/src/components/ble/BleController.h
+++ b/src/components/ble/BleController.h
@@ -12,12 +12,14 @@ namespace Pinetime {
enum class AddressTypes { Public, Random, RPA_Public, RPA_Random };
Ble() = default;
- bool IsConnected() const {
- return isConnected;
- }
+ bool IsConnected() const;
void Connect();
void Disconnect();
+ bool IsRadioEnabled() const;
+ void EnableRadio();
+ void DisableRadio();
+
void StartFirmwareUpdate();
void StopFirmwareUpdate();
void FirmwareUpdateTotalBytes(uint32_t totalBytes);
@@ -57,6 +59,7 @@ namespace Pinetime {
private:
bool isConnected = false;
+ bool isRadioEnabled = true;
bool isFirmwareUpdating = false;
uint32_t firmwareUpdateTotalBytes = 0;
uint32_t firmwareUpdateCurrentBytes = 0;
diff --git a/src/components/ble/DfuService.cpp b/src/components/ble/DfuService.cpp
index 71dcc7e6..cf99f01f 100644
--- a/src/components/ble/DfuService.cpp
+++ b/src/components/ble/DfuService.cpp
@@ -3,6 +3,7 @@
#include "components/ble/BleController.h"
#include "drivers/SpiNorFlash.h"
#include "systemtask/SystemTask.h"
+#include <nrf_log.h>
using namespace Pinetime::Controllers;
diff --git a/src/components/ble/HeartRateService.cpp b/src/components/ble/HeartRateService.cpp
index f178af79..4824a6b3 100644
--- a/src/components/ble/HeartRateService.cpp
+++ b/src/components/ble/HeartRateService.cpp
@@ -1,6 +1,7 @@
#include "components/ble/HeartRateService.h"
#include "components/heartrate/HeartRateController.h"
#include "systemtask/SystemTask.h"
+#include <nrf_log.h>
using namespace Pinetime::Controllers;
diff --git a/src/components/ble/MotionService.cpp b/src/components/ble/MotionService.cpp
index 6381915d..87923c23 100644
--- a/src/components/ble/MotionService.cpp
+++ b/src/components/ble/MotionService.cpp
@@ -1,6 +1,7 @@
#include "components/ble/MotionService.h"
#include "components/motion/MotionController.h"
#include "systemtask/SystemTask.h"
+#include <nrf_log.h>
using namespace Pinetime::Controllers;
diff --git a/src/components/ble/MusicService.cpp b/src/components/ble/MusicService.cpp
index 3457ce4c..c99aa1e3 100644
--- a/src/components/ble/MusicService.cpp
+++ b/src/components/ble/MusicService.cpp
@@ -17,6 +17,7 @@
*/
#include "components/ble/MusicService.h"
#include "systemtask/SystemTask.h"
+#include <cstring>
namespace {
// 0000yyxx-78fc-48fe-8e23-433b3a1942d0
@@ -47,6 +48,8 @@ namespace {
constexpr ble_uuid128_t msRepeatCharUuid {CharUuid(0x0b, 0x00)};
constexpr ble_uuid128_t msShuffleCharUuid {CharUuid(0x0c, 0x00)};
+ constexpr uint8_t MaxStringSize {40};
+
int MusicCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
return static_cast<Pinetime::Controllers::MusicService*>(arg)->OnCommand(conn_handle, attr_handle, ctxt);
}
@@ -125,9 +128,21 @@ void Pinetime::Controllers::MusicService::Init() {
int Pinetime::Controllers::MusicService::OnCommand(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) {
if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
size_t notifSize = OS_MBUF_PKTLEN(ctxt->om);
- char data[notifSize + 1];
- data[notifSize] = '\0';
- os_mbuf_copydata(ctxt->om, 0, notifSize, data);
+ size_t bufferSize = notifSize;
+ if (notifSize > MaxStringSize) {
+ bufferSize = MaxStringSize;
+ }
+
+ char data[bufferSize + 1];
+ os_mbuf_copydata(ctxt->om, 0, bufferSize, data);
+
+ if (notifSize > bufferSize) {
+ data[bufferSize-1] = '.';
+ data[bufferSize-2] = '.';
+ data[bufferSize-3] = '.';
+ }
+ data[bufferSize] = '\0';
+
char* s = &data[0];
if (ble_uuid_cmp(ctxt->chr->uuid, &msArtistCharUuid.u) == 0) {
artistName = s;
diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp
index c7ceed32..d2d6dc03 100644
--- a/src/components/ble/NimbleController.cpp
+++ b/src/components/ble/NimbleController.cpp
@@ -2,6 +2,7 @@
#include <cstring>
#include <hal/nrf_rtc.h>
+#include <nrf_log.h>
#define min // workaround: nimble's min/max macros conflict with libstdc++
#define max
#include <host/ble_gap.h>
@@ -23,14 +24,14 @@
using namespace Pinetime::Controllers;
NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
- Pinetime::Controllers::Ble& bleController,
+ Ble& bleController,
DateTime& dateTimeController,
- Pinetime::Controllers::NotificationManager& notificationManager,
- Controllers::Battery& batteryController,
+ NotificationManager& notificationManager,
+ Battery& batteryController,
Pinetime::Drivers::SpiNorFlash& spiNorFlash,
- Controllers::HeartRateController& heartRateController,
- Controllers::MotionController& motionController,
- Controllers::FS& fs)
+ HeartRateController& heartRateController,
+ MotionController& motionController,
+ FS& fs)
: systemTask {systemTask},
bleController {bleController},
dateTimeController {dateTimeController},
@@ -75,6 +76,7 @@ int GAPEventCallback(struct ble_gap_event* event, void* arg) {
void NimbleController::Init() {
while (!ble_hs_synced()) {
+ vTaskDelay(10);
}
nptr = this;
@@ -183,7 +185,9 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
case BLE_GAP_EVENT_ADV_COMPLETE:
NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_ADV_COMPLETE");
NRF_LOG_INFO("reason=%d; status=%0X", event->adv_complete.reason, event->connect.status);
- StartAdvertising();
+ if (bleController.IsRadioEnabled() && !bleController.IsConnected()) {
+ StartAdvertising();
+ }
break;
case BLE_GAP_EVENT_CONNECT:
@@ -219,9 +223,11 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
currentTimeClient.Reset();
alertNotificationClient.Reset();
connectionHandle = BLE_HS_CONN_HANDLE_NONE;
- bleController.Disconnect();
- fastAdvCount = 0;
- StartAdvertising();
+ if(bleController.IsConnected()) {
+ bleController.Disconnect();
+ fastAdvCount = 0;
+ StartAdvertising();
+ }
break;
case BLE_GAP_EVENT_CONN_UPDATE:
@@ -396,6 +402,23 @@ void NimbleController::NotifyBatteryLevel(uint8_t level) {
}
}
+void NimbleController::EnableRadio() {
+ bleController.EnableRadio();
+ bleController.Disconnect();
+ fastAdvCount = 0;
+ StartAdvertising();
+}
+
+void NimbleController::DisableRadio() {
+ bleController.DisableRadio();
+ if (bleController.IsConnected()) {
+ ble_gap_terminate(connectionHandle, BLE_ERR_REM_USER_CONN_TERM);
+ bleController.Disconnect();
+ } else {
+ ble_gap_adv_stop();
+ }
+}
+
void NimbleController::PersistBond(struct ble_gap_conn_desc& desc) {
union ble_store_key key;
union ble_store_value our_sec, peer_sec, peer_cccd_set[MYNEWT_VAL(BLE_STORE_MAX_CCCDS)] = {0};
diff --git a/src/components/ble/NimbleController.h b/src/components/ble/NimbleController.h
index 6c409740..000231fe 100644
--- a/src/components/ble/NimbleController.h
+++ b/src/components/ble/NimbleController.h
@@ -14,6 +14,7 @@
#include "components/ble/CurrentTimeService.h"
#include "components/ble/DeviceInformationService.h"
#include "components/ble/DfuService.h"
+#include "components/ble/FSService.h"
#include "components/ble/HeartRateService.h"
#include "components/ble/ImmediateAlertService.h"
#include "components/ble/MusicService.h"
@@ -22,7 +23,6 @@
#include "components/ble/MotionService.h"
#include "components/ble/weather/WeatherService.h"
#include "components/fs/FS.h"
-#include "components/ble/FSService.h"
namespace Pinetime {
namespace Drivers {
@@ -42,27 +42,17 @@ namespace Pinetime {
public:
NimbleController(Pinetime::System::SystemTask& systemTask,
- Pinetime::Controllers::Ble& bleController,
+ Ble& bleController,
DateTime& dateTimeController,
- Pinetime::Controllers::NotificationManager& notificationManager,
- Controllers::Battery& batteryController,
+ NotificationManager& notificationManager,
+ Battery& batteryController,
Pinetime::Drivers::SpiNorFlash& spiNorFlash,
- Controllers::HeartRateController& heartRateController,
- Controllers::MotionController& motionController,
- Pinetime::Controllers::FS& fs);
+ HeartRateController& heartRateController,
+ MotionController& motionController,
+ FS& fs);
void Init();
void StartAdvertising();
int OnGAPEvent(ble_gap_event* event);
-
- int OnDiscoveryEvent(uint16_t i, const ble_gatt_error* pError, const ble_gatt_svc* pSvc);
- int OnCTSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_chr* characteristic);
- int OnANSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_chr* characteristic);
- int OnCurrentTimeReadResult(uint16_t connectionHandle, const ble_gatt_error* error, ble_gatt_attr* attribute);
- int OnANSDescriptorDiscoveryEventCallback(uint16_t connectionHandle,
- const ble_gatt_error* error,
- uint16_t characteristicValueHandle,
- const ble_gatt_dsc* descriptor);
-
void StartDiscovery();
Pinetime::Controllers::MusicService& music() {
@@ -83,7 +73,10 @@ namespace Pinetime {
void RestartFastAdv() {
fastAdvCount = 0;
- }
+ };
+
+ void EnableRadio();
+ void DisableRadio();
private:
void PersistBond(struct ble_gap_conn_desc& desc);
@@ -91,11 +84,11 @@ namespace Pinetime {
static constexpr const char* deviceName = "InfiniTime";
Pinetime::System::SystemTask& systemTask;
- Pinetime::Controllers::Ble& bleController;
+ Ble& bleController;
DateTime& dateTimeController;
Pinetime::Drivers::SpiNorFlash& spiNorFlash;
- Pinetime::Controllers::FS& fs;
- Pinetime::Controllers::DfuService dfuService;
+ FS& fs;
+ DfuService dfuService;
DeviceInformationService deviceInformationService;
CurrentTimeClient currentTimeClient;