Contiki-NG
httpd-ws.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2012, Swedish Institute of Computer Science.
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 Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  */
31 
32 /**
33  * \file
34  * A simple webserver for web services
35  * \author
36  * Adam Dunkels <adam@sics.se>
37  * Niclas Finne <nfi@sics.se>
38  * Joakim Eriksson <joakime@sics.se>
39  */
40 
41 #ifndef HTTPD_WS_H_
42 #define HTTPD_WS_H_
43 
44 #include "contiki-net.h"
45 
46 #ifndef WEBSERVER_CONF_CFS_PATHLEN
47 #define HTTPD_PATHLEN 80
48 #else /* WEBSERVER_CONF_CFS_PATHLEN */
49 #define HTTPD_PATHLEN WEBSERVER_CONF_CFS_PATHLEN
50 #endif /* WEBSERVER_CONF_CFS_PATHLEN */
51 
52 #ifndef WEBSERVER_CONF_INBUF_SIZE
53 #define HTTPD_INBUF_SIZE (HTTPD_PATHLEN + 90)
54 #else /* WEBSERVER_CONF_INBUF_SIZE */
55 #define HTTPD_INBUF_SIZE WEBSERVER_CONF_INBUF_SIZE
56 #endif /* WEBSERVER_CONF_INBUF_SIZE */
57 
58 #if HTTPD_INBUF_SIZE < UIP_TCP_MSS || HTTPD_INBUF_SIZE < UIP_RECEIVE_WINDOW
59 #error HTTPD_INBUF_SIZE is too small. Must be at least a TCP window in size.
60 #endif
61 
62 #ifndef WEBSERVER_CONF_OUTBUF_SIZE
63 #define HTTPD_OUTBUF_SIZE (UIP_TCP_MSS + 20)
64 #else /* WEBSERVER_CONF_OUTBUF_SIZE */
65 #define HTTPD_OUTBUF_SIZE WEBSERVER_CONF_OUTBUF_SIZE
66 #endif /* WEBSERVER_CONF_OUTBUF_SIZE */
67 
68 struct httpd_ws_state;
69 typedef char (* httpd_ws_script_t)(struct httpd_ws_state *s);
70 typedef int (* httpd_ws_output_headers_t)(struct httpd_ws_state *s,
71  char *buffer, int buf_size,
72  int index);
73 
74 #define HTTPD_WS_GET 1
75 #define HTTPD_WS_POST 2
76 #define HTTPD_WS_PUT 3
77 #define HTTPD_WS_RESPONSE 4
78 
79 #define HTTPD_WS_STATE_UNUSED 0
80 #define HTTPD_WS_STATE_INPUT 1
81 #define HTTPD_WS_STATE_OUTPUT 2
82 #define HTTPD_WS_STATE_REQUEST_OUTPUT 3
83 #define HTTPD_WS_STATE_REQUEST_INPUT 4
84 
85 struct httpd_ws_state {
86  struct timer timer;
87  struct psock sin, sout;
88  struct pt outputpt;
89  char inputbuf[HTTPD_INBUF_SIZE];
90  char filename[HTTPD_PATHLEN];
91  const char *content_type;
92  uint16_t content_len;
93  char outbuf[HTTPD_OUTBUF_SIZE];
94  uint16_t outbuf_pos;
95  char state;
96  char request_type;
97  int response_index;
98 
99  httpd_ws_output_headers_t output_extra_headers;
100  httpd_ws_script_t script;
101 
102 #ifdef HTTPD_WS_CONF_USER_STATE
103  HTTPD_WS_CONF_USER_STATE;
104 #endif
105 };
106 
107 void httpd_ws_init(void);
108 void httpd_ws_appcall(void *state);
109 
110 struct httpd_ws_state *httpd_ws_request(char request_type,
111  const char *host_ip,
112  const char *host_hdr,
113  uint16_t port,
114  const char *file,
115  const char *content_type,
116  uint16_t content_len,
117  httpd_ws_script_t generator);
118 
119 #define SEND_STRING(s, str, len) PSOCK_SEND((s), (uint8_t *)(str), (len))
120 
121 httpd_ws_script_t httpd_ws_get_script(struct httpd_ws_state *s);
122 
123 PROCESS_NAME(httpd_ws_process);
124 
125 #endif /* HTTPD_WS_H_ */
The representation of a protosocket.
Definition: psock.h:112
A timer.
Definition: timer.h:82
#define PROCESS_NAME(name)
Declare the name of a process.
Definition: process.h:286