123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- #include <stdio.h>
- #include <iostream>
- #include <memory>
- #include "api/video/video_frame.h"
- #include "../common/comm.h"
- #include "../common/iobuffer.h"
- #include "api.h"
- #include "protocol.pb.h"
- #include "PCANBasic.h"
- #include "message_queue.h"
- #include <signal.h>
- #include <cstring>
- #include <unistd.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <sys/ioctl.h>
- #include <sensors/sensors.h>
- #include <sensors/error.h>
- #include <linux/usbdevice_fs.h>
- bool g_collbrate=false;
- void renableUSB(const char *file)
- {
- // return;
- printf("Resetting USB device %s\n", file);
- int fd = open(file, O_WRONLY);
- if (fd < 0)
- {
- char text[256];
- perror(text);
- printf("Error opening output file %s", text);
- return;
- }
- int rc = ioctl(fd, USBDEVFS_RESET, 0);
- if (rc < 0)
- {
- perror("Error in ioctl");
- return;
- }
- printf("Reset successful\n");
- close(fd);
- }
- void daemonize(void)
- {
- signal(SIGTTOU, SIG_IGN);
- signal(SIGTTIN, SIG_IGN);
- signal(SIGTSTP, SIG_IGN);
- if (0 != fork())
- exit(0);
- if (-1 == setsid())
- exit(0);
- signal(SIGHUP, SIG_IGN);
- if (0 != fork())
- exit(0);
- // if (0 != chdir("/")) exit(0);
- }
- bool checkOnly()
- {
- const char filename[] = "./lockfile";
- int fd = open(filename, O_WRONLY | O_CREAT, 0644);
- int flock = lockf(fd, F_TLOCK, 0);
- if (fd == -1)
- {
- return false;
- }
- // 给文件加锁
- if (flock == -1)
- {
- return false;
- }
- // 程序退出后,文件自动解锁
- return true;
- }
- std::string getpath()
- {
- char exec_name[BUFSIZ];
- memset(exec_name, 0, sizeof(exec_name));
- readlink("/proc/self/exe", exec_name, BUFSIZ);
- // std::cout<<exec_name<<std::endl;
- int32_t len = strlen(exec_name);
- for (int i = len; i >= 0; i--)
- {
- if (exec_name[i] == '/')
- {
- exec_name[i] = 0;
- break;
- }
- }
- return std::string(exec_name);
- }
- int8_t hi_byte(int16_t value)
- {
- int8_t hi = (int8_t)((value & 0xFF00) >> 8);
- return hi;
- }
- int8_t lo_byte(int16_t value)
- {
- int8_t lo = (int8_t)(value & 0xff);
- return lo;
- }
- int main(int argc, char *argv[])
- {
- // renableUSB("/dev/bus/usb/002/002");
- // renableUSB("/dev/bus/usb/001/003");
- // daemonize();
-
- // printf("sensors_init:%d, %s\n", err ,sensors_strerror(err));
- auto path = getpath();
- std::cout << "hello" << path << std::endl;
- if (-1 == chdir(path.c_str()))
- return 0;
- if (checkOnly() == false)
- {
- std::cout << "进程已经在运行" << std::endl;
- return 0;
- }
- //
- std::cout << "进程启动..." << std::endl;
- // char temp[BUFSIZ];
- // getcwd(temp,BUFSIZ);
- // std::cout<<temp<<std::endl;
- if(argc==2)
- {
- if (*(argv[1]) == '-')
- {
- switch (*(++argv[1]))
- {
- case 'e':
- g_collbrate=true;
- break;
- }
- }
- }
-
- CMessageQueue Q;
- Q.Create();
-
-
- while (true)
- {
-
- Q.Process();
- }
- // gtk_main();
-
- return 0;
- }
|