Skip to content

Commit 93d240a

Browse files
authored
Merge branch 'develop' into disable_notif_only
2 parents 40765f1 + 3eb7377 commit 93d240a

8 files changed

Lines changed: 121 additions & 69 deletions

File tree

.github/workflows/main.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ name: Build PineTime Firmware
99
# When to run this Workflow...
1010
on:
1111

12-
# Run this Workflow when files are updated (Pushed) in the "master" Branch
12+
# Run this Workflow when files are updated (Pushed) in the "master" and "develop" Branch
1313
push:
14-
branches: [ master ]
14+
branches: [ master, develop ]
1515

16-
# Also run this Workflow when a Pull Request is created or updated in the "master" Branch
16+
# Also run this Workflow when a Pull Request is created or updated in the "master" and "develop" Branch
1717
pull_request:
18-
branches: [ master ]
18+
branches: [ master, develop ]
1919

2020
# Steps to run for the Workflow
2121
jobs:

src/CMakeLists.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ set(SDK_SOURCE_FILES
9292
set(TINYCRYPT_SRC
9393
libs/mynewt-nimble/ext/tinycrypt/src/aes_encrypt.c
9494
libs/mynewt-nimble/ext/tinycrypt/src/utils.c
95+
libs/mynewt-nimble/ext/tinycrypt/src/cmac_mode.c
96+
libs/mynewt-nimble/ext/tinycrypt/src/ecc.c
97+
libs/mynewt-nimble/ext/tinycrypt/src/ecc_dh.c
9598
)
9699

97100
set(NIMBLE_SRC
@@ -104,6 +107,10 @@ set(NIMBLE_SRC
104107
libs/mynewt-nimble/nimble/host/src/ble_l2cap.c
105108
libs/mynewt-nimble/nimble/host/src/ble_hs_mbuf.c
106109
libs/mynewt-nimble/nimble/host/src/ble_sm.c
110+
libs/mynewt-nimble/nimble/host/src/ble_sm_cmd.c
111+
libs/mynewt-nimble/nimble/host/src/ble_sm_lgcy.c
112+
libs/mynewt-nimble/nimble/host/src/ble_sm_alg.c
113+
libs/mynewt-nimble/nimble/host/src/ble_sm_sc.c
107114
libs/mynewt-nimble/nimble/host/src/ble_gap.c
108115
libs/mynewt-nimble/nimble/host/src/ble_gatts.c
109116
libs/mynewt-nimble/nimble/host/src/ble_gattc.c
@@ -127,10 +134,6 @@ set(NIMBLE_SRC
127134
libs/mynewt-nimble/nimble/host/src/ble_hs_atomic.c
128135
libs/mynewt-nimble/nimble/host/src/ble_hs_adv.c
129136
libs/mynewt-nimble/nimble/host/src/ble_hs_flow.c
130-
libs/mynewt-nimble/nimble/host/src/ble_sm.c
131-
libs/mynewt-nimble/nimble/host/src/ble_sm_cmd.c
132-
libs/mynewt-nimble/nimble/host/src/ble_sm_lgcy.c
133-
libs/mynewt-nimble/nimble/host/src/ble_sm_alg.c
134137
libs/mynewt-nimble/nimble/host/src/ble_hs_mqueue.c
135138
libs/mynewt-nimble/nimble/host/src/ble_hs_stop.c
136139
libs/mynewt-nimble/nimble/host/src/ble_hs_startup.c

src/components/ble/NimbleController.cpp

Lines changed: 80 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
4242
serviceDiscovery({&currentTimeClient, &alertNotificationClient}) {
4343
}
4444

45+
void nimble_on_reset(int reason) {
46+
NRF_LOG_INFO("Resetting state; reason=%d\n", reason);
47+
}
48+
49+
void nimble_on_sync(void) {
50+
int rc;
51+
52+
rc = ble_hs_util_ensure_addr(0);
53+
ASSERT(rc == 0);
54+
55+
nptr->StartAdvertising();
56+
}
57+
4558
int GAPEventCallback(struct ble_gap_event* event, void* arg) {
4659
auto nimbleController = static_cast<NimbleController*>(arg);
4760
return nimbleController->OnGAPEvent(event);
@@ -51,6 +64,10 @@ void NimbleController::Init() {
5164
while (!ble_hs_synced()) {
5265
}
5366

67+
nptr = this;
68+
ble_hs_cfg.reset_cb = nimble_on_reset;
69+
ble_hs_cfg.sync_cb = nimble_on_sync;
70+
5471
ble_svc_gap_init();
5572
ble_svc_gatt_init();
5673

@@ -64,28 +81,31 @@ void NimbleController::Init() {
6481
batteryInformationService.Init();
6582
immediateAlertService.Init();
6683
heartRateService.Init();
67-
int res;
68-
res = ble_hs_util_ensure_addr(0);
69-
ASSERT(res == 0);
70-
res = ble_hs_id_infer_auto(0, &addrType);
71-
ASSERT(res == 0);
72-
res = ble_svc_gap_device_name_set(deviceName);
73-
ASSERT(res == 0);
84+
85+
int rc;
86+
rc = ble_hs_util_ensure_addr(0);
87+
ASSERT(rc == 0);
88+
rc = ble_hs_id_infer_auto(0, &addrType);
89+
ASSERT(rc == 0);
90+
rc = ble_svc_gap_device_name_set(deviceName);
91+
ASSERT(rc == 0);
92+
rc = ble_svc_gap_device_appearance_set(0xC2);
93+
ASSERT(rc == 0);
7494
Pinetime::Controllers::Ble::BleAddress address;
75-
res = ble_hs_id_copy_addr(addrType, address.data(), nullptr);
76-
ASSERT(res == 0);
95+
rc = ble_hs_id_copy_addr(addrType, address.data(), nullptr);
96+
ASSERT(rc == 0);
7797
bleController.AddressType((addrType == 0) ? Ble::AddressTypes::Public : Ble::AddressTypes::Random);
7898
bleController.Address(std::move(address));
7999

80-
res = ble_gatts_start();
81-
ASSERT(res == 0);
100+
rc = ble_gatts_start();
101+
ASSERT(rc == 0);
102+
103+
if (!ble_gap_adv_active() && !bleController.IsConnected())
104+
StartAdvertising();
82105
}
83106

84107
void NimbleController::StartAdvertising() {
85-
if (bleController.IsConnected() || ble_gap_conn_active() || ble_gap_adv_active())
86-
return;
87-
88-
ble_svc_gap_device_name_set(deviceName);
108+
int rc;
89109

90110
/* set adv parameters */
91111
struct ble_gap_adv_params adv_params;
@@ -102,11 +122,17 @@ void NimbleController::StartAdvertising() {
102122

103123
adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
104124
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
125+
/* fast advertise for 30 sec */
126+
if (fastAdvCount < 15) {
127+
adv_params.itvl_min = 32;
128+
adv_params.itvl_max = 47;
129+
fastAdvCount++;
130+
} else {
131+
adv_params.itvl_min = 1636;
132+
adv_params.itvl_max = 1651;
133+
}
105134

106135
fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
107-
// fields.uuids128 = BLE_UUID128(BLE_UUID128_DECLARE(
108-
// 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
109-
// 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff));
110136
fields.uuids128 = &dfuServiceUuid;
111137
fields.num_uuids128 = 1;
112138
fields.uuids128_is_complete = 1;
@@ -116,64 +142,70 @@ void NimbleController::StartAdvertising() {
116142
rsp_fields.name_len = strlen(deviceName);
117143
rsp_fields.name_is_complete = 1;
118144

119-
ble_gap_adv_set_fields(&fields);
120-
// ASSERT(res == 0); // TODO this one sometimes fails with error 22 (notsync)
145+
rc = ble_gap_adv_set_fields(&fields);
146+
ASSERT(rc == 0);
121147

122-
ble_gap_adv_rsp_set_fields(&rsp_fields);
123-
// ASSERT(res == 0);
148+
rc = ble_gap_adv_rsp_set_fields(&rsp_fields);
149+
ASSERT(rc == 0);
124150

125-
ble_gap_adv_start(addrType, NULL, 180000, &adv_params, GAPEventCallback, this);
126-
// ASSERT(res == 0);// TODO I've disabled these ASSERT as they sometime asserts and reset the mcu.
127-
// For now, the advertising is restarted as soon as it ends. There may be a race condition
128-
// that prevent the advertising from restarting reliably.
129-
// I remove the assert to prevent this unnecessary crash, but in the long term, the management of
130-
// the advertising should be improve (better error handling, and advertise for 3 minutes after
131-
// the application has been woken up, for example.
151+
rc = ble_gap_adv_start(addrType, NULL, 2000, &adv_params, GAPEventCallback, this);
152+
ASSERT(rc == 0);
132153
}
133154

134155
int NimbleController::OnGAPEvent(ble_gap_event* event) {
135156
switch (event->type) {
136157
case BLE_GAP_EVENT_ADV_COMPLETE:
137158
NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_ADV_COMPLETE");
138-
NRF_LOG_INFO("advertise complete; reason=%dn status=%d", event->adv_complete.reason, event->connect.status);
159+
NRF_LOG_INFO("reason=%d; status=%d", event->adv_complete.reason, event->connect.status);
160+
StartAdvertising();
139161
break;
140-
case BLE_GAP_EVENT_CONNECT: {
162+
163+
case BLE_GAP_EVENT_CONNECT:
141164
NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_CONNECT");
142165

143166
/* A new connection was established or a connection attempt failed. */
144167
NRF_LOG_INFO("connection %s; status=%d ", event->connect.status == 0 ? "established" : "failed", event->connect.status);
145168

146169
if (event->connect.status != 0) {
147170
/* Connection failed; resume advertising. */
148-
StartAdvertising();
171+
currentTimeClient.Reset();
172+
alertNotificationClient.Reset();
173+
connectionHandle = BLE_HS_CONN_HANDLE_NONE;
149174
bleController.Disconnect();
175+
fastAdvCount = 0;
176+
StartAdvertising();
150177
} else {
178+
connectionHandle = event->connect.conn_handle;
151179
bleController.Connect();
152180
systemTask.PushMessage(Pinetime::System::Messages::BleConnected);
153-
connectionHandle = event->connect.conn_handle;
154-
// Service discovery is deffered via systemtask
181+
// Service discovery is deferred via systemtask
155182
}
156-
} break;
183+
break;
184+
157185
case BLE_GAP_EVENT_DISCONNECT:
158186
NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_DISCONNECT");
159-
NRF_LOG_INFO("disconnect; reason=%d", event->disconnect.reason);
187+
NRF_LOG_INFO("disconnect reason=%d", event->disconnect.reason);
160188

161189
/* Connection terminated; resume advertising. */
162190
currentTimeClient.Reset();
163191
alertNotificationClient.Reset();
164192
connectionHandle = BLE_HS_CONN_HANDLE_NONE;
165193
bleController.Disconnect();
194+
fastAdvCount = 0;
166195
StartAdvertising();
167196
break;
197+
168198
case BLE_GAP_EVENT_CONN_UPDATE:
169199
NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_CONN_UPDATE");
170200
/* The central has updated the connection parameters. */
171-
NRF_LOG_INFO("connection updated; status=%d ", event->conn_update.status);
201+
NRF_LOG_INFO("update status=%d ", event->conn_update.status);
172202
break;
203+
173204
case BLE_GAP_EVENT_ENC_CHANGE:
174205
/* Encryption has been enabled or disabled for this connection. */
175206
NRF_LOG_INFO("encryption change event; status=%d ", event->enc_change.status);
176-
return 0;
207+
break;
208+
177209
case BLE_GAP_EVENT_SUBSCRIBE:
178210
NRF_LOG_INFO("subscribe event; conn_handle=%d attr_handle=%d "
179211
"reason=%d prevn=%d curn=%d previ=%d curi=???\n",
@@ -183,10 +215,12 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
183215
event->subscribe.prev_notify,
184216
event->subscribe.cur_notify,
185217
event->subscribe.prev_indicate);
186-
return 0;
218+
break;
219+
187220
case BLE_GAP_EVENT_MTU:
188-
NRF_LOG_INFO("mtu update event; conn_handle=%d cid=%d mtu=%d\n", event->mtu.conn_handle, event->mtu.channel_id, event->mtu.value);
189-
return 0;
221+
NRF_LOG_INFO("mtu update event; conn_handle=%d cid=%d mtu=%d\n",
222+
event->mtu.conn_handle, event->mtu.channel_id, event->mtu.value);
223+
break;
190224

191225
case BLE_GAP_EVENT_REPEAT_PAIRING: {
192226
/* We already have a bond with the peer, but it is attempting to
@@ -217,8 +251,7 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
217251
notifSize);
218252

219253
alertNotificationClient.OnNotification(event);
220-
return 0;
221-
}
254+
} break;
222255
/* Attribute data is contained in event->notify_rx.attr_data. */
223256

224257
default:
@@ -229,15 +262,17 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
229262
}
230263

231264
void NimbleController::StartDiscovery() {
232-
serviceDiscovery.StartDiscovery(connectionHandle);
265+
if (connectionHandle != BLE_HS_CONN_HANDLE_NONE) {
266+
serviceDiscovery.StartDiscovery(connectionHandle);
267+
}
233268
}
234269

235270
uint16_t NimbleController::connHandle() {
236271
return connectionHandle;
237272
}
238273

239274
void NimbleController::NotifyBatteryLevel(uint8_t level) {
240-
if(connectionHandle != BLE_HS_CONN_HANDLE_NONE) {
275+
if (connectionHandle != BLE_HS_CONN_HANDLE_NONE) {
241276
batteryInformationService.NotifyBatteryLevel(connectionHandle, level);
242277
}
243278
}

src/components/ble/NimbleController.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ namespace Pinetime {
7272
uint16_t connHandle();
7373
void NotifyBatteryLevel(uint8_t level);
7474

75+
void RestartFastAdv() {
76+
fastAdvCount = 0;
77+
}
78+
7579
private:
7680
static constexpr const char* deviceName = "InfiniTime";
7781
Pinetime::System::SystemTask& systemTask;
@@ -94,12 +98,15 @@ namespace Pinetime {
9498

9599
uint8_t addrType; // 1 = Random, 0 = PUBLIC
96100
uint16_t connectionHandle = BLE_HS_CONN_HANDLE_NONE;
101+
uint8_t fastAdvCount = 0;
97102

98103
ble_uuid128_t dfuServiceUuid {
99104
.u {.type = BLE_UUID_TYPE_128},
100105
.value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, 0xDE, 0xEF, 0x12, 0x12, 0x30, 0x15, 0x00, 0x00}};
101106

102107
ServiceDiscovery serviceDiscovery;
103108
};
109+
110+
static NimbleController* nptr;
104111
}
105112
}

src/components/motor/MotorController.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

33
#include <cstdint>
4-
#include "app_timer.h"
54

65
namespace Pinetime {
76
namespace Controllers {

src/displayapp/DisplayAppRecovery.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <components/rle/RleDecoder.h>
66
#include <touchhandler/TouchHandler.h>
77
#include "displayapp/icons/infinitime/infinitime-nb.c"
8+
#include "components/ble/BleController.h"
89

910
using namespace Pinetime::Applications;
1011

src/displayapp/DisplayAppRecovery.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,38 @@
66
#include <bits/unique_ptr.h>
77
#include <queue.h>
88
#include "components/gfx/Gfx.h"
9-
#include "components/battery/BatteryController.h"
10-
#include "components/brightness/BrightnessController.h"
11-
#include "components/ble/BleController.h"
12-
#include "components/datetime/DateTimeController.h"
13-
#include "components/ble/NotificationManager.h"
14-
#include "components/firmwarevalidator/FirmwareValidator.h"
159
#include "drivers/Cst816s.h"
1610
#include <date/date.h>
1711
#include <drivers/Watchdog.h>
18-
#include <components/heartrate/HeartRateController.h>
19-
#include <components/motion/MotionController.h>
2012
#include <components/motor/MotorController.h>
21-
#include <components/settings/Settings.h>
2213
#include "TouchEvents.h"
2314
#include "Apps.h"
2415
#include "Messages.h"
2516
#include "DummyLittleVgl.h"
26-
#include "components/timer/TimerController.h"
2717

2818
namespace Pinetime {
29-
namespace System {
30-
class SystemTask;
31-
};
19+
namespace Drivers {
20+
class St7789;
21+
class Cst816S;
22+
class WatchdogView;
23+
}
3224
namespace Controllers {
25+
class Settings;
26+
class Battery;
27+
class Ble;
28+
class DateTime;
29+
class NotificationManager;
30+
class HeartRateController;
31+
class MotionController;
3332
class TouchHandler;
33+
class MotorController;
34+
class TimerController;
3435
}
36+
37+
namespace System {
38+
class SystemTask;
39+
};
40+
3541
namespace Applications {
3642
class DisplayApp {
3743
public:

0 commit comments

Comments
 (0)