Sfoglia il codice sorgente

fix memory leak problem

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1358 bbd45198-f89e-11dd-88c7-29a3b14d5316
itspy.wei 14 anni fa
parent
commit
ac97bc17bd

+ 19 - 9
components/utilities/zmodem/rz.c

@@ -53,6 +53,7 @@ void zr_start(char *path)
 		rt_kprintf("size: %ld bytes\r\n",zf->bytes_received);
 		rt_kprintf("receive completed.\r\n");
 		close(zf->fd);
+		rt_free(zf->fname);
 		rt_free(zf);
     }
     else
@@ -60,8 +61,12 @@ void zr_start(char *path)
         rt_kprintf("\b\b\bfile: %s                           \r\n",zf->fname+1);
 		rt_kprintf("size: 0 bytes\r\n");
 		rt_kprintf("receive failed.\r\n");
-	    close(zf->fd);
-	    unlink(zf->fname);    /* remove this file */ 
+		if (zf->fd >= 0)
+		{
+	        close(zf->fd);
+	        unlink(zf->fname);    /* remove this file */ 
+		}
+		rt_free(zf->fname);
 	    rt_free(zf);
     }
 	/* waiting,clear console buffer */
@@ -101,7 +106,7 @@ again:
 			 res = zget_data(rxbuf, RX_BUFFER_SIZE);
 			 if (res == GOTCRCW)
 			 {
-	             if (zget_file_info((char*)rxbuf,zf) != RT_EOK) 
+	             if ((res =zget_file_info((char*)rxbuf,zf))!= RT_EOK) 
 	             {
 	                 zsend_hex_header(ZSKIP, tx_header);
 		             return (res);
@@ -149,7 +154,7 @@ static rt_err_t zrec_files(struct zfile *zf)
 	rt_kprintf("rz: ready...\r\n");	   /* here ready to receive things */
 	if ((res = zrec_init(rxbuf,zf))!= RT_EOK)
 	{
-	     rt_kprintf("receive init failed !\r\n");
+	     rt_kprintf("\b\b\breceive init failed\r\n");
 		 rt_free(rxbuf);
 		 return -RT_ERROR;
 	}
@@ -254,7 +259,8 @@ static rt_err_t zget_file_info(char *name,struct zfile *zf)
     ftemp = rt_malloc(len);
     if (ftemp == RT_NULL)		 
 	{
-	    rt_kprintf("ftemp: out of memory\n");
+	    zsend_can();
+		rt_kprintf("\b\b\bftemp: out of memory\n");
 		return -RT_ERROR;
 	}
 	memset(ftemp,0,len);
@@ -265,7 +271,8 @@ static rt_err_t zget_file_info(char *name,struct zfile *zf)
 	/* check if is a directory */
 	if ((zf->fd=open(ftemp, DFS_O_DIRECTORY,0)) < 0)	 
 	{
-	    rt_kprintf("can not open file:%s\r\n",zf->fname+1);
+	    zsend_can();
+	    rt_kprintf("\b\b\bcan not open file:%s\r\n",zf->fname+1);
 		close(zf->fd);
 		zf->fd = -1;
 	    return res;
@@ -286,14 +293,16 @@ static rt_err_t zget_file_info(char *name,struct zfile *zf)
 	dfs_statfs(working_directory,&buf);
 	if (zf->bytes_total > (buf.f_blocks * buf.f_bfree))
 	{
-	    rt_kprintf(" not enough disk space !\r\n");
+	    zsend_can();
+	    rt_kprintf("\b\b\bnot enough disk space\r\n");
+		zf->fd = -1;
 		return -RT_ERROR;  
 	}
 	zf->bytes_received   = 0L;
 	if ((zf->fd = open(zf->fname,DFS_O_CREAT|DFS_O_WRONLY,0)) < 0)	 /* create or replace exist file */
 	{
-	    rt_kprintf("can not create file:%s \r\n",zf->fname);	
-		rt_free(ftemp);
+	    zsend_can();
+	    rt_kprintf("\b\b\bcan not create file:%s \r\n",zf->fname);	
 		return -RT_ERROR;
 	}
 	return RT_EOK;
@@ -346,6 +355,7 @@ more_data:
 /* write file */
 static rt_err_t zwrite_file(rt_uint8_t *buf,rt_uint16_t size,struct zfile *zf)
 {
+//    return 0;
 	return (write(zf->fd,buf,size));
 }
 

+ 6 - 0
components/utilities/zmodem/sz.c

@@ -39,6 +39,7 @@ void zs_start(char *path)
 {
     struct zfile *zf;
 	rt_err_t res = RT_ERROR;
+
 	zf = rt_malloc(sizeof(struct zfile));
 	if (zf == RT_NULL)
 	{
@@ -59,12 +60,14 @@ void zs_start(char *path)
         rt_kprintf("\r\nfile: %s \r\nsize: 0 bytes\r\nsend failed.\r\n",zf->fname+1);
     }
 	rt_free(zf);
+
 	return;
 }
 /* init the parameters */
 static void zsend_init(void)
 {
 	rt_err_t res = -RT_ERROR;
+
 	zinit_parameter();
 	for(;;)          /* wait ZPAD */
 	{
@@ -153,6 +156,7 @@ static rt_err_t zsend_file(struct zfile *zf,rt_uint8_t *buf, rt_uint16_t len)
 {
 	rt_uint8_t cnt;
 	rt_err_t res = -RT_ERROR;
+
 	for (cnt=0;cnt<5;cnt++) 
 	{  
 		tx_header[ZF0] = ZF0_CMD;	            /* file conversion option */
@@ -195,6 +199,7 @@ loop:
 			 break;
 		} 
 	}
+
 	return res;
 }
 
@@ -256,6 +261,7 @@ static rt_uint16_t zfill_buffer(struct zfile *zf,rt_uint8_t *buf,rt_uint16_t siz
 static rt_err_t zget_sync(void)
 {
     rt_err_t res = -RT_ERROR;
+
 	for (;;) 
 	{
 		res = zget_header(rx_header);

+ 35 - 12
components/utilities/zmodem/zcore.c

@@ -29,7 +29,8 @@ rt_uint32_t  Rxpos;		         /* received file position */
 rt_uint32_t  Txpos;		         /* transmitted file position */
 rt_uint8_t   Txfcs32;		     /* TURE means send binary frames with 32 bit FCS */
 rt_uint8_t   TxCRC;		         /* controls 32 bit CRC being sent */
-rt_uint8_t   RxCRC;		         /* indicates/controls 32 bit CRC being received */			                     /* 0 == CRC16,  1 == CRC32,  2 == CRC32 + RLE */
+rt_uint8_t   RxCRC;		         /* indicates/controls 32 bit CRC being received */			                     
+                                 /* 0 == CRC16,  1 == CRC32,  2 == CRC32 + RLE */
 char Attn[ZATTNLEN+1];	         /* attention string rx sends to tx on err */
 
 void zinit_parameter(void);
@@ -59,7 +60,7 @@ void zinit_parameter(void)
 {
     rt_uint8_t i;
 
-    ZF0_CMD  = /*CANFC32|*/CANFDX|CANOVIO;		/*  not chose CANFC32,CANRLE,although it have been supported */
+    ZF0_CMD  = CANFC32|CANFDX|CANOVIO;		/*  not chose CANFC32,CANRLE,although it have been supported */
 	ZF1_CMD  = 0;							    /* fix header length,not support CANVHDR */
 	ZF2_CMD  = 0;
 	ZF3_CMD  = 0;	
@@ -70,6 +71,7 @@ void zinit_parameter(void)
 	Rxpos    = Txpos = 0;
 	RxCRC    = 0;	
 	Txfcs32  = 0;
+
 	return ;
 }
 
@@ -136,6 +138,7 @@ void zsend_bin_header(rt_uint8_t type, rt_uint8_t *hdr)
 		    crc >>= 8;
 	    }
 	}
+
 	return;
 }
 
@@ -162,6 +165,7 @@ void zsend_hex_header(rt_uint8_t type, rt_uint8_t *hdr)
 	if (type != ZFIN && type != ZACK)
 		zsend_line(021);
 	TxCRC = 0;               /* clear tx crc type */
+
 	return;
 }
 
@@ -262,6 +266,7 @@ void zsend_bin_data(rt_uint8_t *buf, rt_int16_t len, rt_uint8_t frameend)
 	}
 	if (frameend == ZCRCW)
 		zsend_byte(XON);
+
 	return;
 }
 
@@ -272,9 +277,11 @@ static rt_int16_t zrec_data16(rt_uint8_t *buf,rt_uint16_t len)
 	rt_uint16_t crc;
 	rt_err_t res = -RT_ERROR;
  	rt_uint8_t *p,flag = 0;
+	rt_uint8_t i =0, debug[20];
 	p = buf;
 	crc = 0L;  
-    Rxcount = 0;  
+    Rxcount = 0; 
+	debug[0] = debug[4] = 0; 
 	while(buf <= p+len) 
 	{
 		if ((res = zread_byte()) & ~0377)
@@ -282,6 +289,9 @@ static rt_int16_t zrec_data16(rt_uint8_t *buf,rt_uint16_t len)
 		    if (res == GOTCRCE || res == GOTCRCG ||
 			    res == GOTCRCQ || res == GOTCRCW)
 			{
+				  c = res;
+				  debug[i++] = res;
+				  c = debug[0];
 				  c = res;
 				  crc = updcrc16(res&0377, crc);
 				  flag = 1;	
@@ -298,6 +308,7 @@ static rt_int16_t zrec_data16(rt_uint8_t *buf,rt_uint16_t len)
 		   {
 		       crc = updcrc16(res, crc); 
 			   crc_cnt++;
+			   debug[i++] = res;
 			   if (crc_cnt < 2) continue;
 			   if ((crc & 0xffff))
 			   {
@@ -316,6 +327,7 @@ static rt_int16_t zrec_data16(rt_uint8_t *buf,rt_uint16_t len)
 		   }
 		}
 	}
+
 	return -RT_ERROR;
 }
 
@@ -369,7 +381,8 @@ static rt_int16_t zrec_data32(rt_uint8_t *buf,rt_int16_t len)
 		      crc = updcrc32(res, crc);
 		   }
 		}
