Boot Linux faster!

Check our new training course

Boot Linux faster!

Check our new training course
and Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2010-2011  Nokia Corporation and/or its subsidiary(-ies).
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <glib.h>

#include "cdma-smsutil.h"

#define uninitialized_var(x) x = x

enum cdma_sms_rec_flag {
	CDMA_SMS_REC_FLAG_MANDATORY =	1,
};

typedef gboolean (*rec_handler)(const guint8 *, guint8, void *);

struct simple_iter {
	guint8 max;
	const guint8 *pdu;
	guint8 pos;
	guint8 id;
	guint8 len;
	const guint8 *data;
};

static void simple_iter_init(struct simple_iter *iter,
				const guint8 *pdu, guint8 len)
{
	iter->pdu = pdu;
	iter->max = len;
	iter->pos = 0;
	iter->id = 0;
	iter->len = 0;
	iter->data = NULL;
}

static gboolean simple_iter_next(struct simple_iter *iter)
{
	const guint8 *pdu = iter->pdu + iter->pos;
	const guint8 *end = iter->pdu + iter->max;
	guint8 id;
	guint8 len;

	if (pdu == end)
		return FALSE;

	id = *pdu;
	pdu++;

	if (pdu == end)
		return FALSE;

	len = *pdu++;

	if (pdu + len > end)
		return FALSE;

	iter->id = id;
	iter->len = len;
	iter->data = pdu;

	iter->pos = pdu + len - iter->pdu;

	return TRUE;
}

static guint8 simple_iter_get_id(struct simple_iter *iter)
{
	return iter->id;
}

static guint8 simple_iter_get_length(struct simple_iter *iter)
{
	return iter->len;
}

static const guint8 *simple_iter_get_data(struct simple_iter *iter)
{
	return iter->data;
}

static inline void set_bitmap(guint32 *bitmap, guint8 pos)
{
	*bitmap = *bitmap | (1 << pos);
}

/* Unpacks the byte stream. The field has to be <= 8 bits. */
static guint8 bit_field_unpack(const guint8 *buf, guint16 offset, guint8 nbit)
{
	guint8 bit_pos;
	guint8 val = 0;
	const guint8 *pdu;

	pdu = buf + (offset >> 3);
	bit_pos = 8 - (offset & 0x7);

	/* Field to be extracted is within current byte */
	if (nbit <= bit_pos)
		return (*pdu >> (bit_pos - nbit)) & ((1 << nbit) - 1);

	/* Field to be extracted crossing two bytes */
	val = *pdu & ((1 << bit_pos) - 1);
	nbit -= bit_pos;
	pdu++;

	return (val << nbit) | (*pdu >> (8 - nbit));
}

/* Convert CDMA DTMF digits into a string */
static gboolean dtmf_to_ascii(char *buf, const guint8 *addr,
					guint8 num_fields)
{
	/*
	 * Mapping from binary DTMF code to the digit it represents.
	 * As defined in Table 2.7.1.3.2.4-4 of 3GPP2 C.S0005-E v2.0.
	 * Note, 0 is NOT a valid value and not mapped to
	 * any valid DTMF digit.
	 */
	static const char dtmf_digits[13] = {0, '1', '2', '3', '4', '5', '6',
						'7', '8', '9', '0', '*', '#'};
	guint8 index;
	guint8 value;

	for (index = 0; index < num_fields; index++) {
		if (addr[index] == 0 || addr[index] > 12)
			return FALSE;  /* Invalid digit in address field */

		value = addr[index];
		buf[index] = dtmf_digits[value];
	}

	buf[index] = 0; /* Make it NULL terminated string */

	return TRUE;
}

const char *cdma_sms_address_to_string(const struct cdma_sms_address *addr)
{
	static char buf[CDMA_SMS_MAX_ADDR_FIELDS + 1];

	/* TODO: Only support CDMA_SMS_DIGIT_MODE_4BIT_DTMF currently */
	switch (addr->digit_mode) {
	case CDMA_SMS_DIGIT_MODE_4BIT_DTMF:
		if (dtmf_to_ascii(buf, addr->address,
					addr->num_fields) == TRUE)
			return buf;
		else
			return NULL;
	case CDMA_SMS_DIGIT_MODE_8BIT_ASCII:
		return NULL;
	}

	return NULL;
}

