Code Sync up from tizen_2.4
[platform/core/telephony/tel-plugin-dbus_tapi.git] / src / modem.c
1 /*
2  * tel-plugin-dbus-tapi
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Ja-young Gu <jygu@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <pthread.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #include <glib.h>
28 #include <gio/gio.h>
29
30 #include <tcore.h>
31 #include <server.h>
32 #include <plugin.h>
33 #include <hal.h>
34 #include <communicator.h>
35 #include <storage.h>
36 #include <queue.h>
37 #include <user_request.h>
38 #include <co_modem.h>
39
40 #include "generated-code.h"
41 #include "common.h"
42
43
44 static gboolean
45 on_modem_set_power (TelephonyModem *modem, GDBusMethodInvocation *invocation,
46         gint mode, gpointer user_data)
47 {
48         struct custom_data *ctx = user_data;
49         UserRequest *ur = NULL;
50         enum tcore_request_command command;
51         TReturn ret = TCORE_RETURN_SUCCESS;
52
53         if (!check_access_control (invocation, AC_MODEM, "w"))
54                 return TRUE;
55
56         ur = MAKE_UR(ctx, modem, invocation);
57         if (ur == NULL)
58                 goto ERR;
59
60         switch (mode) {
61         case MODEM_STATE_ONLINE:
62                 command = TREQ_MODEM_POWER_ON;
63         break;
64         case MODEM_STATE_OFFLINE:
65                 command = TREQ_MODEM_POWER_OFF;
66         break;
67         case MODEM_STATE_RESET:
68                 command = TREQ_MODEM_POWER_RESET;
69         break;
70         case MODEM_STATE_LOW:
71                 command = TREQ_MODEM_POWER_LOW;
72         break;
73         default:
74                 ret = TCORE_RETURN_EINVAL;
75                 goto ERR;
76         }
77
78         tcore_user_request_set_command(ur, command);
79         ret = tcore_communicator_dispatch_request(ctx->comm, ur);
80         if (ret != TCORE_RETURN_SUCCESS)
81                 goto ERR;
82
83         return TRUE;
84
85 ERR:
86         FAIL_RESPONSE (invocation, DEFAULT_MSG_REQ_FAILED);
87         tcore_user_request_unref(ur);
88
89         return TRUE;
90 }
91
92 static gboolean
93 on_modem_set_flight_mode (TelephonyModem *modem, GDBusMethodInvocation *invocation,
94         gboolean enable, gpointer user_data)
95 {
96         struct treq_modem_set_flightmode data;
97         struct custom_data *ctx = user_data;
98         UserRequest *ur = NULL;
99         TReturn ret;
100
101         if (!check_access_control (invocation, AC_MODEM, "w"))
102                 return TRUE;
103
104         ur = MAKE_UR(ctx, modem, invocation);
105         if (ur == NULL)
106                 goto ERR;
107
108         data.enable = enable;
109         if (TCORE_RETURN_SUCCESS != tcore_user_request_set_data (ur, sizeof(data), &data))
110                 goto ERR;
111         tcore_user_request_set_command (ur, TREQ_MODEM_SET_FLIGHTMODE);
112
113         ret = tcore_communicator_dispatch_request (ctx->comm, ur);
114         if (ret != TCORE_RETURN_SUCCESS)
115                 goto ERR;
116
117         return TRUE;
118
119 ERR:
120         FAIL_RESPONSE (invocation, DEFAULT_MSG_REQ_FAILED);
121         tcore_user_request_unref(ur);
122
123         return TRUE;
124 }
125
126 static gboolean
127 on_modem_get_flight_mode (TelephonyModem *modem, GDBusMethodInvocation *invocation,
128         gpointer user_data)
129 {
130         struct custom_data *ctx = user_data;
131         UserRequest *ur = NULL;
132         TReturn ret;
133
134         if (!check_access_control (invocation, AC_MODEM, "r"))
135                 return TRUE;
136
137         ur = MAKE_UR(ctx, modem, invocation);
138         if (ur == NULL)
139                 goto ERR;
140
141         tcore_user_request_set_data(ur, 0, NULL);
142         tcore_user_request_set_command(ur, TREQ_MODEM_GET_FLIGHTMODE);
143         ret = tcore_communicator_dispatch_request(ctx->comm, ur);
144         if (ret != TCORE_RETURN_SUCCESS)
145                 goto ERR;
146
147         return TRUE;
148
149 ERR:
150         FAIL_RESPONSE (invocation, DEFAULT_MSG_REQ_FAILED);
151         tcore_user_request_unref(ur);
152
153         return TRUE;
154 }
155
156 static gboolean
157 on_modem_get_version (TelephonyModem *modem, GDBusMethodInvocation *invocation,
158         gpointer user_data)
159 {
160         struct custom_data *ctx = user_data;
161         UserRequest *ur = NULL;
162         TReturn ret;
163
164         if (!check_access_control (invocation, AC_MODEM, "r"))
165                 return TRUE;
166
167         ur = MAKE_UR(ctx, modem, invocation);
168         if (ur == NULL)
169                 goto ERR;
170
171         tcore_user_request_set_command(ur, TREQ_MODEM_GET_VERSION);
172         ret = tcore_communicator_dispatch_request(ctx->comm, ur);
173         if (ret != TCORE_RETURN_SUCCESS)
174                 goto ERR;
175
176         return TRUE;
177
178 ERR:
179         FAIL_RESPONSE (invocation, DEFAULT_MSG_REQ_FAILED);
180         tcore_user_request_unref(ur);
181
182         return TRUE;
183 }
184
185 static gboolean
186 on_modem_get_serial_number (TelephonyModem *modem, GDBusMethodInvocation *invocation,
187         gpointer user_data)
188 {
189         struct custom_data *ctx = user_data;
190         UserRequest *ur = NULL;
191         TReturn ret;
192
193         if (!check_access_control (invocation, AC_MODEM, "r"))
194                 return TRUE;
195
196         ur = MAKE_UR(ctx, modem, invocation);
197         if (ur == NULL)
198                 goto ERR;
199
200         tcore_user_request_set_command(ur, TREQ_MODEM_GET_SN);
201         ret = tcore_communicator_dispatch_request(ctx->comm, ur);
202         if (ret != TCORE_RETURN_SUCCESS)
203                 goto ERR;
204
205         return TRUE;
206
207 ERR:
208         FAIL_RESPONSE (invocation, DEFAULT_MSG_REQ_FAILED);
209         tcore_user_request_unref(ur);
210
211         return TRUE;
212 }
213
214 static gboolean
215 on_modem_get_imei (TelephonyModem *modem, GDBusMethodInvocation *invocation,
216         gpointer user_data)
217 {
218         struct custom_data *ctx = user_data;
219         UserRequest *ur = NULL;
220         TReturn ret;
221
222         if (!check_access_control (invocation, AC_MODEM, "r"))
223                 return TRUE;
224
225         ur = MAKE_UR(ctx, modem, invocation);
226         if (ur == NULL)
227                 goto ERR;
228
229         tcore_user_request_set_command(ur, TREQ_MODEM_GET_IMEI);
230         ret = tcore_communicator_dispatch_request(ctx->comm, ur);
231         if (ret != TCORE_RETURN_SUCCESS)
232                 goto ERR;
233
234         return TRUE;
235
236 ERR:
237         FAIL_RESPONSE (invocation, DEFAULT_MSG_REQ_FAILED);
238         tcore_user_request_unref(ur);
239
240         return TRUE;
241 }
242
243 static gboolean on_modem_set_dun_pin_ctrl (TelephonyModem *modem, GDBusMethodInvocation *invocation,
244         gint arg_signal, gboolean arg_status, gpointer user_data)
245 {
246         struct treq_modem_set_dun_pin_control data;
247         struct custom_data *ctx = user_data;
248         UserRequest *ur = NULL;
249         TReturn ret;
250
251         if (!check_access_control (invocation, AC_MODEM, "w"))
252                 return TRUE;
253
254         ur = MAKE_UR(ctx, modem, invocation);
255         if (ur == NULL)
256                 goto ERR;
257
258         data.signal = arg_signal;
259         data.status = arg_status;
260
261         if (TCORE_RETURN_SUCCESS != tcore_user_request_set_data(ur, sizeof(data), &data))
262                 goto ERR;
263         tcore_user_request_set_command(ur, TREQ_MODEM_SET_DUN_PIN_CONTROL);
264         ret = tcore_communicator_dispatch_request(ctx->comm, ur);
265         if (ret != TCORE_RETURN_SUCCESS)
266                 goto ERR;
267
268         return TRUE;
269
270 ERR:
271         FAIL_RESPONSE (invocation, DEFAULT_MSG_REQ_FAILED);
272         tcore_user_request_unref(ur);
273
274         return TRUE;
275 }
276
277 gboolean dbus_plugin_setup_modem_interface(TelephonyObjectSkeleton *object, struct custom_data *ctx)
278 {
279         TelephonyModem *modem;
280
281         modem = telephony_modem_skeleton_new();
282         telephony_object_skeleton_set_modem(object, modem);
283         g_object_unref(modem);
284
285         dbg("modem: [%p]", modem);
286
287         g_signal_connect (modem,
288                         "handle-set-power",
289                         G_CALLBACK (on_modem_set_power),
290                         ctx);
291
292         g_signal_connect (modem,
293                         "handle-set-flight-mode",
294                         G_CALLBACK (on_modem_set_flight_mode),
295                         ctx);
296
297         g_signal_connect (modem,
298                         "handle-get-flight-mode",
299                         G_CALLBACK (on_modem_get_flight_mode),
300                         ctx);
301
302         g_signal_connect (modem,
303                         "handle-get-version",
304                         G_CALLBACK (on_modem_get_version),
305                         ctx);
306
307         g_signal_connect (modem,
308                         "handle-get-serial-number",
309                         G_CALLBACK (on_modem_get_serial_number),
310                         ctx);
311
312         g_signal_connect (modem,
313                         "handle-get-imei",
314                         G_CALLBACK (on_modem_get_imei),
315                         ctx);
316
317         g_signal_connect (modem,
318                         "handle-set-dun-pin-ctrl",
319                         G_CALLBACK (on_modem_set_dun_pin_ctrl),
320                         ctx);
321
322         telephony_modem_set_power(modem, MODEM_STATE_UNKNOWN);
323
324         return TRUE;
325 }
326
327 gboolean dbus_plugin_modem_response(struct custom_data *ctx, UserRequest *ur,
328         struct dbus_request_info *dbus_info, enum tcore_response_command command,
329         unsigned int data_len, const void *data)
330 {
331         dbg("Response!!! Command: [0x%x] CP Name: [%s]",
332                 command, GET_CP_NAME(dbus_info->invocation));
333
334         switch (command) {
335         case TRESP_MODEM_SET_FLIGHTMODE: {
336                 const struct tresp_modem_set_flightmode *resp_set_flight_mode = data;
337                 int info_set_flight_mode = 3;   /* TAPI_POWER_FLIGHT_MODE_RESP_FAIL */
338
339                 dbg("TRESP_MODEM_SET_FLIGHTMODE - Result: [%s]",
340                         (resp_set_flight_mode->result == TCORE_RETURN_SUCCESS ? "Success" : "Fail"));
341
342                 if (resp_set_flight_mode->result == TCORE_RETURN_SUCCESS) {
343                         const struct treq_modem_set_flightmode *treq_data;
344                         treq_data = tcore_user_request_ref_data(ur, NULL);
345                         if (treq_data == NULL) {
346                                 warn("No Request data!!!");
347                                 info_set_flight_mode = 3;       /* TAPI_POWER_FLIGHT_MODE_RESP_FAIL */
348                         }
349                         else if (treq_data->enable == TRUE) {
350                                 info_set_flight_mode = 1;       /* TAPI_POWER_FLIGHT_MODE_RESP_ON */
351                         } else {
352                                 info_set_flight_mode = 2;       /* TAPI_POWER_FLIGHT_MODE_RESP_OFF */
353                         }
354                 }
355                 dbg("Set Flight mode: [%s]", (info_set_flight_mode == 1 ? "ON"
356                         : (info_set_flight_mode == 2 ? "OFF" : "Request FAIL")));
357
358                 telephony_modem_complete_set_flight_mode(dbus_info->interface_object, dbus_info->invocation,
359                         info_set_flight_mode);
360         }
361         break;
362
363         case TRESP_MODEM_GET_FLIGHTMODE: {
364                 const struct tresp_modem_get_flightmode *resp_get_flight_mode = data;
365
366                 dbg("TRESP_MODEM_GET_FLIGHTMODE - Result: [%s]",
367                         (resp_get_flight_mode->result == TCORE_RETURN_SUCCESS ? "Success" : "Fail"));
368
369                 telephony_modem_complete_get_flight_mode(dbus_info->interface_object, dbus_info->invocation,
370                         resp_get_flight_mode->enable, resp_get_flight_mode->result);
371         }
372         break;
373
374         case TRESP_MODEM_POWER_ON: {
375                 const struct tresp_modem_power_on *resp_modem_power_on = data;
376                 int result = 0;
377
378                 dbg("TRESP_MODEM_POWER_ON - Result: [%s]",
379                         (resp_modem_power_on->result == TCORE_RETURN_SUCCESS ? "Success" : "Fail"));
380
381                 /*TBD: value should be defined in TAPI*/
382                 if(resp_modem_power_on->result == TCORE_RETURN_EALREADY)
383                         result = 1;
384                 else if(resp_modem_power_on->result == TCORE_RETURN_OPERATION_ABORTED)
385                         result = 2;
386
387                 telephony_modem_complete_set_power(dbus_info->interface_object, dbus_info->invocation, result);
388         }
389         break;
390
391         case TRESP_MODEM_POWER_OFF: {
392                 dbg("TRESP_MODEM_POWER_OFF");
393
394                 telephony_modem_complete_set_power(dbus_info->interface_object, dbus_info->invocation, 0);
395         }
396         break;
397
398         case TRESP_MODEM_POWER_RESET: {
399                 dbg("TRESP_MODEM_POWER_RESET");
400
401                 telephony_modem_complete_set_power(dbus_info->interface_object, dbus_info->invocation, 0);
402         }
403         break;
404
405         case TRESP_MODEM_POWER_LOW: {
406                 dbg("TRESP_MODEM_POWER_LOW");
407
408                 telephony_modem_complete_set_power(dbus_info->interface_object, dbus_info->invocation, 0);
409         }
410         break;
411
412         case TRESP_MODEM_GET_IMEI: {
413                 const struct tresp_modem_get_imei *resp_get_imei = data;
414
415                 dbg("TRESP_MODEM_GET_IMEI - Result: [%s]",
416                         (resp_get_imei->result == TCORE_RETURN_SUCCESS ? "Success" : "Fail"));
417
418                 telephony_modem_complete_get_imei(dbus_info->interface_object, dbus_info->invocation,
419                         resp_get_imei->result, resp_get_imei->imei);
420         }
421         break;
422
423         case TRESP_MODEM_GET_SN: {
424                 const struct tresp_modem_get_sn *resp_get_sn = data;
425
426                 dbg("TRESP_MODEM_GET_SN - Result: [%s]",
427                         (resp_get_sn->result == TCORE_RETURN_SUCCESS ? "Success" : "Fail"));
428
429                 telephony_modem_complete_get_serial_number(dbus_info->interface_object, dbus_info->invocation,
430                         resp_get_sn->result, resp_get_sn->sn, resp_get_sn->meid, resp_get_sn->imei, resp_get_sn->imeisv);
431         }
432         break;
433
434         case TRESP_MODEM_GET_VERSION: {
435                 const struct tresp_modem_get_version *resp_get_version = data;
436
437                 dbg("TRESP_MODEM_GET_VERSION - Result: [%s]",
438                         (resp_get_version->result == TCORE_RETURN_SUCCESS ? "Success" : "Fail"));
439
440                 telephony_modem_complete_get_version(dbus_info->interface_object, dbus_info->invocation,
441                                 resp_get_version->result,
442                                 resp_get_version->software,
443                                 resp_get_version->hardware,
444                                 resp_get_version->calibration,
445                                 resp_get_version->product_code,
446                                 resp_get_version->prl_version,
447                                 resp_get_version->eri_version);
448         }
449         break;
450
451         case TRESP_MODEM_SET_DUN_PIN_CONTROL: {
452                 const struct tresp_modem_set_dun_pin_control *resp_dun_pin_ctrl = data;
453
454                 dbg("TRESP_MODEM_SET_DUN_PIN_CONTROL - Result: [%s]",
455                         (resp_dun_pin_ctrl->result == TCORE_RETURN_SUCCESS ? "Success" : "Fail"));
456
457                 telephony_modem_complete_set_dun_pin_ctrl(dbus_info->interface_object, dbus_info->invocation,
458                         resp_dun_pin_ctrl->result);
459         }
460         break;
461
462         default:
463                 err("Unhandled/Unknown Response!!!");
464         break;
465         }
466
467         return TRUE;
468 }
469
470 gboolean dbus_plugin_modem_notification(struct custom_data *ctx, CoreObject *source,
471         TelephonyObjectSkeleton *object, enum tcore_notification_command command,
472         unsigned int data_len, const void *data)
473 {
474         TelephonyModem *modem;
475
476         if (!object) {
477                 dbg("object is NULL");
478                 return FALSE;
479         }
480         dbg("Notification!!! Command: [0x%x] CP Name: [%s]",
481                 command, tcore_server_get_cp_name_by_plugin(tcore_object_ref_plugin(source)));
482
483         modem = telephony_object_peek_modem(TELEPHONY_OBJECT(object));
484         dbg("modem: [%p]", modem);
485
486         switch (command) {
487         case TNOTI_MODEM_POWER: {
488                 const struct tnoti_modem_power *info = data;
489
490                 dbg("TNOTI_MODEM_POWER - Modem state: [%d]", info->state);
491
492                 if (info->state > MODEM_STATE_MAX)
493                         break;
494
495                 telephony_modem_emit_power(modem, info->state);
496                 telephony_modem_set_power(modem, info->state);
497         }
498         break;
499
500         case TNOTI_MODEM_DUN_PIN_CONTROL: {
501                 const struct tnoti_modem_dun_pin_control *pin = data;
502
503                 dbg("TNOTI_MODEM_DUN_PIN_CONTROL - Signal: [0x%2x] Status: [0x%2x]", pin->signal, pin->status);
504
505                 telephony_modem_emit_dun_pin_ctrl(modem, pin->signal, pin->status);
506         }
507         break;
508
509         case TNOTI_MODEM_DUN_EXTERNAL_CALL: {
510                 dbg("TNOTI_MODEM_DUN_EXTERNAL_CALL");
511
512                 telephony_modem_emit_dun_external_call(modem, TRUE);
513         }
514         break;
515
516         default:
517                 err("Unhandled/Unknown Notification!!!");
518         break;
519         }
520
521         return TRUE;
522 }
523