main.cpp 3.0 KB

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