summaryrefslogtreecommitdiffstats
path: root/tinySMS/src/rpdu/tsms_rpdu.c
blob: 72640b9e14d815e61ba5cb8c6041af52e55af07f (plain)
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
/*
* Copyright (C) 2009 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango.org>
*	
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*	
* DOUBANGO 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 DOUBANGO.
*
*/

/**@file tsms_rpdu.c
 * @brief SMS RPDU encoder/decoder as per 3GPP TS 23.011.
 *
 * @author Mamadou Diop <diopmamadou(at)doubango.org>
 *

 */
#include "tinysms/rpdu/tsms_rpdu.h"


#include "tsk_debug.h"

#define TSMS_ERROR_TOO_SHORT(type)\
	TSK_DEBUG_ERROR("%s == Data too short.", type);\
	failed = tsk_true;\
	goto bail;

/*=== RP-DATA as per 3GPP TS 24.011 section 7.3.1. ===*/

/** internal function used to deserialize a RP-DATA message from binary content. */
tsms_rpdu_message_t* _tsms_rpdu_rpdata_deserialize(const void* data, tsk_size_t size)
{
	/* don't need to test data and size --> already done by tsms_rpdu_message_deserialize() */
	tsms_rpdu_data_t* self = tsms_rpdu_data_create(0x00, tsk_null, tsk_null, tsk_false);
	tsk_bool_t failed = tsk_false;
	const uint8_t* pdata = data;
	const uint8_t* pend = pdata + size;
	tsk_size_t smsc_len;
	
	/* 3GPP TS 24.011 ==> 8.2.2 Message type indicator (MTI) */
	TSMS_RPDU_MESSAGE(self)->mti = *pdata++;

	/* 3GPP TS 24.011 ==> 8.2.3 Message reference */
	TSMS_RPDU_MESSAGE(self)->mr = *pdata++;

	/* 3GPP TS 24.011 ==> 8.2.5.1 Originator address element */
	if(TSMS_RPDU_MESSAGE(self)->mti == tsms_rpdu_type_data_mt){
		/* 7.3.1.1	RP DATA (Network to Mobile Station) */
		if((self->oa = tsms_address_deserialize(pdata, (pend-pdata), tsms_addr_smsc, &smsc_len))){
			pdata += smsc_len;
		}
		/* 7.3.1.2	RP DATA (Mobile Station to Network) */
		pdata++; /* 1o */
	}
	else if(TSMS_RPDU_MESSAGE(self)->mti == tsms_rpdu_type_data_mo){
		/* 7.3.1.1	RP DATA (Network to Mobile Station) */
		pdata++; /* 1o */
		/* 7.3.1.2	RP DATA (Mobile Station to Network) */
		if((self->da = tsms_address_deserialize(pdata, (pend-pdata), tsms_addr_smsc, &smsc_len))){
			pdata += smsc_len;
		}
	}
	else{
		failed = tsk_true;
		TSK_DEBUG_ERROR("%d Not a valid RP-DATA messsage", TSMS_RPDU_MESSAGE(self)->mti);
		goto bail;
	}

	/* 3GPP TS 24.011 ==> 8.2.5.3 RP-User data element */
	if((pend-pdata) > 1){
		tsk_size_t length = *pdata++; // length-indicator
		if((length != (pend-pdata))){
			TSK_DEBUG_WARN("Invalid length-indicator.");
		}
		else{
			self->udata = tsk_buffer_create(pdata, length);
		}
	}
	else{
		TSK_DEBUG_WARN("RP-User data element is mandatory for RP-DATA messages.");
	}
	

bail:
	if(failed){
		TSK_OBJECT_SAFE_FREE(self);
	}
	return TSMS_RPDU_MESSAGE(self);
}

