aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libdvben50221/en50221_transport.c
blob: f6f46db7234e6d75f611b1216a36d01090a6cab5 (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
generated by cgit v1.2.3 (git 2.25.1) at 2024-11-12 20:12:16 +0000
 


801'>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
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
/*
    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 <pthread.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <time.h>
#include <libdvbmisc/dvbmisc.h>
#include <libdvbapi/dvbca.h>
#include "en50221_errno.h"
#include "en50221_transport.h"
#include "asn_1.h"

// these are the Transport Tags, like
// described in EN50221, Annex A.4.1.13 (pg70)
#define T_SB                0x80	// sb                           primitive   h<--m
#define T_RCV               0x81	// receive                      primitive   h-->m
#define T_CREATE_T_C        0x82	// create transport connection  primitive   h-->m
#define T_C_T_C_REPLY       0x83	// ctc reply                    primitive   h<--m
#define T_DELETE_T_C        0x84	// delete tc                    primitive   h<->m
#define T_D_T_C_REPLY       0x85	// dtc reply                    primitive   h<->m
#define T_REQUEST_T_C       0x86	// request transport connection primitive   h<--m
#define T_NEW_T_C           0x87	// new tc / reply to t_request  primitive   h-->m
#define T_T_C_ERROR         0x77	// error creating tc            primitive   h-->m
#define T_DATA_LAST         0xA0	// convey data from higher      constructed h<->m
				 // layers
#define T_DATA_MORE         0xA1	// convey data from higher      constructed h<->m
				 // layers

struct en50221_message {
	struct en50221_message *next;
	uint32_t length;
	uint8_t data[0];
};

struct en50221_connection {
	uint32_t state;		// the current state: idle/in_delete/in_create/active
	struct timeval tx_time;	// time last request was sent from host->module, or 0 if ok
	struct timeval last_poll_time;	// time of last poll transmission
	uint8_t *chain_buffer;	// used to save parts of chained packets
	uint32_t buffer_length;

	struct en50221_message *send_queue;
	struct en50221_message *send_queue_tail;
};

struct en50221_slot {
	int ca_hndl;
	uint8_t slot;		// CAM slot
	struct en50221_connection *connections;

	pthread_mutex_t slot_lock;

	uint32_t response_timeout;
	uint32_t poll_delay;
};

struct en50221_transport_layer {
	uint8_t max_slots;
	uint8_t max_connections_per_slot;
	struct en50221_slot *slots;
	struct pollfd *slot_pollfds;
	int slots_changed;

	pthread_mutex_t global_lock;
	pthread_mutex_t setcallback_lock;

	int error;
	int error_slot;

	en50221_tl_callback callback;
	void *callback_arg;
};

static int en50221_tl_process_data(struct en50221_transport_layer *tl,
				   uint8_t slot_id, uint8_t * data,
				   uint32_t data_length);
static int en50221_tl_poll_tc(struct en50221_transport_layer *tl,
			      uint8_t slot_id, uint8_t connection_id);
static int en50221_tl_alloc_new_tc(struct en50221_transport_layer *tl,
				   uint8_t slot_id);
static void queue_message(struct en50221_transport_layer *tl,
			  uint8_t slot_id, uint8_t connection_id,
			  struct en50221_message *msg);
static int en50221_tl_handle_create_tc_reply(struct en50221_transport_layer
					     *tl, uint8_t slot_id,
					     uint8_t connection_id);
static int en50221_tl_handle_delete_tc(struct en50221_transport_layer *tl,
				       uint8_t slot_id,
				       uint8_t connection_id);
static int en50221_tl_handle_delete_tc_reply(struct en50221_transport_layer
					     *tl, uint8_t slot_id,
					     uint8_t connection_id);
static int en50221_tl_handle_request_tc(struct en50221_transport_layer *tl,
					uint8_t slot_id,
					uint8_t connection_id);
static int en50221_tl_handle_data_more(struct en50221_transport_layer *tl,
				       uint8_t slot_id,
				       uint8_t connection_id,
				       uint8_t * data,
				       uint32_t data_length);
static int en50221_tl_handle_data_last(struct en50221_transport_layer *tl,
				       uint8_t slot_id,
				       uint8_t connection_id,
				       uint8_t * data,
				       uint32_t data_length);
static int en50221_tl_handle_sb(struct en50221_transport_layer *tl,
				uint8_t slot_id, uint8_t connection_id,
				uint8_t * data, uint32_t data_length);


struct en50221_transport_layer *en50221_tl_create(uint8_t max_slots,
						  uint8_t
						  max_connections_per_slot)
{
	struct en50221_transport_layer *tl = NULL;
	int i;
	int j;

	// setup structure
	tl = (struct en50221_transport_layer *)
		malloc(sizeof(struct en50221_transport_layer));
	if (tl == NULL)
		goto error_exit;
	tl->max_slots = max_slots;
	tl->max_connections_per_slot = max_connections_per_slot;
	tl->slots = NULL;
	tl->slot_pollfds = NULL;
	tl->slots_changed = 1;
	tl->callback = NULL;
	tl->callback_arg = NULL;
	tl->error_slot = 0;
	tl->error = 0;
	pthread_mutex_init(&tl->global_lock, NULL);
	pthread_mutex_init(&tl->setcallback_lock, NULL);

	// create the slots
	tl->slots = malloc(sizeof(struct en50221_slot) * max_slots);
	if (tl->slots == NULL)
		goto error_exit;

	// set them up
	for (i = 0; i < max_slots; i++) {
		tl->slots[i].ca_hndl = -1;

		// create the connections for this slot
		tl->slots[i].connections =
		    malloc(sizeof(struct en50221_connection) * max_connections_per_slot);
		if (tl->slots[i].connections == NULL)
			goto error_exit;

		// create a mutex for the slot
		pthread_mutex_init(&tl->slots[i].slot_lock, NULL);

		// set them up
		for (j = 0; j < max_connections_per_slot; j++) {
			tl->slots[i].connections[j].state = T_STATE_IDLE;
			tl->slots[i].connections[j].tx_time.tv_sec = 0;
			tl->slots[i].connections[j].last_poll_time.tv_sec = 0;
			tl->slots[i].connections[j].last_poll_time.tv_usec = 0;
			tl->slots[i].connections[j].chain_buffer = NULL;
			tl->slots[i].connections[j].buffer_length = 0;
			tl->slots[i].connections[j].send_queue = NULL;
			tl->slots[i].connections[j].send_queue_tail = NULL;
		}
	}

	// create the pollfds
	tl->slot_pollfds = malloc(sizeof(struct pollfd) * max_slots);
	if (tl->slot_pollfds == NULL) {
		goto error_exit;
	}
	memset(tl->slot_pollfds, 0, sizeof(struct pollfd) * max_slots);

	return tl;

      error_exit:
	en50221_tl_destroy(tl);
	return NULL;
}

// Destroy an instance of the transport layer
void en50221_tl_destroy(struct en50221_transport_layer *tl)
{
	int i, j;

	if (tl) {
		if (tl->slots) {
			for (i = 0; i < tl->max_slots; i++) {
				if (tl->slots[i].connections) {
					for (j = 0; j < tl->max_connections_per_slot; j++) {
						if (tl->slots[i].connections[j].chain_buffer) {
							free(tl->slots[i].connections[j].chain_buffer);
						}

						struct en50221_message *cur_msg =
							tl->slots[i].connections[j].send_queue;
						while (cur_msg) {
							struct en50221_message *next_msg = cur_msg->next;
							free(cur_msg);
							cur_msg = next_msg;
						}
						tl->slots[i].connections[j].send_queue = NULL;
						tl->slots[i].connections[j].send_queue_tail = NULL;
					}
					free(tl->slots[i].connections);
					pthread_mutex_destroy(&tl->slots[i].slot_lock);
				}
			}
			free(tl->slots);
		}
		if (tl->slot_pollfds) {
			free(tl->slot_pollfds);
		}
		pthread_mutex_destroy(&tl->setcallback_lock);
		pthread_mutex_destroy(&tl->global_lock);
		free(tl);
	}
}

// this can be called from the user-space app to
// register new slots that we should work with
int en50221_tl_register_slot(struct en50221_transport_layer *tl,
			     int ca_hndl, uint8_t slot,
			     uint32_t response_timeout,
			     uint32_t poll_delay)
{
	// lock
	pthread_mutex_lock(&tl->global_lock);

	// we browse through the array of slots
	// to look for the first unused one
	int i;
	int16_t slot_id = -1;
	for (i = 0; i < tl->max_slots; i++) {
		if (tl->slots[i].ca_hndl == -1) {
			slot_id = i;
			break;
		}
	}
	if (slot_id == -1) {
		tl->error = EN50221ERR_OUTOFSLOTS;
		pthread_mutex_unlock(&tl->global_lock);
		return -1;
	}
	// set up the slot struct
	pthread_mutex_lock(&tl->slots[slot_id].slot_lock);
	tl->slots[slot_id].ca_hndl = ca_hndl;
	tl->slots[slot_id].slot = slot;
	tl->slots[slot_id].response_timeout = response_timeout;
	tl->slots[slot_id].poll_delay = poll_delay;
	pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);

	tl->slots_changed = 1;
	pthread_mutex_unlock(&tl->global_lock);
	return slot_id;
}

void en50221_tl_destroy_slot(struct en50221_transport_layer *tl,
			     uint8_t slot_id)
{
	int i;

	if (slot_id >= tl->max_slots)
		return;

	// lock
	pthread_mutex_lock(&tl->global_lock);

	// clear the slot
	pthread_mutex_lock(&tl->slots[slot_id].slot_lock);
	tl->slots[slot_id].ca_hndl = -1;
	for (i = 0; i < tl->max_connections_per_slot; i++) {
		tl->slots[slot_id].connections[i].state = T_STATE_IDLE;
		tl->slots[slot_id].connections[i].tx_time.tv_sec = 0;
		tl->slots[slot_id].connections[i].last_poll_time.tv_sec = 0;
		tl->slots[slot_id].connections[i].last_poll_time.tv_usec = 0;
		if (tl->slots[slot_id].connections[i].chain_buffer) {
			free(tl->slots[slot_id].connections[i].
			     chain_buffer);
		}
		tl->slots[slot_id].connections[i].chain_buffer = NULL;
		tl->slots[slot_id].connections[i].buffer_length = 0;

		struct en50221_message *cur_msg =
		    tl->slots[slot_id].connections[i].send_queue;
		while (cur_msg) {
			struct en50221_message *next_msg = cur_msg->next;
			free(cur_msg);
			cur_msg = next_msg;
		}
		tl->slots[slot_id].connections[i].send_queue = NULL;
		tl->slots[slot_id].connections[i].send_queue_tail = NULL;
	}
	pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);

	// tell upper layers
	pthread_mutex_lock(&tl->setcallback_lock);
	en50221_tl_callback cb = tl->callback;
	void *cb_arg = tl->callback_arg;
	pthread_mutex_unlock(&tl->setcallback_lock);
	if (cb)
		cb(cb_arg, T_CALLBACK_REASON_SLOTCLOSE, NULL, 0, slot_id, 0);

	tl->slots_changed = 1;
	pthread_mutex_unlock(&tl->global_lock);
}

int en50221_tl_poll(struct en50221_transport_layer *tl)
{
	uint8_t data[4096];
	int slot_id;
	int j;

	// make up pollfds if the slots have changed
	pthread_mutex_lock(&tl->global_lock);
	if (tl->slots_changed) {
		for (slot_id = 0; slot_id < tl->max_slots; slot_id++) {
			if (tl->slots[slot_id].ca_hndl != -1) {
				tl->slot_pollfds[slot_id].fd = tl->slots[slot_id].ca_hndl;
				tl->slot_pollfds[slot_id].events = POLLIN | POLLPRI | POLLERR;
				tl->slot_pollfds[slot_id].revents = 0;
			} else {
				tl->slot_pollfds[slot_id].fd = 0;
				tl->slot_pollfds[slot_id].events = 0;
				tl->slot_pollfds[slot_id].revents = 0;
			}
		}
		tl->slots_changed = 0;
	}
	pthread_mutex_unlock(&tl->global_lock);

	// anything happened?
	if (poll(tl->slot_pollfds, tl->max_slots, 10) < 0) {
		tl->error_slot = -1;
		tl->error = EN50221ERR_CAREAD;
		return -1;
	}
	// go through all slots (even though poll may not have reported any events
	for (slot_id = 0; slot_id < tl->max_slots; slot_id++) {

		// check if this slot is still used and get its handle
		pthread_mutex_lock(&tl->slots[slot_id].slot_lock);
		if (tl->slots[slot_id].ca_hndl == -1) {
			pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
			continue;
		}
		int ca_hndl = tl->slots[slot_id].ca_hndl;

		if (tl->slot_pollfds[slot_id].revents & (POLLPRI | POLLIN)) {
			// read data
			uint8_t r_slot_id;
			uint8_t connection_id;
			int readcnt = dvbca_link_read(ca_hndl, &r_slot_id,
						      &connection_id,
						      data, sizeof(data));
			if (readcnt < 0) {
				tl->error_slot = slot_id;
				tl->error = EN50221ERR_CAREAD;
				pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
				return -1;
			}
			// process it if we got some
			if (readcnt > 0) {
				if (tl->slots[slot_id].slot != r_slot_id) {
					// this message is for an other CAM of the same CA
					int new_slot_id;
					for (new_slot_id = 0; new_slot_id < tl->max_slots; new_slot_id++) {
						if ((tl->slots[new_slot_id].ca_hndl == ca_hndl) &&
						    (tl->slots[new_slot_id].slot == r_slot_id))
							break;
					}
					if (new_slot_id != tl->max_slots) {
						// we found the requested CAM
						pthread_mutex_lock(&tl->slots[new_slot_id].slot_lock);
						if (en50221_tl_process_data(tl, new_slot_id, data, readcnt)) {
							pthread_mutex_unlock(&tl->slots[new_slot_id].slot_lock);
							pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
							return -1;
						}
						pthread_mutex_unlock(&tl->slots[new_slot_id].slot_lock);
					} else {
						tl->error = EN50221ERR_BADSLOTID;
						pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
						return -1;
					}
				} else
				    if (en50221_tl_process_data(tl, slot_id, data, readcnt)) {
					pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
					return -1;
				}
			}
		} else if (tl->slot_pollfds[slot_id].revents & POLLERR) {
			// an error was reported
			tl->error_slot = slot_id;
			tl->error = EN50221ERR_CAREAD;
			pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
			return -1;
		}
		// poll the connections on this slot + check for timeouts
		for (j = 0; j < tl->max_connections_per_slot; j++) {
			// ignore connection if idle
			if (tl->slots[slot_id].connections[j].state == T_STATE_IDLE) {
				continue;
			}
			// send queued data
			if (tl->slots[slot_id].connections[j].state &
				(T_STATE_IN_CREATION | T_STATE_ACTIVE | T_STATE_ACTIVE_DELETEQUEUED)) {
				// send data if there is some to go and we're not waiting for a response already
				if (tl->slots[slot_id].connections[j].send_queue &&
				    (tl->slots[slot_id].connections[j].tx_time.tv_sec == 0)) {

					// get the message
					struct en50221_message *msg =
						tl->slots[slot_id].connections[j].send_queue;
					if (msg->next != NULL) {
						tl->slots[slot_id].connections[j].send_queue = msg->next;
					} else {
						tl->slots[slot_id].connections[j].send_queue = NULL;
						tl->slots[slot_id].connections[j].send_queue_tail = NULL;
					}

					// send the message
					if (dvbca_link_write(tl->slots[slot_id].ca_hndl,
					    		     tl->slots[slot_id].slot,
							     j,
							     msg->data, msg->length) < 0) {
						free(msg);
						pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
						tl->error_slot = slot_id;
						tl->error = EN50221ERR_CAWRITE;
						print(LOG_LEVEL, ERROR, 1, "CAWrite failed");
						return -1;
					}
					gettimeofday(&tl->slots[slot_id].connections[j].tx_time, 0);

					// fixup connection state for T_DELETE_T_C
					if (msg->length && (msg->data[0] == T_DELETE_T_C)) {
						tl->slots[slot_id].connections[j].state = T_STATE_IN_DELETION;
						if (tl->slots[slot_id].connections[j].chain_buffer) {
							free(tl->slots[slot_id].connections[j].chain_buffer);
						}
						tl->slots[slot_id].connections[j].chain_buffer = NULL;
						tl->slots[slot_id].connections[j].buffer_length = 0;
					}

					free(msg);
				}
			}
			// poll it if we're not expecting a reponse and the poll time has elapsed
			if (tl->slots[slot_id].connections[j].state & T_STATE_ACTIVE) {
				if ((tl->slots[slot_id].connections[j].tx_time.tv_sec == 0) &&
				    (time_after(tl->slots[slot_id].connections[j].last_poll_time,
				     		tl->slots[slot_id].poll_delay))) {

					gettimeofday(&tl->slots[slot_id].connections[j].last_poll_time, 0);
					if (en50221_tl_poll_tc(tl, slot_id, j)) {
						pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
						return -1;
					}
				}
			}

			// check for timeouts - in any state
			if (tl->slots[slot_id].connections[j].tx_time.tv_sec &&
			    (time_after(tl->slots[slot_id].connections[j].tx_time,
			     		tl->slots[slot_id].response_timeout))) {

				if (tl->slots[slot_id].connections[j].state &
				    (T_STATE_IN_CREATION |T_STATE_IN_DELETION)) {
					tl->slots[slot_id].connections[j].state = T_STATE_IDLE;
				} else if (tl->slots[slot_id].connections[j].state &
					   (T_STATE_ACTIVE | T_STATE_ACTIVE_DELETEQUEUED)) {
					tl->error_slot = slot_id;
					tl->error = EN50221ERR_TIMEOUT;
					pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
					return -1;
				}
			}
		}
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
	}

	return 0;
}

void en50221_tl_register_callback(struct en50221_transport_layer *tl,
				  en50221_tl_callback callback, void *arg)
{
	pthread_mutex_lock(&tl->setcallback_lock);
	tl->callback = callback;
	tl->callback_arg = arg;
	pthread_mutex_unlock(&tl->setcallback_lock);
}

int en50221_tl_get_error_slot(struct en50221_transport_layer *tl)
{
	return tl->error_slot;
}

int en50221_tl_get_error(struct en50221_transport_layer *tl)
{
	return tl->error;
}

int en50221_tl_send_data(struct en50221_transport_layer *tl,
			 uint8_t slot_id, uint8_t connection_id,
			 uint8_t * data, uint32_t data_size)
{
#ifdef DEBUG_TXDATA
	printf("[[[[[[[[[[[[[[[[[[[[\n");
	uint32_t ii = 0;
	for (ii = 0; ii < data_size; ii++) {
		printf("%02x: %02x\n", ii, data[ii]);
	}
	printf("]]]]]]]]]]]]]]]]]]]]\n");
#endif

	if (slot_id >= tl->max_slots) {
		tl->error = EN50221ERR_BADSLOTID;
		return -1;
	}

	pthread_mutex_lock(&tl->slots[slot_id].slot_lock);
	if (tl->slots[slot_id].ca_hndl == -1) {
		tl->error = EN50221ERR_BADSLOTID;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	if (connection_id >= tl->max_connections_per_slot) {
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_BADCONNECTIONID;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	if (tl->slots[slot_id].connections[connection_id].state != T_STATE_ACTIVE) {
		tl->error = EN50221ERR_BADCONNECTIONID;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	// allocate msg structure
	struct en50221_message *msg =
	    malloc(sizeof(struct en50221_message) + data_size + 10);
	if (msg == NULL) {
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_OUTOFMEMORY;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	// make up data to send
	int length_field_len;
	msg->data[0] = T_DATA_LAST;
	if ((length_field_len = asn_1_encode(data_size + 1, msg->data + 1, 3)) < 0) {
		free(msg);
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_ASNENCODE;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	msg->data[1 + length_field_len] = connection_id;
	memcpy(msg->data + 1 + length_field_len + 1, data, data_size);
	msg->length = 1 + length_field_len + 1 + data_size;

	// queue it for transmission
	queue_message(tl, slot_id, connection_id, msg);

	pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
	return 0;
}

int en50221_tl_send_datav(struct en50221_transport_layer *tl,
			  uint8_t slot_id, uint8_t connection_id,
			  struct iovec *vector, int iov_count)
{
#ifdef DEBUG_TXDATA
	printf("[[[[[[[[[[[[[[[[[[[[\n");
	uint32_t ii = 0;
	uint32_t iipos = 0;
	for (ii = 0; ii < (uint32_t) iov_count; ii++) {
		uint32_t jj;
		for (jj = 0; jj < vector[ii].iov_len; jj++) {
			printf("%02x: %02x\n", jj + iipos,
			       *((uint8_t *) (vector[ii].iov_base) + jj));
		}
		iipos += vector[ii].iov_len;
	}
	printf("]]]]]]]]]]]]]]]]]]]]\n");
#endif

	if (slot_id >= tl->max_slots) {
		tl->error = EN50221ERR_BADSLOTID;
		return -1;
	}

	pthread_mutex_lock(&tl->slots[slot_id].slot_lock);
	if (tl->slots[slot_id].ca_hndl == -1) {
		tl->error = EN50221ERR_BADSLOTID;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	if (connection_id >= tl->max_connections_per_slot) {
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_BADCONNECTIONID;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	if (tl->slots[slot_id].connections[connection_id].state != T_STATE_ACTIVE) {
		tl->error = EN50221ERR_BADCONNECTIONID;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	// calculate the total length of the data to send
	uint32_t data_size = 0;
	int i;
	for (i = 0; i < iov_count; i++) {
		data_size += vector[i].iov_len;
	}

	// allocate msg structure
	struct en50221_message *msg =
	    malloc(sizeof(struct en50221_message) + data_size + 10);
	if (msg == NULL) {
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_OUTOFMEMORY;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	// make up data to send
	int length_field_len;
	msg->data[0] = T_DATA_LAST;
	if ((length_field_len = asn_1_encode(data_size + 1, msg->data + 1, 3)) < 0) {
		free(msg);
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_ASNENCODE;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	msg->data[1 + length_field_len] = connection_id;
	msg->length = 1 + length_field_len + 1 + data_size;
	msg->next = NULL;

	// merge the iovecs
	uint32_t pos = 1 + length_field_len + 1;
	for (i = 0; i < iov_count; i++) {
		memcpy(msg->data + pos, vector[i].iov_base,
		       vector[i].iov_len);
		pos += vector[i].iov_len;
	}

	// queue it for transmission
	queue_message(tl, slot_id, connection_id, msg);

	pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
	return 0;
}

int en50221_tl_new_tc(struct en50221_transport_layer *tl, uint8_t slot_id)
{
	// check
	if (slot_id >= tl->max_slots) {
		tl->error = EN50221ERR_BADSLOTID;
		return -1;
	}

	pthread_mutex_lock(&tl->slots[slot_id].slot_lock);
	if (tl->slots[slot_id].ca_hndl == -1) {
		tl->error = EN50221ERR_BADSLOTID;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	// allocate a new connection if possible
	int conid = en50221_tl_alloc_new_tc(tl, slot_id);
	if (conid == -1) {
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_OUTOFCONNECTIONS;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	// allocate msg structure
	struct en50221_message *msg =
	    malloc(sizeof(struct en50221_message) + 3);
	if (msg == NULL) {
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_OUTOFMEMORY;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	// make up the data to send
	msg->data[0] = T_CREATE_T_C;
	msg->data[1] = 1;
	msg->data[2] = conid;
	msg->length = 3;
	msg->next = NULL;

	// queue it for transmission
	queue_message(tl, slot_id, conid, msg);

	// done
	pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
	return conid;
}

int en50221_tl_del_tc(struct en50221_transport_layer *tl, uint8_t slot_id,
		      uint8_t connection_id)
{
	// check
	if (slot_id >= tl->max_slots) {
		tl->error = EN50221ERR_BADSLOTID;
		return -1;
	}

	pthread_mutex_lock(&tl->slots[slot_id].slot_lock);
	if (tl->slots[slot_id].ca_hndl == -1) {
		tl->error = EN50221ERR_BADSLOTID;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	if (connection_id >= tl->max_connections_per_slot) {
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_BADCONNECTIONID;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	if (!(tl->slots[slot_id].connections[connection_id].state &
	      (T_STATE_ACTIVE | T_STATE_IN_DELETION))) {
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_BADSTATE;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	// allocate msg structure
	struct en50221_message *msg =
	    malloc(sizeof(struct en50221_message) + 3);
	if (msg == NULL) {
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_OUTOFMEMORY;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	// make up the data to send
	msg->data[0] = T_DELETE_T_C;
	msg->data[1] = 1;
	msg->data[2] = connection_id;
	msg->length = 3;
	msg->next = NULL;

	// queue it for transmission
	queue_message(tl, slot_id, connection_id, msg);
	tl->slots[slot_id].connections[connection_id].state =
	    T_STATE_ACTIVE_DELETEQUEUED;

	pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
	return 0;
}

int en50221_tl_get_connection_state(struct en50221_transport_layer *tl,
				    uint8_t slot_id, uint8_t connection_id)
{
	if (slot_id >= tl->max_slots) {
		tl->error = EN50221ERR_BADSLOTID;
		return -1;
	}

	pthread_mutex_lock(&tl->slots[slot_id].slot_lock);
	if (tl->slots[slot_id].ca_hndl == -1) {
		tl->error = EN50221ERR_BADSLOTID;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	if (connection_id >= tl->max_connections_per_slot) {
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_BADCONNECTIONID;
		pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);
		return -1;
	}
	int state = tl->slots[slot_id].connections[connection_id].state;
	pthread_mutex_unlock(&tl->slots[slot_id].slot_lock);

	return state;
}




// ask the module for new data
static int en50221_tl_poll_tc(struct en50221_transport_layer *tl,
			      uint8_t slot_id, uint8_t connection_id)
{
	gettimeofday(&tl->slots[slot_id].connections[connection_id].
		     tx_time, 0);

	// send command
	uint8_t hdr[3];
	hdr[0] = T_DATA_LAST;
	hdr[1] = 1;
	hdr[2] = connection_id;
	if (dvbca_link_write(tl->slots[slot_id].ca_hndl,
	    		     tl->slots[slot_id].slot,
			     connection_id, hdr, 3) < 0) {
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_CAWRITE;
		return -1;
	}
	return 0;
}

// handle incoming data
static int en50221_tl_process_data(struct en50221_transport_layer *tl,
				   uint8_t slot_id, uint8_t * data,
				   uint32_t data_length)
{
	int result;

#ifdef DEBUG_RXDATA
	printf("-------------------\n");
	uint32_t ii = 0;
	for (ii = 0; ii < data_length; ii++) {
		printf("%02x: %02x\n", ii, data[ii]);
	}
	printf("+++++++++++++++++++\n");
#endif

	// process the received data
	while (data_length) {
		// parse the header
		uint8_t tpdu_tag = data[0];
		uint16_t asn_data_length;
		int length_field_len;
		if ((length_field_len = asn_1_decode(&asn_data_length, data + 1, data_length - 1)) < 0) {
			print(LOG_LEVEL, ERROR, 1,
			      "Received data with invalid asn from module on slot %02x\n",
			      slot_id);
			tl->error_slot = slot_id;
			tl->error = EN50221ERR_BADCAMDATA;
			return -1;
		}
		if ((asn_data_length < 1) ||
		    (asn_data_length > (data_length - (1 + length_field_len)))) {
			print(LOG_LEVEL, ERROR, 1,
			      "Received data with invalid length from module on slot %02x\n",
			      slot_id);
			tl->error_slot = slot_id;
			tl->error = EN50221ERR_BADCAMDATA;
			return -1;
		}
		uint8_t connection_id = data[1 + length_field_len];
		data += 1 + length_field_len + 1;
		data_length -= (1 + length_field_len + 1);
		asn_data_length--;

		// check the connection_id
		if (connection_id >= tl->max_connections_per_slot) {
			print(LOG_LEVEL, ERROR, 1,
			      "Received bad connection id %02x from module on slot %02x\n",
			      connection_id, slot_id);
			tl->error_slot = slot_id;
			tl->error = EN50221ERR_BADCONNECTIONID;
			return -1;
		}
		// process the TPDUs
		switch (tpdu_tag) {
		case T_C_T_C_REPLY:
			if ((result = en50221_tl_handle_create_tc_reply(tl, slot_id, connection_id)) < 0) {
				return -1;
			}
			break;
		case T_DELETE_T_C:
			if ((result = en50221_tl_handle_delete_tc(tl, slot_id, connection_id)) < 0) {
				return -1;
			}
			break;
		case T_D_T_C_REPLY:
			if ((result = en50221_tl_handle_delete_tc_reply(tl, slot_id, connection_id)) < 0) {
				return -1;
			}
			break;
		case T_REQUEST_T_C:
			if ((result = en50221_tl_handle_request_tc(tl, slot_id, connection_id)) < 0) {
				return -1;
			}
			break;
		case T_DATA_MORE:
			if ((result = en50221_tl_handle_data_more(tl, slot_id,
			     					  connection_id,
								  data,
								  asn_data_length)) < 0) {
				return -1;
			}
			break;
		case T_DATA_LAST:
			if ((result = en50221_tl_handle_data_last(tl, slot_id,
			     					  connection_id,
								  data,
								  asn_data_length)) < 0) {
				return -1;
			}
			break;
		case T_SB:
			if ((result = en50221_tl_handle_sb(tl, slot_id,
			     				   connection_id,
							   data,
							   asn_data_length)) < 0) {
				return -1;
			}
			break;
		default:
			print(LOG_LEVEL, ERROR, 1,
			      "Recieved unexpected TPDU tag %02x from module on slot %02x\n",
			      tpdu_tag, slot_id);
			tl->error_slot = slot_id;
			tl->error = EN50221ERR_BADCAMDATA;
			return -1;
		}

		// skip over the consumed data
		data += asn_data_length;
		data_length -= asn_data_length;
	}

	return 0;
}

static int en50221_tl_handle_create_tc_reply(struct en50221_transport_layer
					     *tl, uint8_t slot_id,
					     uint8_t connection_id)
{
	// set this connection to state active
	if (tl->slots[slot_id].connections[connection_id].state == T_STATE_IN_CREATION) {
		tl->slots[slot_id].connections[connection_id].state = T_STATE_ACTIVE;
		tl->slots[slot_id].connections[connection_id].tx_time.tv_sec = 0;

		// tell upper layers
		pthread_mutex_lock(&tl->setcallback_lock);
		en50221_tl_callback cb = tl->callback;
		void *cb_arg = tl->callback_arg;
		pthread_mutex_unlock(&tl->setcallback_lock);
		if (cb)
			cb(cb_arg, T_CALLBACK_REASON_CONNECTIONOPEN, NULL, 0, slot_id, connection_id);
	} else {
		print(LOG_LEVEL, ERROR, 1,
		      "Received T_C_T_C_REPLY for connection not in "
		      "T_STATE_IN_CREATION from module on slot %02x\n",
		      slot_id);
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_BADCAMDATA;
		return -1;
	}

	return 0;
}

static int en50221_tl_handle_delete_tc(struct en50221_transport_layer *tl,
				       uint8_t slot_id,
				       uint8_t connection_id)
{
	// immediately delete this connection and send D_T_C_REPLY
	if (tl->slots[slot_id].connections[connection_id].state &
	    (T_STATE_ACTIVE | T_STATE_IN_DELETION)) {
		// clear down the slot
		tl->slots[slot_id].connections[connection_id].state = T_STATE_IDLE;
		if (tl->slots[slot_id].connections[connection_id].chain_buffer) {
			free(tl->slots[slot_id].connections[connection_id].chain_buffer);
		}
		tl->slots[slot_id].connections[connection_id].chain_buffer = NULL;
		tl->slots[slot_id].connections[connection_id].buffer_length = 0;

		// send the reply
		uint8_t hdr[3];
		hdr[0] = T_D_T_C_REPLY;
		hdr[1] = 1;
		hdr[2] = connection_id;
		if (dvbca_link_write(tl->slots[slot_id].ca_hndl,
		    		     tl->slots[slot_id].slot,
				     connection_id, hdr, 3) < 0) {
			tl->error_slot = slot_id;
			tl->error = EN50221ERR_CAWRITE;
			return -1;
		}
		// tell upper layers
		pthread_mutex_lock(&tl->setcallback_lock);
		en50221_tl_callback cb = tl->callback;
		void *cb_arg = tl->callback_arg;
		pthread_mutex_unlock(&tl->setcallback_lock);
		if (cb)
			cb(cb_arg, T_CALLBACK_REASON_CONNECTIONCLOSE, NULL, 0, slot_id, connection_id);
	} else {
		print(LOG_LEVEL, ERROR, 1,
		      "Received T_DELETE_T_C for inactive connection from module on slot %02x\n",
		      slot_id);
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_BADCAMDATA;
		return -1;
	}

	return 0;
}

static int en50221_tl_handle_delete_tc_reply(struct en50221_transport_layer
					     *tl, uint8_t slot_id,
					     uint8_t connection_id)
{
	// delete this connection, should be in T_STATE_IN_DELETION already
	if (tl->slots[slot_id].connections[connection_id].state == T_STATE_IN_DELETION) {
		tl->slots[slot_id].connections[connection_id].state = T_STATE_IDLE;
	} else {
		print(LOG_LEVEL, ERROR, 1,
		      "Received T_D_T_C_REPLY received for connection not in "
		      "T_STATE_IN_DELETION from module on slot %02x\n",
		      slot_id);
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_BADCAMDATA;
		return -1;
	}

	return 0;
}

static int en50221_tl_handle_request_tc(struct en50221_transport_layer *tl,
					uint8_t slot_id,
					uint8_t connection_id)
{
	// allocate a new connection if possible
	int conid = en50221_tl_alloc_new_tc(tl, slot_id);
	int ca_hndl = tl->slots[slot_id].ca_hndl;
	if (conid == -1) {
		print(LOG_LEVEL, ERROR, 1,
		      "Too many connections requested by module on slot %02x\n",
		      slot_id);

		// send the error
		uint8_t hdr[4];
		hdr[0] = T_T_C_ERROR;
		hdr[1] = 2;
		hdr[2] = connection_id;
		hdr[3] = 1;
		if (dvbca_link_write(ca_hndl, tl->slots[slot_id].slot, connection_id, hdr, 4) < 0) {
			tl->error_slot = slot_id;
			tl->error = EN50221ERR_CAWRITE;
			return -1;
		}
		tl->slots[slot_id].connections[connection_id].tx_time.
		    tv_sec = 0;
	} else {
		// send the NEW_T_C on the connection we received it on
		uint8_t hdr[4];
		hdr[0] = T_NEW_T_C;
		hdr[1] = 2;
		hdr[2] = connection_id;
		hdr[3] = conid;
		if (dvbca_link_write(ca_hndl, tl->slots[slot_id].slot, connection_id, hdr, 4) < 0) {
			tl->slots[slot_id].connections[conid].state = T_STATE_IDLE;
			tl->error_slot = slot_id;
			tl->error = EN50221ERR_CAWRITE;
			return -1;
		}
		tl->slots[slot_id].connections[connection_id].tx_time.tv_sec = 0;

		// send the CREATE_T_C on the new connnection
		hdr[0] = T_CREATE_T_C;
		hdr[1] = 1;
		hdr[2] = conid;
		if (dvbca_link_write(ca_hndl, tl->slots[slot_id].slot, conid, hdr, 3) < 0) {
			tl->slots[slot_id].connections[conid].state = T_STATE_IDLE;
			tl->error_slot = slot_id;
			tl->error = EN50221ERR_CAWRITE;
			return -1;
		}
		gettimeofday(&tl->slots[slot_id].connections[conid].tx_time, 0);

		// tell upper layers
		pthread_mutex_lock(&tl->setcallback_lock);
		en50221_tl_callback cb = tl->callback;
		void *cb_arg = tl->callback_arg;
		pthread_mutex_unlock(&tl->setcallback_lock);
		if (cb)
			cb(cb_arg, T_CALLBACK_REASON_CAMCONNECTIONOPEN, NULL, 0, slot_id, conid);
	}

	return 0;
}

static int en50221_tl_handle_data_more(struct en50221_transport_layer *tl,
				       uint8_t slot_id,
				       uint8_t connection_id,
				       uint8_t * data,
				       uint32_t data_length)
{
	// connection in correct state?
	if (tl->slots[slot_id].connections[connection_id].state != T_STATE_ACTIVE) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received T_DATA_MORE for connection not in "
		      "T_STATE_ACTIVE from module on slot %02x\n",
		      slot_id);
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_BADCAMDATA;
		return -1;
	}
	// a chained data packet is coming in, save
	// it to the buffer and wait for more
	tl->slots[slot_id].connections[connection_id].tx_time.tv_sec = 0;
	int new_data_length =
	    tl->slots[slot_id].connections[connection_id].buffer_length + data_length;
	uint8_t *new_data_buffer =
	    realloc(tl->slots[slot_id].connections[connection_id].chain_buffer, new_data_length);
	if (new_data_buffer == NULL) {
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_OUTOFMEMORY;
		return -1;
	}
	tl->slots[slot_id].connections[connection_id].chain_buffer = new_data_buffer;

	memcpy(tl->slots[slot_id].connections[connection_id].chain_buffer +
	       tl->slots[slot_id].connections[connection_id].buffer_length,
	       data, data_length);
	tl->slots[slot_id].connections[connection_id].buffer_length = new_data_length;

	return 0;
}

static int en50221_tl_handle_data_last(struct en50221_transport_layer *tl,
				       uint8_t slot_id,
				       uint8_t connection_id,
				       uint8_t * data,
				       uint32_t data_length)
{
	// connection in correct state?
	if (tl->slots[slot_id].connections[connection_id].state != T_STATE_ACTIVE) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received T_DATA_LAST received for connection not in "
		      "T_STATE_ACTIVE from module on slot %02x\n",
		      slot_id);
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_BADCAMDATA;
		return -1;
	}
	// last package of a chain or single package comes in
	tl->slots[slot_id].connections[connection_id].tx_time.tv_sec = 0;
	if (tl->slots[slot_id].connections[connection_id].chain_buffer == NULL) {
		// single package => dispatch immediately
		pthread_mutex_lock(&tl->setcallback_lock);
		en50221_tl_callback cb = tl->callback;
		void *cb_arg = tl->callback_arg;
		pthread_mutex_unlock(&tl->setcallback_lock);

		if (cb && data_length) {
			pthread_mutex_unlock(&tl->slots[slot_id].
					     slot_lock);
			cb(cb_arg, T_CALLBACK_REASON_DATA, data, data_length, slot_id, connection_id);
			pthread_mutex_lock(&tl->slots[slot_id].slot_lock);
		}
	} else {
		int new_data_length =
		    tl->slots[slot_id].connections[connection_id].buffer_length + data_length;
		uint8_t *new_data_buffer =
		    realloc(tl->slots[slot_id].connections[connection_id].chain_buffer, new_data_length);
		if (new_data_buffer == NULL) {
			tl->error_slot = slot_id;
			tl->error = EN50221ERR_OUTOFMEMORY;
			return -1;
		}

		memcpy(new_data_buffer +
		       tl->slots[slot_id].connections[connection_id].
		       buffer_length, data, data_length);

		// clean the buffer position
		tl->slots[slot_id].connections[connection_id].chain_buffer = NULL;
		tl->slots[slot_id].connections[connection_id].buffer_length = 0;

		// tell the upper layers
		pthread_mutex_lock(&tl->setcallback_lock);
		en50221_tl_callback cb = tl->callback;
		void *cb_arg = tl->callback_arg;
		pthread_mutex_unlock(&tl->setcallback_lock);
		if (cb && data_length) {
			pthread_mutex_unlock(&tl->slots[slot_id].
					     slot_lock);
			cb(cb_arg, T_CALLBACK_REASON_DATA, new_data_buffer,
			   new_data_length, slot_id, connection_id);
			pthread_mutex_lock(&tl->slots[slot_id].slot_lock);
		}

		free(new_data_buffer);
	}

	return 0;
}

static int en50221_tl_handle_sb(struct en50221_transport_layer *tl,
				uint8_t slot_id, uint8_t connection_id,
				uint8_t * data, uint32_t data_length)
{
	// is the connection id ok?
	if (tl->slots[slot_id].connections[connection_id].state != T_STATE_ACTIVE) {
		print(LOG_LEVEL, ERROR, 1,
		      "Received T_SB for connection not in T_STATE_ACTIVE from module on slot %02x\n",
		      slot_id);
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_BADCAMDATA;
		return -1;
	}
	// did we get enough data in the T_SB?
	if (data_length != 1) {
		print(LOG_LEVEL, ERROR, 1,
		      "Recieved T_SB with invalid length from module on slot %02x\n",
		      slot_id);
		tl->error_slot = slot_id;
		tl->error = EN50221ERR_BADCAMDATA;
		return -1;
	}
	// tell it to send the data if it says there is some
	if (data[0] & 0x80) {
		int ca_hndl = tl->slots[slot_id].ca_hndl;

		// send the RCV
		uint8_t hdr[3];
		hdr[0] = T_RCV;
		hdr[1] = 1;
		hdr[2] = connection_id;
		if (dvbca_link_write(ca_hndl, tl->slots[slot_id].slot, connection_id, hdr, 3) < 0) {
			tl->error_slot = slot_id;
			tl->error = EN50221ERR_CAWRITE;
			return -1;
		}
		gettimeofday(&tl->slots[slot_id].connections[connection_id].tx_time, 0);

	} else {
		// no data - indicate not waiting for anything now
		tl->slots[slot_id].connections[connection_id].tx_time.tv_sec = 0;
	}

	return 0;
}

static int en50221_tl_alloc_new_tc(struct en50221_transport_layer *tl,
				   uint8_t slot_id)
{
	// we browse through the array of connection
	// types, to look for the first unused one
	int i, conid = -1;
	for (i = 1; i < tl->max_connections_per_slot; i++) {
		if (tl->slots[slot_id].connections[i].state == T_STATE_IDLE) {
			conid = i;
			break;
		}
	}
	if (conid == -1) {
		print(LOG_LEVEL, ERROR, 1,
		      "CREATE_T_C failed: no more connections available\n");
		return -1;
	}
	// set up the connection struct
	tl->slots[slot_id].connections[conid].state = T_STATE_IN_CREATION;
	tl->slots[slot_id].connections[conid].chain_buffer = NULL;
	tl->slots[slot_id].connections[conid].buffer_length = 0;

	return conid;
}

static void queue_message(struct en50221_transport_layer *tl,
			  uint8_t slot_id, uint8_t connection_id,
			  struct en50221_message *msg)
{
	msg->next = NULL;
	if (tl->slots[slot_id].connections[connection_id].send_queue_tail) {
		tl->slots[slot_id].connections[connection_id].send_queue_tail->next = msg;
		tl->slots[slot_id].connections[connection_id].send_queue_tail = msg;
	} else {
		tl->slots[slot_id].connections[connection_id].send_queue = msg;
		tl->slots[slot_id].connections[connection_id].send_queue_tail = msg;
	}
}