e4cf5a7809df6f9d9a3783e609c23643301e0a80
[samsung-ril.git] / pwr.c
1 /**
2  * This file is part of samsung-ril.
3  *
4  * Copyright (C) 2010-2011 Joerie de Gram <j.de.gram@gmail.com>
5  * Copyright (C) 2011-2012 Paul Kocialkowski <contact@paulk.fr>
6  *
7  * samsung-ril is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * samsung-ril is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with samsung-ril.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21
22 #define LOG_TAG "RIL-PWR"
23 #include <utils/Log.h>
24
25 #include "samsung-ril.h"
26 #include "util.h"
27
28 /**
29  * Out: RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED
30  *   Modem lets us know it's powered on. Though, it's still in LPM and should
31  *   be considered as OFF. Send this to update RILJ radio state (OFF)
32  */
33 void ipc_pwr_phone_pwr_up(void)
34 {
35         ril_data.state.radio_state = RADIO_STATE_OFF;
36         ril_data.state.power_state = IPC_PWR_PHONE_STATE_LPM;
37
38         ril_request_unsolicited(RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED, NULL, 0);
39 }
40
41 /**
42  * In: IPC_PWR_PHONE_STATE
43  *   Noti from the modem giving current power mode (LPM or NORMAL)
44  *   LPM = Low Power Mode (airplane mode for instance)
45  *
46  * Out: RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED
47  *   Update radio state according to modem power state
48  */
49 void ipc_pwr_phone_state(struct ipc_message_info *info)
50 {
51         uint8_t state = *((uint8_t *) info->data);
52
53         switch(state)
54         {
55                 case IPC_PWR_R(IPC_PWR_PHONE_STATE_LPM):
56                         LOGD("Got power to LPM");
57
58                         if (ril_data.state.power_state == IPC_PWR_PHONE_STATE_NORMAL &&
59                                 ril_data.tokens.radio_power != (RIL_Token) 0x00) {
60                                 ril_request_complete(ril_data.tokens.radio_power, RIL_E_SUCCESS, NULL, 0);
61                                 ril_data.tokens.radio_power = (RIL_Token) 0x00;
62                         }
63
64                         ril_data.state.radio_state = RADIO_STATE_OFF;
65                         ril_data.state.power_state = IPC_PWR_PHONE_STATE_LPM;
66                         ril_request_unsolicited(RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED, NULL, 0);
67                 break;
68                 case IPC_PWR_R(IPC_PWR_PHONE_STATE_NORMAL):
69                         LOGD("Got power to NORMAL");
70
71                         if (ril_data.state.power_state == IPC_PWR_PHONE_STATE_LPM &&
72                                 ril_data.tokens.radio_power != (RIL_Token) 0x00) {
73                                 ril_request_complete(ril_data.tokens.radio_power, RIL_E_SUCCESS, NULL, 0);
74                                 ril_data.tokens.radio_power = (RIL_Token) 0x00;
75                         }
76
77                         ril_data.state.radio_state = RADIO_STATE_SIM_NOT_READY;
78                         ril_data.state.power_state = IPC_PWR_PHONE_STATE_NORMAL;
79                         ril_request_unsolicited(RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED, NULL, 0);
80                 break;
81         }
82
83         ril_tokens_check();
84 }
85
86 /**
87  * In: RIL_REQUEST_RADIO_POWER
88  *   Request ON or OFF radio power mode
89  *
90  * Out: IPC_PWR_PHONE_STATE
91  *   Order the modem to get in required power mode
92  */
93 void ril_request_radio_power(RIL_Token t, void *data, size_t datalen)
94 {
95         int power_state = *((int *)data);
96         unsigned short power_data;
97
98         LOGD("requested power_state is %d", power_state);
99
100         if (power_state > 0) {
101                 LOGD("Request power to NORMAL");
102                 power_data = IPC_PWR_PHONE_STATE_NORMAL;
103
104                 ipc_gen_phone_res_expect_to_abort(ril_request_get_id(t), IPC_PWR_PHONE_STATE);
105                 ipc_fmt_send(IPC_PWR_PHONE_STATE, IPC_TYPE_EXEC, (void *) &power_data, sizeof(power_data), ril_request_get_id(t));
106
107                 ril_data.tokens.radio_power = t;
108         } else {
109                 LOGD("Request power to LPM");
110                 power_data = IPC_PWR_PHONE_STATE_LPM;
111
112                 ipc_gen_phone_res_expect_to_abort(ril_request_get_id(t), IPC_PWR_PHONE_STATE);
113                 ipc_fmt_send(IPC_PWR_PHONE_STATE, IPC_TYPE_EXEC, (void *) &power_data, sizeof(power_data), ril_request_get_id(t));
114
115                 ril_data.tokens.radio_power = t;
116         }
117 }