summaryrefslogtreecommitdiffstats
path: root/sms.c
blob: 2b990e335e7192c558ff99b6d8a6e8adc651f07f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
/*
 * This file is part of Samsung-RIL.
 *
 * Copyright (C) 2010-2011 Joerie de Gram <j.de.gram@gmail.com>
 * Copyright (C) 2011-2013 Paul Kocialkowski <contact@paulk.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"

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

	if (error_code == NULL)
		return RIL_E_GENERIC_FAILURE;

	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;
	}
}

unsigned char ril2ipc_sms_save_msg_status(int status)
{
	switch (status) {
		case 0:
			return IPC_SMS_STATUS_REC_UNREAD;
		case 1:
			return IPC_SMS_STATUS_REC_READ;
		case 2:
			return IPC_SMS_STATUS_STO_UNSENT;
		case 3:
			return IPC_SMS_STATUS_STO_SENT;
		default:
			return IPC_SMS_STATUS_REC_READ;
	}
}

/*
 * Outgoing SMS functions
 */

int ril_request_send_sms_register(char *pdu, int pdu_length, unsigned char *smsc, int smsc_length, RIL_Token t)
{
	struct ril_request_send_sms_info *send_sms;
	struct list_head *list_end;
	struct list_head *list;

	send_sms = calloc(1, sizeof(struct ril_request_send_sms_info));
	if (send_sms == NULL)
		return -1;

	send_sms->pdu = pdu;
	send_sms->pdu_length = pdu_length;
	send_sms->smsc = smsc;
	send_sms->smsc_length = smsc_length;
	send_sms->token = t;

	list_end = ril_data.outgoing_sms;
	while (list_end != NULL && list_end->next != NULL)
		list_end = list_end->next;

	list = list_head_alloc((void *) send_sms, list_end, NULL);

	if (ril_data.outgoing_sms == NULL)
		ril_data.outgoing_sms = list;

	return 0;
}

void ril_request_send_sms_unregister(struct ril_request_send_sms_info *send_sms)
{
	struct list_head *list;

	if (send_sms == NULL)
		return;

	list = ril_data.outgoing_sms;
	while (list != NULL) {
		if (list->data == (void *) send_sms) {
			memset(send_sms, 0, sizeof(struct ril_request_send_sms_info));
			free(send_sms);

			if (list == ril_data.outgoing_sms)
				ril_data.outgoing_sms = list->next;

			list_head_free(list);

			break;
		}
list_continue:
		list = list->next;
	}
}

struct ril_request_send_sms_info *ril_request_send_sms_info_find(void)
{
	struct ril_request_send_sms_info *send_sms;
	struct list_head *list;

	list = ril_data.outgoing_sms;
	while (list != NULL) {
		send_sms = (struct ril_request_send_sms_info *) list->data;
		if (send_sms == NULL)
			goto list_continue;

		return send_sms;

list_continue:
		list = list->next;
	}

	return NULL;
}

struct ril_request_send_sms_info *ril_request_send_sms_info_find_token(RIL_Token t)
{
	struct ril_request_send_sms_info *send_sms;
	struct list_head *list;

	list = ril_data.outgoing_sms;
	while (list != NULL) {
		send_sms = (struct ril_request_send_sms_info *) list->data;
		if (send_sms == NULL)
			goto list_continue;

		if (send_sms->token == t)
			return send_sms;

list_continue:
		list = list->next;
	}

	return NULL;
}

void ril_request_send_sms_info_clear(struct ril_request_send_sms_info *send_sms)
{
	if (send_sms == NULL)
		return;

	if (send_sms->pdu != NULL)
		free(send_sms->pdu);

	if (send_sms->smsc != NULL)
		free(send_sms->smsc);
}