-	}
+	}
+
 	return -RT_ERROR;
 }
 /* receive data,with RLE encoded,32bits CRC check */
@@ -480,6 +493,7 @@ rt_int16_t zget_data(rt_uint8_t *buf, rt_uint16_t len)
 	{
 	    res = zrec_data32r(buf, len);
 	}
+
 	return res;
 }
 /* get type and cmd of header, fix lenght */
@@ -489,7 +503,7 @@ rt_int16_t zget_header(rt_uint8_t *hdr)
 	rt_uint32_t bit;
 	rt_uint16_t get_can,step_out;
 
-	bit = get_device_speed();	  /* getbaudrate */
+	bit = get_device_baud();	             /* get console baud rate */
 	Rxframeind = header_type = 0;
     step_out = 0;
 	prev_char = 0xff;
@@ -612,7 +626,8 @@ static rt_int16_t zget_bin_header(rt_uint8_t *hdr)
 	{
 		rt_kprintf("CRC error\n");
 		return -RT_ERROR;
-	}
+	}
+
 	return header_type;
 }
 
@@ -648,7 +663,8 @@ static rt_int16_t zget_bin_fcs(rt_uint8_t *hdr)
 		rt_kprintf("CRC error\n");
 #endif
 		return -RT_ERROR;
-	}
+	}
+
 	return header_type;
 }
 
