InfiniTime.git

ref: 8e2dcda14fac3e9ebc3b592439ad1f2afbbc7076

src/libs/mynewt-nimble/nimble/host/services/ans/src/ble_svc_ans.c


  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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <assert.h>
#include <string.h>

#include "sysinit/sysinit.h"
#include "syscfg/syscfg.h"
#include "host/ble_hs.h"
#include "host/ble_gap.h"
#include "services/ans/ble_svc_ans.h"

/* Max length of new alert info string */
#define BLE_SVC_ANS_INFO_STR_MAX_LEN        18
/* Max length of a new alert notification, max string length + 2 bytes
 * for category ID and count. */
#define BLE_SVC_ANS_NEW_ALERT_MAX_LEN   (BLE_SVC_ANS_INFO_STR_MAX_LEN + 2)

/* Supported categories bitmasks */
static uint8_t ble_svc_ans_new_alert_cat;
static uint8_t ble_svc_ans_unr_alert_cat;

/* Characteristic values */
static uint8_t ble_svc_ans_new_alert_val[BLE_SVC_ANS_NEW_ALERT_MAX_LEN];
static uint16_t ble_svc_ans_new_alert_val_len;
static uint8_t ble_svc_ans_unr_alert_stat[2];
static uint8_t ble_svc_ans_alert_not_ctrl_pt[2];

/* Alert counts, one value for each category */
static uint8_t ble_svc_ans_new_alert_cnt[BLE_SVC_ANS_CAT_NUM];
static uint8_t ble_svc_ans_unr_alert_cnt[BLE_SVC_ANS_CAT_NUM];

/* Characteristic value handles */
static uint16_t ble_svc_ans_new_alert_val_handle;
static uint16_t ble_svc_ans_unr_alert_val_handle;

/* Connection handle
 *
 * TODO: In order to support multiple connections we would need to save
 *       the handles for every connection, not just the most recent. Then
 *       we would need to notify each connection when needed.
 * */
static uint16_t ble_svc_ans_conn_handle;

/* Access function */
static int
ble_svc_ans_access(uint16_t conn_handle, uint16_t attr_handle,
                   struct ble_gatt_access_ctxt *ctxt, void *arg);

/* Notify new alert */
static int
ble_svc_ans_new_alert_notify(uint8_t cat_id, const char * info_str);

/* Notify unread alert */
static int
ble_svc_ans_unr_alert_notify(uint8_t cat_id);

/* Save written value to local characteristic value */
static int
ble_svc_ans_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len,
                      void *dst, uint16_t *len);

static const struct ble_gatt_svc_def ble_svc_ans_defs[] = {
    {
        /*** Alert Notification Service. */
        .type = BLE_GATT_SVC_TYPE_PRIMARY,
        .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_UUID16),
        .characteristics = (struct ble_gatt_chr_def[]) { {
            /** Supported New Alert Catagory
             *
             * This characteristic exposes what categories of new
             * alert are supported in the server.
             */
            .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_SUP_NEW_ALERT_CAT),
            .access_cb = ble_svc_ans_access,
            .flags = BLE_GATT_CHR_F_READ,
        }, {
            /** New Alert
             *
             * This characteristic exposes information about
             * the count of new alerts (for a given category).
             */
            .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_NEW_ALERT),
            .access_cb = ble_svc_ans_access,
            .val_handle = &ble_svc_ans_new_alert_val_handle,
            .flags = BLE_GATT_CHR_F_NOTIFY,
        }, {
            /** Supported Unread Alert Catagory
             *
             * This characteristic exposes what categories of
             * unread alert are supported in the server.
             */
            .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_SUP_UNR_ALERT_CAT),
            .access_cb = ble_svc_ans_access,
            .flags = BLE_GATT_CHR_F_READ,
        }, {
            /** Unread Alert Status
             *
             * This characteristic exposes the count of unread
             * alert events existing in the server.
             */
            .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_UNR_ALERT_STAT),
            .access_cb = ble_svc_ans_access,
            .val_handle = &ble_svc_ans_unr_alert_val_handle,
            .flags = BLE_GATT_CHR_F_NOTIFY,
        }, {
            /** Alert Notification Control Point
             *
             * This characteristic allows the peer device to
             * enable/disable the alert notification of new alert
             * and unread event more selectively than can be done
             * by setting or clearing the notification bit in the
             * Client Characteristic Configuration for each alert
             * characteristic.
             */
            .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_ALERT_NOT_CTRL_PT),
            .access_cb = ble_svc_ans_access,
            .flags = BLE_GATT_CHR_F_WRITE,
        }, {
            0, /* No more characteristics in this service. */
        } },
    },

    {
        0, /* No more services. */
    },
};