void ril_request_send_sms_next(void)
{
	struct ril_request_send_sms_info *send_sms;
	RIL_Token t;
	char *pdu;
	int pdu_length;
	unsigned char *smsc;
	int smsc_length;
	int rc;

	ril_data.tokens.outgoing_sms = RIL_TOKEN_NULL;

	send_sms = ril_request_send_sms_info_find();
	if (send_sms == NULL)
		return;

	t = send_sms->token;
	pdu = send_sms->pdu;
	pdu_length = send_sms->pdu_length;
	smsc = send_sms->smsc;
	smsc_length = send_sms->smsc_length;

	ril_request_send_sms_unregister(send_sms);

	if (pdu == NULL) {
		RIL_LOGE("SMS send request has no valid PDU");
		if (smsc != NULL)
			free(smsc);
		return;
	}

	ril_data.tokens.outgoing_sms = t;
	if (smsc == NULL) {
		// We first need to get SMS SVC before sending the message
		RIL_LOGD("We have no SMSC, let's ask one");

		rc = ril_request_send_sms_register(pdu, pdu_length, NULL, 0, t);
		if (rc < 0) {
			RIL_LOGE("Unable to add the request to the list");

			ril_request_complete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
			if (pdu != NULL)
				free(pdu);
			// Send the next SMS in the list
			ril_request_send_sms_next();
		}

		ipc_fmt_send_get(IPC_SMS_SVC_CENTER_ADDR, ril_request_get_id(t));
	} else {
		ril_request_send_sms_complete(t, pdu, pdu_length, smsc, smsc_length);
		if (pdu != NULL)
			free(pdu);
		if (smsc != NULL)
			free(smsc);
	}
}

void ril_request_send_sms_complete(RIL_Token t, char *pdu, int pdu_length, unsigned char *smsc, int smsc_length)
{
	struct ipc_sms_send_msg_request send_msg;
	unsigned char send_msg_type;

	unsigned char *pdu_hex;
	int pdu_hex_length;

	void *data;
	int length;

	unsigned char *p;

	if (pdu == NULL || pdu_length <= 0 || smsc == NULL || smsc_length <= 0)
		goto error;

	if ((pdu_length / 2 + smsc_length) > 0xfe) {
		RIL_LOGE("PDU or SMSC too large, aborting");
		goto error;
	}

	pdu_hex_length = pdu_length % 2 == 0 ? pdu_length / 2 :
		(pdu_length ^ 1) / 2;

	// Length of the final message
	length = sizeof(send_msg) + pdu_hex_length + smsc_length;

	RIL_LOGD("Sending SMS message (length: 0x%x)!", length);

	pdu_hex = calloc(1, pdu_hex_length);
	hex2bin(pdu, pdu_length, pdu_hex);
	send_msg_type = IPC_SMS_MSG_SINGLE;

	/* PDU operations */
	int pdu_tp_da_index = 2;
	unsigned char pdu_tp_da_len = pdu_hex[pdu_tp_da_index];

	if (pdu_tp_da_len > 0xff / 2) {
		RIL_LOGE("PDU TP-DA Len failed (0x%x)\n", pdu_tp_da_len);
		goto pdu_end;
	}

	RIL_LOGD("PDU TP-DA Len is 0x%x\n", pdu_tp_da_len);

	int pdu_tp_udh_index = pdu_tp_da_index + pdu_tp_da_len;
	unsigned char pdu_tp_udh_len = pdu_hex[pdu_tp_udh_index];

	if (pdu_tp_udh_len > 0xff / 2 || pdu_tp_udh_len < 5) {
		RIL_LOGE("PDU TP-UDH Len failed (0x%x)\n", pdu_tp_udh_len);
		goto pdu_end;
	}

	RIL_LOGD("PDU TP-UDH Len is 0x%x\n", pdu_tp_udh_len);

	int pdu_tp_udh_num_index = pdu_tp_udh_index + 4;
	unsigned char pdu_tp_udh_num = pdu_hex[pdu_tp_udh_num_index];

	if (pdu_tp_udh_num > 0xf) {
		RIL_LOGE("PDU TP-UDH Num failed (0x%x)\n", pdu_tp_udh_num);
		goto pdu_end;
	}

	int pdu_tp_udh_seq_index = pdu_tp_udh_index + 5;
	unsigned char pdu_tp_udh_seq = pdu_hex[pdu_tp_udh_seq_index];

	if (pdu_tp_udh_seq > 0xf || pdu_tp_udh_seq > pdu_tp_udh_num) {
		RIL_LOGE("PDU TP-UDH Seq failed (0x%x)\n", pdu_tp_udh_seq);
		goto pdu_end;
	}

	RIL_LOGD("We are sending message %d on %d\n", pdu_tp_udh_seq, pdu_tp_udh_num);

	if (pdu_tp_udh_num > 1) {
		RIL_LOGD("We are sending a multi-part message!");
		send_msg_type = IPC_SMS_MSG_MULTIPLE;
	}

pdu_end:
	// Alloc memory for the final message
	data = calloc(1, length);

	// Clear and fill the IPC structure part of the message
	memset(&send_msg, 0, sizeof(struct ipc_sms_send_msg_request));
	send_msg.type = IPC_SMS_TYPE_OUTGOING;
	send_msg.msg_type = send_msg_type;
	send_msg.length = (unsigned char) (pdu_hex_length + smsc_length + 1);
	send_msg.smsc_len = smsc_length;

	// Copy the parts of the message
	p = data;
	memcpy(p, &send_msg, sizeof(send_msg));
	p += sizeof(send_msg);
	memcpy(p, smsc, smsc_length);
	p += smsc_length;
	memcpy(p, pdu_hex, pdu_hex_length);

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

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

	free(pdu_hex);
	free(data);

	return;

error:
	ril_request_complete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
	// Send the next SMS in the list
	ril_request_send_sms_next();
}