@@ -689,7 +705,8 @@ rt_int16_t zget_hex_header(rt_uint8_t *hdr)
 		return res;
 	res = zread_line(100);
 	if (res < 0)
-		return res;
+		return res;
+
 	return header_type;
 }
 
@@ -700,6 +717,7 @@ static void zsend_ascii(rt_uint8_t c)
 
 	zsend_line(hex[(c&0xF0)>>4]);
 	zsend_line(hex[(c)&0xF]);
+
 	return;
 }
 
@@ -753,7 +771,8 @@ static rt_int16_t zget_hex(void)
 		res -= ('a' - ':');
 	if (res & ~0x0f)
 		return -RT_ERROR;
-	res += (n<<4);
+	res += (n<<4);
+
 	return res;
 }
 
@@ -813,7 +832,8 @@ again2:
 		 if ((res & 0140) ==  0100)
 			return (res ^ 0100);
 		 break;
-	}
+	}
+
 	return -RT_ERROR;
 }
 
@@ -850,6 +870,7 @@ void zput_pos(rt_uint32_t pos)
 	tx_header[ZP1] = pos>>8;
 	tx_header[ZP2] = pos>>16;
 	tx_header[ZP3] = pos>>24;
+
 	return;
 }
 
@@ -859,7 +880,9 @@ void zget_pos(rt_uint32_t pos)
 	Rxpos = (rx_header[ZP3] & 0377);
 	Rxpos = (Rxpos << 8) | (rx_header[ZP2] & 0377);
 	Rxpos = (Rxpos << 8) | (rx_header[ZP1] & 0377);
