8 Commits

Author SHA1 Message Date
3508acad10 FTP Client variable, function name modify 2015-04-06 10:55:39 +09:00
413ad6fc84 FTP Client 2015-04-06 09:47:06 +09:00
1f37c8efa5 Update README.md 2015-03-30 14:38:07 +09:00
999cb97cc8 Update README.md 2015-03-30 14:14:50 +09:00
455ab71314 Fixed a compile error. 2015-03-30 13:20:17 +09:00
f0dcff0671 Fixed a casting issue of variable. 2015-03-30 13:12:10 +09:00
460b04a42b Added a SNTP protocol. 2015-03-18 13:01:54 +09:00
97857ce12c Added a protocol of TFTP. 2015-02-23 13:12:34 +09:00
12 changed files with 2233 additions and 5 deletions

613
Internet/FTPClient/ftpc.c Normal file
View File

@ -0,0 +1,613 @@
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>
#include <stdarg.h>
#include "stdio_private.h"
#include "socket.h"
#include "ftpc.h"
#include "mmcHandler.h"
/* FTP Responses */
#define R_150 150 /* File status ok; opening data conn */
#define R_200 200 /* 'Generic' command ok */
#define R_220 220 /* Service ready for new user. */
#define R_226 226 /* Closing data connection. File transfer/abort successful */
#define R_227 227 /* Entering passive mode (h1,h2,h3,h4,p1,p2) */
#define R_230 230 /* User logged in, proceed */
#define R_331 331 /* User name okay, need password. */
#define TransferAscii 0x41
#define TransferBinary 0x49
#define ConnectActive 1
#define ConnectPassive 0
un_l2cval remote_ip;
uint16_t remote_port;
un_l2cval local_ip;
uint16_t local_port;
uint8_t connect_state_controlc = 0;
uint8_t connect_state_datac = 0;
uint8_t gModeActivePassiveflag = 0;
struct ftpd ftp;
enum CommandFirst {
f_nocmd,
f_dir,
f_put,
f_get,
};
enum CommandSecond {
s_nocmd,
s_dir,
s_put,
s_get,
};
struct Command {
enum CommandFirst First;
enum CommandSecond Second;
};
struct Command Command;
int current_yearc = 2014;
int current_monthc = 12;
int current_dayc = 31;
int current_hourc = 10;
int current_minc = 10;
int current_secc = 30;
uint8_t gMenuStart = 0;
uint8_t gDataSockReady = 0;
uint8_t gDataPutGetStart = 0;
uint8_t gModeAsciiBinary = 0;
uint8_t gModeActivePassive = ConnectPassive;
static uint8_t gMsgBuf[20]={0,};
// For FTP client examples; destination network info
uint8_t FTP_destip[4] = {192, 168, 0, 230};
uint16_t FTP_destport = 21;
extern int Board_UARTGetCharBlocking(void);
uint8_t* User_Keyboard_MSG()
{
uint8_t i=0;
do{
gMsgBuf[i] = Board_UARTGetCharBlocking();
i++;
}while(gMsgBuf[i-1]!=0x0d);
gMsgBuf[i-1]=0;
return gMsgBuf;
}
void ftpc_init(uint8_t * src_ip)
{
ftp.state = FTPS_NOT_LOGIN;
ftp.dsock_mode = ACTIVE_MODE;
local_ip.cVal[0] = src_ip[0];
local_ip.cVal[1] = src_ip[1];
local_ip.cVal[2] = src_ip[2];
local_ip.cVal[3] = src_ip[3];
local_port = 35000;
strcpy(ftp.workingdir, "/");
socket(CTRL_SOCK, Sn_MR_TCP, IPPORT_FTP, 0x0);
}
uint8_t ftpc_run(uint8_t * dbuf)
{
uint16_t size = 0;
long ret = 0;
uint32_t send_byte, recv_byte;
uint32_t blocklen;
uint32_t remain_filesize;
uint32_t remain_datasize;
uint8_t msg_c;
uint8_t dat[50]={0,};
uint32_t totalSize = 0, availableSize = 0;
switch(getSn_SR(CTRL_SOCK))
{
case SOCK_ESTABLISHED :
if(!connect_state_controlc){
printf("%d:FTP Connected\r\n", CTRL_SOCK);
strcpy(ftp.workingdir, "/");
connect_state_controlc = 1;
}
if(gMenuStart){
gMenuStart = 0;
printf("\r\n----------------------------------------\r\n");
printf("Press menu key\r\n");
printf("----------------------------------------\r\n");
printf("1> View FTP Server Directory\r\n");
printf("2> View My Directory\r\n");
printf("3> Sets the type of file to be transferred. Current state : %s\r\n", (gModeAsciiBinary==TransferAscii)?"Ascii":"Binary");
printf("4> Sets Data Connection. Current state : %s\r\n", (gModeActivePassive==ConnectActive)?"Active":"Passive");
printf("5> Put File to Server\r\n");
printf("6> Get File from Server\r\n");
printf("7> Delete My File\r\n");
printf("----------------------------------------\r\n");
while(1){
msg_c=Board_UARTGetCharBlocking();
if(msg_c=='1'){
if(gModeActivePassive==ConnectPassive){
sprintf(dat,"PASV\r\n");
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
Command.First = f_dir;
break;
}
else{
wiz_NetInfo gWIZNETINFO;
ctlnetwork(CN_GET_NETINFO, (void*) &gWIZNETINFO);
sprintf(dat,"PORT %d,%d,%d,%d,%d,%d\r\n", gWIZNETINFO.ip[0], gWIZNETINFO.ip[1], gWIZNETINFO.ip[2], gWIZNETINFO.ip[3], (uint8_t)(local_port>>8), (uint8_t)(local_port&0x00ff));
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
Command.First = f_dir;
gModeActivePassiveflag = 1;
break;
}
break;
}
else if(msg_c=='5'){
if(gModeActivePassive==ConnectPassive){
sprintf(dat,"PASV\r\n");
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
Command.First = f_put;
break;
}
else{
wiz_NetInfo gWIZNETINFO;
ctlnetwork(CN_GET_NETINFO, (void*) &gWIZNETINFO);
sprintf(dat,"PORT %d,%d,%d,%d,%d,%d\r\n", gWIZNETINFO.ip[0], gWIZNETINFO.ip[1], gWIZNETINFO.ip[2], gWIZNETINFO.ip[3], (uint8_t)(local_port>>8), (uint8_t)(local_port&0x00ff));
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
Command.First = f_put;
gModeActivePassiveflag = 1;
break;
}
}
else if(msg_c=='6'){
if(gModeActivePassive==ConnectPassive){
sprintf(dat,"PASV\r\n");
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
Command.First = f_get;
break;
}
else{
wiz_NetInfo gWIZNETINFO;
ctlnetwork(CN_GET_NETINFO, (void*) &gWIZNETINFO);
sprintf(dat,"PORT %d,%d,%d,%d,%d,%d\r\n", gWIZNETINFO.ip[0], gWIZNETINFO.ip[1], gWIZNETINFO.ip[2], gWIZNETINFO.ip[3], (uint8_t)(local_port>>8), (uint8_t)(local_port&0x00ff));
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
Command.First = f_get;
gModeActivePassiveflag = 1;
break;
}
}
else if(msg_c=='2'){
scan_files(ftp.workingdir, dbuf, (int *)&size);
printf("\r\n%s\r\n", dbuf);
getMountedMemorySize(SPI_FLASHM, &totalSize, &availableSize);
printf(" - Available Memory Size : %ld kB / %ld kB ( %ld kB is used )\r\n", availableSize, totalSize, (totalSize - availableSize));
gMenuStart = 1;
break;
}
else if(msg_c=='3'){
printf("1> ASCII\r\n");
printf("2> BINARY\r\n");
while(1){
msg_c=Board_UARTGetCharBlocking();
if(msg_c=='1'){
sprintf(dat,"TYPE %c\r\n", TransferAscii);
gModeAsciiBinary = TransferAscii;
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
}
else if(msg_c=='2'){
sprintf(dat,"TYPE %c\r\n", TransferBinary);
gModeAsciiBinary = TransferBinary;
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
}
else{
printf("\r\nRetry...\r\n");
}
}
break;
}
else if(msg_c=='4'){
printf("1> ACTIVE\r\n");
printf("2> PASSIVE\r\n");
while(1){
msg_c=Board_UARTGetCharBlocking();
if(msg_c=='1'){
gModeActivePassive = ConnectActive;
break;
}
else if(msg_c=='2'){
gModeActivePassive = ConnectPassive;
break;
}
else{
printf("\r\nRetry...\r\n");
}
}
gMenuStart = 1;
break;
}
else if(msg_c=='7'){
printf(">del filename?");
sprintf(ftp.filename, "/%s\r\n", User_Keyboard_MSG());
if (f_unlink((const char *)ftp.filename) != 0){
printf("\r\nCould not delete.\r\n");
}
else{
printf("\r\nDeleted.\r\n");
}
gMenuStart = 1;
break;
}
else{
printf("\r\nRetry...\r\n");
}
}
}
if(gDataSockReady){
gDataSockReady = 0;
switch(Command.First){
case f_dir:
sprintf(dat,"LIST\r\n");
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
case f_put:
printf(">put filename?");
sprintf(dat,"STOR %s\r\n", User_Keyboard_MSG());
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
case f_get:
printf(">get filename?");
sprintf(dat,"RETR %s\r\n", User_Keyboard_MSG());
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
default:
printf("shit!!!! Command.First = default\r\n");
break;
}
}
if((size = getSn_RX_RSR(CTRL_SOCK)) > 0){ // Don't need to check SOCKERR_BUSY because it doesn't not occur.
memset(dbuf, 0, _MAX_SS);
if(size > _MAX_SS) size = _MAX_SS - 1;
ret = recv(CTRL_SOCK,dbuf,size);
dbuf[ret] = '\0';
if(ret != size)
{
if(ret==SOCK_BUSY) return 0;
if(ret < 0){
printf("%d:recv() error:%ld\r\n",CTRL_SOCK,ret);
close(CTRL_SOCK);
return ret;
}
}
printf("Rcvd Command: %s\r\n", dbuf);
proc_ftpc((char *)dbuf);
}
break;
case SOCK_CLOSE_WAIT :
printf("%d:CloseWait\r\n",CTRL_SOCK);
if((ret=disconnect(CTRL_SOCK)) != SOCK_OK) return ret;
printf("%d:Closed\r\n",CTRL_SOCK);
break;
case SOCK_CLOSED :
printf("%d:FTPStart\r\n",CTRL_SOCK);
if((ret=socket(CTRL_SOCK, Sn_MR_TCP, IPPORT_FTP, 0x0)) != CTRL_SOCK){
printf("%d:socket() error:%ld\r\n", CTRL_SOCK, ret);
close(CTRL_SOCK);
return ret;
}
break;
case SOCK_INIT :
printf("%d:Opened\r\n",CTRL_SOCK);
if((ret = connect(CTRL_SOCK, FTP_destip, FTP_destport)) != SOCK_OK){
printf("%d:Connect error\r\n",CTRL_SOCK);
return ret;
}
connect_state_controlc = 0;
printf("%d:Connectting...\r\n",CTRL_SOCK);
break;
default :
break;
}
switch(getSn_SR(DATA_SOCK)){
case SOCK_ESTABLISHED :
if(!connect_state_datac){
printf("%d:FTP Data socket Connected\r\n", DATA_SOCK);
connect_state_datac = 1;
//gDataSockReady = 1;
}
if(gDataPutGetStart){
switch(Command.Second){
case s_dir:
printf("waitng...\r\n");
if((size = getSn_RX_RSR(DATA_SOCK)) > 0){ // Don't need to check SOCKERR_BUSY because it doesn't not occur.
printf("ok\r\n");
memset(dbuf, 0, _MAX_SS);
if(size > _MAX_SS) size = _MAX_SS - 1;
ret = recv(DATA_SOCK,dbuf,size);
dbuf[ret] = '\0';
if(ret != size){
if(ret==SOCK_BUSY) return 0;
if(ret < 0){
printf("%d:recv() error:%ld\r\n",CTRL_SOCK,ret);
close(DATA_SOCK);
return ret;
}
}
printf("Rcvd Data:\n\r%s\n\r", dbuf);
gDataPutGetStart = 0;
}
break;
case s_put:
printf("waitng...\r\n");
if(strlen(ftp.workingdir) == 1)
sprintf(ftp.filename, "/%s", (uint8_t *)gMsgBuf);
else
sprintf(ftp.filename, "%s/%s", ftp.workingdir, (uint8_t *)gMsgBuf);
ftp.fr = f_open(&(ftp.fil), (const char *)ftp.filename, FA_READ);
if(ftp.fr == FR_OK){
remain_filesize = ftp.fil.fsize;
printf("f_open return FR_OK\r\n");
do{
memset(dbuf, 0, _MAX_SS);
if(remain_filesize > _MAX_SS)
send_byte = _MAX_SS;
else
send_byte = remain_filesize;
ftp.fr = f_read(&(ftp.fil), (void *)dbuf, send_byte , (UINT *)&blocklen);
if(ftp.fr != FR_OK){
break;
}
printf("#");
send(DATA_SOCK, dbuf, blocklen);
remain_filesize -= blocklen;
}while(remain_filesize != 0);
printf("\r\nFile read finished\r\n");
ftp.fr = f_close(&(ftp.fil));
gDataPutGetStart = 0;
disconnect(DATA_SOCK);
}
else{
printf("File Open Error: %d\r\n", ftp.fr);
ftp.fr = f_close(&(ftp.fil));
gDataPutGetStart = 0;
disconnect(DATA_SOCK);
}
break;
case s_get:
printf("waitng...\r\n");
if(strlen(ftp.workingdir) == 1)
sprintf(ftp.filename, "/%s", (uint8_t *)gMsgBuf);
else
sprintf(ftp.filename, "%s/%s", ftp.workingdir, (uint8_t *)gMsgBuf);
ftp.fr = f_open(&(ftp.fil), (const char *)ftp.filename, FA_CREATE_ALWAYS | FA_WRITE);
if(ftp.fr == FR_OK){
printf("f_open return FR_OK\r\n");
while(1){
if((remain_datasize = getSn_RX_RSR(DATA_SOCK)) > 0){
while(1){
memset(dbuf, 0, _MAX_SS);
if(remain_datasize > _MAX_SS) recv_byte = _MAX_SS;
else recv_byte = remain_datasize;
ret = recv(DATA_SOCK, dbuf, recv_byte);
ftp.fr = f_write(&(ftp.fil), (const void *)dbuf, (UINT)ret, (UINT *)&blocklen);
remain_datasize -= blocklen;
if(ftp.fr != FR_OK){
printf("f_write failed\r\n");
break;
}
if(remain_datasize <= 0) break;
}
if(ftp.fr != FR_OK){
printf("f_write failed\r\n");
break;
}
printf("#");
}
else{
if(getSn_SR(DATA_SOCK) != SOCK_ESTABLISHED) break;
}
}
printf("\r\nFile write finished\r\n");
ftp.fr = f_close(&(ftp.fil));
gDataPutGetStart = 0;
}else{
printf("File Open Error: %d\r\n", ftp.fr);
}
break;
default:
printf("shit!!!! Command.Second = default\r\n");
break;
}
}
break;
case SOCK_CLOSE_WAIT :
printf("%d:CloseWait\r\n",DATA_SOCK);
if((ret=disconnect(DATA_SOCK)) != SOCK_OK) return ret;
printf("%d:Closed\r\n",DATA_SOCK);
break;
case SOCK_CLOSED :
if(ftp.dsock_state == DATASOCK_READY){
if(ftp.dsock_mode == PASSIVE_MODE){
printf("%d:FTPDataStart, port : %d\r\n",DATA_SOCK, local_port);
if((ret=socket(DATA_SOCK, Sn_MR_TCP, local_port, 0x0)) != DATA_SOCK){
printf("%d:socket() error:%ld\r\n", DATA_SOCK, ret);
close(DATA_SOCK);
return ret;
}
local_port++;
if(local_port > 50000)
local_port = 35000;
}else{
printf("%d:FTPDataStart, port : %d\r\n",DATA_SOCK, local_port);
if((ret=socket(DATA_SOCK, Sn_MR_TCP, local_port, 0x0)) != DATA_SOCK){
printf("%d:socket() error:%ld\r\n", DATA_SOCK, ret);
close(DATA_SOCK);
return ret;
}
local_port++;
if(local_port > 50000)
local_port = 35000;
}
ftp.dsock_state = DATASOCK_START;
}
break;
case SOCK_INIT :
printf("%d:Opened\r\n",DATA_SOCK);
if(ftp.dsock_mode == ACTIVE_MODE){
if( (ret = listen(DATA_SOCK)) != SOCK_OK){
printf("%d:Listen error\r\n",DATA_SOCK);
return ret;
}
gDataSockReady = 1;
printf("%d:Listen ok\r\n",DATA_SOCK);
}else{
if((ret = connect(DATA_SOCK, remote_ip.cVal, remote_port)) != SOCK_OK){
printf("%d:Connect error\r\n", DATA_SOCK);
return ret;
}
gDataSockReady = 1;
}
connect_state_datac = 0;
break;
default :
break;
}
return 0;
}
char proc_ftpc(char * buf)
{
uint16_t Responses;
uint8_t dat[30]={0,};
Responses =(buf[0]-'0')*100+(buf[1]-'0')*10+(buf[2]-'0');
switch(Responses){
case R_220:
printf("\r\nInput your User ID > ");
sprintf(dat,"USER %s\r\n", User_Keyboard_MSG());
printf("\r\n");
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
case R_331:
printf("\r\nInput your Password > ");
sprintf(dat,"PASS %s\r\n", User_Keyboard_MSG());
printf("\r\n");
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
case R_230:
printf("\r\nUser logged in, proceed\r\n");
sprintf(dat,"TYPE %c\r\n", TransferAscii);
gModeAsciiBinary = TransferAscii;
send(CTRL_SOCK, (uint8_t *)dat, strlen(dat));
break;
case R_200:
if((gModeActivePassive==ConnectActive)&&gModeActivePassiveflag){
ftp.dsock_mode = ACTIVE_MODE;
ftp.dsock_state = DATASOCK_READY;
gModeActivePassiveflag = 0;
}
else{
gMenuStart = 1;
}
break;
case R_150:
switch(Command.First){
case f_dir:
Command.Second = s_dir;
gDataPutGetStart = 1;
break;
case f_get:
Command.Second = s_get;
gDataPutGetStart = 1;
break;
case f_put:
Command.Second = s_put;
gDataPutGetStart = 1;
break;
default :
printf("shit!!!! Command.First = default\r\n");
break;
}
break;
case R_226: /* Closing data connection. File transfer/abort successful */
gMenuStart = 1;
break;
case R_227:
if (pportc(buf) == -1){
printf("Bad port syntax\r\n");
}
else{
printf("Go Open Data Sock...\r\n ");
ftp.dsock_mode = PASSIVE_MODE;
ftp.dsock_state = DATASOCK_READY;
}
break;
default:
printf("\r\nDefault Status = %d\r\n",(uint16_t)Responses);
gDataSockReady = 1;
break;
}
return 1;
}
int pportc(char * arg)
{
int i;
char* tok=0;
strtok(arg,"(");
for (i = 0; i < 4; i++)
{
if(i==0) tok = strtok(NULL,",\r\n");
else tok = strtok(NULL,",");
remote_ip.cVal[i] = (uint8_t)atoi(tok, 10);
if (!tok){
printf("bad pport : %s\r\n", arg);
return -1;
}
}
remote_port = 0;
for (i = 0; i < 2; i++){
tok = strtok(NULL,",\r\n");
remote_port <<= 8;
remote_port += atoi(tok, 10);
if (!tok){
printf("bad pport : %s\r\n", arg);
return -1;
}
}
printf("ip : %d.%d.%d.%d, port : %d\r\n", remote_ip.cVal[0], remote_ip.cVal[1], remote_ip.cVal[2], remote_ip.cVal[3], remote_port);
return 0;
}
#if defined(F_FILESYSTEM)
void print_filedsc(FIL *fil)
{
#if defined(_FTP_DEBUG_)
printf("File System pointer : %08X\r\n", fil->fs);
printf("File System mount ID : %d\r\n", fil->id);
printf("File status flag : %08X\r\n", fil->flag);
printf("File System pads : %08X\r\n", fil->err);
printf("File read write pointer : %08X\r\n", fil->fptr);
printf("File size : %08X\r\n", fil->fsize);
printf("File start cluster : %08X\r\n", fil->sclust);
printf("current cluster : %08X\r\n", fil->clust);
printf("current data sector : %08X\r\n", fil->dsect);
printf("dir entry sector : %08X\r\n", fil->dir_sect);
printf("dir entry pointer : %08X\r\n", fil->dir_ptr);
#endif
}
#endif

