aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libdvben50221/en50221_session.c
blob: 3fb9902c8cdfd5037abb50dc6b75fcd599141394 (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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
/*
    en50221 encoder An implementation for libdvb
    an implementation for the en50221 transport layer

    Copyright (C) 2004, 2005 Manu Abraham <abraham.manu@gmail.com>
    Copyright (C) 2005 Julian Scheel (julian at jusst dot de)
    Copyright (C) 2006 Andrew de Quincey (adq_dvb@lidskialf.net)

    This library is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as
    published by the Free Software Foundation; either version 2.1 of
    the License, or (at your option) any later version.

    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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
*/

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <time.h>
#include <libdvbmisc/dvbmisc.h>
#include <sys/uio.h>
#include <pthread.h>
#include "en50221_transport.h"
#include "en50221_session.h"
#include "en50221_errno.h"
#include "asn_1.h"


// these are the possible session statuses
#define S_STATUS_OPEN                    0x00	// session is opened
#define S_STATUS_CLOSE_NO_RES            0xF0	// could not open session, no proper resource available
#define S_STATUS_CLOSE_RES_UNAVAILABLE   0xF1	// could not open session, resource unavailable
#define S_STATUS_CLOSE_RES_LOW_VERSION   0xF2	// could not open session, resource version too low
#define S_STATUS_CLOSE_RES_BUSY          0xF3	// could not open session, resource is busy

#define ST_OPEN_SESSION_REQ     0x91	// h<--m
#define ST_OPEN_SESSION_RES     0x92	// h-->m
#define ST_CREATE_SESSION       0x93	// h-->m
#define ST_CREATE_SESSION_RES   0x94	// h<--m
#define ST_CLOSE_SESSION_REQ    0x95	// h<->m
#define ST_CLOSE_SESSION_RES    0x96	// h<->m
#define ST_SESSION_NUMBER       0x90	// h<->m

#define S_STATE_IDLE            0x01	// this session is not in use
#define S_STATE_ACTIVE          0x02	// this session is in use
#define S_STATE_IN_CREATION     0x04	// this session waits for a ST_CREATE_SESSION_RES to become active
#define S_STATE_IN_DELETION     0x08	// this session waits for ST_CLOSE_SESSION_RES to become idle again


// for each session we store its identifier, the resource-id
// it is linked to and the callback of the specific resource
struct en50221_session {
	uint8_t state;
	uint32_t resource_id;
	uint8_t slot_id;
	uint8_t connection_id;

	en50221_sl_resource_callback callback;
	void *callback_arg;

	pthread_mutex_t session_lock;
};

struct en50221_session_layer {
	uint32_t max_sessions;
	struct en50221_transport_layer *tl;

	en50221_sl_lookup_callback lookup;
	void *lookup_arg;

	en50221_sl_session_callback session;
	void *session_arg;

	pthread_mutex_t global_lock;
	pthread_mutex_t setcallback_lock;

	int error;

	struct en50221_session *sessions;
};

static void en50221_sl_transport_callback(void *arg, int reason,
					  uint8_t * data,
					  uint32_t data_length,
					  uint8_t slot_id,
					  uint8_t connection_id);
static int en50221_sl_alloc_new_session(struct en50221_session_layer *sl,
					uint32_t resource_id,
					uint8_t slot_id,
					uint8_t connection_id,
					en50221_sl_resource_callback
					callback, void *arg);




struct en50221_session_layer *en50221_sl_create(struct en50221_transport_layer *tl,
						uint32_t max_sessions)
{
	struct en50221_session_layer *sl = NULL;
	uint32_t i;

	// setup structure
	sl = (struct en50221_session_layer *)
	    malloc(sizeof(struct en50221_session_layer));
	if (sl == NULL)
		goto error_exit;
	sl->max_sessions = max_sessions;
	sl->lookup = NULL;
	sl->session = NULL;
	sl->tl = tl;
	sl->error = 0;

	// init the mutex
	pthread_mutex_init(&sl->global_lock, NULL);
	pthread_mutex_init(&sl->setcallback_lock, NULL);

	// create the slots
	sl->sessions = malloc(sizeof(struct en50221_session) * max_sessions);
	if (sl->sessions == NULL)
		goto error_exit;

	// set them up
	for (i = 0; i < max_sessions; i++) {
		sl->sessions[i].state = S_STATE_IDLE;
		sl->sessions[i].callback = NULL;

		pthread_mutex_init(&sl->sessions[i].session_lock, NULL);
	}

	// register ourselves with the transport layer
	en50221_tl_register_callback(tl, en50221_sl_transport_callback, sl);

	return sl;

error_exit:
	en50221_sl_destroy(sl);
	return NULL;
}

void en50221_sl_destroy(struct en50221_session_layer *sl)
{
	uint32_t i;

	if (sl) {
		if (sl->sessions) {
			for (i = 0; i < sl->max_sessions; i++) {
				pthread_mutex_destroy(&sl->sessions[i].session_lock);
			}
			free(sl->sessions);
		}

		pthread_mutex_destroy(&sl->setcallback_lock);
		pthread_mutex_destroy(&sl->global_lock);

		free(sl);
	}
}

int en50221_sl_get_error(struct en50221_session_layer *sl)
{
	return sl->error;
}

void en50221_sl_register_lookup_callback(struct en50221_session_layer *sl,
					 en50221_sl_lookup_callback
					 callback, void *arg)
{
	pthread_mutex_lock(&sl->setcallback_lock);
	sl->lookup = callback;
	sl->lookup_arg = arg;
	pthread_mutex_unlock(&sl->setcallback_lock);
}

void en50221_sl_register_session_callback(struct en50221_session_layer *sl,
					  en50221_sl_session_callback
					  callback, void *arg)
{
	pthread_mutex_lock(&sl->setcallback_lock);
	sl->session = callback;
	sl->session_arg = arg;
	pthread_mutex_unlock(&sl->setcallback_lock);
}

int en50221_sl_create_session(struct en50221_session_layer *sl,
			      int slot_id, uint8_t connection_id,
			      uint32_t resource_id,
			      en50221_sl_resource_callback callback,
			      void *arg)
{
	// lookup next free session_id:
	pthread_mutex_lock(&sl->global_lock);
	int session_number =
	    en50221_sl_alloc_new_session(sl, resource_id, slot_id,
					 connection_id, callback, arg);
	if (session_number == -1) {
		pthread_mutex_unlock(&sl->global_lock);
		return -1;
	}
	pthread_mutex_unlock(&sl->global_lock);

	// make up the header
	uint8_t hdr[8];
	hdr[0] = ST_CREATE_SESSION;
	hdr[1] = 6;
	hdr[2] = resource_id >> 24;
	hdr[3] = resource_id >> 16;
	hdr[4] = resource_id >> 8;
	hdr[5] = resource_id;
	hdr[6] = session_number >> 8;
	hdr[7] = session_number;

	// send this command
	if (en50221_tl_send_data(sl->tl, slot_id, connection_id, hdr, 8)) {
		pthread_mutex_lock(&sl->sessions[session_number].session_lock);
		if (sl->sessions[session_number].state == S_STATE_IN_CREATION) {
			sl->sessions[session_number].state = S_STATE_IDLE;
		}
		pthread_mutex_unlock(&sl->sessions[session_number].session_lock);

		sl->error = en50221_tl_get_error(sl->tl);
		return -1;
	}
	// ok.
	return session_number;
}

int en50221_sl_destroy_session(struct en50221_session_layer *sl,
			       uint16_t session_number)
{
	if (session_number >= sl->max_sessions) {
		sl->error = EN50221ERR_BADSESSIONNUMBER;
		return -1;
	}

	pthread_mutex_lock(&sl->sessions[session_number].session_lock);
	if (!(sl->sessions[session_number].state & (S_STATE_ACTIVE | S_STATE_IN_DELETION))) {
		sl->error = EN50221ERR_BADSESSIONNUMBER;
		pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
		return -1;
	}
	// set the state
	sl->sessions[session_number].state = S_STATE_IN_DELETION;

	// get essential details
	uint8_t slot_id = sl->sessions[session_number].slot_id;
	uint8_t connection_id = sl->sessions[session_number].connection_id;
	pthread_mutex_unlock(&sl->sessions[session_number].session_lock);

	//  sendit
	uint8_t hdr[4];
	hdr[0] = ST_CLOSE_SESSION_REQ;
	hdr[1] = 2;
	hdr[2] = session_number >> 8;
	hdr[3] = session_number;
	if (en50221_tl_send_data(sl->tl, slot_id, connection_id, hdr, 4)) {
		pthread_mutex_lock(&sl->sessions[session_number].session_lock);
		if (sl->sessions[session_number].state == S_STATE_IN_DELETION) {
			sl->sessions[session_number].state = S_STATE_IDLE;
		}
		pthread_mutex_unlock(&sl->sessions[session_number].session_lock);

		sl->error = en50221_tl_get_error(sl->tl);
		return -1;
	}

	return 0;
}

int en50221_sl_send_data(struct en50221_session_layer *sl,
			 uint16_t session_number,
			 uint8_t *data,
			 uint16_t data_length)
{
	if (session_number >= sl->max_sessions) {
		sl->error = EN50221ERR_BADSESSIONNUMBER;
		return -1;
	}

	pthread_mutex_lock(&sl->sessions[session_number].session_lock);
	if (sl->sessions[session_number].state != S_STATE_ACTIVE) {
		sl->error = EN50221ERR_BADSESSIONNUMBER;
		pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
		return -1;
	}
	// get essential details
	uint8_t slot_id = sl->sessions[session_number].slot_id;
	uint8_t connection_id = sl->sessions[session_number].connection_id;
	pthread_mutex_unlock(&sl->sessions[session_number].session_lock);

	// sendit
	struct iovec iov[2];
	uint8_t hdr[4];
	hdr[0] = ST_SESSION_NUMBER;
	hdr[1] = 2;
	hdr[2] = session_number >> 8;
	hdr[3] = session_number;
	iov[0].iov_base = hdr;
	iov[0].iov_len = 4;
	iov[1].iov_base = data;
	iov[1].iov_len = data_length;
	if (en50221_tl_send_datav(sl->tl, slot_id, connection_id, iov, 2)) {
		sl->error = en50221_tl_get_error(sl->tl);
		return -1;
	}

	return 0;
}

int en50221_sl_send_datav(struct en50221_session_layer *sl,
			  uint16_t session_number,
			  struct iovec *vector,
			  int iov_count)
{
	if (session_number >= sl->max_sessions) {
		sl->error = EN50221ERR_BADSESSIONNUMBER;
		return -1;
	}

	pthread_mutex_lock(&sl->sessions[session_number].session_lock);
	if (sl->sessions[session_number].state != S_STATE_ACTIVE) {
		sl->error = EN50221ERR_BADSESSIONNUMBER;
		pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
		return -1;
	}
	if (iov_count > 9) {
		sl->error = EN50221ERR_IOVLIMIT;
		pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
		return -1;
	}
	uint8_t slot_id = sl->sessions[session_number].slot_id;
	uint8_t connection_id = sl->sessions[session_number].connection_id;
	pthread_mutex_unlock(&sl->sessions[session_number].session_lock);

	// make up the header
	struct iovec out_iov[10];
	uint8_t hdr[4];
	hdr[0] = ST_SESSION_NUMBER;
	hdr[1] = 2;
	hdr[2] = session_number >> 8;
	hdr[3] = session_number;
	out_iov[0].iov_base = hdr;
	out_iov[0].iov_len = 4;

	// make up the data
	memcpy(&out_iov[1], vector, iov_count * sizeof(struct iovec));

	// send this command
	if (en50221_tl_send_datav(sl->tl, slot_id, connection_id, out_iov, iov_count + 1)) {
		sl->error = en50221_tl_get_error(sl->tl);
		return -1;
	}
	return 0;
}

int en50221_sl_broadcast_data(struct en50221_session_layer *sl,
			      int slot_id, uint32_t resource_id,
			      uint8_t *data, uint16_t data_length)
{
	uint32_t i;

	for (i = 0; i < sl->max_sessions; i++) {
		pthread_mutex_lock(&sl->sessions[i].session_lock);

		if (sl->sessions[i].state != S_STATE_ACTIVE) {
			pthread_mutex_unlock(&sl->sessions[i].session_lock);
			continue;
		}
		if ((slot_id != -1)
		    && (slot_id != sl->sessions[i].slot_id)) {
			pthread_mutex_unlock(&sl->sessions[i].session_lock);
			continue;
		}

		if (sl->sessions[i].resource_id == resource_id) {
			pthread_mutex_unlock(&sl->sessions[i].session_lock);
			en50221_sl_send_data(sl, i, data, data_length);
		} else {
			pthread_mutex_unlock(&sl->sessions[i].session_lock);
		}
	}

	return 0;
}



static void en50221_sl_handle_open_session_request(struct en50221_session_layer *sl,
						   uint8_t *data,
						   uint32_t data_length,
						   uint8_t slot_id,
						   uint8_t connection_id)
{
	// check
	if (data_length < 5) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received data with invalid length from module on slot %02x\n",
		      slot_id);
		return;
	}
	if (data[0] != 4) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received data with invalid length from module on slot %02x\n",
		      slot_id);
		return;
	}
	// get the resource id
	uint32_t requested_resource_id =
	    (data[1] << 24) | (data[2] << 16) | (data[3] << 8) | data[4];

	// get lookup callback details
	pthread_mutex_lock(&sl->setcallback_lock);
	en50221_sl_lookup_callback lcb = sl->lookup;
	void *lcb_arg = sl->lookup_arg;
	pthread_mutex_unlock(&sl->setcallback_lock);

	// first of all, lookup this resource id
	int status = S_STATUS_CLOSE_NO_RES;
	en50221_sl_resource_callback resource_callback = NULL;
	void *resource_arg = NULL;
	uint32_t connected_resource_id;
	if (lcb) {
		status =
		    lcb(lcb_arg, slot_id, requested_resource_id,
			&resource_callback, &resource_arg,
			&connected_resource_id);
		switch (status) {
		case 0:
			status = S_STATUS_OPEN;
			break;

		case -1:
			status = S_STATUS_CLOSE_NO_RES;
			break;

		case -2:
			status = S_STATUS_CLOSE_RES_LOW_VERSION;
			break;

		case -3:
			status = S_STATUS_CLOSE_RES_UNAVAILABLE;
			break;
		}
	}
	// if we found it, get a new session for it
	int session_number = -1;
	if (status == S_STATUS_OPEN) {
		// lookup next free session_id:
		pthread_mutex_lock(&sl->global_lock);
		session_number =
		    en50221_sl_alloc_new_session(sl, connected_resource_id,
						 slot_id, connection_id,
						 resource_callback,
						 resource_arg);
		pthread_mutex_unlock(&sl->global_lock);

		if (session_number == -1) {
			status = S_STATUS_CLOSE_NO_RES;
		} else {
			// inform upper layers/ check availability
			pthread_mutex_lock(&sl->setcallback_lock);
			en50221_sl_session_callback cb = sl->session;
			void *cb_arg = sl->session_arg;
			pthread_mutex_unlock(&sl->setcallback_lock);
			if (cb) {
				if (cb(cb_arg, S_SCALLBACK_REASON_CAMCONNECTING,
				       slot_id, session_number,
				       connected_resource_id)) {
					status = S_STATUS_CLOSE_RES_BUSY;
				}
			} else {
				status = S_STATUS_CLOSE_RES_UNAVAILABLE;
			}
		}
	}
	// send response
	uint8_t hdr[9];
	hdr[0] = ST_OPEN_SESSION_RES;
	hdr[1] = 7;
	hdr[2] = status;
	hdr[3] = connected_resource_id >> 24;
	hdr[4] = connected_resource_id >> 16;
	hdr[5] = connected_resource_id >> 8;
	hdr[6] = connected_resource_id;
	hdr[7] = session_number >> 8;
	hdr[8] = session_number;
	if (en50221_tl_send_data(sl->tl, slot_id, connection_id, hdr, 9)) {
		print(LOG_LEVEL, ERROR, 1,
		      "Transport layer error %i occurred\n",
		      en50221_tl_get_error(sl->tl));
		status = S_STATUS_CLOSE_NO_RES;
		// fallthrough
	}
	// inform upper layers what happened
	if (session_number != -1) {
		// setup session state apppropriately from upper layer response
		pthread_mutex_lock(&sl->sessions[session_number].session_lock);
		if (status != S_STATUS_OPEN) {
			sl->sessions[session_number].state = S_STATE_IDLE;
		} else {
			sl->sessions[session_number].state = S_STATE_ACTIVE;
		}
		pthread_mutex_unlock(&sl->sessions[session_number].session_lock);

		// tell upper layers
		if (sl->sessions[session_number].state == S_STATE_ACTIVE) {
			pthread_mutex_lock(&sl->setcallback_lock);
			en50221_sl_session_callback cb = sl->session;
			void *cb_arg = sl->session_arg;
			pthread_mutex_unlock(&sl->setcallback_lock);

			if (status == S_STATUS_OPEN) {
				if (cb)
					cb(cb_arg,
					   S_SCALLBACK_REASON_CAMCONNECTED,
					   slot_id, session_number,
					   connected_resource_id);
			} else {
				sl->sessions[session_number].state =
				    S_STATE_IDLE;
				if (cb)
					cb(cb_arg,
					   S_SCALLBACK_REASON_CAMCONNECTFAIL,
					   slot_id, session_number,
					   connected_resource_id);
			}
		}
	}
}

