modbusd
modbus master daemon
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
test.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <czmq.h>
#include "../../mb.h"
Include dependency graph for test.c:

Functions

void test_json_decode ()
 
void test_json_encode ()
 
int test_init_tcp_handle_and_connect ()
 
void test_multiple_add_find ()
 
void test_single_add_find ()
 
int main ()
 

Variables

int enable_syslog = 1
 

Function Documentation

int main ( )

References BEGIN, enable_syslog, test_init_tcp_handle_and_connect(), test_json_decode(), test_json_encode(), test_multiple_add_find(), and test_single_add_find().

214 {
216 
222 
223 
224 }
int enable_syslog
Definition: test.c:17
#define BEGIN(flag)
Definition: log.h:48
void test_json_encode()
Definition: test.c:60
void test_json_decode()
Definition: test.c:25
int test_init_tcp_handle_and_connect()
Definition: test.c:80
void test_multiple_add_find()
Definition: test.c:101
void test_single_add_find()
Definition: test.c:144

Here is the call graph for this function:

int test_init_tcp_handle_and_connect ( )

References enable_syslog, LOG, mbtcp_do_connect(), mbtcp_get_connection_status(), mbtcp_get_handle(), and mbtcp_init_handle().

Referenced by main().

81 {
82  LOG(enable_syslog, "Init TCP handle");
83  mbtcp_handle_s *handle = NULL;
84 
85  mbtcp_init_handle (&handle, "172.16.9.170", "502");
86  //char * reason = NULL;
87  //mbtcp_do_connect(handle, &reason);
88 
89  LOG(enable_syslog, "Get TCP handle");
90  handle = NULL;
91  mbtcp_get_handle (&handle, "172.16.9.170", "502");
92 
93  LOG(enable_syslog, "Try to connect to slave");
94  char * reason = NULL;
95  mbtcp_do_connect(handle, &reason);
97  return 0;
98 }
int enable_syslog
Definition: test.c:17
bool mbtcp_get_handle(mbtcp_handle_s **ptr_handle, char *ip, char *port)
Get mbtcp handle from hashtable.
Definition: mbtcp.c:135
bool mbtcp_get_connection_status(mbtcp_handle_s *handle)
Get mbtcp handle's connection status.
Definition: mbtcp.c:202
bool mbtcp_do_connect(mbtcp_handle_s *handle, char **reason)
Connect to mbtcp slave via mbtcp hashed handle.
Definition: mbtcp.c:174
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
hashable mbtcp handle type
Definition: mb.h:42
#define LOG(flag, fmt,...)
Definition: log.h:36

Here is the call graph for this function:

Here is the caller graph for this function:

void test_json_decode ( )

References cJSON_Delete(), cJSON_GetArrayItem(), cJSON_GetArraySize(), cJSON_GetObjectItem(), cJSON_Parse(), cJSON_Print(), and cJSON::valueint.

Referenced by main().

26 {
27  char jstr[] = "{\n"
28  " \"ip\": \"192.168.3.2\",\n"
29  " \"port\": \"502\",\n"
30  " \"slave\": 22,\n"
31  " \"tid\": 1,\n"
32  " \"mode\": \"tcp\",\n"
33  " \"cmd\": \"fc5\",\n"
34  " \"addr\": 250,\n"
35  " \"len\": 10,\n"
36  " \"data\": [1,2,3,4]\n"
37  "}";
38  cJSON *json = cJSON_Parse(jstr);
39  if (json)
40  {
41  printf("%s\n", cJSON_Print(json));
42  printf("---------\n");
43  printf("ip:%s\n", cJSON_GetObjectItem(json, "ip")->valuestring);
44  printf("port:%s\n",cJSON_GetObjectItem(json, "port")->valuestring);
45  printf("mode:%s\n", cJSON_GetObjectItem(json, "mode")->valuestring);
46  printf("addr:%d\n",cJSON_GetObjectItem(json, "addr")->valueint);
47 
48  // handle array
49  cJSON * data = cJSON_GetObjectItem(json, "data");
50  for (int i = 0 ; i < cJSON_GetArraySize(data) ; i++)
51  {
52  int subitem = cJSON_GetArrayItem(data, i)->valueint;
53  printf("idx:%d,v:%d\n", i, subitem);
54  }
55  if (json != NULL) cJSON_Delete(json);
56  }
57 }
char * cJSON_Print(cJSON *item)
Definition: cJSON.c:346
cJSON * cJSON_GetObjectItem(cJSON *object, const char *string)
Definition: cJSON.c:666
int valueint
Definition: cJSON.h:57
void cJSON_Delete(cJSON *c)
Definition: cJSON.c:81
cJSON * cJSON_GetArrayItem(cJSON *array, int item)
Definition: cJSON.c:665
cJSON * cJSON_Parse(const char *value)
Definition: cJSON.c:343
int cJSON_GetArraySize(cJSON *array)
Definition: cJSON.c:664
Definition: cJSON.h:47