99
Internet/FTPClient/ftpc.h Normal file
View File

@ -0,0 +1,99 @@
#ifndef _FTPC_H_
#define _FTPC_H_
#include <stdint.h>
#define F_FILESYSTEM // If your target support a file system, you have to activate this feature and implement.
#if defined(F_FILESYSTEM)
#include "ff.h"
#endif
#define F_APP_FTP
#define _FTP_DEBUG_
#define F_APP_FTP_CLIENT
#define LINELEN 100
//#define DATA_BUF_SIZE 100
#if !defined(F_FILESYSTEM)
#define _MAX_SS 512
#endif
#define CTRL_SOCK 2
#define DATA_SOCK 3
#define IPPORT_FTPD 20 /* FTP Data port */
#define IPPORT_FTP 21 /* FTP Control port */
#define HOSTNAME "iinChip"
#define VERSION "1.0"
#define FILENAME "a.txt"
enum ftp_type {
ASCII_TYPE,
IMAGE_TYPE,
LOGICAL_TYPE
};
enum ftp_state {
FTPS_NOT_LOGIN,
FTPS_LOGIN
};
enum datasock_state{
DATASOCK_IDLE,
DATASOCK_READY,
DATASOCK_START
};
enum datasock_mode{
PASSIVE_MODE,
ACTIVE_MODE
};
struct ftpd {
uint8_t control; /* Control stream */
uint8_t data; /* Data stream */
enum ftp_type type; /* Transfer type */
enum ftp_state state;
enum datasock_state dsock_state;
enum datasock_mode dsock_mode;
char username[LINELEN]; /* Arg to USER command */
char workingdir[LINELEN];
char filename[LINELEN];
#if defined(F_FILESYSTEM)
FIL fil; // FatFs File objects
FRESULT fr; // FatFs function common result code
#endif
};
#ifndef un_I2cval
typedef union _un_l2cval {
uint32_t lVal;
uint8_t cVal[4];
}un_l2cval;
#endif
void ftpc_init(uint8_t * src_ip);
uint8_t ftpc_run(uint8_t * dbuf);
char proc_ftpc(char * buf);
int pportc(char * arg);
uint8_t* User_Keyboard_MSG();
int sendit(char * command);
int recvit(char * command);
long sendfile(uint8_t s, char * command);
long recvfile(uint8_t s);
#if defined(F_FILESYSTEM)
void print_filedsc(FIL *fil);
#endif
#endif // _FTPC_H_

View File

@ -0,0 +1,67 @@
/* Copyright (c) 2002, Joerg Wunsch
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of the copyright holders nor the names of
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/* $Id: stdio_private.h,v 1.6 2003/01/07 22:17:24 joerg_wunsch Exp $ */
#include <stdint.h>
#include <stdio.h>
struct __file {
char *buf; /* buffer pointer */
unsigned char unget; /* ungetc() buffer */
uint8_t flags; /* flags, see below */
#define __SRD 0x0001 /* OK to read */
#define __SWR 0x0002 /* OK to write */
#define __SSTR 0x0004 /* this is an sprintf/snprintf string */
#define __SPGM 0x0008 /* fmt string is in progmem */
#define __SERR 0x0010 /* found error */
#define __SEOF 0x0020 /* found EOF */
#define __SUNGET 0x040 /* ungetc() happened */
#if 0
/* possible future extensions, will require uint16_t flags */
#define __SRW 0x0080 /* open for reading & writing */
#define __SLBF 0x0100 /* line buffered */
#define __SNBF 0x0200 /* unbuffered */
#define __SMBF 0x0400 /* buf is from malloc */
#endif
int size; /* size of buffer */
int len; /* characters read or written so far */
int (*put)(char); /* function to write one char to device */
int (*get)(void); /* function to read one char from device */
};
/* values for PRINTF_LEVEL */
#define PRINTF_MIN 1
#define PRINTF_STD 2
#define PRINTF_FLT 3
/* values for SCANF_LEVEL */
#define SCANF_MIN 1
#define SCANF_STD 2
#define SCANF_FLT 3

View File

@ -147,7 +147,7 @@ uint8_t ftpd_run(uint8_t * dbuf)
{
uint16_t size = 0, i;
long ret = 0;
uint16_t blocklen, send_byte, recv_byte;
uint32_t blocklen, send_byte, recv_byte;
uint32_t remain_filesize;
uint32_t remain_datasize;
#if defined(F_FILESYSTEM)
@ -322,7 +322,7 @@ uint8_t ftpd_run(uint8_t * dbuf)
else
send_byte = remain_filesize;
ftp.fr = f_read(&(ftp.fil), dbuf, send_byte , (void *)&blocklen);
ftp.fr = f_read(&(ftp.fil), dbuf, send_byte , &blocklen);
if(ftp.fr != FR_OK)
break;
#if defined(_FTP_DEBUG_)
@ -389,7 +389,7 @@ uint8_t ftpd_run(uint8_t * dbuf)
//printf("----->fn:%s data:%s \r\n", ftp.filename, dbuf);
#endif
ftp.fr = f_write(&(ftp.fil), dbuf, (UINT)ret, (void *)&blocklen);
ftp.fr = f_write(&(ftp.fil), dbuf, (UINT)ret, &blocklen);
#if defined(_FTP_DEBUG_)
//printf("----->dsize:%d recv:%d len:%d \r\n", remain_datasize, ret, blocklen);
#endif

448
Internet/SNTP/sntp.c Normal file
View File

@ -0,0 +1,448 @@
/*
* sntp.c
*
* Created on: 2014. 12. 15.
* Author: Administrator
*/
#include <string.h>
#include "sntp.h"
#include "socket.h"
ntpformat NTPformat;
datetime Nowdatetime;
uint8_t ntpmessage[48];
uint8_t *data_buf;
uint8_t NTP_SOCKET;
uint8_t time_zone;
uint16_t ntp_retry_cnt=0; //counting the ntp retry number
/*
00)UTC-12:00 Baker Island, Howland Island (both uninhabited)
01) UTC-11:00 American Samoa, Samoa
02) UTC-10:00 (Summer)French Polynesia (most), United States (Aleutian Islands, Hawaii)
03) UTC-09:30 Marquesas Islands
04) UTC-09:00 Gambier Islands;(Summer)United States (most of Alaska)
05) UTC-08:00 (Summer)Canada (most of British Columbia), Mexico (Baja California)
06) UTC-08:00 United States (California, most of Nevada, most of Oregon, Washington (state))
07) UTC-07:00 Mexico (Sonora), United States (Arizona); (Summer)Canada (Alberta)
08) UTC-07:00 Mexico (Chihuahua), United States (Colorado)
09) UTC-06:00 Costa Rica, El Salvador, Ecuador (Galapagos Islands), Guatemala, Honduras
10) UTC-06:00 Mexico (most), Nicaragua;(Summer)Canada (Manitoba, Saskatchewan), United States (Illinois, most of Texas)
11) UTC-05:00 Colombia, Cuba, Ecuador (continental), Haiti, Jamaica, Panama, Peru
12) UTC-05:00 (Summer)Canada (most of Ontario, most of Quebec)
13) UTC-05:00 United States (most of Florida, Georgia, Massachusetts, most of Michigan, New York, North Carolina, Ohio, Washington D.C.)
14) UTC-04:30 Venezuela
15) UTC-04:00 Bolivia, Brazil (Amazonas), Chile (continental), Dominican Republic, Canada (Nova Scotia), Paraguay,
16) UTC-04:00 Puerto Rico, Trinidad and Tobago
17) UTC-03:30 Canada (Newfoundland)
18) UTC-03:00 Argentina; (Summer) Brazil (Brasilia, Rio de Janeiro, Sao Paulo), most of Greenland, Uruguay
19) UTC-02:00 Brazil (Fernando de Noronha), South Georgia and the South Sandwich Islands
20) UTC-01:00 Portugal (Azores), Cape Verde
21) UTC&#177;00:00 Cote d'Ivoire, Faroe Islands, Ghana, Iceland, Senegal; (Summer) Ireland, Portugal (continental and Madeira)
22) UTC&#177;00:00 Spain (Canary Islands), Morocco, United Kingdom
23) UTC+01:00 Angola, Cameroon, Nigeria, Tunisia; (Summer)Albania, Algeria, Austria, Belgium, Bosnia and Herzegovina,
24) UTC+01:00 Spain (continental), Croatia, Czech Republic, Denmark, Germany, Hungary, Italy, Kinshasa, Kosovo,
25) UTC+01:00 Macedonia, France (metropolitan), the Netherlands, Norway, Poland, Serbia, Slovakia, Slovenia, Sweden, Switzerland
26) UTC+02:00 Libya, Egypt, Malawi, Mozambique, South Africa, Zambia, Zimbabwe, (Summer)Bulgaria, Cyprus, Estonia,
27) UTC+02:00 Finland, Greece, Israel, Jordan, Latvia, Lebanon, Lithuania, Moldova, Palestine, Romania, Syria, Turkey, Ukraine
28) UTC+03:00 Belarus, Djibouti, Eritrea, Ethiopia, Iraq, Kenya, Madagascar, Russia (Kaliningrad Oblast), Saudi Arabia,
29) UTC+03:00 South Sudan, Sudan, Somalia, South Sudan, Tanzania, Uganda, Yemen
30) UTC+03:30 (Summer)Iran
31) UTC+04:00 Armenia, Azerbaijan, Georgia, Mauritius, Oman, Russia (European), Seychelles, United Arab Emirates
32) UTC+04:30 Afghanistan
33) UTC+05:00 Kazakhstan (West), Maldives, Pakistan, Uzbekistan
34) UTC+05:30 India, Sri Lanka
35) UTC+05:45 Nepal
36) UTC+06:00 Kazakhstan (most), Bangladesh, Russia (Ural: Sverdlovsk Oblast, Chelyabinsk Oblast)
37) UTC+06:30 Cocos Islands, Myanmar
38) UTC+07:00 Jakarta, Russia (Novosibirsk Oblast), Thailand, Vietnam
39) UTC+08:00 China, Hong Kong, Russia (Krasnoyarsk Krai), Malaysia, Philippines, Singapore, Taiwan, most of Mongolia, Western Australia
40) UTC+09:00 Korea, East Timor, Russia (Irkutsk Oblast), Japan
41) UTC+09:30 Australia (Northern Territory);(Summer)Australia (South Australia))
42) UTC+10:00 Russia (Zabaykalsky Krai); (Summer)Australia (New South Wales, Queensland, Tasmania, Victoria)
43) UTC+10:30 Lord Howe Island
44) UTC+11:00 New Caledonia, Russia (Primorsky Krai), Solomon Islands
45) UTC+11:30 Norfolk Island
46) UTC+12:00 Fiji, Russia (Kamchatka Krai);(Summer)New Zealand
47) UTC+12:45 (Summer)New Zealand
48) UTC+13:00 Tonga
49) UTC+14:00 Kiribati (Line Islands)
*/
void get_seconds_from_ntp_server(uint8_t *buf, uint16_t idx)
{
tstamp seconds = 0;
uint8_t i=0;
for (i = 0; i < 4; i++)
{
seconds = (seconds << 8) | buf[idx + i];
}
switch (time_zone)
{
case 0:
seconds -= 12*3600;
break;
case 1:
seconds -= 11*3600;
break;
case 2:
seconds -= 10*3600;
break;
case 3:
seconds -= (9*3600+30*60);
break;
case 4:
seconds -= 9*3600;
break;
case 5:
case 6:
seconds -= 8*3600;
break;
case 7:
case 8:
seconds -= 7*3600;
break;
case 9:
case 10:
seconds -= 6*3600;
break;
case 11:
case 12:
case 13:
seconds -= 5*3600;
break;
case 14:
seconds -= (4*3600+30*60);
break;
case 15:
case 16:
seconds -= 4*3600;
break;
case 17:
seconds -= (3*3600+30*60);
break;
case 18:
seconds -= 3*3600;
break;
case 19:
seconds -= 2*3600;
break;
case 20:
seconds -= 1*3600;
break;
case 21: //
case 22:
break;
case 23:
case 24:
case 25:
seconds += 1*3600;
break;
case 26:
case 27:
seconds += 2*3600;
break;
case 28:
case 29:
seconds += 3*3600;
break;
case 30:
seconds += (3*3600+30*60);
break;
case 31:
seconds += 4*3600;
break;
case 32:
seconds += (4*3600+30*60);
break;
case 33:
seconds += 5*3600;
break;
case 34:
seconds += (5*3600+30*60);
break;
case 35:
seconds += (5*3600+45*60);
break;
case 36:
seconds += 6*3600;
break;
case 37:
seconds += (6*3600+30*60);
break;
case 38:
seconds += 7*3600;
break;
case 39:
seconds += 8*3600;
break;
case 40:
seconds += 9*3600;
break;
case 41:
seconds += (9*3600+30*60);
break;
case 42:
seconds += 10*3600;
break;
case 43:
seconds += (10*3600+30*60);
break;
case 44:
seconds += 11*3600;
break;
case 45:
seconds += (11*3600+30*60);
break;
case 46:
seconds += 12*3600;
break;
case 47:
seconds += (12*3600+45*60);
break;
case 48:
seconds += 13*3600;
break;
case 49:
seconds += 14*3600;
break;
}
//calculation for date
calcdatetime(seconds);
}
void SNTP_init(uint8_t s, uint8_t *ntp_server, uint8_t tz, uint8_t *buf)
{
NTPformat.dstaddr[0] = ntp_server[0];
NTPformat.dstaddr[1] = ntp_server[1];
NTPformat.dstaddr[2] = ntp_server[2];
NTPformat.dstaddr[3] = ntp_server[3];
time_zone = tz;
data_buf = buf;
uint8_t Flag;
NTPformat.leap = 0; /* leap indicator */
NTPformat.version = 4; /* version number */
NTPformat.mode = 3; /* mode */
NTPformat.stratum = 0; /* stratum */
NTPformat.poll = 0; /* poll interval */
NTPformat.precision = 0; /* precision */
NTPformat.rootdelay = 0; /* root delay */
NTPformat.rootdisp = 0; /* root dispersion */
NTPformat.refid = 0; /* reference ID */
NTPformat.reftime = 0; /* reference time */
NTPformat.org = 0; /* origin timestamp */
NTPformat.rec = 0; /* receive timestamp */
NTPformat.xmt = 1; /* transmit timestamp */
Flag = (NTPformat.leap<<6)+(NTPformat.version<<3)+NTPformat.mode; //one byte Flag
memcpy(ntpmessage,(void const*)(&Flag),1);
}
int8_t SNTP_run(datetime *time)
{
uint16_t RSR_len;
uint32_t destip = 0;
uint16_t destport;
uint16_t startindex = 40; //last 8-byte of data_buf[size is 48 byte] is xmt, so the startindex should be 40
switch(getSn_SR(NTP_SOCKET))
{
case SOCK_UDP:
if ((RSR_len = getSn_RX_RSR(NTP_SOCKET)) > 0)
{
if (RSR_len > MAX_SNTP_BUF_SIZE) RSR_len = MAX_SNTP_BUF_SIZE; // if Rx data size is lager than TX_RX_MAX_BUF_SIZE
recvfrom(NTP_SOCKET, data_buf, RSR_len, (uint8_t *)&destip, &destport);
get_seconds_from_ntp_server(data_buf,startindex);
time->yy = Nowdatetime.yy;
time->mo = Nowdatetime.mo;
time->dd = Nowdatetime.dd;
time->hh = Nowdatetime.hh;
time->mm = Nowdatetime.mm;
time->ss = Nowdatetime.ss;
ntp_retry_cnt=0;
close(NTP_SOCKET);
return 1;
}
if(ntp_retry_cnt<0xFFFF)
{
if(ntp_retry_cnt==0)//first send request, no need to wait
{
sendto(NTP_SOCKET,ntpmessage,sizeof(ntpmessage),NTPformat.dstaddr,ntp_port);
ntp_retry_cnt++;
}
else // send request again? it should wait for a while
{
if((ntp_retry_cnt % 0xFFF) == 0) //wait time
{
sendto(NTP_SOCKET,ntpmessage,sizeof(ntpmessage),NTPformat.dstaddr,ntp_port);
#ifdef _SNTP_DEBUG_
printf("ntp retry: %d\r\n", ntp_retry_cnt);
#endif
ntp_retry_cnt++;
}
}
}
else //ntp retry fail
{
ntp_retry_cnt=0;
#ifdef _SNTP_DEBUG_
printf("ntp retry failed!\r\n");
#endif
close(NTP_SOCKET);
}
break;
case SOCK_CLOSED:
socket(NTP_SOCKET,Sn_MR_UDP,ntp_port,0);
break;
}
// Return value
// 0 - failed / 1 - success
return 0;
}
void calcdatetime(tstamp seconds)
{
uint8_t yf=0;
tstamp n=0,d=0,total_d=0,rz=0;
uint16_t y=0,r=0,yr=0;
signed long long yd=0;
n = seconds;
total_d = seconds/(SECS_PERDAY);
d=0;
uint32_t p_year_total_sec=SECS_PERDAY*365;
uint32_t r_year_total_sec=SECS_PERDAY*366;
while(n>=p_year_total_sec)
{
if((EPOCH+r)%400==0 || ((EPOCH+r)%100!=0 && (EPOCH+r)%4==0))
{
n = n -(r_year_total_sec);
d = d + 366;
}
else
{
n = n - (p_year_total_sec);
d = d + 365;
}
r+=1;
y+=1;
}
y += EPOCH;
Nowdatetime.yy = y;
yd=0;
yd = total_d - d;
yf=1;
while(yd>=28)
{
if(yf==1 || yf==3 || yf==5 || yf==7 || yf==8 || yf==10 || yf==12)
{
yd -= 31;
if(yd<0)break;
rz += 31;
}
if (yf==2)
{
if (y%400==0 || (y%100!=0 && y%4==0))
{
yd -= 29;
if(yd<0)break;
rz += 29;
}
else
{
yd -= 28;
if(yd<0)break;
rz += 28;
}
}
if(yf==4 || yf==6 || yf==9 || yf==11 )
{
yd -= 30;
if(yd<0)break;
rz += 30;
}
yf += 1;
}
Nowdatetime.mo=yf;
yr = total_d-d-rz;
yr += 1;
Nowdatetime.dd=yr;
//calculation for time
seconds = seconds%SECS_PERDAY;
Nowdatetime.hh = seconds/3600;
Nowdatetime.mm = (seconds%3600)/60;
Nowdatetime.ss = (seconds%3600)%60;
}
tstamp changedatetime_to_seconds(void)
{
tstamp seconds=0;
uint32_t total_day=0;
uint16_t i=0,run_year_cnt=0,l=0;
l = Nowdatetime.yy;//low
for(i=EPOCH;i<l;i++)
{
if((i%400==0) || ((i%100!=0) && (i%4==0)))
{
run_year_cnt += 1;
}
}
total_day=(l-EPOCH-run_year_cnt)*365+run_year_cnt*366;
for(i=1;i<=Nowdatetime.mo;i++)
{
if(i==5 || i==7 || i==10 || i==12)
{
total_day += 30;
}
if (i==3)
{
if (l%400==0 && l%100!=0 && l%4==0)
{
total_day += 29;
}
else
{
total_day += 28;
}
}
if(i==2 || i==4 || i==6 || i==8 || i==9 || i==11)
{
total_day += 31;
}
}
seconds = (total_day+Nowdatetime.dd-1)*24*3600;
seconds += Nowdatetime.ss;//seconds
seconds += Nowdatetime.mm*60;//minute
seconds += Nowdatetime.hh*3600;//hour
return seconds;
}

68
Internet/SNTP/sntp.h Normal file
View File

@ -0,0 +1,68 @@
/*
* sntp.h
*
* Created on: 2014. 12. 15.
* Author: Administrator
*/
#ifndef SNTP_H_
#define SNTP_H_
#include <stdint.h>
/*
* @brief Define it for Debug & Monitor DNS processing.
* @note If defined, it dependens on <stdio.h>
*/
//#define _SNTP_DEBUG_
#define MAX_SNTP_BUF_SIZE sizeof(ntpformat) ///< maximum size of DNS buffer. */
/* for ntpclient */
typedef signed char s_char;
typedef unsigned long long tstamp;
typedef unsigned int tdist;
typedef struct _ntpformat
{
uint8_t dstaddr[4]; /* destination (local) address */
char version; /* version number */
char leap; /* leap indicator */
char mode; /* mode */
char stratum; /* stratum */
char poll; /* poll interval */
s_char precision; /* precision */
tdist rootdelay; /* root delay */
tdist rootdisp; /* root dispersion */
char refid; /* reference ID */
tstamp reftime; /* reference time */
tstamp org; /* origin timestamp */
tstamp rec; /* receive timestamp */
tstamp xmt; /* transmit timestamp */
} ntpformat;
typedef struct _datetime
{
uint16_t yy;
uint8_t mo;
uint8_t dd;
uint8_t hh;
uint8_t mm;
uint8_t ss;
} datetime;
#define ntp_port 123 //ntp server port number
#define SECS_PERDAY 86400UL // seconds in a day = 60*60*24
#define UTC_ADJ_HRS 9 // SEOUL : GMT+9
#define EPOCH 1900 // NTP start year
void get_seconds_from_ntp_server(uint8_t *buf, uint16_t idx);
void SNTP_init(uint8_t s, uint8_t *ntp_server, uint8_t tz, uint8_t *buf);
int8_t SNTP_run(datetime *time);
tstamp changedatetime_to_seconds(void);
void calcdatetime(tstamp seconds);
#endif /* SNTP_H_ */

158
Internet/TFTP/netutil.c Normal file
View File

@ -0,0 +1,158 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "netutil.h"
/**
* Convert a 32bit Address into a Dotted Decimal Format string.
*
* @param addr 32bit address.
* @return Dotted Decimal Format string.
*/
int8_t* inet_ntoa(uint32_t addr)
{
static int8_t addr_str[16];
memset(addr_str,0,16);
sprintf((char*)addr_str,"%d.%d.%d.%d",(int32_t)(addr>>24 & 0xFF),(int32_t)(addr>>16 & 0xFF),(int32_t)(addr>>8 & 0xFF),(int32_t)(addr & 0xFF));
return addr_str;
}
/**
* Convert a 32bit Address into a Dotted Decimal Format string.
* This is differ from inet_ntoa in fixed length.
*
* @param addr 32bit address.
* @return Dotted Decimal Format string.
*/
int8_t* inet_ntoa_pad(uint32_t addr)
{
static int8_t addr_str[16];
memset(addr_str,0,16);
sprintf((char*)addr_str,"%03d.%03d.%03d.%03d",(int32_t)(addr>>24 & 0xFF),(int32_t)(addr>>16 & 0xFF),(int32_t)(addr>>8 & 0xFF),(int32_t)(addr & 0xFF));
return addr_str;
}
/**
* Converts a string containing an (Ipv4) Internet Protocol decimal dotted address into a 32bit address.
*
* @param addr Dotted Decimal Format string.
* @return 32bit address.
*/
uint32_t inet_addr(uint8_t* addr)
{
int8_t i;
uint32_t inetaddr = 0;
int8_t taddr[30];
int8_t * nexttok;
int32_t num;
strcpy((char*)taddr,(char*)addr);
nexttok = taddr;
for(i = 0; i < 4 ; i++)
{
nexttok = (int8_t*)strtok((char*)nexttok,".");
if(nexttok[0] == '0' && nexttok[1] == 'x') num = strtol((char*)nexttok+2, NULL, 16);
else num = strtol((char*)nexttok, NULL, 10);
inetaddr = inetaddr << 8;
inetaddr |= (num & 0xFF);
nexttok = NULL;
}
return inetaddr;
}
/**
* Swap the byte order of 16bit(short) wide variable.
*
* @param i 16bit value to swap
* @return Swapped value
*/
uint16_t swaps(uint16_t i)
{
uint16_t ret=0;
ret = (i & 0xFF) << 8;
ret |= ((i >> 8)& 0xFF);
return ret;
}
/**
* Swap the byte order of 32bit(long) wide variable.
*
* @param l 32bit value to convert
* @return Swapped value
*/
uint32_t swapl(uint32_t l)
{
uint32_t ret=0;
ret = (l & 0xFF) << 24;
ret |= ((l >> 8) & 0xFF) << 16;
ret |= ((l >> 16) & 0xFF) << 8;
ret |= ((l >> 24) & 0xFF);
return ret;
}
/**
* htons function converts a unsigned short from host to TCP/IP network byte order (which is big-endian).
*
* @param hostshort The value to convert.
* @return The value in TCP/IP network byte order.
*/
uint16_t htons(uint16_t hostshort)
{
#ifdef SYSTEM_LITTLE_ENDIAN
return swaps(hostshort);
#else
return hostshort;
#endif
}
/**
* htonl function converts a unsigned long from host to TCP/IP network byte order (which is big-endian).
*
* @param hostlong The value to convert.
* @return The value in TCP/IP network byte order.
*/
uint32_t htonl(uint32_t hostlong)
{
#ifdef SYSTEM_LITTLE_ENDIAN
return swapl(hostlong);
#else
return hostlong;
#endif
}
/**
* ntohs function converts a unsigned short from TCP/IP network byte order
* to host byte order (which is little-endian on Intel processors).
*
* @param netshort The value to convert.
* @return A 16-bit number in host byte order
*/
uint32_t ntohs(uint16_t netshort)
{
#ifdef SYSTEM_LITTLE_ENDIAN
return htons(netshort);
#else
return netshort;
#endif
}
/**
* converts a unsigned long from TCP/IP network byte order to host byte order
* (which is little-endian on Intel processors).
*
* @param netlong The value to convert.
* @return A 16-bit number in host byte order
*/
uint32_t ntohl(uint32_t netlong)
{
#ifdef SYSTEM_LITTLE_ENDIAN
return swapl(netlong);
#else
return netlong;
#endif
}
/**
* @}
*/

19
Internet/TFTP/netutil.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef __NETUTIL_H__
#define __NETUTIL_H__
#include <stdint.h>
#define SYSTEM_LITTLE_ENDIAN
int8_t* inet_ntoa(uint32_t addr);
int8_t* inet_ntoa_pad(uint32_t addr);
uint32_t inet_addr(uint8_t* addr);
uint16_t swaps(uint16_t i);
uint32_t swapl(uint32_t l);
uint16_t htons(uint16_t hostshort);
uint32_t htonl(uint32_t hostlong);
uint32_t ntohs(uint16_t netshort);
uint32_t ntohl(uint32_t netlong);
#endif

660
Internet/TFTP/tftp.c Normal file
View File

@ -0,0 +1,660 @@
/**
* @file tftp.c
* @brief TFTP Source File.
* @version 0.1.0
* @author Sang-sik Kim
*/
/* Includes -----------------------------------------------------*/
#include <string.h>
#include "tftp.h"
#include "socket.h"
#include "netutil.h"
/* define -------------------------------------------------------*/
/* typedef ------------------------------------------------------*/
/* Extern Variable ----------------------------------------------*/
/* Extern Functions ---------------------------------------------*/
#ifdef F_STORAGE
extern void save_data(uint8_t *data, uint32_t data_len, uint16_t block_number);
#endif
/* Global Variable ----------------------------------------------*/
static int g_tftp_socket = -1;
static uint8_t g_filename[FILE_NAME_SIZE];
static uint32_t g_server_ip = 0;
static uint16_t g_server_port = 0;
static uint16_t g_local_port = 0;
static uint32_t g_tftp_state = STATE_NONE;
static uint16_t g_block_num = 0;
static uint32_t g_timeout = 5;
static uint32_t g_resend_flag = 0;
static uint32_t tftp_time_cnt = 0;
static uint32_t tftp_retry_cnt = 0;
static uint8_t *g_tftp_rcv_buf = NULL;
static TFTP_OPTION default_tftp_opt = {
.code = (uint8_t *)"timeout",
.value = (uint8_t *)"5"
};
uint8_t g_progress_state = TFTP_PROGRESS;
#ifdef __TFTP_DEBUG__
int dbg_level = (INFO_DBG | ERROR_DBG | IPC_DBG);
#endif
/* static function define ---------------------------------------*/
static void set_filename(uint8_t *file, uint32_t file_size)
{
memcpy(g_filename, file, file_size);
}
static inline void set_server_ip(uint32_t ipaddr)
{
g_server_ip = ipaddr;
}
static inline uint32_t get_server_ip()
{
return g_server_ip;
}
static inline void set_server_port(uint16_t port)
{
g_server_port = port;
}
static inline uint16_t get_server_port()
{
return g_server_port;
}
static inline void set_local_port(uint16_t port)
{
g_local_port = port;
}
static inline uint16_t get_local_port()
{
return g_local_port;
}
static inline uint16_t genernate_port()
{
/* TODO */
return 0;
}
static inline void set_tftp_state(uint32_t state)
{
g_tftp_state = state;
}
static inline uint32_t get_tftp_state()
{
return g_tftp_state;
}
static inline void set_tftp_timeout(uint32_t timeout)
{
g_timeout = timeout;
}
static inline uint32_t get_tftp_timeout()
{
return g_timeout;
}
static inline void set_block_number(uint16_t block_number)
{
g_block_num = block_number;
}
static inline uint16_t get_block_number()
{
return g_block_num;
}
static int open_tftp_socket(uint8_t sock)
{
uint8_t sd, sck_state;
sd = socket(sock, Sn_MR_UDP, 51000, SF_IO_NONBLOCK);
if(sd != sock) {
//DBG_PRINT(ERROR_DBG, "[%s] socket error\r\n", __func__);
return -1;
}
do {
getsockopt(sd , SO_STATUS, &sck_state);
} while(sck_state != SOCK_UDP);
return sd;
}
static int send_udp_packet(int socket, uint8_t *packet, uint32_t len, uint32_t ip, uint16_t port)
{
int snd_len;
ip = htonl(ip);
snd_len = sendto(socket, packet, len, (uint8_t *)&ip, port);
if(snd_len != len) {
//DBG_PRINT(ERROR_DBG, "[%s] sendto error\r\n", __func__);
return -1;
}
return snd_len;
}
static int recv_udp_packet(int socket, uint8_t *packet, uint32_t len, uint32_t *ip, uint16_t *port)
{
int ret;
uint8_t sck_state;
uint16_t recv_len;
/* Receive Packet Process */
ret = getsockopt(socket, SO_STATUS, &sck_state);
if(ret != SOCK_OK) {
//DBG_PRINT(ERROR_DBG, "[%s] getsockopt SO_STATUS error\r\n", __func__);
return -1;
}
if(sck_state == SOCK_UDP) {
ret = getsockopt(socket, SO_RECVBUF, &recv_len);
if(ret != SOCK_OK) {
//DBG_PRINT(ERROR_DBG, "[%s] getsockopt SO_RECVBUF error\r\n", __func__);
return -1;
}
if(recv_len) {
recv_len = recvfrom(socket, packet, len, (uint8_t *)ip, port);
if(recv_len < 0) {
//DBG_PRINT(ERROR_DBG, "[%s] recvfrom error\r\n", __func__);
return -1;
}
*ip = ntohl(*ip);
return recv_len;
}
}
return -1;
}
static void close_tftp_socket(int socket)
{
close(socket);
}
static void init_tftp(void)
{
g_filename[0] = 0;
set_server_ip(0);
set_server_port(0);
set_local_port(0);
set_tftp_state(STATE_NONE);
set_block_number(0);
/* timeout flag */
g_resend_flag = 0;
tftp_retry_cnt = tftp_time_cnt = 0;
g_progress_state = TFTP_PROGRESS;
}
static void tftp_cancel_timeout(void)
{
if(g_resend_flag) {
g_resend_flag = 0;
tftp_retry_cnt = tftp_time_cnt = 0;
}
}
static void tftp_reg_timeout()
{
if(g_resend_flag == 0) {
g_resend_flag = 1;
tftp_retry_cnt = tftp_time_cnt = 0;
}
}
static void process_tftp_option(uint8_t *msg, uint32_t msg_len)
{
/* TODO Option Process */
}
static void send_tftp_rrq(uint8_t *filename, uint8_t *mode, TFTP_OPTION *opt, uint8_t opt_len)
{
uint8_t snd_buf[MAX_MTU_SIZE];
uint8_t *pkt = snd_buf;
uint32_t i, len;
*((uint16_t *)pkt) = htons(TFTP_RRQ);
pkt += 2;
strcpy((char *)pkt, (const char *)filename);
pkt += strlen((char *)filename) + 1;
strcpy((char *)pkt, (const char *)mode);
pkt += strlen((char *)mode) + 1;
for(i = 0 ; i < opt_len ; i++) {
strcpy((char *)pkt, (const char *)opt[i].code);
pkt += strlen((char *)opt[i].code) + 1;
strcpy((char *)pkt, (const char *)opt[i].value);
pkt += strlen((char *)opt[i].value) + 1;
}
len = pkt - snd_buf;
send_udp_packet(g_tftp_socket, snd_buf, len, get_server_ip(), TFTP_SERVER_PORT);
set_tftp_state(STATE_RRQ);
set_filename(filename, strlen((char *)filename) + 1);
tftp_reg_timeout();
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, ">> TFTP RRQ : FileName(%s), Mode(%s)\r\n", filename, mode);
#endif
}
#if 0 // 2014.07.01 sskim
static void send_tftp_wrq(uint8_t *filename, uint8_t *mode, TFTP_OPTION *opt, uint8_t opt_len)
{
uint8_t snd_buf[MAX_MTU_SIZE];
uint8_t *pkt = snd_buf;
uint32_t i, len;
*((uint16_t *)pkt) = htons((uint16_t)TFTP_WRQ);
pkt += 2;
strcpy((char *)pkt, (const char *)filename);
pkt += strlen((char *)filename) + 1;
strcpy((char *)pkt, (const char *)mode);
pkt += strlen((char *)mode) + 1;
for(i = 0 ; i < opt_len ; i++) {
strcpy((char *)pkt, (const char *)opt[i].code);
pkt += strlen((char *)opt[i].code) + 1;
strcpy((char *)pkt, (const char *)opt[i].value);
pkt += strlen((char *)opt[i].value) + 1;
}
len = pkt - snd_buf;
send_udp_packet(g_tftp_socket , snd_buf, len, get_server_ip(), TFTP_SERVER_PORT);
set_tftp_state(STATE_WRQ);
set_filename(filename, strlen((char *)filename) + 1);
tftp_reg_timeout();
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, ">> TFTP WRQ : FileName(%s), Mode(%s)\r\n", filename, mode);
#endif
}
#endif
#if 0 // 2014.07.01 sskim
static void send_tftp_data(uint16_t block_number, uint8_t *data, uint16_t data_len)
{
uint8_t snd_buf[MAX_MTU_SIZE];
uint8_t *pkt = snd_buf;
uint32_t len;
*((uint16_t *)pkt) = htons((uint16_t)TFTP_DATA);
pkt += 2;
*((uint16_t *)pkt) = htons(block_number);
pkt += 2;
memcpy(pkt, data, data_len);
pkt += data_len;
len = pkt - snd_buf;
send_udp_packet(g_tftp_socket , snd_buf, len, get_server_ip(), get_server_port());
tftp_reg_timeout();
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, ">> TFTP DATA : Block Number(%d), Data Length(%d)\r\n", block_number, data_len);
#endif
}
#endif
static void send_tftp_ack(uint16_t block_number)
{
uint8_t snd_buf[4];
uint8_t *pkt = snd_buf;
*((uint16_t *)pkt) = htons((uint16_t)TFTP_ACK);
pkt += 2;
*((uint16_t *)pkt) = htons(block_number);
pkt += 2;
send_udp_packet(g_tftp_socket , snd_buf, 4, get_server_ip(), get_server_port());
tftp_reg_timeout();
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, ">> TFTP ACK : Block Number(%d)\r\n", block_number);
#endif
}
#if 0 // 2014.07.01 sskim
static void send_tftp_oack(TFTP_OPTION *opt, uint8_t opt_len)
{
uint8_t snd_buf[MAX_MTU_SIZE];
uint8_t *pkt = snd_buf;
uint32_t i, len;
*((uint16_t *)pkt) = htons((uint16_t)TFTP_OACK);
pkt += 2;
for(i = 0 ; i < opt_len ; i++) {
strcpy((char *)pkt, (const char *)opt[i].code);
pkt += strlen((char *)opt[i].code) + 1;
strcpy((char *)pkt, (const char *)opt[i].value);
pkt += strlen((char *)opt[i].value) + 1;
}
len = pkt - snd_buf;
send_udp_packet(g_tftp_socket , snd_buf, len, get_server_ip(), get_server_port());
tftp_reg_timeout();
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, ">> TFTP OACK \r\n");
#endif
}
#endif
#if 0 // 2014.07.01 sskim
static void send_tftp_error(uint16_t error_number, uint8_t *error_message)
{
uint8_t snd_buf[MAX_MTU_SIZE];
uint8_t *pkt = snd_buf;
uint32_t len;
*((uint16_t *)pkt) = htons((uint16_t)TFTP_ERROR);
pkt += 2;
*((uint16_t *)pkt) = htons(error_number);
pkt += 2;
strcpy((char *)pkt, (const char *)error_message);
pkt += strlen((char *)error_message) + 1;
len = pkt - snd_buf;
send_udp_packet(g_tftp_socket , snd_buf, len, get_server_ip(), get_server_port());
tftp_reg_timeout();
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, ">> TFTP ERROR : Error Number(%d)\r\n", error_number);
#endif
}
#endif
static void recv_tftp_rrq(uint8_t *msg, uint32_t msg_len)
{
/* When TFTP Server Mode */
}
static void recv_tftp_wrq(uint8_t *msg, uint32_t msg_len)
{
/* When TFTP Server Mode */
}
static void recv_tftp_data(uint8_t *msg, uint32_t msg_len)
{
TFTP_DATA_T *data = (TFTP_DATA_T *)msg;
data->opcode = ntohs(data->opcode);
data->block_num = ntohs(data->block_num);
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, "<< TFTP_DATA : opcode(%d), block_num(%d)\r\n", data->opcode, data->block_num);
#endif
switch(get_tftp_state())
{
case STATE_RRQ :
case STATE_OACK :
if(data->block_num == 1) {
set_tftp_state(STATE_DATA);
set_block_number(data->block_num);
#ifdef F_STORAGE
save_data(data->data, msg_len - 4, data->block_num);
#endif
tftp_cancel_timeout();
}
send_tftp_ack(data->block_num);
if((msg_len - 4) < TFTP_BLK_SIZE) {
init_tftp();
g_progress_state = TFTP_SUCCESS;
}
break;
case STATE_DATA :
if(data->block_num == (get_block_number() + 1)) {
set_block_number(data->block_num);
#ifdef F_STORAGE
save_data(data->data, msg_len - 4, data->block_num);
#endif
tftp_cancel_timeout();
}
send_tftp_ack(data->block_num);
if((msg_len - 4) < TFTP_BLK_SIZE) {
init_tftp();
g_progress_state = TFTP_SUCCESS;
}
break;
default :
/* invalid message */
break;
}
}
static void recv_tftp_ack(uint8_t *msg, uint32_t msg_len)
{
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, "<< TFTP_ACK : \r\n");
#endif
switch(get_tftp_state())
{
case STATE_WRQ :
break;
case STATE_ACK :
break;
default :
/* invalid message */
break;
}
}
static void recv_tftp_oack(uint8_t *msg, uint32_t msg_len)
{
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, "<< TFTP_OACK : \r\n");
#endif
switch(get_tftp_state())
{
case STATE_RRQ :
process_tftp_option(msg, msg_len);
set_tftp_state(STATE_OACK);
tftp_cancel_timeout();
send_tftp_ack(0);
break;
case STATE_WRQ :
process_tftp_option(msg, msg_len);
set_tftp_state(STATE_ACK);
tftp_cancel_timeout();
/* TODO DATA Transfer */
//send_tftp_data(...);
break;
default :
/* invalid message */
break;
}
}
static void recv_tftp_error(uint8_t *msg, uint32_t msg_len)
{
TFTP_ERROR_T *data= (TFTP_ERROR_T *)msg;
data->opcode = ntohs(data->opcode);
data->error_code = ntohs(data->error_code);
#ifdef __TFTP_DEBUG__
DBG_PRINT(IPC_DBG, "<< TFTP_ERROR : %d (%s)\r\n", data->error_code, data->error_msg);
DBG_PRINT(ERROR_DBG, "[%s] Error Code : %d (%s)\r\n", __func__, data->error_code, data->error_msg);
#endif
init_tftp();
g_progress_state = TFTP_FAIL;
}
static void recv_tftp_packet(uint8_t *packet, uint32_t packet_len, uint32_t from_ip, uint16_t from_port)
{
uint16_t opcode;
/* Verify Server IP */
if(from_ip != get_server_ip()) {
#ifdef __TFTP_DEBUG__
DBG_PRINT(ERROR_DBG, "[%s] Server IP faults\r\n", __func__);
DBG_PRINT(ERROR_DBG, "from IP : %08x, Server IP : %08x\r\n", from_ip, get_server_ip());
#endif
return;
}
opcode = ntohs(*((uint16_t *)packet));
/* Set Server Port */
if((get_tftp_state() == STATE_WRQ) || (get_tftp_state() == STATE_RRQ)) {
set_server_port(from_port);
#ifdef __TFTP_DEBUG__
DBG_PRINT(INFO_DBG, "[%s] Set Server Port : %d\r\n", __func__, from_port);
#endif
}
switch(opcode)
{
case TFTP_RRQ : /* When Server */
recv_tftp_rrq(packet, packet_len);
break;
case TFTP_WRQ : /* When Server */
recv_tftp_wrq(packet, packet_len);
break;
case TFTP_DATA :
recv_tftp_data(packet, packet_len);
break;
case TFTP_ACK :
recv_tftp_ack(packet, packet_len);
break;
case TFTP_OACK :
recv_tftp_oack(packet, packet_len);
break;
case TFTP_ERROR :
recv_tftp_error(packet, packet_len);
break;
default :
// Unknown Mesage
break;
}
}
/* Functions ----------------------------------------------------*/
void TFTP_init(uint8_t socket, uint8_t *buf)
{
init_tftp();
g_tftp_socket = open_tftp_socket(socket);
g_tftp_rcv_buf = buf;
}
void TFTP_exit(void)
{
init_tftp();
close_tftp_socket(g_tftp_socket);
g_tftp_socket = -1;
g_tftp_rcv_buf = NULL;
}
int TFTP_run(void)
{
uint16_t len, from_port;
uint32_t from_ip;
/* Timeout Process */
if(g_resend_flag) {
if(tftp_time_cnt >= g_timeout) {
switch(get_tftp_state()) {
case STATE_WRQ: // 미구현
break;
case STATE_RRQ:
send_tftp_rrq(g_filename, (uint8_t *)TRANS_BINARY, &default_tftp_opt, 1);
break;
case STATE_OACK:
case STATE_DATA:
send_tftp_ack(get_block_number());
break;
case STATE_ACK: // 미구현
break;
default:
break;
}
tftp_time_cnt = 0;
tftp_retry_cnt++;
if(tftp_retry_cnt >= 5) {
init_tftp();
g_progress_state = TFTP_FAIL;
}
}
}
/* Receive Packet Process */
len = recv_udp_packet(g_tftp_socket, g_tftp_rcv_buf, MAX_MTU_SIZE, &from_ip, &from_port);
if(len < 0) {
#ifdef __TFTP_DEBUG__
DBG_PRINT(ERROR_DBG, "[%s] recv_udp_packet error\r\n", __func__);
#endif
return g_progress_state;
}
recv_tftp_packet(g_tftp_rcv_buf, len, from_ip, from_port);
return g_progress_state;
}
void TFTP_read_request(uint32_t server_ip, uint8_t *filename)
{
set_server_ip(server_ip);
#ifdef __TFTP_DEBUG__
DBG_PRINT(INFO_DBG, "[%s] Set Tftp Server : %x\r\n", __func__, server_ip);
#endif
g_progress_state = TFTP_PROGRESS;
send_tftp_rrq(filename, (uint8_t *)TRANS_BINARY, &default_tftp_opt, 1);
}
void tftp_timeout_handler(void)
{
if(g_resend_flag)
tftp_time_cnt++;
}