/* Decode Teleservice ID */
static gboolean cdma_sms_decode_teleservice(const guint8 *buf, guint8 len,
								void *data)
{
	enum cdma_sms_teleservice_id *id = data;

	*id = bit_field_unpack(buf, 0, 8) << 8 |
				bit_field_unpack(buf, 8, 8);

	switch (*id) {
	case CDMA_SMS_TELESERVICE_ID_CMT91:
	case CDMA_SMS_TELESERVICE_ID_WPT:
	case CDMA_SMS_TELESERVICE_ID_WMT:
	case CDMA_SMS_TELESERVICE_ID_VMN:
	case CDMA_SMS_TELESERVICE_ID_WAP:
	case CDMA_SMS_TELESERVICE_ID_WEMT:
	case CDMA_SMS_TELESERVICE_ID_SCPT:
	case CDMA_SMS_TELESERVICE_ID_CATPT:
		return TRUE;
	}

	return FALSE; /* Invalid teleservice type */
}

/* Decode Address parameter record */
static gboolean cdma_sms_decode_addr(const guint8 *buf, guint8 len,
							void *data)
{
	struct cdma_sms_address *addr = data;
	guint16 bit_offset = 0;
	guint8  chari_len;
	guint16 total_num_bits = len * 8;
	guint8  index;

	addr->digit_mode = bit_field_unpack(buf, bit_offset, 1);
	bit_offset += 1;

	addr->number_mode = bit_field_unpack(buf, bit_offset, 1);
	bit_offset += 1;

	if (addr->digit_mode == CDMA_SMS_DIGIT_MODE_8BIT_ASCII) {
		if (addr->number_mode == CDMA_SMS_NUM_MODE_DIGIT)
			addr->digi_num_type =
				bit_field_unpack(buf, bit_offset, 3);
		else
			addr->data_nw_num_type =
				bit_field_unpack(buf, bit_offset, 3);

		bit_offset += 3;

		if (addr->number_mode == CDMA_SMS_NUM_MODE_DIGIT) {
			if (bit_offset + 4 > total_num_bits)
				return FALSE;

			addr->number_plan =
				bit_field_unpack(buf, bit_offset, 4);
			bit_offset += 4;
		}
	}

	if (bit_offset + 8 > total_num_bits)
		return FALSE;

	addr->num_fields = bit_field_unpack(buf, bit_offset, 8);
	bit_offset += 8;

	if (addr->digit_mode == CDMA_SMS_DIGIT_MODE_4BIT_DTMF)
		chari_len = 4;
	else
		chari_len = 8;

	if ((bit_offset + chari_len * addr->num_fields) > total_num_bits)
		return FALSE;

	for (index = 0; index < addr->num_fields; index++) {
		addr->address[index] = bit_field_unpack(buf,
							bit_offset,
							chari_len);
		bit_offset += chari_len;
	}

	return TRUE;
}

static char *decode_text_7bit_ascii(const struct cdma_sms_ud *ud)
{
	char *buf;

	buf = g_new(char, ud->num_fields + 1);
	if (buf == NULL)
		return NULL;

	memcpy(buf, ud->chari, ud->num_fields);
	buf[ud->num_fields] = 0; /* Make it NULL terminated string */

	return buf;
}

char *cdma_sms_decode_text(const struct cdma_sms_ud *ud)
{
	switch (ud->msg_encoding) {
	case CDMA_SMS_MSG_ENCODING_OCTET:
	case CDMA_SMS_MSG_ENCODING_EXTENDED_PROTOCOL_MSG:
		return NULL; /* TODO */
	case CDMA_SMS_MSG_ENCODING_7BIT_ASCII:
		return decode_text_7bit_ascii(ud);
	case CDMA_SMS_MSG_ENCODING_IA5:
	case CDMA_SMS_MSG_ENCODING_UNICODE:
	case CDMA_SMS_MSG_ENCODING_SHIFT_JIS:
	case CDMA_SMS_MSG_ENCODING_KOREAN:
	case CDMA_SMS_MSG_ENCODING_LATIN_HEBREW:
	case CDMA_SMS_MSG_ENCODING_LATIN:
	case CDMA_SMS_MSG_ENCODING_GSM_7BIT:
	case CDMA_SMS_MSG_ENCODING_GSM_DATA_CODING:
		return NULL; /* TODO */
	}

	return NULL;
}