/** internal function used to serialize a RP-DATA message as binary content.*/
int _tsms_rpdu_rpdata_serialize(const tsms_rpdu_message_t* _self, tsk_buffer_t* output)
{
	/* don't need to test output and self --> already done by tsms_rpdu_message_serialize() */
	const tsms_rpdu_data_t* self = (const tsms_rpdu_data_t*)_self;

	/* 3GPP TS 24.011 ==> 8.2.2 Message type indicator (MTI) */
	tsk_buffer_append(output, &_self->mti, 1);

	/* 3GPP TS 24.011 ==> 8.2.3 Message reference */
	tsk_buffer_append(output, &_self->mr, 1);

	/* 3GPP TS 24.011 ==> 8.2.5.1 Originator address element */
	tsms_address_serialize(self->oa, output);

	/* 3GPP TS 24.011 ==> 8.2.5.2 Destination address element */
	tsms_address_serialize(self->da, output);

	/* 3GPP TS 24.011 ==> 8.2.5.3 RP-User data element */
	if(self->udata){
		tsk_buffer_append(output, &self->udata->size, 1); // length-indicator
		tsk_buffer_append(output, self->udata->data, self->udata->size);
	}
	else{
		static uint8_t _1byte = 0x00;
		tsk_buffer_append(output, &_1byte, 1); // length-indicator
	}

	return 0;
}

/**@ingroup tsms_rpdu_group
* Creates a @a RP-DATA message.
* @a RP-DATA messages could be sent from the Network to Mobile Station (MT) or from the Mobile Station to the Network (MO).
* For more information, please refer to 3GPP TS 24.011 section 7.3.1.
* @param mr @a RP-Message Reference as per 3GPP TS 24.011 section 8.2.3.
* @param smsc The address of the SMSC. e.g. "+331253688".
* @param tpdu A Pointer to the @a TP-Message to relay.
* @param MobOrig Indicates whether to create a Mobile Originated @a RP-DATA message or not.
* @retval Mobile Originated @a RP-DATA message.
* @sa @ref tsms_rpdu_data_create_mt<br>@ref tsms_rpdu_data_create_mo
*
* See For more information, see @ref tsms_rpdu_group_DATA "RP-DATA".
*/
tsms_rpdu_data_t* tsms_rpdu_data_create(uint8_t mr, const tsms_address_string_t smsc, const tsms_tpdu_message_t* tpdu, tsk_bool_t MobOrig)
{
	tsms_rpdu_data_t* ret = tsk_null;
	
	if(!(ret = tsk_object_new(tsms_rpdu_data_def_t))){
		goto bail;
	}

	/* 3GPP TS 24.011 ==> 8.2.2 Message type indicator (MTI) */
	TSMS_RPDU_MESSAGE(ret)->mti = MobOrig? tsms_rpdu_type_data_mo : tsms_rpdu_type_data_mt;

	/* 3GPP TS 24.011 ==> 8.2.3 Message reference */
	TSMS_RPDU_MESSAGE(ret)->mr = mr;

	/* 3GPP TS 24.341 section B5 (Moblie Originated RP-DATA message)
		The payload includes an RP-DATA message (see 3GPP TS 24.011 [8]). It includes:
		-	Address of the originating UE: this field includes the length indicator only;
		-	Address of the destination SC, which is configured in the UE; and
		-	RP-User-Data (see 3GPP TS 23.040 [3]), which includes SMS-SUBMIT as type indicator.
	*/


	/* 3GPP TS 24.011 ==> 8.2.5.1 Originator address element */
	if(smsc){
		if(TSMS_RPDU_MESSAGE(ret)->mti == tsms_rpdu_type_data_mt){
			/* 7.3.1.1	RP DATA (Network to Mobile Station) */
			ret->oa = tsms_address_smsc_create(smsc);
		}
		else{
			/* 7.3.1.2	RP DATA (Mobile Station to Network) */
			ret->da = tsms_address_smsc_create(smsc);
		}
	}

	/* 3GPP TS 24.011 ==> 8.2.5.3 RP-User data element */
	if(tpdu && (ret->udata = tsk_buffer_create_null())){
		tsms_tpdu_message_serialize(tpdu, ret->udata, MobOrig);
	}

bail:
	return ret;
}

//
//	RP-DATA object definition
//
static tsk_object_t* tsms_rpdu_data_ctor(tsk_object_t * self, va_list * app)
{
	tsms_rpdu_data_t *rpdata = self;
	if(rpdata){
		TSMS_RPDU_MESSAGE(rpdata)->mti = tsms_rpdu_type_data_mo;
	}
	else{
		TSK_DEBUG_ERROR("Null");
	}
	return self;
}

