2 * Copyright 2012 Samsung Electronics Co., Ltd
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
8 * http://www.tizenopensource.org/license
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.
21 #endif /* __cplusplus */
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <dbus/dbus.h>
36 /*****************************************************************************
38 *****************************************************************************/
40 #include "network-internal.h"
41 #include "network-signal-handler.h"
42 #include "network-dbus-request.h"
44 /*****************************************************************************
46 *****************************************************************************/
48 /*****************************************************************************
49 * Local Functions Declaration
50 *****************************************************************************/
52 static int __net_get_default_profile(void *param, net_profile_info_t *active_profile_info);
54 /*****************************************************************************
56 *****************************************************************************/
58 /*****************************************************************************
60 *****************************************************************************/
62 extern network_request_table_t request_table[NETWORK_REQUEST_TYPE_MAX];
64 /*****************************************************************************
66 *****************************************************************************/
68 network_info_t NetworkInfo = {0, };
70 /*****************************************************************************
71 * Local Functions Definition
72 *****************************************************************************/
74 static int __net_get_default_profile(void *param, net_profile_info_t *active_profile_info)
76 __NETWORK_FUNC_ENTER__;
78 net_err_t Error = NET_ERR_NONE;
80 if (NetworkInfo.ClientEventCb == NULL) {
81 NETWORK_LOG(NETWORK_ERROR, "Error!!! Application was not registered\n");
82 __NETWORK_FUNC_EXIT__;
83 return NET_ERR_APP_NOT_REGISTERED;
87 NETWORK_LOG(NETWORK_ERROR, "Error!!! Invalid parameter\n");
88 __NETWORK_FUNC_EXIT__;
89 return NET_ERR_INVALID_PARAM;
92 Error = _net_get_default_profile_info(active_profile_info);
94 if (Error != NET_ERR_NONE) {
95 NETWORK_LOG(NETWORK_ERROR,
96 "Error!!! _net_get_default_profile_info() failed. Error [%s]\n",
97 _net_print_error(Error));
99 __NETWORK_FUNC_EXIT__;
103 __NETWORK_FUNC_EXIT__;
107 /*****************************************************************************
108 * ConnMan Client Common Interface API Definition
109 *****************************************************************************/
112 * @fn EXPORT_API int net_register_client(net_event_cb_t event_cb, void *user_data)
114 * This function registers callback with the network client
117 * @return int - NET_ERR_NONE on success, negative values for errors
118 * @param[in] net_event_cb_t event_cb - Pointer to callback function
119 * void* user_data - Pointer to user data
122 EXPORT_API int net_register_client(net_event_cb_t event_cb, void *user_data)
124 __NETWORK_FUNC_ENTER__;
126 net_err_t Error = NET_ERR_NONE;
128 if (event_cb == NULL) {
129 NETWORK_LOG(NETWORK_ASSERT, "Error!! Invalid EventCb parameter\n");
130 __NETWORK_FUNC_EXIT__;
131 return NET_ERR_INVALID_PARAM;
134 if (NetworkInfo.ClientEventCb != NULL) {
135 NETWORK_LOG(NETWORK_ERROR, "Error!!! Application Already registered\n");
136 __NETWORK_FUNC_EXIT__;
137 return NET_ERR_APP_ALREADY_REGISTERED;
140 if (_net_mutex_init() != NET_ERR_NONE) {
141 __NETWORK_FUNC_EXIT__;
142 return NET_ERR_UNKNOWN;
145 Error = _net_register_signal();
146 if (Error != NET_ERR_NONE) {
147 NETWORK_LOG(NETWORK_ERROR, "Error!!! _net_register_signal() failed. Error [%s]\n",
148 _net_print_error(Error));
149 _net_mutex_destroy();
150 __NETWORK_FUNC_EXIT__;
154 NetworkInfo.ClientEventCb = event_cb;
155 NetworkInfo.user_data = user_data;
156 NetworkInfo.wifi_state = _net_get_wifi_state();
157 _net_init_service_state_table();
159 NETWORK_LOG(NETWORK_HIGH, "Client Register Successfully\n");
161 __NETWORK_FUNC_EXIT__;
167 * @fn EXPORT_API int net_deregister_client(void)
169 * This function deregisters with network client
172 * @return int - NET_ERR_NONE on success, negative values for errors
177 EXPORT_API int net_deregister_client(void)
179 __NETWORK_FUNC_ENTER__;
181 net_err_t Error = NET_ERR_NONE;
183 if (NetworkInfo.ClientEventCb == NULL) {
184 NETWORK_LOG(NETWORK_ERROR, "Error!!! Application was not registered\n");
185 __NETWORK_FUNC_EXIT__;
186 return NET_ERR_APP_NOT_REGISTERED;
189 Error = _net_deregister_signal();
190 if (Error != NET_ERR_NONE) {
191 NETWORK_LOG(NETWORK_ERROR, "Error!!! Failed to deregister signal\n");
192 __NETWORK_FUNC_EXIT__;
196 _net_mutex_destroy();
197 _net_clear_request_table();
199 NetworkInfo.ClientEventCb = NULL;
200 NetworkInfo.user_data = NULL;
202 NETWORK_LOG(NETWORK_HIGH, "Client De-Register Successfull\n");
204 __NETWORK_FUNC_EXIT__;
209 * @fn EXPORT_API int net_get_active_net_info(net_profile_info_t *active_profile_info)
211 * This API returns the information of active(default) network profile.
214 * @return int - NET_ERR_NONE on success, negative values for errors
216 * @param[out] active_profile_info The information of active(default) network profile.
218 EXPORT_API int net_get_active_net_info(net_profile_info_t *active_profile_info)
220 __NETWORK_FUNC_ENTER__;
222 net_err_t Error = NET_ERR_NONE;
224 Error = __net_get_default_profile((void*)active_profile_info, active_profile_info);
226 __NETWORK_FUNC_EXIT__;
231 * @fn EXPORT_API int net_get_active_ipaddress(net_addr_t *ip_address)
233 * This API returns a specific information of active(default) network profile.
236 * @return int - NET_ERR_NONE on success, negative values for errors
238 * @param[out] ip_address Ip address of active(default) network profile.
240 EXPORT_API int net_get_active_ipaddress(net_addr_t *ip_address)
242 __NETWORK_FUNC_ENTER__;
244 net_err_t Error = NET_ERR_NONE;
245 net_profile_info_t active_profile_info;
246 net_dev_info_t *net_info = NULL;
248 Error = __net_get_default_profile((void*)ip_address, &active_profile_info);
249 if (Error != NET_ERR_NONE) {
250 __NETWORK_FUNC_EXIT__;
254 if (active_profile_info.profile_type == NET_DEVICE_CELLULAR)
255 net_info = &active_profile_info.ProfileInfo.Pdp.net_info;
256 else if (active_profile_info.profile_type == NET_DEVICE_WIFI)
257 net_info = &active_profile_info.ProfileInfo.Wlan.net_info;
259 Error = NET_ERR_UNKNOWN;
261 if (net_info != NULL)
262 memcpy(ip_address, &net_info->IpAddr, sizeof(net_addr_t));
264 __NETWORK_FUNC_EXIT__;
269 * @fn EXPORT_API int net_get_active_netmask(net_addr_t *netmask)
271 * This API returns a specific information of active(default) network profile.
274 * @return int - NET_ERR_NONE on success, negative values for errors
276 * @param[out] netmask Netmask of active(default) network profile.
278 EXPORT_API int net_get_active_netmask(net_addr_t *netmask)
280 __NETWORK_FUNC_ENTER__;
282 net_err_t Error = NET_ERR_NONE;
283 net_profile_info_t active_profile_info;
284 net_dev_info_t *net_info = NULL;
286 Error = __net_get_default_profile((void*)netmask, &active_profile_info);
287 if (Error != NET_ERR_NONE) {
288 __NETWORK_FUNC_EXIT__;
292 if (active_profile_info.profile_type == NET_DEVICE_CELLULAR)
293 net_info = &active_profile_info.ProfileInfo.Pdp.net_info;
294 else if (active_profile_info.profile_type == NET_DEVICE_WIFI)
295 net_info = &active_profile_info.ProfileInfo.Wlan.net_info;
297 Error = NET_ERR_UNKNOWN;
299 if (net_info != NULL)
300 memcpy(netmask, &net_info->SubnetMask, sizeof(net_addr_t));
302 __NETWORK_FUNC_EXIT__;
307 * @fn EXPORT_API int net_get_active_gateway(net_addr_t *gateway)
309 * This API returns a specific information of active(default) network profile.
312 * @return int - NET_ERR_NONE on success, negative values for errors
314 * @param[out] gateway Gateway address of active(default) network profile.
316 EXPORT_API int net_get_active_gateway(net_addr_t *gateway)
318 __NETWORK_FUNC_ENTER__;
320 net_err_t Error = NET_ERR_NONE;
321 net_profile_info_t active_profile_info;
322 net_dev_info_t *net_info = NULL;
324 Error = __net_get_default_profile((void*)gateway, &active_profile_info);
325 if (Error != NET_ERR_NONE) {
326 __NETWORK_FUNC_EXIT__;
330 if (active_profile_info.profile_type == NET_DEVICE_CELLULAR)
331 net_info = &active_profile_info.ProfileInfo.Pdp.net_info;
332 else if (active_profile_info.profile_type == NET_DEVICE_WIFI)
333 net_info = &active_profile_info.ProfileInfo.Wlan.net_info;
335 Error = NET_ERR_UNKNOWN;
337 if (net_info != NULL)
338 memcpy(gateway, &net_info->GatewayAddr, sizeof(net_addr_t));
340 __NETWORK_FUNC_EXIT__;
345 * @fn EXPORT_API int net_get_active_dns(net_addr_t *dns)
347 * This API returns a specific information of active(default) network profile.
350 * @return int - NET_ERR_NONE on success, negative values for errors
352 * @param[out] dns DNS address of active(default) network profile.
354 EXPORT_API int net_get_active_dns(net_addr_t *dns)
356 __NETWORK_FUNC_ENTER__;
358 net_err_t Error = NET_ERR_NONE;
359 net_profile_info_t active_profile_info;
360 net_dev_info_t *net_info = NULL;
362 Error = __net_get_default_profile((void*)dns, &active_profile_info);
363 if (Error != NET_ERR_NONE) {
364 __NETWORK_FUNC_EXIT__;
368 if (active_profile_info.profile_type == NET_DEVICE_CELLULAR)
369 net_info = &active_profile_info.ProfileInfo.Pdp.net_info;
370 else if (active_profile_info.profile_type == NET_DEVICE_WIFI)
371 net_info = &active_profile_info.ProfileInfo.Wlan.net_info;
373 Error = NET_ERR_UNKNOWN;
375 if (net_info != NULL)
376 memcpy(dns, &net_info->DnsAddr[0], sizeof(net_addr_t));
378 __NETWORK_FUNC_EXIT__;
383 * @fn EXPORT_API int net_get_active_essid(net_essid_t *essid)
385 * This API returns a specific information of active(default) network profile.
388 * @return int - NET_ERR_NONE on success, negative values for errors
390 * @param[out] essid ESSID of active(default) network profile.
392 EXPORT_API int net_get_active_essid(net_essid_t *essid)
394 __NETWORK_FUNC_ENTER__;
396 net_err_t Error = NET_ERR_NONE;
397 net_profile_info_t active_profile_info;
398 net_wifi_profile_info_t *wlan_info = NULL;
400 Error = __net_get_default_profile((void*)essid, &active_profile_info);
401 if (Error != NET_ERR_NONE) {
402 __NETWORK_FUNC_EXIT__;
406 if (active_profile_info.profile_type == NET_DEVICE_CELLULAR) {
407 NETWORK_LOG(NETWORK_ERROR, "Error!!! Active(default) network is cellular type.\n");
408 Error = NET_ERR_NO_SERVICE;
409 } else if (active_profile_info.profile_type == NET_DEVICE_WIFI) {
410 wlan_info = &active_profile_info.ProfileInfo.Wlan;
411 memcpy(essid->essid, wlan_info->essid, NET_WLAN_ESSID_LEN+1);
413 Error = NET_ERR_UNKNOWN;
415 __NETWORK_FUNC_EXIT__;
420 * @fn EXPORT_API int net_get_active_proxy(net_proxy_t *proxy)
422 * This API returns a specific information of active(default) network profile.
425 * @return int - NET_ERR_NONE on success, negative values for errors
427 * @param[out] proxy Proxy of active(default) network profile.
429 EXPORT_API int net_get_active_proxy(net_proxy_t *proxy)
431 __NETWORK_FUNC_ENTER__;
433 net_err_t Error = NET_ERR_NONE;
434 net_profile_info_t active_profile_info;
435 net_dev_info_t *net_info = NULL;
437 Error = __net_get_default_profile((void*)proxy, &active_profile_info);
438 if (Error != NET_ERR_NONE) {
439 __NETWORK_FUNC_EXIT__;
443 if (active_profile_info.profile_type == NET_DEVICE_CELLULAR)
444 net_info = &active_profile_info.ProfileInfo.Pdp.net_info;
445 else if (active_profile_info.profile_type == NET_DEVICE_WIFI)
446 net_info = &active_profile_info.ProfileInfo.Wlan.net_info;
448 Error = NET_ERR_UNKNOWN;
450 if (net_info != NULL)
451 memcpy(proxy->proxy_addr, net_info->ProxyAddr, NET_PROXY_LEN_MAX+1);
453 __NETWORK_FUNC_EXIT__;
458 * @fn EXPORT_API int net_is_connected(void)
460 * This function check's whether connection manager is connected or not
463 * @return int - TRUE if connected, else FALSE
467 EXPORT_API int net_is_connected(void)
469 char state[CONNMAN_MAX_BUFLEN] = ""; /** Possible value are "online", "offline" and "connected" */
470 net_err_t Error = NET_ERR_NONE;
472 __NETWORK_FUNC_ENTER__;
474 if (NetworkInfo.ClientEventCb == NULL) {
475 NETWORK_LOG(NETWORK_ERROR, "Error!!! Application was not registered\n");
476 __NETWORK_FUNC_EXIT__;
477 return NET_ERR_APP_NOT_REGISTERED;
480 if ((Error = _net_dbus_get_state(state)) != NET_ERR_NONE) {
481 NETWORK_LOG(NETWORK_ERROR, "Error!!! failed to get state. Error [%s]\n",
482 _net_print_error(Error));
483 __NETWORK_FUNC_EXIT__;
487 if ((strcmp(state, "online") == 0) || (strcmp(state, "connected") == 0)) {
488 NETWORK_LOG(NETWORK_HIGH, "State [%s]\n", state);
489 __NETWORK_FUNC_EXIT__;
493 __NETWORK_FUNC_EXIT__;
499 * @fn EXPORT_API int net_get_network_status(net_service_type_t network_type, net_cm_network_status_t* pNetworkStatus)
501 * This function requests wifi/pdp network status
504 * @return int - NET_ERR_NONE on success, negative values for errors
505 * @param[in] net_service_type_t network_type - Network type (wlan/pdp/default), of whose status to be checked.
506 * @param[out] net_cm_network_status_t* pNetworkStatus - Status of the requested network.
509 EXPORT_API int net_get_network_status(net_device_t device_type, net_cm_network_status_t* network_status)
511 net_err_t Error = NET_ERR_NONE;
513 __NETWORK_FUNC_ENTER__;
515 if(NetworkInfo.ClientEventCb == NULL)
517 NETWORK_LOG( NETWORK_ERROR, "Error!!! Application was not registered\n");
518 __NETWORK_FUNC_EXIT__;
519 return NET_ERR_APP_NOT_REGISTERED;
522 if((Error = _net_dbus_get_network_status(device_type, network_status)) != NET_ERR_NONE)
524 NETWORK_LOG( NETWORK_ERROR, "Error!!! failed to get network status. Error [%s]\n",
525 _net_print_error(Error));
526 __NETWORK_FUNC_EXIT__;
530 __NETWORK_FUNC_EXIT__;
535 /*****************************************************************************
536 * ConnMan Wi-Fi Client Interface Async Function Definition
537 *****************************************************************************/
541 * @fn EXPORT_API int net_open_connection_with_profile(const char *profile_name)
543 * This function request open connection for the given profile name.
546 * @return int - NET_ERR_NONE on success, negative values for errors
547 * @param[in] char *ProfileName - Profile Name to be connected
551 EXPORT_API int net_open_connection_with_profile(const char *profile_name)
553 __NETWORK_FUNC_ENTER__;
555 net_err_t Error = NET_ERR_NONE;
557 NETWORK_LOG(NETWORK_HIGH, "ProfileName [%s] passed\n", profile_name);
559 if (_net_check_profile_name(profile_name) != NET_ERR_NONE) {
560 NETWORK_LOG(NETWORK_ERROR, "Error!!! Invalid ProfileName passed\n");
561 __NETWORK_FUNC_EXIT__;
562 return NET_ERR_INVALID_PARAM;
565 if (NetworkInfo.ClientEventCb == NULL) {
566 NETWORK_LOG(NETWORK_ERROR, "Error!!! Application was not registered\n");
567 __NETWORK_FUNC_EXIT__;
568 return NET_ERR_APP_NOT_REGISTERED;
571 if (request_table[NETWORK_REQUEST_TYPE_OPEN_CONNECTION].flag == TRUE) {
572 NETWORK_LOG(NETWORK_ASSERT, "Error!! Request already in progress\n");
573 __NETWORK_FUNC_EXIT__;
574 return NET_ERR_INVALID_OPERATION;
577 request_table[NETWORK_REQUEST_TYPE_OPEN_CONNECTION].flag = TRUE;
578 snprintf(request_table[NETWORK_REQUEST_TYPE_OPEN_CONNECTION].ProfileName,
579 NET_PROFILE_NAME_LEN_MAX+1, "%s", profile_name);
581 if ((Error = _net_dbus_open_connection(profile_name)) != NET_ERR_NONE) {
582 NETWORK_LOG(NETWORK_ERROR,
583 "Error!! Failed to request open connection, Error [%s]\n",
584 _net_print_error(Error));
586 if(request_table[NETWORK_REQUEST_TYPE_OPEN_CONNECTION].flag == TRUE)
587 memset(&request_table[NETWORK_REQUEST_TYPE_OPEN_CONNECTION],
588 0, sizeof(network_request_table_t));
590 __NETWORK_FUNC_EXIT__;
594 NETWORK_LOG(NETWORK_HIGH, "Connect Request Success for ProfileName[%s]\n", profile_name);
595 __NETWORK_FUNC_EXIT__;
600 * @fn EXPORT_API int net_open_connection_with_preference(net_service_type_t service_type)
602 * This function request open connection for the given service type.
605 * @return int - NET_ERR_NONE on success, negative values for errors
606 * @param[in] net_service_type_t service_type - Service type to be connected
609 EXPORT_API int net_open_connection_with_preference(net_service_type_t service_type)
611 __NETWORK_FUNC_ENTER__;
613 net_err_t Error = NET_ERR_NONE;
614 net_profile_name_t profile_name;
615 memset(&profile_name, 0, sizeof(net_profile_name_t));
617 if (service_type != NET_SERVICE_INTERNET &&
618 service_type != NET_SERVICE_MMS &&
619 service_type != NET_SERVICE_WAP) {
620 NETWORK_LOG(NETWORK_ERROR, "Error!!! Invalid Service Type passed\n");
621 __NETWORK_FUNC_EXIT__;
622 return NET_ERR_INVALID_PARAM;
625 if (NetworkInfo.ClientEventCb == NULL) {
626 NETWORK_LOG(NETWORK_ERROR, "Error!!! Application was not registered\n");
627 __NETWORK_FUNC_EXIT__;
628 return NET_ERR_APP_NOT_REGISTERED;
631 if (request_table[NETWORK_REQUEST_TYPE_OPEN_CONNECTION].flag == TRUE) {
632 NETWORK_LOG(NETWORK_ASSERT, "Error!! Request already in progress\n");
633 __NETWORK_FUNC_EXIT__;
634 return NET_ERR_INVALID_OPERATION;
637 Error = _net_get_service_profile(service_type, &profile_name);
638 if (Error != NET_ERR_NONE) {
639 NETWORK_LOG(NETWORK_ASSERT, "Error!! Failed to find service\n");
640 __NETWORK_FUNC_EXIT__;
644 request_table[NETWORK_REQUEST_TYPE_OPEN_CONNECTION].flag = TRUE;
645 snprintf(request_table[NETWORK_REQUEST_TYPE_OPEN_CONNECTION].ProfileName,
646 NET_PROFILE_NAME_LEN_MAX+1, "%s", profile_name.ProfileName);
648 if ((Error = _net_dbus_open_connection(profile_name.ProfileName)) != NET_ERR_NONE) {
649 NETWORK_LOG(NETWORK_ERROR,
650 "Error!! Failed to request open connection, Error [%s]\n",
651 _net_print_error(Error));
653 if(request_table[NETWORK_REQUEST_TYPE_OPEN_CONNECTION].flag == TRUE)
654 memset(&request_table[NETWORK_REQUEST_TYPE_OPEN_CONNECTION],
655 0, sizeof(network_request_table_t));
657 __NETWORK_FUNC_EXIT__;
661 NETWORK_LOG(NETWORK_HIGH, "Connect Request Success for ProfileName[%s]\n",
662 profile_name.ProfileName);
663 __NETWORK_FUNC_EXIT__;
668 * @fn EXPORT_API int net_close_connection(const char *profile_name)
670 * This function requests close connection for the given profile name.
673 * @return int - NET_ERR_NONE on success, negative values for errors
674 * @param[in] char *profile_name - Connected profile Name
678 EXPORT_API int net_close_connection(const char *profile_name)
680 __NETWORK_FUNC_ENTER__;
682 net_err_t Error = NET_ERR_NONE;
684 NETWORK_LOG(NETWORK_HIGH, "ProfileName [%s] passed\n", profile_name);
686 if (_net_check_profile_name(profile_name) != NET_ERR_NONE) {
687 NETWORK_LOG(NETWORK_ERROR, "Error!! Invalid ProfileName parameter\n");
688 __NETWORK_FUNC_EXIT__;
689 return NET_ERR_INVALID_PARAM;
692 if (NetworkInfo.ClientEventCb == NULL) {
693 NETWORK_LOG(NETWORK_ERROR, "Error!!! Application was not registered\n");
694 __NETWORK_FUNC_EXIT__;
695 return NET_ERR_APP_NOT_REGISTERED;
698 if (request_table[NETWORK_REQUEST_TYPE_CLOSE_CONNECTION].flag == TRUE) {
699 NETWORK_LOG(NETWORK_ASSERT, "Error!!! Request already in progress\n");
700 __NETWORK_FUNC_EXIT__;
701 return NET_ERR_INVALID_OPERATION;
704 request_table[NETWORK_REQUEST_TYPE_CLOSE_CONNECTION].flag = TRUE;
705 snprintf(request_table[NETWORK_REQUEST_TYPE_CLOSE_CONNECTION].ProfileName,
706 NET_PROFILE_NAME_LEN_MAX+1, "%s", profile_name);
708 if ((Error = _net_dbus_close_connection(profile_name)) != NET_ERR_NONE) {
709 NETWORK_LOG(NETWORK_ERROR,
710 "Error!! Failed to request close connection, Error [%s]\n",
711 _net_print_error(Error));
713 if (request_table[NETWORK_REQUEST_TYPE_CLOSE_CONNECTION].flag == TRUE)
714 memset(&request_table[NETWORK_REQUEST_TYPE_CLOSE_CONNECTION],
715 0, sizeof(network_request_table_t));
717 __NETWORK_FUNC_EXIT__;
721 __NETWORK_FUNC_EXIT__;
727 #endif /* __cplusplus */