static void en50221_sl_handle_close_session_request(struct en50221_session_layer *sl,
						    uint8_t * data,
						    uint32_t data_length,
						    uint8_t slot_id,
						    uint8_t connection_id)
{
	// check
	if (data_length < 3) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received data with invalid length from module on slot %02x\n",
		      slot_id);
		return;
	}
	if (data[0] != 2) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received data with invalid length from module on slot %02x\n",
		      slot_id);
		return;
	}
	// extract session number
	uint16_t session_number = (data[1] << 8) | data[2];

	// check session number is ok
	uint8_t code = 0x00;
	uint32_t resource_id = 0;
	if (session_number >= sl->max_sessions) {
		code = 0xF0;	// session close error
		print(LOG_LEVEL, ERROR, 1, "Received bad session id %i\n",
		      slot_id);
	} else {
		pthread_mutex_lock(&sl->sessions[session_number].
				   session_lock);
		if (slot_id != sl->sessions[session_number].slot_id) {
			print(LOG_LEVEL, ERROR, 1,
			      "Received unexpected session on invalid slot %i\n",
			      slot_id);
			code = 0xF0;	// session close error
		}
		if (connection_id != sl->sessions[session_number].connection_id) {
			print(LOG_LEVEL, ERROR, 1,
			      "Received unexpected session on invalid slot %i\n",
			      slot_id);
			code = 0xF0;	// session close error
		}
		if (!(sl->sessions[session_number].state & (S_STATE_ACTIVE | S_STATE_IN_DELETION))) {
			print(LOG_LEVEL, ERROR, 1,
			      "Received unexpected session on invalid slot %i\n",
			      slot_id);
			code = 0xF0;	// session close error
		}

		if (code == 0x00) {
			sl->sessions[session_number].state = S_STATE_IDLE;
			code = 0x00;	// close ok
		}
		resource_id = sl->sessions[session_number].resource_id;
		pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
	}

	// make up the response
	uint8_t hdr[5];
	hdr[0] = ST_CLOSE_SESSION_RES;
	hdr[1] = 3;
	hdr[2] = code;
	hdr[3] = session_number >> 8;
	hdr[4] = session_number;

	// sendit
	if (en50221_tl_send_data(sl->tl, slot_id, connection_id, hdr, 5)) {
		print(LOG_LEVEL, ERROR, 1,
		      "Transport layer reports error %i on slot %i\n",
		      en50221_tl_get_error(sl->tl), slot_id);
	}
	// callback to announce destruction to resource if it was ok
	if (code == 0x00) {
		pthread_mutex_lock(&sl->setcallback_lock);
		en50221_sl_session_callback cb = sl->session;
		void *cb_arg = sl->session_arg;
		pthread_mutex_unlock(&sl->setcallback_lock);

		if (cb)
			cb(cb_arg, S_SCALLBACK_REASON_CLOSE, slot_id,
			   session_number, resource_id);
	}
}

