add exception code for g_variant_iter_free()
[profile/ivi/tel-plugin-dbus_tapi.git] / src / modem.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <pthread.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <time.h>
7 #include <glib.h>
8 #include <glib-object.h>
9 #include <gio/gio.h>
10
11 #include <tcore.h>
12 #include <server.h>
13 #include <plugin.h>
14 #include <hal.h>
15 #include <communicator.h>
16 #include <storage.h>
17 #include <queue.h>
18 #include <user_request.h>
19 #include <co_modem.h>
20
21 #include "generated-code.h"
22 #include "common.h"
23
24 static gboolean
25 on_modem_set_power (TelephonyModem *modem,
26                 GDBusMethodInvocation *invocation,
27                 gint mode,
28                 gpointer user_data)
29 {
30         struct custom_data *ctx = user_data;
31         UserRequest *ur = NULL;
32         TReturn ret = TCORE_RETURN_SUCCESS;
33
34         ur = MAKE_UR(ctx, modem, invocation);
35         tcore_user_request_set_data(ur, 0, NULL);
36
37         switch (mode) {
38                 case 0:
39                         tcore_user_request_set_command(ur, TREQ_MODEM_POWER_OFF);
40                         break;
41                 case 1:
42                         tcore_user_request_set_command(ur, TREQ_MODEM_POWER_ON);
43                         break;
44                 case 2:
45                         tcore_user_request_set_command(ur, TREQ_MODEM_POWER_RESET);
46                         break;
47                 default:
48                         ret = TCORE_RETURN_EINVAL;
49                         goto ERR;
50                         break;
51         }
52
53         ret = tcore_communicator_dispatch_request(ctx->comm, ur);
54         if (ret != TCORE_RETURN_SUCCESS)
55                 goto ERR;
56
57         return TRUE;
58
59 ERR:
60         telephony_modem_complete_set_power(modem, invocation, ret);
61
62         return TRUE;
63 }
64
65 static gboolean
66 on_modem_set_flight_mode (TelephonyModem *modem,
67                 GDBusMethodInvocation *invocation,
68                 gboolean enable,
69                 gpointer user_data)
70 {
71         struct treq_modem_set_flightmode data;
72         struct custom_data *ctx = user_data;
73         UserRequest *ur = NULL;
74         TReturn ret;
75
76         data.enable = enable;
77
78         ur = MAKE_UR(ctx, modem, invocation);
79         tcore_user_request_set_data(ur, sizeof(struct treq_modem_set_flightmode), &data);
80         tcore_user_request_set_command(ur, TREQ_MODEM_SET_FLIGHTMODE);
81         ret = tcore_communicator_dispatch_request(ctx->comm, ur);
82         if (ret != TCORE_RETURN_SUCCESS) {
83                 telephony_modem_complete_set_flight_mode(modem, invocation, ret);
84                 tcore_user_request_unref(ur);
85         }
86
87         return TRUE;
88 }
89
90 static gboolean
91 on_modem_get_version (TelephonyModem *modem,
92                 GDBusMethodInvocation *invocation,
93                 gpointer user_data)
94 {
95         struct custom_data *ctx = user_data;
96         UserRequest *ur = NULL;
97         TReturn ret;
98
99         ur = MAKE_UR(ctx, modem, invocation);
100         tcore_user_request_set_command(ur, TREQ_MODEM_GET_VERSION);
101         ret = tcore_communicator_dispatch_request(ctx->comm, ur);
102         if (ret != TCORE_RETURN_SUCCESS) {
103                 telephony_modem_complete_get_version(modem, invocation,
104                                 ret,
105                                 NULL, NULL, NULL, NULL);
106                 tcore_user_request_unref(ur);
107         }
108
109         return TRUE;
110 }
111
112 static gboolean
113 on_modem_get_serial_number (TelephonyModem *modem,
114                 GDBusMethodInvocation *invocation,
115                 gpointer user_data)
116 {
117         struct custom_data *ctx = user_data;
118         UserRequest *ur = NULL;
119         TReturn ret;
120
121         ur = MAKE_UR(ctx, modem, invocation);
122         tcore_user_request_set_command(ur, TREQ_MODEM_GET_SN);
123         ret = tcore_communicator_dispatch_request(ctx->comm, ur);
124         if (ret != TCORE_RETURN_SUCCESS) {
125                 telephony_modem_complete_get_serial_number(modem, invocation, ret, NULL);
126                 tcore_user_request_unref(ur);
127         }
128
129         return TRUE;
130 }
131
132 static gboolean
133 on_modem_get_imei (TelephonyModem *modem,
134                 GDBusMethodInvocation *invocation,
135                 gpointer user_data)
136 {
137         struct custom_data *ctx = user_data;
138         UserRequest *ur = NULL;
139         TReturn ret;
140
141         ur = MAKE_UR(ctx, modem, invocation);
142         tcore_user_request_set_command(ur, TREQ_MODEM_GET_IMEI);
143         ret = tcore_communicator_dispatch_request(ctx->comm, ur);
144         if (ret != TCORE_RETURN_SUCCESS) {
145                 telephony_modem_complete_get_imei(modem, invocation, ret, NULL);
146                 tcore_user_request_unref(ur);
147         }
148
149         return TRUE;
150 }
151
152 static gboolean on_modem_set_dun_pin_ctrl (TelephonyModem *modem, GDBusMethodInvocation *invocation,
153                 gint arg_signal, gboolean arg_status, gpointer user_data)
154 {
155         struct treq_modem_set_dun_pin_control data;
156         struct custom_data *ctx = user_data;
157         UserRequest *ur = NULL;
158         TReturn ret;
159
160         data.signal = arg_signal;
161         data.status = arg_status;
162
163         ur = MAKE_UR(ctx, modem, invocation);
164         tcore_user_request_set_data(ur, sizeof(struct treq_modem_set_dun_pin_control), &data);
165         tcore_user_request_set_command(ur, TREQ_MODEM_SET_DUN_PIN_CONTROL);
166         ret = tcore_communicator_dispatch_request(ctx->comm, ur);
167         if (ret != TCORE_RETURN_SUCCESS) {
168                 telephony_modem_complete_set_dun_pin_ctrl(modem, invocation, ret);
169                 tcore_user_request_unref(ur);
170         }
171
172         return TRUE;
173 }
174
175 gboolean dbus_plugin_setup_modem_interface(TelephonyObjectSkeleton *object, struct custom_data *ctx)
176 {
177         TelephonyModem *modem;
178
179         modem = telephony_modem_skeleton_new();
180         telephony_object_skeleton_set_modem(object, modem);
181         g_object_unref(modem);
182
183         g_signal_connect (modem,
184                         "handle-set-power",
185                         G_CALLBACK (on_modem_set_power),
186                         ctx);
187
188         g_signal_connect (modem,
189                         "handle-set-flight-mode",
190                         G_CALLBACK (on_modem_set_flight_mode),
191                         ctx);
192
193         g_signal_connect (modem,
194                         "handle-get-version",
195                         G_CALLBACK (on_modem_get_version),
196                         ctx);
197
198         g_signal_connect (modem,
199                         "handle-get-serial-number",
200                         G_CALLBACK (on_modem_get_serial_number),
201                         ctx);
202
203         g_signal_connect (modem,
204                         "handle-get-imei",
205                         G_CALLBACK (on_modem_get_imei),
206                         ctx);
207
208         g_signal_connect (modem,
209                         "handle-set-dun-pin-ctrl",
210                         G_CALLBACK (on_modem_set_dun_pin_ctrl),
211                         ctx);
212
213
214         return TRUE;
215 }
216
217 gboolean dbus_plugin_modem_response(struct custom_data *ctx, UserRequest *ur, struct dbus_request_info *dbus_info, enum tcore_response_command command, unsigned int data_len, const void *data)
218 {
219         const struct tresp_modem_set_flightmode *resp_set_flight_mode = data;
220         const struct tresp_modem_set_dun_pin_control *resp_dun_pin_ctrl = data;
221         const struct tresp_modem_get_imei *resp_get_imei = data;
222         const struct tresp_modem_get_sn *resp_get_sn = data;
223         const struct tresp_modem_get_version *resp_get_version = data;
224
225         switch (command) {
226                 case TRESP_MODEM_SET_FLIGHTMODE:
227                         dbg("receive TRESP_MODEM_SET_FLIGHTMODE");
228                         dbg("resp->result = %d", resp_set_flight_mode->result);
229                         telephony_modem_complete_set_flight_mode(dbus_info->interface_object, dbus_info->invocation, resp_set_flight_mode->result);
230                         break;
231
232                 case TRESP_MODEM_POWER_ON:
233                         dbg("receive TRESP_MODEM_POWER_ON");
234                         telephony_modem_complete_set_power(dbus_info->interface_object, dbus_info->invocation, 0);
235                         break;
236
237                 case TRESP_MODEM_POWER_OFF:
238                         dbg("receive TRESP_MODEM_POWER_OFF");
239                         telephony_modem_complete_set_power(dbus_info->interface_object, dbus_info->invocation, 0);
240                         break;
241
242                 case TRESP_MODEM_POWER_RESET:
243                         dbg("receive TRESP_MODEM_POWER_RESET");
244                         telephony_modem_complete_set_power(dbus_info->interface_object, dbus_info->invocation, 0);
245                         break;
246
247                 case TRESP_MODEM_GET_IMEI:
248                         dbg("receive TRESP_MODEM_GET_IMEI");
249                         telephony_modem_complete_get_imei(dbus_info->interface_object, dbus_info->invocation, resp_get_imei->result, resp_get_imei->imei);
250                         break;
251
252                 case TRESP_MODEM_GET_SN:
253                         dbg("receive TRESP_MODEM_GET_SN");
254                         telephony_modem_complete_get_serial_number(dbus_info->interface_object, dbus_info->invocation, resp_get_sn->result, resp_get_sn->sn);
255                         break;
256
257                 case TRESP_MODEM_GET_VERSION:
258                         dbg("receive TRESP_MODEM_GET_VERSION");
259                         telephony_modem_complete_get_version(dbus_info->interface_object, dbus_info->invocation,
260                                         resp_get_version->result,
261                                         resp_get_version->software,
262                                         resp_get_version->hardware,
263                                         resp_get_version->calibration,
264                                         resp_get_version->product_code);
265                         break;
266
267                 case TRESP_MODEM_SET_DUN_PIN_CONTROL:
268                         dbg("receive TRESP_MODEM_SET_DUN_PIN_CONTROL");
269                         dbg("resp->result = %d", resp_dun_pin_ctrl->result);
270                         telephony_modem_complete_set_dun_pin_ctrl(dbus_info->interface_object, dbus_info->invocation, resp_dun_pin_ctrl->result);
271                         break;
272
273                 default:
274                         dbg("not handled command[%d]", command);
275                 break;
276         }
277
278         return TRUE;
279 }
280
281 gboolean dbus_plugin_modem_notification(struct custom_data *ctx, const char *plugin_name, TelephonyObjectSkeleton *object, enum tcore_notification_command command, unsigned int data_len, const void *data)
282 {
283         TelephonyModem *modem;
284         const struct tnoti_modem_power *info = data;
285         const struct tnoti_modem_dun_pin_control *pin = data;
286
287         if (!object) {
288                 dbg("object is NULL");
289                 return FALSE;
290         }
291
292         modem = telephony_object_peek_modem(TELEPHONY_OBJECT(object));
293         dbg("modem = %p", modem);
294
295         switch (command) {
296                 case TNOTI_MODEM_POWER:
297                         dbg("modem->state = %d", info->state);
298                         telephony_modem_emit_power(modem, info->state);
299                         telephony_modem_set_power(modem, info->state);
300                         break;
301
302                 case TNOTI_MODEM_DUN_PIN_CONTROL:
303                         dbg("modem dun pin ctrl noti signal(%d), status(%d)", pin->signal, pin->status);
304                         telephony_modem_emit_dun_pin_ctrl(modem, pin->signal, pin->status);
305                         break;
306
307                 case TNOTI_MODEM_DUN_EXTERNAL_CALL:
308                         dbg("modem dun external call noti");
309                         telephony_modem_emit_dun_external_call(modem, TRUE);
310                         break;
311
312                 default:
313                         dbg("not handled command[0x%x]", command);
314                 break;
315         }
316
317         return TRUE;
318 }
319