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 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 | /* * Copyright (c) 2019-2020 Foundries.io * * SPDX-License-Identifier: Apache-2.0 */ #define DT_DRV_COMPAT u_blox_sara_r4 #include <zephyr/logging/log.h> LOG_MODULE_REGISTER(modem_ublox_sara_r4, CONFIG_MODEM_LOG_LEVEL); #include <zephyr/kernel.h> #include <ctype.h> #include <errno.h> #include <zephyr/zephyr.h> #include <zephyr/drivers/gpio.h> #include <zephyr/device.h> #include <zephyr/init.h> #include <fcntl.h> #include <zephyr/net/net_if.h> #include <zephyr/net/net_offload.h> #include <zephyr/net/socket_offload.h> #if defined(CONFIG_MODEM_UBLOX_SARA_AUTODETECT_APN) #include <stdio.h> #endif #include "modem_context.h" #include "modem_socket.h" #include "modem_cmd_handler.h" #include "modem_iface_uart.h" #if !defined(CONFIG_MODEM_UBLOX_SARA_R4_MANUAL_MCCMNO) #define CONFIG_MODEM_UBLOX_SARA_R4_MANUAL_MCCMNO "" #endif #if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) #include "tls_internal.h" #include <zephyr/net/tls_credentials.h> #endif /* pin settings */ enum mdm_control_pins { MDM_POWER = 0, #if DT_INST_NODE_HAS_PROP(0, mdm_reset_gpios) MDM_RESET, #endif #if DT_INST_NODE_HAS_PROP(0, mdm_vint_gpios) MDM_VINT, #endif }; static struct modem_pin modem_pins[] = { /* MDM_POWER */ MODEM_PIN(DT_INST_GPIO_LABEL(0, mdm_power_gpios), DT_INST_GPIO_PIN(0, mdm_power_gpios), DT_INST_GPIO_FLAGS(0, mdm_power_gpios) | GPIO_OUTPUT), #if DT_INST_NODE_HAS_PROP(0, mdm_reset_gpios) /* MDM_RESET */ MODEM_PIN(DT_INST_GPIO_LABEL(0, mdm_reset_gpios), DT_INST_GPIO_PIN(0, mdm_reset_gpios), DT_INST_GPIO_FLAGS(0, mdm_reset_gpios) | GPIO_OUTPUT), #endif #if DT_INST_NODE_HAS_PROP(0, mdm_vint_gpios) /* MDM_VINT */ MODEM_PIN(DT_INST_GPIO_LABEL(0, mdm_vint_gpios), DT_INST_GPIO_PIN(0, mdm_vint_gpios), DT_INST_GPIO_FLAGS(0, mdm_vint_gpios) | GPIO_INPUT), #endif }; #define MDM_UART_NODE DT_INST_BUS(0) #define MDM_UART_DEV DEVICE_DT_GET(MDM_UART_NODE) #define MDM_POWER_ENABLE 1 #define MDM_POWER_DISABLE 0 #define MDM_RESET_NOT_ASSERTED 1 #define MDM_RESET_ASSERTED 0 #define MDM_CMD_TIMEOUT K_SECONDS(10) #define MDM_DNS_TIMEOUT K_SECONDS(70) #define MDM_CMD_CONN_TIMEOUT K_SECONDS(120) #define MDM_REGISTRATION_TIMEOUT K_SECONDS(180) #define MDM_PROMPT_CMD_DELAY K_MSEC(50) #define MDM_MAX_DATA_LENGTH 1024 #define MDM_RECV_MAX_BUF 30 #define MDM_RECV_BUF_SIZE 128 #define MDM_MAX_SOCKETS 6 #define MDM_BASE_SOCKET_NUM 0 #define MDM_NETWORK_RETRY_COUNT 3 #define MDM_WAIT_FOR_RSSI_COUNT 10 #define MDM_WAIT_FOR_RSSI_DELAY K_SECONDS(2) #define MDM_MANUFACTURER_LENGTH 10 #define MDM_MODEL_LENGTH 16 #define MDM_REVISION_LENGTH 64 #define MDM_IMEI_LENGTH 16 #define MDM_IMSI_LENGTH 16 #define MDM_APN_LENGTH 32 #define MDM_MAX_CERT_LENGTH 8192 #if defined(CONFIG_MODEM_UBLOX_SARA_AUTODETECT_VARIANT) #define MDM_VARIANT_UBLOX_R4 4 #define MDM_VARIANT_UBLOX_U2 2 #endif NET_BUF_POOL_DEFINE(mdm_recv_pool, MDM_RECV_MAX_BUF, MDM_RECV_BUF_SIZE, 0, NULL); /* RX thread structures */ K_KERNEL_STACK_DEFINE(modem_rx_stack, CONFIG_MODEM_UBLOX_SARA_R4_RX_STACK_SIZE); struct k_thread modem_rx_thread; #if defined(CONFIG_MODEM_UBLOX_SARA_RSSI_WORK) /* RX thread work queue */ K_KERNEL_STACK_DEFINE(modem_workq_stack, CONFIG_MODEM_UBLOX_SARA_R4_RX_WORKQ_STACK_SIZE); static struct k_work_q modem_workq; #endif /* socket read callback data */ struct socket_read_data { char *recv_buf; size_t recv_buf_len; struct sockaddr *recv_addr; uint16_t recv_read_len; }; /* driver data */ struct modem_data { struct net_if *net_iface; uint8_t mac_addr[6]; /* modem interface */ struct modem_iface_uart_data iface_data; uint8_t iface_rb_buf[MDM_MAX_DATA_LENGTH]; /* modem cmds */ struct modem_cmd_handler_data cmd_handler_data; uint8_t cmd_match_buf[MDM_RECV_BUF_SIZE + 1]; /* socket data */ struct modem_socket_config socket_config; struct modem_socket sockets[MDM_MAX_SOCKETS]; #if defined(CONFIG_MODEM_UBLOX_SARA_RSSI_WORK) /* RSSI work */ struct k_work_delayable rssi_query_work; #endif /* modem data */ char mdm_manufacturer[MDM_MANUFACTURER_LENGTH]; char mdm_model[MDM_MODEL_LENGTH]; char mdm_revision[MDM_REVISION_LENGTH]; char mdm_imei[MDM_IMEI_LENGTH]; char mdm_imsi[MDM_IMSI_LENGTH]; int mdm_rssi; #if defined(CONFIG_MODEM_UBLOX_SARA_AUTODETECT_VARIANT) /* modem variant */ int mdm_variant; #endif #if defined(CONFIG_MODEM_UBLOX_SARA_AUTODETECT_APN) /* APN */ char mdm_apn[MDM_APN_LENGTH]; #endif /* modem state */ int ev_creg; /* bytes written to socket in last transaction */ int sock_written; /* response semaphore */ struct k_sem sem_response; /* prompt semaphore */ struct k_sem sem_prompt; }; static struct modem_data mdata; static struct modem_context mctx; #if defined(CONFIG_DNS_RESOLVER) static struct zsock_addrinfo result; static struct sockaddr result_addr; static char result_canonname[DNS_MAX_NAME_SIZE + 1]; #endif /* helper macro to keep readability */ #define ATOI(s_, value_, desc_) modem_atoi(s_, value_, desc_, __func__) /** * @brief Convert string to long integer, but handle errors * * @param s: string with representation of integer number * @param err_value: on error return this value instead * @param desc: name the string being converted * @param func: function where this is called (typically __func__) * * @retval return integer conversion on success, or err_value on error */ static int modem_atoi(const char *s, const int err_value, const char *desc, const char *func) { int ret; char *endptr; ret = (int)strtol(s, &endptr, 10); if (!endptr || *endptr != '\0') { LOG_ERR("bad %s '%s' in %s", log_strdup(s), log_strdup(desc), log_strdup(func)); return err_value; } return ret; } #if defined(CONFIG_MODEM_UBLOX_SARA_AUTODETECT_APN) /* the list of SIM profiles. Global scope, so the app can change it */ const char *modem_sim_profiles = CONFIG_MODEM_UBLOX_SARA_AUTODETECT_APN_PROFILES; int find_apn(char *apn, int apnlen, const char *profiles, const char *imsi) { int rc = -1; /* try to find a match */ char *s = strstr(profiles, imsi); if (s) { char *eos; /* find the assignment operator preceding the match */ while (s >= profiles && !strchr("=", *s)) { s--; } /* find the apn preceding the assignment operator */ while (s >= profiles && strchr(" =", *s)) { s--; } /* mark end of apn string */ eos = s+1; /* find first character of the apn */ while (s >= profiles && !strchr(" ,", *s)) { s--; } s++; /* copy the key */ if (s >= profiles) { int len = eos - s; if (len < apnlen) { memcpy(apn, s, len); apn[len] = '\0'; rc = 0; } else { LOG_ERR("buffer overflow"); } } } return rc; } /* try to detect APN automatically, based on IMSI */ int modem_detect_apn(const char *imsi) { int rc = -1; if (imsi != NULL && strlen(imsi) >= 5) { /* extract MMC and MNC from IMSI */ char mmcmnc[6]; *mmcmnc = 0; strncat(mmcmnc, imsi, sizeof(mmcmnc)-1); /* try to find a matching IMSI, and assign the APN */ rc = find_apn(mdata.mdm_apn, sizeof(mdata.mdm_apn), modem_sim_profiles, mmcmnc); if (rc < 0) { rc = find_apn(mdata.mdm_apn, sizeof(mdata.mdm_apn), modem_sim_profiles, "*"); } } if (rc == 0) { LOG_INF("Assign APN: \"%s\"", log_strdup(mdata.mdm_apn)); } return rc; } #endif /* Forward declaration */ MODEM_CMD_DEFINE(on_cmd_sockwrite); /* send binary data via the +USO[ST/WR] commands */ static ssize_t send_socket_data(void *obj, const struct msghdr *msg, k_timeout_t timeout) { int ret; char send_buf[sizeof("AT+USO**=#,!###.###.###.###!,#####,####\r\n")]; uint16_t dst_port = 0U; struct modem_socket *sock = (struct modem_socket *)obj; const struct modem_cmd handler_cmds[] = { MODEM_CMD("+USOST: ", on_cmd_sockwrite, 2U, ","), MODEM_CMD("+USOWR: ", on_cmd_sockwrite, 2U, ","), }; struct sockaddr *dst_addr = msg->msg_name; size_t buf_len = 0; if (!sock) { return -EINVAL; } for (int i = 0; i < msg->msg_iovlen; i++) { if (!msg->msg_iov[i].iov_base || msg->msg_iov[i].iov_len == 0) { errno = EINVAL; return -1; } buf_len += msg->msg_iov[i].iov_len; } if (!sock->is_connected && sock->ip_proto != IPPROTO_UDP) { errno = ENOTCONN; return -1; } if (!dst_addr && sock->ip_proto == IPPROTO_UDP) { dst_addr = &sock->dst; } /* * Binary and ASCII mode allows sending MDM_MAX_DATA_LENGTH bytes to * the socket in one command */ if (buf_len > MDM_MAX_DATA_LENGTH) { buf_len = MDM_MAX_DATA_LENGTH; } /* The number of bytes written will be reported by the modem */ mdata.sock_written = 0; if (sock->ip_proto == IPPROTO_UDP) { char ip_str[NET_IPV6_ADDR_LEN]; ret = modem_context_sprint_ip_addr(dst_addr, ip_str, sizeof(ip_str)); if (ret != 0) { LOG_ERR("Error formatting IP string %d", ret); goto exit; } ret = modem_context_get_addr_port(dst_addr, &dst_port); snprintk(send_buf, sizeof(send_buf), "AT+USOST=%d,\"%s\",%u,%zu", sock->id, ip_str, dst_port, buf_len); } else { snprintk(send_buf, sizeof(send_buf), "AT+USOWR=%d,%zu", sock->id, buf_len); } k_sem_take(&mdata.cmd_handler_data.sem_tx_lock, K_FOREVER); /* Reset prompt '@' semaphore */ k_sem_reset(&mdata.sem_prompt); ret = modem_cmd_send_nolock(&mctx.iface, &mctx.cmd_handler, NULL, 0U, send_buf, NULL, K_NO_WAIT); if (ret < 0) { goto exit; } /* set command handlers */ ret = modem_cmd_handler_update_cmds(&mdata.cmd_handler_data, handler_cmds, ARRAY_SIZE(handler_cmds), true); if (ret < 0) { goto exit; } /* Wait for prompt '@' */ ret = k_sem_take(&mdata.sem_prompt, K_SECONDS(1)); if (ret != 0) { ret = -ETIMEDOUT; LOG_ERR("No @ prompt received"); goto exit; } /* * The AT commands manual requires a 50 ms wait * after '@' prompt if using AT+USOWR, but not * if using AT+USOST. This if condition is matched with * the command selection above. */ if (sock->ip_proto != IPPROTO_UDP) { k_sleep(MDM_PROMPT_CMD_DELAY); } /* Reset response semaphore before sending data * So that we are sure that we won't use a previously pending one * And we won't miss the one that is going to be freed */ k_sem_reset(&mdata.sem_response); /* Send data directly on modem iface */ for (int i = 0; i < msg->msg_iovlen; i++) { int len = MIN(buf_len, msg->msg_iov[i].iov_len); if (len == 0) { break; } mctx.iface.write(&mctx.iface, msg->msg_iov[i].iov_base, len); buf_len -= len; } if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) { ret = 0; goto exit; } ret = k_sem_take(&mdata.sem_response, timeout); if (ret == 0) { ret = modem_cmd_handler_get_error(&mdata.cmd_handler_data); } else if (ret == -EAGAIN) { ret = -ETIMEDOUT; } exit: /* unset handler commands and ignore any errors */ (void)modem_cmd_handler_update_cmds(&mdata.cmd_handler_data, NULL, 0U, false); k_sem_give(&mdata.cmd_handler_data.sem_tx_lock); if (ret < 0) { return ret; } return mdata.sock_written; } #if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) /* send binary data via the +USO[ST/WR] commands */ static ssize_t send_cert(struct modem_socket *sock, struct modem_cmd *handler_cmds, size_t handler_cmds_len, const char *cert_data, size_t cert_len, int cert_type) { int ret; char *filename = "ca"; char send_buf[sizeof("AT+USECMNG=#,#,!####!,####\r\n")]; /* TODO support other cert types as well */ if (cert_type != 0) { return -EINVAL; } if (!sock) { return -EINVAL; } __ASSERT_NO_MSG(cert_len <= MDM_MAX_CERT_LENGTH); snprintk(send_buf, sizeof(send_buf), "AT+USECMNG=0,%d,\"%s\",%d", cert_type, filename, cert_len); k_sem_take(&mdata.cmd_handler_data.sem_tx_lock, K_FOREVER); ret = modem_cmd_send_nolock(&mctx.iface, &mctx.cmd_handler, NULL, 0U, send_buf, NULL, K_NO_WAIT); if (ret < 0) { goto exit; } /* set command handlers */ ret = modem_cmd_handler_update_cmds(&mdata.cmd_handler_data, handler_cmds, handler_cmds_len, true); if (ret < 0) { goto exit; } /* slight pause per spec so that @ prompt is received */ k_sleep(MDM_PROMPT_CMD_DELAY); mctx.iface.write(&mctx.iface, cert_data, cert_len); k_sem_reset(&mdata.sem_response); ret = k_sem_take(&mdata.sem_response, K_MSEC(1000)); if (ret == 0) { ret = modem_cmd_handler_get_error(&mdata.cmd_handler_data); } else if (ret == -EAGAIN) { ret = -ETIMEDOUT; } exit: /* unset handler commands and ignore any errors */ (void)modem_cmd_handler_update_cmds(&mdata.cmd_handler_data, NULL, 0U, false); k_sem_give(&mdata.cmd_handler_data.sem_tx_lock); return ret; } #endif /* * Modem Response Command Handlers */ /* Handler: OK */ MODEM_CMD_DEFINE(on_cmd_ok) { modem_cmd_handler_set_error(data, 0); k_sem_give(&mdata.sem_response); return 0; } /* Handler: @ */ MODEM_CMD_DEFINE(on_prompt) { k_sem_give(&mdata.sem_prompt); /* A direct cmd should return the number of byte processed. * Therefore, here we always return 1 */ return 1; } /* Handler: ERROR */ MODEM_CMD_DEFINE(on_cmd_error) { modem_cmd_handler_set_error(data, -EIO); k_sem_give(&mdata.sem_response); return 0; } /* Handler: +CME Error: <err>[0] */ MODEM_CMD_DEFINE(on_cmd_exterror) { /* TODO: map extended error codes to values */ modem_cmd_handler_set_error(data, -EIO); k_sem_give(&mdata.sem_response); return 0; } /* * Modem Info Command Handlers */ /* Handler: <manufacturer> */ MODEM_CMD_DEFINE(on_cmd_atcmdinfo_manufacturer) { size_t out_len; out_len = net_buf_linearize(mdata.mdm_manufacturer, sizeof(mdata.mdm_manufacturer) - 1, data->rx_buf, 0, len); mdata.mdm_manufacturer[out_len] = '\0'; LOG_INF("Manufacturer: %s", log_strdup(mdata.mdm_manufacturer)); return 0; } /* Handler: <model> */ MODEM_CMD_DEFINE(on_cmd_atcmdinfo_model) { size_t out_len; out_len = net_buf_linearize(mdata.mdm_model, sizeof(mdata.mdm_model) - 1, data->rx_buf, 0, len); mdata.mdm_model[out_len] = '\0'; LOG_INF("Model: %s", log_strdup(mdata.mdm_model)); #if defined(CONFIG_MODEM_UBLOX_SARA_AUTODETECT_VARIANT) /* Set modem type */ if (strstr(mdata.mdm_model, "R4")) { mdata.mdm_variant = MDM_VARIANT_UBLOX_R4; } else { if (strstr(mdata.mdm_model, "U2")) { mdata.mdm_variant = MDM_VARIANT_UBLOX_U2; } } LOG_INF("Variant: %d", mdata.mdm_variant); #endif return 0; } /* Handler: <rev> */ MODEM_CMD_DEFINE(on_cmd_atcmdinfo_revision) { size_t out_len; out_len = net_buf_linearize(mdata.mdm_revision, sizeof(mdata.mdm_revision) - 1, data->rx_buf, 0, len); mdata.mdm_revision[out_len] = '\0'; LOG_INF("Revision: %s", log_strdup(mdata.mdm_revision)); return 0; } /* Handler: <IMEI> */ MODEM_CMD_DEFINE(on_cmd_atcmdinfo_imei) { size_t out_len; out_len = net_buf_linearize(mdata.mdm_imei, sizeof(mdata.mdm_imei) - 1, data->rx_buf, 0, len); mdata.mdm_imei[out_len] = '\0'; LOG_INF("IMEI: %s", log_strdup(mdata.mdm_imei)); return 0; } /* Handler: <IMSI> */ MODEM_CMD_DEFINE(on_cmd_atcmdinfo_imsi) { size_t out_len; out_len = net_buf_linearize(mdata.mdm_imsi, sizeof(mdata.mdm_imsi) - 1, data->rx_buf, 0, len); mdata.mdm_imsi[out_len] = '\0'; LOG_INF("IMSI: %s", log_strdup(mdata.mdm_imsi)); #if defined(CONFIG_MODEM_UBLOX_SARA_AUTODETECT_APN) /* set the APN automatically */ modem_detect_apn(mdata.mdm_imsi); #endif return 0; } #if !defined(CONFIG_MODEM_UBLOX_SARA_U2) /* * Handler: +CESQ: <rxlev>[0],<ber>[1],<rscp>[2],<ecn0>[3],<rsrq>[4],<rsrp>[5] */ MODEM_CMD_DEFINE(on_cmd_atcmdinfo_rssi_cesq) { int rsrp, rxlev; rsrp = ATOI(argv[5], 0, "rsrp"); rxlev = ATOI(argv[0], 0, "rxlev"); if (rsrp >= 0 && rsrp <= 97) { mdata.mdm_rssi = -140 + (rsrp - 1); LOG_INF("RSRP: %d", mdata.mdm_rssi); } else if (rxlev >= 0 && rxlev <= 63) { mdata.mdm_rssi = -110 + (rxlev - 1); LOG_INF("RSSI: %d", mdata.mdm_rssi); } else { mdata.mdm_rssi = -1000; LOG_INF("RSRP/RSSI not known"); } return 0; } #endif #if defined(CONFIG_MODEM_UBLOX_SARA_U2) \ || defined(CONFIG_MODEM_UBLOX_SARA_AUTODETECT_VARIANT) /* Handler: +CSQ: <signal_power>[0],<qual>[1] */ MODEM_CMD_DEFINE(on_cmd_atcmdinfo_rssi_csq) { int rssi; rssi = ATOI(argv[0], 0, "signal_power"); if (rssi == 31) { mdata.mdm_rssi = -46; } else if (rssi >= 0 && rssi <= 31) { /* FIXME: This value depends on the RAT */ mdata.mdm_rssi = -110 + ((rssi * 2) + 1); } else { mdata.mdm_rssi = -1000; } LOG_INF("RSSI: %d", mdata.mdm_rssi); return 0; } #endif #if defined(CONFIG_MODEM_CELL_INFO) static int unquoted_atoi(const char *s, int base) { if (*s == '"') { s++; } return strtol(s, NULL, base); } /* * Handler: +COPS: <mode>[0],<format>[1],<oper>[2] */ MODEM_CMD_DEFINE(on_cmd_atcmdinfo_cops) { if (argc >= 3) { mctx.data_operator = unquoted_atoi(argv[2], 10); LOG_INF("operator: %u", mctx.data_operator); } return 0; } /* * Handler: +CEREG: <n>[0],<stat>[1],<tac>[2],<ci>[3],<AcT>[4] */ MODEM_CMD_DEFINE(on_cmd_atcmdinfo_cereg) { if (argc >= 4) { mctx.data_lac = unquoted_atoi(argv[2], 16); mctx.data_cellid = unquoted_atoi(argv[3], 16); LOG_INF("lac: %u, cellid: %u", mctx.data_lac, mctx.data_cellid); } return 0; } static const struct setup_cmd query_cellinfo_cmds[] = { SETUP_CMD_NOHANDLE("AT+CEREG=2"), SETUP_CMD("AT+CEREG?", "", on_cmd_atcmdinfo_cereg, 5U, ","), SETUP_CMD_NOHANDLE("AT+COPS=3,2"), SETUP_CMD("AT+COPS?", "", on_cmd_atcmdinfo_cops, 3U, ","), }; #endif /* CONFIG_MODEM_CELL_INFO */ /* * Modem Socket Command Handlers */ /* Handler: +USOCR: <socket_id>[0] */ MODEM_CMD_DEFINE(on_cmd_sockcreate) { struct modem_socket *sock = NULL; /* look up new socket by special id */ sock = modem_socket_from_newid(&mdata.socket_config); if (sock) { sock->id = ATOI(argv[0], mdata.socket_config.base_socket_num - 1, "socket_id"); /* on error give up modem socket */ if (sock->id == mdata.socket_config.base_socket_num - 1) { modem_socket_put(&mdata.socket_config, sock->sock_fd); } } /* don't give back semaphore -- OK to follow */ return 0; } /* Handler: +USO[WR|ST]: <socket_id>[0],<length>[1] */ MODEM_CMD_DEFINE(on_cmd_sockwrite) { mdata.sock_written = ATOI(argv[1], 0, "length"); LOG_DBG("bytes written: %d", mdata.sock_written); return 0; } #if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) /* Handler: +USECMNG: 0,<type>[0],<internal_name>[1],<md5_string>[2] */ MODEM_CMD_DEFINE(on_cmd_cert_write) { LOG_DBG("cert md5: %s", log_strdup(argv[2])); return 0; } #endif /* Common code for +USOR[D|F]: "<data>" */ static int on_cmd_sockread_common(int socket_id, struct modem_cmd_handler_data *data, int socket_data_length, uint16_t len) { struct modem_socket *sock = NULL; struct socket_read_data *sock_data; int ret; if (!len) { LOG_ERR("Short +USOR[D|F] value. Aborting!"); return -EAGAIN; } /* * make sure we still have buf data and next char in the buffer is a * quote. */ if (!data->rx_buf || *data->rx_buf->data != '\"') { LOG_ERR("Incorrect format! Ignoring data!"); return -EINVAL; } /* zero length */ if (socket_data_length <= 0) { LOG_ERR("Length problem (%d). Aborting!", socket_data_length); return -EAGAIN; } /* check to make sure we have all of the data (minus quotes) */ if ((net_buf_frags_len(data->rx_buf) - 2) < socket_data_length) { LOG_DBG("Not enough data -- wait!"); return -EAGAIN; } /* skip quote */ len--; net_buf_pull_u8(data->rx_buf); if (!data->rx_buf->len) { data->rx_buf = net_buf_frag_del(NULL, data->rx_buf); } sock = modem_socket_from_id(&mdata.socket_config, socket_id); if (!sock) { LOG_ERR("Socket not found! (%d)", socket_id); ret = -EINVAL; goto exit; } sock_data = (struct socket_read_data *)sock->data; if (!sock_data) { LOG_ERR("Socket data not found! Skip handling (%d)", socket_id); ret = -EINVAL; goto exit; } ret = net_buf_linearize(sock_data->recv_buf, sock_data->recv_buf_len, data->rx_buf, 0, (uint16_t)socket_data_length); data->rx_buf = net_buf_skip(data->rx_buf, ret); sock_data->recv_read_len = ret; if (ret != socket_data_length) { LOG_ERR("Total copied data is different then received data!" " copied:%d vs. received:%d", ret, socket_data_length); ret = -EINVAL; } exit: /* remove packet from list (ignore errors) */ (void)modem_socket_packet_size_update(&mdata.socket_config, sock, -socket_data_length); /* don't give back semaphore -- OK to follow */ return ret; } /* * Handler: +USORF: <socket_id>[0],<remote_ip_addr>[1],<remote_port>[2], * <length>[3],"<data>" */ MODEM_CMD_DEFINE(on_cmd_sockreadfrom) { /* TODO: handle remote_ip_addr */ return on_cmd_sockread_common(ATOI(argv[0], 0, "socket_id"), data, ATOI(argv[3], 0, "length"), len); } /* Handler: +USORD: <socket_id>[0],<length>[1],"<data>" */ MODEM_CMD_DEFINE(on_cmd_sockread) { return on_cmd_sockread_common(ATOI(argv[0], 0, "socket_id"), data, ATOI(argv[1], 0, "length"), len); } #if defined(CONFIG_DNS_RESOLVER) /* Handler: +UDNSRN: "<resolved_ip_address>"[0], "<resolved_ip_address>"[1] */ MODEM_CMD_DEFINE(on_cmd_dns) { /* chop off end quote */ argv[0][strlen(argv[0]) - 1] = '\0'; /* FIXME: Hard-code DNS on SARA-R4 to return IPv4 */ result_addr.sa_family = AF_INET; /* skip beginning quote when parsing */ (void)net_addr_pton(result.ai_family, &argv[0][1], &((struct sockaddr_in *)&result_addr)->sin_addr); return 0; } #endif /* * MODEM UNSOLICITED NOTIFICATION HANDLERS */ /* Handler: +UUSOCL: <socket_id>[0] */ MODEM_CMD_DEFINE(on_cmd_socknotifyclose) { struct modem_socket *sock; sock = modem_socket_from_id(&mdata.socket_config, ATOI(argv[0], 0, "socket_id")); if (sock) { sock->is_connected = false; } return 0; } /* Handler: +UUSOR[D|F]: <socket_id>[0],<length>[1] */ MODEM_CMD_DEFINE(on_cmd_socknotifydata) { int ret, socket_id, new_total; struct modem_socket *sock; socket_id = ATOI(argv[0], 0, "socket_id"); new_total = ATOI(argv[1], 0, "length"); sock = modem_socket_from_id(&mdata.socket_config, socket_id); if (!sock) { return 0; } ret = modem_socket_packet_size_update(&mdata.socket_config, sock, new_total); if (ret < 0) { LOG_ERR("socket_id:%d left_bytes:%d err: %d", socket_id, new_total, ret); } if (new_total > 0) { modem_socket_data_ready(&mdata.socket_config, sock); } return 0; } /* Handler: +CREG: <stat>[0] */ MODEM_CMD_DEFINE(on_cmd_socknotifycreg) { mdata.ev_creg = ATOI(argv[0], 0, "stat"); LOG_DBG("CREG:%d", mdata.ev_creg); return 0; } /* RX thread */ static void modem_rx(void) { while (true) { /* wait for incoming data */ k_sem_take(&mdata.iface_data.rx_sem, K_FOREVER); mctx.cmd_handler.process(&mctx.cmd_handler, &mctx.iface); /* give up time if we have a solid stream of data */ k_yield(); } } static int pin_init(void) { LOG_INF("Setting Modem Pins"); #if DT_INST_NODE_HAS_PROP(0, mdm_reset_gpios) LOG_DBG("MDM_RESET_PIN -> NOT_ASSERTED"); modem_pin_write(&mctx, MDM_RESET, MDM_RESET_NOT_ASSERTED); #endif LOG_DBG("MDM_POWER_PIN -> ENABLE"); modem_pin_write(&mctx, MDM_POWER, MDM_POWER_ENABLE); k_sleep(K_SECONDS(4)); LOG_DBG("MDM_POWER_PIN -> DISABLE"); modem_pin_write(&mctx, MDM_POWER, MDM_POWER_DISABLE); #if defined(CONFIG_MODEM_UBLOX_SARA_U2) k_sleep(K_SECONDS(1)); #else k_sleep(K_SECONDS(4)); #endif LOG_DBG("MDM_POWER_PIN -> ENABLE"); modem_pin_write(&mctx, MDM_POWER, MDM_POWER_ENABLE); k_sleep(K_SECONDS(1)); /* make sure module is powered off */ #if DT_INST_NODE_HAS_PROP(0, mdm_vint_gpios) LOG_DBG("Waiting for MDM_VINT_PIN = 0"); while (modem_pin_read(&mctx, MDM_VINT) > 0) { #if defined(CONFIG_MODEM_UBLOX_SARA_U2) /* try to power off again */ LOG_DBG("MDM_POWER_PIN -> DISABLE"); modem_pin_write(&mctx, MDM_POWER, MDM_POWER_DISABLE); k_sleep(K_SECONDS(1)); LOG_DBG("MDM_POWER_PIN -> ENABLE"); modem_pin_write(&mctx, MDM_POWER, MDM_POWER_ENABLE); #endif k_sleep(K_MSEC(100)); } #else k_sleep(K_SECONDS(8)); #endif LOG_DBG("MDM_POWER_PIN -> DISABLE"); unsigned int irq_lock_key = irq_lock(); modem_pin_write(&mctx, MDM_POWER, MDM_POWER_DISABLE); #if defined(CONFIG_MODEM_UBLOX_SARA_U2) k_usleep(50); /* 50-80 microseconds */ #else k_sleep(K_SECONDS(1)); #endif modem_pin_write(&mctx, MDM_POWER, MDM_POWER_ENABLE); irq_unlock(irq_lock_key); LOG_DBG("MDM_POWER_PIN -> ENABLE"); #if DT_INST_NODE_HAS_PROP(0, mdm_vint_gpios) LOG_DBG("Waiting for MDM_VINT_PIN = 1"); do { k_sleep(K_MSEC(100)); } while (modem_pin_read(&mctx, MDM_VINT) == 0); #else k_sleep(K_SECONDS(10)); #endif modem_pin_config(&mctx, MDM_POWER, false); LOG_INF("... Done!"); return 0; } #if defined(CONFIG_MODEM_UBLOX_SARA_AUTODETECT_VARIANT) static void modem_rssi_query_work(struct k_work *work) { static const struct modem_cmd cmds[] = { MODEM_CMD("+CSQ: ", on_cmd_atcmdinfo_rssi_csq, 2U, ","), MODEM_CMD("+CESQ: ", on_cmd_atcmdinfo_rssi_cesq, 6U, ","), }; const char *send_cmd_u2 = "AT+CSQ"; const char *send_cmd_r4 = "AT+CESQ"; int ret; /* choose cmd according to variant */ const char *send_cmd = send_cmd_r4; if (mdata.mdm_variant == MDM_VARIANT_UBLOX_U2) { send_cmd = send_cmd_u2; } /* query modem RSSI */ ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, cmds, ARRAY_SIZE(cmds), send_cmd, &mdata.sem_response, MDM_CMD_TIMEOUT); if (ret < 0) { LOG_ERR("AT+C[E]SQ ret:%d", ret); } #if defined(CONFIG_MODEM_CELL_INFO) /* query cell info */ ret = modem_cmd_handler_setup_cmds_nolock(&mctx.iface, &mctx.cmd_handler, query_cellinfo_cmds, ARRAY_SIZE(query_cellinfo_cmds), &mdata.sem_response, MDM_CMD_TIMEOUT); if (ret < 0) { LOG_WRN("modem query for cell info returned %d", ret); } #endif #if defined(CONFIG_MODEM_UBLOX_SARA_RSSI_WORK) /* re-start RSSI query work */ if (work) { k_work_reschedule_for_queue( &modem_workq, &mdata.rssi_query_work, K_SECONDS(CONFIG_MODEM_UBLOX_SARA_RSSI_WORK_PERIOD)); } #endif } #else static void modem_rssi_query_work(struct k_work *work) { static const struct modem_cmd cmd = #if defined(CONFIG_MODEM_UBLOX_SARA_U2) MODEM_CMD("+CSQ: ", on_cmd_atcmdinfo_rssi_csq, 2U, ","); static char *send_cmd = "AT+CSQ"; #else MODEM_CMD("+CESQ: ", on_cmd_atcmdinfo_rssi_cesq, 6U, ","); static char *send_cmd = "AT+CESQ"; #endif int ret; /* query modem RSSI */ ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, &cmd, 1U, send_cmd, &mdata.sem_response, MDM_CMD_TIMEOUT); if (ret < 0) { LOG_ERR("AT+C[E]SQ ret:%d", ret); } #if defined(CONFIG_MODEM_CELL_INFO) /* query cell info */ ret = modem_cmd_handler_setup_cmds_nolock(&mctx.iface, &mctx.cmd_handler, query_cellinfo_cmds, ARRAY_SIZE(query_cellinfo_cmds), &mdata.sem_response, MDM_CMD_TIMEOUT); if (ret < 0) { LOG_WRN("modem query for cell info returned %d", ret); } #endif #if defined(CONFIG_MODEM_UBLOX_SARA_RSSI_WORK) /* re-start RSSI query work */ if (work) { k_work_reschedule_for_queue( &modem_workq, &mdata.rssi_query_work, K_SECONDS(CONFIG_MODEM_UBLOX_SARA_RSSI_WORK_PERIOD)); } #endif } #endif static void modem_reset(void) { int ret = 0, retry_count = 0, counter = 0; static const struct setup_cmd setup_cmds[] = { /* turn off echo */ SETUP_CMD_NOHANDLE("ATE0"), /* stop functionality */ SETUP_CMD_NOHANDLE("AT+CFUN=0"), /* extended error numbers */ SETUP_CMD_NOHANDLE("AT+CMEE=1"), #if defined(CONFIG_BOARD_PARTICLE_BORON) /* use external SIM */ SETUP_CMD_NOHANDLE("AT+UGPIOC=23,0,0"), #endif #if defined(CONFIG_MODEM_UBLOX_SARA_R4_NET_STATUS_PIN) /* enable the network status indication */ SETUP_CMD_NOHANDLE("AT+UGPIOC=" STRINGIFY(CONFIG_MODEM_UBLOX_SARA_R4_NET_STATUS_PIN) ",2"), #endif /* UNC messages for registration */ SETUP_CMD_NOHANDLE("AT+CREG=1"), /* query modem info */ SETUP_CMD("AT+CGMI", "", on_cmd_atcmdinfo_manufacturer, 0U, ""), SETUP_CMD("AT+CGMM", "", on_cmd_atcmdinfo_model, 0U, ""), SETUP_CMD("AT+CGMR", "", on_cmd_atcmdinfo_revision, 0U, ""), SETUP_CMD("AT+CGSN", "", on_cmd_atcmdinfo_imei, 0U, ""), SETUP_CMD("AT+CIMI", "", on_cmd_atcmdinfo_imsi, 0U, ""), #if !defined(CONFIG_MODEM_UBLOX_SARA_AUTODETECT_APN) /* setup PDP context definition */ SETUP_CMD_NOHANDLE("AT+CGDCONT=1,\"IP\",\"" CONFIG_MODEM_UBLOX_SARA_R4_APN "\""), /* start functionality */ SETUP_CMD_NOHANDLE("AT+CFUN=1"), #endif }; #if defined(CONFIG_MODEM_UBLOX_SARA_AUTODETECT_VARIANT) static const struct setup_cmd post_setup_cmds_u2[] = { #if !defined(CONFIG_MODEM_UBLOX_SARA_AUTODETECT_APN) /* set the APN */ SETUP_CMD_NOHANDLE("AT+UPSD=0,1,\"" CONFIG_MODEM_UBLOX_SARA_R4_APN "\""), #endif /* set dynamic IP */ SETUP_CMD_NOHANDLE("AT+UPSD=0,7,\"0.0.0.0\""), /* activate the GPRS connection */ SETUP_CMD_NOHANDLE("AT+UPSDA=0,3"), }; #endif static const struct setup_cmd post_setup_cmds[] = { #if defined(CONFIG_MODEM_UBLOX_SARA_U2) /* set the APN */ SETUP_CMD_NOHANDLE("AT+UPSD=0,1,\"" CONFIG_MODEM_UBLOX_SARA_R4_APN "\""), /* set dynamic IP */ SETUP_CMD_NOHANDLE("AT+UPSD=0,7,\"0.0.0.0\""), /* activate the GPRS connection */ SETUP_CMD_NOHANDLE("AT+UPSDA=0,3"), #else /* activate the PDP context */ SETUP_CMD_NOHANDLE("AT+CGACT=1,1"), #endif }; restart: #if defined(CONFIG_MODEM_UBLOX_SARA_AUTODETECT_APN) mdata.mdm_apn[0] = '\0'; strncat(mdata.mdm_apn, CONFIG_MODEM_UBLOX_SARA_R4_APN, sizeof(mdata.mdm_apn)-1); #endif #if defined(CONFIG_MODEM_UBLOX_SARA_RSSI_WORK) /* stop RSSI delay work */ k_work_cancel_delayable(&mdata.rssi_query_work); #endif pin_init(); LOG_INF("Waiting for modem to respond"); /* Give the modem a while to start responding to simple 'AT' commands. * Also wait for CSPS=1 or RRCSTATE=1 notification */ ret = -1; while (counter++ < 50 && ret < 0) { k_sleep(K_SECONDS(2)); ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, NULL, 0, "AT", &mdata.sem_response, MDM_CMD_TIMEOUT); if (ret < 0 && ret != -ETIMEDOUT) { break; } } if (ret < 0) { LOG_ERR("MODEM WAIT LOOP ERROR: %d", ret); goto error; } ret = modem_cmd_handler_setup_cmds(&mctx.iface, &mctx.cmd_handler, setup_cmds, ARRAY_SIZE(setup_cmds), &mdata.sem_response, MDM_REGISTRATION_TIMEOUT); if (ret < 0) { goto error; } #if defined(CONFIG_MODEM_UBLOX_SARA_AUTODETECT_APN) /* autodetect APN from IMSI */ char cmd[sizeof("AT+CGDCONT=1,\"IP\",\"\"")+MDM_APN_LENGTH]; snprintk(cmd, sizeof(cmd), "AT+CGDCONT=1,\"IP\",\"%s\"", mdata.mdm_apn); /* setup PDP context definition */ ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, NULL, 0, (const char *)cmd, &mdata.sem_response, MDM_REGISTRATION_TIMEOUT); ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, NULL, 0, "AT+CFUN=1", &mdata.sem_response, MDM_REGISTRATION_TIMEOUT); #endif if (strlen(CONFIG_MODEM_UBLOX_SARA_R4_MANUAL_MCCMNO) > 0) { /* use manual MCC/MNO entry */ ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, NULL, 0, "AT+COPS=1,2,\"" CONFIG_MODEM_UBLOX_SARA_R4_MANUAL_MCCMNO "\"", &mdata.sem_response, MDM_REGISTRATION_TIMEOUT); } else { /* register operator automatically */ ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, NULL, 0, "AT+COPS=0,0", &mdata.sem_response, MDM_REGISTRATION_TIMEOUT); } if (ret < 0) { LOG_ERR("AT+COPS ret:%d", ret); goto error; } LOG_INF("Waiting for network"); /* * TODO: A lot of this should be setup as a 3GPP module to handle * basic connection to the network commands / polling */ /* wait for +CREG: 1(normal) or 5(roaming) */ counter = 0; while (counter++ < 40 && mdata.ev_creg != 1 && mdata.ev_creg != 5) { if (counter == 20) { LOG_WRN("Force restart of RF functionality"); /* Disable RF temporarily */ ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, NULL, 0, "AT+CFUN=0", &mdata.sem_response, MDM_CMD_TIMEOUT); k_sleep(K_SECONDS(1)); /* Enable RF */ ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, NULL, 0, "AT+CFUN=1", &mdata.sem_response, MDM_CMD_TIMEOUT); } k_sleep(K_SECONDS(1)); } /* query modem RSSI */ modem_rssi_query_work(NULL); k_sleep(MDM_WAIT_FOR_RSSI_DELAY); counter = 0; /* wait for RSSI < 0 and > -1000 */ while (counter++ < MDM_WAIT_FOR_RSSI_COUNT && (mdata.mdm_rssi >= 0 || mdata.mdm_rssi <= -1000)) { modem_rssi_query_work(NULL); k_sleep(MDM_WAIT_FOR_RSSI_DELAY); } if (mdata.mdm_rssi >= 0 || mdata.mdm_rssi <= -1000) { retry_count++; if (retry_count >= MDM_NETWORK_RETRY_COUNT) { LOG_ERR("Failed network init. Too many attempts!"); ret = -ENETUNREACH; goto error; } LOG_ERR("Failed network init. Restarting process."); goto restart; } #if defined(CONFIG_MODEM_UBLOX_SARA_AUTODETECT_VARIANT) if (mdata.mdm_variant == MDM_VARIANT_UBLOX_U2) { #if defined(CONFIG_MODEM_UBLOX_SARA_AUTODETECT_APN) /* setup PDP context definition */ char cmd[sizeof("AT+UPSD=0,1,\"%s\"")+MDM_APN_LENGTH]; snprintk(cmd, sizeof(cmd), "AT+UPSD=0,1,\"%s\"", mdata.mdm_apn); ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, NULL, 0, (const char *)cmd, &mdata.sem_response, MDM_REGISTRATION_TIMEOUT); #endif ret = modem_cmd_handler_setup_cmds(&mctx.iface, &mctx.cmd_handler, post_setup_cmds_u2, ARRAY_SIZE(post_setup_cmds_u2), &mdata.sem_response, MDM_REGISTRATION_TIMEOUT); } else { #endif ret = modem_cmd_handler_setup_cmds(&mctx.iface, &mctx.cmd_handler, post_setup_cmds, ARRAY_SIZE(post_setup_cmds), &mdata.sem_response, MDM_REGISTRATION_TIMEOUT); #if defined(CONFIG_MODEM_UBLOX_SARA_AUTODETECT_VARIANT) } #endif if (ret < 0) { goto error; } LOG_INF("Network is ready."); #if defined(CONFIG_MODEM_UBLOX_SARA_RSSI_WORK) /* start RSSI query */ k_work_reschedule_for_queue( &modem_workq, &mdata.rssi_query_work, K_SECONDS(CONFIG_MODEM_UBLOX_SARA_RSSI_WORK_PERIOD)); #endif error: return; } /* * generic socket creation function * which can be called in bind() or connect() */ static int create_socket(struct modem_socket *sock, const struct sockaddr *addr) { int ret; static const struct modem_cmd cmd = MODEM_CMD("+USOCR: ", on_cmd_sockcreate, 1U, ""); char buf[sizeof("AT+USOCR=#,#####\r")]; uint16_t local_port = 0U, proto = 6U; if (addr) { if (addr->sa_family == AF_INET6) { local_port = ntohs(net_sin6(addr)->sin6_port); } else if (addr->sa_family == AF_INET) { local_port = ntohs(net_sin(addr)->sin_port); } } if (sock->ip_proto == IPPROTO_UDP) { proto = 17U; } if (local_port > 0U) { snprintk(buf, sizeof(buf), "AT+USOCR=%d,%u", proto, local_port); } else { snprintk(buf, sizeof(buf), "AT+USOCR=%d", proto); } /* create socket */ ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, &cmd, 1U, buf, &mdata.sem_response, MDM_CMD_TIMEOUT); if (ret < 0) { goto error; } if (sock->ip_proto == IPPROTO_TLS_1_2) { char buf[sizeof("AT+USECPRF=#,#,#######\r")]; /* Enable socket security */ snprintk(buf, sizeof(buf), "AT+USOSEC=%d,1,%d", sock->id, sock->id); ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, NULL, 0U, buf, &mdata.sem_response, MDM_CMD_TIMEOUT); if (ret < 0) { goto error; } /* Reset the security profile */ snprintk(buf, sizeof(buf), "AT+USECPRF=%d", sock->id); ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, NULL, 0U, buf, &mdata.sem_response, MDM_CMD_TIMEOUT); if (ret < 0) { goto error; } /* Validate server cert against the CA. */ snprintk(buf, sizeof(buf), "AT+USECPRF=%d,0,1", sock->id); ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, NULL, 0U, buf, &mdata.sem_response, MDM_CMD_TIMEOUT); if (ret < 0) { goto error; } /* Use TLSv1.2 only */ snprintk(buf, sizeof(buf), "AT+USECPRF=%d,1,3", sock->id); ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, NULL, 0U, buf, &mdata.sem_response, MDM_CMD_TIMEOUT); if (ret < 0) { goto error; } /* Set root CA filename */ snprintk(buf, sizeof(buf), "AT+USECPRF=%d,3,\"ca\"", sock->id); ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, NULL, 0U, buf, &mdata.sem_response, MDM_CMD_TIMEOUT); if (ret < 0) { goto error; } } errno = 0; return 0; error: LOG_ERR("%s ret:%d", log_strdup(buf), ret); modem_socket_put(&mdata.socket_config, sock->sock_fd); errno = -ret; return -1; } /* * Socket Offload OPS */ static const struct socket_op_vtable offload_socket_fd_op_vtable; static int offload_socket(int family, int type, int proto) { int ret; /* defer modem's socket create call to bind() */ ret = modem_socket_get(&mdata.socket_config, family, type, proto); if (ret < 0) { errno = -ret; return -1; } errno = 0; return ret; } static int offload_close(void *obj) { struct modem_socket *sock = (struct modem_socket *)obj; char buf[sizeof("AT+USOCL=#\r")]; int ret; /* make sure we assigned an id */ if (sock->id < mdata.socket_config.base_socket_num) { return 0; } if (sock->is_connected || sock->ip_proto == IPPROTO_UDP) { snprintk(buf, sizeof(buf), "AT+USOCL=%d", sock->id); ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, NULL, 0U, buf, &mdata.sem_response, MDM_CMD_TIMEOUT); if (ret < 0) { LOG_ERR("%s ret:%d", log_strdup(buf), ret); } } modem_socket_put(&mdata.socket_config, sock->sock_fd); return 0; } static int offload_bind(void *obj, const struct sockaddr *addr, socklen_t addrlen) { struct modem_socket *sock = (struct modem_socket *)obj; /* save bind address information */ memcpy(&sock->src, addr, sizeof(*addr)); /* make sure we've created the socket */ if (sock->id == mdata.socket_config.sockets_len + 1) { if (create_socket(sock, addr) < 0) { return -1; } } return 0; } static int offload_connect(void *obj, const struct sockaddr *addr, socklen_t addrlen) { struct modem_socket *sock = (struct modem_socket *)obj; int ret; char buf[sizeof("AT+USOCO=#,!###.###.###.###!,#####,#\r")]; uint16_t dst_port = 0U; char ip_str[NET_IPV6_ADDR_LEN]; if (!addr) { errno = EINVAL; return -1; } if (sock->id < mdata.socket_config.base_socket_num - 1) { LOG_ERR("Invalid socket_id(%d) from fd:%d", sock->id, sock->sock_fd); errno = EINVAL; return -1; } /* make sure we've created the socket */ if (sock->id == mdata.socket_config.sockets_len + 1) { if (create_socket(sock, NULL) < 0) { return -1; } } memcpy(&sock->dst, addr, sizeof(*addr)); if (addr->sa_family == AF_INET6) { dst_port = ntohs(net_sin6(addr)->sin6_port); } else if (addr->sa_family == AF_INET) { dst_port = ntohs(net_sin(addr)->sin_port); } else { errno = EAFNOSUPPORT; return -1; } /* skip socket connect if UDP */ if (sock->ip_proto == IPPROTO_UDP) { errno = 0; return 0; } ret = modem_context_sprint_ip_addr(addr, ip_str, sizeof(ip_str)); if (ret != 0) { errno = -ret; LOG_ERR("Error formatting IP string %d", ret); return -1; } snprintk(buf, sizeof(buf), "AT+USOCO=%d,\"%s\",%d", sock->id, ip_str, dst_port); ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, NULL, 0U, buf, &mdata.sem_response, MDM_CMD_CONN_TIMEOUT); if (ret < 0) { LOG_ERR("%s ret:%d", log_strdup(buf), ret); errno = -ret; return -1; } sock->is_connected = true; errno = 0; return 0; } static ssize_t offload_recvfrom(void *obj, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen) { struct modem_socket *sock = (struct modem_socket *)obj; int ret, next_packet_size; static const struct modem_cmd cmd[] = { MODEM_CMD("+USORF: ", on_cmd_sockreadfrom, 4U, ","), MODEM_CMD("+USORD: ", on_cmd_sockread, 2U, ","), }; char sendbuf[sizeof("AT+USORF=#,#####\r")]; struct socket_read_data sock_data; if (!buf || len == 0) { errno = EINVAL; return -1; } if (flags & ZSOCK_MSG_PEEK) { errno = ENOTSUP; return -1; } next_packet_size = modem_socket_next_packet_size(&mdata.socket_config, sock); if (!next_packet_size) { if (flags & ZSOCK_MSG_DONTWAIT) { errno = EAGAIN; return -1; } if (!sock->is_connected && sock->ip_proto != IPPROTO_UDP) { errno = 0; return 0; } modem_socket_wait_data(&mdata.socket_config, sock); next_packet_size = modem_socket_next_packet_size( &mdata.socket_config, sock); } /* * Binary and ASCII mode allows sending MDM_MAX_DATA_LENGTH bytes to * the socket in one command */ if (next_packet_size > MDM_MAX_DATA_LENGTH) { next_packet_size = MDM_MAX_DATA_LENGTH; } snprintk(sendbuf, sizeof(sendbuf), "AT+USO%s=%d,%zd", sock->ip_proto == IPPROTO_UDP ? "RF" : "RD", sock->id, len < next_packet_size ? len : next_packet_size); /* socket read settings */ (void)memset(&sock_data, 0, sizeof(sock_data)); sock_data.recv_buf = buf; sock_data.recv_buf_len = len; sock_data.recv_addr = from; sock->data = &sock_data; ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, cmd, ARRAY_SIZE(cmd), sendbuf, &mdata.sem_response, MDM_CMD_TIMEOUT); if (ret < 0) { errno = -ret; ret = -1; goto exit; } /* HACK: use dst address as from */ if (from && fromlen) { *fromlen = sizeof(sock->dst); memcpy(from, &sock->dst, *fromlen); } /* return length of received data */ errno = 0; ret = sock_data.recv_read_len; exit: /* clear socket data */ sock->data = NULL; return ret; } static ssize_t offload_sendto(void *obj, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) { struct iovec msg_iov = { .iov_base = (void *)buf, .iov_len = len, }; struct msghdr msg = { .msg_iovlen = 1, .msg_name = (struct sockaddr *)to, .msg_namelen = tolen, .msg_iov = &msg_iov, }; int ret = send_socket_data(obj, &msg, MDM_CMD_TIMEOUT); if (ret < 0) { errno = -ret; return -1; } errno = 0; return ret; } static int offload_ioctl(void *obj, unsigned int request, va_list args) { switch (request) { case ZFD_IOCTL_POLL_PREPARE: { struct zsock_pollfd *pfd; struct k_poll_event **pev; struct k_poll_event *pev_end; pfd = va_arg(args, struct zsock_pollfd *); pev = va_arg(args, struct k_poll_event **); pev_end = va_arg(args, struct k_poll_event *); return modem_socket_poll_prepare(&mdata.socket_config, obj, pfd, pev, pev_end); } case ZFD_IOCTL_POLL_UPDATE: { struct zsock_pollfd *pfd; struct k_poll_event **pev; pfd = va_arg(args, struct zsock_pollfd *); pev = va_arg(args, struct k_poll_event **); return modem_socket_poll_update(obj, pfd, pev); } case F_GETFL: return 0; default: errno = EINVAL; return -1; } } static ssize_t offload_read(void *obj, void *buffer, size_t count) { return offload_recvfrom(obj, buffer, count, 0, NULL, 0); } static ssize_t offload_write(void *obj, const void *buffer, size_t count) { return offload_sendto(obj, buffer, count, 0, NULL, 0); } static ssize_t offload_sendmsg(void *obj, const struct msghdr *msg, int flags) { ssize_t sent = 0; int bkp_iovec_idx; struct iovec bkp_iovec = {0}; struct msghdr crafted_msg = { .msg_name = msg->msg_name, .msg_namelen = msg->msg_namelen, }; size_t full_len = 0; int ret; /* Compute the full length to be send and check for invalid values */ for (int i = 0; i < msg->msg_iovlen; i++) { if (!msg->msg_iov[i].iov_base || msg->msg_iov[i].iov_len == 0) { errno = EINVAL; return -1; } full_len += msg->msg_iov[i].iov_len; } LOG_DBG("msg_iovlen:%zd flags:%d, full_len:%zd", msg->msg_iovlen, flags, full_len); while (full_len > sent) { int removed = 0; int i = 0; crafted_msg.msg_iovlen = msg->msg_iovlen; crafted_msg.msg_iov = &msg->msg_iov[0]; bkp_iovec_idx = -1; /* Iterate on iovec to remove the bytes already sent */ while (removed < sent) { int to_removed = sent - removed; if (to_removed >= msg->msg_iov[i].iov_len) { crafted_msg.msg_iovlen -= 1; crafted_msg.msg_iov = &msg->msg_iov[i + 1]; removed += msg->msg_iov[i].iov_len; } else { /* Backup msg->msg_iov[i] before "removing" * starting bytes already send. */ bkp_iovec_idx = i; bkp_iovec.iov_len = msg->msg_iov[i].iov_len; bkp_iovec.iov_base = msg->msg_iov[i].iov_base; /* Update msg->msg_iov[i] to "remove" * starting bytes already send. */ msg->msg_iov[i].iov_len -= to_removed; msg->msg_iov[i].iov_base = &(((uint8_t *)msg->msg_iov[i].iov_base)[to_removed]); removed += to_removed; } i++; } ret = send_socket_data(obj, &crafted_msg, MDM_CMD_TIMEOUT); /* Restore backup iovec when necessary */ if (bkp_iovec_idx != -1) { msg->msg_iov[bkp_iovec_idx].iov_len = bkp_iovec.iov_len; msg->msg_iov[bkp_iovec_idx].iov_base = bkp_iovec.iov_base; } /* Handle send_socket_data() returned value */ if (ret < 0) { errno = -ret; return -1; } sent += ret; } return (ssize_t)sent; } #if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) static int map_credentials(struct modem_socket *sock, const void *optval, socklen_t optlen) { sec_tag_t *sec_tags = (sec_tag_t *)optval; int ret = 0; int tags_len; sec_tag_t tag; int id; int i; struct tls_credential *cert; if ((optlen % sizeof(sec_tag_t)) != 0 || (optlen == 0)) { return -EINVAL; } tags_len = optlen / sizeof(sec_tag_t); /* For each tag, retrieve the credentials value and type: */ for (i = 0; i < tags_len; i++) { tag = sec_tags[i]; cert = credential_next_get(tag, NULL); while (cert != NULL) { switch (cert->type) { case TLS_CREDENTIAL_CA_CERTIFICATE: id = 0; break; case TLS_CREDENTIAL_NONE: case TLS_CREDENTIAL_PSK: case TLS_CREDENTIAL_PSK_ID: default: /* Not handled */ return -EINVAL; } struct modem_cmd cmd[] = { MODEM_CMD("+USECMNG: ", on_cmd_cert_write, 3U, ","), }; ret = send_cert(sock, cmd, 1, cert->buf, cert->len, id); if (ret < 0) { return ret; } cert = credential_next_get(tag, cert); } } return 0; } #else static int map_credentials(struct modem_socket *sock, const void *optval, socklen_t optlen) { return -EINVAL; } #endif static int offload_setsockopt(void *obj, int level, int optname, const void *optval, socklen_t optlen) { struct modem_socket *sock = (struct modem_socket *)obj; int ret; if (IS_ENABLED(CONFIG_NET_SOCKETS_SOCKOPT_TLS) && level == SOL_TLS) { switch (optname) { case TLS_SEC_TAG_LIST: ret = map_credentials(sock, optval, optlen); break; case TLS_HOSTNAME: LOG_WRN("TLS_HOSTNAME option is not supported"); return -EINVAL; case TLS_PEER_VERIFY: if (*(uint32_t *)optval != TLS_PEER_VERIFY_REQUIRED) { LOG_WRN("Disabling peer verification is not supported"); return -EINVAL; } ret = 0; break; default: return -EINVAL; } } else { return -EINVAL; } return ret; } static const struct socket_op_vtable offload_socket_fd_op_vtable = { .fd_vtable = { .read = offload_read, .write = offload_write, .close = offload_close, .ioctl = offload_ioctl, }, .bind = offload_bind, .connect = offload_connect, .sendto = offload_sendto, .recvfrom = offload_recvfrom, .listen = NULL, .accept = NULL, .sendmsg = offload_sendmsg, .getsockopt = NULL, .setsockopt = offload_setsockopt, }; static bool offload_is_supported(int family, int type, int proto) { if (family != AF_INET && family != AF_INET6) { return false; } if (type != SOCK_DGRAM && type != SOCK_STREAM) { return false; } if (proto != IPPROTO_TCP && proto != IPPROTO_UDP && proto != IPPROTO_TLS_1_2) { return false; } return true; } NET_SOCKET_OFFLOAD_REGISTER(ublox_sara_r4, CONFIG_NET_SOCKETS_OFFLOAD_PRIORITY, AF_UNSPEC, offload_is_supported, offload_socket); #if defined(CONFIG_DNS_RESOLVER) /* TODO: This is a bare-bones implementation of DNS handling * We ignore most of the hints like ai_family, ai_protocol and ai_socktype. * Later, we can add additional handling if it makes sense. */ static int offload_getaddrinfo(const char *node, const char *service, const struct zsock_addrinfo *hints, struct zsock_addrinfo **res) { static const struct modem_cmd cmd = MODEM_CMD("+UDNSRN: ", on_cmd_dns, 1U, ","); uint32_t port = 0U; int ret; /* DNS command + 128 bytes for domain name parameter */ char sendbuf[sizeof("AT+UDNSRN=#,'[]'\r") + 128]; /* init result */ (void)memset(&result, 0, sizeof(result)); (void)memset(&result_addr, 0, sizeof(result_addr)); /* FIXME: Hard-code DNS to return only IPv4 */ result.ai_family = AF_INET; result_addr.sa_family = AF_INET; result.ai_addr = &result_addr; result.ai_addrlen = sizeof(result_addr); result.ai_canonname = result_canonname; result_canonname[0] = '\0'; if (service) { port = ATOI(service, 0U, "port"); if (port < 1 || port > USHRT_MAX) { return DNS_EAI_SERVICE; } } if (port > 0U) { /* FIXME: DNS is hard-coded to return only IPv4 */ if (result.ai_family == AF_INET) { net_sin(&result_addr)->sin_port = htons(port); } } /* check to see if node is an IP address */ if (net_addr_pton(result.ai_family, node, &((struct sockaddr_in *)&result_addr)->sin_addr) == 0) { *res = &result; return 0; } /* user flagged node as numeric host, but we failed net_addr_pton */ if (hints && hints->ai_flags & AI_NUMERICHOST) { return DNS_EAI_NONAME; } snprintk(sendbuf, sizeof(sendbuf), "AT+UDNSRN=0,\"%s\"", node); ret = modem_cmd_send(&mctx.iface, &mctx.cmd_handler, &cmd, 1U, sendbuf, &mdata.sem_response, MDM_DNS_TIMEOUT); if (ret < 0) { return ret; } LOG_DBG("DNS RESULT: %s", log_strdup(net_addr_ntop(result.ai_family, &net_sin(&result_addr)->sin_addr, sendbuf, NET_IPV4_ADDR_LEN))); *res = (struct zsock_addrinfo *)&result; return 0; } static void offload_freeaddrinfo(struct zsock_addrinfo *res) { /* using static result from offload_getaddrinfo() -- no need to free */ res = NULL; } const struct socket_dns_offload offload_dns_ops = { .getaddrinfo = offload_getaddrinfo, .freeaddrinfo = offload_freeaddrinfo, }; #endif static int net_offload_dummy_get(sa_family_t family, enum net_sock_type type, enum net_ip_protocol ip_proto, struct net_context **context) { LOG_ERR("CONFIG_NET_SOCKETS_OFFLOAD must be enabled for this driver"); return -ENOTSUP; } /* placeholders, until Zephyr IP stack updated to handle a NULL net_offload */ static struct net_offload modem_net_offload = { .get = net_offload_dummy_get, }; #define HASH_MULTIPLIER 37 static uint32_t hash32(char *str, int len) { uint32_t h = 0; int i; for (i = 0; i < len; ++i) { h = (h * HASH_MULTIPLIER) + str[i]; } return h; } static inline uint8_t *modem_get_mac(const struct device *dev) { struct modem_data *data = dev->data; uint32_t hash_value; data->mac_addr[0] = 0x00; data->mac_addr[1] = 0x10; /* use IMEI for mac_addr */ hash_value = hash32(mdata.mdm_imei, strlen(mdata.mdm_imei)); UNALIGNED_PUT(hash_value, (uint32_t *)(data->mac_addr + 2)); return data->mac_addr; } static int offload_socket(int family, int type, int proto); static void modem_net_iface_init(struct net_if *iface) { const struct device *dev = net_if_get_device(iface); struct modem_data *data = dev->data; /* Direct socket offload used instead of net offload: */ iface->if_dev->offload = &modem_net_offload; net_if_set_link_addr(iface, modem_get_mac(dev), sizeof(data->mac_addr), NET_LINK_ETHERNET); data->net_iface = iface; #ifdef CONFIG_DNS_RESOLVER socket_offload_dns_register(&offload_dns_ops); #endif net_if_socket_offload_set(iface, offload_socket); } static struct net_if_api api_funcs = { .init = modem_net_iface_init, }; static const struct modem_cmd response_cmds[] = { MODEM_CMD("OK", on_cmd_ok, 0U, ""), /* 3GPP */ MODEM_CMD("ERROR", on_cmd_error, 0U, ""), /* 3GPP */ MODEM_CMD("+CME ERROR: ", on_cmd_exterror, 1U, ""), MODEM_CMD_DIRECT("@", on_prompt), }; static const struct modem_cmd unsol_cmds[] = { MODEM_CMD("+UUSOCL: ", on_cmd_socknotifyclose, 1U, ""), MODEM_CMD("+UUSORD: ", on_cmd_socknotifydata, 2U, ","), MODEM_CMD("+UUSORF: ", on_cmd_socknotifydata, 2U, ","), MODEM_CMD("+CREG: ", on_cmd_socknotifycreg, 1U, ""), }; static int modem_init(const struct device *dev) { int ret = 0; ARG_UNUSED(dev); k_sem_init(&mdata.sem_response, 0, 1); k_sem_init(&mdata.sem_prompt, 0, 1); #if defined(CONFIG_MODEM_UBLOX_SARA_RSSI_WORK) /* initialize the work queue */ k_work_queue_start(&modem_workq, modem_workq_stack, K_KERNEL_STACK_SIZEOF(modem_workq_stack), K_PRIO_COOP(7), NULL); #endif /* socket config */ mdata.socket_config.sockets = &mdata.sockets[0]; mdata.socket_config.sockets_len = ARRAY_SIZE(mdata.sockets); mdata.socket_config.base_socket_num = MDM_BASE_SOCKET_NUM; ret = modem_socket_init(&mdata.socket_config, &offload_socket_fd_op_vtable); if (ret < 0) { goto error; } /* cmd handler */ mdata.cmd_handler_data.cmds[CMD_RESP] = response_cmds; mdata.cmd_handler_data.cmds_len[CMD_RESP] = ARRAY_SIZE(response_cmds); mdata.cmd_handler_data.cmds[CMD_UNSOL] = unsol_cmds; mdata.cmd_handler_data.cmds_len[CMD_UNSOL] = ARRAY_SIZE(unsol_cmds); mdata.cmd_handler_data.match_buf = &mdata.cmd_match_buf[0]; mdata.cmd_handler_data.match_buf_len = sizeof(mdata.cmd_match_buf); mdata.cmd_handler_data.buf_pool = &mdm_recv_pool; mdata.cmd_handler_data.alloc_timeout = K_NO_WAIT; mdata.cmd_handler_data.eol = "\r"; ret = modem_cmd_handler_init(&mctx.cmd_handler, &mdata.cmd_handler_data); if (ret < 0) { goto error; } /* modem interface */ mdata.iface_data.hw_flow_control = DT_PROP(MDM_UART_NODE, hw_flow_control); mdata.iface_data.rx_rb_buf = &mdata.iface_rb_buf[0]; mdata.iface_data.rx_rb_buf_len = sizeof(mdata.iface_rb_buf); ret = modem_iface_uart_init(&mctx.iface, &mdata.iface_data, MDM_UART_DEV); if (ret < 0) { goto error; } /* modem data storage */ mctx.data_manufacturer = mdata.mdm_manufacturer; mctx.data_model = mdata.mdm_model; mctx.data_revision = mdata.mdm_revision; mctx.data_imei = mdata.mdm_imei; mctx.data_rssi = &mdata.mdm_rssi; /* pin setup */ mctx.pins = modem_pins; mctx.pins_len = ARRAY_SIZE(modem_pins); mctx.driver_data = &mdata; ret = modem_context_register(&mctx); if (ret < 0) { LOG_ERR("Error registering modem context: %d", ret); goto error; } /* start RX thread */ k_thread_create(&modem_rx_thread, modem_rx_stack, K_KERNEL_STACK_SIZEOF(modem_rx_stack), (k_thread_entry_t) modem_rx, NULL, NULL, NULL, K_PRIO_COOP(7), 0, K_NO_WAIT); #if defined(CONFIG_MODEM_UBLOX_SARA_RSSI_WORK) /* init RSSI query */ k_work_init_delayable(&mdata.rssi_query_work, modem_rssi_query_work); #endif modem_reset(); error: return ret; } NET_DEVICE_DT_INST_OFFLOAD_DEFINE(0, modem_init, NULL, &mdata, NULL, CONFIG_MODEM_UBLOX_SARA_R4_INIT_PRIORITY, &api_funcs, MDM_MAX_DATA_LENGTH); |