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