Added C interface to HTTPD functions.

This commit is contained in:
Oliver Schmidt 2017-12-07 23:03:04 +01:00
parent b973526d7e
commit 673e2da011
4 changed files with 125 additions and 5 deletions

View File

@ -84,13 +84,13 @@ unsigned int ip65_random_word(void);
// will overwrite.
//
// Inputs: quad: IP address
// Output: Null terminated string containing dotted quad (e.g. "192.168.1.0")
// Output: Zero terminated string containing dotted quad (e.g. "192.168.1.0")
//
char* __fastcall__ dotted_quad(unsigned long quad);
// Convert a string representing a dotted quad (IP address, netmask) into 4 octets
//
// Inputs: quad: Null terminated string containing dotted quad (e.g. "192.168.1.0"),
// Inputs: quad: Zero terminated string containing dotted quad (e.g. "192.168.1.0"),
// to simplify URL parsing, a ':' or '/' can also terminate the string.
// Output: IP address, 0 on error
//
@ -112,9 +112,9 @@ unsigned char dhcp_init(void);
// Resolve a string containing a hostname (or a dotted quad) to an IP address
//
// Inputs: hostname: pointer to null terminated string that contains either
// a DNS hostname (e.g. "host.example.com") or an address
// in "dotted quad" format (e.g. "192.168.1.0").
// Inputs: hostname: Zero terminated string containing either a DNS hostname
// (e.g. "host.example.com") or an address in "dotted quad"
// format (e.g. "192.168.1.0")
// Output: IP address of the hostname, 0 on error
//
unsigned long __fastcall__ dns_resolve(const char* hostname);
@ -227,6 +227,56 @@ unsigned char tcp_send_keep_alive(void);
//
unsigned long sntp_get_time(unsigned long server);
// Start an HTTP server
//
// This routine will stay in an endless loop that is broken only if user press the abort key.
//
// Inputs: port: TCP port to listen on
// callback: Vector to call for each inbound HTTP request
// client: IP address of the client that sent the request
// method: Zero terminaed string containg the HTTP method
// path: Zero terminaed string containg the HTTP path
// Output: None
//
void __fastcall__ httpd_start(unsigned int port, void (*callback)(unsigned long client,
const char* method,
const char* path));
// HTTP response types
//
#define HTTPD_RESPONSE_NOHEADER 0 // No HTTP response header
#define HTTPD_RESPONSE_200_TEXT 1 // HTTP Code: 200 OK, Content Type: 'text/text'
#define HTTPD_RESPONSE_200_HTML 2 // HTTP Code: 200 OK, Content Type: 'text/html'
#define HTTPD_RESPONSE_200_DATA 3 // HTTP Code: 200 OK, Content Type: 'application/octet-stream'
#define HTTPD_RESPONSE_404 4 // HTTP Code: 404 Not Found
#define HTTPD_RESPONSE_500 5 // HTTP Code: 500 System Error
// Send HTTP response.
//
// Calling httpd_send_response is only valid in the context of a httpd_start callback.
// For the response types HTTPD_RESPONSE_404 and HTTPD_RESPONSE_500 'buf' is ignored.
// With the response type HTTPD_RESPONSE_NOHEADER it's possible to add more content to
// an already sent HTTP response.
//
// Inputs: response_type: Value describing HTTP code and content type in response header
// buf: Pointer to buffer with HTTP response content
// len: Length of buffer with HTTP response content
// Output: None
//
void __fastcall__ httpd_send_response(unsigned char response_type,
const unsigned char* buf, unsigned int len);
// Retrieve the value of a variable defined in the previously received HTTP request.
//
// Calling http_get_value is only valid in the context of a httpd_start callback.
// Only the first letter in a variable name is significant. E.g. if a querystring contains
// the variables 'a','alpha' and 'alabama', then only the first one will be retrievable.
//
// Inputs: name: Variable to retrieve
// Output: Variable value (zero terminated string) if variable exists, null otherwise.
//
char* __fastcall__ http_get_value(char name);
// Get number of milliseconds since initialization
//
// Inputs: None

View File

@ -23,7 +23,9 @@ IP65OBJS=\
dottedquad_c.o \
eth.o \
http.o \
http_c.o \
httpd.o \
httpd_c.o \
icmp_c.o \
input_c.o \
ip65.o \

12
ip65/http_c.s Normal file
View File

@ -0,0 +1,12 @@
.include "../inc/common.i"
.export _http_get_value
.import http_get_value
_http_get_value:
jsr http_get_value
bcc :+
ldx #$00
txa
: rts

56
ip65/httpd_c.s Normal file
View File

@ -0,0 +1,56 @@
.include "../inc/common.i"
.export _httpd_start
.export _httpd_send_response
.import httpd_start
.import httpd_port_number
.import httpd_send_response
.import httpd_response_buffer_length
.import tcp_remote_ip
.import http_get_value
.import pushax, pusheax, popax, popa
.importzp sreg
.data
callback:
ldax tcp_remote_ip+2
stax sreg
ldax tcp_remote_ip
jsr pusheax
lda #$01
jsr http_get_value
jsr pushax
lda #$02
jsr http_get_value
jmpvector:
jsr $ffff
sec
rts
.code
_httpd_start:
stax jmpvector+1
jsr popax
stax httpd_port_number
ldax #callback
jmp httpd_start
_httpd_send_response:
stax httpd_response_buffer_length
jsr popax
pha
jsr popa
tay
pla
cpy #$04
bcc :+
lda #$00
sta httpd_response_buffer_length
sta httpd_response_buffer_length+1
: jmp httpd_send_response