summaryrefslogtreecommitdiffstats
path: root/sms.c
blob: 46e6a79d5caabb2d35ea416797e1b9f288f4029b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/**
 * This file is part of samsung-ril.
 *
 * Copyright (C) 2010-2011 Joerie de Gram <j.de.gram@gmail.com>
 * Copyright (C) 2011 Paul Kocialkowski <contact@oaulk.fr>
 *
 * samsung-ril is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * samsung-ril 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 samsung-ril.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#define LOG_TAG "RIL-SMS"
#include <utils/Log.h>

#include "samsung-ril.h"
#include "util.h"

/**
 * SMS global vars
 */

struct ril_request_sms ril_request_sms[10];
int ril_request_sms_lock = 0;

unsigned char ipc_sms_tpid_queue[10];

/**
 * Format conversion utils
 */

unsigned short ril2ipc_sms_ack_error(int success, int failcause)
{
	if(success) {
		return IPC_SMS_ACK_NO_ERROR;
	} else {
		switch(failcause) {
			case 0xD3:
				return IPC_SMS_ACK_PDA_FULL_ERROR;
			default:
				return IPC_SMS_ACK_UNSPEC_ERROR;
		}
	}
}

RIL_Errno ipc2ril_sms_ack_error(unsigned short error, int *error_code)
{
	/* error_code is defined in See 3GPP 27.005, 3.2.5 for GSM/UMTS */

	switch(error) {
		case IPC_SMS_ACK_NO_ERROR:
			*error_code = -1;
			return RIL_E_SUCCESS;
		default:
			// unknown error
			*error_code = 500;
			return RIL_E_GENERIC_FAILURE;
	}
}

/**
 * RIL request SMS (queue) functions
 */

void ril_request_sms_init(void)
{
	memset(ril_request_sms, 0, sizeof(struct ril_request_sms) * 10);
	ril_request_sms_lock = 0;
}

void ril_request_sms_del(int id)
{
	if(id < 0 || id > 9) {
		LOGD("Invalid id (%d) for the SMS queue", id);
		return;
	}

	ril_request_sms[id].aseq = 0;
	ril_request_sms[id].pdu_len = 0;
	ril_request_sms[id].smsc_len = 0;

	if(ril_request_sms[id].pdu != NULL)
		free(ril_request_sms[id].pdu);
	if(ril_request_sms[id].smsc != NULL)
		free(ril_request_sms[id].smsc);
}

void ril_request_sms_clear(int id)
{
	if(id < 0 || id > 9) {
		LOGD("Invalid id (%d) for the SMS queue", id);
		return;
	}

	ril_request_sms[id].aseq = 0;
	ril_request_sms[id].pdu = NULL;
	ril_request_sms[id].pdu_len = 0;
	ril_request_sms[id].smsc = NULL;
	ril_request_sms[id].smsc_len = 0;
}

int ril_request_sms_new(void)
{
	int id = -1;
	int i;

	/* Find the highest place in the queue */
	for(i=10 ; i > 0 ; i--) {
		if(ril_request_sms[i-1].aseq && ril_request_sms[i-1].pdu) {
			break;
		}

		id = i-1;
	}

	if(id < 0) {
		LOGE("The SMS queue is full, removing the oldest req");
	
		/* Free the request at index 0 (oldest) */
		ril_request_sms_del(0);

		/* Increase all the requests id to have the last one free */
		for(i=1 ; i < 10 ; i++) {
			LOGD("SMS queue: moving %d -> %d", i, i-1);
			memcpy(&ril_request_sms[i-1], &ril_request_sms[i], sizeof(struct ril_request_sms));
		}

		/* We must not free the pointers here as we copied these at index 8 */

		ril_request_sms_clear(9);

		return 9;
	}

	return id;
}

int ril_request_sms_add(unsigned char aseq,
			char *pdu, unsigned char pdu_len, 
			char *smsc, unsigned char smsc_len)
{
	int id = ril_request_sms_new();

	LOGD("Storing new SMS request in the queue at index %d\n", id);

	ril_request_sms[id].aseq = aseq;
	ril_request_sms[id].smsc_len = smsc_len;
	ril_request_sms[id].pdu_len = pdu_len;

	if(pdu != NULL) {
		ril_request_sms[id].pdu = malloc(pdu_len);
		memcpy(ril_request_sms[id].pdu, pdu, pdu_len);
	}

	if(smsc != NULL) {
		ril_request_sms[id].smsc = malloc(smsc_len);
		memcpy(ril_request_sms[id].smsc, smsc, smsc_len);
	}

	return id;
}

