+#if defined(LINUX) || defined(__NetBSD__)
+/*
+ * Add batt_info data to acc.
+ */
+static void add_battery_info(struct battery_info *acc, const struct battery_info *batt_info) {
+ if (acc->remaining < 0) {
+ /* initialize accumulator so we can add to it */
+ acc->full_design = 0;
+ acc->full_last = 0;
+ acc->remaining = 0;
+ acc->present_rate = 0;
+ }
+
+ acc->full_design += batt_info->full_design;
+ acc->full_last += batt_info->full_last;
+ acc->remaining += batt_info->remaining;
+
+ /* make present_rate negative for discharging and positive for charging */
+ int present_rate = (acc->status == CS_DISCHARGING ? -1 : 1) * acc->present_rate;
+ present_rate += (batt_info->status == CS_DISCHARGING ? -1 : 1) * batt_info->present_rate;
+
+ /* merge status */
+ switch (acc->status) {
+ case CS_UNKNOWN:
+ acc->status = batt_info->status;
+ break;
+
+ case CS_DISCHARGING:
+ if (present_rate > 0)
+ acc->status = CS_CHARGING;
+ /* else if batt_info is DISCHARGING: no conflict
+ * else if batt_info is CHARGING: present_rate should indicate that
+ * else if batt_info is FULL: but something else is discharging */
+ break;
+
+ case CS_CHARGING:
+ if (present_rate < 0)
+ acc->status = CS_DISCHARGING;
+ /* else if batt_info is DISCHARGING: present_rate should indicate that
+ * else if batt_info is CHARGING: no conflict
+ * else if batt_info is FULL: but something else is charging */
+ break;
+
+ case CS_FULL:
+ if (batt_info->status != CS_UNKNOWN)
+ acc->status = batt_info->status;
+ /* else: retain FULL, since it is more specific than UNKNOWN */
+ break;
+ }
+
+ acc->present_rate = abs(present_rate);
+}
+#endif
+