struct connectionState
{
int state;
struct altcp_pcb *pcb;
char *sendData;
int bytes;
char *recvData;
int start;
};
err_t sent(void *arg, struct altcp_pcb *pcb, u16_t len)
{
printf("data sent %d\n", len);
}
err_t recv(void *arg, struct altcp_pcb *pcb, struct pbuf *p, err_t err)
{
struct connectionState *cs = (struct connectionState *)arg;
if (p != NULL)
{
printf("recv total %d this buffer %d next %d err %d\n", p->tot_len, p->len, p->next, err);
if ((p->tot_len) > 2)
{
pbuf_copy_partial(p, (cs->recvData) + (cs->start), p->tot_len, 0);
cs->start += p->tot_len;
cs->recvData[cs->start] = 0;
cs->state = 4;
altcp_recved(pcb, p->tot_len);
}
pbuf_free(p);
}
else
{
cs->state = 6;
}
return ERR_OK;
}
static err_t connected(void *arg, struct altcp_pcb *pcb, err_t err)
{
struct connectionState *cs = (struct connectionState *)arg;
cs->state = 2;
return ERR_OK;
}
err_t poll(void *arg, struct altcp_pcb *pcb)
{
printf("Connection Closed \n");
struct connectionState *cs = (struct connectionState *)arg;
cs->state = 6;
}
void err(void *arg, err_t err)
{
if (err != ERR_ABRT)
{
printf("client_err %d\n", err);
}
}
struct connectionState *newConnection(char *sendData, int bytes, char *recvData)
{
struct connectionState *cs = (struct connectionState *)malloc(sizeof(struct connectionState));
cs->state = 0;
cs->pcb = altcp_new(NULL);
altcp_recv(cs->pcb, recv);
altcp_sent(cs->pcb, sent);
altcp_err(cs->pcb, err);
altcp_poll(cs->pcb, poll, 10);
altcp_arg(cs->pcb, cs);
cs->sendData = sendData;
cs->bytes=bytes;
cs->recvData = recvData;
cs->start = 0;
return cs;
}
struct connectionState *doRequest(ip_addr_t *ip, char *host, u16_t port, char *request, char *file, char *sendData, char *recvData)
{
char headerTemplate[] = "%s %s HTTP/1.1\r\nHOST:%s:%d\r\nConnection: close\r\nContent-length: %d\r\n\r\n%s";
int len = snprintf(NULL, 0, headerTemplate, request, file, host, port, strlen(sendData), sendData);
char *requestData = malloc(len + 1);
snprintf(requestData, len + 1, headerTemplate, request, file, host, port, strlen(sendData), sendData);
struct connectionState *cs = newConnection(requestData, strlen(requestData),recvData);
cyw43_arch_lwip_begin();
err_t err = altcp_connect(cs->pcb, ip, port, connected);
cyw43_arch_lwip_end();
cs->state = 1;
return cs;
}
struct connectionState *doRequestBinary(ip_addr_t *ip, char *host, u16_t port, char *request, char *file, char *sendData, int bytes, char *recvData)
{
char headerTemplate[] = "%s %s HTTP/1.1\r\nHOST:%s:%d\r\nConnection: close\r\nContent-length: %d\r\n\r\n";
int len = snprintf(NULL, 0, headerTemplate, request, file, host, port, bytes);
char *requestData = malloc(len + bytes + 1);
snprintf(requestData, len + 1, headerTemplate, request, file, host, port, bytes);
memcpy(requestData + len, sendData, bytes);
struct connectionState *cs = newConnection(requestData, len + bytes, recvData);
cyw43_arch_lwip_begin();
err_t err = altcp_connect(cs->pcb, ip, port, connected);
cyw43_arch_lwip_end();
cs->state = 1;
return cs;
}
int pollRequest(struct connectionState **pcs)
{
if (*pcs == NULL)
return 0;
struct connectionState *cs = *pcs;
switch (cs->state)
{
case 0:
case 1:
case 3:
break;
case 2:
cs->state = 3;
cyw43_arch_lwip_begin();
err_t err = altcp_write(cs->pcb, cs->sendData, cs->bytes, 0);
err = altcp_output(cs->pcb);
cyw43_arch_lwip_end();
break;
case 4:
cs->state = 5;
break;
case 6:
cyw43_arch_lwip_begin();
altcp_close(cs->pcb);
cyw43_arch_lwip_end();
free(cs);
*pcs = NULL;
return 0;
}
return cs->state;
}
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include "setupWifi.h"
#include "lwip/altcp.h"
#include "request.h"
#define BUF_SIZE 4096
char myBuff1[BUF_SIZE];
int main()
{
char ssid[] = "laribina";
char pass[] = "hawkhawk";
uint32_t country = CYW43_COUNTRY_UK;
uint32_t auth = CYW43_AUTH_WPA2_MIXED_PSK;
stdio_init_all();
setup(country, ssid, pass, auth, "MyPicoW", NULL, NULL, NULL);
char randdata[500];
ip_addr_t ip;
IP4_ADDR(&ip, 192, 168, 11, 101);
for (int p = 0; p < 500; p++)
{
randdata[p] = (uint8_t)rand();
}
struct connectionState *cs1 = doRequestBinary(&ip, "192.168.11.101", 8080, "PUT", "/", randdata, 500, myBuff1);
while (pollRequest(&cs1))
{
sleep_ms(200);
}
printf("%s\n", myBuff1);
return 0;
}
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include "lwip/altcp.h"
#include "lwip/dns.h"
#include "setupWifi.h"
#include "request.h"
#define BUF_SIZE 2048
char myBuff1[BUF_SIZE];
void dns_found(const char *name, const ip_addr_t *ip, void *arg)
{
ip_addr_t *ipResult = (ip_addr_t *)arg;
if (ip)
{
ip4_addr_copy(*ipResult, *ip);
}
else
{
ip4_addr_set_loopback(ipResult);
}
return;
}
err_t getIP(char *URL, ip_addr_t *ipResult)
{
cyw43_arch_lwip_begin();
err_t err = dns_gethostbyname(URL, ipResult, dns_found, ipResult);
cyw43_arch_lwip_end();
return err;
}
int main()
{
stdio_init_all();
connect();
ip_addr_t ip;
ip4_addr_set_zero(&ip);
getIP("example.com", &ip);
while (!ip_addr_get_ip4_u32(&ip))
{
sleep_ms(100);
};
if (ip4_addr_isloopback(&ip))
{
printf("address not found");
}
struct connectionState *cs1 = doRequest(&ip, "example.com", 80, "GET", "/", NULL, myBuff1);
while (pollRequest(&cs1))
{
sleep_ms(100);
}
printf("Buffer 1 = \n%s\n\n", myBuff1);
printf("Data Transferred\n");
cyw43_arch_deinit();
return 0;
}