dn_tpcomm.c
Go to the documentation of this file.
1 
23 #include "stdint.h"
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #if defined(_USE_WIN_API)
28 #include <process.h>
29 #include <winsock2.h>
30 #pragma comment(lib, "wsock32.lib")
31 #elif defined(_USE_LINUX_API)
32 #include <arpa/inet.h>
33 #include <pthread.h>
34 #include <sys/ioctl.h>
35 #include <termios.h>
36 #include <unistd.h>
37 #else
38 #include "dn_additional.h"
39 #endif
40 
41 #include "dn_common.h"
42 #include "dn_device.h"
43 #include "dn_udp.h"
44 #include "dn_com.h"
45 #include "dn_thread.h"
46 #include "dn_robotalk.h"
47 #include "dn_tpcomm.h"
48 
49 #ifndef _DEBUG
50 #define _DEBUG (0)
51 #endif
52 
57 #define _TP_CMD_SPECIAL (0x0FFF)
58 
64 #define _TIMER_INTERVAL (10)
65 
71 #define _TPERROR_TIMEOUT (10000)
72 
78 #define _TPLESS_INTERVAL (10000)
79 
85 #define _PING_INTERVAL_CONNECT (300)
86 
92 #define _PING_INTERVAL_TPERROR (1000)
93 
99 #define _TP_INIT_WAIT_CLIENT (100)
100 
106 #define _TP_INIT_WAIT_SERVER (3000)
107 
112 struct TP_FLAGS
113 {
118  volatile uint8_t timer_flag :1;
119  volatile uint8_t recv_flag :1;
120 };
121 
127 {
128  struct CONN_PARAM_COMMON device;
129  struct TP_FLAGS flags;
140  union RTK_PACKET last_packet;
141 };
142 
144 static struct CALL_FUNC_TP m_call_func;
145 
151 static int
153 {
154  int i, index = -1;
155 
156  for (i = 0; i < TP_CONN_MAX; i++) {
157  if (m_conn_param[i].device.sock == 0) {
158  index = i;
159  break;
160  }
161  }
162 
163  return (index + 1);
164 }
165 
172 static struct CONN_PARAM_TP*
173 check_address(int index)
174 {
175  index--;
176 
177  if (index < 0 || TP_CONN_MAX <= index) {
178  return NULL;
179  }
180  else if (m_conn_param[index].device.sock == 0) {
181  return NULL;
182  }
183 
184  return &m_conn_param[index];
185 }
186 
194 static HRESULT
195 tp_callfunc(uint16_t command, const uint8_t *data, uint8_t len_data)
196 {
197  void *param = NULL;
198 
199  /* If the function is not implemented, then returns E_NOTIMPL. */
200  HRESULT hr = E_NOTIMPL;
201 
202  /* Copies call back functions */
203  struct CALL_FUNC_TP func = m_call_func;
204 
205  switch (command) {
206  case _TP_CMD_SPECIAL: /* Changes TP state */
207  if (len_data == sizeof(int)) {
208  if (func.Call_TPState != NULL)
209  hr = func.Call_TPState(*(int *) data);
210  } else {
211  hr = E_INVALIDARG;
212  }
213  break;
214 
215  case TP_CMD_REQ_ID: /* Received request ID command */
216  hr = S_OK;
217  break;
218 
219  case TP_CMD_KEYINFO: /* Received key information command */
220  if (len_data == 8) {
221  param = malloc(sizeof(struct TP_KEY_INFO));
222  if (param != NULL) {
223  /* Maps the received data to TP_KEY_INFO */
224  memcpy_be(param, data, len_data);
225  if (func.Call_TPKeyInfo != NULL)
226  hr = func.Call_TPKeyInfo(*(struct TP_KEY_INFO *) param);
227  } else {
228  hr = E_OUTOFMEMORY;
229  }
230  } else {
231  hr = E_INVALIDARG;
232  }
233  break;
234 
235  case TP_CMD_TOUTCHINFO: /* Received touch information command */
236  if (len_data == 5) {
237  param = malloc(sizeof(struct TP_TOUCH_INFO));
238  if (param != NULL) {
239  /* Maps the received data to TP_TOUCH_INFO */
240  ((struct TP_TOUCH_INFO *) param)->mode = data[0];
241  memcpy_be(&((struct TP_TOUCH_INFO *) param)->pos_x, &data[1], 2);
242  ((struct TP_TOUCH_INFO *) param)->pos_x =
243  ((struct TP_TOUCH_INFO *) param)->pos_x * 8 + 4;
244  memcpy_be(&((struct TP_TOUCH_INFO *) param)->pos_y, &data[3], 2);
245  ((struct TP_TOUCH_INFO *) param)->pos_y =
246  ((struct TP_TOUCH_INFO *) param)->pos_y * 8 + 4;
247  if (func.Call_TPTouchInfo != NULL)
248  hr = func.Call_TPTouchInfo(*(struct TP_TOUCH_INFO *) param);
249  } else {
250  hr = E_OUTOFMEMORY;
251  }
252  } else {
253  hr = E_INVALIDARG;
254  }
255  break;
256 
257 #if (_DEBUG)
258  default:
259  if(func.Call_TPDefault != NULL) {
260  hr = func.Call_TPDefault(command, data, len_data);
261  }
262  break;
263 #else
264  default:
265  break;
266 #endif
267  }
268 
269  if (param != NULL) {
270  free(param);
271  param = NULL;
272  }
273 
274  return hr;
275 }
276 
286 static HRESULT
287 tp_send(struct CONN_PARAM_TP *tp_param, uint16_t command, uint8_t *data,
288  uint8_t len_data)
289 {
290  struct CONN_PARAM_COMMON *device;
291  union RTK_PACKET packet;
292  HRESULT hr;
293 
294  device = &tp_param->device;
295 
296  hr = rtk_param2packet(command, data, len_data, tp_param->from_id,
297  tp_param->to_id, &packet);
298  if (SUCCEEDED(hr)) {
299  hr = rtk_send(device, &packet);
300  if (SUCCEEDED(hr)) {
301  /* Keeps the last sent packet and updates the last send time. */
302  tp_param->last_packet = packet;
303  tp_param->ping_clock = gettimeofday_msec();
304  }
305  }
306 
307  return hr;
308 }
309 
320 static HRESULT
321 tp_recv(struct CONN_PARAM_TP *tp_param, unsigned int retry_nak,
322  uint16_t *command, uint8_t *data, uint8_t *len_data)
323 {
324  unsigned int retry_cnt;
325  uint32_t com_state;
326  struct CONN_PARAM_COMMON *device;
327  union RTK_PACKET packet;
328  HRESULT hr = S_OK;
329 
330  device = &tp_param->device;
331 
332  for (retry_cnt = 0; retry_cnt <= retry_nak; retry_cnt++) {
333  if (device->type == CONN_COM) {
334  com_get_modem_state(device->sock, &com_state);
335  /* If the CTS bit is off then changes TP state to TP_LESS */
336  if (com_state & COM_BITS_CTS) { /* CTS bit is on */
337  /* If the previous CTS bit is off then clears temporary buffers. */
338  if (!tp_param->flags.cts) {
339  device->dn_clear(device->sock, device->timeout);
340  }
341  tp_param->flags.cts = 1;
342  } else { /* CTS bit is off */
343  tp_param->flags.cts = 0;
344  hr = E_ACCESSDENIED;
345  break;
346  }
347  }
348 
349  hr = rtk_recv(device, &packet, tp_param->flags.client, retry_nak);
350  if (FAILED(hr))
351  break;
352 
353  /* If received NAK packet then sends the last sent packet. */
354  if (NativeCommand(packet.command) == RTK_CMD_NAK) {
355  hr = rtk_send(device, &tp_param->last_packet);
356  if (FAILED(hr)) {
357  break;
358  }
359  if (retry_cnt == retry_nak) {
360  hr = E_INVALIDPACKET;
361  }
362  } else {
363  *command = packet.command;
364  *len_data = packet.len;
365  if (packet.len > 0) {
366  memcpy(data, packet.data, packet.len);
367  }
368 
369  /* Updates the receiver ID. */
370  tp_param->to_id = packet.from_id;
371 
372  break;
373  }
374  }
375 
376  return hr;
377 }
378 
393 static HRESULT
394 send_receive(int index, unsigned int retry_timeout, uint16_t command_send,
395  uint8_t *data_send, uint8_t len_send, uint16_t *command_recv,
396  uint8_t *data_recv, uint8_t *len_recv)
397 {
398  unsigned int retry_cnt;
399  HRESULT hr;
400  struct CONN_PARAM_TP *tp_param;
401 
402  tp_param = check_address(index);
403  if (tp_param == NULL)
404  return E_HANDLE;
405 
406  if (tp_param->flags.state == TP_TPLESS)
407  return E_ACCESSDENIED;
408 
409  /* Locks mutex and must not returns this function without end of one. */
410  hr = lock_mutex(&tp_param->mutex, INFINITE);
411  if (FAILED(hr))
412  return hr;
413 
414  for (retry_cnt = 0; retry_cnt <= retry_timeout; retry_cnt++) {
415  /* If the timeout retry count is more than 0, then sets retry flag and counts. */
416  if (retry_cnt > 0) {
417  command_send |= RTK_RETRY_FLAG;
418  command_send += RTK_RETRY_COUNT;
419  }
420 
421  hr = tp_send(tp_param, command_send, data_send, len_send);
422  if (FAILED(hr))
423  break;
424 
425  hr = tp_recv(tp_param, TP_RETRY_NAK, command_recv, data_recv, len_recv);
426  if (SUCCEEDED(hr) || hr != E_TIMEOUT) {
427  if (SUCCEEDED(hr)) {
428  switch (NativeCommand(*command_recv)) {
429  case RTK_CMD_REJ:
430  hr = E_FAIL;
431  break;
432  default:
433  break;
434  }
435  }
436  break;
437  }
438  }
439 
440  /* Unlocks mutex */
441  unlock_mutex(&tp_param->mutex);
442 
443  return hr;
444 }
445 
451 static HRESULT
453 {
454  int resp = 1, tmp_state = tp_param->flags.state;
455  uint32_t cur, diff;
456  union RTK_PACKET packet_send, packet_recv;
457  HRESULT hr;
458 
459  /* Locks mutex and must not returns this function without end of one. */
460  hr = lock_mutex(&tp_param->mutex, INFINITE);
461  if (FAILED(hr))
462  return hr;
463 
464  hr = tp_recv(tp_param, TP_RETRY_NAK, &packet_recv.command, packet_recv.data,
465  &packet_recv.len);
466  if (FAILED(hr)) {
467  /* Updates temporary TP state. */
468  if (hr == E_ACCESSDENIED) {
469  tmp_state = TP_TPLESS;
470  } else {
471  tmp_state = TP_TPERROR;
472  }
473  } else {
474  /* Executes callback functions. */
475  switch (NativeCommand(packet_recv.command)) {
476  /* If the received ROBOTalk command is not executable then do nothing. */
477  case RTK_CMD_REJ:
478  case RTK_CMD_ACK:
479  case RTK_CMD_NAK:
480  resp = 0;
481  break;
482  default:
483  hr = tp_callfunc(NativeCommand(packet_recv.command), packet_recv.data,
484  packet_recv.len);
485  resp = 1;
486  packet_send.command = (SUCCEEDED(hr) ? RTK_CMD_ACK : RTK_CMD_REJ);
487  packet_send.len = 0;
488  break;
489  }
490 
491  /* Responds the result of callback functions */
492  if (resp) {
493  hr = tp_send(tp_param, packet_send.command, packet_send.data,
494  packet_send.len);
495  if (FAILED(hr))
496  goto exit_proc;
497  }
498 
499  if (tmp_state != TP_CONNECT) {
500  /* If received the request ID command, then responds another command. */
501  if (NativeCommand(packet_recv.command) == TP_CMD_REQ_ID) {
502  hr = tp_send(tp_param, TP_CMD_PING, NULL, 0);
503  } else {
504  /* If received the another command, then changes temporary TP state. */
505  tmp_state = TP_CONNECT;
506  }
507  }
508  }
509 
510  /* Updates TP state */
511  cur = gettimeofday_msec();
512  diff = calc_time_diff(tp_param->check_clock, cur);
513 
514  packet_send.command = _TP_CMD_SPECIAL;
515  packet_send.len = sizeof(int);
516 
517  switch (tp_param->flags.state) {
518  case TP_CONNECT:
519  switch (tmp_state) {
520  case TP_CONNECT:
521  tp_param->flags.state = tmp_state;
522  tp_param->check_clock = cur;
523  break;
524  case TP_TPLESS:
525  *(int *) packet_send.data = (int) TP_DISCONNECT;
526  hr = tp_callfunc(packet_send.command, packet_send.data, packet_send.len);
527  tp_param->flags.state = tmp_state;
528  tp_param->check_clock = cur;
529  break;
530  case TP_TPERROR:
531  if (diff > _TPERROR_TIMEOUT) {
532  *(int *) packet_send.data = (int) TP_DISCONNECT;
533  hr = tp_callfunc(packet_send.command, packet_send.data,
534  packet_send.len);
535  tp_param->flags.state = tmp_state;
536  tp_param->check_clock = cur;
537  }
538  break;
539  default:
540  break;
541  }
542  break;
543  case TP_TPLESS:
544  case TP_TPERROR:
545  switch (tmp_state) {
546  case TP_CONNECT:
547  *(int *) packet_send.data = (int) TP_CONNECT;
548  hr = tp_callfunc(packet_send.command, packet_send.data, packet_send.len);
549  tp_param->flags.state = tmp_state;
550  tp_param->check_clock = cur;
551  break;
552  case TP_TPLESS:
553  case TP_TPERROR:
554  if (diff > _TPLESS_INTERVAL) {
555  *(int *) packet_send.data = tmp_state;
556  hr = tp_callfunc(packet_send.command, packet_send.data,
557  packet_send.len);
558  tp_param->flags.state = tmp_state;
559  tp_param->check_clock = cur;
560  }
561  break;
562  default:
563  break;
564  }
565  break;
566  default:
567  break;
568  }
569 
570 exit_proc:
571  /* Unlocks mutex */
572  unlock_mutex(&tp_param->mutex);
573 
574  return hr;
575 }
576 
582 static HRESULT
583 timer_event(struct CONN_PARAM_TP *tp_param)
584 {
585  uint32_t cur, diff;
586  HRESULT hr;
587 
588  /* Locks mutex and must not returns this function without end of one. */
589  hr = lock_mutex(&tp_param->mutex, INFINITE);
590  if (FAILED(hr))
591  return hr;
592 
593  if (tp_param->flags.client) {
594  tp_param->flags.init = 1;
595 
596  /* Sends request ID command until receiving ACK packet */
597  cur = gettimeofday_msec();
598  diff = calc_time_diff(tp_param->init_clock, cur);
599  if ((diff > _TP_INIT_WAIT_CLIENT)
600  && (tp_param->flags.state != TP_CONNECT))
601  {
602 
603  hr = tp_send(tp_param, TP_CMD_REQ_ID, NULL, 0);
604  if (SUCCEEDED(hr))
605  hr = S_FALSE; /* Waits receiving thread */
606 
607  tp_param->init_clock = cur;
608  }
609  } else {
610  if (!tp_param->flags.init) {
611  /* Waits until connecting */
612  cur = gettimeofday_msec();
613  diff = calc_time_diff(tp_param->init_clock, cur);
614  if ((diff > _TP_INIT_WAIT_SERVER)
615  || (tp_param->flags.state == TP_CONNECT))
616  {
617 
618  if (tp_param->flags.state != TP_CONNECT) {
619  hr = tp_send(tp_param, TP_CMD_GET_KEYSTATE, NULL, 0);
620  }
621 
622  tp_param->flags.init = 1;
623  }
624  } else {
625  /* Sends ping packet */
626  cur = gettimeofday_msec();
627  diff = calc_time_diff(tp_param->ping_clock, cur);
628  switch (tp_param->flags.state) {
629  case TP_CONNECT:
630  if (diff > _PING_INTERVAL_CONNECT) {
631  hr = tp_send(tp_param, TP_CMD_PING, NULL, 0);
632  if (SUCCEEDED(hr))
633  hr = S_FALSE; /* Waits receiving thread */
634  }
635  break;
636  case TP_TPERROR:
637  if (diff > _PING_INTERVAL_TPERROR) {
638  hr = tp_send(tp_param, TP_CMD_PING, NULL, 0);
639  if (SUCCEEDED(hr))
640  hr = S_FALSE; /* Waits receiving thread */
641  }
642  break;
643  case TP_TPLESS:
644  break;
645  default:
646  break;
647  }
648  }
649  }
650 
651  /* Unlocks mutex */
652  unlock_mutex(&tp_param->mutex);
653 
654  return hr;
655 }
656 
662 static THRET THTYPE
663 recv_thread(void *arg)
664 {
665 #if !defined(THRET)
666  THRET ret = (THRET)NULL;
667 #endif
668 
669  uint32_t time_sleep;
670  struct CONN_PARAM_TP *tp_param = (struct CONN_PARAM_TP *) arg;
671 
672  tp_param->check_clock = gettimeofday_msec();
673 
674  while (tp_param->flags.recv_flag) {
675  time_sleep = (tp_param->flags.state == TP_CONNECT ? 0 : 300);
676  dn_sleep(time_sleep);
677 
678  receive_execute(tp_param);
679 
680  set_event(&tp_param->evt);
681  dn_sleep(0);
682  }
683 
684  set_event(&tp_param->evt);
685 
686 #if !defined(THRET)
687  return ret;
688 #endif
689 }
690 
696 static THRET THTYPE
697 timer_thread(void *arg)
698 {
699 #if !defined(THRET)
700  THRET ret = (THRET)NULL;
701 #endif
702 
703  uint32_t cur, diff;
704  HRESULT hr;
705  struct CONN_PARAM_TP *tp_param = (struct CONN_PARAM_TP *) arg;
706 
707  tp_param->init_clock = gettimeofday_msec();
708 
709  while (tp_param->flags.timer_flag) {
710  /* Calculates time difference */
711  cur = gettimeofday_msec();
712  diff = calc_time_diff(tp_param->timer_clock, cur);
713 
714  /* Makes timer interval */
715  if (_TIMER_INTERVAL > diff) {
716  dn_sleep(_TIMER_INTERVAL - diff);
717  }
718 
719  /* Sets last event time */
720  tp_param->timer_clock = gettimeofday_msec();
721 
722  hr = timer_event(tp_param);
723 
724  /* Waits receiving thread */
725  if (hr == S_FALSE) {
726  reset_event(&tp_param->evt);
727  wait_event(&tp_param->evt, INFINITE);
728  }
729  }
730 
731 #if !defined(THRET)
732  return ret;
733 #endif
734 }
735 
736 HRESULT
737 TPComm_SetCallFunc(const struct CALL_FUNC_TP *func)
738 {
739  if (func == NULL)
740  return E_INVALIDARG;
741 
742  m_call_func = *func;
743 
744  return S_OK;
745 }
746 
747 HRESULT
748 TPComm_Open(const char *connect, uint32_t timeout, int client, int *pfd)
749 {
750  int index, *sock;
751  HRESULT hr;
752  void *conn_param;
753  struct CONN_PARAM_ETH eth_param =
754  { inet_addr("127.0.0.1"), htons(5007), htonl(INADDR_ANY), 0 };
755  struct CONN_PARAM_COM com_param =
756  { 1, 38400, NOPARITY, 8, ONESTOPBIT, 0 };
757  struct CONN_PARAM_TP *tp_param;
758  struct CONN_PARAM_COMMON *device;
759  struct sockaddr_in *paddr;
760 
761  if (connect == NULL || pfd == NULL)
762  return E_INVALIDARG;
763 
764  index = find_open_address();
765  if (index == 0)
766  return E_MAX_OBJECT;
767 
768  tp_param = &m_conn_param[index - 1];
769  device = &tp_param->device;
770 
771  /* Initializes connection parameters */
772  device->type = parse_conn_type(connect);
773  switch (device->type) {
774  case CONN_UDP:
775  hr = parse_conn_param_ether(connect, &eth_param);
776  conn_param = &eth_param;
777  paddr = (struct sockaddr_in *) malloc(sizeof(struct sockaddr_in));
778  if (paddr == NULL) {
779  hr = E_OUTOFMEMORY;
780  break;
781  }
782  paddr->sin_addr.s_addr = eth_param.dst_addr;
783  paddr->sin_port = eth_param.dst_port;
784  paddr->sin_family = AF_INET;
785  device->arg = (void *) paddr;
786  device->dn_open = &udp_open;
787  device->dn_close = &udp_close;
788  device->dn_send = &udp_send;
789  device->dn_recv = &udp_recv;
790  device->dn_set_timeout = &udp_set_timeout;
791  device->dn_clear = &udp_clear;
792  break;
793  case CONN_COM:
794  hr = parse_conn_param_serial(connect, &com_param);
795  conn_param = &com_param;
796  device->arg = NULL;
797  device->dn_open = &com_open;
798  device->dn_close = &com_close;
799  device->dn_send = &com_send;
800  device->dn_recv = &com_recv;
801  device->dn_set_timeout = &com_set_timeout;
802  device->dn_clear = &com_clear;
803  break;
804  default:
805  hr = E_INVALIDARG;
806  break;
807  }
808 
809  if (FAILED(hr)) {
810  if (device->arg != NULL) {
811  free(device->arg);
812  device->arg = NULL;
813  }
814  memset(tp_param, 0, sizeof(struct CONN_PARAM_TP));
815  return hr;
816  }
817 
818  /* Initializes mutex */
819  hr = initialize_mutex(&tp_param->mutex);
820  if (FAILED(hr)) {
821  if (device->arg != NULL) {
822  free(device->arg);
823  device->arg = NULL;
824  }
825  return hr;
826  }
827 
828  /* Initializes event */
829  hr = create_event(&tp_param->evt, 0, 0);
830  if (FAILED(hr)) {
831  release_mutex(&tp_param->mutex);
832  if (device->arg != NULL) {
833  free(device->arg);
834  device->arg = NULL;
835  }
836  memset(tp_param, 0, sizeof(struct CONN_PARAM_TP));
837  return hr;
838  }
839 
840  /* Opens connection */
841  sock = &device->sock;
842  hr = device->dn_open(conn_param, sock);
843  if (FAILED(hr)) {
844  release_mutex(&tp_param->mutex);
845  destroy_event(&tp_param->evt);
846  if (device->arg != NULL) {
847  free(device->arg);
848  device->arg = NULL;
849  }
850  memset(tp_param, 0, sizeof(struct CONN_PARAM_TP));
851  return hr;
852  }
853 
854  /* Sets timeout */
855  hr = TPComm_SetTimeout(index, timeout);
856  if (FAILED(hr)) {
857  TPComm_Close(&index);
858  return hr;
859  }
860 
861  /* Sets parameters */
862  tp_param->flags.client = (client ? 1 : 0);
863  tp_param->flags.init = 0;
864  tp_param->flags.state = TP_TPLESS;
865  tp_param->flags.cts = 0;
866  tp_param->from_id = (client ? TP_ID_CLIENT : TP_ID_SERVER);
867  tp_param->to_id = TP_ID_CLIENT;
868 
869  /* Sets initialize time */
870  tp_param->timer_clock = tp_param->ping_clock = tp_param->init_clock =
871  tp_param->check_clock = gettimeofday_msec();
872 
873  /* Begins thread */
874  tp_param->flags.timer_flag = 1;
875  begin_thread(&tp_param->timer_thread, &timer_thread, tp_param);
876  tp_param->flags.recv_flag = 1;
877  begin_thread(&tp_param->recv_thread, &recv_thread, tp_param);
878 
879  *pfd = index;
880 
881  return S_OK;
882 }
883 
884 HRESULT
885 TPComm_Close(int *pfd)
886 {
887  int index, *sock;
888  struct CONN_PARAM_TP *tp_param;
889  struct CONN_PARAM_COMMON *device;
890 
891  if (pfd == NULL)
892  return E_HANDLE;
893 
894  index = *pfd;
895 
896  tp_param = check_address(index);
897  if (tp_param == NULL)
898  return E_HANDLE;
899 
900  device = &tp_param->device;
901  sock = &device->sock;
902 
903  /* Ends timer thread first */
904  tp_param->flags.timer_flag = 0;
905  exit_thread(tp_param->timer_thread);
906 
907  /* Ends receive thread second */
908  tp_param->flags.recv_flag = 0;
909  exit_thread(tp_param->recv_thread);
910 
911  /* Destroys event */
912  destroy_event(&tp_param->evt);
913 
914  /* Releases mutex */
915  release_mutex(&tp_param->mutex);
916 
917  /* Closes connection */
918  device->dn_close(sock);
919 
920  /* Releases argument */
921  if (device->arg != NULL) {
922  free(device->arg);
923  device->arg = NULL;
924  }
925 
926  /* Resets connection parameters */
927  memset(tp_param, 0, sizeof(struct CONN_PARAM_TP));
928 
929  *pfd = 0;
930 
931  return S_OK;
932 }
933 
934 HRESULT
936 {
937  int *sock;
938  HRESULT hr;
939  struct CONN_PARAM_TP *tp_param;
940  struct CONN_PARAM_COMMON *device;
941 
942  tp_param = check_address(fd);
943  if (tp_param == NULL)
944  return E_HANDLE;
945 
946  device = &tp_param->device;
947  sock = &device->sock;
948 
949  /* Locks mutex and must not returns this function without end of one. */
950  hr = lock_mutex(&tp_param->mutex, INFINITE);
951  if (FAILED(hr))
952  return hr;
953 
954  hr = device->dn_set_timeout(*sock, timeout);
955  if (SUCCEEDED(hr)) {
956  device->timeout = timeout;
957  }
958 
959  /* Unlocks mutex */
960  unlock_mutex(&tp_param->mutex);
961 
962  return hr;
963 }
964 
965 HRESULT
967 {
968  struct CONN_PARAM_TP *tp_param;
969  struct CONN_PARAM_COMMON *device;
970 
971  tp_param = check_address(fd);
972  if (tp_param == NULL)
973  return E_HANDLE;
974 
975  if (timeout == NULL)
976  return E_INVALIDARG;
977 
978  device = &tp_param->device;
979  *timeout = device->timeout;
980 
981  return S_OK;
982 }
983 
984 HRESULT
986 {
987  struct CONN_PARAM_TP *tp_param;
988 
989  tp_param = check_address(fd);
990  if (tp_param == NULL)
991  return E_HANDLE;
992 
993  if (state == NULL)
994  return E_INVALIDARG;
995 
996  *state = tp_param->flags.state;
997 
998  return S_OK;
999 }
1000 
1001 HRESULT
1002 TPComm_BEEP(int fd, int16_t time)
1003 {
1004  uint8_t data[2];
1005  HRESULT hr;
1006  union RTK_PACKET packet;
1007 
1008  memcpy_be(data, &time, 2);
1009 
1010  hr = send_receive(fd, TP_RETRY_TIMEOUT, TP_CMD_BEEP, data, 2, &packet.command,
1011  packet.data, &packet.len);
1012 
1013  return hr;
1014 }
1015 
1016 HRESULT
1018 {
1019  uint8_t data[2];
1020  uint16_t command;
1021  HRESULT hr;
1022  union RTK_PACKET packet;
1023 
1024  switch (state) {
1025  case LED_OFF:
1026  command = TP_CMD_LED_OFF;
1027  break;
1028  case LED_ON:
1029  command = TP_CMD_LED_ON;
1030  break;
1031  case LED_FLASH:
1032  command = TP_CMD_LED_FLASH;
1033  break;
1034  default:
1035  return E_INVALIDARG;
1036  }
1037 
1038  memcpy_be(data, &number, 2);
1039 
1040  hr = send_receive(fd, TP_RETRY_TIMEOUT, command, data, 2, &packet.command,
1041  packet.data, &packet.len);
1042 
1043  return hr;
1044 }
1045 
1046 HRESULT
1047 TPComm_LCD(int fd, int16_t contrast)
1048 {
1049  uint8_t data[2];
1050  HRESULT hr;
1051  union RTK_PACKET packet;
1052 
1053  memcpy_be(data, &contrast, 2);
1054 
1055  hr = send_receive(fd, TP_RETRY_TIMEOUT, TP_CMD_LCD, data, 2, &packet.command,
1056  packet.data, &packet.len);
1057 
1058  return hr;
1059 }
1060 
1061 HRESULT
1062 TPComm_DrawMiniTP(int fd, VARIANT commands)
1063 {
1064  HRESULT hr;
1065  uint32_t ulPosCmd, ulNumCmd, ulNumPar, ulPosPar, ulPosSub, ulNumSub, ulLenStr,
1066  ulLenCmd;
1067  uint8_t chSub[4], data[248], len_data = 0;
1068  VARIANT *vntCmd, vntPar[2], vntSub[5];
1069  union RTK_PACKET packet;
1070 
1071  memset(vntPar, 0, sizeof(vntPar));
1072  memset(vntSub, 0, sizeof(vntSub));
1073 
1074  if (commands.vt != (VT_VARIANT | VT_ARRAY)) {
1075  return E_INVALIDARG;
1076  }
1077 
1078  ulNumCmd = (int32_t) commands.parray->rgsabound[0].cElements;
1079 
1080  SafeArrayAccessData(commands.parray, (void**) &vntCmd);
1081  for (ulPosCmd = 0; ulPosCmd < ulNumCmd; ulPosCmd++) {
1082  if (!(vntCmd[ulPosCmd].vt & VT_ARRAY)) {
1083  hr = E_INVALIDARG;
1084  goto exit_proc;
1085  }
1086 
1087  for (ulPosPar = 0; ulPosPar < 2; ulPosPar++) {
1088  VariantClear(&vntPar[ulPosPar]);
1089  }
1090 
1091  for (ulPosSub = 0; ulPosSub < 5; ulPosSub++) {
1092  VariantClear(&vntSub[ulPosSub]);
1093  }
1094 
1095  ulNumPar = ChangeVarType(vntCmd[ulPosCmd], VT_VARIANT, vntPar, 2);
1096  if (ulNumPar < 2) {
1097  hr = E_INVALIDARG;
1098  goto exit_proc;
1099  }
1100 
1101  hr = VariantChangeType(&vntPar[0], &vntPar[0], 0, VT_UI1);
1102  if (FAILED(hr))
1103  goto exit_proc;
1104 
1105  switch (vntPar[0].bVal) {
1106  case COLOR_FG:
1107  case COLOR_BG:
1108  case COLOR_FILL:
1109  if (248 < len_data + 3) {
1110  hr = E_OUTOFMEMORY;
1111  goto exit_proc;
1112  }
1113 
1114  hr = VariantChangeType(&vntPar[1], &vntPar[1], 0, VT_UI1);
1115  if (FAILED(hr))
1116  goto exit_proc;
1117 
1118  data[len_data] = vntPar[0].bVal;
1119  data[len_data + 1] = 0x1;
1120  data[len_data + 2] = vntPar[1].bVal;
1121  len_data += 3;
1122 
1123  break;
1124  case DRAW_STRING:
1125  ulNumSub = ChangeVarType(vntPar[1], VT_VARIANT, vntSub, 5);
1126  if (ulNumSub < 5) {
1127  hr = E_INVALIDARG;
1128  goto exit_proc;
1129  }
1130 
1131  for (ulPosSub = 0; ulPosSub < 5; ulPosSub++) {
1132  hr = VariantChangeType(&vntSub[ulPosSub], &vntSub[ulPosSub], 0,
1133  (ulPosSub == 0 ? VT_BSTR : VT_UI1));
1134  if (FAILED(hr))
1135  goto exit_proc;
1136  }
1137 
1138  ulLenStr = SysStringLen(vntSub[0].bstrVal);
1139  ulLenCmd = ulLenStr * 2 + 5;
1140 
1141  if (248 < (len_data + ulLenCmd + 2)) {
1142  hr = E_OUTOFMEMORY;
1143  goto exit_proc;
1144  }
1145 
1146  data[len_data] = vntPar[0].bVal;
1147  data[len_data + 1] = (uint8_t) ulLenCmd;
1148  data[len_data + 2] = vntSub[1].bVal;
1149  data[len_data + 3] = vntSub[2].bVal;
1150  data[len_data + 4] = vntSub[3].bVal;
1151  data[len_data + 5] = (uint8_t) ulLenStr;
1152  wcstombs((char*) &data[len_data + 6], vntSub[0].bstrVal, ulLenStr);
1153  data[len_data + ulLenStr + 6] = '\0';
1154  memset(&data[len_data + ulLenStr + 7], vntSub[4].bVal, ulLenStr);
1155  len_data += (uint8_t) (ulLenCmd + 2);
1156 
1157  break;
1158  case DRAW_LINE:
1159  case DRAW_RECT:
1160  ulNumSub = ChangeVarType(vntPar[1], VT_UI1, chSub, 4);
1161  if (ulNumSub < 4) {
1162  hr = E_INVALIDARG;
1163  goto exit_proc;
1164  }
1165 
1166  if (248 < len_data + 6) {
1167  hr = E_OUTOFMEMORY;
1168  goto exit_proc;
1169  }
1170 
1171  data[len_data] = vntPar[0].bVal;
1172  data[len_data + 1] = 0x04;
1173  memcpy(&data[len_data + 2], chSub, 4);
1174  len_data += 6;
1175 
1176  break;
1177  default:
1178  hr = E_INVALIDARG;
1179  goto exit_proc;
1180  }
1181  }
1182 
1183  if (SUCCEEDED(hr)) {
1184  hr = send_receive(fd, TP_RETRY_TIMEOUT, TP_CMD_MTP_DRAW, data, len_data,
1185  &packet.command, packet.data, &packet.len);
1186  }
1187 
1188 exit_proc:
1189  SafeArrayUnaccessData(commands.parray);
1190 
1191  for (ulPosPar = 0; ulPosPar < 2; ulPosPar++) {
1192  VariantClear(&vntPar[ulPosPar]);
1193  }
1194 
1195  for (ulPosSub = 0; ulPosSub < 5; ulPosSub++) {
1196  VariantClear(&vntSub[ulPosSub]);
1197  }
1198 
1199  return hr;
1200 }
1201 
1202 HRESULT
1203 TPComm_DrawString(int fd, BSTR bstr, uint8_t pos_x, uint8_t pos_y, uint8_t size,
1204  uint8_t attr, uint8_t color_fg, uint8_t color_bg)
1205 {
1206  HRESULT hr;
1207  uint8_t *bData;
1208  VARIANT vntCmd, *vntPar, *vntSub1, *vntSub2;
1209 
1210  VariantInit(&vntCmd);
1211 
1212  vntCmd.vt = VT_VARIANT | VT_ARRAY;
1213  vntCmd.parray = SafeArrayCreateVector(VT_VARIANT, 0, 3);
1214  SafeArrayAccessData(vntCmd.parray, (void **) &vntPar);
1215 
1216  // Sets COLOR_FG
1217  vntPar[0].vt = VT_UI1 | VT_ARRAY;
1218  vntPar[0].parray = SafeArrayCreateVector(VT_UI1, 0, 2);
1219  SafeArrayAccessData(vntPar[0].parray, (void **) &bData);
1220  bData[0] = COLOR_FG;
1221  bData[1] = color_fg;
1222  SafeArrayUnaccessData(vntPar[0].parray);
1223 
1224  // Sets COLOR_BG
1225  vntPar[1].vt = VT_UI1 | VT_ARRAY;
1226  vntPar[1].parray = SafeArrayCreateVector(VT_UI1, 0, 2);
1227  SafeArrayAccessData(vntPar[1].parray, (void **) &bData);
1228  bData[0] = COLOR_BG;
1229  bData[1] = color_bg;
1230  SafeArrayUnaccessData(vntPar[1].parray);
1231 
1232  // Sets DRAW_STRING
1233  vntPar[2].vt = VT_VARIANT | VT_ARRAY;
1234  vntPar[2].parray = SafeArrayCreateVector(VT_VARIANT, 0, 2);
1235  SafeArrayAccessData(vntPar[2].parray, (void **) &vntSub1);
1236  vntSub1[0].vt = VT_UI1;
1237  vntSub1[0].bVal = DRAW_STRING;
1238  vntSub1[1].vt = VT_VARIANT | VT_ARRAY;
1239  vntSub1[1].parray = SafeArrayCreateVector(VT_VARIANT, 0, 5);
1240  SafeArrayAccessData(vntSub1[1].parray, (void **) &vntSub2);
1241  vntSub2[0].vt = VT_BSTR;
1242  vntSub2[0].bstrVal = SysAllocString(bstr);
1243  vntSub2[1].vt = VT_UI1;
1244  vntSub2[1].bVal = pos_x;
1245  vntSub2[2].vt = VT_UI1;
1246  vntSub2[2].bVal = pos_y;
1247  vntSub2[3].vt = VT_UI1;
1248  vntSub2[3].bVal = size;
1249  vntSub2[4].vt = VT_UI1;
1250  vntSub2[4].bVal = attr;
1251  SafeArrayUnaccessData(vntSub1[1].parray);
1252  SafeArrayUnaccessData(vntPar[2].parray);
1253 
1254  SafeArrayUnaccessData(vntCmd.parray);
1255 
1256  hr = TPComm_DrawMiniTP(fd, vntCmd);
1257 
1258  VariantClear(&vntCmd);
1259 
1260  return hr;
1261 }
1262 
1263 HRESULT
1264 TPComm_DrawLine(int fd, uint8_t start_x, uint8_t start_y, uint8_t end_x,
1265  uint8_t end_y, uint8_t color_fg)
1266 {
1267  HRESULT hr;
1268  uint8_t *bData;
1269  VARIANT vntCmd, *vntPar, *vntSub1;
1270 
1271  VariantInit(&vntCmd);
1272 
1273  vntCmd.vt = VT_VARIANT | VT_ARRAY;
1274  vntCmd.parray = SafeArrayCreateVector(VT_VARIANT, 0, 2);
1275  SafeArrayAccessData(vntCmd.parray, (void **) &vntPar);
1276 
1277  // Sets COLOR_FG
1278  vntPar[0].vt = VT_UI1 | VT_ARRAY;
1279  vntPar[0].parray = SafeArrayCreateVector(VT_UI1, 0, 2);
1280  SafeArrayAccessData(vntPar[0].parray, (void **) &bData);
1281  bData[0] = COLOR_FG;
1282  bData[1] = color_fg;
1283  SafeArrayUnaccessData(vntPar[0].parray);
1284 
1285  // Sets DRAW_LINE
1286  vntPar[1].vt = VT_VARIANT | VT_ARRAY;
1287  vntPar[1].parray = SafeArrayCreateVector(VT_VARIANT, 0, 2);
1288  SafeArrayAccessData(vntPar[1].parray, (void **) &vntSub1);
1289  vntSub1[0].vt = VT_UI1;
1290  vntSub1[0].bVal = DRAW_LINE;
1291  vntSub1[1].vt = VT_UI1 | VT_ARRAY;
1292  vntSub1[1].parray = SafeArrayCreateVector(VT_UI1, 0, 4);
1293  SafeArrayAccessData(vntSub1[1].parray, (void **) &bData);
1294  bData[0] = start_x;
1295  bData[1] = start_y;
1296  bData[2] = end_x;
1297  bData[3] = end_y;
1298  SafeArrayUnaccessData(vntSub1[1].parray);
1299  SafeArrayUnaccessData(vntPar[1].parray);
1300 
1301  SafeArrayUnaccessData(vntCmd.parray);
1302 
1303  hr = TPComm_DrawMiniTP(fd, vntCmd);
1304 
1305  VariantClear(&vntCmd);
1306 
1307  return hr;
1308 }
1309 
1310 HRESULT
1311 TPComm_DrawRectangle(int fd, uint8_t start_x, uint8_t start_y, uint8_t end_x,
1312  uint8_t end_y, uint8_t color_fg, uint8_t color_bg)
1313 {
1314  HRESULT hr;
1315  uint8_t *bData;
1316  VARIANT vntCmd, *vntPar, *vntSub1;
1317 
1318  VariantInit(&vntCmd);
1319 
1320  vntCmd.vt = VT_VARIANT | VT_ARRAY;
1321  vntCmd.parray = SafeArrayCreateVector(VT_VARIANT, 0, 3);
1322  SafeArrayAccessData(vntCmd.parray, (void **) &vntPar);
1323 
1324  // Sets COLOR_FG
1325  vntPar[0].vt = VT_UI1 | VT_ARRAY;
1326  vntPar[0].parray = SafeArrayCreateVector(VT_UI1, 0, 2);
1327  SafeArrayAccessData(vntPar[0].parray, (void **) &bData);
1328  bData[0] = COLOR_FG;
1329  bData[1] = color_fg;
1330  SafeArrayUnaccessData(vntPar[0].parray);
1331 
1332  // Sets COLOR_BG
1333  vntPar[1].vt = VT_UI1 | VT_ARRAY;
1334  vntPar[1].parray = SafeArrayCreateVector(VT_UI1, 0, 2);
1335  SafeArrayAccessData(vntPar[1].parray, (void **) &bData);
1336  bData[0] = COLOR_BG;
1337  bData[1] = color_bg;
1338  SafeArrayUnaccessData(vntPar[1].parray);
1339 
1340  // Sets DRAW_RECT
1341  vntPar[2].vt = VT_VARIANT | VT_ARRAY;
1342  vntPar[2].parray = SafeArrayCreateVector(VT_VARIANT, 0, 2);
1343  SafeArrayAccessData(vntPar[2].parray, (void **) &vntSub1);
1344  vntSub1[0].vt = VT_UI1;
1345  vntSub1[0].bVal = DRAW_RECT;
1346  vntSub1[1].vt = VT_UI1 | VT_ARRAY;
1347  vntSub1[1].parray = SafeArrayCreateVector(VT_UI1, 0, 4);
1348  SafeArrayAccessData(vntSub1[1].parray, (void **) &bData);
1349  bData[0] = start_x;
1350  bData[1] = start_y;
1351  bData[2] = end_x;
1352  bData[3] = end_y;
1353  SafeArrayUnaccessData(vntSub1[1].parray);
1354  SafeArrayUnaccessData(vntPar[2].parray);
1355 
1356  SafeArrayUnaccessData(vntCmd.parray);
1357 
1358  hr = TPComm_DrawMiniTP(fd, vntCmd);
1359 
1360  VariantClear(&vntCmd);
1361 
1362  return hr;
1363 }
HRESULT TPComm_DrawString(int fd, BSTR bstr, uint8_t pos_x, uint8_t pos_y, uint8_t size, uint8_t attr, uint8_t color_fg, uint8_t color_bg)
Draws a string to Mini TP.
Definition: dn_tpcomm.c:1203
#define TP_CONN_MAX
A definition for the maximum count of TP connections.
Definition: dn_tpcomm.h:46
#define S_FALSE
Succeeded but some processes may remain.
Definition: dn_common.h:95
HRESULT TPComm_Open(const char *connect, uint32_t timeout, int client, int *pfd)
Starts TP communication.
Definition: dn_tpcomm.c:748
_DN_EXP_ROBOTALK HRESULT rtk_param2packet(uint16_t command, const uint8_t *data, uint8_t len_data, uint8_t from_id, uint8_t to_id, union RTK_PACKET *packet)
Creates a ROBOTalk packet.
Definition: dn_robotalk.c:86
static struct CALL_FUNC_TP m_call_func
Definition: dn_tpcomm.c:144
#define _TPLESS_INTERVAL
A definition for the interval of the TP less event.
Definition: dn_tpcomm.c:78
_DN_EXP_THREAD HRESULT wait_event(EVENT *pevt, uint32_t timeout)
Waits a event.
Definition: dn_thread.c:361
_DN_EXP_THREAD HRESULT unlock_mutex(MUTEX *pmutex)
Unlocks mutex handle.
Definition: dn_thread.c:188
short int16_t
Definition: stdint.h:40
#define RTK_RETRY_COUNT
A definition for the ROBOTalk command. The upper 3bits mean the retry count.
Definition: dn_robotalk.h:123
A type definition for the ROBOTalk packet.
Definition: dn_robotalk.h:143
unsigned uint32_t
Definition: stdint.h:43
uint8_t len
Definition: dn_robotalk.h:148
HRESULT TPComm_Close(int *pfd)
Ends TP communication.
Definition: dn_tpcomm.c:885
HRESULT(* dn_set_timeout)(int sock, uint32_t timeout)
Definition: dn_device.h:181
#define TP_CMD_LCD
A definition for the ROBOTalk command which means requests change LCD.
Definition: dn_tpcomm.h:130
_DN_EXP_COM HRESULT com_clear(int sock, uint32_t timeout)
Clears the received buffer.
Definition: dn_com.c:508
UDP API file.
#define TP_CMD_GET_KEYSTATE
A definition for the ROBOTalk command which means requests key state.
Definition: dn_tpcomm.h:118
static struct CONN_PARAM_TP * check_address(int index)
Checks whether the index has been used or not.
Definition: dn_tpcomm.c:173
#define COM_BITS_CTS
A definition for CTS flag.
HRESULT TPComm_BEEP(int fd, int16_t time)
TP beeps.
Definition: dn_tpcomm.c:1002
TP flag objects.
Definition: dn_tpcomm.c:112
uint32_t ping_clock
Definition: dn_tpcomm.c:133
HRESULT(* dn_clear)(int sock, uint32_t timeout)
Definition: dn_device.h:182
uint16_t pos_x
Definition: dn_tpcomm.h:282
_DN_EXP_COMMON HRESULT SafeArrayUnaccessData(SAFEARRAY *psa)
Unaccesses the SAFEARRAY.
Definition: dn_common.c:353
uint32_t timeout
Definition: dn_device.h:174
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
uint8_t bVal
Definition: dn_common.h:321
HRESULT TPComm_DrawRectangle(int fd, uint8_t start_x, uint8_t start_y, uint8_t end_x, uint8_t end_y, uint8_t color_fg, uint8_t color_bg)
Draws a rectangle to Mini TP.
Definition: dn_tpcomm.c:1311
#define _TP_INIT_WAIT_SERVER
A definition for the wait time of the initialization when the server mode.
Definition: dn_tpcomm.c:106
uint16_t pos_y
Definition: dn_tpcomm.h:283
_DN_EXP_COMMON uint32_t ChangeVarType(VARIANT varSrc, uint16_t vt, void *pDest, uint32_t dwSize)
Changes the variant to destination value with the indicated type.
uint8_t to_id
Definition: dn_tpcomm.c:131
#define S_OK
Succeeded.
Definition: dn_common.h:89
uint32_t cElements
Definition: dn_common.h:285
_DN_EXP_THREAD HRESULT lock_mutex(MUTEX *pmutex, uint32_t timeout)
Locks mutex handle.
Definition: dn_thread.c:145
uint8_t init
Definition: dn_tpcomm.c:115
#define TP_CMD_LED_ON
A definition for the ROBOTalk command which means requests LED on.
Definition: dn_tpcomm.h:100
_DN_EXP_THREAD HRESULT create_event(EVENT *pevt, int reset_mode, int init_signal)
Creates a event object.
Definition: dn_thread.c:220
wchar_t * BSTR
Definition: dn_common.h:239
#define TP_CMD_LED_FLASH
A definition for the ROBOTalk command which means requests LED flash.
Definition: dn_tpcomm.h:112
unsigned short uint16_t
Definition: stdint.h:41
_DN_EXP_UDP HRESULT udp_send(int sock, const char *buf, uint32_t len_buf, void *arg)
Sends UDP packet.
Definition: dn_udp.c:94
HRESULT(* Call_TPState)(int state)
Definition: dn_tpcomm.h:294
#define TP_CMD_LED_OFF
A definition for the ROBOTalk command which means requests LED off.
Definition: dn_tpcomm.h:106
_DN_EXP_UDP HRESULT udp_clear(int sock, uint32_t timeout)
Clears the received buffer.
Definition: dn_udp.c:185
#define _TIMER_INTERVAL
A definition for the interval of the timer event.
Definition: dn_tpcomm.c:64
_DN_EXP_COM HRESULT com_get_modem_state(int sock, uint32_t *state)
Gets the serial port pin status.
Definition: dn_com.c:581
uint32_t check_clock
Definition: dn_tpcomm.c:135
#define _TP_INIT_WAIT_CLIENT
A definition for the wait time of the initialization when the client mode.
Definition: dn_tpcomm.c:99
unsigned char uint8_t
Definition: stdint.h:39
BSTR bstrVal
Definition: dn_common.h:318
Thread and mutex API file.
struct CONN_PARAM_COMMON device
Definition: dn_tpcomm.c:128
#define FAILED(hr)
A macro that returns TRUE/FALSE. If hr is less than zero, then returns TRUE.
Definition: dn_common.h:77
#define RTK_CMD_NAK
A definition for the ROBOTalk command which means negative acknowledge.
Definition: dn_robotalk.h:81
_DN_EXP_COM HRESULT com_open(void *param, int *sock)
Opens serial port.
Definition: dn_com.c:360
#define TP_CMD_TOUTCHINFO
A definition for the ROBOTalk command which means requests or raised touch event. ...
Definition: dn_tpcomm.h:88
uint8_t state
Definition: dn_tpcomm.c:116
HRESULT TPComm_LED(int fd, int16_t number, int16_t state)
Changes TP LED.
Definition: dn_tpcomm.c:1017
static uint32_t htonl(uint32_t hostlong)
#define E_HANDLE
Failed because the handle is invalid.
Definition: dn_common.h:119
#define calc_time_diff(start, end)
A macro that calculates the time difference between start and end.
Definition: dn_thread.h:199
_DN_EXP_UDP HRESULT udp_set_timeout(int sock, uint32_t timeout)
Sets timeout value to the UDP socket.
Definition: dn_udp.c:173
#define E_ACCESSDENIED
Failed because the resource is not ready.
Definition: dn_common.h:113
#define TP_CMD_KEYINFO
A definition for the ROBOTalk command which means requests or raised key event.
Definition: dn_tpcomm.h:82
int THREAD
#define E_INVALIDARG
Failed because some arguments are invalid.
Definition: dn_common.h:131
HRESULT(* dn_close)(int *sock)
Definition: dn_device.h:177
int MUTEX
volatile uint8_t timer_flag
Definition: dn_tpcomm.c:118
ROBOTalk API file.
HRESULT TPComm_DrawLine(int fd, uint8_t start_x, uint8_t start_y, uint8_t end_x, uint8_t end_y, uint8_t color_fg)
Draws a line to Mini TP.
Definition: dn_tpcomm.c:1264
Callback functions of TP event.
Definition: dn_tpcomm.h:291
_DN_EXP_COM HRESULT com_set_timeout(int sock, uint32_t timeout)
Sets timeout value to the serial socket.
Definition: dn_com.c:489
_DN_EXP_DEVICE HRESULT parse_conn_param_serial(const char *opt, struct CONN_PARAM_COM *param)
Parses serial connection parameters.
Definition: dn_device.c:235
static int find_open_address()
Returns the open address of m_conn_param.
Definition: dn_tpcomm.c:152
static HRESULT tp_callfunc(uint16_t command, const uint8_t *data, uint8_t len_data)
Execute a callback function with the ROBOTalk command.
Definition: dn_tpcomm.c:195
_DN_EXP_COM HRESULT com_recv(int sock, char *buf, uint32_t len_buf, uint32_t *len_recved, uint32_t timeout, void *arg)
Receives serial packet.
Definition: dn_com.c:453
HRESULT TPComm_DrawMiniTP(int fd, VARIANT commands)
Sends Draw Mini TP commands.
Definition: dn_tpcomm.c:1062
_DN_EXP_UDP HRESULT udp_open(void *param, int *sock)
Opens UDP socket.
Definition: dn_udp.c:52
#define E_FAIL
Failed because unspecified error occurs.
Definition: dn_common.h:107
#define TP_CMD_PING
A definition for the ROBOTalk command which means requests check wire.
Definition: dn_tpcomm.h:142
int32_t HRESULT
Definition: dn_common.h:61
#define TP_RETRY_NAK
A definition for the retry count of sending and receiving NAK packet.
Definition: dn_tpcomm.h:52
_DN_EXP_THREAD HRESULT destroy_event(EVENT *pevt)
Destroys a event object.
Definition: dn_thread.c:251
union RTK_PACKET last_packet
Definition: dn_tpcomm.c:140
static uint32_t inet_addr(const char *addr)
#define E_INVALIDPACKET
Failed because the packet is invalid.
Definition: dn_device.h:107
uint16_t command
Definition: dn_robotalk.h:149
#define THTYPE
TP communication object.
Definition: dn_tpcomm.c:126
_DN_EXP_COM HRESULT com_close(int *sock)
Closes the socket.
Definition: dn_com.c:385
#define RTK_CMD_ACK
A definition for the ROBOTalk command which means acknowledge.
Definition: dn_robotalk.h:75
#define E_OUTOFMEMORY
Failed because there is no enough memory space.
Definition: dn_common.h:125
uint16_t sin_port
_DN_EXP_COMMON uint16_t SysStringLen(BSTR bstr)
Gets and returns the number of characters of BSTR.
Definition: dn_common.c:118
HRESULT TPComm_GetTimeout(int fd, uint32_t *timeout)
Gets timeout.
Definition: dn_tpcomm.c:966
#define TP_ID_SERVER
A definition for the server ID.
Definition: dn_tpcomm.h:70
#define _PING_INTERVAL_TPERROR
A definition for the sending interval of the ping packet when TP is error state.
Definition: dn_tpcomm.c:92
#define THRET
THREAD recv_thread
Definition: dn_tpcomm.c:139
HRESULT TPComm_SetCallFunc(const struct CALL_FUNC_TP *func)
Sets callback functions.
Definition: dn_tpcomm.c:737
HRESULT TPComm_SetTimeout(int fd, uint32_t timeout)
Sets timeout.
Definition: dn_tpcomm.c:935
SAFEARRAY * parray
Definition: dn_common.h:325
#define NOPARITY
A definition for serial communication setting.
Definition: dn_device.h:69
#define AF_INET
Definition: dn_additional.h:66
#define TP_CMD_BEEP
A definition for the ROBOTalk command which means requests BEEP.
Definition: dn_tpcomm.h:94
#define TP_RETRY_TIMEOUT
A definition for the retry count of communication timeout.
Definition: dn_tpcomm.h:58
_DN_EXP_UDP HRESULT udp_recv(int sock, char *buf, uint32_t len_buf, uint32_t *len_recved, uint32_t timeout, void *arg)
Receives UDP packet.
Definition: dn_udp.c:135
#define SUCCEEDED(hr)
A macro that returns TRUE/FALSE. If hr is zero or more, then returns TRUE.
Definition: dn_common.h:71
TP touch bit mapping.
Definition: dn_tpcomm.h:279
#define TP_CMD_MTP_DRAW
A definition for the ROBOTalk command which means requests draw Mini TP.
Definition: dn_tpcomm.h:136
_DN_EXP_THREAD HRESULT set_event(EVENT *pevt)
Sets a event.
Definition: dn_thread.c:295
uint8_t client
Definition: dn_tpcomm.c:114
#define RTK_RETRY_FLAG
A definition for the ROBOTalk command. This bit means the retry packet.
Definition: dn_robotalk.h:117
A type definition for Ethernet connection parameters.
Definition: dn_device.h:144
Common device API file.
uint16_t vt
Definition: dn_common.h:308
#define ONESTOPBIT
A definition for serial communication setting.
Definition: dn_device.h:87
Common API file.
int EVENT
#define _TP_CMD_SPECIAL
A definition for the ROBOTalk command which means special command.
Definition: dn_tpcomm.c:57
#define INFINITE
A definition for infinite wait.
Definition: dn_thread.h:95
_DN_EXP_COMMON void VariantInit(VARIANT *pvarg)
Initializes the VARIANT.
Definition: dn_common.c:368
uint8_t from_id
Definition: dn_tpcomm.c:130
_DN_EXP_DEVICE void memcpy_be(void *dst, const void *src, uint32_t len)
Orders to big endian.
Definition: dn_device.c:495
_DN_EXP_COMMON HRESULT VariantChangeType(VARIANT *pvargDest, VARIANT *pvarSrc, uint16_t wFlags, uint16_t vt)
Changes the source variant to destination variant with the indicated type.
static HRESULT receive_execute(struct CONN_PARAM_TP *tp_param)
Receives the ROBOTalk packet and executes callback functions.
Definition: dn_tpcomm.c:452
A type definition for common communication parameters.
Definition: dn_device.h:170
User own API file.
static uint16_t htons(uint16_t hostshort)
_DN_EXP_THREAD uint32_t gettimeofday_msec()
Gets the current time value [ms].
Definition: dn_thread.c:589
HRESULT(* dn_open)(void *param, int *sock)
Definition: dn_device.h:176
uint8_t cts
Definition: dn_tpcomm.c:117
_DN_EXP_COM HRESULT com_send(int sock, const char *buf, uint32_t len_buf, void *arg)
Sends serial packet.
Definition: dn_com.c:412
HRESULT TPComm_GetTPState(int fd, int *state)
Gets TP state.
Definition: dn_tpcomm.c:985
struct in_addr sin_addr
#define dn_sleep(n)
A macro that sleeps.
#define E_NOTIMPL
Failed because the function is not implemented.
Definition: dn_common.h:101
A type definition for the multi type variable.
Definition: dn_common.h:306
_DN_EXP_COMMON BSTR SysAllocString(const wchar_t *sz)
Allocates and returns BSTR.
Definition: dn_common.c:61
_DN_EXP_THREAD HRESULT release_mutex(MUTEX *pmutex)
Releases mutex handle.
Definition: dn_thread.c:108
#define TP_CMD_REQ_ID
A definition for the ROBOTalk command which means requests ID.
Definition: dn_tpcomm.h:76
_DN_EXP_COMMON void VariantClear(VARIANT *pvarg)
Clears the VARIANT.
Definition: dn_common.c:382
#define E_MAX_OBJECT
Failed because the packet is too much.
Definition: dn_common.h:181
#define begin_thread(p_thread, function, arg)
A macro that begins thread.
uint16_t dst_port
Definition: dn_device.h:147
int int32_t
Definition: stdint.h:42
uint32_t init_clock
Definition: dn_tpcomm.c:134
_DN_EXP_DEVICE int parse_conn_type(const char *opt)
Parses and returns the connection type.
Definition: dn_device.c:93
static HRESULT tp_recv(struct CONN_PARAM_TP *tp_param, unsigned int retry_nak, uint16_t *command, uint8_t *data, uint8_t *len_data)
Definition: dn_tpcomm.c:321
SAFEARRAYBOUND rgsabound[1]
Definition: dn_common.h:299
A type definition for serial connection parameters.
Definition: dn_device.h:156
static THRET THTYPE recv_thread(void *arg)
The receiving thread.
Definition: dn_tpcomm.c:663
#define _TPERROR_TIMEOUT
A definition for the timeout which is used to raise disconnect event.
Definition: dn_tpcomm.c:71
static HRESULT timer_event(struct CONN_PARAM_TP *tp_param)
Executes timer event procedure.
Definition: dn_tpcomm.c:583
static THRET THTYPE timer_thread(void *arg)
The timer thread.
Definition: dn_tpcomm.c:697
#define exit_thread(thread)
A macro that ends thread.
#define _PING_INTERVAL_CONNECT
A definition for the sending interval of the ping packet when TP connected.
Definition: dn_tpcomm.c:85
#define NativeCommand(cmd)
A macro that removes the retry flag and count from the ROBOTalk command.
Definition: dn_robotalk.h:136
uint8_t from_id
Definition: dn_robotalk.h:150
HRESULT TPComm_LCD(int fd, int16_t contrast)
Changes TP LCD.
Definition: dn_tpcomm.c:1047
Serial API file.
_DN_EXP_UDP HRESULT udp_close(int *sock)
Closes the socket.
Definition: dn_udp.c:80
struct TP_FLAGS flags
Definition: dn_tpcomm.c:129
#define E_TIMEOUT
Failed because the communication timed out.
Definition: dn_common.h:169
uint32_t dst_addr
Definition: dn_device.h:146
_DN_EXP_ROBOTALK HRESULT rtk_send(const struct CONN_PARAM_COMMON *device, union RTK_PACKET *packet_send)
Sends ROBOTalk packet.
Definition: dn_robotalk.c:143
#define INADDR_ANY
Definition: dn_additional.h:36
static int connect(SOCKET s, const struct sockaddr *name, int namelen)
HRESULT(* dn_send)(int sock, const char *buf, uint32_t len_buf, void *arg)
Definition: dn_device.h:178
_DN_EXP_DEVICE HRESULT parse_conn_param_ether(const char *opt, struct CONN_PARAM_ETH *param)
Parses Ethernet connection parameters.
Definition: dn_device.c:121
uint32_t timer_clock
Definition: dn_tpcomm.c:132
HRESULT(* Call_TPTouchInfo)(struct TP_TOUCH_INFO touch_info)
Definition: dn_tpcomm.h:298
_DN_EXP_THREAD HRESULT initialize_mutex(MUTEX *pmutex)
Initializes mutex handle.
Definition: dn_thread.c:80
TP key bit mapping.
Definition: dn_tpcomm.h:250
static HRESULT tp_send(struct CONN_PARAM_TP *tp_param, uint16_t command, uint8_t *data, uint8_t len_data)
Definition: dn_tpcomm.c:287
short sin_family
THREAD timer_thread
Definition: dn_tpcomm.c:138
static struct CONN_PARAM_TP m_conn_param[TP_CONN_MAX]
Definition: dn_tpcomm.c:143
_DN_EXP_THREAD HRESULT reset_event(EVENT *pevt)
Resets a event.
Definition: dn_thread.c:328
#define RTK_CMD_REJ
A definition for the ROBOTalk command which means reject.
Definition: dn_robotalk.h:69
uint8_t data[RTK_SIZE_DATA]
Definition: dn_robotalk.h:152
TP communication API file.
_DN_EXP_COMMON SAFEARRAY * SafeArrayCreateVector(uint16_t vt, int32_t lLbound, uint32_t cElements)
Allocates and returns SAFEARRAY.
Definition: dn_common.c:138
volatile uint8_t recv_flag
Definition: dn_tpcomm.c:119
HRESULT(* Call_TPKeyInfo)(struct TP_KEY_INFO key_info)
Definition: dn_tpcomm.h:296
_DN_EXP_ROBOTALK HRESULT rtk_recv(const struct CONN_PARAM_COMMON *device, union RTK_PACKET *packet_recv, int client, unsigned int retry_nak)
Receives ROBOTalk packet.
Definition: dn_robotalk.c:193
static HRESULT send_receive(int index, unsigned int retry_timeout, uint16_t command_send, uint8_t *data_send, uint8_t len_send, uint16_t *command_recv, uint8_t *data_recv, uint8_t *len_recv)
Definition: dn_tpcomm.c:394
#define TP_ID_CLIENT
A definition for the client ID.
Definition: dn_tpcomm.h:64
_DN_EXP_COMMON HRESULT SafeArrayAccessData(SAFEARRAY *psa, void **ppvData)
Accesses the SAFEARRAY and gets the pointer of array data.
Definition: dn_common.c:336


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