static tsk_object_t* tsms_rpdu_data_dtor(tsk_object_t * self)
{ 
	tsms_rpdu_data_t *rpdata = self;
	if(rpdata){
		TSK_OBJECT_SAFE_FREE(rpdata->oa);
		TSK_OBJECT_SAFE_FREE(rpdata->da);
		TSK_OBJECT_SAFE_FREE(rpdata->udata);
	}
	else{
		TSK_DEBUG_ERROR("Null");
	}

	return self;
}

static const tsk_object_def_t tsms_rpdu_data_def_s = 
{
	sizeof(tsms_rpdu_data_t),
	tsms_rpdu_data_ctor, 
	tsms_rpdu_data_dtor,
	tsk_null, 
};
const tsk_object_def_t *tsms_rpdu_data_def_t = &tsms_rpdu_data_def_s;

/*=== RP-SMMA as per 3GPP TS 24.011 section 7.3.2. ===*/

/** internal function used to deserialize a RP-SMMA message from binary content. */
tsms_rpdu_message_t* _tsms_rpdu_rpsmma_deserialize(const void* data, tsk_size_t size)
{
	/* don't need to test data and size --> already done by tsms_rpdu_message_deserialize() */
	tsms_rpdu_smma_t* self = tsms_rpdu_smma_create(0);
	const uint8_t* pdata = data;
	
	/* 3GPP TS 24.011 ==> 8.2.2 Message type indicator (MTI) */
	TSMS_RPDU_MESSAGE(self)->mti = *pdata++;

	/* 3GPP TS 24.011 ==> 8.2.3 Message reference */
	TSMS_RPDU_MESSAGE(self)->mr = *pdata++;
	
	return TSMS_RPDU_MESSAGE(self);
}

/** internal function used to serialize a RP-SMMA message as binary content. */
int _tsms_rpdu_rpsmma_serialize(const tsms_rpdu_message_t* _self, tsk_buffer_t* output)
{
	/* don't need to test output and self --> already done by tsms_rpdu_message_serialize() */
	//const tsms_rpdu_smma_t* self = (const tsms_rpdu_smma_t*)_self;

	/* 3GPP TS 24.011 ==> 8.2.2 Message type indicator (MTI) */
	tsk_buffer_append(output, &_self->mti, 1);

	/* 3GPP TS 24.011 ==> 8.2.3 Message reference */
	tsk_buffer_append(output, &_self->mr, 1);

	return 0;
}

/**@ingroup tsms_rpdu_group
* Creates a @a RP-SMMA message.
* This message is sent by the mobile station to relay a notification to the network that the mobile has memory available to receive one or more short messages.
* For more information, please refer to 3GPP TS 24.011 section 7.3.2.
* @param mr @a RP-Message Reference as per 3GPP TS 24.011 section 8.2.3.
* @retval Mobile Originated @a RP-SMMA message.
*
* See For more information, see @ref tsms_rpdu_group_SMMA "RP-SMMA".
*/
tsms_rpdu_smma_t* tsms_rpdu_smma_create(uint8_t mr)
{
	tsms_rpdu_smma_t* ret = tsk_null;

	if((ret = tsk_object_new(tsms_rpdu_smma_def_t))){
		TSMS_RPDU_MESSAGE(ret)->mr = mr;
	}
	return ret;
}

//
//	RP-SMMA object definition
//
static tsk_object_t* tsms_rpdu_smma_ctor(tsk_object_t * self, va_list * app)
{
	tsms_rpdu_smma_t *smma = self;
	if(smma){
		TSMS_RPDU_MESSAGE(smma)->mti = tsms_rpdu_type_smma_mo;
	}
	else{
		TSK_DEBUG_ERROR("Null");
	}
	return self;
}

static tsk_object_t* tsms_rpdu_smma_dtor(tsk_object_t * self)
{ 
	tsms_rpdu_smma_t *smma = self;
	if(smma){
	}
	else{
		TSK_DEBUG_ERROR("Null");
	}

	return self;
}

static const tsk_object_def_t tsms_rpdu_smma_def_s = 
{
	sizeof(tsms_rpdu_smma_t),
	tsms_rpdu_smma_ctor, 
	tsms_rpdu_smma_dtor,
	tsk_null, 
};
const tsk_object_def_t *tsms_rpdu_smma_def_t = &tsms_rpdu_smma_def_s;


