jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <sys/stat.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <errno.h> |
| 8 | #include <pthread.h> |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 9 | #include<unistd.h> |
| 10 | |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 11 | #include <lynq_autosuspend.h> |
| 12 | #include "lynq-qser-autosuspend.h" |
| 13 | #include "liblog/lynq_deflog.h" |
| 14 | |
| 15 | #ifdef __cplusplus |
| 16 | extern "C" { |
| 17 | #endif |
| 18 | |
| 19 | |
| 20 | extern int lynq_autosleep_enable(void); |
| 21 | extern int lynq_autosleep_disable(void); |
| 22 | extern int release_wake_lock(char *name); |
| 23 | extern int acquire_wake_lock(int lock, char *name); |
| 24 | |
| 25 | #define USER_LOG_TAG "LYNQ_QSER_AUTOSUSPEND" |
| 26 | #define FILE_LOCK_TABLE "/tmp/.lock_table" |
| 27 | |
| 28 | |
| 29 | LOCK_TABLE lock_status; |
| 30 | |
| 31 | |
| 32 | int file_fd; |
| 33 | int first_run = 0; |
| 34 | |
| 35 | |
| 36 | int lock_table_init(void) |
| 37 | { |
| 38 | int ret; |
| 39 | int err; |
| 40 | |
| 41 | file_fd = open(FILE_LOCK_TABLE, O_RDWR| O_CREAT,0777); |
| 42 | if(file_fd < 0) |
| 43 | { |
| 44 | err = errno; |
| 45 | LYINFLOG("Error open lock_table file:%s\n", strerror(errno)); |
| 46 | return -err; |
| 47 | } |
| 48 | memset(&lock_status, 0, sizeof(lock_status)); |
| 49 | ret = write(file_fd, (char*)&lock_status, sizeof(lock_status)); |
| 50 | if(ret <= 0) |
| 51 | { |
| 52 | LYINFLOG("write fail\n"); |
| 53 | close(file_fd); |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame^] | 54 | return E_WRITE; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 55 | } |
| 56 | sync(); |
| 57 | close(file_fd); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | int read_lock_table(void) |
| 63 | { |
| 64 | int err; |
| 65 | int ret; |
| 66 | int i; |
| 67 | LYLOGSET(LOG_INFO); |
| 68 | LYLOGEINIT(USER_LOG_TAG); |
| 69 | |
| 70 | if(access(FILE_LOCK_TABLE,0) < 0) |
| 71 | { |
| 72 | ret = lock_table_init(); |
| 73 | if(ret < 0) |
| 74 | { |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame^] | 75 | return E_READ; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | file_fd = open(FILE_LOCK_TABLE,O_RDWR); |
| 80 | if(file_fd < 0) |
| 81 | { |
| 82 | err = errno; |
| 83 | LYINFLOG("Error open lock_table file:%s\n", strerror(errno)); |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame^] | 84 | return E_READ; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | memset(&lock_status, 0, sizeof(lock_status)); |
| 88 | lseek(file_fd,0,SEEK_SET); |
| 89 | ret = read(file_fd,(unsigned char *)&lock_status,sizeof(lock_status)); |
| 90 | LYINFLOG("read ret=%d\n", ret); |
| 91 | if(ret <= 0) |
| 92 | { |
| 93 | close(file_fd); |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame^] | 94 | return E_READ; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | for(i=0;i<MAX_LOCK_NUM;i++) |
| 98 | { |
| 99 | if(strlen(lock_status.lock_name[i]) != 0) |
| 100 | { |
| 101 | LYINFLOG("fd: %d lock_name:%s strlen:%d\n", i, lock_status.lock_name[i], strlen(lock_status.lock_name[i])); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | close(file_fd); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | int save_lock_table(void) |
| 111 | { |
| 112 | int err; |
| 113 | int ret; |
| 114 | file_fd = open(FILE_LOCK_TABLE,O_RDWR); |
| 115 | if(file_fd < 0) |
| 116 | { |
| 117 | err = errno; |
| 118 | LYINFLOG("Error open lock_table file:%s\n", strerror(errno)); |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame^] | 119 | return E_WRITE; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 120 | } |
| 121 | LYINFLOG("write lock_name[0]: %s\n", lock_status.lock_name[0]); |
| 122 | ret = write(file_fd, (unsigned char *)&lock_status, sizeof(lock_status)); |
| 123 | LYINFLOG("write ret=%d\n", ret); |
| 124 | if(ret <= 0) |
| 125 | { |
| 126 | LYINFLOG("write fail\n"); |
| 127 | close(file_fd); |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame^] | 128 | return E_WRITE; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 129 | } |
| 130 | sync(); |
| 131 | close(file_fd); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | int check_lock(char *name) |
| 138 | { |
| 139 | int j; |
| 140 | int num; |
| 141 | for(j=0;j<MAX_LOCK_NUM;j++) |
| 142 | { |
| 143 | if(strcmp(lock_status.lock_name[j], name) == 0) |
| 144 | { |
| 145 | num = j; |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if(j < MAX_LOCK_NUM) |
| 151 | { |
| 152 | return num; |
| 153 | } |
| 154 | |
| 155 | return -1; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | int add_lock(char *name) |
| 160 | { |
| 161 | int ret; |
| 162 | int i = 0; |
| 163 | int num; |
| 164 | int check_flag; |
| 165 | |
| 166 | LYINFLOG("name:%s\n", name); |
| 167 | ret = read_lock_table(); |
| 168 | LYINFLOG("read_lock_table ret = %d\n", ret); |
| 169 | if(ret <0) |
| 170 | { |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | check_flag = check_lock(name); |
| 175 | |
| 176 | if(check_flag < 0) |
| 177 | { |
| 178 | for(i=0;i<MAX_LOCK_NUM;i++) |
| 179 | { |
| 180 | if(strlen(lock_status.lock_name[i]) == 0) |
| 181 | { |
| 182 | strcpy(lock_status.lock_name[i], name); |
| 183 | LYINFLOG("lock_name[%d] %s\n", i, lock_status.lock_name[i]); |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | if(i == MAX_LOCK_NUM) |
| 188 | { |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame^] | 189 | return E_TABLE_FULL; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 190 | } |
| 191 | else |
| 192 | { |
| 193 | num = i; |
| 194 | } |
| 195 | } |
| 196 | else |
| 197 | { |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame^] | 198 | return E_LOCK_EXIST; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | LYINFLOG("num = %d\n", num); |
| 202 | ret = save_lock_table(); |
| 203 | if(ret < 0) |
| 204 | { |
| 205 | return ret; |
| 206 | } |
| 207 | return num; |
| 208 | } |
| 209 | |
| 210 | int delete_lock(int fd) |
| 211 | { |
| 212 | int ret; |
| 213 | int i; |
| 214 | ret = read_lock_table(); |
| 215 | memset(lock_status.lock_name[fd], 0, sizeof(lock_status.lock_name[fd])); |
| 216 | ret = save_lock_table(); |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | int qser_lpm_init(qser_lpm_Handler_t qser_lpm_handler, qser_pm_cfg_t *qser_lpm_cfg) |
| 221 | { |
| 222 | int ret; |
| 223 | ret = system("uci set lynq_uci.lynq_autosuspend.init='1'"); |
| 224 | system("uci commit"); |
| 225 | if(ret != 0) |
| 226 | { |
| 227 | LYINFLOG("uci set fail"); |
| 228 | } |
| 229 | ret = system("./etc/init.d/lynq-autosuspend.sh restart"); |
| 230 | if(ret != 0) |
| 231 | { |
| 232 | LYINFLOG("restart service fail"); |
| 233 | } |
| 234 | return ret; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | int qser_lpm_deinit(void) |
| 239 | { |
| 240 | int ret; |
| 241 | system("uci set lynq_uci.lynq_autosuspend.init='0'"); |
| 242 | system("uci commit"); |
| 243 | if(ret != 0) |
| 244 | { |
| 245 | LYINFLOG("uci set fail"); |
| 246 | } |
| 247 | system("./etc/init.d/lynq-autosuspend.sh restarts"); |
| 248 | if(ret != 0) |
| 249 | { |
| 250 | LYINFLOG("restart service fail"); |
| 251 | } |
| 252 | return 0; |
| 253 | } |
| 254 | |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame^] | 255 | |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 256 | int qser_autosuspend_enable(char enable) |
| 257 | { |
| 258 | int ret; |
| 259 | if(enable == '0') |
| 260 | { |
| 261 | ret = lynq_autosleep_disable(); |
| 262 | |
| 263 | } |
| 264 | else if(enable == '1') |
| 265 | { |
| 266 | ret = lynq_autosleep_enable(); |
| 267 | } |
| 268 | else |
| 269 | { |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame^] | 270 | return E_INPUT_ERROR; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 271 | |
| 272 | } |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame^] | 273 | if(ret >= 0) |
| 274 | { |
| 275 | ret = 0; |
| 276 | } |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 277 | return ret; |
| 278 | |
| 279 | } |
| 280 | |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame^] | 281 | |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 282 | int qser_wakelock_create(const char *name, size_t len) |
| 283 | { |
| 284 | int ret; |
| 285 | if(name == NULL) |
| 286 | { |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame^] | 287 | return E_INPUT_ERROR; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 288 | } |
| 289 | LYINFLOG("%s\n", name); |
| 290 | ret = add_lock(name); |
| 291 | return ret; |
| 292 | } |
| 293 | |
| 294 | int qser_wakelock_lock(int fd) |
| 295 | { |
| 296 | |
| 297 | int ret; |
| 298 | if(fd < 0 || fd >= MAX_LOCK_NUM) |
| 299 | { |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame^] | 300 | return E_INPUT_ERROR; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 301 | } |
| 302 | ret = read_lock_table(); |
| 303 | ret = acquire_wake_lock( 0, lock_status.lock_name[fd]); |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | int qser_wakelock_unlock(int fd) |
| 308 | { |
| 309 | int ret; |
| 310 | if(fd < 0 || fd >= MAX_LOCK_NUM) |
| 311 | { |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame^] | 312 | return E_INPUT_ERROR; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 313 | } |
| 314 | ret = read_lock_table(); |
| 315 | if(strlen(lock_status.lock_name[fd]) == 0) |
| 316 | { |
| 317 | LYINFLOG("%d is null\n", fd); |
| 318 | return -1; |
| 319 | } |
| 320 | ret = release_wake_lock(lock_status.lock_name[fd]); |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | int qser_wakelock_destroy(int fd) |
| 325 | { |
| 326 | int ret; |
| 327 | if(fd < 0 || fd >= MAX_LOCK_NUM) |
| 328 | { |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame^] | 329 | return E_INPUT_ERROR; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 330 | } |
| 331 | ret = delete_lock(fd); |
| 332 | return ret; |
| 333 | } |
| 334 | |
you.chen | 21c62b7 | 2023-09-08 09:41:11 +0800 | [diff] [blame] | 335 | DEFINE_LYNQ_LIB_LOG(LYNQ_QSER_AUTOSUSPEND) |
| 336 | |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 337 | #ifdef __cplusplus |
| 338 | } |
| 339 | #endif |
| 340 | |
| 341 | |
| 342 | |