Here is the call graph for this function:

Here is the caller graph for this function:

void test_json_encode ( )

References cJSON_AddItemToObject(), cJSON_AddNumberToObject, cJSON_AddStringToObject, cJSON_CreateIntArray(), cJSON_CreateObject(), cJSON_Delete(), and cJSON_Print().

Referenced by main().

61 {
62  int mdata[4]={116,943,234,38793};
63  cJSON *root;
64  root = cJSON_CreateObject();
65  cJSON_AddNumberToObject(root, "tid", 14672035611234);
66  cJSON_AddItemToObject(root,"data", cJSON_CreateIntArray(mdata,4));
67  cJSON_AddStringToObject(root, "status", "ok");
68  printf("%s\n", cJSON_Print(root));
69  cJSON_Delete(root);
70  /*
71  {
72  "tid": 22,
73  "data": [1,2,3,4],
74  "status": "ok"
75  }
76  */
77 }
#define cJSON_AddStringToObject(object, name, s)
Definition: cJSON.h:149
void cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
Definition: cJSON.c:676
char * cJSON_Print(cJSON *item)
Definition: cJSON.c:346
#define cJSON_AddNumberToObject(object, name, n)
Definition: cJSON.h:148
cJSON * cJSON_CreateObject(void)
Definition: cJSON.c:708
cJSON * cJSON_CreateIntArray(const int *numbers, int count)
Definition: cJSON.c:715
void cJSON_Delete(cJSON *c)
Definition: cJSON.c:81
Definition: cJSON.h:47

Here is the call graph for this function:

Here is the caller graph for this function:

void test_multiple_add_find ( )

References BEGIN, mbtcp_handle_s::connected, mbtcp_handle_s::ctx, enable_syslog, HASH_ADD, HASH_COUNT, HASH_FIND, mbtcp_key_s::ip, mbtcp_handle_s::key, LOG, and mbtcp_key_s::port.

Referenced by main().

102 {
104  // important! initialize to NULL
105  mbtcp_handle_s *servers = NULL;
106 
107  for (int idx = 0; idx < 1000; idx++)
108  {
109  mbtcp_handle_s *handle;
110  handle = (mbtcp_handle_s*)malloc(sizeof(mbtcp_handle_s));
111  memset(handle, 0, sizeof(mbtcp_handle_s));
112  handle->connected = false;
113  strcpy(handle->key.ip, "192.168.10.12");
114  sprintf(handle->key.port, "%d", idx);
115  handle->ctx = modbus_new_tcp_pi(handle->key.ip, handle->key.port);
116  HASH_ADD(hh, servers, key, sizeof(mbtcp_key_s), handle);
117  LOG(enable_syslog, "handle:%d, %p\n", idx, handle);
118  }
119 
120  unsigned int num_users;
121  num_users = HASH_COUNT(servers);
122  LOG(enable_syslog, "there are %u users\n", num_users);
123  LOG(enable_syslog,"==========================\n");
124 
125  for (int idx = 0; idx < 1000; idx++)
126  {
127  mbtcp_handle_s query, *ptr;
128  memset(&query, 0, sizeof(mbtcp_handle_s));
129  strcpy(query.key.ip, "192.168.10.12");
130  sprintf(query.key.port, "%d", idx);
131  HASH_FIND(hh, servers, &query.key, sizeof(mbtcp_key_s), ptr);
132 
133  if (ptr)
134  {
135  LOG(enable_syslog, "found: %d, %s, %s, %p\n", idx, ptr->key.ip, ptr->key.port, ptr);
136  }
137  else
138  {
139  LOG(enable_syslog, "not found: %d\n", idx);
140  }
141  }
142 }
int enable_syslog
Definition: test.c:17
modbus_t * ctx
is connect to modbus slave?
Definition: mb.h:46
void * key
Definition: uthash.h:1069
#define BEGIN(flag)
Definition: log.h:48
char port[50]
IP v4/v6 address or hostname.
Definition: mb.h:34
char ip[50]
Definition: mb.h:33
`structure key` for modbus tcp hash table
Definition: mb.h:31
mbtcp_key_s key
Definition: mb.h:44
#define HASH_COUNT(head)
Definition: uthash.h:1003
bool connected
key
Definition: mb.h:45
#define HASH_ADD(hh, head, fieldname, keylen_in, add)
Definition: uthash.h:317
#define HASH_FIND(hh, head, keyptr, keylen, out)
Definition: uthash.h:131
hashable mbtcp handle type
Definition: mb.h:42
#define LOG(flag, fmt,...)
Definition: log.h:36

