forked from guyzmo/avr_nrf_ancs_library
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathancs_notification_source.cpp
308 lines (277 loc) · 8.67 KB
/
ancs_notification_source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include "../BLE/lib_aci.h"
#include <inttypes.h>
#include <services.h>
#define PACK_LITTLE_ENDIAN
//#define DEBUG2
//#define DEBUG1
#include "pack_lib.h"
#include "utilities.h"
#include "linked_list.h"
#include "ancs_base.h"
#include "ancs_notification_source.h"
#include "ancs_notification_list.h"
boolean command_send_enable = true;
unsigned long last_command_send = 0;
static linked_list_t* buffer_commands;
static bool ancs_updated = true;
bool ancs_send_buffered_command() {
if (!command_send_enable && ((millis() - last_command_send) < 1000)) {
return true;
}
uint8_t* buffer;
size_t len = fifo_pop(buffer_commands, &buffer);
if (len == 0) {
return false;
}
uint8_t cmd, aid;
uint32_t uid;
unpack(buffer, "BIB", &cmd, &uid, &aid);
debug_print(F("[ANCS CP] Command sent 0x"));
debug_print(cmd, HEX);
debug_print(F(" for notification #"));
debug_print(uid, DEC);
debug_print(F(" attribute 0x"));
debug_print(aid, HEX);
debug_println();
if (lib_aci_send_data(PIPE_ANCS_CONTROL_POINT_TX_ACK, buffer, len)) {
command_send_enable = false;
last_command_send = millis();
} else {
Serial.print(F("!! Error sending data for notification #"));
Serial.println(uid, DEC);
}
free(buffer);
return true;
}
void ancs_run() {
if (ancs_send_buffered_command()) {
ancs_updated = false;
} else if (!ancs_updated) {
ancs_updated = true;
//ancs_notification_list_apply(&ancs_notifications_use_hook);
free_ram();
}
}
void ancs_init() {
buffer_commands = fifo_create();
ancs_notification_list_init();
}
void ancs_get_notification_data(uint32_t uid) {
if (command_send_enable || ((millis() - last_command_send) > 1000)) {
Serial.print(F("[ANCS NS] ancs_get_notification_data("));
Serial.print(uid, DEC);
Serial.println(F(")"));
debug2_print(F("[ANCS NS] Buffering commands to get details for notification #"));
debug2_println(uid, DEC);
uint8_t* buffer;
#ifdef ANCS_USE_APP
buffer = (uint8_t*)malloc(6);
//
pack(buffer, "BIB", ANCS_COMMAND_GET_NOTIF_ATTRIBUTES, uid,
ANCS_NOTIFICATION_ATTRIBUTE_APP_IDENTIFIER);
fifo_push(buffer_commands, buffer, 6);
free(buffer);
#endif
//
buffer = (uint8_t*)malloc(8);
pack(buffer, "BIBH", ANCS_COMMAND_GET_NOTIF_ATTRIBUTES, uid,
ANCS_NOTIFICATION_ATTRIBUTE_TITLE,
ANCS_NOTIFICATION_ATTRIBUTE_DATA_SIZE);
fifo_push(buffer_commands, buffer, 8);
free(buffer);
//
buffer = (uint8_t*)malloc(6);
pack(buffer, "BIB", ANCS_COMMAND_GET_NOTIF_ATTRIBUTES, uid,
ANCS_NOTIFICATION_ATTRIBUTE_DATE);
fifo_push(buffer_commands, buffer, 6);
free(buffer);
buffer = (uint8_t*)malloc(6);
pack(buffer, "BIB", ANCS_COMMAND_GET_NOTIF_ATTRIBUTES, uid,
ANCS_NOTIFICATION_ATTRIBUTE_MESSAGE_SIZE);
fifo_push(buffer_commands, buffer, 6);
free(buffer);
//
#ifdef ANCS_USE_SUBTITLE
buffer = (uint8_t*)malloc(8);
pack(buffer, "BIBB", ANCS_COMMAND_GET_NOTIF_ATTRIBUTES, uid,
ANCS_NOTIFICATION_ATTRIBUTE_SUBTITLE,
ANCS_NOTIFICATION_ATTRIBUTE_DATA_SIZE);
fifo_push(buffer_commands, buffer, 8);
free(buffer);
#endif
//
buffer = (uint8_t*)malloc(8);
pack(buffer, "BIBH", ANCS_COMMAND_GET_NOTIF_ATTRIBUTES, uid,
ANCS_NOTIFICATION_ATTRIBUTE_MESSAGE,
MESSAGE_SIZE);
fifo_push(buffer_commands, buffer, 8);
free(buffer);
free_ram();
debug_println(F("[ANCS NS] ancs_get_notification_data(): end"));
debug_print(F("[ANCS NS] Command Send Enable: "));
debug_println(command_send_enable);
} else {
Serial.print(F("[ANCS NS] ancs_get_notification_data("));
Serial.print(uid, DEC);
Serial.print(F(") - Need Command Send - "));
Serial.println(millis() - last_command_send);
debug_print(command_send_enable);
}
}
void ancs_notification_source_parser(const uint8_t* buffer) {
Serial.println(F("[ANCS NS] ancs_notification_source_parser()"));
uint8_t event_id;
uint8_t event_flags;
uint8_t category_id;
uint8_t category_count;
uint32_t nid;
ancs_notification_t* notif = ancs_notification_cached();
unpack(buffer, "BBBBI", &event_id,
&event_flags,
&category_id,
&category_count,
&nid);
Serial.print(F("N #"));
Serial.print(nid, DEC);
Serial.print(F(": "));
notif->uid = nid;
notif->flags = event_flags;
notif->category = category_id;
notif->action = event_id;
if (event_flags != 0) {
Serial.print(F("FLAGS: "));
if ((event_flags & ANCS_EVT_FLAG_SILENT) == ANCS_EVT_FLAG_SILENT)
Serial.print(F("SILENT "));
if ((event_flags & ANCS_EVT_FLAG_IMPORTANT) == ANCS_EVT_FLAG_IMPORTANT)
Serial.print(F("IMPORTANT "));
debug_println(F("; "));
} else
debug_println(F("NO FLAGS; "));
Serial.print(F("Category: "));
switch (category_id) {
case ANCS_CATEGORY_OTHER:
Serial.println(F("Other"));
#ifndef DROP_NOTIF_OTHER
break;
#else
debug_println(F(": ignored"));
return;
#endif
case ANCS_CATEGORY_INCOMING_CALL:
Serial.println(F("Incoming call"));
#ifndef DROP_NOTIF_INCOMING_CALL
break;
#else
debug_println(F(": ignored"));
return;
#endif
case ANCS_CATEGORY_MISSED_CALL:
Serial.println(F("Missed call"));
#ifndef DROP_NOTIF_MISSED_CALL
break;
#else
debug_println(F(": ignored"));
return;
#endif
case ANCS_CATEGORY_VOICEMAIL:
Serial.println(F("Voicemail"));
#ifndef DROP_NOTIF_VOICEMAIL_CALL
break;
#else
debug_println(F(": ignored"));
return;
#endif
case ANCS_CATEGORY_SOCIAL:
Serial.println(F("Social"));
#ifndef DROP_NOTIF_SOCIAL
break;
#else
debug_println(F(": ignored"));
return;
#endif
case ANCS_CATEGORY_SCHEDULE:
debug_print(F("Schedule"));
#ifndef DROP_NOTIF_SCHEDULE
break;
#else
debug_println(F(": ignored"));
return;
#endif
case ANCS_CATEGORY_EMAIL:
debug_print(F("Email"));
#ifndef DROP_NOTIF_EMAIL
break;
#else
debug_println(F(": ignored"));
return;
#endif
case ANCS_CATEGORY_NEWS:
debug_print(F("News"));
#ifndef DROP_NOTIF_NEWS
break;
#else
debug_println(F(": ignored"));
return;
#endif
case ANCS_CATEGORY_HEALTH_FITNESS:
debug_print(F("Health & Fitness"));
#ifndef DROP_NOTIF_HEALTH_FITNESS
break;
#else
debug_println(F(": ignored"));
return;
#endif
case ANCS_CATEGORY_BUSINESS_FINANCE:
debug_print(F("Business & Finance"));
#ifndef DROP_NOTIF_BUSINESS_FINANCE
break;
#else
debug_println(F(": ignored"));
return;
#endif
case ANCS_CATEGORY_LOCATION:
debug_print(F("Location"));
#ifndef DROP_NOTIF_CATEGORY_LOCATION
break;
#else
debug_println(F(": ignored"));
return;
#endif
case ANCS_CATEGORY_ENTERTAINMENT:
debug_print(F("Entertainment"));
#ifndef DROP_NOTIF_ENTERTAINMENT
break;
#else
debug_println(F(": ignored"));
return;
#endif
default:
Serial.println(F("UNKOWN CATEGORY"));
return;
}
debug_print(F("["));
debug_print(category_count);
debug_print(F("] -> "));
switch (event_id) {
case ANCS_EVT_NOTIFICATION_ADDED:
debug_println(F("ADDED"));
if (ancs_notification_list_get(nid) == NULL) {
ancs_notification_list_push(notif);
debug_print(F("Adding notification to cache: "));
debug_println(nid);
ancs_get_notification_data(nid);
} else {
debug_print(F("Notification already in cache: "));
debug_println(nid);
}
break;
case ANCS_EVT_NOTIFICATION_MODIFIED:
debug_println(F("MODIFIED"));
ancs_get_notification_data(nid);
break;
case ANCS_EVT_NOTIFICATION_REMOVED:
debug_println(F("REMOVED"));
//ancs_notifications_remove_hook(notif);
break;
}
}