/* Decode User Data */
static gboolean cdma_sms_decode_ud(const guint8 *buf, guint8 len, void *data)
{
	guint16 bit_offset = 0;
	guint8  chari_len = 0;
	guint16 total_num_bits = len * 8;
	guint8  index;
	enum cdma_sms_msg_encoding  msg_encoding;
	struct cdma_sms_ud *ud = data;

	if (total_num_bits < 13)
		return FALSE;

	msg_encoding = bit_field_unpack(buf, bit_offset, 5);
	ud->msg_encoding =  msg_encoding;
	bit_offset += 5;

	if (msg_encoding == CDMA_SMS_MSG_ENCODING_EXTENDED_PROTOCOL_MSG ||
		msg_encoding == CDMA_SMS_MSG_ENCODING_GSM_DATA_CODING) {
		/*
		 * Skip message type field for now.
		 * TODO: Add support for message type field.
		 */
		bit_offset += 8;
	}

	if (bit_offset + 8 > total_num_bits)
		return FALSE;

	ud->num_fields = bit_field_unpack(buf, bit_offset, 8);
	bit_offset += 8;

	switch (msg_encoding) {
	case CDMA_SMS_MSG_ENCODING_OCTET:
		chari_len = 8;
		break;
	case CDMA_SMS_MSG_ENCODING_EXTENDED_PROTOCOL_MSG:
		return FALSE; /* TODO */
	case CDMA_SMS_MSG_ENCODING_7BIT_ASCII:
	case CDMA_SMS_MSG_ENCODING_IA5:
		chari_len = 7;
		break;
	case CDMA_SMS_MSG_ENCODING_UNICODE:
	case CDMA_SMS_MSG_ENCODING_SHIFT_JIS:
	case CDMA_SMS_MSG_ENCODING_KOREAN:
		return FALSE; /* TODO */
	case CDMA_SMS_MSG_ENCODING_LATIN_HEBREW:
	case CDMA_SMS_MSG_ENCODING_LATIN:
		chari_len = 8;
		break;
	case CDMA_SMS_MSG_ENCODING_GSM_7BIT:
		chari_len = 7;
		break;
	case CDMA_SMS_MSG_ENCODING_GSM_DATA_CODING:
		return FALSE; /* TODO */
	}

	/* TODO: Add support for all other encoding types */
	if (chari_len == 0)
		return FALSE;

	if (bit_offset + chari_len * ud->num_fields > total_num_bits)
		return FALSE;

	for (index = 0; index < ud->num_fields; index++) {
		ud->chari[index] = bit_field_unpack(buf,
						bit_offset,
						chari_len);
		bit_offset += chari_len;
	}

	return TRUE;
}

/* Decode Message Identifier */
static gboolean cdma_sms_decode_message_id(const guint8 *buf, guint8 len,
						void *data)
{
	struct cdma_sms_identifier *id = data;

	if (len != 3)
		return FALSE;

	id->msg_type = bit_field_unpack(buf, 0, 4);

	if (id->msg_type <= 0 ||
			id->msg_type > CDMA_SMS_MSG_TYPE_SUBMIT_REPORT)
		return FALSE; /* Invalid message type */

	id->msg_id = (bit_field_unpack(buf, 4, 8) << 8) |
			bit_field_unpack(buf, 12, 8);

	id->header_ind = bit_field_unpack(buf, 20, 1);

	return TRUE;
}

static gboolean find_and_decode(struct simple_iter *iter, guint8 rec_id,
					rec_handler handler, void *data)
{
	guint8 id;
	guint8 len;
	const guint8 *buf;

	while (simple_iter_next(iter) == TRUE) {
		id = simple_iter_get_id(iter);
		if (id != rec_id)
			continue;

		len = simple_iter_get_length(iter);
		buf = simple_iter_get_data(iter);

		return handler(buf, len, data);
	}

	return FALSE;
}

