1 /*
2 * Copyright (c) 2020 PHYTEC Messtechnik GmbH
3 * Copyright (c) 2021 Nordic Semiconductor ASA
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/logging/log.h>
9 LOG_MODULE_REGISTER(modbus, CONFIG_MODBUS_LOG_LEVEL);
10
11 #include <zephyr/kernel.h>
12 #include <string.h>
13 #include <zephyr/sys/byteorder.h>
14 #include <modbus_internal.h>
15
16 #define DT_DRV_COMPAT zephyr_modbus_serial
17
18 #define MB_RTU_DEFINE_GPIO_CFG(inst, prop) \
19 static struct gpio_dt_spec prop##_cfg_##inst = { \
20 .port = DEVICE_DT_GET(DT_INST_PHANDLE(inst, prop)), \
21 .pin = DT_INST_GPIO_PIN(inst, prop), \
22 .dt_flags = DT_INST_GPIO_FLAGS(inst, prop), \
23 };
24
25 #define MB_RTU_DEFINE_GPIO_CFGS(inst) \
26 COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, de_gpios), \
27 (MB_RTU_DEFINE_GPIO_CFG(inst, de_gpios)), ()) \
28 COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, re_gpios), \
29 (MB_RTU_DEFINE_GPIO_CFG(inst, re_gpios)), ())
30
31 DT_INST_FOREACH_STATUS_OKAY(MB_RTU_DEFINE_GPIO_CFGS)
32
33 #define MB_RTU_ASSIGN_GPIO_CFG(inst, prop) \
34 COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, prop), \
35 (&prop##_cfg_##inst), (NULL))
36
37 #define MODBUS_DT_GET_SERIAL_DEV(inst) { \
38 .dev = DEVICE_DT_GET(DT_INST_PARENT(inst)), \
39 .de = MB_RTU_ASSIGN_GPIO_CFG(inst, de_gpios), \
40 .re = MB_RTU_ASSIGN_GPIO_CFG(inst, re_gpios), \
41 },
42
43 #ifdef CONFIG_MODBUS_SERIAL
44 static struct modbus_serial_config modbus_serial_cfg[] = {
45 DT_INST_FOREACH_STATUS_OKAY(MODBUS_DT_GET_SERIAL_DEV)
46 };
47 #endif
48
49 #define MODBUS_DT_GET_DEV(inst) { \
50 .iface_name = DEVICE_DT_NAME(DT_DRV_INST(inst)),\
51 .cfg = &modbus_serial_cfg[inst], \
52 },
53
54 #define DEFINE_MODBUS_RAW_ADU(x, _) { \
55 .iface_name = "RAW_"#x, \
56 .rawcb.raw_tx_cb = NULL, \
57 .mode = MODBUS_MODE_RAW, \
58 }
59
60
61 static struct modbus_context mb_ctx_tbl[] = {
62 DT_INST_FOREACH_STATUS_OKAY(MODBUS_DT_GET_DEV)
63 #ifdef CONFIG_MODBUS_RAW_ADU
64 LISTIFY(CONFIG_MODBUS_NUMOF_RAW_ADU, DEFINE_MODBUS_RAW_ADU, (,), _)
65 #endif
66 };
67
modbus_rx_handler(struct k_work * item)68 static void modbus_rx_handler(struct k_work *item)
69 {
70 struct modbus_context *ctx;
71
72 ctx = CONTAINER_OF(item, struct modbus_context, server_work);
73
74 switch (ctx->mode) {
75 case MODBUS_MODE_RTU:
76 case MODBUS_MODE_ASCII:
77 if (IS_ENABLED(CONFIG_MODBUS_SERIAL)) {
78 modbus_serial_rx_disable(ctx);
79 ctx->rx_adu_err = modbus_serial_rx_adu(ctx);
80 }
81 break;
82 case MODBUS_MODE_RAW:
83 if (IS_ENABLED(CONFIG_MODBUS_RAW_ADU)) {
84 ctx->rx_adu_err = modbus_raw_rx_adu(ctx);
85 }
86 break;
87 default:
88 LOG_ERR("Unknown MODBUS mode");
89 return;
90 }
91
92 if (ctx->client == true) {
93 k_sem_give(&ctx->client_wait_sem);
94 } else if (IS_ENABLED(CONFIG_MODBUS_SERVER)) {
95 bool respond = modbus_server_handler(ctx);
96
97 if (respond) {
98 modbus_tx_adu(ctx);
99 } else {
100 LOG_DBG("Server has dropped frame");
101 }
102
103 switch (ctx->mode) {
104 case MODBUS_MODE_RTU:
105 case MODBUS_MODE_ASCII:
106 if (IS_ENABLED(CONFIG_MODBUS_SERIAL) &&
107 respond == false) {
108 modbus_serial_rx_enable(ctx);
109 }
110 break;
111 default:
112 break;
113 }
114 }
115 }
116
modbus_tx_adu(struct modbus_context * ctx)117 void modbus_tx_adu(struct modbus_context *ctx)
118 {
119 switch (ctx->mode) {
120 case MODBUS_MODE_RTU:
121 case MODBUS_MODE_ASCII:
122 if (IS_ENABLED(CONFIG_MODBUS_SERIAL) &&
123 modbus_serial_tx_adu(ctx)) {
124 LOG_ERR("Unsupported MODBUS serial mode");
125 }
126 break;
127 case MODBUS_MODE_RAW:
128 if (IS_ENABLED(CONFIG_MODBUS_RAW_ADU) &&
129 modbus_raw_tx_adu(ctx)) {
130 LOG_ERR("Unsupported MODBUS raw mode");
131 }
132 break;
133 default:
134 LOG_ERR("Unknown MODBUS mode");
135 }
136 }
137
modbus_tx_wait_rx_adu(struct modbus_context * ctx)138 int modbus_tx_wait_rx_adu(struct modbus_context *ctx)
139 {
140 modbus_tx_adu(ctx);
141
142 if (k_sem_take(&ctx->client_wait_sem, K_USEC(ctx->rxwait_to)) != 0) {
143 LOG_WRN("Client wait-for-RX timeout");
144 return -ETIMEDOUT;
145 }
146
147 return ctx->rx_adu_err;
148 }
149
modbus_get_context(const uint8_t iface)150 struct modbus_context *modbus_get_context(const uint8_t iface)
151 {
152 struct modbus_context *ctx;
153
154 if (iface >= ARRAY_SIZE(mb_ctx_tbl)) {
155 LOG_ERR("Interface %u not available", iface);
156 return NULL;
157 }
158
159 ctx = &mb_ctx_tbl[iface];
160
161 if (!atomic_test_bit(&ctx->state, MODBUS_STATE_CONFIGURED)) {
162 LOG_ERR("Interface not configured");
163 return NULL;
164 }
165
166 return ctx;
167 }
168
modbus_iface_get_by_ctx(const struct modbus_context * ctx)169 int modbus_iface_get_by_ctx(const struct modbus_context *ctx)
170 {
171 for (int i = 0; i < ARRAY_SIZE(mb_ctx_tbl); i++) {
172 if (&mb_ctx_tbl[i] == ctx) {
173 return i;
174 }
175 }
176
177 return -ENODEV;
178 }
179
modbus_iface_get_by_name(const char * iface_name)180 int modbus_iface_get_by_name(const char *iface_name)
181 {
182 for (int i = 0; i < ARRAY_SIZE(mb_ctx_tbl); i++) {
183 if (strcmp(iface_name, mb_ctx_tbl[i].iface_name) == 0) {
184 return i;
185 }
186 }
187
188 return -ENODEV;
189 }
190
modbus_init_iface(const uint8_t iface)191 static struct modbus_context *modbus_init_iface(const uint8_t iface)
192 {
193 struct modbus_context *ctx;
194
195 if (iface >= ARRAY_SIZE(mb_ctx_tbl)) {
196 LOG_ERR("Interface %u not available", iface);
197 return NULL;
198 }
199
200 ctx = &mb_ctx_tbl[iface];
201
202 if (atomic_test_and_set_bit(&ctx->state, MODBUS_STATE_CONFIGURED)) {
203 LOG_ERR("Interface already used");
204 return NULL;
205 }
206
207 k_mutex_init(&ctx->iface_lock);
208 k_sem_init(&ctx->client_wait_sem, 0, 1);
209 k_work_init(&ctx->server_work, modbus_rx_handler);
210
211 return ctx;
212 }
213
modbus_user_fc_init(struct modbus_context * ctx,struct modbus_iface_param param)214 static int modbus_user_fc_init(struct modbus_context *ctx, struct modbus_iface_param param)
215 {
216 sys_slist_init(&ctx->user_defined_cbs);
217 LOG_DBG("Initializing user-defined function code support.");
218
219 return 0;
220 }
221
modbus_init_server(const int iface,struct modbus_iface_param param)222 int modbus_init_server(const int iface, struct modbus_iface_param param)
223 {
224 struct modbus_context *ctx = NULL;
225 int rc = 0;
226
227 if (!IS_ENABLED(CONFIG_MODBUS_SERVER)) {
228 LOG_ERR("Modbus server support is not enabled");
229 rc = -ENOTSUP;
230 goto init_server_error;
231 }
232
233 if (param.server.user_cb == NULL) {
234 LOG_ERR("User callbacks should be available");
235 rc = -EINVAL;
236 goto init_server_error;
237 }
238
239 ctx = modbus_init_iface(iface);
240 if (ctx == NULL) {
241 rc = -EINVAL;
242 goto init_server_error;
243 }
244
245 ctx->client = false;
246
247 if (modbus_user_fc_init(ctx, param) != 0) {
248 LOG_ERR("Failed to init MODBUS user defined function codes");
249 rc = -EINVAL;
250 goto init_server_error;
251 }
252
253 switch (param.mode) {
254 case MODBUS_MODE_RTU:
255 case MODBUS_MODE_ASCII:
256 if (IS_ENABLED(CONFIG_MODBUS_SERIAL) &&
257 modbus_serial_init(ctx, param) != 0) {
258 LOG_ERR("Failed to init MODBUS over serial line");
259 rc = -EINVAL;
260 goto init_server_error;
261 }
262 break;
263 case MODBUS_MODE_RAW:
264 if (IS_ENABLED(CONFIG_MODBUS_RAW_ADU) &&
265 modbus_raw_init(ctx, param) != 0) {
266 LOG_ERR("Failed to init MODBUS raw ADU support");
267 rc = -EINVAL;
268 goto init_server_error;
269 }
270 break;
271 default:
272 LOG_ERR("Unknown MODBUS mode");
273 rc = -ENOTSUP;
274 goto init_server_error;
275 }
276
277 ctx->unit_id = param.server.unit_id;
278 ctx->mbs_user_cb = param.server.user_cb;
279 if (IS_ENABLED(CONFIG_MODBUS_FC08_DIAGNOSTIC)) {
280 modbus_reset_stats(ctx);
281 }
282
283 LOG_DBG("Modbus interface %s initialized", ctx->iface_name);
284
285 return 0;
286
287 init_server_error:
288 if (ctx != NULL) {
289 atomic_clear_bit(&ctx->state, MODBUS_STATE_CONFIGURED);
290 }
291
292 return rc;
293 }
294
modbus_register_user_fc(const int iface,struct modbus_custom_fc * custom_fc)295 int modbus_register_user_fc(const int iface, struct modbus_custom_fc *custom_fc)
296 {
297 struct modbus_context *ctx = modbus_get_context(iface);
298
299 if (!custom_fc) {
300 LOG_ERR("Provided function code handler was NULL");
301 return -EINVAL;
302 }
303
304 if (custom_fc->fc & BIT(7)) {
305 LOG_ERR("Function codes must have MSB of 0");
306 return -EINVAL;
307 }
308
309 custom_fc->excep_code = MODBUS_EXC_NONE;
310
311 LOG_DBG("Registered new custom function code %d", custom_fc->fc);
312 sys_slist_append(&ctx->user_defined_cbs, &custom_fc->node);
313
314 return 0;
315 }
316
modbus_init_client(const int iface,struct modbus_iface_param param)317 int modbus_init_client(const int iface, struct modbus_iface_param param)
318 {
319 struct modbus_context *ctx = NULL;
320 int rc = 0;
321
322 if (!IS_ENABLED(CONFIG_MODBUS_CLIENT)) {
323 LOG_ERR("Modbus client support is not enabled");
324 rc = -ENOTSUP;
325 goto init_client_error;
326 }
327
328 ctx = modbus_init_iface(iface);
329 if (ctx == NULL) {
330 rc = -EINVAL;
331 goto init_client_error;
332 }
333
334 ctx->client = true;
335
336 switch (param.mode) {
337 case MODBUS_MODE_RTU:
338 case MODBUS_MODE_ASCII:
339 if (IS_ENABLED(CONFIG_MODBUS_SERIAL) &&
340 modbus_serial_init(ctx, param) != 0) {
341 LOG_ERR("Failed to init MODBUS over serial line");
342 rc = -EINVAL;
343 goto init_client_error;
344 }
345 break;
346 case MODBUS_MODE_RAW:
347 if (IS_ENABLED(CONFIG_MODBUS_RAW_ADU) &&
348 modbus_raw_init(ctx, param) != 0) {
349 LOG_ERR("Failed to init MODBUS raw ADU support");
350 rc = -EINVAL;
351 goto init_client_error;
352 }
353 break;
354 default:
355 LOG_ERR("Unknown MODBUS mode");
356 rc = -ENOTSUP;
357 goto init_client_error;
358 }
359
360 ctx->unit_id = 0;
361 ctx->mbs_user_cb = NULL;
362 ctx->rxwait_to = param.rx_timeout;
363
364 return 0;
365
366 init_client_error:
367 if (ctx != NULL) {
368 atomic_clear_bit(&ctx->state, MODBUS_STATE_CONFIGURED);
369 }
370
371 return rc;
372 }
373
modbus_disable(const uint8_t iface)374 int modbus_disable(const uint8_t iface)
375 {
376 struct modbus_context *ctx;
377 struct k_work_sync work_sync;
378
379 ctx = modbus_get_context(iface);
380 if (ctx == NULL) {
381 LOG_ERR("Interface %u not initialized", iface);
382 return -EINVAL;
383 }
384
385 switch (ctx->mode) {
386 case MODBUS_MODE_RTU:
387 case MODBUS_MODE_ASCII:
388 if (IS_ENABLED(CONFIG_MODBUS_SERIAL)) {
389 modbus_serial_disable(ctx);
390 }
391 break;
392 case MODBUS_MODE_RAW:
393 break;
394 default:
395 LOG_ERR("Unknown MODBUS mode");
396 }
397
398 k_work_cancel_sync(&ctx->server_work, &work_sync);
399 ctx->rxwait_to = 0;
400 ctx->unit_id = 0;
401 ctx->mbs_user_cb = NULL;
402 atomic_clear_bit(&ctx->state, MODBUS_STATE_CONFIGURED);
403
404 LOG_INF("Modbus interface %u disabled", iface);
405
406 return 0;
407 }
408