Lines Matching full:ctx
70 struct modbus_context *ctx; in modbus_rx_handler() local
72 ctx = CONTAINER_OF(item, struct modbus_context, server_work); in modbus_rx_handler()
74 switch (ctx->mode) { in modbus_rx_handler()
78 modbus_serial_rx_disable(ctx); in modbus_rx_handler()
79 ctx->rx_adu_err = modbus_serial_rx_adu(ctx); in modbus_rx_handler()
84 ctx->rx_adu_err = modbus_raw_rx_adu(ctx); in modbus_rx_handler()
92 if (ctx->client == true) { in modbus_rx_handler()
93 k_sem_give(&ctx->client_wait_sem); in modbus_rx_handler()
95 bool respond = modbus_server_handler(ctx); in modbus_rx_handler()
98 modbus_tx_adu(ctx); in modbus_rx_handler()
103 switch (ctx->mode) { in modbus_rx_handler()
108 modbus_serial_rx_enable(ctx); in modbus_rx_handler()
117 void modbus_tx_adu(struct modbus_context *ctx) in modbus_tx_adu() argument
119 switch (ctx->mode) { in modbus_tx_adu()
123 modbus_serial_tx_adu(ctx)) { in modbus_tx_adu()
129 modbus_raw_tx_adu(ctx)) { in modbus_tx_adu()
138 int modbus_tx_wait_rx_adu(struct modbus_context *ctx) in modbus_tx_wait_rx_adu() argument
140 k_sem_reset(&ctx->client_wait_sem); in modbus_tx_wait_rx_adu()
142 modbus_tx_adu(ctx); in modbus_tx_wait_rx_adu()
144 if (k_sem_take(&ctx->client_wait_sem, K_USEC(ctx->rxwait_to)) != 0) { in modbus_tx_wait_rx_adu()
149 return ctx->rx_adu_err; in modbus_tx_wait_rx_adu()
154 struct modbus_context *ctx; in modbus_get_context() local
161 ctx = &mb_ctx_tbl[iface]; in modbus_get_context()
163 if (!atomic_test_bit(&ctx->state, MODBUS_STATE_CONFIGURED)) { in modbus_get_context()
168 return ctx; in modbus_get_context()
171 int modbus_iface_get_by_ctx(const struct modbus_context *ctx) in modbus_iface_get_by_ctx() argument
174 if (&mb_ctx_tbl[i] == ctx) { in modbus_iface_get_by_ctx()
195 struct modbus_context *ctx; in modbus_init_iface() local
202 ctx = &mb_ctx_tbl[iface]; in modbus_init_iface()
204 if (atomic_test_and_set_bit(&ctx->state, MODBUS_STATE_CONFIGURED)) { in modbus_init_iface()
209 k_mutex_init(&ctx->iface_lock); in modbus_init_iface()
210 k_sem_init(&ctx->client_wait_sem, 0, 1); in modbus_init_iface()
211 k_work_init(&ctx->server_work, modbus_rx_handler); in modbus_init_iface()
213 return ctx; in modbus_init_iface()
216 static int modbus_user_fc_init(struct modbus_context *ctx, struct modbus_iface_param param) in modbus_user_fc_init() argument
218 sys_slist_init(&ctx->user_defined_cbs); in modbus_user_fc_init()
226 struct modbus_context *ctx = NULL; in modbus_init_server() local
241 ctx = modbus_init_iface(iface); in modbus_init_server()
242 if (ctx == NULL) { in modbus_init_server()
247 ctx->client = false; in modbus_init_server()
249 if (modbus_user_fc_init(ctx, param) != 0) { in modbus_init_server()
259 modbus_serial_init(ctx, param) != 0) { in modbus_init_server()
267 modbus_raw_init(ctx, param) != 0) { in modbus_init_server()
279 ctx->unit_id = param.server.unit_id; in modbus_init_server()
280 ctx->mbs_user_cb = param.server.user_cb; in modbus_init_server()
282 modbus_reset_stats(ctx); in modbus_init_server()
285 LOG_DBG("Modbus interface %s initialized", ctx->iface_name); in modbus_init_server()
290 if (ctx != NULL) { in modbus_init_server()
291 atomic_clear_bit(&ctx->state, MODBUS_STATE_CONFIGURED); in modbus_init_server()
299 struct modbus_context *ctx = modbus_get_context(iface); in modbus_register_user_fc() local
314 sys_slist_append(&ctx->user_defined_cbs, &custom_fc->node); in modbus_register_user_fc()
321 struct modbus_context *ctx = NULL; in modbus_init_client() local
330 ctx = modbus_init_iface(iface); in modbus_init_client()
331 if (ctx == NULL) { in modbus_init_client()
336 ctx->client = true; in modbus_init_client()
342 modbus_serial_init(ctx, param) != 0) { in modbus_init_client()
350 modbus_raw_init(ctx, param) != 0) { in modbus_init_client()
362 ctx->unit_id = 0; in modbus_init_client()
363 ctx->mbs_user_cb = NULL; in modbus_init_client()
364 ctx->rxwait_to = param.rx_timeout; in modbus_init_client()
369 if (ctx != NULL) { in modbus_init_client()
370 atomic_clear_bit(&ctx->state, MODBUS_STATE_CONFIGURED); in modbus_init_client()
378 struct modbus_context *ctx; in modbus_disable() local
381 ctx = modbus_get_context(iface); in modbus_disable()
382 if (ctx == NULL) { in modbus_disable()
387 switch (ctx->mode) { in modbus_disable()
391 modbus_serial_disable(ctx); in modbus_disable()
400 k_work_cancel_sync(&ctx->server_work, &work_sync); in modbus_disable()
401 ctx->rxwait_to = 0; in modbus_disable()
402 ctx->unit_id = 0; in modbus_disable()
403 ctx->mbs_user_cb = NULL; in modbus_disable()
404 atomic_clear_bit(&ctx->state, MODBUS_STATE_CONFIGURED); in modbus_disable()