int ril_request_sms_get_id(unsigned char aseq)
{
	int i;

	for(i=0 ; i < 10 ; i++) {
		if(ril_request_sms[i].aseq == aseq) {
			return i;
		}
	}

	return -1;
}

int ril_request_sms_get_next(void)
{
	int id = -1;
	int i;

	for(i=0 ; i < 10 ; i++) {
		if(ril_request_sms[i].aseq && ril_request_sms[i].pdu) {
			id = i;
		}
	}

	if(id < 0)
		LOGD("Nothing left on the queue!");
	else
		LOGD("Next queued request is at id #%d\n", id);

	return id;
}

int ril_request_sms_lock_acquire(void)
{
	if(ril_request_sms_lock > 0) {
		return 0;
	} else
	{
		ril_request_sms_lock = 1;
		return 1;
	}
}

void ril_request_sms_lock_release(void)
{
	ril_request_sms_lock = 0;
}

/**
 * Outgoing SMS functions
 */

/**
 * In: RIL_REQUEST_SEND_SMS
 *   Send an SMS message.
 *
 * Out: IPC_SMS_SEND_MSG
 */
void ril_request_send_sms(RIL_Token t, void *data, size_t datalen)
{
	const char **request = (char **) data;
	char *pdu = request[1];
	int pdu_len = pdu != NULL ? strlen(request[1]) : 0;
	char *smsc = request[0];
	int smsc_len = smsc != NULL ? strlen(request[0]) : 0;

	if(!ril_request_sms_lock_acquire()) {
		LOGD("The SMS lock is already taken, adding req to the SMS queue");

		ril_request_sms_add(reqGetId(t), pdu, pdu_len, smsc, smsc_len);
		return;
	}

	/* We first need to get SMS SVC before sending the message */
	if(smsc == NULL) {
		LOGD("We have no SMSC, let's ask one");

		/* Enqueue the request */
		ril_request_sms_add(reqGetId(t), pdu, pdu_len, NULL, 0);

		ipc_fmt_send_get(IPC_SMS_SVC_CENTER_ADDR, reqGetId(t));
		
	} else {
		ril_request_send_sms_complete(t, pdu, smsc);
	}
}

/**
 * In: RIL_REQUEST_SEND_SMS_EXPECT_MORE
 *   Send an SMS message. Identical to RIL_REQUEST_SEND_SMS,
 *   except that more messages are expected to be sent soon. If possible,
 *   keep SMS relay protocol link open (eg TS 27.005 AT+CMMS command)
 *
 * Out: IPC_SMS_SEND_MSG
 */
void ril_request_send_sms_expect_more(RIL_Token t, void *data, size_t datalen)
{
	/* No particular treatment here, we already have a queue */
	ril_request_send_sms(t, data, datalen);
}

/**
 * Send the next SMS in the queue
 */
int ril_request_send_sms_next(void)
{
	int id = ril_request_sms_get_next();

	char *request[2] = { NULL };
	unsigned char aseq;
	char *pdu;
	char *smsc;

	/* When calling this function, you assume you're done with the previous sms req */
	ril_request_sms_lock_release();

	if(id < 0) 
		return -1;

	LOGD("Sending queued SMS!");

	aseq = ril_request_sms[id].aseq;
	pdu = ril_request_sms[id].pdu;
	smsc = ril_request_sms[id].smsc;

	request[0] = smsc;
	request[1] = pdu;

	/* We need to clear here to prevent infinite loop, but we can't free mem yet */
	ril_request_sms_clear(id);

	ril_request_send_sms(reqGetToken(aseq), (void *) request, sizeof(request));

	if(pdu != NULL)
		free(pdu);

	if(smsc != NULL)
		free(smsc);

	return id;
}

/**
 * Complete (continue) the send_sms request (do the real sending)
 */
