41 #include "contiki-net.h"
43 #include "http-post-auth.h"
48 #define MAX_USERNAME_PASSWORD 32
49 #define MAX_MESSAGE 160
52 #define HOST_NAME "api.example.org"
54 struct http_post_auth_state {
56 struct psock sin, sout;
57 char lengthstr[MAX_LENGTH];
58 char base64_username_password[MAX_USERNAME_PASSWORD];
59 char message[MAX_MESSAGE];
64 struct http_post_auth_state conn;
71 #define PRINTF(...) printf(__VA_ARGS__)
76 PROCESS(http_post_auth_process,
"HTTP POST auth client");
79 base64_encode_6bits(uint8_t c)
98 base64_encode_24bits(
const uint8_t inputdata[],
char outputdata[],
int len)
102 outputdata[0] = outputdata[1] = outputdata[2] = outputdata[3] =
'=';
105 outputdata[0] = base64_encode_6bits((inputdata[0] >> 2) & 0x3f);
106 outputdata[1] = base64_encode_6bits((((inputdata[0] << 4) & 0x30)));
107 outputdata[2] = outputdata[3] =
'=';
110 outputdata[0] = base64_encode_6bits((inputdata[0] >> 2) & 0x3f);
111 outputdata[1] = base64_encode_6bits((((inputdata[0] << 4) & 0x30) |
112 (inputdata[1] >> 4)) & 0x3f);
113 outputdata[2] = base64_encode_6bits((((inputdata[1] << 2) & 0x3f)));
118 outputdata[0] = base64_encode_6bits((inputdata[0] >> 2) & 0x3f);
119 outputdata[1] = base64_encode_6bits((((inputdata[0] << 4) & 0x30) |
120 (inputdata[1] >> 4)) & 0x3f);
121 outputdata[2] = base64_encode_6bits((((inputdata[1] << 2) & 0x3c) |
122 (inputdata[2] >> 6)) & 0x3f);
123 outputdata[3] = base64_encode_6bits((inputdata[2]) & 0x3f);
129 http_post_auth(
const uint8_t *username_password,
const char *msg)
133 struct http_post_auth_state *s;
140 PRINTF(
"Could not allocate memory for the tweet\n");
143 PSOCK_INIT(&s->sin, (uint8_t *)s->inputbuf,
sizeof(s->inputbuf) - 1);
144 PSOCK_INIT(&s->sout, (uint8_t *)s->inputbuf,
sizeof(s->inputbuf) - 1);
148 len = strlen((
char *)username_password);
150 for(i = 0; i < len; i += 3) {
151 base64_encode_24bits(&username_password[i], &s->base64_username_password[j],
155 s->base64_username_password[j] = 0;
158 strcpy(s->message,
"status=");
159 strcpy(&s->message[7], msg);
170 handle_output(
struct http_post_auth_state *s)
175 PSOCK_SEND_STR(&s->sout,
"POST /statuses/update.json HTTP/1.1\r\n");
189 snprintf(s->lengthstr,
sizeof(s->lengthstr),
"%d", strlen(s->message));
195 "Content-Type: application/x-www-form-urlencoded\r\n\r\n");
208 handle_input(
struct http_post_auth_state *s)
218 handle_connection(
struct http_post_auth_state *s)
226 struct http_post_auth_state *s = data;
240 PRINTF(
"Could not open TCP connection\n");
249 struct http_post_auth_state *s = (
struct http_post_auth_state *)data;
253 PRINTF(
"Connection closed\n");
255 PRINTF(
"Connection aborted/timedout\n");
262 handle_connection(s);
264 }
else if(s !=
NULL) {
268 PRINTF(
"Timed out due to inactivity\n");
276 handle_connection(s);
278 PRINTF(
"Abort because s == NULL\n");
#define PROCESS_EXIT()
Exit the currently running process.
#define PROCESS_BEGIN()
Define the beginning of a process.
#define PSOCK_BEGIN(psock)
Start the protosocket protothread in a function.
Representation of a uIP TCP connection.
CCIF struct uip_conn * tcp_connect(uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
Open a TCP connection to the specified IP address and port.
#define uip_aborted()
Has the connection been aborted by the other end?
#define NULL
The null pointer.
#define uip_poll()
Is the connection being polled by uIP?
#define uip_connected()
Has the connection just been connected?
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
#define PROCESS_END()
Define the end of a process.
#define uip_abort()
Abort the current connection.
#define PSOCK_SEND_STR(psock, str)
Send a null-terminated string.
#define PSOCK_INIT(psock, buffer, buffersize)
Initialize a protosocket.
CCIF uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
#define PSOCK_CLOSE(psock)
Close a protosocket.
The representation of a protosocket.
#define PSOCK_END(psock)
Declare the end of a protosocket's protothread.
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
#define PROCESS(name, strname)
Declare a process.
#define PSOCK_EXIT(psock)
Exit the protosocket's protothread.
process_event_t tcpip_event
The uIP event.
#define uip_timedout()
Has the connection timed out?
void process_start(struct process *p, process_data_t data)
Start a process.
#define uip_closed()
Has the connection been closed by the other end?
#define uip_ipaddr(addr, addr0, addr1, addr2, addr3)
Construct an IP address from four bytes.
#define UIP_TCP_MSS
The TCP maximum segment size.
void process_exit(struct process *p)
Cause a process to exit.