void ril_request_send_sms(RIL_Token t, void *data, size_t length)
{
	char *pdu = NULL;
	int pdu_length;
	unsigned char *smsc = NULL;
	int smsc_length;
	int rc;

	if (data == NULL || length < (int) (2 * sizeof(char *)))
		goto error;

	if (ril_radio_state_complete(RADIO_STATE_OFF, t))
		return;

	pdu = ((char **) data)[1];
	smsc = ((unsigned char **) data)[0];
	pdu_length = 0;
	smsc_length = 0;

	if (pdu != NULL) {
		pdu_length = strlen(pdu) + 1;
		pdu = strdup(pdu);
	}
	if (smsc != NULL) {
		smsc_length = strlen((char *) smsc);
		smsc = (unsigned char *) strdup((char *) smsc);
	}

	if (ril_data.tokens.outgoing_sms != RIL_TOKEN_NULL) {
		RIL_LOGD("Another outgoing SMS is being processed, adding to the list");

		rc = ril_request_send_sms_register(pdu, pdu_length, smsc, smsc_length, t);
		if (rc < 0) {
			RIL_LOGE("Unable to add the request to the list");
			goto error;
		}

		return;
	}

	ril_data.tokens.outgoing_sms = t;
	if (smsc == NULL) {
		// We first need to get SMS SVC before sending the message
		RIL_LOGD("We have no SMSC, let's ask one");

		rc = ril_request_send_sms_register(pdu, pdu_length, NULL, 0, t);
		if (rc < 0) {
			RIL_LOGE("Unable to add the request to the list");
			goto error;
		}

		ipc_fmt_send_get(IPC_SMS_SVC_CENTER_ADDR, ril_request_get_id(t));
	} else {
		ril_request_send_sms_complete(t, pdu, pdu_length, smsc, smsc_length);
		if (pdu != NULL)
			free(pdu);
		if (smsc != NULL)
			free(smsc);
	}

	return;

error:
	ril_request_complete(t, RIL_E_GENERIC_FAILURE, NULL, 0);

	if (pdu != NULL)
		free(pdu);
	if (smsc != NULL)
		free(smsc);
	// Send the next SMS in the list
	ril_request_send_sms_next();
}