/**
 * ANS access function
 */
static int
ble_svc_ans_access(uint16_t conn_handle, uint16_t attr_handle,
                          struct ble_gatt_access_ctxt *ctxt,
                          void *arg)
{
    uint16_t uuid16;
    int rc;

    /* ANS Control point command and catagory variables */
    uint8_t cmd_id;
    uint8_t cat_id;
    uint8_t cat_bit_mask;
    int i;

    uuid16 = ble_uuid_u16(ctxt->chr->uuid);
    assert(uuid16 != 0);

    switch (uuid16) {
    case BLE_SVC_ANS_CHR_UUID16_SUP_NEW_ALERT_CAT:
        assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
        rc = os_mbuf_append(ctxt->om, &ble_svc_ans_new_alert_cat,
                            sizeof ble_svc_ans_new_alert_cat);
        return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;

    case BLE_SVC_ANS_CHR_UUID16_NEW_ALERT:
        if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
            rc = ble_svc_ans_chr_write(ctxt->om, 0,
                                       sizeof ble_svc_ans_new_alert_val,
                                       ble_svc_ans_new_alert_val,
                                       &ble_svc_ans_new_alert_val_len);
            return rc;

        } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
            rc = os_mbuf_append(ctxt->om, &ble_svc_ans_new_alert_val,
                                sizeof ble_svc_ans_new_alert_val);
            return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
        }

        assert(0);
        return BLE_ATT_ERR_UNLIKELY;
    case BLE_SVC_ANS_CHR_UUID16_SUP_UNR_ALERT_CAT:
        assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
        rc = os_mbuf_append(ctxt->om, &ble_svc_ans_unr_alert_cat,
                            sizeof ble_svc_ans_unr_alert_cat);
        return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;

    case BLE_SVC_ANS_CHR_UUID16_UNR_ALERT_STAT:
        if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
            rc = ble_svc_ans_chr_write(ctxt->om,
                                       sizeof ble_svc_ans_unr_alert_stat,
                                       sizeof ble_svc_ans_unr_alert_stat,
                                       &ble_svc_ans_unr_alert_stat,
                                       NULL);
            return rc;
        } else {
            rc = os_mbuf_append(ctxt->om, &ble_svc_ans_unr_alert_stat,
                                sizeof ble_svc_ans_unr_alert_stat);
            return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
        }

    case BLE_SVC_ANS_CHR_UUID16_ALERT_NOT_CTRL_PT:
        if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
            rc = ble_svc_ans_chr_write(ctxt->om,
                                       sizeof ble_svc_ans_alert_not_ctrl_pt,
                                       sizeof ble_svc_ans_alert_not_ctrl_pt,
                                       &ble_svc_ans_alert_not_ctrl_pt,
                                       NULL);
            if (rc != 0) {
                return rc;
            }

            /* Get command ID and category ID */
            cmd_id = ble_svc_ans_alert_not_ctrl_pt[0];
            cat_id = ble_svc_ans_alert_not_ctrl_pt[1];


            /* Set cat_bit_mask to the appropriate bitmask based on cat_id */
            if (cat_id < BLE_SVC_ANS_CAT_NUM) {
                cat_bit_mask = (1 << cat_id);
            } else if (cat_id == 0xff) {
                cat_bit_mask = cat_id;
            } else {
                /* invalid category ID */
                return BLE_ATT_ERR_UNLIKELY;
            }

            switch (cmd_id) {
            case BLE_SVC_ANS_CMD_EN_NEW_ALERT_CAT:
                ble_svc_ans_new_alert_cat |= cat_bit_mask;
                break;
            case BLE_SVC_ANS_CMD_EN_UNR_ALERT_CAT:
                ble_svc_ans_unr_alert_cat |= cat_bit_mask;
                break;
            case BLE_SVC_ANS_CMD_DIS_NEW_ALERT_CAT:
                ble_svc_ans_new_alert_cat &= ~cat_bit_mask;
                break;
            case BLE_SVC_ANS_CMD_DIS_UNR_ALERT_CAT:
                ble_svc_ans_unr_alert_cat &= ~cat_bit_mask;
                break;
            case BLE_SVC_ANS_CMD_NOT_NEW_ALERT_IMMEDIATE:
                if (cat_id == 0xff) {
                    /* If cat_id is 0xff, notify on all enabled categories */
                    for (i = BLE_SVC_ANS_CAT_NUM - 1; i > 0; --i) {
                        if ((ble_svc_ans_new_alert_cat >> i) & 0x01) {
                            ble_svc_ans_new_alert_notify(i, NULL);
                        }
                    }
                } else {
                    ble_svc_ans_new_alert_notify(cat_id, NULL);
                }
                break;
            case BLE_SVC_ANS_CMD_NOT_UNR_ALERT_IMMEDIATE:
                if (cat_id == 0xff) {
                    /* If cat_id is 0xff, notify on all enabled categories */
                    for (i = BLE_SVC_ANS_CAT_NUM - 1; i > 0; --i) {
                        if ((ble_svc_ans_unr_alert_cat >> i) & 0x01) {
                            ble_svc_ans_unr_alert_notify(i);
                        }
                    }
                } else {
                    ble_svc_ans_unr_alert_notify(cat_id);
                }
                break;
            default:
                return BLE_SVC_ANS_ERR_CMD_NOT_SUPPORTED;
            }
            return 0;
        } else {
            rc = BLE_ATT_ERR_UNLIKELY;
        }
        return rc;

    default:
        assert(0);
        return BLE_ATT_ERR_UNLIKELY;
    }
}

