5a92d5b34304ef242b065676c7bb6bd47bcb416a
[platform/core/messaging/msg-service.git] / plugin / sms_plugin / 3gpp2 / Sms3gpp2Codec.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 "MsgTextConvert.h"
22
23 #include "Sms3gpp2Types.h"
24 #include "Sms3gpp2ParamCodec.h"
25 #include "Sms3gpp2Codec.h"
26
27
28 Sms3gpp2MsgCodec* Sms3gpp2MsgCodec::pInstance = NULL;
29
30 Sms3gpp2MsgCodec::Sms3gpp2MsgCodec()
31 {
32 }
33
34
35 Sms3gpp2MsgCodec::~Sms3gpp2MsgCodec()
36 {
37 }
38
39
40 Sms3gpp2MsgCodec* Sms3gpp2MsgCodec::instance()
41 {
42         if (!pInstance) {
43                 pInstance = new Sms3gpp2MsgCodec();
44         }
45
46         return pInstance;
47 }
48
49
50 void _shiftNBit(unsigned char *src, unsigned int n_bytes, unsigned int n_shift_bit)
51 {
52         char tmp;
53
54     for (unsigned int index = 1; index < n_bytes; index++) {
55             tmp = src[index] >> (8 - n_shift_bit);
56             src[index-1] |= tmp;
57             src[index] = src[index] << n_shift_bit;
58     }
59 }
60
61
62 void _shiftNBit_for_decode(unsigned char *src, unsigned int n_bytes, unsigned int n_shift_bit)
63 {
64      for (unsigned int index = 0; index < n_bytes; index++) {
65          src[index]<<=n_shift_bit;
66          src[index] |= (src[index+1] >> (8 - n_shift_bit));
67          }
68 }
69
70
71 void _copy_short_to_char(unsigned char *dest, const unsigned short *src)
72 {
73         dest[0] = (0xff00 & *src) >> 8;
74         dest[1] = 0x00ff & *src;
75 }
76
77
78 void _copy_char_to_short(unsigned short *dest, const unsigned char *src)
79 {
80      *dest = src[0]*256 + src[1];
81 }
82
83
84 unsigned char _convert_to_BCD(unsigned char val)
85 {
86         unsigned char ret = 0x00;
87         ret = ((val/10) << 4) | (val%10);
88         return ret;
89 }
90
91
92 int UnpackGSM7bitData(unsigned char *src, unsigned char *dest, unsigned int dataLen)
93 {
94         unsigned int srcIdx = 0, dstIdx = 0, shift = 0;
95
96         MSG_DEBUG("dataLen = %d", dataLen);
97
98         for (; dstIdx < dataLen; dstIdx++) {
99                 if (shift == 0) {
100                         dest[dstIdx] = src[srcIdx] & 0x7F;
101
102                         shift = 7;
103                         srcIdx++;
104                         dstIdx++;
105
106                         if (dstIdx >= dataLen)
107                                 break;
108                 }
109
110                 if (shift > 0) {
111                         dest[dstIdx] = (src[srcIdx-1] >> shift) + (src[srcIdx] << (8 - shift));
112
113                         dest[dstIdx] &= 0x7F;
114
115                         shift--;
116
117                         if (shift > 0)
118                                 srcIdx++;
119                 }
120         }
121
122         return dstIdx;
123 }
124
125
126 int PackGSM7bitData(const unsigned char *pUserData, unsigned char *pPackData, int dataLen)
127 {
128         int srcIdx = 0, dstIdx = 0, shift = 0;
129
130         while (srcIdx < dataLen) {
131                 if (shift == 0) {
132                         pPackData[dstIdx] = pUserData[srcIdx];
133
134                         shift = 7;
135                         srcIdx++;
136                         dstIdx++;
137
138                         if (srcIdx >= dataLen)
139                                 break;
140                 }
141
142                 if (shift > 1) {
143                         pPackData[dstIdx-1] |= pUserData[srcIdx] << shift;
144                         pPackData[dstIdx] = pUserData[srcIdx] >> (8-shift);
145                         shift--;
146
147                         srcIdx++;
148                         dstIdx++;
149                 } else if (shift == 1) {
150                         pPackData[dstIdx-1] |= pUserData[srcIdx] << shift;
151
152                         srcIdx++;
153
154                         shift--;
155                 }
156         }
157
158         return dstIdx;
159 }
160
161
162 bool Sms3gpp2MsgCodec::checkInvalidPDU(const unsigned char *p_pkg_str, const int p_pkg_len)
163 {
164         MSG_BEGIN();
165         int offset = 0;
166
167         if (!(p_pkg_str[offset] == 0x00 || p_pkg_str[offset] == 0x01 || p_pkg_str[offset] == 0x02)) {
168                 MSG_WARN("Invalid PDU : Message Type [%2x]", p_pkg_str[offset]);
169                 return false;
170         }
171
172         offset++;
173
174         while (offset < p_pkg_len) {
175                 switch (p_pkg_str[offset]) {
176                 case 0x00:
177                 case 0x01:
178                 case 0x02:
179                 case 0x03:
180                 case 0x04:
181                 case 0x05:
182                 case 0x06:
183                 case 0x07:
184                 case 0x08:
185                         offset += (p_pkg_str[offset+1]+2);
186                         break;
187                 default:
188                         MSG_WARN("Invalid PDU : Parameter ID [%2x], offset [%d]", p_pkg_str[offset], offset);
189                         return false;
190                 }
191         }
192
193         if (offset != p_pkg_len)
194                 return false;
195
196         MSG_END();
197         return true;
198 }
199
200
201 int Sms3gpp2MsgCodec::encodeMsg(const sms_3gpp2_trans_msg_s *p_msg, unsigned char *p_pkg_str)
202 {
203         MSG_BEGIN();
204
205         int encode_size = 0;
206
207         switch (p_msg->type) {
208         case SMS_TRANS_P2P_MSG:
209                 encode_size = encodeP2PMsg(&(p_msg->data.p2p_msg), p_pkg_str);
210                 break;
211         case SMS_TRANS_BROADCAST_MSG:
212                 encode_size = encodeCBMsg(&(p_msg->data.cb_msg), p_pkg_str);
213                 break;
214         case SMS_TRANS_ACK_MSG:
215                 encode_size = encodeAckMsg(&(p_msg->data.ack_msg), p_pkg_str);
216                 break;
217         default:
218                 break;
219         }
220
221         MSG_END();
222
223         return encode_size;
224 }
225
226
227 int Sms3gpp2MsgCodec::encodeP2PMsg(const sms_3gpp2_trans_p2p_msg_s *p_msg, unsigned char *p_pkg_str)
228 {
229         MSG_BEGIN();
230
231         int offset = 0, encode_size = 0;
232         int addr_len = 0;
233         int index = 0, len_index = 0;
234
235         p_pkg_str[offset++] = SMS_TRANS_P2P_MSG;
236
237         /* 1. teleservice id */
238         p_pkg_str[offset++] = SMS_TRANS_PARAM_TELESVC_IDENTIFIER;
239         p_pkg_str[offset++] = 2;
240         /* fixed */
241         /* memcpy(p_pkg_str+offset, &(p_msg->telesvc_id), sizeof(sms_trans_telesvc_id_t)); */
242         _copy_short_to_char(p_pkg_str+offset, &(p_msg->telesvc_id));
243         offset += sizeof(sms_3gpp2_trans_telesvc_id_t);
244
245         /* 2. Service category */
246         if (p_msg->svc_ctg < SMS_TRANS_SVC_CTG_UNDEFINED) {
247                 p_pkg_str[offset++] = SMS_TRANS_PARAM_SERVICE_CATEGORY;
248                 p_pkg_str[offset++] = 0x02;
249                 /* fixed */
250                 _copy_short_to_char(&p_pkg_str[offset], &(p_msg->svc_ctg));
251         }
252
253         /* 3. Address */
254         p_pkg_str[offset++] = SMS_TRANS_PARAM_DEST_ADDRESS;
255
256         /* Will be set to param length */
257         len_index = offset++;
258
259         p_pkg_str[offset] = p_msg->address.digit_mode ? 0x80 : 0x00;
260         p_pkg_str[offset] |= (p_msg->address.number_mode ? 0x40 : 0x00);
261
262         if (p_msg->address.digit_mode == false) {
263                 index = offset++;
264                 p_pkg_str[offset++] = p_msg->address.addr_len;
265
266                 addr_len = Sms3gpp2ParamCodec::instance()->convertDigitToDTMF(p_msg->address.szData, p_msg->address.addr_len, 0, p_pkg_str+offset);
267
268                 for (int j=0; j < addr_len; j++) {
269                         MSG_DEBUG("ADDRESS 4BIT DTMF [%d] = [%02x]", j, p_pkg_str[offset+j]);
270                 }
271
272                 offset += addr_len;
273
274                 _shiftNBit(&p_pkg_str[index], offset-index+1, 6);
275         } else if (p_msg->address.digit_mode == true) {
276                 p_pkg_str[offset] |= p_msg->address.number_type << 3;
277
278                 if (p_msg->address.number_mode == false) {
279                         p_pkg_str[offset++] |= p_msg->address.number_plan >> 1;
280                         p_pkg_str[offset++] |= p_msg->address.number_plan << 7;
281                         index = offset-1;
282                         p_pkg_str[offset++] = p_msg->address.addr_len;
283
284                         memcpy(p_pkg_str+offset, p_msg->address.szData, p_msg->address.addr_len);
285
286                         offset += p_msg->address.addr_len;
287
288                         _shiftNBit(&p_pkg_str[index], offset-index+1, 7);
289                 } else if (p_msg->address.number_mode == true) {
290                         index = offset++;
291                         p_pkg_str[offset++] = p_msg->address.addr_len;
292                         memcpy(p_pkg_str+offset, p_msg->address.szData, p_msg->address.addr_len);
293
294                         offset += p_msg->address.addr_len;
295
296                         _shiftNBit(&p_pkg_str[index], offset-index+1, 3);
297                 }
298         }
299
300         p_pkg_str[len_index] = offset - len_index - 1 ;
301         MSG_DEBUG("Address subparam length field = [%d]", p_pkg_str[len_index]);
302
303         /* 4. Sub address (optional) */
304         if (p_msg->sub_address.addr_len > 0) {
305                 p_pkg_str[offset++] = SMS_TRANS_PARAM_ORG_SUB_ADDRESS;
306                 p_pkg_str[offset] = p_msg->sub_address.addr_len + 2;
307                 index = offset++;
308                 p_pkg_str[offset] |= p_msg->sub_address.type << 5;
309                 p_pkg_str[offset++] |= (p_msg->sub_address.odd ? 0x10 : 0x00);
310                 p_pkg_str[offset++] = p_msg->sub_address.addr_len;
311                 memcpy(p_pkg_str+offset, p_msg->sub_address.szData, p_msg->sub_address.addr_len);
312
313                 offset += p_msg->sub_address.addr_len;
314
315                 _shiftNBit(&p_pkg_str[index], offset-index+1, 4);
316         }
317
318         /* 5. Bearer reply option (optional) */
319         if (p_msg->reply_seq > 0) {
320                 p_pkg_str[offset++] = SMS_TRANS_PARAM_BEARER_REPLY_OPTION;
321                 p_pkg_str[offset++] = 1;
322                 p_pkg_str[offset++] = (unsigned char)(p_msg->reply_seq << 2);
323                 MSG_DEBUG("Reply sequnce number = [%d]", p_msg->reply_seq);
324         }
325
326         /* 6. Bearer data */
327     p_pkg_str[offset++] = SMS_TRANS_PARAM_BEARER_DATA;
328     /* PARAMETER_LEN field should be filled at the last part. */
329     index = offset++;
330
331         unsigned char *encode_data = &p_pkg_str[offset];
332
333         encode_size = encodeTelesvcMsg(&(p_msg->telesvc_msg), encode_data);
334         /* PARAMETER_LEN */
335         p_pkg_str[index] = encode_size;
336
337         offset += encode_size;
338
339         MSG_END();
340
341         return offset;
342 }
343
344
345 int Sms3gpp2MsgCodec::encodeCBMsg(const sms_3gpp2_trans_broadcast_msg_s *p_msg, unsigned char *p_pkg_str)
346 {
347         MSG_BEGIN();
348
349         int offset = 0, encode_size = 0;
350         int len_index = 0;
351
352         /* 1. Service Category(Mandatory) */
353         p_pkg_str[offset++] = SMS_TRANS_PARAM_SERVICE_CATEGORY;
354         p_pkg_str[offset++] = 0x02;
355         _copy_short_to_char(&p_pkg_str[offset], &p_msg->svc_ctg);
356
357
358         /* 2. Bearer Data(Optional) */
359         /* TODO: give condition */
360     p_pkg_str[offset++] = SMS_TRANS_PARAM_BEARER_DATA;
361     /* PARAMETER_LEN field should be filled at the last part. */
362         len_index = offset++;
363
364         unsigned char *encode_data = &p_pkg_str[offset];
365
366         encode_size = encodeTelesvcMsg(&(p_msg->telesvc_msg), encode_data);
367         /* PARAMETER_LEN */
368         p_pkg_str[len_index] = encode_size;
369
370         offset += encode_size;
371
372         MSG_END();
373
374         return offset;
375 }
376
377
378 int Sms3gpp2MsgCodec::encodeAckMsg(const sms_3gpp2_trans_ack_msg_s *p_msg, unsigned char *p_pkg_str)
379 {
380         MSG_BEGIN();
381
382         int offset = 0;
383         int addr_len = 0, len_index = 0;
384         int index = 0;
385
386         /* 1. Address */
387         p_pkg_str[offset++] = SMS_TRANS_PARAM_DEST_ADDRESS;
388
389         /* Will be set to param length */
390         len_index = offset++;
391
392         p_pkg_str[offset] = p_msg->address.digit_mode ? 0x80 : 0x00;
393         p_pkg_str[offset] |= (p_msg->address.number_mode ? 0x40 : 0x00);
394
395         index = offset++;
396
397         if (p_msg->address.digit_mode == false) {
398                 p_pkg_str[offset++] = p_msg->address.addr_len;
399
400                 addr_len = Sms3gpp2ParamCodec::instance()->convertDigitToDTMF(p_msg->address.szData, p_msg->address.addr_len, 0, p_pkg_str+offset);
401
402                 for (int j=0; j < addr_len; j++) {
403                         MSG_DEBUG("ADDRESS 4BIT DTMF [%d] = [%02x]", j, p_pkg_str[offset+j]);
404                 }
405
406                 offset += addr_len;
407
408                 _shiftNBit(&p_pkg_str[index], offset-index+1, 6);
409         } else if (p_msg->address.digit_mode == true) {
410                 p_pkg_str[offset] |= p_msg->address.number_type << 3;
411
412                 if (p_msg->address.number_mode == false) {
413                         p_pkg_str[offset++] |= p_msg->address.number_plan >> 1;
414                         p_pkg_str[offset++] |= p_msg->address.number_plan << 7;
415                         index++;
416                         p_pkg_str[offset++] = p_msg->address.addr_len;
417
418                         memcpy(p_pkg_str+offset, p_msg->address.szData, p_msg->address.addr_len);
419
420                         offset += p_msg->address.addr_len;
421
422                         _shiftNBit(&p_pkg_str[index], offset-index+1, 7);
423                 } else if (p_msg->address.number_mode == true) {
424                         p_pkg_str[++offset] = p_msg->address.addr_len;
425                         offset++;
426                         memcpy(p_pkg_str+offset, p_msg->address.szData, p_msg->address.addr_len);
427
428                         offset += p_msg->address.addr_len;
429
430                         _shiftNBit(&p_pkg_str[index], offset-index+1, 3);
431                 }
432         }
433
434         p_pkg_str[len_index] = offset - len_index - 1 ;
435         MSG_DEBUG("Address subparam length field = [%d]", p_pkg_str[len_index]);
436
437         /* 2. Sub address */
438         if (p_msg->sub_address.addr_len > 0) {
439                 p_pkg_str[offset++] = SMS_TRANS_PARAM_ORG_SUB_ADDRESS;
440                 p_pkg_str[offset] = p_msg->sub_address.addr_len + 2;
441                 index = offset++;
442                 p_pkg_str[offset] |= p_msg->sub_address.type << 5;
443                 p_pkg_str[offset++] |= (p_msg->sub_address.odd ? 0x10 : 0x00);
444                 p_pkg_str[offset++] = p_msg->sub_address.addr_len;
445                 memcpy(p_pkg_str+offset, p_msg->sub_address.szData, p_msg->sub_address.addr_len);
446
447                 offset += p_msg->sub_address.addr_len;
448
449                 _shiftNBit(&p_pkg_str[index], offset-index+1, 4);
450         }
451
452         /* 3. Cause code */
453         p_pkg_str[offset++] = SMS_TRANS_PARAM_CAUSE_CODES;
454         index = offset++;
455         p_pkg_str[offset] |= p_msg->cause_code.reply_seq << 2;
456         p_pkg_str[offset] |= p_msg->cause_code.error_class;
457         if (p_msg->cause_code.error_class != 0x0) {
458                 p_pkg_str[++offset] = p_msg->cause_code.cause_code;
459         }
460         p_pkg_str[index] = offset - index;
461
462         MSG_END();
463
464         return offset;
465 }
466
467
468 int Sms3gpp2MsgCodec::encodeTelesvcMsg(const sms_3gpp2_telesvc_msg_s *p_msg, unsigned char *p_pkg_str)
469 {
470         MSG_BEGIN();
471         int encode_size = 0;
472
473         MSG_DEBUG("Teleservice msg type = [%d]", p_msg->type);
474
475         switch (p_msg->type) {
476         /* case SMS_TYPE_DELIVER:
477                 encode_size = encodeTelesvcDeliverMsg(&(p_msg->data.delever), p_pkg_str);
478                 break;  */
479         case SMS_TYPE_SUBMIT:
480                 encode_size = encodeTelesvcSubmitMsg(&(p_msg->data.submit), p_pkg_str);
481                 break;
482         case SMS_TYPE_CANCEL:
483                 encode_size = encodeTelesvcCancelMsg(&(p_msg->data.cancel), p_pkg_str);
484                 break;
485         case SMS_TYPE_USER_ACK:
486                 encode_size = encodeTelesvcUserAckMsg(&(p_msg->data.user_ack), p_pkg_str);
487                 break;
488         case SMS_TYPE_READ_ACK:
489                 encode_size = encodeTelesvcReadAckMsg(&(p_msg->data.read_ack), p_pkg_str);
490                 break;
491         case SMS_TYPE_DELIVER_REPORT:
492                 encode_size = encodeTelesvcDeliverReportMsg(&(p_msg->data.report), p_pkg_str);
493                 break;
494         default:
495                 MSG_DEBUG("No matching type for [%d]", p_msg->type);
496                 break;
497         }
498
499         MSG_END();
500         return encode_size;
501 }
502
503
504 /*
505 int Sms3gpp2MsgCodec::encodeTelesvcDeliverMsg(const sms_3gpp2_telesvc_read_ack_s *p_msg, char *p_pkg_str)
506 {
507
508 }
509 */
510
511
512 int Sms3gpp2MsgCodec::encodeTelesvcReadAckMsg(const sms_3gpp2_telesvc_read_ack_s *p_msg, unsigned char *p_pkg_str)
513 {
514         MSG_BEGIN();
515
516         int offset = 0;
517
518         MSG_END();
519
520         return offset;
521 }
522
523
524 int Sms3gpp2MsgCodec::encodeTelesvcUserAckMsg(const sms_3gpp2_telesvc_user_ack_s *p_msg, unsigned char *p_pkg_str)
525 {
526         MSG_BEGIN();
527
528         int offset = 0;
529
530         MSG_END();
531
532         return offset;
533 }
534
535
536 int Sms3gpp2MsgCodec::encodeTelesvcDeliverReportMsg(const sms_3gpp2_telesvc_report_s *p_msg, unsigned char *p_pkg_str)
537 {
538         MSG_BEGIN();
539
540         int offset = 0, len_index = 0, encode_size = 0;
541         bool delReservedBit = false;
542
543         /* 1. Message Identifier (Mandatory) */
544         p_pkg_str[offset++] = SMS_BEARER_MESSAGE_IDENTIFIER;
545         p_pkg_str[offset++] = 3;
546         _copy_short_to_char(p_pkg_str+offset+1, &(p_msg->msg_id.msg_id));
547         _shiftNBit(&p_pkg_str[offset], 3, 4);
548         p_pkg_str[offset] |= SMS_TYPE_SUBMIT << 4;
549         offset += 2;
550         p_pkg_str[offset++] |= (p_msg->msg_id.header_ind ? 0x08 : 0x00);
551
552         /* 2. TP-Failure Cause (Conditional) */
553         if (p_msg->tp_fail_cause >= 0x80) {
554                 p_pkg_str[offset++] = SMS_BEARER_TP_FAILURE_CAUSE;
555                 p_pkg_str[offset++] = 1;
556                 p_pkg_str[offset++] = p_msg->tp_fail_cause;
557         }
558
559         /* 3. User Data (Optional) */
560         if (p_msg->user_data.data_len > 0) {
561                 p_pkg_str[offset++] = SMS_BEARER_USER_DATA;
562                 len_index = offset;
563                 offset++;
564                 if (p_msg->user_data.encode_type == 0x01 || p_msg->user_data.encode_type == 0x0a)
565                         p_pkg_str[offset++] = p_msg->user_data.msg_type;
566
567                 p_pkg_str[offset++] = p_msg->user_data.encode_type << 3;
568                 p_pkg_str[offset++] = p_msg->user_data.data_len;
569
570                 if (p_msg->user_data.encode_type == SMS_ENCODE_7BIT_ASCII || p_msg->user_data.encode_type == SMS_ENCODE_GSM7BIT) {
571                         encode_size = encodeUserData(p_msg->user_data.user_data, &p_pkg_str[offset], p_msg->user_data.data_len);
572                         offset += encode_size;
573                         if (p_msg->user_data.data_len % 8 > 4)
574                                 delReservedBit = true;
575                 } else {
576                         memcpy(p_pkg_str+offset, p_msg->user_data.user_data, p_msg->user_data.data_len);
577                         offset += p_msg->user_data.data_len;
578                 }
579                 _shiftNBit(&p_pkg_str[len_index+1], offset-len_index-1, 3);
580
581                 if (delReservedBit == true)
582                         offset--;
583
584                 p_pkg_str[len_index] = offset - len_index - 1;
585         }
586
587         /* 4. Language Indicator (Optional) */
588         /* TODO : give condition */
589         /*
590         p_pkg_str[offset++] = SMS_BEARER_ALERT_ON_MSG_DELIVERY;
591         p_pkg_str[offset++] = 1;
592         p_pkg_str[offset++] = p_msg->language;
593         */
594
595         /* 5. Multiple Encoding User Data (Optional) */
596         /* Omitted */
597
598         MSG_END();
599
600         return offset;
601 }
602
603
604 int Sms3gpp2MsgCodec::encodeTelesvcCancelMsg(const sms_3gpp2_telesvc_cancel_s *p_msg, unsigned char *p_pkg_str)
605 {
606         MSG_BEGIN();
607
608         int offset = 0;
609
610         /* 1. Message Identifier */
611         p_pkg_str[offset++] = SMS_BEARER_MESSAGE_IDENTIFIER;
612         p_pkg_str[offset++] = 3;
613         _copy_short_to_char(p_pkg_str+offset+1, &(p_msg->msg_id.msg_id));
614         _shiftNBit(&p_pkg_str[offset], 3, 4);
615         p_pkg_str[offset] |= SMS_TYPE_SUBMIT << 4;
616         offset += 2;
617         p_pkg_str[offset++] |= (p_msg->msg_id.header_ind ? 0x08 : 0x00);
618
619         MSG_END();
620
621         return offset;
622 }
623
624
625 int Sms3gpp2MsgCodec::encodeTelesvcSubmitMsg(const sms_3gpp2_telesvc_submit_s *p_msg, unsigned char *p_pkg_str)
626 {
627         MSG_BEGIN();
628
629         int offset = 0, len_index = 0;
630         int encode_size = 0;
631         bool delReservedBit = false;
632
633         /* 1. Message Identifier */
634         p_pkg_str[offset++] = SMS_BEARER_MESSAGE_IDENTIFIER;
635         p_pkg_str[offset++] = 3;
636         _copy_short_to_char(p_pkg_str+offset+1, &(p_msg->msg_id.msg_id));
637         _shiftNBit(&p_pkg_str[offset], 3, 4);
638         p_pkg_str[offset] |= SMS_TYPE_SUBMIT << 4;
639         offset += 2;
640         p_pkg_str[offset++] |= (p_msg->msg_id.header_ind ? 0x08 : 0x00);
641
642         /* 2. User Data */
643         if (p_msg->user_data.data_len > 0) {
644                 p_pkg_str[offset++] = SMS_BEARER_USER_DATA;
645                 len_index = offset;
646                 offset++;
647                 if (p_msg->user_data.encode_type == 0x01 || p_msg->user_data.encode_type == 0x0a)
648                         p_pkg_str[offset++] = p_msg->user_data.msg_type;
649
650                 p_pkg_str[offset++] = p_msg->user_data.encode_type << 3;
651                 p_pkg_str[offset++] = p_msg->user_data.data_len;
652
653                 if (p_msg->user_data.encode_type == SMS_ENCODE_7BIT_ASCII) {
654                         encode_size = encodeUserData(p_msg->user_data.user_data, &p_pkg_str[offset], p_msg->user_data.data_len);
655                         offset += encode_size;
656                         if (p_msg->user_data.data_len % 8 > 4)
657                                 delReservedBit = true;
658                 } else if (p_msg->user_data.encode_type == SMS_ENCODE_GSM7BIT) {
659                         encode_size = PackGSM7bitData(p_msg->user_data.user_data, &p_pkg_str[offset], p_msg->user_data.data_len);
660                         offset += encode_size;
661                         if (p_msg->user_data.data_len % 8 > 4)
662                                 delReservedBit = true;
663                 } else if (p_msg->user_data.encode_type == SMS_ENCODE_UNICODE) {
664                         MsgTextConvert *textCvt = MsgTextConvert::instance();
665                         encode_size = textCvt->convertUTF8ToUCS2(&p_pkg_str[offset], SMS_MAX_USER_DATA_LEN, p_msg->user_data.user_data, p_msg->user_data.data_len);
666                         p_pkg_str[offset-1] = encode_size / 2;
667                         offset += encode_size;
668                 } else {
669                         memcpy(p_pkg_str+offset, p_msg->user_data.user_data, p_msg->user_data.data_len);
670                         offset += p_msg->user_data.data_len;
671                 }
672                 _shiftNBit(&p_pkg_str[len_index+1], offset-len_index-1, 3);
673
674                 if (delReservedBit == true)
675                         offset--;
676
677                 p_pkg_str[len_index] = offset - len_index - 1;
678         }
679         /* Sprint and Verizon issue */
680         /* 3. Validity Period */
681         /*
682         if (p_msg->val_period.format == SMS_3GPP2_TIME_RELATIVE) {
683                 p_pkg_str[offset++] = SMS_BEARER_VALIDITY_PERIOD_RELATIVE;
684                 p_pkg_str[offset++] = 1;
685                 p_pkg_str[offset++] = p_msg->val_period.time.rel_time.rel_time;
686
687         } else if (p_msg->val_period.format == SMS_3GPP2_TIME_ABSOLUTE){
688                 p_pkg_str[offset++] = SMS_BEARER_VALIDITY_PERIOD_ABSOLUTE;
689                 p_pkg_str[offset++] = 6;
690                 p_pkg_str[offset++] = _convert_to_BCD(p_msg->val_period.time.abs_time.year);
691                 p_pkg_str[offset++] = _convert_to_BCD(p_msg->val_period.time.abs_time.month);
692                 p_pkg_str[offset++] = _convert_to_BCD(p_msg->val_period.time.abs_time.day);
693                 p_pkg_str[offset++] = _convert_to_BCD(p_msg->val_period.time.abs_time.hours);
694                 p_pkg_str[offset++] = _convert_to_BCD(p_msg->val_period.time.abs_time.minutes);
695                 p_pkg_str[offset++] = _convert_to_BCD(p_msg->val_period.time.abs_time.seconds);
696         }
697         */
698
699         /* 4. Deferred Delivery Time */
700         if (p_msg->defer_val_period.format == SMS_3GPP2_TIME_RELATIVE) {
701                 p_pkg_str[offset++] = SMS_BEARER_DEFERRED_DELIVERY_TIME_RELATIVE;
702                 p_pkg_str[offset++] = 1;
703                 p_pkg_str[offset++] = p_msg->defer_val_period.time.rel_time.rel_time;
704
705         } else if (p_msg->defer_val_period.format == SMS_3GPP2_TIME_ABSOLUTE) {
706                 p_pkg_str[offset++] = SMS_BEARER_DEFERRED_DELIVERY_TIME_ABSOLUTE;
707                 p_pkg_str[offset++] = 6;
708                 p_pkg_str[offset++] = _convert_to_BCD(p_msg->defer_val_period.time.abs_time.year);
709                 p_pkg_str[offset++] = _convert_to_BCD(p_msg->defer_val_period.time.abs_time.month);
710                 p_pkg_str[offset++] = _convert_to_BCD(p_msg->defer_val_period.time.abs_time.day);
711                 p_pkg_str[offset++] = _convert_to_BCD(p_msg->defer_val_period.time.abs_time.hours);
712                 p_pkg_str[offset++] = _convert_to_BCD(p_msg->defer_val_period.time.abs_time.minutes);
713                 p_pkg_str[offset++] = _convert_to_BCD(p_msg->defer_val_period.time.abs_time.seconds);
714         }
715
716         /* 5. Priority Indicator */
717         if (p_msg->priority >= SMS_PRIORITY_NORMAL && p_msg->priority <=        SMS_PRIORITY_EMERGENCY) {
718                 p_pkg_str[offset++] = SMS_BEARER_PRIORITY_INDICATOR;
719                 p_pkg_str[offset++] = 1;
720                 p_pkg_str[offset++] = p_msg->priority << 6;
721         }
722
723         /* Sprint and Verizon issue */
724         /* 6. Privacy Indicator */
725         /*
726         if (p_msg->privacy >= SMS_PRIVACY_NOT_RESTRICTED && p_msg->privacy <= SMS_PRIVACY_SECRET) {
727                 p_pkg_str[offset++] = SMS_BEARER_PRIVACY_INDICATOR;
728                 p_pkg_str[offset++] = 1;
729                 p_pkg_str[offset++] = p_msg->privacy << 6;
730         }
731         */
732
733         /* 7. Reply Option */
734         if (p_msg->reply_opt.user_ack_req | p_msg->reply_opt.deliver_ack_req | p_msg->reply_opt.read_ack_req | p_msg->reply_opt.report_req) {
735                 p_pkg_str[offset++] = SMS_BEARER_REPLY_OPTION;
736                 p_pkg_str[offset++] = 1;
737                 p_pkg_str[offset] |= p_msg->reply_opt.user_ack_req << 7;
738                 p_pkg_str[offset] |= p_msg->reply_opt.deliver_ack_req << 6;
739                 p_pkg_str[offset] |= p_msg->reply_opt.read_ack_req << 5;
740                 p_pkg_str[offset++] |= p_msg->reply_opt.report_req << 4;
741         }
742
743         /* 8. Alert on Message Delivery */
744         /* TODO : give condition */
745         /*
746         p_pkg_str[offset++] = SMS_BEARER_ALERT_ON_MSG_DELIVERY;
747         p_pkg_str[offset++] = 1;
748         p_pkg_str[offset++] = p_msg->alert_priority << 6;
749         */
750
751         /* 9. Language Indicator */
752         /* TODO : give condition */
753         /*
754         p_pkg_str[offset++] = SMS_BEARER_ALERT_ON_MSG_DELIVERY;
755         p_pkg_str[offset++] = 1;
756         p_pkg_str[offset++] = p_msg->language;
757         */
758
759         /* 10. Call-back Number */
760         if (p_msg->callback_number.addr_len > 0) {
761                 p_pkg_str[offset++] = SMS_BEARER_CALLBACK_NUMBER;
762
763                 int len_index = offset++;
764
765                 p_pkg_str[offset] |= p_msg->callback_number.digit_mode << 7;
766
767                 if (p_msg->callback_number.digit_mode == false) {
768                         p_pkg_str[offset++] |= (p_msg->callback_number.addr_len & 0xfe) >> 1;
769                         p_pkg_str[offset] |= (p_msg->callback_number.addr_len & 0x01) << 7;
770                         int addr_len = Sms3gpp2ParamCodec::instance()->convertDigitToDTMF(p_msg->callback_number.szData, p_msg->callback_number.addr_len, 1, p_pkg_str+offset);
771                         offset += addr_len;
772                 } else if (p_msg->callback_number.digit_mode == true) {
773                         p_pkg_str[offset] |= p_msg->callback_number.number_type << 6;
774                         p_pkg_str[offset++] |= p_msg->callback_number.number_plan;
775                         p_pkg_str[offset++] = p_msg->callback_number.addr_len;
776                         memcpy(p_pkg_str+offset, p_msg->callback_number.szData, p_msg->callback_number.addr_len);
777                         offset += p_msg->callback_number.addr_len;
778                 }
779
780                 p_pkg_str[len_index] = offset- len_index - 1;
781         }
782
783         /* 11. Multiple Encoding User Data */
784         /* Omitted */
785
786         /* 12. Message Deposit Index */
787         /* Omitted */
788
789         /* 13. Service Category Program Results */
790         /* Omitted */
791
792         MSG_END();
793         return offset;
794 }
795
796
797 int Sms3gpp2MsgCodec::decodeMsg(const unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_trans_msg_s *p_msg)
798 {
799         MSG_BEGIN();
800
801         int decodelen = 0;
802
803         char mti = p_pkg_str[0] & 0xff;
804
805         switch (mti) {
806         case SMS_TRANS_P2P_MSG:
807                 p_msg->type = SMS_TRANS_P2P_MSG;
808                 decodelen = decodeP2PMsg(p_pkg_str+1, pkg_len-1, &(p_msg->data.p2p_msg));
809                 break;
810         case SMS_TRANS_BROADCAST_MSG:
811                 p_msg->type = SMS_TRANS_BROADCAST_MSG;
812                 decodelen = decodeCBMsg(p_pkg_str+1, pkg_len-1, &(p_msg->data.cb_msg));
813                 break;
814         case SMS_TRANS_ACK_MSG:
815                 p_msg->type = SMS_TRANS_ACK_MSG;
816                 decodelen = decodeAckMsg(p_pkg_str+1, pkg_len-1, &(p_msg->data.ack_msg));
817                 break;
818         default:
819                 p_msg->type = SMS_TRANS_TYPE_RESERVED;
820                 break;
821         }
822
823         MSG_END();
824
825         return decodelen+1;
826 }
827
828
829 int Sms3gpp2MsgCodec::decodeP2PMsg(const unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_trans_p2p_msg_s *p_p2p)
830 {
831         MSG_BEGIN();
832
833         int offset = 0, tmp_len = 0;
834
835         while (offset < pkg_len) {
836                 switch (p_pkg_str[offset]) {
837                 case 0x00:
838                         /* Teleservice Identifier */
839                         offset += decodeTeleId(p_pkg_str+offset, pkg_len, &(p_p2p->telesvc_id));
840                         break;
841                 case 0x01:
842                         /* Service Category */
843                         offset += decodeSvcCtg(p_pkg_str+offset, pkg_len, &(p_p2p->svc_ctg));
844                         break;
845                 case 0x02:
846                 case 0x04:
847                         /* Address */
848                         offset += decodeAddress(p_pkg_str+offset, pkg_len, &(p_p2p->address));
849                         break;
850                 case 0x03:
851                 case 0x05:
852                         /* Subaddress */
853                         offset += decodeSubAddress(p_pkg_str+offset, pkg_len, &(p_p2p->sub_address));
854                         break;
855                 case 0x06:
856                         /* Bearer Reply Option*/
857                         offset += 2;
858                         p_p2p->reply_seq = (sms_3gpp2_trans_reply_seq_t)(p_pkg_str[offset] >> 2);
859                         offset++;
860                         break;
861                 case 0x08:
862                         /* Bearer Data */
863                         tmp_len = p_pkg_str[++offset];
864                         decodeP2PTelesvcMsg(p_pkg_str+offset+1, tmp_len, &p_p2p->telesvc_msg);
865                         offset += (tmp_len+1);
866                         break;
867                 default:
868                         break;
869                 }
870         }
871
872         MSG_END();
873
874         return offset;
875 }
876
877
878 int Sms3gpp2MsgCodec::decodeCBMsg(const unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_trans_broadcast_msg_s *p_cb)
879 {
880         MSG_BEGIN();
881
882         int offset = 0, tmp_len = 0;
883
884         while (offset < pkg_len) {
885                 if (p_pkg_str[offset] == 0x01) {
886                         /* Service Category */
887                         offset += decodeTeleId(p_pkg_str+offset, pkg_len, &(p_cb->svc_ctg));
888                 } else if (p_pkg_str[offset] == 0x08) {
889                         /* Bearer Data */
890                         tmp_len = p_pkg_str[++offset];
891                         if (p_cb->svc_ctg >= SMS_TRANS_SVC_CTG_CMAS_PRESIDENTIAL && p_cb->svc_ctg <= SMS_TRANS_SVC_CTG_CMAS_TEST) {
892                                 decodeCBBearerData(p_pkg_str+offset+1, tmp_len, &(p_cb->telesvc_msg), true);
893                         } else {
894                                 decodeCBBearerData(p_pkg_str+offset+1, tmp_len, &(p_cb->telesvc_msg), false);
895                         }
896
897                         offset += (tmp_len+1);
898                 }
899         }
900
901         MSG_END();
902
903         return offset;
904 }
905
906
907 int Sms3gpp2MsgCodec::decodeAckMsg(const unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_trans_ack_msg_s *p_ack)
908 {
909         MSG_BEGIN();
910
911         int offset = 0;
912
913         while (offset < pkg_len) {
914                 if (p_pkg_str[offset] == 0x04) {
915                         /* Destination Address */
916                         offset += decodeAddress(p_pkg_str+offset, pkg_len, &(p_ack->address));
917                 } else if (p_pkg_str[offset] == 0x05) {
918                         /* Destination Subaddress */
919                         offset += decodeSubAddress(p_pkg_str+offset, pkg_len, &(p_ack->sub_address));
920                 } else if (p_pkg_str[offset] == 0x07) {
921                         /* Cause Codes */
922                         offset += 2;
923                         p_ack->cause_code.reply_seq = (sms_3gpp2_trans_reply_seq_t)p_pkg_str[offset] >> 2;
924                         switch (p_pkg_str[offset++] & 0x03) {
925                         case 0:
926                                 p_ack->cause_code.error_class = SMS_TRANS_ERR_CLASS_NONE;
927                                 break;
928                         case 1:
929                                 /* Reserved */
930                                 break;
931                         case 2:
932                                 p_ack->cause_code.error_class = SMS_TRANS_ERR_CLASS_TEMPORARY;
933                                 break;
934                         case 3:
935                                 p_ack->cause_code.error_class = SMS_TRANS_ERR_CLASS_PERMANENT;
936                                 break;
937                         }
938
939                         if (p_ack->cause_code.error_class != SMS_TRANS_ERR_CLASS_NONE) {
940                                 p_ack->cause_code.cause_code = (sms_3gpp2_trans_cause_code_t)p_pkg_str[offset++];
941                         }
942                 }
943         }
944
945         MSG_END();
946
947         return offset;
948 }
949
950
951 void Sms3gpp2MsgCodec::decodeP2PTelesvcMsg(const unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_telesvc_msg_s *p_telesvc)
952 {
953         MSG_BEGIN();
954
955         p_telesvc->type = findMsgType(p_pkg_str, pkg_len);
956
957         MSG_DEBUG("Msg Type = [%d]", p_telesvc->type);
958
959         switch (p_telesvc->type) {
960         case SMS_TYPE_DELIVER:
961                 decodeP2PDeliverMsg(p_pkg_str, pkg_len, &p_telesvc->data.deliver);
962                 break;
963         case SMS_TYPE_SUBMIT:
964                 decodeP2PSubmitMsg(p_pkg_str, pkg_len, &p_telesvc->data.submit);
965                 break;
966         case SMS_TYPE_DELIVERY_ACK:
967                 decodeP2PDeliveryAckMsg(p_pkg_str, pkg_len, &p_telesvc->data.delivery_ack);
968                 break;
969         case SMS_TYPE_USER_ACK:
970                 decodeP2PUserAckMsg(p_pkg_str, pkg_len, &p_telesvc->data.user_ack);
971                 break;
972         case SMS_TYPE_READ_ACK:
973                 decodeP2PReadAckMsg(p_pkg_str, pkg_len, &p_telesvc->data.read_ack);
974                 break;
975         case SMS_TYPE_SUBMIT_REPORT:
976                 /* decodeP2PSubmitReportMsg(p_pkg_str, pkg_len, &p_telesvc->data.report); */
977                 break;
978         default:
979                 break;
980         }
981
982         MSG_END();
983 }
984
985
986 void Sms3gpp2MsgCodec::decodeP2PDeliverMsg(const unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_telesvc_deliver_s *p_del)
987 {
988         MSG_BEGIN();
989
990         int offset = 0, tmp_len, tmp_off;
991         unsigned short tmp_param_s;
992         unsigned char tmp_str[pkg_len+1];
993
994         while (offset < pkg_len) {
995                 MSG_DEBUG("current offset = [%d] [%x]", offset, p_pkg_str[offset]);
996
997                 switch (p_pkg_str[offset]) {
998                 case 0x00:
999                         /* Message Identifier */
1000                         offset += decodeMsgId(p_pkg_str+offset, 5, &(p_del->msg_id));
1001                         break;
1002                 case 0x01:
1003                         /* User Data */
1004                         offset += 2;
1005                         tmp_len = p_pkg_str[offset-1];
1006                         memset(tmp_str, 0x00, sizeof(tmp_str));
1007                         memcpy(tmp_str, p_pkg_str+offset, tmp_len);
1008                         decodeUserData(tmp_str, tmp_len, &(p_del->user_data));
1009                         offset += tmp_len;
1010                         break;
1011                 case 0x03:
1012                         /* Message Center Time Stamp */
1013                         offset += 2;
1014                         offset += decodeAbsTime(p_pkg_str+offset, &(p_del->time_stamp));
1015                         break;
1016                 case 0x04:
1017                         /* Validity Period - Absolute */
1018                         offset += 2;
1019                         p_del->val_period.format = SMS_3GPP2_TIME_ABSOLUTE;
1020                         offset += decodeAbsTime(p_pkg_str+offset, &(p_del->val_period.time.abs_time));
1021                         break;
1022                 case 0x05:
1023                         /* Validity Period - Relative */
1024                         offset += 2;
1025                         p_del->val_period.format = SMS_3GPP2_TIME_RELATIVE;
1026                         p_del->val_period.time.rel_time.rel_time = (sms_3gpp2_relative_time_t)p_pkg_str[offset++];
1027                         break;
1028                 case 0x08:
1029                         /* Priority Indicator */
1030                         offset += 2;
1031                         p_del->priority = (sms_3gpp2_priority_indicator_t)((p_pkg_str[offset++] & 0xc0) >> 6);
1032                         break;
1033                 case 0x09:
1034                         /* Privacy Indicator */
1035                         offset += 2;
1036                         p_del->privacy = (sms_3gpp2_privacy_indicator_t)((p_pkg_str[offset++] & 0xc0) >> 6);
1037                         break;
1038                 case 0x0a:
1039                         /* Reply Option */
1040                         offset += 2;
1041
1042                         if (p_pkg_str[offset] & 0x80)
1043                                 p_del->reply_opt.user_ack_req = true;
1044                         else
1045                                 p_del->reply_opt.user_ack_req = false;
1046
1047                         if (p_pkg_str[offset] & 0x40)
1048                                 p_del->reply_opt.deliver_ack_req = true;
1049                         else
1050                                 p_del->reply_opt.deliver_ack_req = false;
1051
1052                         if (p_pkg_str[offset] & 0x20)
1053                                 p_del->reply_opt.read_ack_req = true;
1054                         else
1055                                 p_del->reply_opt.read_ack_req = false;
1056
1057                         if (p_pkg_str[offset] & 0x10)
1058                                 p_del->reply_opt.report_req = true;
1059                         else
1060                                 p_del->reply_opt.report_req = false;
1061
1062                         offset++;
1063                         break;
1064                 case 0x0b:
1065                         /* Number of Message */
1066                         offset += 2;
1067                         p_del->num_msg = (((p_pkg_str[offset] & 0xf0) >> 4) * 10) + (p_pkg_str[offset] & 0x0f);
1068                         offset++;
1069                         break;
1070                 case 0x0c:
1071                         /* Alert on Message Delivery */
1072                         offset += 2;
1073                         tmp_len = p_pkg_str[offset-1];
1074                         if (tmp_len > 0)
1075                                 p_del->alert_priority = (sms_3gpp2_alert_priority_t)((p_pkg_str[offset] & 0xc0) >> 6);
1076                         offset += tmp_len;
1077                         break;
1078                 case 0x0d:
1079                         /* Language Indicator */
1080                         offset += 2;
1081                         p_del->language = (sms_3gpp2_language_type_t)p_pkg_str[offset++];
1082                         break;
1083                 case 0x0e:
1084                         /* Call-Back Number */
1085                         offset += 2;
1086                         tmp_len = p_pkg_str[offset-1];
1087
1088                         decodeCallBackNum(&p_pkg_str[offset], tmp_len, &(p_del->callback_number));
1089
1090                         offset += tmp_len;
1091                         break;
1092                 case 0x0f:
1093                         /* Message Display Mode */
1094                         offset += 2;
1095
1096                         p_del->display_mode = (sms_3gpp2_display_mode_t)((p_pkg_str[offset++] & 0xc0) >> 6);
1097                         break;
1098                 case 0x10:
1099                         /* Multiple Encoding User Data */
1100                         offset += 2;
1101                         tmp_len = p_pkg_str[offset-1];
1102
1103                         /* TODO */
1104
1105                         offset += tmp_len;
1106                         break;
1107                 case 0x11:
1108                         /* Message Deposit Index */
1109                         offset += 2;
1110
1111                         memset(&tmp_param_s, 0x00, sizeof(unsigned short));
1112                         _copy_char_to_short(&tmp_param_s, p_pkg_str+offset);
1113                         p_del->deposit_id = tmp_param_s;
1114
1115                         offset += 2;
1116                         break;
1117                 case 0x16:
1118                         /* Enhanced VMN */
1119                         offset++;
1120                         tmp_len = p_pkg_str[offset++];
1121                         memset(tmp_str, 0x00, sizeof(tmp_str));
1122                         memcpy(tmp_str, p_pkg_str+offset, tmp_len);
1123                         tmp_off = 0;
1124
1125                         p_del->enhanced_vmn.priority = (sms_3gpp2_priority_indicator_t)(p_pkg_str[offset] >> 6);
1126                         p_del->enhanced_vmn.password_req = p_pkg_str[offset] & 0x20 ? true : false;
1127                         p_del->enhanced_vmn.setup_req = p_pkg_str[offset] & 0x10 ? true : false;
1128                         p_del->enhanced_vmn.pw_change_req = p_pkg_str[offset] & 0x08 ? true : false;
1129                         _shiftNBit_for_decode(tmp_str, tmp_len, 5);
1130
1131                         if (p_del->enhanced_vmn.setup_req || p_del->enhanced_vmn.pw_change_req) {
1132                                 p_del->enhanced_vmn.min_pw_len = tmp_str[tmp_off] >> 4;
1133                                 p_del->enhanced_vmn.max_pw_len = tmp_str[tmp_off++] & 0x0f;
1134                         }
1135
1136                         p_del->enhanced_vmn.vm_num_unheard_msg = tmp_str[tmp_off++];
1137                         p_del->enhanced_vmn.vm_mailbox_alm_full = tmp_str[tmp_off] & 0x80 ? true : false;
1138                         p_del->enhanced_vmn.vm_mailbox_full = tmp_str[tmp_off] & 0x40 ? true : false;
1139                         p_del->enhanced_vmn.reply_allowed = tmp_str[tmp_off] & 0x20 ? true : false;
1140                         p_del->enhanced_vmn.fax_included = tmp_str[tmp_off] & 0x10 ? true : false;
1141                         p_del->enhanced_vmn.vm_len = ((tmp_str[tmp_off] & 0x0f) << 8) | tmp_str[tmp_off+1];
1142                         tmp_off += 2;
1143                         p_del->enhanced_vmn.vm_ret_day = tmp_str[tmp_off] >> 1;
1144                         _shiftNBit_for_decode(tmp_str, tmp_len, 7);
1145                         p_del->enhanced_vmn.vm_msg_id = (tmp_str[tmp_off] << 8) | tmp_str[tmp_off+1];
1146                         tmp_off += 2;
1147                         p_del->enhanced_vmn.vm_mailbox_id = (tmp_str[tmp_off] << 8) | tmp_str[tmp_off+1];
1148                         tmp_off += 2;
1149
1150                         p_del->enhanced_vmn.an_digit_mode = (sms_3gpp2_digit_mode_t)(tmp_str[tmp_off] & 0x80 ? true : false);
1151                         p_del->enhanced_vmn.an_number_type = (sms_3gpp2_number_type_t)((tmp_str[tmp_off] & 0x70) >> 4);
1152                         if (p_del->enhanced_vmn.an_digit_mode) {
1153                                 p_del->enhanced_vmn.an_number_plan = (sms_3gpp2_number_plan_t)(tmp_str[tmp_off++] & 0x0f);
1154                                 p_del->enhanced_vmn.an_num_field = tmp_str[tmp_off++];
1155                                 for (int i = 0; i < p_del->enhanced_vmn.an_num_field; i++) {
1156                                         switch (tmp_str[tmp_off] & 0xf0) {
1157                                         case 0x10:
1158                                         case 0x20:
1159                                         case 0x30:
1160                                         case 0x40:
1161                                         case 0x50:
1162                                         case 0x60:
1163                                         case 0x70:
1164                                         case 0x80:
1165                                         case 0x90:
1166                                                 p_del->enhanced_vmn.an_char[i] = ((tmp_str[tmp_off] & 0xf0) >> 4) + '0';
1167                                                 break;
1168                                         case 0xa0:
1169                                                 p_del->enhanced_vmn.an_char[i] = '0';
1170                                                 break;
1171                                         case 0xb0:
1172                                                 p_del->enhanced_vmn.an_char[i] = '*';
1173                                                 break;
1174                                         case 0xc0:
1175                                                 p_del->enhanced_vmn.an_char[i] = '#';
1176                                                 break;
1177                                         default:
1178                                                 break;
1179                                         }
1180
1181                                         _shiftNBit_for_decode(tmp_str, tmp_len, 4);
1182                                 }
1183                         } else {
1184                                 _shiftNBit_for_decode(tmp_str, tmp_len, 4);
1185                                 p_del->enhanced_vmn.an_num_field = tmp_str[tmp_off++];
1186                                 memset(p_del->enhanced_vmn.an_char, 0x00, sizeof(p_del->enhanced_vmn.an_char));
1187                                 memcpy(p_del->enhanced_vmn.an_char, tmp_str+tmp_off, p_del->enhanced_vmn.an_num_field);
1188                                 tmp_off += p_del->enhanced_vmn.an_num_field;
1189                         }
1190
1191                         p_del->enhanced_vmn.cli_digit_mode = (sms_3gpp2_digit_mode_t)(tmp_str[offset] & 0x80 ? true : false);
1192                         p_del->enhanced_vmn.cli_number_type = (sms_3gpp2_number_type_t)(tmp_str[offset] & 0x70 >> 4);
1193                         if (p_del->enhanced_vmn.cli_digit_mode) {
1194                                 p_del->enhanced_vmn.cli_number_plan = (sms_3gpp2_number_plan_t)(tmp_str[tmp_off++] & 0x0f);
1195                                 p_del->enhanced_vmn.cli_num_field = tmp_str[tmp_off++];
1196                                 for (int i = 0; i < p_del->enhanced_vmn.cli_num_field; i++) {
1197                                         switch (tmp_str[tmp_off] & 0xf0) {
1198                                         case 0x10:
1199                                         case 0x20:
1200                                         case 0x30:
1201                                         case 0x40:
1202                                         case 0x50:
1203                                         case 0x60:
1204                                         case 0x70:
1205                                         case 0x80:
1206                                         case 0x90:
1207                                                 p_del->enhanced_vmn.cli_char[i] = ((tmp_str[tmp_off] & 0xf0) >> 4) + '0';
1208                                                 break;
1209                                         case 0xa0:
1210                                                 p_del->enhanced_vmn.cli_char[i] = '0';
1211                                                 break;
1212                                         case 0xb0:
1213                                                 p_del->enhanced_vmn.cli_char[i] = '*';
1214                                                 break;
1215                                         case 0xc0:
1216                                                 p_del->enhanced_vmn.cli_char[i] = '#';
1217                                                 break;
1218                                         default:
1219                                                 break;
1220                                         }
1221
1222                                         _shiftNBit_for_decode(tmp_str, tmp_len, 4);
1223                                 }
1224                         } else {
1225                                 _shiftNBit_for_decode(tmp_str, tmp_len, 4);
1226                                 p_del->enhanced_vmn.cli_num_field = tmp_str[tmp_off++];
1227                                 memset(p_del->enhanced_vmn.cli_char, 0x00, sizeof(p_del->enhanced_vmn.cli_char));
1228                                 memcpy(p_del->enhanced_vmn.cli_char, tmp_str+tmp_off, p_del->enhanced_vmn.cli_num_field);
1229                                 tmp_off += p_del->enhanced_vmn.cli_num_field;
1230                         }
1231
1232                         offset += tmp_len;
1233                         break;
1234                 case 0x17:
1235                         /* Enhanced VMN Ack */
1236                         offset++;
1237                         tmp_len = p_pkg_str[offset++];
1238                         memset(tmp_str, 0x00, sizeof(tmp_str));
1239                         memcpy(tmp_str, p_pkg_str+offset, tmp_len);
1240                         p_del->enhanced_vmn_ack.vm_mailbox_id = tmp_str[offset] << 8 | tmp_str[offset+1];
1241                         offset += 2;
1242                         p_del->enhanced_vmn_ack.vm_num_unheard_msg = tmp_str[offset++];
1243                         p_del->enhanced_vmn_ack.num_delete_ack = tmp_str[offset] >> 5;
1244                         p_del->enhanced_vmn_ack.num_play_ack = (tmp_str[offset] & 0x1c) >> 2;
1245                         _shiftNBit_for_decode(tmp_str, tmp_len, 6);
1246                         for (int i = 0; i < p_del->enhanced_vmn_ack.num_delete_ack; i++) {
1247                                 p_del->enhanced_vmn_ack.da_vm_msg_id[i] = tmp_str[offset] << 8 | tmp_str[offset+1];
1248                                 offset += 2;
1249                         }
1250                         for (int i = 0; i < p_del->enhanced_vmn_ack.num_play_ack; i++) {
1251                                 p_del->enhanced_vmn_ack.pa_vm_msg_id[i] = tmp_str[offset] << 8 | tmp_str[offset+1];
1252                                 offset += 2;
1253                         }
1254                         break;
1255                 default:
1256                         /* skip unrecognized sub parameters */
1257                         offset++;
1258                         tmp_len = p_pkg_str[offset++];
1259                         offset += tmp_len;
1260                         break;
1261                 }
1262         }
1263
1264         MSG_END();
1265 }
1266
1267
1268 void Sms3gpp2MsgCodec::decodeP2PSubmitMsg(const unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_telesvc_submit_s *p_sub)
1269 {
1270         int offset = 0, tmp_len;
1271         unsigned short tmp_param_s;
1272         unsigned char tmp_str[pkg_len+1];
1273
1274         while (offset < pkg_len) {
1275                 if (p_pkg_str[offset] == 0x00) {
1276                         /* Message Identifier */
1277                         offset += decodeMsgId(p_pkg_str+offset, 5, &(p_sub->msg_id));
1278                 } else if (p_pkg_str[offset] == 0x01) {
1279                         /* User Data */
1280                         offset += 2;
1281                         tmp_len = p_pkg_str[offset-1];
1282                         memset(tmp_str, 0x00, sizeof(tmp_str));
1283                         memcpy(tmp_str, p_pkg_str+offset, tmp_len);
1284
1285                         decodeUserData(tmp_str, tmp_len, &(p_sub->user_data));
1286
1287                         offset += tmp_len;
1288                 } else if (p_pkg_str[offset] == 0x04) {
1289                         /* Validity Period - Absolute */
1290                         offset += 2;
1291                         p_sub->val_period.format = SMS_3GPP2_TIME_ABSOLUTE;
1292                         offset += decodeAbsTime(p_pkg_str+offset, &(p_sub->val_period.time.abs_time));
1293                 } else if (p_pkg_str[offset] == 0x05) {
1294                         /* Validity Period - Relative */
1295                         offset += 2;
1296                         p_sub->val_period.format = SMS_3GPP2_TIME_RELATIVE;
1297                         p_sub->val_period.time.rel_time.rel_time = (sms_3gpp2_relative_time_t)p_pkg_str[offset++];
1298                 } else if (p_pkg_str[offset] == 0x06) {
1299                         /* Deferred Delivery Time - Absolute */
1300                         offset += 2;
1301                         p_sub->defer_val_period.format = SMS_3GPP2_TIME_ABSOLUTE;
1302                         offset += decodeAbsTime(p_pkg_str+offset, &(p_sub->defer_val_period.time.abs_time));
1303                 } else if (p_pkg_str[offset] == 0x07) {
1304                         /* Deferred Delivery Time - Relative */
1305                         offset += 2;
1306                         p_sub->defer_val_period.format = SMS_3GPP2_TIME_RELATIVE;
1307                         p_sub->defer_val_period.time.rel_time.rel_time = (sms_3gpp2_relative_time_t)p_pkg_str[offset++];
1308                 } else if (p_pkg_str[offset] == 0x08) {
1309                         /* Priority indicator */
1310                         offset += 2;
1311                         p_sub->priority = (sms_3gpp2_priority_indicator_t)((p_pkg_str[offset++] & 0xc0) >> 6);
1312                 } else if (p_pkg_str[offset] == 0x09) {
1313                         /* Privacy indicator */
1314                         offset += 2;
1315                         p_sub->privacy = (sms_3gpp2_privacy_indicator_t)((p_pkg_str[offset++] & 0xc0) >> 6);
1316                 } else if (p_pkg_str[offset] == 0x0a) {
1317                         /* Reply Option */
1318                         offset += 2;
1319
1320                         if (p_pkg_str[offset] & 0x80)
1321                                 p_sub->reply_opt.user_ack_req = true;
1322                         else
1323                                 p_sub->reply_opt.user_ack_req = false;
1324
1325                         if (p_pkg_str[offset] & 0x40)
1326                                 p_sub->reply_opt.deliver_ack_req = true;
1327                         else
1328                                 p_sub->reply_opt.deliver_ack_req = false;
1329
1330                         if (p_pkg_str[offset] & 0x20)
1331                                 p_sub->reply_opt.read_ack_req = true;
1332                         else
1333                                 p_sub->reply_opt.read_ack_req = false;
1334
1335                         if (p_pkg_str[offset] & 0x10)
1336                                 p_sub->reply_opt.report_req = true;
1337                         else
1338                                 p_sub->reply_opt.report_req = false;
1339
1340                         offset++;
1341                 } else if (p_pkg_str[offset] == 0x0c) {
1342                         /* Alert on Message Delivery */
1343                         offset += 2;
1344
1345                         p_sub->alert_priority = (sms_3gpp2_alert_priority_t)((p_pkg_str[offset++] & 0xc0) >> 6);
1346                 } else if (p_pkg_str[offset] == 0x0d) {
1347                         /* Language Indicator */
1348                         offset += 2;
1349
1350                         p_sub->language = (sms_3gpp2_language_type_t)p_pkg_str[offset++];
1351                 } else if (p_pkg_str[offset] == 0x0e) {
1352                         /* Call-Back Number */
1353                         offset += 2;
1354                         tmp_len = p_pkg_str[offset-1];
1355
1356                         decodeCallBackNum(&p_pkg_str[offset], tmp_len, &(p_sub->callback_number));
1357
1358                         offset += tmp_len;
1359                 } else if (p_pkg_str[offset] == 0x10) {
1360                         /* Multiple Encoding User Data */
1361                         offset += 2;
1362                         tmp_len = p_pkg_str[offset-1];
1363
1364                         /* TODO */
1365
1366                         offset += tmp_len;
1367                 } else if (p_pkg_str[offset] == 0x11) {
1368                         /* Message Deposit Index */
1369                         offset += 2;
1370
1371                         memset(&tmp_param_s, 0x00, sizeof(unsigned short));
1372                         _copy_char_to_short(&tmp_param_s, p_pkg_str+offset);
1373                         p_sub->deposit_id = tmp_param_s;
1374
1375                         offset += 2;
1376                 }
1377         }
1378 }
1379
1380
1381 void Sms3gpp2MsgCodec::decodeP2PUserAckMsg(const unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_telesvc_user_ack_s *p_user_ack)
1382 {
1383         MSG_BEGIN();
1384
1385         int offset = 0, tmp_len;
1386         unsigned short tmp_param_s;
1387         unsigned char tmp_str[pkg_len+1];
1388
1389         while (offset < pkg_len) {
1390                 if (p_pkg_str[offset] == 0x00) {
1391                         /* Message Identifier */
1392                         offset += decodeMsgId(p_pkg_str+offset, 5, &(p_user_ack->msg_id));
1393                 } else if (p_pkg_str[offset] == 0x01) {
1394                         /* User Data */
1395                         offset += 2;
1396                         tmp_len = p_pkg_str[offset-1];
1397                         memset(tmp_str, 0x00, sizeof(tmp_str));
1398                         memcpy(tmp_str, p_pkg_str+offset, tmp_len);
1399
1400                         decodeUserData(tmp_str, tmp_len, &(p_user_ack->user_data));
1401
1402                         offset += tmp_len;
1403                 } else if (p_pkg_str[offset] == 0x02) {
1404                         /* User Response Code */
1405                         offset += 2;
1406                         p_user_ack->resp_code = p_pkg_str[offset++];
1407                 } else if (p_pkg_str[offset] == 0x03) {
1408                         /* Message Center Time Stamp */
1409                         offset += 2;
1410                         offset += decodeAbsTime(p_pkg_str+offset, &(p_user_ack->time_stamp));
1411                 } else if (p_pkg_str[offset] == 0x10) {
1412                         /* Multiple Encoding User Data */
1413                         offset += 2;
1414                         tmp_len = p_pkg_str[offset-1];
1415
1416                         /* TODO */
1417
1418                         offset += tmp_len;
1419                 } else if (p_pkg_str[offset] == 0x11) {
1420                         /* Message Deposit Index */
1421                         offset += 2;
1422
1423                         memset(&tmp_param_s, 0x00, sizeof(unsigned short));
1424                         _copy_char_to_short(&tmp_param_s, p_pkg_str+offset);
1425                         p_user_ack->deposit_id = tmp_param_s;
1426
1427                         offset += 2;
1428                 }
1429         }
1430
1431         MSG_END();
1432 }
1433
1434
1435 void Sms3gpp2MsgCodec::decodeP2PReadAckMsg(const unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_telesvc_read_ack_s *p_read_ack)
1436 {
1437         MSG_BEGIN();
1438
1439         int offset = 0, tmp_len;
1440         unsigned short tmp_param_s;
1441         unsigned char tmp_str[pkg_len+1];
1442
1443         while (offset < pkg_len) {
1444                 if (p_pkg_str[offset] == 0x00) {
1445                         /* Message Identifier */
1446                         offset += decodeMsgId(p_pkg_str+offset, 5, &(p_read_ack->msg_id));
1447                 } else if (p_pkg_str[offset] == 0x01) {
1448                         /* User Data */
1449                         offset += 2;
1450                         tmp_len = p_pkg_str[offset-1];
1451                         memset(tmp_str, 0x00, sizeof(tmp_str));
1452                         memcpy(tmp_str, p_pkg_str+offset, tmp_len);
1453
1454                         decodeUserData(tmp_str, tmp_len, &(p_read_ack->user_data));
1455
1456                         offset += tmp_len;
1457                 } else if (p_pkg_str[offset] == 0x03) {
1458                         /* Message Center Time Stamp */
1459                         offset += 2;
1460                         offset += decodeAbsTime(p_pkg_str+offset, &(p_read_ack->time_stamp));
1461                 } else if (p_pkg_str[offset] == 0x10) {
1462                         /* Multiple Encoding User Data */
1463                         offset += 2;
1464                         tmp_len = p_pkg_str[offset-1];
1465
1466                         /* TODO */
1467
1468                         offset += tmp_len;
1469                 } else if (p_pkg_str[offset] == 0x11) {
1470                         /* Message Deposit Index */
1471                         offset += 2;
1472
1473                         memset(&tmp_param_s, 0x00, sizeof(unsigned short));
1474                         _copy_char_to_short(&tmp_param_s, p_pkg_str+offset);
1475                         p_read_ack->deposit_id = tmp_param_s;
1476
1477                         offset += 2;
1478                 }
1479         }
1480
1481         MSG_END();
1482 }
1483
1484
1485 void Sms3gpp2MsgCodec::decodeP2PSubmitReportMsg(const unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_telesvc_report_s *p_sub_report)
1486 {
1487         MSG_BEGIN();
1488
1489         int offset = 0, tmp_len;
1490         unsigned char tmp_str[pkg_len+1];
1491
1492         while (offset < pkg_len) {
1493                 switch (p_pkg_str[offset]) {
1494                 case 0x00:
1495                         /* Message Identifier */
1496                         offset += decodeMsgId(p_pkg_str+offset, 5, &(p_sub_report->msg_id));
1497                         break;
1498                 case 0x01:
1499                         /* User Data */
1500                         offset += 2;
1501                         tmp_len = p_pkg_str[offset-1];
1502                         memset(tmp_str, 0x00, sizeof(tmp_str));
1503                         memcpy(tmp_str, p_pkg_str+offset, tmp_len);
1504                         decodeUserData(tmp_str, tmp_len, &(p_sub_report->user_data));
1505                         offset += tmp_len;
1506                         break;
1507                 case 0x0d:
1508                         /* Language Indicator */
1509                         offset += 2;
1510                         p_sub_report->language = (sms_3gpp2_language_type_t)p_pkg_str[offset++];
1511                         break;
1512                 case 0x10:
1513                         /* Multiple Encoding User Data */
1514                         offset += 2;
1515                         tmp_len = p_pkg_str[offset-1];
1516
1517                         /* TODO */
1518
1519                         offset += tmp_len;
1520                         break;
1521                 case 0x15:
1522                         /* TP-Failure Cause */
1523                         offset += 2;
1524                         p_sub_report->tp_fail_cause = p_pkg_str[offset++];
1525                         break;
1526                 default:
1527                         break;
1528                 }
1529         }
1530
1531         MSG_END();
1532 }
1533
1534
1535 void Sms3gpp2MsgCodec::decodeP2PDeliveryAckMsg(const unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_telesvc_deliver_ack_s *p_del_ack)
1536 {
1537         MSG_BEGIN();
1538
1539         int offset = 0, tmp_len;
1540         unsigned char tmp_str[pkg_len+1];
1541
1542         while (offset < pkg_len) {
1543                 switch (p_pkg_str[offset]) {
1544                 case 0x00:
1545                         /* Message Identifier */
1546                         offset += decodeMsgId(p_pkg_str+offset, 5, &(p_del_ack->msg_id));
1547                         break;
1548                 case 0x01:
1549                         /* User Data */
1550                         offset += 2;
1551                         tmp_len = p_pkg_str[offset-1];
1552                         memset(tmp_str, 0x00, sizeof(tmp_str));
1553                         memcpy(tmp_str, p_pkg_str+offset, tmp_len);
1554
1555                         decodeUserData(tmp_str, tmp_len, &(p_del_ack->user_data));
1556
1557                         offset += tmp_len;
1558                         break;
1559                 case 0x03:
1560                         /* Message Center Time Stamp */
1561                         offset += 2;
1562                         offset += decodeAbsTime(p_pkg_str+offset, &(p_del_ack->time_stamp));
1563                         break;
1564                 case 0x10:
1565                         /* Multiple Encoding User Data */
1566                         offset += 2;
1567                         tmp_len = p_pkg_str[offset-1];
1568
1569                         /* TODO */
1570
1571                         offset += tmp_len;
1572                         break;
1573                 case 0x14:
1574                         /* Message Status */
1575                         offset += 2;
1576                         p_del_ack->msg_status = (sms_3gpp2_status_code_t)p_pkg_str[offset++];
1577                         break;
1578                 default:
1579                         break;
1580                 }
1581         }
1582
1583         MSG_END();
1584 }
1585
1586
1587 void Sms3gpp2MsgCodec::decodeCBBearerData(const unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_telesvc_msg_s *p_telesvc, bool isCMAS)
1588 {
1589         MSG_BEGIN();
1590
1591         int offset = 0, tmp_len;
1592         unsigned char tmp_str[pkg_len+1];
1593
1594         while (offset < pkg_len) {
1595                 if (p_pkg_str[offset] == 0x00) {
1596                         /* Message Identifier */
1597                         p_telesvc->type = SMS_TYPE_DELIVER;
1598                         offset += decodeMsgId(p_pkg_str+offset, 5, &(p_telesvc->data.deliver.msg_id));
1599                 } else if (p_pkg_str[offset] == 0x01) {
1600                         /* User Data */
1601                         offset += 2;
1602                         tmp_len = p_pkg_str[offset-1];
1603                         memset(tmp_str, 0x00, sizeof(tmp_str));
1604                         memcpy(tmp_str, p_pkg_str+offset, tmp_len);
1605
1606                         if (isCMAS)
1607                                 decodeCMASData(tmp_str, tmp_len, &(p_telesvc->data.deliver.cmas_data));
1608                         else
1609                                 decodeUserData(tmp_str, tmp_len, &(p_telesvc->data.deliver.user_data));
1610
1611                         offset += tmp_len;
1612                 } else if (p_pkg_str[offset] == 0x03) {
1613                         /* Message Center Time Stamp */
1614                         offset += 2;
1615                         offset += decodeAbsTime(p_pkg_str+offset, &(p_telesvc->data.deliver.time_stamp));
1616                 } else if (p_pkg_str[offset] == 0x04) {
1617                         /* Validity Period - Absolute */
1618                         offset += 2;
1619                         p_telesvc->data.deliver.val_period.format = SMS_3GPP2_TIME_ABSOLUTE;
1620                         offset += decodeAbsTime(p_pkg_str+offset, &(p_telesvc->data.deliver.val_period.time.abs_time));
1621                 } else if (p_pkg_str[offset] == 0x05) {
1622                         /* Validity Period - Relative */
1623                         offset += 2;
1624                         p_telesvc->data.deliver.val_period.format = SMS_3GPP2_TIME_RELATIVE;
1625                         p_telesvc->data.deliver.val_period.time.rel_time.rel_time = (sms_3gpp2_relative_time_t)p_pkg_str[offset++];
1626                 } else if (p_pkg_str[offset] == 0x08) {
1627                         /* Priority indicator */
1628                         offset += 2;
1629                         p_telesvc->data.deliver.priority = (sms_3gpp2_priority_indicator_t)((p_pkg_str[offset++] & 0xc0) >> 6);
1630                 } else if (p_pkg_str[offset] == 0x0c) {
1631                         /* Alert on Message Delivery */
1632                         offset += 2;
1633
1634                         p_telesvc->data.deliver.alert_priority = (sms_3gpp2_alert_priority_t)((p_pkg_str[offset++] & 0xc0) >> 6);
1635                 } else if (p_pkg_str[offset] == 0x0d) {
1636                         /* Language Indicator */
1637                         offset += 2;
1638
1639                         p_telesvc->data.deliver.language = (sms_3gpp2_language_type_t)p_pkg_str[offset++];
1640                 } else if (p_pkg_str[offset] == 0x0e) {
1641                         /* Call-Back Number */
1642                         offset += 2;
1643                         tmp_len = p_pkg_str[offset-1];
1644
1645                         decodeCallBackNum(&p_pkg_str[offset], tmp_len, &(p_telesvc->data.deliver.callback_number));
1646
1647                         offset += tmp_len;
1648                 } else if (p_pkg_str[offset] == 0x0f) {
1649                         /* Message Display Mode */
1650                         offset += 2;
1651
1652                         p_telesvc->data.deliver.display_mode = (sms_3gpp2_display_mode_t)((p_pkg_str[offset++] & 0xc0) >> 6);
1653                 } else if (p_pkg_str[offset] == 0x10) {
1654                         /* Multiple Encoding User Data */
1655                         offset += 2;
1656                         tmp_len = p_pkg_str[offset-1];
1657
1658                         /* TODO */
1659
1660                         offset += tmp_len;
1661                 }
1662         }
1663
1664         MSG_END();
1665 }
1666
1667
1668 int Sms3gpp2MsgCodec::decodeTeleId(const unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_trans_telesvc_id_t *tele_id)
1669 {
1670         int offset = 0;
1671         unsigned short tmp_param_s;
1672
1673         offset += 2;
1674
1675     _copy_char_to_short(&tmp_param_s, &p_pkg_str[offset]);
1676
1677         switch (tmp_param_s) {
1678         case SMS_TRANS_TELESVC_CMT_91:
1679                 *tele_id = SMS_TRANS_TELESVC_CMT_91;
1680                 break;
1681         case SMS_TRANS_TELESVC_CPT_95:
1682                 *tele_id = SMS_TRANS_TELESVC_CPT_95;
1683                 break;
1684         case SMS_TRANS_TELESVC_CMT_95:
1685                 *tele_id = SMS_TRANS_TELESVC_CMT_95;
1686                 break;
1687         case SMS_TRANS_TELESVC_VMN_95:
1688                 *tele_id = SMS_TRANS_TELESVC_VMN_95;
1689                 break;
1690         case SMS_TRANS_TELESVC_WAP:
1691                 *tele_id = SMS_TRANS_TELESVC_WAP;
1692                 break;
1693         case SMS_TRANS_TELESVC_WEMT:
1694                 *tele_id = SMS_TRANS_TELESVC_WEMT;
1695                 break;
1696         case SMS_TRANS_TELESVC_SCPT:
1697                 *tele_id = SMS_TRANS_TELESVC_SCPT;
1698                 break;
1699         case SMS_TRANS_TELESVC_CATPT:
1700                 *tele_id = SMS_TRANS_TELESVC_CATPT;
1701                 break;
1702         default:
1703                 *tele_id = SMS_TRANS_TELESVC_RESERVED;
1704                 break;
1705         }
1706
1707         return offset+2;
1708 }
1709
1710
1711 int Sms3gpp2MsgCodec::decodeSvcCtg(const unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_trans_svc_ctg_t *svc_ctg)
1712 {
1713         int offset = 0;
1714         unsigned short tmp_param_s;
1715
1716         offset += 2;
1717
1718         _copy_char_to_short(&tmp_param_s, &p_pkg_str[offset]);
1719         if ((tmp_param_s >= SMS_TRANS_SVC_CTG_UNKNOWN && tmp_param_s <= SMS_TRANS_SVC_CTG_KDDI_CORP_MAX1)
1720                 || (tmp_param_s >= SMS_TRANS_SVC_CTG_KDDI_CORP_MIN2 && tmp_param_s <= SMS_TRANS_SVC_CTG_KDDI_CORP_MAX2)
1721                 || (tmp_param_s >= SMS_TRANS_SVC_CTG_KDDI_CORP_MIN3 && tmp_param_s <= SMS_TRANS_SVC_CTG_KDDI_CORP_MAX3)) {
1722                 *svc_ctg = (sms_3gpp2_trans_svc_ctg_t)tmp_param_s;
1723         } else {
1724                 *svc_ctg = SMS_TRANS_SVC_CTG_RESERVED;
1725         }
1726
1727         return offset+2;
1728 }
1729
1730
1731 int Sms3gpp2MsgCodec::decodeAddress(const unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_trans_addr_s *addr)
1732 {
1733         int offset = 0, tmp_len = 0;
1734         unsigned char tmp_str[pkg_len+1];
1735
1736         tmp_len = p_pkg_str[++offset];
1737
1738         memset(tmp_str, 0x00, sizeof(tmp_str));
1739         memcpy(tmp_str, p_pkg_str+offset+1, tmp_len);
1740
1741         offset += (tmp_len+1);
1742
1743         if (tmp_str[0] & 0x80)
1744                 addr->digit_mode = true;
1745         else
1746                 addr->digit_mode = false;
1747
1748         if (tmp_str[0] & 0x40)
1749                 addr->number_mode = true;
1750         else
1751                 addr->number_mode = false;
1752
1753         _shiftNBit_for_decode(tmp_str, tmp_len, 2);
1754
1755         if (addr->digit_mode == false) {
1756                 addr->addr_len = tmp_str[0];
1757
1758                 memset(addr->szData, 0x00, sizeof(addr->szData));
1759
1760                 for (unsigned int i = 0; i < addr->addr_len; i++) {
1761                         switch (tmp_str[1] & 0xf0) {
1762                         case 0x10:
1763                         case 0x20:
1764                         case 0x30:
1765                         case 0x40:
1766                         case 0x50:
1767                         case 0x60:
1768                         case 0x70:
1769                         case 0x80:
1770                         case 0x90:
1771                                 addr->szData[i] = ((tmp_str[1] & 0xf0) >> 4) + '0';
1772                                 break;
1773                         case 0x00:
1774                                 /* sprint issue */
1775                         case 0xa0:
1776                                 addr->szData[i] = '0';
1777                                 break;
1778                         case 0xb0:
1779                                 addr->szData[i] = '*';
1780                                 break;
1781                         case 0xc0:
1782                                 addr->szData[i] = '#';
1783                                 break;
1784                         default:
1785                                 break;
1786                         }
1787
1788                         _shiftNBit_for_decode(tmp_str, tmp_len, 4);
1789                 }
1790         } else if (addr->digit_mode == true) {
1791                 if (addr->number_mode == false) {
1792                         /* digit mode = 1, number mode = 0 */
1793                         switch (tmp_str[0] & 0xe0) {
1794                         case 0x00:
1795                                 addr->number_type = SMS_NUMBER_TYPE_UNKNOWN;
1796                                 break;
1797                         case 0x20:
1798                                 addr->number_type = SMS_NUMBER_TYPE_INTERNATIONAL;
1799                                 break;
1800                         case 0x40:
1801                                 addr->number_type = SMS_NUMBER_TYPE_NATIONAL;
1802                                 break;
1803                         case 0x60:
1804                                 addr->number_type = SMS_NUMBER_TYPE_NETWORK_SPECIFIC;
1805                                 break;
1806                         case 0x80:
1807                                 addr->number_type = SMS_NUMBER_TYPE_SUBSCRIBER;
1808                                 break;
1809                         case 0xa0:
1810                                 addr->number_type = SMS_NUMBER_TYPE_RESERVED_5;
1811                                 break;
1812                         case 0xc0:
1813                                 addr->number_type = SMS_NUMBER_TYPE_ABBREVIATED;
1814                                 break;
1815                         case 0xe0:
1816                                 addr->number_type = SMS_NUMBER_TYPE_RESERVED_7;
1817                                 break;
1818                         }
1819
1820                         _shiftNBit_for_decode(tmp_str, tmp_len, 3);
1821
1822                         switch (tmp_str[0] & 0xf0) {
1823                         case 0x00:
1824                                 addr->number_plan = SMS_3GPP2_NPI_UNKNOWN;
1825                                 break;
1826                         case 0x10:
1827                                 addr->number_plan = SMS_3GPP2_NPI_ISDN;
1828                                 break;
1829                         case 0x30:
1830                                 addr->number_plan = SMS_3GPP2_NPI_DATA;
1831                                 break;
1832                         case 0x40:
1833                                 addr->number_plan = SMS_3GPP2_NPI_TELEX;
1834                                 break;
1835                         case 0x90:
1836                                 addr->number_plan = SMS_3GPP2_NPI_PRIVATE;
1837                                 break;
1838                         default:
1839                                 addr->number_plan = SMS_3GPP2_NPI_RESERVED;
1840                                 break;
1841                         }
1842
1843                         _shiftNBit_for_decode(tmp_str, tmp_len, 4);
1844                 } else if (addr->number_mode == true) {
1845                         /* digit mode = 1, number mode = 1 */
1846                         switch (tmp_str[0] & 0xe0) {
1847                         case 0x00:
1848                                 addr->number_type = SMS_TRANS_DNET_UNKNOWN;
1849                                 break;
1850                         case 0x20:
1851                                 addr->number_type = SMS_TRANS_DNET_INTERNET_PROTOCOL;
1852                                 break;
1853                         case 0x40:
1854                                 addr->number_type = SMS_TRANS_DNET_INTERNET_MAIL_ADDR;
1855                                 break;
1856                         default:
1857                                 addr->number_type = SMS_TRANS_DNET_RESERVED;
1858                                 break;
1859                         }
1860
1861                         _shiftNBit_for_decode(tmp_str, tmp_len, 3);
1862                 }
1863
1864                 addr->addr_len = tmp_str[0];
1865
1866                 memset(addr->szData, 0x00, sizeof(addr->szData));
1867                 memcpy(addr->szData, &tmp_str[1], addr->addr_len);
1868         }
1869
1870         return offset;
1871 }
1872
1873
1874 int Sms3gpp2MsgCodec::decodeSubAddress(const unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_trans_sub_addr_s *sub_addr)
1875 {
1876         int offset = 0, tmp_len = 0;
1877         unsigned char tmp_str[pkg_len+1];
1878
1879         tmp_len = p_pkg_str[++offset];
1880         memset(tmp_str, 0x00, sizeof(tmp_str));
1881         memcpy(tmp_str, p_pkg_str+offset+1, tmp_len);
1882
1883         offset += (tmp_len+1);
1884
1885         switch (tmp_str[0] & 0xe0) {
1886         case 0x00:
1887                 sub_addr->type = SMS_TRANS_SUB_ADDR_NSAP;
1888                 break;
1889         case 0x20:
1890                 sub_addr->type = SMS_TRANS_SUB_ADDR_USER;
1891                 break;
1892         default:
1893                 sub_addr->type = SMS_TRANS_SUB_ADDR_RESERVED;
1894                 break;
1895         }
1896
1897         if (tmp_str[0] & 0x10)
1898                 sub_addr->odd = true;
1899         else
1900                 sub_addr->odd = false;
1901
1902         _shiftNBit_for_decode(tmp_str, tmp_len, 4);
1903         memset(sub_addr->szData, 0x00, sizeof(sub_addr->szData));
1904         memcpy(sub_addr->szData, tmp_str+1, tmp_str[0]);
1905
1906         return offset;
1907 }
1908
1909
1910 int Sms3gpp2MsgCodec::decodeMsgId(const unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_trans_msg_id_s *p_msg_id)
1911 {
1912         int offset = 0;
1913         unsigned short tmp_param_s;
1914         unsigned char tmp_str[pkg_len+1];
1915
1916         memset(tmp_str, 0x00, sizeof(tmp_str));
1917         memcpy(tmp_str, &p_pkg_str[offset+2], 3);
1918
1919         _shiftNBit_for_decode(tmp_str, 3, 4);
1920
1921         memset(&tmp_param_s, 0x00, sizeof(unsigned short));
1922         _copy_char_to_short(&tmp_param_s, tmp_str);
1923
1924         p_msg_id->msg_id = tmp_param_s;
1925         if (tmp_str[2] & 0x80)
1926                 p_msg_id->header_ind = true;
1927         else
1928                 p_msg_id->header_ind = false;
1929
1930         offset += 5;
1931
1932         return offset;
1933 }
1934
1935
1936 void Sms3gpp2MsgCodec::decodeCallBackNum(const unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_telesvc_addr_s *p_callback)
1937 {
1938         int offset = 0;
1939         unsigned char tmp_str[pkg_len+1];
1940
1941         if (p_pkg_str[offset] & 0x80) {
1942                 p_callback->digit_mode = true;
1943
1944                 switch (p_pkg_str[offset] & 0x70) {
1945                 case 0x00:
1946                         p_callback->number_type = SMS_NUMBER_TYPE_UNKNOWN;
1947                         break;
1948                 case 0x10:
1949                         p_callback->number_type = SMS_NUMBER_TYPE_INTERNATIONAL;
1950                         break;
1951                 case 0x20:
1952                         p_callback->number_type = SMS_NUMBER_TYPE_NATIONAL;
1953                         break;
1954                 case 0x30:
1955                         p_callback->number_type = SMS_NUMBER_TYPE_NETWORK_SPECIFIC;
1956                         break;
1957                 case 0x40:
1958                         p_callback->number_type = SMS_NUMBER_TYPE_SUBSCRIBER;
1959                         break;
1960                 case 0x50:
1961                         p_callback->number_type = SMS_NUMBER_TYPE_RESERVED_5;
1962                         break;
1963                 case 0x60:
1964                         p_callback->number_type = SMS_NUMBER_TYPE_ABBREVIATED;
1965                         break;
1966                 case 0x70:
1967                         p_callback->number_type = SMS_NUMBER_TYPE_RESERVED_7;
1968                         break;
1969                 default:
1970                         break;
1971                 }
1972
1973                 switch (p_pkg_str[offset++] & 0x0f) {
1974                 case 0x00:
1975                         p_callback->number_plan = SMS_3GPP2_NPI_UNKNOWN;
1976                         break;
1977                 case 0x01:
1978                         p_callback->number_plan = SMS_3GPP2_NPI_ISDN;
1979                         break;
1980                 case 0x03:
1981                         p_callback->number_plan = SMS_3GPP2_NPI_DATA;
1982                         break;
1983                 case 0x04:
1984                         p_callback->number_plan = SMS_3GPP2_NPI_TELEX;
1985                         break;
1986                 case 0x09:
1987                         p_callback->number_plan = SMS_3GPP2_NPI_PRIVATE;
1988                         break;
1989                 case 0x0f:
1990                 default:
1991                         p_callback->number_plan = SMS_3GPP2_NPI_RESERVED;
1992                         break;
1993                 }
1994
1995                 p_callback->addr_len = p_pkg_str[offset++];
1996                 memset(p_callback->szData, 0x00, sizeof(p_callback->szData));
1997
1998                 if (p_callback->number_type == SMS_NUMBER_TYPE_INTERNATIONAL) {
1999                         memcpy(&(p_callback->szData[1]), p_pkg_str+offset, p_callback->addr_len);
2000                         if (p_callback->szData[1] != '\0') {
2001                                 p_callback->szData[0] = '+';
2002                         }
2003                 } else {
2004                         memcpy(p_callback->szData, p_pkg_str+offset, p_callback->addr_len);
2005                 }
2006         } else {
2007                 p_callback->digit_mode = false;
2008
2009                 memset(tmp_str, 0x00, sizeof(tmp_str));
2010                 memcpy(tmp_str, p_pkg_str+offset, pkg_len);
2011
2012                 _shiftNBit_for_decode(tmp_str, pkg_len, 1);
2013
2014                 p_callback->addr_len = tmp_str[0];
2015
2016                 memset(p_callback->szData, 0x00, sizeof(p_callback->szData));
2017
2018                 for (unsigned int i = 0; i < p_callback->addr_len; i++) {
2019                         switch (tmp_str[1] & 0xf0) {
2020                         case 0x10:
2021                         case 0x20:
2022                         case 0x30:
2023                         case 0x40:
2024                         case 0x50:
2025                         case 0x60:
2026                         case 0x70:
2027                         case 0x80:
2028                         case 0x90:
2029                                 p_callback->szData[i] = ((tmp_str[1] & 0xf0) >> 4) + '0';
2030                                 break;
2031                         case 0xa0:
2032                                 p_callback->szData[i] = '0';
2033                                 break;
2034                         case 0xb0:
2035                                 p_callback->szData[i] = '*';
2036                                 break;
2037                         case 0xc0:
2038                                 p_callback->szData[i] = '#';
2039                                 break;
2040                         default :
2041                                 break;
2042                         }
2043
2044                         _shiftNBit_for_decode(tmp_str, pkg_len, 4);
2045                 }
2046         }
2047 }
2048
2049
2050 int Sms3gpp2MsgCodec::decodeAbsTime(const unsigned char *p_pkg_str, sms_3gpp2_time_abs_s *p_time_abs)
2051 {
2052         int offset = 0;
2053
2054         p_time_abs->year = (((p_pkg_str[offset] & 0xf0) >> 4) * 10) + (p_pkg_str[offset] & 0x0f);
2055         offset++;
2056         p_time_abs->month = (((p_pkg_str[offset] & 0xf0) >> 4) * 10) + (p_pkg_str[offset] & 0x0f);
2057         offset++;
2058         p_time_abs->day = (((p_pkg_str[offset] & 0xf0) >> 4) * 10) + (p_pkg_str[offset] & 0x0f);
2059         offset++;
2060         p_time_abs->hours = (((p_pkg_str[offset] & 0xf0) >> 4) * 10) + (p_pkg_str[offset] & 0x0f);
2061         offset++;
2062         p_time_abs->minutes = (((p_pkg_str[offset] & 0xf0) >> 4) * 10) + (p_pkg_str[offset] & 0x0f);
2063         offset++;
2064         p_time_abs->seconds = (((p_pkg_str[offset] & 0xf0) >> 4) * 10) + (p_pkg_str[offset] & 0x0f);
2065         offset++;
2066
2067         return offset;
2068 }
2069
2070
2071 int Sms3gpp2MsgCodec::encodeUserData(const unsigned char* src, unsigned char *dest, int src_size)
2072 {
2073         int i, j = 0;
2074         int shift = 0;
2075
2076         unsigned char *tmp = (unsigned char *)calloc(1, src_size+1);
2077         for (i = 0; i < src_size; i++) {
2078                 tmp[i] = src[i] << 1;
2079         }
2080
2081         if (tmp == NULL){
2082                 MSG_ERR("failed to allocate memory");
2083                 goto END;
2084         }
2085
2086         for (i = 0; i < src_size; i++) {
2087                 shift = j % 7;
2088                 dest[j++] = (tmp[i] << shift) + (tmp[i+1] >> (7-shift));
2089                 if (shift == 6) {
2090                         i++;
2091                 }
2092         }
2093
2094         if (tmp) {
2095                 free(tmp);
2096                 tmp = NULL;
2097         }
2098
2099 END:
2100         return j;
2101 }
2102
2103
2104 void Sms3gpp2MsgCodec::decodeCMASData(unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_telesvc_cmasdata_s *p_cmas)
2105 {
2106         MSG_BEGIN();
2107
2108         int offset = 0, tmp_len = 0;
2109         unsigned char tmp_str[pkg_len+1];
2110
2111         if ((p_pkg_str[offset] & 0xf8) != 0x00) {
2112                 MSG_ERR("Wrong Encode Type = [%d]!! The type must be 0", (p_pkg_str[offset]&0xf8)>>3);
2113                 return;
2114         } else {
2115                 _shiftNBit_for_decode(p_pkg_str, pkg_len, 5);
2116
2117                 offset++;
2118
2119                 if (p_pkg_str[offset++] != 0x00) {
2120                         MSG_ERR("Wrong protocol version = [%d]!! This field must be 0", p_pkg_str[offset-1]);
2121                         p_cmas->is_wrong_recode_type = TRUE;
2122                         return;
2123                 }
2124
2125                 while (offset < pkg_len - 1) {
2126                         if (p_pkg_str[offset] == 0x00) {
2127                                 MSG_DEBUG("Type 0 Decode!");
2128                                 offset++;
2129                                 tmp_len = p_pkg_str[offset++];
2130                                 MSG_DEBUG("Type 0 length = [%d]", tmp_len);
2131                                 memset(tmp_str, 0x00, sizeof(tmp_str));
2132                                 memcpy(tmp_str, p_pkg_str+offset, tmp_len);
2133
2134                                 switch (tmp_str[0] & 0xf8) {
2135                                 case 0x00:
2136                                         p_cmas->encode_type = SMS_ENCODE_OCTET;
2137                                         break;
2138                                 case 0x08:
2139                                         p_cmas->encode_type = SMS_ENCODE_EPM;
2140                                         break;
2141                                 case 0x10:
2142                                         p_cmas->encode_type = SMS_ENCODE_7BIT_ASCII;
2143                                         break;
2144                                 case 0x18:
2145                                         p_cmas->encode_type = SMS_ENCODE_IA5;
2146                                         break;
2147                                 case 0x20:
2148                                         p_cmas->encode_type = SMS_ENCODE_UNICODE;
2149                                         break;
2150                                 case 0x28:
2151                                         p_cmas->encode_type = SMS_ENCODE_SHIFT_JIS;
2152                                         break;
2153                                 case 0x30:
2154                                         p_cmas->encode_type = SMS_ENCODE_KOREAN;
2155                                         break;
2156                                 case 0x38:
2157                                         p_cmas->encode_type = SMS_ENCODE_LATIN_HEBREW;
2158                                         break;
2159                                 case 0x40:
2160                                         p_cmas->encode_type = SMS_ENCODE_LATIN;
2161                                         break;
2162                                 case 0x48:
2163                                         p_cmas->encode_type = SMS_ENCODE_GSM7BIT;
2164                                         break;
2165                                 case 0x50:
2166                                         p_cmas->encode_type = SMS_ENCODE_GSMDCS;
2167                                         break;
2168                                 case 0x80:
2169                                         /* reserved value, but SKT use this value for KSC5601 */
2170                                         p_cmas->encode_type = SMS_ENCODE_EUCKR;
2171                                         break;
2172                                 default :
2173                                         p_cmas->encode_type = SMS_ENCODE_RESERVED;
2174                                         break;
2175                                 }
2176                                 _shiftNBit_for_decode(tmp_str, tmp_len, 5);
2177
2178                                 switch (p_cmas->encode_type) {
2179                                 case SMS_ENCODE_7BIT_ASCII:
2180                                 case SMS_ENCODE_IA5:
2181                                 case SMS_ENCODE_GSM7BIT:
2182                                         memset(p_cmas->alert_text, 0x00, sizeof(p_cmas->alert_text));
2183                                         p_cmas->data_len = (tmp_len*8-5) / 7;
2184                                         for (unsigned int i = 0; i < p_cmas->data_len; i++) {
2185                                                 p_cmas->alert_text[i] = tmp_str[0] >> 1;
2186                                                 _shiftNBit_for_decode(tmp_str, tmp_len, 7);
2187                                         }
2188                                         break;
2189                                 case SMS_ENCODE_EPM:
2190                                         break;
2191                                 case SMS_ENCODE_GSMDCS:
2192                                         break;
2193                                 default:
2194                                         p_cmas->data_len = tmp_len - 1;
2195                                         memset(p_cmas->alert_text, 0x00, sizeof(p_cmas->alert_text));
2196                                         memcpy(p_cmas->alert_text, tmp_str+offset, tmp_len-1);
2197                                         break;
2198                                 }
2199
2200                                 offset += tmp_len;
2201                         } else if (p_pkg_str[offset] == 0x01) {
2202                                 MSG_DEBUG("Type 1 Decode!");
2203                                 offset += 2;
2204                                 tmp_len = p_pkg_str[offset-1];
2205                                 MSG_DEBUG("Type 1 length = [%d]", tmp_len);
2206                                 p_cmas->category = (sms_3gpp2_cmae_category_t)p_pkg_str[offset++];
2207                                 p_cmas->response_type = (sms_3gpp2_cmae_response_type_t)p_pkg_str[offset++];
2208                                 p_cmas->severity = (sms_3gpp2_cmae_severity_t)(p_pkg_str[offset] >> 4);
2209                                 p_cmas->urgency = (sms_3gpp2_cmae_urgency_t)(p_pkg_str[offset++] & 0x0f);
2210                                 p_cmas->certainty = (sms_3gpp2_cmae_certainty_t)(p_pkg_str[offset++] >> 4);
2211                         } else if (p_pkg_str[offset] == 0x02) {
2212                                 MSG_DEBUG("Type 2 Decode!");
2213                                 offset += 2;
2214                                 tmp_len = p_pkg_str[offset-1];
2215                                 MSG_DEBUG("Type 2 length = [%d]", tmp_len);
2216                                 _copy_char_to_short(&(p_cmas->id), p_pkg_str+offset);
2217                                 offset += 2;
2218                                 p_cmas->alert_handle = (sms_3gpp2_cmae_alert_handle_t)p_pkg_str[offset++];
2219                                 offset += decodeAbsTime(p_pkg_str+offset, &(p_cmas->expires));
2220                                 p_cmas->language = (sms_3gpp2_language_type_t)p_pkg_str[offset++];
2221                         }
2222
2223                         MSG_DEBUG("offset = [%d], pkg_len = [%d]", offset, pkg_len);
2224                 }
2225         }
2226
2227         MSG_END();
2228 }
2229
2230
2231 void Sms3gpp2MsgCodec::decodeUserData(unsigned char *p_pkg_str, int pkg_len, sms_3gpp2_telesvc_userdata_s *p_user)
2232 {
2233         switch (p_pkg_str[0] & 0xf8) {
2234         case 0x00:
2235                 p_user->encode_type = SMS_ENCODE_OCTET;
2236                 break;
2237         case 0x08:
2238                 p_user->encode_type = SMS_ENCODE_EPM;
2239                 break;
2240         case 0x10:
2241                 p_user->encode_type = SMS_ENCODE_7BIT_ASCII;
2242                 break;
2243         case 0x18:
2244                 p_user->encode_type = SMS_ENCODE_IA5;
2245                 break;
2246         case 0x20:
2247                 p_user->encode_type = SMS_ENCODE_UNICODE;
2248                 break;
2249         case 0x28:
2250                 p_user->encode_type = SMS_ENCODE_SHIFT_JIS;
2251                 break;
2252         case 0x30:
2253                 p_user->encode_type = SMS_ENCODE_KOREAN;
2254                 break;
2255         case 0x38:
2256                 p_user->encode_type = SMS_ENCODE_LATIN_HEBREW;
2257                 break;
2258         case 0x40:
2259                 p_user->encode_type = SMS_ENCODE_LATIN;
2260                 break;
2261         case 0x48:
2262                 p_user->encode_type = SMS_ENCODE_GSM7BIT;
2263                 break;
2264         case 0x50:
2265                 p_user->encode_type = SMS_ENCODE_GSMDCS;
2266                 break;
2267         case 0x80:
2268                 /* reserved value, but SKT use this value for KSC5601 */
2269                 p_user->encode_type = SMS_ENCODE_EUCKR;
2270                 break;
2271         default :
2272                 p_user->encode_type = SMS_ENCODE_RESERVED;
2273                 break;
2274         }
2275
2276         _shiftNBit_for_decode(p_pkg_str, pkg_len, 5);
2277
2278         if (p_user->encode_type == SMS_ENCODE_EPM || p_user->encode_type == SMS_ENCODE_GSMDCS) {
2279                 p_user->msg_type = p_pkg_str[0];
2280                 _shiftNBit_for_decode(p_pkg_str, pkg_len, 8);
2281         }
2282
2283         p_user->data_len = p_pkg_str[0];
2284         switch (p_user->encode_type) {
2285         case SMS_ENCODE_7BIT_ASCII:
2286         case SMS_ENCODE_IA5:
2287                 memset(p_user->user_data, 0x00, sizeof(p_user->user_data));
2288                 for (unsigned int i = 0; i < p_user->data_len; i++) {
2289                         p_user->user_data[i] = p_pkg_str[1] >> 1;
2290                         _shiftNBit_for_decode(p_pkg_str, pkg_len, 7);
2291                 }
2292                 break;
2293         case SMS_ENCODE_GSM7BIT:
2294                 memset(p_user->user_data, 0x00, sizeof(p_user->user_data));
2295                 UnpackGSM7bitData(&(p_pkg_str[1]), p_user->user_data, p_user->data_len);
2296                 break;
2297         case SMS_ENCODE_EPM:
2298                 break;
2299         case SMS_ENCODE_GSMDCS:
2300                 break;
2301         case SMS_ENCODE_UNICODE:
2302                 p_user->data_len*=2;
2303                 memset(p_user->user_data, 0x00, sizeof(p_user->user_data));
2304                 memcpy(p_user->user_data, p_pkg_str+1, p_user->data_len);
2305                 break;
2306         default:
2307                 memset(p_user->user_data, 0x00, sizeof(p_user->user_data));
2308                 memcpy(p_user->user_data, p_pkg_str+1, p_user->data_len);
2309                 break;
2310         }
2311 }
2312
2313
2314 sms_3gpp2_message_type_t Sms3gpp2MsgCodec::findMsgType(const unsigned char *p_pkg_str, int pkg_len)
2315 {
2316         int offset = 0;
2317         while (offset < pkg_len) {
2318                 if (p_pkg_str[offset] == 0x00) {
2319                         return (sms_3gpp2_message_type_t)((p_pkg_str[offset + 2] & 0xf0) >> 4);
2320                 }
2321                 offset += (p_pkg_str[offset+1]+2);
2322         }
2323
2324         return SMS_TYPE_MAX_VALUE;
2325 }