static rec_handler subparam_handler_for_id(enum cdma_sms_subparam_id id)
{
	switch (id) {
	case CDMA_SMS_SUBPARAM_ID_MESSAGE_ID:
		return cdma_sms_decode_message_id;
	case CDMA_SMS_SUBPARAM_ID_USER_DATA:
		return cdma_sms_decode_ud;
	case CDMA_SMS_SUBPARAM_ID_USER_RESPONSE_CODE:
	case CDMA_SMS_SUBPARAM_ID_MC_TIME_STAMP:
	case CDMA_SMS_SUBPARAM_ID_VALIDITY_PERIOD_ABSOLUTE:
	case CDMA_SMS_SUBPARAM_ID_VALIDITY_PERIOD_RELATIVE:
	case CDMA_SMS_SUBPARAM_ID_DEFERRED_DELIVERY_TIME_ABSOLUTE:
	case CDMA_SMS_SUBPARAM_ID_DEFERRED_DELIVERY_TIME_RELATIVE:
	case CDMA_SMS_SUBPARAM_ID_PRIORITY_INDICATOR:
	case CDMA_SMS_SUBPARAM_ID_PRIVACY_INDICATOR:
	case CDMA_SMS_SUBPARAM_ID_REPLY_OPTION:
	case CDMA_SMS_SUBPARAM_ID_NUMBER_OF_MESSAGES:
	case CDMA_SMS_SUBPARAM_ID_ALERT_ON_MESSAGE_DELIVERY:
	case CDMA_SMS_SUBPARAM_ID_LANGUAGE_INDICATOR:
	case CDMA_SMS_SUBPARAM_ID_CALL_BACK_NUMBER:
	case CDMA_SMS_SUBPARAM_ID_MESSAGE_DISPLAY_MODE:
	case CDMA_SMS_SUBPARAM_ID_MULTIPLE_ENCODING_USER_DATA:
	case CDMA_SMS_SUBPARAM_ID_MESSAGE_DEPOSIT_INDEX:
	case CDMA_SMS_SUBPARAM_ID_SERVICE_CATEGORY_PROGRAM_DATA:
	case CDMA_SMS_SUBPARAM_ID_SERVICE_CATEGORY_PROGRAM_RESULT:
	case CDMA_SMS_SUBPARAM_ID_MESSAGE_STATUS:
	case CDMA_SMS_SUBPARAM_ID_TP_FAILURE_CAUSE:
	case CDMA_SMS_SUBPARAM_ID_ENHANCED_VMN:
	case CDMA_SMS_SUBPARAM_ID_ENHANCED_VMN_ACK:
		return NULL; /* TODO */
	}

	return NULL;
}

struct subparam_handler_entry {
	enum cdma_sms_subparam_id id;
	int flags;
	gboolean found;
	void *data;
};

static gboolean decode_subparams(struct simple_iter *iter, guint32 *bitmap,
					void *data, ...)
{
	GSList *entries = NULL;
	GSList *l;
	va_list args;
	gboolean decode_result = TRUE;

	va_start(args, data);

	while (data != NULL) {
		struct subparam_handler_entry *entry;

		entry = g_new0(struct subparam_handler_entry, 1);

		entry->data = data;
		entry->id = va_arg(args, enum cdma_sms_subparam_id);
		entry->flags = va_arg(args, int);

		data = va_arg(args, void *);
		entries = g_slist_prepend(entries, entry);
	}

	va_end(args);

	entries = g_slist_reverse(entries);

	l = entries;
	while (simple_iter_next(iter) == TRUE) {
		rec_handler handler;
		struct subparam_handler_entry *entry;
		guint8 subparam_len;
		const guint8 *subparam_buf;
		GSList *l2;

		for (l2 = l; l2; l2 = l2->next) {
			entry = l2->data;

			if (simple_iter_get_id(iter) == entry->id)
				break;
		}

		/* Ignore unexpected subparameter record */
		if (l2 == NULL)
			continue;

		entry->found = TRUE;

		subparam_len = simple_iter_get_length(iter);
		subparam_buf = simple_iter_get_data(iter);

		handler = subparam_handler_for_id(entry->id);

		decode_result = handler(subparam_buf,
					subparam_len,
					entry->data);
		if (decode_result == FALSE)
			break; /* Stop if decoding failed */

		set_bitmap(bitmap, entry->id);
	}

	for (; l; l = l->next) {
		struct subparam_handler_entry *entry = l->data;

		if ((entry->flags & CDMA_SMS_REC_FLAG_MANDATORY) &&
			(entry->found == FALSE)) {
			decode_result = FALSE;
			break;
		}
	}

	g_slist_foreach(entries, (GFunc) g_free, NULL);
	g_slist_free(entries);

	return decode_result;
}