/**
 * This function must be called with the connection handlewhen a gap
 * connect event is received in order to send notifications to the
 * client.
 *
 * @params conn_handle          The connection handle for the current
 *                                  connection.
 */
void
ble_svc_ans_on_gap_connect(uint16_t conn_handle)
{
    ble_svc_ans_conn_handle = conn_handle;
}

/**
 * Adds a new alert to the given category then notifies the client
 * if the given category is valid and enabled.
 *
 * @param cat_flag              The id for the category which should
 *                                  should be incremented and notified
 * @param info_str              The info string to be sent to the client
 *                                  with the notification.
 *
 * @return 0 on success, non-zero error code otherwise.
 */
int
ble_svc_ans_new_alert_add(uint8_t cat_id, const char * info_str)
{
    uint8_t cat_bit_mask;

    if (cat_id < BLE_SVC_ANS_CAT_NUM) {
        cat_bit_mask = (1 << cat_id);
    } else {
        return BLE_HS_EINVAL;
    }

    if ((cat_bit_mask & ble_svc_ans_new_alert_cat) == 0) {
        return BLE_HS_EINVAL;
    }

    ble_svc_ans_new_alert_cnt[cat_id] += 1;
    return ble_svc_ans_new_alert_notify(cat_id, info_str);
}

/**
 * Adds an unread alert to the given category then notifies the client
 * if the given category is valid and enabled.
 *
 * @param cat_flag              The flag for the category which should
 *                                  should be incremented and notified
 *
 * @return 0 on success, non-zero error code otherwise.
 */
