Bring idle poweroff to RaaA and the sim
[maemo-rb.git] / uisimulator / common / powermgmt-sim.c
blobe747d7d78272c186120fb46b4b137b4eba2609d1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Heikki Hannikainen, Uwe Freese
11 * Revisions copyright (C) 2005 by Gerald Van Baren
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include "config.h"
23 #include "system.h"
24 #include <time.h>
25 #include "kernel.h"
26 #include "powermgmt.h"
28 #define BATT_MINMVOLT 2500 /* minimum millivolts of battery */
29 #define BATT_MAXMVOLT 4500 /* maximum millivolts of battery */
30 #define BATT_MAXRUNTIME (10 * 60) /* maximum runtime with full battery in
31 minutes */
33 extern void send_battery_level_event(void);
34 extern int last_sent_battery_level;
35 extern int battery_percent;
37 static unsigned int battery_millivolts = BATT_MAXMVOLT;
38 /* estimated remaining time in minutes */
39 static int powermgmt_est_runningtime_min = BATT_MAXRUNTIME;
41 static void battery_status_update(void)
43 static time_t last_change = 0;
44 static bool charging = false;
45 time_t now;
47 time(&now);
49 if (last_change < now) {
50 last_change = now;
52 /* change the values: */
53 if (charging) {
54 if (battery_millivolts >= BATT_MAXMVOLT) {
55 /* Pretend the charger was disconnected */
56 charging = false;
57 queue_broadcast(SYS_CHARGER_DISCONNECTED, 0);
58 last_sent_battery_level = 100;
61 else {
62 if (battery_millivolts <= BATT_MINMVOLT) {
63 /* Pretend the charger was connected */
64 charging = true;
65 queue_broadcast(SYS_CHARGER_CONNECTED, 0);
66 last_sent_battery_level = 0;
70 if (charging) {
71 battery_millivolts += (BATT_MAXMVOLT - BATT_MINMVOLT) / 50;
73 else {
74 battery_millivolts -= (BATT_MAXMVOLT - BATT_MINMVOLT) / 100;
77 battery_percent = 100 * (battery_millivolts - BATT_MINMVOLT) /
78 (BATT_MAXMVOLT - BATT_MINMVOLT);
80 powermgmt_est_runningtime_min =
81 battery_percent * BATT_MAXRUNTIME / 100;
84 send_battery_level_event();
87 void battery_read_info(int *voltage, int *level)
89 battery_status_update();
91 if (voltage)
92 *voltage = battery_millivolts;
94 if (level)
95 *level = battery_percent;
98 unsigned int battery_voltage(void)
100 battery_status_update();
101 return battery_millivolts;
104 int battery_level(void)
106 battery_status_update();
107 return battery_percent;
110 int battery_time(void)
112 battery_status_update();
113 return powermgmt_est_runningtime_min;
116 bool battery_level_safe(void)
118 return battery_level() >= 10;
121 void set_battery_capacity(int capacity)
123 (void)capacity;
126 #if BATTERY_TYPES_COUNT > 1
127 void set_battery_type(int type)
129 (void)type;
131 #endif
133 #ifdef HAVE_ACCESSORY_SUPPLY
134 void accessory_supply_set(bool enable)
136 (void)enable;
138 #endif
140 #ifdef HAVE_LINEOUT_POWEROFF
141 void lineout_set(bool enable)
143 (void)enable;
145 #endif