4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include <ITapiModem.h>
22 #include <tapi_event.h>
23 #include <tapi_common.h>
24 #include <TapiCommon.h>
25 #include <TapiEvent.h>
26 #include <ITapiProductivity.h>
32 #include <device-node.h>
33 #include "dd-deviced.h"
35 #include "core/queue.h"
36 #include "core/predefine.h"
37 #include "core/data.h"
38 #include "core/common.h"
39 #include "core/devices.h"
40 #include "core/edbus-handler.h"
41 #include "display/core.h"
42 #include "power/power-handler.h"
44 #define PREDEF_FLIGHT_MODE "flightmode"
45 #define PREDEF_ENTERSLEEP "entersleep"
46 #define PREDEF_LEAVESLEEP "leavesleep"
48 #define POWER_RESTART 5
50 static TapiHandle *tapi_handle = NULL;
51 static Ecore_Timer *poweroff_timer_id = NULL;
52 static int reboot_opt;
54 static Eina_Bool telephony_powerdown_ap_internal(void *data)
59 static void telephony_powerdown_ap(TapiHandle *handle, const char *noti_id, void *data, void *user_data)
61 telephony_powerdown_ap_internal(data);
64 static void telephony_restart_ap(TapiHandle *handle, const char *noti_id, void *data, void *user_data)
66 restart_ap((void *)reboot_opt);
69 static Eina_Bool telephony_restart_ap_by_force(void *data)
71 if (poweroff_timer_id) {
72 ecore_timer_del(poweroff_timer_id);
73 poweroff_timer_id = NULL;
79 static void powerdown_res_cb(TapiHandle *handle, int result, void *data, void *user_data)
81 _D("poweroff command request : %d",result);
84 static Eina_Bool telephony_powerdown_ap_by_force(void *data)
86 if (poweroff_timer_id) {
87 ecore_timer_del(poweroff_timer_id);
88 poweroff_timer_id = NULL;
94 static int telephony_start(void)
99 _I("already initialized");
102 if (vconf_get_bool(VCONFKEY_TELEPHONY_READY,&ready) != 0 || ready != 1) {
103 _E("fail to get %s(%d)", VCONFKEY_TELEPHONY_READY, ready);
106 tapi_handle = tel_init(NULL);
107 if (tapi_handle == NULL) {
108 _E("tapi init error");
114 static int telephony_stop(void)
117 tel_deregister_noti_event(tapi_handle, TAPI_NOTI_MODEM_POWER);
118 ret = tel_deinit(tapi_handle);
120 _E("fail to deinit");
127 static void telephony_exit(void *data)
136 if (!strncmp(data, PREDEF_POWEROFF, strlen(PREDEF_POWEROFF))) {
138 ret = tel_register_noti_event(tapi_handle, TAPI_NOTI_MODEM_POWER,
139 telephony_powerdown_ap, NULL);
140 if (ret != TAPI_API_SUCCESS) {
141 _E("tel_register_event is not subscribed. error %d", ret);
142 telephony_powerdown_ap_by_force(NULL);
145 ret = tel_process_power_command(tapi_handle, TAPI_PHONE_POWER_OFF,
146 powerdown_res_cb, NULL);
147 if (ret != TAPI_API_SUCCESS) {
148 _E("tel_process_power_command() error %d\n", ret);
149 telephony_powerdown_ap_by_force(NULL);
152 poweroff_timer_id = ecore_timer_add(15,
153 telephony_powerdown_ap_internal, NULL);
157 if (strncmp(data, PREDEF_REBOOT, strlen(PREDEF_REBOOT)) &&
158 strncmp(data, PREDEF_RECOVERY, strlen(PREDEF_RECOVERY)) &&
159 strncmp(data, PREDEF_FOTA_REBOOT, strlen(PREDEF_FOTA_REBOOT))) {
164 _I("Option: %s", data);
165 if (!strncmp(data, PREDEF_RECOVERY, strlen(PREDEF_RECOVERY)))
166 reboot_opt = SYSTEMD_STOP_POWER_RESTART_RECOVERY;
167 else if (!strncmp(data, PREDEF_REBOOT, strlen(PREDEF_REBOOT)))
168 reboot_opt = SYSTEMD_STOP_POWER_RESTART;
169 else if (!strncmp(data, PREDEF_FOTA_REBOOT, strlen(PREDEF_FOTA_REBOOT)))
170 reboot_opt = SYSTEMD_STOP_POWER_RESTART_FOTA;
172 ret = tel_register_noti_event(tapi_handle, TAPI_NOTI_MODEM_POWER,
173 telephony_restart_ap, NULL);
174 if (ret != TAPI_API_SUCCESS) {
175 _E("tel_register_event is not subscribed. error %d", ret);
176 telephony_restart_ap_by_force((void *)POWER_RESTART);
179 ret = tel_process_power_command(tapi_handle, TAPI_PHONE_POWER_OFF,
180 powerdown_res_cb, NULL);
181 if (ret != TAPI_API_SUCCESS) {
182 _E("tel_process_power_command() error %d", ret);
183 telephony_restart_ap_by_force((void *)reboot_opt);
186 poweroff_timer_id = ecore_timer_add(15,telephony_restart_ap_by_force,
190 static void telephony_flight_mode_on(TapiHandle *handle, int result, void *data, void *user_data)
193 int bCurFlightMode = 0;
195 if (result != TAPI_POWER_FLIGHT_MODE_ENTER) {
196 _E("flight mode enter failed %d", result);
199 _D("enter flight mode result : %d", result);
200 ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &bCurFlightMode);
202 _D("Flight Mode is %d", bCurFlightMode);
204 _E("failed to get vconf key");
207 static void telephony_flight_mode_off(TapiHandle *handle, int result, void *data, void *user_data)
210 int bCurFlightMode = 0;
212 if (result != TAPI_POWER_FLIGHT_MODE_LEAVE) {
213 _E("flight mode leave failed %d", result);
216 _D("leave flight mode result : %d", result);
217 ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &bCurFlightMode);
219 _D("Flight Mode is %d", bCurFlightMode);
221 _E("failed to get vconf key");
224 static int telephony_flight_mode(int argc, char **argv)
228 int err = TAPI_API_SUCCESS;
230 if (argc != 1 || argv[0] == NULL) {
231 _E("FlightMode Set predefine action failed");
234 mode = atoi(argv[0]);
236 if (tapi_handle == NULL) {
237 ret = telephony_start();
239 _E("fail to get tapi handle");
245 err = tel_set_flight_mode(tapi_handle, TAPI_POWER_FLIGHT_MODE_LEAVE,
246 telephony_flight_mode_off, NULL);
247 } else if (mode == 0) {
248 err = tel_set_flight_mode(tapi_handle, TAPI_POWER_FLIGHT_MODE_ENTER,
249 telephony_flight_mode_on, NULL);
251 if (err != TAPI_API_SUCCESS)
252 _E("FlightMode tel api action failed %d",err);
257 static int telephony_enter_sleep(int argc, char **argv)
261 pm_change_internal(getpid(), LCD_NORMAL);
265 * TODO - add check, cb, etc...
266 * should be checked wirh telephony part */
267 ret = tel_set_flight_mode(tapi_handle, TAPI_POWER_FLIGHT_MODE_ENTER,
268 telephony_flight_mode_on, NULL);
269 _I("request for changing into flight mode : %d", ret);
271 launch_evenif_exist("/etc/rc.d/rc.entersleep", "");
272 pm_change_internal(getpid(), POWER_OFF);
277 static int telephony_leave_sleep(int argc, char **argv)
281 pm_change_internal(getpid(), LCD_NORMAL);
285 * TODO - add check, cb, etc...
286 * should be checked wirh telephony part */
287 ret = tel_set_flight_mode(tapi_handle, TAPI_POWER_FLIGHT_MODE_LEAVE,
288 telephony_flight_mode_off, NULL);
289 _I("request for changing into flight mode : %d", ret);
294 static DBusMessage *flight_mode_handler(E_DBus_Object *obj, DBusMessage *msg)
297 DBusMessageIter iter;
305 dbus_error_init(&err);
307 if (!dbus_message_get_args(msg, &err,
308 DBUS_TYPE_STRING, &type_str,
309 DBUS_TYPE_INT32, &argc,
310 DBUS_TYPE_STRING, &argv, DBUS_TYPE_INVALID)) {
311 _E("there is no message");
317 _E("message is invalid!");
322 pid = get_edbus_sender_pid(msg);
323 if (kill(pid, 0) == -1) {
324 _E("%d process does not exist, dbus ignored!", pid);
329 telephony_flight_mode(argc, (char **)&argv);
332 reply = dbus_message_new_method_return(msg);
333 dbus_message_iter_init_append(reply, &iter);
334 dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
339 static DBusMessage *telephony_handler(E_DBus_Object *obj, DBusMessage *msg)
342 DBusMessageIter iter;
349 dbus_error_init(&err);
351 if (!dbus_message_get_args(msg, &err,
352 DBUS_TYPE_STRING, &type_str,
353 DBUS_TYPE_INT32, &argc, DBUS_TYPE_INVALID)) {
354 _E("there is no message");
360 _E("message is invalid!");
365 pid = get_edbus_sender_pid(msg);
366 if (kill(pid, 0) == -1) {
367 _E("%d process does not exist, dbus ignored!", pid);
372 if (tapi_handle == NULL) {
373 if (telephony_start() != 0)
374 _E("fail to get tapi handle");
377 if (!strncmp(type_str, PREDEF_ENTERSLEEP, strlen(PREDEF_ENTERSLEEP)))
378 ret = telephony_enter_sleep(0, NULL);
379 else if (!strncmp(type_str, PREDEF_LEAVESLEEP, strlen(PREDEF_LEAVESLEEP)))
380 ret = telephony_leave_sleep(0, NULL);
383 reply = dbus_message_new_method_return(msg);
384 dbus_message_iter_init_append(reply, &iter);
385 dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
390 static const struct edbus_method edbus_methods[] = {
391 { PREDEF_FLIGHT_MODE, "sis", "i", flight_mode_handler },
392 { PREDEF_ENTERSLEEP, "si", "i", telephony_handler },
393 { PREDEF_LEAVESLEEP, "si", "i", telephony_handler },
394 /* Add methods here */
397 static void telephony_init(void *data)
401 /* init dbus interface */
402 ret = register_edbus_method(DEVICED_PATH_POWER, edbus_methods,
403 ARRAY_SIZE(edbus_methods));
405 _E("fail to init edbus method(%d)", ret);
407 register_action(PREDEF_FLIGHT_MODE, telephony_flight_mode, NULL, NULL);
408 register_action(PREDEF_ENTERSLEEP, telephony_enter_sleep, NULL, NULL);
409 register_action(PREDEF_LEAVESLEEP, telephony_leave_sleep, NULL, NULL);
412 static const struct device_ops tel_device_ops = {
413 .priority = DEVICE_PRIORITY_NORMAL,
415 .init = telephony_init,
416 .start = telephony_start,
417 .stop = telephony_stop,
418 .exit = telephony_exit,
421 DEVICE_OPS_REGISTER(&tel_device_ops)