int
ble_svc_ans_unr_alert_add(uint8_t cat_id)
{
    uint8_t cat_bit_mask;

    if (cat_id < BLE_SVC_ANS_CAT_NUM) {
        cat_bit_mask = 1 << cat_id;
    } else {
        return BLE_HS_EINVAL;
    }

    if ((cat_bit_mask & ble_svc_ans_unr_alert_cat) == 0) {
        return BLE_HS_EINVAL;
    }

    ble_svc_ans_unr_alert_cnt[cat_id] += 1;
    return ble_svc_ans_unr_alert_notify(cat_id);
}

/**
 * Send a new alert notification to the given category with the
 * given info string.
 *
 * @param cat_id                The ID of the category to send the
 *                                  notification to.
 * @param info_str              The info string to send with the
 *                                  notification
 *
 * @return 0 on success, non-zero error code otherwise.
 */
static int
ble_svc_ans_new_alert_notify(uint8_t cat_id, const char * info_str)
{
    int info_str_len;

    /* Clear notification to remove old infomation that may persist */
    memset(&ble_svc_ans_new_alert_val, '\0',
           BLE_SVC_ANS_NEW_ALERT_MAX_LEN);

    /* Set ID and count values */
    ble_svc_ans_new_alert_val[0] = cat_id;
    ble_svc_ans_new_alert_val[1] = ble_svc_ans_new_alert_cnt[cat_id];

    if (info_str) {
        info_str_len = strlen(info_str);
        if (info_str_len > BLE_SVC_ANS_INFO_STR_MAX_LEN) {
            /* If info_str is longer than the max string length only
             * write up to the maximum length */
            memcpy(&ble_svc_ans_new_alert_val[2], info_str,
                   BLE_SVC_ANS_INFO_STR_MAX_LEN);
        } else {
            memcpy(&ble_svc_ans_new_alert_val[2], info_str, info_str_len);
        }
    }
    return ble_gattc_notify(ble_svc_ans_conn_handle,
                            ble_svc_ans_new_alert_val_handle);
}

/**
 * Send an unread alert notification to the given category.
 *
 * @param cat_id                The ID of the category to send the
 *                                  notificaiton to.
 *
 * @return 0 on success, non-zer0 error code otherwise.
 */
static int
ble_svc_ans_unr_alert_notify(uint8_t cat_id)
{
    ble_svc_ans_unr_alert_stat[0] = cat_id;
    ble_svc_ans_unr_alert_stat[1] = ble_svc_ans_unr_alert_cnt[cat_id];
    return ble_gattc_notify(ble_svc_ans_conn_handle,
                            ble_svc_ans_unr_alert_val_handle);
}

/**
 * Writes the received value from a characteristic write to
 * the given destination.
 */
static int
ble_svc_ans_chr_write(struct os_mbuf *om, uint16_t min_len,
                      uint16_t max_len, void *dst,
                      uint16_t *len)
{
    uint16_t om_len;
    int rc;

    om_len = OS_MBUF_PKTLEN(om);
    if (om_len < min_len || om_len > max_len) {
        return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
    }

    rc = ble_hs_mbuf_to_flat(om, dst, max_len, len);
    if (rc != 0) {
        return BLE_ATT_ERR_UNLIKELY;
    }

    return 0;
}

/**
 * Initialize the ANS with initial values for enabled categories
 * for new and unread alert characteristics. Bitwise or the
 * catagory bitmasks to enable multiple catagories.
 *
 * XXX: We should technically be able to change the new alert and
 *      unread alert catagories when we have no active connections.
 */
void
ble_svc_ans_init(void)
{
    int rc;

    /* Ensure this function only gets called by sysinit. */
    SYSINIT_ASSERT_ACTIVE();

    rc = ble_gatts_count_cfg(ble_svc_ans_defs);
    SYSINIT_PANIC_ASSERT(rc == 0);

    rc = ble_gatts_add_svcs(ble_svc_ans_defs);
    SYSINIT_PANIC_ASSERT(rc == 0);

    ble_svc_ans_new_alert_cat = MYNEWT_VAL(BLE_SVC_ANS_NEW_ALERT_CAT);
    ble_svc_ans_unr_alert_cat = MYNEWT_VAL(BLE_SVC_ANS_UNR_ALERT_CAT);
}