Created
June 28, 2020 13:51
-
-
Save Yannik/deba5bfd70077c9ff7bc91d6cbee6d40 to your computer and use it in GitHub Desktop.
Contiki ipv6 tcp request example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "contiki.h" | |
| #include "contiki-net.h" | |
| #include <string.h> | |
| #include <stdio.h> /* For printf() */ | |
| #include "net/ipv6/tcpip.h" | |
| static struct etimer timer; | |
| static struct psock ps; | |
| static char buffer[100]; | |
| PROCESS(example_psock_client_process, "Cosm test client"); | |
| AUTOSTART_PROCESSES(&example_psock_client_process); | |
| /*---------------------------------------------------------------------------*/ | |
| static int | |
| handle_connection(struct psock *p) | |
| { | |
| PSOCK_BEGIN(p); | |
| while(1) { | |
| PSOCK_READTO(p, '\n'); | |
| printf("Got: %s", buffer); | |
| } | |
| PSOCK_END(p); | |
| } | |
| /*---------------------------------------------------------------------------*/ | |
| PROCESS_THREAD(example_psock_client_process, ev, data) | |
| { | |
| uip_ip6addr_t addr; | |
| PROCESS_BEGIN(); | |
| /* Use nginx proxy that is mapping our local | |
| * `::0:82` to `https://api.pachub.com/v2/` | |
| */ | |
| uip_ip6addr(&addr, 0,0,0,0,0,0,0,1); | |
| tcp_connect(&addr, UIP_HTONS(5000), NULL); | |
| printf("Trying...\n"); | |
| PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event); | |
| if(uip_aborted() || uip_timedout() || uip_closed()) { | |
| printf("Failed!\n"); | |
| } else if(uip_connected()) { | |
| printf("Connected.\n"); | |
| PSOCK_INIT(&ps, (uint8_t *)buffer, sizeof(buffer)); | |
| do { | |
| handle_connection(&ps); | |
| PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event); | |
| } while(!(uip_closed() || uip_aborted() || uip_timedout())); | |
| printf("\nClosed.\n"); | |
| } | |
| PROCESS_END(); | |
| } | |
| /*---------------------------------------------------------------------------*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CFLAGS=-Wno-unused-variable | |
| CONTIKI_PROJECT = example | |
| all: $(CONTIKI_PROJECT) | |
| CONTIKI = ../.. | |
| PLATFORMS_EXCLUDE = nrf52dk | |
| #use this to enable TSCH: MAKE_MAC = MAKE_MAC_TSCH | |
| MAKE_MAC ?= MAKE_MAC_CSMA | |
| MAKE_NET = MAKE_NET_IPV6 | |
| include $(CONTIKI)/Makefile.include |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define NETSTACK_CONF_WITH_IPV6 1 | |
| #define UIP_CONF_TCP 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # test with `telnet localhost 5000` | |
| import socket | |
| TCP_IP = '::0' | |
| TCP_PORT = 5000 | |
| s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) | |
| s.bind((TCP_IP, TCP_PORT)) | |
| s.listen(1) | |
| conn, addr = s.accept() | |
| print('Connection address:', addr) | |
| conn.send("hello world\n".encode("utf-8")) | |
| conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment