8d6279a484c8d8b0c0b3c12272fdd2d88a0a3ed3
[framework/telephony/tel-plugin-packetservice.git] / src / tcore-interface.c
1 /*
2  * tel-plugin-packetservice
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: DongHoo Park <donghoo.park@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
22 #include "ps.h"
23
24 #include <server.h>
25 #include <plugin.h>
26 #include <storage.h>
27 #include <co_ps.h>
28 #include <co_modem.h>
29 #include <co_sim.h>
30 #include <co_network.h>
31
32 static enum tcore_hook_return __on_hook_call_status(Server *s, CoreObject *source,
33                 enum tcore_notification_command command, unsigned int data_len, void *data,
34                 void *user_data)
35 {
36         gpointer service = user_data;
37         struct tnoti_ps_call_status *cstatus = NULL;
38         char *ifname;
39         gboolean netif_updown = FALSE;
40         GSList *contexts;
41         CoreObject *co_context;
42
43         dbg("call status event");
44         g_return_val_if_fail(service != NULL, TCORE_HOOK_RETURN_STOP_PROPAGATION);
45
46         cstatus = (struct tnoti_ps_call_status *) data;
47         dbg("call status event cid(%d) state(%d)",
48                         cstatus->context_id, cstatus->state);
49
50         if (cstatus->state == PS_DATA_CALL_CTX_DEFINED)
51                 goto out;
52         else if (cstatus->state == PS_DATA_CALL_CONNECTED)
53                 netif_updown = TRUE;
54         else if (cstatus->state == PS_DATA_CALL_NOT_CONNECTED)
55                 netif_updown = FALSE;
56
57         contexts = tcore_ps_ref_context_by_id(source, cstatus->context_id);
58         for (; contexts != NULL; contexts = g_slist_next(contexts)) {
59                 co_context = contexts->data;
60                 ifname = tcore_context_get_ipv4_devname(co_context);
61
62                 if (ifname == NULL)
63                         continue;
64
65                 if (tcore_util_netif(ifname, netif_updown)
66                                         != TCORE_RETURN_SUCCESS) {
67                         g_slist_free(contexts);
68                         g_free(ifname);
69                         dbg("tcore_util_netif() failed.");
70                 }
71         }
72
73 out:
74         //send activation event / deactivation event
75         if (cstatus->state == PS_DATA_CALL_CTX_DEFINED) {/* OK: PDP define is complete. */
76                 dbg("service is ready to activate");
77                 _ps_service_set_ps_defined(service, TRUE, cstatus->context_id);
78                 //_ps_service_connect_default_context(service);
79         }
80         else if (cstatus->state == PS_DATA_CALL_CONNECTED) {/* CONNECTED */
81                 dbg("service is activated");
82                 _ps_service_set_connected(service, cstatus->context_id, TRUE);
83         }
84         else if (cstatus->state == PS_DATA_CALL_NOT_CONNECTED) { /* NO CARRIER */
85                 dbg("service is deactivated");
86                 _ps_service_set_ps_defined(service, FALSE, cstatus->context_id);
87                 _ps_service_set_connected(service, cstatus->context_id, FALSE);
88         }
89
90         return TCORE_HOOK_RETURN_CONTINUE;
91 }
92
93 static enum tcore_hook_return __on_hook_session_data_counter(Server *s, CoreObject *source,
94                 enum tcore_notification_command command, unsigned int data_len, void *data,
95                 void *user_data)
96 {
97         g_return_val_if_fail(user_data != NULL, TCORE_HOOK_RETURN_STOP_PROPAGATION);
98
99         dbg("session data counter event");
100
101         return TCORE_HOOK_RETURN_CONTINUE;
102 }
103
104 static enum tcore_hook_return __on_hook_ipconfiguration(Server *s, CoreObject *source,
105                 enum tcore_notification_command command, unsigned int data_len, void *data,
106                 void *user_data)
107 {
108         gpointer service = user_data;
109         CoreObject *co_ps = NULL;
110         struct tnoti_ps_pdp_ipconfiguration *devinfo = NULL;
111
112         g_return_val_if_fail(service != NULL, TCORE_HOOK_RETURN_STOP_PROPAGATION);
113
114         devinfo = (struct tnoti_ps_pdp_ipconfiguration *) data;
115         co_ps = (CoreObject *) _ps_service_ref_co_ps(service);
116
117         if (co_ps != source) {
118                 dbg("ps object is different");
119                 return TCORE_HOOK_RETURN_CONTINUE;
120         }
121
122         dbg("ip configuration event");
123         _ps_service_set_context_info(service, devinfo);
124
125         return TCORE_HOOK_RETURN_CONTINUE;
126 }
127
128 static enum tcore_hook_return __on_hook_powered(Server *s, CoreObject *source,
129                 enum tcore_notification_command command, unsigned int data_len, void *data, void *user_data)
130 {
131         gpointer modem = user_data;
132         struct tnoti_modem_power *modem_power = NULL;
133
134         gboolean power = FALSE;
135
136         dbg("powered event called");
137
138         g_return_val_if_fail(modem != NULL, TCORE_HOOK_RETURN_STOP_PROPAGATION);
139
140         modem_power = (struct tnoti_modem_power *)data;
141
142         if ( modem_power->state == MODEM_STATE_ONLINE )
143                 power = TRUE;
144         else
145                 power = FALSE;
146
147         _ps_modem_processing_power_enable(modem, power);
148
149         return TCORE_HOOK_RETURN_CONTINUE;
150 }
151
152 static enum tcore_hook_return __on_hook_flight(Server *s, CoreObject *source,
153                 enum tcore_notification_command command, unsigned int data_len, void *data, void *user_data)
154 {
155         gpointer modem = user_data;
156         struct tnoti_modem_flight_mode *modem_flight = NULL;
157         dbg("flight event called");
158
159         g_return_val_if_fail(modem != NULL, TCORE_HOOK_RETURN_STOP_PROPAGATION);
160
161         modem_flight = (struct tnoti_modem_flight_mode *)data;
162         _ps_modem_processing_flight_mode(modem, modem_flight->enable);
163
164         return TCORE_HOOK_RETURN_CONTINUE;
165 }
166
167 static enum tcore_hook_return __on_hook_net_register(Server *s, CoreObject *source,
168                 enum tcore_notification_command command, unsigned int data_len, void *data,
169                 void *user_data)
170 {
171         gpointer service = user_data;
172         gboolean ps_attached = FALSE;
173         struct tnoti_network_registration_status *regist_status;
174
175         dbg("network register event called");
176         g_return_val_if_fail(service != NULL, TCORE_HOOK_RETURN_STOP_PROPAGATION);
177
178         regist_status = (struct tnoti_network_registration_status *) data;
179         if (regist_status->ps_domain_status == NETWORK_SERVICE_DOMAIN_STATUS_FULL)
180                 ps_attached = TRUE;
181
182         _ps_service_processing_network_event(service, ps_attached, regist_status->roaming_status);
183
184         return TCORE_HOOK_RETURN_CONTINUE;
185 }
186
187 static enum tcore_hook_return __on_hook_net_change(Server *s, CoreObject *source,
188                 enum tcore_notification_command command, unsigned int data_len, void *data,
189                 void *user_data)
190 {
191         gpointer service = user_data;
192         struct tnoti_network_change *network_change;
193
194         dbg("network change event called");
195         g_return_val_if_fail(service != NULL, TCORE_HOOK_RETURN_STOP_PROPAGATION);
196
197         network_change = (struct tnoti_network_change *) data;
198         dbg("plmn(%s) act(%d)", network_change->plmn, network_change->act);
199         _ps_service_set_access_technology(service, network_change->act);
200
201         return TCORE_HOOK_RETURN_CONTINUE;
202 }
203
204 static enum tcore_hook_return __on_hook_sim_init(Server *s, CoreObject *source,
205                 enum tcore_notification_command command, unsigned int data_len, void *data, void *user_data)
206 {
207         struct tnoti_sim_status *sim_data;
208
209         dbg("sim init event called");
210         g_return_val_if_fail(user_data != NULL, TCORE_HOOK_RETURN_STOP_PROPAGATION);
211
212         sim_data = (struct tnoti_sim_status *)data;
213         dbg("sim status is (%d)", sim_data->sim_status);
214
215         if( sim_data->sim_status == SIM_STATUS_INIT_COMPLETED){
216                 struct tel_sim_imsi *sim_imsi = NULL;
217                 sim_imsi = tcore_sim_get_imsi(source);
218                 _ps_modem_processing_sim_complete( (gpointer)user_data, TRUE, (gchar *)sim_imsi->plmn);
219                 g_free(sim_imsi);
220         }
221
222         return TCORE_HOOK_RETURN_CONTINUE;
223 }
224
225 gboolean _ps_hook_co_modem_event(gpointer modem)
226 {
227         Server *s = NULL;
228         TcorePlugin *p;
229         g_return_val_if_fail(modem != NULL, FALSE);
230
231         p = _ps_modem_ref_plugin(modem);
232         s = tcore_plugin_ref_server(p);
233
234         tcore_server_add_notification_hook(s, TNOTI_MODEM_POWER, __on_hook_powered, modem);
235         tcore_server_add_notification_hook(s, TNOTI_MODEM_FLIGHT_MODE, __on_hook_flight, modem);
236         tcore_server_add_notification_hook(s, TNOTI_SIM_STATUS, __on_hook_sim_init, modem);
237
238         return TRUE;
239 }
240
241 gboolean _ps_get_co_modem_values(gpointer modem)
242 {
243         TcorePlugin *plg;
244         CoreObject *co_modem = NULL;
245         CoreObject *co_sim = NULL;
246
247         gboolean sim_init = FALSE, modem_powered = FALSE, flight_mode = FALSE;
248         int sim_status = 0;
249         struct tel_sim_imsi *sim_imsi = NULL;
250
251         g_return_val_if_fail(modem != NULL, FALSE);
252
253         co_modem = _ps_modem_ref_co_modem(modem);
254         if (!co_modem)
255                 return FALSE;
256
257         plg = tcore_object_ref_plugin(co_modem);
258         if (!plg)
259                 return FALSE;
260
261         co_sim = tcore_plugin_ref_core_object(plg, CORE_OBJECT_TYPE_SIM);
262         if (!co_sim)
263                 return FALSE;
264
265         sim_status = tcore_sim_get_status(co_sim);
266         if(sim_status == SIM_STATUS_INIT_COMPLETED)
267                 sim_init = TRUE;
268
269         sim_imsi = tcore_sim_get_imsi(co_sim);
270         modem_powered = tcore_modem_get_powered(co_modem);
271         flight_mode = tcore_modem_get_flight_mode_state(co_modem);
272
273         _ps_modem_processing_flight_mode(modem, flight_mode);
274         _ps_modem_processing_power_enable(modem, modem_powered);
275         _ps_modem_processing_sim_complete(modem, sim_init, (gchar *)sim_imsi->plmn);
276
277         g_free(sim_imsi);
278         return TRUE;
279 }
280
281 gboolean _ps_hook_co_network_event(gpointer service)
282 {
283         Server *s = NULL;
284         TcorePlugin *p;
285
286         g_return_val_if_fail(service != NULL, FALSE);
287
288         p = _ps_service_ref_plugin(service);
289         s = tcore_plugin_ref_server(p);
290
291         tcore_server_add_notification_hook(s, TNOTI_NETWORK_REGISTRATION_STATUS, __on_hook_net_register, service);
292         tcore_server_add_notification_hook(s, TNOTI_NETWORK_CHANGE, __on_hook_net_change, service);
293
294         return TRUE;
295 }
296
297 gboolean _ps_get_co_network_values(gpointer service)
298 {
299         CoreObject *co_network = NULL;
300         gboolean ps_attached = FALSE;
301
302         enum telephony_network_service_domain_status ps_status;
303         enum telephony_network_access_technology act;
304
305         g_return_val_if_fail(service != NULL, FALSE);
306
307         co_network = _ps_service_ref_co_network(service);
308
309         tcore_network_get_service_status(co_network, TCORE_NETWORK_SERVICE_DOMAIN_TYPE_PACKET, &ps_status);
310         tcore_network_get_access_technology(co_network, &act);
311
312         if (ps_status == NETWORK_SERVICE_DOMAIN_STATUS_FULL)
313                 ps_attached = TRUE;
314
315         _ps_service_set_roaming(service, tcore_network_get_roaming_state(co_network));
316         _ps_service_set_ps_attached(service, ps_attached);
317         _ps_service_set_access_technology(service, act);
318
319         return TRUE;
320 }
321
322 gboolean _ps_hook_co_ps_event(gpointer service)
323 {
324         Server *s = NULL;
325         TcorePlugin *p;
326         g_return_val_if_fail(service != NULL, FALSE);
327
328         p = _ps_service_ref_plugin(service);
329         s = tcore_plugin_ref_server(p);
330
331         tcore_server_add_notification_hook(s, TNOTI_PS_CALL_STATUS, __on_hook_call_status, service);
332         tcore_server_add_notification_hook(s, TNOTI_PS_CURRENT_SESSION_DATA_COUNTER, __on_hook_session_data_counter, service);
333         tcore_server_add_notification_hook(s, TNOTI_PS_PDP_IPCONFIGURATION, __on_hook_ipconfiguration, service);
334
335         return TRUE;
336 }
337
338 gboolean _ps_free_co_ps_event(gpointer service)
339 {
340         Server *s = NULL;
341         TcorePlugin *p;
342         g_return_val_if_fail(service != NULL, FALSE);
343
344         p = _ps_service_ref_plugin(service);
345         s = tcore_plugin_ref_server(p);
346
347         tcore_server_remove_notification_hook(s, __on_hook_call_status);
348         tcore_server_remove_notification_hook(s, __on_hook_session_data_counter);
349         tcore_server_remove_notification_hook(s, __on_hook_ipconfiguration);
350
351         return TRUE;
352 }
353
354 gboolean _ps_free_co_network_event(gpointer service)
355 {
356         Server *s = NULL;
357         TcorePlugin *p;
358         g_return_val_if_fail(service != NULL, FALSE);
359
360         p = _ps_service_ref_plugin(service);
361         s = tcore_plugin_ref_server(p);
362
363         tcore_server_remove_notification_hook(s, __on_hook_net_register);
364         tcore_server_remove_notification_hook(s, __on_hook_net_change);
365
366         return TRUE;
367 }
368
369 gboolean _ps_update_cellular_state_key(gpointer service)
370 {
371         Server *s = NULL;
372         gpointer handle = NULL;
373         static Storage *strg;
374         int err_reason = 0;
375
376         s = tcore_plugin_ref_server( (TcorePlugin *)_ps_service_ref_plugin(service) );
377         strg = tcore_server_find_storage(s, "vconf");
378         handle = tcore_storage_create_handle(strg, "vconf");
379         if (!handle){
380                 err("fail to create vconf handle");
381                 return FALSE;
382         }
383
384         err_reason = _ps_service_check_cellular_state(service);
385         tcore_storage_set_int(strg,STORAGE_KEY_CELLULAR_STATE, err_reason);
386
387         return TRUE;
388 }