merge wrt-plugins-tizen_0.2.0-3
[platform/framework/web/wrt-plugins-tizen.git] / src / platform / Tizen / Call / CallService.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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 <cassert>
18 #include <Commons/Exception.h>
19 #include <ITapiCall.h>
20 #include <ITapiSs.h>
21 #include <ITapiSim.h>
22 #include <call.h>
23 #include <dpl/log/log.h>
24 #include <API/Call/CallDefine.h>
25 #include "CallService.h"
26
27 using namespace WrtDeviceApis::Commons;
28 using namespace TizenApis::Api::Call;
29 using namespace DPL;
30
31 namespace TizenApis {
32 namespace Platform {
33 namespace Call {
34
35 namespace {
36 static void callAsyncCB(TelTapiEvent_t *event, void *userdata)
37 {
38         if (event == NULL || userdata == NULL) {
39                 return;
40         }
41
42         (static_cast<CallService*>(userdata))->changedEvent(static_cast<void*>(event));
43 }
44 }
45
46 CallService::CallService() : m_lockSendUSSD(false)
47 {
48         m_handles = handleListPtr(new handleList());
49         if (tel_init() == TAPI_API_SUCCESS) {
50                 LogDebug("TAPI init success");
51                 std::string widget("org.tizen.widget");
52                 tel_register_app_name((char*)(widget.c_str()));
53         }
54 }
55
56 CallService::~CallService()
57 {
58         deregisterCallEvent();
59         tel_deinit();
60 }
61
62 void CallService::registerCallEvent()
63 {
64         TapiResult_t api_err = TAPI_API_SUCCESS;
65         unsigned int handle = 0;
66
67         int call_event_list[] =
68         {
69                 TAPI_EVENT_SS_USSD_CNF,
70         };
71
72         int num_event = sizeof(call_event_list)/sizeof(int);
73         for (int index = 0 ; index < num_event; index++) {
74                 api_err = (TapiResult_t)tel_register_event(call_event_list[index], &handle, (TelAppCallback)&callAsyncCB, this);
75                 m_handles->push_back(handle);
76         }
77         LogDebug("register TAPI event count[" << m_handles->size() << "]");
78 }
79
80 void CallService::deregisterCallEvent()
81 {
82         std::vector<unsigned int>::iterator it = m_handles->begin();
83
84         for (;it < m_handles->end();) {
85                 tel_deregister_event((*it));
86                 it = CallService::m_handles->erase(it);
87                 ++it;
88         }
89         LogDebug("The handle of TAPI event is removed. (" << m_handles->size() << ")");
90 }
91
92 void CallService::launchDialer(const EventLaunchDialerPtr& event)
93 {
94         EventRequestReceiver<EventLaunchDialer>::PostRequest(event);
95 }
96
97 void CallService::sendUSSD(const EventSendUSSDPtr& event)
98 {
99         EventRequestReceiver<EventSendUSSD>::PostRequest(event);
100 }
101
102 StringListPtr CallService::getVoicemailNumbers()
103 {
104         StringListPtr numberList = StringListPtr(new StringList());
105         TelSimMailboxNumbers_s mbox;
106
107         if (tel_get_sim_mailbox_info(&mbox) != TAPI_API_SUCCESS) {
108                 return numberList;
109         }
110
111         if (mbox.voice_line1.bUsed != 0) {
112                 std::string DiallingNum1(mbox.voice_line1.DiallingNum);
113                 if (DiallingNum1.size() > 0)
114                         numberList->push_back(DiallingNum1);
115         }
116
117         if (mbox.voice_line2.bUsed != 0) {
118                 std::string DiallingNum2(mbox.voice_line2.DiallingNum);
119                 if (DiallingNum2.size() > 0)
120                         numberList->push_back(DiallingNum2);
121         }
122
123         if (mbox.video.bUsed != 0) {
124                 std::string DiallingNum3(mbox.video.DiallingNum);
125                 if (DiallingNum3.size() > 0)
126                         numberList->push_back(DiallingNum3);
127         }
128
129         if (mbox.fax.bUsed != 0) {
130                 std::string DiallingNum4(mbox.fax.DiallingNum);
131                 if (DiallingNum4.size() > 0)
132                         numberList->push_back(DiallingNum4);
133         }
134
135         if (mbox.email.bUsed != 0) {
136                 std::string DiallingNum5(mbox.email.DiallingNum);
137                 if (DiallingNum5.size() > 0)
138                         numberList->push_back(DiallingNum5);
139         }
140
141         LogDebug("return voicemail Numbers count [" << numberList->size() << "]");
142         return numberList;
143
144 }
145
146 StringListPtr CallService::getSubscriberNumbers()
147 {
148         StringListPtr numberList = StringListPtr(new StringList());
149         TelSimSubscriberInfo_t msisdn;
150
151         if (tel_get_sim_msisdn(&msisdn) != TAPI_API_SUCCESS) {
152                 return numberList;
153         }
154
155         std::string sub1(msisdn.num);
156         if (sub1.size() > 0)
157                 numberList->push_back(sub1);
158
159         LogDebug("return subscriber count [" << numberList->size() << "]");
160         return numberList;
161 }
162
163 StringListPtr CallService::getEmergencyNumbers()
164 {
165         StringListPtr eccNumber = StringListPtr(new StringList());
166         TelSimCardType_t cardType;
167         TelSimEccData_t eccData;
168         int eccCount = 0;
169
170         if (tel_get_sim_type(&cardType) != TAPI_API_SUCCESS) {
171                 return eccNumber;
172         }
173
174         if (tel_get_sim_ecc(&eccData, &eccCount) != TAPI_API_SUCCESS) {
175                 return eccNumber;
176         }
177
178         LogDebug("eccCount [" << eccCount << "] cardType[" << cardType <<"]");
179
180         if (eccCount > 0) {
181                 if (cardType == TAPI_SIM_CARD_TYPE_GSM) {
182                         std::string ecc1 (eccData.EccInfo.szEcc1);
183                         std::string ecc2 (eccData.EccInfo.szEcc2);
184                         std::string ecc3 (eccData.EccInfo.szEcc3);
185                         std::string ecc4 (eccData.EccInfo.szEcc4);
186                         std::string ecc5 (eccData.EccInfo.szEcc5);
187
188                         if (ecc1.size() > 0)
189                                 eccNumber->push_back(ecc1);
190                         if (ecc2.size() > 0)
191                                 eccNumber->push_back(ecc2);
192                         if (ecc3.size() > 0)
193                                 eccNumber->push_back(ecc3);
194                         if (ecc4.size() > 0)
195                                 eccNumber->push_back(ecc4);
196                         if (ecc5.size() > 0)
197                                 eccNumber->push_back(ecc5);
198                 } else if (cardType == TAPI_SIM_CARD_TYPE_USIM) {
199                         for (int cnt = 0; cnt < eccCount; cnt++) {
200                                 std::string ecc (eccData.UeccInfo[cnt].szEcc);
201                                 if (ecc.size() > 0)
202                                         eccNumber->push_back(ecc);
203                         }
204                 }
205         }
206
207         LogDebug("return ecc count [" << eccNumber->size() << "]");
208         return eccNumber;
209 }
210
211 void CallService::OnRequestReceived(const EventLaunchDialerPtr& event)
212 {
213         try {
214                 event->switchToManualAnswer();
215
216                 std::string remoteParty(STR_TEL_URI);
217                 remoteParty.append(event->getRemoteParty());
218                 remoteParty.append(event->getExtension());
219
220                 service_h service;
221                 service_create(&service);
222
223                 service_set_operation(service, SERVICE_OPERATION_DEFAULT);
224                 service_set_package(service, event->getAppId().c_str());
225
226                 service_set_uri(service, remoteParty.c_str());
227
228                 if (service == NULL) {
229                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "Unknown exception");
230                 }
231
232                 int result = service_send_launch_request (service, NULL, NULL);
233
234                 if (result != SERVICE_ERROR_NONE) {
235                         switch (result){
236                         case SERVICE_ERROR_INVALID_PARAMETER:
237                                 LogDebug("service_send_launch_request returns SERVICE_ERROR_INVALID_PARAMETER");
238                                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
239                                 break;
240                         case SERVICE_ERROR_OUT_OF_MEMORY:
241                                 LogDebug("service_send_launch_request returns SERVICE_ERROR_OUT_OF_MEMORY");
242                                 event->setExceptionCode(ExceptionCodes::PlatformException);
243                                 break;
244                         case SERVICE_ERROR_APP_NOT_FOUND:
245                                 LogDebug("service_send_launch_request returns SERVICE_ERROR_APP_NOT_FOUND");
246                                 event->setExceptionCode(ExceptionCodes::NotFoundException);
247                                 break;
248                         default:
249                                 LogDebug("service_send_launch_request returns UNKNOWN ERROR!!!");
250                                 event->setExceptionCode(ExceptionCodes::UnknownException);
251                                 break;
252                         }
253                 } else {
254                         LogDebug("service_send_launch_request successful");
255                 }
256
257                 EventRequestReceiver<EventLaunchDialer>::ManualAnswer(event);
258                 service_destroy(service);
259         } catch (const PlatformException& ex) {
260                 LogError("Exception: " << ex.GetMessage());
261                 event->setExceptionCode(ExceptionCodes::PlatformException);
262                 EventRequestReceiver<EventLaunchDialer>::ManualAnswer(event);
263         }
264 }
265
266 void CallService::OnRequestReceived(const EventSendUSSDPtr& event)
267 {
268         try {
269                 event->switchToManualAnswer();
270                 if (m_lockSendUSSD == false) {
271                         m_lockSendUSSD = true;
272                         m_EventSendUSSDPtr = event;
273                 } else {
274                         event->setExceptionCode(ExceptionCodes::PlatformException);
275                         EventRequestReceiver<EventSendUSSD>::ManualAnswer(event);
276                 }
277
278                 registerCallEvent();
279
280                 int reqId = 0;
281                 TelSsUssdMsgInfo_t ussdInfo;
282
283                 std::string cmd(m_EventSendUSSDPtr->getCommand());
284
285                 ussdInfo.UssdStringLength = (int)cmd.size();
286                 if (ussdInfo.UssdStringLength >= TAPI_SS_USSD_DATA_SIZE_MAX) {
287                         ussdInfo.UssdStringLength = TAPI_SS_USSD_DATA_SIZE_MAX - 1;
288                 }
289                 cmd.copy(ussdInfo.szUssdString, (size_t)(ussdInfo.UssdStringLength), 0);
290
291                 int ret = tel_send_ss_ussd_request(&ussdInfo, &reqId);
292
293                 if (ret != TAPI_API_SUCCESS) {
294                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Platform exception");
295                 }
296
297         } catch (const PlatformException& ex) {
298                 LogError("Exception: " << ex.GetMessage());
299                 m_EventSendUSSDPtr->setExceptionCode(ExceptionCodes::PlatformException);
300                 EventRequestReceiver<EventSendUSSD>::ManualAnswer(m_EventSendUSSDPtr);
301                 m_lockSendUSSD = false;
302         }
303 }
304
305 void CallService::changedEvent(void *data)
306 {
307         TelTapiEvent_t *tapiEvent = (TelTapiEvent_t *)data;
308
309         switch (tapiEvent->EventClass) {
310         case TAPI_EVENT_CLASS_SS:
311         {
312                 switch(tapiEvent->EventType) {
313                 case TAPI_EVENT_SS_USSD_CNF:
314                 {
315                         TelSsUssdMsgIndInfo_t ussdRecord;
316                         std::string result("");
317
318                         if (tapiEvent->pData != NULL) {
319                                 memcpy(&ussdRecord, (TelSsUssdMsgIndInfo_t *)tapiEvent->pData, sizeof(TelSsUssdMsgInfo_t));
320                                 result.append(ussdRecord.UssdInfo.szUssdString, ussdRecord.UssdInfo.UssdStringLength);
321                                 m_EventSendUSSDPtr->setResult(result);
322                         } else {
323                                 m_EventSendUSSDPtr->setExceptionCode(ExceptionCodes::PlatformException);
324                         }
325
326                         deregisterCallEvent();
327                         EventRequestReceiver<EventSendUSSD>::ManualAnswer(m_EventSendUSSDPtr);
328                         m_lockSendUSSD = false;
329                 }
330                 break;
331                 default :
332                         break;
333                 }
334         }
335         break;
336         default :
337                 break;
338         }
339 }
340
341 }
342 }
343 }
344