/*=== RP-ACK as per 3GPP TS 24.011 section 7.3.3. ===*/

/** internal function used to deserialize an RP-ACK message from binary content. */
tsms_rpdu_message_t* _tsms_rpdu_rpack_deserialize(const void* data, tsk_size_t size)
{
	/* don't need to test data and size --> already done by tsms_rpdu_message_deserialize() */
	tsms_rpdu_ack_t* self = tsms_rpdu_ack_create(0, tsk_null, tsk_false);
	const uint8_t* pdata = data;
	const uint8_t* pend = pdata + size;
	
	/* 3GPP TS 24.011 ==> 8.2.2 Message type indicator (MTI) */
	TSMS_RPDU_MESSAGE(self)->mti = *pdata++;

	/* 3GPP TS 24.011 ==> 8.2.3 Message reference */
	TSMS_RPDU_MESSAGE(self)->mr = *pdata++;	

	/* 3GPP TS 24.011 ==> 8.2.5.3 RP-User data element */
	if((pend-pdata) > 1){
		tsk_size_t length;
		/*uint8_t iei =*/ (void)(*pdata++); // IEI MUST be equal to 0x41
		length = *pdata++; // length-indicator
		if((length != (pend-pdata))){
			TSK_DEBUG_WARN("Invalid length-indicator.");
		}
		else{
			self->udata = tsk_buffer_create(pdata, length);
		}
	}

	return TSMS_RPDU_MESSAGE(self);
}

/** internal function used to serialize a RP-ACK message as binary content. */
int _tsms_rpdu_rpack_serialize(const tsms_rpdu_message_t* _self, tsk_buffer_t* output)
{
	/* don't need to test output and self --> already done by tsms_rpdu_message_serialize() */
	const tsms_rpdu_ack_t* self = (const tsms_rpdu_ack_t*)_self;

	/* 3GPP TS 24.011 ==> 8.2.2 Message type indicator (MTI) */
	tsk_buffer_append(output, &_self->mti, 1);

	/* 3GPP TS 24.011 ==> 8.2.3 Message reference */
	tsk_buffer_append(output, &_self->mr, 1);

	/* 3GPP TS 24.011 ==> 8.2.5.3 RP-User data element */
	if(self->udata){
		static uint8_t iei = 0x41;
		tsk_buffer_append(output, &iei, 1); // IEI
		tsk_buffer_append(output, &self->udata->size, 1); // length-indicator
		tsk_buffer_append(output, self->udata->data, self->udata->size);
	}
	else{
		static uint8_t _1byte = 0x00;
		tsk_buffer_append(output, &_1byte, 1); // length-indicator
	}

	return 0;
}

/**@ingroup tsms_rpdu_group
* Creates a @a RP-ACK message.
* @a RP-ACK is sent between the MSC and the mobile station in both directions and used to relay the acknowledgement of a RP-DATA or RP-SMMA message reception.
* For more information, please refer to 3GPP TS 24.011 section 7.3.2.3
* @param mr @a RP-Message Reference as per 3GPP TS 24.011 section 8.2.3.
* @param tpdu A Pointer to the @a TP-Message to include as RP-User data.
* @param MobOrig Indicates whether to create a Mobile Originated @a RP-ACK message or not.
* @retval Mobile Originated @a RP-ACK message.
*
* See For more information, see @ref tsms_rpdu_group_ACK "RP-ACK".
*/
tsms_rpdu_ack_t* tsms_rpdu_ack_create(uint8_t mr, const tsms_tpdu_message_t* tpdu, tsk_bool_t MobOrig)
{
	tsms_rpdu_ack_t* ret = tsk_null;
	
	if(!tpdu){
		TSK_DEBUG_WARN("TPDU for RP-ACK message is Null.");
	}
	
	if(!(ret = tsk_object_new(tsms_rpdu_ack_def_t))){
		goto bail;
	}

	/* 3GPP TS 24.011 ==> 8.2.2 Message type indicator (MTI) */
	TSMS_RPDU_MESSAGE(ret)->mti = MobOrig? tsms_rpdu_type_ack_mo : tsms_rpdu_type_ack_mt;

	/* 3GPP TS 24.011 ==> 8.2.3 Message reference */
	TSMS_RPDU_MESSAGE(ret)->mr = mr;	

	/* 3GPP TS 24.011 ==> 8.2.5.3 RP-User data element */
	if(tpdu && (ret->udata = tsk_buffer_create_null())){
		tsms_tpdu_message_serialize(tpdu, ret->udata, MobOrig);
	}

bail:
	return ret;
}

