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