pwr: Handle phone reset message
[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_START_UNLOCK();
39 }
40
41 void ipc_pwr_phone_reset(void)
42 {
43         ril_data.state.radio_state = RADIO_STATE_OFF;
44         ril_request_unsolicited(RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED, NULL, 0);
45 }
46
47 /**
48  * In: IPC_PWR_PHONE_STATE
49  *   Noti from the modem giving current power mode (LPM or NORMAL)
50  *   LPM = Low Power Mode (airplane mode for instance)
51  *
52  * Out: RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED
53  *   Update radio state according to modem power state
54  */
55 void ipc_pwr_phone_state(struct ipc_message_info *info)
56 {
57         uint8_t state = *((uint8_t *) info->data);
58
59         switch(state)
60         {
61                 case IPC_PWR_R(IPC_PWR_PHONE_STATE_LPM):
62                         LOGD("Got power to LPM");
63
64                         if (ril_data.tokens.radio_power != (RIL_Token) 0x00) {
65                                 ril_request_complete(ril_data.tokens.radio_power, RIL_E_SUCCESS, NULL, 0);
66                                 ril_data.tokens.radio_power = (RIL_Token) 0x00;
67                         }
68
69                         ril_data.state.radio_state = RADIO_STATE_OFF;
70                         ril_data.state.power_state = IPC_PWR_PHONE_STATE_LPM;
71                         ril_request_unsolicited(RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED, NULL, 0);
72                 break;
73                 case IPC_PWR_R(IPC_PWR_PHONE_STATE_NORMAL):
74                         LOGD("Got power to NORMAL");
75
76                         if (ril_data.tokens.radio_power != (RIL_Token) 0x00) {
77                                 ril_request_complete(ril_data.tokens.radio_power, RIL_E_SUCCESS, NULL, 0);
78                                 ril_data.tokens.radio_power = (RIL_Token) 0x00;
79                         }
80
81                         ril_data.state.radio_state = RADIO_STATE_SIM_NOT_READY;
82                         ril_data.state.power_state = IPC_PWR_PHONE_STATE_NORMAL;
83                         ril_request_unsolicited(RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED, NULL, 0);
84                 break;
85         }
86
87         ril_tokens_check();
88 }
89
90 /**
91  * In: RIL_REQUEST_RADIO_POWER
92  *   Request ON or OFF radio power mode
93  *
94  * Out: IPC_PWR_PHONE_STATE
95  *   Order the modem to get in required power mode
96  */
97 void ril_request_radio_power(RIL_Token t, void *data, size_t datalen)
98 {
99         int power_state = *((int *)data);
100         unsigned short power_data;
101
102         LOGD("requested power_state is %d", power_state);
103
104         if (power_state > 0) {
105                 LOGD("Request power to NORMAL");
106                 power_data = IPC_PWR_PHONE_STATE_NORMAL;
107
108                 ipc_gen_phone_res_expect_to_abort(ril_request_get_id(t), IPC_PWR_PHONE_STATE);
109                 ipc_fmt_send(IPC_PWR_PHONE_STATE, IPC_TYPE_EXEC, (void *) &power_data, sizeof(power_data), ril_request_get_id(t));
110
111                 ril_data.tokens.radio_power = t;
112         } else {
113                 LOGD("Request power to LPM");
114                 power_data = IPC_PWR_PHONE_STATE_LPM;
115
116                 ipc_gen_phone_res_expect_to_abort(ril_request_get_id(t), IPC_PWR_PHONE_STATE);
117                 ipc_fmt_send(IPC_PWR_PHONE_STATE, IPC_TYPE_EXEC, (void *) &power_data, sizeof(power_data), ril_request_get_id(t));
118
119                 ril_data.tokens.radio_power = t;
120         }
121 }