93
Internet/TFTP/tftp.h Normal file
View File

@ -0,0 +1,93 @@
/**
* @file tftp.h
* @brief TFTP Header File.
* @version 0.1.0
* @author Sang-sik Kim
*/
#ifndef __TFTP_H__
#define __TFTP_H__
#include <stdint.h>
#define F_APP_TFTP
#define __TFTP_DEBUG__
#define F_STORAGE // If your target support a storage, you have to activate this feature and implement.
#define SOCK_TFTP 1
#define INFO_DBG 0x01
#define ERROR_DBG 0x02
#define DEBUG_DBG 0x04
#define IPC_DBG 0x08
#define DBG_PRINT(level, format, args...) { \
if(dbg_level & level) \
printf(format, ##args); \
}
#define NORMAL_MODE 0
#define TFTP_MODE 1
extern int dbg_level;
/* tftp message */
#define TFTP_RRQ 1
#define TFTP_WRQ 2
#define TFTP_DATA 3
#define TFTP_ACK 4
#define TFTP_ERROR 5
#define TFTP_OACK 6
/* tftp state */
#define STATE_NONE 0
#define STATE_RRQ 1
#define STATE_WRQ 2
#define STATE_DATA 3
#define STATE_ACK 4
#define STATE_OACK 5
/* tftp transfer mode */
#define TRANS_ASCII "netascii"
#define TRANS_BINARY "octet"
/* tftp progress state */
#define TFTP_PROGRESS 0
#define TFTP_FAIL 1
#define TFTP_SUCCESS 2
/* define */
#define TFTP_SERVER_PORT 69
#define TFTP_TEMP_PORT 51000
#define TFTP_BLK_SIZE 512
#define MAX_MTU_SIZE 1514
#define FILE_NAME_SIZE 20
//#define __TFTP_DEBUG__
/* typedef */
typedef struct tftp_data {
uint16_t opcode;
uint16_t block_num;
uint8_t data[0];
} TFTP_DATA_T;
typedef struct tftp_error {
uint16_t opcode;
uint16_t error_code;
uint8_t error_msg[0];
} TFTP_ERROR_T;
typedef struct tftp_option {
uint8_t *code;
uint8_t *value;
} TFTP_OPTION;
/* Functions */
void TFTP_init(uint8_t socket, uint8_t *buf);
void TFTP_exit(void);
int TFTP_run(void);
void TFTP_read_request(uint32_t server_ip, uint8_t *filename);
void tftp_timeout_handler(void);
#endif /*__TFTP_H__ */

View File

@ -400,7 +400,7 @@ static void send_http_response_body(uint8_t s, uint8_t * uri_name, uint8_t * buf
read_userReg_webContent(start_addr, &buf[0], HTTPSock_Status[get_seqnum].file_offset, send_len);
}
#ifdef _USE_SDCARD_
else if(HTTPSock_Status[get_seqnum]->storage_type == SDCARD)
else if(HTTPSock_Status[get_seqnum].storage_type == SDCARD)
{
// Data read from SD Card
fr = f_read(&fs, &buf[0], send_len, (void *)&blocklen);
@ -561,7 +561,7 @@ static void http_process_handler(uint8_t s, st_http_request * p_http_request)
file_len = fs.fsize;
content_addr = fs.sclust;
HTTPSock_Status[get_seqnum]->storage_type = SDCARD;
HTTPSock_Status[get_seqnum].storage_type = SDCARD;
}
#elif _USE_FLASH_
else if(/* Read content from Dataflash */)

View File

@ -14,5 +14,8 @@ This driver provides the Berkeley Socket type APIs.
- DNS client
- Others will be added.
## How to add an ioLibrary in project through github site.
- Example, refer to https://www.youtube.com/watch?v=mt815RBGdsA
## Revision History
Last release : Nov. 2014