blob: ee491be0df03a4eed27f09269318dd41529d8ade [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/**
2 *
3 * @file log_agent_usblog.c
4 * @brief
5 * This file is part of ZCAT.
6 * zcatÓ¦Óòãlog_agent´¦Àíͨ¹ýusbµÄÊý¾ÝÁ÷
7 *
8 * @details
9 * @author Tools Team.
10 * @email
11 * @copyright Copyright (C) 2013 Sanechips Technology Co., Ltd.
12 * @warning
13 * @date 2019/02/22
14 * @version 1.4
15 * @pre
16 * @post
17 *
18 * @par
19 * Change History :
20 * ---------------------------------------------------------------------------
21 * date version author description
22 * ---------------------------------------------------------------------------
23 * 2017/07/17 1.0 hou.bing Create file
24 * 2019/01/24 1.1 jiang.fenglin 1.Ìí¼Óusblog¶ÁÐ´Ëø
25 * 2.Ìí¼ÓÏß³ÌÃû³Æ
26 * 2019/01/25 1.2 jiang.fenglin Ôö¼ÓAPUSBģʽÏÂtty·¾¶ÅäÖù¦ÄÜ
27 * 2019/02/02 1.3 jiang.fenglin ÐÞ¸Ä×¢ÊÍ·½Ê½Îªdoxygen
28 * 2019/02/22 1.4 jiang.fenglin ¶¯Ì¬¿ÉÅäÖÃLOG¶Ë¿Ú,log¶Ë¿Ú¶¯Ì¬ÅäÖÃÖ»Õë¶ÔusbÅäÖÃΪÕý³£Ä£Ê½µÄÇé¿ö£¬¹¤³§Ä£Ê½±£³Ö²»±ä
29 * ---------------------------------------------------------------------------
30 *
31 *
32 */
33
34#include <unistd.h>
35#include <pthread.h>
36#include <stdio.h>
37#include <string.h>
38#include <sys/prctl.h>
39#include "cfg_api.h"
40#include "log_agent.h"
41#include "port_com.h"
42
43
44#define ZCAT_FACTORY_PATH "/sys/devices/virtual/android_usb/android0/f_acm/instances"
45#define ZCAT_DEFAULT_TTY "ttyGS2"
46char g_zcat_usblog[8] = { 't', 't', 'y', 'G', 'S', '2' };
47
48int usblog_fd = -1;
49pthread_t read_usb_thread = 0;
50unsigned char g_UsbLogBuf[2048 + 1] = { 0 };
51const struct timespec notimer_interval = {0, 300000}; // 20ms
52pthread_rwlock_t usblog_rwlock = PTHREAD_RWLOCK_INITIALIZER;
53
54extern int send_log_to_cp(unsigned char *buf, int len);
55extern int deal_with_encoded_data(unsigned char *buffer, int buf_len);
56extern int init_monitor_hotplug();
57extern int hb_flag;
58
59/**
60 * @brief ·¢ËÍÊý¾Ýµ½usbµÄlog¿Ú
61 * @param[in] buf ´ý·¢Ë͵ÄÊý¾Ý»º´æÖ¸Õë
62 * @param[in] len ´ý·¢Ë͵ÄÊý¾Ý³¤¶È
63 * @return void
64 * @note
65 * @see
66 */
67int send_message_to_usblog(unsigned char* buf, int len)
68{
69 int write_len = 0;
70 int i = 0;
71 int ret = -1;
72
73 if ( buf && len )
74 {
75 for(i = 0; i < 100; i++)
76 {
77 pthread_rwlock_rdlock(&usblog_rwlock);
78 if ( usblog_fd >= 0 )
79 {
80 write_len = write(usblog_fd, buf, len);
81 }
82 pthread_rwlock_unlock(&usblog_rwlock);
83
84 //printf("send_message_to_usblog %8d,%8d\n", len, write_len);
85
86 if ( write_len == len )
87 {
88 ret = 0;
89 break;
90 }
91 else if( write_len )
92 {
93 len -= write_len;
94 }
95 else
96 {
97 nanosleep(&notimer_interval, NULL);
98 }
99 }
100 }
101
102 return ret;
103}
104
105/**
106 * @brief ÐÄÌø°üºÍ¹æÔòÎļþ´¦ÀíÏ̺߳¯Êý£¬\n
107 * ½ÓÊÕpc´«Ë͵½ueµÄÊý¾Ý£¬×ª·¢¸ø¹æÔò´¦ÀíºÍÐÄÌø¼à²â
108 * @param[in] args Ï̺߳¯Êý²ÎÊý
109 * @return void
110 * @note
111 * @see
112 */
113static void *read_usblog(void *args)
114{
115 unsigned char *pbuf = NULL;
116 int read_len = 0;
117
118 pbuf = g_UsbLogBuf;
119 /*
120 if(pbuf == NULL)
121 {
122 printf("[zcat] read_usblog fail.\n");
123 return NULL;
124 }*/
125
126 prctl(PR_SET_NAME, "read_usblog");
127
128 while(1)
129 {
130 pthread_rwlock_rdlock(&usblog_rwlock);
131 if( usblog_fd >= 0)
132 {
133 read_len = read(usblog_fd, pbuf, 2*1024);
134 }
135 pthread_rwlock_unlock(&usblog_rwlock);
136
137 if(read_len > 0)
138 {
139 //printf("[zcat] read_usblog %d bytes\n",read_len);
140 send_log_to_cp(pbuf, read_len); // Ïòcp·¢Ë͹æÔò
141 deal_with_encoded_data(pbuf, read_len); // Ïòap·¢Ë͹æÔò
142 }
143
144 if( usblog_fd < 0 || read_len <= 0 )
145 {
146 //printf("[zcat] read_usblog sleep 2\n");
147 sleep(2);
148 }
149 }
150
151 return NULL;
152}
153
154
155/**
156 * @brief »ñÈ¡Îļþ´óС
157 * @param[in] fp ÊäÈëÎļþÖ¸Õë
158 * @return Îļþ´óС£¬µ¥Î»×Ö½Ú
159 * @note
160 * @see
161 */
162int filelength(FILE *fp)
163{
164 int num;
165 fseek(fp, 0, SEEK_END);
166 num = ftell(fp);
167 fseek(fp, 0, SEEK_SET);
168 return num;
169}
170
171/**
172 * @brief ¶ÁÈ¡ÎļþÄÚÈÝ
173 * @param[in] path ÊäÈëÎļþ·¾¶
174 * @param[out] buf »º´æÖ¸Õë
175 * @param[in] len »º´æµÄ¿Õ¼ä´óС
176 * @return Îļþʵ¼Ê´óС£¬µ¥Î»×Ö½Ú
177 * @note
178 * @see
179 */
180static int readfile(char *path, char* buf, unsigned len)
181{
182 FILE *fp;
183 int length;
184 if((fp = fopen(path, "r")) == NULL)
185 {
186 printf("[zcat] open file %s error.\n", path);
187 return -1;
188 }
189 length = filelength(fp);
190 length = length > len ? len : length;
191 //ch=(char *)malloc(length+1);
192 fread(buf, (unsigned int)length, 1, fp);
193 fclose(fp);
194 *(buf + length) = '\0';
195 return length;
196}
197
198/**
199 * @brief ÅжÏÊÇ·ñΪ¹¤³§Ä£Ê½
200 * @param[in] void
201 * @return 1 - ¹¤³§Ä£Ê½£¬0 - Õý³£Ä£Ê½
202 * @note
203 * @see
204 */
205static int is_factory_mode()
206{
207 char buf[2] = { 0 };
208 int itemp;
209
210 readfile(ZCAT_FACTORY_PATH, buf, 1);
211
212 itemp = atoi(buf);
213 printf("[zcat] ZCAT_FACTORY_PATH : %d\n", itemp);
214
215 if(itemp == 2)
216 return 1;
217 else
218 return 0;
219}
220
221#if 0
222/**
223 * @brief дÎļþÄÚÈÝ
224 * @param[in] path ÊäÈëÎļþ·¾¶
225 * @param[in] buf »º´æÖ¸Õë
226 * @param[in] len »º´æµÄ¿Õ¼ä´óС
227 * @return ʵ¼ÊдÈëµÄ³¤¶È£¬µ¥Î»×Ö½Ú
228 * @note
229 * @see
230 */
231static int writefile(char*path, char*buf, unsigned len)
232{
233 FILE *fp;
234 int rtv;
235
236 if((fp = fopen(path, "w")) == NULL)
237 {
238 printf("[zcat] open file %s error.\n", path);
239 return -1;
240 }
241 rtv = fwrite(buf, len, 1, fp);
242 fclose(fp);
243 return rtv;
244}
245#endif
246
247/**
248 * @brief »ñÈ¡usblogµÄtty·¾¶
249 * @param[in] void
250 * @return void
251 * @note
252 * @see
253 */
254void get_usblog_tty_path()
255{
256 int isFactory = is_factory_mode();
257
258 memset(g_zcat_usblog, 0, sizeof(g_zcat_usblog));
259
260 if(isFactory)
261 {
262 sprintf(g_zcat_usblog, ZCAT_DEFAULT_TTY);
263 g_zcat_usblog[5] = '1'; // ¹¤³§Ä£Ê½ÏÂlog¿Ú¹Ì¶¨ÎªttyGS1£¬°´ÐèÇó²»Ö¸¶¨
264 }
265 else
266 {
267 // Èç¹ûÉèÖÃÁËtty·¾¶£¬ÔòʹÓÃÉèÖõÄ·¾¶£¬·ñÔòʹÓÃĬÈϵÄ
268 cfg_get_item("zcat_usblog", g_zcat_usblog, sizeof(g_zcat_usblog) - 1);
269 if (strlen(g_zcat_usblog) < 3)
270 {
271 sprintf(g_zcat_usblog, ZCAT_DEFAULT_TTY);
272 }
273 }
274}
275
276/**
277 * @brief ¹Ø±ÕusbÉ豸
278 * @param[in] void
279 * @return 0 on success, errno otherwise
280 * @note
281 * @see
282 */
283int close_usblog()
284{
285 int ret = -1;
286
287 pthread_rwlock_wrlock(&usblog_rwlock); // Ð´Ëø£¬¹Ø±Õ¶Ë¿Ú¹ý³ÌÖв»ÔÊÐí¶Áд
288 if(usblog_fd >= 0)
289 {
290 ret = close(usblog_fd);
291 if (ret < 0)
292 {
293 printf("[zcat] close usblog_dev faild, ret = %d, error = %s\n", ret, strerror(errno));
294 pthread_rwlock_unlock(&usblog_rwlock);
295 return ret;
296 }
297 else
298 {
299 usblog_fd = -1;
300 }
301 }
302 pthread_rwlock_unlock(&usblog_rwlock);
303
304 return 0;
305}
306
307/**
308 * @brief ´ò¿ªusbÉ豸
309 * @param[in] void
310 * @return ·µ»ØÉ豸ÃèÊö·û
311 * @note
312 * @see
313 */
314int open_usblog()
315{
316 int fd = -1;
317 char usblog_dev[16] = { 0 };
318
319 if ( usblog_fd >= 0 ) {
320 close_usblog();
321 }
322
323 sprintf(usblog_dev, "/dev/%s", g_zcat_usblog);
324
325 fd = open(usblog_dev, O_RDWR);
326 if ( fd < 0 )
327 {
328 printf("[zcat] open %s error!\n", usblog_dev);
329 return -1;
330 }
331 printf("[zcat] open %s\n", usblog_dev);
332
333 usblog_fd = fd;
334
335 PortSet(fd);
336
337 return fd;
338}
339
340/**
341 * @brief ³õʼ»¯usbģʽ£¬\n
342 * ¶ÁÈ¡usbÐéÄâµÄtty·¾¶.
343 * ´ò¿ªtty£¬¶ÁÈ¡pc²à·¢¹ýÀ´µÄÐÄÌø°ü¡¢¹æÔòÎļþµÈÊý¾Ý
344 * ´´½¨usbÈȲå°Î¼à²âÏß³Ì
345 * @param[in] void
346 * @return 0 on success, errno otherwise
347 * @note
348 * @see
349 */
350int init_usblog_mode()
351{
352 int ret = 0;
353
354 ret = pthread_rwlock_init(&usblog_rwlock, NULL);
355 if(ret)
356 {
357 printf("[zcat] usblog_rwlock init error.\n");
358 return -1;
359 }
360 else
361 {
362 ret = init_monitor_hotplug();
363 if(ret < 0)
364 {
365 printf("[zcat] init_monitor_hotplug error.\n");
366 goto __exit;
367 }
368
369 ret = pthread_create(&read_usb_thread, NULL, read_usblog, NULL);
370 if(ret != 0)
371 {
372 printf("[zcat] create read_usb_thread error.\n");
373 goto __exit;
374 }
375 return ret;
376 __exit:
377 pthread_rwlock_destroy(&usblog_rwlock);
378 return ret;
379 }
380}
381
382