├── LICENSE ├── README.md ├── SConscript ├── class ├── ppp_device_air720.c ├── ppp_device_ec20.c ├── ppp_device_m6312.c ├── ppp_device_ml305.c └── ppp_device_sim800.c ├── docs ├── figures │ ├── code_transplantation_one.jpg │ ├── code_transplantation_three.jpg │ ├── code_transplantation_two.jpg │ └── ppp_frame.jpg └── port.md ├── inc ├── ppp_chat.h ├── ppp_device.h └── ppp_netif.h ├── samples └── ppp_sample.c └── src ├── ppp_chat.c ├── ppp_device.c └── ppp_netif.c /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PPP Device 2 | 3 | ## 1. 简介 4 | 5 | PPP Device 软件包,是 RT-Thread 针对不同模块的 lwIP PPP 功能实现的软件包。它使蜂窝模块不再需要发送 AT 命令,使用 PPP 方式即可实现网络数据传输。 6 | 7 | PPP Device 软件包特点如下: 8 | 9 | - 支持多种蜂窝模块; 10 | - 无缝兼容多种应用层网络协议(HTTP、MQTT、TLS 等); 11 | - 支持数据差错检测,提供可靠数据传输; 12 | 13 | * 提供 CHAT 方式初始化功能,方便设备移植; 14 | 15 | 目前 PPP 功能支持 Luat Air720,Quectel EC20,China mobile M6312, SIMCOM SIM800 模块,后续会接入更多蜂窝模块。 16 | 17 | 18 | 19 | 对 PPP Device 有疑惑,或者对 PPP Device 感兴趣的开发者欢迎入群详细了解。 20 | 21 | QQ群:749347156 [<传送门>](https://jq.qq.com/?_wv=1027&k=5KcuPGI) 22 | 23 | 移植文档地址:[<移植文档>](./docs/port.md) 24 | 25 | ### 1.1 框架图 26 | 27 | ![](docs/figures/ppp_frame.jpg) 28 | 29 | - ppp_device 软件包是 lwIP 协议栈中 ppp 功能针对不同设备的具体实现; 30 | - ppp_device 软件包中实现 netdev 网卡设备,并对接 SAL 抽象层,应用层可直接使用 BSD Socket API 进行网络编程; 31 | 32 | ### 1.2 目录结构 33 | 34 | | 名称 | 说明 | 35 | | ---- | ---- | 36 | | src | PPP Device 实现源码目录 | 37 | | inc | PPP Device 头文件目录 | 38 | | sample | PPP 功能示例文件目录 | 39 | | class | 不同设备针对 PPP 功能的移植适配目录 | 40 | 41 | ### 1.3 许可证 42 | 43 | ppp_device 软件包遵循 Apache-2.0 许可,详见 LICENSE 文件。 44 | 45 | ### 1.4 依赖 46 | 47 | - RT-Thread 3.1.0+ 48 | - lwIP 组件( ppp 功能) 49 | 50 | ## 2. 获取方式 51 | 52 | **PPP Device 软件包相关配置选项介绍** 53 | 54 | 55 | ```c 56 | [*] PPP DEVICE: lwIP PPP porting for Cellular Module( 2G/3G/4G ) 57 | [ ] Enable debug log output 58 | [ ] Enbale authorize connect feature 59 | [*] Enable link status detect feature 60 | (10) Link status detecct timeout 61 | Select Internet Service Provider (china mobile) ---> 62 | Select modem type (Luat Air720) ---> 63 | [*] Enable ppp device sample 64 | (uart3) ppp device uart name 65 | Version (latest) ---> 66 | ``` 67 | - **Enable debug log output:** 开启调试日志功能 68 | - **Enbale authorize connect feature:** 开启身份认证功能 69 | - **Enable lin status detect feature:** PPP链路连接监控,检测链路连接正常;设置为 0 则不开启链路监控; 70 | - **Select modem type:** 模块选择 71 | - **Select Internet Service Provider:** 网络运营商选择 72 | - **Enable ppp device sample:** 选择模块后会提示的模块使用示例 73 | - **ppp device uart name:** 模块使用的串口 74 | - **Version:** 软件包版本号 75 | 76 | ## 3. 使用方式 77 | 78 | PPP Device 软件包初始化函数如下所示: 79 | 80 | **PPP 功能启动函数,该函数自动调用;没有调用停止函数前,不可再次调用** 81 | 82 | ```c 83 | int ppp_sample_start(void) 84 | { 85 | rt_device_t device = RT_NULL; 86 | device = rt_device_find(PPP_DEVICE_NAME); 87 | if(device == RT_NULL) 88 | { 89 | LOG_E("Can't find device (%s).", PPP_DEVICE_NAME); 90 | return -RT_ERROR; 91 | } 92 | if(ppp_device_attach((struct ppp_device *)device, PPP_CLIENT_NAME, RT_NULL) != RT_EOK) 93 | { 94 | LOG_E("ppp_device_attach execute failed."); 95 | return -RT_ERROR; 96 | } 97 | return RT_EOK; 98 | } 99 | // 自动初始化 100 | INIT_APP_EXPORT(ppp_sample_start); 101 | // 命令导出到MSH( ppp_sample_start 变更为ppp_start ) 102 | MSH_CMD_EXPORT_ALIAS(ppp_sample_start, ppp_start, a sample of ppp device for dailing to network); 103 | ``` 104 | 105 | * 模块拨号,模块进入 PPP 模式; 106 | * 注册 netdev 设备,接入标准网络框架; 107 | 108 | **PPP 功能停止函数,该函数可以退出 PPP 模式** 109 | 110 | ```c 111 | int ppp_sample_stop(void) 112 | { 113 | rt_device_t device = RT_NULL; 114 | device = rt_device_find(PPP_DEVICE_NAME); 115 | if(device == RT_NULL) 116 | { 117 | LOG_E("Can't find device (%s).", PPP_DEVICE_NAME); 118 | return -RT_ERROR; 119 | } 120 | if(ppp_device_detach((struct ppp_device *)device) != RT_EOK) 121 | { 122 | LOG_E("ppp_device_detach execute failed."); 123 | return -RT_ERROR; 124 | } 125 | return RT_EOK; 126 | } 127 | MSH_CMD_EXPORT_ALIAS(ppp_sample_stop, ppp_stop, a sample of ppp device for turning off network); 128 | ``` 129 | 130 | * 退出 PPP 模式,模块退出拨号模式; 131 | * 解注册 netdev 设备; 132 | 133 | 模块上电后,自动初始化流程如下: 134 | 135 | ```c 136 | \ | / 137 | - RT - Thread Operating System 138 | / | \ 4.0.2 build Sep 23 2019 139 | 2006 - 2019 Copyright by rt-thread team 140 | lwIP-2.0.2 initialized! 141 | [I/sal.skt] Socket Abstraction Layer initialize success. 142 | [I/ppp.chat] (uart3) has control by modem_chat. 143 | [I/ppp.dev] (uart3) is used by ppp_device. 144 | msh />[I/ppp.dev] ppp connect successful. 145 | ``` 146 | 147 | 设备上电初始化完成,模块提示拨号成功,然后可以在 FinSH 中输入命令 `ifconfig` 查看设备 IP 地址、MAC 地址等网络信息,如下所示: 148 | 149 | ```shell 150 | msh />ifconfig 151 | network interface device: pp (Default) ## 设备名称 152 | MTU: 1500 ## 网络最大传输单元 153 | MAC: 95 45 68 39 68 52 ## 设备 MAC 地址 154 | FLAGS: UP LINK_UP INTERNET_DOWN DHCP_DISABLE ## 设备标志 155 | ip address: 10.32.76.151 ## 设备 IP 地址 156 | gw address: 10.64.64.64 ## 设备网关地址 157 | net mask : 255.255.255.255 ## 设备子网掩码 158 | dns server #0: 114.114.114.114 ## 域名解析服务器地址0 159 | dns server #1: 120.196.165.7 ## 域名解析服务器地址1 160 | ``` 161 | 162 | 获取 IP 地址成功之后,如果开启 Ping 命令功能,可以在 FinSH 中输入命令 `ping + 域名地址` 测试网络连接状态, 如下所示: 163 | 164 | ```shell 165 | msh />ping www.baidu.com 166 | 60 bytes from 183.232.231.172 icmp_seq=0 ttl=55 time=84 ms 167 | 60 bytes from 183.232.231.172 icmp_seq=1 ttl=55 time=78 ms 168 | 60 bytes from 183.232.231.172 icmp_seq=2 ttl=55 time=78 ms 169 | 60 bytes from 183.232.231.172 icmp_seq=3 ttl=55 time=78 ms 170 | ``` 171 | 172 | `ping` 命令测试正常说明 PPP Device 设备网络连接成功,之后可以使用 SAL(套接字抽象层) 抽象出来的标准 BSD Socket APIs 进行网络开发(MQTT、HTTP、MbedTLS、NTP、Iperf 等)。 173 | 174 | ## 4. 注意事项 175 | 176 | * 一般的 SIM 卡因为只能从运营商网络获取内网地址,所以一般不能实现服务器相关功能 177 | * 特殊的 SIM 卡可以从运营商获取公网地址,可是实现服务器功能 178 | * 目前只支持一个设备通过 PPP 连接网络 179 | * 建议不要打开 RT_DEVICE_FLAG_DMA_TX 180 | * 如果网络环境不好,建议关闭 Enable lin status detect feature 选项,或调整成大一点的时间 181 | * RT_LWIP_TCPTHREAD_STACKSIZE 需要配置为不小于 2048,及时在 network 组件中修改 182 | * 不要忘记打开串口 183 | * 对于开启模块电源控制引脚的开发者,要注意模块的启动时间;通常模块启动时间较长,这时会出现长时间拨号不成功的问题,一定要注意模块的启动时间,避免造成使用上的一些障碍。 184 | 185 | ## 5. 联系方式 186 | 187 | 联系人:xiangxistu 188 | 189 | Email: liuxianliang@rt-thread.com 190 | 191 | ## 6. 致谢 192 | 193 | 感谢网友 [@xfan1024](https://github.com/xfan1024) 的一些贡献及建议,在制作PPP DEVICE 时给与的帮助。提出了很多宝贵的意见来一同完善PPP DEVICE 软件包。 -------------------------------------------------------------------------------- /SConscript: -------------------------------------------------------------------------------- 1 | from building import * 2 | 3 | cwd = GetCurrentDir() 4 | path = [cwd + '/inc'] 5 | src = Glob('src/*.c') 6 | if GetDepend(['PPP_DEVICE_SAMPLE']): 7 | src += Glob('samples/ppp_sample.c') 8 | 9 | # Air720 10 | if GetDepend(['PPP_DEVICE_USING_AIR720']): 11 | src += Glob('class/ppp_device_air720.c') 12 | 13 | # EC20 14 | if GetDepend(['PPP_DEVICE_USING_EC20']): 15 | src += Glob('class/ppp_device_ec20.c') 16 | 17 | # M6312 18 | if GetDepend(['PPP_DEVICE_USING_M6312']): 19 | src += Glob('class/ppp_device_m6312.c') 20 | 21 | # SIM800 22 | if GetDepend(['PPP_DEVICE_USING_SIM800']): 23 | src += Glob('class/ppp_device_sim800.c') 24 | 25 | # ML305 26 | if GetDepend(['PPP_DEVICE_USING_ML305']): 27 | src += Glob('class/ppp_device_ml305.c') 28 | 29 | group = DefineGroup('ppp_device', src, depend = ['PKG_USING_PPP_DEVICE'], CPPPATH = path) 30 | 31 | Return('group') 32 | -------------------------------------------------------------------------------- /class/ppp_device_air720.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2019, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2019-08-15 xiangxistu the first version 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #define DBG_TAG "ppp.air720" 16 | 17 | #ifdef PPP_DEVICE_DEBUG 18 | #define DBG_LVL DBG_LOG 19 | #else 20 | #define DBG_LVL DBG_INFO 21 | #endif 22 | #include 23 | 24 | #define AIR720_POWER_ON PIN_HIGH 25 | #define AIR720_POWER_OFF PIN_LOW 26 | #ifndef AIR720_POWER_PIN 27 | #define AIR720_POWER_PIN -1 28 | #endif 29 | #define AIR720_WARTING_TIME_BASE 2000 30 | 31 | #ifndef PKG_USING_CMUX 32 | static const struct modem_chat_data rst_mcd[] = 33 | { 34 | {"+++", MODEM_CHAT_RESP_NOT_NEED, 30, 1, RT_TRUE}, 35 | {"ATH", MODEM_CHAT_RESP_OK, 30, 1, RT_FALSE}, 36 | }; 37 | 38 | static const struct modem_chat_data mcd[] = 39 | { 40 | {"AT", MODEM_CHAT_RESP_OK, 10, 1, RT_FALSE}, 41 | {"ATE0", MODEM_CHAT_RESP_OK, 1, 1, RT_FALSE}, 42 | {PPP_APN_CMD, MODEM_CHAT_RESP_OK, 1, 5, RT_FALSE}, 43 | {PPP_DAIL_CMD, MODEM_CHAT_RESP_CONNECT, 1, 30, RT_FALSE}, 44 | }; 45 | #else 46 | static const struct modem_chat_data mcd[] = 47 | { 48 | {"AT", MODEM_CHAT_RESP_NOT_NEED, 10, 1, RT_FALSE}, 49 | {PPP_APN_CMD, MODEM_CHAT_RESP_NOT_NEED, 1, 5, RT_FALSE}, 50 | {PPP_DAIL_CMD, MODEM_CHAT_RESP_NOT_NEED, 1, 30, RT_FALSE}, 51 | }; 52 | #endif 53 | 54 | static rt_err_t ppp_air720_prepare(struct ppp_device *device) 55 | { 56 | #ifndef PKG_USING_CMUX 57 | if (device->power_pin >= 0) 58 | { 59 | rt_pin_write(device->power_pin, AIR720_POWER_OFF); 60 | rt_thread_mdelay(AIR720_WARTING_TIME_BASE / 20); 61 | rt_pin_write(device->power_pin, AIR720_POWER_ON); 62 | rt_thread_mdelay(AIR720_WARTING_TIME_BASE); 63 | } 64 | else 65 | { 66 | rt_err_t err; 67 | err = modem_chat(device->uart, rst_mcd, sizeof(rst_mcd) / sizeof(rst_mcd[0])); 68 | if (err) 69 | return err; 70 | } 71 | #endif 72 | return modem_chat(device->uart, mcd, sizeof(mcd) / sizeof(mcd[0])); 73 | } 74 | 75 | /* ppp_air720_ops for ppp_device_ops , a common interface */ 76 | static struct ppp_device_ops air720_ops = 77 | { 78 | .prepare = ppp_air720_prepare, 79 | }; 80 | 81 | /** 82 | * register air720 into ppp_device 83 | * 84 | * @return =0: ppp_device register successfully 85 | * <0: ppp_device register failed 86 | */ 87 | int ppp_air720_register(void) 88 | { 89 | struct ppp_device *ppp_device = RT_NULL; 90 | 91 | ppp_device = rt_malloc(sizeof(struct ppp_device)); 92 | if(ppp_device == RT_NULL) 93 | { 94 | LOG_E("No memory for air720 ppp_device."); 95 | return -RT_ENOMEM; 96 | } 97 | 98 | ppp_device->power_pin = AIR720_POWER_PIN; 99 | if (ppp_device->power_pin >= 0) 100 | { 101 | rt_pin_mode(ppp_device->power_pin, PIN_MODE_OUTPUT); 102 | rt_pin_write(ppp_device->power_pin, AIR720_POWER_ON); 103 | rt_thread_mdelay(AIR720_WARTING_TIME_BASE); 104 | } 105 | ppp_device->ops = &air720_ops; 106 | 107 | LOG_D("ppp air720 is registering ppp_device"); 108 | 109 | return ppp_device_register(ppp_device, PPP_DEVICE_NAME, RT_NULL, RT_NULL); 110 | } 111 | INIT_COMPONENT_EXPORT(ppp_air720_register); 112 | -------------------------------------------------------------------------------- /class/ppp_device_ec20.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2019, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2019-08-15 xiangxistu the first version 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #define DBG_TAG "ppp.ec20" 16 | 17 | #ifdef PPP_DEVICE_DEBUG 18 | #define DBG_LVL DBG_LOG 19 | #else 20 | #define DBG_LVL DBG_INFO 21 | #endif 22 | #include 23 | 24 | 25 | #define EC20_POWER_ON PIN_LOW 26 | #define EC20_POWER_OFF PIN_HIGH 27 | #ifndef EC20_POWER_PIN 28 | #include 29 | #define EC20_POWER_PIN GET_PIN(A, 11) 30 | #endif 31 | 32 | #define EC20_WARTING_TIME_BASE 2000 33 | 34 | #ifndef PKG_USING_CMUX 35 | static const struct modem_chat_data rst_mcd[] = 36 | { 37 | {"+++", MODEM_CHAT_RESP_NOT_NEED, 30, 1, RT_TRUE}, 38 | {"ATH", MODEM_CHAT_RESP_OK, 30, 1, RT_FALSE}, 39 | }; 40 | 41 | static const struct modem_chat_data mcd[] = 42 | { 43 | {"AT", MODEM_CHAT_RESP_OK, 10, 1, RT_FALSE}, 44 | {"ATE0", MODEM_CHAT_RESP_OK, 1, 1, RT_FALSE}, 45 | {PPP_APN_CMD, MODEM_CHAT_RESP_OK, 1, 5, RT_FALSE}, 46 | {PPP_DAIL_CMD, MODEM_CHAT_RESP_CONNECT, 1, 30, RT_FALSE}, 47 | }; 48 | #else 49 | static const struct modem_chat_data mcd[] = 50 | { 51 | {"AT", MODEM_CHAT_RESP_OK, 10, 1, RT_FALSE}, 52 | {PPP_APN_CMD, MODEM_CHAT_RESP_OK, 1, 5, RT_FALSE}, 53 | {PPP_DAIL_CMD, MODEM_CHAT_RESP_CONNECT, 1, 30, RT_FALSE}, 54 | }; 55 | #endif 56 | 57 | static rt_err_t ppp_ec20_prepare(struct ppp_device *device) 58 | { 59 | if (device->power_pin >= 0) 60 | { 61 | rt_pin_write(device->power_pin, EC20_POWER_OFF); 62 | rt_thread_mdelay(EC20_WARTING_TIME_BASE / 20); 63 | rt_pin_write(device->power_pin, EC20_POWER_ON); 64 | rt_thread_mdelay(EC20_WARTING_TIME_BASE / 2 + EC20_WARTING_TIME_BASE); 65 | } 66 | else 67 | { 68 | #ifndef PKG_USING_CMUX 69 | rt_err_t err; 70 | err = modem_chat(device->uart, rst_mcd, sizeof(rst_mcd) / sizeof(rst_mcd[0])); 71 | if (err) 72 | return err; 73 | #endif 74 | } 75 | return modem_chat(device->uart, mcd, sizeof(mcd) / sizeof(mcd[0])); 76 | } 77 | 78 | /* ppp_ec20_ops for ppp_device_ops , a common interface */ 79 | static struct ppp_device_ops ec20_ops = 80 | { 81 | .prepare = ppp_ec20_prepare, 82 | }; 83 | 84 | /** 85 | * register ec20 into ppp_device 86 | * 87 | * @return =0: ppp_device register successfully 88 | * <0: ppp_device register failed 89 | */ 90 | int ppp_ec20_register(void) 91 | { 92 | struct ppp_device *ppp_device = RT_NULL; 93 | 94 | ppp_device = rt_malloc(sizeof(struct ppp_device)); 95 | if(ppp_device == RT_NULL) 96 | { 97 | LOG_E("No memory for ec20 ppp_device."); 98 | return -RT_ENOMEM; 99 | } 100 | 101 | ppp_device->power_pin = EC20_POWER_PIN; 102 | if (ppp_device->power_pin >= 0) 103 | { 104 | rt_pin_mode(ppp_device->power_pin, PIN_MODE_OUTPUT); 105 | rt_pin_write(ppp_device->power_pin, EC20_POWER_ON); 106 | rt_thread_mdelay(EC20_WARTING_TIME_BASE / 2 + EC20_WARTING_TIME_BASE); 107 | } 108 | ppp_device->ops = &ec20_ops; 109 | 110 | LOG_D("ppp ec20 is registering ppp_device"); 111 | 112 | return ppp_device_register(ppp_device, PPP_DEVICE_NAME, RT_NULL, RT_NULL); 113 | } 114 | INIT_COMPONENT_EXPORT(ppp_ec20_register); 115 | -------------------------------------------------------------------------------- /class/ppp_device_m6312.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2019, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2019-09-24 xiaofao the first version 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #define DBG_TAG "ppp.m6312" 16 | 17 | #ifdef PPP_DEVICE_DEBUG 18 | #define DBG_LVL DBG_LOG 19 | #else 20 | #define DBG_LVL DBG_INFO 21 | #endif 22 | #include 23 | 24 | #define M6312_POWER_ON PIN_HIGH 25 | #define M6312_POWER_OFF PIN_LOW 26 | #ifndef M6312_POWER_PIN 27 | #define M6312_POWER_PIN -1 28 | #endif 29 | 30 | #define M6312_WARTING_TIME_BASE 500 31 | 32 | static const struct modem_chat_data rst_mcd[] = 33 | { 34 | {"+++", MODEM_CHAT_RESP_OK, 10, 1, RT_FALSE}, 35 | {"ATH", MODEM_CHAT_RESP_OK, 1, 1, RT_FALSE}, 36 | {"AT+CMRESET", MODEM_CHAT_RESP_NOT_NEED, 1, 1, RT_FALSE}, 37 | }; 38 | 39 | static const struct modem_chat_data mcd[] = 40 | { 41 | {"AT", MODEM_CHAT_RESP_OK, 10, 1, RT_FALSE}, 42 | {"ATE0", MODEM_CHAT_RESP_OK, 1, 1, RT_FALSE}, 43 | {PPP_APN_CMD, MODEM_CHAT_RESP_OK, 1, 5, RT_FALSE}, 44 | {PPP_DAIL_CMD, MODEM_CHAT_RESP_CONNECT, 1, 30, RT_FALSE}, 45 | }; 46 | 47 | static rt_err_t ppp_m6312_prepare(struct ppp_device *device) 48 | { 49 | if (device->power_pin >= 0) 50 | { 51 | rt_pin_write(device->power_pin, M6312_POWER_OFF); 52 | rt_thread_mdelay(M6312_WARTING_TIME_BASE); 53 | rt_pin_write(device->power_pin, M6312_POWER_ON); 54 | } 55 | else 56 | { 57 | rt_err_t err; 58 | err = modem_chat(device->uart, rst_mcd, sizeof(rst_mcd) / sizeof(rst_mcd[0])); 59 | if (err) 60 | return err; 61 | } 62 | return modem_chat(device->uart, mcd, sizeof(mcd) / sizeof(mcd[0])); 63 | } 64 | 65 | /* ppp_m6312_ops for ppp_device_ops , a common interface */ 66 | static struct ppp_device_ops m6312_ops = 67 | { 68 | .prepare = ppp_m6312_prepare, 69 | }; 70 | 71 | /** 72 | * register m6312 into ppp_device 73 | * 74 | * @return =0: ppp_device register successfully 75 | * <0: ppp_device register failed 76 | */ 77 | int ppp_m6312_register(void) 78 | { 79 | struct ppp_device *ppp_device = RT_NULL; 80 | 81 | ppp_device = rt_malloc(sizeof(struct ppp_device)); 82 | if(ppp_device == RT_NULL) 83 | { 84 | LOG_E("No memory for struct m6312 ppp_device."); 85 | return -RT_ENOMEM; 86 | } 87 | 88 | ppp_device->power_pin = M6312_POWER_PIN; 89 | if (ppp_device->power_pin >= 0) 90 | { 91 | rt_pin_mode(ppp_device->power_pin, PIN_MODE_OUTPUT); 92 | rt_pin_write(ppp_device->power_pin, M6312_POWER_OFF); 93 | } 94 | 95 | ppp_device->ops = &m6312_ops; 96 | LOG_D("ppp m6312 is registering ppp_device"); 97 | return ppp_device_register(ppp_device, PPP_DEVICE_NAME, RT_NULL, RT_NULL); 98 | } 99 | INIT_COMPONENT_EXPORT(ppp_m6312_register); 100 | -------------------------------------------------------------------------------- /class/ppp_device_ml305.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2019, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2021-06-23 HelloByeAll the first version 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #define DBG_TAG "ppp.ml305" 16 | 17 | #ifdef PPP_DEVICE_DEBUG 18 | #define DBG_LVL DBG_LOG 19 | #else 20 | #define DBG_LVL DBG_INFO 21 | #endif 22 | #include 23 | 24 | #define ML305POWER_ON PIN_HIGH 25 | #define ML305POWER_OFF PIN_LOW 26 | #ifndef ML305POWER_PIN 27 | #define ML305POWER_PIN -1 28 | #endif 29 | #define ML305WARTING_TIME_BASE 2000 30 | 31 | #define PPP_DAIL_CMD "ATD*99***1#" /* redefine DAIL CMD */ 32 | #ifndef PKG_USING_CMUX 33 | static const struct modem_chat_data rst_mcd[] = 34 | { 35 | {"AT+MREBOOT", MODEM_CHAT_RESP_NOT_NEED, 30, 1, RT_FALSE}, 36 | {"AT", MODEM_CHAT_RESP_OK, 30, 10,RT_FALSE}, 37 | {"+++", MODEM_CHAT_RESP_NOT_NEED, 30, 1, RT_TRUE}, 38 | {"ATH", MODEM_CHAT_RESP_OK, 30, 1, RT_FALSE}, 39 | }; 40 | 41 | static const struct modem_chat_data mcd[] = 42 | { 43 | {"AT", MODEM_CHAT_RESP_OK, 10, 1, RT_FALSE}, 44 | {"ATE0", MODEM_CHAT_RESP_OK, 1, 1, RT_FALSE}, 45 | {"AT+CPIN?", MODEM_CHAT_RESP_READY, 10, 1, RT_FALSE}, 46 | {"AT+CFUN?", MODEM_CHAT_RESP_OK, 10, 1, RT_FALSE}, 47 | {"AT+CSQ", MODEM_CHAT_RESP_OK, 10, 1, RT_FALSE}, 48 | {PPP_APN_CMD, MODEM_CHAT_RESP_OK, 1, 5, RT_FALSE}, 49 | {"AT+CGACT=1,1", MODEM_CHAT_RESP_OK, 10, 1, RT_FALSE}, 50 | {PPP_DAIL_CMD, MODEM_CHAT_RESP_CONNECT, 1, 30, RT_FALSE}, 51 | }; 52 | #else 53 | static const struct modem_chat_data mcd[] = 54 | { 55 | {"AT", MODEM_CHAT_RESP_NOT_NEED, 10, 1, RT_FALSE}, 56 | {PPP_APN_CMD, MODEM_CHAT_RESP_NOT_NEED, 1, 5, RT_FALSE}, 57 | {PPP_DAIL_CMD, MODEM_CHAT_RESP_NOT_NEED, 1, 30, RT_FALSE}, 58 | }; 59 | #endif 60 | 61 | static rt_err_t ppp_ml305prepare(struct ppp_device *device) 62 | { 63 | #ifndef PKG_USING_CMUX 64 | if (device->power_pin >= 0) 65 | { 66 | rt_pin_write(device->power_pin, ML305POWER_OFF); 67 | rt_thread_mdelay(ML305WARTING_TIME_BASE / 20); 68 | rt_pin_write(device->power_pin, ML305POWER_ON); 69 | rt_thread_mdelay(ML305WARTING_TIME_BASE); 70 | } 71 | else 72 | { 73 | rt_err_t err; 74 | err = modem_chat(device->uart, rst_mcd, sizeof(rst_mcd) / sizeof(rst_mcd[0])); 75 | if (err) 76 | return err; 77 | } 78 | #endif 79 | return modem_chat(device->uart, mcd, sizeof(mcd) / sizeof(mcd[0])); 80 | } 81 | 82 | /* ppp_mlm305ops for ppp_device_ops , a common interface */ 83 | static struct ppp_device_ops ml305ops = 84 | { 85 | .prepare = ppp_ml305prepare, 86 | }; 87 | 88 | /** 89 | * register air720 into ppp_device 90 | * 91 | * @return =0: ppp_device register successfully 92 | * <0: ppp_device register failed 93 | */ 94 | int ppp_ml305register(void) 95 | { 96 | struct ppp_device *ppp_device = RT_NULL; 97 | 98 | ppp_device = rt_malloc(sizeof(struct ppp_device)); 99 | if(ppp_device == RT_NULL) 100 | { 101 | LOG_E("No memory for ml305 ppp_device."); 102 | return -RT_ENOMEM; 103 | } 104 | 105 | ppp_device->power_pin = ML305POWER_PIN; 106 | if (ppp_device->power_pin >= 0) 107 | { 108 | rt_pin_mode(ppp_device->power_pin, PIN_MODE_OUTPUT); 109 | rt_pin_write(ppp_device->power_pin, ML305POWER_ON); 110 | rt_thread_mdelay(ML305WARTING_TIME_BASE); 111 | } 112 | ppp_device->ops = &ml305ops; 113 | 114 | LOG_D("ppp ml305 is registering ppp_device"); 115 | 116 | return ppp_device_register(ppp_device, PPP_DEVICE_NAME, RT_NULL, RT_NULL); 117 | } 118 | INIT_COMPONENT_EXPORT(ppp_ml305register); 119 | -------------------------------------------------------------------------------- /class/ppp_device_sim800.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2019, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2019-08-15 xiangxistu the first version 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #define DBG_TAG "ppp.sim800" 16 | 17 | #ifdef PPP_DEVICE_DEBUG 18 | #define DBG_LVL DBG_LOG 19 | #else 20 | #define DBG_LVL DBG_INFO 21 | #endif 22 | #include 23 | 24 | #define SIM800_POWER_ON PIN_HIGH 25 | #define SIM800_POWER_OFF PIN_LOW 26 | #ifndef SIM800_POWER_PIN 27 | #define SIM800_POWER_PIN -1 28 | #endif 29 | 30 | #define SIM800_WARTING_TIME_BASE 500 31 | 32 | #ifndef PKG_USING_CMUX 33 | static const struct modem_chat_data rst_mcd[] = 34 | { 35 | {"+++", MODEM_CHAT_RESP_NOT_NEED, 30, 1, RT_TRUE}, 36 | {"ATH", MODEM_CHAT_RESP_OK, 30, 1, RT_FALSE}, 37 | }; 38 | 39 | static const struct modem_chat_data mcd[] = 40 | { 41 | {"AT", MODEM_CHAT_RESP_OK, 10, 1, RT_FALSE}, 42 | {"ATE0", MODEM_CHAT_RESP_OK, 1, 1, RT_FALSE}, 43 | {PPP_APN_CMD, MODEM_CHAT_RESP_OK, 1, 5, RT_FALSE}, 44 | {PPP_DAIL_CMD, MODEM_CHAT_RESP_CONNECT, 1, 30, RT_FALSE}, 45 | }; 46 | #else 47 | static const struct modem_chat_data mcd[] = 48 | { 49 | {"AT", MODEM_CHAT_RESP_NOT_NEED, 10, 1, RT_FALSE}, 50 | {PPP_APN_CMD, MODEM_CHAT_RESP_NOT_NEED, 1, 5, RT_FALSE}, 51 | {PPP_DAIL_CMD, MODEM_CHAT_RESP_NOT_NEED, 1, 30, RT_FALSE}, 52 | }; 53 | #endif 54 | 55 | static rt_err_t ppp_sim800_prepare(struct ppp_device *device) 56 | { 57 | #ifndef PKG_USING_CMUX 58 | if (device->power_pin >= 0) 59 | { 60 | rt_pin_write(device->power_pin, SIM800_POWER_OFF); 61 | rt_thread_mdelay(SIM800_WARTING_TIME_BASE); 62 | rt_pin_write(device->power_pin, SIM800_POWER_ON); 63 | } 64 | else 65 | { 66 | rt_err_t err; 67 | err = modem_chat(device->uart, rst_mcd, sizeof(rst_mcd) / sizeof(rst_mcd[0])); 68 | if (err) 69 | return err; 70 | } 71 | #endif 72 | return modem_chat(device->uart, mcd, sizeof(mcd) / sizeof(mcd[0])); 73 | } 74 | 75 | /* ppp_sim800_ops for ppp_device_ops , a common interface */ 76 | static struct ppp_device_ops sim800_ops = 77 | { 78 | .prepare = ppp_sim800_prepare, 79 | }; 80 | 81 | /** 82 | * register sim800 into ppp_device 83 | * 84 | * @return =0: ppp_device register successfully 85 | * <0: ppp_device register failed 86 | */ 87 | int ppp_sim800_register(void) 88 | { 89 | struct ppp_device *ppp_device = RT_NULL; 90 | 91 | ppp_device = rt_malloc(sizeof(struct ppp_device)); 92 | if(ppp_device == RT_NULL) 93 | { 94 | LOG_E("No memory for struct sim800 ppp_device."); 95 | return -RT_ENOMEM; 96 | } 97 | 98 | ppp_device->power_pin = SIM800_POWER_PIN; 99 | if (ppp_device->power_pin >= 0) 100 | { 101 | rt_pin_mode(ppp_device->power_pin, PIN_MODE_OUTPUT); 102 | rt_pin_write(ppp_device->power_pin, SIM800_POWER_OFF); 103 | } 104 | 105 | ppp_device->ops = &sim800_ops; 106 | 107 | LOG_D("ppp sim800 is registering ppp_device"); 108 | 109 | return ppp_device_register(ppp_device, PPP_DEVICE_NAME, RT_NULL, RT_NULL); 110 | } 111 | INIT_COMPONENT_EXPORT(ppp_sim800_register); 112 | -------------------------------------------------------------------------------- /docs/figures/code_transplantation_one.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/ppp_device/0309505e00d45f4626ff0ded5fe6e8ed32c5283e/docs/figures/code_transplantation_one.jpg -------------------------------------------------------------------------------- /docs/figures/code_transplantation_three.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/ppp_device/0309505e00d45f4626ff0ded5fe6e8ed32c5283e/docs/figures/code_transplantation_three.jpg -------------------------------------------------------------------------------- /docs/figures/code_transplantation_two.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/ppp_device/0309505e00d45f4626ff0ded5fe6e8ed32c5283e/docs/figures/code_transplantation_two.jpg -------------------------------------------------------------------------------- /docs/figures/ppp_frame.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/ppp_device/0309505e00d45f4626ff0ded5fe6e8ed32c5283e/docs/figures/ppp_frame.jpg -------------------------------------------------------------------------------- /docs/port.md: -------------------------------------------------------------------------------- 1 | # 模块移植 2 | 3 | 得益于大佬的指导,PPP_DEVICE 的模块功能解耦做得很好; 4 | 5 | 对于新模块的支持,只需要修改 class 文件夹下的文件即可。 6 | 7 | 以添加 Air720 模块为例,从模块的添加到提交到 github 上,**整体来说分为三个部分:** 8 | 9 | #### 1. 在 class 下添加 ppp_device_air720.c 文件 10 | 11 | *ps : 从 class 文件夹下选择任意一个文件重命名也行* 12 | 13 | ![code_transplantation_one](./figures/code_transplantation_one.jpg) 14 | 15 | 在设置引导模块复位命令的时候,需要着重注意 +++ 命令,这里需要查看对应模块的 AT 命令手册; 16 | 17 | ![code_transplantation_one](./figures/code_transplantation_two.jpg) 18 | 19 | 手册中明确要求,+++ 命令在命令输入前 1 秒不能有数据输入,在发送 +++ 命令后有 0.5 秒不能有数据输入; 20 | 21 | 也就是说,我们需要设置这条命令为 22 | 23 | ```c 24 | {"+++", MODEM_CHAT_RESP_NOT_NEED, 30, 1, RT_TRUE}, 25 | ``` 26 | 27 | 如果 AT 手册没有明确要求这样的操作,可以写成 28 | 29 | ```c 30 | {"+++", MODEM_CHAT_RESP_NOT_NEED, 30, 1, RT_FLASE}, 31 | ``` 32 | 33 | 这两者的区别就是,在发送完成 +++ 命令后会不会发送 '\r' 结束符; 34 | 35 | **因为 AT 命令普遍要求结束符,但恰巧 +++ 命令较为特殊**,所以需要特别注意。 36 | 37 | ATTENTION: 38 | 39 | **MODEM_CHAT_RESP_NOT_NEED**,表示发送该条命令需不需要回复,看个人需求选择需要等待回复的内容 40 | 41 | 现在回复内容的判断有以下支持: 42 | 43 | ```c 44 | MODEM_CHAT_RESP_OK, "OK" 45 | MODEM_CHAT_RESP_READY, "READY" 46 | MODEM_CHAT_RESP_CONNECT, "CONNECT" 47 | MODEM_CHAT_RESP_BUSY, "BUSY" 48 | MODEM_CHAT_RESP_NO_CARRIER, "NO CARRIER" 49 | MODEM_CHAT_RESP_ERROR, "ERROR" 50 | MODEM_CHAT_RESP_NOT_NEED 51 | ``` 52 | 53 | 如果模块重启时间很短,在几秒时间左右,可以添加软重启支持(当然需要查看 AT 手册看看命令是什么) 54 | 55 | ```c 56 | {"AT+RESET", MODEM_CHAT_RESP_NOT_NEED, 30, 1, RT_FLASE}, 57 | ``` 58 | 59 | 因为 Air720 的模块重启时间比较长在 20s 左右,所以没有添加。 60 | 61 | 62 | 63 | 剩下的**修改**一下**函数名称**,**宏定义**设置什么的,新模块就已经支持了。 64 | 65 | #### 2. 修改 SConscirpt 脚本 66 | 67 | 在合适位置添加 68 | 69 | ```c 70 | # Air720 71 | if GetDepend(['PPP_DEVICE_USING_AIR720']): 72 | src += Glob('class/ppp_device_air720.c') 73 | ``` 74 | 75 | 命令格式尽量统一,提交 PR 时候也提升效率 76 | 77 | #### 3. 修改 package 的索引 78 | 79 | 这里主要是修改 Kconfig 文件,添加 env 对新模块的支持 80 | 81 | 文件路径 82 | 83 | > env\packages\iot\ppp_device\Kconfig 84 | 85 | ![code_transplantation_three](./figures/code_transplantation_three.jpg) 86 | 87 | 在合适的位置添加 88 | 89 | ```c 90 | config PPP_DEVICE_USING_AIR720 91 | bool "Luat Air720" 92 | ``` 93 | 94 | 如果命名格式统一的话,这里也不用太费脑筋 95 | 96 | **ATTENTION:**修改 env 下的可能会导致更新出现问题,实验时候修改的数据最好做好备份及时还原; 97 | 98 | 99 | 100 | ## 欢迎大家 PR ,一起实现更多模块支持 101 | 102 | * PR 需要有两个不同的仓库,建议 star 并且 frok 103 | * 更多 PR 信息,可以在群里询问,提交前可以把自己仓库里在群里发一下,大家审阅一下提升代码质量。 -------------------------------------------------------------------------------- /inc/ppp_chat.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 xiaofan 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2019-09-19 xiaofan the first version 9 | */ 10 | 11 | #ifndef __MODEM_CHAT_H__ 12 | #define __MODEM_CHAT_H__ 13 | 14 | #include 15 | #include 16 | 17 | #define MODEM_CHAT_RESP_LIST(F) \ 18 | F(MODEM_CHAT_RESP_OK, "OK"), \ 19 | F(MODEM_CHAT_RESP_READY, "READY"), \ 20 | F(MODEM_CHAT_RESP_CONNECT, "CONNECT"), \ 21 | F(MODEM_CHAT_RESP_BUSY, "BUSY"), \ 22 | F(MODEM_CHAT_RESP_NO_CARRIER, "NO CARRIER"), \ 23 | F(MODEM_CHAT_RESP_ERROR, "ERROR") \ 24 | 25 | #define DEFINE_MODEM_RESP_ID_TABLE(id, s) id 26 | 27 | enum { 28 | MODEM_CHAT_RESP_LIST(DEFINE_MODEM_RESP_ID_TABLE), 29 | MODEM_CHAT_RESP_MAX, 30 | MODEM_CHAT_RESP_NOT_NEED, 31 | }; 32 | 33 | struct modem_chat_data { 34 | const char* transmit; 35 | rt_uint8_t expect; // use CHAT_RESP_xxx 36 | rt_uint8_t retries; 37 | rt_uint8_t timeout; // second 38 | rt_bool_t ignore_cr; // ignore CR character if it is RT_TRUE 39 | }; 40 | 41 | rt_err_t modem_chat(rt_device_t serial, const struct modem_chat_data *data, rt_size_t len); 42 | 43 | #endif /* __MODEM_CHAT_H__ */ 44 | -------------------------------------------------------------------------------- /inc/ppp_device.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2019, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2019-08-15 xiangxistu the first version 9 | */ 10 | 11 | #ifndef __PPP_DEVICE_H__ 12 | #define __PPP_DEVICE_H__ 13 | 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | //#define PPP_DEVICE_DEBUG 23 | //#define PPP_DEVICE_DEBUG_TX 24 | //#define PPP_DEVICE_DEBUG_RX 25 | 26 | #ifndef PPP_DEVICE_NAME 27 | #define PPP_DEVICE_NAME "pp" 28 | #endif 29 | 30 | #define PPP_DAIL_CMD "ATD*99#" /* common dailing cmd */ 31 | #ifndef PPP_USING_PUBLIC_APN 32 | #define PPP_APN_CMD "AT+CGDCONT=1,\"IP\","PPP_PRIVATE_APN 33 | #else 34 | #ifdef PPP_APN_CMCC 35 | #define PPP_APN_CMD "AT+CGDCONT=1,\"IP\",\"CMNET\"" /* China Mobile Communication Company */ 36 | #endif 37 | #ifdef PPP_APN_CUCC 38 | #define PPP_APN_CMD "AT+CGDCONT=1,\"IP\",\"UNINET\"" /* China Unicom Communication Company */ 39 | #endif 40 | #ifdef PPP_APN_CTCC 41 | #define PPP_APN_CMD "AT+CGDCONT=1,\"IP\",\"CTNET\"" /* China Telecom Communication Company */ 42 | #endif 43 | #endif 44 | #define PPP_CTL_GET_CSQ 1 45 | #define PPP_CTL_GET_IEMI 2 46 | #define PPP_CTL_GET_TYPE 3 47 | #define PPP_CTL_PREPARE 10 48 | 49 | #define PPP_FRAME_MAX 1550 50 | #define PPP_DROP_BUF PPP_FRAME_MAX 51 | 52 | 53 | #define PPP_DEVICE_SW_VERSION "1.2.0" 54 | #define PPP_DEVICE_SW_VERSION_NUM 0x10200 55 | 56 | enum ppp_trans_type 57 | { 58 | PPP_TRANS_CHAT, 59 | PPP_TRANS_AT_CLIENT 60 | }; 61 | 62 | enum ppp_conn_type 63 | { 64 | PPP_CONNET_UART, /* using uart connect ppp */ 65 | PPP_CONNET_USB /* using usb connect ppp */ 66 | }; 67 | 68 | /** 69 | * @brief PPPoS Client IP Information 70 | * 71 | */ 72 | struct ppp_device 73 | { 74 | struct rt_device parent; /* join rt_device frame */ 75 | rt_device_t uart; /* low-level uart device object */ 76 | const struct ppp_device_ops *ops; /* ppp device ops interface */ 77 | enum ppp_conn_type conn_type; /* using usb or uart */ 78 | rt_base_t power_pin; /* power pin, if device need hardware reset */ 79 | 80 | ppp_pcb *pcb; /* ppp protocol control block */ 81 | struct netif pppif; 82 | 83 | #ifdef PPP_DEVICE_DEBUG_DROP 84 | rt_size_t dropcnt; /* counter of drop bytes */ 85 | rt_size_t droppos; /* valid size of drop buffer */ 86 | rt_uint8_t dropbuf[PPP_DROP_BUF]; /* drop buffer */ 87 | #endif 88 | 89 | rt_size_t rxpos; /* valid size of receive frame buffer */ 90 | rt_uint8_t rxbuf[PPP_FRAME_MAX]; /* receive frame buffer */ 91 | rt_uint8_t state; /* internal state */ 92 | 93 | struct rt_event event; /* interthread communication */ 94 | 95 | rt_thread_t recv_tid; /* recieve thread point */ 96 | void *user_data; /* reserve */ 97 | }; 98 | 99 | struct ppp_device_ops 100 | { 101 | rt_err_t (*prepare) (struct ppp_device *dev); 102 | }; 103 | 104 | enum ppp_reci_status 105 | { 106 | PPP_DATA_VERIFY, 107 | PPP_DATA_START, 108 | PPP_DATA_END 109 | }; 110 | 111 | /* store at_client rx_callback function */ 112 | typedef rt_err_t (*uart_rx_cb)(rt_device_t dev, rt_size_t size); 113 | 114 | /* offer register funciton to user */ 115 | int ppp_device_register(struct ppp_device *ppp_device, const char *dev_name, const char *uart_name, void *user_data); 116 | int ppp_device_attach(struct ppp_device *ppp_device, const char *uart_name, void *user_data); 117 | int ppp_device_detach(struct ppp_device *ppp_device); 118 | 119 | #endif /* __PPP_DEVICE_H__ */ 120 | -------------------------------------------------------------------------------- /inc/ppp_netif.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2019, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2019-08-15 xiangxistu the first version 9 | */ 10 | 11 | #ifndef __NETIF_PPPNETIF_H__ 12 | #define __NETIF_PPPNETIF_H__ 13 | 14 | #include "lwip/netif.h" 15 | #include 16 | 17 | #define NIOCTL_GADDR 0x01 18 | #ifndef RT_LWIP_PPP_MTU 19 | #define PPPNET_MTU 1500 20 | #else 21 | #define PPPNET_MTU RT_LWIP_PPP_MTU 22 | #endif 23 | 24 | /* eth flag with auto_linkup or phy_linkup */ 25 | #define ETHIF_LINK_AUTOUP 0x0000 26 | #define ETHIF_LINK_PHYUP 0x0100 27 | 28 | /* proviode a public interface to register netdev */ 29 | rt_err_t ppp_netdev_add(struct netif *ppp_netif); 30 | rt_err_t ppp_netdev_refresh(struct netif *ppp_netif); 31 | void ppp_netdev_del(struct netif *ppp_netif); 32 | extern struct netdev *netdev_get_by_name(const char *name); 33 | extern int netdev_unregister(struct netdev *netdev); 34 | 35 | #endif /* __NETIF_PPPNETIF_H__ */ 36 | -------------------------------------------------------------------------------- /samples/ppp_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2019, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2019-08-15 xiangxistu the first version 9 | * 2019-09-26 xiangxistu add ppp_device_attach and ppp_device_detach 10 | */ 11 | 12 | #include 13 | #include 14 | 15 | #define DBG_TAG "ppp.sample" 16 | 17 | #ifdef PPP_DEVICE_DEBUG 18 | #define DBG_LVL DBG_LOG 19 | #else 20 | #define DBG_LVL DBG_INFO 21 | #endif 22 | 23 | #include 24 | 25 | /* if you want connect network again, you can use this function to create a new ppp link */ 26 | int ppp_sample_start(void) 27 | { 28 | rt_device_t device = RT_NULL; 29 | device = rt_device_find(PPP_DEVICE_NAME); 30 | if(device == RT_NULL) 31 | { 32 | LOG_E("Can't find device (%s).", PPP_DEVICE_NAME); 33 | return -RT_ERROR; 34 | } 35 | if(ppp_device_attach((struct ppp_device *)device, PPP_CLIENT_NAME, RT_NULL) != RT_EOK) 36 | { 37 | LOG_E("ppp_device_attach execute failed."); 38 | return -RT_ERROR; 39 | } 40 | return RT_EOK; 41 | } 42 | #ifndef PKG_USING_CMUX 43 | INIT_APP_EXPORT(ppp_sample_start); 44 | #endif 45 | MSH_CMD_EXPORT_ALIAS(ppp_sample_start, ppp_start, a sample of ppp device for dailing to network); 46 | 47 | /* close ppp link ,turn off modem form network */ 48 | int ppp_sample_stop(void) 49 | { 50 | rt_device_t device = RT_NULL; 51 | device = rt_device_find(PPP_DEVICE_NAME); 52 | if(device == RT_NULL) 53 | { 54 | LOG_E("Can't find device (%s).", PPP_DEVICE_NAME); 55 | return -RT_ERROR; 56 | } 57 | if(ppp_device_detach((struct ppp_device *)device) != RT_EOK) 58 | { 59 | LOG_E("ppp_device_detach execute failed."); 60 | return -RT_ERROR; 61 | } 62 | return RT_EOK; 63 | } 64 | MSH_CMD_EXPORT_ALIAS(ppp_sample_stop, ppp_stop, a sample of ppp device for turning off network); 65 | 66 | #ifdef PPP_DEVICE_DEBUG_DROP 67 | static int ppp_drop(void) 68 | { 69 | rt_device_t device = RT_NULL; 70 | struct ppp_device *ppp_device; 71 | device = rt_device_find(PPP_DEVICE_NAME); 72 | if(device == RT_NULL) 73 | { 74 | LOG_E("Can't find device (%s).", PPP_DEVICE_NAME); 75 | return -RT_ERROR; 76 | } 77 | ppp_device = (struct ppp_device*)device; 78 | LOG_I("%ld", (unsigned long)(ppp_device->dropcnt + ppp_device->droppos)); 79 | return RT_EOK; 80 | } 81 | 82 | MSH_CMD_EXPORT_ALIAS(ppp_drop, ppp_drop, show drop statistics); 83 | #endif 84 | -------------------------------------------------------------------------------- /src/ppp_chat.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 xiaofan 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2019-09-19 xiaofan the first version 9 | */ 10 | 11 | #include "ppp_chat.h" 12 | #include "ppp_device.h" 13 | 14 | #ifndef PKG_USING_CMUX 15 | #define DBG_TAG "ppp.chat" 16 | #else 17 | #define DBG_TAG "chat" 18 | #endif 19 | 20 | #ifdef PPP_DEVICE_DEBUG 21 | #define DBG_LVL DBG_LOG 22 | #else 23 | #define DBG_LVL DBG_INFO 24 | #endif 25 | 26 | #include 27 | 28 | #define CHAT_READ_BUF_MAX 16 29 | 30 | /** 31 | * In order to match response, we need a string search algorithm 32 | * KMP and AC algorithm both are good choice, But we need the code 33 | * is simple, readable and use lower RAM/ROM. 34 | * So We use a simplified search algorithm, this alg is like the KMP. 35 | * Specifically we assume the failure vecotor is [-1, 0, 0, 0, ...] 36 | * This assuming is not work for all pattern string. Fortunately, 37 | * it's work for this scene. 38 | */ 39 | 40 | #define DEFINE_MODEM_RESP_STRDATA_TABLE(id, str) [id] = str 41 | #define DEFINE_MODEM_RESP_STRLEN_TABLE(id, str) [id] = (sizeof(str)-1) 42 | 43 | 44 | static char *resp_strdata[] = 45 | { 46 | MODEM_CHAT_RESP_LIST(DEFINE_MODEM_RESP_STRDATA_TABLE) 47 | }; 48 | 49 | static rt_uint8_t resp_strlen[] = 50 | { 51 | MODEM_CHAT_RESP_LIST(DEFINE_MODEM_RESP_STRLEN_TABLE) 52 | }; 53 | 54 | #define CHAT_DATA_FMT "" 55 | #define CHAT_DATA_STR(data) (data)->transmit, resp2str((data)->expect), (data)->retries, (data)->timeout 56 | 57 | static const char* resp2str(rt_uint8_t resp_id) 58 | { 59 | if (resp_id == MODEM_CHAT_RESP_NOT_NEED) 60 | return "(not need)"; 61 | RT_ASSERT(resp_id < MODEM_CHAT_RESP_MAX); 62 | return resp_strdata[resp_id]; 63 | } 64 | 65 | /* only one device support */ 66 | static struct rt_completion rx_comp_p; 67 | 68 | static rt_uint8_t resp_match(rt_uint8_t resp_id, rt_uint8_t state, char ch) 69 | { 70 | while (1) 71 | { 72 | if (resp2str(resp_id)[state] == ch) 73 | return state + 1; 74 | if (state == 0) 75 | return 0; 76 | state = 0; 77 | }; 78 | } 79 | 80 | static rt_bool_t resp_matched(rt_uint8_t resp_id, rt_uint8_t state) 81 | { 82 | return state == resp_strlen[resp_id]; 83 | } 84 | 85 | /** 86 | * chat_rx_ind, callback function if serial recieve data 87 | * 88 | * @param device the point of device driver structure, uart structure 89 | * @param size the indication callback function need this parameter 90 | * 91 | * @return RT_EOK 92 | */ 93 | static rt_err_t chat_rx_ind(rt_device_t device, rt_size_t size) 94 | { 95 | rt_completion_done(&rx_comp_p); 96 | return RT_EOK; 97 | } 98 | 99 | /** 100 | * chat_read_until, waitting for recieve data from serial 101 | * 102 | * @param serial the point of device driver structure, uart structure 103 | * @param buffer the buffer is waitting for recieve uart data from ppp 104 | * @param size the max length of CHAT_READ_BUF_MAX 105 | * @param stop the max of tick time 106 | * 107 | * @return >=0: the length of read data 108 | * <0 : rt_device_read failed 109 | */ 110 | static rt_size_t chat_read_until(rt_device_t serial, void *buffer, rt_size_t size, rt_tick_t stop) 111 | { 112 | rt_size_t rdlen; 113 | rt_tick_t wait; 114 | 115 | rt_completion_init(&rx_comp_p); 116 | rdlen = rt_device_read(serial, 0, buffer, size); 117 | if (rdlen) 118 | return rdlen; 119 | 120 | wait = stop - rt_tick_get(); 121 | if (wait > RT_TICK_MAX / 2) 122 | return 0; 123 | 124 | rt_completion_wait(&rx_comp_p, wait); 125 | return rt_device_read(serial, 0, buffer, size); 126 | } 127 | 128 | /** 129 | * modem_flush_rx , clear data what is in the rx buffer 130 | * 131 | * @param serial the point of device driver structure, uart structure 132 | */ 133 | static void modem_flush_rx(rt_device_t serial) 134 | { 135 | char rdbuf[CHAT_READ_BUF_MAX] = {0}; 136 | 137 | while (rt_device_read(serial, 0, rdbuf, CHAT_READ_BUF_MAX)); 138 | } 139 | 140 | /** 141 | * modem_chat_once , send an order to control modem 142 | * 143 | * @param serial the point of device driver structure, uart structure 144 | * @param data the AT command 145 | * 146 | * @return =0: modem_chat_once successful 147 | * <0: modem_chat_once failed 148 | */ 149 | static rt_err_t modem_chat_once(rt_device_t serial, const struct modem_chat_data *data) 150 | { 151 | rt_uint8_t resp_state[MODEM_CHAT_RESP_MAX] = { 0 }, resp; 152 | rt_tick_t stop = rt_tick_get() + data->timeout*RT_TICK_PER_SECOND; 153 | rt_size_t rdlen, pos; 154 | char rdbuf[CHAT_READ_BUF_MAX]; 155 | 156 | if (data->transmit) 157 | { 158 | LOG_D(CHAT_DATA_FMT " transmit --> modem", CHAT_DATA_STR(data)); 159 | rt_device_write(serial, 0, data->transmit, rt_strlen(data->transmit)); 160 | if (data->ignore_cr == RT_FALSE) 161 | rt_device_write(serial, 0, "\r", 1); 162 | } 163 | 164 | if (data->expect == MODEM_CHAT_RESP_NOT_NEED) 165 | { 166 | rt_thread_mdelay(1000*data->timeout); 167 | return RT_EOK; 168 | } 169 | 170 | do 171 | { 172 | rdlen = chat_read_until(serial, rdbuf, CHAT_READ_BUF_MAX, stop); 173 | for (pos = 0; pos < rdlen; pos++) 174 | { 175 | for (resp = 0; resp < MODEM_CHAT_RESP_MAX; resp++) 176 | { 177 | resp_state[resp] = resp_match(resp, resp_state[resp], rdbuf[pos]); 178 | if (resp_matched(resp, resp_state[resp])) 179 | { 180 | if (resp == data->expect) 181 | return RT_EOK; 182 | 183 | LOG_W(CHAT_DATA_FMT" not matched, got: %s", CHAT_DATA_STR(data), resp2str(resp)); 184 | return -RT_ERROR; 185 | } 186 | } 187 | } 188 | } while ( stop - rt_tick_get() < RT_TICK_MAX / 2); 189 | LOG_W(CHAT_DATA_FMT" timeout", CHAT_DATA_STR(data)); 190 | return -RT_ETIMEOUT; 191 | } 192 | 193 | /** 194 | * modem_chat_internal , init modem or turn modem into ppp type 195 | * 196 | * @param serial the point of device driver structure, uart structure 197 | * @param data the AT command, it is the address of chat strcuture, a collection of AT command 198 | * @param len the length of this collection of AT command 199 | * 200 | * @return =0: modem_chat_internal successful 201 | * <0: modem_chat_internal failed 202 | */ 203 | static rt_err_t modem_chat_internal(rt_device_t serial, const struct modem_chat_data *data, rt_size_t len) 204 | { 205 | rt_err_t err = RT_EOK; 206 | rt_size_t i; 207 | rt_uint8_t retry_time; 208 | 209 | for (i = 0; i < len; i++) 210 | { 211 | LOG_D(CHAT_DATA_FMT" running", CHAT_DATA_STR(&data[i])); 212 | for (retry_time = 0; retry_time < data[i].retries; retry_time++) 213 | { 214 | modem_flush_rx(serial); 215 | err = modem_chat_once(serial, &data[i]); 216 | if (err == RT_EOK) 217 | break; 218 | } 219 | if (err != RT_EOK) 220 | { 221 | LOG_E(CHAT_DATA_FMT" fail", CHAT_DATA_STR(&data[i])); 222 | break; 223 | } 224 | LOG_D(CHAT_DATA_FMT" success", CHAT_DATA_STR(&data[i])); 225 | } 226 | return err; 227 | } 228 | 229 | /** 230 | * modem_chat , a function for ppp dailing to network, it will set rx_indicate 231 | * 232 | * @param device the point of device driver structure, uart structure 233 | * @param data the AT command, it is the address of chat strcuture, a collection of AT command 234 | * @param len the length of this collection of AT command 235 | * 236 | * @return =0: modem_chat successful 237 | * <0: modem_chat failed 238 | */ 239 | rt_err_t modem_chat(rt_device_t serial, const struct modem_chat_data *data, rt_size_t len) 240 | { 241 | rt_err_t (*old_rx_ind)(rt_device_t dev, rt_size_t size) = RT_NULL; 242 | 243 | rt_err_t err = RT_EOK; 244 | 245 | rt_completion_init(&rx_comp_p); 246 | old_rx_ind = serial->rx_indicate; 247 | rt_device_set_rx_indicate(serial, chat_rx_ind); 248 | 249 | LOG_D("(%.*s) has control by modem_chat.", RT_NAME_MAX, serial->parent.name); 250 | err = modem_chat_internal(serial, data, len); 251 | if (err != RT_EOK) 252 | { 253 | LOG_E("chat failed"); 254 | } 255 | 256 | serial->rx_indicate = old_rx_ind; 257 | #ifdef PKG_USING_CMUX 258 | LOG_D("(%.*s) has control by cmux.", RT_NAME_MAX, serial->parent.name); 259 | #else 260 | LOG_D("(%.*s) has control by ppp_device.", RT_NAME_MAX, serial->parent.name); 261 | #endif 262 | return err; 263 | } 264 | -------------------------------------------------------------------------------- /src/ppp_device.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2019, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2019-08-15 xiangxistu the first version 9 | * 2019-10-01 xiaofan rewrite ppp_recv thread 10 | * 2022-01-16 Jianjia Ma implement timeout for continuous data 11 | */ 12 | 13 | #include 14 | #include 15 | 16 | #ifdef RT_USING_NETDEV 17 | #include 18 | #endif 19 | 20 | #define DBG_TAG "ppp.dev" 21 | 22 | #ifdef PPP_DEVICE_DEBUG 23 | #define DBG_LVL DBG_LOG 24 | #else 25 | #define DBG_LVL DBG_INFO 26 | #endif 27 | 28 | #include 29 | 30 | #define PPP_THREAD_STACK_SIZE (PPP_RECV_READ_MAX + 2048) 31 | #define PPP_THREAD_PRIORITY 9 32 | 33 | enum 34 | { 35 | PPP_STATE_PREPARE, 36 | PPP_STATE_RECV_DATA, 37 | }; 38 | 39 | #ifndef PPP_RECV_READ_MAX 40 | #define PPP_RECV_READ_MAX 2048 41 | #endif 42 | 43 | #define PPP_EVENT_RX_NOTIFY 1 // serial incoming a byte 44 | #define PPP_EVENT_LOST 2 // PPP connection is lost 45 | #define PPP_EVENT_CLOSE_REQ 4 // user want close ppp_device 46 | #define PPP_EVENT_CLOSED 8 // ppp_recv thread will send CLOSED event when ppp_recv thread is safe exit 47 | 48 | #if RT_LWIP_TCPTHREAD_STACKSIZE < 2048 49 | #error "tcpip stack is too small, should greater than 2048." 50 | #endif 51 | 52 | static struct ppp_device *_g_ppp_device = RT_NULL; 53 | 54 | /** 55 | * Receive callback function , send PPP_EVENT_RX_NOTIFY event when uart acquire data 56 | * 57 | * @param dev the point of device driver structure, uart structure 58 | * @param size the indication callback function need this parameter 59 | * 60 | * @return RT_EOK 61 | */ 62 | static rt_err_t ppp_device_rx_ind(rt_device_t dev, rt_size_t size) 63 | { 64 | RT_ASSERT(dev != RT_NULL); 65 | struct ppp_device *ppp_dev = _g_ppp_device; 66 | 67 | /* when recieve data from uart , send event to wake up recieve thread */ 68 | rt_event_send(&ppp_dev->event, PPP_EVENT_RX_NOTIFY); 69 | 70 | return RT_EOK; 71 | } 72 | 73 | /** 74 | * using ppp_data_send send data to lwIP procotol stack PPPoS serial output callback 75 | * 76 | * @param pcb pcb PPP control block 77 | * @param data data Buffer to write to serial port 78 | * @param len the Length of the data buffer 79 | * @param ppp_device ctx Context of callback , ppp_device 80 | * 81 | * @return the point of rt_device_write fucntion or RT_NULL 82 | */ 83 | static uint32_t ppp_data_send(ppp_pcb *pcb, uint8_t *data, uint32_t len, void *ppp_device) 84 | { 85 | RT_ASSERT(pcb != RT_NULL); 86 | RT_ASSERT(ppp_device != RT_NULL); 87 | 88 | struct ppp_device *device = (struct ppp_device *)ppp_device; 89 | 90 | if (device->state == PPP_STATE_PREPARE) 91 | return 0; 92 | 93 | #ifdef PPP_DEVICE_DEBUG_TX 94 | LOG_HEX("ppp_tx", 32, data, len); 95 | #endif 96 | /* the return data is the actually written size on successful */ 97 | return rt_device_write(device->uart, 0, data, len); 98 | } 99 | 100 | /** 101 | * ppp_status_changed callback function 102 | * 103 | * @param pcb protocol caontrol block 104 | * @param err_code the result of ppp conncet 105 | * @param ctx ctx Context of callback , ppp_device 106 | */ 107 | static void ppp_status_changed(ppp_pcb *pcb, int err_code, void *ctx) 108 | { 109 | struct ppp_device *pppdev = (struct ppp_device *)ctx; 110 | struct netif *pppif = ppp_netif(pcb); 111 | switch (err_code) 112 | { 113 | case PPPERR_NONE: /* Connected */ 114 | pppdev->pppif.mtu = pppif->mtu; 115 | ppp_netdev_refresh(&pppdev->pppif); 116 | /* set default route */ 117 | pppapi_set_default(pcb); 118 | { 119 | #ifdef RT_USING_NETDEV 120 | netdev_set_default(netdev_get_by_name(PPP_DEVICE_NAME)); 121 | #endif /* RT_USING_NETDEV */ 122 | } 123 | LOG_I("ppp_device connect successfully."); 124 | break; 125 | case PPPERR_PARAM: 126 | LOG_E("Invalid parameter."); 127 | break; 128 | case PPPERR_OPEN: 129 | LOG_E("Unable to open PPP session."); 130 | break; 131 | case PPPERR_DEVICE: 132 | LOG_E("Invalid I/O device for PPP."); 133 | break; 134 | case PPPERR_ALLOC: 135 | LOG_E("Unable to allocate resources."); 136 | break; 137 | case PPPERR_USER: 138 | LOG_D("User interrupt"); 139 | break; 140 | case PPPERR_CONNECT: /* Connection lost */ 141 | LOG_E("ppp connect lost."); 142 | break; 143 | case PPPERR_AUTHFAIL: 144 | LOG_E("Failed authentication challenge."); 145 | break; 146 | case PPPERR_PROTOCOL: 147 | LOG_E("Failed to meet protocol."); 148 | break; 149 | case PPPERR_PEERDEAD: 150 | LOG_E("Connection timeout."); 151 | break; 152 | case PPPERR_IDLETIMEOUT: 153 | LOG_E("Idle Timeout."); 154 | break; 155 | case PPPERR_CONNECTTIME: 156 | LOG_E("Max connect time reached."); 157 | break; 158 | case PPPERR_LOOPBACK: 159 | LOG_E("Loopback detected."); 160 | break; 161 | default: 162 | LOG_E("Unknown error code %d.", err_code); 163 | break; 164 | } 165 | if (err_code != PPPERR_NONE) 166 | rt_event_send(&pppdev->event, PPP_EVENT_LOST); 167 | } 168 | 169 | /** 170 | * Receive thread , store uart data and transform tcpip stack 171 | * 172 | * @param device the point of device driver structure, ppp_device structure 173 | * 174 | * @return RT_EOK we shouldn't let the recieve thread return data, recieve thread need keepalive all the time 175 | */ 176 | static int ppp_recv_entry(struct ppp_device *device) 177 | { 178 | const rt_uint32_t interested_event = PPP_EVENT_RX_NOTIFY | PPP_EVENT_LOST | PPP_EVENT_CLOSE_REQ; 179 | rt_uint32_t event; 180 | rt_tick_t last_recv_tick; 181 | rt_size_t rlen; 182 | rt_size_t len; 183 | rt_uint8_t buffer[PPP_RECV_READ_MAX]; 184 | rt_bool_t closing = RT_FALSE; 185 | 186 | rt_event_control(&device->event, RT_IPC_CMD_RESET, RT_NULL); 187 | device->state = PPP_STATE_PREPARE; 188 | 189 | while (1) 190 | { 191 | if (device->state == PPP_STATE_PREPARE) 192 | { 193 | if (!device->ops->prepare || device->ops->prepare(device) == RT_EOK) 194 | { 195 | /* throw away the dirty data in the uart buffer */ 196 | rt_device_read(device->uart, 0, buffer, PPP_RECV_READ_MAX); 197 | 198 | device->state = PPP_STATE_RECV_DATA; 199 | pppapi_connect(device->pcb, 0); 200 | } 201 | else 202 | { 203 | LOG_E("%s prepare fail, try again later", device->parent.parent.name); 204 | rt_thread_mdelay(10*1000); 205 | } 206 | continue; 207 | } 208 | 209 | rt_event_recv(&device->event, interested_event, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &event); 210 | 211 | if (event & PPP_EVENT_RX_NOTIFY) 212 | { 213 | len = 0; 214 | rlen = 0; 215 | last_recv_tick = rt_tick_get_millisecond(); 216 | do 217 | { 218 | rlen = rt_device_read(device->uart, 0, &buffer[len], PPP_RECV_READ_MAX - len); 219 | #ifdef PPP_DEVICE_DEBUG_RX 220 | LOG_HEX("ppp_rx", 32, &buffer[len], rlen); 221 | #endif 222 | if (rlen > 0) 223 | { 224 | len += rlen; 225 | last_recv_tick = rt_tick_get_millisecond(); 226 | } 227 | /* wait until buffer full or no more data receiving for 1ms */ 228 | if (len >= PPP_RECV_READ_MAX || rt_tick_get_millisecond() - last_recv_tick >= 1) 229 | { 230 | pppos_input_tcpip(device->pcb, (u8_t *)buffer, len); 231 | len = 0; 232 | } 233 | 234 | /* slow down the speed of receive, transfer more data to ppp in once. */ 235 | rt_thread_mdelay(1); 236 | } while (len); 237 | } 238 | 239 | if (event & PPP_EVENT_CLOSE_REQ) 240 | { 241 | LOG_D("user request close ppp"); 242 | closing = RT_TRUE; 243 | pppapi_close(device->pcb, 0); 244 | } 245 | 246 | if (event & PPP_EVENT_LOST) 247 | { 248 | if (closing) 249 | { 250 | LOG_W("ppp closing success"); 251 | rt_event_send(&device->event, PPP_EVENT_CLOSED); 252 | break; 253 | } 254 | LOG_W("ppp lost, try reconnect"); 255 | device->state = PPP_STATE_PREPARE; 256 | } 257 | } 258 | 259 | return RT_EOK; 260 | } 261 | 262 | /** 263 | * Creat a thread to creat receive thread function 264 | * 265 | * @param device the point of device driver structure, ppp_device structure 266 | * 267 | * @return RT_EOK recieve thread create and startup successfully 268 | * -RT_ERROR create recieve thread successfully 269 | * -RT_ENOMEM startup recieve thread successfully 270 | */ 271 | static int ppp_recv_entry_creat(struct ppp_device *device) 272 | { 273 | rt_int8_t result = RT_EOK; 274 | 275 | /* dynamic creat a recv_thread */ 276 | device->recv_tid = rt_thread_create("ppp_recv", 277 | (void (*)(void *parameter))ppp_recv_entry, 278 | device, 279 | PPP_THREAD_STACK_SIZE, 280 | PPP_THREAD_PRIORITY, 281 | 20); 282 | if (device->recv_tid == RT_NULL) 283 | { 284 | LOG_E("PPP device initialize failed! ppp_device_recv_tid create failed!"); 285 | result = -RT_ENOMEM; 286 | goto __exit; 287 | } 288 | 289 | /* if you create a thread, never forget to start it */ 290 | result = rt_thread_startup(device->recv_tid); 291 | if(result != RT_EOK) 292 | goto __exit; 293 | 294 | return result; 295 | 296 | __exit: 297 | rt_memset(device, 0x00, sizeof(struct ppp_device)); 298 | return result; 299 | } 300 | 301 | /** 302 | * ppp device init function,set ops funciton and base config 303 | * 304 | * @param device the point of device driver structure, rt_device structure 305 | * 306 | * @return RT_EOK 307 | */ 308 | static rt_err_t ppp_device_init(struct rt_device *device) 309 | { 310 | RT_ASSERT(device != RT_NULL); 311 | 312 | struct ppp_device *ppp_device = (struct ppp_device *)device; 313 | RT_ASSERT(ppp_device != RT_NULL); 314 | device->flag |= RT_DEVICE_FLAG_STANDALONE; 315 | #ifdef PPP_DEVICE_DEBUG_DROP 316 | ppp_device->dropcnt = 0; 317 | ppp_device->droppos = 0; 318 | #endif 319 | return RT_EOK; 320 | } 321 | 322 | /** 323 | * initialize ppp device and set callback function 324 | * 325 | * @param device the point of device driver structure, rt_device structure 326 | * @param oflag the open flag of rt_device 327 | * 328 | * @return the result 329 | */ 330 | static rt_err_t ppp_device_open(struct rt_device *device, rt_uint16_t oflag) 331 | { 332 | int result = RT_EOK; 333 | rt_uint16_t uart_oflag; 334 | RT_ASSERT(device != RT_NULL); 335 | 336 | struct ppp_device *ppp_device = (struct ppp_device *)device; 337 | 338 | uart_oflag = RT_DEVICE_OFLAG_RDWR; 339 | if (ppp_device->uart->flag & RT_DEVICE_FLAG_DMA_RX) 340 | uart_oflag |= RT_DEVICE_FLAG_DMA_RX; 341 | else if (ppp_device->uart->flag & RT_DEVICE_FLAG_INT_RX) 342 | uart_oflag = RT_DEVICE_FLAG_INT_RX; 343 | if (ppp_device->uart->flag & RT_DEVICE_FLAG_DMA_TX) 344 | uart_oflag |= RT_DEVICE_FLAG_DMA_TX; 345 | else if (ppp_device->uart->flag & RT_DEVICE_FLAG_INT_TX) 346 | uart_oflag |= RT_DEVICE_FLAG_INT_TX; 347 | 348 | if (rt_device_open(ppp_device->uart, uart_oflag) != RT_EOK) 349 | { 350 | LOG_E("ppp device open failed."); 351 | result = -RT_ERROR; 352 | goto __exit; 353 | } 354 | 355 | rt_event_init(&ppp_device->event, "pppev", RT_IPC_FLAG_FIFO); 356 | /* we can do nothing */ 357 | 358 | RT_ASSERT(ppp_device->uart && ppp_device->uart->type == RT_Device_Class_Char); 359 | 360 | /* uart transfer into tcpip protocol stack */ 361 | rt_device_set_rx_indicate(ppp_device->uart, ppp_device_rx_ind); 362 | LOG_D("(%.*s) is used by ppp_device.", RT_NAME_MAX, ppp_device->uart->parent.name); 363 | 364 | 365 | /* creat pppos */ 366 | ppp_device->pcb = pppapi_pppos_create(&(ppp_device->pppif), ppp_data_send, ppp_status_changed, ppp_device); 367 | if (ppp_device->pcb == RT_NULL) 368 | { 369 | LOG_E("Create ppp pcb failed."); 370 | result = -RT_ERROR; 371 | goto __exit; 372 | } 373 | LOG_D("pppapi_pppos_create has created a protocol control block."); 374 | ppp_netdev_add(&ppp_device->pppif); 375 | 376 | /* set netif name */ 377 | ppp_device->pppif.name[0] = ppp_device->parent.parent.name[0]; 378 | ppp_device->pppif.name[1] = ppp_device->parent.parent.name[1]; 379 | 380 | if (result != RT_EOK) 381 | { 382 | LOG_E("pppapi_set_default execute failed."); 383 | result = -RT_ERROR; 384 | goto __exit; 385 | } 386 | LOG_D("pppapi_set_default has set a default route."); 387 | 388 | /* dns */ 389 | ppp_set_usepeerdns(ppp_device->pcb, 1); 390 | LOG_D("ppp_set_usepeerdns has set a dns number."); 391 | 392 | #ifdef PPP_USING_PRIVATE_APN 393 | /* set authorize */ 394 | #if PAP_SUPPORT 395 | ppp_set_auth(ppp_device->pcb , PPPAUTHTYPE_PAP, PPP_PRIVATE_APN_ACCOUNT, PPP_PRIVATE_APN_PASSWORD); 396 | #elif CHAP_SUPPORT 397 | ppp_set_auth(ppp_device->pcb, PPPAUTHTYPE_CHAP, ppp_device->config.user_name, ppp_device->config.user_name); 398 | #else 399 | #error "Unsupported AUTH Negotiation" 400 | #endif 401 | LOG_D("ppp_set_auth has passed verification."); 402 | #endif /* PPP_AUTHORIZE */ 403 | 404 | /* Creat a thread to creat ppp recieve function */ 405 | result = ppp_recv_entry_creat(ppp_device); 406 | if (result != RT_EOK) 407 | { 408 | LOG_E("Creat a thread to creat ppp recieve function failed."); 409 | result = -RT_ERROR; 410 | goto __exit; 411 | } 412 | LOG_D("Creat a thread to creat ppp recieve function successful."); 413 | 414 | __exit: 415 | 416 | return result; 417 | } 418 | 419 | /** 420 | * Close ppp device 421 | * 422 | * @param device the point of device driver structure, rt_device structure 423 | * 424 | * @return RT_EOK 425 | */ 426 | static rt_err_t ppp_device_close(struct rt_device *device) 427 | { 428 | rt_uint32_t event; 429 | RT_ASSERT(device != RT_NULL); 430 | extern void ppp_netdev_del(struct netif *ppp_netif); 431 | 432 | struct ppp_device *ppp_device = (struct ppp_device *)device; 433 | RT_ASSERT(ppp_device != RT_NULL); 434 | 435 | rt_event_send(&ppp_device->event, PPP_EVENT_CLOSE_REQ); 436 | rt_event_recv(&ppp_device->event, PPP_EVENT_CLOSED, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &event); 437 | 438 | rt_device_set_rx_indicate(ppp_device->uart, RT_NULL); 439 | ppp_netdev_del(&ppp_device->pppif); 440 | ppp_free(ppp_device->pcb); 441 | LOG_D("ppp netdev has been detach."); 442 | rt_event_detach(&ppp_device->event); 443 | 444 | rt_device_close(ppp_device->uart); 445 | /* cut down piont to piont at data link layer */ 446 | LOG_I("ppp_device has been closed."); 447 | return RT_EOK; 448 | } 449 | 450 | /** 451 | * Control ppp device , access ppp mode or accsee AT mode; but not it is useless 452 | * 453 | * @param device the point of device driver structure, rt_device structure 454 | * @param cmd the command of device 455 | * @param args the private data of you send 456 | * 457 | * @return -RT_ENOSYS 458 | */ 459 | static rt_err_t ppp_device_control(struct rt_device *device,int cmd, void *args) 460 | { 461 | RT_ASSERT(device != RT_NULL); 462 | 463 | return -RT_ENOSYS; 464 | } 465 | 466 | /* ppp device ops */ 467 | #ifdef RT_USING_DEVICE_OPS 468 | const struct rt_device_ops ppp_device_ops = 469 | { 470 | ppp_device_init, 471 | ppp_device_open, 472 | ppp_device_close, 473 | ppp_device_control 474 | }; 475 | #endif 476 | 477 | /** 478 | * Register ppp_device into rt_device frame,set ops function to rt_device inferface 479 | * 480 | * @param ppp_device the point of device driver structure, ppp_device structure 481 | * @param dev_name the name of ppp_device name 482 | * @param uart_name the name of uart name what you used 483 | * @param user_data private data 484 | * 485 | * @return RT_EOK ppp_device registered into rt_device frame successfully 486 | */ 487 | int ppp_device_register(struct ppp_device *ppp_device, const char *dev_name, const char *uart_name, void *user_data) 488 | { 489 | RT_ASSERT(ppp_device != RT_NULL); 490 | rt_err_t result = RT_EOK; 491 | struct rt_device *device = RT_NULL; 492 | 493 | device = &(ppp_device->parent); 494 | device->type = RT_Device_Class_NetIf; 495 | 496 | #ifdef RT_USING_DEVICE_OPS 497 | device->ops = &ppp_device_ops; 498 | #else 499 | device->init = ppp_device_init; 500 | device->open = ppp_device_open; 501 | device->close = ppp_device_close; 502 | device->read = RT_NULL; 503 | device->write = RT_NULL; 504 | device->control = ppp_device_control; 505 | #endif 506 | 507 | /* now we supprot only one device */ 508 | if (_g_ppp_device != RT_NULL) 509 | { 510 | LOG_E("Only one device support."); 511 | RT_ASSERT(_g_ppp_device == RT_NULL); 512 | } 513 | 514 | /* attention: you can't use ppp_device as a server in you network, unless 515 | sim of the modem module used supprots getting public IP address. */ 516 | 517 | /* register ppp device into rt_device frame */ 518 | result = rt_device_register(&ppp_device->parent, dev_name, RT_DEVICE_OFLAG_RDWR); 519 | if( result == RT_EOK) 520 | { 521 | _g_ppp_device = ppp_device; 522 | LOG_I("ppp_device(%s) register successfully.", PPP_DEVICE_NAME); 523 | } 524 | 525 | return result; 526 | } 527 | 528 | /** 529 | * attach data interface device into ppp device frame 530 | * 531 | * @param ppp_device the point of device driver structure, ppp_device structure 532 | * @param uart_name the name of uart name what you used 533 | * @param user_data private data 534 | * 535 | * @return RT_EOK execute successful 536 | * @return -RT_ERROR error 537 | */ 538 | int ppp_device_attach(struct ppp_device *ppp_device, const char *uart_name, void *user_data) 539 | { 540 | RT_ASSERT(ppp_device != RT_NULL); 541 | 542 | ppp_device->uart = rt_device_find(uart_name); 543 | if (!ppp_device->uart) 544 | { 545 | LOG_E("ppp_device_attach, cannot found %s", uart_name); 546 | return -RT_ERROR; 547 | } 548 | ppp_device->user_data = user_data; 549 | 550 | if (rt_device_open(&ppp_device->parent, 0) != RT_EOK) 551 | { 552 | LOG_E("ppp_device_attach failed. Can't open device(%d)"); 553 | return -RT_ERROR; 554 | } 555 | 556 | return RT_EOK; 557 | } 558 | 559 | /** 560 | * detach data interface device from ppp device frame 561 | * 562 | * @param ppp_device the point of device driver structure, ppp_device structure 563 | * 564 | * @return RT_EOK execute successful 565 | * @return -RT_ERROR error 566 | */ 567 | int ppp_device_detach(struct ppp_device *ppp_device) 568 | { 569 | RT_ASSERT(ppp_device != RT_NULL); 570 | 571 | if (rt_device_close(&ppp_device->parent) != RT_EOK) 572 | { 573 | LOG_E("ppp_device_detach failed. Can't open device."); 574 | return -RT_ERROR; 575 | } 576 | 577 | ppp_device->uart = RT_NULL; 578 | ppp_device->user_data = RT_NULL; 579 | 580 | return RT_EOK; 581 | } 582 | -------------------------------------------------------------------------------- /src/ppp_netif.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2019, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2019-08-15 xiangxistu the first version 9 | * 2019-09-19 xiaofan use lwip_netdev_ops instead of ppp_netdev_ops 10 | */ 11 | 12 | #include "lwip/opt.h" 13 | #include "ppp_netif.h" 14 | 15 | #ifdef RT_USING_NETDEV 16 | 17 | #include "lwip/ip.h" 18 | #include "lwip/netdb.h" 19 | #include 20 | 21 | extern const struct netdev_ops lwip_netdev_ops; 22 | 23 | /** 24 | * add ppp_netdev into netdev 25 | * 26 | * @param ppp_netif the piont of netif control block 27 | * 28 | * @return RT_EOK execute successful 29 | * @return -ERR_IF netif error, can't alloc memory for netdev 30 | */ 31 | rt_err_t ppp_netdev_add(struct netif *ppp_netif) 32 | { 33 | #define LWIP_NETIF_NAME_LEN 2 34 | #define PPP_NETIF_ADDR_LEN 6 35 | int result = 0; 36 | struct netdev *netdev = RT_NULL; 37 | char name[LWIP_NETIF_NAME_LEN + 1] = {0}; 38 | 39 | RT_ASSERT(ppp_netif); 40 | 41 | netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev)); 42 | if (netdev == RT_NULL) 43 | { 44 | return -ERR_IF; 45 | } 46 | 47 | #ifdef SAL_USING_LWIP 48 | extern int sal_lwip_netdev_set_pf_info(struct netdev *netdev); 49 | 50 | /* set the lwIP network interface device protocol family information */ 51 | sal_lwip_netdev_set_pf_info(netdev); 52 | #endif /* SAL_USING_LWIP */ 53 | 54 | rt_strncpy(name, ppp_netif->name, LWIP_NETIF_NAME_LEN); 55 | result = netdev_register(netdev, name, (void *)ppp_netif); 56 | 57 | /* OUI 00-04-A3 (hex): Microchip Technology, Inc. */ 58 | netdev->hwaddr[0] = 0x95; 59 | netdev->hwaddr[1] = 0x45; 60 | netdev->hwaddr[2] = 0x68; 61 | /* set MAC address, only for test */ 62 | netdev->hwaddr[3] = 0x39; 63 | netdev->hwaddr[4] = 0x68; 64 | netdev->hwaddr[5] = 0x52; 65 | 66 | /* Update netdev info after registered */ 67 | netdev->flags = ppp_netif->flags; 68 | netdev->mtu = ppp_netif->mtu; 69 | netdev->ops = &lwip_netdev_ops; 70 | netdev->hwaddr_len = PPP_NETIF_ADDR_LEN; 71 | netdev->ip_addr = ppp_netif->ip_addr; 72 | netdev->gw = ppp_netif->gw; 73 | netdev->netmask = ppp_netif->netmask; 74 | 75 | return result; 76 | } 77 | 78 | /** 79 | * refresh ppp_netif state, include DNS and netdev flag information 80 | * 81 | * @param ppp_netif the piont of netif control block 82 | * 83 | * @return RT_EOK execute successful 84 | */ 85 | rt_err_t ppp_netdev_refresh(struct netif *ppp_netif) 86 | { 87 | extern const ip_addr_t *dns_getserver(u8_t numdns); 88 | extern void dns_setserver(u8_t numdns, const ip_addr_t *dnsserver); 89 | 90 | char name[LWIP_NETIF_NAME_LEN + 1]; 91 | struct netdev *netdev = RT_NULL; 92 | 93 | RT_ASSERT(ppp_netif); 94 | 95 | rt_strncpy(name, ppp_netif->name, LWIP_NETIF_NAME_LEN); 96 | netdev = netdev_get_by_name(name); 97 | netdev->mtu = ppp_netif->mtu; 98 | 99 | /* sometime we can get second dns server but first dns server is empty, wo need do something to fix it */ 100 | if (!ip_addr_isany(dns_getserver(0))) 101 | { 102 | netdev_low_level_set_dns_server(netdev, 0, dns_getserver(0)); 103 | } 104 | else 105 | { 106 | #define DEF_DNS_SERVER "114.114.114.114" 107 | ip_addr_t dns_server; 108 | inet_aton(DEF_DNS_SERVER, &dns_server); 109 | dns_setserver(0, &dns_server); 110 | } 111 | 112 | netdev_low_level_set_dns_server(netdev, 1, dns_getserver(1)); 113 | 114 | return RT_EOK; 115 | } 116 | 117 | /** 118 | * delete netdev of ppp_netif create 119 | * 120 | * @param ppp_netif the piont of netif control block 121 | */ 122 | void ppp_netdev_del(struct netif *ppp_netif) 123 | { 124 | char name[LWIP_NETIF_NAME_LEN + 1]; 125 | struct netdev *netdev = RT_NULL; 126 | 127 | RT_ASSERT(ppp_netif); 128 | 129 | rt_strncpy(name, ppp_netif->name, LWIP_NETIF_NAME_LEN); 130 | netdev = netdev_get_by_name(name); 131 | if (netdev) 132 | { 133 | netdev_unregister(netdev); 134 | rt_free(netdev); 135 | } 136 | } 137 | 138 | #endif /* RT_USING_NETDEV */ 139 | --------------------------------------------------------------------------------