dn_device.c
Go to the documentation of this file.
1 
25 #include "stdint.h"
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #if defined(_USE_WIN_API)
30 #include <winsock2.h>
31 #pragma comment(lib, "wsock32.lib")
32 #elif defined(_USE_LINUX_API)
33 #include <arpa/inet.h>
34 #include <errno.h>
35 #include <sys/time.h>
36 #ifndef strnicmp
37 #define strnicmp strncasecmp
38 #endif
39 #else
40 #include "dn_additional.h"
41 #endif
42 
43 #include "dn_common.h"
44 #include "dn_device.h"
45 
46 #define _STR_ISNUMERIC "0123456789"
47 
48 #define _TYPE_MIN (3)
49 #define _PARA_MAX_COM (7)
50 #define _PARA_MAX_ETH (5)
51 
52 /* Endian switching */
53 #ifndef __LITTLE_ENDIAN__
54 #ifndef __BIG_ENDIAN__
55 #if __BYTE_ORDER == __LITTLE_ENDIAN
56 #define __LITTLE_ENDIAN__
57 #elif __BYTE_ORDER == __BIG_ENDIAN
58 #define __BIG_ENDIAN__
59 #endif
60 #endif
61 #endif
62 
68 static int
69 is_numeric(const char *src)
70 {
71  int len;
72 
73  /* Invalid argument */
74  if ((src == NULL) || ((len = strlen(src)) == 0)) {
75  return 0;
76  }
77 
78  /* Only the head character can be '-' */
79  if (*src == '-') {
80  src++;
81  len--;
82  }
83 
84  return (strspn(src, _STR_ISNUMERIC) == len);
85 }
86 
92 int
93 parse_conn_type(const char *opt)
94 {
95  int type = -1; /* Invalid type */
96 
97  if (opt != NULL && strlen(opt) >= _TYPE_MIN) {
98  if (strnicmp(opt, "tcp", _TYPE_MIN) == 0) {
99  type = CONN_TCP;
100  }
101  else if (strnicmp(opt, "udp", _TYPE_MIN) == 0) {
102  type = CONN_UDP;
103  }
104  else if (strnicmp(opt, "com", _TYPE_MIN) == 0) {
105  type = CONN_COM;
106  }
107  }
108 
109  return type;
110 }
111 
120 HRESULT
121 parse_conn_param_ether(const char *opt, struct CONN_PARAM_ETH *param)
122 {
123  int tmp, type, n = 0;
124  uint32_t uitmp;
125  char *top, *pos, *pos_param[_PARA_MAX_ETH];
126  char *opt_cpy = NULL;
127  HRESULT hr = E_INVALIDARG;
128 
129  if (param != NULL) {
130  type = parse_conn_type(opt);
131  if ((type == CONN_TCP) || (type == CONN_UDP)) {
132  /* Copy connection option string */
133  opt_cpy = (char *) malloc(strlen(opt) + 1);
134 
135  if (opt_cpy == NULL) {
136  hr = E_OUTOFMEMORY;
137  goto exit_proc;
138  }
139 
140  strcpy(opt_cpy, opt);
141 
142  /* Split connection option string */
143  top = opt_cpy;
144  while (1) {
145  if (n >= _PARA_MAX_ETH) {
146  goto exit_proc;
147  }
148 
149  pos_param[n++] = top;
150 
151  pos = strchr(top, ':');
152  if (pos == NULL) {
153  break;
154  }
155 
156  *pos = '\0';
157  top = (pos + 1);
158  }
159 
160  /* Source Port */
161  if (n >= 5) {
162  if (is_numeric(pos_param[4]) == 0) {
163  goto exit_proc;
164  }
165  tmp = atoi(pos_param[4]);
166  if ((tmp < 0) || (tmp != (uint16_t) tmp)) {
167  hr = DISP_E_OVERFLOW;
168  goto exit_proc;
169  }
170  param->src_port = htons(tmp);
171  }
172 
173  /* Source IP */
174  if (n >= 4) {
175  uitmp = inet_addr(pos_param[3]);
176  if (uitmp == htonl(INADDR_NONE)
177  && strcmp(pos_param[3], "255.255.255.255") != 0)
178  {
179  goto exit_proc;
180  }
181  param->src_addr = uitmp;
182  }
183 
184  /* Dest Port */
185  if (n >= 3) {
186  if (is_numeric(pos_param[2]) == 0) {
187  goto exit_proc;
188  }
189  tmp = atoi(pos_param[2]);
190  if ((tmp < 0) || (tmp != (uint16_t) tmp)) {
191  hr = DISP_E_OVERFLOW;
192  goto exit_proc;
193  }
194  param->dst_port = htons(tmp);
195  }
196 
197  /* Dest IP */
198  if (n >= 2) {
199  uitmp = inet_addr(pos_param[1]);
200  if (uitmp == htonl(INADDR_NONE)
201  && strcmp(pos_param[1], "255.255.255.255") != 0)
202  {
203  goto exit_proc;
204  }
205  param->dst_addr = uitmp;
206  }
207 
208  hr = S_OK;
209  }
210  }
211 
212 exit_proc:
213  if (opt_cpy != NULL) {
214  free(opt_cpy);
215  opt_cpy = NULL;
216  }
217 
218  return hr;
219 }
220 
234 HRESULT
235 parse_conn_param_serial(const char *opt, struct CONN_PARAM_COM *param)
236 {
237  int type, n = 0;
238  long tmp;
239  char *top, *pos, *pos_param[_PARA_MAX_COM];
240  char *opt_cpy = NULL;
241  HRESULT hr = E_INVALIDARG;
242 
243  if (param != NULL) {
244  type = parse_conn_type(opt);
245  if (type == CONN_COM) {
246  /* Copy connection option string */
247  opt_cpy = (char *) malloc(strlen(opt) + 1);
248 
249  if (opt_cpy == NULL) {
250  hr = E_OUTOFMEMORY;
251  goto exit_proc;
252  }
253 
254  strcpy(opt_cpy, opt);
255 
256  /* Split connection option string */
257  top = opt_cpy;
258  while (1) {
259  if (n >= _PARA_MAX_COM) {
260  goto exit_proc;
261  }
262 
263  pos_param[n++] = top;
264 
265  pos = strchr(top, ':');
266  if (pos == NULL) {
267  break;
268  }
269 
270  *pos = '\0';
271  top = (pos + 1);
272  }
273 
274  if (n == 4 || n == 5) {
275  goto exit_proc;
276  }
277 
278  /* Flow */
279  if (n >= 7) {
280  if (is_numeric(pos_param[6]) == 0) {
281  goto exit_proc;
282  }
283  tmp = atol(pos_param[6]);
284  if (tmp < 0 || 3 < tmp) {
285  hr = DISP_E_OVERFLOW;
286  goto exit_proc;
287  }
288  param->flow = (char) tmp;
289  }
290 
291  /* Stop bits */
292  if (n >= 6) {
293  tmp = (int) (atof(pos_param[5]) * 10.0);
294 
295  switch (tmp) {
296  case 10:
297  param->stop_bits = ONESTOPBIT;
298  break;
299  case 20:
300  param->stop_bits = TWOSTOPBITS;
301  break;
302  default:
303  goto exit_proc;
304  }
305  }
306 
307  /* Data bits */
308  if (n >= 5) {
309  if (is_numeric(pos_param[4]) == 0) {
310  goto exit_proc;
311  }
312  tmp = atol(pos_param[4]);
313  if (tmp < 5 || 8 < tmp) {
314  hr = DISP_E_OVERFLOW;
315  goto exit_proc;
316  }
317  param->data_bits = (char) tmp;
318  }
319 
320  /* Parity */
321  if (n >= 4) {
322  if (strlen(pos_param[3]) != 1) {
323  goto exit_proc;
324  }
325 
326  switch (*pos_param[3]) {
327  case L'N':
328  case L'n':
329  param->parity = NOPARITY;
330  break;
331  case L'O':
332  case L'o':
333  param->parity = ODDPARITY;
334  break;
335  case L'E':
336  case L'e':
337  param->parity = EVENPARITY;
338  break;
339  default:
340  goto exit_proc;
341  }
342  }
343 
344  /* Baud rate */
345  if (n >= 3) {
346  if (is_numeric(pos_param[2]) == 0) {
347  goto exit_proc;
348  }
349  param->baud_rate = (uint32_t) atol(pos_param[2]);
350  }
351 
352  /* Port */
353  if (n >= 2) {
354  if (is_numeric(pos_param[1]) == 0) {
355  goto exit_proc;
356  }
357  tmp = atol(pos_param[1]);
358  if ((tmp < 0) || (tmp != (int) tmp)) {
359  hr = DISP_E_OVERFLOW;
360  goto exit_proc;
361  }
362  param->port = tmp;
363  }
364 
365  hr = S_OK;
366  }
367  }
368 
369 exit_proc:
370  if (opt_cpy != NULL) {
371  free(opt_cpy);
372  opt_cpy = NULL;
373  }
374 
375  return hr;
376 }
377 
384 HRESULT
385 check_timeout(int sock, uint32_t timeout)
386 {
387  int ret;
388  fd_set fds;
389  struct timeval tv;
390  HRESULT hr = S_OK;
391 
392  if (sock <= 0)
393  return E_HANDLE;
394 
395  FD_ZERO(&fds); FD_SET(sock, &fds);
396 
397  tv.tv_sec = timeout / 1000;
398  tv.tv_usec = (timeout % 1000) * 1000;
399 
400  ret = select(sock + 1, &fds, NULL, NULL, &tv);
401  if (ret == 0) {
402  hr = E_TIMEOUT;
403  }
404  else if (ret < 0) {
405  ret = DNGetLastError();
406  hr = OSERR2HRESULT(ret);
407  }
408 
409  return hr;
410 }
411 
418 HRESULT
419 check_conn_param(const struct CONN_PARAM_COMMON *device, int flag)
420 {
421  if (device == NULL)
422  return E_INVALIDARG;
423 
424  /* Checks the socket */
425  if (device->sock <= 0)
426  return E_HANDLE;
427 
428  /* Checks the connection type */
429  if (flag & CHECK_TYPE_ALL) {
430  if (!(flag & device->type))
431  return E_INVALIDARG;
432  }
433 
434  /* Checks the dn_open function */
435  if ((flag & CHECK_FUNC_OPEN) && (device->dn_open == NULL))
436  return E_INVALIDARG;
437 
438  /* Checks the dn_close function */
439  if ((flag & CHECK_FUNC_CLOSE) && (device->dn_close == NULL))
440  return E_INVALIDARG;
441 
442  /* Checks the dn_send function */
443  if ((flag & CHECK_FUNC_SEND) && (device->dn_send == NULL))
444  return E_INVALIDARG;
445 
446  /* Checks the dn_recv function */
447  if ((flag & CHECK_FUNC_RECV) && (device->dn_recv == NULL))
448  return E_INVALIDARG;
449 
450  /* Checks the dn_timeout function */
451  if ((flag & CHECK_FUNC_TIMEOUT) && (device->dn_set_timeout == NULL))
452  return E_INVALIDARG;
453 
454  /* Checks the dn_clear function */
455  if ((flag & CHECK_FUNC_CLEAR) && (device->dn_clear == NULL))
456  return E_INVALIDARG;
457 
458  return S_OK;
459 }
460 
468 void
469 memcpy_le(void *dst, const void *src, uint32_t len)
470 {
471 #ifdef __BIG_ENDIAN__
472  uint32_t i;
473  uint8_t *pdst;
474  uint8_t *psrc;
475 
476  psrc = (uint8_t *)(src) + len - 1;
477  pdst = (uint8_t *)(dst);
478 
479  for (i = 0; i < len; i++) {
480  *pdst++ = *psrc--;
481  }
482 #else
483  memcpy(dst, src, len);
484 #endif
485 }
486 
494 void
495 memcpy_be(void *dst, const void *src, uint32_t len)
496 {
497 #ifndef __BIG_ENDIAN__
498  uint32_t i;
499  uint8_t *pdst;
500  uint8_t *psrc;
501 
502  psrc = (uint8_t *) (src) + len - 1;
503  pdst = (uint8_t *) (dst);
504 
505  for (i = 0; i < len; i++) {
506  *pdst++ = *psrc--;
507  }
508 #else
509  memcpy(dst, src, len);
510 #endif
511 }
HRESULT check_conn_param(const struct CONN_PARAM_COMMON *device, int flag)
Checks the communication parameters.
Definition: dn_device.c:419
#define _PARA_MAX_ETH
Definition: dn_device.c:50
unsigned uint32_t
Definition: stdint.h:43
HRESULT(* dn_set_timeout)(int sock, uint32_t timeout)
Definition: dn_device.h:181
#define ODDPARITY
A definition for serial communication setting.
Definition: dn_device.h:75
HRESULT(* dn_clear)(int sock, uint32_t timeout)
Definition: dn_device.h:182
long tv_sec
Definition: dn_additional.h:56
HRESULT(* dn_recv)(int sock, char *buf, uint32_t len_buf, uint32_t *len_recved, uint32_t timeout, void *arg)
Definition: dn_device.h:179
#define _PARA_MAX_COM
Definition: dn_device.c:49
#define S_OK
Succeeded.
Definition: dn_common.h:89
unsigned short uint16_t
Definition: stdint.h:41
HRESULT parse_conn_param_serial(const char *opt, struct CONN_PARAM_COM *param)
Parses serial connection parameters.
Definition: dn_device.c:235
#define EVENPARITY
A definition for serial communication setting.
Definition: dn_device.h:81
unsigned char uint8_t
Definition: stdint.h:39
void memcpy_be(void *dst, const void *src, uint32_t len)
Orders to big endian.
Definition: dn_device.c:495
static uint32_t htonl(uint32_t hostlong)
#define E_HANDLE
Failed because the handle is invalid.
Definition: dn_common.h:119
#define INADDR_NONE
Definition: dn_additional.h:37
int parse_conn_type(const char *opt)
Parses and returns the connection type.
Definition: dn_device.c:93
#define E_INVALIDARG
Failed because some arguments are invalid.
Definition: dn_common.h:131
HRESULT(* dn_close)(int *sock)
Definition: dn_device.h:177
long tv_usec
Definition: dn_additional.h:57
int32_t HRESULT
Definition: dn_common.h:61
uint32_t src_addr
Definition: dn_device.h:148
#define DNGetLastError()
A macro that gets last OS error.
static uint32_t inet_addr(const char *addr)
#define E_OUTOFMEMORY
Failed because there is no enough memory space.
Definition: dn_common.h:125
#define DISP_E_OVERFLOW
Failed because out of range.
Definition: dn_common.h:155
#define NOPARITY
A definition for serial communication setting.
Definition: dn_device.h:69
uint16_t src_port
Definition: dn_device.h:149
A type definition for Ethernet connection parameters.
Definition: dn_device.h:144
Common device API file.
#define ONESTOPBIT
A definition for serial communication setting.
Definition: dn_device.h:87
Common API file.
HRESULT check_timeout(int sock, uint32_t timeout)
Checks the communication timeout.
Definition: dn_device.c:385
A type definition for common communication parameters.
Definition: dn_device.h:170
User own API file.
static uint16_t htons(uint16_t hostshort)
HRESULT(* dn_open)(void *param, int *sock)
Definition: dn_device.h:176
uint32_t baud_rate
Definition: dn_device.h:159
static int is_numeric(const char *src)
Checks the argument is numeric or not.
Definition: dn_device.c:69
#define FD_ZERO(fdsetp)
Definition: dn_additional.h:52
#define _STR_ISNUMERIC
Definition: dn_device.c:46
#define FD_SET(fd, fdsetp)
Definition: dn_additional.h:51
uint16_t dst_port
Definition: dn_device.h:147
A type definition for serial connection parameters.
Definition: dn_device.h:156
static int select(int __nfds, fd_set *__restrict __readfds, fd_set *__restrict __writefds, fd_set *__restrict __exceptfds, struct timeval *__restrict __timeout)
HRESULT parse_conn_param_ether(const char *opt, struct CONN_PARAM_ETH *param)
Parses Ethernet connection parameters.
Definition: dn_device.c:121
void memcpy_le(void *dst, const void *src, uint32_t len)
Orders to little endian.
Definition: dn_device.c:469
#define E_TIMEOUT
Failed because the communication timed out.
Definition: dn_common.h:169
uint32_t dst_addr
Definition: dn_device.h:146
HRESULT(* dn_send)(int sock, const char *buf, uint32_t len_buf, void *arg)
Definition: dn_device.h:178
#define OSERR2HRESULT(err)
A macro that returns HREUSLT(0x8091) which means OS error.
Definition: dn_common.h:205
#define _TYPE_MIN
Definition: dn_device.c:48
#define TWOSTOPBITS
A definition for serial communication setting.
Definition: dn_device.h:93


bcap_core
Author(s): DENSO WAVE INCORPORATED
autogenerated on Mon Jun 10 2019 13:12:20