9d44b41cd979160e26d42d99a6e259ca86620bb3
[platform/framework/native/messaging.git] / src / FMsg_SmsMmsCallbackHandler.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file                FMsg_SmsMmsCallbackHandler.cpp
19  * @brief               This is the implementation file for the %_SmsMmsCallbackHandler class.
20  *
21  * This file contains the implementation of the %_SmsMmsCallbackHandler class.
22  */
23
24 #include <messages.h>
25 #include <email.h>
26 #include <FBaseInteger.h>
27 #include <FBaseSysLog.h>
28 #include <FMsgEtwsPrimaryNotification.h>
29 #include "FMsg_SmsEvent.h"
30 #include "FMsg_SmsEventArg.h"
31 #include "FMsg_MmsEvent.h"
32 #include "FMsg_MmsEventArg.h"
33 #include "FMsg_MsgUtil.h"
34 #include "FMsg_SmsMmsCallbackHandler.h"
35
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Base::Runtime;
39
40 namespace Tizen { namespace Messaging
41 {
42
43 _SmsMmsCallbackHandler* _SmsMmsCallbackHandler::__pMessageCallbackHandler = 0;
44
45 _SmsMmsCallbackHandler::_SmsMmsCallbackHandler(void)
46         : __msgHandle(0)
47         , __cbsSettingsHandle(0)
48 {
49
50 }
51
52 _SmsMmsCallbackHandler::~_SmsMmsCallbackHandler(void)
53 {
54         if (__msgHandle)
55         {
56                 messages_close_service(__msgHandle);
57                 __msgHandle = 0;
58         }
59
60 }
61
62 _SmsMmsCallbackHandler*
63 _SmsMmsCallbackHandler::GetInstance(void)
64 {
65         if (!__pMessageCallbackHandler)
66         {
67                 __pMessageCallbackHandler = new (std::nothrow) _SmsMmsCallbackHandler();
68         }
69
70         return __pMessageCallbackHandler;
71 }
72
73 int
74 _SmsMmsCallbackHandler::OpenMessageHandle(messages_service_h& messageHandle)
75 {
76         int err = MESSAGES_ERROR_NONE;
77
78         if (!__msgHandle)
79         {
80                 err = messages_open_service(&__msgHandle);
81         }
82
83         messageHandle = __msgHandle;
84         return err;
85 }
86
87 int
88 _SmsMmsCallbackHandler::OpenCbsSettingsHandle(messages_cb_settings_h& cbsSettingsHandle)
89 {
90         int err = MESSAGES_ERROR_NONE;
91
92         if (!__cbsSettingsHandle)
93         {
94                 err = messages_cb_load_settings(__msgHandle, &__cbsSettingsHandle);
95         }
96
97         cbsSettingsHandle = __cbsSettingsHandle;
98         return err;
99 }
100
101 int
102 _SmsMmsCallbackHandler::CloseMessageHandle()
103 {
104         int err = MESSAGES_ERROR_NONE;
105
106         if (__msgHandle)
107         {
108                 err = messages_close_service(__msgHandle);
109                 __msgHandle = 0;
110         }
111
112         return err;
113 }
114
115 void
116 _SmsMmsCallbackHandler::SentSmsMessageStatusCallback(messages_sending_result_e status, void* pUserEvent)
117 {
118         result r = E_SUCCESS;
119         _SmsEvent* pSmsEvent = static_cast <_SmsEvent*>(pUserEvent);
120         _SmsEventArg* pSmsEventArg = null;
121
122         SysLog(NID_MSG, "SMS message sent status[%d]", status);
123
124         switch (status)
125         {
126         case MESSAGES_SENDING_SUCCEEDED:      // Message is sent successfully
127                 SysLog(NID_MSG, "MESSAGES_SENDING_SUCCEEDED -> E_SUCCESS");
128                 r = E_SUCCESS;
129                 break;
130
131         case MESSAGES_SENDING_FAILED:         // Message is not sending
132                 SysLog(NID_MSG, "MESSAGES_SENDING_FAILED -> E_FAILURE");
133                 r = E_FAILURE;
134                 SysLog(NID_MSG, "Failed to send the SMS message. error cause: Message is not sending");
135                 break;
136
137         default:
138                 SysLog(NID_MSG, "default -> E_FAILURE");
139                 r = E_FAILURE;
140                 SysLog(NID_MSG, "Failed to send the SMS message. error cause: Unknown");
141                 break;
142         }
143
144         if (pSmsEvent)
145         {
146                 pSmsEventArg = new (std::nothrow) _SmsEventArg(r);
147                 if (pSmsEventArg)
148                 {
149                         pSmsEvent->Fire(*pSmsEventArg);
150                 }
151         }
152 }
153
154 void
155 _SmsMmsCallbackHandler::SentMmsMessageStatusCallback(messages_sending_result_e status, void* pUserEvent)
156 {
157         result r = E_SUCCESS;
158         _MmsEvent* pMmsEvent = static_cast <_MmsEvent*>(pUserEvent);
159         _MmsEventArg* pMmsEventArg = null;
160
161         SysLog(NID_MSG, "MMS message sent status[%d]", status);
162
163         switch (status)
164         {
165         case MESSAGES_SENDING_SUCCEEDED:      // Message is sent successfully
166                 SysLog(NID_MSG, "MESSAGES_SENDING_SUCCEEDED -> E_SUCCESS");
167                 r = E_SUCCESS;
168                 break;
169
170         case MESSAGES_SENDING_FAILED:         // Message is not sending
171                 SysLog(NID_MSG, "MESSAGES_SENDING_SUCCEEDED -> E_FAILURE");
172                 r = E_FAILURE;
173                 break;
174
175         default:
176                 SysLog(NID_MSG, "default -> E_FAILURE");
177                 r = E_FAILURE;
178                 break;
179         }
180
181         if (pMmsEvent)
182         {
183                 pMmsEventArg = new (std::nothrow) _MmsEventArg(r);
184                 if (pMmsEventArg)
185                 {
186                         pMmsEvent->Fire(*pMmsEventArg);
187                 }
188         }
189 }
190
191 void
192 _SmsMmsCallbackHandler::IncomingSmsMessageCallback(messages_message_h incomingMsg, void* pUserParam)
193 {
194         _SmsEvent* pSmsEvent = static_cast <_SmsEvent*>(pUserParam);
195         _SmsEventArg* pSmsEventArg = null;
196         char* pMsgBody = null;
197         char* pMsgAddress = null;
198         time_t tempReceivedTime = 0;
199         int port = 0;
200         DateTime receivedTime;
201         messages_recipient_type_e recipientType = MESSAGES_RECIPIENT_TO;
202
203         messages_get_text(incomingMsg, &pMsgBody);
204         messages_get_time(incomingMsg, &tempReceivedTime);   // get msg time
205         messages_get_message_port(incomingMsg, &port);   // get port number
206         messages_get_address(incomingMsg, 0, &pMsgAddress, &recipientType);
207
208         String message(pMsgBody);       // porting char* to String
209         String senderAddress(pMsgAddress);
210
211         receivedTime = _MsgUtil::ConvertTime(&tempReceivedTime);
212
213         SysLog(NID_MSG, "incoming sms msg_body: [%s]", pMsgBody);
214         SysLog(NID_MSG, "incoming sms port: [%d]", port);
215         SysLog(NID_MSG, "incoming sms received time: [%s]", ctime(&tempReceivedTime));
216
217         free(pMsgBody);
218         pMsgBody = null;
219         free(pMsgAddress);
220         pMsgAddress = null;
221
222         if (pSmsEvent)
223         {
224                 if (port == 0)  // normal incoming SMS message case
225                 {
226                         pSmsEventArg = new (std::nothrow) _SmsEventArg(message, senderAddress, receivedTime);
227                         SysTryReturnVoidResult(NID_MSG, pSmsEventArg != null, E_OUT_OF_MEMORY, "memory allocation failed.");
228                 }
229                 else                    // SMS trigger message case
230                 {
231                         pSmsEventArg = new (std::nothrow) _SmsEventArg(port, receivedTime, message);
232                         SysTryReturnVoidResult(NID_MSG, pSmsEventArg != null, E_OUT_OF_MEMORY, "memory allocation failed.");
233                 }
234
235                 pSmsEvent->Fire(*pSmsEventArg);
236         }
237 }
238
239 void
240 _SmsMmsCallbackHandler::IncomingCbsMessageCallback(messages_cb_message_h incomingMsg, void* pUserParam)
241 {
242         _SmsEvent* pSmsEvent = static_cast <_SmsEvent*>(pUserParam);
243         _SmsEventArg* pSmsEventArg = null;
244         char* pMsgBody = null;
245         time_t tempReceivedTime = 0;
246         DateTime receivedTime;
247         bool etwsNotifiation = false;
248         int messgeId = 0;
249         int serialNumber = 0;
250         int length = 0;
251         int dataCodingScheme = 0;
252         messages_message_type_e cbsType;
253         Tizen::Base::ByteBuffer msgSerialNumber;
254
255         messages_cb_get_text(incomingMsg, &pMsgBody);
256         messages_cb_get_time(incomingMsg, &tempReceivedTime);
257         messages_cb_get_message_id(incomingMsg, &messgeId);
258         messages_cb_get_serial_number(incomingMsg, &serialNumber);
259         messages_cb_get_message_type(incomingMsg, &cbsType);
260         messages_cb_get_dcs(incomingMsg, &dataCodingScheme);
261
262         Tizen::Base::Integer  serialno(serialNumber);
263         length = serialno.ToString().GetLength();
264
265         msgSerialNumber.Construct(length);
266         msgSerialNumber.SetInt(0, serialNumber);
267         msgSerialNumber.SetPosition(0);
268
269         String message(pMsgBody);       // porting char* to String
270
271         receivedTime = _MsgUtil::ConvertTime(&tempReceivedTime);
272
273         Tizen::Base::Integer dataCodingSchemeTemp(dataCodingScheme);
274
275         if (cbsType == MESSAGES_TYPE_SMS_ETWS_PRIMARY || cbsType == MESSAGES_TYPE_SMS_ETWS_SECONDARY)
276         {
277                 etwsNotifiation = true;
278         }
279
280         if (pMsgBody)
281         {
282                 free(pMsgBody);
283                 pMsgBody = null;
284         }
285         SysLog(NID_MSG, "cbs msg_body: [%s]", pMsgBody);
286         SysLog(NID_MSG, "cbs msg_id: [%d]", messgeId);
287         SysLog(NID_MSG, "cbs serialNumber: [%d]", serialNumber);
288         SysLog(NID_MSG, "cbs received time: [%s]", ctime(&tempReceivedTime));
289         SysLog(NID_MSG, "cbs etwsNotifiation: [%d]", etwsNotifiation);
290         SysLog(NID_MSG, "cbs dataCodingScheme: [%d]", dataCodingScheme);
291
292         if (pSmsEvent)
293         {
294                 pSmsEventArg = new (std::nothrow) _SmsEventArg(message, msgSerialNumber, receivedTime, messgeId, etwsNotifiation, ((byte) dataCodingSchemeTemp.ToChar()), _SMS_EVENT_TYPE_CBS_RECEIVE_RESULT);
295                 SysTryReturnVoidResult(NID_MSG, pSmsEventArg != null, E_OUT_OF_MEMORY, "memory allocation failed.");
296                 pSmsEvent->Fire(*pSmsEventArg);
297         }
298 }
299
300 void
301 _SmsMmsCallbackHandler::IncomingWapPushMessageCallback(const char* pHeader, const char* pBody, int bodyLength, void* pUserParam)
302 {
303         _SmsEvent* pSmsEvent = static_cast <_SmsEvent*>(pUserParam);
304         _SmsEventArg* pSmsEventArg = null;
305         String msgHeader(pHeader);
306         Tizen::Base::ByteBuffer msgBody;
307         msgBody.Construct(bodyLength);
308         msgBody.SetArray((byte*) pBody, 0, bodyLength);
309         msgBody.SetPosition(0);
310
311         SysLog(NID_MSG, "push msg body: [%s]", pBody);
312         SysLog(NID_MSG, "push msg header: [%s]", pHeader);
313         SysLog(NID_MSG, "push body length: [%d]", bodyLength);
314
315         if (pSmsEvent)
316         {
317                 pSmsEventArg = new (std::nothrow) _SmsEventArg(msgHeader, msgBody, _SMS_EVENT_TYPE_WAP_PUSH_RECEIVE_RESULT);
318                 SysTryReturnVoidResult(NID_MSG, pSmsEventArg != null, E_OUT_OF_MEMORY, "memory allocation failed.");
319                 pSmsEvent->Fire(*pSmsEventArg);
320         }
321 }
322
323 void
324 _SmsMmsCallbackHandler::IncomingEtwsMessageCallback(messages_cb_message_h incomingMsg, int etwsWarningType, const char* pEtwsSecurityInfo, void* pUserParam)
325 {
326         _SmsEvent* pSmsEvent = static_cast <_SmsEvent*>(pUserParam);
327         _SmsEventArg* pSmsEventArg = null;
328         time_t tempReceivedTime = 0;
329         DateTime receivedTime;
330         int messgeId = 0;
331         int serialNumber = 0;
332         int length = 0;
333         Tizen::Base::ByteBuffer msgSerialNumber;
334         Tizen::Base::ByteBuffer msgSecurityInfo;
335         Tizen::Base::ByteBuffer etwsMsgWarningType;
336
337         messages_cb_get_time(incomingMsg, &tempReceivedTime);
338         messages_cb_get_message_id(incomingMsg, &messgeId);
339         messages_cb_get_serial_number(incomingMsg, &serialNumber);
340
341         Tizen::Base::Integer  serialno(serialNumber);
342         length = serialno.ToString().GetLength();
343
344         msgSerialNumber.Construct(length);
345         msgSerialNumber.SetInt(0, serialNumber);
346         msgSerialNumber.SetPosition(0);
347
348         String etwsSecurityInfo(pEtwsSecurityInfo);
349         length = etwsSecurityInfo.GetLength();
350         msgSecurityInfo.Construct(length);
351         msgSecurityInfo.SetArray((byte*) pEtwsSecurityInfo, 0, length);
352         msgSecurityInfo.SetPosition(0);
353
354         Tizen::Base::Integer  warningtype(etwsWarningType);
355         length = warningtype.ToString().GetLength();
356
357         etwsMsgWarningType.Construct(length);
358         etwsMsgWarningType.SetInt(0, etwsWarningType);
359         etwsMsgWarningType.SetPosition(0);
360
361         receivedTime = _MsgUtil::ConvertTime(&tempReceivedTime);
362
363         SysLog(NID_MSG, "cbs msg_id: [%d]", messgeId);
364         SysLog(NID_MSG, "cbs received time: [%s]", ctime(&tempReceivedTime));
365         SysLog(NID_MSG, "cbs etwsWarningType: [%d]", etwsWarningType);
366
367         if (pSmsEvent)
368         {
369                 pSmsEventArg = new (std::nothrow) _SmsEventArg(receivedTime, msgSerialNumber, msgSecurityInfo, messgeId, etwsMsgWarningType, _SMS_EVENT_TYPE_ETWS_RECEIVE_RESULT);
370                 SysTryReturnVoidResult(NID_MSG, pSmsEventArg != null, E_OUT_OF_MEMORY, "memory allocation failed.");
371                 pSmsEvent->Fire(*pSmsEventArg);
372         }
373 }
374
375 } } // Tizen::Messaging