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