3 * oFono - Open Source Telephony
5 * Copyright (C) 2008-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include <sys/types.h>
48 static GSList *g_drivers = NULL;
50 static void sim_own_numbers_update(struct ofono_sim *sim);
51 static void sim_pin_check(struct ofono_sim *sim);
52 static void sim_set_ready(struct ofono_sim *sim);
55 /* Contents of the SIM file system, in rough initialization order */
58 char **language_prefs;
60 unsigned char efli_length;
62 enum ofono_sim_password_type pin_type;
63 gboolean locked_pins[OFONO_SIM_PASSWORD_SIM_PUK]; /* Number of PINs */
65 enum ofono_sim_phase phase;
66 unsigned char mnc_length;
67 enum ofono_sim_cphs_phase cphs_phase;
68 unsigned char cphs_service_table[2];
70 unsigned char efust_length;
72 unsigned char efest_length;
74 unsigned char efsst_length;
75 gboolean fixed_dialing;
76 gboolean barred_dialing;
82 unsigned char efmsisdn_length;
83 unsigned char efmsisdn_records;
85 GSList *service_numbers;
89 unsigned short efimg_length;
91 enum ofono_sim_state state;
92 struct ofono_watchlist *state_watches;
96 unsigned char *iidf_image;
99 const struct ofono_sim_driver *driver;
101 struct ofono_atom *atom;
104 struct msisdn_set_request {
105 struct ofono_sim *sim;
111 struct service_number {
113 struct ofono_phone_number ph;
116 static const char *const passwd_name[] = {
117 [OFONO_SIM_PASSWORD_NONE] = "none",
118 [OFONO_SIM_PASSWORD_SIM_PIN] = "pin",
119 [OFONO_SIM_PASSWORD_SIM_PUK] = "puk",
120 [OFONO_SIM_PASSWORD_PHSIM_PIN] = "phone",
121 [OFONO_SIM_PASSWORD_PHFSIM_PIN] = "firstphone",
122 [OFONO_SIM_PASSWORD_PHFSIM_PUK] = "firstphonepuk",
123 [OFONO_SIM_PASSWORD_SIM_PIN2] = "pin2",
124 [OFONO_SIM_PASSWORD_SIM_PUK2] = "puk2",
125 [OFONO_SIM_PASSWORD_PHNET_PIN] = "network",
126 [OFONO_SIM_PASSWORD_PHNET_PUK] = "networkpuk",
127 [OFONO_SIM_PASSWORD_PHNETSUB_PIN] = "netsub",
128 [OFONO_SIM_PASSWORD_PHNETSUB_PUK] = "netsubpuk",
129 [OFONO_SIM_PASSWORD_PHSP_PIN] = "service",
130 [OFONO_SIM_PASSWORD_PHSP_PUK] = "servicepuk",
131 [OFONO_SIM_PASSWORD_PHCORP_PIN] = "corp",
132 [OFONO_SIM_PASSWORD_PHCORP_PUK] = "corppuk",
135 static const char *sim_passwd_name(enum ofono_sim_password_type type)
137 return passwd_name[type];
140 static enum ofono_sim_password_type sim_string_to_passwd(const char *name)
142 int len = sizeof(passwd_name) / sizeof(*passwd_name);
145 for (i = 0; i < len; i++)
146 if (!strcmp(passwd_name[i], name))
149 return OFONO_SIM_PASSWORD_INVALID;
152 static gboolean password_is_pin(enum ofono_sim_password_type type)
155 case OFONO_SIM_PASSWORD_SIM_PIN:
156 case OFONO_SIM_PASSWORD_PHSIM_PIN:
157 case OFONO_SIM_PASSWORD_PHFSIM_PIN:
158 case OFONO_SIM_PASSWORD_SIM_PIN2:
159 case OFONO_SIM_PASSWORD_PHNET_PIN:
160 case OFONO_SIM_PASSWORD_PHNETSUB_PIN:
161 case OFONO_SIM_PASSWORD_PHSP_PIN:
162 case OFONO_SIM_PASSWORD_PHCORP_PIN:
164 case OFONO_SIM_PASSWORD_SIM_PUK:
165 case OFONO_SIM_PASSWORD_PHFSIM_PUK:
166 case OFONO_SIM_PASSWORD_SIM_PUK2:
167 case OFONO_SIM_PASSWORD_PHNET_PUK:
168 case OFONO_SIM_PASSWORD_PHNETSUB_PUK:
169 case OFONO_SIM_PASSWORD_PHSP_PUK:
170 case OFONO_SIM_PASSWORD_PHCORP_PUK:
171 case OFONO_SIM_PASSWORD_INVALID:
172 case OFONO_SIM_PASSWORD_NONE:
179 static enum ofono_sim_password_type puk2pin(enum ofono_sim_password_type type)
182 case OFONO_SIM_PASSWORD_SIM_PUK:
183 return OFONO_SIM_PASSWORD_SIM_PIN;
184 case OFONO_SIM_PASSWORD_PHFSIM_PUK:
185 return OFONO_SIM_PASSWORD_PHFSIM_PIN;
186 case OFONO_SIM_PASSWORD_SIM_PUK2:
187 return OFONO_SIM_PASSWORD_SIM_PIN2;
188 case OFONO_SIM_PASSWORD_PHNET_PUK:
189 return OFONO_SIM_PASSWORD_PHNET_PUK;
190 case OFONO_SIM_PASSWORD_PHNETSUB_PUK:
191 return OFONO_SIM_PASSWORD_PHNETSUB_PIN;
192 case OFONO_SIM_PASSWORD_PHSP_PUK:
193 return OFONO_SIM_PASSWORD_PHSP_PIN;
194 case OFONO_SIM_PASSWORD_PHCORP_PUK:
195 return OFONO_SIM_PASSWORD_PHCORP_PIN;
197 return OFONO_SIM_PASSWORD_INVALID;
201 static char **get_own_numbers(GSList *own_numbers)
205 struct ofono_phone_number *num;
209 nelem = g_slist_length(own_numbers);
211 ret = g_new0(char *, nelem + 1);
214 for (l = own_numbers; l; l = l->next) {
217 ret[nelem++] = g_strdup(phone_number_to_string(num));
223 static char **get_locked_pins(struct ofono_sim *sim)
229 for (i = 1; i < OFONO_SIM_PASSWORD_SIM_PUK; i++) {
230 if (sim->locked_pins[i] == FALSE)
236 ret = g_new0(char *, nelem + 1);
240 for (i = 1; i < OFONO_SIM_PASSWORD_SIM_PUK; i++) {
241 if (sim->locked_pins[i] == FALSE)
244 ret[nelem] = g_strdup(sim_passwd_name(i));
251 static char **get_service_numbers(GSList *service_numbers)
255 struct service_number *num;
258 nelem = g_slist_length(service_numbers) * 2;
260 ret = g_new0(char *, nelem + 1);
263 for (l = service_numbers; l; l = l->next) {
266 ret[nelem++] = g_strdup(num->id);
267 ret[nelem++] = g_strdup(phone_number_to_string(&num->ph));
273 static void service_number_free(struct service_number *num)
279 static DBusMessage *sim_get_properties(DBusConnection *conn,
280 DBusMessage *msg, void *data)
282 struct ofono_sim *sim = data;
284 DBusMessageIter iter;
285 DBusMessageIter dict;
287 char **service_numbers;
289 const char *pin_name;
290 dbus_bool_t present = sim->state != OFONO_SIM_STATE_NOT_PRESENT;
294 reply = dbus_message_new_method_return(msg);
298 dbus_message_iter_init_append(reply, &iter);
300 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
301 OFONO_PROPERTIES_ARRAY_SIGNATURE,
304 ofono_dbus_dict_append(&dict, "Present", DBUS_TYPE_BOOLEAN, &present);
310 ofono_dbus_dict_append(&dict, "CardIdentifier",
311 DBUS_TYPE_STRING, &sim->iccid);
314 ofono_dbus_dict_append(&dict, "SubscriberIdentity",
315 DBUS_TYPE_STRING, &sim->imsi);
317 fdn = sim->fixed_dialing;
318 ofono_dbus_dict_append(&dict, "FixedDialing", DBUS_TYPE_BOOLEAN, &fdn);
320 bdn = sim->barred_dialing;
321 ofono_dbus_dict_append(&dict, "BarredDialing", DBUS_TYPE_BOOLEAN, &bdn);
323 if (sim->mnc_length && sim->imsi) {
324 char mcc[OFONO_MAX_MCC_LENGTH + 1];
325 char mnc[OFONO_MAX_MNC_LENGTH + 1];
328 strncpy(mcc, sim->imsi, OFONO_MAX_MCC_LENGTH);
329 mcc[OFONO_MAX_MCC_LENGTH] = '\0';
330 strncpy(mnc, sim->imsi + OFONO_MAX_MCC_LENGTH, sim->mnc_length);
331 mnc[sim->mnc_length] = '\0';
334 ofono_dbus_dict_append(&dict, "MobileCountryCode",
335 DBUS_TYPE_STRING, &str);
338 ofono_dbus_dict_append(&dict, "MobileNetworkCode",
339 DBUS_TYPE_STRING, &str);
342 own_numbers = get_own_numbers(sim->own_numbers);
344 ofono_dbus_dict_append_array(&dict, "SubscriberNumbers",
345 DBUS_TYPE_STRING, &own_numbers);
346 g_strfreev(own_numbers);
348 locked_pins = get_locked_pins(sim);
349 ofono_dbus_dict_append_array(&dict, "LockedPins",
350 DBUS_TYPE_STRING, &locked_pins);
351 g_strfreev(locked_pins);
353 if (sim->service_numbers && sim->sdn_ready) {
354 service_numbers = get_service_numbers(sim->service_numbers);
356 ofono_dbus_dict_append_dict(&dict, "ServiceNumbers",
359 g_strfreev(service_numbers);
362 if (sim->language_prefs)
363 ofono_dbus_dict_append_array(&dict, "PreferredLanguages",
365 &sim->language_prefs);
367 pin_name = sim_passwd_name(sim->pin_type);
368 ofono_dbus_dict_append(&dict, "PinRequired",
373 dbus_message_iter_close_container(&iter, &dict);
378 static void msisdn_set_done(struct msisdn_set_request *req)
383 reply = __ofono_error_failed(req->msg);
385 reply = dbus_message_new_method_return(req->msg);
387 __ofono_dbus_pending_reply(&req->msg, reply);
389 /* Re-read the numbers and emit signal if needed */
390 sim_own_numbers_update(req->sim);
395 static void msisdn_set_cb(int ok, void *data)
397 struct msisdn_set_request *req = data;
405 msisdn_set_done(req);
408 static gboolean set_own_numbers(struct ofono_sim *sim,
409 GSList *new_numbers, DBusMessage *msg)
411 struct msisdn_set_request *req;
413 unsigned char efmsisdn[255];
414 struct ofono_phone_number *number;
416 if (new_numbers && g_slist_length(new_numbers) > sim->efmsisdn_records)
419 req = g_new0(struct msisdn_set_request, 1);
422 req->msg = dbus_message_ref(msg);
424 for (record = 1; record <= sim->efmsisdn_records; record++) {
426 number = new_numbers->data;
427 sim_adn_build(efmsisdn, sim->efmsisdn_length,
429 new_numbers = new_numbers->next;
431 memset(efmsisdn, 0xff, sim->efmsisdn_length);
434 if (ofono_sim_write(req->sim, SIM_EFMSISDN_FILEID,
435 msisdn_set_cb, OFONO_SIM_FILE_STRUCTURE_FIXED,
437 sim->efmsisdn_length, req) == 0)
444 msisdn_set_done(req);
449 static DBusMessage *sim_set_property(DBusConnection *conn, DBusMessage *msg,
452 struct ofono_sim *sim = data;
453 DBusMessageIter iter;
455 DBusMessageIter var_elem;
456 const char *name, *value;
458 if (!dbus_message_iter_init(msg, &iter))
459 return __ofono_error_invalid_args(msg);
461 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
462 return __ofono_error_invalid_args(msg);
464 dbus_message_iter_get_basic(&iter, &name);
466 if (!strcmp(name, "SubscriberNumbers")) {
467 gboolean set_ok = FALSE;
468 struct ofono_phone_number *own;
469 GSList *own_numbers = NULL;
471 if (sim->efmsisdn_length == 0)
472 return __ofono_error_busy(msg);
474 dbus_message_iter_next(&iter);
476 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
477 return __ofono_error_invalid_args(msg);
479 dbus_message_iter_recurse(&iter, &var);
481 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_ARRAY ||
482 dbus_message_iter_get_element_type(&var) !=
484 return __ofono_error_invalid_args(msg);
486 dbus_message_iter_recurse(&var, &var_elem);
488 /* Empty lists are supported */
489 while (dbus_message_iter_get_arg_type(&var_elem) !=
491 if (dbus_message_iter_get_arg_type(&var_elem) !=
495 dbus_message_iter_get_basic(&var_elem, &value);
497 if (!valid_phone_number_format(value))
500 own = g_new0(struct ofono_phone_number, 1);
501 string_to_phone_number(value, own);
503 own_numbers = g_slist_prepend(own_numbers, own);
505 dbus_message_iter_next(&var_elem);
508 own_numbers = g_slist_reverse(own_numbers);
509 set_ok = set_own_numbers(sim, own_numbers, msg);
512 g_slist_foreach(own_numbers, (GFunc) g_free, 0);
513 g_slist_free(own_numbers);
519 return __ofono_error_invalid_args(msg);
522 static void sim_locked_cb(struct ofono_sim *sim, gboolean locked)
524 DBusConnection *conn = ofono_dbus_get_connection();
525 const char *path = __ofono_atom_get_path(sim->atom);
529 enum ofono_sim_password_type type;
532 reply = dbus_message_new_method_return(sim->pending);
534 dbus_message_get_args(sim->pending, NULL, DBUS_TYPE_STRING, &typestr,
535 DBUS_TYPE_STRING, &pin,
538 type = sim_string_to_passwd(typestr);
540 /* This is used by lock/unlock pin, no puks allowed */
541 sim->locked_pins[type] = locked;
542 __ofono_dbus_pending_reply(&sim->pending, reply);
544 locked_pins = get_locked_pins(sim);
545 ofono_dbus_signal_array_property_changed(conn, path,
546 OFONO_SIM_MANAGER_INTERFACE,
547 "LockedPins", DBUS_TYPE_STRING,
549 g_strfreev(locked_pins);
552 static void sim_unlock_cb(const struct ofono_error *error, void *data)
554 struct ofono_sim *sim = data;
556 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
557 DBusMessage *reply = __ofono_error_failed(sim->pending);
558 __ofono_dbus_pending_reply(&sim->pending, reply);
562 sim_locked_cb(sim, FALSE);
565 static void sim_lock_cb(const struct ofono_error *error, void *data)
567 struct ofono_sim *sim = data;
569 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
570 DBusMessage *reply = __ofono_error_failed(sim->pending);
571 __ofono_dbus_pending_reply(&sim->pending, reply);
575 sim_locked_cb(sim, TRUE);
578 static DBusMessage *sim_lock_or_unlock(struct ofono_sim *sim, int lock,
579 DBusConnection *conn, DBusMessage *msg)
581 enum ofono_sim_password_type type;
585 if (!sim->driver->lock)
586 return __ofono_error_not_implemented(msg);
589 return __ofono_error_busy(msg);
591 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &typestr,
592 DBUS_TYPE_STRING, &pin,
593 DBUS_TYPE_INVALID) == FALSE)
594 return __ofono_error_invalid_args(msg);
596 type = sim_string_to_passwd(typestr);
599 * SIM PIN2 cannot be locked / unlocked according to 27.007,
600 * however the PIN combination can be changed
602 if (password_is_pin(type) == FALSE ||
603 type == OFONO_SIM_PASSWORD_SIM_PIN2)
604 return __ofono_error_invalid_format(msg);
606 if (!is_valid_pin(pin, PIN_TYPE_PIN))
607 return __ofono_error_invalid_format(msg);
609 sim->pending = dbus_message_ref(msg);
611 sim->driver->lock(sim, type, lock, pin,
612 lock ? sim_lock_cb : sim_unlock_cb, sim);
617 static DBusMessage *sim_lock_pin(DBusConnection *conn, DBusMessage *msg,
620 struct ofono_sim *sim = data;
622 return sim_lock_or_unlock(sim, 1, conn, msg);
625 static DBusMessage *sim_unlock_pin(DBusConnection *conn, DBusMessage *msg,
628 struct ofono_sim *sim = data;
630 return sim_lock_or_unlock(sim, 0, conn, msg);
633 static void sim_change_pin_cb(const struct ofono_error *error, void *data)
635 struct ofono_sim *sim = data;
637 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
638 __ofono_dbus_pending_reply(&sim->pending,
639 __ofono_error_failed(sim->pending));
643 __ofono_dbus_pending_reply(&sim->pending,
644 dbus_message_new_method_return(sim->pending));
647 static DBusMessage *sim_change_pin(DBusConnection *conn, DBusMessage *msg,
650 struct ofono_sim *sim = data;
651 enum ofono_sim_password_type type;
656 if (!sim->driver->change_passwd)
657 return __ofono_error_not_implemented(msg);
660 return __ofono_error_busy(msg);
662 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &typestr,
663 DBUS_TYPE_STRING, &old,
664 DBUS_TYPE_STRING, &new,
665 DBUS_TYPE_INVALID) == FALSE)
666 return __ofono_error_invalid_args(msg);
668 type = sim_string_to_passwd(typestr);
670 if (password_is_pin(type) == FALSE)
671 return __ofono_error_invalid_format(msg);
673 if (!is_valid_pin(old, PIN_TYPE_PIN))
674 return __ofono_error_invalid_format(msg);
676 if (!is_valid_pin(new, PIN_TYPE_PIN))
677 return __ofono_error_invalid_format(msg);
679 if (!strcmp(new, old))
680 return dbus_message_new_method_return(msg);
682 sim->pending = dbus_message_ref(msg);
683 sim->driver->change_passwd(sim, type, old, new,
684 sim_change_pin_cb, sim);
689 static void sim_enter_pin_cb(const struct ofono_error *error, void *data)
691 struct ofono_sim *sim = data;
694 if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
695 reply = __ofono_error_failed(sim->pending);
697 reply = dbus_message_new_method_return(sim->pending);
699 __ofono_dbus_pending_reply(&sim->pending, reply);
704 static DBusMessage *sim_enter_pin(DBusConnection *conn, DBusMessage *msg,
707 struct ofono_sim *sim = data;
709 enum ofono_sim_password_type type;
712 if (!sim->driver->send_passwd)
713 return __ofono_error_not_implemented(msg);
716 return __ofono_error_busy(msg);
718 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &typestr,
719 DBUS_TYPE_STRING, &pin,
720 DBUS_TYPE_INVALID) == FALSE)
721 return __ofono_error_invalid_args(msg);
723 type = sim_string_to_passwd(typestr);
725 if (type == OFONO_SIM_PASSWORD_NONE || type != sim->pin_type)
726 return __ofono_error_invalid_format(msg);
728 if (!is_valid_pin(pin, PIN_TYPE_PIN))
729 return __ofono_error_invalid_format(msg);
731 sim->pending = dbus_message_ref(msg);
732 sim->driver->send_passwd(sim, pin, sim_enter_pin_cb, sim);
737 static void sim_get_image_cb(struct ofono_sim *sim,
738 unsigned char id, char *xpm, gboolean cache)
741 DBusMessageIter iter, array;
745 reply = __ofono_error_failed(sim->pending);
746 __ofono_dbus_pending_reply(&sim->pending, reply);
750 xpm_len = strlen(xpm);
752 reply = dbus_message_new_method_return(sim->pending);
753 dbus_message_iter_init_append(reply, &iter);
755 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
756 DBUS_TYPE_BYTE_AS_STRING, &array);
758 dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
760 dbus_message_iter_close_container(&iter, &array);
762 __ofono_dbus_pending_reply(&sim->pending, reply);
765 sim_fs_cache_image(sim->simfs, (const char *) xpm, id);
770 static void sim_iidf_read_clut_cb(int ok, int length, int record,
771 const unsigned char *data,
772 int record_length, void *userdata)
774 struct ofono_sim *sim = userdata;
776 unsigned char *efimg;
777 unsigned short iidf_len;
778 unsigned short clut_len;
783 dbus_message_get_args(sim->pending, NULL, DBUS_TYPE_BYTE, &id,
786 efimg = &sim->efimg[id * 9];
789 sim_get_image_cb(sim, id, NULL, FALSE);
793 iidf_len = efimg[7] << 8 | efimg[8];
795 if (sim->iidf_image[3] == 0)
798 clut_len = sim->iidf_image[3] * 3;
800 xpm = stk_image_to_xpm(sim->iidf_image, iidf_len, efimg[2],
802 sim_get_image_cb(sim, id, xpm, TRUE);
805 g_free(sim->iidf_image);
806 sim->iidf_image = NULL;
809 static void sim_iidf_read_cb(int ok, int length, int record,
810 const unsigned char *data,
811 int record_length, void *userdata)
813 struct ofono_sim *sim = userdata;
815 unsigned char *efimg;
816 unsigned short iidf_id;
817 unsigned short offset;
818 unsigned short clut_len;
822 dbus_message_get_args(sim->pending, NULL, DBUS_TYPE_BYTE, &id,
825 efimg = &sim->efimg[id * 9];
828 sim_get_image_cb(sim, id, NULL, FALSE);
832 if (efimg[2] == STK_IMG_SCHEME_BASIC) {
833 char *xpm = stk_image_to_xpm(data, length, efimg[2], NULL, 0);
834 sim_get_image_cb(sim, id, xpm, TRUE);
838 offset = data[4] << 8 | data[5];
843 clut_len = data[3] * 3;
845 iidf_id = efimg[3] << 8 | efimg[4];
846 sim->iidf_image = g_memdup(data, length);
848 /* read the clut data */
849 ofono_sim_read_bytes(sim, iidf_id, offset, clut_len,
850 sim_iidf_read_clut_cb, sim);
853 static void sim_get_image(struct ofono_sim *sim, unsigned char id,
856 unsigned char *efimg;
858 unsigned short iidf_id;
859 unsigned short iidf_offset;
860 unsigned short iidf_len;
862 image = sim_fs_get_cached_image(sim->simfs, id);
865 sim_get_image_cb(sim, id, image, FALSE);
869 if (sim->efimg_length <= (id * 9)) {
870 sim_get_image_cb(sim, id, NULL, FALSE);
874 efimg = &sim->efimg[id * 9];
876 iidf_id = efimg[3] << 8 | efimg[4];
877 iidf_offset = efimg[5] << 8 | efimg[6];
878 iidf_len = efimg[7] << 8 | efimg[8];
880 /* read the image data */
881 ofono_sim_read_bytes(sim, iidf_id, iidf_offset, iidf_len,
882 sim_iidf_read_cb, sim);
885 static DBusMessage *sim_get_icon(DBusConnection *conn,
886 DBusMessage *msg, void *data)
888 struct ofono_sim *sim = data;
891 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_BYTE, &id,
892 DBUS_TYPE_INVALID) == FALSE)
893 return __ofono_error_invalid_args(msg);
895 /* zero means no icon */
897 return __ofono_error_invalid_args(msg);
900 return __ofono_error_busy(msg);
902 if (sim->efimg == NULL)
903 return __ofono_error_not_implemented(msg);
905 sim->pending = dbus_message_ref(msg);
907 sim_get_image(sim, id - 1, sim);
912 static DBusMessage *sim_reset_pin(DBusConnection *conn, DBusMessage *msg,
915 struct ofono_sim *sim = data;
917 enum ofono_sim_password_type type;
921 if (!sim->driver->reset_passwd)
922 return __ofono_error_not_implemented(msg);
925 return __ofono_error_busy(msg);
927 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &typestr,
928 DBUS_TYPE_STRING, &puk,
929 DBUS_TYPE_STRING, &pin,
930 DBUS_TYPE_INVALID) == FALSE)
931 return __ofono_error_invalid_args(msg);
933 type = sim_string_to_passwd(typestr);
935 if (type == OFONO_SIM_PASSWORD_NONE || type != sim->pin_type)
936 return __ofono_error_invalid_format(msg);
938 if (!is_valid_pin(puk, PIN_TYPE_PUK))
939 return __ofono_error_invalid_format(msg);
941 if (!is_valid_pin(pin, PIN_TYPE_PIN))
942 return __ofono_error_invalid_format(msg);
944 sim->pending = dbus_message_ref(msg);
945 sim->driver->reset_passwd(sim, puk, pin, sim_enter_pin_cb, sim);
950 static GDBusMethodTable sim_methods[] = {
951 { "GetProperties", "", "a{sv}", sim_get_properties },
952 { "SetProperty", "sv", "", sim_set_property,
953 G_DBUS_METHOD_FLAG_ASYNC },
954 { "ChangePin", "sss", "", sim_change_pin,
955 G_DBUS_METHOD_FLAG_ASYNC },
956 { "EnterPin", "ss", "", sim_enter_pin,
957 G_DBUS_METHOD_FLAG_ASYNC },
958 { "ResetPin", "sss", "", sim_reset_pin,
959 G_DBUS_METHOD_FLAG_ASYNC },
960 { "LockPin", "ss", "", sim_lock_pin,
961 G_DBUS_METHOD_FLAG_ASYNC },
962 { "UnlockPin", "ss", "", sim_unlock_pin,
963 G_DBUS_METHOD_FLAG_ASYNC },
964 { "GetIcon", "y", "ay", sim_get_icon,
965 G_DBUS_METHOD_FLAG_ASYNC },
969 static GDBusSignalTable sim_signals[] = {
970 { "PropertyChanged", "sv" },
974 static gboolean numbers_list_equal(GSList *a, GSList *b)
976 struct ofono_phone_number *num_a, *num_b;
985 if (!g_str_equal(num_a->number, num_b->number) ||
986 num_a->type != num_b->type)
996 static void sim_msisdn_read_cb(int ok, int length, int record,
997 const unsigned char *data,
998 int record_length, void *userdata)
1000 struct ofono_sim *sim = userdata;
1002 struct ofono_phone_number ph;
1007 if (record_length < 14 || length < record_length)
1010 total = length / record_length;
1012 sim->efmsisdn_length = record_length;
1013 sim->efmsisdn_records = total;
1015 if (sim_adn_parse(data, record_length, &ph, NULL) == TRUE) {
1016 struct ofono_phone_number *own;
1018 own = g_new(struct ofono_phone_number, 1);
1019 memcpy(own, &ph, sizeof(struct ofono_phone_number));
1020 sim->new_numbers = g_slist_prepend(sim->new_numbers, own);
1023 if (record != total)
1027 /* All records retrieved */
1028 if (sim->new_numbers)
1029 sim->new_numbers = g_slist_reverse(sim->new_numbers);
1031 if (!numbers_list_equal(sim->new_numbers, sim->own_numbers)) {
1032 const char *path = __ofono_atom_get_path(sim->atom);
1034 DBusConnection *conn = ofono_dbus_get_connection();
1036 g_slist_foreach(sim->own_numbers, (GFunc) g_free, NULL);
1037 g_slist_free(sim->own_numbers);
1038 sim->own_numbers = sim->new_numbers;
1040 own_numbers = get_own_numbers(sim->own_numbers);
1042 ofono_dbus_signal_array_property_changed(conn, path,
1043 OFONO_SIM_MANAGER_INTERFACE,
1044 "SubscriberNumbers",
1045 DBUS_TYPE_STRING, &own_numbers);
1047 g_strfreev(own_numbers);
1049 g_slist_foreach(sim->new_numbers, (GFunc) g_free, NULL);
1050 g_slist_free(sim->new_numbers);
1053 sim->new_numbers = NULL;
1056 static gint service_number_compare(gconstpointer a, gconstpointer b)
1058 const struct service_number *sdn = a;
1061 return strcmp(sdn->id, id);
1064 static void sim_sdn_read_cb(int ok, int length, int record,
1065 const unsigned char *data,
1066 int record_length, void *userdata)
1068 struct ofono_sim *sim = userdata;
1069 DBusConnection *conn = ofono_dbus_get_connection();
1070 const char *path = __ofono_atom_get_path(sim->atom);
1072 struct ofono_phone_number ph;
1074 struct service_number *sdn;
1079 if (record_length < 14 || length < record_length)
1082 total = length / record_length;
1084 if (sim_adn_parse(data, record_length, &ph, &alpha) == FALSE)
1088 /* Use phone number if Id is unavailable */
1089 if (alpha && alpha[0] == '\0') {
1095 alpha = g_strdup(phone_number_to_string(&ph));
1097 if (sim->service_numbers &&
1098 g_slist_find_custom(sim->service_numbers,
1099 alpha, service_number_compare)) {
1100 ofono_error("Duplicate EFsdn entries for `%s'\n",
1107 sdn = g_new(struct service_number, 1);
1109 memcpy(&sdn->ph, &ph, sizeof(struct ofono_phone_number));
1111 sim->service_numbers = g_slist_prepend(sim->service_numbers, sdn);
1114 if (record != total)
1118 /* All records retrieved */
1119 if (sim->service_numbers) {
1120 char **service_numbers;
1122 sim->service_numbers = g_slist_reverse(sim->service_numbers);
1123 sim->sdn_ready = TRUE;
1125 service_numbers = get_service_numbers(sim->service_numbers);
1127 ofono_dbus_signal_dict_property_changed(conn, path,
1128 OFONO_SIM_MANAGER_INTERFACE,
1132 g_strfreev(service_numbers);
1136 static void sim_own_numbers_update(struct ofono_sim *sim)
1138 ofono_sim_read(sim, SIM_EFMSISDN_FILEID, OFONO_SIM_FILE_STRUCTURE_FIXED,
1139 sim_msisdn_read_cb, sim);
1142 static void sim_efimg_read_cb(int ok, int length, int record,
1143 const unsigned char *data,
1144 int record_length, void *userdata)
1146 struct ofono_sim *sim = userdata;
1147 unsigned char *efimg;
1153 num_records = length / record_length;
1156 * EFimg descriptors are 9 bytes long.
1157 * Byte 1 of the record is the number of descriptors per record.
1159 if ((record_length < 10) ||
1160 ((record_length % 9 != 2) && (record_length % 9 != 1)))
1163 if (sim->efimg == NULL) {
1164 sim->efimg = g_try_malloc0(num_records * 9);
1166 if (sim->efimg == NULL)
1169 sim->efimg_length = num_records * 9;
1173 * TBD - if we have more than one descriptor per record,
1174 * pick the nicest one. For now we use the first one.
1177 /* copy descriptor into slot for this record */
1178 efimg = &sim->efimg[(record - 1) * 9];
1180 memcpy(efimg, &data[1], 9);
1183 static void sim_ready(enum ofono_sim_state new_state, void *user)
1185 struct ofono_sim *sim = user;
1187 if (new_state != OFONO_SIM_STATE_READY)
1190 sim_own_numbers_update(sim);
1192 ofono_sim_read(sim, SIM_EFSDN_FILEID, OFONO_SIM_FILE_STRUCTURE_FIXED,
1193 sim_sdn_read_cb, sim);
1194 ofono_sim_read(sim, SIM_EFIMG_FILEID, OFONO_SIM_FILE_STRUCTURE_FIXED,
1195 sim_efimg_read_cb, sim);
1198 static void sim_imsi_cb(const struct ofono_error *error, const char *imsi,
1201 struct ofono_sim *sim = data;
1202 DBusConnection *conn = ofono_dbus_get_connection();
1203 const char *path = __ofono_atom_get_path(sim->atom);
1205 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
1206 ofono_error("Unable to read IMSI, emergency calls only");
1210 sim->imsi = g_strdup(imsi);
1212 ofono_dbus_signal_property_changed(conn, path,
1213 OFONO_SIM_MANAGER_INTERFACE,
1214 "SubscriberIdentity",
1215 DBUS_TYPE_STRING, &sim->imsi);
1217 if (sim->mnc_length) {
1218 char mcc[OFONO_MAX_MCC_LENGTH + 1];
1219 char mnc[OFONO_MAX_MNC_LENGTH + 1];
1222 strncpy(mcc, sim->imsi, OFONO_MAX_MCC_LENGTH);
1223 mcc[OFONO_MAX_MCC_LENGTH] = '\0';
1224 strncpy(mnc, sim->imsi + OFONO_MAX_MCC_LENGTH, sim->mnc_length);
1225 mnc[sim->mnc_length] = '\0';
1228 ofono_dbus_signal_property_changed(conn, path,
1229 OFONO_SIM_MANAGER_INTERFACE,
1230 "MobileCountryCode",
1231 DBUS_TYPE_STRING, &str);
1234 ofono_dbus_signal_property_changed(conn, path,
1235 OFONO_SIM_MANAGER_INTERFACE,
1236 "MobileNetworkCode",
1237 DBUS_TYPE_STRING, &str);
1243 static void sim_retrieve_imsi(struct ofono_sim *sim)
1245 if (!sim->driver->read_imsi) {
1246 ofono_error("IMSI retrieval not implemented,"
1247 " only emergency calls will be available");
1251 sim->driver->read_imsi(sim, sim_imsi_cb, sim);
1254 static void sim_fdn_enabled(struct ofono_sim *sim)
1256 DBusConnection *conn = ofono_dbus_get_connection();
1257 const char *path = __ofono_atom_get_path(sim->atom);
1260 sim->fixed_dialing = TRUE;
1262 val = sim->fixed_dialing;
1263 ofono_dbus_signal_property_changed(conn, path,
1264 OFONO_SIM_MANAGER_INTERFACE,
1266 DBUS_TYPE_BOOLEAN, &val);
1269 static void sim_bdn_enabled(struct ofono_sim *sim)
1271 DBusConnection *conn = ofono_dbus_get_connection();
1272 const char *path = __ofono_atom_get_path(sim->atom);
1275 sim->barred_dialing = TRUE;
1277 val = sim->barred_dialing;
1278 ofono_dbus_signal_property_changed(conn, path,
1279 OFONO_SIM_MANAGER_INTERFACE,
1281 DBUS_TYPE_BOOLEAN, &val);
1284 static void sim_efbdn_info_read_cb(int ok, unsigned char file_status,
1285 int total_length, int record_length,
1288 struct ofono_sim *sim = userdata;
1293 if (file_status & SIM_FILE_STATUS_VALID)
1294 sim_bdn_enabled(sim);
1297 if (sim->fixed_dialing != TRUE &&
1298 sim->barred_dialing != TRUE)
1299 sim_retrieve_imsi(sim);
1302 static gboolean check_bdn_status(struct ofono_sim *sim)
1305 * Check the status of Barred Dialing in the SIM-card
1306 * (TS 11.11/TS 51.011, Section 11.5.1: BDN capability request).
1307 * If BDN is allocated, activated in EFsst and EFbdn is validated,
1308 * halt the SIM initialization.
1310 if (sim_sst_is_active(sim->efsst, sim->efsst_length,
1311 SIM_SST_SERVICE_BDN)) {
1312 sim_fs_read_info(sim->simfs, SIM_EFBDN_FILEID,
1313 OFONO_SIM_FILE_STRUCTURE_FIXED,
1314 sim_efbdn_info_read_cb, sim);
1321 static void sim_efadn_info_read_cb(int ok, unsigned char file_status,
1322 int total_length, int record_length,
1325 struct ofono_sim *sim = userdata;
1330 if (!(file_status & SIM_FILE_STATUS_VALID))
1331 sim_fdn_enabled(sim);
1334 if (check_bdn_status(sim) != TRUE) {
1335 if (sim->fixed_dialing != TRUE &&
1336 sim->barred_dialing != TRUE)
1337 sim_retrieve_imsi(sim);
1341 static void sim_efsst_read_cb(int ok, int length, int record,
1342 const unsigned char *data,
1343 int record_length, void *userdata)
1345 struct ofono_sim *sim = userdata;
1351 ofono_error("EFsst shall contain at least two bytes");
1355 sim->efsst = g_memdup(data, length);
1356 sim->efsst_length = length;
1359 * Check if Fixed Dialing is enabled in the SIM-card
1360 * (TS 11.11/TS 51.011, Section 11.5.1: FDN capability request).
1361 * If FDN is activated and ADN is invalidated,
1362 * don't continue initialization routine.
1364 if (sim_sst_is_active(sim->efsst, sim->efsst_length,
1365 SIM_SST_SERVICE_FDN)) {
1366 sim_fs_read_info(sim->simfs, SIM_EFADN_FILEID,
1367 OFONO_SIM_FILE_STRUCTURE_FIXED,
1368 sim_efadn_info_read_cb, sim);
1372 if (check_bdn_status(sim) == TRUE)
1376 sim_retrieve_imsi(sim);
1379 static void sim_efest_read_cb(int ok, int length, int record,
1380 const unsigned char *data,
1381 int record_length, void *userdata)
1383 struct ofono_sim *sim = userdata;
1389 ofono_error("EFest shall contain at least one byte");
1393 sim->efest = g_memdup(data, length);
1394 sim->efest_length = length;
1397 * Check if Fixed Dialing is enabled in the USIM-card
1398 * (TS 31.102, Section 5.3.2: FDN capability request).
1399 * If FDN is activated, don't continue initialization routine.
1401 if (sim_est_is_active(sim->efest, sim->efest_length,
1402 SIM_EST_SERVICE_FDN))
1403 sim_fdn_enabled(sim);
1406 * Check the status of Barred Dialing in the USIM-card
1407 * (TS 31.102, Section 5.3.2: BDN capability request).
1408 * If BDN service is enabled, halt the USIM initialization.
1410 if (sim_est_is_active(sim->efest, sim->efest_length,
1411 SIM_EST_SERVICE_BDN))
1412 sim_bdn_enabled(sim);
1415 if (sim->fixed_dialing != TRUE &&
1416 sim->barred_dialing != TRUE)
1417 sim_retrieve_imsi(sim);
1420 static void sim_efust_read_cb(int ok, int length, int record,
1421 const unsigned char *data,
1422 int record_length, void *userdata)
1424 struct ofono_sim *sim = userdata;
1430 ofono_error("EFust shall contain at least one byte");
1434 sim->efust = g_memdup(data, length);
1435 sim->efust_length = length;
1438 * Check whether the SIM provides EFest file
1439 * According to 31.102, section 4.2.24 and 4.2.44 the EFest file
1440 * must be present if EFfdn or EFbdn are present
1442 if (sim_ust_is_available(sim->efust, sim->efust_length,
1443 SIM_UST_SERVICE_ENABLED_SERVICE_TABLE)) {
1444 ofono_sim_read(sim, SIM_EFEST_FILEID,
1445 OFONO_SIM_FILE_STRUCTURE_TRANSPARENT,
1446 sim_efest_read_cb, sim);
1452 sim_retrieve_imsi(sim);
1455 static void sim_cphs_information_read_cb(int ok, int length, int record,
1456 const unsigned char *data,
1457 int record_length, void *userdata)
1459 struct ofono_sim *sim = userdata;
1461 sim->cphs_phase = OFONO_SIM_CPHS_PHASE_NONE;
1463 if (!ok || length < 3)
1466 if (data[0] == 0x01)
1467 sim->cphs_phase = OFONO_SIM_CPHS_PHASE_1G;
1468 else if (data[0] >= 0x02)
1469 sim->cphs_phase = OFONO_SIM_CPHS_PHASE_2G;
1471 memcpy(sim->cphs_service_table, data + 1, 2);
1474 static void sim_ad_read_cb(int ok, int length, int record,
1475 const unsigned char *data,
1476 int record_length, void *userdata)
1478 struct ofono_sim *sim = userdata;
1487 new_mnc_length = data[3] & 0xf;
1489 /* sanity check for potential invalid values */
1490 if (new_mnc_length < 2 || new_mnc_length > 3)
1493 sim->mnc_length = new_mnc_length;
1496 static void sim_efphase_read_cb(int ok, int length, int record,
1497 const unsigned char *data,
1498 int record_length, void *userdata)
1500 struct ofono_sim *sim = userdata;
1502 if (!ok || length != 1) {
1503 sim->phase = OFONO_SIM_PHASE_3G;
1505 ofono_sim_read(sim, SIM_EFUST_FILEID,
1506 OFONO_SIM_FILE_STRUCTURE_TRANSPARENT,
1507 sim_efust_read_cb, sim);
1514 sim->phase = OFONO_SIM_PHASE_1G;
1517 sim->phase = OFONO_SIM_PHASE_2G;
1520 sim->phase = OFONO_SIM_PHASE_2G_PLUS;
1523 ofono_error("Unknown phase");
1527 ofono_sim_read(sim, SIM_EFSST_FILEID,
1528 OFONO_SIM_FILE_STRUCTURE_TRANSPARENT,
1529 sim_efsst_read_cb, sim);
1532 static void sim_initialize_after_pin(struct ofono_sim *sim)
1534 ofono_sim_read(sim, SIM_EFPHASE_FILEID,
1535 OFONO_SIM_FILE_STRUCTURE_TRANSPARENT,
1536 sim_efphase_read_cb, sim);
1538 ofono_sim_read(sim, SIM_EFAD_FILEID,
1539 OFONO_SIM_FILE_STRUCTURE_TRANSPARENT,
1540 sim_ad_read_cb, sim);
1543 * Read CPHS-support bits, this is still part of the SIM
1544 * initialisation but no order is specified for it.
1546 ofono_sim_read(sim, SIM_EF_CPHS_INFORMATION_FILEID,
1547 OFONO_SIM_FILE_STRUCTURE_TRANSPARENT,
1548 sim_cphs_information_read_cb, sim);
1551 static void sim_pin_query_cb(const struct ofono_error *error,
1552 enum ofono_sim_password_type pin_type,
1555 struct ofono_sim *sim = data;
1556 DBusConnection *conn = ofono_dbus_get_connection();
1557 const char *path = __ofono_atom_get_path(sim->atom);
1558 const char *pin_name;
1560 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
1561 ofono_error("Querying PIN authentication state failed");
1566 if (sim->pin_type != pin_type) {
1567 sim->pin_type = pin_type;
1568 pin_name = sim_passwd_name(pin_type);
1570 if (pin_type != OFONO_SIM_PASSWORD_NONE &&
1571 password_is_pin(pin_type) == FALSE)
1572 pin_type = puk2pin(pin_type);
1574 if (pin_type != OFONO_SIM_PASSWORD_INVALID)
1575 sim->locked_pins[pin_type] = TRUE;
1577 ofono_dbus_signal_property_changed(conn, path,
1578 OFONO_SIM_MANAGER_INTERFACE,
1579 "PinRequired", DBUS_TYPE_STRING,
1584 if (pin_type == OFONO_SIM_PASSWORD_NONE)
1585 sim_initialize_after_pin(sim);
1588 static void sim_pin_check(struct ofono_sim *sim)
1590 if (!sim->driver->query_passwd_state) {
1591 sim_initialize_after_pin(sim);
1595 sim->driver->query_passwd_state(sim, sim_pin_query_cb, sim);
1598 static void sim_efli_read_cb(int ok, int length, int record,
1599 const unsigned char *data,
1600 int record_length, void *userdata)
1602 struct ofono_sim *sim = userdata;
1607 sim->efli = g_memdup(data, length);
1608 sim->efli_length = length;
1611 /* Detect whether the file is in EFli format, as opposed to 51.011 EFlp */
1612 static gboolean sim_efli_format(const unsigned char *ef, int length)
1619 for (i = 0; i < length; i += 2) {
1620 if (ef[i] == 0xff && ef[i+1] == 0xff)
1624 * ISO 639 country codes are each two lower-case SMS 7-bit
1625 * characters while CB DCS language codes are in ranges
1626 * (0 - 15) or (32 - 47), so the ranges don't overlap
1628 if (g_ascii_isalpha(ef[i]) == 0)
1631 if (g_ascii_isalpha(ef[i+1]) == 0)
1638 static GSList *parse_language_list(const unsigned char *ef, int length)
1643 for (i = 0; i < length; i += 2) {
1644 if (ef[i] > 0x7f || ef[i+1] > 0x7f)
1648 * ISO 639 codes contain only characters that are coded
1649 * identically in SMS 7 bit charset, ASCII or UTF8 so
1652 ret = g_slist_prepend(ret, g_ascii_strdown((char *)ef + i, 2));
1656 ret = g_slist_reverse(ret);
1661 static GSList *parse_eflp(const unsigned char *eflp, int length)
1667 for (i = 0; i < length; i++) {
1668 if (iso639_2_from_language(eflp[i], code) == FALSE)
1671 ret = g_slist_prepend(ret, g_strdup(code));
1675 ret = g_slist_reverse(ret);
1680 static char **concat_lang_prefs(GSList *a, GSList *b)
1685 int total = g_slist_length(a) + g_slist_length(b);
1690 ret = g_new0(char *, total + 1);
1692 for (l = a; l; l = l->next)
1693 ret[i++] = g_strdup(l->data);
1695 for (l = b; l; l = l->next) {
1696 gboolean duplicate = FALSE;
1698 for (k = a; k; k = k->next)
1699 if (!strcmp(k->data, l->data))
1705 ret[i++] = g_strdup(l->data);
1711 static void sim_efpl_read_cb(int ok, int length, int record,
1712 const unsigned char *data,
1713 int record_length, void *userdata)
1715 struct ofono_sim *sim = userdata;
1716 const char *path = __ofono_atom_get_path(sim->atom);
1717 DBusConnection *conn = ofono_dbus_get_connection();
1718 gboolean efli_format = TRUE;
1719 GSList *efli = NULL;
1720 GSList *efpl = NULL;
1722 if (!ok || length < 2)
1725 efpl = parse_language_list(data, length);
1728 if (sim->efli && sim->efli_length > 0) {
1729 efli_format = sim_efli_format(sim->efli, sim->efli_length);
1732 efli = parse_language_list(sim->efli, sim->efli_length);
1734 efli = parse_eflp(sim->efli, sim->efli_length);
1738 * If efli_format is TRUE, make a list of languages in both files in
1739 * order of preference following TS 31.102.
1740 * Quoting 31.102 Section 5.1.1.2:
1741 * The preferred language selection shall always use the EFLI in
1742 * preference to the EFPL at the MF unless:
1743 * - if the EFLI has the value 'FFFF' in its highest priority position,
1744 * then the preferred language selection shall be the language
1745 * preference in the EFPL at the MF level
1746 * Otherwise in order of preference according to TS 51.011
1749 if (sim->efli_length >= 2 && sim->efli[0] == 0xff &&
1750 sim->efli[1] == 0xff)
1751 sim->language_prefs = concat_lang_prefs(NULL, efpl);
1753 sim->language_prefs = concat_lang_prefs(efli, efpl);
1755 sim->language_prefs = concat_lang_prefs(efpl, efli);
1761 sim->efli_length = 0;
1765 g_slist_foreach(efli, (GFunc)g_free, NULL);
1770 g_slist_foreach(efpl, (GFunc)g_free, NULL);
1774 if (sim->language_prefs != NULL)
1775 ofono_dbus_signal_array_property_changed(conn, path,
1776 OFONO_SIM_MANAGER_INTERFACE,
1777 "PreferredLanguages",
1779 &sim->language_prefs);
1784 static void sim_iccid_read_cb(int ok, int length, int record,
1785 const unsigned char *data,
1786 int record_length, void *userdata)
1788 struct ofono_sim *sim = userdata;
1789 const char *path = __ofono_atom_get_path(sim->atom);
1790 DBusConnection *conn = ofono_dbus_get_connection();
1791 char iccid[21]; /* ICCID max length is 20 + 1 for NULL */
1793 if (!ok || length < 10)
1796 extract_bcd_number(data, length, iccid);
1798 sim->iccid = g_strdup(iccid);
1800 ofono_dbus_signal_property_changed(conn, path,
1801 OFONO_SIM_MANAGER_INTERFACE,
1807 static void sim_initialize(struct ofono_sim *sim)
1810 * Perform SIM initialization according to 3GPP 31.102 Section 5.1.1.2
1811 * The assumption here is that if sim manager is being initialized,
1812 * then sim commands are implemented, and the sim manager is then
1813 * responsible for checking the PIN, reading the IMSI and signaling
1814 * SIM ready condition.
1816 * The procedure according to 31.102, 51.011, 11.11 and CPHS 4.2 is
1820 * Read EFli and EFpl
1822 * Request SIM phase (only in 51.011)
1823 * Administrative information request (read EFad)
1824 * Request CPHS Information (only in CPHS 4.2)
1825 * Read EFsst (only in 11.11 & 51.011)
1826 * Read EFust (only in 31.102)
1827 * Read EFest (only in 31.102)
1830 * At this point we signal the SIM ready condition and allow
1831 * arbitrary files to be written or read, assuming their presence
1835 /* Grab the EFiccid which is always available */
1836 ofono_sim_read(sim, SIM_EF_ICCID_FILEID,
1837 OFONO_SIM_FILE_STRUCTURE_TRANSPARENT,
1838 sim_iccid_read_cb, sim);
1840 /* EFecc is read by the voicecall atom */
1843 * According to 31.102 the EFli is read first and EFpl is then
1844 * only read if none of the EFli languages are supported by user
1845 * interface. 51.011 mandates the exact opposite, making EFpl/EFelp
1846 * preferred over EFlp (same EFid as EFli, different format).
1847 * However we don't depend on the user interface and so
1848 * need to read both files now.
1850 ofono_sim_read(sim, SIM_EFLI_FILEID,
1851 OFONO_SIM_FILE_STRUCTURE_TRANSPARENT,
1852 sim_efli_read_cb, sim);
1853 ofono_sim_read(sim, SIM_EFPL_FILEID,
1854 OFONO_SIM_FILE_STRUCTURE_TRANSPARENT,
1855 sim_efpl_read_cb, sim);
1858 int ofono_sim_read_bytes(struct ofono_sim *sim, int id,
1859 unsigned short offset, unsigned short num_bytes,
1860 ofono_sim_file_read_cb_t cb, void *data)
1868 return sim_fs_read(sim->simfs, id, OFONO_SIM_FILE_STRUCTURE_TRANSPARENT,
1869 offset, num_bytes, cb, data);
1872 int ofono_sim_read(struct ofono_sim *sim, int id,
1873 enum ofono_sim_file_structure expected_type,
1874 ofono_sim_file_read_cb_t cb, void *data)
1879 return sim_fs_read(sim->simfs, id, expected_type, 0, 0, cb, data);
1882 int ofono_sim_write(struct ofono_sim *sim, int id,
1883 ofono_sim_file_write_cb_t cb,
1884 enum ofono_sim_file_structure structure, int record,
1885 const unsigned char *data, int length, void *userdata)
1890 return sim_fs_write(sim->simfs, id, cb, structure, record, data, length,
1894 const char *ofono_sim_get_imsi(struct ofono_sim *sim)
1902 enum ofono_sim_phase ofono_sim_get_phase(struct ofono_sim *sim)
1910 enum ofono_sim_cphs_phase ofono_sim_get_cphs_phase(struct ofono_sim *sim)
1913 return OFONO_SIM_CPHS_PHASE_NONE;
1915 return sim->cphs_phase;
1918 const unsigned char *ofono_sim_get_cphs_service_table(struct ofono_sim *sim)
1923 return sim->cphs_service_table;
1926 static void sim_inserted_update(struct ofono_sim *sim)
1928 DBusConnection *conn = ofono_dbus_get_connection();
1929 const char *path = __ofono_atom_get_path(sim->atom);
1930 dbus_bool_t present = sim->state != OFONO_SIM_STATE_NOT_PRESENT;
1932 ofono_dbus_signal_property_changed(conn, path,
1933 OFONO_SIM_MANAGER_INTERFACE,
1935 DBUS_TYPE_BOOLEAN, &present);
1938 static void sim_free_state(struct ofono_sim *sim)
1950 if (sim->own_numbers) {
1951 g_slist_foreach(sim->own_numbers, (GFunc)g_free, NULL);
1952 g_slist_free(sim->own_numbers);
1953 sim->own_numbers = NULL;
1956 if (sim->service_numbers) {
1957 g_slist_foreach(sim->service_numbers,
1958 (GFunc)service_number_free, NULL);
1959 g_slist_free(sim->service_numbers);
1960 sim->service_numbers = NULL;
1966 sim->efli_length = 0;
1969 if (sim->language_prefs) {
1970 g_strfreev(sim->language_prefs);
1971 sim->language_prefs = NULL;
1977 sim->efust_length = 0;
1983 sim->efest_length = 0;
1989 sim->efsst_length = 0;
1992 sim->mnc_length = 0;
1997 sim->efimg_length = 0;
2000 g_free(sim->iidf_image);
2001 sim->iidf_image = NULL;
2003 sim->fixed_dialing = FALSE;
2004 sim->barred_dialing = FALSE;
2007 void ofono_sim_inserted_notify(struct ofono_sim *sim, ofono_bool_t inserted)
2009 ofono_sim_state_event_cb_t notify;
2012 if (inserted == TRUE && sim->state == OFONO_SIM_STATE_NOT_PRESENT)
2013 sim->state = OFONO_SIM_STATE_INSERTED;
2014 else if (inserted == FALSE && sim->state != OFONO_SIM_STATE_NOT_PRESENT)
2015 sim->state = OFONO_SIM_STATE_NOT_PRESENT;
2019 if (!__ofono_atom_get_registered(sim->atom))
2022 sim_inserted_update(sim);
2024 for (l = sim->state_watches->items; l; l = l->next) {
2025 struct ofono_watchlist_item *item = l->data;
2026 notify = item->notify;
2028 notify(sim->state, item->notify_data);
2032 sim_initialize(sim);
2034 sim_free_state(sim);
2037 unsigned int ofono_sim_add_state_watch(struct ofono_sim *sim,
2038 ofono_sim_state_event_cb_t notify,
2039 void *data, ofono_destroy_func destroy)
2041 struct ofono_watchlist_item *item;
2051 item = g_new0(struct ofono_watchlist_item, 1);
2053 item->notify = notify;
2054 item->destroy = destroy;
2055 item->notify_data = data;
2057 return __ofono_watchlist_add_item(sim->state_watches, item);
2060 void ofono_sim_remove_state_watch(struct ofono_sim *sim, unsigned int id)
2062 __ofono_watchlist_remove_item(sim->state_watches, id);
2065 enum ofono_sim_state ofono_sim_get_state(struct ofono_sim *sim)
2068 return OFONO_SIM_STATE_NOT_PRESENT;
2073 static void sim_set_ready(struct ofono_sim *sim)
2076 ofono_sim_state_event_cb_t notify;
2081 if (sim->state != OFONO_SIM_STATE_INSERTED)
2084 sim->state = OFONO_SIM_STATE_READY;
2086 sim_fs_check_version(sim->simfs);
2088 for (l = sim->state_watches->items; l; l = l->next) {
2089 struct ofono_watchlist_item *item = l->data;
2090 notify = item->notify;
2092 notify(sim->state, item->notify_data);
2096 int ofono_sim_driver_register(const struct ofono_sim_driver *d)
2098 DBG("driver: %p, name: %s", d, d->name);
2100 if (d->probe == NULL)
2103 g_drivers = g_slist_prepend(g_drivers, (void *)d);
2108 void ofono_sim_driver_unregister(const struct ofono_sim_driver *d)
2110 DBG("driver: %p, name: %s", d, d->name);
2112 g_drivers = g_slist_remove(g_drivers, (void *)d);
2115 static void sim_unregister(struct ofono_atom *atom)
2117 DBusConnection *conn = ofono_dbus_get_connection();
2118 struct ofono_modem *modem = __ofono_atom_get_modem(atom);
2119 const char *path = __ofono_atom_get_path(atom);
2120 struct ofono_sim *sim = __ofono_atom_get_data(atom);
2122 __ofono_watchlist_free(sim->state_watches);
2123 sim->state_watches = NULL;
2125 g_dbus_unregister_interface(conn, path, OFONO_SIM_MANAGER_INTERFACE);
2126 ofono_modem_remove_interface(modem, OFONO_SIM_MANAGER_INTERFACE);
2129 static void sim_remove(struct ofono_atom *atom)
2131 struct ofono_sim *sim = __ofono_atom_get_data(atom);
2133 DBG("atom: %p", atom);
2138 if (sim->driver && sim->driver->remove)
2139 sim->driver->remove(sim);
2141 sim_free_state(sim);
2144 sim_fs_free(sim->simfs);
2151 struct ofono_sim *ofono_sim_create(struct ofono_modem *modem,
2152 unsigned int vendor,
2156 struct ofono_sim *sim;
2162 sim = g_try_new0(struct ofono_sim, 1);
2167 sim->phase = OFONO_SIM_PHASE_UNKNOWN;
2168 sim->atom = __ofono_modem_add_atom(modem, OFONO_ATOM_TYPE_SIM,
2171 for (l = g_drivers; l; l = l->next) {
2172 const struct ofono_sim_driver *drv = l->data;
2174 if (g_strcmp0(drv->name, driver))
2177 if (drv->probe(sim, vendor, data) < 0)
2187 void ofono_sim_register(struct ofono_sim *sim)
2189 DBusConnection *conn = ofono_dbus_get_connection();
2190 struct ofono_modem *modem = __ofono_atom_get_modem(sim->atom);
2191 const char *path = __ofono_atom_get_path(sim->atom);
2193 if (!g_dbus_register_interface(conn, path,
2194 OFONO_SIM_MANAGER_INTERFACE,
2195 sim_methods, sim_signals, NULL,
2197 ofono_error("Could not create %s interface",
2198 OFONO_SIM_MANAGER_INTERFACE);
2203 ofono_modem_add_interface(modem, OFONO_SIM_MANAGER_INTERFACE);
2204 sim->state_watches = __ofono_watchlist_new(g_free);
2205 sim->simfs = sim_fs_new(sim, sim->driver);
2207 __ofono_atom_register(sim->atom, sim_unregister);
2209 ofono_sim_add_state_watch(sim, sim_ready, sim, NULL);
2211 if (sim->state > OFONO_SIM_STATE_NOT_PRESENT)
2212 sim_initialize(sim);
2215 void ofono_sim_remove(struct ofono_sim *sim)
2217 __ofono_atom_free(sim->atom);
2220 void ofono_sim_set_data(struct ofono_sim *sim, void *data)
2222 sim->driver_data = data;
2225 void *ofono_sim_get_data(struct ofono_sim *sim)
2227 return sim->driver_data;