update tizen source
[framework/messaging/msg-service.git] / plugin / sms_plugin / SmsPluginTpduCodec.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 <stdio.h>
32 #include <string.h>
33
34 #include "MsgDebug.h"
35 #include "MsgCppTypes.h"
36 #include "MsgException.h"
37 #include "MsgGconfWrapper.h"
38 #include "MsgUtilFile.h"
39
40 #include "SmsPluginTpduCodec.h"
41 #include "SmsPluginParamCodec.h"
42 #include "SmsPluginUDCodec.h"
43
44
45 /*==================================================================================================
46                                      IMPLEMENTATION OF SmsPluginTpduCodec - Member Functions
47 ==================================================================================================*/
48 SmsPluginTpduCodec::SmsPluginTpduCodec()
49 {
50
51
52 }
53
54
55 SmsPluginTpduCodec::~SmsPluginTpduCodec()
56 {
57
58
59 }
60
61
62 int SmsPluginTpduCodec::encodeTpdu(const SMS_TPDU_S *pSmsTpdu, char *pTpdu)
63 {
64         int tpduLen = 0;
65
66         switch (pSmsTpdu->tpduType)
67         {
68                 case SMS_TPDU_SUBMIT:
69                         tpduLen = encodeSubmit(&(pSmsTpdu->data.submit), pTpdu);
70                 break;
71
72                 case SMS_TPDU_DELIVER:
73                         tpduLen = encodeDeliver(&(pSmsTpdu->data.deliver), pTpdu);
74                 break;
75
76                 case SMS_TPDU_DELIVER_REP:
77                         tpduLen = encodeDeliverReport(&(pSmsTpdu->data.deliverRep), pTpdu);
78                 break;
79
80                 case SMS_TPDU_STATUS_REP:
81                         tpduLen = encodeStatusReport(&(pSmsTpdu->data.statusRep), pTpdu);
82                 break;
83         }
84
85         return tpduLen;
86 }
87
88
89 int SmsPluginTpduCodec::decodeTpdu(const unsigned char *pTpdu, int TpduLen, SMS_TPDU_S *pSmsTpdu)
90 {
91         int decodeLen = 0;
92
93         char mti = pTpdu[0] & 0x03;
94
95         switch (mti)
96         {
97                 case 0x00:
98                         pSmsTpdu->tpduType = SMS_TPDU_DELIVER;
99                         decodeLen = decodeDeliver(pTpdu, TpduLen, &(pSmsTpdu->data.deliver));
100                 break;
101
102                 case 0x01:
103                         pSmsTpdu->tpduType = SMS_TPDU_SUBMIT;
104                         decodeLen = decodeSubmit(pTpdu, TpduLen, &(pSmsTpdu->data.submit));
105                 break;
106
107                 case 0x02:
108                         pSmsTpdu->tpduType = SMS_TPDU_STATUS_REP;
109                         decodeLen = decodeStatusReport(pTpdu, TpduLen, &(pSmsTpdu->data.statusRep));
110                 break;
111         }
112
113         return decodeLen;
114 }
115
116
117 int SmsPluginTpduCodec::encodeSubmit(const SMS_SUBMIT_S *pSubmit, char *pTpdu)
118 {
119         int offset = 0, length = 0, encodeSize = 0;
120
121         char* address = NULL;
122         AutoPtr<char> addressBuf(&address);
123
124         char* dcs = NULL;
125         AutoPtr<char> dcsBuf(&dcs);
126
127         char* vpTime = NULL;
128         AutoPtr<char> vpBuf(&vpTime);
129
130         //TP-MTI
131         pTpdu[offset] = 0x01;
132
133         //TP-RD
134         if(pSubmit->bRejectDup == false)
135                 pTpdu[offset] |= 0x04;
136
137         //TP-VPF
138         switch (pSubmit->vpf)
139         {
140                 case SMS_VPF_NOT_PRESENT:
141                         break;
142                 case SMS_VPF_ENHANCED:
143                         pTpdu[offset] |= 0x08;
144                         break;
145                 case SMS_VPF_RELATIVE:
146                         pTpdu[offset] |= 0x10;
147                         break;
148                 case SMS_VPF_ABSOLUTE:
149                         pTpdu[offset] |= 0x18;
150                         break;
151                 default:
152                         break;
153         }
154
155         //TP-SRR
156         if (pSubmit->bStatusReport == true)
157                 pTpdu[offset] |= 0x20;
158
159         MSG_DEBUG("TP-SRR pSubmit->bStatusReport : %d.", pSubmit->bStatusReport);
160
161         //TP-UDHI
162         if (pSubmit->bHeaderInd == true)
163                 pTpdu[offset] |= 0x40;
164
165         //TP-RP
166         if (pSubmit->bReplyPath == true)
167                 pTpdu[offset] |= 0x80;
168
169         offset++;
170
171         //TP-MR
172         pTpdu[offset++] = pSubmit->msgRef;
173
174         MSG_DEBUG("TP-MR pSubmit->msgRef : %d.", pSubmit->msgRef);
175
176         //TP-DA
177         length = SmsPluginParamCodec::encodeAddress(&pSubmit->destAddress, &address);
178         memcpy(&(pTpdu[offset]), address, length);
179         offset += length;
180
181         MSG_DEBUG("TP-DA length : %d.", length);
182
183         //TP-PID
184         pTpdu[offset++] = pSubmit->pid;
185
186         MSG_DEBUG("TP-PID pSubmit->pid : %d.", pSubmit->pid);
187
188         //TP-DCS
189         length = SmsPluginParamCodec::encodeDCS(&pSubmit->dcs, &dcs);
190         memcpy(&(pTpdu[offset]), dcs, length);
191         offset += length;
192
193         MSG_DEBUG("TP-DCS length : %d.", length);
194
195         //TP-VP
196         if (pSubmit->vpf != SMS_VPF_NOT_PRESENT)
197         {
198                 length = SmsPluginParamCodec::encodeTime(&pSubmit->validityPeriod, &vpTime);
199
200                 if (length > 0)
201                 {
202                         memcpy(&(pTpdu[offset]), vpTime, length);
203                         offset += length;
204                 }
205         }
206
207         encodeSize = SmsPluginUDCodec::encodeUserData(&(pSubmit->userData), pSubmit->dcs.codingScheme, &(pTpdu[offset]));
208
209 MSG_DEBUG("encodeSize : %d", encodeSize);
210
211         offset += encodeSize;
212
213         return offset;
214 }
215
216
217 int SmsPluginTpduCodec::encodeDeliver(const SMS_DELIVER_S *pDeliver, char *pTpdu)
218 {
219         int offset = 0, length = 0, encodeSize = 0;
220
221         char* address = NULL;
222         AutoPtr<char> addressBuf(&address);
223
224         char* dcs = NULL;
225         AutoPtr<char> dcsBuf(&dcs);
226
227         char* scts = NULL;
228         AutoPtr<char> timeBuf(&scts);
229
230         // TP-MTI : 00
231         pTpdu[offset] = 0x00;
232
233         // TP-MMS
234         if (pDeliver->bMoreMsg == false)
235                 pTpdu[offset] |= 0x04;
236
237         // TP-SRI
238         if (pDeliver->bStatusReport == true)
239                 pTpdu[offset] |= 0x20;
240
241         // TP-UDHI
242         if (pDeliver->bHeaderInd == true)
243                 pTpdu[offset] |= 0x40;
244
245         // TP-RP
246         if (pDeliver->bReplyPath == true)
247                 pTpdu[offset] |= 0x80;
248
249         offset++;
250
251         // TP-OA
252         length = SmsPluginParamCodec::encodeAddress(&pDeliver->originAddress, &address);
253         memcpy(&(pTpdu[offset]), address, length);
254         offset += length;
255
256         // TP-PID
257         pTpdu[offset++] = pDeliver->pid;
258
259         // TP-DCS
260         length = SmsPluginParamCodec::encodeDCS(&pDeliver->dcs, &dcs);
261         memcpy(&(pTpdu[offset]), dcs, length);
262         offset += length;
263
264         // TP-SCTS
265         length = SmsPluginParamCodec::encodeTime(&pDeliver->timeStamp, &scts);
266         memcpy(&(pTpdu[offset]), scts, length);
267         offset += length;
268
269         // TP-UDL & TP-UD
270         encodeSize = SmsPluginUDCodec::encodeUserData(&(pDeliver->userData), pDeliver->dcs.codingScheme, &(pTpdu[offset]));
271
272         MSG_DEBUG("encodeSize : %d", encodeSize);
273
274         offset += encodeSize;
275
276         return offset;
277 }
278
279
280 int SmsPluginTpduCodec::encodeDeliverReport(const SMS_DELIVER_REPORT_S *pDeliverRep, char *pTpdu)
281 {
282         int offset = 0;
283
284         // TP-MTI : 00
285         pTpdu[offset] = 0x00;
286
287         // TP-UDHI
288         if (pDeliverRep->bHeaderInd == true)
289                 pTpdu[offset] |= 0x40;
290
291         offset++;
292
293         // TP-FCS
294         if (pDeliverRep->reportType == SMS_REPORT_NEGATIVE)
295                 pTpdu[offset++] = pDeliverRep->failCause;
296
297         // TP-PI
298         pTpdu[offset++] = pDeliverRep->paramInd;
299
300         // TP-PID
301         if (pDeliverRep->paramInd & 0x01)
302                 pTpdu[offset++] = pDeliverRep->pid;
303
304         // TP-DCS
305         if (pDeliverRep->paramInd & 0x02)
306         {
307                 int length = 0;
308
309                 char* dcs = NULL;
310                 AutoPtr<char> dcsBuf(&dcs);
311
312                 length = SmsPluginParamCodec::encodeDCS(&pDeliverRep->dcs, &dcs);
313                 memcpy(&(pTpdu[offset]), dcs, length);
314
315                 offset += length;
316         }
317
318         // TP-UDL & TP-UD
319         if (pDeliverRep->paramInd & 0x04)
320         {
321                 int encodeSize = 0;
322
323                 encodeSize = SmsPluginUDCodec::encodeUserData(&(pDeliverRep->userData), pDeliverRep->dcs.codingScheme, &(pTpdu[offset]));
324
325                 MSG_DEBUG("encodeSize : %d", encodeSize);
326
327                 offset += encodeSize;
328         }
329
330         pTpdu[offset] = '\0';
331
332         return offset;
333 }
334
335
336 int SmsPluginTpduCodec::encodeStatusReport(const SMS_STATUS_REPORT_S *pStatusRep, char *pTpdu)
337 {
338         int offset = 0, length = 0;
339
340         char* address = NULL;
341         AutoPtr<char> addressBuf(&address);
342
343         char* scts = NULL;
344         AutoPtr<char> sctsBuf(&scts);
345
346         char* dt = NULL;
347         AutoPtr<char> dtBuf(&dt);
348
349         // TP-MTI : 10
350         pTpdu[offset] = 0x02;
351
352         // TP-MMS
353         if (pStatusRep->bMoreMsg == true)
354                 pTpdu[offset] |= 0x04;
355
356         // TP-SRQ
357         if (pStatusRep->bStatusReport == true)
358                 pTpdu[offset] |= 0x20;
359
360         // TP-UDHI
361         if (pStatusRep->bHeaderInd == true)
362                 pTpdu[offset] |= 0x40;
363
364         offset++;
365
366         // TP-MR
367         pTpdu[offset++] = pStatusRep->msgRef;
368
369         // TP-RA
370         length = SmsPluginParamCodec::encodeAddress(&pStatusRep->recipAddress, &address);
371         memcpy(&(pTpdu[offset]), address, length);
372         offset += length;
373
374         // TP-SCTS
375         length = SmsPluginParamCodec::encodeTime(&pStatusRep->timeStamp, &scts);
376         memcpy(&(pTpdu[offset]), scts, length);
377         offset += length;
378
379         // TP-DT
380         length = SmsPluginParamCodec::encodeTime(&pStatusRep->dischargeTime, &dt);
381         memcpy(&(pTpdu[offset]), dt, length);
382         offset += length;
383
384         // TP-Status
385         pTpdu[offset++] = pStatusRep->status;
386
387         // TP-PI
388         pTpdu[offset++] = pStatusRep->paramInd;
389
390         // TP-PID
391         if (pStatusRep->paramInd & 0x01)
392                 pTpdu[offset++] = pStatusRep->pid;
393
394         // TP-DCS
395         if (pStatusRep->paramInd & 0x02)
396         {
397                 int length = 0;
398
399                 char* dcs = NULL;
400                 AutoPtr<char> dcsBuf(&dcs);
401
402                 length = SmsPluginParamCodec::encodeDCS(&pStatusRep->dcs, &dcs);
403                 memcpy(&(pTpdu[offset]), dcs, length);
404
405                 offset += length;
406         }
407
408         // TP-UDL & TP-UD
409         if (pStatusRep->paramInd & 0x04)
410         {
411                 int encodeSize = 0;
412
413                 encodeSize = SmsPluginUDCodec::encodeUserData(&(pStatusRep->userData), pStatusRep->dcs.codingScheme, &(pTpdu[offset]));
414
415                 MSG_DEBUG("encodeSize : %d", encodeSize);
416
417                 offset += encodeSize;
418         }
419
420         pTpdu[offset] = '\0';
421
422         return offset;
423 }
424
425
426 int SmsPluginTpduCodec::decodeSubmit(const unsigned char *pTpdu, int TpduLen, SMS_SUBMIT_S *pSubmit)
427 {
428         int offset = 0, udLen = 0;
429
430         // TP-RD
431         if (pTpdu[offset] & 0x04)
432                 pSubmit->bRejectDup = false;
433         else
434                 pSubmit->bRejectDup = true;
435
436         // TP-VPF
437         pSubmit->vpf = (SMS_VPF_T)(pTpdu[offset] & 0x18);
438
439         // TP-SRR
440         if (pTpdu[offset] & 0x20)
441                 pSubmit->bStatusReport = true;
442         else
443                 pSubmit->bStatusReport = false;
444
445         // TP-UDHI
446         if (pTpdu[offset] & 0x40)
447                 pSubmit->bHeaderInd = true;
448         else
449                 pSubmit->bHeaderInd = false;
450
451         // TP-RP
452         if (pTpdu[offset] & 0x80)
453                 pSubmit->bReplyPath = true;
454         else
455                 pSubmit->bReplyPath = false;
456
457         offset++;
458
459         // TP-MR
460         pSubmit->msgRef = pTpdu[offset++];
461
462         // TP-DA
463         offset += SmsPluginParamCodec::decodeAddress(pTpdu+offset, &(pSubmit->destAddress));
464
465         // TP-PID
466         pSubmit->pid = pTpdu[offset++];
467
468         // TP-DCS
469         offset += SmsPluginParamCodec::decodeDCS(pTpdu+offset, &(pSubmit->dcs));
470
471         // TP-VP
472         if (pSubmit->vpf != SMS_VPF_NOT_PRESENT)
473         {
474                 // Decode VP
475         }
476
477         // TP-UDL & TP-UD
478         udLen = SmsPluginUDCodec::decodeUserData(pTpdu+offset, TpduLen, pSubmit->bHeaderInd, pSubmit->dcs.codingScheme, &(pSubmit->userData));
479
480         return udLen;
481 }
482
483
484 int SmsPluginTpduCodec::decodeDeliver(const unsigned char *pTpdu, int TpduLen, SMS_DELIVER_S *pDeliver)
485 {
486         int offset = 0, udLen = 0;
487
488         // TP-MMS
489         if (pTpdu[offset] & 0x04)
490                 pDeliver->bMoreMsg = false;
491         else
492                 pDeliver->bMoreMsg = true;
493
494         // TP-SRI
495         if (pTpdu[offset] & 0x20)
496                 pDeliver->bStatusReport = true;
497         else
498                 pDeliver->bStatusReport = false;
499
500         // TP-UDHI
501         if (pTpdu[offset] & 0x40)
502                 pDeliver->bHeaderInd = true;
503         else
504                 pDeliver->bHeaderInd = false;
505
506         // TP-RP
507         if (pTpdu[offset] & 0x80)
508                 pDeliver->bReplyPath = true;
509         else
510                 pDeliver->bReplyPath = false;
511
512         offset++;
513
514         // TP-OA
515         offset += SmsPluginParamCodec::decodeAddress(&pTpdu[offset], &(pDeliver->originAddress));
516
517         // TP-PID
518         pDeliver->pid = pTpdu[offset++];
519
520         // TP-DCS
521         offset += SmsPluginParamCodec::decodeDCS(&pTpdu[offset], &(pDeliver->dcs));
522
523         // TP-SCTS
524         offset += SmsPluginParamCodec::decodeTime(&pTpdu[offset], &(pDeliver->timeStamp));
525
526         // TP-UD
527         udLen = SmsPluginUDCodec::decodeUserData(&pTpdu[offset], TpduLen, pDeliver->bHeaderInd, pDeliver->dcs.codingScheme, &(pDeliver->userData), &(pDeliver->udData));
528
529         return udLen;
530 }
531
532
533 int SmsPluginTpduCodec::decodeStatusReport(const unsigned char *pTpdu, int TpduLen, SMS_STATUS_REPORT_S *pStatusRep)
534 {
535         int offset = 0, udLen = 0;
536
537         char* address = NULL;
538         AutoPtr<char> addressBuf(&address);
539
540         char* scts = NULL;
541         AutoPtr<char> sctsBuf(&scts);
542
543         char* dt = NULL;
544         AutoPtr<char> dtBuf(&dt);
545
546         // TP-MMS
547         if (pTpdu[offset] & 0x04)
548                 pStatusRep->bMoreMsg = false;
549         else
550                 pStatusRep->bMoreMsg = true;
551
552         // TP-SRQ
553         if (pTpdu[offset] & 0x20)
554                 pStatusRep->bStatusReport = true;
555         else
556                 pStatusRep->bStatusReport = false;
557
558         // TP-UDHI
559         if (pTpdu[offset] & 0x40)
560                 pStatusRep->bHeaderInd = true;
561         else
562                 pStatusRep->bHeaderInd = false;
563
564         offset++;
565
566         // TP-MR
567         pStatusRep->msgRef = pTpdu[offset++];
568
569         // TP-RA
570         offset += SmsPluginParamCodec::decodeAddress(&pTpdu[offset], &(pStatusRep->recipAddress));
571
572         // TP-SCTS
573         // Decode timestamp
574         offset += SmsPluginParamCodec::decodeTime(&pTpdu[offset], &(pStatusRep->timeStamp));
575
576         // TP-DT
577         // Decode timestamp
578         offset += SmsPluginParamCodec::decodeTime(&pTpdu[offset], &(pStatusRep->dischargeTime));
579
580         // TP-Status
581         pStatusRep->status = pTpdu[offset++];
582
583         // TP-PI
584         pStatusRep->paramInd = pTpdu[offset++];
585
586         // No Parameters
587         if (pStatusRep->paramInd == 0)
588         {
589                 pStatusRep->pid = SMS_PID_NORMAL;
590
591                 pStatusRep->dcs.bCompressed = false;
592                 pStatusRep->dcs.bMWI = false;
593                 pStatusRep->dcs.bIndActive = false;
594
595                 pStatusRep->dcs.msgClass = MSG_CLASS_NONE;
596                 pStatusRep->dcs.codingScheme = SMS_CHARSET_7BIT;
597                 pStatusRep->dcs.codingGroup = SMS_GROUP_GENERAL;
598                 pStatusRep->dcs.indType = SMS_OTHER_INDICATOR;
599
600                 pStatusRep->userData.headerCnt = 0;
601                 pStatusRep->userData.length = 0;
602                 memset(pStatusRep->userData.data, 0x00, MAX_USER_DATA_LEN+1);
603         }
604
605         // TP-PID
606         if (pStatusRep->paramInd & 0x01)
607                 pStatusRep->pid = pTpdu[offset++];
608
609         // TP-DCS
610         if (pStatusRep->paramInd & 0x02)
611         {
612                 offset += SmsPluginParamCodec::decodeDCS(&pTpdu[offset], &(pStatusRep->dcs));
613         }
614
615         // TP-UDL & TP-UD
616         if (pStatusRep->paramInd & 0x04)
617         {
618                 // Decode User Data
619                 udLen = SmsPluginUDCodec::decodeUserData(&pTpdu[offset], TpduLen, pStatusRep->bHeaderInd, pStatusRep->dcs.codingScheme, &(pStatusRep->userData));
620         }
621
622         return udLen;
623 }
624