1 /** @file 2 * @brief Bluetooth Mesh Access Layer APIs. 3 */ 4 5 /* 6 * Copyright (c) 2017 Intel Corporation 7 * 8 * SPDX-License-Identifier: Apache-2.0 9 */ 10 #ifndef __BT_MESH_ACCESS_H 11 #define __BT_MESH_ACCESS_H 12 13 /** 14 * @brief Bluetooth Mesh Access Layer 15 * @defgroup bt_mesh_access Bluetooth Mesh Access Layer 16 * @ingroup bt_mesh 17 * @{ 18 */ 19 20 #ifdef __cplusplus 21 extern "C" { 22 #endif 23 24 #define BT_MESH_ADDR_UNASSIGNED 0x0000 25 #define BT_MESH_ADDR_ALL_NODES 0xffff 26 #define BT_MESH_ADDR_PROXIES 0xfffc 27 #define BT_MESH_ADDR_FRIENDS 0xfffd 28 #define BT_MESH_ADDR_RELAYS 0xfffe 29 30 #define BT_MESH_KEY_UNUSED 0xffff 31 #define BT_MESH_KEY_DEV 0xfffe 32 33 /*[Genie begin] add by lgy at 2020-09-10*/ 34 #define BT_MESH_ADDR_TMALL_GENIE 0xf000 35 #define BT_MESH_ADDR_GENIE_ALL_NODES 0xcfff 36 /*[Genie end] add by lgy at 2020-09-10*/ 37 38 /** Helper to define a mesh element within an array. 39 * 40 * In case the element has no SIG or Vendor models the helper 41 * macro BT_MESH_MODEL_NONE can be given instead. 42 * 43 * @param _loc Location Descriptor. 44 * @param _mods Array of models. 45 * @param _vnd_mods Array of vendor models. 46 */ 47 #define BT_MESH_ELEM(_loc, _mods, _vnd_mods, _grop_addr) \ 48 { \ 49 .loc = (_loc), .model_count = ARRAY_SIZE(_mods), .models = (_mods), .vnd_model_count = ARRAY_SIZE(_vnd_mods), \ 50 .vnd_models = (_vnd_mods), .grop_addr = (_grop_addr), \ 51 } 52 53 /** Abstraction that describes a Mesh Element */ 54 struct bt_mesh_elem { 55 /* Unicast Address. Set at runtime during provisioning. */ 56 u16_t addr; 57 u16_t grop_addr; 58 59 /* Location Descriptor (GATT Bluetooth Namespace Descriptors) */ 60 u16_t loc; 61 62 u8_t model_count; 63 u8_t vnd_model_count; 64 65 struct bt_mesh_model *models; 66 struct bt_mesh_model *vnd_models; 67 }; 68 69 /* Foundation Models */ 70 #define BT_MESH_MODEL_ID_CFG_SRV 0x0000 71 #define BT_MESH_MODEL_ID_CFG_CLI 0x0001 72 #define BT_MESH_MODEL_ID_HEALTH_SRV 0x0002 73 #define BT_MESH_MODEL_ID_HEALTH_CLI 0x0003 74 75 /* Models from the Mesh Model Specification */ 76 #define BT_MESH_MODEL_ID_GEN_ONOFF_SRV 0x1000 77 #define BT_MESH_MODEL_ID_GEN_ONOFF_CLI 0x1001 78 #define BT_MESH_MODEL_ID_GEN_LEVEL_SRV 0x1002 79 #define BT_MESH_MODEL_ID_GEN_LEVEL_CLI 0x1003 80 #define BT_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV 0x1004 81 #define BT_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI 0x1005 82 #define BT_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV 0x1006 83 #define BT_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV 0x1007 84 #define BT_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI 0x1008 85 #define BT_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV 0x1009 86 #define BT_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV 0x100a 87 #define BT_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI 0x100b 88 #define BT_MESH_MODEL_ID_GEN_BATTERY_SRV 0x100c 89 #define BT_MESH_MODEL_ID_GEN_BATTERY_CLI 0x100d 90 #define BT_MESH_MODEL_ID_GEN_LOCATION_SRV 0x100e 91 #define BT_MESH_MODEL_ID_GEN_LOCATION_SETUPSRV 0x100f 92 #define BT_MESH_MODEL_ID_GEN_LOCATION_CLI 0x1010 93 #define BT_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV 0x1011 94 #define BT_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV 0x1012 95 #define BT_MESH_MODEL_ID_GEN_USER_PROP_SRV 0x1013 96 #define BT_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV 0x1014 97 #define BT_MESH_MODEL_ID_GEN_PROP_CLI 0x1015 98 #define BT_MESH_MODEL_ID_SENSOR_SRV 0x1100 99 #define BT_MESH_MODEL_ID_SENSOR_SETUP_SRV 0x1101 100 #define BT_MESH_MODEL_ID_SENSOR_CLI 0x1102 101 #define BT_MESH_MODEL_ID_TIME_SRV 0x1200 102 #define BT_MESH_MODEL_ID_TIME_SETUP_SRV 0x1201 103 #define BT_MESH_MODEL_ID_TIME_CLI 0x1202 104 #define BT_MESH_MODEL_ID_SCENE_SRV 0x1203 105 #define BT_MESH_MODEL_ID_SCENE_SETUP_SRV 0x1204 106 #define BT_MESH_MODEL_ID_SCENE_CLI 0x1205 107 #define BT_MESH_MODEL_ID_SCHEDULER_SRV 0x1206 108 #define BT_MESH_MODEL_ID_SCHEDULER_SETUP_SRV 0x1207 109 #define BT_MESH_MODEL_ID_SCHEDULER_CLI 0x1208 110 #define BT_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV 0x1300 111 #define BT_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV 0x1301 112 #define BT_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI 0x1302 113 #define BT_MESH_MODEL_ID_LIGHT_CTL_SRV 0x1303 114 #define BT_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV 0x1304 115 #define BT_MESH_MODEL_ID_LIGHT_CTL_CLI 0x1305 116 #define BT_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV 0x1306 117 #define BT_MESH_MODEL_ID_LIGHT_HSL_SRV 0x1307 118 #define BT_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV 0x1308 119 #define BT_MESH_MODEL_ID_LIGHT_HSL_CLI 0x1309 120 #define BT_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV 0x130a 121 #define BT_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV 0x130b 122 #define BT_MESH_MODEL_ID_LIGHT_XYL_SRV 0x130c 123 #define BT_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV 0x130d 124 #define BT_MESH_MODEL_ID_LIGHT_XYL_CLI 0x130e 125 #define BT_MESH_MODEL_ID_LIGHT_LC_SRV 0x130f 126 #define BT_MESH_MODEL_ID_LIGHT_LC_SETUPSRV 0x1310 127 #define BT_MESH_MODEL_ID_LIGHT_LC_CLI 0x1311 128 129 /** Message sending context. */ 130 struct bt_mesh_msg_ctx { 131 /** NetKey Index of the subnet to send the message on. */ 132 u16_t net_idx; 133 134 /** AppKey Index to encrypt the message with. */ 135 u16_t app_idx; 136 137 /** Remote address. */ 138 u16_t addr; 139 140 /** Destination address of a received message. Not used for sending. */ 141 u16_t recv_dst; 142 143 /** Received TTL value. Not used for sending. */ 144 u8_t recv_ttl:7; 145 146 /** Force sending reliably by using segment acknowledgement */ 147 u8_t send_rel:1; 148 149 /** TTL, or BT_MESH_TTL_DEFAULT for default TTL. */ 150 u8_t send_ttl; 151 }; 152 153 struct bt_mesh_model_op { 154 /* OpCode encoded using the BT_MESH_MODEL_OP_* macros */ 155 bt_u32_t opcode; 156 157 /* Minimum required message length */ 158 size_t min_len; 159 160 /* Message handler for the opcode */ 161 void (*func)(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf); 162 163 void (*func2)(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf, 164 bt_u32_t opcode); 165 }; 166 167 #define BT_MESH_MODEL_OP_1(b0) (b0) 168 #define BT_MESH_MODEL_OP_2(b0, b1) (((b0) << 8) | (b1)) 169 #define BT_MESH_MODEL_OP_3(b0, cid) ((((b0) << 16) | 0xc00000) | (cid)) 170 171 #define BT_MESH_MODEL_OP_END \ 172 { \ 173 0, 0, NULL \ 174 } 175 #define BT_MESH_MODEL_NO_OPS ((struct bt_mesh_model_op[]){ BT_MESH_MODEL_OP_END }) 176 177 /** Helper to define an empty model array */ 178 #define BT_MESH_MODEL_NONE ((struct bt_mesh_model[]){}) 179 180 #define BT_MESH_MODEL(_id, _op, _pub, _user_data) \ 181 { \ 182 .id = (_id), .op = _op, .keys = { [0 ...(CONFIG_BT_MESH_MODEL_KEY_COUNT - 1)] = BT_MESH_KEY_UNUSED }, \ 183 .pub = _pub, .groups = { [0 ...(CONFIG_BT_MESH_MODEL_GROUP_COUNT - 1)] = BT_MESH_ADDR_UNASSIGNED }, \ 184 .user_data = _user_data, \ 185 } 186 187 #define BT_MESH_MODEL_VND(_company, _id, _op, _pub, _user_data) \ 188 { \ 189 .vnd.company = (_company), .vnd.id = (_id), .op = _op, .pub = _pub, \ 190 .keys = { [0 ...(CONFIG_BT_MESH_MODEL_KEY_COUNT - 1)] = BT_MESH_KEY_UNUSED }, \ 191 .groups = { [0 ...(CONFIG_BT_MESH_MODEL_GROUP_COUNT - 1)] = BT_MESH_ADDR_UNASSIGNED }, \ 192 .user_data = _user_data, \ 193 } 194 195 /** @def BT_MESH_TRANSMIT 196 * 197 * @brief Encode transmission count & interval steps. 198 * 199 * @param count Number of retransmissions (first transmission is excluded). 200 * @param int_ms Interval steps in milliseconds. Must be greater than 0, 201 * less than or equal to 320, and a multiple of 10. 202 * 203 * @return Mesh transmit value that can be used e.g. for the default 204 * values of the configuration model data. 205 */ 206 #define BT_MESH_TRANSMIT(count, int_ms) ((count) | (((int_ms / 10) - 1) << 3)) 207 208 /** @def BT_MESH_TRANSMIT_COUNT 209 * 210 * @brief Decode transmit count from a transmit value. 211 * 212 * @param transmit Encoded transmit count & interval value. 213 * 214 * @return Transmission count (actual transmissions is N + 1). 215 */ 216 #define BT_MESH_TRANSMIT_COUNT(transmit) (((transmit) & (u8_t)BIT_MASK(3))) 217 218 /** @def BT_MESH_TRANSMIT_INT 219 * 220 * @brief Decode transmit interval from a transmit value. 221 * 222 * @param transmit Encoded transmit count & interval value. 223 * 224 * @return Transmission interval in milliseconds. 225 */ 226 #define BT_MESH_TRANSMIT_INT(transmit) ((((transmit) >> 3) + 1) * 10) 227 228 /** @def BT_MESH_PUB_TRANSMIT 229 * 230 * @brief Encode Publish Retransmit count & interval steps. 231 * 232 * @param count Number of retransmissions (first transmission is excluded). 233 * @param int_ms Interval steps in milliseconds. Must be greater than 0 234 * and a multiple of 50. 235 * 236 * @return Mesh transmit value that can be used e.g. for the default 237 * values of the configuration model data. 238 */ 239 #define BT_MESH_PUB_TRANSMIT(count, int_ms) BT_MESH_TRANSMIT(count, (int_ms) / 5) 240 241 /** @def BT_MESH_PUB_TRANSMIT_COUNT 242 * 243 * @brief Decode Publish Retransmit count from a given value. 244 * 245 * @param transmit Encoded Publish Retransmit count & interval value. 246 * 247 * @return Retransmission count (actual transmissions is N + 1). 248 */ 249 #define BT_MESH_PUB_TRANSMIT_COUNT(transmit) BT_MESH_TRANSMIT_COUNT(transmit) 250 251 /** @def BT_MESH_PUB_TRANSMIT_INT 252 * 253 * @brief Decode Publish Retransmit interval from a given value. 254 * 255 * @param transmit Encoded Publish Retransmit count & interval value. 256 * 257 * @return Transmission interval in milliseconds. 258 */ 259 #define BT_MESH_PUB_TRANSMIT_INT(transmit) ((((transmit) >> 3) + 1) * 50) 260 261 /** Model publication context. 262 * 263 * The context should primarily be created using the 264 * BT_MESH_MODEL_PUB_DEFINE macro. 265 */ 266 struct bt_mesh_model_pub { 267 /** The model the context belongs to. Initialized by the stack. */ 268 struct bt_mesh_model *mod; 269 270 u16_t addr; /**< Publish Address. */ 271 u16_t key; /**< Publish AppKey Index. */ 272 273 u8_t ttl; /**< Publish Time to Live. */ 274 u8_t retransmit; /**< Retransmit Count & Interval Steps. */ 275 u8_t period; /**< Publish Period. */ 276 u8_t period_div:4, /**< Divisor for the Period. */ 277 cred:1, /**< Friendship Credentials Flag. */ 278 fast_period:1, /**< Use FastPeriodDivisor */ 279 count:3; /**< Retransmissions left. */ 280 281 bt_u32_t period_start; /**< Start of the current period. */ 282 283 /** @brief Publication buffer, containing the publication message. 284 * 285 * This will get correctly created when the publication context 286 * has been defined using the BT_MESH_MODEL_PUB_DEFINE macro. 287 * 288 * BT_MESH_MODEL_PUB_DEFINE(name, update, size); 289 */ 290 struct net_buf_simple *msg; 291 292 /** @brief Callback for updating the publication buffer. 293 * 294 * When set to NULL, the model is assumed not to support 295 * periodic publishing. When set to non-NULL the callback 296 * will be called periodically and is expected to update 297 * @ref bt_mesh_model_pub.msg with a valid publication 298 * message. 299 * 300 * @param mod The Model the Publication Context belogs to. 301 * 302 * @return Zero on success or (negative) error code otherwise. 303 */ 304 int (*update)(struct bt_mesh_model *mod); 305 306 /** Publish Period Timer. Only for stack-internal use. */ 307 struct k_delayed_work timer; 308 }; 309 310 /** @def BT_MESH_MODEL_PUB_DEFINE 311 * 312 * Define a model publication context. 313 * 314 * @param _name Variable name given to the context. 315 * @param _update Optional message update callback (may be NULL). 316 * @param _msg_len Length of the publication message. 317 */ 318 #define BT_MESH_MODEL_PUB_DEFINE(_name, _update, _msg_len) \ 319 NET_BUF_SIMPLE_DEFINE_STATIC(bt_mesh_pub_msg_##_name, _msg_len); \ 320 static struct bt_mesh_model_pub _name = { \ 321 .update = _update, \ 322 .msg = &bt_mesh_pub_msg_##_name, \ 323 } 324 325 /** Abstraction that describes a Mesh Model instance */ 326 struct bt_mesh_model { 327 union { 328 u16_t id; 329 struct { 330 u16_t company; 331 u16_t id; 332 } vnd; 333 }; 334 335 /* Internal information, mainly for persistent storage */ 336 u8_t elem_idx; /* Belongs to Nth element */ 337 u8_t mod_idx; /* Is the Nth model in the element */ 338 u16_t flags; /* Information about what has changed */ 339 340 /* Model Publication */ 341 struct bt_mesh_model_pub *pub; 342 343 /* AppKey List */ 344 u16_t keys[CONFIG_BT_MESH_MODEL_KEY_COUNT]; 345 346 /* Subscription List (group or virtual addresses) */ 347 u16_t groups[CONFIG_BT_MESH_MODEL_GROUP_COUNT]; 348 349 const struct bt_mesh_model_op *op; 350 351 /* Model-specific user data */ 352 void *user_data; 353 }; 354 355 struct bt_mesh_send_cb { 356 void (*start)(u16_t duration, int err, void *cb_data); 357 void (*end)(int err, void *cb_data); 358 }; 359 360 void bt_mesh_model_msg_init(struct net_buf_simple *msg, bt_u32_t opcode); 361 362 /** Special TTL value to request using configured default TTL */ 363 #define BT_MESH_TTL_DEFAULT 0xff 364 365 /** Maximum allowed TTL value */ 366 #define BT_MESH_TTL_MAX 0x7f 367 368 /** 369 * @brief Send an Access Layer message. 370 * 371 * @param model Mesh (client) Model that the message belongs to. 372 * @param ctx Message context, includes keys, TTL, etc. 373 * @param msg Access Layer payload (the actual message to be sent). 374 * @param cb Optional "message sent" callback. 375 * @param cb_data User data to be passed to the callback. 376 * 377 * @return 0 on success, or (negative) error code on failure. 378 */ 379 int bt_mesh_model_send(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *msg, 380 const struct bt_mesh_send_cb *cb, void *cb_data); 381 382 /** 383 * @brief Send a model publication message. 384 * 385 * Before calling this function, the user needs to ensure that the model 386 * publication message (@ref bt_mesh_model_pub.msg) contains a valid 387 * message to be sent. Note that this API is only to be used for 388 * non-period publishing. For periodic publishing the app only needs 389 * to make sure that @ref bt_mesh_model_pub.msg contains a valid message 390 * whenever the @ref bt_mesh_model_pub.update callback is called. 391 * 392 * @param model Mesh (client) Model that's publishing the message. 393 * 394 * @return 0 on success, or (negative) error code on failure. 395 */ 396 int bt_mesh_model_publish(struct bt_mesh_model *model); 397 398 /** 399 * @brief Get the element that a model belongs to. 400 * 401 * @param mod Mesh model. 402 * 403 * @return Pointer to the element that the given model belongs to. 404 */ 405 struct bt_mesh_elem *bt_mesh_model_elem(struct bt_mesh_model *mod); 406 407 struct bt_mesh_model *bt_mesh_model_find_vnd(struct bt_mesh_elem *elem, u16_t company, u16_t id); 408 409 u16_t bt_mesh_model_get_netkey_id(struct bt_mesh_elem *elem); 410 411 u16_t bt_mesh_model_get_appkey_id(struct bt_mesh_elem *elem, struct bt_mesh_model *p_model); 412 413 /* Find local element based on element id */ 414 struct bt_mesh_elem *bt_mesh_elem_find_by_id(u8_t id); 415 416 /** Node Composition */ 417 struct bt_mesh_comp { 418 u16_t cid; 419 u16_t pid; 420 u16_t vid; 421 422 size_t elem_count; 423 struct bt_mesh_elem *elem; 424 }; 425 426 #ifdef __cplusplus 427 } 428 #endif 429 430 /** 431 * @} 432 */ 433 434 #endif /* __BT_MESH_ACCESS_H */ 435