ble.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. #include <assert.h>
  20. #include <stdbool.h>
  21. #include <stdint.h>
  22. #include "nimble/nimble_port.h"
  23. #include "host/ble_hs.h"
  24. #include "host/util/util.h"
  25. #include "services/gap/ble_svc_gap.h"
  26. static const char gap_name[] = "nimble";
  27. static uint8_t own_addr_type;
  28. static void start_advertise(void);
  29. static void
  30. put_ad(uint8_t ad_type, uint8_t ad_len, const void *ad, uint8_t *buf,
  31. uint8_t *len)
  32. {
  33. buf[(*len)++] = ad_len + 1;
  34. buf[(*len)++] = ad_type;
  35. memcpy(&buf[*len], ad, ad_len);
  36. *len += ad_len;
  37. }
  38. static void
  39. update_ad(void)
  40. {
  41. uint8_t ad[BLE_HS_ADV_MAX_SZ];
  42. uint8_t ad_len = 0;
  43. uint8_t ad_flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
  44. put_ad(BLE_HS_ADV_TYPE_FLAGS, 1, &ad_flags, ad, &ad_len);
  45. put_ad(BLE_HS_ADV_TYPE_COMP_NAME, sizeof(gap_name), gap_name, ad, &ad_len);
  46. ble_gap_adv_set_data(ad, ad_len);
  47. }
  48. static int
  49. gap_event_cb(struct ble_gap_event *event, void *arg)
  50. {
  51. switch (event->type) {
  52. case BLE_GAP_EVENT_CONNECT:
  53. if (event->connect.status) {
  54. start_advertise();
  55. }
  56. break;
  57. case BLE_GAP_EVENT_DISCONNECT:
  58. start_advertise();
  59. break;
  60. }
  61. return 0;
  62. }
  63. static void
  64. start_advertise(void)
  65. {
  66. struct ble_gap_adv_params advp;
  67. int rc;
  68. printf("advertise\n");
  69. update_ad();
  70. memset(&advp, 0, sizeof advp);
  71. advp.conn_mode = BLE_GAP_CONN_MODE_UND;
  72. advp.disc_mode = BLE_GAP_DISC_MODE_GEN;
  73. rc = ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER,
  74. &advp, gap_event_cb, NULL);
  75. assert(rc == 0);
  76. }
  77. static void
  78. app_ble_sync_cb(void)
  79. {
  80. int rc;
  81. ble_addr_t addr;
  82. /* generate new non-resolvable private address */
  83. rc = ble_hs_id_gen_rnd(1, &addr);
  84. assert(rc == 0);
  85. /* set generated address */
  86. rc = ble_hs_id_set_rnd(addr.val);
  87. assert(rc == 0);
  88. rc = ble_hs_util_ensure_addr(0);
  89. assert(rc == 0);
  90. rc = ble_hs_id_infer_auto(0, &own_addr_type);
  91. assert(rc == 0);
  92. start_advertise();
  93. }
  94. void
  95. nimble_host_task(void *param)
  96. {
  97. ble_hs_cfg.sync_cb = app_ble_sync_cb;
  98. ble_svc_gap_device_name_set(gap_name);
  99. nimble_port_run();
  100. }