-	Rxpos = (Rxpos << 8) | (rx_header[ZP0] & 0377);
+	Rxpos = (Rxpos << 8) | (rx_header[ZP0] & 0377);
+
+	return;
 }
 
-/* End of zm.c */
+/* end of zcore.c */

+ 39 - 39
components/utilities/zmodem/zdef.h

@@ -65,20 +65,20 @@
 #define ZP2	      2
 #define ZP3	      3	    /* high order 8 bits of file position */
 
-/* Parameters for ZRINIT header */
+/* parameters for ZRINIT header */
 #define ZRPXWN	  8	    /* 9th byte in header contains window size/256 */
 #define ZRPXQQ	  9	    /* 10th to 14th bytes contain quote mask */
-/* Bit Masks for ZRINIT flags byte ZF0 */
-#define CANFDX	  01	/* rx can send and receive true FDX */
-#define CANOVIO	  02	/* rx can receive data during disk I/O */
-#define CANBRK	  04	/* rx can send a break signal */
-#define CANRLE	  010	/* receiver can decode RLE */
-#define CANLZW	  020	/* receiver can uncompress */
-#define CANFC32	  040	/* receiver can use 32 bit Frame Check */
-#define ESCCTL    0100	/* receiver expects ctl chars to be escaped */
-#define ESC8      0200	/* receiver expects 8th bit to be escaped */
+/* bit Masks for ZRINIT flags byte ZF0 */
+#define CANFDX	  0x01	/* rx can send and receive true FDX */
+#define CANOVIO	  0x02	/* rx can receive data during disk I/O */
+#define CANBRK	  0x04	/* rx can send a break signal */
+#define CANRLE	  0x10	/* receiver can decode RLE */
+#define CANLZW	  0x20	/* receiver can uncompress */
+#define CANFC32	  0x28	/* receiver can use 32 bit Frame Check */
+#define ESCCTL    0x64	/* receiver expects ctl chars to be escaped */
+#define ESC8      0xc8	/* receiver expects 8th bit to be escaped */
 
-/* Bit Masks for ZRINIT flags byte ZF1 */
+/* bit Masks for ZRINIT flags byte ZF1 */
 #define CANVHDR	  01	/* variable headers OK */
 #define ZRRQWN	  8	    /* receiver specified window size in ZRPXWN */
 #define ZRRQQQ	  16	/* additional control chars to quote in ZRPXQQ	*/
@@ -93,43 +93,43 @@
 #define ZCBIN	  1	   /* binary transfer - inhibit conversion */
 #define ZCNL	  2	   /* convert NL to local end of line convention */
 #define ZCRESUM	  3	   /* resume interrupted file transfer */
-/* Management include options, one of these ored in ZF1 */
+/* management include options, one of these ored in ZF1 */
 #define ZMSKNOLOC 0200 /* skip file if not present at rx */
-/* Management options, one of these ored in ZF1 */
+/* management options, one of these ored in ZF1 */
 #define ZMMASK	  037  /* mask for the choices below */
 #define ZMNEWL	  1	   /* transfer if source newer or longer */
 #define ZMCRC	  2	   /* transfer if different file CRC or length */
 #define ZMAPND	  3	   /* append contents to existing file (if any) */
 #define ZMCLOB	  4	   /* replace existing file */
 #define ZMNEW	  5	   /* transfer if source newer */
