Added C interface to URL functions.

This commit is contained in:
Oliver Schmidt 2018-07-20 18:12:51 +02:00
parent c1ddcbc5af
commit 3fb437f7cb
4 changed files with 119 additions and 36 deletions

View File

@ -238,10 +238,10 @@ bool tcp_send_keep_alive(void);
//
uint32_t __fastcall__ sntp_get_time(uint32_t server);
// Download a file from a TFTP server and provide data to user supplied vector.
// Download a file from a TFTP server and provide data to user supplied vector
//
// Inputs: server: IP address of server to receive file from
// name: Name of file to download
// name: Zero terminated string containing the name of file to download
// callback: Vector to call once for each 512 byte packet received
// buf: Pointer to buffer containing data received
// len: 512 if buffer is full, otherwise number of bytes
@ -252,20 +252,20 @@ bool __fastcall__ tftp_download(uint32_t server, const char* name,
void __fastcall__ (*callback)(const uint8_t* buf,
uint16_t len));
// Download a file from a TFTP server and provide data to specified memory location.
// Download a file from a TFTP server and provide data to specified memory location
//
// Inputs: server: IP address of server to receive file from
// name: Name of file to download
// name: Zero terminated string containing the name of file to download
// buf: Pointer to buffer containing data received
// Output: Length of data received, 0 on error
//
uint16_t __fastcall__ tftp_download_to_memory(uint32_t server, const char* name,
const uint8_t* buf);
// Upload a file to a TFTP server with data retrieved from user supplied vector.
// Upload a file to a TFTP server with data retrieved from user supplied vector
//
// Inputs: server: IP address of server to send file to
// name: Name of file to upload
// name: Zero terminated string containing the name of file to upload
// callback: Vector to call once for each 512 byte packet to be sent
// buf: Pointer to buffer containing data to be sent
// Output: 512 if buffer is full, otherwise number of bytes
@ -275,10 +275,10 @@ uint16_t __fastcall__ tftp_download_to_memory(uint32_t server, const char* name,
bool __fastcall__ tftp_upload(uint32_t server, const char* name,
uint16_t __fastcall__ (*callback)(const uint8_t* buf));
// Upload a file to a TFTP server with data retrieved from specified memory location.
// Upload a file to a TFTP server with data retrieved from specified memory location
//
// Inputs: server: IP address of server to send file to
// name: Name of file to upload
// name: Zero terminated string containing the name of file to upload
// buf: Pointer to buffer containing data to be sent
// len: Length of data to be sent
// Output: true if an error occured, false otherwise
@ -286,6 +286,35 @@ bool __fastcall__ tftp_upload(uint32_t server, const char* name,
bool __fastcall__ tftp_upload_from_memory(uint32_t server, const char* name,
const uint8_t* buf, uint16_t len);
// Parse an HTTP URL into a form that makes it easy to retrieve the specified resource
//
// On success the variables url_ip, url_port and url_selector (see below) are valid.
//
// Inputs: url: Zero (or ctrl char) terminated string containing the URL
// Output: true if an error occured, false otherwise
//
bool __fastcall__ url_parse(const char* url);
// Access to parsed HTTP URL
//
// Access to the three items below is only valid after url_parse returned false.
//
extern uint32_t url_ip; // IP address of host in URL
extern uint16_t url_port; // Port number of URL
extern char* url_selector; // Zero terminated string containing selector part of URL
// Download a resource specified by an HTTP URL
//
// On success the resource is zero terminated.
//
// Inputs: url: Zero (or ctrl char) terminated string containing the URL
// buf: Pointer to a buffer that the resource will be downloaded into
// len: Length of buffer
// Output: Length of resource downloaded, 0 on error
//
uint16_t __fastcall__ url_download(const char* url, const uint8_t* buf, uint16_t len);
// Start an HTTP server
//
// This routine will stay in an endless loop that is broken only if user press the abort key.
@ -293,8 +322,8 @@ bool __fastcall__ tftp_upload_from_memory(uint32_t server, const char* name,
// 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
// method: Zero terminated string containing the HTTP method
// path: Zero terminated string containing the HTTP path
// Output: None
//
void __fastcall__ httpd_start(uint16_t port,

View File

@ -42,7 +42,8 @@ IP65OBJS=\
string_utils.o \
udp.o \
udp_c.o \
url.o
url.o \
url_c.o
ip65.lib: $(IP65OBJS) ip.o icmp.o
ar65 a $@ $^

View File

@ -37,6 +37,7 @@ TIMEOUT_SECONDS = 15
.export url_download_buffer
.export url_download_buffer_length
.export resource_download
.export resource_buffer
target_string = ptr1
search_string = ptr2
@ -61,8 +62,8 @@ selector_buffer = output_buffer
url_download_buffer: .res 2 ; points to a buffer that url will be downloaded into
url_download_buffer_length: .res 2 ; length of buffer that url will be downloaded into
temp_buffer: .res 2
temp_buffer_length: .res 2
resource_buffer: .res 2
resource_buffer_length: .res 2
download_flag: .res 1
@ -287,13 +288,12 @@ url_download:
; url_download_buffer_length - length of buffer
; outputs:
; sec if an error occured, else buffer pointed at by url_download_buffer is filled with contents
; of specified resource (with an extra 2 null bytes at the end),
; AX = length of resource downloaded.
; of specified resource (with an extra 2 null bytes at the end).
resource_download:
ldax url_download_buffer
stax temp_buffer
stax resource_buffer
ldax url_download_buffer_length
stax temp_buffer_length
stax resource_buffer_length
jsr put_zero_at_end_of_dl_buffer
ldx #3 ; save IP address just retrieved
@ -342,7 +342,7 @@ url_download_callback:
put_zero_at_end_of_dl_buffer:
; put a zero byte at the end of the file
ldax temp_buffer
ldax resource_buffer
stax ptr2
lda #0
tay
@ -351,45 +351,45 @@ put_zero_at_end_of_dl_buffer:
not_end_of_file:
; copy this chunk to our input buffer
ldax temp_buffer
ldax resource_buffer
stax copy_dest
ldax tcp_inbound_data_ptr
stax copy_src
sec
lda temp_buffer_length
lda resource_buffer_length
sbc tcp_inbound_data_length
pha
lda temp_buffer_length+1
lda resource_buffer_length+1
sbc tcp_inbound_data_length+1
bcc @would_overflow_buffer
sta temp_buffer_length+1
sta resource_buffer_length+1
pla
sta temp_buffer_length
sta resource_buffer_length
ldax tcp_inbound_data_length
jsr copymem
; increment the pointer into the input buffer
clc
lda temp_buffer
lda resource_buffer
adc tcp_inbound_data_length
sta temp_buffer
lda temp_buffer+1
sta resource_buffer
lda resource_buffer+1
adc tcp_inbound_data_length+1
sta temp_buffer+1
sta resource_buffer+1
jmp put_zero_at_end_of_dl_buffer
@would_overflow_buffer:
pla ; clean up the stack
ldax temp_buffer_length
ldax resource_buffer_length
jsr copymem
lda temp_buffer
adc temp_buffer_length
sta temp_buffer
lda temp_buffer+1
adc temp_buffer_length+1
sta temp_buffer+1
lda resource_buffer
adc resource_buffer_length
sta resource_buffer
lda resource_buffer+1
adc resource_buffer_length+1
sta resource_buffer+1
lda #0
sta temp_buffer_length
sta temp_buffer_length+1
sta resource_buffer_length
sta resource_buffer_length+1
jmp put_zero_at_end_of_dl_buffer

53
ip65/url_c.s Normal file
View File

@ -0,0 +1,53 @@
.include "../inc/common.inc"
.export _url_parse
.export _url_download
.export _url_ip
.export _url_port
.export _url_selector
.import url_parse
.import url_download
.import url_download_buffer
.import url_download_buffer_length
.import url_ip
.import url_port
.import url_selector
.import resource_buffer
.import popax
_url_parse:
jsr url_parse
ldx #$00
txa
rol
rts
_url_download:
stax url_download_buffer_length
jsr popax
stax url_download_buffer
jsr popax
jsr url_download
bcs error
sec
lda resource_buffer
sbc url_download_buffer
tay
lda resource_buffer+1
sbc url_download_buffer+1
tax
tya
rts
error:
ldx #$00
txa
rts
_url_ip := url_ip
_url_port := url_port
_url_selector := url_selector