专家
公告
财富商城
电子网
旗下网站
首页
问题库
专栏
标签库
话题
专家
NEW
门户
发布
提问题
发文章
NXP
上传个LwIP HttpClient源码(raw api)
2020-02-11 09:12
发布
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
站内问答
/
51单片机
11390
52
52
上传个LwIP HttpClient源码(raw api),有K60板子移植LWip的可以试试
webclient.rar
(3.69 KB, 下载次数: 135)
2014-12-31 11:48 上传 点击文件名下载附件
webclient
友情提示:
此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
52条回答
superrf
2020-02-11 12:50
本帖最后由 superrf 于 2014-12-31 11:59 编辑
/*
HTTP CLIENT FOR RAW LWIP
(c) 2008-2009 Noyens Kenneth
PUBLIC VERSION V0.2 16/05/2009
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1 as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the
Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdlib.h>
#include <string.h>
#include "webclient.h"
#include "utils/ustdlib.h"
// Close a PCB(connection)
void hc_clearpcb(struct tcp_pcb *pcb)
{
if(pcb != NULL)
{
// Close the TCP connection
tcp_close(pcb);
}
}
// Function that lwip calls for handling recv'd data
err_t hc_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
struct hc_state *state = arg;
char * page = NULL;
struct pbuf * temp_p;
hc_errormsg errormsg = GEN_ERROR;
int i;
if((err == ERR_OK) && (p != NULL))
{
tcp_recved(pcb, p->tot_len);
// Add payload (p) to state
temp_p = p;
while(temp_p != NULL)
{
state->RecvData = realloc(state->RecvData, temp_p->len + state->Len + 1);
// CHECK 'OUT OF MEM'
if(state->RecvData == NULL)
{
// OUT OF MEMORY
(*state->ReturnPage)(state->Num, OUT_MEM, NULL, 0);
return(ERR_OK);
}
strncpy(state->RecvData + state->Len, temp_p->payload, temp_p->len);
state->RecvData[temp_p->len + state->Len] = ' ';
state->Len += temp_p->len;
temp_p = temp_p->next;
}
// Removing payloads
while(p != NULL)
{
temp_p = p->next;
pbuf_free(p);
p = temp_p;
}
}
// NULL packet == CONNECTION IS CLOSED(by remote host)
else if((err == ERR_OK) && (p == NULL))
{
// Simple code for checking 200 OK
for(i=0; i < state->Len; i++)
{
if(errormsg == GEN_ERROR)
{
// Check for 200 OK
if((*(state->RecvData+i) == '2') && (*(state->RecvData+ ++i) == '0') && (*(state->RecvData+ ++i) == '0')) errormsg = OK;
if(*(state->RecvData+i) == ' ') errormsg = NOT_FOUND;
}
else
{
// Remove headers
if((*(state->RecvData+i) == ' ') && (*(state->RecvData+ ++i) == ' ') && (*(state->RecvData+ ++i) == ' ') && (*(state->RecvData + ++i) == ' '))
{
i++;
page = malloc(strlen(state->RecvData+i));
strcpy(page, state->RecvData+i);
break;
}
}
}
if(errormsg == OK)
{
// Put recv data to ---> p->ReturnPage
(*state->ReturnPage)(state->Num, OK, page, state->Len);
}
else
{
// 200 OK not found Return NOT_FOUND (WARNING: NOT_FOUND COULD ALSO BE 5xx SERVER ERROR, ...)
(*state->ReturnPage)(state->Num, errormsg, NULL, 0);
}
// Clear the PCB
hc_clearpcb(pcb);
// free the memory containing state
free(state->RecvData);
free(state);
}
return(ERR_OK);
}
// Function that lwip calls when there is an error
static void hc_error(void *arg, err_t err)
{
struct hc_state *state = arg;
// pcb already deallocated
// Call return function
// TO-DO: Check err_t err for out_mem, ...
(*state->ReturnPage)(state->Num, GEN_ERROR, NULL, 0);
free(state->RecvData);
free(state->PostVars);
free(state->Page);
free(state);
}
// Function that lwip calls when the connection is idle
// Here we can kill connections that have stayed idle for too long
static err_t hc_poll(void *arg, struct tcp_pcb *pcb)
{
struct hc_state *state = arg;
state->ConnectionTimeout++;
if(state->ConnectionTimeout > 20)
{
// Close the connection
tcp_abort(pcb);
// Give err msg to callback function
// Call return function
(*state->ReturnPage)(state->Num, TIMEOUT, NULL, 0);
}
return(ERR_OK);
}
// lwip calls this function when the remote host has successfully received data (ack)
static err_t hc_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
{
struct hc_state *state = arg;
// Reset connection timeout
state->ConnectionTimeout = 0;
return(ERR_OK);
}
// lwip calls this function when the connection is established
static err_t hc_connected(void *arg, struct tcp_pcb *pcb, err_t err)
{
struct hc_state *state = arg;
char * headers;
// error?
if(err != ERR_OK)
{
hc_clearpcb(pcb);
// Call return function
(*state->ReturnPage)(state->Num, GEN_ERROR, NULL, 0);
// Free wc state
free(state->RecvData);
free(state);
return(ERR_OK);
}
// Define Headers
if(state->PostVars == NULL)
{
// GET headers (without page)(+ ) = 19
headers = malloc(19 + strlen(state->Page));
usprintf(headers,"GET /%s HTTP/1.0 ", state->Page);
}
else
{
// POST headers (without PostVars or Page)(+ ) = 91
// Content-length: %d <== ??? (max 10)
headers = malloc(91 + strlen(state->PostVars) + strlen(state->Page) + 10);
usprintf(headers, "POST /%s HTTP/1.0 Content-type: application/x-www-form-urlencoded Content-length: %d %s ", state->Page, strlen(state->PostVars), state->PostVars);
}
// Check if we are nut running out of memory
if(headers == NULL)
{
hc_clearpcb(pcb);
// Call return function
(*state->ReturnPage)(state->Num, OUT_MEM, NULL, 0);
// Free wc state
free(state->RecvData);
free(state);
return(ERR_OK);
}
// Setup the TCP receive function
tcp_recv(pcb, hc_recv);
// Setup the TCP error function
tcp_err(pcb, hc_error);
// Setup the TCP polling function/interval //TCP_POLL IS NOT CORRECT DEFINED @ DOC!!!
tcp_poll(pcb, hc_poll, 10);
// Setup the TCP sent callback function
tcp_sent(pcb, hc_sent);
// Send data
tcp_write(pcb, headers, strlen(headers), 1);
tcp_output(pcb);
// remove headers
free(headers);
free(state->PostVars); // postvars are send, so we don't need them anymore
free(state->Page); // page is requested, so we don't need it anymore
return(ERR_OK);
}
// Public function for request a webpage (REMOTEIP, ...
int hc_open(struct ip_addr remoteIP, char *Page, char *PostVars, void (* returnpage)(u8_t, hc_errormsg, char *, u16_t))
{
struct tcp_pcb *pcb = NULL;
struct hc_state *state;
static u8_t num = 0;
// local port
u16_t port= 4545;
// Get a place for a new webclient state in the memory
state = malloc(sizeof(struct hc_state));
// Create a new PCB (PROTOCOL CONTROL BLOCK)
pcb = tcp_new();
if(pcb == NULL || state == NULL)
{
//UARTprintf("hc_open: Not enough memory for pcb or state ");
//Not enough memory
return 0;
}
// Define webclient state vars
num++;
state->Num = num;
state->RecvData = NULL;
state->ConnectionTimeout = 0;
state->Len = 0;
state->ReturnPage = returnpage;
// Make place for PostVars & Page
if(PostVars != NULL) state->PostVars = malloc(strlen(PostVars) +1);
state->Page = malloc(strlen(Page) +1);
// Check for "out of memory"
if(state->Page == NULL || (state->PostVars == NULL && PostVars != NULL))
{
free(state->Page);
free(state->PostVars);
free(state);
tcp_close(pcb);
return 0;
}
// Place allocated copy data
strcpy(state->Page, Page);
if(PostVars != NULL) strcpy(state->PostVars, PostVars);
// Bind to local IP & local port
while(tcp_bind(pcb, IP_ADDR_ANY, port) != ERR_OK)
{
// Local port in use, use port+1
port++;
}
// Use conn -> argument(s)
tcp_arg(pcb, state);
// Open connect (SEND SYN)
tcp_connect(pcb, &remoteIP, 80, hc_connected);
return num;
}
复制代码
加载中...
查看其它52个回答
一周热门
更多
>
相关问题
手把手学会例程系列:i.MX<二>:图解i.mx53源码补丁+烧录(...
59 个回答
从零开始MQX开发之二 创建与调试MQX项目
24 个回答
7009: Trim value invalid, value is blank or zero是什么错误
0 个回答
怎么头像消失啊?
23 个回答
求推荐KINETIS评估板:必备USB HS,ETHERNET
5 个回答
相关文章
基于IMX6Q移植uboot2018-09——添加单板
0个评论
IMX6UL定时器按键消抖实验
0个评论
I.MX6U处理器LED灯点亮汇编程序代码编写
0个评论
在NXP I.MX6上做一个基于Opencv和OpenGL的打砖块游戏
0个评论
【RFID安全】浅谈卡片破解
0个评论
LM75a
0个评论
emwin字库制作及汉字显示
0个评论
STM32学习之I2C
0个评论
×
关闭
采纳回答
向帮助了您的知道网友说句感谢的话吧!
非常感谢!
确 认
×
关闭
编辑标签
最多设置5个标签!
NXP
保存
关闭
×
关闭
举报内容
检举类型
检举内容
检举用户
检举原因
广告推广
恶意灌水
回答内容与提问无关
抄袭答案
其他
检举说明(必填)
提交
关闭
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
×
付费偷看金额在0.1-10元之间
确定
×
关闭
您已邀请
0
人回答
查看邀请
擅长该话题的人
回答过该话题的人
我关注的人
- /*
- HTTP CLIENT FOR RAW LWIP
- (c) 2008-2009 Noyens Kenneth
- PUBLIC VERSION V0.2 16/05/2009
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1 as published by
- the Free Software Foundation.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Lesser General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the
- Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
- */
- #include <stdlib.h>
- #include <string.h>
- #include "webclient.h"
- #include "utils/ustdlib.h"
-
- // Close a PCB(connection)
- void hc_clearpcb(struct tcp_pcb *pcb)
- {
- if(pcb != NULL)
- {
- // Close the TCP connection
- tcp_close(pcb);
- }
- }
-
- // Function that lwip calls for handling recv'd data
- err_t hc_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
- {
- struct hc_state *state = arg;
- char * page = NULL;
- struct pbuf * temp_p;
- hc_errormsg errormsg = GEN_ERROR;
- int i;
-
- if((err == ERR_OK) && (p != NULL))
- {
- tcp_recved(pcb, p->tot_len);
-
- // Add payload (p) to state
- temp_p = p;
- while(temp_p != NULL)
- {
- state->RecvData = realloc(state->RecvData, temp_p->len + state->Len + 1);
-
- // CHECK 'OUT OF MEM'
- if(state->RecvData == NULL)
- {
- // OUT OF MEMORY
- (*state->ReturnPage)(state->Num, OUT_MEM, NULL, 0);
- return(ERR_OK);
- }
-
- strncpy(state->RecvData + state->Len, temp_p->payload, temp_p->len);
- state->RecvData[temp_p->len + state->Len] = ' ';
- state->Len += temp_p->len;
-
- temp_p = temp_p->next;
- }
-
- // Removing payloads
-
- while(p != NULL)
- {
- temp_p = p->next;
- pbuf_free(p);
- p = temp_p;
- }
-
- }
-
- // NULL packet == CONNECTION IS CLOSED(by remote host)
- else if((err == ERR_OK) && (p == NULL))
- {
- // Simple code for checking 200 OK
- for(i=0; i < state->Len; i++)
- {
- if(errormsg == GEN_ERROR)
- {
- // Check for 200 OK
- if((*(state->RecvData+i) == '2') && (*(state->RecvData+ ++i) == '0') && (*(state->RecvData+ ++i) == '0')) errormsg = OK;
- if(*(state->RecvData+i) == '
') errormsg = NOT_FOUND;
- }
- else
- {
- // Remove headers
- if((*(state->RecvData+i) == '
') && (*(state->RecvData+ ++i) == '
') && (*(state->RecvData+ ++i) == '
') && (*(state->RecvData + ++i) == '
'))
- {
- i++;
- page = malloc(strlen(state->RecvData+i));
- strcpy(page, state->RecvData+i);
- break;
- }
- }
- }
-
- if(errormsg == OK)
- {
- // Put recv data to ---> p->ReturnPage
- (*state->ReturnPage)(state->Num, OK, page, state->Len);
- }
- else
- {
- // 200 OK not found Return NOT_FOUND (WARNING: NOT_FOUND COULD ALSO BE 5xx SERVER ERROR, ...)
- (*state->ReturnPage)(state->Num, errormsg, NULL, 0);
- }
-
- // Clear the PCB
- hc_clearpcb(pcb);
-
- // free the memory containing state
- free(state->RecvData);
- free(state);
- }
-
- return(ERR_OK);
- }
-
- // Function that lwip calls when there is an error
- static void hc_error(void *arg, err_t err)
- {
- struct hc_state *state = arg;
- // pcb already deallocated
-
- // Call return function
- // TO-DO: Check err_t err for out_mem, ...
- (*state->ReturnPage)(state->Num, GEN_ERROR, NULL, 0);
-
- free(state->RecvData);
- free(state->PostVars);
- free(state->Page);
- free(state);
- }
-
- // Function that lwip calls when the connection is idle
- // Here we can kill connections that have stayed idle for too long
- static err_t hc_poll(void *arg, struct tcp_pcb *pcb)
- {
- struct hc_state *state = arg;
-
- state->ConnectionTimeout++;
- if(state->ConnectionTimeout > 20)
- {
- // Close the connection
- tcp_abort(pcb);
-
- // Give err msg to callback function
- // Call return function
- (*state->ReturnPage)(state->Num, TIMEOUT, NULL, 0);
- }
-
- return(ERR_OK);
- }
-
- // lwip calls this function when the remote host has successfully received data (ack)
- static err_t hc_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
- {
- struct hc_state *state = arg;
-
- // Reset connection timeout
- state->ConnectionTimeout = 0;
-
- return(ERR_OK);
- }
-
- // lwip calls this function when the connection is established
- static err_t hc_connected(void *arg, struct tcp_pcb *pcb, err_t err)
- {
- struct hc_state *state = arg;
- char * headers;
-
- // error?
- if(err != ERR_OK)
- {
- hc_clearpcb(pcb);
-
- // Call return function
- (*state->ReturnPage)(state->Num, GEN_ERROR, NULL, 0);
-
- // Free wc state
- free(state->RecvData);
- free(state);
-
- return(ERR_OK);
- }
-
- // Define Headers
- if(state->PostVars == NULL)
- {
- // GET headers (without page)(+ ) = 19
- headers = malloc(19 + strlen(state->Page));
- usprintf(headers,"GET /%s HTTP/1.0
", state->Page);
- }
- else
- {
- // POST headers (without PostVars or Page)(+ ) = 91
- // Content-length: %d <== ??? (max 10)
- headers = malloc(91 + strlen(state->PostVars) + strlen(state->Page) + 10);
- usprintf(headers, "POST /%s HTTP/1.0
Content-type: application/x-www-form-urlencoded
Content-length: %d
%s
", state->Page, strlen(state->PostVars), state->PostVars);
- }
-
- // Check if we are nut running out of memory
- if(headers == NULL)
- {
- hc_clearpcb(pcb);
-
- // Call return function
- (*state->ReturnPage)(state->Num, OUT_MEM, NULL, 0);
-
- // Free wc state
- free(state->RecvData);
- free(state);
-
- return(ERR_OK);
- }
-
- // Setup the TCP receive function
- tcp_recv(pcb, hc_recv);
-
- // Setup the TCP error function
- tcp_err(pcb, hc_error);
-
- // Setup the TCP polling function/interval //TCP_POLL IS NOT CORRECT DEFINED @ DOC!!!
- tcp_poll(pcb, hc_poll, 10);
-
- // Setup the TCP sent callback function
- tcp_sent(pcb, hc_sent);
-
- // Send data
- tcp_write(pcb, headers, strlen(headers), 1);
- tcp_output(pcb);
-
- // remove headers
- free(headers);
- free(state->PostVars); // postvars are send, so we don't need them anymore
- free(state->Page); // page is requested, so we don't need it anymore
-
- return(ERR_OK);
- }
-
-
- // Public function for request a webpage (REMOTEIP, ...
- int hc_open(struct ip_addr remoteIP, char *Page, char *PostVars, void (* returnpage)(u8_t, hc_errormsg, char *, u16_t))
- {
- struct tcp_pcb *pcb = NULL;
- struct hc_state *state;
- static u8_t num = 0;
- // local port
- u16_t port= 4545;
-
- // Get a place for a new webclient state in the memory
- state = malloc(sizeof(struct hc_state));
-
- // Create a new PCB (PROTOCOL CONTROL BLOCK)
- pcb = tcp_new();
- if(pcb == NULL || state == NULL)
- {
- //UARTprintf("hc_open: Not enough memory for pcb or state
");
- //Not enough memory
- return 0;
- }
-
- // Define webclient state vars
- num++;
- state->Num = num;
- state->RecvData = NULL;
- state->ConnectionTimeout = 0;
- state->Len = 0;
- state->ReturnPage = returnpage;
-
- // Make place for PostVars & Page
- if(PostVars != NULL) state->PostVars = malloc(strlen(PostVars) +1);
- state->Page = malloc(strlen(Page) +1);
-
- // Check for "out of memory"
- if(state->Page == NULL || (state->PostVars == NULL && PostVars != NULL))
- {
- free(state->Page);
- free(state->PostVars);
- free(state);
- tcp_close(pcb);
- return 0;
- }
- // Place allocated copy data
- strcpy(state->Page, Page);
- if(PostVars != NULL) strcpy(state->PostVars, PostVars);
-
- // Bind to local IP & local port
- while(tcp_bind(pcb, IP_ADDR_ANY, port) != ERR_OK)
- {
- // Local port in use, use port+1
- port++;
- }
-
- // Use conn -> argument(s)
- tcp_arg(pcb, state);
-
- // Open connect (SEND SYN)
- tcp_connect(pcb, &remoteIP, 80, hc_connected);
-
- return num;
- }
复制代码一周热门 更多>