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