c9bc0b26f796a36ddf14ba2b118308cfb219555d
[framework/connectivity/libnet-client.git] / src / network-internal.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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
18 #ifdef __cplusplus
19 extern "C"
20 {
21 #endif /* __cplusplus */
22
23 /*****************************************************************************
24  *      Standard headers
25  *****************************************************************************/
26 #include <stdio.h> 
27 #include <errno.h> 
28 #include <stdlib.h> 
29 #include <string.h> 
30 #include <glib.h>
31
32 #include <dbus/dbus.h> 
33
34 #include <sys/types.h>
35 #include <sys/wait.h>
36
37 #include <fcntl.h>
38 #include <sys/ioctl.h>
39 #include <net/if.h>
40 #include <net/if_arp.h>
41 #include <linux/if_ether.h>
42
43 /*****************************************************************************
44  *      Platform headers
45  *****************************************************************************/
46
47 #include "network-internal.h"
48 #include "network-dbus-request.h"
49
50 /*****************************************************************************
51  *      Macros and Typedefs
52  *****************************************************************************/
53
54 #define EAP_CONFIG_KEY_TYPE                     "Type"
55 #define EAP_CONFIG_KEY_NAME                     "Name"
56 #define EAP_CONFIG_KEY_SSID                     "SSID"
57 #define EAP_CONFIG_KEY_EAP                              "EAP"
58 #define EAP_CONFIG_KEY_IDENTITY         "Identity"
59 #define EAP_CONFIG_KEY_PASSPHRASE       "Passphrase"
60
61 #define EAP_CONFIG_KEY_CA_CERT          "CACertFile"
62 #define EAP_CONFIG_KEY_CL_CERT          "ClientCertFile"
63 #define EAP_CONFIG_KEY_PRV_KEY          "PrivateKeyFile"
64 #define EAP_CONFIG_KEY_PRV_KEY_PASS     "PrivateKeyPassphrase"
65 #define EAP_CONFIG_KEY_PRV_KEY_PASS_TYPE  "PrivateKeyPassphraseType"
66 #define EAP_CONFIG_KEY_PHASE2                   "Phase2"
67
68 #define EAP_TYPE_LEN_MAX                        8               //tls / ttls / peap
69 #define EAP_AUTH_TYPE_LEN_MAX           16
70 #define EAP_TYPE_STR_TLS        "tls"
71 #define EAP_TYPE_STR_TTLS       "ttls"
72 #define EAP_TYPE_STR_PEAP       "peap"
73
74 #define CONNMAN_STORAGE_DIR             "/var/lib/connman"
75
76 /*****************************************************************************
77  *      Local Functions Declaration
78  *****************************************************************************/
79
80
81 /*****************************************************************************
82  *      Global Functions
83  *****************************************************************************/
84
85 /*****************************************************************************
86  *      Extern Global Variables
87  *****************************************************************************/
88 extern network_info_t NetworkInfo;
89
90
91 /*****************************************************************************
92  *      Global Variables
93  *****************************************************************************/
94
95 /** set all request to FALSE (0) */
96 network_request_table_t request_table[NETWORK_REQUEST_TYPE_MAX] = {{0, }, };
97
98 struct {
99         pthread_mutex_t callback_mutex;
100         pthread_mutex_t wifi_state_mutex;
101 } networkinfo_mutex;
102
103 /*****************************************************************************
104  *      Local Functions Definition
105  *****************************************************************************/
106
107 gboolean __convert_eap_type_to_string(gchar eap_type, char * eap_str)
108 {
109         switch(eap_type)
110         {
111                 case WLAN_SEC_EAP_TYPE_PEAP:
112                         memcpy(eap_str, EAP_TYPE_STR_PEAP, EAP_TYPE_LEN_MAX);
113                         return TRUE;
114                 case WLAN_SEC_EAP_TYPE_TLS:
115                         memcpy(eap_str, EAP_TYPE_STR_TLS, EAP_TYPE_LEN_MAX);
116                         return TRUE;
117                 case WLAN_SEC_EAP_TYPE_TTLS:
118                         memcpy(eap_str, EAP_TYPE_STR_TTLS, EAP_TYPE_LEN_MAX);
119                         return TRUE;
120                 case WLAN_SEC_EAP_TYPE_SIM:             //Not supported yet
121                 case WLAN_SEC_EAP_TYPE_AKA:             //Not supported yet
122                 default:
123                         return FALSE;
124         }
125
126 }
127
128 void __convert_eap_auth_to_string(gchar eap_auth, char * auth_str)
129 {
130         switch(eap_auth)
131         {
132                 case WLAN_SEC_EAP_AUTH_NONE:
133                         return ;
134                 case WLAN_SEC_EAP_AUTH_PAP:
135                         memcpy(auth_str, "PAP", strlen("PAP")+1);
136                         break;
137                 case WLAN_SEC_EAP_AUTH_MSCHAP:
138                         memcpy(auth_str, "MSCHAP", strlen("MSCHAP")+1);
139                         break;
140                 case WLAN_SEC_EAP_AUTH_MSCHAPV2:
141                         memcpy(auth_str, "MSCHAPV2", strlen("MSCHAPV2")+1);
142                         break;
143                 case WLAN_SEC_EAP_AUTH_GTC:
144                         memcpy(auth_str, "GTC", strlen("GTC")+1);
145                         break;
146                 case WLAN_SEC_EAP_AUTH_MD5:
147                         memcpy(auth_str, "MD5", strlen("MD5")+1);
148                         break;
149                 default:
150                         return ;
151
152         }
153 }
154
155 static void __update_config(GKeyFile * keyfile, char * group,
156                                                         const wlan_eap_info_t * eap_info, const char * essid,
157                                                         char * eap_type, char * auth_type)
158 {
159         g_key_file_set_string(keyfile, group, EAP_CONFIG_KEY_TYPE, "wifi");
160
161         NETWORK_LOG(NETWORK_HIGH, "------------eap info------------------");
162         NETWORK_LOG(NETWORK_HIGH, "-essid : %s", essid);
163         NETWORK_LOG(NETWORK_HIGH, "-eap type : %s", eap_type);
164         NETWORK_LOG(NETWORK_HIGH, "-phase2 authentication type : %s", auth_type);
165         NETWORK_LOG(NETWORK_HIGH, "-username : %s", eap_info->username);
166         NETWORK_LOG(NETWORK_HIGH, "-password : %s", eap_info->password);
167         NETWORK_LOG(NETWORK_HIGH, "-ca certi filename : %s", eap_info->ca_cert_filename);
168         NETWORK_LOG(NETWORK_HIGH, "-client certi filename : %s", eap_info->client_cert_filename);
169         NETWORK_LOG(NETWORK_HIGH, "-private key filename : %s", eap_info->private_key_filename);
170         NETWORK_LOG(NETWORK_HIGH, "-private key password : %s", eap_info->private_key_passwd);
171         NETWORK_LOG(NETWORK_HIGH, "--------------------------------------");
172
173         if(essid != NULL)
174                 g_key_file_set_string(keyfile, group, EAP_CONFIG_KEY_NAME, essid);
175
176         if(eap_type != NULL)
177                 g_key_file_set_string(keyfile, group, EAP_CONFIG_KEY_EAP, eap_type);
178
179         if(auth_type != NULL)
180                 g_key_file_set_string(keyfile, group, EAP_CONFIG_KEY_PHASE2, auth_type);
181
182         if((eap_info->username != NULL) && (strlen(eap_info->username) > 0))
183                 g_key_file_set_string(keyfile, group, EAP_CONFIG_KEY_IDENTITY, eap_info->username);
184
185         if((eap_info->password != NULL) && (strlen(eap_info->password) > 0))
186                 g_key_file_set_string(keyfile, group, EAP_CONFIG_KEY_PASSPHRASE, eap_info->password);
187
188         if((eap_info->ca_cert_filename != NULL) && (strlen(eap_info->ca_cert_filename) > 0))
189                 g_key_file_set_string(keyfile, group, EAP_CONFIG_KEY_CA_CERT, eap_info->ca_cert_filename);
190
191         if((eap_info->client_cert_filename != NULL) && (strlen(eap_info->client_cert_filename) > 0))
192                 g_key_file_set_string(keyfile, group, EAP_CONFIG_KEY_CL_CERT, eap_info->client_cert_filename);
193
194         if((eap_info->private_key_filename != NULL) && (strlen(eap_info->private_key_filename) > 0))
195                 g_key_file_set_string(keyfile, group, EAP_CONFIG_KEY_PRV_KEY, eap_info->private_key_filename);
196
197         if((eap_info->private_key_passwd != NULL) && (strlen(eap_info->private_key_passwd) > 0))
198                 g_key_file_set_string(keyfile, group, EAP_CONFIG_KEY_PRV_KEY_PASS, eap_info->private_key_passwd);
199 }
200
201 static net_err_t __net_add_eap_config(const wlan_eap_info_t * eap_info, const char * essid)
202 {
203         GKeyFile * keyfile;
204         char group[16];
205         char * eap_str;
206         char * auth_str;
207         gchar *data = NULL;
208         gsize length = 0;
209         net_err_t Error = NET_ERR_NONE;
210
211         __NETWORK_FUNC_ENTER__;
212
213         if((eap_info->eap_type < WLAN_SEC_EAP_TYPE_PEAP) || (eap_info->eap_type > WLAN_SEC_EAP_TYPE_AKA)) {
214                 NETWORK_LOG(NETWORK_HIGH, "Invalid EAP type (%d)\n", eap_info->eap_type);
215                 return NET_ERR_INVALID_PARAM;
216         }
217
218         eap_str = (char*)calloc(EAP_TYPE_LEN_MAX, sizeof(char));
219         if(eap_str == NULL)
220                 return NET_ERR_UNKNOWN;
221
222         if(__convert_eap_type_to_string(eap_info->eap_type, eap_str) == FALSE) {
223                 NETWORK_LOG(NETWORK_HIGH, "Invalid EAP type (%d)\n", eap_info->eap_type);
224                 NET_MEMFREE(eap_str);
225                 return NET_ERR_INVALID_PARAM;
226         }
227
228         auth_str = (char*)calloc(EAP_AUTH_TYPE_LEN_MAX, sizeof(char));
229         if(auth_str == NULL) {
230                 NET_MEMFREE(eap_str);
231                 __NETWORK_FUNC_EXIT__;
232                 return NET_ERR_UNKNOWN;
233         }
234
235         __convert_eap_auth_to_string(eap_info->eap_auth, auth_str);
236
237         sprintf(group, "service_%s", eap_str);
238         NETWORK_LOG(NETWORK_HIGH, "group (%s)\n", group);
239
240         keyfile = g_key_file_new();
241
242         __update_config(keyfile, group, eap_info, essid, eap_str, auth_str);
243
244         data = g_key_file_to_data(keyfile, &length, NULL);
245
246         NETWORK_LOG(NETWORK_ERROR, "-----length of data : %d\n", length);
247         Error = _net_dbus_provision_service(data, length+1);
248         if(Error != NET_ERR_NONE)
249                 NETWORK_LOG(NETWORK_ERROR, "Failed to update EAP info in ConnMan\n");
250
251         NET_MEMFREE(eap_str);
252         NET_MEMFREE(auth_str);
253
254         g_key_file_free(keyfile);
255         g_free(data);
256
257         return Error;
258
259 }
260
261
262 /*****************************************************************************
263  *      Global Functions Definition
264  *****************************************************************************/
265
266 char* _net_print_error(net_err_t error)
267 {
268         switch (error) {
269                 /** No error */
270         case NET_ERR_NONE:
271                 return "NET_ERR_NONE";
272
273                 /* Common Error value */
274
275                 /** Error unknown */
276         case NET_ERR_UNKNOWN:
277                 return "NET_ERR_UNKNOWN";
278
279                 /* Client Register related Errors used in API return */
280
281                 /** Application is already registered */
282         case NET_ERR_APP_ALREADY_REGISTERED:
283                 return "NET_ERR_APP_ALREADY_REGISTERED";
284                 /** Application is not registered */
285         case NET_ERR_APP_NOT_REGISTERED:
286                 return "NET_ERR_APP_NOT_REGISTERED";
287
288                 /* Connection Related Error */
289
290                 /** No active connection exists for the given profile name */
291         case NET_ERR_NO_ACTIVE_CONNECTIONS:
292                 return "NET_ERR_NO_ACTIVE_CONNECTIONS";
293                 /** Active connection already exists for the given profile name  */
294         case NET_ERR_ACTIVE_CONNECTION_EXISTS:
295                 return "NET_ERR_ACTIVE_CONNECTION_EXISTS";
296
297                 /** Connection failure : out of range */
298         case NET_ERR_CONNECTION_OUT_OF_RANGE:
299                 return "NET_ERR_CONNECTION_OUT_OF_RANGE";
300                 /** Connection failure : pin missing */
301         case NET_ERR_CONNECTION_PIN_MISSING:
302                 return "NET_ERR_CONNECTION_PIN_MISSING";
303                 /** Connection failure : dhcp failed */
304         case NET_ERR_CONNECTION_DHCP_FAILED:
305                 return "NET_ERR_CONNECTION_DHCP_FAILED";
306                 /** Connection failure */
307         case NET_ERR_CONNECTION_CONNECT_FAILED:
308                 return "NET_ERR_CONNECTION_CONNECT_FAILED";
309                 /** Connection failure : login failed */
310         case NET_ERR_CONNECTION_LOGIN_FAILED:
311                 return "NET_ERR_CONNECTION_LOGIN_FAILED";
312                 /** Connection failure : authentication failed */
313         case NET_ERR_CONNECTION_AUTH_FAILED:
314                 return "NET_ERR_CONNECTION_AUTH_FAILED";
315                 /** Connection failure : invalid key */
316         case NET_ERR_CONNECTION_INVALID_KEY:
317                 return "NET_ERR_CONNECTION_INVALID_KEY";
318
319                 /* Other Error */
320
321                 /** Access is denied */
322         case NET_ERR_ACCESS_DENIED:
323                 return "NET_ERR_ACCESS_DENIED";
324                 /** Operation is in progress */
325         case NET_ERR_IN_PROGRESS:
326                 return "NET_ERR_IN_PROGRESS";
327                 /** Operation was aborted by client or network*/
328         case NET_ERR_OPERATION_ABORTED:
329                 return "NET_ERR_OPERATION_ABORTED";
330                 /** Invalid value of API parameter */
331         case NET_ERR_INVALID_PARAM:
332                 return "NET_ERR_INVALID_PARAM";
333                 /** invalid operation depending on current state */
334         case NET_ERR_INVALID_OPERATION:
335                 return "NET_ERR_INVALID_OPERATION";
336
337                 /** Feature not supported */
338         case NET_ERR_NOT_SUPPORTED:
339                 return "NET_ERR_NOT_SUPPORTED";
340                 /** TimeOut Error */
341         case NET_ERR_TIME_OUT:
342                 return "NET_ERR_TIME_OUT";
343                 /** Network service is not available*/
344         case NET_ERR_NO_SERVICE:
345                 return "NET_ERR_NO_SERVICE";
346                 /** DBus can't find appropriate method */
347         case NET_ERR_UNKNOWN_METHOD:
348                 return "NET_ERR_UNKNOWN_METHOD";
349         default:
350                 return "INVALID";
351         }
352 }
353
354 net_device_t _net_get_tech_type_from_path(const char *profile_name)
355 {
356         __NETWORK_FUNC_ENTER__;
357
358         net_device_t device_type = NET_DEVICE_UNKNOWN;
359
360         if (strstr(profile_name, "/wifi_") != NULL)
361                 device_type = NET_DEVICE_WIFI;
362         else if (strstr(profile_name, "/cellular_") != NULL)
363                 device_type = NET_DEVICE_CELLULAR;
364
365         __NETWORK_FUNC_EXIT__;
366         return device_type;
367 }
368
369 char* _net_get_string(DBusMessage* msg)
370 {
371         __NETWORK_FUNC_ENTER__;
372
373         DBusMessageIter args;
374         char* sigvalue = NULL;
375
376         if (!dbus_message_iter_init(msg, &args)) {
377                 NETWORK_LOG(NETWORK_LOW, "Message does not have parameters\n");
378         } else if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_STRING) {
379                 NETWORK_LOG(NETWORK_LOW, "Argument is not string\n");
380         } else {
381                 dbus_message_iter_get_basic(&args, &sigvalue);
382         }
383
384         __NETWORK_FUNC_EXIT__;
385         return sigvalue;
386 }
387
388 char* _net_get_object(DBusMessage* msg)
389 {
390         __NETWORK_FUNC_ENTER__;
391
392         DBusMessageIter args;
393         char* sigvalue = NULL;
394
395         if (!dbus_message_iter_init(msg, &args)) {
396                 NETWORK_LOG(NETWORK_LOW, "Message does not have parameters\n");
397         } else if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH) {
398                 NETWORK_LOG(NETWORK_LOW, "Argument is not string\n");
399         } else {
400                 dbus_message_iter_get_basic(&args, &sigvalue);
401         }
402
403         __NETWORK_FUNC_EXIT__;
404         return sigvalue;
405 }
406
407 int _net_get_boolean(DBusMessage* msg)
408 {
409         __NETWORK_FUNC_ENTER__;
410
411         DBusMessageIter args;
412         dbus_bool_t val = FALSE;
413         int retvalue = FALSE;
414
415         if (!dbus_message_iter_init(msg, &args)) {
416                 NETWORK_LOG(NETWORK_LOW, "Message does not have parameters\n");
417         } else if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_BOOLEAN) {
418                 NETWORK_LOG(NETWORK_LOW, "Argument is not boolean\n");
419         } else {
420                 dbus_message_iter_get_basic(&args, &val);
421
422                 if (val)
423                         retvalue = TRUE;
424                 else
425                         retvalue = FALSE;
426         }
427
428         __NETWORK_FUNC_EXIT__;
429         return retvalue;
430 }
431
432 int _net_get_path(DBusMessage *msg, char *profile_name)
433 {
434         __NETWORK_FUNC_ENTER__;
435
436         char* ProfileName = NULL;
437
438         ProfileName = (char*)dbus_message_get_path(msg);
439         snprintf(profile_name, strlen(ProfileName) + 1, "%s", ProfileName);
440
441         __NETWORK_FUNC_EXIT__;
442
443         return NET_ERR_NONE;
444 }
445
446 int _net_get_tech_state(DBusMessage* msg, network_get_tech_state_info_t* tech_state)
447 {
448         __NETWORK_FUNC_ENTER__;
449
450         net_err_t Error = NET_ERR_NONE;
451         DBusMessageIter args, dict;
452
453         if (!dbus_message_iter_init(msg, &args)) {
454                 NETWORK_LOG(NETWORK_LOW, "Message does not have parameters\n");
455                 Error = NET_ERR_UNKNOWN;
456                 goto done;
457         }
458
459         dbus_message_iter_recurse(&args, &dict);
460
461         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
462                 DBusMessageIter key_iter, sub_iter1, sub_iter2;
463                 const char *key = NULL;
464                 const char *tech_name = NULL;
465
466                 dbus_message_iter_recurse(&dict, &key_iter);
467                 dbus_message_iter_get_basic(&key_iter, &key);
468
469                 if (strcmp(key, "AvailableTechnologies") == 0 ||
470                     strcmp(key, "EnabledTechnologies") == 0 ||
471                     strcmp(key, "ConnectedTechnologies") == 0) {
472                         dbus_message_iter_next(&key_iter);
473                         dbus_message_iter_recurse(&key_iter, &sub_iter1);
474
475                         if (dbus_message_iter_get_arg_type(&sub_iter1) == DBUS_TYPE_ARRAY)
476                                 dbus_message_iter_recurse(&sub_iter1, &sub_iter2);
477                         else
478                                 goto next_dict;
479
480                         while (dbus_message_iter_get_arg_type(&sub_iter2) == DBUS_TYPE_STRING) {
481                                 dbus_message_iter_get_basic(&sub_iter2, &tech_name);
482                                 if (tech_name != NULL &&
483                                     strcmp(tech_name, tech_state->technology) == 0) {
484                                         if (strcmp(key, "AvailableTechnologies") == 0)
485                                                 tech_state->AvailableTechnology = TRUE;
486                                         else if (strcmp(key, "EnabledTechnologies") == 0)
487                                                 tech_state->EnabledTechnology = TRUE;
488                                         else
489                                                 tech_state->ConnectedTechnology = TRUE;
490                                 }
491
492                                 dbus_message_iter_next(&sub_iter2);
493                         }
494                 } else if (strcmp(key, "DefaultTechnology") == 0) {
495                         dbus_message_iter_next(&key_iter);
496                         dbus_message_iter_recurse(&key_iter, &sub_iter1);
497
498                         if (dbus_message_iter_get_arg_type(&sub_iter1) == DBUS_TYPE_STRING) {
499                                 dbus_message_iter_get_basic(&sub_iter1, &tech_name);
500                                 if (tech_name != NULL &&
501                                     strcmp(tech_name, tech_state->technology) == 0)
502                                         tech_state->AvailableTechnology = TRUE;
503                         }
504                 }
505 next_dict:
506                 dbus_message_iter_next(&dict);
507         }
508
509 done:
510         __NETWORK_FUNC_EXIT__;
511         return Error;
512 }
513
514
515 /** This function is used only to open Wi-Fi connection with hidden APs */
516 int _net_open_connection_with_wifi_info(const net_wifi_connection_info_t* wifi_info)
517 {
518         __NETWORK_FUNC_ENTER__;
519
520         /** path = manager.ConnectService(({ "Type": "wifi", "Mode": "managed",
521          "SSID": sys.argv[1],
522          "Security": security,
523          "Passphrase": passphrase }));
524         */
525
526         net_err_t Error = NET_ERR_NONE;
527         char type[] = "wifi";
528         char mode[128] = "";
529         char essid[NET_WLAN_ESSID_LEN + 1] = "";
530         char security[128] = "";
531         char passphrase[NETPM_WLAN_MAX_PSK_PASSPHRASE_LEN] = "";
532         net_wifi_connect_service_info_t wifi_connection_info;
533
534         snprintf(mode, 128, "%s", (wifi_info->wlan_mode == NETPM_WLAN_CONNMODE_ADHOC)?"adhoc":"managed");
535
536         switch(wifi_info->security_info.sec_mode) {
537         case WLAN_SEC_MODE_NONE:
538                 snprintf(security, 128, "%s", "none");
539                 snprintf(passphrase, NETPM_WLAN_MAX_PSK_PASSPHRASE_LEN, "%s", "");
540                 break;
541
542         case WLAN_SEC_MODE_WEP:
543                 snprintf(security, 128, "%s", "wep");
544                 snprintf(passphrase, NETPM_WLAN_MAX_PSK_PASSPHRASE_LEN, "%s", wifi_info->security_info.authentication.wep.wepKey);
545                 break;
546
547         /** WPA-PSK(equivalent to WPA-NONE in case of Ad-Hoc) */
548         case WLAN_SEC_MODE_WPA_PSK:
549                 snprintf(security, 128, "%s", "psk");
550                 snprintf(passphrase, NETPM_WLAN_MAX_PSK_PASSPHRASE_LEN, "%s", wifi_info->security_info.authentication.psk.pskKey);
551                 break;
552
553         /** WPA2-PSK */
554         /** WPA-PSK / WPA2-PSK supported */
555         case WLAN_SEC_MODE_WPA2_PSK:
556                 snprintf(security, 128, "%s", "rsn");
557                 snprintf(passphrase, NETPM_WLAN_MAX_PSK_PASSPHRASE_LEN, "%s", wifi_info->security_info.authentication.psk.pskKey);
558                 break;
559
560         case WLAN_SEC_MODE_IEEE8021X:
561                 snprintf(security, 128, "%s", "ieee8021x");
562                 snprintf(passphrase, NETPM_WLAN_MAX_PSK_PASSPHRASE_LEN, "%s", "");
563                 Error = __net_add_eap_config(&(wifi_info->security_info.authentication.eap), wifi_info->essid);
564                 if(Error != NET_ERR_NONE) {
565                         __NETWORK_FUNC_EXIT__;
566                         return Error;
567                 }
568                 break;
569
570         default:
571                 NETWORK_LOG(NETWORK_ERROR, "Error!!! Invalid security type\n");
572                 __NETWORK_FUNC_EXIT__;
573                 return NET_ERR_INVALID_PARAM;
574         }
575
576         snprintf(essid, NET_WLAN_ESSID_LEN + 1, "%s", wifi_info->essid);
577
578         wifi_connection_info.type = type;
579         wifi_connection_info.mode = mode;
580         wifi_connection_info.ssid = essid;
581         wifi_connection_info.security = security;
582         wifi_connection_info.passphrase = passphrase;
583
584         NETWORK_LOG( NETWORK_HIGH,
585                         "Parameters: type:\t%s\nmode:\t%s\nssid:\t%s\nsecurity:\t%s\npassphrase:\t%s\n",
586                         wifi_connection_info.type, wifi_connection_info.mode,
587                         wifi_connection_info.ssid, wifi_connection_info.security,
588                         wifi_connection_info.passphrase);
589
590         if( (Error = _net_dbus_connect_service(&wifi_connection_info)) != NET_ERR_NONE ) {
591                 NETWORK_LOG(NETWORK_EXCEPTION, "Error!!! Failed to request connect_service. Error [%s]\n",
592                                 _net_print_error(Error));
593                 __NETWORK_FUNC_EXIT__;
594                 return Error;
595         }
596
597         NETWORK_LOG(NETWORK_HIGH, "Successfully requested to ConnMan\n");
598
599         __NETWORK_FUNC_EXIT__;
600         return NET_ERR_NONE;
601 }
602
603 int _net_mutex_init()
604 {
605         __NETWORK_FUNC_ENTER__;
606
607         if (pthread_mutex_init(&networkinfo_mutex.callback_mutex, NULL) != 0) {
608                 NETWORK_LOG(NETWORK_ERROR, "Mutex for callback initialization failed!\n");
609                 __NETWORK_FUNC_EXIT__;
610                 return NET_ERR_UNKNOWN;
611         }
612
613         if (pthread_mutex_init(&networkinfo_mutex.wifi_state_mutex, NULL) != 0) {
614                 NETWORK_LOG(NETWORK_ERROR, "Mutex for wifi state initialization failed!\n");
615                 pthread_mutex_destroy(&networkinfo_mutex.callback_mutex);
616                 __NETWORK_FUNC_EXIT__;
617                 return NET_ERR_UNKNOWN;
618         }
619
620         __NETWORK_FUNC_EXIT__;
621         return NET_ERR_NONE;
622 }
623
624 void _net_mutex_destroy()
625 {
626         __NETWORK_FUNC_ENTER__;
627
628         pthread_mutex_destroy(&networkinfo_mutex.callback_mutex);
629         pthread_mutex_destroy(&networkinfo_mutex.wifi_state_mutex);
630
631         __NETWORK_FUNC_EXIT__;
632 }
633
634 void _net_client_callback(net_event_info_t *event_data)
635 {
636         pthread_mutex_lock(&networkinfo_mutex.callback_mutex);
637         __NETWORK_FUNC_ENTER__;
638
639         NETWORK_CALLBACK(event_data, NetworkInfo.user_data);
640
641         __NETWORK_FUNC_EXIT__;
642         pthread_mutex_unlock(&networkinfo_mutex.callback_mutex);
643 }
644
645 net_wifi_state_t _net_get_wifi_state()
646 {
647         pthread_mutex_lock(&networkinfo_mutex.wifi_state_mutex);
648         __NETWORK_FUNC_ENTER__;
649
650         net_err_t Error = NET_ERR_NONE;
651         network_get_tech_state_info_t tech_state = {{0,},};
652         net_wifi_state_t wifi_state = WIFI_UNKNOWN;
653
654         snprintf(tech_state.technology, NET_TECH_LENGTH_MAX, "%s", "wifi");
655         Error = _net_dbus_get_technology_state(&tech_state);
656         if (Error != NET_ERR_NONE) {
657                 NETWORK_LOG(NETWORK_ERROR,
658                         "Error!!! _net_dbus_get_technology_state() failed. Error [%s]\n",
659                         _net_print_error(Error));
660                 goto state_done;
661         }
662
663         if (tech_state.EnabledTechnology == TRUE &&
664             tech_state.AvailableTechnology == TRUE)
665                 wifi_state = WIFI_ON;
666         else
667                 wifi_state = WIFI_OFF;
668
669 state_done:
670         __NETWORK_FUNC_EXIT__;
671         pthread_mutex_unlock(&networkinfo_mutex.wifi_state_mutex);
672         return wifi_state;
673 }
674
675 void _net_clear_request_table()
676 {
677         __NETWORK_FUNC_ENTER__;
678
679         int i = 0;
680
681         for (i = 0;i < NETWORK_REQUEST_TYPE_MAX;i++)
682                 memset(&request_table[i], 0, sizeof(network_request_table_t));
683
684         __NETWORK_FUNC_EXIT__;
685 }
686
687 #ifdef __cplusplus
688 }
689 #endif /* __cplusplus */