gatt_svr.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 <stdio.h>
  21. #include <string.h>
  22. #include "host/ble_hs.h"
  23. #include "host/ble_uuid.h"
  24. #include "services/gap/ble_svc_gap.h"
  25. #include "services/gatt/ble_svc_gatt.h"
  26. #include "bleprph.h"
  27. /**
  28. * The vendor specific security test service consists of two characteristics:
  29. * o random-number-generator: generates a random 32-bit number each time
  30. * it is read. This characteristic can only be read over an encrypted
  31. * connection.
  32. * o static-value: a single-byte characteristic that can always be read,
  33. * but can only be written over an encrypted connection.
  34. */
  35. /* 59462f12-9543-9999-12c8-58b459a2712d */
  36. static const ble_uuid128_t gatt_svr_svc_sec_test_uuid =
  37. BLE_UUID128_INIT(0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12,
  38. 0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59);
  39. /* 5c3a659e-897e-45e1-b016-007107c96df6 */
  40. static const ble_uuid128_t gatt_svr_chr_sec_test_rand_uuid =
  41. BLE_UUID128_INIT(0xf6, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0,
  42. 0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c);
  43. /* 5c3a659e-897e-45e1-b016-007107c96df7 */
  44. static const ble_uuid128_t gatt_svr_chr_sec_test_static_uuid =
  45. BLE_UUID128_INIT(0xf7, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0,
  46. 0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c);
  47. static uint8_t gatt_svr_sec_test_static_val;
  48. static int
  49. gatt_svr_chr_access_sec_test(uint16_t conn_handle, uint16_t attr_handle,
  50. struct ble_gatt_access_ctxt *ctxt,
  51. void *arg);
  52. static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
  53. {
  54. /*** Service: Security test. */
  55. .type = BLE_GATT_SVC_TYPE_PRIMARY,
  56. .uuid = &gatt_svr_svc_sec_test_uuid.u,
  57. .characteristics = (struct ble_gatt_chr_def[])
  58. { {
  59. /*** Characteristic: Random number generator. */
  60. .uuid = &gatt_svr_chr_sec_test_rand_uuid.u,
  61. .access_cb = gatt_svr_chr_access_sec_test,
  62. .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC,
  63. }, {
  64. /*** Characteristic: Static value. */
  65. .uuid = &gatt_svr_chr_sec_test_static_uuid.u,
  66. .access_cb = gatt_svr_chr_access_sec_test,
  67. .flags = BLE_GATT_CHR_F_READ |
  68. BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_ENC,
  69. }, {
  70. 0, /* No more characteristics in this service. */
  71. }
  72. },
  73. },
  74. {
  75. 0, /* No more services. */
  76. },
  77. };
  78. static int
  79. gatt_svr_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len,
  80. void *dst, uint16_t *len)
  81. {
  82. uint16_t om_len;
  83. int rc;
  84. om_len = OS_MBUF_PKTLEN(om);
  85. if (om_len < min_len || om_len > max_len) {
  86. return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
  87. }
  88. rc = ble_hs_mbuf_to_flat(om, dst, max_len, len);
  89. if (rc != 0) {
  90. return BLE_ATT_ERR_UNLIKELY;
  91. }
  92. return 0;
  93. }
  94. static int
  95. gatt_svr_chr_access_sec_test(uint16_t conn_handle, uint16_t attr_handle,
  96. struct ble_gatt_access_ctxt *ctxt,
  97. void *arg)
  98. {
  99. const ble_uuid_t *uuid;
  100. int rand_num;
  101. int rc;
  102. uuid = ctxt->chr->uuid;
  103. /* Determine which characteristic is being accessed by examining its
  104. * 128-bit UUID.
  105. */
  106. if (ble_uuid_cmp(uuid, &gatt_svr_chr_sec_test_rand_uuid.u) == 0) {
  107. assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
  108. /* Respond with a 32-bit random number. */
  109. rand_num = rand();
  110. rc = os_mbuf_append(ctxt->om, &rand_num, sizeof rand_num);
  111. return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
  112. }
  113. if (ble_uuid_cmp(uuid, &gatt_svr_chr_sec_test_static_uuid.u) == 0) {
  114. switch (ctxt->op) {
  115. case BLE_GATT_ACCESS_OP_READ_CHR:
  116. rc = os_mbuf_append(ctxt->om, &gatt_svr_sec_test_static_val,
  117. sizeof gatt_svr_sec_test_static_val);
  118. return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
  119. case BLE_GATT_ACCESS_OP_WRITE_CHR:
  120. rc = gatt_svr_chr_write(ctxt->om,
  121. sizeof gatt_svr_sec_test_static_val,
  122. sizeof gatt_svr_sec_test_static_val,
  123. &gatt_svr_sec_test_static_val, NULL);
  124. return rc;
  125. default:
  126. assert(0);
  127. return BLE_ATT_ERR_UNLIKELY;
  128. }
  129. }
  130. /* Unknown characteristic; the nimble stack should not have called this
  131. * function.
  132. */
  133. assert(0);
  134. return BLE_ATT_ERR_UNLIKELY;
  135. }
  136. void
  137. gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
  138. {
  139. char buf[BLE_UUID_STR_LEN];
  140. switch (ctxt->op) {
  141. case BLE_GATT_REGISTER_OP_SVC:
  142. MODLOG_DFLT(DEBUG, "registered service %s with handle=%d\n",
  143. ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf),
  144. ctxt->svc.handle);
  145. break;
  146. case BLE_GATT_REGISTER_OP_CHR:
  147. MODLOG_DFLT(DEBUG, "registering characteristic %s with "
  148. "def_handle=%d val_handle=%d\n",
  149. ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf),
  150. ctxt->chr.def_handle,
  151. ctxt->chr.val_handle);
  152. break;
  153. case BLE_GATT_REGISTER_OP_DSC:
  154. MODLOG_DFLT(DEBUG, "registering descriptor %s with handle=%d\n",
  155. ble_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf),
  156. ctxt->dsc.handle);
  157. break;
  158. default:
  159. assert(0);
  160. break;
  161. }
  162. }
  163. int
  164. gatt_svr_init(void)
  165. {
  166. int rc;
  167. ble_svc_gap_init();
  168. ble_svc_gatt_init();
  169. rc = ble_gatts_count_cfg(gatt_svr_svcs);
  170. if (rc != 0) {
  171. return rc;
  172. }
  173. rc = ble_gatts_add_svcs(gatt_svr_svcs);
  174. if (rc != 0) {
  175. return rc;
  176. }
  177. return 0;
  178. }