-	/* Number 5 is alive ... */
+/* number 5 is alive ... */
 #define ZMDIFF	  6	   /* transfer if dates or lengths different */
 #define ZMPROT	  7	   /* protect destination file */
 #define ZMCHNG	  8	   /* change filename if destination exists */
-/* Transport options, one of these in ZF2 */
+/* transport options, one of these in ZF2 */
 #define ZTLZW	  1	   /* lempel-Ziv compression */
 #define ZTRLE	  3	   /* run Length encoding */
-/* Extended options for ZF3, bit encoded */
+/* extended options for ZF3, bit encoded */
 #define ZXSPARS	  64   /* encoding for sparse file operations */
 #define ZCANVHDR  01   /* variable headers OK */
-/* Receiver window size override */
+/* receiver window size override */
 #define ZRWOVR    4	   /* byte position for receive window override/256 */
 
-/* Parameters for ZCOMMAND frame ZF0 (otherwise 0) */
+/* parameters for ZCOMMAND frame ZF0 (otherwise 0) */
 #define ZCACK1	  1	   /* acknowledge, then do command */
 extern char Attn[ZATTNLEN+1];	/* Attention string rx sends to tx on err */
 
-/* Globals used by ZMODEM functions */
-extern rt_uint8_t  Rxframeind; /* ZBIN ZBIN32, or ZHEX type of frame */
-extern char header_type;	   /* type of header received */
-extern rt_uint8_t  rx_header[4];		/* received header */
-extern rt_uint8_t  tx_header[4];		/* transmitted header */
-extern rt_uint8_t  Txfcs32;		/* TRUE means send binary frames with 32 bit FCS */
-extern rt_uint16_t Rxcount;	   /* count of data bytes received */
-extern rt_uint16_t Rxtimeout;  /* tenths of seconds to wait for something */
-extern rt_uint32_t Rxpos;	   /* received file position */
-extern rt_uint32_t Txpos;	   /* transmitted file position */
-extern rt_uint8_t  Txfcs32;	   /* TURE means send binary frames with 32 bit FCS */
+/* globals used by ZMODEM functions */
+extern rt_uint8_t  Rxframeind;     /* ZBIN ZBIN32, or ZHEX type of frame */
+extern char header_type;	       /* type of header received */
+extern rt_uint8_t  rx_header[4];   /* received header */
+extern rt_uint8_t  tx_header[4];   /* transmitted header */
+extern rt_uint8_t  Txfcs32;		   /* TRUE means send binary frames with 32 bit FCS */
+extern rt_uint16_t Rxcount;	       /* count of data bytes received */
+extern rt_uint16_t Rxtimeout;      /* tenths of seconds to wait for something */
+extern rt_uint32_t Rxpos;	       /* received file position */
+extern rt_uint32_t Txpos;	       /* transmitted file position */
+extern rt_uint8_t  Txfcs32;	       /* TURE means send binary frames with 32 bit FCS */
 
 /* ward Christensen / CP/M parameters - Don't change these! */
 #define ENQ     005
@@ -153,18 +153,18 @@ extern rt_uint8_t  Txfcs32;	   /* TURE means send binary frames with 32 bit FCS
 #define ERRORMAX 5
 #define RETRYMAX 5
 #define WCEOT   (-10)
-#define PATHLEN  257	/* ready for 4.2 bsd ? */
-#define UNIXFILE 0xF000	/* the S_IFMT file mask bit for stat */
+ 
+ 
 
 
 
 #define BITRATE 115200
-#define TX_BUFFER_SIZE 1024
-#define RX_BUFFER_SIZE  1024	  /*sender or receiver's max buffer size */
-extern char ZF0_CMD;	/* local ZMODEM file conversion request */
-extern char ZF1_CMD;	/* local ZMODEM file management request */
-extern char ZF2_CMD;	/* local ZMODEM file management request */
-extern char ZF3_CMD;	/* local ZMODEM file management request */
+#define TX_BUFFER_SIZE  1024
+#define RX_BUFFER_SIZE  1024	  /* sender or receiver's max buffer size */
+extern char ZF0_CMD;	          /* local ZMODEM file conversion request */
+extern char ZF1_CMD;	          /* local ZMODEM file management request */
+extern char ZF2_CMD;	          /* local ZMODEM file management request */
+extern char ZF3_CMD;	          /* local ZMODEM file management request */
 extern rt_uint32_t Baudrate ;
 extern rt_uint32_t Left_bytes;
 extern rt_uint32_t Left_sizes;
