1 /* Bluetooth: Mesh Generic OnOff, Generic Level, Lighting & Vendor Models
2 *
3 * Copyright (c) 2018 Vikrant More
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include "console/console.h"
9 #include "hal/hal_gpio.h"
10
11 #include "common.h"
12 #include "ble_mesh.h"
13 #include "publisher.h"
14 #include "device_composition.h"
15
16 #define ONOFF
17 #define GENERIC_LEVEL
18 /* #define LIGHT_CTL */
19 /* #define LIGHT_CTL_TEMP */
20
21 static bool is_randomization_of_TIDs_done;
22
23 #if defined(ONOFF)
24 static u8_t tid_onoff;
25 #elif defined(VND_MODEL_TEST)
26 static u8_t tid_vnd;
27 #endif
28
29 static u8_t tid_level;
30
randomize_publishers_TID(void)31 void randomize_publishers_TID(void)
32 {
33 #if defined(ONOFF)
34 bt_rand(&tid_onoff, sizeof(tid_onoff));
35 #elif defined(VND_MODEL_TEST)
36 bt_rand(&tid_vnd, sizeof(tid_vnd));
37 #endif
38
39 bt_rand(&tid_level, sizeof(tid_level));
40
41 is_randomization_of_TIDs_done = true;
42 }
43
button_read(int button)44 static u32_t button_read(int button)
45 {
46 return (uint32_t) hal_gpio_read(button);
47 }
48
publish(struct ble_npl_event * work)49 void publish(struct ble_npl_event *work)
50 {
51 int err = 0;
52
53 if (is_randomization_of_TIDs_done == false) {
54 return;
55 }
56
57 if (button_read(button_device[0]) == 0) {
58 #if defined(ONOFF)
59 bt_mesh_model_msg_init(root_models[3].pub->msg,
60 BT_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK);
61 net_buf_simple_add_u8(root_models[3].pub->msg, 0x01);
62 net_buf_simple_add_u8(root_models[3].pub->msg, tid_onoff++);
63 err = bt_mesh_model_publish(&root_models[3]);
64 #elif defined(ONOFF_TT)
65 bt_mesh_model_msg_init(root_models[3].pub->msg,
66 BT_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK);
67 net_buf_simple_add_u8(root_models[3].pub->msg, 0x01);
68 net_buf_simple_add_u8(root_models[3].pub->msg, tid_onoff++);
69 net_buf_simple_add_u8(root_models[3].pub->msg, 0x45);
70 net_buf_simple_add_u8(root_models[3].pub->msg, 0x28);
71 err = bt_mesh_model_publish(&root_models[3]);
72 #elif defined(VND_MODEL_TEST)
73 bt_mesh_model_msg_init(vnd_models[0].pub->msg,
74 BT_MESH_MODEL_OP_3(0x02, CID_RUNTIME));
75 net_buf_simple_add_le16(vnd_models[0].pub->msg, 0x0001);
76 net_buf_simple_add_u8(vnd_models[0].pub->msg, tid_vnd++);
77 err = bt_mesh_model_publish(&vnd_models[0]);
78 #endif
79
80 } else if (button_read(button_device[1]) == 0) {
81 #if defined(ONOFF)
82 bt_mesh_model_msg_init(root_models[3].pub->msg,
83 BT_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK);
84 net_buf_simple_add_u8(root_models[3].pub->msg, 0x00);
85 net_buf_simple_add_u8(root_models[3].pub->msg, tid_onoff++);
86 err = bt_mesh_model_publish(&root_models[3]);
87 #elif defined(ONOFF_TT)
88 bt_mesh_model_msg_init(root_models[3].pub->msg,
89 BT_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK);
90 net_buf_simple_add_u8(root_models[3].pub->msg, 0x00);
91 net_buf_simple_add_u8(root_models[3].pub->msg, tid_onoff++);
92 net_buf_simple_add_u8(root_models[3].pub->msg, 0x45);
93 net_buf_simple_add_u8(root_models[3].pub->msg, 0x28);
94 err = bt_mesh_model_publish(&root_models[3]);
95 #elif defined(VND_MODEL_TEST)
96 bt_mesh_model_msg_init(vnd_models[0].pub->msg,
97 BT_MESH_MODEL_OP_3(0x02, CID_RUNTIME));
98 net_buf_simple_add_le16(vnd_models[0].pub->msg, 0x0000);
99 net_buf_simple_add_u8(vnd_models[0].pub->msg, tid_vnd++);
100 err = bt_mesh_model_publish(&vnd_models[0]);
101 #endif
102
103 } else if (button_read(button_device[2]) == 0) {
104 #if defined(GENERIC_LEVEL)
105 bt_mesh_model_msg_init(root_models[5].pub->msg,
106 BT_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK);
107 net_buf_simple_add_le16(root_models[5].pub->msg, LEVEL_S25);
108 net_buf_simple_add_u8(root_models[5].pub->msg, tid_level++);
109 err = bt_mesh_model_publish(&root_models[5]);
110 #elif defined(GENERIC_DELTA_LEVEL)
111 bt_mesh_model_msg_init(root_models[5].pub->msg,
112 BT_MESH_MODEL_OP_GEN_DELTA_SET_UNACK);
113 net_buf_simple_add_le32(root_models[5].pub->msg, 100);
114 net_buf_simple_add_u8(root_models[5].pub->msg, tid_level++);
115 err = bt_mesh_model_publish(&root_models[5]);
116 #elif defined(GENERIC_MOVE_LEVEL_TT)
117 bt_mesh_model_msg_init(root_models[5].pub->msg,
118 BT_MESH_MODEL_OP_GEN_MOVE_SET_UNACK);
119 net_buf_simple_add_le16(root_models[5].pub->msg, 13100);
120 net_buf_simple_add_u8(root_models[5].pub->msg, tid_level++);
121 net_buf_simple_add_u8(root_models[5].pub->msg, 0x45);
122 net_buf_simple_add_u8(root_models[5].pub->msg, 0x00);
123 err = bt_mesh_model_publish(&root_models[5]);
124 #elif defined(LIGHT_LIGHTNESS_TT)
125 bt_mesh_model_msg_init(root_models[13].pub->msg,
126 BT_MESH_MODEL_OP_2(0x82, 0x4D));
127 net_buf_simple_add_le16(root_models[13].pub->msg, LEVEL_U25);
128 net_buf_simple_add_u8(root_models[13].pub->msg, tid_level++);
129 net_buf_simple_add_u8(root_models[13].pub->msg, 0x45);
130 net_buf_simple_add_u8(root_models[13].pub->msg, 0x28);
131 err = bt_mesh_model_publish(&root_models[13]);
132 #elif defined(LIGHT_CTL)
133 bt_mesh_model_msg_init(root_models[16].pub->msg,
134 BT_MESH_MODEL_OP_2(0x82, 0x5F));
135 /* Lightness */
136 net_buf_simple_add_le16(root_models[16].pub->msg, LEVEL_U25);
137 /* Temperature (value should be from 0x0320 to 0x4E20 */
138 /* This is as per 6.1.3.1 in Mesh Model Specification */
139 net_buf_simple_add_le16(root_models[16].pub->msg, 0x0320);
140 /* Delta UV */
141 net_buf_simple_add_le16(root_models[16].pub->msg, 0x0000);
142 net_buf_simple_add_u8(root_models[16].pub->msg, tid_level++);
143 err = bt_mesh_model_publish(&root_models[16]);
144 #elif defined(LIGHT_CTL_TT)
145 bt_mesh_model_msg_init(root_models[16].pub->msg,
146 BT_MESH_MODEL_OP_2(0x82, 0x5F));
147 /* Lightness */
148 net_buf_simple_add_le16(root_models[16].pub->msg, LEVEL_U25);
149 /* Temperature (value should be from 0x0320 to 0x4E20 */
150 /* This is as per 6.1.3.1 in Mesh Model Specification */
151 net_buf_simple_add_le16(root_models[16].pub->msg, 0x0320);
152 /* Delta UV */
153 net_buf_simple_add_le16(root_models[16].pub->msg, 0x0000);
154 net_buf_simple_add_u8(root_models[16].pub->msg, tid_level++);
155 net_buf_simple_add_u8(root_models[16].pub->msg, 0x45);
156 net_buf_simple_add_u8(root_models[16].pub->msg, 0x00);
157 err = bt_mesh_model_publish(&root_models[16]);
158 #elif defined(LIGHT_CTL_TEMP)
159 bt_mesh_model_msg_init(root_models[16].pub->msg,
160 BT_MESH_MODEL_OP_2(0x82, 0x65));
161 /* Temperature (value should be from 0x0320 to 0x4E20 */
162 /* This is as per 6.1.3.1 in Mesh Model Specification */
163 net_buf_simple_add_le16(root_models[16].pub->msg, 0x0320);
164 /* Delta UV */
165 net_buf_simple_add_le16(root_models[16].pub->msg, 0x0000);
166 net_buf_simple_add_u8(root_models[16].pub->msg, tid_level++);
167 err = bt_mesh_model_publish(&root_models[16]);
168 #endif
169
170 } else if (button_read(button_device[3]) == 0) {
171 #if defined(GENERIC_LEVEL)
172 bt_mesh_model_msg_init(root_models[5].pub->msg,
173 BT_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK);
174 net_buf_simple_add_le16(root_models[5].pub->msg, LEVEL_S100);
175 net_buf_simple_add_u8(root_models[5].pub->msg, tid_level++);
176 err = bt_mesh_model_publish(&root_models[5]);
177 #elif defined(GENERIC_DELTA_LEVEL)
178 bt_mesh_model_msg_init(root_models[5].pub->msg,
179 BT_MESH_MODEL_OP_GEN_DELTA_SET_UNACK);
180 net_buf_simple_add_le32(root_models[5].pub->msg, -100);
181 net_buf_simple_add_u8(root_models[5].pub->msg, tid_level++);
182 err = bt_mesh_model_publish(&root_models[5]);
183 #elif defined(GENERIC_MOVE_LEVEL_TT)
184 bt_mesh_model_msg_init(root_models[5].pub->msg,
185 BT_MESH_MODEL_OP_GEN_MOVE_SET_UNACK);
186 net_buf_simple_add_le16(root_models[5].pub->msg, -13100);
187 net_buf_simple_add_u8(root_models[5].pub->msg, tid_level++);
188 net_buf_simple_add_u8(root_models[5].pub->msg, 0x45);
189 net_buf_simple_add_u8(root_models[5].pub->msg, 0x00);
190 err = bt_mesh_model_publish(&root_models[5]);
191 #elif defined(LIGHT_LIGHTNESS_TT)
192 bt_mesh_model_msg_init(root_models[13].pub->msg,
193 BT_MESH_MODEL_OP_2(0x82, 0x4D));
194 net_buf_simple_add_le16(root_models[13].pub->msg, LEVEL_U100);
195 net_buf_simple_add_u8(root_models[13].pub->msg, tid_level++);
196 net_buf_simple_add_u8(root_models[13].pub->msg, 0x45);
197 net_buf_simple_add_u8(root_models[13].pub->msg, 0x28);
198 err = bt_mesh_model_publish(&root_models[13]);
199 #elif defined(LIGHT_CTL)
200 bt_mesh_model_msg_init(root_models[16].pub->msg,
201 BT_MESH_MODEL_OP_2(0x82, 0x5F));
202 /* Lightness */
203 net_buf_simple_add_le16(root_models[16].pub->msg, LEVEL_U100);
204 /* Temperature (value should be from 0x0320 to 0x4E20 */
205 /* This is as per 6.1.3.1 in Mesh Model Specification */
206 net_buf_simple_add_le16(root_models[16].pub->msg, 0x4E20);
207 /* Delta UV */
208 net_buf_simple_add_le16(root_models[16].pub->msg, 0x0000);
209 net_buf_simple_add_u8(root_models[16].pub->msg, tid_level++);
210 err = bt_mesh_model_publish(&root_models[16]);
211 #elif defined(LIGHT_CTL_TT)
212 bt_mesh_model_msg_init(root_models[16].pub->msg,
213 BT_MESH_MODEL_OP_2(0x82, 0x5F));
214 /* Lightness */
215 net_buf_simple_add_le16(root_models[16].pub->msg, LEVEL_U100);
216 /* Temperature (value should be from 0x0320 to 0x4E20 */
217 /* This is as per 6.1.3.1 in Mesh Model Specification */
218 net_buf_simple_add_le16(root_models[16].pub->msg, 0x4E20);
219 /* Delta UV */
220 net_buf_simple_add_le16(root_models[16].pub->msg, 0x0000);
221 net_buf_simple_add_u8(root_models[16].pub->msg, tid_level++);
222 net_buf_simple_add_u8(root_models[16].pub->msg, 0x45);
223 net_buf_simple_add_u8(root_models[16].pub->msg, 0x00);
224 err = bt_mesh_model_publish(&root_models[16]);
225 #elif defined(LIGHT_CTL_TEMP)
226 bt_mesh_model_msg_init(root_models[16].pub->msg,
227 BT_MESH_MODEL_OP_2(0x82, 0x65));
228 /* Temperature (value should be from 0x0320 to 0x4E20 */
229 /* This is as per 6.1.3.1 in Mesh Model Specification */
230 net_buf_simple_add_le16(root_models[16].pub->msg, 0x4E20);
231 /* Delta UV */
232 net_buf_simple_add_le16(root_models[16].pub->msg, 0x0000);
233 net_buf_simple_add_u8(root_models[16].pub->msg, tid_level++);
234 err = bt_mesh_model_publish(&root_models[16]);
235 #endif
236 }
237
238 if (err) {
239 printk("bt_mesh_model_publish: err: %d\n", err);
240 }
241 }
242