summaryrefslogtreecommitdiffstats
path: root/tinyMEDIA/src/tmedia_qos.c
blob: c5c9d3dd5f741abc5ea9567531c5ef2ce997f743 (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
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango[dot]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 tmedia_qos.c
 * @brief RFC 3312 (Preconditions) implementation.
 *
 * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
 *

 */
#include "tinymedia/tmedia_qos.h"

#include "tsk_memory.h"
#include "tsk_debug.h"

/* ========================= Internal ==================================*/

static const char* tmedia_qos_strength_tostring(tmedia_qos_strength_t strength)
{
	switch(strength){
		case tmedia_qos_strength_none:
			return "none";
		case tmedia_qos_strength_mandatory:
			return "mandatory";
		case tmedia_qos_strength_optional:
			return "optional";
		case tmedia_qos_strength_failure:
			return "failure";
		case tmedia_qos_strength_unknown:
		default:
			return "unknown";
	}
}

static tmedia_qos_strength_t tmedia_qos_strength_fromstring(const char* strength)
{
	if(tsk_strequals(strength, "none")){
		return tmedia_qos_strength_none;
	}
	else if(tsk_strequals(strength, "mandatory")){
		return tmedia_qos_strength_mandatory;
	}
	else if(tsk_strequals(strength, "optional")){
		return tmedia_qos_strength_optional;
	}
	else if(tsk_strequals(strength, "failure")){
		return tmedia_qos_strength_failure;
	}
	else{
		return tmedia_qos_strength_unknown;
	}
}

static tmedia_qos_direction_t tmedia_qos_direction_fromstring(const char* direction)
{
	if(tsk_strequals(direction, "send")){
		return tmedia_qos_direction_send;
	}
	else if(tsk_strequals(direction, "recv")){
		return tmedia_qos_direction_recv;
	}
	else if(tsk_strequals(direction, "sendrecv")){
		return tmedia_qos_direction_sendrecv;
	}
	else{
		return tmedia_qos_direction_none;
	}
}

/* ========================= Common ==================================*/

tmedia_qos_tline_t* tmedia_qos_tline_create(tmedia_qos_stype_t type, tmedia_qos_strength_t strength)
{
	switch(type){
		case tmedia_qos_stype_e2e:
			return (tmedia_qos_tline_t*)tmedia_qos_tline_e2e_create(strength);
		case tmedia_qos_stype_segmented:
			return (tmedia_qos_tline_t*)tmedia_qos_tline_segmented_create(strength);
        default: break;
	}
	return tsk_null;
}

tmedia_qos_stype_t tmedia_qos_get_type(const tsdp_header_M_t* m)
{
	const tsdp_header_A_t* A;
	char s0[10];

	if(!m){
		TSK_DEBUG_ERROR("Invalid parameter");
		return tmedia_qos_stype_none;
	}

	if((A = tsdp_header_M_findA(m, "curr"))){
		if(sscanf(A->value, "qos %s %*s", s0) != EOF){
			return tsk_strequals(s0, "e2e") ? tmedia_qos_stype_e2e : tmedia_qos_stype_segmented;
		}
		else{
			TSK_DEBUG_ERROR("Failed to parse a=%s:%s", A->field, A->value);
		}
	}
	
	return tmedia_qos_stype_none;
}

tmedia_qos_tline_t* tmedia_qos_tline_from_sdp(const tsdp_header_M_t* m)
{
	if(!m){
		TSK_DEBUG_ERROR("Invalid parameter");
		return tsk_null;
	}

	switch(tmedia_qos_get_type(m)){
		case tmedia_qos_stype_e2e:
			return (tmedia_qos_tline_t*)tmedia_qos_tline_e2e_from_sdp(m);
		case tmedia_qos_stype_segmented:
			return (tmedia_qos_tline_t*)tmedia_qos_tline_segmented_from_sdp(m);
		default:
			return tsk_null;
	}
}

int tmedia_qos_tline_to_sdp(const tmedia_qos_tline_t* self, tsdp_header_M_t* m)
{
	if(!self || !m){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	switch(self->type){
		case tmedia_qos_stype_e2e:
			return tmedia_qos_tline_e2e_to_sdp((tmedia_qos_tline_e2e_t*)self, m);
		case tmedia_qos_stype_segmented:
			return tmedia_qos_tline_segmented_to_sdp((tmedia_qos_tline_segmented_t*)self, m);
		default:
			TSK_DEBUG_ERROR("Invalid type");
			return -2;
	}
}

int tmedia_qos_tline_set_ro(tmedia_qos_tline_t* self, const tmedia_qos_tline_t* ro)
{
	if(!self || !ro){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	switch(self->type){
		case tmedia_qos_stype_e2e:
			return tmedia_qos_tline_e2e_set_ro((tmedia_qos_tline_e2e_t*)self, (const tmedia_qos_tline_e2e_t*)ro);
		case tmedia_qos_stype_segmented:
			return tmedia_qos_tline_segmented_set_ro((tmedia_qos_tline_segmented_t*)self, (const tmedia_qos_tline_segmented_t*)ro);
		default:
			TSK_DEBUG_ERROR("Invalid type");
			return -2;
	}
}

tsk_bool_t tmedia_qos_tline_canresume(const tmedia_qos_tline_t* self)
{
	if(!self){
		TSK_DEBUG_ERROR("Invalid parameter");
		return tsk_true;
	}
	switch(self->type){
		case tmedia_qos_stype_segmented:
			return tmedia_qos_tline_segmented_canresume((const tmedia_qos_tline_segmented_t*)self); 
		case tmedia_qos_stype_e2e:
			return tmedia_qos_tline_e2e_canresume((const tmedia_qos_tline_e2e_t*)self);
		default:
			return tsk_true;
	}
}

/* ========================= E2E ==================================*/

tmedia_qos_tline_e2e_t* tmedia_qos_tline_e2e_create(tmedia_qos_strength_t strength)
{
	return tsk_object_new(tmedia_qos_tline_e2e_def_t, strength);
}

tmedia_qos_tline_e2e_t* tmedia_qos_tline_e2e_from_sdp(const tsdp_header_M_t* m)
{
	tmedia_qos_tline_e2e_t* e2e = tsk_null;
	const tsdp_header_A_t* A;
	tsk_size_t i;

	char s0[10], s1[10];

	if(!m){
		TSK_DEBUG_ERROR("invalid parameter");
		return tsk_null;
	}
	
	/*  Example
		a=curr:qos e2e none
		a=des:qos mandatory e2e sendrecv
		a=conf:qos e2e recv
	*/
	
	e2e = tmedia_qos_tline_e2e_create(tmedia_qos_strength_unknown);

	/* curr */
	for(i = 0; (A = tsdp_header_M_findA_at(m, "curr", i)); i++){
		if(sscanf(A->value, "qos e2e %10s", s0) != EOF){
			tmedia_qos_direction_t dir = tmedia_qos_direction_fromstring(s0);
			switch(dir){
				case tmedia_qos_direction_send:
					e2e->send.current = tsk_true;
					break;
				case tmedia_qos_direction_recv:
					e2e->recv.current = tsk_true;
					break;
				case tmedia_qos_direction_sendrecv:
					e2e->send.current = tsk_true;
					e2e->recv.current = tsk_true;
					break;
				case tmedia_qos_direction_none:
					e2e->send.current = tsk_false;
					e2e->recv.current = tsk_false;
					break;
				default:
					break;
			}
		}
		else{
			TSK_DEBUG_ERROR("Failed to parse a=%s:%s", A->field, A->value);
		}
	}

	/* des */
	for(i = 0; (A = tsdp_header_M_findA_at(m, "des", i)); i++){
		if(sscanf(A->value, "qos %10s e2e %10s", s0, s1) != EOF){
			tmedia_qos_strength_t strength = tmedia_qos_strength_fromstring(s0);
			tmedia_qos_direction_t dir = tmedia_qos_direction_fromstring(s1);
			switch(dir){
				case tmedia_qos_direction_send:
					e2e->send.strength = strength;
					break;
				case tmedia_qos_direction_recv:
					e2e->recv.strength = strength;
					break;
				case tmedia_qos_direction_sendrecv:
					e2e->send.strength = strength;
					e2e->recv.strength = strength;
					break;
				default:
					break;
			}
		}
		else{
			TSK_DEBUG_ERROR("Failed to parse a=%s:%s", A->field, A->value);
		}
	}
	
	/* conf */
	for(i = 0; (A = tsdp_header_M_findA_at(m, "conf", i)); i++){
		if(sscanf(A->value, "qos e2e %10s", s0) != EOF){
			tmedia_qos_direction_t dir = tmedia_qos_direction_fromstring(s0);
			switch(dir){
				case tmedia_qos_direction_send:
					e2e->send.confirm = tsk_true;
					break;
				case tmedia_qos_direction_recv:
					e2e->recv.confirm = tsk_true;
					break;
				case tmedia_qos_direction_sendrecv:
					e2e->send.confirm = tsk_true;
					e2e->recv.confirm = tsk_true;
					break;
				default:
					break;
			}
		}
		else{
			TSK_DEBUG_ERROR("Failed to parse a=%s:%s", A->field, A->value);
		}
	}

	return e2e;
}

int tmedia_qos_tline_e2e_to_sdp(const tmedia_qos_tline_e2e_t* self, tsdp_header_M_t* m)
{
   /* RFC 3312 - 5.1.1 SDP encoding

	   For the end-to-end status type, the user agent MUST generate one
	   current status line with the tag "e2e" for the media stream.  If the
	   strength-tags for both directions are equal (e.g., both "mandatory")
	   in the transaction status table, the user agent MUST add one desired
	   status line with the tag "sendrecv".  If both tags are different, the
	   user agent MUST include two desired status lines, one with the tag
	   "send" and the other with the tag "recv".
	*/
	char* temp = tsk_null;

	if(!self || !m){
		TSK_DEBUG_ERROR("invalid parameter");
		return -1;
	}
	/*  Example
		a=curr:qos e2e none
		a=des:qos mandatory e2e sendrecv
	*/

	
	/* curr */
	tsk_sprintf(&temp, "qos e2e %s", (self->recv.current && self->send.current) ? "sendrecv" : (self->recv.current ? "recv" : (self->send.current ? "send" : "none")));
	tsdp_header_M_add_headers(m,
		TSDP_HEADER_A_VA_ARGS("curr", temp),
		tsk_null);
	TSK_FREE(temp);
	
	/* des */
	if(self->recv.strength == self->send.strength){
		/* sendrecv */
		tsk_sprintf(&temp, "qos %s e2e sendrecv", tmedia_qos_strength_tostring(self->recv.strength));
		tsdp_header_M_add_headers(m,
			TSDP_HEADER_A_VA_ARGS("des", temp),
			tsk_null);
		TSK_FREE(temp);
	}
	else{
		/* send */
		tsk_sprintf(&temp, "qos %s e2e send", tmedia_qos_strength_tostring(self->send.strength));
		tsdp_header_M_add_headers(m,
			TSDP_HEADER_A_VA_ARGS("des", temp),
			tsk_null);
		TSK_FREE(temp);

		/* recv */
		tsk_sprintf(&temp, "qos %s e2e recv", tmedia_qos_strength_tostring(self->recv.strength));
		tsdp_header_M_add_headers(m,
			TSDP_HEADER_A_VA_ARGS("des", temp),
			tsk_null);
		TSK_FREE(temp);
	}

	/* conf (should not request confirm on "send" direction)*/
	if(self->recv.confirm){
		tsdp_header_M_add_headers(m,
			TSDP_HEADER_A_VA_ARGS("conf", "qos e2e recv"),
			tsk_null);
	}
	return 0;
}

int tmedia_qos_tline_e2e_set_ro(tmedia_qos_tline_e2e_t* self, const tmedia_qos_tline_e2e_t* ro)
{
	if(!self || !ro){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	/* We were the offerer
	* Remote asked for confirmation in its "recv" direction? 
	* "recv" direction for remote is our "send" direction
	* As we don't support RSVP (under the way), confirm immediatly.
	* "send" direction should not requested for confirmation
	*/
	if(ro->recv.confirm){
		self->send.current = tsk_true;
		goto bail;
	}
	if(ro->send.current){
		self->recv.confirm = tsk_false; /* remote confirmed */
		self->recv.current = tsk_true; /* because ro confirmed */
		self->send.current = tsk_true; /* beacuse we don't support RSVP */
		goto bail;
	}

	/* We are the answerer
	* As we don't support RSVP (under the way):
	* ==> request confirmation for "recv" direction if equal to "none" (not reserved)
	* =>
	*/
	if(!self->recv.current){
		self->recv.confirm = tsk_true;
		goto bail;
	}

bail:
	/* all other cases: success */
	return 0;
}

tsk_bool_t tmedia_qos_tline_e2e_canresume(const tmedia_qos_tline_e2e_t* self)
{
	if(!self){
		TSK_DEBUG_ERROR("Invalid parameter");
		return tsk_true;
	}

	/*  Example
		a=curr:qos e2e none
		a=des:qos mandatory e2e sendrecv

		Or

		a=curr:qos e2e send
		a=des:qos mandatory e2e recv
		a=des:qos optional e2e send
	*/

	/* only "mandatory" strength should force the application to continue nego. */
	if(self->recv.strength == tmedia_qos_strength_mandatory && !self->recv.current){
		return tsk_false;
	}
	/*else */if(self->send.strength == tmedia_qos_strength_mandatory && !self->send.current){
		return tsk_false;
	}
	
	/* "optinal" and "none" strengths */
	return tsk_true;
}

//
//	E2E QoS line object definition
//
static tsk_object_t* tmedia_qos_tline_e2e_ctor(tsk_object_t * self, va_list * app)
{
	tmedia_qos_tline_e2e_t *e2e = self;
	if(e2e){
		e2e->recv.strength = e2e->send.strength = va_arg(*app, tmedia_qos_strength_t);
		TMEDIA_QOS_TLINE(e2e)->type = tmedia_qos_stype_e2e;
	}
	return self;
}

static tsk_object_t* tmedia_qos_tline_e2e_dtor(tsk_object_t * self)
{ 
	tmedia_qos_tline_e2e_t *e2e = self;
	if(e2e){
	}

	return self;
}

static const tsk_object_def_t tmedia_qos_tline_e2e_def_s = 
{
	sizeof(tmedia_qos_tline_e2e_t),
	tmedia_qos_tline_e2e_ctor, 
	tmedia_qos_tline_e2e_dtor,
	tsk_null, 
};
const tsk_object_def_t *tmedia_qos_tline_e2e_def_t = &tmedia_qos_tline_e2e_def_s;










/* ========================= Segmented ==================================*/

tmedia_qos_tline_segmented_t* tmedia_qos_tline_segmented_create(tmedia_qos_strength_t strength)
{
	return tsk_object_new(tmedia_qos_tline_segmented_def_t, strength);
}

tmedia_qos_tline_segmented_t* tmedia_qos_tline_segmented_from_sdp(const tsdp_header_M_t* m)
{
	tmedia_qos_tline_segmented_t* segmented = tsk_null;
	const tsdp_header_A_t* A;
	tsk_size_t i;

	char s0[10], s1[10], s2[10];

	if(!m){
		TSK_DEBUG_ERROR("invalid parameter");
		return tsk_null;
	}
	
	/*  Example
		a=curr:qos local none
		a=curr:qos remote none
		a=des:qos optional remote send
		a=des:qos none remote recv
		a=des:qos none local sendrecv
		a=conf:qos local recv
	*/
	segmented = tmedia_qos_tline_segmented_create(tmedia_qos_strength_unknown);

	/* curr */
	for(i = 0; (A = tsdp_header_M_findA_at(m, "curr", i)); i++){
		if(sscanf(A->value, "qos %10s %10s", s0, s1) != EOF){
			/* For segmented, s0 should be equal to "local" or "remote" */
			tmedia_qos_direction_t dir = tmedia_qos_direction_fromstring(s1);
			if(tsk_strequals(s0, "local")){
				/* local */
				switch(dir){
					case tmedia_qos_direction_send:
						segmented->local_send.current = tsk_true;
						break;
					case tmedia_qos_direction_recv:
						segmented->local_recv.current = tsk_true;
						break;
					case tmedia_qos_direction_sendrecv:
						segmented->local_send.current = tsk_true;
						segmented->local_recv.current = tsk_true;
						break;
					case tmedia_qos_direction_none:
						segmented->local_send.current = tsk_false;
						segmented->local_recv.current = tsk_false;
						break;
					default:
						break;
				}
			}
			else{
				/* remote */
				switch(dir){
					case tmedia_qos_direction_send:
						segmented->remote_send.current = tsk_true;
						break;
					case tmedia_qos_direction_recv:
						segmented->remote_recv.current = tsk_true;
						break;
					case tmedia_qos_direction_sendrecv:
						segmented->remote_send.current = tsk_true;
						segmented->remote_recv.current = tsk_true;
						break;
					case tmedia_qos_direction_none:
						segmented->remote_send.current = tsk_false;
						segmented->remote_recv.current = tsk_false;
						break;
					default:
						break;
				}
			}
		}
		else{
			TSK_DEBUG_ERROR("Failed to parse a=%s:%s", A->field, A->value);
		}
	}

	/* des */
	for(i = 0; (A = tsdp_header_M_findA_at(m, "des", i)); i++){
		if(sscanf(A->value, "qos %10s %10s %10s", s0, s1, s2) != EOF){
			/* For segmented, s1 should be equal to "local" or "remote" */
			tmedia_qos_strength_t strength = tmedia_qos_strength_fromstring(s0);
			tmedia_qos_direction_t dir = tmedia_qos_direction_fromstring(s2);
			if(tsk_strequals(s1, "local")){
				/* local */
				switch(dir){
					case tmedia_qos_direction_send:
						segmented->local_send.strength = strength;
						break;
					case tmedia_qos_direction_recv:
						segmented->local_recv.strength = strength;
						break;
					case tmedia_qos_direction_sendrecv:
						segmented->local_send.strength = strength;
						segmented->local_recv.strength = strength;
						break;
					default:
						break;
				}
			}
			else{
				/* remote */
				switch(dir){
					case tmedia_qos_direction_send:
						segmented->remote_send.strength = strength;
						break;
					case tmedia_qos_direction_recv:
						segmented->remote_recv.strength = strength;
						break;
					case tmedia_qos_direction_sendrecv:
						segmented->remote_send.strength = strength;
						segmented->remote_recv.strength = strength;
						break;
					default:
						break;
				}
			}
		}
		else{
			TSK_DEBUG_ERROR("Failed to parse a=%s:%s", A->field, A->value);
		}
	}

	/* conf */
	for(i = 0; (A = tsdp_header_M_findA_at(m, "conf", i)); i++){
		if(sscanf(A->value, "qos %10s %10s", s0, s1) != EOF){
			/* For segmented, s0 should be equal to "local" or "remote" */
			tmedia_qos_direction_t dir = tmedia_qos_direction_fromstring(s1);
			if(tsk_strequals(s0, "local")){
				/* local */
				switch(dir){
					case tmedia_qos_direction_send:
						segmented->local_send.confirm = tsk_true;
						break;
					case tmedia_qos_direction_recv:
						segmented->local_recv.confirm = tsk_true;
						break;
					case tmedia_qos_direction_sendrecv:
						segmented->local_send.confirm = tsk_true;
						segmented->local_recv.confirm = tsk_true;
						break;
					default:
						break;
				}
			}
			else{
				/* remote */
				switch(dir){
					case tmedia_qos_direction_send:
						segmented->remote_send.confirm = tsk_true;
						break;
					case tmedia_qos_direction_recv:
						segmented->remote_recv.confirm = tsk_true;
						break;
					case tmedia_qos_direction_sendrecv:
						segmented->remote_send.confirm = tsk_true;
						segmented->remote_recv.confirm = tsk_true;
						break;
					default:
						break;
				}
			}
		}
		else{
			TSK_DEBUG_ERROR("Failed to parse a=%s:%s", A->field, A->value);
		}
	}

	return segmented;
}

int tmedia_qos_tline_segmented_to_sdp(const tmedia_qos_tline_segmented_t* self, tsdp_header_M_t* m)
{
	/*	RFC 3312 - 5.1.1 SDP encoding
		
		For the segmented status type, the user agent MUST generate two
		current status lines: one with the tag "local" and the other with the
		tag "remote".  The user agent MUST add one or two desired status
		lines per segment (i.e., local and remote).  If, for a particular
		segment (local or remote), the tags for both directions in the
		transaction status table are equal (e.g., both "mandatory"), the user
		agent MUST add one desired status line with the tag "sendrecv".  If
		both tags are different, the user agent MUST include two desired
		status lines, one with the tag "send" and the other with the tag "recv".
	*/
	char* temp = tsk_null;

	if(!self || !m){
		TSK_DEBUG_ERROR("invalid parameter");
		return -1;
	}

	/*  Example
		a=curr:qos local none
		a=curr:qos remote none
		a=des:qos optional remote send
		a=des:qos none remote recv
		a=des:qos none local sendrecv
	*/

	/* curr:local */
	tsk_sprintf(&temp, "qos local %s", (self->local_recv.current && self->local_send.current) ? "sendrecv" : (self->local_recv.current ? "recv" : (self->local_send.current ? "send" : "none")));
		tsdp_header_M_add_headers(m,
			TSDP_HEADER_A_VA_ARGS("curr", temp),
			tsk_null);
		TSK_FREE(temp);

	/* curr:remote */
	tsk_sprintf(&temp, "qos remote %s", (self->remote_recv.current && self->remote_send.current) ? "sendrecv" : (self->remote_recv.current ? "recv" : (self->remote_send.current ? "send" : "none")));
	tsdp_header_M_add_headers(m,
		TSDP_HEADER_A_VA_ARGS("curr", temp),
		tsk_null);
	TSK_FREE(temp);

	
	/* des:local */
	if(self->local_recv.strength == self->local_send.strength){
		/* sendrecv */
		tsk_sprintf(&temp, "qos %s local sendrecv", tmedia_qos_strength_tostring(self->local_send.strength));
		tsdp_header_M_add_headers(m,
			TSDP_HEADER_A_VA_ARGS("des", temp),
			tsk_null);
		TSK_FREE(temp);
	}
	else{
		/* send */
		tsk_sprintf(&temp, "qos %s local send", tmedia_qos_strength_tostring(self->local_send.strength));
		tsdp_header_M_add_headers(m,
			TSDP_HEADER_A_VA_ARGS("des", temp),
			tsk_null);
		TSK_FREE(temp);

		/* recv */
		tsk_sprintf(&temp, "qos %s local recv", tmedia_qos_strength_tostring(self->local_recv.strength));
		tsdp_header_M_add_headers(m,
			TSDP_HEADER_A_VA_ARGS("des", temp),
			tsk_null);
		TSK_FREE(temp);
	}
	

	/* des:remote */
	if(self->remote_recv.strength == self->remote_send.strength){
		/* sendrecv */
		tsk_sprintf(&temp, "qos %s remote sendrecv", tmedia_qos_strength_tostring(self->remote_send.strength));
		tsdp_header_M_add_headers(m,
			TSDP_HEADER_A_VA_ARGS("des", temp),
			tsk_null);
		TSK_FREE(temp);
	}
	else{
		/* send */
		tsk_sprintf(&temp, "qos %s remote send", tmedia_qos_strength_tostring(self->remote_send.strength));
		tsdp_header_M_add_headers(m,
			TSDP_HEADER_A_VA_ARGS("des", temp),
			tsk_null);
		TSK_FREE(temp);

		/* recv */
		tsk_sprintf(&temp, "qos %s remote recv", tmedia_qos_strength_tostring(self->remote_recv.strength));
		tsdp_header_M_add_headers(m,
			TSDP_HEADER_A_VA_ARGS("des", temp),
			tsk_null);
		TSK_FREE(temp);
	}

	/* conf */
	if(self->remote_recv.confirm || self->remote_send.confirm){
		tsk_sprintf(&temp, "qos remote %s", (self->remote_recv.confirm && self->remote_send.confirm) ? "sendrecv" : (self->remote_recv.confirm ? "recv" : (self->remote_send.confirm ? "send" : "none")));
		tsdp_header_M_add_headers(m,
			TSDP_HEADER_A_VA_ARGS("conf", temp),
			tsk_null);
		TSK_FREE(temp);
	}

	return 0;
}

int tmedia_qos_tline_segmented_set_ro(tmedia_qos_tline_segmented_t* self, const tmedia_qos_tline_segmented_t* ro)
{
	if(!self || !ro){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	//////////////
	if(!ro->local_recv.current && !ro->remote_recv.confirm){
		/* request confirmation */
		self->remote_recv.confirm = tsk_true;
	}
	else{
		self->remote_recv.confirm = tsk_false;
		self->local_recv.current = tsk_true;
	}
	if(!ro->local_send.current && !ro->remote_send.confirm){
		/* request confirmation */
		self->remote_send.confirm = tsk_true;
	}
	else{
		self->remote_send.confirm = tsk_false;
		self->local_send.current = tsk_true;
	}

	//////////////
	if(ro->remote_recv.confirm){
		self->local_recv.current = tsk_true;
	}
	if(ro->remote_send.confirm){
		self->local_send.current = tsk_true;
	}

	//////////////
	if(ro->local_recv.current){
		self->remote_recv.current = tsk_true;
	}
	if(ro->local_send.current){
		self->remote_send.current = tsk_true;
	}

	return 0;
}

tsk_bool_t tmedia_qos_tline_segmented_canresume(const tmedia_qos_tline_segmented_t* self)
{
	if(!self){
		TSK_DEBUG_ERROR("Invalid parameter");
		return tsk_true;
	}

	/* == Strength is mandatory == */
	if(self->local_recv.strength == tmedia_qos_strength_mandatory && !self->local_recv.current){
		return tsk_false;
	}
	
	if(self->local_send.strength == tmedia_qos_strength_mandatory && !self->local_send.current){
		return tsk_false;
	}

	if(self->remote_recv.strength == tmedia_qos_strength_mandatory && !self->remote_recv.current){
		return tsk_false;
	}

	if(self->remote_send.strength == tmedia_qos_strength_mandatory && !self->remote_send.current){
		return tsk_false;
	}

	/* "optinal" and "none" strengths */
	return tsk_true;
}

//
//	Segmented QoS line object definition
//
static tsk_object_t* tmedia_qos_tline_segmented_ctor(tsk_object_t * self, va_list * app)
{
	tmedia_qos_tline_segmented_t *segmented = self;
	if(segmented){
		segmented->local_recv.strength = segmented->local_send.strength
			= segmented->remote_recv.strength = segmented->remote_send.strength = va_arg(*app, tmedia_qos_strength_t);
		TMEDIA_QOS_TLINE(segmented)->type = tmedia_qos_stype_segmented;
	}
	return self;
}

static tsk_object_t* tmedia_qos_tline_segmented_dtor(tsk_object_t * self)
{ 
	tmedia_qos_tline_segmented_t *segmented = self;
	if(segmented){
	}

	return self;
}

static const tsk_object_def_t tmedia_qos_tline_segmented_def_s = 
{
	sizeof(tmedia_qos_tline_segmented_t),
	tmedia_qos_tline_segmented_ctor, 
	tmedia_qos_tline_segmented_dtor,
	tsk_null, 
};
const tsk_object_def_t *tmedia_qos_tline_segmented_def_t = &tmedia_qos_tline_segmented_def_s;
OpenPOWER on IntegriCloud