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