2.0_beta
[framework/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         // TP-MMS
475         if (pTpdu[offset] & 0x04)
476                 pDeliver->bMoreMsg = false;
477         else
478                 pDeliver->bMoreMsg = true;
479
480         // TP-SRI
481         if (pTpdu[offset] & 0x20)
482                 pDeliver->bStatusReport = true;
483         else
484                 pDeliver->bStatusReport = false;
485
486         // TP-UDHI
487         if (pTpdu[offset] & 0x40)
488                 pDeliver->bHeaderInd = true;
489         else
490                 pDeliver->bHeaderInd = false;
491
492         // TP-RP
493         if (pTpdu[offset] & 0x80)
494                 pDeliver->bReplyPath = true;
495         else
496                 pDeliver->bReplyPath = false;
497
498         offset++;
499
500         // TP-OA
501         offset += SmsPluginParamCodec::decodeAddress(&pTpdu[offset], &(pDeliver->originAddress));
502
503         // TP-PID
504         pDeliver->pid = pTpdu[offset++];
505
506         // TP-DCS
507         offset += SmsPluginParamCodec::decodeDCS(&pTpdu[offset], &(pDeliver->dcs));
508
509         // TP-SCTS
510         offset += SmsPluginParamCodec::decodeTime(&pTpdu[offset], &(pDeliver->timeStamp));
511
512         // TP-UD
513         udLen = SmsPluginUDCodec::decodeUserData(&pTpdu[offset], TpduLen, pDeliver->bHeaderInd, pDeliver->dcs.codingScheme, &(pDeliver->userData), &(pDeliver->udData));
514
515         return udLen;
516 }
517
518
519 int SmsPluginTpduCodec::decodeStatusReport(const unsigned char *pTpdu, int TpduLen, SMS_STATUS_REPORT_S *pStatusRep)
520 {
521         int offset = 0, udLen = 0;
522
523         char* address = NULL;
524         AutoPtr<char> addressBuf(&address);
525
526         char* scts = NULL;
527         AutoPtr<char> sctsBuf(&scts);
528
529         char* dt = NULL;
530         AutoPtr<char> dtBuf(&dt);
531
532         // TP-MMS
533         if (pTpdu[offset] & 0x04)
534                 pStatusRep->bMoreMsg = false;
535         else
536                 pStatusRep->bMoreMsg = true;
537
538         // TP-SRQ
539         if (pTpdu[offset] & 0x20)
540                 pStatusRep->bStatusReport = true;
541         else
542                 pStatusRep->bStatusReport = false;
543
544         // TP-UDHI
545         if (pTpdu[offset] & 0x40)
546                 pStatusRep->bHeaderInd = true;
547         else
548                 pStatusRep->bHeaderInd = false;
549
550         offset++;
551
552         // TP-MR
553         pStatusRep->msgRef = pTpdu[offset++];
554
555         // TP-RA
556         offset += SmsPluginParamCodec::decodeAddress(&pTpdu[offset], &(pStatusRep->recipAddress));
557
558         // TP-SCTS
559         // Decode timestamp
560         offset += SmsPluginParamCodec::decodeTime(&pTpdu[offset], &(pStatusRep->timeStamp));
561
562         // TP-DT
563         // Decode timestamp
564         offset += SmsPluginParamCodec::decodeTime(&pTpdu[offset], &(pStatusRep->dischargeTime));
565
566         // TP-Status
567         pStatusRep->status = pTpdu[offset++];
568
569         // TP-PI
570         pStatusRep->paramInd = pTpdu[offset++];
571
572         // No Parameters
573         if (pStatusRep->paramInd == 0)
574         {
575                 pStatusRep->pid = SMS_PID_NORMAL;
576
577                 pStatusRep->dcs.bCompressed = false;
578                 pStatusRep->dcs.bMWI = false;
579                 pStatusRep->dcs.bIndActive = false;
580
581                 pStatusRep->dcs.msgClass = MSG_CLASS_NONE;
582                 pStatusRep->dcs.codingScheme = SMS_CHARSET_7BIT;
583                 pStatusRep->dcs.codingGroup = SMS_GROUP_GENERAL;
584                 pStatusRep->dcs.indType = SMS_OTHER_INDICATOR;
585
586                 pStatusRep->userData.headerCnt = 0;
587                 pStatusRep->userData.length = 0;
588                 memset(pStatusRep->userData.data, 0x00, MAX_USER_DATA_LEN+1);
589         }
590
591         // TP-PID
592         if (pStatusRep->paramInd & 0x01)
593                 pStatusRep->pid = pTpdu[offset++];
594
595         // TP-DCS
596         if (pStatusRep->paramInd & 0x02)
597         {
598                 offset += SmsPluginParamCodec::decodeDCS(&pTpdu[offset], &(pStatusRep->dcs));
599         }
600
601         // TP-UDL & TP-UD
602         if (pStatusRep->paramInd & 0x04)
603         {
604                 // Decode User Data
605                 udLen = SmsPluginUDCodec::decodeUserData(&pTpdu[offset], TpduLen, pStatusRep->bHeaderInd, pStatusRep->dcs.codingScheme, &(pStatusRep->userData));
606         }
607
608         return udLen;
609 }
610