void ril_request_send_sms_expect_more(RIL_Token t, void *data, size_t length)
{
	// No particular treatment here, we already have a queue
	ril_request_send_sms(t, data, length);
}

void ipc_sms_svc_center_addr(struct ipc_message_info *info)
{
	struct ril_request_send_sms_info *send_sms;
	RIL_Token t;
	char *pdu;
	int pdu_length;
	unsigned char *smsc;
	int smsc_length;
	int rc;

	if (info->data == NULL || info->length < sizeof(unsigned char))
		goto error;

	send_sms = ril_request_send_sms_info_find_token(ril_request_get_token(info->aseq));
	if (send_sms == NULL || send_sms->pdu == NULL || send_sms->pdu_length <= 0) {
		RIL_LOGE("The request wasn't queued, reporting generic error!");

		ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
		ril_request_send_sms_info_clear(send_sms);
		ril_request_send_sms_unregister(send_sms);
		// Send the next SMS in the list
		ril_request_send_sms_next();

		return;
	}

	t = send_sms->token;
	pdu = send_sms->pdu;
	pdu_length = send_sms->pdu_length;
	smsc = (unsigned char *) info->data + sizeof(unsigned char);
	smsc_length = (int) ((unsigned char *) info->data)[0];

	RIL_LOGD("Got SMSC, completing the request");
	ril_request_send_sms_unregister(send_sms);
	ril_request_send_sms_complete(t, pdu, pdu_length, smsc, smsc_length);
	if (pdu != NULL)
		free(pdu);

	return;

error:
	ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
}

void ipc_sms_send_msg_complete(struct ipc_message_info *info)
{
	struct ril_request_send_sms_info *send_sms;
	struct ipc_gen_phone_res *phone_res;

	phone_res = (struct ipc_gen_phone_res *) info->data;
	if (ipc_gen_phone_res_check(phone_res) < 0) {
		RIL_LOGE("IPC_GEN_PHONE_RES indicates error, abort request to RILJ");

		ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
		// Send the next SMS in the list
		ril_request_send_sms_next();
	}
}

void ipc_sms_send_msg(struct ipc_message_info *info)
{
	struct ipc_sms_send_msg_response *report_msg;
	RIL_SMS_Response response;
	RIL_Errno e;

	if (info->data == NULL || info->length < sizeof(struct ipc_sms_send_msg_response))
		goto error;

	report_msg = (struct ipc_sms_send_msg_response *) info->data;

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

	memset(&response, 0, sizeof(response));
	response.messageRef = report_msg->msg_tpid;
	response.ackPDU = NULL;

	e = ipc2ril_sms_ack_error(report_msg->error, &response.errorCode);

	ril_request_complete(ril_request_get_token(info->aseq), e, (void *) &response, sizeof(response));

	// Send the next SMS in the list
	ril_request_send_sms_next();

	return;

error:
	ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
}

/*
 * Incoming SMS functions
 */

int ipc_sms_incoming_msg_register(char *pdu, int length, unsigned char type, unsigned char tpid)
{
	struct ipc_sms_incoming_msg_info *incoming_msg;
	struct list_head *list_end;
	struct list_head *list;

	incoming_msg = calloc(1, sizeof(struct ipc_sms_incoming_msg_info));
	if (incoming_msg == NULL)
		return -1;

	incoming_msg->pdu = pdu;
	incoming_msg->length = length;
	incoming_msg->type = type;
	incoming_msg->tpid = tpid;

	list_end = ril_data.incoming_sms;
	while (list_end != NULL && list_end->next != NULL)
		list_end = list_end->next;

	list = list_head_alloc((void *) incoming_msg, list_end, NULL);

	if (ril_data.incoming_sms == NULL)
		ril_data.incoming_sms = list;

	return 0;
}

