AT: Better failure handling and removed wrong unlock
[hayes-ril.git] / power.c
1 /*
2  * This file is part of hayes-ril.
3  *
4  * Copyright (C) 2012 Paul Kocialkowski <contact@paulk.fr>
5  *
6  * Parts of this code are based on htcgeneric-ril, reference-ril:
7  * Copyright 2006-2011, htcgeneric-ril contributors
8  * Copyright 2006, The Android Open Source Project
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *     http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22
23 #define LOG_TAG "RIL-PWR"
24 #include <utils/Log.h>
25 #include <telephony/ril.h>
26
27 #include <hayes-ril.h>
28
29 int at_cfun_enable_expect(struct at_response *response, void *data, RIL_Token t)
30 {
31         if(response->status == AT_STATUS_UNDEF)
32                 return AT_RESPONSE_UNHANDELD_REASON_STATUS;
33
34         LOGD("Updating radio state from AT+CFUN response");
35
36         switch(response->status) {
37                 case AT_STATUS_OK:
38                         ril_globals.radio_state = RADIO_STATE_SIM_NOT_READY;
39                         RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED, NULL, 0);
40                         break;
41         }
42
43         return AT_RESPONSE_HANDLED_OK;
44 }
45
46 int at_cfun_disable_expect(struct at_response *response, void *data, RIL_Token t)
47 {
48         if(response->status == AT_STATUS_UNDEF)
49                 return AT_RESPONSE_UNHANDELD_REASON_STATUS;
50
51         LOGD("Updating radio state from AT+CFUN response");
52
53         switch(response->status) {
54                 case AT_STATUS_OK:
55                         ril_globals.radio_state = RADIO_STATE_OFF;
56                         RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED, NULL, 0);
57                         break;
58         }
59
60         return AT_RESPONSE_HANDLED_OK;
61 }
62
63 void ril_power_on(void)
64 {
65         LOGD("Powering on");
66
67         at_send_expect_to_func("AT+CFUN=1", NULL, NULL, 0x0000, at_cfun_enable_expect);
68
69         ril_device_at_power_on(ril_device);
70 }
71
72 void ril_power_off(void)
73 {
74         LOGD("Powering off");
75
76         ril_device_at_power_off(ril_device);
77
78         at_send_expect_to_func("AT+CFUN=0", NULL, NULL, 0x0000, at_cfun_disable_expect);
79 }
80
81 void ril_request_radio_power(RIL_Token t, void *data, size_t length)
82 {
83         int power_state = *((int *)data);
84         int rc;
85
86         if(power_state > 0) {
87                 ril_power_on();
88
89                 // Ask for PIN status
90                 at_send_expect_to_func("AT+CPIN?", NULL, NULL, 0x0000, at_cpin_expect);
91         } else {
92                 ril_power_off();
93         }
94
95         RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
96 }