/* Decode WMT */
static gboolean cdma_sms_decode_wmt(struct simple_iter *iter,
					struct cdma_sms_bearer_data *bd)
{
	switch (bd->id.msg_type) {
	case CDMA_SMS_MSG_TYPE_RESERVED:
		return FALSE; /* Invalid */
	case CDMA_SMS_MSG_TYPE_DELIVER:
		/*
		 * WMT DELIVER, table 4.3.4-1 of C.S0015-B v2.0
		 * TODO: Not all optional subparameters supported.
		 */
		return decode_subparams(iter,
					&bd->subparam_bitmap,
					&bd->wmt_deliver.ud,
					CDMA_SMS_SUBPARAM_ID_USER_DATA,
					0,
					NULL);
		break;
	case CDMA_SMS_MSG_TYPE_SUBMIT:
	case CDMA_SMS_MSG_TYPE_CANCEL:
		return FALSE; /* Invalid for MT WMT */
	case CDMA_SMS_MSG_TYPE_DELIVER_ACK:
	case CDMA_SMS_MSG_TYPE_USER_ACK:
	case CDMA_SMS_MSG_TYPE_READ_ACK:
		return FALSE; /* TODO: Not supported yet */
	case CDMA_SMS_MSG_TYPE_DELIVER_REPORT:
	case CDMA_SMS_MSG_TYPE_SUBMIT_REPORT:
		return FALSE; /* Invalid for MT WMT */
	}

	return FALSE;
}

static gboolean p2p_decode_bearer_data(const guint8 *buf, guint8 len,
					enum cdma_sms_teleservice_id tele_id,
					struct cdma_sms_bearer_data *bd)
{
	struct simple_iter iter;

	simple_iter_init(&iter, buf, len);

	/* Message Identifier is mandatory, * Section 4 of C.S0015-B v2.0 */
	if (find_and_decode(&iter,
				CDMA_SMS_SUBPARAM_ID_MESSAGE_ID,
				cdma_sms_decode_message_id,
				&bd->id) != TRUE)
		return FALSE;

	set_bitmap(&bd->subparam_bitmap, CDMA_SMS_SUBPARAM_ID_MESSAGE_ID);

	simple_iter_init(&iter, buf, len);

	switch (tele_id) {
	case CDMA_SMS_TELESERVICE_ID_CMT91:
	case CDMA_SMS_TELESERVICE_ID_WPT:
		return FALSE; /* TODO */
	case CDMA_SMS_TELESERVICE_ID_WMT:
		return cdma_sms_decode_wmt(&iter, bd);
	case CDMA_SMS_TELESERVICE_ID_VMN:
	case CDMA_SMS_TELESERVICE_ID_WAP:
	case CDMA_SMS_TELESERVICE_ID_WEMT:
	case CDMA_SMS_TELESERVICE_ID_SCPT:
	case CDMA_SMS_TELESERVICE_ID_CATPT:
		return FALSE; /* TODO */
	}

	return FALSE;
}

/* Decode Bearer Data */
static gboolean cdma_sms_decode_bearer_data(const guint8 *buf, guint8 len,
								void *data)
{
	struct cdma_sms *msg = data;

	switch (msg->type) {
	case CDMA_SMS_TP_MSG_TYPE_P2P:
		return p2p_decode_bearer_data(buf, len,
						msg->p2p_msg.teleservice_id,
						&msg->p2p_msg.bd);
	case CDMA_SMS_TP_MSG_TYPE_BCAST:
		return FALSE; /* TODO */
	case CDMA_SMS_TP_MSG_TYPE_ACK:
		return FALSE; /* Invalid */
	}

	return FALSE;
}