@@ -191,7 +191,7 @@ struct zfile
 };
 extern struct finsh_shell* shell;
  
-#define ZDEBUG 1
+#define ZDEBUG 0
 /* sz.c */
 extern void zs_start(char *path);
 /* rz.c */
@@ -207,7 +207,7 @@ extern void zsend_bin_data(rt_uint8_t *buf, rt_int16_t len, rt_uint8_t frameend)
 extern void zput_pos(rt_uint32_t pos);
 extern void zget_pos(rt_uint32_t pos);
 /* zdevice.c */
-extern rt_uint32_t get_device_speed(void);
+extern rt_uint32_t get_device_baud(void);
 extern void zsend_byte(rt_uint16_t c);
 extern void zsend_line(rt_uint16_t c);
 extern rt_int16_t zread_line(rt_uint16_t timeout);

+ 15 - 9
components/utilities/zmodem/zdevice.c

@@ -22,7 +22,7 @@ rt_uint32_t Baudrate   = BITRATE; 	 /* console baudrate */
  
 
 
-rt_uint32_t get_device_speed(void)
+rt_uint32_t get_device_baud(void)
 {
     return(Baudrate);
 }
@@ -35,23 +35,25 @@ rt_uint32_t get_sys_time(void)
 void zsend_byte(rt_uint16_t ch)
 {
     rt_device_write(zmodem.device,0,&ch,1);  
+
     return;
 }
 
 void zsend_line(rt_uint16_t c)
 {
     rt_uint16_t ch;
+
 	ch = (c & 0377);
     rt_device_write(zmodem.device,0,&ch,1);   
+
     return;
-}
-
-static char buf[RX_BUFFER_SIZE + 1];
+}
 
 rt_int16_t zread_line(rt_uint16_t timeout)
 {
 	char *str;	 
-	
+	static char buf[10];
+
 	if (Line_left > 0)
 	{
 	    Line_left -= 1;
@@ -61,7 +63,7 @@ rt_int16_t zread_line(rt_uint16_t timeout)
 	timeout/=5;
 	while (1)
 	{
-     	if (rt_sem_take(&zmodem.zsem, RT_TICK_PER_SECOND*timeout) != RT_EOK) continue;
+//     	if (rt_sem_take(&zmodem.zsem, RT_TICK_PER_SECOND*timeout) != RT_EOK) continue;
      	Line_left = rt_device_read(shell->device, 0, buf, 1);
 		if (Line_left)
 		{
@@ -72,6 +74,7 @@ rt_int16_t zread_line(rt_uint16_t timeout)
 	}
 	if (Line_left < 1) return TIMEOUT;
 	Line_left -=1;
+
 	return (*str++ & 0377);
 }
 
@@ -100,10 +103,13 @@ void zsend_break(char *cmd)
 /* send cancel string to get the other end to shut up */
 void zsend_can(void)
 {
-	static char cmd[] = {24,24,24,24,24,24,24,24,24,24,0};
+	static char cmd[] = {24,24,24,24,24,24,24,24,24,24,0};
+
 	zsend_break(cmd);
 	rt_kprintf("\x0d");
-	Line_left=0;	       /* clear Line_left */
+	Line_left=0;	       /* clear Line_left */
+
+	return;
 }
 
-/* end of rbsb.c */
+/* end of zdevice.c */

+ 1 - 1
components/utilities/zmodem/zstart.c

@@ -38,7 +38,7 @@ void finsh_rz(void *parameter)
     /* save old rx_indicate	*/
     rx_indicate = zmodem.device->rx_indicate;
     /* set new rx_indicate */
-    rt_device_set_rx_indicate(zmodem.device, zmodem_rx_ind);
+    rt_device_set_rx_indicate(zmodem.device, RT_NULL);
 	/* start receive remote files */
     zr_start(path);
 	zmodem.device->flag |=flag;