-
Notifications
You must be signed in to change notification settings - Fork 4
/
mb.h
218 lines (190 loc) · 5.64 KB
/
mb.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* @file mb.h
* @author Taka Wang
* @brief modbus daemon API(Interface)
*/
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <stdbool.h>
#include <modbus.h>
#include "uthash.h"
#include "log.h"
#include "json.h"
/* ==================================================
* struct
================================================== */
/**
* @brief `structure key` for modbus tcp hash table
*
* Hash key. Ip v4/v6 address and port composition.
*/
typedef struct
{
char ip[50]; /** IP v4/v6 address or hostname */
char port[50]; /** service name/port number to connect to */
} mbtcp_key_s;
/**
* @brief hashable mbtcp handle type
*
* Hashable tcp handle strucut for keeping connection.
*/
typedef struct
{
mbtcp_key_s key; /** key */
bool connected; /** is connect to modbus slave? */
modbus_t *ctx; /** modbus context pointer */
UT_hash_handle hh; /** makes this structure hashable */
} mbtcp_handle_s;
/**
* @brief Function pointer of modbus tcp function code
*
* Function pointer to `modbus tcp function code request` for `generic command handle`.
*
* @param fc function code
* @param handle Mbtcp handle.
* @param req cJSON request object.
* @return Modbus response string in JSON format.
*/
typedef char * (*mbtcp_fc)(uint8_t fc, mbtcp_handle_s *handle, cJSON *req);
/* ==================================================
* api
================================================== */
/**
* @brief Set modbusd success response string with data (i.e., read func)
*
* @param tid Transaction ID.
* @param json_arr cJSON pointer to data array
* @return Modbus ok response string in JSON format.
*/
char * set_modbus_success_resp_str_with_data (char *tid, cJSON *json_arr);
/**
* @brief Set modbusd success response string without data (i.e., write func)
*
* @param tid Transaction ID.
* @return Modbus ok response string in JSON format.
*/
char * set_modbus_success_resp_str (char *tid);
/**
* @brief Set modbusd fail response string.
*
* @param tid Transaction ID.
* @param reason Fail reason string.
* @return Modbus response string in JSON format.
*/
char * set_modbus_fail_resp_str (char *tid, const char *reason);
/**
* @brief Set modbusd fail response string with error number.
*
* @param tid Transaction ID.
* @param handle Mbtcp handle.
* @param errnum Error number from modbus tcp handle.
* @return Modbus error response string in JSON format.
*/
char * set_modbus_fail_resp_str_with_errno (char *tid, mbtcp_handle_s *handle, int errnum);
/* ==================================================
* modbus tcp (mbtcp)
================================================== */
/**
* @brief Init mbtcp handle (to hashtable) and try to connect
*
* @param ptr_handle Pointer to mbtcp handle.
* @param ip IP address string.
* @param port Modbus TCP server port string.
* @return Success or not.
*/
bool mbtcp_init_handle (mbtcp_handle_s **ptr_handle, char *ip, char *port);
/**
* @brief Get mbtcp handle from hashtable
*
* @param ptr_handle Pointer to mbtcp handle.
* @param ip IP address string.
* @param port Modbus TCP server port string or service name.
* @return Success or not.
*/
bool mbtcp_get_handle (mbtcp_handle_s **ptr_handle, char *ip, char *port);
/**
* @brief List all handles in mbtcp hash table
*
* @return Void.
*/
void mbtcp_list_handles ();
/**
* @brief Connect to mbtcp slave via mbtcp hashed handle
*
* @param handle Mbtcp handle.
* @param reason Fail reason string.
* @return Success or not.
*/
bool mbtcp_do_connect (mbtcp_handle_s *handle, char **reason);
/**
* @brief Get mbtcp handle's connection status
*
* @param handle Mbtcp handle.
* @return Success or not.
*/
bool mbtcp_get_connection_status (mbtcp_handle_s *handle);
/**
* @brief Generic mbtcp command handler
*
* @param fc function code
* @param req cJSON request object.
* @param ptr_handler Function pointer to modbus tcp fc handler.
* @return Modbus response string in JSON format.
*/
char * mbtcp_cmd_hanlder (uint8_t fc, cJSON *req, mbtcp_fc ptr_handler);
/**
* @brief Set mbtcp response timeout in usec
*
* @param tid Transaction ID.
* @param timeout Timeout in usec.
* @return Modbus response string in JSON format.
*/
char * mbtcp_set_response_timeout (char *tid, long timeout);
/**
* @brief Get mbtcp response timeout
*
* @param tid Transaction ID.
* @return Modbus response string in JSON format.
*/
char * mbtcp_get_response_timeout (char *tid);
/**
* @brief Help function. FC1, FC2 request handler
*
* @fc Function code 1 and 2 only.
* @param handle Mbtcp handle.
* @param req cJSON request object.
* @return Modbus response string in JSON format.
*/
char * mbtcp_read_bit_req_fn (uint8_t fc, mbtcp_handle_s *handle, cJSON *req);
/**
* @brief Help function. FC3, FC4 request handler
*
* @fc Function code 3 and 4 only.
* @param handle Mbtcp handle.
* @param req cJSON request object.
* @return Modbus response string in JSON format.
*/
char * mbtcp_read_reg_req_fn (uint8_t fc, mbtcp_handle_s *handle, cJSON *req);
/**
* @brief Help function. FC5, FC6 request handler
*
* @fc Function code 5 and 6 only.
* @param handle Mbtcp handle.
* @param req cJSON request object.
* @return Modbus response string in JSON format.
*/
char * mbtcp_single_write_req_fn (uint8_t fc, mbtcp_handle_s *handle, cJSON *req);
/**
* @brief Help function. FC15, FC16 request handler
*
* @fc Function code 15 and 16 only.
* @param handle Mbtcp handle.
* @param req cJSON request object.
* @return Modbus response string in JSON format.
*/
char * mbtcp_multi_write_req_fn (uint8_t fc, mbtcp_handle_s *handle, cJSON *req);