void ipc_sms_incoming_msg_unregister(struct ipc_sms_incoming_msg_info *incoming_msg)
{
	struct list_head *list;

	if (incoming_msg == NULL)
		return;

	list = ril_data.incoming_sms;
	while (list != NULL) {
		if (list->data == (void *) incoming_msg) {
			memset(incoming_msg, 0, sizeof(struct ipc_sms_incoming_msg_info));
			free(incoming_msg);

			if (list == ril_data.incoming_sms)
				ril_data.incoming_sms = list->next;

			list_head_free(list);

			break;
		}
list_continue:
		list = list->next;
	}
}

struct ipc_sms_incoming_msg_info *ipc_sms_incoming_msg_info_find(void)
{
	struct ipc_sms_incoming_msg_info *incoming_msg;
	struct list_head *list;

	list = ril_data.incoming_sms;
	while (list != NULL) {
		incoming_msg = (struct ipc_sms_incoming_msg_info *) list->data;
		if (incoming_msg == NULL)
			goto list_continue;

		return incoming_msg;

list_continue:
		list = list->next;
	}

	return NULL;
}

void ipc_sms_incoming_msg_next(void)
{
	struct ipc_sms_incoming_msg_info *incoming_msg;

	ril_data.state.sms_incoming_msg_tpid = 0;

	incoming_msg = ipc_sms_incoming_msg_info_find();
	if (incoming_msg == NULL)
		return;

	ipc_sms_incoming_msg_complete(incoming_msg->pdu, incoming_msg->length, incoming_msg->type, incoming_msg->tpid);
	ipc_sms_incoming_msg_unregister(incoming_msg);
}

void ipc_sms_incoming_msg_complete(char *pdu, int length, unsigned char type, unsigned char tpid)
{
	if (pdu == NULL || length <= 0)
		return;

	ril_data.state.sms_incoming_msg_tpid = tpid;

	if (type == IPC_SMS_TYPE_POINT_TO_POINT) {
		ril_request_unsolicited(RIL_UNSOL_RESPONSE_NEW_SMS, pdu, length);
	} else if (type == IPC_SMS_TYPE_STATUS_REPORT) {
		ril_request_unsolicited(RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT, pdu, length);
	} else {
		RIL_LOGE("Unhandled message type: %x", type);
	}

	free(pdu);
}

void ipc_sms_incoming_msg(struct ipc_message_info *info)
{
	struct ipc_sms_incoming_msg *msg;
	unsigned char *pdu_hex;
	char *pdu;
	int length;
	int rc;

	if (info->data == NULL || info->length < sizeof(struct ipc_sms_incoming_msg))
		goto error;

	msg = (struct ipc_sms_incoming_msg *) info->data;
	pdu_hex = ((unsigned char *) info->data + sizeof(struct ipc_sms_incoming_msg));

	length = msg->length * 2 + 1;
	pdu = (char *) calloc(1, length);

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

	if (ril_data.state.sms_incoming_msg_tpid != 0) {
		RIL_LOGD("Another message is waiting ACK, queuing");
		rc = ipc_sms_incoming_msg_register(pdu, length, msg->type, msg->msg_tpid);
		if (rc < 0)
			RIL_LOGE("Unable to register incoming msg");

		return;
	}

	ipc_sms_incoming_msg_complete(pdu, length, msg->type, msg->msg_tpid);

	return;

error:
	if (info != NULL)
		ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
}

