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