Rk_wake_lock.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <fcntl.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <pthread.h>
  9. #include "Rk_wake_lock.h"
  10. struct rk_wake_lock {
  11. char *id;
  12. };
  13. #define TAG "WAKE_LOCK"
  14. #define debug(fmt, args...) \
  15. printf(TAG "[%s] " fmt "\n", __func__, ##args)
  16. enum {
  17. ACQUIRE_PARTIAL_WAKE_LOCK = 0,
  18. RELEASE_WAKE_LOCK,
  19. OUR_FD_COUNT
  20. };
  21. const char * const WAKE_LOCK_PATHS[] = {
  22. "/sys/power/wake_lock",
  23. "/sys/power/wake_unlock",
  24. };
  25. static int g_initialized = 0;
  26. static int g_fds[2];
  27. static int g_error = -1;
  28. static int open_file_descriptors(const char * const paths[])
  29. {
  30. for (int i = 0 ; i < 2; i++) {
  31. int fd = open(paths[i], O_RDWR | O_CLOEXEC);
  32. if (fd < 0) {
  33. g_error = -errno;
  34. debug("fatal error opening \"%s\": %s", paths[i],
  35. strerror(errno));
  36. return -1;
  37. }
  38. g_fds[i] = fd;
  39. }
  40. g_error = 0;
  41. return 0;
  42. }
  43. static inline void initialize_fds(void) {
  44. if (g_initialized == 0) {
  45. open_file_descriptors(WAKE_LOCK_PATHS);
  46. g_initialized = 1;
  47. }
  48. }
  49. struct rk_wake_lock* RK_wake_lock_new(const char *id) {
  50. struct rk_wake_lock* lock;
  51. if (!id)
  52. return NULL;
  53. initialize_fds();
  54. lock = (struct rk_wake_lock*)malloc(sizeof(struct rk_wake_lock));
  55. lock->id = strdup(id);
  56. return lock;
  57. }
  58. void RK_wake_lock_delete(struct rk_wake_lock* lock) {
  59. if (lock && lock->id)
  60. free(lock->id);
  61. if (lock)
  62. free(lock);
  63. }
  64. int RK_acquire_wake_lock(struct rk_wake_lock *wake_lock) {
  65. char *id = wake_lock->id;
  66. debug("id=%s", id);
  67. if (g_error)
  68. return g_error;
  69. int fd;
  70. size_t len;
  71. ssize_t ret;
  72. fd = g_fds[0];
  73. ret = write(fd, id, strlen(id));
  74. if (ret < 0) {
  75. return -errno;
  76. }
  77. return ret;
  78. }
  79. int RK_release_wake_lock(struct rk_wake_lock *wake_lock) {
  80. char *id = wake_lock->id;
  81. int fd;
  82. debug("id=%s", id);
  83. if (g_error)
  84. return g_error;
  85. fd = g_fds[1];
  86. ssize_t len = write(fd, id, strlen(id));
  87. if (len < 0) {
  88. return -errno;
  89. }
  90. return len;
  91. }
  92. static int exec(const char *cmd, char *buf, const size_t size) {
  93. FILE *stream = NULL;
  94. char tmp[1024];
  95. if ((stream = popen(cmd,"r")) == NULL) {
  96. return -1;
  97. }
  98. if (buf == NULL) {
  99. pclose(stream);
  100. return -2;
  101. }
  102. buf[0] = '\0';
  103. while (fgets(tmp, sizeof(tmp) -1, stream)) {
  104. if (strlen(buf) + strlen(tmp) >= size) {
  105. pclose(stream);
  106. return -3;
  107. }
  108. strcat(buf, tmp);
  109. }
  110. pclose(stream);
  111. return 0;
  112. }
  113. int RK_wait_all_wake_lock_release(int timeout_ms) {
  114. int cnt = timeout_ms / 10;
  115. char result[1024];
  116. while(cnt--) {
  117. memset(result, 0, 1024);
  118. exec("cat /sys/power/wake_lock", result, 1024);
  119. if (strlen(result) <= 1) //换行符
  120. break;
  121. usleep(10*1000);
  122. printf("## suspend waiting wake_lock: %s\n", result);
  123. }
  124. if (cnt == 0) {
  125. printf("wait suspend timeout, abort suspend\n");
  126. printf("unreleased wake_lock: %s\n", result);
  127. return -1;
  128. }
  129. return 0;
  130. }