static void en50221_sl_handle_create_session_response(struct en50221_session_layer *sl,
						      uint8_t * data,
						      uint32_t data_length,
						      uint8_t slot_id,
						      uint8_t connection_id)
{
	// check
	if (data_length < 8) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received data with invalid length from module on slot %02x\n",
		      slot_id);
		return;
	}
	if (data[0] != 7) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received data with invalid length from module on slot %02x\n",
		      slot_id);
		return;
	}
	// extract session number
	uint16_t session_number = (data[5] << 8) | data[6];

	// check session number is ok
	if (session_number >= sl->max_sessions) {
		print(LOG_LEVEL, ERROR, 1, "Received bad session id %i\n",
		      slot_id);
		return;
	}

	pthread_mutex_lock(&sl->sessions[session_number].session_lock);
	if (slot_id != sl->sessions[session_number].slot_id) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received unexpected session on invalid slot %i\n",
		      slot_id);
		pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
		return;
	}
	if (connection_id != sl->sessions[session_number].connection_id) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received unexpected session on invalid slot %i\n",
		      slot_id);
		pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
		return;
	}
	if (sl->sessions[session_number].state != S_STATE_IN_CREATION) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received unexpected session on invalid slot %i\n",
		      slot_id);
		pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
		return;
	}
	// extract status
	if (data[1] != S_STATUS_OPEN) {
		print(LOG_LEVEL, ERROR, 1,
		      "Session creation failed 0x%02x\n", data[1]);
		sl->sessions[session_number].state = S_STATE_IDLE;
		pthread_mutex_unlock(&sl->sessions[session_number].session_lock);

		// inform upper layers
		pthread_mutex_lock(&sl->setcallback_lock);
		en50221_sl_session_callback cb = sl->session;
		void *cb_arg = sl->session_arg;
		pthread_mutex_unlock(&sl->setcallback_lock);
		if (cb)
			cb(cb_arg, S_SCALLBACK_REASON_CONNECTFAIL, slot_id,
			   session_number,
			   sl->sessions[session_number].resource_id);
		return;
	}
	// set it active
	sl->sessions[session_number].state = S_STATE_ACTIVE;
	pthread_mutex_unlock(&sl->sessions[session_number].session_lock);

	// inform upper layers
	pthread_mutex_lock(&sl->setcallback_lock);
	en50221_sl_session_callback cb = sl->session;
	void *cb_arg = sl->session_arg;
	pthread_mutex_unlock(&sl->setcallback_lock);
	if (cb)
		cb(cb_arg, S_SCALLBACK_REASON_CONNECTED, slot_id,
		   session_number,
		   sl->sessions[session_number].resource_id);
}

