IoT Wireless

WizFi250 FTP Client File Upload 본문

WiFi module

WizFi250 FTP Client File Upload

DH0815 2014. 11. 14. 12:38
이번에 포스팅 할 내용은 WizFi250을 이용하여, FTP 서버에 파일을 업로드 하는 방법이다.

WizFi250을 AT Command로 제어하면서, FTP 서버에 로그인한 후에 20 Byte 사이즈의 로그 파일(a.txt)을 업로드하는 과정에 대해서 설명하겠다.
테스트에 사용된 FTP 서버는 <ProFTPD 1.3.4a>이고, RFC959에 나와 있는 내용을 참조로 하여 최소한의 명령어로 구현하였다.

RFC959 https://www.ietf.org/rfc/rfc959.txt

FTP 서버 로그인에 사용된 USER, PASS 명령어(4.1.1.  ACCESS CONTROL COMMANDS)를 제외하고, 테스트에 사용된 주요 FTP Control Command들과 실제 구현 예제에 대해 살펴보겠다.

(1) 데이터는 Binary mode로 
REPRESENTATION TYPE (TYPE)

The argument specifies the representation type as described
in the Section on Data Representation and Storage.  Several
types take a second parameter.  The first parameter is
denoted by a single Telnet character, as is the second
Format parameter for ASCII and EBCDIC; the second parameter
for local byte is a decimal integer to indicate Bytesize.
The parameters are separated by a space (Space, ASCII code
32).

(RFC959 4.1.2. TRANSFER PARAMETER COMMANDS)
(2) 실무에서 방화벽이나 공유기 문제로 정상 동작하지 않는 FTP Active mode 대신에 FTP Passive mode로 
PASSIVE (PASV)

This command requests the server-DTP to "listen" on a data
port (which is not its default data port) and to wait for a
connection rather than initiate one upon receipt of a
transfer command.  The response to this command includes the
host and port address this server is listening on.

(RFC959 4.1.2. TRANSFER PARAMETER COMMANDS)
(3) Data 포트를 계산하는 방법
DATA PORT (PORT)

The argument is a HOST-PORT specification for the data port
to be used in data connection.  There are defaults for both
the user and server data ports, and under normal
circumstances this command and its reply are not needed.  If
this command is used, the argument is the concatenation of a
32-bit internet host address and a 16-bit TCP port address.
This address information is broken into 8-bit fields and the
value of each field is transmitted as a decimal number (in
character string representation).  The fields are separated
by commas.  A port command would be:

   PORT h1,h2,h3,h4,p1,p2

(RFC959 4.1.2. TRANSFER PARAMETER COMMANDS)
여기에서, p1, p2가 16bit TCP Port address의 2 Byte를 의미하므로, 계산식은 아래와 같다.
(256 * p1) + p2 = FTP Data Port
즉, 아래 예제에서처럼, FTP 서버가 아래의 데이터를 보낸다면, 
227 Entering Passive Mode (222,98,xxx,xxx,194,23)
WizFi250은 데이터 전송을 위한 포트로 (256*194) + 23 = 49687을 사용하면 된다.

(4) FTP 서버에 File Upload

STORE (STOR)

This command causes the server-DTP to accept the data
transferred via the data connection and to store the data as
a file at the server site.  If the file specified in the
pathname exists at the server site, then its contents shall
be replaced by the data being transferred.  A new file is
created at the server site if the file specified in the
pathname does not already exist.

(RFC959 4.1.3. FTP SERVICE COMMANDS)
(5) 실제 구현 로그
WizFi250 Version 1.0.1.8 (WIZnet Co.Ltd)
AT+WSET=0,WizFiDemoAP
[OK]
AT+WSEC=0,,123456789
[OK]
AT+WNET=1
[OK]
AT+WJOIN
Joining : WizFiDemoAP
Successfully joined : WizFiDemoAP

[Link-Up Event]
  IP Addr    : 192.168.3.138
  Gateway    : 192.168.3.1
[OK]
(WizFi210 Association 하기)

AT+SCON=O,TCN,222.98.xxx.xxx,21,0,0

[CONNECT 0]
[OK]
{0,222.98.xxx.xxx,21,54}220 ProFTPD 1.3.4a Server (Debian) [::ffff:10.0.1.2]
(Control Port에 소켓 ID 0으로 접속되었다.)

AT+SSEND=0,,,12
[0,,,12]
(여기에서 "USER xxxxxx{0x0A}" 12 Byte를 전송한다.)
[OK]
{0,222.98.xxx.xxx,21,34}331 Password required for wiznet

AT+SSEND=0,,,12
[0,,,12]
(여기에서 "PASS xxxxxx{0x0A}" 12 Byte를 전송한다.)
[OK]
{0,222.98.xxx.xxx,21,27}230 User wiznet logged in


AT+SSEND=0,,,7
[0,,,7]
(여기에서 "TYPE I{0x0A}" 7 Byte를 전송한다.)
[OK]
{0,222.98.xxx.xxx,21,19}200 Type set to I

AT+SSEND=0,,,5
[0,,,5]
(여기에서 "PASV{0x0A}" 7 Byte를 전송한다.)
[OK]
{0,222.98.xxx.xxx,21,51}227 Entering Passive Mode (222,98,xxx,xxx,194,23)

(이제, FTP Data 전송을 위한 Connection을 한다. IP : 222.98.xxx.xxx Port : 49687 => (256*194) + 23)

AT+SCON=O,TCN,222.98.xxx.xxx,49687,0,0
[OK]

[CONNECT 1]
(Data Port에 소켓 ID 1로 접속되었다.)
AT+SSEND=0,,,11
[0,,,11]
(여기에서 "STOR a.txt{0x0A}" 11 Byte를 전송한다.)
[OK]
{0,222.98.xxx.xxx,21,51}150 Opening BINARY mode data connection for a.txt

AT+SSEND=1,,,20
(자, 이제 Data 포트로 실제 파일 데이터 20 Byte를 전송한다.)
[1,,,20]
[OK]

(전송이 끝나면, FTP Control Connection와 Data Connection을 모두 종료한다.)

AT+SMGMT=ALL

[DISCONNECT 0]

[DISCONNECT 1]
[OK]
이제, FTP 서버에 접속하여, ls를 해보면, 20 Byte의 a.txt 파일이 전송된 것을 확인 할 수 있다. 


지금까지, WizFi250으로 FTP 서버에 접속하여 간단하게 파일을 업로드하는 예제를 설명하였는데, RFC959에는 이 외에도 몇 가지 FTP Command가 있는데, 이를 이용하면 보다 다양한 FTP 기능을 구현할 수 있다.
CWD - change working directory
MKD - make a remote directory
LIST - list remote files
RETR - retrieve a remote file
SIZE - return the size of a file
DELE - delete a remote file
...........................

(RFC959 5.3.1.  FTP COMMANDS)