1 /*
2  * Copyright (c) 2017 Intel Corporation
3  * Copyright (c) 2024 Nordic Semiconductor ASA
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <errno.h>
9 #include <stddef.h>
10 #include <stdint.h>
11 
12 #include <zephyr/bluetooth/mesh/access.h>
13 #include <zephyr/kernel.h>
14 #include <zephyr/sys/slist.h>
15 
16 #include "net.h"
17 #include "lpn.h"
18 #include "rpl.h"
19 #include "testing.h"
20 #include "transport.h"
21 
22 static sys_slist_t cb_slist;
23 
bt_mesh_test_cb_register(struct bt_mesh_test_cb * cb)24 int bt_mesh_test_cb_register(struct bt_mesh_test_cb *cb)
25 {
26 	if (sys_slist_find(&cb_slist, &cb->node, NULL)) {
27 		return -EEXIST;
28 	}
29 
30 	sys_slist_append(&cb_slist, &cb->node);
31 
32 	return 0;
33 }
34 
bt_mesh_test_cb_unregister(struct bt_mesh_test_cb * cb)35 void bt_mesh_test_cb_unregister(struct bt_mesh_test_cb *cb)
36 {
37 	sys_slist_find_and_remove(&cb_slist, &cb->node);
38 }
39 
bt_mesh_test_net_recv(uint8_t ttl,uint8_t ctl,uint16_t src,uint16_t dst,const void * payload,size_t payload_len)40 void bt_mesh_test_net_recv(uint8_t ttl, uint8_t ctl, uint16_t src, uint16_t dst,
41 			   const void *payload, size_t payload_len)
42 {
43 	struct bt_mesh_test_cb *cb;
44 
45 	SYS_SLIST_FOR_EACH_CONTAINER(&cb_slist, cb, node) {
46 		if (cb->net_recv) {
47 			cb->net_recv(ttl, ctl, src, dst, payload, payload_len);
48 		}
49 	}
50 }
51 
bt_mesh_test_model_recv(uint16_t src,uint16_t dst,const void * payload,size_t payload_len)52 void bt_mesh_test_model_recv(uint16_t src, uint16_t dst, const void *payload, size_t payload_len)
53 {
54 	struct bt_mesh_test_cb *cb;
55 
56 	SYS_SLIST_FOR_EACH_CONTAINER(&cb_slist, cb, node) {
57 		if (cb->model_recv) {
58 			cb->model_recv(src, dst, payload, payload_len);
59 		}
60 	}
61 }
62 
bt_mesh_test_model_bound(uint16_t addr,const struct bt_mesh_model * model,uint16_t key_idx)63 void bt_mesh_test_model_bound(uint16_t addr, const struct bt_mesh_model *model, uint16_t key_idx)
64 {
65 	struct bt_mesh_test_cb *cb;
66 
67 	SYS_SLIST_FOR_EACH_CONTAINER(&cb_slist, cb, node) {
68 		if (cb->model_bound) {
69 			cb->model_bound(addr, model, key_idx);
70 		}
71 	}
72 }
73 
bt_mesh_test_model_unbound(uint16_t addr,const struct bt_mesh_model * model,uint16_t key_idx)74 void bt_mesh_test_model_unbound(uint16_t addr, const struct bt_mesh_model *model, uint16_t key_idx)
75 {
76 	struct bt_mesh_test_cb *cb;
77 
78 	SYS_SLIST_FOR_EACH_CONTAINER(&cb_slist, cb, node) {
79 		if (cb->model_unbound) {
80 			cb->model_unbound(addr, model, key_idx);
81 		}
82 	}
83 }
84 
bt_mesh_test_prov_invalid_bearer(uint8_t opcode)85 void bt_mesh_test_prov_invalid_bearer(uint8_t opcode)
86 {
87 	struct bt_mesh_test_cb *cb;
88 
89 	SYS_SLIST_FOR_EACH_CONTAINER(&cb_slist, cb, node) {
90 		if (cb->prov_invalid_bearer) {
91 			cb->prov_invalid_bearer(opcode);
92 		}
93 	}
94 }
95 
bt_mesh_test_trans_incomp_timer_exp(void)96 void bt_mesh_test_trans_incomp_timer_exp(void)
97 {
98 	struct bt_mesh_test_cb *cb;
99 
100 	SYS_SLIST_FOR_EACH_CONTAINER(&cb_slist, cb, node) {
101 		if (cb->trans_incomp_timer_exp) {
102 			cb->trans_incomp_timer_exp();
103 		}
104 	}
105 }
106 
107 #if defined(CONFIG_BT_MESH_LOW_POWER)
bt_mesh_test_lpn_group_add(uint16_t group)108 int bt_mesh_test_lpn_group_add(uint16_t group)
109 {
110 	bt_mesh_lpn_group_add(group);
111 
112 	return 0;
113 }
114 
bt_mesh_test_lpn_group_remove(uint16_t * groups,size_t groups_count)115 int bt_mesh_test_lpn_group_remove(uint16_t *groups, size_t groups_count)
116 {
117 	bt_mesh_lpn_group_del(groups, groups_count);
118 
119 	return 0;
120 }
121 #endif /* CONFIG_BT_MESH_LOW_POWER */
122 
bt_mesh_test_rpl_clear(void)123 int bt_mesh_test_rpl_clear(void)
124 {
125 	bt_mesh_rpl_clear();
126 
127 	return 0;
128 }
129