//
//	RP-ACK object definition
//
static tsk_object_t* tsms_rpdu_ack_ctor(tsk_object_t * self, va_list * app)
{
	tsms_rpdu_ack_t *ack = self;
	if(ack){
		TSMS_RPDU_MESSAGE(ack)->mti = tsms_rpdu_type_ack_mo;
	}
	else{
		TSK_DEBUG_ERROR("Null");
	}
	return self;
}

static tsk_object_t* tsms_rpdu_ack_dtor(tsk_object_t * self)
{ 
	tsms_rpdu_ack_t *ack = self;
	if(ack){
		TSK_OBJECT_SAFE_FREE(ack->udata);
	}
	else{
		TSK_DEBUG_ERROR("Null");
	}

	return self;
}

static const tsk_object_def_t tsms_rpdu_ack_def_s = 
{
	sizeof(tsms_rpdu_ack_t),
	tsms_rpdu_ack_ctor, 
	tsms_rpdu_ack_dtor,
	tsk_null, 
};
const tsk_object_def_t *tsms_rpdu_ack_def_t = &tsms_rpdu_ack_def_s;

/*=== RP-ERROR as per 3GPP TS 24.011 section 7.3.4. ===*/

/** internal function used to deserialize a RP-ERROR message from binary content. */
tsms_rpdu_message_t* _tsms_rpdu_rperror_deserialize(const void* data, tsk_size_t size)
{
	/* don't need to test data and size --> already done by tsms_rpdu_message_deserialize() */
	tsms_rpdu_error_t* self = tsms_rpdu_error_create(0, tsk_null, 0, tsk_false);
	const uint8_t* pdata = data;
	const uint8_t* pend = pdata + size;
	tsk_bool_t failed = tsk_false;
	uint8_t cause_count;
	
	/* 3GPP TS 24.011 ==> 8.2.2 Message type indicator (MTI) */
	TSMS_RPDU_MESSAGE(self)->mti = *pdata++;
	
	/* 3GPP TS 24.011 ==> 8.2.3 Message reference */
	TSMS_RPDU_MESSAGE(self)->mr = *pdata++;

	/* 3GPP TS 24.011 ==> 8.2.5.4 RP Cause 
	* Only 1 cause (LV)*/
	if((cause_count = *pdata++)){
		self->cause[0] = cause_count;
		self->cause[1] = *pdata++;
		if((pdata += (cause_count-1)) >= pend){
			TSMS_ERROR_TOO_SHORT("RP-ERROR");
		}
	}
	
	/* 3GPP TS 24.011 ==> 8.2.5.3 RP-User data element */
	if((pend-pdata) > 2){
		tsk_size_t length;
		/*uint8_t iei =*/ (void)(*pdata++); // IEI MUST be equal to 0x41
		length = *pdata++; // length-indicator
		if((length != (pend-pdata))){
			TSK_DEBUG_WARN("Invalid length-indicator.");
		}
		else{
			self->udata = tsk_buffer_create(pdata, length);
		}
	}
	

bail:
	if(failed){
		TSK_OBJECT_SAFE_FREE(self);
	}
	return TSMS_RPDU_MESSAGE(self);
}

