1 /*
2 * Copyright (c) 2017 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "mesh/mesh.h"
8
9 #define BT_DBG_ENABLED (MYNEWT_VAL(BLE_MESH_DEBUG_MODEL))
10
11 #include "mesh/model_srv.h"
12 #include "mesh_priv.h"
13
gen_onoff_status(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx)14 static void gen_onoff_status(struct bt_mesh_model *model,
15 struct bt_mesh_msg_ctx *ctx)
16 {
17 struct bt_mesh_gen_onoff_srv_cb *cb = model->user_data;
18 struct os_mbuf *msg = NET_BUF_SIMPLE(3);
19 u8_t *state;
20
21 bt_mesh_model_msg_init(msg, OP_GEN_ONOFF_STATUS);
22 state = net_buf_simple_add(msg, 1);
23 if (cb && cb->get) {
24 cb->get(model, state);
25 }
26
27 BT_DBG("state: %d", *state);
28
29 if (bt_mesh_model_send(model, ctx, msg, NULL, NULL)) {
30 BT_ERR("Send status failed");
31 }
32
33 os_mbuf_free_chain(msg);
34 }
35
gen_onoff_get(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct os_mbuf * buf)36 static void gen_onoff_get(struct bt_mesh_model *model,
37 struct bt_mesh_msg_ctx *ctx,
38 struct os_mbuf *buf)
39 {
40 BT_DBG("");
41
42 gen_onoff_status(model, ctx);
43 }
44
gen_onoff_set_unack(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct os_mbuf * buf)45 static void gen_onoff_set_unack(struct bt_mesh_model *model,
46 struct bt_mesh_msg_ctx *ctx,
47 struct os_mbuf *buf)
48 {
49 struct bt_mesh_gen_onoff_srv_cb *cb = model->user_data;
50 u8_t state;
51
52 state = buf->om_data[0];
53
54 BT_DBG("state: %d", state);
55
56 if (cb && cb->set) {
57 cb->set(model, state);
58 }
59 }
60
gen_onoff_set(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct os_mbuf * buf)61 static void gen_onoff_set(struct bt_mesh_model *model,
62 struct bt_mesh_msg_ctx *ctx,
63 struct os_mbuf *buf)
64 {
65 BT_DBG("");
66
67 gen_onoff_set_unack(model, ctx, buf);
68 gen_onoff_status(model, ctx);
69 }
70
gen_level_status(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx)71 static void gen_level_status(struct bt_mesh_model *model,
72 struct bt_mesh_msg_ctx *ctx)
73 {
74 struct bt_mesh_gen_level_srv_cb *cb = model->user_data;
75 struct os_mbuf *msg = NET_BUF_SIMPLE(4);
76 s16_t *level;
77
78 bt_mesh_model_msg_init(msg, OP_GEN_LEVEL_STATUS);
79 level = net_buf_simple_add(msg, 2);
80 if (cb && cb->get) {
81 cb->get(model, level);
82 }
83
84 BT_DBG("level: %d", *level);
85
86 if (bt_mesh_model_send(model, ctx, msg, NULL, NULL)) {
87 BT_ERR("Send status failed");
88 }
89
90 os_mbuf_free_chain(msg);
91 }
92
gen_level_get(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct os_mbuf * buf)93 static void gen_level_get(struct bt_mesh_model *model,
94 struct bt_mesh_msg_ctx *ctx,
95 struct os_mbuf *buf)
96 {
97 BT_DBG("");
98
99 gen_level_status(model, ctx);
100 }
101
gen_level_set_unack(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct os_mbuf * buf)102 static void gen_level_set_unack(struct bt_mesh_model *model,
103 struct bt_mesh_msg_ctx *ctx,
104 struct os_mbuf *buf) {
105 struct bt_mesh_gen_level_srv_cb *cb = model->user_data;
106 s16_t level;
107
108 level = (s16_t) net_buf_simple_pull_le16(buf);
109 BT_DBG("level: %d", level);
110
111 if (cb && cb->set) {
112 cb->set(model, level);
113 }
114 }
115
gen_level_set(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct os_mbuf * buf)116 static void gen_level_set(struct bt_mesh_model *model,
117 struct bt_mesh_msg_ctx *ctx,
118 struct os_mbuf *buf)
119 {
120 gen_level_set_unack(model, ctx, buf);
121 gen_level_status(model, ctx);
122 }
123
light_lightness_status(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx)124 static void light_lightness_status(struct bt_mesh_model *model,
125 struct bt_mesh_msg_ctx *ctx)
126 {
127 struct bt_mesh_light_lightness_srv_cb *cb = model->user_data;
128 struct os_mbuf *msg = NET_BUF_SIMPLE(4);
129 s16_t *lightness;
130
131 bt_mesh_model_msg_init(msg, OP_GEN_LEVEL_STATUS);
132 lightness = net_buf_simple_add(msg, 2);
133 if (cb && cb->get) {
134 cb->get(model, lightness);
135 }
136
137 BT_DBG("lightness: %d", *lightness);
138
139 if (bt_mesh_model_send(model, ctx, msg, NULL, NULL)) {
140 BT_ERR("Send status failed");
141 }
142
143 os_mbuf_free_chain(msg);
144 }
145
light_lightness_get(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct os_mbuf * buf)146 static void light_lightness_get(struct bt_mesh_model *model,
147 struct bt_mesh_msg_ctx *ctx,
148 struct os_mbuf *buf)
149 {
150 BT_DBG("");
151
152 light_lightness_status(model, ctx);
153 }
154
light_lightness_set_unack(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct os_mbuf * buf)155 static void light_lightness_set_unack(struct bt_mesh_model *model,
156 struct bt_mesh_msg_ctx *ctx,
157 struct os_mbuf *buf) {
158 struct bt_mesh_light_lightness_srv_cb *cb = model->user_data;
159 s16_t lightness;
160
161 lightness = (s16_t) net_buf_simple_pull_le16(buf);
162 BT_DBG("lightness: %d", lightness);
163
164 if (cb && cb->set) {
165 cb->set(model, lightness);
166 }
167 }
168
light_lightness_set(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct os_mbuf * buf)169 static void light_lightness_set(struct bt_mesh_model *model,
170 struct bt_mesh_msg_ctx *ctx,
171 struct os_mbuf *buf)
172 {
173 light_lightness_set_unack(model, ctx, buf);
174 light_lightness_status(model, ctx);
175 }
176
177 const struct bt_mesh_model_op gen_onoff_srv_op[] = {
178 { OP_GEN_ONOFF_GET, 0, gen_onoff_get },
179 { OP_GEN_ONOFF_SET, 2, gen_onoff_set },
180 { OP_GEN_ONOFF_SET_UNACK, 2, gen_onoff_set_unack },
181 BT_MESH_MODEL_OP_END,
182 };
183
184 const struct bt_mesh_model_op gen_level_srv_op[] = {
185 { OP_GEN_LEVEL_GET, 0, gen_level_get },
186 { OP_GEN_LEVEL_SET, 3, gen_level_set },
187 { OP_GEN_LEVEL_SET_UNACK, 3, gen_level_set_unack },
188 BT_MESH_MODEL_OP_END,
189 };
190
191 const struct bt_mesh_model_op light_lightness_srv_op[] = {
192 { OP_LIGHT_LIGHTNESS_GET, 0, light_lightness_get },
193 { OP_LIGHT_LIGHTNESS_SET, 3, light_lightness_set },
194 { OP_LIGHT_LIGHTNESS_SET_UNACK, 3, light_lightness_set_unack },
195 BT_MESH_MODEL_OP_END,
196 };
197