Merge from master.
[framework/messaging/msg-service.git] / plugin / mms_plugin / MmsPluginConnManWrapper.cpp
1 /*
2 * Copyright 2012-2013  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://floralicense.org
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 <errno.h>
18 #include "MmsPluginConnManWrapper.h"
19 #include "MmsPluginDebug.h"
20
21 void network_print_profile(net_profile_info_t *ProfInfo)
22 {
23         int di = 0;
24
25         unsigned char *ipaddr;
26         unsigned char *netmaskaddr;
27         unsigned char *gwaddr;
28         net_dev_info_t *net_info;
29
30         if (ProfInfo->profile_type == NET_DEVICE_CELLULAR) {
31                 ipaddr = (unsigned char *)&ProfInfo->ProfileInfo.Pdp.net_info.IpAddr.Data.Ipv4.s_addr;
32                 netmaskaddr = (unsigned char *)&ProfInfo->ProfileInfo.Pdp.net_info.SubnetMask.Data.Ipv4.s_addr;
33                 gwaddr = (unsigned char *)&ProfInfo->ProfileInfo.Pdp.net_info.GatewayAddr.Data.Ipv4.s_addr;
34                 net_info = &(ProfInfo->ProfileInfo.Pdp.net_info);
35         } else {
36                 MSG_DEBUG("Error!!! Invalid profile type\n");
37                 return;
38         }
39
40         MSG_DEBUG("**************************************************************************************************");
41
42         MSG_DEBUG("Profile Name = [%s]\n", ProfInfo->ProfileName);
43
44         if (ProfInfo->ProfileState == NET_STATE_TYPE_IDLE)
45                 MSG_DEBUG("Profile State = [idle]\n");
46         else if (ProfInfo->ProfileState == NET_STATE_TYPE_FAILURE)
47                 MSG_DEBUG("Profile State = [failure]\n");
48         else if (ProfInfo->ProfileState == NET_STATE_TYPE_ASSOCIATION)
49                 MSG_DEBUG("Profile State = [association]\n");
50         else if (ProfInfo->ProfileState == NET_STATE_TYPE_CONFIGURATION)
51                 MSG_DEBUG("Profile State = [configuration]\n");
52         else if (ProfInfo->ProfileState == NET_STATE_TYPE_READY)
53                 MSG_DEBUG("Profile State = [ready]\n");
54         else if (ProfInfo->ProfileState == NET_STATE_TYPE_ONLINE)
55                 MSG_DEBUG("Profile State = [online]\n");
56         else if (ProfInfo->ProfileState == NET_STATE_TYPE_DISCONNECT)
57                 MSG_DEBUG("Profile State = [disconnect]\n");
58         else
59                 MSG_DEBUG("Profile State = [unknown]\n");
60
61                 MSG_DEBUG("Profile Type = [pdp]\n");
62
63         if (ProfInfo->ProfileInfo.Pdp.ProtocolType == NET_PDP_TYPE_GPRS)
64                 MSG_DEBUG("Profile Protocol Type = [GPRS]\n");
65         else if (ProfInfo->ProfileInfo.Pdp.ProtocolType == NET_PDP_TYPE_EDGE)
66                 MSG_DEBUG("Profile Protocol Type = [EDGE]\n");
67         else if (ProfInfo->ProfileInfo.Pdp.ProtocolType == NET_PDP_TYPE_UMTS)
68                 MSG_DEBUG("Profile Protocol Type = [UMTS]\n");
69         else
70                 MSG_DEBUG("Profile Protocol Type = [NONE]\n");
71
72         MSG_DEBUG("Profile APN = [%s]\n", ProfInfo->ProfileInfo.Pdp.Apn);
73
74         if (ProfInfo->ProfileInfo.Pdp.AuthInfo.AuthType == NET_PDP_AUTH_PAP)
75                 MSG_DEBUG("Profile Auth Type = [PAP]\n");
76         else if (ProfInfo->ProfileInfo.Pdp.AuthInfo.AuthType == NET_PDP_AUTH_CHAP)
77                 MSG_DEBUG("Profile Auth Type = [CHAP]\n");
78         else
79                 MSG_DEBUG("Profile Auth Type = [NONE]\n");
80
81         MSG_DEBUG("Profile Auth UserName = [%s]\n", ProfInfo->ProfileInfo.Pdp.AuthInfo.UserName);
82         MSG_DEBUG("Profile Auth Password = [%s]\n", ProfInfo->ProfileInfo.Pdp.AuthInfo.Password);
83         MSG_DEBUG("Profile Home URL = [%s]\n", ProfInfo->ProfileInfo.Pdp.HomeURL);
84         MSG_DEBUG("Profile MCC = [%s]\n", ProfInfo->ProfileInfo.Pdp.Mcc);
85         MSG_DEBUG("Profile MNC = [%s]\n", ProfInfo->ProfileInfo.Pdp.Mnc);
86         MSG_DEBUG("Profile Roaming = [%d]\n", (int)ProfInfo->ProfileInfo.Pdp.Roaming);
87         MSG_DEBUG("Profile Setup Required = [%d]\n", (int)ProfInfo->ProfileInfo.Pdp.SetupRequired);
88         MSG_DEBUG("Profile Favourite = [%d]\n", (int)ProfInfo->Favourite);
89         MSG_DEBUG("Profile Device Name = [%s]\n", net_info->DevName);
90         MSG_DEBUG("Profile DNS Count = [%d]\n", net_info->DnsCount);
91         for (di = 0; di < net_info->DnsCount; di++) {
92                 unsigned char *dns = (unsigned char *)&net_info->DnsAddr[di].Data.Ipv4.s_addr;
93                 MSG_DEBUG("Profile DNS Address %d = [%d.%d.%d.%d]\n", di+1, dns[0], dns[1], dns[2], dns[3]);
94         }
95
96         if (net_info->IpConfigType == NET_IP_CONFIG_TYPE_DYNAMIC)
97                 MSG_DEBUG("Profile IPv4 Method = [NET_IP_CONFIG_TYPE_DYNAMIC]\n");
98         else if (net_info->IpConfigType == NET_IP_CONFIG_TYPE_STATIC)
99                 MSG_DEBUG("Profile IPv4 Method = [NET_IP_CONFIG_TYPE_STATIC]\n");
100         else if (net_info->IpConfigType == NET_IP_CONFIG_TYPE_FIXED)
101                 MSG_DEBUG("Profile IPv4 Method = [NET_IP_CONFIG_TYPE_FIXED]\n");
102         else if (net_info->IpConfigType == NET_IP_CONFIG_TYPE_OFF)
103                 MSG_DEBUG("Profile IPv4 Method = [NET_IP_CONFIG_TYPE_OFF]\n");
104         else
105                 MSG_DEBUG("Profile IPv4 Method = [UNKNOWN]\n");
106
107         MSG_DEBUG("Profile IP Address = [%d.%d.%d.%d]\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
108         MSG_DEBUG("Profile Netmask = [%d.%d.%d.%d]\n", netmaskaddr[0], netmaskaddr[1], netmaskaddr[2], netmaskaddr[3]);
109         MSG_DEBUG("Profile Gateway = [%d.%d.%d.%d]\n", gwaddr[0], gwaddr[1], gwaddr[2], gwaddr[3]);
110
111         if (net_info->ProxyMethod == NET_PROXY_TYPE_DIRECT)
112                 MSG_DEBUG("Proxy Method = [direct]\n");
113         else if (net_info->ProxyMethod == NET_PROXY_TYPE_AUTO)
114                 MSG_DEBUG("Proxy Method = [auto]\n");
115         else if (net_info->ProxyMethod == NET_PROXY_TYPE_MANUAL)
116                 MSG_DEBUG("Proxy Method = [manual]\n");
117         else
118                 MSG_DEBUG("Proxy Method = [unknown]\n");
119
120         MSG_DEBUG("Profile Proxy = [%s]\n", net_info->ProxyAddr);
121
122         MSG_DEBUG("**************************************************************************************************");
123 }
124
125
126
127 void network_evt_cb (net_event_info_t *event_cb, void *user_data)
128 {
129         MSG_BEGIN();
130
131         MmsPluginCmAgent::instance()->processCBdatas(event_cb, user_data);
132
133         MSG_END();
134 }
135
136
137 MmsPluginCmAgent *MmsPluginCmAgent::pInstance = NULL;
138
139 MmsPluginCmAgent *MmsPluginCmAgent::instance()
140 {
141         if (!pInstance)
142                 pInstance = new MmsPluginCmAgent();
143
144         return pInstance;
145 }
146
147 MmsPluginCmAgent::MmsPluginCmAgent()
148 {
149         MSG_BEGIN();
150
151         isCmOpened = false;
152         bzero(&mmsProfile, sizeof (net_profile_info_t));
153
154         MSG_END();
155 }
156
157 MmsPluginCmAgent::~MmsPluginCmAgent()
158 {
159
160 }
161
162 bool MmsPluginCmAgent::registration()
163 {
164         MSG_BEGIN();
165
166         bool registResult = true;
167
168         if (net_register_client((net_event_cb_t) network_evt_cb, NULL) != NET_ERR_NONE) {
169                 MSG_DEBUG("Error!! net_register_client() failed.\n");
170                 registResult = false;
171         }
172
173         MSG_END();
174
175         return registResult;
176 }
177
178 bool MmsPluginCmAgent::open()
179 {
180         MSG_BEGIN();
181
182         if (!isCmOpened) {
183                 if (!registration())
184                         return false;
185
186                 int netOpenResult = NET_ERR_NONE;
187                 net_service_type_t service_type = NET_SERVICE_MMS;
188
189                 netOpenResult = net_open_connection_with_preference(service_type);
190                 if (netOpenResult != NET_ERR_NONE) {
191                         MSG_DEBUG("Error!! net_open_connection_with_profile() failed. [%d]", netOpenResult);
192                         deregistration();
193                         return false;
194                 }
195
196                 MSG_DEBUG("WAITING UNTIL Network Connection Open.");
197
198                 lock();
199
200                 int time_ret = 0;
201                 time_ret = cv.timedwait(mx.pMutex(), 50);
202
203                 unlock();
204
205                 if (time_ret == ETIMEDOUT) {
206                         MSG_DEBUG("Network Connection Open Time Out.");
207                         deregistration();
208                         return false;
209                 } else {
210                         if(!isCmOpened) {
211                                 MSG_DEBUG("Network connection open failed");
212                                 deregistration();
213                                 return false;
214                         }
215                 }
216         } else {
217                 MSG_DEBUG("Network is already opened.");
218                 return false;
219         }
220
221         MSG_END();
222
223         return isCmOpened;
224 }
225
226
227 void MmsPluginCmAgent::close()
228 {
229         MSG_BEGIN();
230
231         if (isCmOpened) {
232                 int netOpenResult = NET_ERR_NONE;
233
234                 netOpenResult = net_close_connection(mmsProfile.ProfileName);
235                 if (netOpenResult != NET_ERR_NONE) {
236                         MSG_DEBUG("Error!! net_close_connection() failed.\n");
237                         deregistration();
238                         return;
239                 }
240
241                 MSG_DEBUG("WAITING UNTIL Network Connection Close.");
242
243                 lock();
244
245                 int time_ret = 0;
246                 time_ret = cv.timedwait(mx.pMutex(), 50);
247
248                 unlock();
249
250                 if (time_ret == ETIMEDOUT) {
251                         MSG_DEBUG("Network Connection Close Timed Out.");
252                 }
253
254                 deregistration();
255                 isCmOpened = false;
256         } else {
257                 MSG_DEBUG ("Network Connection is not opened.");
258                 return;
259         }
260 }
261
262
263 void MmsPluginCmAgent::deregistration()
264 {
265         MSG_BEGIN();
266
267         int netOpenResult = NET_ERR_NONE;
268
269         netOpenResult = net_deregister_client();
270         if (netOpenResult != NET_ERR_NONE)
271                 MSG_DEBUG("Error!! net_deregister_client() failed. [%d]", netOpenResult);
272         else
273                 MSG_DEBUG ("net_deregister_client() Success.");
274
275         MSG_END();
276 }
277
278
279 void MmsPluginCmAgent::processCBdatas(net_event_info_t *event_cb, void *user_data)
280 {
281         MSG_BEGIN();
282
283         net_dev_info_t *dev_info = NULL;
284         net_profile_info_t *prof_info = NULL;
285
286         switch (event_cb->Event) {
287         case NET_EVENT_NET_CONFIGURE_RSP:
288                 MSG_DEBUG("Received Network Configuration response: %d \n", event_cb->Error);
289                 dev_info = (net_dev_info_t *)event_cb->Data;
290
291                 /*Successful PDP Activation*/
292                 if (event_cb->Error == NET_ERR_NONE) {
293                         char *ip = (char *)&dev_info->IpAddr.Data.Ipv4;
294                         char *netmask = (char *)&dev_info->SubnetMask.Data.Ipv4;
295                         char *gateway = (char *)&dev_info->GatewayAddr.Data.Ipv4;
296                         char *dns1 = (char *)&dev_info->DnsAddr[0].Data.Ipv4.s_addr;
297                         char *dns2 = (char *)&dev_info->DnsAddr[1].Data.Ipv4.s_addr;
298
299                         MSG_DEBUG("= IP address [%d.%d.%d.%d]\n",
300                                         (int)ip[0], ip[1], ip[2], ip[3]);
301                         MSG_DEBUG("= Netmask [%d.%d.%d.%d]\n",
302                                         (int)netmask[0], netmask[1], netmask[2], netmask[3]);
303                         MSG_DEBUG("= Gateway [%d.%d.%d.%d]\n",
304                                         (int)gateway[0], gateway[1], gateway[2], gateway[3]);
305                         MSG_DEBUG("= DNS address [%d.%d.%d.%d]\n",
306                                         (int)dns1[0], dns1[1], dns1[2], dns1[3]);
307                         MSG_DEBUG("= DNS2 address [%d.%d.%d.%d]\n",
308                                         (int)dns2[0], dns2[1], dns2[2], dns2[3]);
309                         MSG_DEBUG("= Device name [%s]\n", dev_info->DevName);
310                         MSG_DEBUG("= Profile name [%s]\n", dev_info->ProfileName);
311                 } else {
312                         MSG_DEBUG("Error!!! Network Configuration Failed %d \n", event_cb->Error);
313                 }
314                 break;
315
316                 /*Response from Datacom for PDP Activation Request*/
317         case NET_EVENT_OPEN_IND:
318                 MSG_DEBUG("Got Open Indication\n");
319                 MSG_DEBUG("Received ACTIVATION response: %d \n", event_cb->Error);
320                 break;
321         case NET_EVENT_OPEN_RSP:
322                 MSG_DEBUG("Got Open RSP\n");
323                 MSG_DEBUG("Received ACTIVATION response: %d \n", event_cb->Error);
324                 prof_info = (net_profile_info_t *)event_cb->Data;
325
326                 /*Successful PDP Activation*/
327                 if (event_cb->Error == NET_ERR_NONE || event_cb->Error == NET_ERR_ACTIVE_CONNECTION_EXISTS) {
328                         network_print_profile(prof_info);
329
330                         lock();
331
332                         memcpy(&mmsProfile, prof_info, sizeof(net_profile_info_t));
333                         isCmOpened = true;
334                         signal();
335
336                         unlock();
337                 } else {
338                         MSG_DEBUG("Activation Failed %d \n", event_cb->Error); // open failed
339                         lock();
340                         isCmOpened = false;
341                         signal();
342                         unlock();
343                 }
344                 break;
345
346         case NET_EVENT_CLOSE_RSP:
347                 MSG_DEBUG("Got Close RSP\n");
348
349                 lock();
350
351                 bzero(&mmsProfile, sizeof(net_profile_info_t));
352                 isCmOpened = false;
353                 signal();
354
355                 unlock();
356                 break;
357
358         case NET_EVENT_CLOSE_IND:
359                 MSG_DEBUG("Got Close IND\n");
360                 break;
361         case NET_EVENT_PROFILE_MODIFY_IND:
362                 MSG_DEBUG("Received Profile modified Indication\n");
363                 MSG_DEBUG("No. of profile [%d]\n", event_cb->Datalength);
364                 break;
365         case NET_EVENT_NET_STATE_IND:
366                 MSG_DEBUG("Received NET_EVENT_NET_STATE_IND\n");
367                 break;
368         default :
369                 MSG_DEBUG("Error! Unknown Event = %d\n\n", event_cb->Event);
370                 break;
371         }
372
373         MSG_END();
374 }
375
376
377 bool MmsPluginCmAgent::getDeviceName(char *deviceName)
378 {
379         if (!isCmOpened)
380                 return false;
381
382         int deviceNameLength = strlen(mmsProfile.ProfileInfo.Pdp.net_info.DevName);
383
384         if (deviceNameLength > 0) {
385                 strncpy(deviceName, mmsProfile.ProfileInfo.Pdp.net_info.DevName, deviceNameLength);
386                 deviceName[deviceNameLength] = '\0';
387                 return true;
388         }
389
390         return false;
391 }
392
393
394 bool MmsPluginCmAgent::getHomeURL(char *homeURL)
395 {
396         if (!isCmOpened)
397                 return false;
398
399         int homeURLLength = strlen(mmsProfile.ProfileInfo.Pdp.HomeURL);
400
401         if (homeURLLength > 0) {
402                 strncpy(homeURL, mmsProfile.ProfileInfo.Pdp.HomeURL, homeURLLength);
403                 homeURL[homeURLLength] = '\0';
404                 return true;
405         }
406
407         return false;
408 }
409
410
411 bool MmsPluginCmAgent::getProxyAddr(char *proxyAddr)
412 {
413         if (!isCmOpened)
414                 return false;
415
416         int proxyAddrLength = strlen(mmsProfile.ProfileInfo.Pdp.net_info.ProxyAddr);
417
418         if (proxyAddrLength > 0) {
419                 if (strchr(mmsProfile.ProfileInfo.Pdp.net_info.ProxyAddr, ':') == NULL)
420                         return false;
421
422                 proxyAddrLength = proxyAddrLength - strlen(strchr(mmsProfile.ProfileInfo.Pdp.net_info.ProxyAddr, ':'));
423                 strncpy(proxyAddr, mmsProfile.ProfileInfo.Pdp.net_info.ProxyAddr, proxyAddrLength);
424                 proxyAddr[proxyAddrLength] = '\0';
425
426                 return true;
427         }
428
429         return false;
430 }
431
432
433 int MmsPluginCmAgent::getProxyPort()
434 {
435         if (!isCmOpened)
436                 return -1;
437
438         int proxyAddrLength = strlen(mmsProfile.ProfileInfo.Pdp.net_info.ProxyAddr);
439
440         if (proxyAddrLength > 0) {
441                 char *pPort = strchr(mmsProfile.ProfileInfo.Pdp.net_info.ProxyAddr, ':') + 1;
442                 /* pPort cannot be NULL */
443                 return atoi(pPort);
444         }
445
446         return -1;
447 }
448