1 /* 2 * Copyright (c) 2017 Nordic Semiconductor ASA 3 * Copyright (c) 2015-2017 Intel Corporation 4 * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD 5 * 6 * SPDX-License-Identifier: Apache-2.0 7 */ 8 9 #ifndef _BLE_MESH_BEARER_ADAPT_H_ 10 #define _BLE_MESH_BEARER_ADAPT_H_ 11 12 #include <sys/types.h> 13 #include "mesh_config.h" 14 #include "mesh_types.h" 15 #include "mesh_util.h" 16 #include "mesh_uuid.h" 17 #include "mesh_buf.h" 18 19 #ifdef __cplusplus 20 extern "C" { 21 #endif 22 23 /* BLE Mesh Max Connection Count */ 24 #ifdef CONFIG_BT_BLUEDROID_ENABLED 25 #define BLE_MESH_MAX_CONN CONFIG_BT_ACL_CONNECTIONS 26 #endif 27 28 #ifdef CONFIG_BT_NIMBLE_ENABLED 29 #define BLE_MESH_MAX_CONN CONFIG_BT_NIMBLE_MAX_CONNECTIONS 30 #endif 31 32 #define BLE_MESH_GAP_ADV_MAX_LEN 31 33 34 #define BLE_MESH_GATT_DEF_MTU_SIZE 23 35 36 /* BD ADDR types */ 37 #define BLE_MESH_ADDR_PUBLIC 0x00 38 #define BLE_MESH_ADDR_RANDOM 0x01 39 #define BLE_MESH_ADDR_PUBLIC_ID 0x02 40 #define BLE_MESH_ADDR_RANDOM_ID 0x03 41 42 /* BD ADDR length */ 43 #define BLE_MESH_ADDR_LEN 0x06 44 45 /* Advertising types */ 46 #define BLE_MESH_ADV_IND 0x00 47 #define BLE_MESH_ADV_DIRECT_IND 0x01 48 #define BLE_MESH_ADV_SCAN_IND 0x02 49 #define BLE_MESH_ADV_NONCONN_IND 0x03 50 #define BLE_MESH_ADV_DIRECT_IND_LOW_DUTY 0x04 51 52 /* advertising channel map */ 53 #define BLE_MESH_ADV_CHNL_37 BIT(0) 54 #define BLE_MESH_ADV_CHNL_38 BIT(1) 55 #define BLE_MESH_ADV_CHNL_39 BIT(2) 56 57 /* Advertising filter policy */ 58 #define BLE_MESH_AP_SCAN_CONN_ALL 0x00 59 #define BLE_MESH_AP_SCAN_WL_CONN_ALL 0x01 60 #define BLE_MESH_AP_SCAN_ALL_CONN_WL 0x02 61 #define BLE_MESH_AP_SCAN_CONN_WL 0x03 62 63 /* Scan types */ 64 #define BLE_MESH_SCAN_PASSIVE 0x00 65 #define BLE_MESH_SCAN_ACTIVE 0x01 66 67 /* Scan operation */ 68 #define BLE_MESH_SCAN_DISABLE 0x00 69 #define BLE_MESH_SCAN_ENABLE 0x01 70 71 /* Scan duplicate operation */ 72 #define BLE_MESH_SCAN_FILTER_DUP_DISABLE 0x00 73 #define BLE_MESH_SCAN_FILTER_DUP_ENABLE 0x01 74 75 /* Scan filter policy */ 76 #define BLE_MESH_SP_ADV_ALL 0x00 77 #define BLE_MESH_SP_ADV_WL 0x01 78 #define BLE_MESH_SP_ADV_ALL_RPA_DIR_ADV 0x02 79 #define BLE_MESH_SP_ADV_WL_RPA_DIR_ADV 0x03 80 81 /* Error codes for Error response PDU */ 82 #define BLE_MESH_ATT_ERR_INVALID_HANDLE 0x01 83 #define BLE_MESH_ATT_ERR_READ_NOT_PERMITTED 0x02 84 #define BLE_MESH_ATT_ERR_WRITE_NOT_PERMITTED 0x03 85 #define BLE_MESH_ATT_ERR_INVALID_PDU 0x04 86 #define BLE_MESH_ATT_ERR_AUTHENTICATION 0x05 87 #define BLE_MESH_ATT_ERR_NOT_SUPPORTED 0x06 88 #define BLE_MESH_ATT_ERR_INVALID_OFFSET 0x07 89 #define BLE_MESH_ATT_ERR_AUTHORIZATION 0x08 90 #define BLE_MESH_ATT_ERR_PREPARE_QUEUE_FULL 0x09 91 #define BLE_MESH_ATT_ERR_ATTRIBUTE_NOT_FOUND 0x0a 92 #define BLE_MESH_ATT_ERR_ATTRIBUTE_NOT_LONG 0x0b 93 #define BLE_MESH_ATT_ERR_ENCRYPTION_KEY_SIZE 0x0c 94 #define BLE_MESH_ATT_ERR_INVALID_ATTRIBUTE_LEN 0x0d 95 #define BLE_MESH_ATT_ERR_UNLIKELY 0x0e 96 #define BLE_MESH_ATT_ERR_INSUFFICIENT_ENCRYPTION 0x0f 97 #define BLE_MESH_ATT_ERR_UNSUPPORTED_GROUP_TYPE 0x10 98 #define BLE_MESH_ATT_ERR_INSUFFICIENT_RESOURCES 0x11 99 100 /* Common Profile Error Codes (from CSS) */ 101 #define BLE_MESH_ATT_ERR_WRITE_REQ_REJECTED 0xfc 102 #define BLE_MESH_ATT_ERR_CCC_IMPROPER_CONF 0xfd 103 #define BLE_MESH_ATT_ERR_PROCEDURE_IN_PROGRESS 0xfe 104 #define BLE_MESH_ATT_ERR_OUT_OF_RANGE 0xff 105 106 /* EIR/AD data type definitions */ 107 #define BLE_MESH_DATA_FLAGS 0x01 /* AD flags */ 108 #define BLE_MESH_DATA_UUID16_SOME 0x02 /* 16-bit UUID, more available */ 109 #define BLE_MESH_DATA_UUID16_ALL 0x03 /* 16-bit UUID, all listed */ 110 #define BLE_MESH_DATA_UUID32_SOME 0x04 /* 32-bit UUID, more available */ 111 #define BLE_MESH_DATA_UUID32_ALL 0x05 /* 32-bit UUID, all listed */ 112 #define BLE_MESH_DATA_UUID128_SOME 0x06 /* 128-bit UUID, more available */ 113 #define BLE_MESH_DATA_UUID128_ALL 0x07 /* 128-bit UUID, all listed */ 114 #define BLE_MESH_DATA_NAME_SHORTENED 0x08 /* Shortened name */ 115 #define BLE_MESH_DATA_NAME_COMPLETE 0x09 /* Complete name */ 116 #define BLE_MESH_DATA_TX_POWER 0x0a /* Tx Power */ 117 #define BLE_MESH_DATA_SOLICIT16 0x14 /* Solicit UUIDs, 16-bit */ 118 #define BLE_MESH_DATA_SOLICIT128 0x15 /* Solicit UUIDs, 128-bit */ 119 #define BLE_MESH_DATA_SVC_DATA16 0x16 /* Service data, 16-bit UUID */ 120 #define BLE_MESH_DATA_GAP_APPEARANCE 0x19 /* GAP appearance */ 121 #define BLE_MESH_DATA_SOLICIT32 0x1f /* Solicit UUIDs, 32-bit */ 122 #define BLE_MESH_DATA_SVC_DATA32 0x20 /* Service data, 32-bit UUID */ 123 #define BLE_MESH_DATA_SVC_DATA128 0x21 /* Service data, 128-bit UUID */ 124 #define BLE_MESH_DATA_URI 0x24 /* URI */ 125 #define BLE_MESH_DATA_MESH_PROV 0x29 /* Mesh Provisioning PDU */ 126 #define BLE_MESH_DATA_MESH_MESSAGE 0x2a /* Mesh Networking PDU */ 127 #define BLE_MESH_DATA_MESH_BEACON 0x2b /* Mesh Beacon */ 128 129 #define BLE_MESH_DATA_MANUFACTURER_DATA 0xff /* Manufacturer Specific Data */ 130 131 #define BLE_MESH_AD_LIMITED 0x01 /* Limited Discoverable */ 132 #define BLE_MESH_AD_GENERAL 0x02 /* General Discoverable */ 133 #define BLE_MESH_AD_NO_BREDR 0x04 /* BR/EDR not supported */ 134 135 /* Client Characteristic Configuration Values */ 136 137 /** @def BLE_MESH_GATT_CCC_NOTIFY 138 * @brief Client Characteristic Configuration Notification. 139 * 140 * If set, changes to Characteristic Value shall be notified. 141 */ 142 #define BLE_MESH_GATT_CCC_NOTIFY 0x0001 143 144 /** @def BLE_MESH_GATT_CCC_INDICATE 145 * @brief Client Characteristic Configuration Indication. 146 * 147 * If set, changes to Characteristic Value shall be indicated. 148 */ 149 #define BLE_MESH_GATT_CCC_INDICATE 0x0002 150 151 /** @def BLE_MESH_GATT_ERR 152 * @brief Construct error return value for attribute read and write callbacks. 153 * 154 * @param _att_err ATT error code 155 * 156 * @return Appropriate error code for the attribute callbacks. 157 * 158 */ 159 #define BLE_MESH_GATT_ERR(_att_err) (-(_att_err)) 160 161 enum { 162 BLE_MESH_GATT_ITER_STOP = 0, 163 BLE_MESH_GATT_ITER_CONTINUE, 164 }; 165 166 /* GATT attribute permission bit field values */ 167 enum { 168 /** No operations supported, e.g. for notify-only */ 169 BLE_MESH_GATT_PERM_NONE = 0, 170 171 /** Attribute read permission. */ 172 BLE_MESH_GATT_PERM_READ = BIT(0), 173 174 /** Attribute write permission. */ 175 BLE_MESH_GATT_PERM_WRITE = BIT(1), 176 177 /** Attribute read permission with encryption. 178 * 179 * If set, requires encryption for read access. 180 */ 181 BLE_MESH_GATT_PERM_READ_ENCRYPT = BIT(2), 182 183 /** Attribute write permission with encryption. 184 * 185 * If set, requires encryption for write access. 186 */ 187 BLE_MESH_GATT_PERM_WRITE_ENCRYPT = BIT(3), 188 189 /** Attribute read permission with authentication. 190 * 191 * If set, requires encryption using authenticated link-key for read 192 * access. 193 */ 194 BLE_MESH_GATT_PERM_READ_AUTHEN = BIT(4), 195 196 /** Attribute write permission with authentication. 197 * 198 * If set, requires encryption using authenticated link-key for write 199 * access. 200 */ 201 BLE_MESH_GATT_PERM_WRITE_AUTHEN = BIT(5), 202 203 /** Attribute prepare write permission. 204 * 205 * If set, allows prepare writes with use of BT_GATT_WRITE_FLAG_PREPARE 206 * passed to write callback. 207 */ 208 BLE_MESH_GATT_PERM_PREPARE_WRITE = BIT(6), 209 }; 210 211 /** Advertising options */ 212 enum { 213 /** Convenience value when no options are specified. */ 214 BLE_MESH_ADV_OPT_NONE = 0, 215 216 /** Advertise as connectable. Type of advertising is determined by 217 * providing SCAN_RSP data and/or enabling local privacy support. 218 */ 219 BLE_MESH_ADV_OPT_CONNECTABLE = BIT(0), 220 221 /** Don't try to resume connectable advertising after a connection. 222 * This option is only meaningful when used together with 223 * BLE_MESH_ADV_OPT_CONNECTABLE. If set the advertising will be stopped 224 * when bt_le_adv_stop() is called or when an incoming (slave) 225 * connection happens. If this option is not set the stack will 226 * take care of keeping advertising enabled even as connections 227 * occur. 228 */ 229 BLE_MESH_ADV_OPT_ONE_TIME = BIT(1), 230 }; 231 232 /* Defined GAP timers */ 233 #define BLE_MESH_GAP_SCAN_FAST_INTERVAL 0x0060 /* 60 ms */ 234 #define BLE_MESH_GAP_SCAN_FAST_WINDOW 0x0030 /* 30 ms */ 235 #define BLE_MESH_GAP_SCAN_SLOW_INTERVAL_1 0x0800 /* 1.28 s */ 236 #define BLE_MESH_GAP_SCAN_SLOW_WINDOW_1 0x0012 /* 11.25 ms */ 237 #define BLE_MESH_GAP_SCAN_SLOW_INTERVAL_2 0x1000 /* 2.56 s */ 238 #define BLE_MESH_GAP_SCAN_SLOW_WINDOW_2 0x0012 /* 11.25 ms */ 239 #define BLE_MESH_GAP_ADV_FAST_INT_MIN_0 0x0020 /* 20 ms */ 240 #define BLE_MESH_GAP_ADV_FAST_INT_MAX_0 0x0020 /* 20 ms */ 241 #define BLE_MESH_GAP_ADV_FAST_INT_MIN_1 0x0030 /* 30 ms */ 242 #define BLE_MESH_GAP_ADV_FAST_INT_MAX_1 0x0060 /* 60 ms */ 243 #define BLE_MESH_GAP_ADV_FAST_INT_MIN_2 0x00a0 /* 100 ms */ 244 #define BLE_MESH_GAP_ADV_FAST_INT_MAX_2 0x00f0 /* 150 ms */ 245 #define BLE_MESH_GAP_ADV_SLOW_INT_MIN 0x0320 /* 500 ms */ 246 #define BLE_MESH_GAP_ADV_SLOW_INT_MAX 0x0320 /* 500 ms */ 247 #define BLE_MESH_GAP_INIT_CONN_INT_MIN 0x0018 /* 30 ms */ 248 #define BLE_MESH_GAP_INIT_CONN_INT_MAX 0x0028 /* 50 ms */ 249 250 /* Characteristic Properties Bit field values */ 251 252 /** @def BLE_MESH_GATT_CHRC_BROADCAST 253 * @brief Characteristic broadcast property. 254 * 255 * If set, permits broadcasts of the Characteristic Value using Server 256 * Characteristic Configuration Descriptor. 257 */ 258 #define BLE_MESH_GATT_CHRC_BROADCAST 0x01 259 260 /** @def BLE_MESH_GATT_CHRC_READ 261 * @brief Characteristic read property. 262 * 263 * If set, permits reads of the Characteristic Value. 264 */ 265 #define BLE_MESH_GATT_CHRC_READ 0x02 266 267 /** @def BLE_MESH_GATT_CHRC_WRITE_WITHOUT_RESP 268 * @brief Characteristic write without response property. 269 * 270 * If set, permits write of the Characteristic Value without response. 271 */ 272 #define BLE_MESH_GATT_CHRC_WRITE_WITHOUT_RESP 0x04 273 274 /** @def BLE_MESH_GATT_CHRC_WRITE 275 * @brief Characteristic write with response property. 276 * 277 * If set, permits write of the Characteristic Value with response. 278 */ 279 #define BLE_MESH_GATT_CHRC_WRITE 0x08 280 281 /** @def BLE_MESH_GATT_CHRC_NOTIFY 282 * @brief Characteristic notify property. 283 * 284 * If set, permits notifications of a Characteristic Value without 285 * acknowledgment. 286 */ 287 #define BLE_MESH_GATT_CHRC_NOTIFY 0x10 288 289 /** @def BLE_MESH_GATT_CHRC_INDICATE 290 * @brief Characteristic indicate property. 291 * 292 * If set, permits indications of a Characteristic Value with acknowledgment. 293 */ 294 #define BLE_MESH_GATT_CHRC_INDICATE 0x20 295 296 /** @def BLE_MESH_GATT_CHRC_AUTH 297 * @brief Characteristic Authenticated Signed Writes property. 298 * 299 * If set, permits signed writes to the Characteristic Value. 300 */ 301 #define BLE_MESH_GATT_CHRC_AUTH 0x40 302 303 /** @def BLE_MESH_GATT_CHRC_EXT_PROP 304 * @brief Characteristic Extended Properties property. 305 * 306 * If set, additional characteristic properties are defined in the 307 * Characteristic Extended Properties Descriptor. 308 */ 309 #define BLE_MESH_GATT_CHRC_EXT_PROP 0x80 310 311 /** @brief Characteristic Attribute Value. */ 312 struct bt_mesh_gatt_char { 313 /** Characteristic UUID. */ 314 const struct bt_mesh_uuid *uuid; 315 /** Characteristic properties. */ 316 uint8_t properties; 317 }; 318 319 /** @brief GATT Service structure */ 320 struct bt_mesh_gatt_service { 321 /** Service Attributes */ 322 struct bt_mesh_gatt_attr *attrs; 323 /** Service Attribute count */ 324 uint16_t attr_count; 325 sys_snode_t node; 326 }; 327 328 typedef struct { 329 uint8_t type; 330 uint8_t val[6]; 331 } bt_mesh_addr_t; 332 333 /** Description of different data types that can be encoded into 334 * advertising data. Used to form arrays that are passed to the 335 * bt_le_adv_start() function. 336 */ 337 struct bt_mesh_adv_data { 338 uint8_t type; 339 uint8_t data_len; 340 const uint8_t *data; 341 }; 342 343 /** @brief Helper to declare elements of bt_data arrays 344 * 345 * This macro is mainly for creating an array of struct 346 * bt_mesh_adv_data elements which is then passed to 347 * bt_le_adv_start(). 348 * 349 * @param _type Type of advertising data field 350 * @param _data Pointer to the data field payload 351 * @param _data_len Number of bytes behind the _data pointer 352 */ 353 #define BLE_MESH_ADV_DATA(_type, _data, _data_len) \ 354 { \ 355 .type = (_type), \ 356 .data_len = (_data_len), \ 357 .data = (const uint8_t *)(_data), \ 358 } 359 360 /** @brief Helper to declare elements of bt_data arrays 361 * 362 * This macro is mainly for creating an array of struct bt_mesh_adv_data 363 * elements which is then passed to bt_le_adv_start(). 364 * 365 * @param _type Type of advertising data field 366 * @param _bytes Variable number of single-byte parameters 367 */ 368 #define BLE_MESH_ADV_DATA_BYTES(_type, _bytes...) \ 369 BLE_MESH_ADV_DATA(_type, ((uint8_t []) { _bytes }), \ 370 sizeof((uint8_t []) { _bytes })) 371 372 /* BLE Mesh Advertising Parameters */ 373 struct bt_mesh_adv_param { 374 /** Bit-field of advertising options */ 375 uint8_t options; 376 377 /** Minimum Advertising Interval (N * 0.625) */ 378 uint16_t interval_min; 379 380 /** Maximum Advertising Interval (N * 0.625) */ 381 uint16_t interval_max; 382 }; 383 384 #if CONFIG_BLE_MESH_SUPPORT_BLE_ADV 385 enum bt_mesh_ble_adv_priority { 386 BLE_MESH_BLE_ADV_PRIO_LOW, 387 BLE_MESH_BLE_ADV_PRIO_HIGH, 388 }; 389 390 struct bt_mesh_ble_adv_param { 391 uint16_t interval; /* Advertising interval */ 392 uint8_t adv_type; /* Advertising type */ 393 uint8_t own_addr_type; /* Own address type */ 394 uint8_t peer_addr_type; /* Peer address type */ 395 uint8_t peer_addr[6]; /* Peer address */ 396 uint16_t duration; /* Duration is milliseconds */ 397 uint16_t period; /* Period in milliseconds */ 398 uint16_t count; /* Number of advertising duration */ 399 uint8_t priority:2; /* Priority of BLE advertising packet */ 400 }; 401 402 struct bt_mesh_ble_adv_data { 403 uint8_t adv_data_len; /* Advertising data length */ 404 uint8_t adv_data[31]; /* Advertising data */ 405 uint8_t scan_rsp_data_len; /* Scan response data length */ 406 uint8_t scan_rsp_data[31]; /* Scan response data */ 407 }; 408 #endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ 409 410 /* BLE Mesh scan parameters */ 411 struct bt_mesh_scan_param { 412 /** Scan type (BLE_MESH_SCAN_ACTIVE or BLE_MESH_SCAN_PASSIVE) */ 413 uint8_t type; 414 415 /** Duplicate filtering (BLE_MESH_SCAN_FILTER_DUP_ENABLE or 416 * BLE_MESH_SCAN_FILTER_DUP_DISABLE) 417 */ 418 uint8_t filter_dup; 419 420 /** Scan interval (N * 0.625 ms) */ 421 uint16_t interval; 422 423 /** Scan window (N * 0.625 ms) */ 424 uint16_t window; 425 426 /** BLE scan filter policy */ 427 uint8_t scan_fil_policy; 428 }; 429 430 struct bt_mesh_conn { 431 uint16_t handle; 432 bt_mesh_atomic_t ref; 433 }; 434 435 /** @typedef bt_mesh_scan_cb_t 436 * @brief Callback type for reporting LE scan results. 437 * 438 * A function of this type is given to the bt_le_scan_start() function 439 * and will be called for any discovered LE device. 440 * 441 * @param addr Advertiser LE address and type. 442 * @param rssi Strength of advertiser signal. 443 * @param adv_type Type of advertising response from advertiser. 444 * @param data Buffer containing advertiser data. 445 */ 446 typedef void bt_mesh_scan_cb_t(const bt_mesh_addr_t *addr, int8_t rssi, 447 uint8_t adv_type, struct net_buf_simple *buf); 448 449 /* @typedef bt_mesh_dh_key_cb_t 450 * @brief Callback type for DH Key calculation. 451 * 452 * Used to notify of the calculated DH Key. 453 * 454 * @param key Public key. 455 * @param idx Provisioning link index, only used by Provisioner. 456 * 457 * @return The DH Key, or NULL in case of failure. 458 */ 459 typedef void (*bt_mesh_dh_key_cb_t)(const uint8_t key[32], const uint8_t idx); 460 461 /** @typedef bt_mesh_gatt_attr_func_t 462 * @brief Attribute iterator callback. 463 * 464 * @param attr Attribute found. 465 * @param user_data Data given. 466 * 467 * @return BLE_MESH_GATT_ITER_CONTINUE if should continue to the next attribute 468 * or BLE_MESH_GATT_ITER_STOP to stop. 469 */ 470 typedef uint8_t (*bt_mesh_gatt_attr_func_t)(const struct bt_mesh_gatt_attr *attr, 471 void *user_data); 472 473 /** @brief Connection callback structure. 474 * 475 * This structure is used for tracking the state of a connection. 476 * It is registered with the help of the bt_mesh_gatts_conn_cb_register() API. 477 * It's permissible to register multiple instances of this @ref bt_conn_cb 478 * type, in case different modules of an application are interested in 479 * tracking the connection state. If a callback is not of interest for 480 * an instance, it may be set to NULL and will as a consequence not be 481 * used for that instance. 482 */ 483 struct bt_mesh_conn_cb { 484 /** @brief A new connection has been established. 485 * 486 * This callback notifies the application of a new connection. 487 * In case the err parameter is non-zero it means that the 488 * connection establishment failed. 489 * 490 * @param conn New connection object. 491 * @param err HCI error. Zero for success, non-zero otherwise. 492 */ 493 void (*connected)(struct bt_mesh_conn *conn, uint8_t err); 494 495 /** @brief A connection has been disconnected. 496 * 497 * This callback notifies the application that a connection 498 * has been disconnected. 499 * 500 * @param conn Connection object. 501 * @param reason HCI reason for the disconnection. 502 */ 503 void (*disconnected)(struct bt_mesh_conn *conn, uint8_t reason); 504 }; 505 506 struct bt_mesh_prov_conn_cb { 507 void (*connected)(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn, int id); 508 509 void (*disconnected)(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn, uint8_t reason); 510 511 ssize_t (*prov_write_descr)(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn); 512 513 ssize_t (*prov_notify)(struct bt_mesh_conn *conn, uint8_t *data, uint16_t len); 514 515 ssize_t (*proxy_write_descr)(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn); 516 517 ssize_t (*proxy_notify)(struct bt_mesh_conn *conn, uint8_t *data, uint16_t len); 518 }; 519 520 /** @brief GATT Attribute structure. */ 521 struct bt_mesh_gatt_attr { 522 /** Attribute UUID */ 523 const struct bt_mesh_uuid *uuid; 524 525 /** Attribute read callback 526 * 527 * @param conn The connection that is requesting to read 528 * @param attr The attribute that's being read 529 * @param buf Buffer to place the read result in 530 * @param len Length of data to read 531 * @param offset Offset to start reading from 532 * 533 * @return Number fo bytes read, or in case of an error 534 * BLE_MESH_GATT_ERR() with a specific ATT error code. 535 */ 536 ssize_t (*read)(struct bt_mesh_conn *conn, 537 const struct bt_mesh_gatt_attr *attr, 538 void *buf, uint16_t len, 539 uint16_t offset); 540 541 /** Attribute write callback 542 * 543 * @param conn The connection that is requesting to write 544 * @param attr The attribute that's being written 545 * @param buf Buffer with the data to write 546 * @param len Number of bytes in the buffer 547 * @param offset Offset to start writing from 548 * @param flags Flags (BT_GATT_WRITE_*) 549 * 550 * @return Number of bytes written, or in case of an error 551 * BLE_MESH_GATT_ERR() with a specific ATT error code. 552 */ 553 ssize_t (*write)(struct bt_mesh_conn *conn, 554 const struct bt_mesh_gatt_attr *attr, 555 const void *buf, uint16_t len, 556 uint16_t offset, uint8_t flags); 557 558 /** Attribute user data */ 559 void *user_data; 560 /** Attribute handle */ 561 uint16_t handle; 562 /** Attribute permissions */ 563 uint8_t perm; 564 }; 565 566 /** @def BLE_MESH_GATT_PRIMARY_SERVICE 567 * @brief Primary Service Declaration Macro. 568 * 569 * Helper macro to declare a primary service attribute. 570 * 571 * @param _service Service attribute value. 572 */ 573 #define BLE_MESH_GATT_PRIMARY_SERVICE(_service) \ 574 { \ 575 .uuid = BLE_MESH_UUID_GATT_PRIMARY, \ 576 .perm = BLE_MESH_GATT_PERM_READ, \ 577 .read = bt_mesh_gatts_attr_read_service, \ 578 .user_data = _service, \ 579 } 580 581 /** @def BLE_MESH_GATT_SECONDARY_SERVICE 582 * @brief Secondary Service Declaration Macro. 583 * 584 * Helper macro to declare a secondary service attribute. 585 * 586 * @param _service Service attribute value. 587 */ 588 #define BLE_MESH_GATT_SECONDARY_SERVICE(_service) \ 589 { \ 590 .uuid = BLE_MESH_UUID_GATT_SECONDARY, \ 591 .perm = BLE_MESH_GATT_PERM_READ, \ 592 .read = bt_mesh_gatts_attr_read_service, \ 593 .user_data = _service, \ 594 } 595 596 /** @def BLE_MESH_GATT_INCLUDE_SERVICE 597 * @brief Include Service Declaration Macro. 598 * 599 * Helper macro to declare database internal include service attribute. 600 * 601 * @param _service_incl the first service attribute of service to include 602 */ 603 #define BLE_MESH_GATT_INCLUDE_SERVICE(_service_incl) \ 604 { \ 605 .uuid = BLE_MESH_UUID_GATT_INCLUDE, \ 606 .perm = BLE_MESH_GATT_PERM_READ, \ 607 .read = bt_mesh_gatts_attr_read_included, \ 608 .user_data = _service_incl, \ 609 } 610 611 /** @def BLE_MESH_GATT_CHARACTERISTIC 612 * @brief Characteristic Declaration Macro. 613 * 614 * Helper macro to declare a characteristic attribute. 615 * 616 * @param _uuid Characteristic attribute uuid. 617 * @param _props Characteristic attribute properties. 618 */ 619 #define BLE_MESH_GATT_CHARACTERISTIC(_uuid, _props) \ 620 { \ 621 .uuid = BLE_MESH_UUID_GATT_CHRC, \ 622 .perm = BLE_MESH_GATT_PERM_READ, \ 623 .read = bt_mesh_gatts_attr_read_chrc, \ 624 .user_data = (&(struct bt_mesh_gatt_char) { .uuid = _uuid, \ 625 .properties = _props, }), \ 626 } 627 628 /** @def BLE_MESH_GATT_DESCRIPTOR 629 * @brief Descriptor Declaration Macro. 630 * 631 * Helper macro to declare a descriptor attribute. 632 * 633 * @param _uuid Descriptor attribute uuid. 634 * @param _perm Descriptor attribute access permissions. 635 * @param _read Descriptor attribute read callback. 636 * @param _write Descriptor attribute write callback. 637 * @param _value Descriptor attribute value. 638 */ 639 #define BLE_MESH_GATT_DESCRIPTOR(_uuid, _perm, _read, _write, _value) \ 640 { \ 641 .uuid = _uuid, \ 642 .perm = _perm, \ 643 .read = _read, \ 644 .write = _write, \ 645 .user_data = _value, \ 646 } 647 648 /** @def BLE_MESH_GATT_SERVICE 649 * @brief Service Structure Declaration Macro. 650 * 651 * Helper macro to declare a service structure. 652 * 653 * @param _attrs Service attributes. 654 */ 655 #define BLE_MESH_GATT_SERVICE(_attrs) \ 656 { \ 657 .attrs = _attrs, \ 658 .attr_count = ARRAY_SIZE(_attrs), \ 659 } 660 661 int bt_mesh_host_init(void); 662 663 int bt_le_adv_start(const struct bt_mesh_adv_param *param, 664 const struct bt_mesh_adv_data *ad, size_t ad_len, 665 const struct bt_mesh_adv_data *sd, size_t sd_len); 666 667 #if CONFIG_BLE_MESH_SUPPORT_BLE_ADV 668 int bt_mesh_ble_adv_start(const struct bt_mesh_ble_adv_param *param, 669 const struct bt_mesh_ble_adv_data *data); 670 #endif 671 672 int bt_le_adv_stop(void); 673 674 int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t cb); 675 676 int bt_le_scan_stop(void); 677 678 typedef enum { 679 BLE_MESH_WHITELIST_REMOVE, 680 BLE_MESH_WHITELIST_ADD, 681 } bt_mesh_wl_operation; 682 683 struct bt_mesh_white_list { 684 bool add_remove; 685 uint8_t remote_bda[BLE_MESH_ADDR_LEN]; 686 uint8_t addr_type; 687 /* For Bluedroid host, this callback is used to notify the 688 * result of updating white list. 689 */ 690 void (*update_wl_comp_cb)(uint8_t status, bt_mesh_wl_operation wl_operation); 691 }; 692 693 int bt_le_update_white_list(struct bt_mesh_white_list *wl); 694 695 void bt_mesh_gatts_conn_cb_register(struct bt_mesh_conn_cb *cb); 696 void bt_mesh_gatts_conn_cb_deregister(void); 697 698 int bt_mesh_gatts_disconnect(struct bt_mesh_conn *conn, uint8_t reason); 699 700 int bt_mesh_gatts_service_register(struct bt_mesh_gatt_service *svc); 701 int bt_mesh_gatts_service_deregister(struct bt_mesh_gatt_service *svc); 702 703 int bt_mesh_gatts_service_unregister(struct bt_mesh_gatt_service *svc); 704 705 ssize_t bt_mesh_gatts_attr_read_included(struct bt_mesh_conn *conn, 706 const struct bt_mesh_gatt_attr *attr, 707 void *buf, uint16_t len, uint16_t offset); 708 709 ssize_t bt_mesh_gatts_attr_read(struct bt_mesh_conn *conn, 710 const struct bt_mesh_gatt_attr *attr, 711 void *buf, uint16_t buf_len, uint16_t offset, 712 const void *value, uint16_t value_len); 713 714 ssize_t bt_mesh_gatts_attr_read_service(struct bt_mesh_conn *conn, 715 const struct bt_mesh_gatt_attr *attr, 716 void *buf, uint16_t len, uint16_t offset); 717 718 ssize_t bt_mesh_gatts_attr_read_chrc(struct bt_mesh_conn *conn, 719 const struct bt_mesh_gatt_attr *attr, 720 void *buf, uint16_t len, uint16_t offset); 721 722 int bt_mesh_gatts_notify(struct bt_mesh_conn *conn, 723 const struct bt_mesh_gatt_attr *attr, 724 const void *data, uint16_t len); 725 726 uint16_t bt_mesh_gatt_get_mtu(struct bt_mesh_conn *conn); 727 728 /** APIs added by Espressif */ 729 int bt_mesh_gatts_service_stop(struct bt_mesh_gatt_service *svc); 730 int bt_mesh_gatts_service_start(struct bt_mesh_gatt_service *svc); 731 732 int bt_mesh_gatts_set_local_device_name(const char *name); 733 734 void bt_mesh_gattc_conn_cb_register(struct bt_mesh_prov_conn_cb *cb); 735 void bt_mesh_gattc_conn_cb_deregister(void); 736 737 uint8_t bt_mesh_gattc_get_free_conn_count(void); 738 739 uint16_t bt_mesh_gattc_get_service_uuid(struct bt_mesh_conn *conn); 740 741 int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, uint16_t service_uuid); 742 743 void bt_gattc_conn_close(struct bt_mesh_conn *conn); 744 745 void bt_mesh_gattc_exchange_mtu(uint8_t index); 746 747 uint16_t bt_mesh_gattc_get_mtu_info(struct bt_mesh_conn *conn); 748 749 int bt_mesh_gattc_write_no_rsp(struct bt_mesh_conn *conn, 750 const struct bt_mesh_gatt_attr *attr, 751 const void *data, uint16_t len); 752 753 void bt_mesh_gattc_disconnect(struct bt_mesh_conn *conn); 754 755 struct bt_mesh_conn *bt_mesh_conn_ref(struct bt_mesh_conn *conn); 756 757 void bt_mesh_conn_unref(struct bt_mesh_conn *conn); 758 759 void bt_mesh_gatt_init(void); 760 void bt_mesh_gatt_deinit(void); 761 762 void bt_mesh_adapt_init(void); 763 764 void bt_mesh_set_private_key(const uint8_t pri_key[32]); 765 766 const uint8_t *bt_mesh_pub_key_get(void); 767 768 bool bt_mesh_check_public_key(const uint8_t key[64]); 769 770 int bt_mesh_dh_key_gen(const uint8_t remote_pk[64], bt_mesh_dh_key_cb_t cb, const uint8_t idx); 771 772 int bt_mesh_encrypt_le(const uint8_t key[16], const uint8_t plaintext[16], 773 uint8_t enc_data[16]); 774 775 int bt_mesh_encrypt_be(const uint8_t key[16], const uint8_t plaintext[16], 776 uint8_t enc_data[16]); 777 778 enum { 779 BLE_MESH_EXCEP_LIST_ADD = 0, 780 BLE_MESH_EXCEP_LIST_REMOVE, 781 BLE_MESH_EXCEP_LIST_CLEAN, 782 }; 783 784 enum { 785 BLE_MESH_EXCEP_INFO_ADV_ADDR = 0, 786 BLE_MESH_EXCEP_INFO_MESH_LINK_ID, 787 BLE_MESH_EXCEP_INFO_MESH_BEACON, 788 BLE_MESH_EXCEP_INFO_MESH_PROV_ADV, 789 BLE_MESH_EXCEP_INFO_MESH_PROXY_ADV, 790 }; 791 792 enum { 793 BLE_MESH_EXCEP_CLEAN_ADDR_LIST = BIT(0), 794 BLE_MESH_EXCEP_CLEAN_MESH_LINK_ID_LIST = BIT(1), 795 BLE_MESH_EXCEP_CLEAN_MESH_BEACON_LIST = BIT(2), 796 BLE_MESH_EXCEP_CLEAN_MESH_PROV_ADV_LIST = BIT(3), 797 BLE_MESH_EXCEP_CLEAN_MESH_PROXY_ADV_LIST = BIT(4), 798 BLE_MESH_EXCEP_CLEAN_ALL_LIST = 0xFFFF, 799 }; 800 801 int bt_mesh_update_exceptional_list(uint8_t sub_code, uint8_t type, void *info); 802 803 #ifdef __cplusplus 804 } 805 #endif 806 807 #endif /* _BLE_MESH_BEARER_ADAPT_H_ */ 808