cash_app.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <sys/select.h>
  8. #include <sys/time.h>
  9. #include <pthread.h>
  10. #define BUFFER_SIZE 1024
  11. int mode = 0;
  12. int fd = 0;
  13. int thread_run = 1;
  14. void print_usage(const char *prog_name)
  15. {
  16. printf("%s 1 /dev/cashd0\n", prog_name);
  17. printf("%s 0 /dev/cashd0\n", prog_name);
  18. }
  19. int set_nonblocking(int fd)
  20. {
  21. int flags = fcntl(fd, F_GETFL, 0);
  22. if (flags == -1)
  23. {
  24. perror("fcntl F_GETFL");
  25. return -1;
  26. }
  27. if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
  28. {
  29. perror("fcntl F_SETFL");
  30. return -1;
  31. }
  32. return 0;
  33. }
  34. int set_blocking(int fd)
  35. {
  36. int flags = fcntl(fd, F_GETFL, 0);
  37. if (flags == -1)
  38. {
  39. perror("fcntl F_GETFL");
  40. return -1;
  41. }
  42. if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) == -1)
  43. {
  44. perror("fcntl F_SETFL");
  45. return -1;
  46. }
  47. return 0;
  48. }
  49. void blocking_read(int fd)
  50. {
  51. fd_set read_fds;
  52. struct timeval tv;
  53. char buffer[BUFFER_SIZE];
  54. int ret;
  55. printf("wait data...\n");
  56. while (thread_run)
  57. {
  58. FD_ZERO(&read_fds);
  59. FD_SET(fd, &read_fds);
  60. tv.tv_sec = 1;
  61. tv.tv_usec = 0;
  62. ret = select(fd + 1, &read_fds, NULL, NULL, &tv);
  63. if (ret == -1)
  64. {
  65. if (errno == EINTR)
  66. {
  67. continue;
  68. }
  69. perror("select error");
  70. break;
  71. }
  72. if (FD_ISSET(fd, &read_fds))
  73. {
  74. memset(buffer, 0, sizeof(buffer));
  75. ssize_t n = read(fd, buffer, sizeof(buffer) - 1);
  76. if (n > 0)
  77. {
  78. buffer[n] = '\0';
  79. printf("read %zd byte: %s\n", n, buffer);
  80. if (strncmp(buffer, "quit", 4) == 0 ||
  81. strncmp(buffer, "exit", 4) == 0)
  82. {
  83. printf("exit\n");
  84. break;
  85. }
  86. }
  87. else if (n == 0)
  88. {
  89. printf("close\n");
  90. break;
  91. }
  92. else
  93. {
  94. if (errno != EAGAIN && errno != EWOULDBLOCK)
  95. {
  96. perror("read error");
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. void nonblocking_read(int fd)
  104. {
  105. fd_set read_fds;
  106. struct timeval timeout;
  107. char buffer[BUFFER_SIZE];
  108. int ret;
  109. while (thread_run)
  110. {
  111. memset(buffer, 0, sizeof(buffer));
  112. ssize_t n = read(fd, buffer, sizeof(buffer) - 1);
  113. if (n > 0)
  114. {
  115. printf("read %zd byte: %s\n", n, buffer);
  116. }
  117. sleep(1);
  118. }
  119. }
  120. void *read_pthread_func(void *arg)
  121. {
  122. if (mode == 0)
  123. {
  124. if (set_nonblocking(fd) < 0)
  125. {
  126. return NULL;
  127. }
  128. nonblocking_read(fd);
  129. }
  130. else
  131. {
  132. if (set_blocking(fd) < 0)
  133. {
  134. return NULL;
  135. }
  136. blocking_read(fd);
  137. }
  138. }
  139. int main(int argc, char *argv[])
  140. {
  141. const char *device_path;
  142. int open_flags;
  143. int ret = 0;
  144. char cmd = 0;
  145. pthread_t read_thread = 0;
  146. if (argc != 3)
  147. {
  148. print_usage(argv[0]);
  149. return 1;
  150. }
  151. mode = atoi(argv[1]);
  152. if (mode != 0 && mode != 1)
  153. {
  154. print_usage(argv[0]);
  155. return 1;
  156. }
  157. device_path = argv[2];
  158. open_flags = O_RDWR;
  159. fd = open(device_path, open_flags);
  160. if (fd < 0)
  161. {
  162. perror("open fail:");
  163. return 1;
  164. }
  165. pthread_create(&read_thread, NULL, read_pthread_func, NULL);
  166. while(1)
  167. {
  168. printf("please input cmd: o/O-open q-quit\n");
  169. scanf(" %c", &cmd);
  170. if (cmd == 'o' || cmd == 'O')
  171. {
  172. ret = write(fd, &cmd, 1);
  173. if (ret < 0)
  174. {
  175. perror("write fail:");
  176. break;
  177. }
  178. }
  179. else if( cmd == 'q')
  180. {
  181. break;
  182. }
  183. else
  184. {
  185. printf("input error\n");
  186. }
  187. }
  188. thread_run = 0;
  189. pthread_join(read_thread, NULL);
  190. close(fd);
  191. return 0;
  192. }