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