Contiki-NG
websocket.h
1/*
2 * Copyright (c) 2012, Thingsquare, http://www.thingsquare.com/.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 */
31#ifndef WEBSOCKET_H
32#define WEBSOCKET_H
33
34#include "websocket-http-client.h"
35
36typedef enum {
37 WEBSOCKET_ERR = 0,
38 WEBSOCKET_OK = 1,
39 WEBSOCKET_IN_PROGRESS = 2,
40 WEBSOCKET_HOSTNAME_NOT_FOUND = 3,
41 WEBSOCKET_CONNECTED = 4,
42 WEBSOCKET_DATA = 5,
43 WEBSOCKET_RESET = 6,
44 WEBSOCKET_TIMEDOUT = 7,
45 WEBSOCKET_CLOSED = 8,
46 WEBSOCKET_PINGED = 9,
47 WEBSOCKET_DATA_RECEIVED = 10,
48 WEBSOCKET_PONG_RECEIVED = 11,
49} websocket_result_t;
50
51struct websocket;
52
53typedef void (* websocket_callback)(struct websocket *s,
54 websocket_result_t result,
55 const uint8_t *data,
56 uint16_t datalen);
57#ifdef WEBSOCKET_CONF_MAX_MSGLEN
58#define WEBSOCKET_MAX_MSGLEN WEBSOCKET_CONF_MAX_MSGLEN
59#else /* WEBSOCKET_CONF_MAX_MSGLEN */
60#define WEBSOCKET_MAX_MSGLEN 200
61#endif /* WEBSOCKET_CONF_MAX_MSGLEN */
62
63struct websocket {
64 struct websocket *next; /* Must be first. */
65 struct websocket_http_client_state s;
66 websocket_callback callback;
67
68 uint8_t mask[4];
69 uint32_t left, len;
70 uint8_t opcode;
71
72 uint8_t state;
73
74 uint8_t headercacheptr;
75 uint8_t headercache[10]; /* The maximum websocket header + mask is 6
76 + 4 bytes long */
77};
78
79enum {
80 WEBSOCKET_STATE_CLOSED = 0,
81 WEBSOCKET_STATE_DNS_REQUEST_SENT = 1,
82 WEBSOCKET_STATE_HTTP_REQUEST_SENT = 2,
83 WEBSOCKET_STATE_WAITING_FOR_HEADER = 3,
84 WEBSOCKET_STATE_RECEIVING_HEADER = 4,
85 WEBSOCKET_STATE_HEADER_RECEIVED = 5,
86 WEBSOCKET_STATE_RECEIVING_DATA = 6,
87};
88
89
90void websocket_init(struct websocket *s);
91
92void websocket_set_proxy(struct websocket *s,
93 const uip_ipaddr_t *addr, uint16_t port);
94
95websocket_result_t websocket_open(struct websocket *s,
96 const char *url,
97 const char *subprotocol,
98 const char *hdr,
99 websocket_callback c);
100
101int websocket_send(struct websocket *s,
102 const uint8_t *data, uint16_t datalen);
103
104int websocket_send_str(struct websocket *s,
105 const char *strptr);
106
107void websocket_close(struct websocket *s);
108
109int websocket_ping(struct websocket *s);
110
111int websocket_queuelen(struct websocket *s);
112
113#endif /* WEBSOCKET_H */
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition: uip-nd6.c:107