modbusd
modbus master daemon
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
mb.c File Reference

modbus common functions More...

#include "mb.h"
Include dependency graph for mb.c:

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...
 

Variables

int enable_syslog
 

Detailed Description

modbus common functions

Author
Taka Wang

Function Documentation

char* set_modbus_fail_resp_str ( char *  tid,
const char *  reason 
)

Set modbusd fail response string.

Parameters
tidTransaction ID.
reasonFail reason string.
Returns
Modbus response string in JSON format.

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().

42 {
44 
45  ERR (enable_syslog, "Fail: %s", reason);
46 
47  cJSON *resp_root;
48  resp_root = cJSON_CreateObject ();
49  cJSON_AddStringToObject (resp_root, "tid", tid);
50  cJSON_AddStringToObject (resp_root, "status", reason);
51 
52  char * resp_json_string = cJSON_PrintUnformatted (resp_root);
53  LOG(enable_syslog, "resp: %s", resp_json_string);
54  // clean up
55  cJSON_Delete (resp_root);
56  return resp_json_string;
57 }
#define cJSON_AddStringToObject(object, name, s)
Definition: cJSON.h:149
#define BEGIN(flag)
Definition: log.h:48
cJSON * cJSON_CreateObject(void)
Definition: cJSON.c:708
char * cJSON_PrintUnformatted(cJSON *item)
Definition: cJSON.c:347
int enable_syslog
Definition: main.c:46
void cJSON_Delete(cJSON *c)
Definition: cJSON.c:81
#define ERR(flag, fmt,...)
Definition: log.h:24
Definition: cJSON.h:47
#define LOG(flag, fmt,...)
Definition: log.h:36

Here is the call graph for this function:

Here is the caller graph for this function:

char* set_modbus_fail_resp_str_with_errno ( char *  tid,
mbtcp_handle_s handle,
int  errnum 
)

Set modbusd fail response string with error number.

Parameters
tidTransaction ID.
handleMbtcp handle.
errnumError number from modbus tcp handle.
Returns
Modbus error response string in JSON format.

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().

60 {
62 
63  // [TODO][enhance] reconnect proactively?
64  // ... if the request interval is very large,
65  // we should try to reconnect automatically
66 
67  if (errnum == 104) // Connection reset by peer (i.e, tcp connection timeout)
68  {
69  handle->connected = false;
70  }
71 
72  ERR (enable_syslog, "%s:%d", modbus_strerror (errnum), errnum);
73  return set_modbus_fail_resp_str (tid, modbus_strerror (errnum));
74 }
char * set_modbus_fail_resp_str(char *tid, const char *reason)
Set modbusd fail response string.
Definition: mb.c:41
#define BEGIN(flag)
Definition: log.h:48
int enable_syslog
Definition: main.c:46
bool connected
key
Definition: mb.h:45
#define ERR(flag, fmt,...)
Definition: log.h:24

Here is the call graph for this function:

Here is the caller graph for this function:

char* set_modbus_success_resp_str ( char *  tid)

Set modbusd success response string without data (i.e., write func)

Parameters
tidTransaction ID.
Returns
Modbus ok response string in JSON format.

References set_modbus_success_resp_str_with_data().

Referenced by mbtcp_multi_write_req_fn(), and mbtcp_single_write_req_fn().

37 {
38  return set_modbus_success_resp_str_with_data (tid, NULL);
39 }
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

Here is the call graph for this function:

Here is the caller graph for this function:

char* set_modbus_success_resp_str_with_data ( char *  tid,
cJSON json_arr 
)

Set modbusd success response string with data (i.e., read func)

Parameters
tidTransaction ID.
json_arrcJSON pointer to data array
Returns
Modbus ok response string in JSON format.

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().

16 {
18 
19  cJSON *resp_root;
20  resp_root = cJSON_CreateObject ();
21  cJSON_AddStringToObject (resp_root, "tid", tid);
22 
23  if (json_arr != NULL)
24  {
25  cJSON_AddItemToObject (resp_root, "data", json_arr);
26  }
27  cJSON_AddStringToObject (resp_root, "status", "ok");
28 
29  char * resp_json_str = cJSON_PrintUnformatted (resp_root);
30  LOG (enable_syslog, "resp: %s", resp_json_str);
31  // clean up
32  cJSON_Delete (resp_root);
33  return resp_json_str;
34 }
#define cJSON_AddStringToObject(object, name, s)
Definition: cJSON.h:149
void cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
Definition: cJSON.c:676
#define BEGIN(flag)
Definition: log.h:48
cJSON * cJSON_CreateObject(void)
Definition: cJSON.c:708
char * cJSON_PrintUnformatted(cJSON *item)
Definition: cJSON.c:347
int enable_syslog
Definition: main.c:46
void cJSON_Delete(cJSON *c)
Definition: cJSON.c:81
Definition: cJSON.h:47
#define LOG(flag, fmt,...)
Definition: log.h:36

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation