Merge from private.
[platform/core/messaging/msg-service.git] / proxy / MsgHandleControl.cpp
1 /*
2 * Copyright 2012  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.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.tizenopensource.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 <errno.h>
18 #include <stdlib.h>
19
20 #include <security-server.h>
21
22 #include "MsgDebug.h"
23 #include "MsgCppTypes.h"
24 #include "MsgException.h"
25 #include "MsgUtilFile.h"
26 #include "MsgGconfWrapper.h"
27 #include "MsgProxyListener.h"
28 #include "MsgHandle.h"
29
30 /*==================================================================================================
31                                      IMPLEMENTATION OF MsgHandle - Control Member Functions
32 ==================================================================================================*/
33 MsgHandle::MsgHandle() : mCounter(0), mClientSock()
34 {
35         memset(mConnectionId, 0x00, sizeof(mConnectionId));
36 }
37
38
39 MsgHandle::~MsgHandle()
40 {
41
42 }
43
44
45 void MsgHandle::openHandle()
46 {
47         int ret = 0;
48         size_t cookieSize;
49
50         bool bReady = false;
51
52         // server is currently booting and service is not available until the end of booting
53         MsgSettingGetBool(VCONFKEY_MSG_SERVER_READY, &bReady);
54
55         if (bReady == false) {
56                 THROW(MsgException::SERVER_READY_ERROR, "Msg Server is not ready !!!!!");
57         } else {
58                 MSG_DEBUG("Msg Server is ready !!!!!");
59         }
60
61         // Get Cookie Size
62         cookieSize = security_server_get_cookie_size();
63
64         MSG_DEBUG("cookie size : [%d]", cookieSize);
65
66         // Request Cookie
67         ret = security_server_request_cookie(mCookie, cookieSize);
68
69         if (ret < 0) {
70
71                 MSG_DEBUG("security_server_request_cookie() error!! [%d]", ret);
72                 return;
73         }
74
75         // Open Socket IPC
76         connectSocket();
77 }
78
79
80 void MsgHandle::closeHandle(MsgHandle* pHandle)
81 {
82         MSG_BEGIN();
83
84         //Remove CB List of closing Handle
85         MsgProxyListener* eventListener = MsgProxyListener::instance();
86
87         eventListener->clearListOfClosedHandle(pHandle);
88         //eventListener->stop();
89
90         // Close Socket IPC
91         disconnectSocket();
92
93         MSG_END();
94 }
95
96
97 void MsgHandle::connectSocket()
98 {
99         mClientSock.connect(MSG_SOCKET_PATH);
100 }
101
102
103 void MsgHandle::disconnectSocket()
104 {
105         mClientSock.close();
106 }
107
108
109 void MsgHandle::write(const char *pCmdData, int cmdSize, char **ppEvent)
110 {
111         if (pCmdData == NULL || ppEvent == NULL) {
112                 THROW(MsgException::INVALID_PARAM, "Param is NULL");
113         }
114
115         // Send Command to MSG FW
116         mClientSock.write(pCmdData, cmdSize);
117
118         // Receive Result from MSG FW
119         read(ppEvent);
120
121         if (ppEvent == NULL) {
122                 THROW(MsgException::INVALID_RESULT, "event is NULL");
123         }
124 }
125
126
127 void MsgHandle::read(char **ppEvent)
128 {
129         unsigned int dataSize = 0;
130
131         dataSize = mClientSock.read(ppEvent, &dataSize);
132
133         if (dataSize == 0) {
134                 THROW(MsgException::IPC_ERROR, "Server closed connection");
135         } else if(dataSize < 0) {
136                 THROW(MsgException::IPC_ERROR, "negative length??? %d", dataSize);
137         }
138 }
139
140
141 void MsgHandle::convertMsgStruct(const MSG_MESSAGE_HIDDEN_S *pSrc, MSG_MESSAGE_INFO_S *pDest)
142 {
143         MSG_BEGIN();
144
145         pDest->msgId = pSrc->msgId;
146         pDest->threadId = pSrc->threadId;
147         pDest->folderId = pSrc->folderId;
148         pDest->msgType.mainType = pSrc->mainType;
149         pDest->msgType.subType = pSrc->subType;
150         pDest->msgType.classType= pSrc->classType;
151         pDest->storageId = pSrc->storageId;
152
153         msg_struct_list_s *addr_info_s = pSrc->addr_list;
154
155         if (addr_info_s) {
156                 msg_struct_s *addr_info = NULL;
157                 MSG_ADDRESS_INFO_S *address = NULL;
158
159                 pDest->nAddressCnt = addr_info_s->nCount;
160
161                 for (int i = 0; i < addr_info_s->nCount; i++)
162                 {
163                         addr_info = (msg_struct_s *)addr_info_s->msg_struct_info[i];
164                         address = (MSG_ADDRESS_INFO_S *)addr_info->data;
165
166                         pDest->addressList[i].addressType = address->addressType;
167                         pDest->addressList[i].recipientType = address->recipientType;
168                         pDest->addressList[i].contactId = address->contactId;
169                         strncpy(pDest->addressList[i].addressVal, address->addressVal, MAX_ADDRESS_VAL_LEN);
170                         strncpy(pDest->addressList[i].displayName, address->displayName, MAX_DISPLAY_NAME_LEN);
171                         pDest->addressList[i].displayName[MAX_DISPLAY_NAME_LEN] = '\0';
172                 }
173         }
174
175         strncpy(pDest->replyAddress, pSrc->replyAddress, MAX_PHONE_NUMBER_LEN);
176         strncpy(pDest->subject, pSrc->subject, MAX_SUBJECT_LEN);
177
178         pDest->displayTime = pSrc->displayTime;
179         pDest->networkStatus = pSrc->networkStatus;
180         pDest->encodeType = pSrc->encodeType;
181         pDest->bRead = pSrc->bRead;
182         pDest->bProtected = pSrc->bProtected;
183         pDest->bBackup = pSrc->bBackup;
184         pDest->priority = pSrc->priority;
185         pDest->direction = pSrc->direction;
186
187         // Set Port Info.
188         pDest->msgPort.valid = pSrc->bPortValid;
189
190         if (pDest->msgPort.valid == true) {
191                 pDest->msgPort.dstPort = pSrc->dstPort;
192                 pDest->msgPort.srcPort = pSrc->srcPort;
193         }
194
195         MSG_DEBUG("nSize = %d",  pSrc->dataSize);
196
197         if (pSrc->mainType == MSG_SMS_TYPE){
198                 pDest->bTextSms = true;
199                 pDest->dataSize = pSrc->dataSize;
200
201                 memset(pDest->msgText, 0x00, sizeof(pDest->msgText));
202
203                 if (pSrc->dataSize > MAX_MSG_TEXT_LEN) {
204                         // Save Message Data into File
205                         char fileName[MAX_COMMON_INFO_SIZE+1];
206                         memset(fileName, 0x00, sizeof(fileName));
207
208                         if(MsgCreateFileName(fileName) == false)
209                                 THROW(MsgException::FILE_ERROR, "MsgCreateFileName error");
210
211                         MSG_DEBUG("Save pSrc->pData into file : size[%d] name[%s]", pDest->dataSize, fileName);
212
213                         if (MsgWriteIpcFile(fileName, (char*)pSrc->pData, pSrc->dataSize) == false)
214                                 THROW(MsgException::FILE_ERROR, "MsgWriteIpcFile error");
215
216                         memset(pDest->msgData, 0x00, sizeof(pDest->msgData));
217                         strncpy(pDest->msgData, fileName, MAX_MSG_DATA_LEN);
218
219                         pDest->bTextSms = false;
220
221                 } else {
222                         if (pDest->encodeType == MSG_ENCODE_8BIT)
223                                 memcpy(pDest->msgText, pSrc->pData, pSrc->dataSize);
224                         else
225                                 strncpy(pDest->msgText, (char*)pSrc->pData, pSrc->dataSize);
226                 }
227
228                 MSG_DEBUG("pData = %s",  pSrc->pData);
229                 MSG_DEBUG("msgText = %s",  pDest->msgText);
230         } else if (pSrc->mainType == MSG_MMS_TYPE) {
231
232                 pDest->bTextSms = false;
233                 pDest->dataSize = pSrc->dataSize;
234
235                 if(pSrc->subType == MSG_READREPLY_MMS) {
236                         memset(pDest->msgData, 0x00, sizeof(pDest->msgData));
237                         memcpy(pDest->msgData, pSrc->pMmsData, pSrc->dataSize);
238                 } else {
239                         // Save Message Data into File
240                         char fileName[MAX_COMMON_INFO_SIZE+1];
241                         memset(fileName, 0x00, sizeof(fileName));
242
243                         if(MsgCreateFileName(fileName) == false)
244                                 THROW(MsgException::FILE_ERROR, "MsgCreateFileName error");
245
246                         // change file extension in case of java MMS msg
247                         if (pSrc->subType == MSG_SENDREQ_JAVA_MMS) {
248                                 char* pFileNameExt;
249                                 pFileNameExt = strstr(fileName,"DATA");
250                                 strncpy(pFileNameExt,"JAVA", MAX_COMMON_INFO_SIZE);
251                         }
252
253                         MSG_DEBUG("Save Message Data into file : size[%d] name[%s]", pDest->dataSize, fileName);
254
255                         if (MsgWriteIpcFile(fileName, (char*)pSrc->pMmsData, pSrc->dataSize) == false)
256                                 THROW(MsgException::FILE_ERROR, "MsgWriteIpcFile error");
257
258                         memset(pDest->msgData, 0x00, sizeof(pDest->msgData));
259                         strncpy(pDest->msgData, fileName, MAX_MSG_DATA_LEN);
260                         if (pSrc->pData) {
261                                 strncpy(pDest->msgText, (char*)pSrc->pData, sizeof(pDest->msgText));
262                         }
263                 }
264         }
265
266         MSG_END();
267 }
268
269
270 void MsgHandle::convertMsgStruct(const MSG_MESSAGE_INFO_S *pSrc, MSG_MESSAGE_HIDDEN_S *pDest)
271 {
272         MSG_BEGIN();
273
274         pDest->msgId = pSrc->msgId;
275         pDest->threadId = pSrc->threadId;
276         pDest->folderId = pSrc->folderId;
277         pDest->mainType = pSrc->msgType.mainType;
278         pDest->subType = pSrc->msgType.subType;
279         pDest->storageId = pSrc->storageId;
280
281         strncpy(pDest->replyAddress, pSrc->replyAddress, MAX_PHONE_NUMBER_LEN);
282         strncpy(pDest->subject, pSrc->subject, MAX_SUBJECT_LEN);
283
284         pDest->displayTime = pSrc->displayTime;
285         pDest->networkStatus = pSrc->networkStatus;
286         pDest->encodeType = pSrc->encodeType;
287         pDest->bRead = pSrc->bRead;
288         pDest->bProtected = pSrc->bProtected;
289         pDest->bBackup = pSrc->bBackup;
290         pDest->priority = pSrc->priority;
291         pDest->direction = pSrc->direction;
292
293         // Set Port Info.
294         pDest->bPortValid = pSrc->msgPort.valid;
295
296         if (pDest->bPortValid == true) {
297                 pDest->dstPort = pSrc->msgPort.dstPort;
298                 pDest->srcPort = pSrc->msgPort.srcPort;
299         }
300
301         if(pSrc->thumbPath[0] != '\0')
302                 strncpy(pDest->thumbPath, pSrc->thumbPath, MSG_FILEPATH_LEN_MAX);
303
304         pDest->addr_list->nCount = pSrc->nAddressCnt;
305
306         msg_struct_s *addr_info_s = NULL;
307         MSG_ADDRESS_INFO_S *addr_info = NULL;
308
309         for (int i = 0; i < pDest->addr_list->nCount; i++)
310         {
311                 addr_info_s = (msg_struct_s *)pDest->addr_list->msg_struct_info[i];
312                 addr_info = (MSG_ADDRESS_INFO_S *)addr_info_s->data;
313
314                 addr_info->addressType = pSrc->addressList[i].addressType;
315                 addr_info->recipientType = pSrc->addressList[i].recipientType;
316                 addr_info->contactId = pSrc->addressList[i].contactId;
317                 strncpy(addr_info->addressVal, pSrc->addressList[i].addressVal, MAX_ADDRESS_VAL_LEN);
318                 strncpy(addr_info->displayName, pSrc->addressList[i].displayName, MAX_DISPLAY_NAME_LEN);
319                 addr_info->displayName[MAX_DISPLAY_NAME_LEN] = '\0';
320         }
321
322
323         if (pSrc->bTextSms == false) {
324                 int fileSize = 0;
325
326                 char* pFileData = NULL;
327                 AutoPtr<char> buf(&pFileData);
328
329                 pDest->dataSize = pSrc->dataSize;
330
331                 // Get Message Data from File
332                 if (pSrc->networkStatus != MSG_NETWORK_RETRIEVE_FAIL) {
333                         MSG_DEBUG("Get Message Data from file : size[%d] name[%s]\n", pDest->dataSize, pSrc->msgData);
334                         if (MsgOpenAndReadFile(pSrc->msgData, &pFileData, &fileSize) == false)
335                                 THROW(MsgException::FILE_ERROR, "MsgOpenAndReadFile error");
336
337                         if (pSrc->msgType.mainType == MSG_SMS_TYPE) {
338                                 if (pDest->encodeType == MSG_ENCODE_8BIT) {
339                                         pDest->pData = (void*)new char[fileSize];
340                                         memset(pDest->pData, 0x00, fileSize);
341                                         memcpy(pDest->pData, pFileData, fileSize);
342                                 } else {
343                                         pDest->pData = (void*)new char[fileSize+1];
344                                         memset(pDest->pData, 0x00, fileSize+1);
345                                         strncpy((char*)pDest->pData, pFileData, fileSize);
346                                 }
347                         } else {
348                                 if (pSrc->msgText[0] != '\0') {
349                                         pDest->pData = (void*)new char[strlen(pSrc->msgText)+1];
350                                         memset(pDest->pData, 0x00, strlen(pSrc->msgText)+1);
351                                         strncpy((char*)pDest->pData, pSrc->msgText, strlen(pSrc->msgText));
352                                 }
353                                 pDest->pMmsData = (void*)new char[fileSize];
354                                 memset(pDest->pMmsData, 0x00, fileSize);
355                                 memcpy(pDest->pMmsData, pFileData, fileSize);
356                         }
357                 }
358         } else {
359                 pDest->dataSize = pSrc->dataSize;
360
361                 if (pSrc->msgType.mainType == MSG_SMS_TYPE) {
362                         if (pDest->encodeType == MSG_ENCODE_8BIT || pSrc->msgType.subType == MSG_ETWS_SMS) {
363                                 pDest->pData = (void*)new char[pDest->dataSize];
364                                 memset(pDest->pData, 0x00, pDest->dataSize);
365                                 memcpy(pDest->pData, pSrc->msgText, pDest->dataSize);
366                         } else {
367                                 pDest->pData = (void*)new char[pDest->dataSize+1];
368                                 memset(pDest->pData, 0x00, pDest->dataSize+1);
369                                 strncpy((char*)pDest->pData, pSrc->msgText, pDest->dataSize);
370                         }
371                 } else {
372                         if (pSrc->msgText[0] != '\0') {
373                                 pDest->pData = (void*)new char[strlen(pSrc->msgText)+1];
374                                 memset(pDest->pData, 0x00, strlen(pSrc->msgText)+1);
375                                 strncpy((char*)pDest->pData, pSrc->msgText, strlen(pSrc->msgText));
376                         }
377
378                         pDest->pMmsData = (void*)new char[pDest->dataSize];
379                         memset(pDest->pMmsData, 0x00, pDest->dataSize);
380                         memcpy(pDest->pMmsData, pSrc->msgData, pDest->dataSize);
381                 }
382         }
383
384         MSG_END();
385 }
386
387
388 void MsgHandle::convertSendOptStruct(const MSG_SENDINGOPT_S* pSrc, MSG_SENDINGOPT_INFO_S* pDest, MSG_MESSAGE_TYPE_S msgType)
389 {
390         MSG_BEGIN();
391
392         pDest->bSetting = pSrc->bSetting;
393
394         if (pDest->bSetting == false) {
395                 MSG_DEBUG("No Sending Option");
396                 return;
397         }
398
399         pDest->bDeliverReq = pSrc->bDeliverReq;
400         pDest->bKeepCopy = pSrc->bKeepCopy;
401
402         MSG_DEBUG("pDest->bSetting = %d", pDest->bSetting);
403         MSG_DEBUG("pDest->bDeliverReq = %d", pDest->bDeliverReq);
404         MSG_DEBUG("pDest->bKeepCopy = %d", pDest->bKeepCopy);
405
406         if (msgType.mainType == MSG_SMS_TYPE) {
407                 msg_struct_s *pStruct = (msg_struct_s *)pSrc->smsSendOpt;
408                 SMS_SENDINGOPT_S *pSms = (SMS_SENDINGOPT_S *)pStruct->data;
409                 pDest->option.smsSendOptInfo.bReplyPath = pSms->bReplyPath;
410         } else if (msgType.mainType == MSG_MMS_TYPE) {
411                 msg_struct_s *pStruct = (msg_struct_s *)pSrc->mmsSendOpt;
412                 MMS_SENDINGOPT_S *pMms = (MMS_SENDINGOPT_S *)pStruct->data;
413                 pDest->option.mmsSendOptInfo.priority = pMms->priority;
414                 pDest->option.mmsSendOptInfo.bReadReq = pMms->bReadReq;
415
416                 MSG_DEBUG("pDest->option.mmsSendOpt.priority = %d", pMms->priority);
417                 MSG_DEBUG("pDest->option.mmsSendOpt.bReadReq = %d", pMms->bReadReq);
418
419                 if (pMms->expiryTime == 0) {
420                         pDest->option.mmsSendOptInfo.expiryTime.type = MMS_TIMETYPE_NONE;
421                         pDest->option.mmsSendOptInfo.expiryTime.time = pMms->expiryTime;
422                 } else {
423                         pDest->option.mmsSendOptInfo.expiryTime.type = MMS_TIMETYPE_RELATIVE;
424                         pDest->option.mmsSendOptInfo.expiryTime.time = pMms->expiryTime;
425                 }
426
427                 if (pMms->bUseDeliveryCustomTime == true) {
428                         pDest->option.mmsSendOptInfo.bUseDeliveryCustomTime = true;
429                 } else {
430                         pDest->option.mmsSendOptInfo.bUseDeliveryCustomTime = false;
431                 }
432                 pDest->option.mmsSendOptInfo.deliveryTime.type = MMS_TIMETYPE_RELATIVE;
433                 pDest->option.mmsSendOptInfo.deliveryTime.time = pMms->deliveryTime;
434
435                 MSG_DEBUG("pDest->option.mmsSendOpt.expiryTime = %d", pDest->option.mmsSendOptInfo.expiryTime.time);
436         }
437
438         MSG_END();
439 }
440
441
442 void MsgHandle::convertSendOptStruct(const MSG_SENDINGOPT_INFO_S* pSrc, MSG_SENDINGOPT_S* pDest, MSG_MESSAGE_TYPE_S msgType)
443 {
444         MSG_BEGIN();
445
446         pDest->bDeliverReq = pSrc->bDeliverReq;
447         pDest->bKeepCopy = pSrc->bKeepCopy;
448
449         MSG_DEBUG("pDest->bDeliverReq = %d", pDest->bDeliverReq);
450         MSG_DEBUG("pDest->bKeepCopy = %d", pDest->bKeepCopy);
451
452         if (msgType.mainType == MSG_SMS_TYPE) {
453                 msg_struct_s *pStruct = (msg_struct_s *)pDest->smsSendOpt;
454                 SMS_SENDINGOPT_S *pSms = (SMS_SENDINGOPT_S *)pStruct->data;
455                 pSms->bReplyPath = pSrc->option.smsSendOptInfo.bReplyPath;
456         } else if (msgType.mainType == MSG_MMS_TYPE) {
457                 msg_struct_s *pStruct = (msg_struct_s *)pDest->mmsSendOpt;
458                 MMS_SENDINGOPT_S *pMms = (MMS_SENDINGOPT_S *)pStruct->data;
459                 pMms->priority = pSrc->option.mmsSendOptInfo.priority;
460                 pMms->bReadReq = pSrc->option.mmsSendOptInfo.bReadReq;
461                 pMms->expiryTime = pSrc->option.mmsSendOptInfo.expiryTime.time;
462                 pMms->deliveryTime = pSrc->option.mmsSendOptInfo.deliveryTime.time;
463
464                 MSG_DEBUG("pDest->option.mmsSendOpt.priority = %d", pMms->priority);
465                 MSG_DEBUG("pDest->option.mmsSendOpt.bReadReq = %d", pMms->bReadReq);
466                 MSG_DEBUG("pDest->option.mmsSendOpt.expiryTime = %d", pMms->expiryTime);
467         }
468
469         MSG_END();
470 }
471
472
473 int MsgHandle::getSettingCmdSize(MSG_OPTION_TYPE_T optionType)
474 {
475         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_OPTION_TYPE_T);
476
477         switch (optionType)
478         {
479                 case MSG_GENERAL_OPT :
480                         cmdSize += sizeof(MSG_GENERAL_OPT_S);
481                 break;
482                 case MSG_SMS_SENDOPT :
483                         cmdSize += sizeof(MSG_SMS_SENDOPT_S);
484                 break;
485                 case MSG_SMSC_LIST :
486                         cmdSize += sizeof(MSG_SMSC_LIST_S);
487                 break;
488                 case MSG_MMS_SENDOPT :
489                         cmdSize += sizeof(MSG_MMS_SENDOPT_S);
490                 break;
491                 case MSG_MMS_RECVOPT :
492                         cmdSize += sizeof(MSG_MMS_RECVOPT_S);
493                 break;
494                 case MSG_MMS_STYLEOPT :
495                         cmdSize += sizeof(MSG_MMS_STYLEOPT_S);
496                 break;
497                 case MSG_PUSHMSG_OPT :
498                         cmdSize += sizeof(MSG_PUSHMSG_OPT_S);
499                 break;
500                 case MSG_CBMSG_OPT :
501                         cmdSize += sizeof(MSG_CBMSG_OPT_S);
502                 break;
503                 case MSG_VOICEMAIL_OPT :
504                         cmdSize += sizeof(MSG_VOICEMAIL_OPT_S);
505                 break;
506                 case MSG_MSGSIZE_OPT :
507                         cmdSize += sizeof(MSG_MSGSIZE_OPT_S);
508                 break;
509         }
510
511         return cmdSize;
512 }