Here is the caller graph for this function:

void test_single_add_find ( )

References BEGIN, mbtcp_handle_s::connected, mbtcp_handle_s::ctx, enable_syslog, HASH_ADD, HASH_DEL, HASH_FIND, HASH_ITER, mbtcp_key_s::ip, mbtcp_handle_s::key, LOG, and mbtcp_key_s::port.

Referenced by main().

145 {
147 
148  // important! initialize to NULL
149  mbtcp_handle_s *servers = NULL;
150  mbtcp_handle_s ff, *p, *h1, *h2, *tmp;
151 
152  // server #1
153  h1 = (mbtcp_handle_s*)malloc(sizeof(mbtcp_handle_s));
154  // let alignment bytes being set to zero-value.
155  // ref: https://troydhanson.github.io/uthash/userguide.html#_structure_keys
156  memset(h1, 0, sizeof(h1));
157  h1->connected = false;
158  strcpy(h1->key.ip, "192.168.10.1");
159  strcpy(h1->key.port, "555");
160  h1->ctx = modbus_new_tcp_pi(h1->key.ip, h1->key.port);
161  HASH_ADD(hh, servers, key, sizeof(mbtcp_key_s), h1);
162 
163  // server #2
164  h2 = (mbtcp_handle_s*)malloc(sizeof(mbtcp_handle_s));
165  memset(h2, 0, sizeof(h2));
166  h2->connected = false;
167  strcpy(h2->key.ip, "192.168.10.2");
168  strcpy(h2->key.port, "556");
169  h2->ctx = modbus_new_tcp_pi(h2->key.ip, h2->key.port);
170  HASH_ADD(hh, servers, key, sizeof(mbtcp_key_s), h2);
171 
172  // find #1
173  memset(&ff, 0, sizeof(mbtcp_handle_s));
174  strcpy(ff.key.ip, "192.168.10.1");
175  strcpy(ff.key.port, "555");
176  HASH_FIND(hh, servers, &ff.key, sizeof(mbtcp_key_s), p);
177 
178  if (p)
179  {
180  LOG(enable_syslog, "Found %s, %s\n", p->key.ip, p->key.port);
181  }
182  else
183  {
184  LOG(enable_syslog, "Not found\n");
185  }
186 
187  // find #2
188  memset(&ff, 0, sizeof(mbtcp_handle_s));
189  strcpy(ff.key.ip, "192.168.10.2");
190  strcpy(ff.key.port, "556");
191  HASH_FIND(hh, servers, &ff.key, sizeof(mbtcp_key_s), p);
192 
193  if (p)
194  {
195  LOG(enable_syslog, "Found %s, %s\n", p->key.ip, p->key.port);
196  }
197  else
198  {
199  LOG(enable_syslog, "Not found\n");
200  }
201 
202  // delete
203  HASH_ITER(hh, servers, p, tmp)
204  {
205  HASH_DEL(servers, p); // remove from hash
206  modbus_free(p->ctx); // release modbus context
207  free(p);
208  p = NULL;
209  }
210 }
int enable_syslog
Definition: test.c:17
modbus_t * ctx
is connect to modbus slave?
Definition: mb.h:46
void * key
Definition: uthash.h:1069
#define BEGIN(flag)
Definition: log.h:48
#define HASH_ITER(hh, head, el, tmp)
Definition: uthash.h:997
char port[50]
IP v4/v6 address or hostname.
Definition: mb.h:34
char ip[50]
Definition: mb.h:33
`structure key` for modbus tcp hash table
Definition: mb.h:31
#define HASH_DEL(head, delptr)
Definition: uthash.h:392
mbtcp_key_s key
Definition: mb.h:44
bool connected
key
Definition: mb.h:45
#define HASH_ADD(hh, head, fieldname, keylen_in, add)
Definition: uthash.h:317
#define HASH_FIND(hh, head, keyptr, keylen, out)
Definition: uthash.h:131
hashable mbtcp handle type
Definition: mb.h:42
#define LOG(flag, fmt,...)
Definition: log.h:36

Here is the caller graph for this function:

Variable Documentation