TriOS/system/regnatix/ftp.spin

357 lines
12 KiB
Plaintext
Raw Normal View History

2013-12-11 15:50:58 +01:00
{{
┌──────────────────────────────────────────────────────────────────────────────────────────────────────┐
2013-12-22 18:10:57 +01:00
│ Autor: Jörg Deckert │
│ Copyright (c) 2013 Jörg Deckert │
2013-12-11 15:50:58 +01:00
│ See end of file for terms of use. │
│ Die Nutzungsbedingungen befinden sich am Ende der Datei │
└──────────────────────────────────────────────────────────────────────────────────────────────────────┘
Informationen : hive-project.de
Kontakt : joergd@bitquell.de
System : TriOS
Name : flash
Chip : Regnatix
Typ : Programm
Version :
Subversion :
2013-12-16 22:34:11 +01:00
Funktion : FTP-Client
2013-12-11 15:50:58 +01:00
Komponenten : -
COG's : -
Logbuch :
2013-12-22 18:10:57 +01:00
22.12.2013-joergd - erste Version
2013-12-11 15:50:58 +01:00
Kommandoliste :
Notizen :
}}
OBJ
ios: "reg-ios"
str: "glob-string"
num: "glob-numbers" 'Number Engine
CON
_CLKMODE = XTAL1 + PLL16X
_XINFREQ = 5_000_000
VAR
2013-12-12 16:47:00 +01:00
long ip_addr
byte parastr[64]
2013-12-22 18:10:57 +01:00
byte remdir[64]
2013-12-18 18:33:20 +01:00
byte filename[64]
2013-12-12 16:47:00 +01:00
byte strTemp[128]
byte addrset
2013-12-16 22:34:11 +01:00
byte handleidx_control 'Handle FTP Control Verbindung
byte handleidx_data 'Handle FTP Data Verbindung
2013-12-11 15:50:58 +01:00
2013-12-18 18:33:20 +01:00
PUB main
2013-12-11 15:50:58 +01:00
2013-12-11 20:40:20 +01:00
ip_addr := 0
2013-12-22 18:10:57 +01:00
remdir[0] := 0
2013-12-18 18:33:20 +01:00
filename[0] := 0
2013-12-11 20:40:20 +01:00
2013-12-11 15:50:58 +01:00
ios.start 'ios initialisieren
ios.printnl
ios.parastart 'parameterübergabe starten
repeat while ios.paranext(@parastr) 'parameter einlesen
if byte[@parastr][0] == "/" 'option?
case byte[@parastr][1]
"?": ios.print(@help)
2013-12-11 20:37:08 +01:00
"s": if ios.paranext(@parastr)
2013-12-11 15:50:58 +01:00
setaddr(@parastr)
2013-12-22 18:10:57 +01:00
"v": ios.paranext(@remdir)
"d": ios.paranext(@filename)
2013-12-11 15:50:58 +01:00
other: ios.print(@help)
2013-12-16 06:56:03 +01:00
ifnot ftpconnect
ifnot ftplogin(string("anonymous"),string("password"))
2013-12-22 18:10:57 +01:00
ifnot byte[@remdir][0] == 0
ftpcwd
ifnot byte[@filename][0] == 0
if ftppasv
ftpretr
2013-12-18 18:33:20 +01:00
2013-12-22 10:44:16 +01:00
ftpclose
2013-12-16 06:56:03 +01:00
ios.stop
PRI ftpconnect
ifnot (ip_addr) ' Adresse 0.0.0.0
ios.print(string("FTP-Server nicht angegeben (Parameter /s)"))
2013-12-13 08:00:29 +01:00
ios.printnl
2013-12-16 06:56:03 +01:00
return(-1)
ios.print(string("Starte LAN..."))
ios.printnl
ios.lanstart
2013-12-22 18:10:57 +01:00
delay_ms(800) 'nach ios.lanstart dauert es, bis der Stack funktioniert
2013-12-16 06:56:03 +01:00
ios.print(string("Verbinde mit FTP-Server..."))
ios.printnl
2013-12-16 22:34:11 +01:00
if (handleidx_control := ios.lan_connect(ip_addr, 21)) == $FF
2013-12-16 06:56:03 +01:00
ios.print(string("Kein Socket frei..."))
2013-12-11 15:50:58 +01:00
ios.printnl
2013-12-16 06:56:03 +01:00
return(-1)
2013-12-16 22:34:11 +01:00
ifnot (ios.lan_waitconntimeout(handleidx_control, 2000))
2013-12-16 06:56:03 +01:00
ios.print(string("Verbindung mit FTP-Server konnte nicht aufgebaut werden."))
2013-12-11 15:50:58 +01:00
ios.printnl
2013-12-16 06:56:03 +01:00
return(-1)
ios.print(string("Verbindung mit FTP-Server hergestellt, warte auf Antwort..."))
ios.printnl
ifnot getResponse(string("220 "))
ios.print(string("Keine oder falsche Antwort vom FTP-Server erhalten."))
ios.printnl
return(-1)
return(0)
2013-12-22 10:44:16 +01:00
PRI ftpclose
if handleidx_control
ios.lan_close(handleidx_control)
handleidx_control := 0
if handleidx_data
ios.lan_close(handleidx_data)
handleidx_data := 0
2013-12-22 18:10:57 +01:00
PRI ftplogin(username, password) | pwreq, respOK
pwreq := FALSE
respOK := FALSE
2013-12-16 06:56:03 +01:00
ifnot strsize(username)
username := string("anonymous")
if sendStr(string("USER ")) || sendStr(username) || sendStr(string(13,10))
ios.print(string("Fehler beim Senden des Usernamens"))
ios.printnl
return(-1)
2013-12-22 18:10:57 +01:00
repeat until readLine == -1
ios.print(string(" < "))
ios.print(@strTemp)
ios.printnl
strTemp[4] := 0
if strcomp(@strTemp, string("230 "))
respOk := TRUE
quit
2013-12-22 18:10:57 +01:00
elseif strcomp(@strTemp, string("331 "))
pwreq := TRUE
respOk := TRUE
quit
2013-12-22 18:10:57 +01:00
ifnot respOK
ios.print(string("Keine oder falsche Antwort vom FTP-Server erhalten."))
ios.printnl
return(-1)
ifnot pwreq
return(0)
if sendStr(string("PASS ")) || sendStr(password) || sendStr(string(13,10))
ios.print(string("Fehler beim Senden des Passworts"))
ios.printnl
return(-1)
2013-12-16 06:56:03 +01:00
ifnot getResponse(string("230 "))
ios.print(string("Keine oder falsche Antwort vom FTP-Server erhalten."))
ios.printnl
return(-1)
2013-12-22 18:10:57 +01:00
2013-12-16 06:56:03 +01:00
return(0)
2013-12-12 16:47:00 +01:00
2013-12-22 18:10:57 +01:00
PRI ftpcwd
2013-12-12 16:47:00 +01:00
2013-12-22 18:10:57 +01:00
if sendStr(string("CWD ")) || sendStr(@remdir) || sendStr(string(13,10))
2013-12-16 06:56:03 +01:00
ios.print(string("Fehler beim Senden des Verzeichnisses"))
ios.printnl
return(-1)
ifnot getResponse(string("250 "))
ios.print(string("Keine oder falsche Antwort vom FTP-Server erhalten."))
ios.printnl
return(-1)
return(0)
2013-12-12 16:47:00 +01:00
2013-12-16 06:56:03 +01:00
PRI ftppasv : port | i, k, port256, port1
2013-12-11 15:50:58 +01:00
2013-12-16 06:56:03 +01:00
port := 0
port256 := 0
port1 := 0
k := 0
if sendStr(string("PASV",13,10))
2013-12-18 18:33:20 +01:00
return(0)
2013-12-16 06:56:03 +01:00
repeat until readLine == -1
ios.print(string(" < "))
ios.print(@strTemp)
ios.printnl
strTemp[4] := 0
if strcomp(@strTemp, string("227 "))
repeat i from 5 to 126
if (strTemp[i] == 0) OR (strTemp[i] == 13) OR (strTemp[i] == 10)
quit
if strTemp[i] == 44 'Komma
strTemp[i] := 0
k++
if k == 4 '4. Komma, Port Teil 1 folgt
port256 := i + 1
if k == 5 '5. Komma, Port Teil 2 folgt
port1 := i + 1
if strTemp[i] == 41 'Klammer zu
strTemp[i] := 0
if (port256 & port1)
port := (num.FromStr(@strTemp+port256, num#DEC) * 256) + num.FromStr(@strTemp+port1, num#DEC)
quit
2013-12-11 15:50:58 +01:00
2013-12-18 18:33:20 +01:00
if (port == 0)
ios.print(string("FTP-Server-Fehler beim Öffnen des Passiv-Ports"))
ios.printnl
return(0)
ios.print(string("Öffne Verbindung zu Passiv-Port "))
ios.print(num.ToStr(port, num#DEC))
ios.printnl
if (handleidx_data := ios.lan_connect(ip_addr, port)) == $FF
ios.print(string("Kein Socket frei..."))
ios.printnl
return(0)
ifnot (ios.lan_waitconntimeout(handleidx_data, 2000))
ios.print(string("Verbindung mit FTP-Server konnte nicht aufgebaut werden."))
ios.printnl
return(0)
PRI ftpretr | len, respOK
2013-12-22 10:44:16 +01:00
if sendStr(string("TYPE I",13,10))
ios.print(string("Fehler beim Senden des Types"))
2013-12-22 10:44:16 +01:00
ios.printnl
return(-1)
ifnot getResponse(string("200 "))
2013-12-22 10:44:16 +01:00
ios.print(string("Keine oder falsche Antwort vom FTP-Server erhalten."))
ios.printnl
return(-1)
if sendStr(string("SIZE ")) || sendStr(@filename) || sendStr(string(13,10))
ios.print(string("Fehler beim Senden des SIZE-Kommandos"))
2013-12-22 10:44:16 +01:00
ios.printnl
return(-1)
ifnot getResponse(string("213"))
2013-12-22 10:44:16 +01:00
ios.print(string("Keine oder falsche Antwort vom FTP-Server erhalten."))
ios.printnl
return(-1)
ifnot(len := num.FromStr(@strTemp+4, num#DEC))
return(-1)
2013-12-18 18:33:20 +01:00
2013-12-22 18:10:57 +01:00
if sendStr(string("RETR ")) || sendStr(@filename) || sendStr(string(13,10))
2013-12-18 18:33:20 +01:00
ios.print(string("Fehler beim Senden des Filenamens"))
return -1
respOK := FALSE
repeat until readLine == -1
ios.print(string(" < "))
ios.print(@strTemp)
ios.printnl
strTemp[4] := 0
if strcomp(@strTemp, string("150 "))
respOk := TRUE
quit
elseif strcomp(@strTemp, string("125 "))
respOk := TRUE
quit
ifnot respOK
2013-12-22 10:44:16 +01:00
ios.print(string("Keine oder falsche Antwort vom FTP-Server erhalten."))
ios.printnl
return(-1)
2013-12-22 18:10:57 +01:00
if ios.lan_rxdata(handleidx_data, @filename, len)
2013-12-22 10:44:16 +01:00
ios.print(string("Fehler beim Empfang der Datei."))
ios.printnl
return(-1)
ifnot getResponse(string("226 "))
ios.print(string("Keine oder falsche Antwort vom FTP-Server erhalten."))
ios.printnl
return(-1)
2013-12-18 18:33:20 +01:00
2013-12-11 15:50:58 +01:00
PRI setaddr (ipaddr) | pos, count 'IP-Adresse in Variable schreiben
2013-12-11 20:37:08 +01:00
count := 3
2013-12-11 15:50:58 +01:00
repeat while ipaddr
pos := str.findCharacter(ipaddr, ".")
if(pos)
byte[pos++] := 0
2013-12-11 20:37:08 +01:00
ip_addr += num.FromStr(ipaddr, num#DEC) << (8*count--)
2013-12-11 15:50:58 +01:00
ipaddr := pos
2013-12-11 20:37:08 +01:00
if(count == -1)
2013-12-11 15:50:58 +01:00
quit
2013-12-12 16:47:00 +01:00
PRI getResponse (strOk) : respOk | len
respOk := FALSE
2013-12-16 06:56:03 +01:00
repeat until readLine == -1
ios.print(string(" < "))
ios.print(@strTemp)
2013-12-12 16:47:00 +01:00
ios.printnl
strTemp[strsize(strOk)] := 0
if strcomp(@strTemp, strOk)
respOk := TRUE
quit
2013-12-12 16:47:00 +01:00
return respOk
PRI readLine | i, ch
repeat i from 0 to 126
ch := ios.lan_rxtime(handleidx_control, 2000)
2013-12-12 16:47:00 +01:00
if ch == 13
ch := ios.lan_rxtime(handleidx_control, 2000)
2013-12-12 16:47:00 +01:00
if ch == -1 or ch == 10
quit
strTemp[i] := ch
strTemp[i] := 0
2013-12-16 06:56:03 +01:00
return ch 'letztes Zeichen oder -1, wenn keins mehr empfangen
2013-12-12 16:47:00 +01:00
2013-12-16 06:56:03 +01:00
PRI sendStr (strSend) : error
2013-12-12 16:47:00 +01:00
2013-12-16 06:56:03 +01:00
ios.print(string(" > "))
ios.print(strSend)
ios.printnl
2013-12-16 22:34:11 +01:00
error := ios.lan_txdata(handleidx_control, strSend, strsize(strSend))
2013-12-12 16:47:00 +01:00
2013-12-16 06:56:03 +01:00
PRI delay_ms(Duration)
waitcnt(((clkfreq / 1_000 * Duration - 3932)) + cnt)
2013-12-12 16:47:00 +01:00
2013-12-11 15:50:58 +01:00
DAT 'sys: helptext
2013-12-22 18:10:57 +01:00
help byte "/? : Hilfe",13
byte "/s <a.b.c.d> : Server-Adresse",13
byte "/v <verzeichnis>: in entferntes Verzeichnis wechseln",13
byte "/d <filename> : Download <filename>",13
2013-12-11 15:50:58 +01:00
byte 0
DAT 'lizenz
{{
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ TERMS OF USE: MIT License │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation │
│files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, │
│modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software│
│is furnished to do so, subject to the following conditions: │
│ │
│The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.│
│ │
│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 AUTHORS 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 IN THE SOFTWARE. │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
}}