static rec_handler param_handler_for_id(enum cdma_sms_param_id id,
						struct cdma_sms *incoming,
						void **data)
{
	if (incoming->type != CDMA_SMS_TP_MSG_TYPE_P2P)
		return NULL; /* TODO: Other types not supported yet */

	switch (id) {
	case CDMA_SMS_PARAM_ID_TELESERVICE_IDENTIFIER:
		*data = &incoming->p2p_msg.teleservice_id;
		return cdma_sms_decode_teleservice;
	case CDMA_SMS_PARAM_ID_SERVICE_CATEGORY:
		return NULL; /* TODO */
	case CDMA_SMS_PARAM_ID_ORIGINATING_ADDRESS:
		*data = &incoming->p2p_msg.oaddr;
		return cdma_sms_decode_addr;
	case CDMA_SMS_PARAM_ID_ORIGINATING_SUBADDRESS:
	case CDMA_SMS_PARAM_ID_DESTINATION_ADDRESS:
	case CDMA_SMS_PARAM_ID_DESTINATION_SUBADDRESS:
	case CDMA_SMS_PARAM_ID_BEARER_REPLY_OPTION:
	case CDMA_SMS_PARAM_ID_CAUSE_CODE:
		return NULL; /* TODO */
	case CDMA_SMS_PARAM_ID_BEARER_DATA:
		*data = incoming;
		return cdma_sms_decode_bearer_data;
	}

	return NULL;
}

static gboolean cdma_sms_p2p_decode(const guint8 *pdu, guint8 len,
					struct cdma_sms *incoming)
{
	struct simple_iter iter;

	simple_iter_init(&iter, pdu, len);

	/*
	 * Teleservice Identifier is mandatory,
	 * Table 3.4.2.1-1 of C.S0015-B v2.0
	 */
	if (find_and_decode(&iter,
				CDMA_SMS_PARAM_ID_TELESERVICE_IDENTIFIER,
				cdma_sms_decode_teleservice,
				&incoming->p2p_msg.teleservice_id) != TRUE)
		return FALSE;

	set_bitmap(&incoming->p2p_msg.param_bitmap,
			CDMA_SMS_PARAM_ID_TELESERVICE_IDENTIFIER);

	simple_iter_init(&iter, pdu, len);

	while (simple_iter_next(&iter) == TRUE) {
		rec_handler handler;
		enum cdma_sms_param_id rec_id;
		guint8 rec_len;
		const guint8 *rec_buf;
		void *uninitialized_var(dataobj);

		rec_id = simple_iter_get_id(&iter);
		if (rec_id == CDMA_SMS_PARAM_ID_TELESERVICE_IDENTIFIER)
			continue;

		rec_len = simple_iter_get_length(&iter);
		rec_buf = simple_iter_get_data(&iter);

		handler = param_handler_for_id(rec_id, incoming, &dataobj);
		if (handler != NULL) {
			if (handler(rec_buf, rec_len, dataobj) == FALSE)
				return FALSE;

			set_bitmap(&incoming->p2p_msg.param_bitmap, rec_id);
		}
	}

	/*
	 * Originating Address is mandatory field,
	 * Table 3.4.2.1-1 of C.S0015-B v2.0
	 */
	if ((incoming->p2p_msg.param_bitmap &
			(1 << CDMA_SMS_PARAM_ID_ORIGINATING_ADDRESS)) == 0)
		return FALSE;

	return TRUE;
}

gboolean cdma_sms_decode(const guint8 *pdu, guint8 len,
				struct cdma_sms *incoming)
{
	incoming->type = bit_field_unpack(pdu, 0, 8);
	pdu += 1;
	len -= 1;

	switch (incoming->type) {
	case CDMA_SMS_TP_MSG_TYPE_P2P:
		return cdma_sms_p2p_decode(pdu, len, incoming);
	case CDMA_SMS_TP_MSG_TYPE_BCAST:
	case CDMA_SMS_TP_MSG_TYPE_ACK:
		/* TODO: Not supported yet */
		return FALSE;
	}

	return FALSE;
}