blob: 03f356c42ff1c044d519d757f9baabc9a3287e94 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001/*
2*
3* Data : 2023/03/28 16:00:28
4* Author : Hanzhiyu
5*
6*/
7#include "ql/ql_sleep_wakelock.h"
8#include <stdio.h>
9#include <unistd.h>
10#include <stddef.h>
11#include "mbtk_type.h"
12#include "mbtk_log.h"
13
14typedef struct
15{
16 int fd;
17 char name[128];
18} lock_name;
19
20lock_name ql_lock_name[512]={0};
21static bool autosleep_enable = FALSE;
22
23extern int Ql_Autosleep_Enable(char enable)
24{
25 if(enable == 1)
26 {
27 if(!access("/sys/power/autosleep", W_OK))
28 {
29 system("echo mem > /sys/power/autosleep");
30 autosleep_enable = TRUE;
31 return 0;
32 }
33 else
34 {
35 LOGE("/sys/power/autosleep can not write.");
36 return -1;
37 }
38 }
39 else
40 {
41 if(!access("/sys/power/autosleep", W_OK))
42 {
43 system("echo off > /sys/power/autosleep");
44 autosleep_enable = FALSE;
45 return 0;
46 }
47 else
48 {
49 LOGE("/sys/power/autosleep can not write.");
50 return -1;
51 }
52 }
53}
54
55extern int Ql_SLP_WakeLock_Create(const char *name, size_t len)
56{
57 if(!autosleep_enable) {
58 LOGE("Autosleep not enable.");
59 return -1;
60 }
61
62 if(name != NULL && len < 127)
63 {
64 int i;
65 for(i=0;i<512;i++)
66 {
67 if(ql_lock_name[i].fd == 0)
68 break;
69 }
70 memcpy(ql_lock_name[i].name, name, strlen(name)+1);
71 ql_lock_name[i].fd = i;
72 return ql_lock_name[i].fd;
73 }
74 else
75 return -1;
76}
77
78extern int Ql_SLP_WakeLock_Lock(int fd)
79{
80 if(!autosleep_enable) {
81 LOGE("Autosleep not enable.");
82 return -1;
83 }
84
85 int i;
86 for(i=0;i<512;i++)
87 {
88 if(ql_lock_name[i].fd == fd)
89 break;
90 }
91 if(i == 512)
92 return -1;
93
94 if(!access("/sys/power/wake_lock", W_OK))
95 {
96 char cmd[128]={0};
97 sprintf(cmd, "echo %s > /sys/power/wake_lock", ql_lock_name[i].name);
98 system(cmd);
99 return 0;
100 }
101 else
102 {
103 printf("/sys/power/wake_lock can not write.");
104 return -1;
105 }
106}
107
108extern int Ql_SLP_WakeLock_Unlock(int fd)
109{
110 if(!autosleep_enable) {
111 LOGE("Autosleep not enable.");
112 return -1;
113 }
114
115 int i;
116 for(i=0;i<512;i++)
117 {
118 if(ql_lock_name[i].fd == fd)
119 break;
120 }
121 if(i == 512)
122 return -1;
123
124 if(!access("/sys/power/wake_unlock", W_OK))
125 {
126 char cmd[128]={0};
127 sprintf(cmd, "echo %s > /sys/power/wake_unlock", ql_lock_name[i].name);
128 system(cmd);
129 return 0;
130 }
131 else
132 {
133 printf("/sys/power/wake_unlock can not write.");
134 return -1;
135 }
136
137}
138
139extern int Ql_SLP_WakeLock_Destroy(int fd)
140{
141 if(!autosleep_enable) {
142 LOGE("Autosleep not enable.");
143 return -1;
144 }
145
146 int i;
147 for(i=0;i<512;i++)
148 {
149 if(ql_lock_name[i].fd == fd)
150 break;
151 }
152 if(i == 512)
153 return -1;
154 else
155 {
156 ql_lock_name[i].fd = 0;
157 memset(ql_lock_name[i].name, 0, 128);
158 return 0;
159 }
160}
161