Przeglądaj źródła

Merge pull request #9 from xiangxistu/master

【增加】对AIR720的适配
朱天龙 (Armink) 6 lat temu
rodzic
commit
663f11f8f9

+ 4 - 3
README.md

@@ -174,10 +174,11 @@ msh />ping www.baidu.com
 
 ## 4. 注意事项
 
-* 一般的SIM卡因为只能从运营商网络获取内网地址,所以不能实现服务器相关功能
-* 目前只支持一个设备通过 PPP 连接网络
+* 一般的SIM卡因为只能从运营商网络获取内网地址,所以不能实现服务器相关功能
+* 目前只支持一个设备通过 PPP 连接网络
 * 建议不要打开 RT_DEVICE_FLAG_DMA_TX
-* 如果网络环境不好,建议关闭 Enable lin status detect feature 选项,或调整成大一点的时间。
+* 如果网络环境不好,建议关闭 Enable lin status detect feature 选项,或调整成大一点的时间
+* RT_LWIP_TCPTHREAD_STACKSIZE 需要配置为不小于 2048,及时在 network 组件中修改
 
 ## 5. 联系方式
 

+ 37 - 11
class/air720/ppp_device_air720.c

@@ -10,6 +10,7 @@
 
 #include <ppp_device_air720.h>
 #include <ppp_chat.h>
+#include <rtdevice.h>
 
 #define DBG_TAG    "ppp.air720"
 
@@ -18,21 +19,44 @@
 #else
 #define DBG_LVL   DBG_INFO
 #endif
-
 #include <rtdbg.h>
 
-static const struct modem_chat_data mcd[] =
+#define AIR720_POWER_ON  PIN_HIGH
+#define AIR720_POWER_OFF PIN_LOW
+#ifndef AIR720_POWER_PIN
+#define AIR720_POWER_PIN -1
+#endif
+
+static const struct modem_chat_data rst_mcd[] =
 {
     {"+++",          MODEM_CHAT_RESP_NOT_NEED,        30, 1, RT_TRUE},
     {"ATH",          MODEM_CHAT_RESP_OK,              30, 1, RT_FALSE},
+};
+
+static const struct modem_chat_data mcd[] =
+{
     {"AT",           MODEM_CHAT_RESP_OK,              10, 1, RT_FALSE},
     {"ATE0",         MODEM_CHAT_RESP_OK,              1,  1, RT_FALSE},
     {PPP_APN_CMD,    MODEM_CHAT_RESP_OK,              1,  5, RT_FALSE},
-    {PPP_DAIL_CMD,   MODEM_CHAT_RESP_CONNECT,         2, 30, RT_FALSE},
+    {PPP_DAIL_CMD,   MODEM_CHAT_RESP_CONNECT,         1, 30, RT_FALSE},
 };
 
 static rt_err_t ppp_air720_prepare(struct ppp_device *device)
 {
+    struct ppp_air720 *air720 = (struct ppp_air720*)device;
+    if (air720->power_pin >= 0)
+    {
+        rt_pin_write(air720->power_pin, AIR720_POWER_OFF);
+        rt_thread_mdelay(AIR720_WARTING_TIME_BASE);
+        rt_pin_write(air720->power_pin, AIR720_POWER_ON);
+    }
+    else
+    {
+        rt_err_t err;
+        err = modem_chat(device->uart, rst_mcd, sizeof(rst_mcd) / sizeof(rst_mcd[0]));
+        if (err)
+            return err;
+    }
     return modem_chat(device->uart, mcd, sizeof(mcd) / sizeof(mcd[0]));
 }
 
@@ -42,16 +66,11 @@ static struct ppp_device_ops air720_ops =
     .prepare = ppp_air720_prepare,
 };
 
-/*
+/**
  * register air720 into ppp_device
  *
- * @parameter   RT_NULL
- *
- *
- *
- *
- * @return  ppp_device function piont
- *
+ * @return  =0:   ppp_device register successfully
+ *          <0:   ppp_device register failed
  */
 int ppp_air720_register(void)
 {
@@ -65,6 +84,13 @@ int ppp_air720_register(void)
         return -RT_ENOMEM;
     }
 
+    air720->power_pin = AIR720_POWER_PIN;
+    if (air720->power_pin >= 0)
+    {
+        rt_pin_mode(air720->power_pin, PIN_MODE_OUTPUT);
+        rt_pin_write(air720->power_pin, AIR720_POWER_OFF);
+    }
+
     ppp_device = &(air720->device);
     ppp_device->ops = &air720_ops;
 

+ 3 - 0
class/air720/ppp_device_air720.h

@@ -13,11 +13,14 @@
 
 #include <ppp_device.h>
 
+#define AIR720_WARTING_TIME_BASE 500
+
 /* ppp_device base from ppp_device */
 struct ppp_air720
 {
     struct ppp_device  device;          /* ppp_device struct in ppp_air720 */
     enum ppp_trans_type type;           /* the type is used to establish a ppp connection */
+    rt_base_t power_pin;                /* power pin, if device need hardware reset */
 };
 
 extern int ppp_air720_register(void);

+ 6 - 10
class/m6312/ppp_device_m6312.c

@@ -19,6 +19,7 @@
 #else
 #define DBG_LVL   DBG_INFO
 #endif
+#include <rtdbg.h>
 
 #define M6312_POWER_ON  PIN_HIGH
 #define M6312_POWER_OFF PIN_LOW
@@ -26,7 +27,6 @@
 #define M6312_POWER_PIN -1
 #endif
 
-#include <rtdbg.h>
 
 static const struct modem_chat_data rst_mcd[] =
 {
@@ -40,7 +40,7 @@ static const struct modem_chat_data mcd[] =
     {"AT",           MODEM_CHAT_RESP_OK,        10, 1, RT_FALSE},
     {"ATE0",         MODEM_CHAT_RESP_OK,        1,  1, RT_FALSE},
     {PPP_APN_CMD,    MODEM_CHAT_RESP_OK,        1,  5, RT_FALSE},
-    {PPP_DAIL_CMD,   MODEM_CHAT_RESP_CONNECT,   2, 30, RT_FALSE},
+    {PPP_DAIL_CMD,   MODEM_CHAT_RESP_CONNECT,   1, 30, RT_FALSE},
 };
 
 static rt_err_t ppp_m6312_prepare(struct ppp_device *device)
@@ -49,7 +49,7 @@ static rt_err_t ppp_m6312_prepare(struct ppp_device *device)
     if (m6312->power_pin >= 0)
     {
         rt_pin_write(m6312->power_pin, M6312_POWER_OFF);
-        rt_thread_mdelay(500);
+        rt_thread_mdelay(M6312_WARTING_TIME_BASE);
         rt_pin_write(m6312->power_pin, M6312_POWER_ON);
     }
     else
@@ -68,15 +68,11 @@ static struct ppp_device_ops m6312_ops =
     .prepare = ppp_m6312_prepare,
 };
 
