battery_powerbook.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C) 2010 Michael Buesch <m@bues.ch>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include "battery_powerbook.h"
  15. #include "fileaccess.h"
  16. #include "log.h"
  17. #include "util.h"
  18. #include <stdio.h>
  19. #include <errno.h>
  20. #include <string.h>
  21. static int battery_powerbook_update(struct battery *b)
  22. {
  23. struct battery_powerbook *bp = container_of(b, struct battery_powerbook, battery);
  24. LIST_HEAD(lines);
  25. struct text_line *tl;
  26. int err;
  27. unsigned int value;
  28. int got_ac = 0, got_max_chg = 0, got_cur_chg = 0;
  29. int value_changed = 0;
  30. err = file_read_text_lines(bp->info_file, &lines, 0);
  31. if (err || list_empty(&lines)) {
  32. logerr("WARNING: Failed to read battery info file\n");
  33. return -ETXTBSY;
  34. }
  35. list_for_each_entry(tl, &lines, list) {
  36. char *id, *elem;
  37. elem = strchr(tl->text, ':');
  38. if (!elem)
  39. continue;
  40. elem[0] = '\0';
  41. elem++;
  42. elem = string_strip(elem);
  43. id = string_strip(tl->text);
  44. if (strcmp(id, "AC Power") == 0) {
  45. if (sscanf(elem, "%u", &value) == 1) {
  46. if ((int)value != bp->on_ac)
  47. value_changed = 1;
  48. bp->on_ac = (int)value;
  49. got_ac = 1;
  50. }
  51. }
  52. if (got_ac)
  53. break;
  54. }
  55. text_lines_free(&lines);
  56. err = file_read_text_lines(bp->stat_file, &lines, 0);
  57. if (err || list_empty(&lines)) {
  58. logerr("WARNING: Failed to read battery stat file\n");
  59. return -ETXTBSY;
  60. }
  61. list_for_each_entry(tl, &lines, list) {
  62. char *id, *elem;
  63. elem = strchr(tl->text, ':');
  64. if (!elem)
  65. continue;
  66. elem[0] = '\0';
  67. elem++;
  68. elem = string_strip(elem);
  69. id = string_strip(tl->text);
  70. if (strcmp(id, "max_charge") == 0) {
  71. if (sscanf(elem, "%u", &value) == 1) {
  72. if ((int)value != bp->max_charge)
  73. value_changed = 1;
  74. bp->max_charge = (int)value;
  75. got_max_chg = 1;
  76. }
  77. }
  78. if (strcmp(id, "charge") == 0) {
  79. if (sscanf(elem, "%u", &value) == 1) {
  80. if ((int)value != bp->charge)
  81. value_changed = 1;
  82. bp->charge = (int)value;
  83. got_cur_chg = 1;
  84. }
  85. }
  86. if (got_max_chg && got_cur_chg)
  87. break;
  88. }
  89. text_lines_free(&lines);
  90. if (!got_ac)
  91. logerr("WARNING: Failed to get AC status\n");
  92. if (!got_max_chg)
  93. logerr("WARNING: Failed to get max battery charge value\n");
  94. if (!got_cur_chg)
  95. logerr("WARNING: Failed to get current battery charge value\n");
  96. if (value_changed)
  97. battery_notify_state_change(b);
  98. return 0;
  99. }
  100. static int battery_powerbook_on_ac(struct battery *b)
  101. {
  102. struct battery_powerbook *bp = container_of(b, struct battery_powerbook, battery);
  103. return bp->on_ac;
  104. }
  105. static int battery_powerbook_max_level(struct battery *b)
  106. {
  107. struct battery_powerbook *bp = container_of(b, struct battery_powerbook, battery);
  108. return bp->max_charge;
  109. }
  110. static int battery_powerbook_charge_level(struct battery *b)
  111. {
  112. struct battery_powerbook *bp = container_of(b, struct battery_powerbook, battery);
  113. return bp->charge;
  114. }
  115. static void battery_powerbook_destroy(struct battery *b)
  116. {
  117. struct battery_powerbook *bp = container_of(b, struct battery_powerbook, battery);
  118. file_close(bp->info_file);
  119. file_close(bp->stat_file);
  120. free(bp);
  121. }
  122. static struct battery * battery_powerbook_probe(void)
  123. {
  124. struct fileaccess *info, *stat;
  125. struct battery_powerbook *bp;
  126. info = procfs_file_open(O_RDONLY, "/pmu/info");
  127. stat = procfs_file_open(O_RDONLY, "/pmu/battery_0");
  128. if (!info || !stat)
  129. goto err_close;
  130. bp = zalloc(sizeof(*bp));
  131. if (!bp)
  132. goto err_close;
  133. bp->info_file = info;
  134. bp->stat_file = stat;
  135. battery_init(&bp->battery, "powerbook");
  136. bp->battery.destroy = battery_powerbook_destroy;
  137. bp->battery.update = battery_powerbook_update;
  138. bp->battery.on_ac = battery_powerbook_on_ac;
  139. bp->battery.max_level = battery_powerbook_max_level;
  140. bp->battery.charge_level = battery_powerbook_charge_level;
  141. bp->battery.poll_interval = 1000;
  142. return &bp->battery;
  143. err_close:
  144. file_close(info);
  145. file_close(stat);
  146. return NULL;
  147. }
  148. BATTERY_PROBE(powerbook, battery_powerbook_probe);