void ril_request_send_sms_complete(RIL_Token t, char *pdu, char *smsc)
{
	struct ipc_sms_send_msg send_msg;

	char *data;
	char *decoded_pdu;
	char *p;

	if(pdu == NULL || smsc == NULL) {
		LOGE("Provided PDU or SMSC is NULL! Aborting");

		RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);

		/* Release the lock so we can accept new requests */
		ril_request_sms_lock_release();
		/* Now send the next message in the queue if any */
		ril_request_send_sms_next();

		return;
	}

	int pdu_str_len = strlen(pdu);
	int pdu_len = pdu_str_len / 2;
	int smsc_len = smsc[0];
	int send_msg_len = sizeof(struct ipc_sms_send_msg);

	/* Final length of the message */
	int length = pdu_len + smsc_len + send_msg_len;

	LOGD("Sending SMS message!");

	LOGD("length is 0x%x + 0x%x + 0x%x = 0x%x\n", pdu_len, smsc_len, send_msg_len, length);

	decoded_pdu = malloc(pdu_len);
	hex2bin(pdu, pdu_str_len, decoded_pdu);

	data = malloc(length);
	memset(&send_msg, 0, sizeof(struct ipc_sms_send_msg));

	send_msg.type = IPC_SMS_TYPE_OUTGOING;
	send_msg.msg_type =  IPC_SMS_MSG_SINGLE; //FIXME: decide based on PDU
	send_msg.length = (unsigned char) length; //FIXME: in case of multiple msg, you need to send total size (pdu_size in logs)
	send_msg.smsc_len = (unsigned char) smsc_len;

	p = data;

	memcpy(p, &send_msg, send_msg_len);
	p +=  send_msg_len;
	memcpy(p, (char *) (smsc + 1), smsc_len);
	p += smsc_len;
	memcpy(p, decoded_pdu, pdu_len);

	ipc_gen_phone_res_expect_to_func(reqGetId(t), IPC_SMS_SEND_MSG, ipc_sms_send_msg_complete);

	ipc_fmt_send(IPC_SMS_SEND_MSG, IPC_TYPE_EXEC, data, length, reqGetId(t));

	free(decoded_pdu);
	free(data);
}

void ipc_sms_send_msg_complete(struct ipc_message_info *info)
{
	struct ipc_gen_phone_res *phone_res = (struct ipc_gen_phone_res *) info->data;

	if(ipc_gen_phone_res_check(phone_res) < 0) {
		LOGE("IPC_GEN_PHONE_RES indicates error, abort request to RILJ");

		RIL_onRequestComplete(reqGetToken(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);

		/* Release the lock so we can accept new requests */
		ril_request_sms_lock_release();
		/* Now send the next message in the queue if any */
		ril_request_send_sms_next();
	}
}

/**
 * In: IPC_SMS_SVC_CENTER_ADDR
 *   SMSC: Service Center Address, needed to send an SMS
 *
 * Out: IPC_SMS_SEND_MSG
 */
void ipc_sms_svc_center_addr(struct ipc_message_info *info)
{
	int id = ril_request_sms_get_id(info->aseq);

	char *pdu;
	unsigned char pdu_len;

	if(id < 0) {
		LOGE("The request wasn't queued, reporting generic error!");

		RIL_onRequestComplete(reqGetToken(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);

		/* Release the lock so we can accept new requests */
		ril_request_sms_lock_release();
		/* Now send the next message in the queue if any */
		ril_request_send_sms_next();

		return;
	}

	LOGD("Completing the request");

	pdu = ril_request_sms[id].pdu;
	pdu_len = ril_request_sms[id].pdu_len;

	/* We need to clear here to prevent infinite loop, but we can't free mem yet */
	ril_request_sms_clear(id);

	ril_request_send_sms_complete(reqGetToken(info->aseq), pdu, info->data);

	/* Now it is safe to free mem */
	if(pdu != NULL)
		free(pdu);
}

/**
 * In: IPC_SMS_SEND_MSG
 *   This comes to ACK the latest sent SMS message
 */
void ipc_sms_send_msg(struct ipc_message_info *info)
{
	struct ipc_sms_deliv_report_msg *report_msg = (struct ipc_sms_deliv_report_msg *) info->data;
	RIL_SMS_Response response;
	
	RIL_Errno ril_ack_err;

	LOGD("Got ACK for msg_tpid #%d\n", report_msg->msg_tpid);

	response.messageRef = report_msg->msg_tpid;
	response.ackPDU = NULL;
	ril_ack_err = ipc2ril_sms_ack_error(report_msg->error, &(response.errorCode));

	RIL_onRequestComplete(reqGetToken(info->aseq), ril_ack_err, &response, sizeof(response));

	/* Release the lock so we can accept new requests */
	ril_request_sms_lock_release();
	/* Now send the next message in the queue if any */
	ril_request_send_sms_next();
}

/**
 * IPC incoming SMS queue functions
 */

void ipc_sms_tpid_queue_init(void)
{
	memset(ipc_sms_tpid_queue, 0, sizeof(unsigned char) * 10);
}

void ipc_sms_tpid_queue_del(int id)
{
	if(id < 0 || id > 9) {
		LOGD("Invalid id (%d) for the SMS tpid queue", id);
		return;
	}

	ipc_sms_tpid_queue[id] = 0;
}

int ipc_sms_tpid_queue_new(void)
{
	int id = -1;
	int i;

	/* Find the highest place in the queue */
	for(i=10 ; i > 0 ; i--) {
		if(ipc_sms_tpid_queue[i-1]) {
			break;
		}

		id = i-1;
	}

	if(id < 0) {
		LOGE("The SMS tpid queue is full, removing the oldest tpid");

		ipc_sms_tpid_queue_del(0);

		for(i=1 ; i < 10 ; i++) {
			LOGD("SMS tpid queue: moving %d -> %d", i, i-1);
			ipc_sms_tpid_queue[i-1] = ipc_sms_tpid_queue[i];
		}

		ipc_sms_tpid_queue_del(9);

		return 9;
	}

	return id;
}

int ipc_sms_tpid_queue_add(unsigned char sms_tpid)
{
	int id = ipc_sms_tpid_queue_new();

	LOGD("Storing new SMS tpid in the queue at index %d\n", id);

	ipc_sms_tpid_queue[id] = sms_tpid;

	return id;
}

int ipc_sms_tpid_queue_get_next(void)
{
	int id = -1;
	int i;

	for(i=0 ; i < 10 ; i++) {
		if(ipc_sms_tpid_queue[i]) {
			id = i;
		}
	}

	if(id < 0)
		LOGD("Nothing left on the queue!");
	else
		LOGD("Next queued tpid is at id #%d\n", id);

	return id;
}

/**
 * Incoming SMS functions
 */

/**
 * In: IPC_SMS_INCOMING_MSG
 *   Message to notify an incoming message, with PDU
 *
 * Out: RIL_UNSOL_RESPONSE_NEW_SMS or RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT
 *   Notify RILJ about the incoming message
 */

void ipc_sms_incoming_msg(struct ipc_message_info *info)
{
	struct ipc_sms_incoming_msg *msg = (struct ipc_sms_incoming_msg *) info->data;
	unsigned char *pdu = ((unsigned char *) info->data + sizeof(struct ipc_sms_incoming_msg));

	int resp_length = msg->length * 2 + 1;
	char *resp = (char *) malloc(resp_length);

	bin2hex(pdu, msg->length, resp);

	LOGD("PDU string is '%s'\n", resp);

	ipc_sms_tpid_queue_add(msg->msg_tpid);

	if(msg->type == IPC_SMS_TYPE_POINT_TO_POINT) {
		RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_NEW_SMS, resp, resp_length);
	} else if(msg->type == IPC_SMS_TYPE_STATUS_REPORT) {
		// FIXME: do we need to enqueue for this?

		RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT, resp, resp_length);
	} else {
		LOGE("%s: Unknown message type", __FUNCTION__);
	}

exit:
	free(resp);
}

