blob: 7b6d2a314666bb45b57d65c685397a06d6e53619 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001
2
3#include <fcntl.h> // open
4#include <string.h> // bzero
5#include <stdlib.h> // exit
6#include <sys/types.h> // pid_t
7//#include "ufi_def.h"
8#include "os_type_def.h"
9
10#include "cc_at.h"
11#include "cc_proc.h"
12#include "cc_main.h"
13#include <termios.h> //termios, tcgetattr(), tcsetattr()
14#ifndef TEST_CCAPP
15#include <unistd.h>
16#include <sys/ioctl.h> // ioctl
17#include <sys/times.h> // times
18#endif
19
20//ÈçºÎ¶¨Òå
21#define ZCC_APP_AT_COMM_CC "/dev/armps_rpmsgch12"
22#define ZCC_APP_AT_COMM_SS "/dev/armps_rpmsgch13"
23#define ZCC_APP_AT_COMM_DEV "/dev/armps_rpmsgch14"
24#define ZCC_APP_AT_COMM_IND "/dev/armps_rpmsgch15"
25
26
27static int g_zCcApp_ComFd[4] = {0};
28
29/*******************************************
30 * ²¨ÌØÂÊת»¯×ª»»º¯Êý
31 ********************************************/
32static int convbaud(unsigned long int baudrate)
33{
34 switch (baudrate)
35 {
36 case 2400:
37 return B2400;
38 case 4800:
39 return B4800;
40 case 9600:
41 return B9600;
42 case 19200:
43 return B19200;
44 case 38400:
45 return B38400;
46 case 57600:
47 return B57600;
48 case 115200:
49 return B115200;
50 default:
51 return B9600;
52 }
53}
54
55#ifndef TEST_CCAPP
56int zCc_CleanCCMSG()
57{
58 int ret = -1;
59 //get CC fd
60 int fd = zCcApp_GetAtCCFd();
61 fd_set readfdForCC;
62
63 struct timeval tv={0};
64
65 while(1)
66 {
67 //0s timeout
68 tv.tv_sec = 0;
69 FD_ZERO(&readfdForCC);
70 FD_SET(fd, &readfdForCC);
71 ret = select(fd + 1, &readfdForCC, NULL, NULL, &tv);
72 if(ret<0)
73 {
74 continue;
75 }
76 //"Select timeout"
77 if(ret == 0)
78 {
79 break;
80 }
81
82 if(FD_ISSET(fd, &readfdForCC))
83 {
84 char AtBuf[ZCC_APP_AT_BUF_MAX_LEN] = {0};
85 int readlen = 0;
86 readlen = read(fd, AtBuf, ZCC_APP_AT_BUF_MAX_LEN);
87 }
88 else
89 {
90 continue;
91 }
92 }
93
94 return 0;
95}
96
97int zCcApp_GetAtCmdFd(void)
98{
99 int fd = 0;
100 E_CC_APP_AtReqCmdId AtChnlCmdId = g_Cc_CallDetail.curAtCmdId;
101
102 switch (AtChnlCmdId)
103 {
104 case CC_APP_AT_CMD_REQ_A:
105 case CC_APP_AT_CMD_REQ_D:
106 case CC_APP_AT_CMD_REQ_CHUP:
107 case CC_APP_AT_CMD_REQ_SET_CHLD:
108 case CC_APP_AT_CMD_REQ_VTS:
109 case CC_APP_AT_CMD_REQ_IMSPLUS:
110 fd = g_zCcApp_ComFd[1];
111 break;
112
113
114 case CC_APP_AT_CMD_REQ_GET_CLCC:
115 case CC_APP_AT_CMD_REQ_CMUT:
116 case CC_APP_AT_CMD_REQ_CLVL:
117 fd = g_zCcApp_ComFd[3];
118 break;
119
120
121
122 case CC_APP_AT_CMD_REQ_SET_CCFC:
123 case CC_APP_AT_CMD_REQ_SET_CCWA:
124 case CC_APP_AT_CMD_REQ_SET_CLCK:
125 case CC_APP_AT_CMD_REQ_GET_CCFC:
126 case CC_APP_AT_CMD_REQ_GET_CCWA:
127 case CC_APP_AT_CMD_REQ_SET_CPWD:
128 fd = g_zCcApp_ComFd[2];
129 break;
130
131 default:
132 break;
133 }
134
135 return fd;
136}
137/********************************************
138 * send data
139 * fdcom: ´®¿ÚÃèÊö·û, data: ´ý·¢ËÍÊý¾Ý, datalen: Êý¾Ý³¤¶È
140 * ·µ»ØÊµ¼Ê·¢Ëͳ¤¶È
141 *********************************************/
142int zCcApp_PortSend(int fdcom, CHAR *data, int datalen)
143{
144 int len = 0;
145 zte_log_append(__FILE__, __LINE__, "zte_ccapp.log","%s zCcApp_PortSend:AtcmdMsg = %s\n",__FUNCTION__,data);
146 len = write(fdcom, data, datalen); //ʵ¼ÊдÈëµÄ³¤¶È
147 if (len == datalen)
148 {
149 zte_log_append(__FILE__, __LINE__, "zte_ccapp.log","%s zCcApp_PortSend succ fdcom=%d,data=%s,len=%d\n <---\n",__FUNCTION__,fdcom,data,datalen);
150 return len;
151 }
152 else
153 {
154 zte_log_append(__FILE__, __LINE__, "zte_ccapp.log","%s zCcApp_PortSend fail fdcom=%d,data=%s,len=%d\n <---\n",__FUNCTION__,fdcom,data,datalen);
155 tcflush(fdcom, TCOFLUSH);
156 return -1;
157 }
158}
159
160/*******************************************
161 * receive data
162 * ·µ»ØÊµ¼Ê¶ÁÈëµÄ×Ö½ÚÊý
163 *
164 ********************************************/
165int zCcApp_PortRecv(int fdcom, CHAR *data, int datalen)
166{
167
168 char AtBuf[ZCC_APP_AT_BUF_MAX_LEN] = {0};
169 int readlen = 0;
170 readlen = read(fdcom, AtBuf, ZCC_APP_AT_BUF_MAX_LEN);
171 if (readlen >= 0) { //cov m
172 strncpy(data,AtBuf,readlen);
173 }
174
175 return readlen;
176}
177
178/*******************************************
179 * Setup comm attr
180 * fdcom: ´®¿ÚÎļþÃèÊö·û, pportinfo: ´ýÉèÖõĶ˿ÚÐÅÏ¢s
181 *
182 ********************************************/
183int zCcApp_PortSet(int fdcom, T_CC_APP_Portinfo *pPortinfo)
184{
185 struct termios termios_old, termios_new;
186 int baudrate, tmp;
187 char databit, stopbit, parity, fctl;
188 bzero(&termios_old, sizeof(termios_old));
189 bzero(&termios_new, sizeof(termios_new));
190 cfmakeraw(&termios_new);
191 tcgetattr(fdcom, &termios_old); //get the serial port attributions
192
193 /*------------ÉèÖö˿ÚÊôÐÔ----------------*/
194 //baudrates
195 baudrate = convbaud(pPortinfo->baudrate);
196 cfsetispeed(&termios_new, baudrate); //ÌîÈë´®¿ÚÊäÈë¶Ë²¨ÌØÂÊ
197 cfsetospeed(&termios_new, baudrate); //ÌîÈë´®¿ÚÊä³ö¶Ë²¨ÌØÂÊ
198 termios_new.c_cflag |= CLOCAL; //¿ØÖÆÄ£Ê½, ±£Ö¤³ÌÐò²»»á³ÉΪ¶Ë¿ÚµÄÕ¼ÓÐÕß
199 termios_new.c_cflag |= CREAD; //¿ØÖÆÄ£Ê½, ʹÄܶ˿ڶÁÈ¡ÊäÈëµÄÊý¾Ý
200
201 // ¿ØÖÆÄ£Ê½, flow control
202 fctl = pPortinfo->fctl;
203 switch (fctl)
204 {
205 case '0':
206 {
207 termios_new.c_cflag &= ~CRTSCTS; //no flow control
208 break;
209 }
210 case '1':
211 {
212 termios_new.c_cflag |= CRTSCTS; //hardware flow control
213 break;
214 }
215 case '2':
216 {
217 termios_new.c_iflag |= IXON | IXOFF | IXANY; //software flow control
218 break;
219 }
220 default:
221 {
222 //ZTE_LOG(LOG_ERR, "Unknown fctl %c\n", fctl);
223 break;
224 }
225 }
226
227 //¿ØÖÆÄ£Ê½, data bits
228 termios_new.c_cflag &= ~CSIZE; //¿ØÖÆÄ£Ê½, ÆÁ±Î×Ö·û´óСλ
229 databit = pPortinfo->databit;
230 switch (databit)
231 {
232 case '5':
233 termios_new.c_cflag |= CS5;
234 //lint -fallthrough
235 case '6':
236 termios_new.c_cflag |= CS6;
237 //lint -fallthrough
238 case '7':
239 termios_new.c_cflag |= CS7;
240 //lint -fallthrough
241 default:
242 termios_new.c_cflag |= CS8;
243 }
244
245 //¿ØÖÆÄ£Ê½ parity check
246 parity = pPortinfo->parity;
247 switch (parity)
248 {
249 case '0':
250 {
251 termios_new.c_cflag &= ~PARENB; //no parity check
252 break;
253 }
254 case '1':
255 {
256 termios_new.c_cflag |= PARENB; //odd check
257 termios_new.c_cflag &= ~PARODD;
258 break;
259 }
260 case '2':
261 {
262 termios_new.c_cflag |= PARENB; //even check
263 termios_new.c_cflag |= PARODD;
264 break;
265 }
266 default:
267 {
268 //ZTE_LOG(LOG_ERR, "Unknown parity %c\n", parity);
269 break;
270 }
271 }
272
273 //¿ØÖÆÄ£Ê½, stop bits
274 stopbit = pPortinfo->stopbit;
275 if (stopbit == '2')
276 {
277 termios_new.c_cflag |= CSTOPB; //2 stop bits
278 }
279 else
280 {
281 termios_new.c_cflag &= ~CSTOPB; //1 stop bits
282 }
283 //other attributions default
284 termios_new.c_oflag &= ~OPOST; //Êä³öģʽ, ԭʼÊý¾ÝÊä³ö
285 termios_new.c_cc[VMIN] = 1; //¿ØÖÆ×Ö·û, ËùÒª¶ÁÈ¡×Ö·ûµÄ×îСÊýÁ¿
286 termios_new.c_cc[VTIME] = 1; //¿ØÖÆ×Ö·û, ¶ÁÈ¡µÚÒ»¸ö×Ö·ûµÄµÈ´ýʱ¼ä, unit: (1/10)second
287 tcflush(fdcom, TCIFLUSH); //Òç³öµÄÊý¾Ý¿ÉÒÔ½ÓÊÕ,µ«²»¶Á
288 tmp = tcsetattr(fdcom, TCSANOW, &termios_new); //ÉèÖÃÐÂÊôÐÔ, TCSANOW: ËùÓɸıäÁ¢¼´ÉúЧ
289
290 tcgetattr(fdcom, &termios_old);
291
292 //printf("%s \n",__FUNCTION__);
293 //printf("%s %d: tty %d,baudrate %d,stopbit %d, parity%d, databit %d\n",__FUNCTION__,termios_old.tty,termios_old.baudrate,termios_old.stopbit,termios_old.parity,termios_old.databit);
294 return(tmp);
295}
296
297
298/*******************************************
299 * Open serial port
300 * DevName: "/dev/armps_rpmsgch9"
301 * ·µ»ØÖµÎª´®¿ÚÎļþÃèÊö·û
302 ********************************************/
303int zCcApp_PortOpen(char* DevName)
304{
305
306 // serial port information
307 T_CC_APP_Portinfo PortInfo = {'0',115200,'8', '0','0','0','0','0','1', 0 };
308 int ComFd =0 ;//serial port handle
309
310 int nTryOpen = 0;
311 ComFd = open(DevName, O_RDWR);
312 while (ComFd < 0)
313 {
314 //assert(nTryOpen < ZCC_APP_MAX_OPEN_PORT_TIMES);
315 nTryOpen++;
316 zCc_sleep(500);//500 ms later to try again
317 ComFd = open(DevName, O_RDWR);
318 }
319
320 if (ioctl(ComFd, (('R'<<8)|1|(0x4004<<16)), 0x400) < 0) {//cov m
321 zte_log_append(__FILE__, __LINE__, "zte_ccapp.log","%s ioctl1 fail\n <---\n",__FUNCTION__);
322 }
323 if (ioctl(ComFd, (('R'<<8)|4|(0x4004<<16)), 0) < 0) {//cov m
324 zte_log_append(__FILE__, __LINE__, "zte_ccapp.log","%s ioctl2 fail\n <---\n",__FUNCTION__);
325 }
326 zCcApp_PortSet(ComFd, &PortInfo);
327 return ComFd;
328}
329
330
331int zCcApp_StartComm(void)
332{
333 /*´ò¿ª´®¿Ú*/
334 g_zCcApp_ComFd[1] = zCcApp_PortOpen(ZCC_APP_AT_COMM_CC);
335 g_zCcApp_ComFd[2] = zCcApp_PortOpen(ZCC_APP_AT_COMM_SS);
336 g_zCcApp_ComFd[3] = zCcApp_PortOpen(ZCC_APP_AT_COMM_DEV);
337 g_zCcApp_ComFd[0] = zCcApp_PortOpen(ZCC_APP_AT_COMM_IND);
338 return 0;
339}
340
341int zCcApp_GetAtIndFd(void)
342{
343 int fd = 0;
344 fd = g_zCcApp_ComFd[0];
345
346 return fd;
347
348}
349
350int zCcApp_GetAtCCFd(void)
351{
352 int fd = 0;
353 fd = g_zCcApp_ComFd[1];
354
355 return fd;
356
357}
358#endif
359
360