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" |
jb.qi | 422ee11 | 2023-11-21 03:04:03 -0800 | [diff] [blame^] | 14 | #include "sc_mnet_whitelist.h" |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 15 | |
| 16 | #ifdef __cplusplus |
| 17 | extern "C" { |
| 18 | #endif |
| 19 | |
| 20 | |
| 21 | extern int lynq_autosleep_enable(void); |
| 22 | extern int lynq_autosleep_disable(void); |
| 23 | extern int release_wake_lock(char *name); |
| 24 | extern int acquire_wake_lock(int lock, char *name); |
| 25 | |
| 26 | #define USER_LOG_TAG "LYNQ_QSER_AUTOSUSPEND" |
| 27 | #define FILE_LOCK_TABLE "/tmp/.lock_table" |
| 28 | |
| 29 | |
| 30 | LOCK_TABLE lock_status; |
| 31 | |
| 32 | |
| 33 | int file_fd; |
| 34 | int first_run = 0; |
| 35 | |
| 36 | |
| 37 | int lock_table_init(void) |
| 38 | { |
| 39 | int ret; |
| 40 | int err; |
| 41 | |
| 42 | file_fd = open(FILE_LOCK_TABLE, O_RDWR| O_CREAT,0777); |
| 43 | if(file_fd < 0) |
| 44 | { |
| 45 | err = errno; |
| 46 | LYINFLOG("Error open lock_table file:%s\n", strerror(errno)); |
| 47 | return -err; |
| 48 | } |
| 49 | memset(&lock_status, 0, sizeof(lock_status)); |
| 50 | ret = write(file_fd, (char*)&lock_status, sizeof(lock_status)); |
| 51 | if(ret <= 0) |
| 52 | { |
| 53 | LYINFLOG("write fail\n"); |
| 54 | close(file_fd); |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame] | 55 | return E_WRITE; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 56 | } |
| 57 | sync(); |
| 58 | close(file_fd); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | int read_lock_table(void) |
| 64 | { |
| 65 | int err; |
| 66 | int ret; |
| 67 | int i; |
| 68 | LYLOGSET(LOG_INFO); |
| 69 | LYLOGEINIT(USER_LOG_TAG); |
| 70 | |
| 71 | if(access(FILE_LOCK_TABLE,0) < 0) |
| 72 | { |
| 73 | ret = lock_table_init(); |
| 74 | if(ret < 0) |
| 75 | { |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame] | 76 | return E_READ; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | file_fd = open(FILE_LOCK_TABLE,O_RDWR); |
| 81 | if(file_fd < 0) |
| 82 | { |
| 83 | err = errno; |
| 84 | LYINFLOG("Error open lock_table file:%s\n", strerror(errno)); |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame] | 85 | return E_READ; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | memset(&lock_status, 0, sizeof(lock_status)); |
| 89 | lseek(file_fd,0,SEEK_SET); |
| 90 | ret = read(file_fd,(unsigned char *)&lock_status,sizeof(lock_status)); |
| 91 | LYINFLOG("read ret=%d\n", ret); |
| 92 | if(ret <= 0) |
| 93 | { |
| 94 | close(file_fd); |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame] | 95 | return E_READ; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | for(i=0;i<MAX_LOCK_NUM;i++) |
| 99 | { |
| 100 | if(strlen(lock_status.lock_name[i]) != 0) |
| 101 | { |
| 102 | LYINFLOG("fd: %d lock_name:%s strlen:%d\n", i, lock_status.lock_name[i], strlen(lock_status.lock_name[i])); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | close(file_fd); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | int save_lock_table(void) |
| 112 | { |
| 113 | int err; |
| 114 | int ret; |
| 115 | file_fd = open(FILE_LOCK_TABLE,O_RDWR); |
| 116 | if(file_fd < 0) |
| 117 | { |
| 118 | err = errno; |
| 119 | LYINFLOG("Error open lock_table file:%s\n", strerror(errno)); |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame] | 120 | return E_WRITE; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 121 | } |
| 122 | LYINFLOG("write lock_name[0]: %s\n", lock_status.lock_name[0]); |
| 123 | ret = write(file_fd, (unsigned char *)&lock_status, sizeof(lock_status)); |
| 124 | LYINFLOG("write ret=%d\n", ret); |
| 125 | if(ret <= 0) |
| 126 | { |
| 127 | LYINFLOG("write fail\n"); |
| 128 | close(file_fd); |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame] | 129 | return E_WRITE; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 130 | } |
| 131 | sync(); |
| 132 | close(file_fd); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | int check_lock(char *name) |
| 139 | { |
| 140 | int j; |
| 141 | int num; |
| 142 | for(j=0;j<MAX_LOCK_NUM;j++) |
| 143 | { |
| 144 | if(strcmp(lock_status.lock_name[j], name) == 0) |
| 145 | { |
| 146 | num = j; |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if(j < MAX_LOCK_NUM) |
| 152 | { |
| 153 | return num; |
| 154 | } |
| 155 | |
| 156 | return -1; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | int add_lock(char *name) |
| 161 | { |
| 162 | int ret; |
| 163 | int i = 0; |
| 164 | int num; |
| 165 | int check_flag; |
| 166 | |
| 167 | LYINFLOG("name:%s\n", name); |
| 168 | ret = read_lock_table(); |
| 169 | LYINFLOG("read_lock_table ret = %d\n", ret); |
| 170 | if(ret <0) |
| 171 | { |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | check_flag = check_lock(name); |
| 176 | |
| 177 | if(check_flag < 0) |
| 178 | { |
| 179 | for(i=0;i<MAX_LOCK_NUM;i++) |
| 180 | { |
| 181 | if(strlen(lock_status.lock_name[i]) == 0) |
| 182 | { |
| 183 | strcpy(lock_status.lock_name[i], name); |
| 184 | LYINFLOG("lock_name[%d] %s\n", i, lock_status.lock_name[i]); |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | if(i == MAX_LOCK_NUM) |
| 189 | { |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame] | 190 | return E_TABLE_FULL; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 191 | } |
| 192 | else |
| 193 | { |
| 194 | num = i; |
| 195 | } |
| 196 | } |
| 197 | else |
| 198 | { |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame] | 199 | return E_LOCK_EXIST; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | LYINFLOG("num = %d\n", num); |
| 203 | ret = save_lock_table(); |
| 204 | if(ret < 0) |
| 205 | { |
| 206 | return ret; |
| 207 | } |
| 208 | return num; |
| 209 | } |
| 210 | |
| 211 | int delete_lock(int fd) |
| 212 | { |
| 213 | int ret; |
| 214 | int i; |
| 215 | ret = read_lock_table(); |
| 216 | memset(lock_status.lock_name[fd], 0, sizeof(lock_status.lock_name[fd])); |
| 217 | ret = save_lock_table(); |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | int qser_lpm_init(qser_lpm_Handler_t qser_lpm_handler, qser_pm_cfg_t *qser_lpm_cfg) |
| 222 | { |
| 223 | int ret; |
| 224 | ret = system("uci set lynq_uci.lynq_autosuspend.init='1'"); |
| 225 | system("uci commit"); |
| 226 | if(ret != 0) |
| 227 | { |
| 228 | LYINFLOG("uci set fail"); |
| 229 | } |
| 230 | ret = system("./etc/init.d/lynq-autosuspend.sh restart"); |
| 231 | if(ret != 0) |
| 232 | { |
| 233 | LYINFLOG("restart service fail"); |
| 234 | } |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | int qser_lpm_deinit(void) |
| 240 | { |
| 241 | int ret; |
| 242 | system("uci set lynq_uci.lynq_autosuspend.init='0'"); |
| 243 | system("uci commit"); |
| 244 | if(ret != 0) |
| 245 | { |
| 246 | LYINFLOG("uci set fail"); |
| 247 | } |
| 248 | system("./etc/init.d/lynq-autosuspend.sh restarts"); |
| 249 | if(ret != 0) |
| 250 | { |
| 251 | LYINFLOG("restart service fail"); |
| 252 | } |
| 253 | return 0; |
| 254 | } |
| 255 | |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame] | 256 | |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 257 | int qser_autosuspend_enable(char enable) |
| 258 | { |
| 259 | int ret; |
| 260 | if(enable == '0') |
| 261 | { |
| 262 | ret = lynq_autosleep_disable(); |
| 263 | |
| 264 | } |
| 265 | else if(enable == '1') |
| 266 | { |
| 267 | ret = lynq_autosleep_enable(); |
| 268 | } |
| 269 | else |
| 270 | { |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame] | 271 | return E_INPUT_ERROR; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 272 | |
| 273 | } |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame] | 274 | if(ret >= 0) |
| 275 | { |
| 276 | ret = 0; |
| 277 | } |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 278 | return ret; |
| 279 | |
| 280 | } |
| 281 | |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame] | 282 | |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 283 | int qser_wakelock_create(const char *name, size_t len) |
| 284 | { |
| 285 | int ret; |
| 286 | if(name == NULL) |
| 287 | { |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame] | 288 | return E_INPUT_ERROR; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 289 | } |
| 290 | LYINFLOG("%s\n", name); |
| 291 | ret = add_lock(name); |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | int qser_wakelock_lock(int fd) |
| 296 | { |
| 297 | |
| 298 | int ret; |
| 299 | if(fd < 0 || fd >= MAX_LOCK_NUM) |
| 300 | { |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame] | 301 | return E_INPUT_ERROR; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 302 | } |
| 303 | ret = read_lock_table(); |
| 304 | ret = acquire_wake_lock( 0, lock_status.lock_name[fd]); |
jb.qi | 68fe778 | 2023-09-18 20:40:58 -0700 | [diff] [blame] | 305 | if(ret > 0) |
| 306 | { |
| 307 | ret = 0; |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | ret = -1; |
| 312 | } |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | int qser_wakelock_unlock(int fd) |
| 317 | { |
| 318 | int ret; |
| 319 | if(fd < 0 || fd >= MAX_LOCK_NUM) |
| 320 | { |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame] | 321 | return E_INPUT_ERROR; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 322 | } |
| 323 | ret = read_lock_table(); |
| 324 | if(strlen(lock_status.lock_name[fd]) == 0) |
| 325 | { |
| 326 | LYINFLOG("%d is null\n", fd); |
| 327 | return -1; |
| 328 | } |
| 329 | ret = release_wake_lock(lock_status.lock_name[fd]); |
jb.qi | 68fe778 | 2023-09-18 20:40:58 -0700 | [diff] [blame] | 330 | if(ret > 0) |
| 331 | { |
| 332 | ret = 0; |
| 333 | } |
| 334 | else |
| 335 | { |
| 336 | ret = -1; |
| 337 | } |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | int qser_wakelock_destroy(int fd) |
| 342 | { |
| 343 | int ret; |
| 344 | if(fd < 0 || fd >= MAX_LOCK_NUM) |
| 345 | { |
jb.qi | aa241cf | 2023-09-18 18:35:00 -0700 | [diff] [blame] | 346 | return E_INPUT_ERROR; |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 347 | } |
jb.qi | 5221035 | 2023-09-19 23:29:09 -0700 | [diff] [blame] | 348 | ret = qser_wakelock_unlock(fd); |
| 349 | if(ret) |
| 350 | { |
| 351 | LYINFLOG("unlock is fail\n"); |
| 352 | return ret; |
| 353 | } |
| 354 | |
jb.qi | 422ee11 | 2023-11-21 03:04:03 -0800 | [diff] [blame^] | 355 | ret = delete_lock(fd); |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 356 | return ret; |
| 357 | } |
| 358 | |
jb.qi | 422ee11 | 2023-11-21 03:04:03 -0800 | [diff] [blame^] | 359 | int qser_whitelist_add(int id) |
| 360 | { |
| 361 | int ret; |
| 362 | if(sc_mnet_whitelist_init() != 0) |
| 363 | { |
| 364 | LYINFLOG("sc_mnet_whitelist_init failed.\n"); |
| 365 | return -1; |
| 366 | } |
| 367 | |
| 368 | ret = sc_mnet_whitelist_add(id); |
| 369 | LYINFLOG("sc_mnet_whitelist_add ret %d.\n", ret); |
| 370 | |
| 371 | sc_mnet_whitelist_deinit(); |
| 372 | return ret; |
| 373 | |
| 374 | } |
| 375 | |
| 376 | int qser_whitelist_del(int id) |
| 377 | { |
| 378 | int ret; |
| 379 | if(sc_mnet_whitelist_init() != 0) |
| 380 | { |
| 381 | LYINFLOG("sc_mnet_whitelist_init failed.\n"); |
| 382 | return -1; |
| 383 | } |
| 384 | |
| 385 | ret = sc_mnet_whitelist_del(id); |
| 386 | LYINFLOG("qser_whitelist_del ret %d.\n", ret); |
| 387 | |
| 388 | sc_mnet_whitelist_deinit(); |
| 389 | return ret; |
| 390 | |
| 391 | } |
| 392 | |
| 393 | int qser_whitelist_get(char* whitelist) |
| 394 | { |
| 395 | int ret; |
| 396 | if(sc_mnet_whitelist_init() != 0) |
| 397 | { |
| 398 | LYINFLOG("sc_mnet_whitelist_init failed.\n"); |
| 399 | return -1; |
| 400 | } |
| 401 | |
| 402 | whitelist = sc_mnet_whitelist_get(); |
| 403 | if(NULL != whitelist) |
| 404 | { |
| 405 | LYINFLOG("sc_mnet_whitelist_get :%s\n",whitelist); |
| 406 | ret = 0; |
| 407 | } |
| 408 | else |
| 409 | { |
| 410 | LYINFLOG("sc_mnet_whitelist_get empty\n"); |
| 411 | ret = -1; |
| 412 | } |
| 413 | sc_mnet_whitelist_deinit(); |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | int qser_whitelist_clear(void) |
| 418 | { |
| 419 | int ret; |
| 420 | if(sc_mnet_whitelist_init() != 0) |
| 421 | { |
| 422 | LYINFLOG("sc_mnet_whitelist_init failed.\n"); |
| 423 | return -1; |
| 424 | } |
| 425 | ret = sc_mnet_whitelist_clear(); |
| 426 | LYINFLOG("sc_mnet_whitelist_clear ret %d\n", ret); |
| 427 | sc_mnet_whitelist_deinit(); |
| 428 | return ret; |
| 429 | |
| 430 | } |
| 431 | |
| 432 | |
you.chen | 21c62b7 | 2023-09-08 09:41:11 +0800 | [diff] [blame] | 433 | DEFINE_LYNQ_LIB_LOG(LYNQ_QSER_AUTOSUSPEND) |
| 434 | |
jb.qi | 3a8298c | 2023-09-07 04:48:51 -0700 | [diff] [blame] | 435 | #ifdef __cplusplus |
| 436 | } |
| 437 | #endif |
| 438 | |
| 439 | |
| 440 | |