⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 tini的http-slientC程序
💻 C
字号:
/* ---------------------------------------------------------------------------
 *  Copyright (C) 2003 Dallas Semiconductor Corporation, All Rights Reserved.
 * 
 *  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 DALLAS SEMICONDUCTOR 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.
 * 
 *  Except as contained in this notice, the name of Dallas Semiconductor
 *  shall not be used except as stated in the Dallas Semiconductor
 *  Branding Policy.
 * ---------------------------------------------------------------------------
 */

/*****************************************************************************
 *	Module Name : TINI FTP Client Library Test Application
 *	Description : This application tests FTPClient library functionality
 * 	   Filename : main.c
 *	   Compiler : keil C51 Compiler V7.06
 ****************************************************************************/

//include files
#include <reg400.h>
#include "stdio.h"
#include <string.h>
#include <rom400_init.h>
#include <rom400_task.h>
#include <rom400_sock.h>
#include <rom400_kmem.h>
#include <rom400_mem.h>
#include <tini400_dns.h>
#include <rom400_xnetstack.h>
#include "tini400_ftpclient.h"

//leave 4000h bytes for function parameters
#define RAM_START             0x16000
#define RAM_END               0x6F000

//file system memory location
#define FS_START              0x70000
#define FS_BLOCKS             256

//Static IP Address (used if you are selecting a static IP)
#define STATIC_IP_MSB             180
#define STATIC_IP_2                 0
#define STATIC_IP_3                54
#define STATIC_IP_LSB              94

//Static Subnet Mask (used if you are selecting a static IP)
#define STATIC_SUBNETMASK_MSB     255
#define STATIC_SUBNETMASK_2       255
#define STATIC_SUBNETMASK_3         0
#define STATIC_SUBNETMASK_LSB       0

#define STATIC_IPV4_PREFIX         16

//Static Gateway (used if you are selecting a static IP)
#define STATIC_GATEWAY_MSB        180
#define STATIC_GATEWAY_2            0  
#define STATIC_GATEWAY_3          110
#define STATIC_GATEWAY_LSB         24

//function declarations
void printfile(char *filename);
void store_files_in_filesystem(void);

/******************************************************************************
 *   Function Name : do_static
 *     Description : configures IP address of TINI
 *        Input(s) : nothing
 *       Output(s) : nothing
 *   	 Destroyed :
 *    		 Notes :
 ******************************************************************************/
void do_static()
{
    unsigned int result;
    unsigned char xdata config[56];
    unsigned int i;
    for (i=0;i<56;i++)
        config[i] = 0;

    //set the ip address
    config[12] = STATIC_IP_MSB;
    config[13] = STATIC_IP_2;
    config[14] = STATIC_IP_3;
    config[15] = STATIC_IP_LSB;

    //set the subnet mask
    config[16] = STATIC_SUBNETMASK_MSB;
    config[17] = STATIC_SUBNETMASK_2;
    config[18] = STATIC_SUBNETMASK_3;
    config[19] = STATIC_SUBNETMASK_LSB;

    //set the iP4 prefix
    config[20] = STATIC_IPV4_PREFIX;

    //set the gateway
    config[33] = STATIC_GATEWAY_MSB;
    config[34] = STATIC_GATEWAY_2;
    config[35] = STATIC_GATEWAY_3;
    config[36] = STATIC_GATEWAY_LSB;

    result = setnetworkparams(config);
}