/** internal function used to serialize a RP-ERROR message as binary content. */
int _tsms_rpdu_rperror_serialize(const tsms_rpdu_message_t* _self, tsk_buffer_t* output)
{
	/* don't need to test output and self --> already done by tsms_rpdu_message_serialize() */
	const tsms_rpdu_error_t* self = (const tsms_rpdu_error_t*)_self;

	/* 3GPP TS 24.011 ==> 8.2.2 Message type indicator (MTI) */
	tsk_buffer_append(output, &_self->mti, 1);

	/* 3GPP TS 24.011 ==> 8.2.3 Message reference */
	tsk_buffer_append(output, &_self->mr, 1);

	/* 3GPP TS 24.011 ==> 8.2.5.4 RP Cause 
	* Only 1 cause (LV)*/
	tsk_buffer_append(output, self->cause, 2);

	/* 3GPP TS 24.011 ==> 8.2.5.3 RP-User data element */
	if(self->udata){
		static uint8_t iei = 0x41;
		tsk_buffer_append(output, &iei, 1); // IEI
		tsk_buffer_append(output, &self->udata->size, 1); // length-indicator
		tsk_buffer_append(output, self->udata->data, self->udata->size);
	}
	else{
		static uint8_t _1byte = 0x00;
		tsk_buffer_append(output, &_1byte, 1); // length-indicator
	}

	return 0;
}

/**@ingroup tsms_rpdu_group
* Creates a @a RP-ERROR message.
* @a RP-ERROR is sent between the MSC and the mobile station in both directions and used to relay an error cause from an erroneous short message or notification transfer attempt.
* For more information, please refer to 3GPP TS 24.011 section 7.3.2.4.
* @param mr @a RP-Message Reference as per 3GPP TS 24.011 section 8.2.3.
* @param tpdu A Pointer to the @a TP-Message to include as RP-User data.
* @param cause RP-Cause value as per 3GPP TS 24.011 section 8.2.5.4.
* @param MobOrig Indicates whether to create a Mobile Originated @a RP-ERROR message or not.
* @retval Mobile Originated @a RP-ERROR message.
*
* See For more information, see @ref tsms_rpdu_group_ERROR "RP-ERROR".
*/
tsms_rpdu_error_t* tsms_rpdu_error_create(uint8_t mr, const tsms_tpdu_message_t* tpdu, uint8_t cause, tsk_bool_t MobOrig)
{
	tsms_rpdu_error_t* ret = tsk_null;
	
	/*if(!tpdu){
		TSK_DEBUG_WARN("TPDU for RP-ERROR message is Null.");
	}*/
	
	if(!(ret = tsk_object_new(tsms_rpdu_error_def_t))){
		goto bail;
	}

	/* 3GPP TS 24.011 ==> 8.2.2 Message type indicator (MTI) */
	TSMS_RPDU_MESSAGE(ret)->mti = MobOrig? tsms_rpdu_type_error_mo : tsms_rpdu_type_error_mt;

	/* 3GPP TS 24.011 ==> 8.2.3 Message reference */
	TSMS_RPDU_MESSAGE(ret)->mr = mr;

	/* 3GPP TS 24.011 ==> 8.2.5.4 RP Cause */
	ret->cause[0] = 1 /*len*/;
	ret->cause[1] = cause;
	
	/* 3GPP TS 24.011 ==> 8.2.5.3 RP-User data element */
	if(tpdu && (ret->udata = tsk_buffer_create_null())){
		tsms_tpdu_message_serialize(tpdu, ret->udata, MobOrig);
	}

bail:
	return ret;
}

//
//	RP-ERROR object definition
//
static tsk_object_t* tsms_rpdu_error_ctor(tsk_object_t * self, va_list * app)
{
	tsms_rpdu_error_t *error = self;
	if(error){
		TSMS_RPDU_MESSAGE(error)->mti = tsms_rpdu_type_error_mo;
	}
	else{
		TSK_DEBUG_ERROR("Null");
	}
	return self;
}

static tsk_object_t* tsms_rpdu_error_dtor(tsk_object_t * self)
{ 
	tsms_rpdu_error_t *error = self;
	if(error){
		TSK_OBJECT_SAFE_FREE(error->udata);
	}
	else{
		TSK_DEBUG_ERROR("Null");
	}

	return self;
}

static const tsk_object_def_t tsms_rpdu_error_def_s = 
{
	sizeof(tsms_rpdu_error_t),
	tsms_rpdu_error_ctor, 
	tsms_rpdu_error_dtor,
	tsk_null, 
};
const tsk_object_def_t *tsms_rpdu_error_def_t = &tsms_rpdu_error_def_s;

OpenPOWER on IntegriCloud