modbusd
modbus master daemon
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
mb.h
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <string.h>
13 #include <time.h>
14 #include <errno.h>
15 #include <stdbool.h>
16 
17 #include <modbus.h>
18 #include "uthash.h"
19 #include "log.h"
20 #include "json.h"
21 
22 /* ==================================================
23  * struct
24 ================================================== */
25 
31 typedef struct
32 {
33  char ip[50];
34  char port[50];
35 } mbtcp_key_s;
36 
42 typedef struct
43 {
45  bool connected;
46  modbus_t *ctx;
49 
50 
61 typedef char * (*mbtcp_fc)(uint8_t fc, mbtcp_handle_s *handle, cJSON *req);
62 
63 
64 /* ==================================================
65  * api
66 ================================================== */
67 
75 char * set_modbus_success_resp_str_with_data (char *tid, cJSON *json_arr);
76 
83 char * set_modbus_success_resp_str (char *tid);
84 
92 char * set_modbus_fail_resp_str (char *tid, const char *reason);
93 
102 char * set_modbus_fail_resp_str_with_errno (char *tid, mbtcp_handle_s *handle, int errnum);
103 
104 /* ==================================================
105  * modbus tcp (mbtcp)
106 ================================================== */
107 
116 bool mbtcp_init_handle (mbtcp_handle_s **ptr_handle, char *ip, char *port);
117 
126 bool mbtcp_get_handle (mbtcp_handle_s **ptr_handle, char *ip, char *port);
127 
133 void mbtcp_list_handles ();
134 
142 bool mbtcp_do_connect (mbtcp_handle_s *handle, char **reason);
143 
151 
160 char * mbtcp_cmd_hanlder (uint8_t fc, cJSON *req, mbtcp_fc ptr_handler);
161 
162 
170 char * mbtcp_set_response_timeout (char *tid, long timeout);
171 
178 char * mbtcp_get_response_timeout (char *tid);
179 
188 char * mbtcp_read_bit_req_fn (uint8_t fc, mbtcp_handle_s *handle, cJSON *req);
189 
198 char * mbtcp_read_reg_req_fn (uint8_t fc, mbtcp_handle_s *handle, cJSON *req);
199 
208 char * mbtcp_single_write_req_fn (uint8_t fc, mbtcp_handle_s *handle, cJSON *req);
209 
218 char * mbtcp_multi_write_req_fn (uint8_t fc, mbtcp_handle_s *handle, cJSON *req);
char * mbtcp_read_bit_req_fn(uint8_t fc, mbtcp_handle_s *handle, cJSON *req)
Help function.
Definition: mbtcp.c:288
modbus_t * ctx
is connect to modbus slave?
Definition: mb.h:46
`structure key` for modbus tcp hash table
Definition: mb.h:31
void mbtcp_list_handles()
List all handles in mbtcp hash table.
Definition: mbtcp.c:161
cJSON helper functions header
char * set_modbus_success_resp_str_with_data(char *tid, cJSON *json_arr)
Set modbusd success response string with data (i.e., read func)
Definition: mb.c:15
char * set_modbus_success_resp_str(char *tid)
Set modbusd success response string without data (i.e., write func)
Definition: mb.c:36
bool mbtcp_get_handle(mbtcp_handle_s **ptr_handle, char *ip, char *port)
Get mbtcp handle from hashtable.
Definition: mbtcp.c:135
mbtcp_key_s key
Definition: mb.h:44
char * set_modbus_fail_resp_str_with_errno(char *tid, mbtcp_handle_s *handle, int errnum)
Set modbusd fail response string with error number.
Definition: mb.c:59
bool connected
key
Definition: mb.h:45
char * mbtcp_multi_write_req_fn(uint8_t fc, mbtcp_handle_s *handle, cJSON *req)
Help function.
Definition: mbtcp.c:417
char * mbtcp_read_reg_req_fn(uint8_t fc, mbtcp_handle_s *handle, cJSON *req)
Help function.
Definition: mbtcp.c:337
char *(* mbtcp_fc)(uint8_t fc, mbtcp_handle_s *handle, cJSON *req)
Function pointer of modbus tcp function code.
Definition: mb.h:61
char * mbtcp_get_response_timeout(char *tid)
Get mbtcp response timeout.
Definition: mbtcp.c:270
bool mbtcp_get_connection_status(mbtcp_handle_s *handle)
Get mbtcp handle's connection status.
Definition: mbtcp.c:202
char * set_modbus_fail_resp_str(char *tid, const char *reason)
Set modbusd fail response string.
Definition: mb.c:41
bool mbtcp_do_connect(mbtcp_handle_s *handle, char **reason)
Connect to mbtcp slave via mbtcp hashed handle.
Definition: mbtcp.c:174
unsigned char uint8_t
Definition: uthash.h:78
char * mbtcp_cmd_hanlder(uint8_t fc, cJSON *req, mbtcp_fc ptr_handler)
Generic mbtcp command handler.
Definition: mbtcp.c:220
bool mbtcp_init_handle(mbtcp_handle_s **ptr_handle, char *ip, char *port)
Init mbtcp handle (to hashtable) and try to connect.
Definition: mbtcp.c:89
Logging system (Syslog Marcos)
char * mbtcp_single_write_req_fn(uint8_t fc, mbtcp_handle_s *handle, cJSON *req)
Help function.
Definition: mbtcp.c:386
Definition: cJSON.h:47
hashable mbtcp handle type
Definition: mb.h:42
char * mbtcp_set_response_timeout(char *tid, long timeout)
Set mbtcp response timeout in usec.
Definition: mbtcp.c:250
UT_hash_handle hh
modbus context pointer
Definition: mb.h:47
Definition: uthash.h:1063