//main function of tini test application
void main(void)
{
	int status, count, cmd;
    unsigned int temp;
	struct sockaddr_in ftp_serv_addr, dns_serv_addr;
	char inputstr[100], temp1[25], temp2[25],dir_str[1000],*tempptr;
	struct hostent *phostent;
	unsigned long ftp_ip,uc0, uc1, uc2, uc3;
    
	//for file system.
	FILE *file;
    void *start;

	//print the version number of ftpclient library
	printf("\n the version information of ftpclient library is : %d",
			ftpclient_version());

	//install extended stack module
    xnetstack_install();
		
    //install kernel memory manager
    kmem_install(ROM400_KMEM_MODEL_SMALLEST + 2);

	//initialize ROM
	init_rom(RAM_START, RAM_END);

	//clears socket library parameters
	clear_param_buffers();

	//configure IP address
    do_static();

	//configure filesystem library
    start = (void*)FS_START;
    temp = finit(FOPEN_MAX, FS_BLOCKS, start);
	printf("Result of FS init: %d \r\n", temp);

	//configure dns library
	dns_init();
	dns_settimeout(1000);
    dns_serv_addr.sin_family = AF_INET;
    dns_serv_addr.sin_port = htons(53);
    dns_serv_addr.sin_addr.s_addr=inet_addr("180.0.34.2");
    memset(&(dns_serv_addr.sin_zero), 0, 12);
    dns_setprimary((struct sockaddr*)&dns_serv_addr);
    dns_setsecondary((struct sockaddr*)&dns_serv_addr);

	//initialize ftp client library with 10 seconds timeout
	ftpclient_init(10000);

	//store some datafiles in local file system
	store_files_in_filesystem();

	//print ftpclient test application banner
	printf("\n\n\nDallas Semiconductor TINI FTP v 1.0");
	printf("\nType \"help\" to get list of ftp commands");

	//now, we are ready to accept commands from user
	while(1)
	{
		printf("\n>>");
		gets(inputstr,50);

		if(strcmp(inputstr,"help")==0)
		{
			printf("\nCommand Summary:");
			printf("\n----------------");
			printf("\nopen  - connect with ftp server, SYNTAX: open server IP address"); 
			printf("\nget   - download file from server, SYNTAX: get <<server filename>> <<tini");
			printf("\nput   - upload  file to server, SYNTAX: put filename");
			printf("\nclose - disconnects from ftp server");
			printf("\ncddir  - change directory SYNTAX cddir <<new directory name>>");
			printf("\npwd   - display current directory SYNTAX pwd");
			printf("\ndir	- displays file name with detailed information SYNTAX: dir or dir filename");
			printf("\nmode  - sets file transfer mode, SYNTAX: mode a for ascii, mode b for binary");
            printf("\ndataconnectionmode - sets data connection mode, SYNTAX:dataconnectionmode \"a\" for active,\"p\" for passive");
			printf("\ndumpfile -displays tini file content, SYNTAX: dumpfile <<filename>>\n");
			continue;
		}

		if(strncmp(inputstr,"open",4)==0)
		{
			strcpy(inputstr,strchr(inputstr,' ')+1); //it will store server name
		  										 	 //to inputstr
			//find out IP address of ftp server

		    if((ftp_ip = inet_addr(inputstr))==0)
            {
                phostent= gethostbyname(inputstr);
    
                if((phostent!=NULL)&&(phostent->h_addr_list!=NULL))
                {

                    uc0=(unsigned char)phostent->h_addr_list[0][0];
                    uc1=(unsigned char)phostent->h_addr_list[0][1];
                    uc2=(unsigned char)phostent->h_addr_list[0][2];
                    uc3=(unsigned char)phostent->h_addr_list[0][3];

    
                    ftp_ip=((uc0 << 24) | (uc1 << 16) | (uc2 << 8) | uc3);

                }
                else
                {
                    printf("IP address for %s is not available",inputstr);
                    continue;
                }
            }
    
    		ftp_serv_addr.sin_addr.s_addr = ftp_ip;
            ftp_serv_addr.sin_port=0;
		    ftp_serv_addr.sin_family = AF_INET;
		    memset(&(ftp_serv_addr.sin_zero), 0, 12);

			printf("\nuser:");
			scanf("%s",temp1);
			printf("\npassword:");
			scanf("%s",temp2);

			if((status=ftpclient_connect(&ftp_serv_addr, temp1, temp2))!=230)
				printf("\nftp error: %d",status);
			else
				printf("\n%s logged-in",temp1);

			continue;					
		}

		if(strcmp(inputstr,"close")==0)
		{
			if((status=ftpclient_disconnect())!=FTPSERVER_CLOSE_CTRL_CONNECTION)
				printf("\nftp error: %d",status);
			else
				printf("\nOkay! connection closed successfully");
			continue;
		}

		if(strncmp(inputstr,"dir",3)==0)
		{
			tempptr=strchr(inputstr,' ');
			if((status=ftpclient_dir(tempptr,dir_str,1000,           \
                                FTPCLIENT_DETAILED_DIRLISTING))!=226)
            {
				printf("\nftp error: %d",status);
            }
			else
            {   
				printf("%s",dir_str);		
            }
			continue;
		}

		if(strncmp(inputstr,"cddir",5)==0)
		{
			tempptr=strchr(inputstr,' ');
            while(*tempptr==' ') tempptr++;
			if((status=ftpclient_cd(tempptr))!=250)
				printf("\nftp error: %d",status);
			else
				printf("\ndirectory successfully changed");
			continue;
		}

		if(strncmp(inputstr,"pwd",3)==0)
		{
			if((status=ftpclient_pwd(dir_str,1000))!=257)
				printf("\nftp error: %d",status);
			else
				printf("%s",dir_str);		
			continue;
		}

		if(strncmp(inputstr,"get",3)==0)
		{
			printf("enter server file name:");
			scanf("%s",temp1);
			printf("enter tini file name:");
			scanf("%s",temp2);
			if((status=ftpclient_getfile(temp1,temp2))!=226)
				printf("\nftp error: %d",status);
			else
				printf("file was successfully downloaded");		
			continue;
			
		}

		if(strncmp(inputstr,"put",3)==0)
		{
			printf("enter server file name:");
			scanf("%s",temp2);
			printf("enter tini file name:");
			scanf("%s",temp1);
			if((status=ftpclient_putfile(temp1,temp2))!=226)
				printf("\nftp error: %d",status);
			else
				printf("file was successfully uploaded");		
			continue;
		}
		if(strncmp(inputstr,"mode",4)==0)
		{
			tempptr=strchr(inputstr,' ');
			if(*(tempptr+1)=='a')
				status=ftpclient_settransmissionmode(FTPCLIENT_ASCII);
			else
				status=ftpclient_settransmissionmode(FTPCLIENT_BINARY);
    
            printf("\nreturn value: %d",status);
		}
        if(strncmp(inputstr,"dataconnectionmode",18)==0)
		{
			tempptr=strchr(inputstr,' ');
			if(*(tempptr+1)=='p')
            {
				ftpclient_setdataconnectionmode(FTPCLIENT_PASSIVE_MODE);
                printf("\npassive mode is set");
            }
			else
            {
				ftpclient_setdataconnectionmode(FTPCLIENT_ACTIVE_MODE);
                printf("\nactive mode is set");

            }
		
		}
		if(strncmp(inputstr,"dumpfile",8)==0)
		{
			tempptr=strchr(inputstr,' ');
			printfile(tempptr+1);
		}
	}
	while(1)
	{
	}
}

//print file content
void printfile(char *filename)
{
    int temp, _eof;
	FILE *file;
	file = fopen(filename, "r");
	if (file==NULL)
	{
		printf("\n file not found");
		return;
	}
	printf("\nfile contents:");
    temp = fgetc(file);

    while (temp != -1)
    {
        if(temp==0)
            printf("\\0");
        else
            printf("%c", (char)temp);

        temp = fgetc(file);
    }
	printf("\r\n");
	fclose(file);
}

//store files in filesystem
void store_files_in_filesystem(void)
{
	FILE *fp;
    int temp, _eof;

	//open the file
	fp=fopen("test.dat","w");
	
	if(fp==NULL)
	{
		printf("\nfile could not be opened");
		return;
	}

	//write the content to the file
	fputs("This is test data file",fp);
	fputs("\n\n\n\nThere is nothing either good or bad but thinking makes it so --William Shakespeare\n\n\n",fp);
    fwrite("string with embedded zerooo\0\0\0\0\0\0ooooos",1,39,fp);

	//close the file
	fclose(fp);
	return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -