pppos_client_main.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* PPPoS Client Example with GSM (tested with Telit GL865-DUAL-V3)
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <string.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/event_groups.h"
  11. #include "esp_system.h"
  12. #include "esp_wifi.h"
  13. #include "esp_event_loop.h"
  14. #include "esp_log.h"
  15. #include "nvs_flash.h"
  16. #include "driver/uart.h"
  17. #include "netif/ppp/pppos.h"
  18. #include "lwip/err.h"
  19. #include "lwip/sockets.h"
  20. #include "lwip/sys.h"
  21. #include "lwip/netdb.h"
  22. #include "lwip/dns.h"
  23. #include "lwip/pppapi.h"
  24. /* The examples use simple GSM configuration that you can set via
  25. 'make menuconfig'.
  26. */
  27. #define BUF_SIZE (1024)
  28. const char *PPP_User = CONFIG_GSM_INTERNET_USER;
  29. const char *PPP_Pass = CONFIG_GSM_INTERNET_PASSWORD;
  30. const char *PPP_ApnATReq = "AT+CGDCONT=1,\"IP\",\"" \
  31. CONFIG_GSM_APN \
  32. "\"";
  33. /* Pins used for serial communication with GSM module */
  34. #define UART1_TX_PIN CONFIG_UART1_TX_PIN
  35. #define UART1_RX_PIN CONFIG_UART1_RX_PIN
  36. #define UART1_RTS_PIN CONFIG_UART1_RTS_PIN
  37. #define UART1_CTS_PIN CONFIG_UART1_CTS_PIN
  38. /* UART */
  39. int uart_num = UART_NUM_1;
  40. /* The PPP control block */
  41. ppp_pcb *ppp;
  42. /* The PPP IP interface */
  43. struct netif ppp_netif;
  44. static const char *TAG = "example";
  45. typedef struct {
  46. char *cmd;
  47. uint16_t cmdSize;
  48. char *cmdResponseOnOk;
  49. uint32_t timeoutMs;
  50. } GSM_Cmd;
  51. #define GSM_OK_Str "OK"
  52. GSM_Cmd GSM_MGR_InitCmds[] = {
  53. {
  54. .cmd = "AT\r",
  55. .cmdSize = sizeof("AT\r") - 1,
  56. .cmdResponseOnOk = GSM_OK_Str,
  57. .timeoutMs = 3000,
  58. },
  59. {
  60. .cmd = "ATE0\r",
  61. .cmdSize = sizeof("ATE0\r") - 1,
  62. .cmdResponseOnOk = GSM_OK_Str,
  63. .timeoutMs = 3000,
  64. },
  65. {
  66. .cmd = "AT+CPIN?\r",
  67. .cmdSize = sizeof("AT+CPIN?\r") - 1,
  68. .cmdResponseOnOk = "CPIN: READY",
  69. .timeoutMs = 3000,
  70. },
  71. {
  72. //AT+CGDCONT=1,"IP","apn"
  73. .cmd = "AT+CGDCONT=1,\"IP\",\"playmetric\"\r",
  74. .cmdSize = sizeof("AT+CGDCONT=1,\"IP\",\"playmetric\"\r") - 1,
  75. .cmdResponseOnOk = GSM_OK_Str,
  76. .timeoutMs = 3000,
  77. },
  78. {
  79. .cmd = "ATDT*99***1#\r",
  80. .cmdSize = sizeof("ATDT*99***1#\r") - 1,
  81. .cmdResponseOnOk = "CONNECT",
  82. .timeoutMs = 30000,
  83. }
  84. };
  85. #define GSM_MGR_InitCmdsSize (sizeof(GSM_MGR_InitCmds)/sizeof(GSM_Cmd))
  86. /* PPP status callback example */
  87. static void ppp_status_cb(ppp_pcb *pcb, int err_code, void *ctx)
  88. {
  89. struct netif *pppif = ppp_netif(pcb);
  90. LWIP_UNUSED_ARG(ctx);
  91. switch (err_code) {
  92. case PPPERR_NONE: {
  93. ESP_LOGI(TAG, "status_cb: Connected\n");
  94. #if PPP_IPV4_SUPPORT
  95. ESP_LOGI(TAG, " our_ipaddr = %s\n", ipaddr_ntoa(&pppif->ip_addr));
  96. ESP_LOGI(TAG, " his_ipaddr = %s\n", ipaddr_ntoa(&pppif->gw));
  97. ESP_LOGI(TAG, " netmask = %s\n", ipaddr_ntoa(&pppif->netmask));
  98. #endif /* PPP_IPV4_SUPPORT */
  99. #if PPP_IPV6_SUPPORT
  100. ESP_LOGI(TAG, " our6_ipaddr = %s\n", ip6addr_ntoa(netif_ip6_addr(pppif, 0)));
  101. #endif /* PPP_IPV6_SUPPORT */
  102. break;
  103. }
  104. case PPPERR_PARAM: {
  105. ESP_LOGE(TAG, "status_cb: Invalid parameter\n");
  106. break;
  107. }
  108. case PPPERR_OPEN: {
  109. ESP_LOGE(TAG, "status_cb: Unable to open PPP session\n");
  110. break;
  111. }
  112. case PPPERR_DEVICE: {
  113. ESP_LOGE(TAG, "status_cb: Invalid I/O device for PPP\n");
  114. break;
  115. }
  116. case PPPERR_ALLOC: {
  117. ESP_LOGE(TAG, "status_cb: Unable to allocate resources\n");
  118. break;
  119. }
  120. case PPPERR_USER: {
  121. ESP_LOGE(TAG, "status_cb: User interrupt\n");
  122. break;
  123. }
  124. case PPPERR_CONNECT: {
  125. ESP_LOGE(TAG, "status_cb: Connection lost\n");
  126. break;
  127. }
  128. case PPPERR_AUTHFAIL: {
  129. ESP_LOGE(TAG, "status_cb: Failed authentication challenge\n");
  130. break;
  131. }
  132. case PPPERR_PROTOCOL: {
  133. ESP_LOGE(TAG, "status_cb: Failed to meet protocol\n");
  134. break;
  135. }
  136. case PPPERR_PEERDEAD: {
  137. ESP_LOGE(TAG, "status_cb: Connection timeout\n");
  138. break;
  139. }
  140. case PPPERR_IDLETIMEOUT: {
  141. ESP_LOGE(TAG, "status_cb: Idle Timeout\n");
  142. break;
  143. }
  144. case PPPERR_CONNECTTIME: {
  145. ESP_LOGE(TAG, "status_cb: Max connect time reached\n");
  146. break;
  147. }
  148. case PPPERR_LOOPBACK: {
  149. ESP_LOGE(TAG, "status_cb: Loopback detected\n");
  150. break;
  151. }
  152. default: {
  153. ESP_LOGE(TAG, "status_cb: Unknown error code %d\n", err_code);
  154. break;
  155. }
  156. }
  157. /*
  158. * This should be in the switch case, this is put outside of the switch
  159. * case for example readability.
  160. */
  161. if (err_code == PPPERR_NONE) {
  162. return;
  163. }
  164. /* ppp_close() was previously called, don't reconnect */
  165. if (err_code == PPPERR_USER) {
  166. /* ppp_free(); -- can be called here */
  167. return;
  168. }
  169. /*
  170. * Try to reconnect in 30 seconds, if you need a modem chatscript you have
  171. * to do a much better signaling here ;-)
  172. */
  173. //ppp_connect(pcb, 30);
  174. /* OR ppp_listen(pcb); */
  175. }
  176. static u32_t ppp_output_callback(ppp_pcb *pcb, u8_t *data, u32_t len, void *ctx)
  177. {
  178. ESP_LOGI(TAG, "PPP tx len %d", len);
  179. return uart_write_bytes(uart_num, (const char *)data, len);
  180. }
  181. static void pppos_client_task()
  182. {
  183. char *data = (char *) malloc(BUF_SIZE);
  184. uart_config_t uart_config = {
  185. .baud_rate = 115200,
  186. .data_bits = UART_DATA_8_BITS,
  187. .parity = UART_PARITY_DISABLE,
  188. .stop_bits = UART_STOP_BITS_1,
  189. .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS
  190. };
  191. //Configure UART1 parameters
  192. uart_param_config(uart_num, &uart_config);
  193. // Configure UART1 pins (as set in example's menuconfig)
  194. ESP_LOGI(TAG, "Configuring UART1 GPIOs: TX:%d RX:%d RTS:%d CTS: %d",
  195. UART1_TX_PIN, UART1_RX_PIN, UART1_RTS_PIN, UART1_CTS_PIN);
  196. uart_set_pin(uart_num, UART1_TX_PIN, UART1_RX_PIN, UART1_RTS_PIN, UART1_CTS_PIN);
  197. uart_driver_install(uart_num, BUF_SIZE * 2, BUF_SIZE * 2, 0, NULL, 0);
  198. while (1) {
  199. //init gsm
  200. int gsmCmdIter = 0;
  201. while (1) {
  202. ESP_LOGI(TAG, "%s", GSM_MGR_InitCmds[gsmCmdIter].cmd);
  203. uart_write_bytes(uart_num, (const char *)GSM_MGR_InitCmds[gsmCmdIter].cmd,
  204. GSM_MGR_InitCmds[gsmCmdIter].cmdSize);
  205. int timeoutCnt = 0;
  206. while (1) {
  207. memset(data, 0, BUF_SIZE);
  208. int len = uart_read_bytes(uart_num, (uint8_t *)data, BUF_SIZE, 500 / portTICK_RATE_MS);
  209. if (len > 0) {
  210. ESP_LOGI(TAG, "%s", data);
  211. }
  212. timeoutCnt += 500;
  213. if (strstr(data, GSM_MGR_InitCmds[gsmCmdIter].cmdResponseOnOk) != NULL) {
  214. break;
  215. }
  216. if (timeoutCnt > GSM_MGR_InitCmds[gsmCmdIter].timeoutMs) {
  217. ESP_LOGE(TAG, "Gsm Init Error");
  218. return;
  219. }
  220. }
  221. gsmCmdIter++;
  222. if (gsmCmdIter >= GSM_MGR_InitCmdsSize) {
  223. break;
  224. }
  225. }
  226. ESP_LOGI(TAG, "Gsm init end");
  227. ppp = pppapi_pppos_create(&ppp_netif,
  228. ppp_output_callback, ppp_status_cb, NULL);
  229. ESP_LOGI(TAG, "After pppapi_pppos_create");
  230. if (ppp == NULL) {
  231. ESP_LOGE(TAG, "Error init pppos");
  232. return;
  233. }
  234. pppapi_set_default(ppp);
  235. ESP_LOGI(TAG, "After pppapi_set_default");
  236. pppapi_set_auth(ppp, PPPAUTHTYPE_PAP, PPP_User, PPP_Pass);
  237. ESP_LOGI(TAG, "After pppapi_set_auth");
  238. pppapi_connect(ppp, 0);
  239. ESP_LOGI(TAG, "After pppapi_connect");
  240. while (1) {
  241. memset(data, 0, BUF_SIZE);
  242. int len = uart_read_bytes(uart_num, (uint8_t *)data, BUF_SIZE, 10 / portTICK_RATE_MS);
  243. if (len > 0) {
  244. ESP_LOGI(TAG, "PPP rx len %d", len);
  245. pppos_input_tcpip(ppp, (u8_t *)data, len);
  246. }
  247. }
  248. }
  249. }
  250. void app_main()
  251. {
  252. tcpip_adapter_init();
  253. xTaskCreate(&pppos_client_task, "pppos_client_task", 2048, NULL, 5, NULL);
  254. }