xref: /nrf52832-nimble/rt-thread/components/utilities/zmodem/zdevice.c (revision 167494296f0543431a51b6b1b83e957045294e05)
1 /*
2  * File      : zdevice.c
3  * the implemention of zmodem protocol.
4  * Change Logs:
5  * Date           Author       Notes
6  * 2011-03-29     itspy
7  */
8 
9 #include <rtthread.h>
10 #include <finsh.h>
11 #include <shell.h>
12 #include <rtdef.h>
13 #include <dfs.h>
14 #include <dfs_file.h>
15 #include <dfs_posix.h>
16 #include "zdef.h"
17 
18 
19 rt_uint32_t Line_left  = 0;		     /* left number of data in the read line buffer*/
20 rt_uint32_t Left_sizes = 0;			 /* left file sizes */
21 rt_uint32_t Baudrate   = BITRATE; 	 /* console baudrate */
22 
23 
24 
25 rt_uint32_t get_device_baud(void)
26 {
27     return(Baudrate);
28 }
29 
30 rt_uint32_t get_sys_time(void)
31 {
32     return(0L);
33 }
34 
35 void zsend_byte(rt_uint16_t ch)
36 {
37     rt_device_write(zmodem.device, 0, &ch,1);
38 
39     return;
40 }
41 
42 void zsend_line(rt_uint16_t c)
43 {
44     rt_uint16_t ch;
45 
46 	ch = (c & 0377);
47     rt_device_write(zmodem.device, 0, &ch, 1);
48 
49     return;
50 }
51 
52 rt_int16_t zread_line(rt_uint16_t timeout)
53 {
54 	char *str;
55 	static char buf[10];
56 
57 	if (Line_left > 0)
58 	{
59 	    Line_left -= 1;
60 		return (*str++ & 0377);
61 	}
62 	Line_left = 0;
63 	timeout/=5;
64 	while (1)
65 	{
66      	Line_left = rt_device_read(shell->device, 0, buf, 1);
67 		if (Line_left)
68 		{
69 		     Line_left = Line_left;
70 		     str = buf;
71 			 break;
72 		}
73 	}
74 	if (Line_left < 1) return TIMEOUT;
75 	Line_left -=1;
76 
77 	return (*str++ & 0377);
78 }
79 
80 /*
81  * send a string to the modem, processing for \336 (sleep 1 sec)
82  *   and \335 (break signal)
83  */
84 void zsend_break(char *cmd)
85 {
86 
87 	while (*cmd++)
88 	{
89 		switch (*cmd)
90 		{
91 		case '\336':
92 			 continue;
93 		case '\335':
94 		     rt_thread_delay(RT_TICK_PER_SECOND);
95 			 continue;
96 		default:
97 			 zsend_line(*cmd);
98 			 break;
99 		}
100 	}
101 }
102 /* send cancel string to get the other end to shut up */
103 void zsend_can(void)
104 {
105 	static char cmd[] = {24,24,24,24,24,24,24,24,24,24,0};
106 
107 	zsend_break(cmd);
108 	rt_kprintf("\x0d");
109 	Line_left=0;	       /* clear Line_left */
110 
111 	return;
112 }
113 
114 /* end of zdevice.c */
115