static void en50221_sl_handle_close_session_response(struct en50221_session_layer *sl,
						     uint8_t *data,
						     uint32_t data_length,
						     uint8_t slot_id,
						     uint8_t connection_id)
{
	// check
	if (data_length < 5) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received data with invalid length from module on slot %02x\n",
		      slot_id);
		return;
	}
	if (data[0] != 4) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received data with invalid length from module on slot %02x\n",
		      slot_id);
		return;
	}
	// extract session number
	uint16_t session_number = (data[2] << 8) | data[3];

	// check session number is ok
	if (session_number >= sl->max_sessions) {
		print(LOG_LEVEL, ERROR, 1, "Received bad session id %i\n", slot_id);
		return;
	}

	pthread_mutex_lock(&sl->sessions[session_number].session_lock);
	if (slot_id != sl->sessions[session_number].slot_id) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received unexpected session on invalid slot %i\n",
		      slot_id);
		pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
		return;
	}
	if (connection_id != sl->sessions[session_number].connection_id) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received unexpected session on invalid slot %i\n",
		      slot_id);
		pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
		return;
	}
	if (sl->sessions[session_number].state != S_STATE_IN_DELETION) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received unexpected session on invalid slot %i\n",
		      slot_id);
		pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
		return;
	}
	// extract status
	if (data[1] != 0x00) {
		print(LOG_LEVEL, ERROR, 1, "Session close failed 0x%02x\n", data[1]);
		// just fallthrough anyway
	}
	// completed
	sl->sessions[session_number].state = S_STATE_IDLE;
	pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
}