-/*
+/**
  * register m6312 into ppp_device
  *
- * @param
- *
- *
- *
- * @return  ppp_device function piont
- *
+ * @return  =0:   ppp_device register successfully
+ *          <0:   ppp_device register failed
  */
 int ppp_m6312_register(void)
 {

+ 3 - 1
class/m6312/ppp_device_m6312.h

@@ -13,12 +13,14 @@
 
 #include <ppp_device.h>
 
+#define M6312_WARTING_TIME_BASE 500
+
 /* ppp_device base from ppp_device */
 struct ppp_m6312
 {
     struct ppp_device  device;          /* ppp_device struct in ppp_m6312 */
     enum ppp_trans_type type;           /* the type is used to establish a ppp connection */
-    rt_base_t power_pin;
+    rt_base_t power_pin;                /* power pin, if device need hardware reset */
 };
 
 extern int ppp_m6312_register(void);

+ 37 - 9
class/sim800/ppp_device_sim800.c

@@ -10,6 +10,7 @@
 
 #include <ppp_device_sim800.h>
 #include <ppp_chat.h>
+#include <rtdevice.h>
 
 #define DBG_TAG    "ppp.sim800"
 
@@ -18,13 +19,22 @@
 #else
 #define DBG_LVL   DBG_INFO
 #endif
-
 #include <rtdbg.h>
 
+#define SIM800_POWER_ON  PIN_HIGH
+#define SIM800_POWER_OFF PIN_LOW
+#ifndef SIM800_POWER_PIN
+#define SIM800_POWER_PIN -1
+#endif
+
+static const struct modem_chat_data rst_mcd[] =
+{
+    {"+++",          MODEM_CHAT_RESP_NOT_NEED,        30, 1, RT_TRUE},
+    {"ATH",          MODEM_CHAT_RESP_OK,              30, 1, RT_FALSE},
+};
+
 static const struct modem_chat_data mcd[] =
 {
-    {"+++",          MODEM_CHAT_RESP_NOT_NEED,        1,  2, RT_TRUE},
-    {"ATH",          MODEM_CHAT_RESP_OK,              30, 3, RT_FALSE},
     {"AT",           MODEM_CHAT_RESP_OK,              10, 1, RT_FALSE},
     {"ATE0",         MODEM_CHAT_RESP_OK,              1,  1, RT_FALSE},
     {PPP_APN_CMD,    MODEM_CHAT_RESP_OK,              1,  5, RT_FALSE},
@@ -33,6 +43,20 @@ static const struct modem_chat_data mcd[] =
 
 static rt_err_t ppp_sim800_prepare(struct ppp_device *device)
 {
+    struct ppp_sim800 *sim800 = (struct ppp_sim800*)device;
+    if (sim800->power_pin >= 0)
+    {
+        rt_pin_write(sim800->power_pin, SIM800_POWER_OFF);
+        rt_thread_mdelay(SIM800_WARTING_TIME_BASE);
+        rt_pin_write(sim800->power_pin, SIM800_POWER_ON);
+    }
+    else
+    {
+        rt_err_t err;
+        err = modem_chat(device->uart, rst_mcd, sizeof(rst_mcd) / sizeof(rst_mcd[0]));
+        if (err)
+            return err;
+    }
     return modem_chat(device->uart, mcd, sizeof(mcd) / sizeof(mcd[0]));
 }
 
@@ -42,14 +66,11 @@ static struct ppp_device_ops sim800_ops =
     .prepare = ppp_sim800_prepare,
 };
 
-/*
+/**
  * register sim800 into ppp_device
  *
- * @param
- *
- *
- * @return  ppp_device function piont
- *
+ * @return  =0:   ppp_device register successfully
+ *          <0:   ppp_device register failed
  */
 int ppp_sim800_register(void)
 {
@@ -63,6 +84,13 @@ int ppp_sim800_register(void)
         return -RT_ENOMEM;
     }
 
+    sim800->power_pin = SIM800_POWER_PIN;
+    if (sim800->power_pin >= 0)
+    {
+        rt_pin_mode(sim800->power_pin, PIN_MODE_OUTPUT);
+        rt_pin_write(sim800->power_pin, SIM800_POWER_OFF);
+    }
+
     ppp_device = &(sim800->device);
     ppp_device->ops = &sim800_ops;
 

+ 3 - 0
class/sim800/ppp_device_sim800.h

@@ -13,11 +13,14 @@
 
 #include <ppp_device.h>
 
+#define SIM800_WARTING_TIME_BASE 500
+
 /* ppp_device base from ppp_device */
 struct ppp_sim800
 {
     struct ppp_device  device;          /* ppp_device struct in ppp_sim800 */
     enum ppp_trans_type type;           /* the type is used to establish a ppp connection */
+    rt_base_t power_pin;                /* power pin, if device need hardware reset */
 };
 
 extern int ppp_sim800_register(void);

+ 4 - 0
inc/ppp_device.h

@@ -41,6 +41,10 @@
 #define PPP_FRAME_MAX       1550
 #define PPP_DROP_BUF        PPP_FRAME_MAX
 
+
+#define PPP_DEVICE_SW_VERSION           "1.0.0"
+#define PPP_DEVICE_SW_VERSION_NUM       0x10000
+
 enum ppp_trans_type
 {
     PPP_TRANS_CHAT,

+ 1 - 0
inc/ppp_netif.h

@@ -27,6 +27,7 @@
 
 /* proviode a public interface to register netdev */
 rt_err_t ppp_netdev_add(struct netif *ppp_netif);
+rt_err_t ppp_netdev_refresh(struct netif *ppp_netif);
 void ppp_netdev_del(struct netif *ppp_netif);
 extern struct netdev *netdev_get_by_name(const char *name);
 extern int netdev_unregister(struct netdev *netdev);

+ 45 - 44
src/ppp_chat.c

@@ -21,13 +21,15 @@
 
 #define CHAT_READ_BUF_MAX 16
 
-// In order to match response, we need a string search algorithm
-// KMP and AC algorithm both are good choice, But we need the code
-// is simple, readable and use lower RAM/ROM.
-// So We use a simplified search algorithm, this alg is like the KMP.
-// Specifically we assume the failure vecotor is [-1, 0, 0, 0, ...]
-// This assuming is not work for all pattern string. Fortunately,
-// it's work  for this scene.
+/**
+* In order to match response, we need a string search algorithm
+* KMP and AC algorithm both are good choice, But we need the code
+* is simple, readable and use lower RAM/ROM.
+* So We use a simplified search algorithm, this alg is like the KMP.
+* Specifically we assume the failure vecotor is [-1, 0, 0, 0, ...]
+* This assuming is not work for all pattern string. Fortunately,
+* it's work  for this scene.
+*/
 
 #define DEFINE_MODEM_RESP_STRDATA_TABLE(id, str) [id] = str
 #define DEFINE_MODEM_RESP_STRLEN_TABLE(id, str)  [id] = (sizeof(str)-1)
@@ -74,14 +76,13 @@ static rt_bool_t resp_matched(rt_uint8_t resp_id, rt_uint8_t state)
     return state == resp_strlen[resp_id];
 }
 
-/*
- * chat_rx_ind , callback function if serial recieve data
- *
- * @param rt_device_t                       device
- *        rt_size_t                         size
+/**
+ * chat_rx_ind, callback function if serial recieve data
  *
- * @return  0   :   successful
+ * @param device the point of device driver structure, uart structure
+ * @param size  the indication callback function need this parameter
  *
+ * @return RT_EOK
  */
 static rt_err_t chat_rx_ind(rt_device_t device, rt_size_t size)
 {
@@ -89,14 +90,16 @@ static rt_err_t chat_rx_ind(rt_device_t device, rt_size_t size)
     return RT_EOK;
 }
 
-/*
- * chat_read_until , waitting for recieve data from serial
+/**
+ * chat_read_until, waitting for recieve data from serial
  *
- * @param struct rt_serial_device           *serial
- *        const struct modem_chat_data      *data
+ * @param serial    the point of device driver structure, uart structure
+ * @param buffer    the buffer is waitting for recieve uart data from ppp
+ * @param size      the max length of CHAT_READ_BUF_MAX
+ * @param stop      the max of tick time
  *
- * @return  0   :   timeout, can't recieve any data
- *          size:   the size of recieve data
+ * @return  >=0:   the length of read data
+ *          <0 :   rt_device_read failed
  */
 static rt_size_t chat_read_until(rt_device_t serial, void *buffer, rt_size_t size, rt_tick_t stop)
 {
@@ -116,13 +119,10 @@ static rt_size_t chat_read_until(rt_device_t serial, void *buffer, rt_size_t siz
     return rt_device_read(serial, 0, buffer, size);
 }
 
-/*
+/**
  * modem_flush_rx , clear data what is in the rx buffer
  *
- * @param struct rt_serial_device           *serial
- *
- * @return  RT_NULL: none
- *
+ * @param serial    the point of device driver structure, uart structure
  */
 static void modem_flush_rx(rt_device_t serial)
 {
@@ -131,14 +131,14 @@ static void modem_flush_rx(rt_device_t serial)
     while (rt_device_read(serial, 0, rdbuf, CHAT_READ_BUF_MAX));
 }
 
-/*
+/**
  * modem_chat_once , send an order to control modem
  *
- * @param struct rt_serial_device           *serial
- *        const struct modem_chat_data      *data
- *
- * @return  0: execute successful
+ * @param serial    the point of device driver structure, uart structure
+ * @param data      the AT command
  *
+ * @return  =0:   modem_chat_once successful
+ *          <0:   modem_chat_once failed
  */
 static rt_err_t modem_chat_once(rt_device_t serial, const struct modem_chat_data *data)
 {
@@ -184,15 +184,15 @@ static rt_err_t modem_chat_once(rt_device_t serial, const struct modem_chat_data
     return -RT_ETIMEOUT;
 }
 
-/*
- * modem_chat_internal , init modem and turn modem into ppp type
+/**
+ * modem_chat_internal , init modem or turn modem into ppp type
  *
- * @param struct rt_serial_device           *serial
- *        const struct modem_chat_data      *data
- *        rt_size_t                         len
- *
- * @return  0: execute successful
+ * @param serial    the point of device driver structure, uart structure
+ * @param data      the AT command, it is the address of chat strcuture, a collection of AT command
+ * @param len       the length of this collection of AT command
  *
+ * @return  =0:   modem_chat_internal successful
+ *          <0:   modem_chat_internal failed
  */
 static rt_err_t modem_chat_internal(rt_device_t serial, const struct modem_chat_data *data, rt_size_t len)
 {
@@ -220,15 +220,15 @@ static rt_err_t modem_chat_internal(rt_device_t serial, const struct modem_chat_
     return err;
 }
 
-/*
- * modem_chat , a function for ppp dailing to network
- *
- * @param struct ppp_device                 *device
- *        const struct modem_chat_data      *data
- *        rt_size_t                         len
+/**
+ * modem_chat , a function for ppp dailing to network, it will set rx_indicate
  *
- * @return  0: execute successful
+ * @param device    the point of device driver structure, uart structure
+ * @param data      the AT command, it is the address of chat strcuture, a collection of AT command
+ * @param len       the length of this collection of AT command
  *
+ * @return  =0:   modem_chat successful
+ *          <0:   modem_chat failed
  */
 rt_err_t modem_chat(rt_device_t serial, const struct modem_chat_data *data, rt_size_t len)
 {
@@ -240,7 +240,7 @@ rt_err_t modem_chat(rt_device_t serial, const struct modem_chat_data *data, rt_s
     old_rx_ind = serial->rx_indicate;
     rt_device_set_rx_indicate(serial, chat_rx_ind);
 
-    LOG_I("(%s) has control by modem_chat.", serial->parent.name);
+    LOG_D("(%s) has control by modem_chat.", serial->parent.name);
     err = modem_chat_internal(serial, data, len);
     if (err != RT_EOK)
     {
@@ -248,5 +248,6 @@ rt_err_t modem_chat(rt_device_t serial, const struct modem_chat_data *data, rt_s
     }
 
     serial->rx_indicate = old_rx_ind;
+    LOG_D("(%s) has control by ppp_device.", serial->parent.name);
     return err;
 }

+ 130 - 93
src/ppp_device.c

@@ -22,7 +22,8 @@
 
 #include <rtdbg.h>
 
-enum {
+enum
+{
     PPP_STATE_PREPARE,
     PPP_STATE_WAIT_HEAD,
     PPP_STATE_RECV_DATA,
@@ -44,24 +45,29 @@ enum {
 #error "tcpip stack is too small, should greater than 2048."
 #endif
 
-
 static struct ppp_device *_g_ppp_device = RT_NULL;
 
+/**
+ * dump ppp data according to hex format,you can see data what you recieve , send, and ppp_device dorp out
+ *
+ * @param data      the data what you want to dump out
+ * @param len       the length of those data
+ */
+#ifdef PPP_DEVICE_DEBUG
 static void ppp_debug_hexdump(const void *data, size_t len)
 {
-#ifdef PPP_DEVICE_DEBUG
     const size_t maxlen = 16;
     rt_uint32_t offset = 0;
-    size_t curlen;
-    char line[maxlen*4+3];
-    char *p;
+    size_t curlen = 0, i = 0;
+    char line[maxlen * 4 + 3] = {0};
+    char *p = RT_NULL;
     const unsigned char *src = data;
 
     while (len > 0)
     {
         curlen = len < maxlen ? len : maxlen;
         p = line;
-        for (size_t i = 0; i < curlen; i++)
+        for (i = 0; i < curlen; i++)
         {
             rt_sprintf(p, "%02x ", (unsigned char)src[i]);
             p += 3;
@@ -70,7 +76,7 @@ static void ppp_debug_hexdump(const void *data, size_t len)
         p += (maxlen - curlen) * 3;
         *p++ = '|';
         *p++ = ' ';
-        for (size_t i = 0; i < curlen; i++)
+        for (i = 0; i < curlen; i++)
         {
             *p++ = (0x20 < src[i] && src[i] < 0x7e) ? src[i] : '.';
         }
@@ -80,39 +86,37 @@ static void ppp_debug_hexdump(const void *data, size_t len)
         src += curlen;
         offset += curlen;
     }
-#endif
 }
+#endif
 
-/*
- * Receive callback function , release rx_notice when uart acquire data
+/**
+ * Receive callback function , send PPP_EVENT_RX_NOTIFY event when uart acquire data
  *
- * @param rt_device_t
- *        rt_size_t
- *
- * @return  0: execute successful
+ * @param dev       the point of device driver structure, uart structure
+ * @param size      the indication callback function need this parameter
  *
+ * @return  RT_EOK
  */
 static rt_err_t ppp_device_rx_ind(rt_device_t dev, rt_size_t size)
 {
     RT_ASSERT(dev != RT_NULL);
     struct ppp_device *ppp_dev = _g_ppp_device;
 
-    /* when recieve data from uart , release semphone to wake up recieve thread */
+    /* when recieve data from uart , send event to wake up recieve thread */
     rt_event_send(&ppp_dev->event, PPP_EVENT_RX_NOTIFY);
 
     return RT_EOK;
 }
 
-/*
+/**
  *  using ppp_data_send send data to lwIP procotol stack     PPPoS serial output callback
  *
- * @param ppp_pcb   *pcb                 pcb PPP control block
- *        uint8_t   *data                data Buffer to write to serial port
- *        uint32_t  len                  len Length of the data buffer
- *        void      *ppp_device          ctx Context of callback , ppp_device
- *
- * @return  0: creat response fail
+ * @param pcb           pcb PPP control block
+ * @param data          data Buffer to write to serial port
+ * @param len           the Length of the data buffer
+ * @param ppp_device    ctx Context of callback , ppp_device
  *
+ * @return  the point of rt_device_write fucntion or RT_NULL
  */
 static uint32_t ppp_data_send(ppp_pcb *pcb, uint8_t *data, uint32_t len, void *ppp_device)
 {
@@ -132,15 +136,12 @@ static uint32_t ppp_data_send(ppp_pcb *pcb, uint8_t *data, uint32_t len, void *p
     return rt_device_write(device->uart, 0, data, len);
 }
 
-/*
+/**
  * ppp_status_changed callback function
  *
- * @param   ppp_pcb *pcb            protocol caontrol block
- *          int     err_code
- *          void    *ctx            reserver
- *
- * @return  0: creat response fail
- *
+ * @param pcb           protocol caontrol block
+ * @param err_code      the result of ppp conncet
+ * @param ctx           ctx Context of callback , ppp_device
  */
 static void ppp_status_changed(ppp_pcb *pcb, int err_code, void *ctx)
 {
@@ -148,9 +149,10 @@ static void ppp_status_changed(ppp_pcb *pcb, int err_code, void *ctx)
     struct netif *pppif = ppp_netif(pcb);
     switch (err_code)
     {
-    case PPPERR_NONE:                /* Connected */
+    case PPPERR_NONE: /* Connected */
         pppdev->pppif.mtu = pppif->mtu;
-        LOG_I("ppp connect successful.");
+        ppp_netdev_refresh(&pppdev->pppif);
+        LOG_I("ppp_device connect successfully.");
         break;
     case PPPERR_PARAM:
         LOG_E("Invalid parameter.");
@@ -167,7 +169,7 @@ static void ppp_status_changed(ppp_pcb *pcb, int err_code, void *ctx)
     case PPPERR_USER:
         LOG_D("User interrupt");
         break;
-    case PPPERR_CONNECT:            /* Connection lost */
+    case PPPERR_CONNECT: /* Connection lost */
         LOG_E("ppp connect lost.");
         break;
     case PPPERR_AUTHFAIL:
@@ -196,6 +198,11 @@ static void ppp_status_changed(ppp_pcb *pcb, int err_code, void *ctx)
         rt_event_send(&pppdev->event, PPP_EVENT_LOST);
 }
 
+/**
+ * prepare for starting recieve ppp frame, clear recieve buff and set ppp device state
+ *
+ * @param device  the point of device driver structure, ppp_device structure
+ */
 static inline void ppp_start_receive_frame(struct ppp_device *device)
 {
     device->rxpos = 0;
@@ -203,6 +210,11 @@ static inline void ppp_start_receive_frame(struct ppp_device *device)
 }
 
 #ifdef PPP_DEVICE_DEBUG_DROP
+/**
+ * ppp_show_dropbuf, printf the data whom ppp devcie drop out, and increase drop data conut
+ *
+ * @param device  the point of device driver structure, ppp_device structure
+ */
 static inline void ppp_show_dropbuf(struct ppp_device *device)
 {
     if (!device->droppos)
@@ -213,6 +225,11 @@ static inline void ppp_show_dropbuf(struct ppp_device *device)
     device->droppos = 0;
 }
 
+/**
+ * ppp_show_rxbuf_as_drop, printf the data whom ppp devcie drop out, and increase drop data conut
+ *
+ * @param device  the point of device driver structure, ppp_device structure
+ */
 static inline void ppp_show_rxbuf_as_drop(struct ppp_device *device)
 {
     if (!device->rxpos)
@@ -223,13 +240,19 @@ static inline void ppp_show_rxbuf_as_drop(struct ppp_device *device)
     device->rxpos = 0;
 }
 
+/**
+ * ppp_rxbuf_drop, printf the data whom ppp devcie drop out
+ *
+ * @param device  the point of device driver structure, ppp_device structure
+ */
 static inline void ppp_rxbuf_drop(struct ppp_device *device)
 {
-    // if we have no enough drop-buffer, we should display and clear drop-buffer
+    /* if we have no enough drop-buffer, we should display and clear drop-buffer */
     if (PPP_DROP_BUF - device->droppos < device->rxpos)
     {
         ppp_show_dropbuf(device);
-        // if our drop-buffer size less than or equal current valid size of rx-buffer, we should display and clear rx-buffer
+        /* if our drop-buffer size less than or equal current valid size of rx-buffer,
+            we should display and clear rx-buffer */
         if (PPP_DROP_BUF <= device->rxpos)
         {
             ppp_show_rxbuf_as_drop(device);
@@ -250,12 +273,16 @@ static inline void ppp_rxbuf_drop(struct ppp_device *device)
 
     ppp_start_receive_frame(device);
 }
-
 #else
 static inline void ppp_show_dropbuf(struct ppp_device *device) {}
 #define ppp_rxbuf_drop(device) ppp_start_receive_frame(device)
-#endif // !PPP_DEVICE_DEBUG_DROP
+#endif /* PPP_DEVICE_DEBUG_DROP */
 
+/**
+ * ppp_processdata_enter, prepare to recieve data
+ *
+ * @param device  the point of device driver structure, ppp_device structure
+ */
 static inline void ppp_processdata_enter(struct ppp_device *device)
 {
     ppp_start_receive_frame(device);
@@ -264,18 +291,36 @@ static inline void ppp_processdata_enter(struct ppp_device *device)
 #endif
 }
 
+/**
+ * ppp_processdata_leave, throw ppp device data when ppp connection is closed
+ *
+ * @param device  the point of device driver structure, ppp_device structure
+ */
 static inline void ppp_processdata_leave(struct ppp_device *device)
 {
     ppp_rxbuf_drop(device);
     ppp_show_dropbuf(device);
 }
 
+/**
+ * ppp_savebyte, save this data into ppp device buff
+ *
+ * @param   device  the point of device driver structure, ppp_device structure
+ * @param   dat     the character of recieve data
+ */
 static inline void ppp_savebyte(struct ppp_device *device, rt_uint8_t dat)
 {
     RT_ASSERT(device->rxpos < sizeof(device->rxbuf));
     device->rxbuf[device->rxpos++] = dat;
 }
 
+/**
+ * ppp_recv_processdata, save data from uart, recieve complete ppp frame data
+ *
+ * @param   device  the point of device driver structure, ppp_device structure
+ * @param   buf     the address of recieve data from uart
+ * @param   len     the length of recieve data
+ */
 static void ppp_recv_processdata(struct ppp_device *device, const rt_uint8_t *buf, rt_size_t len)
 {
     rt_uint8_t dat;
@@ -289,10 +334,10 @@ process_dat:
         {
         case PPP_STATE_WAIT_HEAD:
             ppp_savebyte(device, dat);
-            if (dat == PPP_DATA_BEGIN_END)
+            if (dat == PPP_DATA_BEGIN_END)              /* if recieve 0x7e */
             {
                 ppp_show_dropbuf(device);
-                device->state = PPP_STATE_RECV_DATA;
+                device->state = PPP_STATE_RECV_DATA;    /* begin recieve the second data */
             }
             else
             {
@@ -300,15 +345,15 @@ process_dat:
             }
             break;
         case PPP_STATE_RECV_DATA:
-            if (dat == PPP_DATA_BEGIN_END && device->rxpos == 1)
+            if (dat == PPP_DATA_BEGIN_END && device->rxpos == 1)    /* if we recieve 0x7e when this data is the second data */
             {
                 LOG_D("found continuous 0x7e");
                 // start receive a new frame
-                ppp_rxbuf_drop(device);
+                ppp_rxbuf_drop(device);                 /* throw this data, because 0x7e is the begin of ppp frame, the second data shouldn't been 0x7e */
                 goto process_dat;
             }
             ppp_savebyte(device, dat);
-            if (dat == PPP_DATA_BEGIN_END)
+            if (dat == PPP_DATA_BEGIN_END)      /* the end of ppp frame */
             {
 #ifdef PPP_DEVICE_DEBUG_RX
                 LOG_D("RX:");
@@ -331,14 +376,12 @@ process_dat:
     }
 }
 
-/*
+/**
  * Receive thread , store uart data and transform tcpip stack
  *
- * @param ppp_device *device
- *
- *
- * @return  0: execute successful
+ * @param device    the point of device driver structure, ppp_device structure
  *
+ * @return  RT_EOK  we shouldn't let the recieve thread return data, recieve thread need keepalive all the time
  */
 static int ppp_recv_entry(struct ppp_device *device)
 {
@@ -404,14 +447,14 @@ static int ppp_recv_entry(struct ppp_device *device)
     return RT_EOK;
 }
 
-/*
+/**
  * Creat a thread to creat receive thread function
  *
- * @param ppp_device *device
- *
- *
- * @return  0: execute successful
+ * @param   device      the point of device driver structure, ppp_device structure
  *
+ * @return  RT_EOK      recieve thread create and startup successfully
+ *          -RT_ERROR   create recieve thread successfully
+ *          -RT_ENOMEM  startup recieve thread successfully
  */
 static int ppp_recv_entry_creat(struct ppp_device *device)
 {
@@ -443,14 +486,12 @@ __exit:
     return result;
 }
 
-/*
+/**
  * ppp device init function,set ops funciton and base config
  *
- * @param rt_device_t *device
- *
- *
- * @return  0: execute successful
+ * @param device    the point of device driver structure, rt_device structure
  *
+ * @return  RT_EOK
  */
 static rt_err_t ppp_device_init(struct rt_device *device)
 {
@@ -466,14 +507,13 @@ static rt_err_t ppp_device_init(struct rt_device *device)
     return RT_EOK;
 }
 
-/*
+/**
  * initialize ppp device and set callback function
  *
- * @param rt_device_t *device
- *        rt_uint16_t oflag
- *
- * @return  0:  execute successful
+ * @param device    the point of device driver structure, rt_device structure
+ * @param oflag     the open flag of rt_device
  *
+ * @return  the result
  */
 static rt_err_t ppp_device_open(struct rt_device *device, rt_uint16_t oflag)
 {
@@ -507,7 +547,7 @@ static rt_err_t ppp_device_open(struct rt_device *device, rt_uint16_t oflag)
 
     /* uart transfer into tcpip protocol stack */
     rt_device_set_rx_indicate(ppp_device->uart, ppp_device_rx_ind);
-    LOG_I("(%s) is used by ppp_device.", ppp_device->uart->parent.name);
+    LOG_D("(%s) is used by ppp_device.", ppp_device->uart->parent.name);
 
 
     /* creat pppos */
@@ -566,13 +606,12 @@ __exit:
     return result;
 }
 
-/*
+/**
  * Close ppp device
  *
- * @param rt_device_t   *device
- *
+ * @param device    the point of device driver structure, rt_device structure
  *
- * @return  0: execute successful
+ * @return  RT_EOK
  */
 static rt_err_t ppp_device_close(struct rt_device *device)
 {
@@ -598,14 +637,14 @@ static rt_err_t ppp_device_close(struct rt_device *device)
     return RT_EOK;
 }
 
-/*
- * Control ppp device , access ppp mode or accsee AT mode
+/**
+ * Control ppp device , access ppp mode or accsee AT mode; but not it is useless
  *
- * @param rt_device_t   *device
- *        int           cmd
- *        void          *args
+ * @param device    the point of device driver structure, rt_device structure
+ * @param cmd       the command of device
+ * @param args      the private data of you send
  *
- * @return  0: execute successful
+ * @return  -RT_ENOSYS
  */
 static rt_err_t ppp_device_control(struct rt_device *device,int cmd, void *args)
 {
@@ -614,10 +653,7 @@ static rt_err_t ppp_device_control(struct rt_device *device,int cmd, void *args)
     return -RT_ENOSYS;
 }
 
-/*
- * ppp device ops
- *
- */
+/* ppp device ops */
 #ifdef RT_USING_DEVICE_OPS
 const struct rt_device_ops ppp_device_ops =
 {
@@ -628,13 +664,15 @@ const struct rt_device_ops ppp_device_ops =
 };
 #endif
 
-/*
+/**
  * Register ppp_device into rt_device frame,set ops function to rt_device inferface
  *
- * @param struct ppp_device *ppp_device
- *
- * @return  0: execute successful
+ * @param ppp_device    the point of device driver structure, ppp_device structure
+ * @param dev_name      the name of ppp_device name
+ * @param uart_name     the name of uart name what you used
+ * @param user_data     private data
  *
+ * @return  RT_EOK      ppp_device registered into rt_device frame successfully
  */
 int ppp_device_register(struct ppp_device *ppp_device, const char *dev_name, const char *uart_name, void *user_data)
 {
@@ -671,21 +709,21 @@ int ppp_device_register(struct ppp_device *ppp_device, const char *dev_name, con
     if( result == RT_EOK)
     {
         _g_ppp_device = ppp_device;
-        LOG_I("ppp_device has registered rt_device frame successful.");
+        LOG_I("ppp_device(%s) register successfully.", PPP_DEVICE_NAME);
     }
 
     return result;
 }
 
-/*
+/**
  * attach data interface device into ppp device frame
  *
- * @param       struct ppp_device *ppp_device
- *              char *uart_name
- *              void *user_data
- * @return  0: execute successful
- *         -1: error
+ * @param   ppp_device      the point of device driver structure, ppp_device structure
+ * @param   uart_name       the name of uart name what you used
+ * @param   user_data       private data
  *
+ * @return  RT_EOK          execute successful
+ * @return -RT_ERROR        error
  */
 int ppp_device_attach(struct ppp_device *ppp_device, const char *uart_name, void *user_data)
 {
@@ -708,14 +746,13 @@ int ppp_device_attach(struct ppp_device *ppp_device, const char *uart_name, void
     return RT_EOK;
 }
 
-/*
+/**
  * detach data interface device from ppp device frame
  *
- * @param       struct ppp_device *ppp_device
- *
- * @return  0: execute successful
- *         -1: error
+ * @param   ppp_device      the point of device driver structure, ppp_device structure
  *
+ * @return  RT_EOK          execute successful
+ * @return -RT_ERROR        error
  */
 int ppp_device_detach(struct ppp_device *ppp_device)
 {

+ 50 - 20
src/ppp_netif.c

@@ -20,6 +20,14 @@
 
 extern const struct netdev_ops lwip_netdev_ops;
 
+/**
+ * add ppp_netdev into netdev
+ *
+ * @param ppp_netif      the piont of netif control block
+ *
+ * @return  RT_EOK       execute successful
+ * @return  -ERR_IF      netif error, can't alloc memory for netdev
+ */
 rt_err_t ppp_netdev_add(struct netif *ppp_netif)
 {
 #define LWIP_NETIF_NAME_LEN 2
@@ -64,34 +72,56 @@ rt_err_t ppp_netdev_add(struct netif *ppp_netif)
     netdev->gw = ppp_netif->gw;
     netdev->netmask = ppp_netif->netmask;
 
-	{
-        extern const ip_addr_t* dns_getserver(u8_t numdns);
-        extern void  dns_setserver(u8_t numdns, const ip_addr_t *dnsserver);
-
-		/* sometime we can get second dns server but first dns server is empty, wo need do something to fix it */
-		if (!ip_addr_isany(dns_getserver(0)))
-        {
-            netdev_low_level_set_dns_server(netdev, 0, dns_getserver(0));
-        }
-        else
-        {
-        #define DEF_DNS_SERVER "114.114.114.114"
-            ip_addr_t dns_server;
-            inet_aton(DEF_DNS_SERVER, &dns_server);
-            dns_setserver(0, &dns_server);
-        }
-
-        netdev_low_level_set_dns_server(netdev, 1, dns_getserver(1));
+    return result;
+}
+
+/**
+ * refresh ppp_netif state, include DNS and netdev flag information
+ *
+ * @param ppp_netif      the piont of netif control block
+ *
+ * @return  RT_EOK       execute successful
+ */
+rt_err_t ppp_netdev_refresh(struct netif *ppp_netif)
+{
+    extern const ip_addr_t *dns_getserver(u8_t numdns);
+    extern void dns_setserver(u8_t numdns, const ip_addr_t *dnsserver);
+
+    char name[LWIP_NETIF_NAME_LEN + 1];
+    struct netdev *netdev = RT_NULL;
+
+    RT_ASSERT(ppp_netif);
+
+    rt_strncpy(name, ppp_netif->name, LWIP_NETIF_NAME_LEN);
+    netdev = netdev_get_by_name(name);
+
+    /* sometime we can get second dns server but first dns server is empty, wo need do something to fix it */
+    if (!ip_addr_isany(dns_getserver(0)))
+    {
+        netdev_low_level_set_dns_server(netdev, 0, dns_getserver(0));
+    }
+    else
+    {
+#define DEF_DNS_SERVER "114.114.114.114"
+        ip_addr_t dns_server;
+        inet_aton(DEF_DNS_SERVER, &dns_server);
+        dns_setserver(0, &dns_server);
     }
 
+    netdev_low_level_set_dns_server(netdev, 1, dns_getserver(1));
 
-    return result;
+    return RT_EOK;
 }
 
+/**
+ * delete netdev of ppp_netif create
+ *
+ * @param ppp_netif      the piont of netif control block
+ */
 void ppp_netdev_del(struct netif *ppp_netif)
 {
     char name[LWIP_NETIF_NAME_LEN + 1];
-    struct netdev *netdev;
+    struct netdev *netdev = RT_NULL;
 
     RT_ASSERT(ppp_netif);