lwip pppos 例程
时间: 2023-07-23 11:05:31 浏览: 116
以下是使用lwIP库中的pppos协议栈的简单例程:
```c
#include "lwip/opt.h"
#include "lwip/sys.h"
#include "lwip/api.h"
#include "lwip/ppp.h"
#include "lwip/sio.h"
#include "lwip/tcpip.h"
#define PPP_TTY_OUTQLEN 256
#define PPP_TTY_INQLEN 256
static struct netif pppos_netif;
static u8_t ppp_output_buf[PPP_MRU + PPP_HDRLEN];
static u8_t ppp_input_buf[PPP_MRU + PPP_HDRLEN];
static void ppp_link_status_cb(void *ctx, int err_code, void *arg)
{
if (err_code == PPPERR_NONE) {
printf("ppp_link_status_cb: Connected\n");
} else {
printf("ppp_link_status_cb: Disconnected. err_code = %d\n", err_code);
}
}
static err_t ppp_output_cb(ppp_pcb *pcb, u8_t *data, int len, void *ctx)
{
sio_write(&pppos_netif, data, len);
return ERR_OK;
}
static void pppos_thread(void *arg)
{
ppp_pcb *ppp;
u32_t i;
/* Set up PPP control block */
ppp = ppp_new();
ppp_set_auth(ppp, PPPAUTHTYPE_NONE, "username", "password");
ppp_set_default(ppp);
ppp_set_link_status_callback(ppp, ppp_link_status_cb, NULL);
/* Set up PPP over serial I/O */
pppos_init(ppp);
/* Open the serial port */
if (sio_open() != SIO_OK) {
printf("Failed to open serial port\n");
return;
}
/* Initialise the netif struct */
netif_add(&pppos_netif, ip4_addr_unspecified(), ip4_addr_unspecified(),
ip4_addr_unspecified(), NULL, pppos_init, tcpip_input);
netif_set_default(&pppos_netif);
netif_set_link_up(&pppos_netif);
/* Start PPP connection */
ppp_connect(ppp, 0);
while (1) {
/* Wait for incoming data */
i = sio_read(&pppos_netif, ppp_input_buf, PPP_TTY_INQLEN);
if (i > 0) {
pppos_input_tcpip(ppp, ppp_input_buf, i);
}
/* Send outgoing data */
if (ppp_output_waiting(ppp)) {
i = ppp_output(ppp, ppp_output_buf, PPP_TTY_OUTQLEN);
if (i > 0) {
sio_write(&pppos_netif, ppp_output_buf, i);
}
}
/* Sleep for 100ms */
sys_msleep(100);
}
}
int main()
{
sys_init();
tcpip_init(NULL, NULL);
sys_thread_new("pppos", pppos_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
while (1) {
sys_msleep(1000);
}
return 0;
}
```
这个例程使用了串口进行物理层的通信,将串口数据传递给ppp协议栈进行处理,通过回调函数处理ppp连接状态和ppp输出数据,最终将ppp输出数据通过串口发送出去。需要注意的是,ppp协议栈的输入和输出缓冲区大小需要根据实际应用进行调整。
阅读全文