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