Fix issue : fail to make thumbnail
[platform/core/messaging/msg-service.git] / plugin / sms_cdma_plugin / SmsCdmaPluginSetting.cpp
1 /*
2  * Copyright (c) 2015 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 <errno.h>
18 #include <pthread.h>
19
20 #include "MsgDebug.h"
21 #include "MsgCppTypes.h"
22 #include "MsgException.h"
23 #include "MsgGconfWrapper.h"
24 #include "MsgNotificationWrapper.h"
25
26 #include "MsgSoundPlayer.h"
27 #include "MsgContact.h"
28 #include "MsgUtilStorage.h"
29 #include "MsgTextConvert.h"
30
31 #include "SmsCdmaPluginParamCodec.h"
32 #include "SmsCdmaPluginCallback.h"
33 #include "SmsCdmaPluginEventHandler.h"
34 #include "SmsCdmaPluginMain.h"
35 #include "SmsCdmaPluginSetting.h"
36
37
38 extern "C"
39 {
40         #include <tapi_common.h>
41         #include <TelSms.h>
42         #include <TapiUtility.h>
43         #include <ITapiNetText.h>
44         #include <ITapiSim.h>
45         #include <ITapiModem.h>
46 }
47
48 extern struct tapi_handle *pTapiHandle;
49
50 /*==================================================================================================
51                                      IMPLEMENTATION OF SmsPluginSetting - Member Functions
52 ==================================================================================================*/
53 SmsPluginSetting* SmsPluginSetting::pInstance = NULL;
54
55
56 SmsPluginSetting::SmsPluginSetting()
57 {
58         /* Initialize member variables */
59         memset(&cbOpt, 0x00, sizeof(MSG_CBMSG_OPT_S));
60         memset(&meImei, 0x00, sizeof(meImei));
61
62         bTapiResult = false;
63         bUpdateVoicemailByMdn = false;
64 }
65
66
67 SmsPluginSetting::~SmsPluginSetting()
68 {
69 }
70
71
72 SmsPluginSetting* SmsPluginSetting::instance()
73 {
74         if (!pInstance)
75                 pInstance = new SmsPluginSetting();
76
77         return pInstance;
78 }
79
80
81 void* SmsPluginSetting::initSimInfo(void *data)
82 {
83         MSG_BEGIN();
84
85         int tapiRet = TAPI_API_SUCCESS;
86
87         /* Get IMSI */
88         char imsi[17];
89         memset(imsi, 0x00, sizeof(imsi));
90
91         /* Get IMSI */
92         TelSimImsiInfo_t imsiInfo;
93         memset(&imsiInfo, 0x00, sizeof(TelSimImsiInfo_t));
94
95         tapiRet = tel_get_sim_imsi(pTapiHandle, &imsiInfo);
96
97         if (tapiRet == TAPI_API_SUCCESS) {
98                 MSG_SEC_DEBUG("tel_get_sim_imsi() Success - MCC [%s], MNC [%s], MSIN [%s]", imsiInfo.szMcc, imsiInfo.szMnc, imsiInfo.szMsin);
99                 snprintf(imsi, sizeof(imsi), "%03d%03d%s", atoi(imsiInfo.szMcc), atoi(imsiInfo.szMnc), imsiInfo.szMsin);
100                 MSG_SEC_DEBUG("IMSI [%s]", imsi);
101         } else {
102                 MSG_DEBUG("tel_get_sim_imsi() Error![%d]", tapiRet);
103         }
104
105         MsgSettingSetString(MSG_SIM_IMSI, imsi);
106
107         instance()->updateSimStatus();
108
109         MSG_END();
110         return NULL;
111 }
112
113
114 void SmsPluginSetting::updateSimStatus()
115 {
116         MSG_BEGIN();
117
118         if (!pTapiHandle) {
119                 MSG_DEBUG("pTapiHandle is NULL.");
120                 return;
121         }
122
123         int status = 0;
124         int tapiRet = TAPI_API_SUCCESS;
125
126         tapiRet = tel_check_sms_device_status(pTapiHandle, &status);
127
128         if (tapiRet != TAPI_API_SUCCESS) {
129                 MSG_DEBUG("tel_check_sms_device_status() Error! [%d], Status [%d]", tapiRet, status);
130                 return;
131         }
132
133         if (status == 1 || status == 2) {
134                 MSG_DEBUG("Device Is Ready, status = %d", status);
135                 SmsPluginEventHandler::instance()->setNeedInitConfig(false);
136         } else if (status == 0) {
137                 MSG_DEBUG("Device Is Not Ready.. Waiting For Ready Callback");
138
139                 if (SmsPluginEventHandler::instance()->getDeviceStatus() == true) {
140                         MSG_DEBUG("Device Is Ready");
141                 } else {
142                         MSG_DEBUG("Device Is Not Ready.");
143                         return;
144                 }
145         }
146
147         /* init config data. */
148         initConfigData();
149
150         MSG_END();
151
152         return;
153 }
154
155
156 void SmsPluginSetting::setSimChangeStatus()
157 {
158         MSG_BEGIN();
159
160         pthread_t thd;
161
162         if (pthread_create(&thd, NULL, &initSimInfo, NULL) < 0) {
163                 MSG_DEBUG("pthread_create() error");
164         }
165
166         pthread_detach(thd);
167
168         MSG_END();
169 }
170
171
172 void SmsPluginSetting::initConfigData()
173 {
174         MSG_BEGIN();
175
176         msg_error_t     err = MSG_SUCCESS;
177         char keyName[MAX_VCONFKEY_NAME_LEN];
178         int sim_idx = 1;
179
180         /*==================== CB configuration ====================*/
181         /*
182         if (simStatus != MSG_SIM_STATUS_NOT_FOUND) {
183                 MSG_DEBUG("simStatus == [%d]", simStatus);
184         */
185                 MSG_CBMSG_OPT_S cbMsgOpt = {0, };
186
187                 if (getCbConfig(&cbMsgOpt) == true) {
188                         err = addCbOpt(&cbMsgOpt);
189
190                         if (err == MSG_SUCCESS) {
191                                 MSG_DEBUG("########  Add CB Option Success !!! #######");
192                                 MSG_SETTING_S cbSetting;
193                                 cbSetting.type = MSG_CBMSG_OPT;
194                                 getCbOpt(&cbSetting);
195                                 setCbConfig(&(cbSetting.option.cbMsgOpt));
196                         } else {
197                                 MSG_DEBUG("########  Add CB Option Fail !!! return : %d #######", err);
198                         }
199                 } else {
200                         MSG_DEBUG("########  getCbConfig Fail !!! #######");
201                 }
202
203                 /*==================== MSISDN update ====================*/
204                 if (getMsisdnInfo() == true) {
205                         MSG_DEBUG("########  getMsisdnInfo Success !!! #######");
206                 } else {
207                         MSG_DEBUG("########  getMsisdnInfo Fail !!! #######");
208                 }
209
210                 /*==================== Default Voice mail Setting ====================*/
211                 MSG_DEBUG("Voicemail Default Number is NULL");
212                 memset(keyName, 0x00, sizeof(keyName));
213                 snprintf(keyName, sizeof(keyName), "%s/%d", VOICEMAIL_NUMBER, sim_idx);
214                 if (MsgSettingSetString(keyName, VOICEMAIL_DEFAULT_NUMBER) != MSG_SUCCESS)
215                         MSG_DEBUG("Error to set config data [%s]", keyName);
216
217                 memset(keyName, 0x00, sizeof(keyName));
218                 snprintf(keyName, sizeof(keyName), "%s/%d", VOICEMAIL_NUMBER, sim_idx);
219
220                 memset(keyName, 0x00, sizeof(keyName));
221                 snprintf(keyName, sizeof(keyName), "%s/%d", VOICEMAIL_ALPHA_ID, sim_idx);
222                 if (MsgSettingSetString(keyName, VOICEMAIL_DEFAULT_ALPHA_ID) != MSG_SUCCESS)
223                         MSG_DEBUG("Error to set config data [%s]", keyName);
224
225         MSG_END();
226 }
227
228
229 void SmsPluginSetting::SimRefreshCb()
230 {
231         pthread_t thd;
232
233         if (pthread_create(&thd, NULL, &init_config_data, NULL) < 0) {
234                 MSG_DEBUG("pthread_create() error");
235         }
236
237         pthread_detach(thd);
238 }
239
240
241 void* SmsPluginSetting::init_config_data(void *data)
242 {
243         instance()->initConfigData();
244         return NULL;
245 }
246
247
248 void SmsPluginSetting::setConfigData(const MSG_SETTING_S *pSetting)
249 {
250         MSG_DEBUG("Setting Type : [%d]", pSetting->type);
251
252         switch (pSetting->type) {
253 #if 0
254         case MSG_SMS_SENDOPT :
255                 setNetworkMode(&pSetting->option.smsSendOpt);
256                 break;
257         case MSG_SMSC_LIST :
258                 setParamList(&pSetting->option.smscList);
259                 break;
260 #endif
261         case MSG_VOICEMAIL_OPT:
262                 setVoiceMailInfo(&pSetting->option.voiceMailOpt);
263                 break;
264
265         case MSG_CBMSG_OPT :
266                 setCbConfig(&pSetting->option.cbMsgOpt);
267                 break;
268         default :
269                 THROW(MsgException::SMS_PLG_ERROR, "The Setting type is not supported. [%d]", pSetting->type);
270                 break;
271         }
272 }
273
274
275 void SmsPluginSetting::getConfigData(MSG_SETTING_S *pSetting)
276 {
277         MSG_DEBUG("Setting Type : [%d]", pSetting->type);
278
279         switch (pSetting->type) {
280 #if 0
281         case MSG_SMSC_LIST :
282                 getParamList(&pSetting->option.smscList);
283         break;
284 #endif
285         case MSG_CBMSG_OPT :
286                 getCbConfig(&pSetting->option.cbMsgOpt);
287         break;
288
289         default :
290                 THROW(MsgException::SMS_PLG_ERROR, "The Setting type is not supported. [%d]", pSetting->type);
291         break;
292         }
293 }
294
295
296 msg_error_t SmsPluginSetting::addCbOpt(MSG_CBMSG_OPT_S *pCbOpt)
297 {
298         msg_error_t err = MSG_SUCCESS;
299
300         /* MSG_DEBUG("Receive [%d], Max SIM Count [%d]", pCbOpt->bReceive, pCbOpt->maxSimCnt); */
301
302         MSG_DEBUG("Receive [%d], Channel Count [%d]", pCbOpt->bReceive, pCbOpt->channelData.channelCnt);
303
304         for (int i = 0; i < pCbOpt->channelData.channelCnt; i++) {
305                 MSG_DEBUG("Channel Category [%d], Channel Language [%d]", pCbOpt->channelData.channelInfo[i].ctg, pCbOpt->channelData.channelInfo[i].lang);
306         }
307
308 #if 0
309         /* Set Setting Data into Vconf */
310         if (MsgSettingSetBool(CB_RECEIVE, pCbOpt->bReceive) != MSG_SUCCESS) {
311                 MSG_DEBUG("Error to set config data [%s]", CB_RECEIVE);
312                 return MSG_ERR_SET_SETTING;
313         }
314 #endif
315
316 #if 0
317         if (MsgSettingSetInt(CB_MAX_SIM_COUNT, pCbOpt->maxSimCnt) != MSG_SUCCESS) {
318                 MSG_DEBUG("Error to set config data [%s]", CB_MAX_SIM_COUNT);
319                 return MSG_ERR_SET_SETTING;
320         }
321 #endif
322
323 #if 0
324         MsgDbHandler dbHandle;
325         err = MsgStoAddCBChannelInfo(&dbHandle, &pCbOpt->channelData);
326         if (err != MSG_SUCCESS) {
327                 MSG_DEBUG("MsgStoGetCBChannelInfo is failed [%d]", err);
328                 return MSG_ERR_SET_SETTING;
329         }
330 #endif
331
332         return err;
333 }
334
335
336 void SmsPluginSetting::getCbOpt(MSG_SETTING_S *pSetting)
337 {
338         msg_error_t err = MSG_SUCCESS;
339         MsgDbHandler dbHandle;
340
341         memset(&(pSetting->option.cbMsgOpt), 0x00, sizeof(MSG_CBMSG_OPT_S));
342
343         if (MsgSettingGetBool(CB_RECEIVE, &pSetting->option.cbMsgOpt.bReceive) != MSG_SUCCESS)
344                 MSG_INFO("MsgSettingGetBool() is failed");
345
346         err = MsgStoGetCBChannelInfo(&dbHandle, &pSetting->option.cbMsgOpt.channelData);
347         MSG_DEBUG("MsgStoAddCBChannelInfo : err=[%d]", err);
348
349 #if 0
350         char keyName[128];
351
352         for (int i = MSG_CBLANG_TYPE_ALL; i < MSG_CBLANG_TYPE_MAX; i++) {
353                 memset(keyName, 0x00, sizeof(keyName));
354                 snprintf(keyName, sizeof(keyName), "%s/%d", CB_LANGUAGE, i);
355
356                 if (MsgSettingGetBool(keyName, &pSetting->option.cbMsgOpt.bLanguage[i]) != MSG_SUCCESS)
357                         MSG_INFO("MsgSettingGetBool() is failed");
358         }
359 #endif
360 }
361
362
363 void SmsPluginSetting::setVoiceMailInfo(const MSG_VOICEMAIL_OPT_S *pVoiceOpt)
364 {
365         bUpdateVoicemailByMdn = false;
366
367         return;
368 }
369
370
371 bool SmsPluginSetting::setCbConfig(const MSG_CBMSG_OPT_S *pCbOpt)
372 {
373         int ret = TAPI_API_SUCCESS;
374
375 #if 1
376         TelSmsCbConfig_t cbConfig = {};
377
378         cbConfig.CBEnabled = (int)pCbOpt->bReceive;
379         cbConfig.Net3gppType = TAPI_NETTEXT_NETTYPE_3GPP2;
380         /* cbConfig.MsgIdMaxCount = pCbOpt->maxSimCnt; */
381         cbConfig.MsgIdRangeCount = pCbOpt->channelData.channelCnt;
382
383         for (int i = 0; i < cbConfig.MsgIdRangeCount; i++) {
384                 cbConfig.MsgIDs[i].Net3gpp2.Selected = (unsigned short)pCbOpt->channelData.channelInfo[i].bActivate;
385                 cbConfig.MsgIDs[i].Net3gpp2.CBCategory = (unsigned short)pCbOpt->channelData.channelInfo[i].ctg;
386                 cbConfig.MsgIDs[i].Net3gpp2.CBLanguage = (unsigned short)pCbOpt->channelData.channelInfo[i].lang;
387
388                 MSG_DEBUG("Category: %d, Language: %d", cbConfig.MsgIDs[i].Net3gpp2.CBCategory, cbConfig.MsgIDs[i].Net3gpp2.CBLanguage);
389         }
390         MSG_DEBUG("CBEnabled: %d, range_count: %d", cbConfig.CBEnabled, cbConfig.MsgIdRangeCount);
391
392         ret = tel_set_sms_cb_config(pTapiHandle, &cbConfig, TapiEventSetConfigData, NULL);
393
394         if (ret == TAPI_API_SUCCESS) {
395                 MSG_DEBUG("######## tel_set_sms_cb_config() Success !!! #######");
396         } else {
397                 MSG_DEBUG("######## tel_set_sms_cb_config() Fail !!! return : %d #######", ret);
398                 return false;
399         }
400
401 #if 0
402         if (getResultFromSim() == true) {
403                 MSG_DEBUG("######## Set Cb Config was Successful !!! #######");
404         } else {
405                 MSG_DEBUG("######## Set Cb Config was Failed !!! #######");
406                 return false;
407         }
408 #endif
409 #endif
410         return true;
411 }
412
413
414 bool SmsPluginSetting::getCbConfig(MSG_CBMSG_OPT_S *pCbOpt)
415 {
416         int ret = TAPI_API_SUCCESS;
417 #if 1
418         ret = tel_get_sms_cb_config(pTapiHandle, TapiEventGetCBConfig, NULL);
419
420         if (ret == TAPI_API_SUCCESS) {
421                 MSG_DEBUG("######## tel_get_sms_cb_config() Success !!! #######");
422         } else {
423                 MSG_DEBUG("######## tel_get_sms_cb_config() Fail !!! return : %d #######", ret);
424                 return false;
425         }
426
427         if (getCbConfigEvent(pCbOpt) == true) {
428                 MSG_DEBUG("######## Get Cb Config was Successful !!! #######");
429         } else {
430                 MSG_DEBUG("######## Get Cb Config was Failed !!! #######");
431                 return false;
432         }
433 #endif
434         return true;
435 }
436
437
438 void SmsPluginSetting::getMeImei(char *pImei)
439 {
440 #if 0
441         int ret = TAPI_API_SUCCESS;
442         ret = tel_get_misc_me_imei(pTapiHandle, TapiEventGetMeImei, NULL);
443
444         if (ret == TAPI_API_SUCCESS) {
445                 MSG_SEC_DEBUG("######## tel_get_misc_me_imei() Success !!! #######");
446
447                 if (getResultImei(pImei) == true) {
448                         MSG_SEC_DEBUG("######## Get ME IMEI was Successful !!! #######");
449                 } else {
450                         MSG_SEC_DEBUG("######## Get ME IMEI was Failed !!! #######");
451                 }
452         } else {
453                 MSG_SEC_DEBUG("######## tel_get_misc_me_imei() Fail !!! return : %d #######", ret);
454         }
455 #endif
456 }
457
458 bool SmsPluginSetting::getUpdateVoicemailByMdn()
459 {
460         return bUpdateVoicemailByMdn;
461 }
462
463 void SmsPluginSetting::setCbConfigEvent(const MSG_CBMSG_OPT_S *pCbOpt, bool bSuccess)
464 {
465         mx.lock();
466
467         bTapiResult = bSuccess;
468
469         memset(&cbOpt, 0x00, sizeof(MSG_CBMSG_OPT_S));
470
471         if (bTapiResult == true) {
472                 MSG_DEBUG("Success to get cb config data");
473
474                 memcpy(&cbOpt, pCbOpt, sizeof(MSG_CBMSG_OPT_S));
475         }
476
477         cv.signal();
478
479         mx.unlock();
480 }
481
482
483 bool SmsPluginSetting::getCbConfigEvent(MSG_CBMSG_OPT_S *pCbOpt)
484 {
485         int ret = 0;
486
487         mx.lock();
488
489         bTapiResult = false;
490         ret = cv.timedwait(mx.pMsgMutex(), 25);
491
492         mx.unlock();
493
494         if (ret == ETIMEDOUT) {
495                 MSG_DEBUG("WARNING: TAPI callback TIME-OUT");
496                 return false;
497         }
498
499         memset(pCbOpt, 0x00, sizeof(MSG_CBMSG_OPT_S));
500
501         if (bTapiResult == true) {
502                 memcpy(pCbOpt, &cbOpt, sizeof(MSG_CBMSG_OPT_S));
503         }
504
505         return bTapiResult;
506 }
507
508
509 void SmsPluginSetting::setResultImei(bool bResult, char *pImei)
510 {
511         mx.lock();
512
513         bTapiResult = bResult;
514
515         memset(&meImei, 0x00, sizeof(meImei));
516
517         if (bTapiResult == true && pImei) {
518                 snprintf(meImei, sizeof(meImei), "%s", pImei);
519         }
520
521         cv.signal();
522
523         mx.unlock();
524 }
525
526
527 bool SmsPluginSetting::getResultImei(char *pImei)
528 {
529         int ret = 0;
530
531         mx.lock();
532
533         ret = cv.timedwait(mx.pMsgMutex(), 25);
534
535         mx.unlock();
536
537         if (ret == ETIMEDOUT) {
538                 MSG_DEBUG("WARNING: TAPI callback TIME-OUT");
539                 return false;
540         }
541
542         if (bTapiResult == true && pImei) {
543                 snprintf(pImei, sizeof(meImei), "%s", meImei);
544         }
545
546         return bTapiResult;
547 }
548
549
550 void SmsPluginSetting::setResultFromEvent(bool bResult)
551 {
552         mx.lock();
553
554         bTapiResult = bResult;
555
556         cv.signal();
557
558         mx.unlock();
559 }
560
561
562 void SmsPluginSetting::setMwiInfo(MSG_SUB_TYPE_T type, int count)
563 {
564         MSG_DEBUG("SET MWI INFO, type=[%d]", type);
565         MSG_DEBUG("SET MWI INFO, count=[%d]", count);
566
567         if (MsgSettingSetInt(VOICEMAIL_COUNT, count) != MSG_SUCCESS)
568                 MSG_DEBUG("MsgSettingSetInt is failed!!");
569         /*
570         if (count == 0) {
571                 MsgStoClearUniquenessTable();
572         }
573
574         if (count <= 0) {
575                 if (type == MSG_MWI_VOICE_SMS)
576                         MsgCleanAndResetNotification(MSG_NOTI_TYPE_VOICE_1);
577                 else if (type == MSG_MWI_VOICE2_SMS)
578                         MsgCleanAndResetNotification(MSG_NOTI_TYPE_VOICE_2);
579         }
580
581         if (bMbdnEnable == false) {
582                 MSG_DEBUG("MBDN service is disable.");
583                 return;
584         }
585         */
586         return;
587 }
588
589
590 bool SmsPluginSetting::getMsisdnInfo(void)
591 {
592         int ret = TAPI_API_SUCCESS;
593
594         ret = tel_get_sim_msisdn(pTapiHandle, TapiEventGetMsisdnInfo, NULL);
595
596         if (ret == TAPI_API_SUCCESS) {
597                 MSG_DEBUG("######## tel_get_sim_msisdn() Success !!! #######");
598         } else {
599                 MSG_DEBUG("######## tel_get_sim_msisdn() Fail !!! return : %d #######", ret);
600                 return false;
601         }
602
603         if (getResultFromSim() == true) {
604                 MSG_DEBUG("######## Get Sim msisdn was Successful !!! #######");
605         } else {
606                 MSG_DEBUG("######## Get Sim msisdn was Failed !!! #######");
607                 return false;
608         }
609
610         return true;
611 }
612
613
614 void SmsPluginSetting::setResultFromSim(bool bResult)
615 {
616         mx.lock();
617
618         bTapiResult = bResult;
619
620         cv.signal();
621
622         mx.unlock();
623 }
624
625
626 bool SmsPluginSetting::getResultFromSim()
627 {
628         int ret = 0;
629
630         mx.lock();
631
632         ret = cv.timedwait(mx.pMsgMutex(), 25);
633
634         mx.unlock();
635
636         if (ret == ETIMEDOUT) {
637                 MSG_DEBUG("WARNING: TAPI callback TIME-OUT");
638                 return false;
639         }
640
641         return bTapiResult;
642 }
643