Skip to content

Commit

Permalink
header size is 3 only for customHuami category
Browse files Browse the repository at this point in the history
header size should be 2 according to specification

fixes: #1895
  • Loading branch information
jmlich committed Jan 15, 2024
1 parent 264b5be commit 347487b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
7 changes: 4 additions & 3 deletions doc/ble.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,21 @@ InfiniTime uses the Alert Notification Service (ANS) for notifications. The rele
The new alert characteristic allows sending new notifications to InfiniTime. It requires the following format:

```
<category><amount>\x00<\x00-separated data>
<category><amount><\x00-separated data>
```

For example, here is what a normal notification looks like in Golang (language of `itd`):

```go
// \x00 is the category for simple alert, and there is one new notification, hence \x01.
"\x00\x01\x00Test Title\x00Test Body"
"\x00\x01Test Title\x00Test Body"
```

A call notification looks like so:

```go
// \x03 is the category for calls, and there is one new call notification, hence \x01.
"\x03\x01\x00Mary"
"\x03\x01Mary"
```

The `\x00` stands for hexadecimal `00` which means null.
Expand All @@ -180,6 +180,7 @@ Here is the list of categories and commands:
- Schedule: `7`
- High Prioritized Alert: `8`
- Instant Message: `9`
- Custom Huami: `xFA` - one extra byte for icon is added into header
- All Alerts: `0xFF`

These lists and information were retrieved from the following pages in the Nordic docs:
Expand Down
2 changes: 1 addition & 1 deletion src/components/ble/AlertNotificationClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ int AlertNotificationClient::OnDescriptorDiscoveryEventCallback(uint16_t connect
void AlertNotificationClient::OnNotification(ble_gap_event* event) {
if (event->notify_rx.attr_handle == newAlertHandle) {
constexpr size_t stringTerminatorSize = 1; // end of string '\0'
constexpr size_t headerSize = 3;
constexpr size_t headerSize = 2;
const auto maxMessageSize {NotificationManager::MaximumMessageSize()};
const auto maxBufferSize {maxMessageSize + headerSize};

Expand Down
16 changes: 11 additions & 5 deletions src/components/ble/AlertNotificationService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,29 @@ AlertNotificationService::AlertNotificationService(System::SystemTask& systemTas
int AlertNotificationService::OnAlert(struct ble_gatt_access_ctxt* ctxt) {
if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
constexpr size_t stringTerminatorSize = 1; // end of string '\0'
constexpr size_t headerSize = 3;
const auto maxMessageSize {NotificationManager::MaximumMessageSize()};
const auto maxBufferSize {maxMessageSize + headerSize};
size_t headerSize = 3;

// Ignore notifications with empty message
const auto packetLen = OS_MBUF_PKTLEN(ctxt->om);
if (packetLen <= headerSize) {
return 0;
}

Categories category;
os_mbuf_copydata(ctxt->om, 0, 1, &category);

if (category != Categories::CustomHuami) {
headerSize = 2;
}

const auto maxMessageSize {NotificationManager::MaximumMessageSize()};
const auto maxBufferSize {maxMessageSize + headerSize};

size_t bufferSize = std::min(packetLen + stringTerminatorSize, maxBufferSize);
auto messageSize = std::min(maxMessageSize, (bufferSize - headerSize));
Categories category;

NotificationManager::Notification notif;
os_mbuf_copydata(ctxt->om, headerSize, messageSize - 1, notif.message.data());
os_mbuf_copydata(ctxt->om, 0, 1, &category);
notif.message[messageSize - 1] = '\0';
notif.size = messageSize;

Expand Down
1 change: 1 addition & 0 deletions src/components/ble/AlertNotificationService.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace Pinetime {
Schedule = 0x07,
HighPrioritizedAlert = 0x08,
InstantMessage = 0x09,
CustomHuami = 0xFA,
All = 0xff
};

Expand Down

0 comments on commit 347487b

Please sign in to comment.