Loading...
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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 | /* main.c - Application main entry point */ /* * Copyright (c) 2015-2016 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr/types.h> #include <stddef.h> #include <string.h> #include <errno.h> #include <sys/printk.h> #include <sys/util.h> #include <sys/byteorder.h> #include <zephyr.h> #include <bluetooth/bluetooth.h> #include <bluetooth/hci.h> #include <bluetooth/conn.h> #include <bluetooth/uuid.h> #include <bluetooth/gatt.h> #define NUMBER_OF_SLOTS 1 #define EDS_VERSION 0x00 #define EDS_URL_READ_OFFSET 2 #define EDS_URL_WRITE_OFFSET 4 #define EDS_IDLE_TIMEOUT K_SECONDS(30) /* Idle timer */ struct k_delayed_work idle_work; static const struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), /* Eddystone Service UUID a3c87500-8ed3-4bdf-8a39-a01bebede295 */ BT_DATA_BYTES(BT_DATA_UUID128_ALL, 0x95, 0xe2, 0xed, 0xeb, 0x1b, 0xa0, 0x39, 0x8a, 0xdf, 0x4b, 0xd3, 0x8e, 0x00, 0x75, 0xc8, 0xa3), }; /* Eddystone Service Variables */ /* Service UUID a3c87500-8ed3-4bdf-8a39-a01bebede295 */ static struct bt_uuid_128 eds_uuid = BT_UUID_INIT_128( 0x95, 0xe2, 0xed, 0xeb, 0x1b, 0xa0, 0x39, 0x8a, 0xdf, 0x4b, 0xd3, 0x8e, 0x00, 0x75, 0xc8, 0xa3); /* Characteristic UUID a3c87501-8ed3-4bdf-8a39-a01bebede295 */ static struct bt_uuid_128 eds_caps_uuid = BT_UUID_INIT_128( 0x95, 0xe2, 0xed, 0xeb, 0x1b, 0xa0, 0x39, 0x8a, 0xdf, 0x4b, 0xd3, 0x8e, 0x01, 0x75, 0xc8, 0xa3); /* Characteristic UUID a3c87502-8ed3-4bdf-8a39-a01bebede295 */ static struct bt_uuid_128 eds_slot_uuid = BT_UUID_INIT_128( 0x95, 0xe2, 0xed, 0xeb, 0x1b, 0xa0, 0x39, 0x8a, 0xdf, 0x4b, 0xd3, 0x8e, 0x02, 0x75, 0xc8, 0xa3); /* Characteristic UUID a3c87503-8ed3-4bdf-8a39-a01bebede295 */ static struct bt_uuid_128 eds_intv_uuid = BT_UUID_INIT_128( 0x95, 0xe2, 0xed, 0xeb, 0x1b, 0xa0, 0x39, 0x8a, 0xdf, 0x4b, 0xd3, 0x8e, 0x03, 0x75, 0xc8, 0xa3); /* Characteristic UUID a3c87504-8ed3-4bdf-8a39-a01bebede295 */ static struct bt_uuid_128 eds_tx_uuid = BT_UUID_INIT_128( 0x95, 0xe2, 0xed, 0xeb, 0x1b, 0xa0, 0x39, 0x8a, 0xdf, 0x4b, 0xd3, 0x8e, 0x04, 0x75, 0xc8, 0xa3); /* Characteristic UUID a3c87505-8ed3-4bdf-8a39-a01bebede295 */ static struct bt_uuid_128 eds_adv_tx_uuid = BT_UUID_INIT_128( 0x95, 0xe2, 0xed, 0xeb, 0x1b, 0xa0, 0x39, 0x8a, 0xdf, 0x4b, 0xd3, 0x8e, 0x05, 0x75, 0xc8, 0xa3); /* Characteristic UUID a3c87506-8ed3-4bdf-8a39-a01bebede295 */ static struct bt_uuid_128 eds_lock_uuid = BT_UUID_INIT_128( 0x95, 0xe2, 0xed, 0xeb, 0x1b, 0xa0, 0x39, 0x8a, 0xdf, 0x4b, 0xd3, 0x8e, 0x06, 0x75, 0xc8, 0xa3); /* Characteristic UUID a3c87507-8ed3-4bdf-8a39-a01bebede295 */ static struct bt_uuid_128 eds_unlock_uuid = BT_UUID_INIT_128( 0x95, 0xe2, 0xed, 0xeb, 0x1b, 0xa0, 0x39, 0x8a, 0xdf, 0x4b, 0xd3, 0x8e, 0x07, 0x75, 0xc8, 0xa3); /* Characteristic UUID a3c87508-8ed3-4bdf-8a39-a01bebede295 */ static struct bt_uuid_128 eds_ecdh_uuid = BT_UUID_INIT_128( 0x95, 0xe2, 0xed, 0xeb, 0x1b, 0xa0, 0x39, 0x8a, 0xdf, 0x4b, 0xd3, 0x8e, 0x08, 0x75, 0xc8, 0xa3); /* Characteristic UUID a3c87509-8ed3-4bdf-8a39-a01bebede295 */ static struct bt_uuid_128 eds_eid_uuid = BT_UUID_INIT_128( 0x95, 0xe2, 0xed, 0xeb, 0x1b, 0xa0, 0x39, 0x8a, 0xdf, 0x4b, 0xd3, 0x8e, 0x09, 0x75, 0xc8, 0xa3); /* Characteristic UUID a3c8750a-8ed3-4bdf-8a39-a01bebede295 */ static struct bt_uuid_128 eds_data_uuid = BT_UUID_INIT_128( 0x95, 0xe2, 0xed, 0xeb, 0x1b, 0xa0, 0x39, 0x8a, 0xdf, 0x4b, 0xd3, 0x8e, 0x0a, 0x75, 0xc8, 0xa3); /* Characteristic UUID a3c8750b-8ed3-4bdf-8a39-a01bebede295 */ static struct bt_uuid_128 eds_reset_uuid = BT_UUID_INIT_128( 0x95, 0xe2, 0xed, 0xeb, 0x1b, 0xa0, 0x39, 0x8a, 0xdf, 0x4b, 0xd3, 0x8e, 0x0b, 0x75, 0xc8, 0xa3); /* Characteristic UUID a3c8750c-8ed3-4bdf-8a39-a01bebede295 */ static struct bt_uuid_128 eds_connectable_uuid = BT_UUID_INIT_128( 0x95, 0xe2, 0xed, 0xeb, 0x1b, 0xa0, 0x39, 0x8a, 0xdf, 0x4b, 0xd3, 0x8e, 0x0c, 0x75, 0xc8, 0xa3); enum { EDS_TYPE_UID = 0x00, EDS_TYPE_URL = 0x10, EDS_TYPE_TLM = 0x20, EDS_TYPE_EID = 0x30, EDS_TYPE_NONE = 0xff, }; enum { EDS_SLOT_UID = sys_cpu_to_be16(BIT(0)), EDS_SLOT_URL = sys_cpu_to_be16(BIT(1)), EDS_SLOT_TLM = sys_cpu_to_be16(BIT(2)), EDS_SLOT_EID = sys_cpu_to_be16(BIT(3)), }; struct eds_capabilities { u8_t version; u8_t slots; u8_t uids; u8_t adv_types; u16_t slot_types; u8_t tx_power; } __packed; static struct eds_capabilities eds_caps = { .version = EDS_VERSION, .slots = NUMBER_OF_SLOTS, .slot_types = EDS_SLOT_URL, /* TODO: Add support for other slot types */ }; u8_t eds_active_slot; enum { EDS_LOCKED = 0x00, EDS_UNLOCKED = 0x01, EDS_UNLOCKED_NO_RELOCKING = 0x02, }; struct eds_slot { u8_t type; u8_t state; u8_t connectable; u16_t interval; u8_t tx_power; u8_t adv_tx_power; u8_t lock[16]; u8_t challenge[16]; struct bt_data ad[3]; }; static struct eds_slot eds_slots[NUMBER_OF_SLOTS] = { [0 ... (NUMBER_OF_SLOTS - 1)] = { .type = EDS_TYPE_NONE, /* Start as disabled */ .state = EDS_UNLOCKED, /* Start unlocked */ .interval = sys_cpu_to_be16(BT_GAP_ADV_FAST_INT_MIN_2), .lock = { 'Z', 'e', 'p', 'h', 'y', 'r', ' ', 'E', 'd', 'd', 'y', 's', 't', 'o', 'n', 'e' }, .challenge = {}, .ad = { BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR), BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0xaa, 0xfe), BT_DATA_BYTES(BT_DATA_SVC_DATA16, 0xaa, 0xfe, /* Eddystone UUID */ 0x10, /* Eddystone-URL frame type */ 0x00, /* Calibrated Tx power at 0m */ 0x00, /* URL Scheme Prefix http://www. */ 'z', 'e', 'p', 'h', 'y', 'r', 'p', 'r', 'o', 'j', 'e', 'c', 't', 0x08) /* .org */ }, }, }; static ssize_t read_caps(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) { const struct eds_capabilities *caps = attr->user_data; return bt_gatt_attr_read(conn, attr, buf, len, offset, caps, sizeof(*caps)); } static ssize_t read_slot(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) { return bt_gatt_attr_read(conn, attr, buf, len, offset, &eds_active_slot, sizeof(eds_active_slot)); } static ssize_t write_slot(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, u16_t len, u16_t offset, u8_t flags) { u8_t value; if (offset + len > sizeof(value)) { return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); } memcpy(&value, buf, len); if (value + 1 > NUMBER_OF_SLOTS) { return BT_GATT_ERR(BT_ATT_ERR_WRITE_NOT_PERMITTED); } eds_active_slot = value; return len; } static ssize_t read_tx_power(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) { struct eds_slot *slot = &eds_slots[eds_active_slot]; if (slot->state == EDS_LOCKED) { return BT_GATT_ERR(BT_ATT_ERR_READ_NOT_PERMITTED); } return bt_gatt_attr_read(conn, attr, buf, len, offset, &slot->tx_power, sizeof(slot->tx_power)); } static ssize_t write_tx_power(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, u16_t len, u16_t offset, u8_t flags) { struct eds_slot *slot = &eds_slots[eds_active_slot]; if (slot->state == EDS_LOCKED) { return BT_GATT_ERR(BT_ATT_ERR_WRITE_NOT_PERMITTED); } if (offset + len > sizeof(slot->tx_power)) { return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); } memcpy(&slot->tx_power, buf, len); return len; } static ssize_t read_adv_tx_power(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) { struct eds_slot *slot = &eds_slots[eds_active_slot]; if (slot->state == EDS_LOCKED) { return BT_GATT_ERR(BT_ATT_ERR_READ_NOT_PERMITTED); } return bt_gatt_attr_read(conn, attr, buf, len, offset, &slot->tx_power, sizeof(slot->tx_power)); } static ssize_t write_adv_tx_power(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, u16_t len, u16_t offset, u8_t flags) { struct eds_slot *slot = &eds_slots[eds_active_slot]; if (slot->state == EDS_LOCKED) { return BT_GATT_ERR(BT_ATT_ERR_WRITE_NOT_PERMITTED); } if (offset + len > sizeof(slot->adv_tx_power)) { return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); } memcpy(&slot->adv_tx_power, buf, len); return len; } static ssize_t read_interval(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) { struct eds_slot *slot = &eds_slots[eds_active_slot]; if (slot->state == EDS_LOCKED) { return BT_GATT_ERR(BT_ATT_ERR_WRITE_NOT_PERMITTED); } return bt_gatt_attr_read(conn, attr, buf, len, offset, &slot->interval, sizeof(slot->interval)); } static ssize_t read_lock(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) { struct eds_slot *slot = &eds_slots[eds_active_slot]; return bt_gatt_attr_read(conn, attr, buf, len, offset, &slot->state, sizeof(slot->state)); } static ssize_t write_lock(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, u16_t len, u16_t offset, u8_t flags) { struct eds_slot *slot = &eds_slots[eds_active_slot]; u8_t value; if (slot->state == EDS_LOCKED) { return BT_GATT_ERR(BT_ATT_ERR_WRITE_NOT_PERMITTED); } if (offset) { return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); } /* Write 1 byte to lock or 17 bytes to transition to a new lock state */ if (len != 1U) { /* TODO: Allow setting new lock code, using AES-128-ECB to * decrypt with the existing lock code and set the unencrypted * value as the new code. */ return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN); } memcpy(&value, buf, sizeof(value)); if (value > EDS_UNLOCKED_NO_RELOCKING) { return BT_GATT_ERR(BT_ATT_ERR_WRITE_NOT_PERMITTED); } slot->state = value; return len; } static ssize_t read_unlock(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) { struct eds_slot *slot = &eds_slots[eds_active_slot]; if (slot->state != EDS_LOCKED) { return BT_GATT_ERR(BT_ATT_ERR_READ_NOT_PERMITTED); } /* returns a 128-bit challenge token. This token is for one-time use * and cannot be replayed. */ if (bt_rand(slot->challenge, sizeof(slot->challenge))) { return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY); } return bt_gatt_attr_read(conn, attr, buf, len, offset, slot->challenge, sizeof(slot->challenge)); } static ssize_t write_unlock(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, u16_t len, u16_t offset, u8_t flags) { struct eds_slot *slot = &eds_slots[eds_active_slot]; if (slot->state != EDS_LOCKED) { return BT_GATT_ERR(BT_ATT_ERR_READ_NOT_PERMITTED); } /* TODO: accepts a 128-bit encrypted value that verifies the client * knows the beacon's lock code. */ return BT_GATT_ERR(BT_ATT_ERR_NOT_SUPPORTED); } static u8_t eds_ecdh[32] = {}; /* TODO: Add ECDH key */ static ssize_t read_ecdh(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) { u8_t *value = attr->user_data; return bt_gatt_attr_read(conn, attr, buf, len, offset, value, sizeof(eds_ecdh)); } static u8_t eds_eid[16] = {}; /* TODO: Add EID key */ static ssize_t read_eid(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) { u8_t *value = attr->user_data; return bt_gatt_attr_read(conn, attr, buf, len, offset, value, sizeof(eds_eid)); } static ssize_t read_adv_data(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) { struct eds_slot *slot = &eds_slots[eds_active_slot]; if (slot->state == EDS_LOCKED) { return BT_GATT_ERR(BT_ATT_ERR_READ_NOT_PERMITTED); } /* If the slot is currently not broadcasting, reading the slot data * shall return either an empty array or a single byte of 0x00. */ if (slot->type == EDS_TYPE_NONE) { return 0; } return bt_gatt_attr_read(conn, attr, buf, len, offset, slot->ad[2].data + EDS_URL_READ_OFFSET, slot->ad[2].data_len - EDS_URL_READ_OFFSET); } static int eds_slot_restart(struct eds_slot *slot, u8_t type) { int err; /* Restart advertising */ bt_le_adv_stop(); if (type == EDS_TYPE_NONE) { /* Restore connectable if slot */ err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0); } else { err = bt_le_adv_start(BT_LE_ADV_NCONN_NAME, slot->ad, ARRAY_SIZE(slot->ad), NULL, 0); } if (err) { printk("Advertising failed to start (err %d)\n", err); return err; } slot->type = type; return 0; } static ssize_t write_adv_data(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, u16_t len, u16_t offset, u8_t flags) { struct eds_slot *slot = &eds_slots[eds_active_slot]; u8_t type; if (slot->state == EDS_LOCKED) { return BT_GATT_ERR(BT_ATT_ERR_READ_NOT_PERMITTED); } if (offset) { return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); } /* Writing an empty array, clears the slot and stops Tx. */ if (!len) { eds_slot_restart(slot, EDS_TYPE_NONE); return len; } /* Write length: 17 bytes (UID), 19 bytes (URL), 1 byte (TLM), 34 or * 18 bytes (EID) */ if (len > 19) { return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN); } memcpy(&type, buf, sizeof(type)); switch (type) { case EDS_TYPE_URL: /* written data is just the frame type and any ID-related * information, and doesn't include the Tx power since that is * controlled by characteristics 4 (Radio Tx Power) and * 5 (Advertised Tx Power). */ slot->ad[2].data_len = MIN(slot->ad[2].data_len, len + EDS_URL_WRITE_OFFSET); memcpy(&slot->ad[2].data + EDS_URL_WRITE_OFFSET, buf, slot->ad[2].data_len - EDS_URL_WRITE_OFFSET); /* Restart slot */ if (eds_slot_restart(slot, type) < 0) { return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY); } return len; case EDS_TYPE_UID: case EDS_TYPE_TLM: case EDS_TYPE_EID: default: /* TODO: Add support for other types. */ return BT_GATT_ERR(BT_ATT_ERR_WRITE_NOT_PERMITTED); } } static ssize_t write_reset(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, u16_t len, u16_t offset, u8_t flags) { /* TODO: Power cycle or reload for storage the values */ return BT_GATT_ERR(BT_ATT_ERR_WRITE_NOT_PERMITTED); } static ssize_t read_connectable(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) { u8_t connectable = 0x01; /* Returning a non-zero value indicates that the beacon is capable * of becoming non-connectable */ return bt_gatt_attr_read(conn, attr, buf, len, offset, &connectable, sizeof(connectable)); } static ssize_t write_connectable(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, u16_t len, u16_t offset, u8_t flags) { struct eds_slot *slot = &eds_slots[eds_active_slot]; if (slot->state == EDS_LOCKED) { return BT_GATT_ERR(BT_ATT_ERR_WRITE_NOT_PERMITTED); } if (offset) { return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); } if (len > sizeof(slot->connectable)) { return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN); } /* If any non-zero value is written, the beacon shall remain in its * connectable state until any other value is written. */ memcpy(&slot->connectable, buf, len); return len; } /* Eddystone Configuration Service Declaration */ BT_GATT_SERVICE_DEFINE(eds_svc, BT_GATT_PRIMARY_SERVICE(&eds_uuid), /* Capabilities: Readable only when unlocked. Never writable. */ BT_GATT_CHARACTERISTIC(&eds_caps_uuid.uuid, BT_GATT_CHRC_READ, BT_GATT_PERM_READ, read_caps, NULL, &eds_caps), /* Active slot: Must be unlocked for both read and write. */ BT_GATT_CHARACTERISTIC(&eds_slot_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, read_slot, write_slot, NULL), /* Advertising Interval: Must be unlocked for both read and write. */ BT_GATT_CHARACTERISTIC(&eds_intv_uuid.uuid, BT_GATT_CHRC_READ, BT_GATT_PERM_READ, read_interval, NULL, NULL), /* Radio TX Power: Must be unlocked for both read and write. */ BT_GATT_CHARACTERISTIC(&eds_tx_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, read_tx_power, write_tx_power, NULL), /* Advertised TX Power: Must be unlocked for both read and write. */ BT_GATT_CHARACTERISTIC(&eds_adv_tx_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, read_adv_tx_power, write_adv_tx_power, NULL), /* Lock State: * Readable in locked or unlocked state. * Writeable only in unlocked state. */ BT_GATT_CHARACTERISTIC(&eds_lock_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, read_lock, write_lock, NULL), /* Unlock: * Readable only in locked state. * Writeable only in locked state. */ BT_GATT_CHARACTERISTIC(&eds_unlock_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, read_unlock, write_unlock, NULL), /* Public ECDH Key: Readable only in unlocked state. Never writable. */ BT_GATT_CHARACTERISTIC(&eds_ecdh_uuid.uuid, BT_GATT_CHRC_READ, BT_GATT_PERM_READ, read_ecdh, NULL, &eds_ecdh), /* EID Identity Key:Readable only in unlocked state. Never writable. */ BT_GATT_CHARACTERISTIC(&eds_eid_uuid.uuid, BT_GATT_CHRC_READ, BT_GATT_PERM_READ, read_eid, NULL, eds_eid), /* ADV Slot Data: Must be unlocked for both read and write. */ BT_GATT_CHARACTERISTIC(&eds_data_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, read_adv_data, write_adv_data, NULL), /* ADV Factory Reset: Must be unlocked for write. */ BT_GATT_CHARACTERISTIC(&eds_reset_uuid.uuid, BT_GATT_CHRC_WRITE, BT_GATT_PERM_WRITE, NULL, write_reset, NULL), /* ADV Remain Connectable: Must be unlocked for write. */ BT_GATT_CHARACTERISTIC(&eds_connectable_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, read_connectable, write_connectable, NULL), ); static void bt_ready(int err) { if (err) { printk("Bluetooth init failed (err %d)\n", err); return; } printk("Bluetooth initialized\n"); /* Start advertising */ err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0); if (err) { printk("Advertising failed to start (err %d)\n", err); return; } k_delayed_work_submit(&idle_work, EDS_IDLE_TIMEOUT); printk("Configuration mode: waiting connections...\n"); } static void idle_timeout(struct k_work *work) { if (eds_slots[eds_active_slot].type == EDS_TYPE_NONE) { printk("Switching to Beacon mode.\n"); eds_slot_restart(&eds_slots[eds_active_slot], EDS_TYPE_URL); } } static void connected(struct bt_conn *conn, u8_t err) { if (err) { printk("Connection failed (err 0x%02x)\n", err); } else { printk("Connected\n"); k_delayed_work_cancel(&idle_work); } } static void disconnected(struct bt_conn *conn, u8_t reason) { struct eds_slot *slot = &eds_slots[eds_active_slot]; printk("Disconnected (reason 0x%02x)\n", reason); if (!slot->connectable) { k_delayed_work_submit(&idle_work, 0); } } static struct bt_conn_cb conn_callbacks = { .connected = connected, .disconnected = disconnected, }; void main(void) { int err; bt_conn_cb_register(&conn_callbacks); k_delayed_work_init(&idle_work, idle_timeout); /* Initialize the Bluetooth Subsystem */ err = bt_enable(bt_ready); if (err) { printk("Bluetooth init failed (err %d)\n", err); } } |