#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#include <time.h>
#include <sys/time.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <signal.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "ftplib.h"
#include "message.h"
#include "shmem.h"
#include "CYNET_API.h"
#include "memory.h"
#include "rtc.h"
// bcd码转换为int8类型 2位bcd转换为1字节bin
unsigned char inline bcd2int8(char val)
{
return((((val & 0xf0) >> 4) * 10) + (val & 0xf));
}
// int8类型转换为bcd码 1字节bin转换为2位bcd
char inline int82bcd(unsigned char val)
{
return(((val / 10) << 4) + (val % 10));
}
struct date_t
{
short year;
short month;
short day;
short hour;
short minute;
short second;
};
static struct date_t default_time;
static netbuf *conn = NULL;
struct ftp_config ftp_cfg;
static FILE *config_fd;
char sendbuf[PICTURE_SIZE];
int analyse_config_para(char *filepath, char *src, char *dist)
{
int ret = -1;
char line[MAX_STRING];
if(filepath == NULL)
return ret;
config_fd = fopen(filepath,"r");
if(config_fd < 0)
{
printf("config file not exist \n");
return ret;
}
while(!feof(config_fd))
{
//printf("config \n");
if( fgets(line, MAX_STRING, config_fd) == NULL)
continue;
if(memcmp(line,src,strlen(src))==0)
{
char *ptr;
int i =0 ;
//printf("memcmp %s \n",src);
while(i++ < MAX_STRING)
{
if(memcmp(&line[i], "=", 1) == 0)
{
strcpy(dist , &line[i+1]);
ret = 0;
break;
}
}
//printf(" %s \n",dist);
break;
}
}
fclose(config_fd);
}
static void timer_proc(int signo)
{
//printf("notice background %d \n ",getpid());
i_am_alive(getpid());
}
int sendUdp(const char *host, unsigned short port, char *data, int len)
{
int sockfd;
int result;
struct sockaddr_in address;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);/* Construct the address for use with sendto/recvfrom... */
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_addr.s_addr = inet_addr(host);
connect(sockfd,(struct sockaddr *)&address,sizeof(address));
result = sendto(sockfd, data, len, 0, (struct sockaddr *)&address, sizeof(address));
//printf("send udp result %d \n",result);
close(sockfd);
return result;
}
void make_file_dir(char * name, char *dir ,struct date_t *date, struct sharedata *carinfo)
{
static int flg = 0;
short chan = 0;
if(flg == 0)
{
default_time.year = date->year;
default_time.month = date->month;
default_time.day = date->day;
for (chan = 1;chan<5 ;chan++)
{
sprintf(ftp_cfg.filename,"%02ld%02ld%02ld/%02ld",
(date->year),date->month, date->day, chan);
FtpMkdir(ftp_cfg.filename, conn);
}
flg = 1;
}
if(default_time.year != date->year ||default_time.month != date->month || default_time.day != date->day)
{
for (chan = 1; chan<5 ; chan++)
{
sprintf(ftp_cfg.filename,"%02ld%02ld%02ld/%02ld",
(date->year), date->month, date->day, chan);
FtpMkdir(ftp_cfg.filename, conn);
}
}
sprintf(ftp_cfg.filename,"%02ld%02ld%02ld/%02ld/%02ld%02ld%02ld_%2d_%2d_%2d.jpg",
(date->year), date->month, date->day, carinfo->channel, date->hour,date->minute,date->second,carinfo->carspeed, carinfo->limitspeed,carinfo->picture_count);
sendUdp(ftp_cfg.host, 8844, ftp_cfg.filename,128);
}
/*
* FtpInit for stupid operating systems that require it (Windows NT)
*/
void FtpInit(struct ftp_config *ftp_cfg,char *config_file)
{
char *u,h[64];
if(config_file == NULL)
{
strcpy(ftp_cfg->username,FTP_USER);
u = getenv("USER");
if (gethostname(h,64) < 0)
{
perror("gethostname");
exit(-1);
}
if ((u != NULL) && (h != NULL))
{
static char xxx[64];
sprintf(xxx,"%s@%s",u,h);
strcpy(ftp_cfg->password,xxx);
}
strcpy(ftp_cfg->password,FTP_PASS);
strcpy(ftp_cfg->filename,FTP_PATH);
strcpy(ftp_cfg->host,FTP_SERVER);
ftp_cfg->port = 21;
}
else
{
int handler;
char dist[32];
char src[4];
strcpy(ftp_cfg->username,FTP_USER);
u = getenv("USER");
if (gethostname(h,64) < 0)
{
perror("gethostname");
exit(-1);
}
if ((u != NULL) && (h != NULL))
{
static char xxx[64];
sprintf(xxx,"%s@%s",u,h);
strcpy(ftp_cfg->password,xxx);
}
analyse_config_para(config_file, "ftpip0", dist);
src[0]= atoi(dist);
analyse_config_para(config_file, "ftpip1", dist);
src[1]= atoi(dist);
analyse_config_para(config_file, "ftpip2", dist);
src[2]= atoi(dist);
analyse_config_para(config_file, "ftpip3", dist);
src[3]= atoi(dist);
memset(dist, 0 ,16);
sprintf(dist,"%d.%d.%d.%d",src[0],src[1],src[2],src[3]);
strncpy(ftp_cfg->host,dist,16);
analyse_config_para(config_file, "ftpport", dist);
ftp_cfg->port = atoi(dist);
analyse_config_para(config_file, "upload", dist);
strcpy(ftp_cfg->filename,dist);
}
}
int main(int argc, char *argv[])
{
int iFtp_fd = -1;
long ulTimeAdjust;
int ret;
struct itimerval val;
struct timeval tmval;
struct tm tm,*ptm;
struct sharedata carinfo;
struct shmem_msg msg;
struct shm *shmem;
_time_ settime;
time_t timep;
int shmem_msg_id;
static char *filepath="/var/config.txt";
ret = rtc_init();
if(ret < 0)
return (-1);
rtc_get(&settime);
tm.tm_year = (short)bcd2int8(settime.year);
//tm.tm_year +=2000;
tm.tm_mon = (short)bcd2int8(settime.month);
tm.tm_mday = (short)bcd2int8(settime.day);
tm.tm_hour = (short)bcd2int8(settime.hour);
tm.tm_min = (short)bcd2int8(settime.min);
tm.tm_sec = (short)bcd2int8(settime.sec);
long lTime = mktime(&tm);
tmval.tv_sec = lTime;
tmval.tv_usec = 0;
printf("set time before %d %d \n",tm.tm_year,tm.tm_mon);
settimeofday(&tmval, (struct timezone *) 0);
memset((char *)&tm, 0 ,sizeof(struct tm));
time(&timep);
ptm = localtime(&timep);
printf (("%d %d %d \n"), (2000+ptm->tm_year),( 1+ptm->tm_mon), ptm->tm_mday);
shmem_msg_id = shmem_msg_init();
if( shmem_msg_id < 0)
{
rtc_exit();
return (-1);
}
shmem = (struct shm *)shmem_init(SHMEMFTP, sizeof(SHMEMFTP));
if(shmem == NULL)
{
rtc_exit();
shmem_msg_exit(shmem_msg_id);
return (-1);
}
signal(SIGALRM, timer_proc);
val.it_interval.tv_sec = 1;
val.it_interval.tv_usec = 0;
val.it_value.tv_sec = 1;
val.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &val, NULL);
FtpInit(&ftp_cfg,filepath);
while(1)
{
ret = shmem_msg_get(shmem_msg_id,&msg);
if (ret < 0)
continue;
//printf("msg %d \n",msg.mtype);
if(iFtp_fd < 0)
{
if ((iFtp_fd = FtpConnect(ftp_cfg.host,ftp_cfg.port,&conn)) <= 0)
{
FtpClose(conn);
printf("connect error \n");
exit(-1);
}
else
{
if ((iFtp_fd = FtpLogin(ftp_cfg.username,ftp_cfg.password,conn) <=0))
{
FtpClose(conn);
iFtp_fd = -1;
}
}
iFtp_fd = 1;
}
if (iFtp_fd > 0)
{
FRAME_HEADER *pFh;
struct date_t date;
_time_ tmpdate;
int len;
pFh = (FRAME_HEADER*)shmem->addr;
len= pFh->len;
//printf("frame len %d \n", len);
//date.day = pFh->day;
//date.hour = pFh->hour;
//date.minute = pFh->minute;
//date.month = pFh->month;
//date.second = pFh->second;
//date.year = pFh->year;
rtc_get(&tmpdate);
date.day = (short)bcd2int8(tmpdate.day);
date.hour = (short)bcd2int8(tmpdate.hour);
date.minute = (short)bcd2int8(tmpdate.min);
date.month = (short)bcd2int8(tmpdate.month);
date.secon