Modify flora license version.
[platform/core/messaging/msg-service.git] / plugin / mms_plugin / MmsPluginUserAgent.cpp
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.1 (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/license/
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 <sys/utsname.h>
18
19 #include "MsgException.h"
20 #include "MsgUtilFile.h"
21 #include "MsgGconfWrapper.h"
22
23
24 #include "MmsPluginDebug.h"
25 #include "MmsPluginUserAgent.h"
26 #include "MmsPluginHttp.h"
27 #include "MmsPluginConnManWrapper.h"
28 #include "MmsPluginEventHandler.h"
29 #include "MmsPluginInternal.h"
30 #include "MmsPluginCodec.h"
31 #include "MmsPluginDrm.h"
32 #include "MmsPluginStorage.h"
33
34 extern MmsHeader mmsHeader;
35
36 void PRINT_PDU_TYPE(MMS_PDU_TYPE_T pduType)
37 {
38         switch (pduType) {
39         case eMMS_SEND_REQ:
40                 MSG_DEBUG("[SEND_REQ]");
41                 break;
42         case eMMS_SEND_CONF:
43                 MSG_DEBUG("[MMS_SEND_CONF]");
44                 break;
45         case eMMS_RETRIEVE_AUTO:
46                 MSG_DEBUG("[MMS_RETRIEVE_AUTO]");
47                 break;
48         case eMMS_RETRIEVE_MANUAL:
49                 MSG_DEBUG("[MMS_RETRIEVE_MANUAL]");
50                 break;
51         case eMMS_RETRIEVE_AUTO_CONF:
52                 MSG_DEBUG("[MMS_RETRIEVE_AUTO_CONF]");
53                 break;
54         case eMMS_RETRIEVE_MANUAL_CONF:
55                 MSG_DEBUG("[MMS_RETRIEVE_MANUAL_CONF]");
56                 break;
57         case eMMS_DELIVERY_IND:
58                 MSG_DEBUG("[MMS_DELIVERY_IND]");
59                 break;
60         case eMMS_NOTIFICATION_IND:
61                 MSG_DEBUG("[MMS_NOTIFICATION_IND]");
62                 break;
63         case eMMS_NOTIFYRESP_IND:
64                 MSG_DEBUG("[MMS_NOTIFYRESP_IND]");
65                 break;
66         case eMMS_ACKNOWLEDGE_IND:
67                 MSG_DEBUG("[MMS_ACKNOWLEDGE_IND]");
68                 break;
69         case eMMS_FORWARD_REQ:
70                 MSG_DEBUG("[MMS_FORWARD_REQ]");
71                 break;
72         case eMMS_FORWARD_CONF:
73                 MSG_DEBUG("[MMS_FORWARD_CONF]");
74                 break;
75         case eMMS_CANCEL_REQ:
76                 MSG_DEBUG("[MMS_CANCEL_REQ]");
77                 break;
78         case eMMS_CANCEL_CONF:
79                 MSG_DEBUG("[MMS_CANCEL_CONF]");
80                 break;
81         case eMMS_DELETE_REQ:
82                 MSG_DEBUG("[MMS_DELETE_REQ]");
83                 break;
84         case eMMS_DELETE_CONF:
85                 MSG_DEBUG("[MMS_DELETE_CONF]");
86                 break;
87         case eMMS_READREC_IND:
88                 MSG_DEBUG("[MMS_READREC_IND]");
89                 break;
90         case eMMS_READORIG_IND:
91                 MSG_DEBUG("[MMS_READORIG_IND]");
92                 break;
93         case eMMS_MBOX_STORE_REQ:
94                 MSG_DEBUG("[MMS_MBOX_STORE_REQ]");
95                 break;
96         case eMMS_MBOX_STORE_CONF:
97                 MSG_DEBUG("[MMS_MBOX_STORE_CONF]");
98                 break;
99         case eMMS_MBOX_VIEW_REQ:
100                 MSG_DEBUG("[MMS_MBOX_VIEW_REQ]");
101                 break;
102         case eMMS_MBOX_VIEW_CONF:
103                 MSG_DEBUG("[MMS_MBOX_VIEW_CONF]");
104                 break;
105         case eMMS_MBOX_UPLOAD_REQ:
106                 MSG_DEBUG("[MMS_MBOX_UPLOAD_REQ]");
107                 break;
108         case eMMS_MBOX_UPLOAD_CONF:
109                 MSG_DEBUG("[MMS_MBOX_UPLOAD_CONF]");
110                 break;
111         case eMMS_MBOX_DELETE_REQ:
112                 MSG_DEBUG("[MMS_MBOX_DELETE_REQ]");
113                 break;
114         case eMMS_MBOX_DELETE_CONF:
115                 MSG_DEBUG("[MMS_MBOX_DELETE_CONF]");
116                 break;
117         default:
118                 MSG_DEBUG("[Unknown PDU Type]");
119                 break;
120         }
121 }
122
123
124 void PRINT_QUEUE_ENTITY(mmsTranQEntity *entity)
125 {
126         MSG_DEBUG("Entity: msgId: %d", entity->msgId);
127         MSG_DEBUG("Entity: completed: %d", entity->isCompleted);
128         MSG_DEBUG("Entity: eMmsPduType: %d", entity->eMmsPduType);
129         MSG_DEBUG("Entity: eHttpCmdType: %d", entity->eHttpCmdType);
130         MSG_DEBUG("Entity: GetLen: %d", entity->getDataLen);
131         MSG_DEBUG("Entity: GetData: (%s)", entity->pGetData);
132         MSG_DEBUG("Entity: postLen: %d", entity->postDataLen);
133         MSG_DEBUG("Entity: pPostData: (%s)", entity->pPostData);
134 }
135
136 void updatePduType(mmsTranQEntity *qEntity)
137 {
138         switch(qEntity->eMmsPduType) {
139         case eMMS_SEND_REQ:
140                 qEntity->eMmsPduType = eMMS_SEND_CONF;
141                 break;
142         case eMMS_RETRIEVE_AUTO:
143                 qEntity->eMmsPduType = eMMS_RETRIEVE_AUTO_CONF;
144                 break;
145         case eMMS_RETRIEVE_MANUAL:
146                 qEntity->eMmsPduType = eMMS_RETRIEVE_MANUAL_CONF;
147                 break;
148         case eMMS_RETRIEVE_AUTO_CONF:
149                 qEntity->eMmsPduType = eMMS_NOTIFYRESP_IND;
150                 break;
151         case eMMS_READREC_IND:
152                 qEntity->eMmsPduType = eMMS_SEND_CONF;
153                 break;
154         case eMMS_READREPORT_REQ:
155                 qEntity->eMmsPduType = eMMS_READREPORT_CONF;
156                 break;
157         case eMMS_RETRIEVE_MANUAL_CONF:
158                 qEntity->eMmsPduType = eMMS_ACKNOWLEDGE_IND;
159                 break;
160         case eMMS_DELETE_REQ:
161                 qEntity->eMmsPduType = eMMS_DELETE_CONF;
162                 break;
163         case eMMS_FORWARD_REQ:
164                 qEntity->eMmsPduType = eMMS_FORWARD_CONF;
165                 break;
166         case eMMS_MBOX_STORE_REQ:
167                 qEntity->eMmsPduType = eMMS_MBOX_STORE_CONF;
168                 break;
169         case eMMS_MBOX_VIEW_REQ:
170                 qEntity->eMmsPduType = eMMS_MBOX_VIEW_CONF;
171                 break;
172         case eMMS_MBOX_UPLOAD_REQ:
173                 qEntity->eMmsPduType = eMMS_MBOX_UPLOAD_CONF;
174                 break;
175         case eMMS_MBOX_DELETE_REQ:
176                 qEntity->eMmsPduType = eMMS_MBOX_DELETE_CONF;
177                 break;
178         default:
179                 break;
180         }
181
182         MSG_DEBUG("Update PDU Type:");
183         PRINT_PDU_TYPE(qEntity->eMmsPduType);
184 }
185
186 MmsPluginUaManager *MmsPluginUaManager::pInstance = NULL;
187
188 MmsPluginUaManager::MmsPluginUaManager()
189 {
190         running = false;
191         mmsTranQ.clear();
192 }
193
194 MmsPluginUaManager::~MmsPluginUaManager()
195 {
196         if (pInstance) {
197                 delete pInstance;
198                 pInstance = NULL;
199         }
200 }
201
202 MmsPluginUaManager *MmsPluginUaManager::instance()
203 {
204         if (!pInstance)
205                 pInstance = new MmsPluginUaManager();
206
207         return pInstance;
208 }
209
210 void MmsPluginUaManager::start()
211 {
212 //      bool bStart = true;
213
214         MutexLocker lock(mx);
215
216         if (!running) {
217
218                 running = true;
219                 MsgThread::start();
220         }
221 }
222
223 MMS_NET_ERROR_T MmsPluginUaManager::submitHandler(mmsTranQEntity *qEntity)
224 {
225         MMS_NET_ERROR_T ret = eMMS_UNKNOWN;
226         int retryCount = 0;
227
228         MSG_DEBUG("request Submit:");
229         PRINT_PDU_TYPE(qEntity->eMmsPduType);
230         PRINT_QUEUE_ENTITY(qEntity);
231
232         MmsPluginHttpAgent*     httpAgent = MmsPluginHttpAgent::instance();
233
234         while (retryCount < RETRY_MAX) {
235                 ret = httpAgent->cmdRequest(qEntity->eHttpCmdType);
236
237                 // Process result
238                 if (ret == eMMS_HTTP_SENT_SUCCESS) {
239                         MSG_DEBUG("Submit request sent");
240                         break;
241                 } else if (ret == eMMS_HTTP_ERROR_NETWORK) {
242                         retryCount++;
243                         MSG_DEBUG("HTTP sent timeout and try again: %d", retryCount);
244                         continue;
245                 } else {
246                         MSG_DEBUG("Unexpected Error %d", ret);
247                         break;
248                 }
249         }
250
251         return ret;
252 }
253
254 MMS_NET_ERROR_T MmsPluginUaManager::waitingConf(mmsTranQEntity *qEntity)
255 {
256         MMS_NET_ERROR_T ret = eMMS_HTTP_ERROR_UNKNOWN;
257         MmsPluginHttpAgent *pHttpAgent = MmsPluginHttpAgent::instance();
258         MMS_PLUGIN_HTTP_CONTEXT_S *pMmsPldCd = NULL;
259
260         pMmsPldCd = pHttpAgent->getMmsPldCd();
261
262         if (qEntity->pGetData) {
263                 free(qEntity->pGetData);
264                 qEntity->pGetData = NULL;
265         }
266         qEntity->getDataLen = pMmsPldCd->bufOffset;
267         qEntity->pGetData = (char *)calloc(1, pMmsPldCd->bufOffset + 1);
268
269         memcpy(qEntity->pGetData, pMmsPldCd->final_content_buf, pMmsPldCd->bufOffset);
270         free(pMmsPldCd->final_content_buf);
271         pMmsPldCd->final_content_buf = NULL;
272         pMmsPldCd->bufOffset = 0;
273
274         MSG_DEBUG("dataLen:%d  pData:(%s)", qEntity->getDataLen, qEntity->pGetData);
275
276         ret = eMMS_HTTP_CONF_SUCCESS;
277
278         return ret;
279 }
280
281 void MmsPluginUaManager::run()
282 {
283         MSG_BEGIN();
284
285         MmsPluginCmAgent *cmAgent = MmsPluginCmAgent::instance();
286         MmsPluginHttpAgent *httpAgent = MmsPluginHttpAgent::instance();
287
288         int trId;
289 //      CURL *session = NULL;
290
291         int msgId;
292
293         while (1) {
294                 if (mmsTranQ.empty()) {
295                         lock();
296                         wait();
297                         unlock();
298                 }
299
300
301                 { // Check is it emulator or not.
302                         struct utsname buf;
303                         int ret = uname(&buf);
304
305                         if (ret == 0) {
306                                 MSG_DEBUG("System runs on [%s].", buf.machine);
307                                 //if (strncmp(buf.machine, "i686", 4) == 0) {
308                                 if(strcasestr(buf.machine, "emulated")) {
309                                         MSG_DEBUG("Running on Emulator mode.");
310
311                                         int mmsResult = MsgSettingGetInt(VCONFKEY_TELEPHONY_MMS_SENT_STATUS);
312
313                                         MSG_DEBUG("MMS result has to be [%d]", mmsResult);
314
315                                         while (!mmsTranQ.empty()) {
316                                                 MSG_DEBUG("###### mmsTranQ.size [%d]", mmsTranQ.size());
317
318                                                 mmsTranQEntity reqEntity;
319                                                 memset(&reqEntity, 0, sizeof(mmsTranQEntity));
320
321                                                 mmsTranQ.front(&reqEntity);
322
323                                                 if (mmsResult > 0) {
324                                                         // For MMS send fail.
325                                                         MmsPluginEventHandler::instance()->handleMmsError(&reqEntity);
326                                                         mmsTranQ.pop_front();
327                                                 } else {
328                                                         // For MMS send success.
329                                                         MSG_DEBUG("conf received successfully");
330
331                                                         reqEntity.eMmsPduType = eMMS_SEND_CONF;
332
333                                                         try {
334                                                                 MmsPluginEventHandler::instance()->handleMmsReceivedData(&reqEntity, NULL);
335                                                         } catch (MsgException& e) {
336                                                                 MSG_FATAL("%s", e.what());
337                                                                 break;
338                                                         } catch (exception& e) {
339                                                                 MSG_FATAL("%s", e.what());
340                                                                 break;
341                                                         }
342
343                                                         mmsTranQ.pop_front();
344                                                 }
345                                         }
346
347                                         mmsTranQ.clear();
348                                         MutexLocker locker(mx);
349                                         running = false;
350
351                                         return;
352                                 }
353                         }
354                 }
355
356
357                 // Request CM Open
358                 if (!(cmAgent->open())) {
359                         MSG_DEBUG("Cm Open Failed");
360
361                         // delete all request from reqQEntities
362                         goto CLEANUP;
363                 }
364
365                 httpAgent->SetMMSProfile();
366
367                 while (!mmsTranQ.empty()) {
368
369                         MSG_DEBUG("###### mmsTranQ.size [%d]", mmsTranQ.size());
370
371                         mmsTranQEntity reqEntity;
372                         memset(&reqEntity, 0, sizeof(mmsTranQEntity));
373
374                         mmsTranQ.front(&reqEntity);
375
376                         reqEntity.isCompleted = false;
377
378                         PRINT_QUEUE_ENTITY(&reqEntity);
379
380                         if (reqEntity.eMmsPduType == eMMS_RETRIEVE_AUTO) {
381                                 msgId = reqEntity.msgId;
382                                 MmsPluginStorage::instance()->updateNetStatus(msgId, MSG_NETWORK_RETRIEVING);
383                         }
384
385                         if (httpAgent->cmdRequest(eHTTP_CMD_INIT_SESSION) == eMMS_HTTP_SESSION_OPEN_FAILED) {
386                                 MSG_DEBUG("HTTP session open failed");
387                                 // cm close
388                                 cmAgent->close();
389                                 // delete all request from reqQEntities
390                                 goto CLEANUP;
391                         }
392
393                         // MMS Transaction
394                         MSG_DEBUG("\n\n ===================  MMS Transaction Start ========================");
395
396                         do {
397                                 httpAgent->setSession(&reqEntity);
398
399                                 if (submitHandler(&reqEntity) != eMMS_HTTP_SENT_SUCCESS) {
400                                         MSG_DEBUG("Transaction Error: submit failed");
401
402                                         MmsPluginEventHandler::instance()->handleMmsError(&reqEntity);
403                                         mmsTranQ.pop_front();
404                                         //try to next mmsTranQ
405                                         break;
406                                 }
407
408                                 MSG_DEBUG("submitHandler(&reqEntity) success.");
409                                 trId = httpAgent->getHttpConfigData()->transactionId;
410
411                                 MSG_DEBUG("#### MMS PDU TYPE = %d ####", reqEntity.eMmsPduType);
412
413                                 if (reqEntity.eMmsPduType == eMMS_NOTIFYRESP_IND ||
414                                         reqEntity.eMmsPduType == eMMS_ACKNOWLEDGE_IND ||
415                                         reqEntity.eMmsPduType == eMMS_READREC_IND ||
416                                         reqEntity.eMmsPduType == eMMS_CANCEL_CONF) {
417                                         reqEntity.isCompleted = true;
418
419                                         mmsTranQ.pop_front();
420
421                                         MSG_DEBUG("Transaction Completed");
422                                         break;
423                                 } else {
424                                         // change MmsPduType from XXX.req to XXX.conf for waiting
425                                         MSG_DEBUG("Update Pdutype");
426                                         updatePduType(&reqEntity);
427                                         MSG_DEBUG("Waiting Conf");
428                                 }
429
430                                 mmsTranQ.pop_front();
431
432                                 //////// Waiting Conf //////////////////////
433                                 MMS_NET_ERROR_T networkErr;
434
435                                 if ((networkErr = waitingConf(&reqEntity)) == eMMS_HTTP_CONF_SUCCESS) {
436                                         bool bReportAllowed;
437                                         char retrievedFilePath[MAX_FULL_PATH_SIZE+1] = {0,};
438
439                                         // process Http data
440                                         try {
441                                                 if (processReceivedData(reqEntity.msgId, reqEntity.pGetData, reqEntity.getDataLen, retrievedFilePath) == false) {
442                                                         MmsPluginEventHandler::instance()->handleMmsError(&reqEntity);
443                                                         break;
444                                                 }
445                                         } catch (MsgException& e) {
446                                                 MSG_FATAL("%s", e.what());
447                                                 MmsPluginEventHandler::instance()->handleMmsError(&reqEntity);
448                                                 break;
449
450                                         } catch (exception& e) {
451                                                 MSG_FATAL("%s", e.what());
452                                                 MmsPluginEventHandler::instance()->handleMmsError(&reqEntity);
453                                                 break;
454                                         }
455
456                                         if (reqEntity.eMmsPduType != (MMS_PDU_TYPE_T)mmsHeader.type) {
457                                                 if (!(reqEntity.eMmsPduType == eMMS_RETRIEVE_MANUAL_CONF && mmsHeader.type == MMS_MSGTYPE_RETRIEVE_CONF)) {
458                                                         MSG_DEBUG("FAIL::type mismatched req:%d received:%d", reqEntity.eMmsPduType, mmsHeader.type);
459
460                                                         MmsPluginEventHandler::instance()->handleMmsError(&reqEntity);
461                                                         break;
462                                                 }
463                                         }
464
465                                         MSG_DEBUG("conf received successfully");
466
467                                         try {
468                                                 MmsPluginEventHandler::instance()->handleMmsReceivedData(&reqEntity, retrievedFilePath);
469                                         } catch (MsgException& e) {
470                                                 MSG_FATAL("%s", e.what());
471                                                 break;
472                                         } catch (exception& e) {
473                                                 MSG_FATAL("%s", e.what());
474                                                 break;
475                                         }
476
477                                         MsgSettingGetBool(MMS_RECV_DELIVERY_RECEIPT, &bReportAllowed);
478
479                                         MSG_DEBUG("conf received successfully -2");
480                                         MSG_DEBUG("reqEntity.eMmsPduType [%d]", reqEntity.eMmsPduType);
481
482                                         // send NotifyResponseInd
483                                         if (reqEntity.eMmsPduType == eMMS_RETRIEVE_AUTO_CONF) {
484                                                 char filepath[MAX_FULL_PATH_SIZE] = {0};
485                                                 // change MmsPduType for ind or ack
486                                                 // make the PDU and then attach to reqEntity also.
487                                                 updatePduType(&reqEntity);
488
489                                                 MSG_DEBUG("#### eMmsPduType:%d ####", reqEntity.eMmsPduType);
490
491                                                 //update http command type & encode m-notify-response-ind
492                                                 reqEntity.eHttpCmdType = eHTTP_CMD_POST_TRANSACTION;
493
494                                                 try {
495                                                         MmsPluginInternal::instance()->encodeNotifyRespInd(reqEntity.transactionId, MMS_MSGSTATUS_RETRIEVED, bReportAllowed, filepath);
496                                                         //m-notification-resp-ind encoding      if err is not MSG_SUCCESS then should set x-mms-status to deferred
497                                                         if (MsgGetFileSize(filepath, &reqEntity.postDataLen) == false) {
498                                                                 MSG_DEBUG("MsgGetFileSize: failed");
499                                                                 break;
500                                                         }
501                                                 } catch (MsgException& e) {
502                                                         MSG_FATAL("%s", e.what());
503                                                         break;
504                                                 } catch (exception& e) {
505                                                         MSG_FATAL("%s", e.what());
506                                                         break;
507                                                 }
508
509                                                 if (reqEntity.pPostData) {
510                                                         free(reqEntity.pPostData);
511                                                         reqEntity.pPostData = NULL;
512                                                 }
513
514                                                 reqEntity.pPostData = MsgOpenAndReadMmsFile(filepath, 0, -1, &reqEntity.postDataLen);
515
516                                                 mmsTranQ.push_front(reqEntity);
517
518                                                 remove(filepath);
519
520                                                 MSG_DEBUG("Submit Ind");
521                                         } else if (reqEntity.eMmsPduType == eMMS_RETRIEVE_MANUAL_CONF) {
522                                                 /* saved msg trId should be checked  */
523                                                 // Send Acknowledge Ind
524                                                 char filepath[MAX_FULL_PATH_SIZE] = {0};
525                                                 // change MmsPduType for ind or ack
526                                                 // make the PDU and then attach to reqEntity also.
527                                                 updatePduType(&reqEntity);
528
529                                                 MSG_DEBUG("#### eMmsPduType:%d ####", reqEntity.eMmsPduType);
530
531                                                 //update http command type & encode m-notify-response-ind
532                                                 reqEntity.eHttpCmdType = eHTTP_CMD_POST_TRANSACTION;
533
534                                                 try {
535                                                         MmsPluginInternal::instance()->encodeAckInd(reqEntity.transactionId, bReportAllowed, filepath);
536                                                         if (MsgGetFileSize(filepath, &reqEntity.postDataLen) == false) {
537                                                                 MSG_DEBUG("MsgGetFileSize: failed");
538                                                                 break;
539                                                         }
540                                                 } catch (MsgException& e) {
541                                                         MSG_FATAL("%s", e.what());
542                                                         break;
543                                                 } catch (exception& e) {
544                                                         MSG_FATAL("%s", e.what());
545                                                         break;
546                                                 }
547
548                                                 if (reqEntity.pPostData) {
549                                                         free(reqEntity.pPostData);
550                                                         reqEntity.pPostData = NULL;
551                                                 }
552
553                                                 reqEntity.pPostData = MsgOpenAndReadMmsFile(filepath, 0, -1, &reqEntity.postDataLen);
554
555                                                 mmsTranQ.push_front(reqEntity);
556
557                                                 remove(filepath); // not ipc
558
559                                                 MSG_DEBUG("Submit Ack");
560                                         } else {
561                                                 reqEntity.isCompleted = true;
562
563                                                 MSG_DEBUG("Transaction complete");
564                                         }
565                                 } else {
566                                         MmsPluginEventHandler::instance()->handleMmsError(&reqEntity);
567                                         break;
568                                 }
569                         } while (reqEntity.isCompleted == false);
570
571                         MSG_DEBUG("==== MMS Transaction Completed ====\n\n");
572
573                         if (reqEntity.pPostData) {
574                                 free(reqEntity.pPostData);
575                                 reqEntity.pPostData = NULL;
576                         }
577
578                         if (reqEntity.pGetData) {
579                                 free(reqEntity.pGetData);
580                                 reqEntity.pGetData = NULL;
581                         }
582
583                         // Http Session Close
584                         httpAgent->clearSession();
585                 }
586
587                 // Request CM Close
588                 cmAgent->close();
589
590         }
591
592 CLEANUP:
593         MSG_DEBUG("CLEANUP");
594
595         while (!mmsTranQ.empty()) {
596                 MSG_DEBUG("clear mmsTranQ");
597
598                 mmsTranQEntity reqEntity;
599                 memset(&reqEntity, 0, sizeof(mmsTranQEntity));
600
601                 mmsTranQ.front(&reqEntity);
602
603                 // notify send fail to APP
604                 MmsPluginEventHandler::instance()->handleMmsError(&reqEntity);
605
606                 if (reqEntity.pGetData) {
607                         MSG_DEBUG("free pGetData");
608                         free(reqEntity.pGetData);
609                         reqEntity.pGetData = NULL;
610                 }
611
612                 if (reqEntity.pPostData) {
613                         MSG_DEBUG("free pPostData");
614                         free(reqEntity.pPostData);
615                         reqEntity.pPostData = NULL;
616                 }
617
618                 mmsTranQ.pop_front();
619         }
620
621         mmsTranQ.clear();
622         MutexLocker locker(mx);
623         running = false;
624
625         MSG_END();
626
627         return;
628 }
629
630 void MmsPluginUaManager::getMmsPduData(mmsTranQEntity *qEntity)
631 {
632         mmsTranQ.front(qEntity);
633 }
634
635 void MmsPluginUaManager::addMmsReqEntity(mmsTranQEntity req)
636 {
637         MSG_DEBUG("New MMS Tran Added");
638         mmsTranQ.push_back(req);
639         lock();
640         signal();
641         unlock();
642 }
643
644 bool MmsPluginUaManager::processReceivedData(int msgId, char *pRcvdBody, int rcvdBodyLen, char *retrievedFilePath)
645 {
646         MSG_BEGIN();
647
648         char fileName[MSG_FILENAME_LEN_MAX] = {0};
649
650         MSG_DEBUG(":::%d :%s ", rcvdBodyLen, pRcvdBody);
651
652         MmsInitHeader();
653         MmsRegisterDecodeBuffer();
654
655         if (MsgCreateFileName(fileName) == false)
656                 return false;
657
658         snprintf(retrievedFilePath, MSG_FILEPATH_LEN_MAX, MSG_DATA_PATH"%s", fileName);
659
660         MSG_DEBUG("retrievedFilePaths [%s]", retrievedFilePath);
661
662         // create temp file
663         if (!MsgOpenCreateAndOverwriteFile(retrievedFilePath, (char *)pRcvdBody, rcvdBodyLen)) {
664                 MSG_DEBUG( "_MmsUaInitMsgDecoder: creating temporary file failed(msgID=%d)\n", msgId);
665                 return false;
666         }
667
668         if (MmsReadMsgBody(msgId, true, true, retrievedFilePath) == false) {
669                 MSG_DEBUG("The MMS Message might include drm contents!!!");
670
671 #ifdef __SUPPORT_DRM__
672                 if (MmsDrm2GetConvertState() == MMS_DRM2_CONVERT_REQUIRED) {
673                         bool bRetToConvert = true;
674                         MSG_MESSAGE_INFO_S pMsg = {0, };
675
676                         pMsg.msgId = msgId;
677
678                         bRetToConvert = MmsDrm2ConvertMsgBody(mmsHeader.msgType.szOrgFilePath);
679
680                         MmsDrm2SetConvertState(MMS_DRM2_CONVERT_FINISH);
681
682                         if (bRetToConvert) {
683                                 int ret;
684                                 ret = remove(mmsHeader.msgType.szOrgFilePath);
685                                 if (ret != 0) {
686                                         MSG_DEBUG("remove fail\n");
687                                         goto ERR_MMS_UA_PROCESS_CONF;
688                                 }
689
690                                 ret = rename(MMS_DECODE_DRM_CONVERTED_TEMP_FILE, mmsHeader.msgType.szOrgFilePath);
691                                 if (ret != 0) {
692                                         MSG_DEBUG("rename fail\n");
693                                         goto ERR_MMS_UA_PROCESS_CONF;
694                                 }
695
696                                 if (MmsDrm2ReadMsgConvertedBody(&pMsg, true, true, retrievedFilePath) == false) {
697                                         MSG_DEBUG("MmsLoadMsg:MmsDrm2ReadMsgConvertedBody() returns false\n");
698                                         goto ERR_MMS_UA_PROCESS_CONF;
699                                 }
700                         }
701                 }
702 #endif
703         }
704
705         MSG_END();
706
707         return true;
708
709 ERR_MMS_UA_PROCESS_CONF:
710         {
711                 MmsMsg *pMsg;
712                 MmsPluginStorage::instance()->getMmsMessage(&pMsg);
713
714
715                 MmsInitHeader();
716                 MmsUnregisterDecodeBuffer();
717
718 #ifdef __SUPPORT_DRM__
719                 MmsReleaseMsgDRMInfo(&pMsg->msgType.drmInfo);
720 #endif
721                 MmsReleaseMsgBody(&pMsg->msgBody, pMsg->msgType.type);
722
723                 return false;
724         }
725 }
726