aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libdvben50221/en50221_app_mmi.c
blob: 830eaa3e6ac262124dec6bc4738a36f941b3376d (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
generated by cgit v1.2.3 (git 2.25.1) at 2024-12-24 13:34:28 +0000
 


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
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
/*
    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 <string.h>
#include <libdvbmisc/dvbmisc.h>
#include <pthread.h>
#include <libucsi/dvb/types.h>
#include "en50221_app_mmi.h"
#include "en50221_app_tags.h"
#include "asn_1.h"

struct en50221_app_mmi_session {
	uint16_t session_number;

	uint8_t *menu_block_chain;
	uint32_t menu_block_length;

	uint8_t *list_block_chain;
	uint32_t list_block_length;

	uint8_t *subtitlesegment_block_chain;
	uint32_t subtitlesegment_block_length;

	uint8_t *subtitledownload_block_chain;
	uint32_t subtitledownload_block_length;

	struct en50221_app_mmi_session *next;
};

struct en50221_app_mmi {
	struct en50221_app_send_functions *funcs;
	struct en50221_app_mmi_session *sessions;

	en50221_app_mmi_close_callback closecallback;
	void *closecallback_arg;

	en50221_app_mmi_display_control_callback displaycontrolcallback;
	void *displaycontrolcallback_arg;

	en50221_app_mmi_keypad_control_callback keypadcontrolcallback;
	void *keypadcontrolcallback_arg;

	en50221_app_mmi_subtitle_segment_callback subtitlesegmentcallback;
	void *subtitlesegmentcallback_arg;

	en50221_app_mmi_scene_end_mark_callback sceneendmarkcallback;
	void *sceneendmarkcallback_arg;

	en50221_app_mmi_scene_control_callback scenecontrolcallback;
	void *scenecontrolcallback_arg;

	en50221_app_mmi_subtitle_download_callback subtitledownloadcallback;
	void *subtitledownloadcallback_arg;

	en50221_app_mmi_flush_download_callback flushdownloadcallback;
	void *flushdownloadcallback_arg;

	en50221_app_mmi_enq_callback enqcallback;
	void *enqcallback_arg;

	en50221_app_mmi_menu_callback menucallback;
	void *menucallback_arg;

	en50221_app_mmi_list_callback listcallback;
	void *listcallback_arg;

	pthread_mutex_t lock;
};

static int en50221_app_mmi_parse_close(struct en50221_app_mmi *mmi,
				       uint8_t slot_id,
				       uint16_t session_number,
				       uint8_t * data,
				       uint32_t data_length);
static int en50221_app_mmi_parse_display_control(struct en50221_app_mmi
						 *mmi, uint8_t slot_id,
						 uint16_t session_number,
						 uint8_t * data,
						 uint32_t data_length);
static int en50221_app_mmi_parse_keypad_control(struct en50221_app_mmi
						*mmi, uint8_t slot_id,
						uint16_t session_number,
						uint8_t * data,
						uint32_t data_length);
static int en50221_app_mmi_parse_enq(struct en50221_app_mmi *mmi,
				     uint8_t slot_id,
				     uint16_t session_number,
				     uint8_t * data, uint32_t data_length);
static int en50221_app_mmi_parse_list_menu(struct en50221_app_mmi *mmi,
					   uint8_t slot_id,
					   uint16_t session_number,
					   uint32_t tag_id, int more_last,
					   uint8_t * data,
					   uint32_t data_length);
static int en50221_app_mmi_parse_subtitle(struct en50221_app_mmi *mmi,
					  uint8_t slot_id,
					  uint16_t session_number,
					  uint32_t tag_id, int more_last,
					  uint8_t * data,
					  uint32_t data_length);
static int en50221_app_mmi_parse_scene_end_mark(struct en50221_app_mmi *mmi,
						uint8_t slot_id,
						uint16_t session_number,
						uint8_t * data,
						uint32_t data_length);
static int en50221_app_mmi_parse_scene_control(struct en50221_app_mmi *mmi,
					       uint8_t slot_id,
					       uint16_t session_number,
					       uint8_t * data,
					       uint32_t data_length);
static int en50221_app_mmi_parse_subtitle(struct en50221_app_mmi *mmi,
					  uint8_t slot_id,
					  uint16_t session_number,
					  uint32_t tag_id, int more_last,
					  uint8_t * data,
					  uint32_t data_length);
static int en50221_app_mmi_parse_flush_download(struct en50221_app_mmi *mmi,
						uint8_t slot_id,
						uint16_t session_number,
						uint8_t * data,
						uint32_t data_length);
static int en50221_app_mmi_defragment(struct en50221_app_mmi *mmi,
				      uint16_t session_number,
				      uint32_t tag_id, int more_last,
				      uint8_t * indata,
				      uint32_t indata_length,
				      uint8_t ** outdata,
				      uint32_t * outdata_length);
static int en50221_app_mmi_defragment_text(uint8_t * data,
					   uint32_t data_length,
					   uint8_t ** outdata,
					   uint32_t * outdata_length,
					   uint32_t * outconsumed);



struct en50221_app_mmi *en50221_app_mmi_create(struct en50221_app_send_functions *funcs)
{
	struct en50221_app_mmi *mmi = NULL;

	// create structure and set it up
	mmi = malloc(sizeof(struct en50221_app_mmi));
	if (mmi == NULL) {
		return NULL;
	}
	mmi->funcs = funcs;
	mmi->closecallback = NULL;
	mmi->displaycontrolcallback = NULL;
	mmi->keypadcontrolcallback = NULL;
	mmi->subtitlesegmentcallback = NULL;
	mmi->sceneendmarkcallback = NULL;
	mmi->scenecontrolcallback = NULL;
	mmi->subtitledownloadcallback = NULL;
	mmi->flushdownloadcallback = NULL;
	mmi->enqcallback = NULL;
	mmi->menucallback = NULL;
	mmi->listcallback = NULL;
	mmi->sessions = NULL;

	pthread_mutex_init(&mmi->lock, NULL);

	// done
	return mmi;
}

void en50221_app_mmi_destroy(struct en50221_app_mmi *mmi)
{
	struct en50221_app_mmi_session *cur_s = mmi->sessions;
	while (cur_s) {
		struct en50221_app_mmi_session *next = cur_s->next;
		if (cur_s->menu_block_chain)
			free(cur_s->menu_block_chain);
		if (cur_s->list_block_chain)
			free(cur_s->list_block_chain);
		if (cur_s->subtitlesegment_block_chain)
			free(cur_s->subtitlesegment_block_chain);
		if (cur_s->subtitledownload_block_chain)
			free(cur_s->subtitledownload_block_chain);
		free(cur_s);
		cur_s = next;
	}

	pthread_mutex_destroy(&mmi->lock);
	free(mmi);
}

void en50221_app_mmi_clear_session(struct en50221_app_mmi *mmi,
				   uint16_t session_number)
{
	pthread_mutex_lock(&mmi->lock);
	struct en50221_app_mmi_session *cur_s = mmi->sessions;
	struct en50221_app_mmi_session *prev_s = NULL;
	while (cur_s) {
		if (cur_s->session_number == session_number) {
			if (cur_s->menu_block_chain)
				free(cur_s->menu_block_chain);
			if (cur_s->list_block_chain)
				free(cur_s->list_block_chain);
			if (cur_s->subtitlesegment_block_chain)
				free(cur_s->subtitlesegment_block_chain);
			if (cur_s->subtitledownload_block_chain)
				free(cur_s->subtitledownload_block_chain);
			if (prev_s) {
				prev_s->next = cur_s->next;
			} else {
				mmi->sessions = cur_s->next;
			}
			free(cur_s);
			return;
		}

		prev_s = cur_s;
		cur_s = cur_s->next;
	}
	pthread_mutex_unlock(&mmi->lock);
}

void en50221_app_mmi_register_close_callback(struct en50221_app_mmi *mmi,
					     en50221_app_mmi_close_callback callback,
					     void *arg)
{
	pthread_mutex_lock(&mmi->lock);
	mmi->closecallback = callback;
	mmi->closecallback_arg = arg;
	pthread_mutex_unlock(&mmi->lock);
}

void en50221_app_mmi_register_display_control_callback(struct en50221_app_mmi *mmi,
						       en50221_app_mmi_display_control_callback callback,
						       void *arg)
{
	pthread_mutex_lock(&mmi->lock);
	mmi->displaycontrolcallback = callback;
	mmi->displaycontrolcallback_arg = arg;
	pthread_mutex_unlock(&mmi->lock);
}

void en50221_app_mmi_register_keypad_control_callback(struct en50221_app_mmi *mmi,
						      en50221_app_mmi_keypad_control_callback callback,
						      void *arg)
{
	pthread_mutex_lock(&mmi->lock);
	mmi->keypadcontrolcallback = callback;
	mmi->keypadcontrolcallback_arg = arg;
	pthread_mutex_unlock(&mmi->lock);
}

void en50221_app_mmi_register_subtitle_segment_callback(struct en50221_app_mmi *mmi,
							en50221_app_mmi_subtitle_segment_callback callback,
							void *arg)
{
	pthread_mutex_lock(&mmi->lock);
	mmi->subtitlesegmentcallback = callback;
	mmi->subtitlesegmentcallback_arg = arg;
	pthread_mutex_unlock(&mmi->lock);
}

void en50221_app_mmi_register_scene_end_mark_callback(struct en50221_app_mmi *mmi,
						      en50221_app_mmi_scene_end_mark_callback callback, void *arg)
{
	pthread_mutex_lock(&mmi->lock);
	mmi->sceneendmarkcallback = callback;
	mmi->sceneendmarkcallback_arg = arg;
	pthread_mutex_unlock(&mmi->lock);
}

void en50221_app_mmi_register_scene_control_callback(struct en50221_app_mmi *mmi,
						     en50221_app_mmi_scene_control_callback callback, void *arg)
{
	pthread_mutex_lock(&mmi->lock);
	mmi->scenecontrolcallback = callback;
	mmi->scenecontrolcallback_arg = arg;
	pthread_mutex_unlock(&mmi->lock);
}

void en50221_app_mmi_register_subtitle_download_callback(struct en50221_app_mmi *mmi,
							 en50221_app_mmi_subtitle_download_callback callback,
							 void *arg)
{
	pthread_mutex_lock(&mmi->lock);
	mmi->subtitledownloadcallback = callback;
	mmi->subtitledownloadcallback_arg = arg;
	pthread_mutex_unlock(&mmi->lock);
}

void en50221_app_mmi_register_flush_download_callback(struct en50221_app_mmi *mmi,
						      en50221_app_mmi_flush_download_callback callback,
						      void *arg)
{
	pthread_mutex_lock(&mmi->lock);
	mmi->flushdownloadcallback = callback;
	mmi->flushdownloadcallback_arg = arg;
	pthread_mutex_unlock(&mmi->lock);
}

void en50221_app_mmi_register_enq_callback(struct en50221_app_mmi *mmi,
					   en50221_app_mmi_enq_callback callback,
					   void *arg)
{
	pthread_mutex_lock(&mmi->lock);
	mmi->enqcallback = callback;
	mmi->enqcallback_arg = arg;
	pthread_mutex_unlock(&mmi->lock);
}

void en50221_app_mmi_register_menu_callback(struct en50221_app_mmi *mmi,
					    en50221_app_mmi_menu_callback callback,
					    void *arg)
{
	pthread_mutex_lock(&mmi->lock);
	mmi->menucallback = callback;
	mmi->menucallback_arg = arg;
	pthread_mutex_unlock(&mmi->lock);
}

void en50221_app_mmi_register_list_callback(struct en50221_app_mmi *mmi,
					    en50221_app_mmi_list_callback callback,
					    void *arg)
{
	pthread_mutex_lock(&mmi->lock);
	mmi->listcallback = callback;
	mmi->listcallback_arg = arg;
	pthread_mutex_unlock(&mmi->lock);
}

int en50221_app_mmi_close(struct en50221_app_mmi *mmi,
			  uint16_t session_number,
			  uint8_t cmd_id, uint8_t delay)
{
	uint8_t data[6];
	int data_length = 5;

	data[0] = (TAG_CLOSE_MMI >> 16) & 0xFF;
	data[1] = (TAG_CLOSE_MMI >> 8) & 0xFF;
	data[2] = TAG_CLOSE_MMI & 0xFF;
	data[3] = 1;
	data[4] = cmd_id;
	if (cmd_id == MMI_CLOSE_MMI_CMD_ID_DELAY) {
		data[3] = 2;
		data[5] = delay;
		data_length = 6;
	}
	return mmi->funcs->send_data(mmi->funcs->arg, session_number, data,
				     data_length);
}

int en50221_app_mmi_display_reply(struct en50221_app_mmi *mmi,
				  uint16_t session_number,
				  uint8_t reply_id,
				  struct en50221_app_mmi_display_reply_details *details)
{
	uint8_t data[32];
	struct iovec iov[2];
	uint32_t iov_count;
	int length_field_len;

	// fill out the start of the header
	data[0] = (TAG_DISPLAY_REPLY >> 16) & 0xFF;
	data[1] = (TAG_DISPLAY_REPLY >> 8) & 0xFF;
	data[2] = TAG_DISPLAY_REPLY & 0xFF;

	switch (reply_id) {
	case MMI_DISPLAY_REPLY_ID_MMI_MODE_ACK:
		data[3] = 2;
		data[4] = reply_id;
		data[5] = details->u.mode_ack.mmi_mode;
		iov[0].iov_base = data;
		iov[0].iov_len = 6;
		iov_count = 1;
		break;

	case MMI_DISPLAY_REPLY_ID_LIST_DISPLAY_CHAR_TABLES:
	case MMI_DISPLAY_REPLY_ID_LIST_INPUT_CHAR_TABLES:
		if ((length_field_len =
			asn_1_encode(details->u.char_table.table_length + 1, data + 3, 3)) < 0) {
			return -1;
		}
		data[3 + length_field_len] = reply_id;
		iov[0].iov_base = data;
		iov[0].iov_len = 3 + length_field_len + 1;
		iov[1].iov_base = details->u.char_table.table;
		iov[1].iov_len = details->u.char_table.table_length;
		iov_count = 2;
		break;

	case MMI_DISPLAY_REPLY_ID_LIST_OVERLAY_GFX_CHARACTERISTICS:
	case MMI_DISPLAY_REPLY_ID_LIST_FULLSCREEN_GFX_CHARACTERISTICS:
		{
			if ((length_field_len =
				asn_1_encode(1 + 9 + (details->u.gfx.num_pixel_depths * 2), data + 3, 3)) < 0) {
				return -1;
			}
			data[3 + length_field_len] = reply_id;
			data[3 + length_field_len + 1] = details->u.gfx.width >> 8;
			data[3 + length_field_len + 2] = details->u.gfx.width;
			data[3 + length_field_len + 3] = details->u.gfx.height >> 8;
			data[3 + length_field_len + 4] = details->u.gfx.height;
			data[3 + length_field_len + 5] =
				((details->u.gfx.aspect_ratio & 0x0f) << 4) |
				((details->u.gfx.gfx_relation_to_video & 0x07) << 1) |
				(details->u.gfx.multiple_depths & 1);
			data[3 + length_field_len + 6] = details->u.gfx.display_bytes >> 4;
			data[3 + length_field_len + 7] =
				((details->u.gfx.display_bytes & 0x0f) << 4) |
				((details->u.gfx.composition_buffer_bytes & 0xf0) >> 4);
			data[3 + length_field_len + 8] =
			    	((details->u.gfx.composition_buffer_bytes & 0x0f) << 4) |
				((details->u.gfx.object_cache_bytes & 0xf0) >> 4);
			data[3 + length_field_len + 9] =
			    	((details->u.gfx.object_cache_bytes & 0x0f) << 4) |
				 (details->u.gfx.num_pixel_depths & 0x0f);

			// render the pixel depths themselves
			uint8_t *pixdepths =
			    alloca(details->u.gfx.num_pixel_depths * 2);
			if (pixdepths == NULL) {
				return -1;
			}
			uint32_t i;
			for (i = 0; i < details->u.gfx.num_pixel_depths; i++) {
				pixdepths[0] =
				    ((details->u.gfx.pixel_depths[i].display_depth & 0x07) << 5) |
				    ((details->u.gfx.pixel_depths[i].pixels_per_byte & 0x07) << 2);
				pixdepths[1] =
				    details->u.gfx.pixel_depths[i].region_overhead;
				pixdepths += 2;
			}

			// make up the iovs
			iov[0].iov_base = data;
			iov[0].iov_len = 3 + length_field_len + 10;
			iov[1].iov_base = pixdepths;
			iov[1].iov_len =
			    details->u.gfx.num_pixel_depths * 2;
			iov_count = 2;
			break;
		}

	default:
		data[3] = 1;
		data[4] = reply_id;
		iov[0].iov_base = data;
		iov[0].iov_len = 5;
		iov_count = 1;
		break;
	}

	// sendit
	return mmi->funcs->send_datav(mmi->funcs->arg, session_number, iov, iov_count);
}

int en50221_app_mmi_keypress(struct en50221_app_mmi *mmi,
			     uint16_t session_number, uint8_t keycode)
{
	uint8_t data[5];

	data[0] = (TAG_KEYPRESS >> 16) & 0xFF;
	data[1] = (TAG_KEYPRESS >> 8) & 0xFF;
	data[2] = TAG_KEYPRESS & 0xFF;
	data[3] = 1;
	data[4] = keycode;
	return mmi->funcs->send_data(mmi->funcs->arg, session_number, data, 5);
}

int en50221_app_mmi_display_message(struct en50221_app_mmi *mmi,
				    uint16_t session_number,
				    uint8_t display_message_id)
{
	uint8_t data[5];

	data[0] = (TAG_DISPLAY_MESSAGE >> 16) & 0xFF;
	data[1] = (TAG_DISPLAY_MESSAGE >> 8) & 0xFF;
	data[2] = TAG_DISPLAY_MESSAGE & 0xFF;
	data[3] = 1;
	data[4] = display_message_id;
	return mmi->funcs->send_data(mmi->funcs->arg, session_number, data, 5);
}

int en50221_app_mmi_scene_done(struct en50221_app_mmi *mmi,
			       uint16_t session_number,
			       uint8_t decoder_continue,
			       uint8_t scene_reveal, uint8_t scene_tag)
{
	uint8_t data[5];

	data[0] = (TAG_SCENE_DONE >> 16) & 0xFF;
	data[1] = (TAG_SCENE_DONE >> 8) & 0xFF;
	data[2] = TAG_SCENE_DONE & 0xFF;
	data[3] = 1;
	data[4] =
		(decoder_continue ? 0x80 : 0x00) |
	    	(scene_reveal ? 0x40 : 0x00) |
		(scene_tag & 0x0f);
	return mmi->funcs->send_data(mmi->funcs->arg, session_number, data, 5);
}

int en50221_app_mmi_download_reply(struct en50221_app_mmi *mmi,
				   uint16_t session_number,
				   uint16_t object_id,
				   uint8_t download_reply_id)
{
	uint8_t data[7];

	data[0] = (TAG_DOWNLOAD_REPLY >> 16) & 0xFF;
	data[1] = (TAG_DOWNLOAD_REPLY >> 8) & 0xFF;
	data[2] = TAG_DOWNLOAD_REPLY & 0xFF;
	data[3] = 3;
	data[4] = object_id >> 8;
	data[5] = object_id;
	data[6] = download_reply_id;
	return mmi->funcs->send_data(mmi->funcs->arg, session_number, data, 7);
}

int en50221_app_mmi_answ(struct en50221_app_mmi *mmi,
			 uint16_t session_number,
			 uint8_t answ_id,
			 uint8_t * text, uint32_t text_count)
{
	uint8_t buf[10];

	// set up the tag
	buf[0] = (TAG_ANSWER >> 16) & 0xFF;
	buf[1] = (TAG_ANSWER >> 8) & 0xFF;
	buf[2] = TAG_ANSWER & 0xFF;

	// encode the length field
	struct iovec iov[2];
	int length_field_len = 0;
	int iov_count = 1;
	if (answ_id == MMI_ANSW_ID_ANSWER) {
		if ((length_field_len = asn_1_encode(text_count + 1, buf + 3, 3)) < 0) {
			return -1;
		}
		buf[3 + length_field_len] = answ_id;

		iov[0].iov_base = buf;
		iov[0].iov_len = 3 + length_field_len + 1;
		iov[1].iov_base = text;
		iov[1].iov_len = text_count;
		iov_count = 2;
	} else {
		buf[3] = 1;
		buf[4] = answ_id;
		iov[0].iov_base = buf;
		iov[0].iov_len = 5;
		iov_count = 1;
	}

	// create the data and send it
	return mmi->funcs->send_datav(mmi->funcs->arg, session_number, iov,
				      iov_count);
}

int en50221_app_mmi_menu_answ(struct en50221_app_mmi *mmi,
			      uint16_t session_number, uint8_t choice_ref)
{
	uint8_t data[5];

	data[0] = (TAG_MENU_ANSWER >> 16) & 0xFF;
	data[1] = (TAG_MENU_ANSWER >> 8) & 0xFF;
	data[2] = TAG_MENU_ANSWER & 0xFF;
	data[3] = 1;
	data[4] = choice_ref;
	return mmi->funcs->send_data(mmi->funcs->arg, session_number, data, 5);
}

int en50221_app_mmi_message(struct en50221_app_mmi *mmi,
			    uint8_t slot_id,
			    uint16_t session_number,
			    uint32_t resource_id,
			    uint8_t * data, uint32_t data_length)
{
	(void) resource_id;

	// get the tag
	if (data_length < 3) {
		print(LOG_LEVEL, ERROR, 1, "Received short data\n");
		return -1;
	}
	uint32_t tag = (data[0] << 16) | (data[1] << 8) | data[2];

	switch (tag) {
	case TAG_CLOSE_MMI:
		return en50221_app_mmi_parse_close(mmi, slot_id,
						   session_number,
						   data + 3,
						   data_length - 3);
	case TAG_DISPLAY_CONTROL:
		return en50221_app_mmi_parse_display_control(mmi, slot_id,
							     session_number,
							     data + 3,
							     data_length - 3);
	case TAG_KEYPAD_CONTROL:
		return en50221_app_mmi_parse_keypad_control(mmi, slot_id,
							    session_number,
							    data + 3,
							    data_length - 3);
	case TAG_ENQUIRY:
		return en50221_app_mmi_parse_enq(mmi, slot_id,
						 session_number, data + 3,
						 data_length - 3);
	case TAG_MENU_LAST:
		return en50221_app_mmi_parse_list_menu(mmi, slot_id,
						       session_number, tag,
						       1, data + 3,
						       data_length - 3);
	case TAG_MENU_MORE:
		return en50221_app_mmi_parse_list_menu(mmi, slot_id,
						       session_number, tag,
						       0, data + 3,
						       data_length - 3);
	case TAG_LIST_LAST:
		return en50221_app_mmi_parse_list_menu(mmi, slot_id,
						       session_number, tag,
						       1, data + 3,
						       data_length - 3);
	case TAG_LIST_MORE:
		return en50221_app_mmi_parse_list_menu(mmi, slot_id,
						       session_number, tag,
						       0, data + 3,
						       data_length - 3);
	case TAG_SUBTITLE_SEGMENT_LAST:
		return en50221_app_mmi_parse_subtitle(mmi, slot_id,
						      session_number, tag,
						      1, data + 3,
						      data_length - 3);
	case TAG_SUBTITLE_SEGMENT_MORE:
		return en50221_app_mmi_parse_subtitle(mmi, slot_id,
						      session_number, tag,
						      0, data + 3,
						      data_length - 3);
	case TAG_SCENE_END_MARK:
		return en50221_app_mmi_parse_scene_end_mark(mmi, slot_id,
							    session_number,
							    data + 3,
							    data_length - 3);
	case TAG_SCENE_CONTROL:
		return en50221_app_mmi_parse_scene_control(mmi, slot_id,
							   session_number,
							   data + 3,
							   data_length - 3);
	case TAG_SUBTITLE_DOWNLOAD_LAST:
		return en50221_app_mmi_parse_subtitle(mmi, slot_id,
						      session_number, tag,
						      1, data + 3,
						      data_length - 3);
	case TAG_SUBTITLE_DOWNLOAD_MORE:
		return en50221_app_mmi_parse_subtitle(mmi, slot_id,
						      session_number, tag,
						      0, data + 3,
						      data_length - 3);
	case TAG_FLUSH_DOWNLOAD:
		return en50221_app_mmi_parse_flush_download(mmi, slot_id,
							    session_number,
							    data + 3,
							    data_length - 3);
	}

	print(LOG_LEVEL, ERROR, 1, "Received unexpected tag %x\n", tag);
	return -1;
}





static int en50221_app_mmi_parse_close(struct en50221_app_mmi *mmi,
				       uint8_t slot_id,
				       uint16_t session_number,
				       uint8_t * data,
				       uint32_t data_length)
{
	// validate data
	if (data_length < 2) {
		print(LOG_LEVEL, ERROR, 1, "Received short data\n");
		return -1;
	}
	if (data[0] > (data_length - 1)) {
		print(LOG_LEVEL, ERROR, 1, "Received short data\n");
		return -1;
	}
	uint8_t cmd_id = data[1];
	uint8_t delay = 0;
	if (cmd_id == MMI_CLOSE_MMI_CMD_ID_DELAY) {
		if (data[0] != 2) {
			print(LOG_LEVEL, ERROR, 1,
			      "Received short data\n");
			return -1;
		}
		delay = data[2];
	}
	// tell the app
	pthread_mutex_lock(&mmi->lock);
	en50221_app_mmi_close_callback cb = mmi->closecallback;
	void *cb_arg = mmi->closecallback_arg;
	pthread_mutex_unlock(&mmi->lock);
	if (cb) {
		return cb(cb_arg, slot_id, session_number, cmd_id, delay);
	}
	return 0;
}

static int en50221_app_mmi_parse_display_control(struct en50221_app_mmi *mmi,
						 uint8_t slot_id,
						 uint16_t session_number,
						 uint8_t *data,
						 uint32_t data_length)
{
	// validate data
	if (data_length < 2) {
		print(LOG_LEVEL, ERROR, 1, "Received short data\n");
		return -1;
	}
	if (data[0] > (data_length - 1)) {
		print(LOG_LEVEL, ERROR, 1, "Received short data\n");
		return -1;
	}
	uint8_t cmd_id = data[1];
	uint8_t mmi_mode = 0;
	if (cmd_id == MMI_DISPLAY_CONTROL_CMD_ID_SET_MMI_MODE) {
		if (data[0] != 2) {
			print(LOG_LEVEL, ERROR, 1,
			      "Received short data\n");
			return -1;
		}
		mmi_mode = data[2];
	}
	// tell the app
	pthread_mutex_lock(&mmi->lock);
	en50221_app_mmi_display_control_callback cb = mmi->displaycontrolcallback;
	void *cb_arg = mmi->displaycontrolcallback_arg;
	pthread_mutex_unlock(&mmi->lock);
	if (cb) {
		return cb(cb_arg, slot_id, session_number, cmd_id, mmi_mode);
	}
	return 0;
}

static int en50221_app_mmi_parse_keypad_control(struct en50221_app_mmi *mmi,
						uint8_t slot_id,
						uint16_t session_number,
						uint8_t *data,
						uint32_t data_length)
{
	// first of all, decode the length field
	uint16_t asn_data_length;
	int length_field_len;
	if ((length_field_len = asn_1_decode(&asn_data_length, data, data_length)) < 0) {
		print(LOG_LEVEL, ERROR, 1, "ASN.1 decode error\n");
		return -1;
	}
	// check it
	if (asn_data_length > (data_length - length_field_len)) {
		print(LOG_LEVEL, ERROR, 1, "Received short data\n");
		return -1;
	}
	if (asn_data_length < 1) {
		print(LOG_LEVEL, ERROR, 1, "Received short data\n");
		return -1;
	}
	// skip over the length field
	data += length_field_len;

	// extract the information
	uint8_t cmd_id = data[0];
	uint8_t *keycodes = data + 1;

	// tell the app
	pthread_mutex_lock(&mmi->lock);
	en50221_app_mmi_keypad_control_callback cb = mmi->keypadcontrolcallback;
	void *cb_arg = mmi->keypadcontrolcallback_arg;
	pthread_mutex_unlock(&mmi->lock);
	if (cb) {
		return cb(cb_arg, slot_id, session_number, cmd_id,
			  keycodes, asn_data_length - 1);
	}
	return 0;
}

static int en50221_app_mmi_parse_enq(struct en50221_app_mmi *mmi,
				     uint8_t slot_id,
				     uint16_t session_number,
				     uint8_t * data, uint32_t data_length)
{
	// first of all, decode the length field
	uint16_t asn_data_length;
	int length_field_len;
	if ((length_field_len = asn_1_decode(&asn_data_length, data, data_length)) < 0) {
		print(LOG_LEVEL, ERROR, 1, "ASN.1 decode error\n");
		return -1;
	}
	// check it
	if (asn_data_length > (data_length - length_field_len)) {
		print(LOG_LEVEL, ERROR, 1, "Received short data\n");
		return -1;
	}
	if (asn_data_length < 2) {
		print(LOG_LEVEL, ERROR, 1, "Received short data\n");
		return -1;
	}
	// skip over the length field
	data += length_field_len;

	// extract the information
	uint8_t blind_answer = (data[0] & 0x01) ? 1 : 0;
	uint8_t answer_length = data[1];
	uint8_t *text = data + 2;

	// tell the app
	pthread_mutex_lock(&mmi->lock);
	en50221_app_mmi_enq_callback cb = mmi->enqcallback;
	void *cb_arg = mmi->enqcallback_arg;
	pthread_mutex_unlock(&mmi->lock);
	if (cb) {
		return cb(cb_arg, slot_id, session_number, blind_answer,
			  answer_length, text, asn_data_length - 2);
	}
	return 0;
}

static int en50221_app_mmi_parse_list_menu(struct en50221_app_mmi *mmi,
					   uint8_t slot_id,
					   uint16_t session_number,
					   uint32_t tag_id, int more_last,
					   uint8_t * data,
					   uint32_t data_length)
{
	int result = 0;
	uint8_t *text_flags = NULL;
	struct en50221_app_mmi_text *text_data = NULL;
	uint32_t i;
	uint8_t text_count = 0;

	// first of all, decode the length field
	uint16_t asn_data_length;
	int length_field_len;
	if ((length_field_len = asn_1_decode(&asn_data_length, data, data_length)) < 0) {
		print(LOG_LEVEL, ERROR, 1, "ASN.1 decode error\n");
		return -1;
	}
	// check it
	if (asn_data_length > (data_length - length_field_len)) {
		print(LOG_LEVEL, ERROR, 1, "Received short data\n");
		return -1;
	}
	// skip over the length field
	data += length_field_len;

	// defragment
	pthread_mutex_lock(&mmi->lock);
	uint8_t *outdata;
	uint32_t outdata_length;
	int dfstatus =
	    en50221_app_mmi_defragment(mmi, session_number, tag_id,
				       more_last,
				       data, asn_data_length,
				       &outdata, &outdata_length);
	if (dfstatus <= 0) {
		pthread_mutex_unlock(&mmi->lock);
		return dfstatus;
	}
	data = outdata;
	data_length = outdata_length;

	// check the reassembled data length
	if (data_length < 1) {
		print(LOG_LEVEL, ERROR, 1, "Received short data\n");
		pthread_mutex_unlock(&mmi->lock);
		result = -1;
		goto exit_cleanup;
	}
	// now, parse the data
	uint8_t choice_nb = data[0];
	text_count = choice_nb + 3;
	if (choice_nb == 0xff)
		text_count = 3;
	data++;
	data_length--;

	// variables for extracted text state
	text_flags = alloca(text_count);
	if (text_flags == NULL) {
		pthread_mutex_unlock(&mmi->lock);
		result = -1;
		goto exit_cleanup;
	}
	memset(text_flags, 0, text_count);
	text_data = (struct en50221_app_mmi_text *)
	    alloca(sizeof(struct en50221_app_mmi_text) * text_count);
	if (text_data == NULL) {
		pthread_mutex_unlock(&mmi->lock);
		result = -1;
		goto exit_cleanup;
	}
	memset(text_data, 0,
	       sizeof(struct en50221_app_mmi_text) * text_count);

	// extract the text!
	for (i = 0; i < text_count; i++) {
		uint32_t consumed = 0;
		int cur_status =
		    en50221_app_mmi_defragment_text(data, data_length,
						    &text_data[i].text,
						    &text_data[i].text_length,
						    &consumed);
		if (cur_status < 0) {
			pthread_mutex_unlock(&mmi->lock);
			result = -1;
			goto exit_cleanup;
		}

		text_flags[i] = cur_status;
		data += consumed;
		data_length -= consumed;
	}

	// work out what to pass to the user
	struct en50221_app_mmi_text *text_data_for_user = (struct en50221_app_mmi_text *)
	    alloca(sizeof(struct en50221_app_mmi_text) * text_count);
	if (text_data_for_user == NULL) {
		result = -1;
		goto exit_cleanup;
	}
	memcpy(text_data_for_user, text_data,
	       sizeof(struct en50221_app_mmi_text) * text_count);
	struct en50221_app_mmi_text *text_ptr = NULL;
	if (text_count > 3) {
		text_ptr = &text_data_for_user[3];
	}
	uint8_t *items_raw = NULL;
	uint32_t items_raw_length = 0;
	if (choice_nb == 0xff) {
		items_raw = data;
		items_raw_length = data_length;
	}
	// do callback
	result = 0;
	switch (tag_id) {
	case TAG_MENU_LAST:
	{
		en50221_app_mmi_menu_callback cb = mmi->menucallback;
		void *cb_arg = mmi->menucallback_arg;
		pthread_mutex_unlock(&mmi->lock);
		if (cb) {
			result =
				cb(cb_arg, slot_id, session_number,
				&text_data_for_user[0],
				&text_data_for_user[1],
				&text_data_for_user[2],
				text_count - 3, text_ptr,
				items_raw_length, items_raw);
		}
		break;
	}

	case TAG_LIST_LAST:
	{
		en50221_app_mmi_list_callback cb = mmi->listcallback;
		void *cb_arg = mmi->listcallback_arg;
		pthread_mutex_unlock(&mmi->lock);
		if (cb) {
			result =
				cb(cb_arg, slot_id, session_number,
				&text_data_for_user[0],
				&text_data_for_user[1],
				&text_data_for_user[2],
				text_count - 3, text_ptr,
				items_raw_length, items_raw);
		}
		break;
	}

	default:
		pthread_mutex_unlock(&mmi->lock);
		break;
	}

exit_cleanup:
	if ((dfstatus == 2) && outdata)
		free(outdata);
	if (text_flags && text_data) {
		for (i = 0; i < text_count; i++) {
			if ((text_flags[i] == 2) && text_data[i].text) {
				free(text_data[i].text);
			}
		}
	}
	return result;
}

static int en50221_app_mmi_parse_subtitle(struct en50221_app_mmi *mmi,
					  uint8_t slot_id,
					  uint16_t session_number,
					  uint32_t tag_id, int more_last,
					  uint8_t * data,
					  uint32_t data_length)
{
	// first of all, decode the length field
	uint16_t asn_data_length;
	int length_field_len;
	if ((length_field_len = asn_1_decode(&asn_data_length, data, data_length)) < 0) {
		print(LOG_LEVEL, ERROR, 1, "ASN.1 decode error\n");
		return -1;
	}
	// check it
	if (asn_data_length > (data_length - length_field_len)) {
		print(LOG_LEVEL, ERROR, 1, "Received short data\n");
		return -1;
	}
	// skip over the length field
	data += length_field_len;

	// defragment
	pthread_mutex_lock(&mmi->lock);
	uint8_t *outdata;
	uint32_t outdata_length;
	int dfstatus =
	    en50221_app_mmi_defragment(mmi, session_number, tag_id,
				       more_last,
				       data, asn_data_length,
				       &outdata, &outdata_length);
	if (dfstatus <= 0) {
		pthread_mutex_unlock(&mmi->lock);
		return dfstatus;
	}
	// do callback
	int cbstatus = 0;
	switch (tag_id) {
	case TAG_SUBTITLE_SEGMENT_LAST:
		{
			en50221_app_mmi_subtitle_segment_callback cb =
			    mmi->subtitlesegmentcallback;
			void *cb_arg = mmi->subtitlesegmentcallback_arg;
			pthread_mutex_unlock(&mmi->lock);
			if (cb) {
				cbstatus =
				    cb(cb_arg, slot_id, session_number, outdata, outdata_length);
			}
			break;
		}

	case TAG_SUBTITLE_DOWNLOAD_LAST:
		{
			en50221_app_mmi_subtitle_download_callback cb =
			    mmi->subtitledownloadcallback;
			void *cb_arg = mmi->subtitledownloadcallback_arg;
			pthread_mutex_unlock(&mmi->lock);
			if (cb) {
				cbstatus =
				    cb(cb_arg, slot_id, session_number, outdata, outdata_length);
			}
			break;
		}
	}

	// free the data returned by the defragment call if asked to
	if (dfstatus == 2) {
		free(outdata);
	}
	// done
	return cbstatus;
}

static int en50221_app_mmi_parse_scene_end_mark(struct en50221_app_mmi *mmi,
						uint8_t slot_id,
						uint16_t session_number,
						uint8_t * data,
						uint32_t data_length)
{
	// validate data
	if (data_length != 2) {
		print(LOG_LEVEL, ERROR, 1, "Received short data\n");
		return -1;
	}
	if (data[0] != 1) {
		print(LOG_LEVEL, ERROR, 1, "Received short data\n");
		return -1;
	}
	uint8_t flags = data[1];

	// tell the app
	pthread_mutex_lock(&mmi->lock);
	en50221_app_mmi_scene_end_mark_callback cb =
	    mmi->sceneendmarkcallback;
	void *cb_arg = mmi->sceneendmarkcallback_arg;
	pthread_mutex_unlock(&mmi->lock);
	if (cb) {
		return cb(cb_arg, slot_id, session_number,
			  (flags & 0x80) ? 1 : 0,
			  (flags & 0x40) ? 1 : 0,
			  (flags & 0x20) ? 1 : 0, flags & 0x0f);
	}
	return 0;
}

static int en50221_app_mmi_parse_scene_control(struct en50221_app_mmi *mmi,
					       uint8_t slot_id,
					       uint16_t session_number,
					       uint8_t * data,
					       uint32_t data_length)
{
	// validate data
	if (data_length != 2) {
		print(LOG_LEVEL, ERROR, 1, "Received short data\n");
		return -1;
	}
	if (data[0] != 1) {
		print(LOG_LEVEL, ERROR, 1, "Received short data\n");
		return -1;
	}
	uint8_t flags = data[1];

	// tell the app
	pthread_mutex_lock(&mmi->lock);
	en50221_app_mmi_scene_control_callback cb = mmi->scenecontrolcallback;
	void *cb_arg = mmi->scenecontrolcallback_arg;
	pthread_mutex_unlock(&mmi->lock);
	if (cb) {
		return cb(cb_arg, slot_id, session_number,
			  (flags & 0x80) ? 1 : 0,
			  (flags & 0x40) ? 1 : 0, flags & 0x0f);
	}
	return 0;
}

static int en50221_app_mmi_parse_flush_download(struct en50221_app_mmi *mmi,
						uint8_t slot_id,
						uint16_t session_number,
						uint8_t *data,
						uint32_t data_length)
{
	// validate data
	if (data_length != 1) {
		print(LOG_LEVEL, ERROR, 1, "Received short data\n");
		return -1;
	}
	if (data[0] != 0) {
		print(LOG_LEVEL, ERROR, 1, "Received short data\n");
		return -1;
	}
	// tell the app
	pthread_mutex_lock(&mmi->lock);
	en50221_app_mmi_flush_download_callback cb = mmi->flushdownloadcallback;
	void *cb_arg = mmi->flushdownloadcallback_arg;
	pthread_mutex_unlock(&mmi->lock);
	if (cb) {
		return cb(cb_arg, slot_id, session_number);
	}
	return 0;
}

static int en50221_app_mmi_defragment(struct en50221_app_mmi *mmi,
				      uint16_t session_number,
				      uint32_t tag_id,
				      int more_last,
				      uint8_t * indata,
				      uint32_t indata_length,
				      uint8_t ** outdata,
				      uint32_t * outdata_length)
{
	struct en50221_app_mmi_session *cur_s = mmi->sessions;
	while (cur_s) {
		if (cur_s->session_number == session_number)
			break;
		cur_s = cur_s->next;
	}

	// more data is still to come
	if (!more_last) {
		// if there was no previous session, create one
		if (cur_s == NULL) {
			cur_s = malloc(sizeof(struct en50221_app_mmi_session));
			if (cur_s == NULL) {
				print(LOG_LEVEL, ERROR, 1, "Ran out of memory\n");
				return -1;
			}
			cur_s->session_number = session_number;
			cur_s->menu_block_chain = NULL;
			cur_s->menu_block_length = 0;
			cur_s->list_block_chain = NULL;
			cur_s->list_block_length = 0;
			cur_s->subtitlesegment_block_chain = NULL;
			cur_s->subtitlesegment_block_length = 0;
			cur_s->subtitledownload_block_chain = NULL;
			cur_s->subtitledownload_block_length = 0;
			cur_s->next = mmi->sessions;
			mmi->sessions = cur_s;
		}
		// find the block/block_length to use
		uint8_t **block_chain;
		uint32_t *block_length;
		switch (tag_id) {
		case TAG_MENU_LAST:
		case TAG_MENU_MORE:
			block_chain = &cur_s->menu_block_chain;
			block_length = &cur_s->menu_block_length;
			break;
		case TAG_LIST_LAST:
		case TAG_LIST_MORE:
			block_chain = &cur_s->list_block_chain;
			block_length = &cur_s->list_block_length;
			break;
		case TAG_SUBTITLE_SEGMENT_LAST:
		case TAG_SUBTITLE_SEGMENT_MORE:
			block_chain = &cur_s->subtitlesegment_block_chain;
			block_length = &cur_s->subtitlesegment_block_length;
			break;
		case TAG_SUBTITLE_DOWNLOAD_LAST:
		case TAG_SUBTITLE_DOWNLOAD_MORE:
			block_chain = &cur_s->subtitledownload_block_chain;
			block_length = &cur_s->subtitledownload_block_length;
			break;
		default:
			return -1;
		}

		// append the data
		uint8_t *new_data =
		    realloc(*block_chain, *block_length + indata_length);
		if (new_data == NULL) {
			print(LOG_LEVEL, ERROR, 1, "Ran out of memory\n");
			return -1;
		}
		memcpy(new_data + *block_length, indata, indata_length);
		*block_chain = new_data;
		*block_length += indata_length;

		// success, but block not complete yet
		return 0;
	}
	// we hit the last of a possible chain of fragments
	if (cur_s != NULL) {
		// find the block/block_length to use
		uint8_t **block_chain;
		uint32_t *block_length;
		switch (tag_id) {
		case TAG_MENU_LAST:
		case TAG_MENU_MORE:
			block_chain = &cur_s->menu_block_chain;
			block_length = &cur_s->menu_block_length;
			break;
		case TAG_LIST_LAST:
		case TAG_LIST_MORE:
			block_chain = &cur_s->list_block_chain;
			block_length = &cur_s->list_block_length;
			break;
		case TAG_SUBTITLE_SEGMENT_LAST:
		case TAG_SUBTITLE_SEGMENT_MORE:
			block_chain = &cur_s->subtitlesegment_block_chain;
			block_length = &cur_s->subtitlesegment_block_length;
			break;
		case TAG_SUBTITLE_DOWNLOAD_LAST:
		case TAG_SUBTITLE_DOWNLOAD_MORE:
			block_chain = &cur_s->subtitledownload_block_chain;
			block_length = &cur_s->subtitledownload_block_length;
			break;
		default:
			return -1;
		}

		// we have a preceding fragment - need to append
		uint8_t *new_data =
		    realloc(*block_chain, *block_length + indata_length);
		if (new_data == NULL) {
			print(LOG_LEVEL, ERROR, 1, "Ran out of memory\n");
			return -1;
		}
		memcpy(new_data + *block_length, indata, indata_length);
		*outdata_length = *block_length + indata_length;
		*outdata = new_data;
		*block_chain = NULL;
		*block_length = 0;

		// success, and indicate to free the block when done
		return 2;
	}
	// success, but indicate it is not to be freed
	*outdata_length = indata_length;
	*outdata = indata;
	return 1;
}

static int en50221_app_mmi_defragment_text(uint8_t * data,
					   uint32_t data_length,
					   uint8_t ** outdata,
					   uint32_t * outdata_length,
					   uint32_t * outconsumed)
{
	uint8_t *text = NULL;
	uint32_t text_length = 0;
	uint32_t consumed = 0;

	while (1) {
		// get the tag
		if (data_length < 3) {
			print(LOG_LEVEL, ERROR, 1, "Short data\n");
			if (text)
				free(text);
			return -1;
		}
		uint32_t tag = (data[0] << 16) | (data[1] << 8) | data[2];
		data += 3;
		data_length -= 3;
		consumed += 3;

		// get the length of the data and adjust
		uint16_t asn_data_length;
		int length_field_len;
		if ((length_field_len =
		     asn_1_decode(&asn_data_length, data,
				  data_length)) < 0) {
			print(LOG_LEVEL, ERROR, 1, "ASN.1 decode error\n");
			if (text)
				free(text);
			return -1;
		}
		data += length_field_len;
		data_length -= length_field_len;
		consumed += length_field_len;

		// deal with the tags
		if (tag == TAG_TEXT_LAST) {
			if (text == NULL) {
				*outdata = data;
				*outdata_length = asn_data_length;
				*outconsumed = consumed + asn_data_length;
				return 1;
			} else {
				// append the data
				uint8_t *new_text = realloc(text,
							    text_length + asn_data_length);
				if (new_text == NULL) {
					print(LOG_LEVEL, ERROR, 1,
					      "Ran out of memory\n");
					if (text)
						free(text);
					return -1;
				}
				memcpy(new_text + text_length, data,
				       asn_data_length);
				*outdata = new_text;
				*outdata_length =
				    text_length + asn_data_length;
				*outconsumed = consumed + asn_data_length;
				return 2;
			}

		} else if (tag == TAG_TEXT_MORE) {
			// append the data
			uint8_t *new_text =
			    realloc(text, text_length + asn_data_length);
			if (new_text == NULL) {
				print(LOG_LEVEL, ERROR, 1,
				      "Ran out of memory\n");
				if (text)
					free(text);
				return -1;
			}
			memcpy(new_text + text_length, data,
			       asn_data_length);
			text = new_text;
			text_length += asn_data_length;

			// consume the data
			data += asn_data_length;
			data_length -= asn_data_length;
			consumed += asn_data_length;
		} else {
			// unknown tag
			print(LOG_LEVEL, ERROR, 1,
			      "Unknown MMI text tag\n");
			if (text)
				free(text);
			return -1;
		}
	}
}