2 * Copyright 2012-2013 Samsung Electronics Co., Ltd
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
8 * http://www.tizenopensource.org/license
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.
21 #include "MsgCppTypes.h"
22 #include "MsgException.h"
23 #include "MsgGconfWrapper.h"
25 #include "SmsPluginTpduCodec.h"
26 #include "SmsPluginParamCodec.h"
27 #include "SmsPluginUDCodec.h"
30 /*==================================================================================================
31 IMPLEMENTATION OF SmsPluginTpduCodec - Member Functions
32 ==================================================================================================*/
33 SmsPluginTpduCodec::SmsPluginTpduCodec()
40 SmsPluginTpduCodec::~SmsPluginTpduCodec()
47 int SmsPluginTpduCodec::encodeTpdu(const SMS_TPDU_S *pSmsTpdu, char *pTpdu)
51 switch (pSmsTpdu->tpduType)
54 tpduLen = encodeSubmit(&(pSmsTpdu->data.submit), pTpdu);
57 case SMS_TPDU_DELIVER:
58 tpduLen = encodeDeliver(&(pSmsTpdu->data.deliver), pTpdu);
61 case SMS_TPDU_DELIVER_REP:
62 tpduLen = encodeDeliverReport(&(pSmsTpdu->data.deliverRep), pTpdu);
65 case SMS_TPDU_STATUS_REP:
66 tpduLen = encodeStatusReport(&(pSmsTpdu->data.statusRep), pTpdu);
74 int SmsPluginTpduCodec::decodeTpdu(const unsigned char *pTpdu, int TpduLen, SMS_TPDU_S *pSmsTpdu)
78 char mti = pTpdu[0] & 0x03;
83 pSmsTpdu->tpduType = SMS_TPDU_DELIVER;
84 decodeLen = decodeDeliver(pTpdu, TpduLen, &(pSmsTpdu->data.deliver));
88 pSmsTpdu->tpduType = SMS_TPDU_SUBMIT;
89 decodeLen = decodeSubmit(pTpdu, TpduLen, &(pSmsTpdu->data.submit));
93 pSmsTpdu->tpduType = SMS_TPDU_STATUS_REP;
94 decodeLen = decodeStatusReport(pTpdu, TpduLen, &(pSmsTpdu->data.statusRep));
102 int SmsPluginTpduCodec::encodeSubmit(const SMS_SUBMIT_S *pSubmit, char *pTpdu)
104 int offset = 0, length = 0, encodeSize = 0;
106 char* address = NULL;
107 AutoPtr<char> addressBuf(&address);
110 AutoPtr<char> dcsBuf(&dcs);
113 AutoPtr<char> vpBuf(&vpTime);
116 pTpdu[offset] = 0x01;
119 if(pSubmit->bRejectDup == false)
120 pTpdu[offset] |= 0x04;
123 switch (pSubmit->vpf)
125 case SMS_VPF_NOT_PRESENT:
127 case SMS_VPF_ENHANCED:
128 pTpdu[offset] |= 0x08;
130 case SMS_VPF_RELATIVE:
131 pTpdu[offset] |= 0x10;
133 case SMS_VPF_ABSOLUTE:
134 pTpdu[offset] |= 0x18;
141 if (pSubmit->bStatusReport == true)
142 pTpdu[offset] |= 0x20;
144 MSG_DEBUG("TP-SRR pSubmit->bStatusReport : %d.", pSubmit->bStatusReport);
147 if (pSubmit->bHeaderInd == true)
148 pTpdu[offset] |= 0x40;
151 if (pSubmit->bReplyPath == true)
152 pTpdu[offset] |= 0x80;
157 pTpdu[offset++] = pSubmit->msgRef;
159 MSG_DEBUG("TP-MR pSubmit->msgRef : %d.", pSubmit->msgRef);
162 length = SmsPluginParamCodec::encodeAddress(&pSubmit->destAddress, &address);
163 memcpy(&(pTpdu[offset]), address, length);
166 MSG_DEBUG("TP-DA length : %d.", length);
169 pTpdu[offset++] = pSubmit->pid;
171 MSG_DEBUG("TP-PID pSubmit->pid : %d.", pSubmit->pid);
174 length = SmsPluginParamCodec::encodeDCS(&pSubmit->dcs, &dcs);
175 memcpy(&(pTpdu[offset]), dcs, length);
178 MSG_DEBUG("TP-DCS length : %d.", length);
181 if (pSubmit->vpf != SMS_VPF_NOT_PRESENT)
183 length = SmsPluginParamCodec::encodeTime(&pSubmit->validityPeriod, &vpTime);
187 memcpy(&(pTpdu[offset]), vpTime, length);
192 encodeSize = SmsPluginUDCodec::encodeUserData(&(pSubmit->userData), pSubmit->dcs.codingScheme, &(pTpdu[offset]));
194 MSG_DEBUG("encodeSize : %d", encodeSize);
196 offset += encodeSize;
202 int SmsPluginTpduCodec::encodeDeliver(const SMS_DELIVER_S *pDeliver, char *pTpdu)
204 int offset = 0, length = 0, encodeSize = 0;
206 char* address = NULL;
207 AutoPtr<char> addressBuf(&address);
210 AutoPtr<char> dcsBuf(&dcs);
213 AutoPtr<char> timeBuf(&scts);
216 pTpdu[offset] = 0x00;
219 if (pDeliver->bMoreMsg == false)
220 pTpdu[offset] |= 0x04;
223 if (pDeliver->bStatusReport == true)
224 pTpdu[offset] |= 0x20;
227 if (pDeliver->bHeaderInd == true)
228 pTpdu[offset] |= 0x40;
231 if (pDeliver->bReplyPath == true)
232 pTpdu[offset] |= 0x80;
237 length = SmsPluginParamCodec::encodeAddress(&pDeliver->originAddress, &address);
238 memcpy(&(pTpdu[offset]), address, length);
242 pTpdu[offset++] = pDeliver->pid;
245 length = SmsPluginParamCodec::encodeDCS(&pDeliver->dcs, &dcs);
246 memcpy(&(pTpdu[offset]), dcs, length);
250 length = SmsPluginParamCodec::encodeTime(&pDeliver->timeStamp, &scts);
251 memcpy(&(pTpdu[offset]), scts, length);
255 encodeSize = SmsPluginUDCodec::encodeUserData(&(pDeliver->userData), pDeliver->dcs.codingScheme, &(pTpdu[offset]));
257 MSG_DEBUG("encodeSize : %d", encodeSize);
259 offset += encodeSize;
265 int SmsPluginTpduCodec::encodeDeliverReport(const SMS_DELIVER_REPORT_S *pDeliverRep, char *pTpdu)
270 pTpdu[offset] = 0x00;
273 if (pDeliverRep->bHeaderInd == true)
274 pTpdu[offset] |= 0x40;
279 if (pDeliverRep->reportType == SMS_REPORT_NEGATIVE)
280 pTpdu[offset++] = pDeliverRep->failCause;
283 pTpdu[offset++] = pDeliverRep->paramInd;
286 if (pDeliverRep->paramInd & 0x01)
287 pTpdu[offset++] = pDeliverRep->pid;
290 if (pDeliverRep->paramInd & 0x02)
295 AutoPtr<char> dcsBuf(&dcs);
297 length = SmsPluginParamCodec::encodeDCS(&pDeliverRep->dcs, &dcs);
298 memcpy(&(pTpdu[offset]), dcs, length);
304 if (pDeliverRep->paramInd & 0x04)
308 encodeSize = SmsPluginUDCodec::encodeUserData(&(pDeliverRep->userData), pDeliverRep->dcs.codingScheme, &(pTpdu[offset]));
310 MSG_DEBUG("encodeSize : %d", encodeSize);
312 offset += encodeSize;
315 pTpdu[offset] = '\0';
321 int SmsPluginTpduCodec::encodeStatusReport(const SMS_STATUS_REPORT_S *pStatusRep, char *pTpdu)
323 int offset = 0, length = 0;
325 char* address = NULL;
326 AutoPtr<char> addressBuf(&address);
329 AutoPtr<char> sctsBuf(&scts);
332 AutoPtr<char> dtBuf(&dt);
335 pTpdu[offset] = 0x02;
338 if (pStatusRep->bMoreMsg == true)
339 pTpdu[offset] |= 0x04;
342 if (pStatusRep->bStatusReport == true)
343 pTpdu[offset] |= 0x20;
346 if (pStatusRep->bHeaderInd == true)
347 pTpdu[offset] |= 0x40;
352 pTpdu[offset++] = pStatusRep->msgRef;
355 length = SmsPluginParamCodec::encodeAddress(&pStatusRep->recipAddress, &address);
356 memcpy(&(pTpdu[offset]), address, length);
360 length = SmsPluginParamCodec::encodeTime(&pStatusRep->timeStamp, &scts);
361 memcpy(&(pTpdu[offset]), scts, length);
365 length = SmsPluginParamCodec::encodeTime(&pStatusRep->dischargeTime, &dt);
366 memcpy(&(pTpdu[offset]), dt, length);
370 pTpdu[offset++] = pStatusRep->status;
373 pTpdu[offset++] = pStatusRep->paramInd;
376 if (pStatusRep->paramInd & 0x01)
377 pTpdu[offset++] = pStatusRep->pid;
380 if (pStatusRep->paramInd & 0x02)
385 AutoPtr<char> dcsBuf(&dcs);
387 length = SmsPluginParamCodec::encodeDCS(&pStatusRep->dcs, &dcs);
388 memcpy(&(pTpdu[offset]), dcs, length);
394 if (pStatusRep->paramInd & 0x04)
398 encodeSize = SmsPluginUDCodec::encodeUserData(&(pStatusRep->userData), pStatusRep->dcs.codingScheme, &(pTpdu[offset]));
400 MSG_DEBUG("encodeSize : %d", encodeSize);
402 offset += encodeSize;
405 pTpdu[offset] = '\0';
411 int SmsPluginTpduCodec::decodeSubmit(const unsigned char *pTpdu, int TpduLen, SMS_SUBMIT_S *pSubmit)
413 int offset = 0, udLen = 0;
416 if (pTpdu[offset] & 0x04)
417 pSubmit->bRejectDup = false;
419 pSubmit->bRejectDup = true;
422 pSubmit->vpf = (SMS_VPF_T)(pTpdu[offset] & 0x18);
425 if (pTpdu[offset] & 0x20)
426 pSubmit->bStatusReport = true;
428 pSubmit->bStatusReport = false;
431 if (pTpdu[offset] & 0x40)
432 pSubmit->bHeaderInd = true;
434 pSubmit->bHeaderInd = false;
437 if (pTpdu[offset] & 0x80)
438 pSubmit->bReplyPath = true;
440 pSubmit->bReplyPath = false;
445 pSubmit->msgRef = pTpdu[offset++];
448 offset += SmsPluginParamCodec::decodeAddress(pTpdu+offset, &(pSubmit->destAddress));
451 pSubmit->pid = pTpdu[offset++];
454 offset += SmsPluginParamCodec::decodeDCS(pTpdu+offset, &(pSubmit->dcs));
457 if (pSubmit->vpf != SMS_VPF_NOT_PRESENT)
463 udLen = SmsPluginUDCodec::decodeUserData(pTpdu+offset, TpduLen, pSubmit->bHeaderInd, pSubmit->dcs.codingScheme, &(pSubmit->userData));
469 int SmsPluginTpduCodec::decodeDeliver(const unsigned char *pTpdu, int TpduLen, SMS_DELIVER_S *pDeliver)
471 int offset = 0, udLen = 0;
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]);
479 MSG_DEBUG("Deliver TPDU.");
480 MSG_DEBUG("[%s]", tpduTmp);
484 if (pTpdu[offset] & 0x04)
485 pDeliver->bMoreMsg = false;
487 pDeliver->bMoreMsg = true;
490 if (pTpdu[offset] & 0x20)
491 pDeliver->bStatusReport = true;
493 pDeliver->bStatusReport = false;
496 if (pTpdu[offset] & 0x40)
497 pDeliver->bHeaderInd = true;
499 pDeliver->bHeaderInd = false;
502 if (pTpdu[offset] & 0x80)
503 pDeliver->bReplyPath = true;
505 pDeliver->bReplyPath = false;
510 offset += SmsPluginParamCodec::decodeAddress(&pTpdu[offset], &(pDeliver->originAddress));
513 pDeliver->pid = pTpdu[offset++];
516 offset += SmsPluginParamCodec::decodeDCS(&pTpdu[offset], &(pDeliver->dcs));
519 offset += SmsPluginParamCodec::decodeTime(&pTpdu[offset], &(pDeliver->timeStamp));
522 udLen = SmsPluginUDCodec::decodeUserData(&pTpdu[offset], TpduLen, pDeliver->bHeaderInd, pDeliver->dcs.codingScheme, &(pDeliver->userData), &(pDeliver->udData));
528 int SmsPluginTpduCodec::decodeStatusReport(const unsigned char *pTpdu, int TpduLen, SMS_STATUS_REPORT_S *pStatusRep)
531 printf("\n\n[decodeStatusReport] pTpdu data - Length [%d]\n", TpduLen);
533 for (int i = 0; i < TpduLen; i++)
535 printf(" [%02x]", pTpdu[i]);
540 int offset = 0, udLen = 0;
542 char* address = NULL;
543 AutoPtr<char> addressBuf(&address);
546 AutoPtr<char> sctsBuf(&scts);
549 AutoPtr<char> dtBuf(&dt);
552 if (pTpdu[offset] & 0x04)
553 pStatusRep->bMoreMsg = false;
555 pStatusRep->bMoreMsg = true;
558 if (pTpdu[offset] & 0x20)
559 pStatusRep->bStatusReport = true;
561 pStatusRep->bStatusReport = false;
564 if (pTpdu[offset] & 0x40)
565 pStatusRep->bHeaderInd = true;
567 pStatusRep->bHeaderInd = false;
572 pStatusRep->msgRef = pTpdu[offset++];
575 offset += SmsPluginParamCodec::decodeAddress(&pTpdu[offset], &(pStatusRep->recipAddress));
579 offset += SmsPluginParamCodec::decodeTime(&pTpdu[offset], &(pStatusRep->timeStamp));
583 offset += SmsPluginParamCodec::decodeTime(&pTpdu[offset], &(pStatusRep->dischargeTime));
586 pStatusRep->status = pTpdu[offset++];
589 pStatusRep->paramInd = pTpdu[offset++];
592 if (pStatusRep->paramInd == 0)
594 pStatusRep->pid = SMS_PID_NORMAL;
596 pStatusRep->dcs.bCompressed = false;
597 pStatusRep->dcs.bMWI = false;
598 pStatusRep->dcs.bIndActive = false;
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;
605 pStatusRep->userData.headerCnt = 0;
606 pStatusRep->userData.length = 0;
607 memset(pStatusRep->userData.data, 0x00, MAX_USER_DATA_LEN+1);
611 if (pStatusRep->paramInd & 0x01)
612 pStatusRep->pid = pTpdu[offset++];
615 if (pStatusRep->paramInd & 0x02)
617 offset += SmsPluginParamCodec::decodeDCS(&pTpdu[offset], &(pStatusRep->dcs));
621 if (pStatusRep->paramInd & 0x04)
624 udLen = SmsPluginUDCodec::decodeUserData(&pTpdu[offset], TpduLen, pStatusRep->bHeaderInd, pStatusRep->dcs.codingScheme, &(pStatusRep->userData));