static void en50221_sl_handle_session_package(struct en50221_session_layer *sl,
					      uint8_t *data,
					      uint32_t data_length,
					      uint8_t slot_id,
					      uint8_t connection_id)
{
	// check
	if (data_length < 3) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received data with invalid length from module on slot %i\n",
		      slot_id);
		return;
	}
	if (data[0] != 2) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received data with invalid length from module on slot %i\n",
		      slot_id);
		return;
	}
	// get session number
	uint16_t session_number = (data[1] << 8) | data[2];

	// check it
	if (session_number >= sl->max_sessions) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received data with bad session_number from module on slot %i\n",
		      slot_id);
		return;
	}

	pthread_mutex_lock(&sl->sessions[session_number].session_lock);
	if (slot_id != sl->sessions[session_number].slot_id) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received unexpected session on invalid slot %i\n",
		      slot_id);
		pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
		return;
	}
	if (connection_id != sl->sessions[session_number].connection_id) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received unexpected session on invalid slot %i\n",
		      slot_id);
		pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
		return;
	}
	if (sl->sessions[session_number].state != S_STATE_ACTIVE) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received data with bad session_number from module on slot %i\n",
		      slot_id);
		pthread_mutex_unlock(&sl->sessions[session_number].session_lock);
		return;
	}

	en50221_sl_resource_callback cb = sl->sessions[session_number].callback;
	void *cb_arg = sl->sessions[session_number].callback_arg;
	uint32_t resource_id = sl->sessions[session_number].resource_id;
	pthread_mutex_unlock(&sl->sessions[session_number].session_lock);

	// there can be > 1 APDU following the package - all for the same session/resource_id tho.
	data += 3;
	data_length -= 3;
	while (data_length) {
		// check length field
		if (data_length < 3) {
			print(LOG_LEVEL, ERROR, 1,
			      "Received invalid sized session package from slot %i\n",
			      slot_id);
			return;
		}
		// parse the APDU's length field
		int length_field_len;
		uint16_t asn_data_length;
		if ((length_field_len = asn_1_decode(&asn_data_length, data + 3, data_length - 3)) < 0) {
			print(LOG_LEVEL, ERROR, 1,
			      "Received invalid sized session package from slot %i\n",
			      slot_id);
			return;
		}
		uint32_t apdu_length = 3 + length_field_len + asn_data_length;

		// check there is enough data
		if (apdu_length > data_length) {
			print(LOG_LEVEL, ERROR, 1,
			      "Received invalid sized session package from slot %i\n",
			      slot_id);
			return;
		}
		// pass the APDU up to the higher layers
		if (cb)
			cb(cb_arg, slot_id, session_number, resource_id, data, apdu_length);

		// next!
		data += apdu_length;
		data_length -= apdu_length;
	}

}

