|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Unlicense OR CC0-1.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdlib.h> |
| 8 | +#include <string.h> |
| 9 | +#include <inttypes.h> |
| 10 | +#include "esp_log.h" |
| 11 | +#include "ble_ancs.h" |
| 12 | + |
| 13 | +#define NimBLE_ANCS_TAG "NimBLE_ANCS" |
| 14 | + |
| 15 | +/* |
| 16 | +| EventID(1 Byte) | EventFlags(1 Byte) | CategoryID(1 Byte) | CategoryCount(1 Byte) | NotificationUID(4 Bytes) | |
| 17 | +
|
| 18 | +A GATT notification delivered through the Notification Source characteristic contains the following information: |
| 19 | +* EventID: This field informs the accessory whether the given iOS notification was added, modified, or removed. The enumerated values for this field are defined |
| 20 | + in EventID Values. |
| 21 | +* EventFlags: A bitmask whose set bits inform an NC of specificities with the iOS notification. For example, if an iOS notification is considered “important”, |
| 22 | + the NC may want to display a more aggressive user interface (UI) to make sure the user is properly alerted. The enumerated bits for this field |
| 23 | + are defined in EventFlags. |
| 24 | +* CategoryID: A numerical value providing a category in which the iOS notification can be classified. The NP will make a best effort to provide an accurate category |
| 25 | + for each iOS notification. The enumerated values for this field are defined in CategoryID Values. |
| 26 | +* CategoryCount: The current number of active iOS notifications in the given category. For example, if two unread emails are sitting in a user’s email inbox, and a new |
| 27 | + email is pushed to the user’s iOS device, the value of CategoryCount is 3. |
| 28 | +* NotificationUID: A 32-bit numerical value that is the unique identifier (UID) for the iOS notification. This value can be used as a handle in commands sent to the |
| 29 | + Control Point characteristic to interact with the iOS notification. |
| 30 | +*/ |
| 31 | + |
| 32 | +char *EventID_to_String(uint8_t EventID) |
| 33 | +{ |
| 34 | + char *str = NULL; |
| 35 | + switch (EventID) |
| 36 | + { |
| 37 | + case EventIDNotificationAdded: |
| 38 | + str = "New message"; |
| 39 | + break; |
| 40 | + case EventIDNotificationModified: |
| 41 | + str = "Modified message"; |
| 42 | + break; |
| 43 | + case EventIDNotificationRemoved: |
| 44 | + str = "Removed message"; |
| 45 | + break; |
| 46 | + default: |
| 47 | + str = "unknown EventID"; |
| 48 | + break; |
| 49 | + } |
| 50 | + return str; |
| 51 | +} |
| 52 | + |
| 53 | +char *CategoryID_to_String(uint8_t CategoryID) |
| 54 | +{ |
| 55 | + char *Cidstr = NULL; |
| 56 | + switch(CategoryID) { |
| 57 | + case CategoryIDOther: |
| 58 | + Cidstr = "Other"; |
| 59 | + break; |
| 60 | + case CategoryIDIncomingCall: |
| 61 | + Cidstr = "IncomingCall"; |
| 62 | + break; |
| 63 | + case CategoryIDMissedCall: |
| 64 | + Cidstr = "MissedCall"; |
| 65 | + break; |
| 66 | + case CategoryIDVoicemail: |
| 67 | + Cidstr = "Voicemail"; |
| 68 | + break; |
| 69 | + case CategoryIDSocial: |
| 70 | + Cidstr = "Social"; |
| 71 | + break; |
| 72 | + case CategoryIDSchedule: |
| 73 | + Cidstr = "Schedule"; |
| 74 | + break; |
| 75 | + case CategoryIDEmail: |
| 76 | + Cidstr = "Email"; |
| 77 | + break; |
| 78 | + case CategoryIDNews: |
| 79 | + Cidstr = "News"; |
| 80 | + break; |
| 81 | + case CategoryIDHealthAndFitness: |
| 82 | + Cidstr = "HealthAndFitness"; |
| 83 | + break; |
| 84 | + case CategoryIDBusinessAndFinance: |
| 85 | + Cidstr = "BusinessAndFinance"; |
| 86 | + break; |
| 87 | + case CategoryIDLocation: |
| 88 | + Cidstr = "Location"; |
| 89 | + break; |
| 90 | + case CategoryIDEntertainment: |
| 91 | + Cidstr = "Entertainment"; |
| 92 | + break; |
| 93 | + default: |
| 94 | + Cidstr = "Unknown CategoryID"; |
| 95 | + break; |
| 96 | + } |
| 97 | + return Cidstr; |
| 98 | +} |
| 99 | + |
| 100 | +/* |
| 101 | +| EventID(1 Byte) | EventFlags(1 Byte) | CategoryID(1 Byte) | CategoryCount(1 Byte) | NotificationUID(4 Bytes) | |
| 102 | +*/ |
| 103 | + |
| 104 | +void ble_receive_apple_notification_source(uint8_t *message, uint16_t message_len) |
| 105 | +{ |
| 106 | + if (!message || message_len < 5) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + uint8_t EventID = message[0]; |
| 111 | + char *EventIDS = EventID_to_String(EventID); |
| 112 | + uint8_t EventFlags = message[1]; |
| 113 | + uint8_t CategoryID = message[2]; |
| 114 | + char *Cidstr = CategoryID_to_String(CategoryID); |
| 115 | + uint8_t CategoryCount = message[3]; |
| 116 | + uint32_t NotificationUID = (message[4]) | (message[5]<< 8) | (message[6]<< 16) | (message[7] << 24); |
| 117 | + ESP_LOGI(NimBLE_ANCS_TAG, "EventID:%s EventFlags:0x%x CategoryID:%s CategoryCount:%d NotificationUID:%" PRIu32, EventIDS, EventFlags, Cidstr, CategoryCount, NotificationUID); |
| 118 | +} |
| 119 | + |
| 120 | +void ble_receive_apple_data_source(uint8_t *message, uint16_t message_len) |
| 121 | +{ |
| 122 | + if (!message || message_len == 0) { |
| 123 | + return; |
| 124 | + } |
| 125 | + uint8_t Command_id = message[0]; |
| 126 | + switch (Command_id) |
| 127 | + { |
| 128 | + case CommandIDGetNotificationAttributes: { |
| 129 | + uint32_t NotificationUID = (message[1]) | (message[2]<< 8) | (message[3]<< 16) | (message[4] << 24); |
| 130 | + uint32_t remian_attr_len = message_len - 5; |
| 131 | + uint8_t *attrs = &message[5]; |
| 132 | + ESP_LOGI(NimBLE_ANCS_TAG, "recevice Notification Attributes response Command_id %d NotificationUID %" PRIu32, Command_id, NotificationUID); |
| 133 | + while(remian_attr_len > 0) { |
| 134 | + uint8_t AttributeID = attrs[0]; |
| 135 | + uint16_t len = attrs[1] | (attrs[2] << 8); |
| 136 | + if(len > (remian_attr_len -3)) { |
| 137 | + ESP_LOGE(NimBLE_ANCS_TAG, "data error"); |
| 138 | + break; |
| 139 | + } |
| 140 | + switch (AttributeID) |
| 141 | + { |
| 142 | + case NotificationAttributeIDAppIdentifier: |
| 143 | + ESP_LOG_BUFFER_CHAR("Identifier", &attrs[3], len); |
| 144 | + break; |
| 145 | + case NotificationAttributeIDTitle: |
| 146 | + ESP_LOG_BUFFER_CHAR("Title", &attrs[3], len); |
| 147 | + break; |
| 148 | + case NotificationAttributeIDSubtitle: |
| 149 | + ESP_LOG_BUFFER_CHAR("Subtitle", &attrs[3], len); |
| 150 | + break; |
| 151 | + case NotificationAttributeIDMessage: |
| 152 | + ESP_LOG_BUFFER_CHAR("Message", &attrs[3], len); |
| 153 | + break; |
| 154 | + case NotificationAttributeIDMessageSize: |
| 155 | + ESP_LOG_BUFFER_CHAR("MessageSize", &attrs[3], len); |
| 156 | + break; |
| 157 | + case NotificationAttributeIDDate: |
| 158 | + //yyyyMMdd'T'HHmmSS |
| 159 | + ESP_LOG_BUFFER_CHAR("Date", &attrs[3], len); |
| 160 | + break; |
| 161 | + case NotificationAttributeIDPositiveActionLabel: |
| 162 | + ESP_LOG_BUFFER_HEX("PActionLabel", &attrs[3], len); |
| 163 | + break; |
| 164 | + case NotificationAttributeIDNegativeActionLabel: |
| 165 | + ESP_LOG_BUFFER_HEX("NActionLabel", &attrs[3], len); |
| 166 | + break; |
| 167 | + default: |
| 168 | + ESP_LOG_BUFFER_HEX("unknownAttributeID", &attrs[3], len); |
| 169 | + break; |
| 170 | + } |
| 171 | + |
| 172 | + attrs += (1 + 2 + len); |
| 173 | + remian_attr_len -= (1 + 2 + len); |
| 174 | + } |
| 175 | + |
| 176 | + break; |
| 177 | + } |
| 178 | + case CommandIDGetAppAttributes: |
| 179 | + ESP_LOGI(NimBLE_ANCS_TAG, "recevice APP Attributes response"); |
| 180 | + break; |
| 181 | + case CommandIDPerformNotificationAction: |
| 182 | + ESP_LOGI(NimBLE_ANCS_TAG, "recevice Perform Notification Action"); |
| 183 | + break; |
| 184 | + default: |
| 185 | + ESP_LOGI(NimBLE_ANCS_TAG, "unknown Command ID"); |
| 186 | + break; |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +char *Errcode_to_String(uint16_t status) |
| 191 | +{ |
| 192 | + char *Errstr = NULL; |
| 193 | + switch (status) { |
| 194 | + case Unknown_command: |
| 195 | + Errstr = "Unknown_command"; |
| 196 | + break; |
| 197 | + case Invalid_command: |
| 198 | + Errstr = "Invalid_command"; |
| 199 | + break; |
| 200 | + case Invalid_parameter: |
| 201 | + Errstr = "Invalid_parameter"; |
| 202 | + break; |
| 203 | + case Action_failed: |
| 204 | + Errstr = "Action_failed"; |
| 205 | + break; |
| 206 | + default: |
| 207 | + Errstr = "unknown_failed"; |
| 208 | + break; |
| 209 | + } |
| 210 | + return Errstr; |
| 211 | + |
| 212 | +} |
0 commit comments