1 /*  Bluetooth Mesh */
2 
3 /*
4  * Copyright (c) 2017 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <ble_os.h>
10 #include <bt_errno.h>
11 #include <misc/util.h>
12 #include <misc/byteorder.h>
13 
14 #include <net/buf.h>
15 #include <bluetooth/bluetooth.h>
16 #include <api/mesh.h>
17 
18 #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_MESH_DEBUG_ACCESS)
19 #include "common/log.h"
20 
21 #include "mesh.h"
22 #include "adv.h"
23 #include "net.h"
24 #include "lpn.h"
25 #include "ble_transport.h"
26 #include "access.h"
27 #include "foundation.h"
28 
29 #ifdef CONFIG_BT_MESH_PROVISIONER
30 #include "provisioner_prov.h"
31 #include "provisioner_main.h"
32 #include "provisioner_proxy.h"
33 #endif
34 
35 #define MESH_TX_TAG "\t[TX]"
36 #define MESH_RX_TAG "\t[RX]"
37 #define MESH_TX_D(f, ...) LOGI(MESH_TX_TAG, "\033[0;34m " f "\033[0m", ##__VA_ARGS__)
38 #define MESH_RX_D(f, ...) LOGI(MESH_RX_TAG, "\033[0;32m " f "\033[0m", ##__VA_ARGS__)
39 
40 static const struct bt_mesh_comp *dev_comp = NULL;
41 static u16_t dev_primary_addr;
42 
43 static const struct {
44     const u16_t id;
45     int (*const init)(struct bt_mesh_model *model, bool primary);
46 } model_init[] = {
47     { BT_MESH_MODEL_ID_CFG_SRV, bt_mesh_cfg_srv_init },
48     { BT_MESH_MODEL_ID_HEALTH_SRV, bt_mesh_health_srv_init },
49 #if defined(CONFIG_BT_MESH_CFG_CLI)
50     { BT_MESH_MODEL_ID_CFG_CLI, bt_mesh_cfg_cli_init },
51 #endif
52 #if defined(CONFIG_BT_MESH_HEALTH_CLI)
53     { BT_MESH_MODEL_ID_HEALTH_CLI, bt_mesh_health_cli_init },
54 #endif
55 };
56 
bt_mesh_model_foreach(void (* func)(struct bt_mesh_model * mod,struct bt_mesh_elem * elem,bool vnd,bool primary,void * user_data),void * user_data)57 void bt_mesh_model_foreach(void (*func)(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, bool vnd, bool primary,
58                                         void *user_data),
59                            void *user_data)
60 {
61     int i, j;
62 
63     for (i = 0; i < dev_comp->elem_count; i++) {
64         struct bt_mesh_elem *elem = &dev_comp->elem[i];
65 
66         for (j = 0; j < elem->model_count; j++) {
67             struct bt_mesh_model *model = &elem->models[j];
68 
69             func(model, elem, false, i == 0, user_data);
70         }
71 
72         for (j = 0; j < elem->vnd_model_count; j++) {
73             struct bt_mesh_model *model = &elem->vnd_models[j];
74 
75             func(model, elem, true, i == 0, user_data);
76         }
77     }
78 }
79 
bt_mesh_model_pub_period_get(struct bt_mesh_model * mod)80 bt_s32_t bt_mesh_model_pub_period_get(struct bt_mesh_model *mod)
81 {
82     int period;
83 
84     if (!mod->pub) {
85         return 0;
86     }
87 
88     switch (mod->pub->period >> 6) {
89     case 0x00:
90         /* 1 step is 100 ms */
91         period = K_MSEC((mod->pub->period & BIT_MASK(6)) * 100);
92         break;
93     case 0x01:
94         /* 1 step is 1 second */
95         period = K_SECONDS(mod->pub->period & BIT_MASK(6));
96         break;
97     case 0x02:
98         /* 1 step is 10 seconds */
99         period = K_SECONDS((mod->pub->period & BIT_MASK(6)) * 10);
100         break;
101     case 0x03:
102         /* 1 step is 10 minutes */
103         period = K_MINUTES((mod->pub->period & BIT_MASK(6)) * 10);
104         break;
105     default:
106         // CODE_UNREACHABLE;
107         while (1)
108             ;
109     }
110 
111     if (mod->pub->fast_period) {
112         return period >> mod->pub->period_div;
113     } else {
114         return period;
115     }
116 }
117 
next_period(struct bt_mesh_model * mod)118 static bt_s32_t next_period(struct bt_mesh_model *mod)
119 {
120     struct bt_mesh_model_pub *pub = mod->pub;
121     bt_u32_t elapsed, period;
122 
123     period = bt_mesh_model_pub_period_get(mod);
124     if (!period) {
125         return 0;
126     }
127 
128     elapsed = k_uptime_get_32() - pub->period_start;
129 
130     BT_DBG("Publishing took %ums", elapsed);
131 
132     if (elapsed > period) {
133         BT_WARN("Publication sending took longer than the period");
134         /* Return smallest positive number since 0 means disabled */
135         return K_MSEC(1);
136     }
137 
138     return period - elapsed;
139 }
140 
publish_sent(int err,void * user_data)141 static void publish_sent(int err, void *user_data)
142 {
143     struct bt_mesh_model *mod = user_data;
144     bt_s32_t delay;
145 
146     BT_DBG("err %d", err);
147     if (mod->pub->count) {
148         delay = BT_MESH_PUB_TRANSMIT_INT(mod->pub->retransmit);
149     } else {
150         delay = next_period(mod);
151     }
152 
153     if (delay) {
154         BT_DBG("Publishing next time in %dms", delay);
155         k_delayed_work_submit(&mod->pub->timer, delay);
156     }
157 }
158 
159 static const struct bt_mesh_send_cb pub_sent_cb = {
160     .end = publish_sent,
161 };
162 
publish_retransmit(struct bt_mesh_model * mod)163 static int publish_retransmit(struct bt_mesh_model *mod)
164 {
165     NET_BUF_SIMPLE_DEFINE(sdu, BT_MESH_TX_SDU_MAX);
166     struct bt_mesh_model_pub *pub = mod->pub;
167     struct bt_mesh_app_key *key;
168     struct bt_mesh_msg_ctx ctx = {
169         .addr = pub->addr,
170         .send_ttl = pub->ttl,
171     };
172     int err;
173     struct bt_mesh_net_tx tx = {
174         .ctx = &ctx,
175         .src = bt_mesh_model_elem(mod)->addr,
176         .xmit = bt_mesh_net_transmit_get(),
177         .friend_cred = pub->cred,
178     };
179 
180     key = bt_mesh_app_key_find(pub->key);
181     if (!key) {
182         return -EADDRNOTAVAIL;
183     }
184 
185     tx.sub = bt_mesh_subnet_get(key->net_idx);
186     if (!tx.sub) {
187         BT_ERR("No available subnet found");
188         return -EINVAL;
189     }
190 
191     ctx.net_idx = key->net_idx;
192     ctx.app_idx = key->app_idx;
193 
194     net_buf_simple_add_mem(&sdu, pub->msg->data, pub->msg->len);
195     err = bt_mesh_trans_send(&tx, &sdu, &pub_sent_cb, mod);
196     pub->count--;
197     return err;
198 }
199 
mod_publish(struct k_work * work)200 static void mod_publish(struct k_work *work)
201 {
202     struct bt_mesh_model_pub *pub = CONTAINER_OF(work, struct bt_mesh_model_pub, timer.work);
203     bt_s32_t period_ms;
204     int err;
205     uint8_t pub_count = BT_MESH_PUB_TRANSMIT_COUNT(pub->retransmit);
206 
207     BT_DBG("");
208 
209     period_ms = bt_mesh_model_pub_period_get(pub->mod);
210 
211     if (!period_ms) {
212         return;
213     }
214 
215     BT_DBG("period %u ms", period_ms);
216     __ASSERT_NO_MSG(pub->update != NULL);
217 
218     if (pub->count) {
219         if (pub->count == pub_count) {
220             pub->period_start = k_uptime_get_32();
221             err = pub->update(pub->mod);
222             if (err) {
223                 BT_ERR("Failed to update publication message");
224                 return;
225             }
226         }
227         err = publish_retransmit(pub->mod);
228         if (err) {
229             BT_ERR("Failed to retransmit (err %d)", err);
230 
231             pub->count = pub_count;
232 
233             /* Continue with normal publication */
234             if (period_ms) {
235                 k_delayed_work_submit(&pub->timer, period_ms);
236             }
237         }
238 
239         return;
240     }
241 
242     if (pub_count == 0) {
243         pub->period_start = k_uptime_get_32();
244         err = pub->update(pub->mod);
245         if (err) {
246             BT_ERR("Failed to update publication message");
247             return;
248         }
249     }
250 
251     err = bt_mesh_model_publish(pub->mod);
252     if (err) {
253         BT_ERR("Publishing failed (err %d)", err);
254     }
255 }
256 
bt_mesh_model_elem(struct bt_mesh_model * mod)257 struct bt_mesh_elem *bt_mesh_model_elem(struct bt_mesh_model *mod)
258 {
259     if (mod == NULL || dev_comp == NULL || mod->elem_idx >= dev_comp->elem_count) {
260         return NULL;
261     }
262 
263     return &dev_comp->elem[mod->elem_idx];
264 }
265 
bt_mesh_model_get(bool vnd,u8_t elem_idx,u8_t mod_idx)266 struct bt_mesh_model *bt_mesh_model_get(bool vnd, u8_t elem_idx, u8_t mod_idx)
267 {
268     struct bt_mesh_elem *elem;
269 
270     if (dev_comp == NULL || elem_idx >= dev_comp->elem_count) {
271         BT_ERR("Invalid element index %u", elem_idx);
272         return NULL;
273     }
274 
275     elem = &dev_comp->elem[elem_idx];
276 
277     if (vnd) {
278         if (mod_idx >= elem->vnd_model_count) {
279             BT_ERR("Invalid vendor model index %u", mod_idx);
280             return NULL;
281         }
282 
283         return &elem->vnd_models[mod_idx];
284     } else {
285         if (mod_idx >= elem->model_count) {
286             BT_ERR("Invalid SIG model index %u", mod_idx);
287             return NULL;
288         }
289 
290         return &elem->models[mod_idx];
291     }
292 }
293 
mod_init(struct bt_mesh_model * mod,struct bt_mesh_elem * elem,bool vnd,bool primary,void * user_data)294 static void mod_init(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, bool vnd, bool primary, void *user_data)
295 {
296     int i;
297 
298     if (mod->pub) {
299         mod->pub->mod = mod;
300         k_delayed_work_init(&mod->pub->timer, mod_publish);
301     }
302 
303     for (i = 0; i < ARRAY_SIZE(mod->keys); i++) {
304         mod->keys[i] = BT_MESH_KEY_UNUSED;
305     }
306 
307     mod->elem_idx = elem - dev_comp->elem;
308     if (vnd) {
309         mod->mod_idx = mod - elem->vnd_models;
310     } else {
311         mod->mod_idx = mod - elem->models;
312     }
313 
314     if (vnd) {
315         return;
316     }
317 
318     for (i = 0; i < ARRAY_SIZE(model_init); i++) {
319         if (model_init[i].id == mod->id) {
320             model_init[i].init(mod, primary);
321         }
322     }
323 }
324 
bt_mesh_comp_register(const struct bt_mesh_comp * comp)325 int bt_mesh_comp_register(const struct bt_mesh_comp *comp)
326 {
327     /* There must be at least one element */
328     if (!comp->elem_count) {
329         return -EINVAL;
330     }
331 
332     dev_comp = comp;
333 
334     bt_mesh_model_foreach(mod_init, NULL);
335 
336     return 0;
337 }
338 
bt_mesh_comp_provision(u16_t addr)339 void bt_mesh_comp_provision(u16_t addr)
340 {
341     int i;
342 
343     if (dev_comp == NULL) {
344         BT_ERR("dev_comp is NULL");
345         return;
346     }
347 
348     dev_primary_addr = addr;
349 
350     BT_DBG("addr 0x%04x elem_count %zu", addr, dev_comp->elem_count);
351 
352     for (i = 0; i < dev_comp->elem_count; i++) {
353         struct bt_mesh_elem *elem = &dev_comp->elem[i];
354 
355         elem->addr = addr++;
356 
357         BT_DBG("addr 0x%04x mod_count %u vnd_mod_count %u", elem->addr, elem->model_count, elem->vnd_model_count);
358     }
359 }
360 
bt_mesh_comp_unprovision(void)361 void bt_mesh_comp_unprovision(void)
362 {
363     BT_DBG("");
364 
365     dev_primary_addr = BT_MESH_ADDR_UNASSIGNED;
366 
367     bt_mesh_model_foreach(mod_init, NULL);
368 }
369 
bt_mesh_primary_addr(void)370 u16_t bt_mesh_primary_addr(void)
371 {
372     return dev_primary_addr;
373 }
374 
bt_mesh_model_find_group(struct bt_mesh_model * mod,u16_t addr)375 u16_t *bt_mesh_model_find_group(struct bt_mesh_model *mod, u16_t addr)
376 {
377     int i;
378     struct bt_mesh_elem *elem = NULL;
379 
380     for (i = 0; i < ARRAY_SIZE(mod->groups); i++) {
381         if (mod->groups[i] == addr) {
382             BT_INFO("model group match");
383             return &mod->groups[i];
384         }
385     }
386 
387     /*[Genie begin] add by lgy at 2021-02-09*/
388     elem = bt_mesh_model_elem(mod);
389     if (elem && addr == elem->grop_addr) {
390         BT_INFO("elem group match");
391         return &elem->grop_addr;
392     }
393 
394     for (i = 0; i < CONFIG_BT_MESH_MODEL_GROUP_COUNT; i++) {
395         if (g_sub_list[i] == addr) {
396             BT_INFO("global group match");
397             return &g_sub_list[i];
398         }
399     }
400 
401     // BT_WARN("addr:0x%04x no match", addr);
402     /*[Genie begin] add by lgy at 2021-02-09*/
403 
404     return NULL;
405 }
406 
bt_mesh_elem_find_group(struct bt_mesh_elem * elem,u16_t group_addr)407 static struct bt_mesh_model *bt_mesh_elem_find_group(struct bt_mesh_elem *elem, u16_t group_addr)
408 {
409     struct bt_mesh_model *model;
410     u16_t *match;
411     int i;
412 
413     for (i = 0; i < elem->model_count; i++) {
414         model = &elem->models[i];
415 
416         match = bt_mesh_model_find_group(model, group_addr);
417         if (match) {
418             return model;
419         }
420     }
421 
422     for (i = 0; i < elem->vnd_model_count; i++) {
423         model = &elem->vnd_models[i];
424 
425         match = bt_mesh_model_find_group(model, group_addr);
426         if (match) {
427             return model;
428         }
429     }
430 
431     return NULL;
432 }
433 
bt_mesh_elem_find(u16_t addr)434 struct bt_mesh_elem *bt_mesh_elem_find(u16_t addr)
435 {
436     int i;
437 
438     for (i = 0; i < dev_comp->elem_count; i++) {
439         struct bt_mesh_elem *elem = &dev_comp->elem[i];
440 
441         if (BT_MESH_ADDR_IS_GROUP(addr) || BT_MESH_ADDR_IS_VIRTUAL(addr)) {
442             if (bt_mesh_elem_find_group(elem, addr)) {
443                 return elem;
444             }
445         } else if (elem->addr == addr) {
446             return elem;
447         }
448     }
449 
450     return NULL;
451 }
452 
bt_mesh_elem_find_by_id(u8_t id)453 struct bt_mesh_elem *bt_mesh_elem_find_by_id(u8_t id)
454 {
455     if (id < dev_comp->elem_count) {
456         return &dev_comp->elem[id];
457     }
458 
459     return NULL;
460 }
461 
bt_mesh_elem_count(void)462 u8_t bt_mesh_elem_count(void)
463 {
464     return dev_comp->elem_count;
465 }
466 
model_has_key(struct bt_mesh_model * mod,u16_t key)467 static bool model_has_key(struct bt_mesh_model *mod, u16_t key)
468 {
469     int i;
470 
471     for (i = 0; i < ARRAY_SIZE(mod->keys); i++) {
472         if (mod->keys[i] == key) {
473             return true;
474         }
475     }
476 
477     return false;
478 }
479 
find_op(struct bt_mesh_model * models,u8_t model_count,u16_t dst,u16_t app_idx,bt_u32_t opcode,struct bt_mesh_model ** model)480 static const struct bt_mesh_model_op *find_op(struct bt_mesh_model *models, u8_t model_count, u16_t dst, u16_t app_idx,
481                                               bt_u32_t opcode, struct bt_mesh_model **model)
482 {
483     u8_t i;
484 
485     for (i = 0; i < model_count; i++) {
486         const struct bt_mesh_model_op *op;
487 
488         *model = &models[i];
489 
490         if (BT_MESH_ADDR_IS_GROUP(dst) || BT_MESH_ADDR_IS_VIRTUAL(dst)) {
491             if (!bt_mesh_model_find_group(*model, dst)) {
492                 continue;
493             }
494         }
495 
496         if (!model_has_key(*model, app_idx)) {
497             continue;
498         }
499 
500         for (op = (*model)->op; op->func; op++) {
501             if (op->opcode == opcode) {
502                 return op;
503             }
504         }
505     }
506 
507     *model = NULL;
508     return NULL;
509 }
510 
get_opcode(struct net_buf_simple * buf,bt_u32_t * opcode)511 static int get_opcode(struct net_buf_simple *buf, bt_u32_t *opcode)
512 {
513     switch (buf->data[0] >> 6) {
514     case 0x00:
515     case 0x01:
516         if (buf->data[0] == 0x7f) {
517             BT_ERR("Ignoring RFU OpCode");
518             return -EINVAL;
519         }
520 
521         *opcode = net_buf_simple_pull_u8(buf);
522         return 0;
523     case 0x02:
524         if (buf->len < 2) {
525             BT_ERR("Too short payload for 2-octet OpCode");
526             return -EINVAL;
527         }
528 
529         *opcode = net_buf_simple_pull_be16(buf);
530         return 0;
531     case 0x03:
532         if (buf->len < 3) {
533             BT_ERR("Too short payload for 3-octet OpCode");
534             return -EINVAL;
535         }
536 
537         *opcode = net_buf_simple_pull_u8(buf) << 16;
538         *opcode |= net_buf_simple_pull_le16(buf);
539         return 0;
540     }
541 
542     // CODE_UNREACHABLE;
543     while (1)
544         ;
545 }
546 
bt_mesh_fixed_group_match(u16_t addr)547 bool bt_mesh_fixed_group_match(u16_t addr)
548 {
549     /* Check for fixed group addresses */
550     switch (addr) {
551     case BT_MESH_ADDR_ALL_NODES:
552 #ifdef CONFIG_GENIE_MESH_ENABLE
553     /*[Genie begin] add by lgy at 2020-09-10*/
554     case BT_MESH_ADDR_GENIE_ALL_NODES:
555         /*[Genie end] add by lgy at 2020-09-10*/
556 #endif
557         return true;
558     case BT_MESH_ADDR_PROXIES:
559         /* TODO: Proxy not yet supported */
560         return false;
561     case BT_MESH_ADDR_FRIENDS:
562         return (bt_mesh_friend_get() == BT_MESH_FRIEND_ENABLED);
563     case BT_MESH_ADDR_RELAYS:
564         return (bt_mesh_relay_get() == BT_MESH_RELAY_ENABLED);
565     default:
566         return false;
567     }
568 }
569 
bt_mesh_model_recv(struct bt_mesh_net_rx * rx,struct net_buf_simple * buf)570 void bt_mesh_model_recv(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf)
571 {
572     struct bt_mesh_model *models = NULL, *model = NULL;
573     const struct bt_mesh_model_op *op;
574     bt_u32_t opcode;
575     u8_t count;
576     int i;
577 
578     BT_DBG("app_idx 0x%04x src 0x%04x dst 0x%04x", rx->ctx.app_idx, rx->ctx.addr, rx->ctx.recv_dst);
579     BT_DBG("len %u: %s", buf->len, bt_hex(buf->data, buf->len));
580 
581     if (get_opcode(buf, &opcode) < 0) {
582         BT_WARN("Unable to decode OpCode");
583         return;
584     }
585 
586     BT_DBG("OpCode 0x%08x", opcode);
587 
588 #ifdef MESH_DEBUG_RX
589     MESH_RX_D("RSSI: %d\n", rx->rssi);
590     MESH_RX_D("TTL: %d\n", rx->ctx.recv_ttl);
591     MESH_RX_D("SRC: 0x%02X\n", rx->ctx.addr);
592     MESH_RX_D("DST: 0x%02X\n", rx->ctx.recv_dst);
593     MESH_RX_D("OPCODE: 0x%04X\n", opcode);
594     MESH_RX_D("Payload size: %d\n", buf->len);
595     MESH_RX_D("%s\n", bt_hex_real(buf->data, buf->len));
596 #endif
597 
598     for (i = 0; i < dev_comp->elem_count; i++) {
599         struct bt_mesh_elem *elem = &dev_comp->elem[i];
600 
601         if (BT_MESH_ADDR_IS_UNICAST(rx->ctx.recv_dst)) {
602             if (elem->addr != rx->ctx.recv_dst) {
603                 continue;
604             }
605         } else if (BT_MESH_ADDR_IS_GROUP(rx->ctx.recv_dst) || BT_MESH_ADDR_IS_VIRTUAL(rx->ctx.recv_dst)) {
606             /* find_op() will do proper model/group matching */
607         } else if (i != 0 || !bt_mesh_fixed_group_match(rx->ctx.recv_dst)) {
608             continue;
609         }
610 
611         /* SIG models cannot contain 3-byte (vendor) OpCodes, and
612 		 * vendor models cannot contain SIG (1- or 2-byte) OpCodes, so
613 		 * we only need to do the lookup in one of the model lists.
614 		 */
615         if (opcode < 0x10000) {
616             models = elem->models;
617             count = elem->model_count;
618         } else {
619             models = elem->vnd_models;
620             count = elem->vnd_model_count;
621         }
622 
623         op = find_op(models, count, rx->ctx.recv_dst, rx->ctx.app_idx, opcode, &model);
624         if (op) {
625             struct net_buf_simple_state state;
626 
627             if (buf->len < op->min_len) {
628                 BT_ERR("Too short message for OpCode 0x%08x", opcode);
629                 continue;
630             }
631 
632             /* The callback will likely parse the buffer, so
633 			 * store the parsing state in case multiple models
634 			 * receive the message.
635 			 */
636             net_buf_simple_save(buf, &state);
637             if (op->func2) {
638                 op->func2(model, &rx->ctx, buf, opcode);
639             } else {
640                 op->func(model, &rx->ctx, buf);
641             }
642             net_buf_simple_restore(buf, &state);
643 
644         } else {
645             BT_DBG("No OpCode 0x%08x for elem %d", opcode, i);
646         }
647     }
648 }
649 
bt_mesh_model_msg_init(struct net_buf_simple * msg,bt_u32_t opcode)650 void bt_mesh_model_msg_init(struct net_buf_simple *msg, bt_u32_t opcode)
651 {
652     net_buf_simple_init(msg, 0);
653 
654     if (opcode < 0x100) {
655         /* 1-byte OpCode */
656         net_buf_simple_add_u8(msg, opcode);
657         return;
658     }
659 
660     if (opcode < 0x10000) {
661         /* 2-byte OpCode */
662         net_buf_simple_add_be16(msg, opcode);
663         return;
664     }
665 
666     /* 3-byte OpCode */
667     net_buf_simple_add_u8(msg, ((opcode >> 16) & 0xff));
668     net_buf_simple_add_le16(msg, opcode & 0xffff);
669 }
670 
model_send(struct bt_mesh_model * model,struct bt_mesh_net_tx * tx,bool implicit_bind,struct net_buf_simple * msg,const struct bt_mesh_send_cb * cb,void * cb_data)671 static int model_send(struct bt_mesh_model *model, struct bt_mesh_net_tx *tx, bool implicit_bind,
672                       struct net_buf_simple *msg, const struct bt_mesh_send_cb *cb, void *cb_data)
673 {
674     if (model == NULL || tx == NULL || msg == NULL) {
675         return -EINVAL;
676     }
677     BT_DBG("net_idx 0x%04x app_idx 0x%04x dst 0x%04x", tx->ctx->net_idx, tx->ctx->app_idx, tx->ctx->addr);
678     BT_DBG("len %u: %s", msg->len, bt_hex(msg->data, msg->len));
679 
680     if (!bt_mesh_is_provisioned()) {
681         BT_ERR("Local node is not yet provisioned");
682         return -EAGAIN;
683     }
684 
685     if (net_buf_simple_tailroom(msg) < 4) {
686         BT_ERR("Not enough tailroom for TransMIC");
687         return -EINVAL;
688     }
689 
690     if (msg->len > BT_MESH_TX_SDU_MAX - 4) {
691         BT_ERR("Too big message");
692         return -EMSGSIZE;
693     }
694 
695     if (!implicit_bind && !model_has_key(model, tx->ctx->app_idx)) {
696         BT_ERR("Model not bound to AppKey 0x%04x", tx->ctx->app_idx);
697         return -EINVAL;
698     }
699 
700 #ifdef MESH_DEBUG_TX
701     MESH_TX_D("TTL: %d\n", tx->ctx->send_ttl);
702     MESH_TX_D("SRC: 0x%02X\n", bt_mesh_model_elem(model)->addr);
703     MESH_TX_D("DST: 0x%02X\n", tx->ctx->addr);
704     MESH_TX_D("msg size: %d\n", msg->len);
705     MESH_TX_D("%s\n", bt_hex_real(msg->data, msg->len));
706 #endif
707 
708     return bt_mesh_trans_send(tx, msg, cb, cb_data);
709 }
710 
bt_mesh_model_send(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * msg,const struct bt_mesh_send_cb * cb,void * cb_data)711 int bt_mesh_model_send(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *msg,
712                        const struct bt_mesh_send_cb *cb, void *cb_data)
713 {
714     if (model == NULL || ctx == NULL || msg == NULL) {
715         return -EINVAL;
716     }
717     struct bt_mesh_net_tx tx = {
718         .sub = bt_mesh_subnet_get(ctx->net_idx),
719         .ctx = ctx,
720         .src = bt_mesh_model_elem(model)->addr,
721         .xmit = bt_mesh_net_transmit_get(),
722         .friend_cred = 0,
723     };
724 
725     if (!tx.sub) {
726         BT_ERR("No available subnet found");
727         return -EINVAL;
728     }
729 
730     return model_send(model, &tx, false, msg, cb, cb_data);
731 }
732 
bt_mesh_model_publish(struct bt_mesh_model * model)733 int bt_mesh_model_publish(struct bt_mesh_model *model)
734 {
735     if (NULL == model) {
736         return -EINVAL;
737     }
738     NET_BUF_SIMPLE_DEFINE(sdu, BT_MESH_TX_SDU_MAX);
739     struct bt_mesh_model_pub *pub = model->pub;
740     struct bt_mesh_app_key *key;
741     struct bt_mesh_msg_ctx ctx = {};
742     struct bt_mesh_net_tx tx = {
743         .ctx = &ctx,
744         .src = bt_mesh_model_elem(model)->addr,
745         .xmit = bt_mesh_net_transmit_get(),
746     };
747     int err;
748 
749     BT_DBG("");
750 
751     if (!pub) {
752         return -ENOTSUP;
753     }
754 
755     if (pub->addr == BT_MESH_ADDR_UNASSIGNED) {
756         return -EADDRNOTAVAIL;
757     }
758 
759 #ifdef CONFIG_BT_MESH_PROVISIONER
760     key = provisioner_app_key_find(pub->key);
761 #else
762     key = bt_mesh_app_key_find(pub->key);
763 #endif
764     if (!key) {
765         return -EADDRNOTAVAIL;
766     }
767 
768     if (pub->msg->len + 4 > BT_MESH_TX_SDU_MAX) {
769         BT_ERR("Message does not fit maximum SDU size");
770         return -EMSGSIZE;
771     }
772 
773     if (pub->count) {
774         BT_WARN("Clearing publish retransmit timer");
775         k_delayed_work_cancel(&pub->timer);
776     }
777 
778     net_buf_simple_add_mem(&sdu, pub->msg->data, pub->msg->len);
779 
780     ctx.addr = pub->addr;
781     ctx.send_ttl = pub->ttl;
782     ctx.net_idx = key->net_idx;
783     ctx.app_idx = key->app_idx;
784 
785     tx.friend_cred = pub->cred;
786     tx.sub = bt_mesh_subnet_get(ctx.net_idx);
787     if (!tx.sub) {
788         BT_ERR("No available subnet found");
789         return -EINVAL;
790     }
791 
792     BT_DBG("Publish Retransmit Count %u Interval %ums", pub->count, BT_MESH_PUB_TRANSMIT_INT(pub->retransmit));
793 
794     err = model_send(model, &tx, true, &sdu, &pub_sent_cb, model);
795     if (err) {
796         /* Don't try retransmissions for this publish attempt */
797         pub->count = 0U;
798         /* Make sure the publish timer gets reset */
799         publish_sent(err, model);
800         return err;
801     }
802 
803     pub->count = BT_MESH_PUB_TRANSMIT_COUNT(pub->retransmit);
804 
805     return 0;
806 }
807 
bt_mesh_model_find_vnd(struct bt_mesh_elem * elem,u16_t company,u16_t id)808 struct bt_mesh_model *bt_mesh_model_find_vnd(struct bt_mesh_elem *elem, u16_t company, u16_t id)
809 {
810     u8_t i;
811 
812     for (i = 0; i < elem->vnd_model_count; i++) {
813         if (elem->vnd_models[i].vnd.company == company && elem->vnd_models[i].vnd.id == id) {
814             return &elem->vnd_models[i];
815         }
816     }
817 
818     return NULL;
819 }
820 
bt_mesh_model_find(struct bt_mesh_elem * elem,u16_t id)821 struct bt_mesh_model *bt_mesh_model_find(struct bt_mesh_elem *elem, u16_t id)
822 {
823     u8_t i;
824 
825     for (i = 0; i < elem->model_count; i++) {
826         if (elem->models[i].id == id) {
827             return &elem->models[i];
828         }
829     }
830 
831     return NULL;
832 }
833 
bt_mesh_comp_get(void)834 const struct bt_mesh_comp *bt_mesh_comp_get(void)
835 {
836     return dev_comp;
837 }
838 
bt_mesh_model_get_netkey_id(struct bt_mesh_elem * elem)839 u16_t bt_mesh_model_get_netkey_id(struct bt_mesh_elem *elem)
840 {
841     return 0;
842 }
843 
bt_mesh_model_get_appkey_id(struct bt_mesh_elem * elem,struct bt_mesh_model * p_model)844 u16_t bt_mesh_model_get_appkey_id(struct bt_mesh_elem *elem, struct bt_mesh_model *p_model)
845 {
846     return 0;
847 }
848