static void en50221_sl_transport_callback(void *arg, int reason,
					  uint8_t *data,
					  uint32_t data_length,
					  uint8_t slot_id,
					  uint8_t connection_id)
{
	struct en50221_session_layer *sl =
	    (struct en50221_session_layer *) arg;
	uint32_t i;

	// deal with the reason for this callback
	switch (reason) {
	case T_CALLBACK_REASON_DATA:
		// fallthrough into rest of this function
		break;

	case T_CALLBACK_REASON_CONNECTIONOPEN:
	{
		pthread_mutex_lock(&sl->setcallback_lock);
		en50221_sl_session_callback cb = sl->session;
		void *cb_arg = sl->session_arg;
		pthread_mutex_unlock(&sl->setcallback_lock);

		if (cb)
			cb(cb_arg, S_SCALLBACK_REASON_TC_CONNECT,
				slot_id, connection_id, 0);
		return;
	}

	case T_CALLBACK_REASON_CAMCONNECTIONOPEN:
	{
		pthread_mutex_lock(&sl->setcallback_lock);
		en50221_sl_session_callback cb = sl->session;
		void *cb_arg = sl->session_arg;
		pthread_mutex_unlock(&sl->setcallback_lock);

		if (cb)
			cb(cb_arg,
				S_SCALLBACK_REASON_TC_CAMCONNECT,
				slot_id, connection_id, 0);
		return;
	}

	case T_CALLBACK_REASON_CONNECTIONCLOSE:
	{
		pthread_mutex_lock(&sl->setcallback_lock);
		en50221_sl_session_callback cb = sl->session;
		void *cb_arg = sl->session_arg;
		pthread_mutex_unlock(&sl->setcallback_lock);

		for (i = 0; i < sl->max_sessions; i++) {
			pthread_mutex_lock(&sl->sessions[i].session_lock);

			if (sl->sessions[i].state == S_STATE_IDLE) {
				pthread_mutex_unlock(&sl->sessions[i].session_lock);
				continue;
			}
			if (sl->sessions[i].connection_id != connection_id) {
				pthread_mutex_unlock(&sl->sessions[i].session_lock);
				continue;
			}

			sl->sessions[i].state = S_STATE_IDLE;

			uint8_t _slot_id = sl->sessions[i].slot_id;
			uint32_t resource_id = sl->sessions[i].resource_id;
			pthread_mutex_unlock(&sl->sessions[i].session_lock);

			if (cb)
				cb(cb_arg, S_SCALLBACK_REASON_CLOSE, _slot_id, i, resource_id);
		}
		return;
	}

	case T_CALLBACK_REASON_SLOTCLOSE:
	{
		pthread_mutex_lock(&sl->setcallback_lock);
		en50221_sl_session_callback cb = sl->session;
		void *cb_arg = sl->session_arg;
		pthread_mutex_unlock(&sl->setcallback_lock);

		for (i = 0; i < sl->max_sessions; i++) {
			pthread_mutex_lock(&sl->sessions[i].session_lock);

			if (sl->sessions[i].state == S_STATE_IDLE) {
				pthread_mutex_unlock(&sl->sessions[i].session_lock);
				continue;
			}
			if (sl->sessions[i].slot_id != slot_id) {
				pthread_mutex_unlock(&sl->sessions[i].session_lock);
				continue;
			}
			sl->sessions[i].state = S_STATE_IDLE;

			uint32_t resource_id = sl->sessions[i].resource_id;
			pthread_mutex_unlock(&sl->sessions[i].session_lock);

			if (cb)
				cb(cb_arg, S_SCALLBACK_REASON_CLOSE, slot_id, i, resource_id);

		}
		return;
	}
	}

	// sanity check data length
	if (data_length < 1) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received data with invalid length from module on slot %i\n",
		      slot_id);
		return;
	}
	// deal with the data
	uint8_t spdu_tag = data[0];
	switch (spdu_tag) {
	case ST_OPEN_SESSION_REQ:
		en50221_sl_handle_open_session_request(sl, data + 1,
						       data_length - 1,
						       slot_id,
						       connection_id);
		break;

	case ST_CLOSE_SESSION_REQ:
		en50221_sl_handle_close_session_request(sl, data + 1,
							data_length - 1,
							slot_id,
							connection_id);
		break;

	case ST_SESSION_NUMBER:
		en50221_sl_handle_session_package(sl, data + 1,
						  data_length - 1, slot_id,
						  connection_id);
		break;

	case ST_CREATE_SESSION_RES:
		en50221_sl_handle_create_session_response(sl, data + 1,
							  data_length - 1,
							  slot_id,
							  connection_id);
		break;

	case ST_CLOSE_SESSION_RES:
		en50221_sl_handle_close_session_response(sl, data + 1,
							 data_length - 1,
							 slot_id,
							 connection_id);
		break;

	default:
		print(LOG_LEVEL, ERROR, 1,
		      "Received unknown session tag %02x from module on slot %i",
		      spdu_tag, slot_id);
		break;
	}
}

static int en50221_sl_alloc_new_session(struct en50221_session_layer *sl,
					uint32_t resource_id,
					uint8_t slot_id,
					uint8_t connection_id,
					en50221_sl_resource_callback
					callback, void *arg)
{
	int session_number = -1;
	uint32_t i;
	for (i = 1; i < sl->max_sessions; i++) {
		if (sl->sessions[i].state == S_STATE_IDLE) {
			session_number = i;
			break;
		}
	}
	if (session_number == -1) {
		sl->error = EN50221ERR_OUTOFSESSIONS;
		return -1;
	}
	// setup the session
	sl->sessions[session_number].state = S_STATE_IN_CREATION;
	sl->sessions[session_number].resource_id = resource_id;
	sl->sessions[session_number].slot_id = slot_id;
	sl->sessions[session_number].connection_id = connection_id;
	sl->sessions[session_number].callback = callback;
	sl->sessions[session_number].callback_arg = arg;

	// ok
	return session_number;
}