|
|
@@ -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)
|
|
|
{
|