main.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <memory>
  4. #include "api/video/video_frame.h"
  5. #include "../common/comm.h"
  6. #include "../common/iobuffer.h"
  7. #include "api.h"
  8. #include "protocol.pb.h"
  9. #include "PCANBasic.h"
  10. #include "message_queue.h"
  11. #include <signal.h>
  12. #include <cstring>
  13. #include <unistd.h>
  14. #include <fcntl.h>
  15. #include <errno.h>
  16. #include <sys/ioctl.h>
  17. #include <sensors/sensors.h>
  18. #include <sensors/error.h>
  19. #include <linux/usbdevice_fs.h>
  20. bool g_collbrate=false;
  21. void renableUSB(const char *file)
  22. {
  23. // return;
  24. printf("Resetting USB device %s\n", file);
  25. int fd = open(file, O_WRONLY);
  26. if (fd < 0)
  27. {
  28. char text[256];
  29. perror(text);
  30. printf("Error opening output file %s", text);
  31. return;
  32. }
  33. int rc = ioctl(fd, USBDEVFS_RESET, 0);
  34. if (rc < 0)
  35. {
  36. perror("Error in ioctl");
  37. return;
  38. }
  39. printf("Reset successful\n");
  40. close(fd);
  41. }
  42. void daemonize(void)
  43. {
  44. signal(SIGTTOU, SIG_IGN);
  45. signal(SIGTTIN, SIG_IGN);
  46. signal(SIGTSTP, SIG_IGN);
  47. if (0 != fork())
  48. exit(0);
  49. if (-1 == setsid())
  50. exit(0);
  51. signal(SIGHUP, SIG_IGN);
  52. if (0 != fork())
  53. exit(0);
  54. // if (0 != chdir("/")) exit(0);
  55. }
  56. bool checkOnly()
  57. {
  58. const char filename[] = "./lockfile";
  59. int fd = open(filename, O_WRONLY | O_CREAT, 0644);
  60. int flock = lockf(fd, F_TLOCK, 0);
  61. if (fd == -1)
  62. {
  63. return false;
  64. }
  65. // 给文件加锁
  66. if (flock == -1)
  67. {
  68. return false;
  69. }
  70. // 程序退出后,文件自动解锁
  71. return true;
  72. }
  73. std::string getpath()
  74. {
  75. char exec_name[BUFSIZ];
  76. memset(exec_name, 0, sizeof(exec_name));
  77. readlink("/proc/self/exe", exec_name, BUFSIZ);
  78. // std::cout<<exec_name<<std::endl;
  79. int32_t len = strlen(exec_name);
  80. for (int i = len; i >= 0; i--)
  81. {
  82. if (exec_name[i] == '/')
  83. {
  84. exec_name[i] = 0;
  85. break;
  86. }
  87. }
  88. return std::string(exec_name);
  89. }
  90. int8_t hi_byte(int16_t value)
  91. {
  92. int8_t hi = (int8_t)((value & 0xFF00) >> 8);
  93. return hi;
  94. }
  95. int8_t lo_byte(int16_t value)
  96. {
  97. int8_t lo = (int8_t)(value & 0xff);
  98. return lo;
  99. }
  100. int main(int argc, char *argv[])
  101. {
  102. // renableUSB("/dev/bus/usb/002/002");
  103. // renableUSB("/dev/bus/usb/001/003");
  104. // daemonize();
  105. // printf("sensors_init:%d, %s\n", err ,sensors_strerror(err));
  106. auto path = getpath();
  107. std::cout << "hello" << path << std::endl;
  108. if (-1 == chdir(path.c_str()))
  109. return 0;
  110. if (checkOnly() == false)
  111. {
  112. std::cout << "进程已经在运行" << std::endl;
  113. return 0;
  114. }
  115. //
  116. std::cout << "进程启动..." << std::endl;
  117. // char temp[BUFSIZ];
  118. // getcwd(temp,BUFSIZ);
  119. // std::cout<<temp<<std::endl;
  120. if(argc==2)
  121. {
  122. if (*(argv[1]) == '-')
  123. {
  124. switch (*(++argv[1]))
  125. {
  126. case 'e':
  127. g_collbrate=true;
  128. break;
  129. }
  130. }
  131. }
  132. CMessageQueue Q;
  133. Q.Create();
  134. while (true)
  135. {
  136. Q.Process();
  137. }
  138. // gtk_main();
  139. return 0;
  140. }