battery.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.h"
  15. #include "log.h"
  16. #include "main.h"
  17. #include <stdint.h>
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include <errno.h>
  21. static int battery_get_charge_percent(struct battery *b)
  22. {
  23. int min_level = b->min_level(b);
  24. int max_level = b->max_level(b);
  25. int level = b->charge_level(b);
  26. int range, percent;
  27. if (min_level < 0 || max_level < 0 || level < 0 ||
  28. max_level <= min_level)
  29. return -1;
  30. range = max_level - min_level;
  31. level = max(0, level - min_level);
  32. if (range)
  33. percent = div_round((int64_t)level * 100, (int64_t)range);
  34. else
  35. percent = 0;
  36. return percent;
  37. }
  38. static int default_on_ac(struct battery *b)
  39. {
  40. return 0;
  41. }
  42. static int default_charger_enable(struct battery *b, int enable)
  43. {
  44. return -ENODEV;
  45. }
  46. static int default_charging(struct battery *b)
  47. {
  48. int on_ac = b->on_ac(b);
  49. int percent;
  50. percent = battery_get_charge_percent(b);
  51. if (on_ac < 0 || percent < 0)
  52. return -1;
  53. return on_ac && percent < 95;
  54. }
  55. static int default_min_level(struct battery *b)
  56. {
  57. return 0;
  58. }
  59. static int default_max_level(struct battery *b)
  60. {
  61. return 100;
  62. }
  63. static int default_charge_level(struct battery *b)
  64. {
  65. return 0;
  66. }
  67. static int default_capacity_mAh(struct battery *b)
  68. {
  69. return -ENODEV;
  70. }
  71. static int default_current_mA(struct battery *b)
  72. {
  73. return -ENODEV;
  74. }
  75. static int default_temperature_K(struct battery *b)
  76. {
  77. return -ENODEV;
  78. }
  79. static void battery_poll_callback(struct sleeptimer *timer)
  80. {
  81. struct battery *b = container_of(timer, struct battery, timer);
  82. b->update(b);
  83. sleeptimer_set_timeout_relative(&b->timer, b->poll_interval);
  84. sleeptimer_enqueue(&b->timer);
  85. }
  86. void battery_init(struct battery *b, const char *name)
  87. {
  88. memset(b, 0, sizeof(*b));
  89. b->name = name;
  90. b->on_ac = default_on_ac;
  91. b->charger_enable = default_charger_enable;
  92. b->charging = default_charging;
  93. b->min_level = default_min_level;
  94. b->max_level = default_max_level;
  95. b->charge_level = default_charge_level;
  96. b->capacity_mAh = default_capacity_mAh;
  97. b->current_mA = default_current_mA;
  98. b->temperature_K = default_temperature_K;
  99. }
  100. static void battery_start(struct battery *b)
  101. {
  102. if (b->poll_interval) {
  103. b->update(b);
  104. sleeptimer_init(&b->timer, "battery", battery_poll_callback);
  105. sleeptimer_set_timeout_relative(&b->timer, b->poll_interval);
  106. sleeptimer_enqueue(&b->timer);
  107. }
  108. }
  109. struct battery * battery_probe(void)
  110. {
  111. struct battery *b;
  112. const struct probe *probe;
  113. for_each_probe(probe, battery) {
  114. b = probe->func();
  115. if (b) {
  116. battery_start(b);
  117. logdebug("Initialized battery driver \"%s\"\n",
  118. b->name);
  119. return b;
  120. }
  121. }
  122. logerr("Failed to find a battery\n");
  123. return NULL;
  124. }
  125. void battery_destroy(struct battery *b)
  126. {
  127. if (b)
  128. b->destroy(b);
  129. }
  130. static void battery_emergency_check(struct battery *b)
  131. {
  132. int percent, threshold;
  133. int on_ac, charging;
  134. const char *command;
  135. pid_t pid;
  136. threshold = config_get_int(backend.config, "BATTERY",
  137. "emergency_threshold", 0);
  138. if (threshold <= 0) {
  139. /* Check disabled. */
  140. return;
  141. }
  142. threshold = min(threshold, 95);
  143. on_ac = b->on_ac(b);
  144. charging = b->charging(b);
  145. if (on_ac < 0 && charging < 0) {
  146. /* We don't know whether we are charging or draining
  147. * the battery. Assume charging and don't flag emergency.
  148. */
  149. return;
  150. }
  151. if (on_ac > 0 || charging > 0) {
  152. /* We are not draining the battery. */
  153. return;
  154. }
  155. percent = battery_get_charge_percent(b);
  156. if (percent < 0)
  157. return;
  158. if (percent > threshold) {
  159. /* No emergency status */
  160. b->emergency_handled = 0;
  161. return;
  162. }
  163. if (b->emergency_handled)
  164. return;
  165. loginfo("Battery emergency threshold reached. (level=%d%% <= threshold=%d%%)\n",
  166. percent, threshold);
  167. b->emergency_handled = 1;
  168. command = config_get(backend.config, "BATTERY",
  169. "emergency_command", NULL);
  170. if (strempty(command)) {
  171. logerr("No 'emergency_command' in 'BATTERY' section of "
  172. "config file. Ignoring emergency state.\n");
  173. return;
  174. }
  175. loginfo("Executing '%s'\n", command);
  176. pid = subprocess_exec(command);
  177. if (pid < 0) {
  178. logerr("Failed to execute 'emergency_command'.\n");
  179. return;
  180. }
  181. }
  182. int battery_fill_pt_message_stat(struct battery *b, struct pt_message *msg)
  183. {
  184. int on_ac = b->on_ac(b);
  185. int charging = b->charging(b);
  186. msg->bat_stat.flags = 0;
  187. if (on_ac > 0)
  188. msg->bat_stat.flags |= htonl(PT_BAT_FLG_ONAC);
  189. else if (on_ac < 0)
  190. msg->bat_stat.flags |= htonl(PT_BAT_FLG_ACUNKNOWN);
  191. if (charging > 0)
  192. msg->bat_stat.flags |= htonl(PT_BAT_FLG_CHARGING);
  193. else if (charging < 0)
  194. msg->bat_stat.flags |= htonl(PT_BAT_FLG_CHUNKNOWN);
  195. msg->bat_stat.min_level = htonl(b->min_level(b));
  196. msg->bat_stat.max_level = htonl(b->max_level(b));
  197. msg->bat_stat.level = htonl(b->charge_level(b));
  198. return 0;
  199. }
  200. int battery_notify_state_change(struct battery *b)
  201. {
  202. struct pt_message msg = {
  203. .id = htons(PTNOTI_BAT_CHANGED),
  204. };
  205. int err;
  206. battery_emergency_check(b);
  207. if (backend.autodim)
  208. autodim_handle_battery_event(backend.autodim);
  209. err = battery_fill_pt_message_stat(b, &msg);
  210. if (err)
  211. return err;
  212. notify_clients(&msg, PT_FLG_OK);
  213. return 0;
  214. }