-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheb_common.c
123 lines (98 loc) · 2.31 KB
/
eb_common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifdef __cplusplus
extern "C"{
#endif
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <fcntl.h>
#include <linux/fb.h>
//#include <sys/mman.h>
#include <sys/ioctl.h>
#include <time.h>
#include <sys/time.h>
#ifdef __cplusplus
}
#endif
#define GY_GLOBAL
#include "eb_common.h"
char *getBacklightNodeName(void)
{
DIR *dir;
struct dirent *ent;
char *retName = NULL;
dir = opendir("/sys/class/backlight/");
if(NULL == dir)
return NULL;
while((ent = readdir(dir)) != NULL) {
if((0 == strcmp(ent->d_name, ".")) || (0 == strcmp(ent->d_name, ".."))) {
continue;
}else {
retName = ent->d_name;
break;
}
}
closedir(dir);
return retName;
}
int getScreenInfo(struct fb_var_screeninfo *vinfo)
{
int fbfd = 0;
fbfd = open("/dev/fb0", O_RDONLY);
if (!fbfd) {
//printf("Error: cannot open framebuffer device.\n");
return -1;
}
//printf("The framebuffer device was opened successfully.\n");
if (ioctl(fbfd, FBIOGET_VSCREENINFO, vinfo)) {
//printf("Error reading variable information.\n");
return -1;
}
close(fbfd);
return 0;
}
unsigned char calcCheckSum(unsigned char *data, unsigned int len)
{
unsigned char sum = 0x00;
while(len--) {
sum += data[len];
}
return sum;
}
bool is_singleton(void)
{
int fd = -1;
char buf[32];
fd = open(DEFAULT_FILE, O_WRONLY | O_CREAT, 0666);
if(fd < 0) {
printf("(E) Open %s failed.\n", DEFAULT_FILE);
return false;
}
struct flock lock;
bzero(&lock, sizeof(lock));
if(fcntl(fd, F_GETLK, &lock) < 0) {
printf("(E) Fail to fcntl F_GETLK.\n");
return false;
}
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
if(fcntl(fd, F_SETLK, &lock) < 0) {
printf("(E) Fail to fcntl F_SETLK.\n");
return false;
}
pid_t pid = getpid();
int len = snprintf(buf, 32, "%d\n", (int)pid);
// Write pid to the file, don't close
if(-1 == write(fd, buf, len)) {
printf("(E) Fail to write pid into file.\n");
return false;
}
return true;
}
int get_timezone()
{
struct timezone tz;
gettimeofday(NULL, &tz);
return tz.tz_minuteswest / -60;
}