Loading...
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 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 | /*
*
* oFono - Open Source Telephony
*
* Copyright (C) 2011 ST-Ericsson AB.
* Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <glib.h>
#include <gisi/message.h>
#include <gisi/client.h>
#include <gisi/iter.h>
#include <ofono/log.h>
#include <ofono/modem.h>
#include <ofono/sim.h>
#include "simutil.h"
#include "isimodem.h"
#include "isiutil.h"
#include "sim.h"
#include "uicc.h"
#include "uicc-util.h"
#include "debug.h"
/* File info parameters */
#define FCP_TEMPLATE 0x62
#define FCP_FILE_SIZE 0x80
#define FCP_FILE_DESC 0x82
#define FCP_FILE_ID 0x83
#define FCP_FILE_LIFECYCLE 0x8A
#define FCP_FILE_SECURITY_ARR 0x8B
#define FCP_FILE_SECURITY_COMPACT 0x8C
#define FCP_FILE_SECURITY_EXPANDED 0xAB
#define FCP_PIN_STATUS 0xC6
#define SIM_EFARR_FILEID 0x6f06
#define MAX_SIM_APPS 10
#define MAX_IMSI_LENGTH 15
enum uicc_flag {
UICC_FLAG_APP_STARTED = 1 << 0,
UICC_FLAG_PIN_STATE_RECEIVED = 1 << 1,
UICC_FLAG_PASSWD_REQUIRED = 1 << 2,
};
static GHashTable *g_modems;
struct file_info {
int fileid;
int length;
int structure;
int record_length;
uint8_t access[3];
uint8_t file_status;
};
static const struct file_info static_file_info[] = {
{ SIM_EFSPN_FILEID, 17, 0, 0, { 0x0e, 0xff, 0xee }, 1 },
{ SIM_EF_ICCID_FILEID, 10, 0, 10, { 0x0f, 0xff, 0xee }, 1 },
{ SIM_EFPL_FILEID, 1, 0, 1, { 0x0f, 0xff, 0xff }, 1 },
{ SIM_EFLI_FILEID, 1, 0, 1, { 0x0f, 0xff, 0xff }, 1 },
{ SIM_EFMSISDN_FILEID, 28, 1, 28, { 0x01, 0xff, 0xee }, 1 },
{ SIM_EFAD_FILEID, 20, 0, 20, { 0x0e, 0xff, 0xee }, 1 },
{ SIM_EFPHASE_FILEID, 1, 0, 1, { 0x0e, 0xff, 0xee }, 1 },
{ SIM_EFPNN_FILEID, 4 * 18, 1, 18, { 0x0e, 0xff, 0xee }, 1 },
{ SIM_EFOPL_FILEID, 4 * 24, 1, 24, { 0x0e, 0xff, 0xee }, 1 },
{ SIM_EFMBI_FILEID, 5, 1, 5, { 0x0e, 0xff, 0xee }, 1 },
{ SIM_EFMWIS_FILEID, 6, 1, 6, { 0x01, 0xff, 0xee }, 1 },
{ SIM_EFSPDI_FILEID, 64, 0, 64, { 0x0e, 0xff, 0xee }, 1 },
{ SIM_EFECC_FILEID, 5 * 3, 0, 3, { 0x0e, 0xff, 0xee }, 1 },
{ SIM_EFCBMIR_FILEID, 8 * 4, 0, 4, { 0x01, 0xff, 0xee }, 1 },
{ SIM_EFCBMI_FILEID, 8 * 2, 0, 2, { 0x01, 0xff, 0xee }, 1 },
{ SIM_EFCBMID_FILEID, 8 * 2, 0, 2, { 0x01, 0xff, 0x11 }, 1 },
{ SIM_EFSMSP_FILEID, 56, 1, 56, { 0x01, 0xff, 0xee }, 1 },
{ SIM_EFIMSI_FILEID, 9, 0, 9, { 0x0e, 0xff, 0xee }, 1 },
};
static gboolean check_resp(const GIsiMessage *msg, uint8_t msgid, uint8_t service)
{
uint8_t type;
uint8_t cause;
if (g_isi_msg_error(msg) < 0) {
DBG("Error: %s", g_isi_msg_strerror(msg));
return FALSE;
}
if (g_isi_msg_id(msg) != msgid) {
DBG("Unexpected msg: %s",
sim_message_id_name(g_isi_msg_id(msg)));
return FALSE;
}
if (!g_isi_msg_data_get_byte(msg, 1, &cause) ||
cause != UICC_STATUS_OK) {
DBG("Request failed: %s", uicc_status_name(cause));
return FALSE;
}
if (!g_isi_msg_data_get_byte(msg, 0, &type) || type != service) {
DBG("Unexpected service: 0x%02X (0x%02X)", type, service);
return FALSE;
}
return TRUE;
}
struct uicc_file_info_cb_data {
void *cb;
void *data;
void *user;
struct ofono_sim *sim;
};
static gboolean decode_uicc_usim_type(GIsiSubBlockIter *iter, uint16_t *length,
uint16_t *file_id,
uint16_t *record_length,
uint8_t *records, uint8_t *structure)
{
uint8_t fcp = 0;
uint8_t desc = 0;
uint8_t coding = 0;
uint8_t fcp_len = 0;
uint8_t read = 0;
uint8_t item_len = 0;
if (!g_isi_sb_iter_get_byte(iter, &fcp, 8))
return FALSE;
if (fcp != FCP_TEMPLATE)
return FALSE;
if (!g_isi_sb_iter_get_byte(iter, &fcp_len, 9))
return FALSE;
for (read = 0; read < fcp_len; read += item_len + 2) {
uint8_t id;
if (!g_isi_sb_iter_get_byte(iter, &id, read + 10))
return FALSE;
if (!g_isi_sb_iter_get_byte(iter, &item_len, read + 11))
return FALSE;
switch (id) {
case FCP_FILE_SIZE:
if (item_len != 2)
return FALSE;
if (!g_isi_sb_iter_get_word(iter, length, read + 10 + 2))
return FALSE;
break;
case FCP_FILE_ID:
if (item_len != 2)
return FALSE;
if (!g_isi_sb_iter_get_word(iter, file_id, read + 10 + 2))
return FALSE;
break;
case FCP_FILE_DESC:
if (item_len < 2)
return FALSE;
if (!g_isi_sb_iter_get_byte(iter, &desc, read + 10 + 2))
return FALSE;
if (!g_isi_sb_iter_get_byte(iter, &coding, read + 10 + 3))
return FALSE;
if (item_len < 4)
break;
if (!g_isi_sb_iter_get_word(iter, record_length,
read + 10 + 4))
return FALSE;
if (!g_isi_sb_iter_get_byte(iter, records, read + 10 + 6))
return FALSE;
break;
/*
* Not implemented, using static access rules
* as these are used only for cacheing See
* ETSI TS 102 221, ch 11.1.1.4.7 and Annexes
* E, F and G.
*/
case FCP_FILE_SECURITY_ARR:
case FCP_FILE_SECURITY_COMPACT:
case FCP_FILE_SECURITY_EXPANDED:
case FCP_FILE_LIFECYCLE:
default:
DBG("FCP id %02X not supported", id);
break;
}
}
if ((desc & 7) == 1)
*structure = OFONO_SIM_FILE_STRUCTURE_TRANSPARENT;
else if ((desc & 7) == 2)
*structure = OFONO_SIM_FILE_STRUCTURE_FIXED;
else if ((desc & 7) == 6)
*structure = OFONO_SIM_FILE_STRUCTURE_CYCLIC;
return TRUE;
}
static void uicc_file_info_resp_cb(const GIsiMessage *msg, void *opaque)
{
struct uicc_file_info_cb_data *cbd = opaque;
struct uicc_sim_data *sd = ofono_sim_get_data(cbd->sim);
struct file_info const *info = cbd->user;
ofono_sim_file_info_cb_t cb = cbd->cb;
GIsiSubBlockIter iter;
uint16_t length = 0;
uint16_t record_length = 0;
uint8_t structure = 0xFF;
uint8_t records = 0;
uint16_t file_id = 0;
uint8_t access[3] = {0, 0, 0};
uint8_t item_len = 0;
uint8_t message_id = 0;
uint8_t service_type = 0;
uint8_t status = 0;
uint8_t details = 0;
uint8_t num_subblocks = 0;
uint8_t file_status = 1;
message_id = g_isi_msg_id(msg);
DBG("uicc_file_info_resp_cb: msg_id=%d, msg len=%zu", message_id,
g_isi_msg_data_len(msg));
if (message_id != UICC_APPL_CMD_RESP)
goto error;
if (!g_isi_msg_data_get_byte(msg, 0, &service_type) ||
!g_isi_msg_data_get_byte(msg, 1, &status) ||
!g_isi_msg_data_get_byte(msg, 2, &details) ||
!g_isi_msg_data_get_byte(msg, 5, &num_subblocks))
goto error;
DBG("%s, service %s, status %s, details %s, nm_sb %d",
uicc_message_id_name(message_id),
uicc_service_type_name(service_type),
uicc_status_name(status), uicc_details_name(details),
num_subblocks);
if (info) {
access[0] = info->access[0];
access[1] = info->access[1];
access[2] = info->access[2];
file_status = info->file_status;
}
for (g_isi_sb_iter_init_full(&iter, msg, 6, TRUE, num_subblocks);
g_isi_sb_iter_is_valid(&iter);
g_isi_sb_iter_next(&iter)) {
uint8_t sb_id = g_isi_sb_iter_get_id(&iter);
DBG("Subblock %s", uicc_subblock_name(sb_id));
if (sb_id != UICC_SB_FCI)
continue;
DBG("Decoding UICC_SB_FCI");
switch (sd->app_type) {
case UICC_APPL_TYPE_UICC_USIM:
DBG("UICC_APPL_TYPE_UICC_USIM");
if (!decode_uicc_usim_type(&iter, &length, &file_id,
&record_length,
&records,
&structure))
goto error;
break;
case UICC_APPL_TYPE_ICC_SIM:
DBG("UICC_APPL_TYPE_ICC_SIM");
if (!g_isi_sb_iter_get_word(&iter, &length, 10))
goto error;
if (!g_isi_sb_iter_get_word(&iter, &file_id, 12))
goto error;
if (!g_isi_sb_iter_get_byte(&iter, &access[0], 16))
goto error;
if (!g_isi_sb_iter_get_byte(&iter, &access[0], 17))
goto error;
if (!g_isi_sb_iter_get_byte(&iter, &access[0], 18))
goto error;
if (!g_isi_sb_iter_get_byte(&iter, &item_len, 20))
goto error;
if (!g_isi_sb_iter_get_byte(&iter, &structure, 21))
goto error;
if (item_len == 2) {
uint8_t byte;
if (!g_isi_sb_iter_get_byte(&iter, &byte, 22))
goto error;
record_length = byte;
}
break;
default:
DBG("Application type %d not supported", sd->app_type);
break;
}
DBG("fileid=%04X, filelen=%d, records=%d, reclen=%d, structure=%d",
file_id, length, records, record_length, structure);
CALLBACK_WITH_SUCCESS(cb, length, structure, record_length,
access, file_status, cbd->data);
return;
}
error:
DBG("Error reading file info");
CALLBACK_WITH_FAILURE(cb, -1, -1, -1, NULL, 0, cbd->data);
}
static gboolean send_uicc_read_file_info(GIsiClient *client, uint8_t app_id,
int fileid, uint8_t df_len,
int mf_path, int df1_path,
int df2_path,
GIsiNotifyFunc notify, void *data,
GDestroyNotify destroy)
{
const uint8_t msg[] = {
UICC_APPL_CMD_REQ,
UICC_APPL_FILE_INFO, /* Service type */
app_id,
UICC_SESSION_ID_NOT_USED,
0, 0, /* Filler */
1, /* Number of subblocks */
ISI_16BIT(UICC_SB_APPL_PATH),
ISI_16BIT(16), /* Subblock length */
ISI_16BIT(fileid),
uicc_get_sfi(fileid), /* Elementary file short file id */
0, /* Filler */
df_len,
0, /* Filler */
ISI_16BIT(mf_path),
ISI_16BIT(df1_path),
ISI_16BIT(df2_path),
};
return g_isi_client_send(client, msg, sizeof(msg), notify, data, destroy);
}
static void uicc_read_file_info(struct ofono_sim *sim, int fileid,
const unsigned char *path,
unsigned int path_len,
ofono_sim_file_info_cb_t cb, void *data)
{
struct uicc_sim_data *sd = ofono_sim_get_data(sim);
struct uicc_file_info_cb_data *cbd;
/* Prepare for static file info used for access rights */
int i;
int N = sizeof(static_file_info) / sizeof(static_file_info[0]);
int mf_path = 0;
int df1_path = 0;
int df2_path = 0;
uint8_t df_len = 0;
cbd = g_try_new0(struct uicc_file_info_cb_data, 1);
if (!cbd)
goto error;
cbd->cb = cb;
cbd->data = data;
cbd->sim = sim;
cbd->user = NULL;
DBG("File info for ID=%04X app id %d", fileid, sd->app_id);
for (i = 0; i < N; i++) {
if (fileid == static_file_info[i].fileid) {
cbd->user = (void *) &static_file_info[i];
break;
}
}
DBG("File info for ID=%04X: %p", fileid, cbd->user);
if (!uicc_get_fileid_path(sd, &mf_path, &df1_path, &df2_path,
&df_len, fileid))
goto error;
if (send_uicc_read_file_info(sd->client, sd->app_id, fileid, df_len,
mf_path, df1_path, df2_path,
uicc_file_info_resp_cb,
cbd, g_free))
return;
error:
CALLBACK_WITH_FAILURE(cb, -1, -1, -1, NULL, 0, data);
g_free(cbd);
}
static void uicc_read_file_transp_resp_cb(const GIsiMessage *msg, void *opaque)
{
struct isi_cb_data *cbd = opaque;
ofono_sim_read_cb_t cb = cbd->cb;
GIsiSubBlockIter iter;
uint32_t filelen = 0;
uint8_t *filedata = NULL;
uint8_t num_sb = 0;
DBG("");
if (!check_resp(msg, UICC_APPL_CMD_RESP, UICC_APPL_READ_TRANSPARENT))
goto error;
if (!g_isi_msg_data_get_byte(msg, 5, &num_sb))
goto error;
for (g_isi_sb_iter_init_full(&iter, msg, 6, TRUE, num_sb);
g_isi_sb_iter_is_valid(&iter);
g_isi_sb_iter_next(&iter)) {
int sb_id = g_isi_sb_iter_get_id(&iter);
DBG("Subblock %s", uicc_subblock_name(sb_id));
if (sb_id != UICC_SB_FILE_DATA)
continue;
if (!g_isi_sb_iter_get_dword(&iter, &filelen, 4))
goto error;
if (!g_isi_sb_iter_get_struct(&iter, (void **) &filedata,
filelen, 8))
goto error;
DBG("Transparent EF read: 1st byte %02x, len %d",
filedata[0], filelen);
CALLBACK_WITH_SUCCESS(cb, filedata, filelen, cbd->data);
return;
}
error:
DBG("Error reading transparent EF");
CALLBACK_WITH_FAILURE(cb, NULL, 0, cbd->data);
}
static gboolean send_uicc_read_file_transparent(GIsiClient *client,
uint8_t app_id, uint8_t client_id,
int fileid, uint8_t df_len,
int mf_path, int df1_path,
int df2_path,
GIsiNotifyFunc notify,
void *data,
GDestroyNotify destroy)
{
const uint8_t msg[] = {
UICC_APPL_CMD_REQ,
UICC_APPL_READ_TRANSPARENT,
app_id,
UICC_SESSION_ID_NOT_USED,
0, 0, /* Filler */
3, /* Number of subblocks */
ISI_16BIT(UICC_SB_CLIENT),
ISI_16BIT(8), /* Subblock length*/
0, 0, 0, /* Filler */
client_id,
ISI_16BIT(UICC_SB_TRANSPARENT),
ISI_16BIT(8), /* Subblock length */
ISI_16BIT(0), /* File offset */
ISI_16BIT(0), /* Data amount (0=all) */
ISI_16BIT(UICC_SB_APPL_PATH),
ISI_16BIT(16), /* Subblock length */
ISI_16BIT(fileid),
uicc_get_sfi(fileid), /* Elementary file short file id */
0, /* Filler */
df_len,
0,
ISI_16BIT(mf_path),
ISI_16BIT(df1_path),
ISI_16BIT(df2_path),
};
return g_isi_client_send(client, msg, sizeof(msg), notify, data, destroy);
}
static void uicc_read_file_transparent(struct ofono_sim *sim, int fileid,
int start, int length,
const unsigned char *path,
unsigned int path_len,
ofono_sim_read_cb_t cb, void *data)
{
struct uicc_sim_data *sd = ofono_sim_get_data(sim);
struct isi_cb_data *cbd = isi_cb_data_new(sim, cb, data);
int mf_path = 0;
int df1_path = 0;
int df2_path = 0;
uint8_t df_len = 0;
if (!cbd || !sd)
goto error;
DBG("File ID=%04X, client %d, AID %d", fileid, sd->client_id,
sd->app_id);
if (!uicc_get_fileid_path(sd, &mf_path, &df1_path,
&df2_path, &df_len, fileid))
goto error;
if (send_uicc_read_file_transparent(sd->client, sd->app_id, sd->client_id,
fileid, df_len, mf_path,
df1_path, df2_path,
uicc_read_file_transp_resp_cb,
cbd, g_free))
return;
error:
DBG("Read file transparent failed");
CALLBACK_WITH_FAILURE(cb, NULL, 0, data);
g_free(cbd);
}
static void read_file_linear_resp(const GIsiMessage *msg, void *opaque)
{
struct isi_cb_data *cbd = opaque;
ofono_sim_read_cb_t cb = cbd->cb;
GIsiSubBlockIter iter;
uint8_t num_sb = 0;
uint8_t *filedata = NULL;
uint32_t filelen = 0;
DBG("");
if (!check_resp(msg, UICC_APPL_CMD_RESP, UICC_APPL_READ_LINEAR_FIXED))
goto error;
if (!g_isi_msg_data_get_byte(msg, 5, &num_sb))
goto error;
for (g_isi_sb_iter_init_full(&iter, msg, 6, TRUE, num_sb);
g_isi_sb_iter_is_valid(&iter);
g_isi_sb_iter_next(&iter)) {
uint8_t sb_id = g_isi_sb_iter_get_id(&iter);
DBG("Subblock %s", uicc_subblock_name(sb_id));
if (sb_id != UICC_SB_FILE_DATA)
continue;
if (!g_isi_sb_iter_get_dword(&iter, &filelen, 4))
goto error;
if (!g_isi_sb_iter_get_struct(&iter, (void **) &filedata,
filelen, 8))
goto error;
DBG("Linear fixed EF read: 1st byte %02x, len %d", filedata[0],
filelen);
CALLBACK_WITH_SUCCESS(cb, filedata, filelen, cbd->data);
return;
}
error:
CALLBACK_WITH_FAILURE(cb, NULL, 0, cbd->data);
}
static gboolean send_uicc_read_file_linear(GIsiClient *client, uint8_t app_id,
uint8_t client_id,
int fileid, int record,
int rec_length,
unsigned char df_len,
int mf_path, int df1_path,
int df2_path,
GIsiNotifyFunc notify,
void *data,
GDestroyNotify destroy)
{
const uint8_t msg[] = {
UICC_APPL_CMD_REQ,
UICC_APPL_READ_LINEAR_FIXED,
app_id,
UICC_SESSION_ID_NOT_USED,
0, 0, /* Filler */
3, /* Number of subblocks */
ISI_16BIT(UICC_SB_CLIENT),
ISI_16BIT(8), /*Subblock length */
0, 0, 0, /* Filler */
client_id,
ISI_16BIT(UICC_SB_LINEAR_FIXED),
ISI_16BIT(8), /*Subblock length */
record,
0, /* Record offset */
rec_length & 0xff, /*Data amount (0=all)*/
0,
ISI_16BIT(UICC_SB_APPL_PATH),
ISI_16BIT(16), /* Subblock length */
ISI_16BIT(fileid),
uicc_get_sfi(fileid), /* Elementary file short file id */
0, /* Filler */
df_len,
0,
ISI_16BIT(mf_path),
ISI_16BIT(df1_path),
ISI_16BIT(df2_path),
};
return g_isi_client_send(client, msg, sizeof(msg), notify, data, destroy);
}
static void uicc_read_file_linear(struct ofono_sim *sim, int fileid, int record,
int rec_length,
const unsigned char *path,
unsigned int path_len,
ofono_sim_read_cb_t cb, void *data)
{
struct uicc_sim_data *sd = ofono_sim_get_data(sim);
struct isi_cb_data *cbd = isi_cb_data_new(sim, cb, data);
int mf_path = 0;
int df1_path = 0;
int df2_path = 0;
uint8_t df_len = 0;
if (!sd || !cbd)
goto error;
DBG("File ID=%04X, record %d, client %d AID %d", fileid, record,
sd->client_id, sd->app_id);
if (!uicc_get_fileid_path(sd, &mf_path, &df1_path, &df2_path,
&df_len, fileid))
goto error;
if (send_uicc_read_file_linear(sd->client, sd->app_id, sd->client_id,
fileid, record, rec_length, df_len,
mf_path, df1_path, df2_path,
read_file_linear_resp, cbd, g_free))
return;
error:
CALLBACK_WITH_FAILURE(cb, NULL, 0, data);
g_free(cbd);
}
static void uicc_read_file_cyclic(struct ofono_sim *sim, int fileid,
int record, int length,
const unsigned char *path,
unsigned int path_len,
ofono_sim_read_cb_t cb, void *data)
{
DBG("Not implemented");
CALLBACK_WITH_FAILURE(cb, NULL, 0, data);
}
static void uicc_write_file_transparent(struct ofono_sim *sim, int fileid,
int start, int length,
const unsigned char *value,
const unsigned char *path,
unsigned int path_len,
ofono_sim_write_cb_t cb, void *data)
{
DBG("Not implemented");
CALLBACK_WITH_FAILURE(cb, data);
}
static void uicc_write_file_linear(struct ofono_sim *sim, int fileid, int record,
int length, const unsigned char *value,
const unsigned char *path,
unsigned int path_len,
ofono_sim_write_cb_t cb, void *data)
{
DBG("Not implemented");
CALLBACK_WITH_FAILURE(cb, data);
}
static void uicc_write_file_cyclic(struct ofono_sim *sim, int fileid,
int length,
const unsigned char *value,
const unsigned char *path,
unsigned int path_len,
ofono_sim_write_cb_t cb, void *data)
{
DBG("Not implemented");
CALLBACK_WITH_FAILURE(cb, data);
}
static gboolean decode_imsi(uint8_t *data, int len, char *imsi)
{
int i = 1; /* Skip first byte, the length field */
int j = 0;
if (data == NULL || len == 0)
return FALSE;
if (data[0] != 8 || data[0] > len)
return FALSE;
/* Ignore low-order semi-octet of the first byte */
imsi[j] = ((data[i] & 0xF0) >> 4) + '0';
for (i++, j++; i - 1 < data[0] && j < MAX_IMSI_LENGTH; i++) {
char nibble;
imsi[j++] = (data[i] & 0x0F) + '0';
nibble = (data[i] & 0xF0) >> 4;
if (nibble != 0x0F)
imsi[j++] = nibble + '0';
}
imsi[j] = '\0';
return TRUE;
}
static void uicc_read_imsi_resp(const GIsiMessage *msg, void *opaque)
{
struct isi_cb_data *cbd = opaque;
ofono_sim_imsi_cb_t cb = cbd->cb;
GIsiSubBlockIter iter;
uint32_t filelen = 0;
uint8_t *filedata = NULL;
uint8_t num_sb = 0;
char imsi[MAX_IMSI_LENGTH + 1] = { 0 };
DBG("");
if (!check_resp(msg, UICC_APPL_CMD_RESP, UICC_APPL_READ_TRANSPARENT))
goto error;
if (!g_isi_msg_data_get_byte(msg, 5, &num_sb))
goto error;
for (g_isi_sb_iter_init_full(&iter, msg, 6, TRUE, num_sb);
g_isi_sb_iter_is_valid(&iter);
g_isi_sb_iter_next(&iter)) {
int sb_id = g_isi_sb_iter_get_id(&iter);
DBG("Subblock %s", uicc_subblock_name(sb_id));
if (sb_id != UICC_SB_FILE_DATA)
continue;
if (!g_isi_sb_iter_get_dword(&iter, &filelen, 4))
goto error;
if (!g_isi_sb_iter_get_struct(&iter, (void **) &filedata,
filelen, 8))
goto error;
DBG("Transparent EF read: 1st byte %02x, len %d",
filedata[0], filelen);
if (!decode_imsi(filedata, filelen, imsi))
goto error;
DBG("IMSI %s", imsi);
CALLBACK_WITH_SUCCESS(cb, imsi, cbd->data);
return;
}
error:
CALLBACK_WITH_FAILURE(cb, NULL, cbd->data);
}
static void uicc_read_imsi(struct ofono_sim *sim, ofono_sim_imsi_cb_t cb,
void *data)
{
struct uicc_sim_data *sd = ofono_sim_get_data(sim);
struct isi_cb_data *cbd = isi_cb_data_new(sim, cb, data);
int mf_path = 0;
int df1_path = 0;
int df2_path = 0;
uint8_t df_len = 0;
if (!cbd)
goto error;
DBG("Client %d, AID %d", sd->client_id, sd->app_id);
if (!uicc_get_fileid_path(sd, &mf_path, &df1_path, &df2_path, &df_len,
SIM_EFIMSI_FILEID))
goto error;
if (send_uicc_read_file_transparent(sd->client, sd->app_id, sd->client_id,
SIM_EFIMSI_FILEID, df_len,
mf_path, df1_path, df2_path,
uicc_read_imsi_resp,
cbd, g_free))
return;
error:
CALLBACK_WITH_FAILURE(cb, NULL, data);
g_free(cbd);
}
static void uicc_query_passwd_state_resp(const GIsiMessage *msg, void *opaque)
{
struct isi_cb_data *cbd = opaque;
ofono_sim_passwd_cb_t cb = cbd->cb;
uint8_t type;
uint8_t cause;
DBG("");
if (g_isi_msg_error(msg) < 0) {
DBG("Error: %s", g_isi_msg_strerror(msg));
goto error;
}
if (g_isi_msg_id(msg) != UICC_PIN_RESP) {
DBG("Unexpected msg: %s", sim_message_id_name(g_isi_msg_id(msg)));
goto error;
}
if (!g_isi_msg_data_get_byte(msg, 0, &type) ||
type != UICC_PIN_PROMPT_VERIFY) {
DBG("Unexpected service: 0x%02X (0x%02X)", type,
UICC_PIN_PROMPT_VERIFY);
goto error;
}
if (!g_isi_msg_data_get_byte(msg, 1, &cause))
goto error;
DBG("Status: %d %s", cause, uicc_status_name(cause));
if (cause == UICC_STATUS_PIN_DISABLED) {
CALLBACK_WITH_SUCCESS(cb, OFONO_SIM_PASSWORD_NONE, cbd->data);
return;
}
DBG("Request failed or not implemented: %s", uicc_status_name(cause));
error:
CALLBACK_WITH_FAILURE(cb, -1, cbd->data);
}
static void uicc_query_passwd_state(struct ofono_sim *sim,
ofono_sim_passwd_cb_t cb, void *data)
{
struct uicc_sim_data *sd = ofono_sim_get_data(sim);
struct isi_cb_data *cbd = isi_cb_data_new(sim, cb, data);
const uint8_t req[] = {
UICC_PIN_REQ,
UICC_PIN_PROMPT_VERIFY,
sd->app_id,
0, 0, 0, /* Filler */
1, /* Number of subblocks */
ISI_16BIT(UICC_SB_PIN_REF),
ISI_16BIT(8), /*Sub block length*/
sd->pin1_id, /* Pin ID */
0, 0, 0, /* Filler */
};
DBG("");
if (g_isi_client_send(sd->client, req, sizeof(req),
uicc_query_passwd_state_resp, cbd, g_free))
return;
CALLBACK_WITH_FAILURE(cb, -1, data);
g_free(cbd);
}
static void uicc_send_passwd(struct ofono_sim *sim, const char *passwd,
ofono_sim_lock_unlock_cb_t cb, void *data)
{
DBG("Not implemented");
CALLBACK_WITH_FAILURE(cb, data);
}
static void uicc_query_pin_retries_resp(const GIsiMessage *msg, void *opaque)
{
struct isi_cb_data *cbd = opaque;
ofono_sim_pin_retries_cb_t cb = cbd->cb;
int retries[OFONO_SIM_PASSWORD_INVALID];
GIsiSubBlockIter iter;
uint8_t num_sb = 0;
uint8_t pins = 0;
uint8_t pina = 0;
uint8_t puka = 0;
DBG("");
if (!check_resp(msg, UICC_PIN_RESP, UICC_PIN_INFO))
goto error;
if (!g_isi_msg_data_get_byte(msg, 5, &num_sb))
goto error;
DBG("Subblock count %d", num_sb);
for (g_isi_sb_iter_init_full(&iter, msg, 6, TRUE, num_sb);
g_isi_sb_iter_is_valid(&iter);
g_isi_sb_iter_next(&iter)) {
uint8_t sb_id = g_isi_sb_iter_get_id(&iter);
DBG("Sub-block %s", uicc_subblock_name(sb_id));
if (sb_id != UICC_SB_PIN_INFO)
continue;
if (!g_isi_sb_iter_get_byte(&iter, &pins, 4))
goto error;
if (!g_isi_sb_iter_get_byte(&iter, &pina, 5))
goto error;
if (!g_isi_sb_iter_get_byte(&iter, &puka, 6))
goto error;
DBG("PIN status %X PIN Attrib %d PUK attrib %d", pins,
pina, puka);
retries[OFONO_SIM_PASSWORD_SIM_PIN] = pina;
retries[OFONO_SIM_PASSWORD_SIM_PUK] = puka;
CALLBACK_WITH_SUCCESS(cb, retries, cbd->data);
return;
}
error:
CALLBACK_WITH_FAILURE(cb, NULL, cbd->data);
}
static void uicc_query_pin_retries(struct ofono_sim *sim,
ofono_sim_pin_retries_cb_t cb,
void *data)
{
struct uicc_sim_data *sd = ofono_sim_get_data(sim);
struct isi_cb_data *cbd = isi_cb_data_new(sim, cb, data);
const uint8_t req[] = {
UICC_PIN_REQ,
UICC_PIN_INFO,
sd->app_id,
0, 0, 0, /* Filler */
1, /* Number of subblocks */
ISI_16BIT(UICC_SB_PIN_REF),
ISI_16BIT(8), /* Subblock length */
sd->pin1_id, /* Pin ID */
0, 0, 0, /* Filler */
};
DBG("");
if (g_isi_client_send(sd->client, req, sizeof(req),
uicc_query_pin_retries_resp, cbd, g_free))
return;
CALLBACK_WITH_FAILURE(cb, NULL, data);
g_free(cbd);
}
static void uicc_reset_passwd(struct ofono_sim *sim, const char *puk,
const char *passwd, ofono_sim_lock_unlock_cb_t cb,
void *data)
{
DBG("Not implemented");
CALLBACK_WITH_FAILURE(cb, data);
}
static void uicc_change_passwd(struct ofono_sim *sim,
enum ofono_sim_password_type passwd_type,
const char *old, const char *new,
ofono_sim_lock_unlock_cb_t cb, void *data)
{
DBG("Not implemented");
CALLBACK_WITH_FAILURE(cb, data);
}
static void uicc_lock(struct ofono_sim *sim, enum ofono_sim_password_type type,
int enable, const char *passwd,
ofono_sim_lock_unlock_cb_t cb, void *data)
{
DBG("Not implemented");
CALLBACK_WITH_FAILURE(cb, data);
}
static void uicc_query_locked(struct ofono_sim *sim,
enum ofono_sim_password_type type,
ofono_sim_locked_cb_t cb, void *data)
{
DBG("Not implemented");
CALLBACK_WITH_FAILURE(cb, -1, data);
}
static gboolean decode_fcp_pin_status(const GIsiSubBlockIter *iter, uint8_t read,
uint8_t *pin1, uint8_t *pin2)
{
uint8_t do_len;
uint8_t len;
uint8_t tag;
uint8_t id;
uint8_t tag_pos;
DBG("Decoding PIN status");
if (!g_isi_sb_iter_get_byte(iter, &do_len, read))
return FALSE;
tag_pos = read + 1 + do_len;
if (!g_isi_sb_iter_get_byte(iter, &tag, tag_pos))
return FALSE;
while (tag == 0x83) {
if (!g_isi_sb_iter_get_byte(iter, &len, tag_pos + 1))
return FALSE;
if (!g_isi_sb_iter_get_byte(iter, &id, tag_pos + 2))
return FALSE;
tag_pos += 2 + len;
if (!g_isi_sb_iter_get_byte(iter, &tag, tag_pos))
return FALSE;
DBG("PIN_len %d, PIN id %02x, PIN tag %02x", len, id, tag);
if (id >= 0x01 && id <= 0x08)
*pin1 = id;
else if (id >= 0x81 && id <= 0x88)
*pin2 = id;
}
return TRUE;
}
static gboolean decode_fci_sb(const GIsiSubBlockIter *iter, int app_type,
uint8_t *pin1, uint8_t *pin2)
{
uint8_t fcp = 0;
uint8_t fcp_len = 0;
uint8_t read = 0;
uint8_t item_len = 0;
DBG("Decoding UICC_SB_FCI");
if (app_type != UICC_APPL_TYPE_UICC_USIM)
return FALSE;
if (!g_isi_sb_iter_get_byte(iter, &fcp, 8))
return FALSE;
if (fcp != FCP_TEMPLATE)
return FALSE;
if (!g_isi_sb_iter_get_byte(iter, &fcp_len, 9))
return FALSE;
for (read = 0; read < fcp_len; read += item_len + 2) {
uint8_t id;
if (!g_isi_sb_iter_get_byte(iter, &id, read + 10))
return FALSE;
if (!g_isi_sb_iter_get_byte(iter, &item_len, read + 11))
return FALSE;
if (id != FCP_PIN_STATUS)
continue;
if (!decode_fcp_pin_status(iter, read + 13, pin1, pin2))
return FALSE;
}
return TRUE;
}
static gboolean decode_chv_sb(const GIsiSubBlockIter *iter, int app_type,
uint8_t *pin1, uint8_t *pin2)
{
uint8_t chv_id = 0;
uint8_t pin_id = 0;
DBG("Decoding UICC_SB_CHV");
if (app_type != UICC_APPL_TYPE_ICC_SIM)
return FALSE;
if (!g_isi_sb_iter_get_byte(iter, &chv_id, 4))
return FALSE;
if (!g_isi_sb_iter_get_byte(iter, &pin_id, 5))
return FALSE;
switch (chv_id) {
case 1:
*pin1 = pin_id;
break;
case 2:
*pin2 = pin_id;
break;
default:
return FALSE;
}
DBG("CHV=%d, pin_id=%2x, PIN1 %02x, PIN2 %02x", chv_id, pin_id, *pin1,
*pin2);
return TRUE;
}
static void uicc_application_activate_resp(const GIsiMessage *msg, void *opaque)
{
struct ofono_sim *sim = opaque;
struct uicc_sim_data *sd = ofono_sim_get_data(sim);
GIsiSubBlockIter iter;
uint8_t cause, num_sb;
DBG("");
if (g_isi_msg_error(msg) < 0) {
DBG("Error: %s", g_isi_msg_strerror(msg));
return;
}
if (g_isi_msg_id(msg) != UICC_APPLICATION_RESP) {
DBG("Unexpected msg: %s",
sim_message_id_name(g_isi_msg_id(msg)));
return;
}
if (!g_isi_msg_data_get_byte(msg, 1, &cause))
return;
if (cause != UICC_STATUS_OK && cause != UICC_STATUS_APPL_ACTIVE) {
DBG("TODO: handle application activation");
return;
}
if (!sd->uicc_app_started) {
sd->app_id = sd->trying_app_id;
sd->app_type = sd->trying_app_type;
sd->uicc_app_started = TRUE;
DBG("UICC application activated");
ofono_sim_inserted_notify(sim, TRUE);
ofono_sim_register(sim);
g_hash_table_remove_all(sd->app_table);
}
if (!g_isi_msg_data_get_byte(msg, 5, &num_sb))
return;
for (g_isi_sb_iter_init_full(&iter, msg, 6, TRUE, num_sb);
g_isi_sb_iter_is_valid(&iter);
g_isi_sb_iter_next(&iter)) {
uint8_t sb_id = g_isi_sb_iter_get_id(&iter);
DBG("Subblock %s", uicc_subblock_name(sb_id));
switch (sb_id) {
case UICC_SB_CLIENT:
if (!g_isi_sb_iter_get_byte(&iter, &sd->client_id, 7))
return;
DBG("Client id %d", sd->client_id);
break;
case UICC_SB_FCI:
if (!decode_fci_sb(&iter, sd->app_type, &sd->pin1_id,
&sd->pin2_id))
return;
DBG("PIN1 %02x, PIN2 %02x", sd->pin1_id, sd->pin2_id);
break;
case UICC_SB_CHV:
if (!decode_chv_sb(&iter, sd->app_type, &sd->pin1_id,
&sd->pin2_id))
return;
DBG("PIN1 %02x, PIN2 %02x", sd->pin1_id, sd->pin2_id);
break;
default:
DBG("Skipping sub-block: %s (%zu bytes)",
uicc_subblock_name(g_isi_sb_iter_get_id(&iter)),
g_isi_sb_iter_get_len(&iter));
break;
}
}
}
static gboolean send_application_activate_req(GIsiClient *client,
uint8_t app_type,
uint8_t app_id,
GIsiNotifyFunc notify,
void *data,
GDestroyNotify destroy)
{
const uint8_t msg[] = {
UICC_APPLICATION_REQ,
UICC_APPL_HOST_ACTIVATE,
2, /* Number of subblocks */
ISI_16BIT(UICC_SB_APPLICATION),
ISI_16BIT(8), /* Subblock length */
0, 0, /* Filler */
app_type,
app_id,
ISI_16BIT(UICC_SB_APPL_INFO),
ISI_16BIT(8), /* Subblock length */
0, 0, 0, /* Filler */
/*
* Next field indicates whether the application
* initialization procedure will follow the activation
* or not
*/
UICC_APPL_START_UP_INIT_PROC,
};
DBG("App type %d, AID %d", app_type, app_id);
return g_isi_client_send(client, msg, sizeof(msg), notify, data, destroy);
}
static void uicc_application_list_resp(const GIsiMessage *msg, void *data)
{
struct ofono_sim *sim = data;
struct uicc_sim_data *sd = ofono_sim_get_data(sim);
GIsiSubBlockIter iter;
uint8_t num_sb;
struct uicc_sim_application *sim_app;
/* Throw away old app table */
g_hash_table_remove_all(sd->app_table);
if (!check_resp(msg, UICC_APPLICATION_RESP, UICC_APPL_LIST))
goto error;
if (!g_isi_msg_data_get_byte(msg, 5, &num_sb))
goto error;
/* Iterate through the application list */
for (g_isi_sb_iter_init_full(&iter, msg, 6, TRUE, num_sb);
g_isi_sb_iter_is_valid(&iter);
g_isi_sb_iter_next(&iter)) {
uint8_t app_type;
uint8_t app_id;
uint8_t app_status;
uint8_t app_len;
if (g_isi_sb_iter_get_id(&iter) != UICC_SB_APPL_DATA_OBJECT)
continue;
if (!g_isi_sb_iter_get_byte(&iter, &app_type, 6))
goto error;
if (!g_isi_sb_iter_get_byte(&iter, &app_id, 7))
goto error;
if (!g_isi_sb_iter_get_byte(&iter, &app_status, 8))
goto error;
if (!g_isi_sb_iter_get_byte(&iter, &app_len, 9))
goto error;
if (app_type != UICC_APPL_TYPE_ICC_SIM &&
app_type != UICC_APPL_TYPE_UICC_USIM)
continue;
sim_app = g_try_new0(struct uicc_sim_application, 1);
if (!sim_app) {
DBG("out of memory!");
goto error;
}
sim_app->type = app_type;
sim_app->id = app_id;
sim_app->status = app_status;
sim_app->length = app_len;
sim_app->sim = sd;
g_hash_table_replace(sd->app_table, &sim_app->id, sim_app);
}
if (!sd->uicc_app_started) {
GHashTableIter app_iter;
struct uicc_sim_application *app;
gpointer key;
gpointer value;
g_hash_table_iter_init(&app_iter, sd->app_table);
if (!g_hash_table_iter_next(&app_iter, &key, &value))
return;
app = value;
sd->trying_app_type = app->type;
sd->trying_app_id = app->id;
g_hash_table_remove(sd->app_table, &app->id);
if (!send_application_activate_req(sd->client, app->type, app->id,
uicc_application_activate_resp,
data, NULL)) {
DBG("Failed to activate: 0x%02X (type=0x%02X)",
app->id, app->type);
return;
}
}
return;
error:
DBG("Decoding application list failed");
g_isi_client_destroy(sd->client);
sd->client = NULL;
ofono_sim_remove(sim);
}
static void uicc_card_status_resp(const GIsiMessage *msg, void *data)
{
struct ofono_sim *sim = data;
struct uicc_sim_data *sd = ofono_sim_get_data(sim);
GIsiSubBlockIter iter;
uint8_t card_status = 0;
uint8_t num_sb = 0;
DBG("");
if (!sd->server_running)
return;
if (!check_resp(msg, UICC_CARD_RESP, UICC_CARD_STATUS_GET))
goto error;
if (!g_isi_msg_data_get_byte(msg, 1, &card_status))
goto error;
if (!g_isi_msg_data_get_byte(msg, 5, &num_sb))
goto error;
DBG("Subblock count %d", num_sb);
for (g_isi_sb_iter_init_full(&iter, msg, 6, TRUE, num_sb);
g_isi_sb_iter_is_valid(&iter);
g_isi_sb_iter_next(&iter)) {
if (g_isi_sb_iter_get_id(&iter) != UICC_SB_CARD_STATUS)
continue;
if (!g_isi_sb_iter_get_byte(&iter, &card_status, 7))
goto error;
DBG("card_status = 0x%X", card_status);
/* Check if card is ready */
if (card_status == 0x21) {
const uint8_t req[] = {
UICC_APPLICATION_REQ,
UICC_APPL_LIST,
0, /* Number of subblocks */
};
DBG("card is ready");
ofono_sim_inserted_notify(sim, TRUE);
if (g_isi_client_send(sd->client, req, sizeof(req),
uicc_application_list_resp,
data, NULL))
return;
DBG("Failed to query application list");
goto error;
} else {
DBG("card not ready");
ofono_sim_inserted_notify(sim, FALSE);
return;
}
}
error:
g_isi_client_destroy(sd->client);
sd->client = NULL;
ofono_sim_remove(sim);
}
static void uicc_card_status_req(struct ofono_sim *sim,
struct uicc_sim_data *sd)
{
const uint8_t req[] = {
UICC_CARD_REQ,
UICC_CARD_STATUS_GET,
0,
};
DBG("");
if (g_isi_client_send(sd->client, req, sizeof(req),
uicc_card_status_resp, sim, NULL))
return;
g_isi_client_destroy(sd->client);
sd->client = NULL;
ofono_sim_remove(sim);
}
static void uicc_card_ind_cb(const GIsiMessage *msg, void *data)
{
struct ofono_sim *sim = data;
struct uicc_sim_data *sd = ofono_sim_get_data(sim);
DBG("");
if (g_isi_msg_id(msg) != UICC_CARD_IND)
return;
/* We're not interested in card indications if server isn't running */
if (!sd->server_running)
return;
/* Request card status */
uicc_card_status_req(sim, sd);
}
static void uicc_status_resp(const GIsiMessage *msg, void *data)
{
struct ofono_sim *sim = data;
struct uicc_sim_data *sd = ofono_sim_get_data(sim);
uint8_t status = 0, server_status = 0;
gboolean server_running = FALSE;
if (!check_resp(msg, UICC_RESP, UICC_STATUS_GET))
goto error;
if (g_isi_msg_error(msg) < 0)
goto error;
if (!g_isi_msg_data_get_byte(msg, 1, &status) ||
!g_isi_msg_data_get_byte(msg, 3, &server_status))
goto error;
DBG("status=0x%X, server_status=0x%X", status, server_status);
if (status == UICC_STATUS_OK &&
server_status == UICC_STATUS_START_UP_COMPLETED) {
DBG("server is up!");
server_running = TRUE;
}
if (!server_running) {
sd->server_running = FALSE;
/* TODO: Remove SIM etc... */
return;
}
if (sd->server_running && server_running) {
DBG("Server status didn't change...");
return;
}
/* Server is running */
sd->server_running = TRUE;
/* Request card status */
uicc_card_status_req(sim, sd);
return;
error:
g_isi_client_destroy(sd->client);
sd->client = NULL;
ofono_sim_remove(sim);
}
static void uicc_ind_cb(const GIsiMessage *msg, void *data)
{
struct ofono_sim *sim = data;
struct uicc_sim_data *sd = ofono_sim_get_data(sim);
const uint8_t req[] = { UICC_REQ, UICC_STATUS_GET, 0 };
int msg_id = g_isi_msg_id(msg);
DBG("%s", uicc_message_id_name(msg_id));
if (msg_id != UICC_IND)
return;
/* Request status */
if (g_isi_client_send(sd->client, req, sizeof(req), uicc_status_resp,
data, NULL))
return;
DBG("status request failed!");
g_isi_client_destroy(sd->client);
sd->client = NULL;
ofono_sim_remove(sim);
}
static void uicc_reachable_cb(const GIsiMessage *msg, void *data)
{
struct ofono_sim *sim = data;
struct uicc_sim_data *sd = ofono_sim_get_data(sim);
const uint8_t req[] = {
UICC_REQ,
UICC_STATUS_GET,
0, /* Number of Sub Blocks (only from version 4.0) */
};
ISI_RESOURCE_DBG(msg);
if (g_isi_msg_error(msg) < 0)
goto error;
sd->version.major = g_isi_msg_version_major(msg);
sd->version.minor = g_isi_msg_version_minor(msg);
/* UICC server is reachable: request indications */
g_isi_client_ind_subscribe(sd->client, UICC_IND, uicc_ind_cb, sim);
g_isi_client_ind_subscribe(sd->client, UICC_CARD_IND, uicc_card_ind_cb,
sim);
/* Update status */
if (g_isi_client_send(sd->client, req,
sizeof(req) - ((sd->version.major < 4) ? 1 : 0),
uicc_status_resp, data, NULL))
return;
error:
g_isi_client_destroy(sd->client);
sd->client = NULL;
ofono_sim_remove(sim);
}
static void sim_app_destroy(gpointer p)
{
struct uicc_sim_application *app = p;
if (!app)
return;
g_free(app);
}
static int uicc_sim_probe(struct ofono_sim *sim, unsigned int vendor,
void *user)
{
GIsiModem *modem = user;
struct uicc_sim_data *sd;
sd = g_try_new0(struct uicc_sim_data, 1);
if (sd == NULL)
return -ENOMEM;
/* Create hash table for the UICC applications */
sd->app_table = g_hash_table_new_full(g_int_hash, g_int_equal, NULL,
sim_app_destroy);
if (sd->app_table == NULL) {
g_free(sd);
return -ENOMEM;
}
sd->client = g_isi_client_create(modem, PN_UICC);
if (sd->client == NULL) {
g_hash_table_destroy(sd->app_table);
g_free(sd);
return -ENOMEM;
}
g_hash_table_insert(g_modems, g_isi_client_modem(sd->client), sim);
sd->server_running = FALSE;
sd->uicc_app_started = FALSE;
sd->pin_state_received = FALSE;
sd->passwd_required = TRUE;
ofono_sim_set_data(sim, sd);
g_isi_client_verify(sd->client, uicc_reachable_cb, sim, NULL);
return 0;
}
static void uicc_sim_remove(struct ofono_sim *sim)
{
struct uicc_sim_data *data = ofono_sim_get_data(sim);
ofono_sim_set_data(sim, NULL);
if (data == NULL)
return;
g_hash_table_remove(g_modems, g_isi_client_modem(data->client));
g_hash_table_destroy(data->app_table);
g_isi_client_destroy(data->client);
g_free(data);
}
static struct ofono_sim_driver driver = {
.name = "wgmodem2.5",
.probe = uicc_sim_probe,
.remove = uicc_sim_remove,
.read_file_info = uicc_read_file_info,
.read_file_transparent = uicc_read_file_transparent,
.read_file_linear = uicc_read_file_linear,
.read_file_cyclic = uicc_read_file_cyclic,
.write_file_transparent = uicc_write_file_transparent,
.write_file_linear = uicc_write_file_linear,
.write_file_cyclic = uicc_write_file_cyclic,
.read_imsi = uicc_read_imsi,
.query_passwd_state = uicc_query_passwd_state,
.send_passwd = uicc_send_passwd,
.query_pin_retries = uicc_query_pin_retries,
.reset_passwd = uicc_reset_passwd,
.change_passwd = uicc_change_passwd,
.lock = uicc_lock,
.query_locked = uicc_query_locked,
};
void isi_uicc_init(void)
{
g_modems = g_hash_table_new(g_direct_hash, g_direct_equal);
ofono_sim_driver_register(&driver);
}
void isi_uicc_exit(void)
{
g_hash_table_destroy(g_modems);
ofono_sim_driver_unregister(&driver);
}
gboolean isi_uicc_properties(GIsiModem *modem, int *app_id, int *app_type,
int *client_id)
{
struct ofono_sim *sim;
struct uicc_sim_data *sd;
sim = g_hash_table_lookup(g_modems, modem);
if (sim == NULL)
return FALSE;
sd = ofono_sim_get_data(sim);
if (sd == NULL)
return FALSE;
if (app_id != NULL)
*app_id = sd->app_id;
if (app_type != NULL)
*app_type = sd->app_type;
if (client_id != NULL)
*client_id = sd->client_id;
return TRUE;
}
|