2 * This file is part of Samsung-RIL.
4 * Copyright (C) 2010-2011 Joerie de Gram <j.de.gram@gmail.com>
5 * Copyright (C) 2011-2012 Paul Kocialkowski <contact@paulk.fr>
7 * Samsung-RIL is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * Samsung-RIL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Samsung-RIL. If not, see <http://www.gnu.org/licenses/>.
22 #define LOG_TAG "RIL-SMS"
23 #include <utils/Log.h>
25 #include "samsung-ril.h"
28 unsigned short ril2ipc_sms_ack_error(int success, int failcause)
31 return IPC_SMS_ACK_NO_ERROR;
35 return IPC_SMS_ACK_PDA_FULL_ERROR;
37 return IPC_SMS_ACK_UNSPEC_ERROR;
42 RIL_Errno ipc2ril_sms_ack_error(unsigned short error, int *error_code)
44 // error_code is defined in See 3GPP 27.005, 3.2.5 for GSM/UMTS
46 if (error_code == NULL)
47 return RIL_E_GENERIC_FAILURE;
50 case IPC_SMS_ACK_NO_ERROR:
56 return RIL_E_GENERIC_FAILURE;
61 * Outgoing SMS functions
64 int ril_request_send_sms_register(char *pdu, int pdu_length, unsigned char *smsc, int smsc_length, RIL_Token t)
66 struct ril_request_send_sms_info *send_sms;
67 struct list_head *list_end;
68 struct list_head *list;
70 send_sms = calloc(1, sizeof(struct ril_request_send_sms_info));
75 send_sms->pdu_length = pdu_length;
76 send_sms->smsc = smsc;
77 send_sms->smsc_length = smsc_length;
80 list_end = ril_data.outgoing_sms;
81 while (list_end != NULL && list_end->next != NULL)
82 list_end = list_end->next;
84 list = list_head_alloc((void *) send_sms, list_end, NULL);
86 if (ril_data.outgoing_sms == NULL)
87 ril_data.outgoing_sms = list;
92 void ril_request_send_sms_unregister(struct ril_request_send_sms_info *send_sms)
94 struct list_head *list;
99 list = ril_data.outgoing_sms;
100 while (list != NULL) {
101 if (list->data == (void *) send_sms) {
102 memset(send_sms, 0, sizeof(struct ril_request_send_sms_info));
105 if (list == ril_data.outgoing_sms)
106 ril_data.outgoing_sms = list->next;
108 list_head_free(list);
117 struct ril_request_send_sms_info *ril_request_send_sms_info_find(void)
119 struct ril_request_send_sms_info *send_sms;
120 struct list_head *list;
122 list = ril_data.outgoing_sms;
123 while (list != NULL) {
124 send_sms = (struct ril_request_send_sms_info *) list->data;
125 if (send_sms == NULL)
137 struct ril_request_send_sms_info *ril_request_send_sms_info_find_token(RIL_Token t)
139 struct ril_request_send_sms_info *send_sms;
140 struct list_head *list;
142 list = ril_data.outgoing_sms;
143 while (list != NULL) {
144 send_sms = (struct ril_request_send_sms_info *) list->data;
145 if (send_sms == NULL)
148 if (send_sms->token == t)
158 void ril_request_send_sms_info_clear(struct ril_request_send_sms_info *send_sms)
160 if (send_sms == NULL)
163 if (send_sms->pdu != NULL)
166 if (send_sms->smsc != NULL)
167 free(send_sms->smsc);
170 void ril_request_send_sms_next(void)
172 struct ril_request_send_sms_info *send_sms;
180 ril_data.tokens.outgoing_sms = RIL_TOKEN_NULL;
182 send_sms = ril_request_send_sms_info_find();
183 if (send_sms == NULL)
188 pdu_length = send_sms->pdu_length;
189 smsc = send_sms->smsc;
190 smsc_length = send_sms->smsc_length;
192 ril_request_send_sms_unregister(send_sms);
195 LOGE("SMS send request has no valid PDU");
201 ril_data.tokens.outgoing_sms = t;
203 // We first need to get SMS SVC before sending the message
204 LOGD("We have no SMSC, let's ask one");
206 rc = ril_request_send_sms_register(pdu, pdu_length, NULL, 0, t);
208 LOGE("Unable to add the request to the list");
210 ril_request_complete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
213 // Send the next SMS in the list
214 ril_request_send_sms_next();
217 ipc_fmt_send_get(IPC_SMS_SVC_CENTER_ADDR, ril_request_get_id(t));
219 ril_request_send_sms_complete(t, pdu, pdu_length, smsc, smsc_length);
227 void ril_request_send_sms_complete(RIL_Token t, char *pdu, int pdu_length, unsigned char *smsc, int smsc_length)
229 struct ipc_sms_send_msg_request send_msg;
230 unsigned char send_msg_type;
232 unsigned char *pdu_hex;
240 if (pdu == NULL || pdu_length <= 0 || smsc == NULL || smsc_length <= 0)
243 if ((pdu_length / 2 + smsc_length) > 0xfe) {
244 LOGE("PDU or SMSC too large, aborting");
248 pdu_hex_length = pdu_length % 2 == 0 ? pdu_length / 2 :
249 (pdu_length ^ 1) / 2;
251 // Length of the final message
252 length = sizeof(send_msg) + pdu_hex_length + smsc_length;
254 LOGD("Sending SMS message (length: 0x%x)!", length);
256 pdu_hex = calloc(1, pdu_hex_length);
257 hex2bin(pdu, pdu_length, pdu_hex);
258 send_msg_type = IPC_SMS_MSG_SINGLE;
261 int pdu_tp_da_index = 2;
262 unsigned char pdu_tp_da_len = pdu_hex[pdu_tp_da_index];
264 if (pdu_tp_da_len > 0xff / 2) {
265 LOGE("PDU TP-DA Len failed (0x%x)\n", pdu_tp_da_len);
269 LOGD("PDU TP-DA Len is 0x%x\n", pdu_tp_da_len);
271 int pdu_tp_udh_index = pdu_tp_da_index + pdu_tp_da_len;
272 unsigned char pdu_tp_udh_len = pdu_hex[pdu_tp_udh_index];
274 if (pdu_tp_udh_len > 0xff / 2 || pdu_tp_udh_len < 5) {
275 LOGE("PDU TP-UDH Len failed (0x%x)\n", pdu_tp_udh_len);
279 LOGD("PDU TP-UDH Len is 0x%x\n", pdu_tp_udh_len);
281 int pdu_tp_udh_num_index = pdu_tp_udh_index + 4;
282 unsigned char pdu_tp_udh_num = pdu_hex[pdu_tp_udh_num_index];
284 if (pdu_tp_udh_num > 0xf) {
285 LOGE("PDU TP-UDH Num failed (0x%x)\n", pdu_tp_udh_num);
289 int pdu_tp_udh_seq_index = pdu_tp_udh_index + 5;
290 unsigned char pdu_tp_udh_seq = pdu_hex[pdu_tp_udh_seq_index];
292 if (pdu_tp_udh_seq > 0xf || pdu_tp_udh_seq > pdu_tp_udh_num) {
293 LOGE("PDU TP-UDH Seq failed (0x%x)\n", pdu_tp_udh_seq);
297 LOGD("We are sending message %d on %d\n", pdu_tp_udh_seq, pdu_tp_udh_num);
299 if (pdu_tp_udh_num > 1) {
300 LOGD("We are sending a multi-part message!");
301 send_msg_type = IPC_SMS_MSG_MULTIPLE;
305 // Alloc memory for the final message
306 data = calloc(1, length);
308 // Clear and fill the IPC structure part of the message
309 memset(&send_msg, 0, sizeof(struct ipc_sms_send_msg_request));
310 send_msg.type = IPC_SMS_TYPE_OUTGOING;
311 send_msg.msg_type = send_msg_type;
312 send_msg.length = (unsigned char) (pdu_hex_length + smsc_length + 1);
313 send_msg.smsc_len = smsc_length;
315 // Copy the parts of the message
317 memcpy(p, &send_msg, sizeof(send_msg));
318 p += sizeof(send_msg);
319 memcpy(p, smsc, smsc_length);
321 memcpy(p, pdu_hex, pdu_hex_length);
323 ipc_gen_phone_res_expect_to_func(ril_request_get_id(t), IPC_SMS_SEND_MSG, ipc_sms_send_msg_complete);
325 ipc_fmt_send(IPC_SMS_SEND_MSG, IPC_TYPE_EXEC, data, length, ril_request_get_id(t));
333 ril_request_complete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
334 // Send the next SMS in the list
335 ril_request_send_sms_next();
338 void ril_request_send_sms(RIL_Token t, void *data, size_t length)
342 unsigned char *smsc = NULL;
346 if (data == NULL || length < (int) (2 * sizeof(char *)))
349 pdu = ((char **) data)[1];
350 smsc = ((unsigned char **) data)[0];
355 pdu_length = strlen(pdu) + 1;
359 smsc_length = strlen((char *) smsc);
360 smsc = (unsigned char *) strdup((char *) smsc);
363 if (ril_data.tokens.outgoing_sms != RIL_TOKEN_NULL) {
364 LOGD("Another outgoing SMS is being processed, adding to the list");
366 rc = ril_request_send_sms_register(pdu, pdu_length, smsc, smsc_length, t);
368 LOGE("Unable to add the request to the list");
375 ril_data.tokens.outgoing_sms = t;
377 // We first need to get SMS SVC before sending the message
378 LOGD("We have no SMSC, let's ask one");
380 rc = ril_request_send_sms_register(pdu, pdu_length, NULL, 0, t);
382 LOGE("Unable to add the request to the list");
386 ipc_fmt_send_get(IPC_SMS_SVC_CENTER_ADDR, ril_request_get_id(t));
388 ril_request_send_sms_complete(t, pdu, pdu_length, smsc, smsc_length);
398 ril_request_complete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
404 // Send the next SMS in the list
405 ril_request_send_sms_next();
408 void ril_request_send_sms_expect_more(RIL_Token t, void *data, size_t length)
410 // No particular treatment here, we already have a queue
411 ril_request_send_sms(t, data, length);
414 void ipc_sms_svc_center_addr(struct ipc_message_info *info)
416 struct ril_request_send_sms_info *send_sms;
424 if (info == NULL || info->data == NULL || info->length < sizeof(unsigned char))
427 send_sms = ril_request_send_sms_info_find_token(ril_request_get_token(info->aseq));
428 if (send_sms == NULL || send_sms->pdu == NULL || send_sms->pdu_length <= 0) {
429 LOGE("The request wasn't queued, reporting generic error!");
431 ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
432 ril_request_send_sms_info_clear(send_sms);
433 ril_request_send_sms_unregister(send_sms);
434 // Send the next SMS in the list
435 ril_request_send_sms_next();
442 pdu_length = send_sms->pdu_length;
443 smsc = (unsigned char *) info->data + sizeof(unsigned char);
444 smsc_length = (int) ((unsigned char *) info->data)[0];
446 LOGD("Got SMSC, completing the request");
447 ril_request_send_sms_unregister(send_sms);
448 ril_request_send_sms_complete(t, pdu, pdu_length, smsc, smsc_length);
456 ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
459 void ipc_sms_send_msg_complete(struct ipc_message_info *info)
461 struct ril_request_send_sms_info *send_sms;
462 struct ipc_gen_phone_res *phone_res;
464 phone_res = (struct ipc_gen_phone_res *) info->data;
465 if (ipc_gen_phone_res_check(phone_res) < 0) {
466 LOGE("IPC_GEN_PHONE_RES indicates error, abort request to RILJ");
468 ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
469 // Send the next SMS in the list
470 ril_request_send_sms_next();
474 void ipc_sms_send_msg(struct ipc_message_info *info)
476 struct ipc_sms_send_msg_response *report_msg;
477 RIL_SMS_Response response;
480 if (info == NULL || info->data == NULL || info->length < sizeof(struct ipc_sms_send_msg_response))
483 report_msg = (struct ipc_sms_send_msg_response *) info->data;
485 LOGD("Got ACK for msg_tpid #%d\n", report_msg->msg_tpid);
487 memset(&response, 0, sizeof(response));
488 response.messageRef = report_msg->msg_tpid;
489 response.ackPDU = NULL;
491 e = ipc2ril_sms_ack_error(report_msg->error, &response.errorCode);
493 ril_request_complete(ril_request_get_token(info->aseq), e, (void *) &response, sizeof(response));
495 // Send the next SMS in the list
496 ril_request_send_sms_next();
502 ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
506 * Incoming SMS functions
509 int ipc_sms_incoming_msg_register(char *pdu, int length, unsigned char type, unsigned char tpid)
511 struct ipc_sms_incoming_msg_info *incoming_msg;
512 struct list_head *list_end;
513 struct list_head *list;
515 incoming_msg = calloc(1, sizeof(struct ipc_sms_incoming_msg_info));
516 if (incoming_msg == NULL)
519 incoming_msg->pdu = pdu;
520 incoming_msg->length = length;
521 incoming_msg->type = type;
522 incoming_msg->tpid = tpid;
524 list_end = ril_data.incoming_sms;
525 while (list_end != NULL && list_end->next != NULL)
526 list_end = list_end->next;
528 list = list_head_alloc((void *) incoming_msg, list_end, NULL);
530 if (ril_data.incoming_sms == NULL)
531 ril_data.incoming_sms = list;
536 void ipc_sms_incoming_msg_unregister(struct ipc_sms_incoming_msg_info *incoming_msg)
538 struct list_head *list;
540 if (incoming_msg == NULL)
543 list = ril_data.incoming_sms;
544 while (list != NULL) {
545 if (list->data == (void *) incoming_msg) {
546 memset(incoming_msg, 0, sizeof(struct ipc_sms_incoming_msg_info));
549 if (list == ril_data.incoming_sms)
550 ril_data.incoming_sms = list->next;
552 list_head_free(list);
561 struct ipc_sms_incoming_msg_info *ipc_sms_incoming_msg_info_find(void)
563 struct ipc_sms_incoming_msg_info *incoming_msg;
564 struct list_head *list;
566 list = ril_data.incoming_sms;
567 while (list != NULL) {
568 incoming_msg = (struct ipc_sms_incoming_msg_info *) list->data;
569 if (incoming_msg == NULL)
581 void ipc_sms_incoming_msg_next(void)
583 struct ipc_sms_incoming_msg_info *incoming_msg;
585 ril_data.state.sms_incoming_msg_tpid = 0;
587 incoming_msg = ipc_sms_incoming_msg_info_find();
588 if (incoming_msg == NULL)
591 ipc_sms_incoming_msg_complete(incoming_msg->pdu, incoming_msg->length, incoming_msg->type, incoming_msg->tpid);
592 ipc_sms_incoming_msg_unregister(incoming_msg);
595 void ipc_sms_incoming_msg_complete(char *pdu, int length, unsigned char type, unsigned char tpid)
597 if (pdu == NULL || length <= 0)
600 ril_data.state.sms_incoming_msg_tpid = tpid;
602 if (type == IPC_SMS_TYPE_POINT_TO_POINT) {
603 ril_request_unsolicited(RIL_UNSOL_RESPONSE_NEW_SMS, pdu, length);
604 } else if (type == IPC_SMS_TYPE_STATUS_REPORT) {
605 ril_request_unsolicited(RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT, pdu, length);
607 LOGE("Unhandled message type: %x", type);
613 void ipc_sms_incoming_msg(struct ipc_message_info *info)
615 struct ipc_sms_incoming_msg *msg;
616 unsigned char *pdu_hex;
621 if (info == NULL || info->data == NULL || info->length < sizeof(struct ipc_sms_incoming_msg))
624 msg = (struct ipc_sms_incoming_msg *) info->data;
625 pdu_hex = ((unsigned char *) info->data + sizeof(struct ipc_sms_incoming_msg));
627 length = msg->length * 2 + 1;
628 pdu = (char *) calloc(1, length);
630 bin2hex(pdu_hex, msg->length, pdu);
632 if (ril_data.state.sms_incoming_msg_tpid != 0) {
633 LOGD("Another message is waiting ACK, queuing");
634 rc = ipc_sms_incoming_msg_register(pdu, length, msg->type, msg->msg_tpid);
636 LOGE("Unable to register incoming msg");
641 ipc_sms_incoming_msg_complete(pdu, length, msg->type, msg->msg_tpid);
647 ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
650 void ril_request_sms_acknowledge(RIL_Token t, void *data, size_t length)
652 struct ipc_sms_deliver_report_request report_msg;
653 int success, fail_cause;
655 if (data == NULL || length < 2 * sizeof(int))
658 success = ((int *) data)[0];
659 fail_cause = ((int *) data)[1];
661 if (ril_data.state.sms_incoming_msg_tpid == 0) {
662 LOGE("There is no SMS message to ACK!");
666 report_msg.type = IPC_SMS_TYPE_STATUS_REPORT;
667 report_msg.error = ril2ipc_sms_ack_error(success, fail_cause);
668 report_msg.msg_tpid = ril_data.state.sms_incoming_msg_tpid;
671 ipc_gen_phone_res_expect_to_abort(ril_request_get_id(t), IPC_SMS_DELIVER_REPORT);
673 ipc_fmt_send(IPC_SMS_DELIVER_REPORT, IPC_TYPE_EXEC, (void *) &report_msg, sizeof(report_msg), ril_request_get_id(t));
675 ipc_sms_incoming_msg_next();
680 ril_request_complete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
682 ipc_sms_incoming_msg_next();
685 void ipc_sms_deliver_report(struct ipc_message_info *info)
687 struct ipc_sms_deliver_report_response *report_msg;
691 if (info == NULL || info->data == NULL || info->length < sizeof(struct ipc_sms_deliver_report_response))
694 report_msg = (struct ipc_sms_deliver_report_response *) info->data;
695 e = ipc2ril_sms_ack_error(report_msg->error, &error_code);
697 ril_request_complete(ril_request_get_token(info->aseq), e, NULL, 0);
703 ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
707 * Apparently non-SMS-messages-related function
710 void ipc_sms_device_ready(struct ipc_message_info *info)
713 if (ril_data.state.radio_state == RADIO_STATE_ON) {
715 if (ril_data.state.radio_state == RADIO_STATE_SIM_READY) {
717 ipc_fmt_send(IPC_SMS_DEVICE_READY, IPC_TYPE_SET, NULL, 0, info->aseq);