adopt dbus cynara check
[platform/core/telephony/tel-plugin-dbus_tapi.git] / src / dtapi_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 <stdlib.h>
23
24 #include <glib.h>
25
26 #include <tcore.h>
27 #include <server.h>
28 #include <user_request.h>
29 #include <co_modem.h>
30
31 #include "generated-code.h"
32 #include "dtapi_common.h"
33
34 static gboolean on_modem_set_power(TelephonyModem *modem,
35         GDBusMethodInvocation *invocation, gint mode, gpointer user_data)
36 {
37         struct custom_data *ctx = user_data;
38         enum tcore_request_command command;
39
40         switch (mode) {
41         case MODEM_STATE_ONLINE:
42                 command = TREQ_MODEM_POWER_ON;
43         break;
44
45         case MODEM_STATE_OFFLINE:
46                 command = TREQ_MODEM_POWER_OFF;
47         break;
48
49         case MODEM_STATE_RESET:
50                 command = TREQ_MODEM_POWER_RESET;
51         break;
52
53         case MODEM_STATE_LOW:
54                 command = TREQ_MODEM_POWER_LOW;
55         break;
56
57         default:
58                 FAIL_RESPONSE(invocation, DEFAULT_MSG_REQ_FAILED);
59                 return TRUE;
60         }
61
62         /* Dispatch request */
63         dtapi_dispatch_request(ctx, modem, invocation,
64                 command,
65                 NULL, 0);
66
67         return TRUE;
68 }
69
70 static gboolean on_modem_set_flight_mode(TelephonyModem *modem,
71         GDBusMethodInvocation *invocation, gboolean enable, gpointer user_data)
72 {
73         struct treq_modem_set_flightmode req;
74         struct custom_data *ctx = user_data;
75
76         req.enable = enable;
77
78         /* Dispatch request */
79         dtapi_dispatch_request(ctx, modem, invocation,
80                 TREQ_MODEM_SET_FLIGHTMODE,
81                 &req, sizeof(struct treq_modem_set_flightmode));
82
83         return TRUE;
84 }
85
86 static gboolean on_modem_get_flight_mode(TelephonyModem *modem,
87         GDBusMethodInvocation *invocation, gpointer user_data)
88 {
89         struct custom_data *ctx = user_data;
90
91         /* Dispatch request */
92         dtapi_dispatch_request(ctx, modem, invocation,
93                 TREQ_MODEM_GET_FLIGHTMODE,
94                 NULL, 0);
95
96         return TRUE;
97 }
98
99 static gboolean on_modem_get_version(TelephonyModem *modem,
100         GDBusMethodInvocation *invocation, gpointer user_data)
101 {
102         struct custom_data *ctx = user_data;
103
104         /* Dispatch request */
105         dtapi_dispatch_request(ctx, modem, invocation,
106                 TREQ_MODEM_GET_VERSION,
107                 NULL, 0);
108
109         return TRUE;
110 }
111
112 static gboolean on_modem_get_serial_number(TelephonyModem *modem,
113         GDBusMethodInvocation *invocation, gpointer user_data)
114 {
115         struct custom_data *ctx = user_data;
116
117         /* Dispatch request */
118         dtapi_dispatch_request(ctx, modem, invocation,
119                 TREQ_MODEM_GET_SN,
120                 NULL, 0);
121
122         return TRUE;
123 }
124
125 static gboolean on_modem_get_imei(TelephonyModem *modem,
126         GDBusMethodInvocation *invocation, gpointer user_data)
127 {
128         struct custom_data *ctx = user_data;
129
130         /* Dispatch request */
131         dtapi_dispatch_request(ctx, modem, invocation,
132                 TREQ_MODEM_GET_IMEI,
133                 NULL, 0);
134
135         return TRUE;
136 }
137
138 static gboolean on_modem_set_dun_pin_ctrl(TelephonyModem *modem,
139         GDBusMethodInvocation *invocation,
140         gint signal, gboolean status, gpointer user_data)
141 {
142         struct treq_modem_set_dun_pin_control req;
143         struct custom_data *ctx = user_data;
144
145         req.signal = signal;
146         req.status = status;
147
148         /* Dispatch request */
149         dtapi_dispatch_request(ctx, modem, invocation,
150                 TREQ_MODEM_GET_VERSION,
151                 &req, sizeof(struct treq_modem_set_dun_pin_control));
152
153         return TRUE;
154 }
155
156 static gboolean on_modem_get_device_info(TelephonyModem *modem,
157         GDBusMethodInvocation *invocation, gpointer user_data)
158 {
159         struct custom_data *ctx = user_data;
160
161         /* Dispatch request */
162         dtapi_dispatch_request(ctx, modem, invocation,
163                 TREQ_MODEM_GET_DEVICE_INFO,
164                 NULL, 0);
165
166         return TRUE;
167 }
168
169 gboolean dbus_plugin_setup_modem_interface(TelephonyObjectSkeleton *object,
170         struct custom_data *ctx)
171 {
172         TelephonyModem *modem;
173
174         modem = telephony_modem_skeleton_new();
175         telephony_object_skeleton_set_modem(object, modem);
176         g_object_unref(modem);
177
178         dbg("modem: [%p]", modem);
179
180         /*
181          * Register signal handlers for Modem interface
182          */
183         g_signal_connect(modem,
184                 "handle-set-power",
185                 G_CALLBACK(on_modem_set_power), ctx);
186
187         g_signal_connect(modem,
188                 "handle-set-flight-mode",
189                 G_CALLBACK(on_modem_set_flight_mode), ctx);
190
191         g_signal_connect(modem,
192                 "handle-get-flight-mode",
193                 G_CALLBACK(on_modem_get_flight_mode), ctx);
194
195         g_signal_connect(modem,
196                 "handle-get-version",
197                 G_CALLBACK(on_modem_get_version), ctx);
198
199         g_signal_connect(modem,
200                 "handle-get-serial-number",
201                 G_CALLBACK(on_modem_get_serial_number), ctx);
202
203         g_signal_connect(modem,
204                 "handle-get-imei",
205                 G_CALLBACK(on_modem_get_imei), ctx);
206
207         g_signal_connect(modem,
208                 "handle-set-dun-pin-ctrl",
209                 G_CALLBACK(on_modem_set_dun_pin_ctrl), ctx);
210
211         g_signal_connect(modem,
212                 "handle-get-device-info",
213                 G_CALLBACK(on_modem_get_device_info), ctx);
214
215         /*
216          * Initialize 'properties'
217          */
218         telephony_modem_set_power(modem, MODEM_STATE_UNKNOWN);
219
220         return TRUE;
221 }
222
223 gboolean dbus_plugin_modem_response(struct custom_data *ctx,
224         UserRequest *ur, struct dbus_request_info *dbus_info,
225         enum tcore_response_command command, unsigned int data_len, const void *data)
226 {
227         char *cpname = GET_CP_NAME(dbus_info->invocation);
228
229         switch (command) {
230         case TRESP_MODEM_SET_FLIGHTMODE: {
231                 const struct tresp_modem_set_flightmode *resp_set_flight_mode = data;
232                 int set_flight_mode = 3;        /* TAPI_POWER_FLIGHT_MODE_RESP_FAIL */
233
234                 if (resp_set_flight_mode->result == TCORE_RETURN_SUCCESS) {
235                         const struct treq_modem_set_flightmode *treq_data;
236                         treq_data = tcore_user_request_ref_data(ur, NULL);
237                         if (treq_data == NULL) {
238                                 warn("No Request data!!!");
239                                 set_flight_mode = 3;    /* TAPI_POWER_FLIGHT_MODE_RESP_FAIL */
240                         } else if (treq_data->enable == TRUE) {
241                                 set_flight_mode = 1;    /* TAPI_POWER_FLIGHT_MODE_RESP_ON */
242                         } else {
243                                 set_flight_mode = 2;    /* TAPI_POWER_FLIGHT_MODE_RESP_OFF */
244                         }
245                 }
246
247                 dbg("[%s] SET_FLIGHTMODE - [%s] [%s]", cpname,
248                         (resp_set_flight_mode->result == TCORE_RETURN_SUCCESS ? "Success" : "Fail"),
249                         (set_flight_mode == 1 ? "ON" :
250                         (set_flight_mode == 2 ? "OFF" : "Request FAIL")));
251
252                 telephony_modem_complete_set_flight_mode(dbus_info->interface_object,
253                         dbus_info->invocation, set_flight_mode);
254         }
255         break;
256
257         case TRESP_MODEM_GET_FLIGHTMODE: {
258                 const struct tresp_modem_get_flightmode *resp_get_flight_mode = data;
259
260                 dbg("[%s] GET_FLIGHTMODE - [%s]", cpname,
261                         (resp_get_flight_mode->result == TCORE_RETURN_SUCCESS ? "Success" : "Fail"));
262
263                 telephony_modem_complete_get_flight_mode(dbus_info->interface_object,
264                         dbus_info->invocation,
265                         resp_get_flight_mode->enable, resp_get_flight_mode->result);
266         }
267         break;
268
269         case TRESP_MODEM_POWER_ON: {
270                 const struct tresp_modem_power_on *resp_modem_power_on = data;
271                 int result = 0;
272
273                 dbg("[%s] POWER_ON - [%s]", cpname,
274                         (resp_modem_power_on->result == TCORE_RETURN_SUCCESS ? "Success" : "Fail"));
275
276                 /* TBD: value should be defined in TAPI */
277                 if (resp_modem_power_on->result == TCORE_RETURN_EALREADY)
278                         result = 1;
279                 else if (resp_modem_power_on->result == TCORE_RETURN_OPERATION_ABORTED)
280                         result = 2;
281
282                 telephony_modem_complete_set_power(dbus_info->interface_object,
283                         dbus_info->invocation, result);
284         }
285         break;
286
287         case TRESP_MODEM_POWER_OFF: {
288                 dbg("[%s] POWER_OFF", cpname);
289
290                 telephony_modem_complete_set_power(dbus_info->interface_object,
291                         dbus_info->invocation, 0);
292         }
293         break;
294
295         case TRESP_MODEM_POWER_RESET: {
296                 dbg("[%s] POWER_RESET", cpname);
297
298                 telephony_modem_complete_set_power(dbus_info->interface_object,
299                         dbus_info->invocation, 0);
300         }
301         break;
302
303         case TRESP_MODEM_POWER_LOW: {
304                 dbg("[%s] POWER_LOW", cpname);
305
306                 telephony_modem_complete_set_power(dbus_info->interface_object,
307                         dbus_info->invocation, 0);
308         }
309         break;
310
311         case TRESP_MODEM_GET_IMEI: {
312                 const struct tresp_modem_get_imei *resp_get_imei = data;
313
314                 dbg("[%s] GET_IMEI - [%s]", cpname,
315                         (resp_get_imei->result == TCORE_RETURN_SUCCESS ? "Success" : "Fail"));
316
317                 telephony_modem_complete_get_imei(dbus_info->interface_object,
318                         dbus_info->invocation,
319                         resp_get_imei->result, resp_get_imei->imei);
320         }
321         break;
322
323         case TRESP_MODEM_GET_SN: {
324                 const struct tresp_modem_get_sn *resp_get_sn = data;
325
326                 dbg("[%s] GET_SN - [%s]", cpname,
327                         (resp_get_sn->result == TCORE_RETURN_SUCCESS ? "Success" : "Fail"));
328
329                 telephony_modem_complete_get_serial_number(dbus_info->interface_object,
330                         dbus_info->invocation, resp_get_sn->result,
331                         resp_get_sn->sn, resp_get_sn->meid,
332                         resp_get_sn->imei, resp_get_sn->imeisv);
333         }
334         break;
335
336         case TRESP_MODEM_GET_VERSION: {
337                 const struct tresp_modem_get_version *resp_get_version = data;
338
339                 dbg("[%s] GET_VERSION - [%s]", cpname,
340                         (resp_get_version->result == TCORE_RETURN_SUCCESS ? "Success" : "Fail"));
341
342                 telephony_modem_complete_get_version(dbus_info->interface_object,
343                         dbus_info->invocation, resp_get_version->result,
344                         resp_get_version->software, resp_get_version->hardware,
345                         resp_get_version->calibration, resp_get_version->product_code,
346                         resp_get_version->prl_version, resp_get_version->eri_version);
347         }
348         break;
349
350         case TRESP_MODEM_SET_DUN_PIN_CONTROL: {
351                 const struct tresp_modem_set_dun_pin_control *resp_dun_pin_ctrl = data;
352
353                 dbg("[%s] SET_DUN_PIN_CONTROL - [%s]", cpname,
354                         (resp_dun_pin_ctrl->result == TCORE_RETURN_SUCCESS ? "Success" : "Fail"));
355
356                 telephony_modem_complete_set_dun_pin_ctrl(dbus_info->interface_object,
357                         dbus_info->invocation, resp_dun_pin_ctrl->result);
358         }
359         break;
360
361         case TRESP_MODEM_GET_DEVICE_INFO: {
362                 const struct tresp_modem_get_device_info *resp_get_device_info = data;
363
364                 dbg("[%s] TGET_DEVICE_INFO - Vendor[%s] Device[%s]", cpname,
365                         resp_get_device_info->vendor_name,
366                         resp_get_device_info->device_name);
367
368                 telephony_modem_complete_get_device_info(dbus_info->interface_object,
369                         dbus_info->invocation, resp_get_device_info->result,
370                         resp_get_device_info->vendor_name, resp_get_device_info->device_name);
371         }
372         break;
373
374         default:
375                 err("Unhandled/Unknown Response: [0x%x]", command);
376         break;
377         }
378
379         return TRUE;
380 }
381
382 gboolean dbus_plugin_modem_notification(struct custom_data *ctx,
383         CoreObject *source, TelephonyObjectSkeleton *object,
384         enum tcore_notification_command command, unsigned int data_len, const void *data)
385 {
386         TelephonyModem *modem;
387         const char *cp_name;
388         cp_name = tcore_server_get_cp_name_by_plugin(tcore_object_ref_plugin(source));
389
390         if (!object) {
391                 dbg("object is NULL");
392                 return FALSE;
393         }
394         modem = telephony_object_peek_modem(TELEPHONY_OBJECT(object));
395         if (modem == NULL) {
396                 err("modem object is NULL!!!");
397                 return FALSE;
398         }
399
400         switch (command) {
401         case TNOTI_MODEM_POWER: {
402                 const struct tnoti_modem_power *info = data;
403                 enum modem_state state = info->state;
404
405                 dbg("[%s] MODEM_POWER: [%s]", cp_name,
406                         (state == MODEM_STATE_UNKNOWN ? "UNKNOWN" :
407                         (state == MODEM_STATE_ONLINE ? "ONLINE" :
408                         (state == MODEM_STATE_OFFLINE ? "OFFLINE" :
409                         (state == MODEM_STATE_RESET ? "RESET" :
410                         (state == MODEM_STATE_LOW ? "LOW" :
411                         "INTERNAL STATE"))))));
412                 if (state > MODEM_STATE_MAX)
413                         break;
414
415                 telephony_modem_emit_power(modem, state);
416                 telephony_modem_set_power(modem, state);
417         }
418         break;
419
420         case TNOTI_MODEM_DUN_PIN_CONTROL: {
421                 const struct tnoti_modem_dun_pin_control *pin = data;
422
423                 dbg("[%s] MODEM_DUN_PIN_CONTROL (Signal: [0x%2x] Status: [0x%2x])",
424                         cp_name, pin->signal, pin->status);
425
426                 telephony_modem_emit_dun_pin_ctrl(modem, pin->signal, pin->status);
427         }
428         break;
429
430         case TNOTI_MODEM_DUN_EXTERNAL_CALL: {
431                 dbg("[%s] MODEM_DUN_EXTERNAL_CALL", cp_name);
432
433                 telephony_modem_emit_dun_external_call(modem, TRUE);
434         }
435         break;
436
437         case TNOTI_MODEM_DONGLE_STATUS:
438                 dbg("[%s] MODEM_DONGLE_STATUS (state:[%d])", cp_name, *(int *)data);
439                 telephony_modem_set_dongle_status(modem,  *(int *)data);
440         break;
441
442         case TNOTI_MODEM_DONGLE_LOGIN:
443                 dbg("[%s] MODEM_DONGLE_LOGIN (login state:[%d])", cp_name, *(int *)data);
444                 telephony_modem_set_dongle_login(modem,  *(int *)data);
445         break;
446
447         default:
448                 err("Unhandled/Unknown Notification: [0x%x]", command);
449         break;
450         }
451
452         return TRUE;
453 }
454