modbusd
modbus master daemon
|
modbus daemon API(Interface) More...
#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"
Go to the source code of this file.
Data Structures | |
struct | mbtcp_key_s |
`structure key` for modbus tcp hash table More... | |
struct | mbtcp_handle_s |
hashable mbtcp handle type More... | |
Typedefs | |
typedef char *(* | mbtcp_fc )(uint8_t fc, mbtcp_handle_s *handle, cJSON *req) |
Function pointer of modbus tcp function code. More... | |
Functions | |
char * | set_modbus_success_resp_str_with_data (char *tid, cJSON *json_arr) |
Set modbusd success response string with data (i.e., read func) More... | |
char * | set_modbus_success_resp_str (char *tid) |
Set modbusd success response string without data (i.e., write func) More... | |
char * | set_modbus_fail_resp_str (char *tid, const char *reason) |
Set modbusd fail response string. More... | |
char * | set_modbus_fail_resp_str_with_errno (char *tid, mbtcp_handle_s *handle, int errnum) |
Set modbusd fail response string with error number. More... | |
bool | mbtcp_init_handle (mbtcp_handle_s **ptr_handle, char *ip, char *port) |
Init mbtcp handle (to hashtable) and try to connect. More... | |
bool | mbtcp_get_handle (mbtcp_handle_s **ptr_handle, char *ip, char *port) |
Get mbtcp handle from hashtable. More... | |
void | mbtcp_list_handles () |
List all handles in mbtcp hash table. More... | |
bool | mbtcp_do_connect (mbtcp_handle_s *handle, char **reason) |
Connect to mbtcp slave via mbtcp hashed handle. More... | |
bool | mbtcp_get_connection_status (mbtcp_handle_s *handle) |
Get mbtcp handle's connection status. More... | |
char * | mbtcp_cmd_hanlder (uint8_t fc, cJSON *req, mbtcp_fc ptr_handler) |
Generic mbtcp command handler. More... | |
char * | mbtcp_set_response_timeout (char *tid, long timeout) |
Set mbtcp response timeout in usec. More... | |
char * | mbtcp_get_response_timeout (char *tid) |
Get mbtcp response timeout. More... | |
char * | mbtcp_read_bit_req_fn (uint8_t fc, mbtcp_handle_s *handle, cJSON *req) |
Help function. More... | |
char * | mbtcp_read_reg_req_fn (uint8_t fc, mbtcp_handle_s *handle, cJSON *req) |
Help function. More... | |
char * | mbtcp_single_write_req_fn (uint8_t fc, mbtcp_handle_s *handle, cJSON *req) |
Help function. More... | |
char * | mbtcp_multi_write_req_fn (uint8_t fc, mbtcp_handle_s *handle, cJSON *req) |
Help function. More... | |
modbus daemon API(Interface)
typedef char*(* mbtcp_fc)(uint8_t fc, mbtcp_handle_s *handle, cJSON *req) |
Function pointer of modbus tcp function code.
Function pointer to `modbus tcp function code request` for `generic command handle`.
fc | function code |
handle | Mbtcp handle. |
req | cJSON request object. |
Generic mbtcp command handler.
fc | function code |
req | cJSON request object. |
ptr_handler | Function pointer to modbus tcp fc handler. |
References BEGIN, mbtcp_handle_s::ctx, enable_syslog, json_get_char(), json_get_int(), LOG, and set_modbus_fail_resp_str().
Referenced by main().
bool mbtcp_do_connect | ( | mbtcp_handle_s * | handle, |
char ** | reason | ||
) |
Connect to mbtcp slave via mbtcp hashed handle.
handle | Mbtcp handle. |
reason | Fail reason string. |
References BEGIN, mbtcp_handle_s::connected, mbtcp_handle_s::ctx, enable_syslog, ERR, mbtcp_key_s::ip, mbtcp_handle_s::key, LOG, and mbtcp_key_s::port.
Referenced by mbtcp_init_handle(), and test_init_tcp_handle_and_connect().
bool mbtcp_get_connection_status | ( | mbtcp_handle_s * | handle | ) |
Get mbtcp handle's connection status.
handle | Mbtcp handle. |
References BEGIN, mbtcp_handle_s::connected, enable_syslog, ERR, mbtcp_key_s::ip, mbtcp_handle_s::key, LOG, and mbtcp_key_s::port.
Referenced by test_init_tcp_handle_and_connect().
bool mbtcp_get_handle | ( | mbtcp_handle_s ** | ptr_handle, |
char * | ip, | ||
char * | port | ||
) |
Get mbtcp handle from hashtable.
ptr_handle | Pointer to mbtcp handle. |
ip | IP address string. |
port | Modbus TCP server port string or service name. |
References BEGIN, enable_syslog, ERR, HASH_FIND, mbtcp_key_s::ip, mbtcp_handle_s::key, LOG, and mbtcp_key_s::port.
Referenced by test_init_tcp_handle_and_connect().
char* mbtcp_get_response_timeout | ( | char * | tid | ) |
Get mbtcp response timeout.
tid | Transaction ID. |
References BEGIN, cJSON_AddNumberToObject, cJSON_AddStringToObject, cJSON_CreateObject(), cJSON_Delete(), cJSON_PrintUnformatted(), enable_syslog, END, LOG, and tcp_conn_timeout_usec.
Referenced by main().
bool mbtcp_init_handle | ( | mbtcp_handle_s ** | ptr_handle, |
char * | ip, | ||
char * | port | ||
) |
Init mbtcp handle (to hashtable) and try to connect.
ptr_handle | Pointer to mbtcp handle. |
ip | IP address string. |
port | Modbus TCP server port string. |
References BEGIN, mbtcp_handle_s::connected, mbtcp_handle_s::ctx, enable_syslog, ERR, HASH_ADD, mbtcp_key_s::ip, mbtcp_handle_s::key, LOG, mbtcp_do_connect(), mbtcp_key_s::port, and tcp_conn_timeout_usec.
Referenced by test_init_tcp_handle_and_connect().
void mbtcp_list_handles | ( | ) |
List all handles in mbtcp hash table.
References BEGIN, enable_syslog, END, mbtcp_handle_s::hh, mbtcp_key_s::ip, mbtcp_handle_s::key, LOG, UT_hash_handle::next, and mbtcp_key_s::port.
char* mbtcp_multi_write_req_fn | ( | uint8_t | fc, |
mbtcp_handle_s * | handle, | ||
cJSON * | req | ||
) |
Help function.
FC15, FC16 request handler
Function code 15 and 16 only.
handle | Mbtcp handle. |
req | cJSON request object. |
References BEGIN, cJSON_GetArrayItem(), cJSON_GetArraySize(), cJSON_GetObjectItem(), mbtcp_handle_s::ctx, enable_syslog, json_get_char(), json_get_int(), LOG, set_modbus_fail_resp_str(), set_modbus_fail_resp_str_with_errno(), set_modbus_success_resp_str(), and cJSON::valueint.
Referenced by main().
char* mbtcp_read_bit_req_fn | ( | uint8_t | fc, |
mbtcp_handle_s * | handle, | ||
cJSON * | req | ||
) |
Help function.
FC1, FC2 request handler
Function code 1 and 2 only.
handle | Mbtcp handle. |
req | cJSON request object. |
References BEGIN, cJSON_CreateUInt8Array(), mbtcp_handle_s::ctx, enable_syslog, json_get_char(), json_get_int(), LOG, set_modbus_fail_resp_str(), set_modbus_fail_resp_str_with_errno(), and set_modbus_success_resp_str_with_data().
Referenced by main().
char* mbtcp_read_reg_req_fn | ( | uint8_t | fc, |
mbtcp_handle_s * | handle, | ||
cJSON * | req | ||
) |
Help function.
FC3, FC4 request handler
Function code 3 and 4 only.
handle | Mbtcp handle. |
req | cJSON request object. |
References BEGIN, cJSON_CreateUInt16Array(), mbtcp_handle_s::ctx, enable_syslog, json_get_char(), json_get_int(), LOG, set_modbus_fail_resp_str(), set_modbus_fail_resp_str_with_errno(), and set_modbus_success_resp_str_with_data().
Referenced by main().
char* mbtcp_set_response_timeout | ( | char * | tid, |
long | timeout | ||
) |
Set mbtcp response timeout in usec.
tid | Transaction ID. |
timeout | Timeout in usec. |
References BEGIN, cJSON_AddStringToObject, cJSON_CreateObject(), cJSON_Delete(), cJSON_PrintUnformatted(), enable_syslog, END, LOG, and tcp_conn_timeout_usec.
Referenced by main().
char* mbtcp_single_write_req_fn | ( | uint8_t | fc, |
mbtcp_handle_s * | handle, | ||
cJSON * | req | ||
) |
Help function.
FC5, FC6 request handler
Function code 5 and 6 only.
handle | Mbtcp handle. |
req | cJSON request object. |
References BEGIN, mbtcp_handle_s::ctx, enable_syslog, json_get_char(), json_get_int(), set_modbus_fail_resp_str(), set_modbus_fail_resp_str_with_errno(), and set_modbus_success_resp_str().
Referenced by main().
char* set_modbus_fail_resp_str | ( | char * | tid, |
const char * | reason | ||
) |
Set modbusd fail response string.
tid | Transaction ID. |
reason | Fail reason string. |
References BEGIN, cJSON_AddStringToObject, cJSON_CreateObject(), cJSON_Delete(), cJSON_PrintUnformatted(), enable_syslog, ERR, and LOG.
Referenced by main(), mbtcp_cmd_hanlder(), mbtcp_multi_write_req_fn(), mbtcp_read_bit_req_fn(), mbtcp_read_reg_req_fn(), mbtcp_single_write_req_fn(), and set_modbus_fail_resp_str_with_errno().
char* set_modbus_fail_resp_str_with_errno | ( | char * | tid, |
mbtcp_handle_s * | handle, | ||
int | errnum | ||
) |
Set modbusd fail response string with error number.
tid | Transaction ID. |
handle | Mbtcp handle. |
errnum | Error number from modbus tcp handle. |
References BEGIN, mbtcp_handle_s::connected, enable_syslog, ERR, and set_modbus_fail_resp_str().
Referenced by mbtcp_multi_write_req_fn(), mbtcp_read_bit_req_fn(), mbtcp_read_reg_req_fn(), and mbtcp_single_write_req_fn().
char* set_modbus_success_resp_str | ( | char * | tid | ) |
Set modbusd success response string without data (i.e., write func)
tid | Transaction ID. |
References set_modbus_success_resp_str_with_data().
Referenced by mbtcp_multi_write_req_fn(), and mbtcp_single_write_req_fn().
char* set_modbus_success_resp_str_with_data | ( | char * | tid, |
cJSON * | json_arr | ||
) |
Set modbusd success response string with data (i.e., read func)
tid | Transaction ID. |
json_arr | cJSON pointer to data array |
References BEGIN, cJSON_AddItemToObject(), cJSON_AddStringToObject, cJSON_CreateObject(), cJSON_Delete(), cJSON_PrintUnformatted(), enable_syslog, and LOG.
Referenced by mbtcp_read_bit_req_fn(), mbtcp_read_reg_req_fn(), and set_modbus_success_resp_str().