blob: dfdd8694f0a6b2010ee9cc06db1116ec770945ca [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/**
2 * ±¾³ÌÐò·ûºÏGPLÌõÔ¼
3 * Beneboy 2003-5-16
4 */
5#include <stdio.h> // printf
6#include <fcntl.h> // open
7#include <string.h> // bzero
8#include <stdlib.h> // exit
9#include <termios.h> //termios, tcgetattr(), tcsetattr()
10#include <unistd.h>
11#include "port_com.h"
12
13// ½ÓÊÕ³¬Ê±
14#define TIMEOUT_SEC(buflen,baud) (buflen*20/baud+1)
15#define TIMEOUT_USEC 0
16
17// serial port information
18portinfo_t g_sPortInfo =
19{
20 '0', // print prompt after receiving
21 115200, // baudrate: 115200
22 '8', // databit: 8
23 '0', // debug: off
24 '0', // echo: off
25 '0', // flow control: software
26 '0', // default tty: COM1
27 '0', // parity: none
28 '1', // stopbit: 1
29 0 // reserved
30};
31
32/**
33 * ²¨ÌØÂÊת»¯×ª»»º¯Êý
34 */
35int convbaud(unsigned long int baudrate)
36{
37 switch (baudrate)
38 {
39 case 2400:
40 return B2400;
41 case 4800:
42 return B4800;
43 case 9600:
44 return B9600;
45 case 19200:
46 return B19200;
47 case 38400:
48 return B38400;
49 case 57600:
50 return B57600;
51 case 115200:
52 return B115200;
53 default:
54 return B9600;
55 }
56}
57
58/**
59 * Setup comm attr
60 * fd: ´®¿ÚÎļþÃèÊö·û, pportinfo: ´ýÉèÖõĶ˿ÚÐÅÏ¢s
61 *
62 */
63int PortSet(int fd)
64{
65 struct termios termios_old, termios_new;
66 int baudrate, tmp;
67 char databit, stopbit, parity, fctl;
68 bzero(&termios_old, sizeof(termios_old));
69 bzero(&termios_new, sizeof(termios_new));
70 cfmakeraw(&termios_new);
71 tcgetattr(fd, &termios_old); //get the serial port attributions
72 /*------------ÉèÖö˿ÚÊôÐÔ----------------*/
73 //baudrates
74 baudrate = convbaud(g_sPortInfo.baudrate);
75 cfsetispeed(&termios_new, baudrate); // ÌîÈë´®¿ÚÊäÈë¶Ë²¨ÌØÂÊ
76 cfsetospeed(&termios_new, baudrate); // ÌîÈë´®¿ÚÊä³ö¶Ë²¨ÌØÂÊ
77 termios_new.c_cflag |= CLOCAL; // ¿ØÖÆÄ£Ê½, ±£Ö¤³ÌÐò²»»á³ÉΪ¶Ë¿ÚµÄÕ¼ÓÐÕß
78 termios_new.c_cflag |= CREAD; // ¿ØÖÆÄ£Ê½, ʹÄܶ˿ڶÁÈ¡ÊäÈëµÄÊý¾Ý
79 // ¿ØÖÆÄ£Ê½, flow control
80 fctl = g_sPortInfo.fctl;
81 switch (fctl)
82 {
83 case '0':
84 {
85 termios_new.c_cflag &= ~CRTSCTS; //no flow control
86 }
87 break;
88 case '1':
89 {
90 termios_new.c_cflag |= CRTSCTS; //hardware flow control
91 }
92 break;
93 case '2':
94 {
95 termios_new.c_iflag |= IXON | IXOFF | IXANY; //software flow control
96 }
97 break;
98 default:
99 {
100 printf("Unknown fctl %c\n", fctl);
101 break;
102 }
103 }
104
105 // ¿ØÖÆÄ£Ê½, data bits
106 termios_new.c_cflag &= ~CSIZE; // ¿ØÖÆÄ£Ê½, ÆÁ±Î×Ö·û´óСλ
107 databit = g_sPortInfo.databit;
108 switch (databit)
109 {
110 case '5':
111 termios_new.c_cflag |= CS5;
112 //lint -fallthrough
113 case '6':
114 termios_new.c_cflag |= CS6;
115 //lint -fallthrough
116 case '7':
117 termios_new.c_cflag |= CS7;
118 //lint -fallthrough
119 default:
120 termios_new.c_cflag |= CS8;
121 }
122
123 // ¿ØÖÆÄ£Ê½ parity check
124 parity = g_sPortInfo.parity;
125 switch (parity)
126 {
127 case '0':
128 {
129 termios_new.c_cflag &= ~PARENB; // no parity check
130 }
131 break;
132 case '1':
133 {
134 termios_new.c_cflag |= PARENB; // odd check
135 termios_new.c_cflag &= ~PARODD;
136 }
137 break;
138 case '2':
139 {
140 termios_new.c_cflag |= PARENB; // even check
141 termios_new.c_cflag |= PARODD;
142 }
143 break;
144 default:
145 {
146 printf("Unknown parity %c\n", parity);
147 break;
148 }
149 }
150
151 // ¿ØÖÆÄ£Ê½, stop bits
152 stopbit = g_sPortInfo.stopbit;
153 if (stopbit == '2')
154 {
155 termios_new.c_cflag |= CSTOPB; // 2 stop bits
156 }
157 else
158 {
159 termios_new.c_cflag &= ~CSTOPB; // 1 stop bits
160 }
161
162 //other attributions default
163 termios_new.c_oflag &= ~OPOST; // Êä³öģʽ, ԭʼÊý¾ÝÊä³ö
164 termios_new.c_cc[VMIN] = 1; // ¿ØÖÆ×Ö·û, ËùÒª¶ÁÈ¡×Ö·ûµÄ×îСÊýÁ¿
165 termios_new.c_cc[VTIME] = 1; // ¿ØÖÆ×Ö·û, ¶ÁÈ¡µÚÒ»¸ö×Ö·ûµÄµÈ´ýʱ¼ä, unit: (1/10)second
166 tcflush(fd, TCIFLUSH); // Òç³öµÄÊý¾Ý¿ÉÒÔ½ÓÊÕ,µ«²»¶Á
167 tmp = tcsetattr(fd, TCSANOW, &termios_new); // ÉèÖÃÐÂÊôÐÔ, TCSANOW: ËùÓɸıäÁ¢¼´ÉúЧ
168 tcgetattr(fd, &termios_old);
169 return(tmp);
170}
171
172/**
173 * Open serial port
174 * tty: ¶Ë¿ÚºÅ ttyS0, ttyS1, ....
175 * ·µ»ØÖµÎª´®¿ÚÎļþÃèÊö·û
176 */
177int PortOpen(pportinfo_t pportinfo, int mode)
178{
179 return 0;
180}
181
182/**
183 * Close serial port
184 */
185void PortClose(int fd)
186{
187 close(fd);
188}
189
190/**
191 * PortSend
192 * fd: ´®¿ÚÃèÊö·û,
193 * data: ´ý·¢ËÍÊý¾ÝÖ¸Õë
194 * datalen: Êý¾Ý³¤¶È
195 * flag: ·¢ËÍ·½Ê½±êʶ
196 * ·µ»ØÊµ¼Ê·¢Ëͳ¤¶È
197 */
198int PortSend(int fd, unsigned char* data, int datalen)
199{
200 int len = 0;
201
202 len = write(fd, data, datalen); //ʵ¼ÊдÈëµÄ³¤¶È
203 if (len == datalen)
204 {
205 return (len);
206 }
207 else
208 {
209 tcflush(fd, TCOFLUSH);
210 return -1;
211 }
212}
213
214/**
215 * PortRecv
216 * ²ÎÊý£º
217 * fd: ´®¿ÚÃèÊö·û,
218 * data: ´ý½ÓÊÕÊý¾ÝÖ¸Õë
219 * datalen: Êý¾Ý³¤¶È
220 * flag: ½ÓÊÕ·½Ê½±êʶ
221 * ·µ»ØÊµ¼Ê¶ÁÈëµÄ×Ö½ÚÊý
222 */
223int PortRecv(int fd, unsigned char* data, int datalen)
224{
225#if 0
226 int readlen, fs_sel;
227 fd_set fs_read;
228 struct timeval tv_timeout;
229 FD_ZERO(&fs_read);
230 FD_SET(fd, &fs_read);
231
232 /*RAT ¸ÃÖµÓ¦¸Ã¼õСÒÔÌá¸ßÏìÓ¦ËÙ¶È*/
233 tv_timeout.tv_sec = 0;/*TIMEOUT_SEC(datalen, baudrate);*/
234
235 tv_timeout.tv_usec = 200;/*TIMEOUT_USEC;*/
236
237 fs_sel = select(fd + 1, &fs_read, NULL, NULL, &tv_timeout);
238 if (fs_sel)
239 {
240
241 printf("rpmsg: portrecv fd =%d\n",fd);
242 readlen = read(fd, data, datalen);
243 return(readlen);
244 }
245 else
246 {
247 return(0);
248 }
249#else
250 int readlen = read(fd, data, datalen);
251 return(readlen);
252#endif
253}
254
255int tty_set_log_port(int fd)
256{
257 if (fd < 0)
258 {
259 printf("tty_set_port failed. %d\n", fd);
260 return 0;
261 }
262 printf("tty_set_port ok.\n");
263 //ioctl(g_AtProxy_RpProxyFd, (('R'<<8)|6|(0x4004<<16)), 0x400);
264 //printf("rpmsg: ap port open fd = %d\n",fd);//add shideyou
265 //ioctl(fd, (('R'<<8)|1|(0x4004<<16)), 0x400);
266 //ioctl(fd, (('R'<<8)|4|(0x4004<<16)), 0);
267 PortSet(fd);
268 //ZTE_LOG(LOG_DEBUG, "port is open!");
269 return 1;
270}
271
272