Replaced type from state for restriction
[platform/core/connectivity/stc-manager.git] / src / monitor / stc-default-connection.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <vconf/vconf.h>
18 #include <openssl/sha.h>
19
20 #include "stc-monitor.h"
21 #include "stc-firewall.h"
22 #include "stc-manager-gdbus.h"
23 #include "stc-default-connection.h"
24
25 /* connman service dbus details */
26 #define CONNMAN_SERVICE                          "net.connman"
27 #define CONNMAN_PATH                             "/net/connman"
28
29 #define CONNMAN_MANAGER_PATH                     "/"
30 #define CONNMAN_MANAGER_INTERFACE                CONNMAN_SERVICE ".Manager"
31 #define CONNMAN_SERVICE_INTERFACE                CONNMAN_SERVICE ".Service"
32
33 #define CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX  CONNMAN_PATH "/service/cellular_"
34 #define CONNMAN_WIFI_SERVICE_PROFILE_PREFIX      CONNMAN_PATH "/service/wifi_"
35 #define CONNMAN_ETHERNET_SERVICE_PROFILE_PREFIX  CONNMAN_PATH "/service/ethernet_"
36 #define CONNMAN_BLUETOOTH_SERVICE_PROFILE_PREFIX CONNMAN_PATH "/service/bluetooth_"
37
38 #define CONNMAN_SIGNAL_PROPERTY_CHANGED          "PropertyChanged"
39
40 /* telephony service dbus details */
41 #define TELEPHONY_SERVICE                        "org.tizen.telephony"
42 #define TELEPHONY_DEFAULT_PATH                   "/org/tizen/telephony"
43
44 #define TELEPHONY_SERVICE_MANAGER                TELEPHONY_SERVICE".Manager"
45 #define TELEPHONY_SIM_INTERFACE                  TELEPHONY_SERVICE".Sim"
46
47 #define TELEPHONY_GET_MODEMS                     "GetModems"
48 #define TELEPHONY_GET_IMSI                       "GetIMSI"
49
50 #define SIM_SLOT_SINGLE 1
51
52 #define VCONF_TELEPHONY_DEFAULT_DATA_SERVICE     "db/telephony/dualsim/default_data_service"
53
54 default_connection_s g_default_connection;
55 guint g_default_connection_sub_id = 0;
56
57 static int __telephony_get_current_sim(void)
58 {
59         int sim_slot_count = 0;
60         int current_sim = 0;
61
62         if (vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT_COUNT, &sim_slot_count) != 0) {
63                 STC_LOGD("failed to get sim slot count"); //LCOV_EXCL_LINE
64                 return -1; //LCOV_EXCL_LINE
65         }
66
67         if (sim_slot_count == SIM_SLOT_SINGLE) {
68                STC_LOGD("It's single sim model"); //LCOV_EXCL_LINE
69                return current_sim; //LCOV_EXCL_LINE
70         }
71
72         if (vconf_get_int(VCONF_TELEPHONY_DEFAULT_DATA_SERVICE, &current_sim) != 0) {
73                 STC_LOGD("failed to get default data service = %d\n", //LCOV_EXCL_LINE
74                          current_sim);
75                 return -1; //LCOV_EXCL_LINE
76         }
77
78         return current_sim;
79 }
80
81 static void __make_imsi_to_subscriber_id(char *imsi)
82 {
83         int i = 0;
84         SHA256_CTX ctx;
85         unsigned char md[SHA256_DIGEST_LENGTH];
86
87         SHA256_Init(&ctx);
88         SHA256_Update(&ctx, imsi, strlen(imsi));
89         SHA256_Final(md, &ctx);
90
91         for (i = 0; i < SHA256_DIGEST_LENGTH; ++i)
92                 snprintf(g_default_connection.subscriber_id + (i * 2), 3, "%02x", md[i]);
93 }
94
95 static void __telephony_get_modem_subscriber_id(GDBusConnection *connection,
96                                        const char *default_modem_name)
97 {
98         GVariant *message = NULL;
99         char tel_path[MAX_PATH_LENGTH];
100         char imsi[IMSI_LENGTH];
101         const char *plmn = NULL;
102         int plmn_len = 0;
103         const char *msin = NULL;
104         int msin_len = 0;
105
106         snprintf(tel_path, sizeof(tel_path), "%s/%s", TELEPHONY_DEFAULT_PATH,
107                  default_modem_name);
108         message = stc_manager_gdbus_call_sync(connection,
109                                               TELEPHONY_SERVICE,
110                                               tel_path,
111                                               TELEPHONY_SIM_INTERFACE,
112                                               TELEPHONY_GET_IMSI,
113                                               NULL);
114         if (message == NULL) {
115                 STC_LOGE("Failed to get services informations"); //LCOV_EXCL_LINE
116                 goto done; //LCOV_EXCL_LINE
117         }
118
119         DEBUG_PARAMS(message);
120         DEBUG_PARAM_TYPE(message);
121         g_variant_get(message, "(&s&s)", &plmn, &msin);
122         if (plmn)
123                 plmn_len = strlen(plmn);
124         if (msin)
125                 msin_len = strlen(msin);
126
127         if (msin_len + plmn_len >= IMSI_LENGTH) {
128                 STC_LOGD("Incorrect length of mobile subscriber identifier + net id"); //LCOV_EXCL_LINE
129                 goto done; //LCOV_EXCL_LINE
130         }
131
132         snprintf(imsi, IMSI_LENGTH, "%s%s", plmn, msin);
133         __make_imsi_to_subscriber_id(imsi);
134
135 done:
136         g_variant_unref(message);
137         return;
138 }
139
140 static void __telephony_update_default_modem_subscriber_id(GDBusConnection *connection)
141 {
142         GVariant *message = NULL;
143         GVariantIter *iter = NULL;
144         gchar *default_modem_name = NULL;
145         gchar *modem_name = NULL;
146         int current_sim = __telephony_get_current_sim();
147
148         if (current_sim < 0) {
149                 STC_LOGI("Sim not found"); //LCOV_EXCL_LINE
150                 return; //LCOV_EXCL_LINE
151         }
152
153         message = stc_manager_gdbus_call_sync(connection,
154                                               TELEPHONY_SERVICE,
155                                               TELEPHONY_DEFAULT_PATH,
156                                               TELEPHONY_SERVICE_MANAGER,
157                                               TELEPHONY_GET_MODEMS,
158                                               NULL);
159         if (message == NULL) {
160                 STC_LOGE("Failed to get services informations"); //LCOV_EXCL_LINE
161                 return; //LCOV_EXCL_LINE
162         }
163
164         g_variant_get(message, "(as)", &iter);
165         DEBUG_PARAMS(message);
166         DEBUG_PARAM_TYPE(message);
167         while (g_variant_iter_loop(iter, "s", &modem_name)) {
168                 if (current_sim == 0) {
169                         default_modem_name = g_strdup(modem_name);
170                         FREE(modem_name);
171                         break;
172                 }
173                 current_sim--; //LCOV_EXCL_LINE
174         }
175
176         __telephony_get_modem_subscriber_id(connection, default_modem_name);
177
178         FREE(default_modem_name);
179         g_variant_iter_free(iter);
180         g_variant_unref(message);
181         return;
182 }
183
184 static void __print_default_connection_info(void)
185 {
186         STC_LOGI("============= default connection info ============");
187         STC_LOGI("path    [%s]", g_default_connection.path);
188         STC_LOGI("type    [%d]", g_default_connection.type);
189         STC_LOGI("ifname  [%s]", g_default_connection.ifname);
190         STC_LOGI("roaming [%u]", g_default_connection.roaming ? TRUE : FALSE);
191         if (g_default_connection.type == STC_IFACE_DATACALL)
192                 STC_LOGI("sub_id  [%s]", g_default_connection.subscriber_id);
193         STC_LOGI("==================================================");
194 }
195
196 static void __reset_default_connection_data(void)
197 {
198         FREE(g_default_connection.path);
199         FREE(g_default_connection.ifname);
200         g_default_connection.type = STC_IFACE_UNKNOWN;
201         g_default_connection.roaming = FALSE;
202 }
203
204 static gboolean __is_cellular_internet_profile(const char *profile)
205 {
206         const char internet_suffix[] = "_1";
207
208         if (profile == NULL)
209                 return FALSE;
210
211         if (g_str_has_prefix(profile, CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX)
212             == TRUE) {
213                 char *suffix = strrchr(profile, '_');
214                 if (g_strcmp0(suffix, internet_suffix) == 0)
215                         return TRUE;
216         }
217
218         return FALSE;
219 }
220
221 static gboolean __is_cellular_profile(const char *profile)
222 {
223         if (profile == NULL)
224                 return FALSE;
225
226         return g_str_has_prefix(profile, //LCOV_EXCL_LINE
227                                 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX);
228 }
229
230 static gboolean __is_wifi_profile(const char *profile)
231 {
232         if (profile == NULL)
233                 return FALSE;
234
235         return g_str_has_prefix(profile, //LCOV_EXCL_LINE
236                                 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX);
237 }
238
239 static gboolean __is_ethernet_profile(const char *profile)
240 {
241         if (profile == NULL)
242                 return FALSE;
243
244         return g_str_has_prefix(profile, //LCOV_EXCL_LINE
245                                 CONNMAN_ETHERNET_SERVICE_PROFILE_PREFIX);
246 }
247
248 static gboolean __is_bluetooth_profile(const char *profile)
249 {
250         if (profile == NULL)
251                 return FALSE;
252
253         return g_str_has_prefix(profile, //LCOV_EXCL_LINE
254                                 CONNMAN_BLUETOOTH_SERVICE_PROFILE_PREFIX);
255 }
256
257 static gboolean __is_connected(GVariantIter *array)
258 {
259         gboolean is_connected = FALSE;
260         GVariant *variant = NULL;
261         gchar *key = NULL;
262
263         while (g_variant_iter_loop(array, "{sv}", &key, &variant)) {
264                 if (g_strcmp0(key, "State") != 0)
265                         continue;
266
267                 if (g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
268                         const gchar *state = NULL;
269
270                         state = g_variant_get_string(variant, NULL);
271                         if (g_strcmp0(state, "ready") == 0 ||
272                             g_strcmp0(state, "online") == 0)
273                                 is_connected = TRUE;
274                 }
275
276                 g_free(key);
277                 g_variant_unref(variant);
278                 break;
279         }
280
281         return is_connected;
282 }
283
284 static void __get_default_connection_info(GDBusConnection *connection,
285                                           const char *object_path)
286 {
287         GVariant *message = NULL;
288         GVariantIter *iter = NULL;
289         GVariant *variant = NULL;
290         gchar *key = NULL;
291
292         if (object_path == NULL) {
293                 STC_LOGI("Object path is NULL, so information not available.");
294                 return;
295         }
296
297         message = stc_manager_gdbus_call_sync(connection,
298                                               CONNMAN_SERVICE,
299                                               object_path,
300                                               CONNMAN_SERVICE_INTERFACE,
301                                               "GetProperties", NULL);
302         if (message == NULL) {
303                 STC_LOGE("Failed to get services informations"); //LCOV_EXCL_LINE
304                 goto done; //LCOV_EXCL_LINE
305         }
306
307         g_variant_get(message, "(a{sv})", &iter);
308         if (iter == NULL) {
309                 STC_LOGE("Profile %s doesn't exist", object_path); //LCOV_EXCL_LINE
310                 goto done; //LCOV_EXCL_LINE
311         }
312
313         while (g_variant_iter_loop(iter, "{sv}", &key, &variant)) {
314                 if (g_strcmp0(key, "Ethernet") == 0) {
315                         GVariantIter *iter1 = NULL;
316                         GVariant *variant1 = NULL;
317                         gchar *key1 = NULL;
318
319                         g_variant_get(variant, "a{sv}", &iter1);
320                         if (iter1 == NULL)
321                                 continue; //LCOV_EXCL_LINE
322
323                         while (g_variant_iter_loop(iter1, "{sv}", &key1,
324                                                    &variant1)) {
325                                 if (g_strcmp0(key1, "Interface") == 0) {
326                                         const gchar *value =
327                                                 g_variant_get_string(variant1,
328                                                                      NULL);
329                                         g_default_connection.ifname =
330                                                 g_strdup(value);
331                                 }
332                         }
333
334                         g_variant_iter_free(iter1);
335
336                 } else if (g_strcmp0(key, "Roaming") == 0) {
337                         gboolean roaming = 0;
338
339                         if (g_variant_is_of_type(variant,
340                                                  G_VARIANT_TYPE_BOOLEAN)) {
341                                 roaming = g_variant_get_boolean(variant);
342                                 g_default_connection.roaming = roaming;
343                         }
344                 }
345         }
346
347 done:
348         if (iter)
349                 g_variant_iter_free(iter);
350
351         if (message)
352                 g_variant_unref(message);
353
354         return;
355 }
356
357 static stc_error_e __get_default_profile(GDBusConnection *connection)
358 {
359         GVariant *message = NULL;
360         GVariantIter *iter = NULL;
361         GVariantIter *next;
362         gchar *object_path;
363
364         message = stc_manager_gdbus_call_sync(connection,
365                                               CONNMAN_SERVICE,
366                                               CONNMAN_MANAGER_PATH,
367                                               CONNMAN_MANAGER_INTERFACE,
368                                               "GetServices", NULL);
369         if (message == NULL) {
370                 STC_LOGE("Failed to get profiles"); //LCOV_EXCL_LINE
371                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
372         }
373
374         g_variant_get(message, "(a(oa{sv}))", &iter);
375         while (g_variant_iter_loop(iter, "(oa{sv})", &object_path, &next)) {
376                 if (object_path == NULL)
377                         continue; //LCOV_EXCL_LINE
378
379                 if (__is_cellular_profile(object_path) &&
380                     !__is_cellular_internet_profile(object_path))
381                         continue;
382
383                 if (__is_connected(next) == TRUE) {
384                         /* reset old default connection data */
385                         FREE(g_default_connection.path);
386                         FREE(g_default_connection.ifname);
387                         g_default_connection.type = STC_IFACE_UNKNOWN;
388                         g_default_connection.roaming = FALSE;
389
390                         g_default_connection.path = g_strdup(object_path);
391                         g_free(object_path);
392                         g_variant_iter_free(next);
393                         break;
394                 }
395         }
396
397         g_variant_iter_free(iter);
398         g_variant_unref(message);
399
400         if (__is_cellular_profile(g_default_connection.path)) {
401                 g_default_connection.type = STC_IFACE_DATACALL; //LCOV_EXCL_LINE
402                 __telephony_update_default_modem_subscriber_id(connection); //LCOV_EXCL_LINE
403         } else if (__is_wifi_profile(g_default_connection.path)) {
404                 g_default_connection.type = STC_IFACE_WIFI; //LCOV_EXCL_LINE
405         } else if (__is_ethernet_profile(g_default_connection.path)) {
406                 g_default_connection.type = STC_IFACE_WIRED; //LCOV_EXCL_LINE
407         } else if (__is_bluetooth_profile(g_default_connection.path)) {
408                 g_default_connection.type = STC_IFACE_BLUETOOTH; //LCOV_EXCL_LINE
409         } else {
410                 g_default_connection.type = STC_IFACE_UNKNOWN; //LCOV_EXCL_LINE
411         }
412
413         __get_default_connection_info(connection, g_default_connection.path);
414
415         __print_default_connection_info();
416
417         stc_monitor_update_rstn_by_default_connection(&g_default_connection);
418         stc_firewall_update();
419
420         return STC_ERROR_NONE;
421 }
422
423 static void _service_signal_cb(GDBusConnection *conn,
424                                const gchar *name, const gchar *path,
425                                const gchar *interface, const gchar *sig,
426                                GVariant *param, gpointer user_data)
427 {
428         gchar *sigvalue = NULL;
429         GVariant *variant = NULL;
430         stc_s *stc = (stc_s *)stc_get_manager();
431         ret_msg_if(stc == NULL, "failed to get stc data");
432
433         if (path == NULL || param == NULL)
434                 goto done;
435
436         g_variant_get(param, "(sv)", &sigvalue, &variant);
437         if (sigvalue == NULL)
438                 goto done;
439
440         if (g_strcmp0(sig, CONNMAN_SIGNAL_PROPERTY_CHANGED) != 0)
441                 goto done;
442
443         if (g_strcmp0(sigvalue, "State") == 0 &&
444             g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
445                 const gchar *state = NULL;
446
447                 state = g_variant_get_string(variant, NULL);
448                 if (g_strcmp0(state, "ready") == 0 ||
449                     g_strcmp0(state, "online") == 0) {
450                         if (g_strcmp0(g_default_connection.path, path)) {
451                                 __reset_default_connection_data();
452                                 __get_default_profile(stc->connection);
453                         }
454                 } else {
455                         if (g_strcmp0(g_default_connection.path, path) == 0) {
456                                 __reset_default_connection_data(); //LCOV_EXCL_LINE
457                                 __get_default_profile(stc->connection); //LCOV_EXCL_LINE
458                         }
459                 }
460         } else if (g_strcmp0(sigvalue, "Roaming") == 0) {
461                 //LCOV_EXCL_START
462                 if (g_strcmp0(g_default_connection.path, path) == 0) {
463                         gboolean roaming = 0;
464
465                         if (g_variant_is_of_type(variant,
466                                                  G_VARIANT_TYPE_BOOLEAN)) {
467                                 roaming = g_variant_get_boolean(variant);
468                                 g_default_connection.roaming = roaming;
469                         }
470                 }
471                 //LCOV_EXCL_STOP
472         } else {
473                 ;//Do nothing
474         }
475 done:
476         if (sigvalue)
477                 g_free(sigvalue);
478
479         if (variant)
480                 g_variant_unref(variant);
481
482         return;
483 }
484
485 stc_error_e stc_default_connection_monitor_init(stc_s *stc)
486 {
487         ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data");
488
489         __get_default_profile(stc->connection);
490         g_default_connection_sub_id =
491                 stc_manager_gdbus_subscribe_signal(stc->connection,
492                                                    CONNMAN_SERVICE,
493                                                    CONNMAN_SERVICE_INTERFACE,
494                                                    CONNMAN_SIGNAL_PROPERTY_CHANGED,
495                                                    NULL, NULL,
496                                                    G_DBUS_SIGNAL_FLAGS_NONE,
497                                                    _service_signal_cb,
498                                                    NULL, NULL);
499
500         STC_LOGI("Successfully subscribed connman [%s] signal", CONNMAN_SIGNAL_PROPERTY_CHANGED);
501         return STC_ERROR_NONE;
502 }
503
504 stc_error_e stc_default_connection_monitor_deinit(stc_s *stc)
505 {
506         ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data");
507
508         stc_manager_gdbus_unsubscribe_signal(stc->connection,
509                                              g_default_connection_sub_id);
510         FREE(g_default_connection.path);
511         FREE(g_default_connection.ifname);
512         return STC_ERROR_NONE;
513 }
514
515 gchar *stc_default_connection_get_ifname(void)
516 {
517         return g_strdup(g_default_connection.ifname);
518 }
519
520 default_connection_s *stc_get_default_connection(void)
521 {
522         return &g_default_connection;
523 }