2017-08-13 05:16:50 +00:00
|
|
|
/*
|
|
|
|
* WiFi configuration via a simple web server.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 OurAirQuality.org
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0, January 2004 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
* http://www.apache.org/licenses/
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT
|
|
|
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS WITH THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __WIFICFG_H__
|
|
|
|
#define __WIFICFG_H__
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2019-10-03 19:17:45 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2017-08-13 05:16:50 +00:00
|
|
|
/*
|
|
|
|
* Printf format used to initialize a default AP ssid. It is passed the last
|
|
|
|
* three bytes of the mac address. This may be NULL to not default the ssid,
|
|
|
|
* but the AP network will not run without a ssid.
|
|
|
|
*/
|
2017-12-13 20:40:14 +00:00
|
|
|
extern const char *wificfg_default_ssid;
|
2017-08-13 05:16:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A default password for the AP interface. This may be NULL to not default the
|
|
|
|
* password, but the AP network will not run without a password. The minimum
|
|
|
|
* length is 8 characters.
|
|
|
|
*/
|
2017-12-13 20:40:14 +00:00
|
|
|
extern const char *wificfg_default_password;
|
2017-08-13 05:16:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A default hostname printf format string. This may be NULL to not default the
|
|
|
|
* hostname.
|
|
|
|
*/
|
2017-12-13 20:40:14 +00:00
|
|
|
extern const char *wificfg_default_hostname;
|
2017-08-13 05:16:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The web server parses the http method string in these enums. The ANY method
|
|
|
|
* is only use for dispatch. The method enum is passed to the handler functions.
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
HTTP_METHOD_GET,
|
|
|
|
HTTP_METHOD_POST,
|
|
|
|
HTTP_METHOD_HEAD,
|
|
|
|
HTTP_METHOD_OTHER,
|
|
|
|
HTTP_METHOD_ANY,
|
|
|
|
} wificfg_method;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The web server parses these content-type header values. This is passed to the
|
|
|
|
* dispatch function.
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
HTTP_CONTENT_TYPE_WWW_FORM_URLENCODED,
|
|
|
|
HTTP_CONTENT_TYPE_OTHER
|
|
|
|
} wificfg_content_type;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The function signature for the http server request handler functions.
|
|
|
|
*
|
|
|
|
* The buffer, with its length, is usable by the handler.
|
|
|
|
*/
|
|
|
|
typedef int (* wificfg_handler)(int s, wificfg_method method,
|
|
|
|
uint32_t content_length,
|
|
|
|
wificfg_content_type content_type,
|
|
|
|
char *buf, size_t len);
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const char *path;
|
|
|
|
wificfg_method method;
|
|
|
|
wificfg_handler handler;
|
|
|
|
bool secure;
|
|
|
|
} wificfg_dispatch;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start the Wifi Configuration http server task. The IP port number
|
|
|
|
* and a path dispatch list are needed. The dispatch list can not be
|
|
|
|
* stack allocated as it is passed to another task.
|
|
|
|
*/
|
|
|
|
void wificfg_init(uint32_t port, const wificfg_dispatch *dispatch);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Support for reading a form name or value from the socket. The name or value
|
|
|
|
* is truncated to the buffer length. The number of characters read is limited
|
|
|
|
* to the remainder which is updated. The 'valp' flag is set if a value follows.
|
|
|
|
*/
|
2017-12-13 16:29:49 +00:00
|
|
|
ssize_t wificfg_form_name_value(int s, bool *valp, size_t *rem, char *buf, size_t len);
|
2017-08-13 05:16:50 +00:00
|
|
|
|
|
|
|
/* Support for form url-encoding decoder. */
|
|
|
|
void wificfg_form_url_decode(char *string);
|
|
|
|
|
|
|
|
/* Support for html-escaping of form values. */
|
|
|
|
void wificfg_html_escape(char *string, char *buf, size_t len);
|
|
|
|
|
|
|
|
/* Support for writing a string in a response. */
|
2017-12-13 16:29:49 +00:00
|
|
|
ssize_t wificfg_write_string(int s, const char *str);
|
2017-08-13 05:16:50 +00:00
|
|
|
|
|
|
|
/* Support for writing a string in a response, with chunk transfer encoding.
|
|
|
|
* An optional buffer may be supplied to use to construct a chunk with the
|
|
|
|
* header and trailer, reducing the number of write() calls, and the str may be
|
|
|
|
* at the start of this buffer.
|
|
|
|
*/
|
2017-12-13 16:29:49 +00:00
|
|
|
ssize_t wificfg_write_string_chunk(int s, const char *str, char *buf, size_t len);
|
2017-08-13 05:16:50 +00:00
|
|
|
|
|
|
|
/* Write a chunk transfer encoding end marker. */
|
2017-12-13 16:29:49 +00:00
|
|
|
ssize_t wificfg_write_chunk_end(int s);
|
2017-08-13 05:16:50 +00:00
|
|
|
|
|
|
|
/* Write a html title meta data, using the hostname or AP SSI. */
|
|
|
|
int wificfg_write_html_title(int s, char *buf, size_t len, const char *str);
|
|
|
|
|
2018-06-17 06:04:34 +00:00
|
|
|
/* Wait until the station interface has connected to an access point,
|
|
|
|
* and obtained an IP address. */
|
|
|
|
void wificfg_wait_until_sta_connected(void);
|
|
|
|
|
|
|
|
bool wificfg_add_shutdown_hook(void (*fn)(void *), void *arg);
|
2017-08-13 05:16:50 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // __WIFICFG_H__
|