void ril_request_sms_acknowledge(RIL_Token t, void *data, size_t length)
{
	struct ipc_sms_deliver_report_request report_msg;
	int success, fail_cause;

	if (data == NULL || length < 2 * sizeof(int))
		goto error;

	if (ril_data.state.sms_incoming_msg_tpid == ril_data.state.ril_sms_tpid) {
		ril_data.state.ril_sms_tpid = 0;

		ril_request_complete(t, RIL_E_SUCCESS, NULL, 0);

		ipc_sms_incoming_msg_next();

		return;
	}

	if (ril_radio_state_complete(RADIO_STATE_OFF, t))
		return;

	success = ((int *) data)[0];
	fail_cause = ((int *) data)[1];

	if (ril_data.state.sms_incoming_msg_tpid == 0) {
		RIL_LOGE("There is no SMS message to ACK!");
		goto error;
	}

	report_msg.type = IPC_SMS_TYPE_STATUS_REPORT;
	report_msg.error = ril2ipc_sms_ack_error(success, fail_cause);
	report_msg.msg_tpid = ril_data.state.sms_incoming_msg_tpid;
	report_msg.unk = 0;

	ipc_gen_phone_res_expect_to_abort(ril_request_get_id(t), IPC_SMS_DELIVER_REPORT);

	ipc_fmt_send(IPC_SMS_DELIVER_REPORT, IPC_TYPE_EXEC, (void *) &report_msg, sizeof(report_msg), ril_request_get_id(t));

	ipc_sms_incoming_msg_next();

	return;

error:
	ril_request_complete(t, RIL_E_GENERIC_FAILURE, NULL, 0);

	ipc_sms_incoming_msg_next();
}

void ipc_sms_deliver_report(struct ipc_message_info *info)
{
	struct ipc_sms_deliver_report_response *report_msg;
	RIL_Errno e;
	int error_code;

	if (info->data == NULL || info->length < sizeof(struct ipc_sms_deliver_report_response))
		goto error;

	report_msg = (struct ipc_sms_deliver_report_response *) info->data;
	e = ipc2ril_sms_ack_error(report_msg->error, &error_code);

	ril_request_complete(ril_request_get_token(info->aseq), e, NULL, 0);

	return;

error:
	if (info != NULL)
		ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
}

void ril_request_write_sms_to_sim(RIL_Token token, void *data, size_t size)
{
	struct ipc_sms_save_msg_request_data *sms_save_msg_request_data;
	RIL_SMS_WriteArgs *args;
	void *buffer = NULL;
	size_t length;
	size_t data_length;
	size_t pdu_length = 0;
	size_t pdu_hex_length = 0;
	size_t smsc_length = 0;
	size_t smsc_hex_length = 0;
	unsigned char *p;

	if (data == NULL || size < sizeof(RIL_SMS_WriteArgs))
		goto error;

	if (ril_radio_state_complete(RADIO_STATE_OFF, token))
		return;

	args = (RIL_SMS_WriteArgs *) data;

	if (args->pdu != NULL) {
		pdu_length = strlen(args->pdu);
		pdu_hex_length = pdu_length % 2 == 0 ? pdu_length / 2 : (pdu_length ^ 1) / 2;
	}

	if (args->smsc != NULL) {
		smsc_length = strlen(args->smsc);
		smsc_hex_length = smsc_length % 2 == 0 ? smsc_length / 2 : (smsc_length ^ 1) / 2;
	}

	data_length = pdu_hex_length + smsc_hex_length;
	if (data_length == 0 || data_length > 0xff)
		goto error;

	length = sizeof(struct ipc_sms_save_msg_request_data) + data_length;
	buffer = malloc(length);

	sms_save_msg_request_data = (struct ipc_sms_save_msg_request_data *) buffer;

	memset(sms_save_msg_request_data, 0, sizeof(struct ipc_sms_save_msg_request_data));
	sms_save_msg_request_data->unknown = 0x02;
	sms_save_msg_request_data->index = 12 - 1;
	sms_save_msg_request_data->status = ril2ipc_sms_save_msg_status(args->status);
	sms_save_msg_request_data->length = (unsigned char) (data_length & 0xff);

	p = (unsigned char *) buffer + sizeof(struct ipc_sms_save_msg_request_data);

	if (args->smsc != NULL && smsc_length > 0) {
		hex2bin(args->smsc, smsc_length, p);
		p += smsc_hex_length;
	}

	if (args->pdu != NULL && pdu_length > 0) {
		hex2bin(args->pdu, pdu_length, p);
		p += pdu_hex_length;
	}

	ipc_gen_phone_res_expect_to_abort(ril_request_get_id(token), IPC_SMS_SAVE_MSG);

	ipc_fmt_send(IPC_SMS_SAVE_MSG, IPC_TYPE_EXEC, buffer, length, ril_request_get_id(token));

	goto complete;

error:
	ril_request_complete(token, RIL_E_GENERIC_FAILURE, NULL, 0);

complete:
	if (buffer != NULL)
		free(buffer);
}

