summaryrefslogtreecommitdiff
path: root/src/pdu_encode.c
blob: 34028a916a992da993b2c067d49df06c02d2b78d (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
/*
 * PDU Encode
 *
 * Copyright (C) 2010 by James Maki
 *
 * Author: James Maki <jmaki@multitech.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>

#include "global.h"
#include "utils.h"
#include "pdu_encode.h"

#define ENCODE_FAIL(cond, name, ret) \
do { \
	if (cond) { \
		log_error("encode failed at %s", name); \
		return ret; \
	} \
} while (0)

#define CEIL(X) ((X-(int)(X)) > 0 ? (int)(X+1) : (int)(X))

int pdu_encode_timestamp(char *pdu_str, size_t len, struct tm *tm)
{
	int tmp;
	int off_upper;
	int off_lower;
	int off;
	int negative;

	if (len < PDU_TIMESTAMP_SIZE) {
		log_error("buffer is not large enough to hold a timestamp");
		return -1;
	}

	tmp = strftime(pdu_str, PDU_TIMESTAMP_SIZE, "%y%m%d%H%M%S", tm);
	ENCODE_FAIL(tmp != GMT_OFFSET_IDX, "timestamp", -1);

	off = tm->tm_gmtoff;
	if (off < 0) {
		off = -off;
		negative = 1;
	}
	off = off / 60 / 15;

	off_upper = off / 10;
	off_lower = off % 10;

	if (negative) {
		off_upper |= BIT(3);
	}

	snprintf(pdu_str + GMT_OFFSET_IDX, 2, "%X", off_upper & 0xF);
	snprintf(pdu_str + GMT_OFFSET_IDX + 1, 2, "%X", off_lower & 0xF);

	strpad(pdu_str, PDU_TIMESTAMP_LEN, 'F');
	nibble_swap(pdu_str, PDU_TIMESTAMP_LEN);

	log_debug("encode_timestamp: %s", pdu_str);

	return PDU_TIMESTAMP_LEN;
}

int pdu_encode_addr(char *pdu_str, size_t len, struct pdu_addr *addr, int smsc)
{
	char *begin = pdu_str;
	int addr_len;
	int tmp;

	if (is_telit_3gpp2_format()) {
		//CDMA length is # of bytes including address type
		//Ex: Type: 80 #: 9132074666 --> 80 19 23 70 64 66 => 6 bytes
		addr_len = 1;  //1 byte for address type
		addr_len += CEIL(strlen(addr->addr) / 2); //Number of hex bytes
	}
	else {
		//GSM length is # of digits in converted number
		//Ex: Type: 81 #: 9132074666 --> 1923706466 => 10 digits
		addr_len = strlen(addr->addr);
	}

	if (smsc) {
		if (addr_len) {
			if (addr_len & 1) {
				addr_len++;
			}
			addr_len = addr_len / HEX_BYTE_LEN + 1;
		}
	}

	log_debug("addr-len [transformed]: 0x%02X", addr_len);
	addr->len = addr_len;

	tmp = hex_byte_encode(pdu_str, len, addr_len);
	ENCODE_FAIL(tmp != HEX_BYTE_LEN, "addr-len", -1);
	pdu_str += tmp;
	len -= tmp;

	if (smsc && !addr_len) {
		log_debug("smsc addr is empty");
		goto done;
	}

	tmp = hex_byte_encode(pdu_str, len, addr->type);
	ENCODE_FAIL(tmp != HEX_BYTE_LEN, "addr-type", -1);
	pdu_str += tmp;
	len -= tmp;

	addr_len = strlen(addr->addr);
	if (addr_len & 1) {
		addr_len++;
	}

	if (len < addr_len + 1) {
		log_error("buffer is not large enough to hold the addr");
		return -1;
	}

	strcpy(pdu_str, addr->addr);
	strpad(pdu_str, addr_len, 'F');
	nibble_swap(pdu_str, addr_len);
	len -= addr_len;
	pdu_str += addr_len;

done:

	return pdu_str - begin;
}



#define encode_octet(buf, n) \
shiftr(bit_maskl(cycledown(n, 7) + 1, 7) & buf[septet_idx(n)], cycleup(n, 7)) | \
shiftl(bit_maskr(cycleup(n, 7) + 1) & buf[septet_idx(n) + 1], cycledown(n, 7) + 1)

#define encode_cdma_octet(buf, n) \
shiftl(buf[septet_idx(n)], cycleup(n, 7) + 1) | shiftr(buf[septet_idx(n) + 1], cycledown(n, 7))

int pdu_encode_user_data(char *pdu_str, size_t len, struct pdu_info *pdu, int *nr_octets)
{
	char *begin = pdu_str;
	int tmp;
	uint8_t octets[PDU_OCTETS_MAX];
	int i;

	if (pdu->data_coding.general.unused) {
		log_error("data coding group 0x%02X not implemented",
		          pdu->data_coding.data_coding & 0xF0);
		return -1;
	}

	if (pdu->data_coding.general.alphabet == PDU_ALPHABET_DEFAULT) {
		log_debug("data coding alphabet is default (7-bit)");
	} else if (pdu->data_coding.general.alphabet == PDU_ALPHABET_CDMA_DEFAULT) {
		log_debug("data coding alphabet is CDMA default (7-bit)");
	} else if (pdu->data_coding.general.alphabet == PDU_ALPHABET_EIGHT) {
		log_debug("data coding alphabet is eight");
	} else if (pdu->data_coding.general.alphabet == PDU_ALPHABET_CDMA_EIGHT) {
		log_debug("data coding alphabet is CDMA eight");
	} else {
		log_debug("data coding alphabet 0x%02X not implemented",
		          pdu->data_coding.general.alphabet);
		return -1;
	}


	// ---------------------------------------------------------------------
	// FOR GSM 7-BIT ENCODING ONLY, ENCODE FROM IRA ALPHABET TO GSM ALPHABET
	// ---------------------------------------------------------------------
	if (pdu->data_coding.general.alphabet == PDU_ALPHABET_DEFAULT) {
		int read = 0;
		int store = 0;
		char initial_user_data[PDU_UD_7BIT_MAX];

		for (read = 0; read < pdu->user_data_len; read++) {
			initial_user_data[read] = pdu->user_data[read];
		}
		initial_user_data[read] = 0;

		log_debug("Converting from IRA character set to GSM");
		for (read = 0; read < pdu->user_data_len; read++) {
			int GSMchar = indexOfChar(strGSMTable, GSM_TABLE_SIZE, initial_user_data[read]);

			if (GSMchar >= 0) {
				log_debug("IRA before: 0x%02X | GSM after: 0x%02X", initial_user_data[read], GSMchar);
				pdu->user_data[store] = GSMchar;
			}
			else {
				//Check in extended table
				GSMchar = indexOfChar(strExtendedTable, GSM_TABLE_SIZE, initial_user_data[read]);

				if (GSMchar >= 0) {
					log_debug("IRA before: 0x%02X | GSM after: 0x1B%02X", initial_user_data[read], GSMchar);
					pdu->user_data[store] = 0x1B;
					store++;
					pdu->user_data[store] = GSMchar;
				}
				else {
					log_debug("IRA before: 0x%02X | Translation not found - no change", initial_user_data[read]);
					pdu->user_data[store] = initial_user_data[read];
				}
			}

			store++;
		}

		//Update lengths
		pdu->user_data_len += store - read;
	}


	// ------------------------------
	// ENCODE USER DATA INTO PDU DATA
	// ------------------------------
	// GSM 7-BIT ENCODING
	if (pdu->data_coding.general.alphabet == PDU_ALPHABET_DEFAULT) {
		if (pdu->user_data_len > PDU_UD_7BIT_MAX) {
			log_error("string exceeds 7-bit data max length");
			return -1;
		}

		*nr_octets = octets_from_septets(pdu->user_data_len);
		for (i = 0; i < *nr_octets; i++) {
			octets[i] = encode_octet(pdu->user_data, i);
			log_debug("ENCODE: i: %d data: 0x%02X --> octet: 0x%02X", i, pdu->user_data[i], octets[i]);
		}

	// CDMA 7-BIT ENCODING
	} else if (pdu->data_coding.general.alphabet == PDU_ALPHABET_CDMA_DEFAULT) {
		if (pdu->user_data_len > PDU_UD_7BIT_MAX) {
			log_error("string exceeds 7-bit data max length");
			return -1;
		}

		*nr_octets = octets_from_septets(pdu->user_data_len);
		for (i = 0; i < *nr_octets; i++) {
			octets[i] = encode_cdma_octet(pdu->user_data, i);
			log_debug("ENCODE: i: %d data: 0x%02X --> octet: 0x%02X", i, pdu->user_data[i], octets[i]);
		}

	// ALL 8-BIT ENCODING
	} else {
		if (pdu->user_data_len > PDU_UD_8BIT_MAX) {
			log_error("string exceeds 8-bit data max length");
			return -1;
		}

		*nr_octets = pdu->user_data_len;
		for (i = 0; i < pdu->user_data_len; i++) {
			octets[i] = pdu->user_data[i];
		}
	}

	if (len < *nr_octets * 2 + HEX_BYTE_LEN + 1) {
		log_error("buffer is not large enough to hold user-data");
		return -1;
	}

	if ((pdu->data_coding.general.alphabet == PDU_ALPHABET_CDMA_DEFAULT) &&
	    !strcmp(Global.core.model, "DE910-DUAL")) {
		// user_data_len is stored as number of encoded octets
		log_debug("putting nr_octets into PDU as data length (DE910-DUAL)");
		tmp = hex_byte_encode(pdu_str, len, *nr_octets);
	}
	else {
		// user_data_len is stored as number of septets
		log_debug("putting user_data_len into PDU as data length");
		tmp = hex_byte_encode(pdu_str, len, pdu->user_data_len);
	}
	ENCODE_FAIL(tmp != HEX_BYTE_LEN, "user-data-len", -1);
	pdu_str += tmp;
	len -= tmp;

	for (i = 0; i < *nr_octets; i++) {
		hex_byte_encode(pdu_str, len, octets[i]);
		pdu_str += HEX_BYTE_LEN;
	}
	len -= *nr_octets * 2;
	pdu_str[i] = '\0';

	log_debug("user-data-len: 0x%02X (before encoding)", pdu->user_data_len);
	log_debug("nr_octets: 0x%02X (after encoding)", *nr_octets);

	return pdu_str - begin;
}

int pdu_encode(char *pdu_str, size_t len, struct pdu_info *pdu)
{
	char *begin = pdu_str;
	char *msg_begin = pdu_str;
	int tmp;
	int nr_octets;

	tmp = pdu_encode_addr(pdu_str, len, &pdu->smsc_addr, 1);
	ENCODE_FAIL(tmp < 0, "smsc-addr", -1);
	len -= tmp;
	pdu_str += tmp;

	msg_begin = pdu_str;

	tmp = hex_byte_encode(pdu_str, len, pdu->type.type);
	ENCODE_FAIL(tmp != HEX_BYTE_LEN, "type", -1);
	len -= tmp;
	pdu_str += tmp;

	if (pdu->type.msg_type == PDU_MTI_SUBMIT ||
	    pdu->type.msg_type == PDU_MTI_REPORT) {
		tmp = hex_byte_encode(pdu_str, len, pdu->msg_reference);
		ENCODE_FAIL(tmp != HEX_BYTE_LEN, "msg-reference", -1);
		len -= tmp;
		pdu_str += tmp;
	}

	tmp = pdu_encode_addr(pdu_str, len, &pdu->addr, 0);
	ENCODE_FAIL(tmp < 0, "addr", -1);
	len -= tmp;
	pdu_str += tmp;

	switch (pdu->type.msg_type) {
	case PDU_MTI_REPORT:
		tmp = pdu_encode_timestamp(pdu_str, len, &pdu->timestamp);
		ENCODE_FAIL(tmp < 0, "report-timestamp", -1);
		len -= tmp;
		pdu_str += tmp;

		break;
	case PDU_MTI_DELIVER:
	case PDU_MTI_SUBMIT:
		tmp = hex_byte_encode(pdu_str, len, pdu->protocol_id);
		ENCODE_FAIL(tmp != HEX_BYTE_LEN, "protocol-id", -1);
		len -= tmp;
		pdu_str += tmp;

		tmp = hex_byte_encode(pdu_str, len, pdu->data_coding.data_coding);
		ENCODE_FAIL(tmp != HEX_BYTE_LEN, "data-coding-scheme", -1);
		len -= tmp;
		pdu_str += tmp;

		if (pdu->type.msg_type == PDU_MTI_SUBMIT) {
			switch (pdu->type.validity_period_format) {
			case PDU_VPF_RELATIVE:
				tmp = hex_byte_encode(pdu_str, len, pdu->validity_period);
				ENCODE_FAIL(tmp != HEX_BYTE_LEN, "validity-period", -1);
				len -= tmp;
				pdu_str += tmp;

				break;

			case PDU_VPF_ENHANCED:
				log_warning("PDU_VPF_ENHANCED? Falling through to absolute");
			case PDU_VPF_ABSOUTE:
				tmp = pdu_encode_timestamp(pdu_str, len, &pdu->timestamp);
				ENCODE_FAIL(tmp < 0, "validity-period-timestamp", -1);
				len -= tmp;
				pdu_str += tmp;

				break;

			case PDU_VPF_NOT_PRESENT:
			default:
				break;
			}
		} else if (pdu->type.msg_type == PDU_MTI_DELIVER) {
			tmp = pdu_encode_timestamp(pdu_str, len, &pdu->timestamp);
			ENCODE_FAIL(tmp < 0, "delivery-timestamp", -1);
			len -= tmp;
			pdu_str += tmp;
		}

		tmp = pdu_encode_user_data(pdu_str, len, pdu, &nr_octets);
		ENCODE_FAIL(tmp < 0, "user-data", -1);
		len -= tmp;
		pdu_str += tmp;
	}

	pdu->msg_len = (pdu_str - msg_begin) / 2;
	log_debug("msg-len: %d", pdu->msg_len);

	return pdu_str - begin;
}


int pdu_encode_cdma(char *pdu_str, size_t len, struct pdu_info *pdu)
{
	char *begin = pdu_str;
	char *msg_begin = pdu_str;
	int tmp;
	int nr_octets;

	//Set pdu encoding for CDMA
	pdu->data_coding.general.alphabet = PDU_ALPHABET_CDMA_DEFAULT;

	//Destination address
	tmp = pdu_encode_addr(pdu_str, len, &pdu->addr, 0);
	ENCODE_FAIL(tmp < 0, "addr", -1);
	len -= tmp;
	pdu_str += tmp;

	//Callback address (Always 00 because it's empty)
	tmp = hex_byte_encode(pdu_str, len, 0);
	ENCODE_FAIL(tmp != HEX_BYTE_LEN, "callback-addr-len", -1);
	pdu_str += tmp;
	len -= tmp;

	//Teleservice ID (always 1002)
	tmp = hex_byte_encode(pdu_str, len, 16);
	ENCODE_FAIL(tmp != HEX_BYTE_LEN, "tele-id-1", -1);
	pdu_str += tmp;
	len -= tmp;
	tmp = hex_byte_encode(pdu_str, len, 2);
	ENCODE_FAIL(tmp != HEX_BYTE_LEN, "tele-id-2", -1);
	pdu_str += tmp;
	len -= tmp;

	//Priority (always 00 for normal)
	tmp = hex_byte_encode(pdu_str, len, 0);
	ENCODE_FAIL(tmp != HEX_BYTE_LEN, "priority", -1);
	pdu_str += tmp;
	len -= tmp;

	switch (pdu->type.msg_type) {
	case PDU_MTI_REPORT:
		log_error("msg_type PDU_MTI_REPORT not supported!");
		return 0;

	case PDU_MTI_DELIVER:
		log_debug("msg_type: PDU_MTI_DELIVER");
	case PDU_MTI_SUBMIT:
		log_debug("msg_type: PDU_MTI_SUBMIT");

		//add data coding scheme
		tmp = hex_byte_encode(pdu_str, len, pdu->data_coding.data_coding);
		ENCODE_FAIL(tmp != HEX_BYTE_LEN, "data-coding-scheme", -1);
		len -= tmp;
		pdu_str += tmp;


		tmp = pdu_encode_user_data(pdu_str, len, pdu, &nr_octets);
		ENCODE_FAIL(tmp < 0, "user-data", -1);
		len -= tmp;
		pdu_str += tmp;
	}

	// pdu->msg_len = (pdu_str - msg_begin) / 2;
	pdu->msg_len = 6 + nr_octets;
	log_debug("msg-len: %d", pdu->msg_len);

	return pdu_str - begin;
}