/**
 * In: RIL_REQUEST_SMS_ACKNOWLEDGE
 *   Acknowledge successful or failed receipt of SMS previously indicated
 *   via RIL_UNSOL_RESPONSE_NEW_SMS
 *
 * Out: IPC_SMS_DELIVER_REPORT
 *   Sends a SMS delivery report
 */
void ril_request_sms_acknowledge(RIL_Token t, void *data, size_t datalen)
{
	struct ipc_sms_deliv_report_msg report_msg;
	int success = ((int *)data)[0];
	int failcause = ((int *)data)[1];
	int id = ipc_sms_tpid_queue_get_next();

	if(id < 0) {
		LOGE("There is no SMS message to ACK!");
		RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);

		return;
	}

	report_msg.type = IPC_SMS_TYPE_STATUS_REPORT;
	report_msg.error = ril2ipc_sms_ack_error(success, failcause);
	report_msg.msg_tpid = ipc_sms_tpid_queue[id];
	report_msg.unk = 0;

	ipc_gen_phone_res_expect_to_abort(reqGetId(t), IPC_SMS_DELIVER_REPORT);

	ipc_fmt_send(IPC_SMS_DELIVER_REPORT, IPC_TYPE_EXEC, (void *) &report_msg, sizeof(struct ipc_sms_deliv_report_msg), reqGetId(t));

	ipc_sms_tpid_queue_del(id);
}

/**
 * In: IPC_SMS_DELIVER_REPORT
 *   Attest that the modem successfully sent our SMS recv ACK 
 */
void ipc_sms_deliver_report(struct ipc_message_info *info)
{
	// TODO: check error code to eventually resend ACK

	RIL_onRequestComplete(reqGetToken(info->aseq), RIL_E_SUCCESS, NULL, 0);
}

/**
 * Apparently non-SMS-messages-related function
 */

void ipc_sms_device_ready(struct ipc_message_info *info)
{
	if(ril_state.radio_state == RADIO_STATE_SIM_READY) {
		ipc_fmt_send(IPC_SMS_DEVICE_READY, IPC_TYPE_SET, NULL, 0, info->aseq);
	}

	ril_tokens_check();
}