void ipc_sms_save_msg(struct ipc_message_info *info)
{
	struct ipc_sms_save_msg_response_data *sms_save_msg_response_data;

	if (info == NULL || info->data == NULL || info->length < sizeof(struct ipc_sms_save_msg_response_data))
		return;

	sms_save_msg_response_data = (struct ipc_sms_save_msg_response_data *) info->data;

	if (sms_save_msg_response_data->error)
		ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
	else
		ril_request_complete(ril_request_get_token(info->aseq), RIL_E_SUCCESS, NULL, 0);
}

void ril_request_delete_sms_on_sim(RIL_Token token, void *data, size_t size)
{
	struct ipc_sms_del_msg_request_data sms_del_msg_request_data;
	int index = 0;

	if (data == NULL || size < sizeof(index))
		goto error;

	if (ril_radio_state_complete(RADIO_STATE_OFF, token))
		return;

	index = *((int *) data);

	if (index <= 0 || index > 0xffff)
		goto error;

	memset(&sms_del_msg_request_data, 0, sizeof(sms_del_msg_request_data));
	sms_del_msg_request_data.unknown = 0x02;
	sms_del_msg_request_data.index = (short) index - 1;

	ipc_gen_phone_res_expect_to_abort(ril_request_get_id(token), IPC_SMS_DEL_MSG);

	ipc_fmt_send(IPC_SMS_DEL_MSG, IPC_TYPE_EXEC, (unsigned char *) &sms_del_msg_request_data, sizeof(sms_del_msg_request_data), ril_request_get_id(token));

	return;

error:
	ril_request_complete(token, RIL_E_GENERIC_FAILURE, NULL, 0);
}

void ipc_sms_del_msg(struct ipc_message_info *info)
{
	struct ipc_sms_del_msg_response_data *sms_del_msg_response_data;

	if (info == NULL || info->data == NULL || info->length < sizeof(struct ipc_sms_del_msg_response_data))
		return;

	sms_del_msg_response_data = (struct ipc_sms_del_msg_response_data *) info->data;

	if (sms_del_msg_response_data->error)
		ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
	else
		ril_request_complete(ril_request_get_token(info->aseq), RIL_E_SUCCESS, NULL, 0);
}

/*
 * RIL SMS
 */

int ril_sms_send(char *number, char *message)
{
	char *pdu;
	size_t length;
	int rc;

	pdu = pdu_create(number, message);
	if (pdu == NULL)
		return -1;

	length = strlen(pdu);
	if (length == 0)
		return -1;

	ril_data.state.ril_sms_tpid = RIL_SMS_TPID;

	if (ril_data.state.sms_incoming_msg_tpid != 0) {
		RIL_LOGD("Another message is waiting ACK, queuing");
		rc = ipc_sms_incoming_msg_register(pdu, length, IPC_SMS_TYPE_POINT_TO_POINT, ril_data.state.ril_sms_tpid);
		if (rc < 0) {
			RIL_LOGE("Unable to register incoming msg");
			return -1;
		}

		return 0;
	}

	ipc_sms_incoming_msg_complete(pdu, length, IPC_SMS_TYPE_POINT_TO_POINT, ril_data.state.ril_sms_tpid);

	return 0;
}

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

void ipc_sms_device_ready(struct ipc_message_info *info)
{
#if RIL_VERSION >= 7
	if (ril_data.state.radio_state == RADIO_STATE_ON) {
#else
	if (ril_data.state.radio_state == RADIO_STATE_SIM_READY) {
#endif
		ipc_fmt_send(IPC_SMS_DEVICE_READY, IPC_TYPE_SET, NULL, 0, info->aseq);
	}

	ril_tokens_check();
}