nes-proj/examples/websocket/http-example.c

63 lines
1.9 KiB
C
Raw Normal View History

#include "contiki-net.h"
#include "http-socket.h"
2017-09-22 13:06:44 +00:00
#include "ipv6/ip64-addr.h"
#include <stdio.h>
static struct http_socket s;
static int bytes_received = 0;
/*---------------------------------------------------------------------------*/
PROCESS(http_example_process, "HTTP Example");
AUTOSTART_PROCESSES(&http_example_process);
/*---------------------------------------------------------------------------*/
static void
callback(struct http_socket *s, void *ptr,
http_socket_event_t e,
const uint8_t *data, uint16_t datalen)
{
if(e == HTTP_SOCKET_ERR) {
printf("HTTP socket error\n");
} else if(e == HTTP_SOCKET_TIMEDOUT) {
printf("HTTP socket error: timed out\n");
} else if(e == HTTP_SOCKET_ABORTED) {
printf("HTTP socket error: aborted\n");
} else if(e == HTTP_SOCKET_HOSTNAME_NOT_FOUND) {
printf("HTTP socket error: hostname not found\n");
} else if(e == HTTP_SOCKET_CLOSED) {
printf("HTTP socket closed, %d bytes received\n", bytes_received);
} else if(e == HTTP_SOCKET_DATA) {
bytes_received += datalen;
printf("HTTP socket received %d bytes of data\n", datalen);
}
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(http_example_process, ev, data)
{
static struct etimer et;
uip_ip4addr_t ip4addr;
uip_ip6addr_t ip6addr;
PROCESS_BEGIN();
uip_ipaddr(&ip4addr, 8,8,8,8);
ip64_addr_4to6(&ip4addr, &ip6addr);
uip_nameserver_update(&ip6addr, UIP_NAMESERVER_INFINITE_LIFETIME);
etimer_set(&et, CLOCK_SECOND * 60);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
http_socket_init(&s);
http_socket_get(&s, "http://www.contiki-os.org/", 0, 0,
callback, NULL);
etimer_set(&et, CLOCK_SECOND);
while(1) {
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
etimer_reset(&et);
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/