2 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "MmsPluginConnManWrapper.h"
20 #include "MmsPluginDebug.h"
21 #include "MmsPluginHttp.h"
22 #include "net_connection.h"
23 #include "MmsPluginUtil.h"
25 #define MMS_CONTEXT_INVOKE_WAIT_TIME 30
26 #define MMS_CONNECTION_API_WAIT_TIME 420
30 static connection_h g_connection = NULL;
31 static connection_profile_h g_profile = NULL;
33 void __connection_profile_print(connection_profile_h profile)
36 char *profile_id = NULL;
37 char *profile_name = NULL;
38 char *interface_name = NULL;
39 char *ip_address = NULL;
40 char *subnet_mask = NULL;
41 char *gateway_address = NULL;
42 char *dns_address = NULL;
43 char *proxy_address = NULL;
45 char *user_name = NULL;
46 char *password = NULL;
47 char *home_url = NULL;
50 connection_profile_type_e profile_type;
51 connection_profile_state_e profile_state;
52 connection_ip_config_type_e ip_type = CONNECTION_IP_CONFIG_TYPE_NONE;
53 connection_proxy_type_e proxy_type;
54 connection_cellular_service_type_e service_type = CONNECTION_CELLULAR_SERVICE_TYPE_UNKNOWN;
55 connection_cellular_auth_type_e auth_type = CONNECTION_CELLULAR_AUTH_TYPE_NONE;
57 MSG_DEBUG("**************************************************************************************************");
58 ret = connection_profile_get_id(profile, &profile_id);
59 MSG_DEBUG("return value of connection_profile_get_id [%d]", ret);
60 MSG_SEC_INFO("Profile Id = [%s]", profile_id);
62 ret = connection_profile_get_name(profile, &profile_name);
63 MSG_SEC_INFO("Profile Name = [%s]", profile_name);
65 ret = connection_profile_get_type(profile, &profile_type);
67 if (profile_type == CONNECTION_PROFILE_TYPE_CELLULAR) {
68 MSG_SEC_INFO("Profile Type = [CELLULAR]");
69 } else if (profile_type == CONNECTION_PROFILE_TYPE_WIFI) {
70 MSG_SEC_INFO("Profile Type = [WIFI]");
71 } else if (profile_type == CONNECTION_PROFILE_TYPE_ETHERNET) {
72 MSG_SEC_INFO("Profile Type = [ETHERNET]");
73 } else if (profile_type == CONNECTION_PROFILE_TYPE_BT) {
74 MSG_SEC_INFO("Profile Type = [BT]");
76 MSG_SEC_INFO("Profile Type = Unknown [%d]", profile_type);
79 ret = connection_profile_get_network_interface_name(profile, &interface_name);
80 MSG_SEC_INFO("Profile Interface Name = [%s]", interface_name);
82 ret = connection_profile_get_state(profile, &profile_state);
83 if (profile_state == CONNECTION_PROFILE_STATE_DISCONNECTED) {
84 MSG_SEC_INFO("Profile State = [DISCONNECTED]");
85 } else if (profile_state == CONNECTION_PROFILE_STATE_ASSOCIATION) {
86 MSG_SEC_INFO("Profile State = [ASSOCIATION]");
87 } else if (profile_state == CONNECTION_PROFILE_STATE_CONFIGURATION) {
88 MSG_SEC_INFO("Profile State = [CONFIGURATION]");
89 } else if (profile_state == CONNECTION_PROFILE_STATE_CONNECTED) {
90 MSG_SEC_INFO("Profile State = [CONNECTED]");
92 MSG_SEC_INFO("Profile State = Unknown [%d]", profile_state);
95 ret = connection_profile_get_ip_config_type(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &ip_type);
96 MSG_SEC_INFO("Profile Ip Config Type = [%d]", ip_type);
98 ret = connection_profile_get_ip_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &ip_address);
99 MSG_SEC_INFO("Profile Ip Address = [%s]", ip_address);
101 ret = connection_profile_get_subnet_mask(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &subnet_mask);
102 MSG_SEC_INFO("Profile Subnet Mask = [%s]", subnet_mask);
104 ret = connection_profile_get_gateway_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &gateway_address);
105 MSG_SEC_INFO("Profile Gateway Address = [%s]", gateway_address);
107 ret = connection_profile_get_dns_address(profile, 1, CONNECTION_ADDRESS_FAMILY_IPV4, &dns_address);
108 MSG_SEC_INFO("Profile Dns Address = [%s]", dns_address);
110 ret = connection_profile_get_proxy_type(profile, &proxy_type);
111 MSG_SEC_INFO("Profile Proxy Type = [%d]", proxy_type);
113 ret = connection_profile_get_proxy_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &proxy_address);
114 MSG_SEC_INFO("Profile Proxy Address = [%s]", proxy_address);
116 ret = connection_profile_get_cellular_service_type(profile, &service_type);
117 if (service_type == CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET) {
118 MSG_SEC_INFO("Profile Service Type = [INTERNET]");
119 } else if (service_type == CONNECTION_CELLULAR_SERVICE_TYPE_MMS) {
120 MSG_SEC_INFO("Profile Service Type = [MMS]");
121 } else if (service_type == CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET) {
122 MSG_SEC_INFO("Profile Service Type = [PREPAID_INTERNET]");
123 } else if (service_type == CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_MMS) {
124 MSG_SEC_INFO("Profile Service Type = [PREPAID_MMS]");
125 } else if (service_type == CONNECTION_CELLULAR_SERVICE_TYPE_TETHERING) {
126 MSG_SEC_INFO("Profile Service Type = [TETHERING]");
127 } else if (service_type == CONNECTION_CELLULAR_SERVICE_TYPE_APPLICATION) {
128 MSG_SEC_INFO("Profile Service Type = [APPLICATION]");
130 MSG_SEC_INFO("Profile Service Type = [Unknown][%d]", service_type);
133 ret = connection_profile_get_cellular_apn(profile, &apn);
134 MSG_SEC_INFO("Profile Apn = [%s]", apn);
136 ret = connection_profile_get_cellular_auth_info(profile, &auth_type, &user_name, &password);
137 MSG_SEC_INFO("Profile Auth Type = [%d]", &auth_type);
138 MSG_SEC_INFO("Profile Auth Name = [%s]", &user_name);
139 MSG_SEC_INFO("Profile Auth Passward = [%s]", &password);
141 ret = connection_profile_get_cellular_home_url(profile, &home_url);
142 MSG_SEC_INFO("Profile Home Url = [%s]", home_url);
144 ret = connection_profile_is_cellular_roaming(profile, &is_roaming);
145 MSG_SEC_INFO("Profile Roaming = [%d]", is_roaming);
146 MSG_DEBUG("**************************************************************************************************");
148 MSG_FREE(profile_id);
149 MSG_FREE(profile_name);
150 MSG_FREE(interface_name);
151 MSG_FREE(ip_address);
152 MSG_FREE(subnet_mask);
153 MSG_FREE(gateway_address);
154 MSG_FREE(dns_address);
155 MSG_FREE(proxy_address);
162 static void __connection_type_changed_cb(connection_type_e type, void* user_data)
164 MSG_INFO("Type changed callback, connection type : %d", type);
167 static void __connection_ip_changed_cb(const char* ipv4_address, const char* ipv6_address, void* user_data)
169 MSG_INFO("IP changed callback, IPv4 address : %s, IPv6 address : %s",
170 ipv4_address, (ipv6_address ? ipv6_address : "NULL"));
173 static void __connection_proxy_changed_cb(const char* ipv4_address, const char* ipv6_address, void* user_data)
175 MSG_INFO("Proxy changed callback, IPv4 address : %s, IPv6 address : %s",
176 ipv4_address, (ipv6_address ? ipv6_address : "NULL"));
179 static void __connection_profile_opened_cb(connection_error_e result, void* user_data)
181 if (result == CONNECTION_ERROR_NONE || result == CONNECTION_ERROR_ALREADY_EXISTS)
182 MSG_INFO("Connection open Succeeded [%d]", result);
184 MSG_ERR("Connection open Failed, err : %d", result);
186 MmsPluginCmAgent *cmAgent = MmsPluginCmAgent::instance();
188 cmAgent->connection_profile_open_callback(result, user_data);
191 static void __connection_profile_closed_cb(connection_error_e result, void* user_data)
193 if (result == CONNECTION_ERROR_NONE)
194 MSG_INFO("Connection close Succeeded");
196 MSG_ERR("Connection close Failed, err : %d", result);
198 MmsPluginCmAgent *cmAgent = MmsPluginCmAgent::instance();
200 cmAgent->connection_profile_close_callback(result, user_data);
204 void __connection_profile_state_changed_cb(connection_profile_state_e state, void* user_data)
206 MmsPluginCmAgent *cmAgent = MmsPluginCmAgent::instance();
208 cmAgent->connection_profile_state_changed_cb(state, user_data);
212 static gboolean __connection_create(void *pVoid)
217 bool *ret_val = (bool *)pVoid;
220 MSG_INFO("connection already exist");
223 int err = connection_create(&g_connection);
225 if (CONNECTION_ERROR_NONE == err) {
227 connection_cellular_state_e cellular_state;
228 connection_type_e net_state;
230 err = connection_get_cellular_state(g_connection, &cellular_state);
232 err = connection_get_type(g_connection, &net_state);
234 if (cellular_state == CONNECTION_CELLULAR_STATE_AVAILABLE
235 || cellular_state == CONNECTION_CELLULAR_STATE_CONNECTED) {
237 MSG_INFO("Client registration success [%p], cellular_state [%d], net_state [%d]", g_connection, cellular_state, net_state);
239 err = connection_set_type_changed_cb(g_connection, __connection_type_changed_cb, NULL);
241 err = connection_set_ip_address_changed_cb(g_connection, __connection_ip_changed_cb, NULL);
243 err = connection_set_proxy_address_changed_cb(g_connection, __connection_proxy_changed_cb, NULL);
248 MSG_INFO("Client registration Failed, cellular state [%d], net_state [%d]", cellular_state, net_state);
249 connection_destroy(g_connection);
254 MSG_WARN("Client registration failed %d", err);
266 static gboolean __connection_destroy(void *pVoid)
272 if (g_connection != NULL) {
273 rv = connection_destroy(g_connection);
275 MSG_INFO("connection destory !!");
277 MSG_ERR("Cannot connection destroy : Handle is NULL");
278 rv = CONNECTION_ERROR_INVALID_OPERATION;
281 MSG_DEBUG("return value of connection destroy [%d]", rv);
287 static gboolean __connection_profile_open(void *pVoid)
291 int netOpenResult = MSG_CM_ERR_NONE;
292 int *ret_val = (int *)pVoid;
296 MSG_WARN("connection profile Already exist!!, It will destroy");
297 connection_profile_unset_state_changed_cb(g_profile);
298 connection_profile_destroy(g_profile);
302 err = connection_get_default_cellular_service_profile(g_connection, CONNECTION_CELLULAR_SERVICE_TYPE_MMS, &g_profile);
304 if (err != CONNECTION_ERROR_NONE) {
305 MSG_ERR("connection_get_default_cellular_service_profile Failed!! [%d]", err);
306 netOpenResult = MSG_CM_ERR_UNKNOWN;
309 err = connection_profile_set_state_changed_cb(g_profile, __connection_profile_state_changed_cb, g_profile);
311 if (connection_open_profile(g_connection, g_profile, __connection_profile_opened_cb, NULL) != CONNECTION_ERROR_NONE) {
312 MSG_ERR("Connection open Failed!!");
313 netOpenResult = MSG_CM_ERR_UNKNOWN;
320 *ret_val = netOpenResult;
321 MSG_DEBUG("[%d]", netOpenResult);
329 static gboolean __connection_profile_close(void *pVoid)
333 int netOpenResult = MSG_CM_ERR_NONE;
335 int *ret_val = (int *)pVoid;
338 connection_profile_unset_state_changed_cb(g_profile);
340 if (connection_close_profile(g_connection, g_profile, __connection_profile_closed_cb, NULL) != CONNECTION_ERROR_NONE) {
341 MSG_ERR("Connection close Failed!!");
342 netOpenResult = MSG_CM_ERR_UNKNOWN;
345 connection_profile_destroy(g_profile);
350 *ret_val = netOpenResult;
359 void context_invoke_end_cb(gpointer data)
363 MSG_INFO("@@ SIGNAL @@");
371 * Network api should run at g_main_loop to receive callback
373 void context_invoke(GSourceFunc func, void *ret)
381 g_main_context_invoke_full(NULL, G_PRIORITY_DEFAULT, func, ret, context_invoke_end_cb);
383 MSG_INFO("@@ WAIT @@");
385 time_ret = g_cv.timedwait(g_mx.pMutex(), MMS_CONTEXT_INVOKE_WAIT_TIME);
389 if (time_ret == ETIMEDOUT) {
390 MSG_INFO("@@ WAKE by timeout@@");
392 MSG_INFO("@@ WAKE by signal@@");
398 MmsPluginCmAgent *MmsPluginCmAgent::pInstance = NULL;
400 MmsPluginCmAgent *MmsPluginCmAgent::instance()
403 pInstance = new MmsPluginCmAgent();
408 MmsPluginCmAgent::MmsPluginCmAgent()
415 interface_name = NULL;
416 proxy_address = NULL;
420 MmsPluginCmAgent::~MmsPluginCmAgent()
423 MSG_FREE(interface_name);
424 MSG_FREE(proxy_address);
427 bool MmsPluginCmAgent::open()
431 int netOpenResult = MSG_CM_ERR_NONE;
432 int bConnection = false;
437 context_invoke(__connection_create, &bConnection);
439 if (bConnection == false || g_connection == NULL) {
440 MSG_ERR("Failed __connection_create");
445 MSG_WARN("connection profile already exist");
450 waitProfileOpen = true;
452 context_invoke(__connection_profile_open, &netOpenResult);
454 if (netOpenResult != MSG_CM_ERR_NONE) {
455 MSG_ERR("Failed __connection_profile_open. [%d]", netOpenResult);
459 MSG_INFO("## WAITING UNTIL __connection_profile_state CONNECT. ##");
461 // cv.wait(mx.pMutex());
462 // MSG_INFO("## WAKE by SIGNAL ##");
464 time_ret = cv.timedwait(mx.pMutex(), MMS_CONNECTION_API_WAIT_TIME); // isCmOpened will changed by processCBdatas
466 if (time_ret == ETIMEDOUT) {
467 MSG_WARN("## WAKE by timeout ##");
469 MSG_INFO("## WAKE by SIGNAL ##");
472 if(isCmOpened == false) {
483 context_invoke(__connection_profile_close, NULL);
484 context_invoke( __connection_destroy, NULL);
491 void MmsPluginCmAgent::close()
497 int netOpenResult = MSG_CM_ERR_NONE;
502 if (g_profile == NULL) {
503 MSG_INFO("connection profile is NULL");
507 context_invoke(__connection_profile_close, &netOpenResult);
509 if (netOpenResult != MSG_CM_ERR_NONE) {
510 MSG_ERR("Failed __connection_profile_close. [%d]", netOpenResult);
514 MSG_INFO("## WAITING UNTIL connection_profile_close_callback ##");
516 time_ret = cv.timedwait(mx.pMutex(), MMS_CONNECTION_API_WAIT_TIME);
518 if (time_ret == ETIMEDOUT) {
519 MSG_WARN("## WAKE by timeout ##");
521 MSG_INFO("## WAKE by SIGNAL ##");
527 context_invoke(__connection_destroy, NULL);
532 MSG_FREE(this->home_url);
533 MSG_FREE(this->interface_name);
534 MSG_FREE(this->proxy_address);
541 //profile open callback
542 void MmsPluginCmAgent::connection_profile_open_callback(connection_error_e result, void* user_data)
546 connection_cellular_state_e state;
547 connection_profile_h profile = NULL;
548 connection_profile_state_e profile_state;
549 int err = CONNECTION_ERROR_NONE;
551 if (result == CONNECTION_ERROR_NONE || result == CONNECTION_ERROR_ALREADY_EXISTS) {
553 err = connection_get_cellular_state(g_connection, &state);
555 MSG_INFO("connection_get_cellular_state ret [%d], state [%d]", err, state);
557 err = connection_get_default_cellular_service_profile(g_connection, CONNECTION_CELLULAR_SERVICE_TYPE_MMS, &profile);
558 if (err != CONNECTION_ERROR_NONE || profile == NULL) {
560 MSG_ERR("Failed connection_get_default_cellular_service_profile. err [%d], profile [%p]", err, profile);
561 goto __SIGNAL_RETURN;
564 err = connection_profile_get_state(profile, &profile_state);
566 MSG_DEBUG("profile state [%d]", profile_state);
568 if (profile_state == CONNECTION_PROFILE_STATE_CONNECTED && waitProfileOpen == true) {
570 __connection_profile_print(profile);
572 MSG_FREE(this->home_url);
573 MSG_FREE(this->interface_name);
574 MSG_FREE(this->proxy_address);
576 err = connection_profile_get_cellular_home_url(profile, &this->home_url);
577 if (err != CONNECTION_ERROR_NONE) {
578 MSG_ERR("Failed connection_profile_get_cellular_home_url");
581 err = connection_profile_get_network_interface_name(profile, &this->interface_name);
582 if (err != CONNECTION_ERROR_NONE) {
583 MSG_ERR("Failed connection_profile_get_cellular_home_url");
586 err = connection_profile_get_proxy_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &this->proxy_address);
587 if (err != CONNECTION_ERROR_NONE) {
588 MSG_ERR("Failed connection_profile_get_cellular_home_url");
593 goto __SIGNAL_RETURN; //open success
596 goto __NO_SIGNAL_RETURN; //Just open success
600 MSG_ERR("connection open profile Failed!! [%d]", result);
602 goto __SIGNAL_RETURN;
606 __NO_SIGNAL_RETURN: //Just Open
608 connection_profile_destroy(profile);
612 __SIGNAL_RETURN: //Error or Already connected
614 connection_profile_destroy(profile);
616 if (waitProfileOpen == true) {//open fail
617 waitProfileOpen = false;
618 MSG_INFO("## SIGNAL ##");
626 void MmsPluginCmAgent::connection_profile_close_callback(connection_error_e result, void* user_data)
629 MSG_INFO("result [%d]", result);
630 MSG_INFO("## SIGNAL ##");
636 void MmsPluginCmAgent::connection_profile_state_changed_cb(connection_profile_state_e state, void* user_data)
643 connection_profile_h profile = NULL;
645 MSG_INFO("state [%d]", state);
647 if (state != CONNECTION_PROFILE_STATE_CONNECTED) {
649 goto __NO_SIGNAL_RETURN;
652 if (isCmOpened == true) {
653 MSG_INFO("already opened");
654 goto __SIGNAL_RETURN;
657 /* Should get profile to get latest profile info*/
658 err = connection_get_default_cellular_service_profile(g_connection, CONNECTION_CELLULAR_SERVICE_TYPE_MMS, &profile);
659 if (err != CONNECTION_ERROR_NONE || profile == NULL) {
661 MSG_ERR("Failed connection_get_default_cellular_service_profile. err [%d], profile [%p]", err, profile);
662 goto __SIGNAL_RETURN;
665 if (state == CONNECTION_PROFILE_STATE_CONNECTED ) {
666 __connection_profile_print(profile);
668 MSG_FREE(this->home_url);
669 MSG_FREE(this->interface_name);
670 MSG_FREE(this->proxy_address);
672 err = connection_profile_get_cellular_home_url(profile, &this->home_url);
673 if (err != CONNECTION_ERROR_NONE) {
674 MSG_ERR("Failed connection_profile_get_cellular_home_url");
677 err = connection_profile_get_network_interface_name(profile, &this->interface_name);
678 if (err != CONNECTION_ERROR_NONE) {
679 MSG_ERR("Failed connection_profile_get_cellular_home_url");
682 err = connection_profile_get_proxy_address(profile, CONNECTION_ADDRESS_FAMILY_IPV4, &this->proxy_address);
683 if (err != CONNECTION_ERROR_NONE) {
684 MSG_ERR("Failed connection_profile_get_cellular_home_url");
688 goto __SIGNAL_RETURN;
691 __NO_SIGNAL_RETURN://Default
695 __SIGNAL_RETURN: //Error or connected
697 connection_profile_destroy(profile);
699 if (waitProfileOpen == true) {
700 waitProfileOpen = false;
701 MSG_INFO("## SIGNAL ##");
708 bool MmsPluginCmAgent::getInterfaceName(const char **deviceName)
713 if (deviceName == NULL)
716 *deviceName = interface_name;
721 bool MmsPluginCmAgent::getHomeUrl(const char **homeURL)
735 bool MmsPluginCmAgent::getProxyAddr(const char **proxyAddr)
740 if (proxyAddr == NULL)
743 *proxyAddr = proxy_address;