blob: 0e9c52b0377f82cc1c12991eff87271ee6f3e1d9 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/**
2 *
3 * @file cp_ramdump.c
4 * @brief
5 * This file is part of ZCAT.
6 * zcatÓ¦Óòãlog_agent´¦Àícp_ramdumpÉ豸
7 *
8 * @details
9 * @author Tools Team.
10 * @email
11 * @copyright Copyright (C) 2013 Sanechips Technology Co., Ltd.
12 * @warning
13 * @date 2019/02/02
14 * @version 1.1
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/02/02 1.1 jiang.fenglin ÐÞ¸Ä×¢ÊÍ·½Ê½Îªdoxygen
25 * ---------------------------------------------------------------------------
26 *
27 *
28 */
29#if 0
30#include "log_agent.h"
31#include <stdlib.h>
32#include <stdio.h>
33#include <string.h>
34#include <sys/types.h>
35#include <sys/stat.h>
36#include <fcntl.h>
37#include <errno.h>
38#include <getopt.h>
39#include <stdarg.h>
40#include <termios.h>
41#include <stddef.h>
42#include <dirent.h>
43#include <time.h>
44#include <pthread.h>
45
46/**
47 * Êý¾Ý½á¹¹¶¨Òå
48 */
49typedef struct {
50 char file_name[32];
51 unsigned int file_size;
52} file_info_t;
53
54/**
55 * È«¾Ö±äÁ¿ºÍºê¶¨Òå
56 */
57#define LOG_TAG "Modem_Ramdump"
58#define ZTE_LOG_PATH "/mnt/sdcard/ZTELog"
59#define RAMDUMP_LOG_PATH "/mnt/sdcard/ZTELog/Ramdump"
60#define DEFAULT_RAMDUMP_PATH "/sdcard/ZTELog/ramdump"
61#define USB_RAMDUMP_FLAG_FILE_NAME "/sys/dwc_usb/usbconfig/ramdumpFlag"
62// #define MODEM_RAMDUMP_PATH "/data/local/log/Ramdump"
63
64pthread_t get_ramdump_thread = 0;
65//typedef unsigned int UINT32;
66int ramdump_fd = -1;
67
68#define RAMDUMP_DEFAULT_DELAY 10000
69#define MAX_AP_LOG_BUFF_LEN 8192
70
71/*Ramdump Ö¸ÁÒå*/
72#define DUMPFILE_LINK_REQ (UINT32)1 //ͬ²½ÇëÇó
73#define DUMPFILE_LINK_RSP (UINT32)2 //ͬ²½ÇëÇóÓ¦´ð£¬¸½´øramdumpÎļþÊýÄ¿
74#define DUMPFILE_FILE_REQ (UINT32)3 //ÇëÇóÖ¸¶¨±àºÅÎļþÐÅÏ¢
75#define DUMPFILE_FILE_RSP (UINT32)4 //Îļþ±àºÅÎļþÐÅÏ¢Ó¦´ð£¬¸½´ø´«ÊäÎļþÃû¼°´óС
76#define DUMPFILE_READ_REQ (UINT32)5 //ÇëÇó¶Áȡָ¶¨±àºÅÎļþÄÚÈÝ
77#define DUMPFILE_READ_RSP (UINT32)6 //ÎļþÄÚÈݶÁȡӦ´ð£¬¸½´øÎļþÄÚÈÝ
78#define DUMPFILE_END_REQ (UINT32)7 //´«Êä½áÊø
79#define DUMPFILE_END_RSP (UINT32)8 //´«Êä½áÊøÓ¦´ð
80#define DUMPFILE_CMD_FAIL (UINT32)9 //Ö¸Áî´íÎó
81#define DUMPFILE_NO_FAIL (UINT32)10 //Îļþ±àºÅ´íÎó
82#define DUMPFILE_LENGTH_FAIL (UINT32)11 //ÎļþλÖôóС´íÎó
83#define CMD_BUFFER_LEN (UINT32)16 //Ö¸Áî²Á·À¶¾
84#define FILENAME_LEN (UINT32)32 //ÎļþÃû³¤¶È
85#define FILENAME_MAX_LEN (UINT32)256 //ÎļþÃû×î´ó³¤¶È
86//#define DATA_BLOCK_SIZE (0x40000) // Êý¾Ý»º³å´óС
87#define DATA_BLOCK_SIZE (0x01000) // Êý¾Ý»º³å´óС
88
89#define MIN(a, b) ((a)< (b) ? (a): (b))
90
91static char g_ramdump_datablock_buffer[DATA_BLOCK_SIZE] = {0};
92//static T_LOG_SDCARD_PARA g_ramdump_sdcard_para = { {0} };
93static char* g_ramdump_path = NULL;
94static int g_modem_fd = -1;
95/*É豸·¾¶*/
96static char* ramdump_dev="/dev/ramdump";
97static char* usb_ramdump_dev="/dev/ttyGS0";
98
99BOOL g_cp_trap_flag = TRUE;
100BOOL g_cpcrash_flag = FALSE;
101BOOL g_trap_flag = TRUE;
102
103int usb_ramdump_dev_fd = -1;
104pthread_t usb_ramdump_dev_thread = -1;
105
106/**
107 * Íⲿ±äÁ¿ºÍº¯ÊýÒýÓÃ
108 */
109extern int ramdump_fd;
110extern E_ZCAT_MODE g_log_dir;
111extern unsigned int zTools_SendData(unsigned char *buf, unsigned int buf_len,unsigned char tgt_mdl_no, unsigned char tgt_submdl_no, unsigned char src_submdl_no);
112extern int filelength(FILE *fp);
113extern BOOL zCatAgt_Ap_ReadBuffer(T_AP_SRC_BUFFER_TYPE *src, T_AP_DEST_BUFFER_TYPE *dest);
114extern int test_dir_exist(char* dir_path);
115
116/**
117 * º¯ÊýʵÏÖ
118 */
119
120BOOL get_cp_crash_flag()
121{
122 return g_cpcrash_flag;
123}
124
125#if 0
126static char * zte_time()
127{
128 time_t timep;
129 struct tm *p;
130 static char buf[22];
131 memset(buf,0,22);
132 time(&timep);
133 p =localtime(&timep);
134 snprintf(buf,21,"%4d/%02d/%02d %02d:%02d:%02d ",1900 + p->tm_year,1 + p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
135 return buf;
136}
137#endif
138static int writefile(char*path, char*buf, unsigned len)
139{
140 FILE *fp;
141 int rtv;
142 if((fp=fopen(path,"w"))==NULL)
143 {
144 printf("open file %s error.\n",path);
145 return -1;
146 }
147 rtv = fwrite(buf,len,1, fp);
148 fclose(fp);
149 return rtv;
150}
151
152static int readfile(char *path, char* buf, unsigned len)
153{
154 FILE *fp;
155 int length;
156 if((fp=fopen(path,"r"))==NULL)
157 {
158 printf("open file %s error.\n",path);
159 return -1;
160 }
161 length = filelength(fp);
162 length = length > len? len: length;
163 //ch=(char *)malloc(length+1);
164 fread(buf,length,1,fp);
165 fclose(fp);
166 *(buf+length)='\0';
167 return length;
168}
169
170BOOL get_trap_flag_from_dev()
171{
172
173 int ramdumpFlag = 0;
174 char buf[2]={0};
175
176 readfile(USB_RAMDUMP_FLAG_FILE_NAME, buf, 1);
177
178 ramdumpFlag = atoi(buf);
179 printf("ramdumpFlag : %d \n", ramdumpFlag);
180
181 if(ramdumpFlag == 0){
182 printf("usb normal \n");
183 }else{
184 printf("usb ramdump \n");
185 }
186 if(ramdumpFlag == 1)
187 {
188 return TRUE;
189 }
190
191 return FALSE;
192}
193
194
195int send_ramdump_to_trap(unsigned char *buf, int read_len)
196{
197 int write_len = -1;
198 while(usb_ramdump_dev_fd == -1)
199 {
200 sleep(1);
201 printf("hb ramdump test\n");
202 }
203
204 write_len = write(usb_ramdump_dev_fd, buf, read_len);
205
206 if (write_len != read_len)
207 {
208 printf("modem_log, send_rule error, write_len = %d, len = %d. \n", write_len, read_len);
209 return -1;
210 }
211
212 return 0;
213}
214
215static unsigned char gReadApplogBuffer[1024*130] = {0};
216/**********************************************************************************
217*º¯Êý˵Ã÷:´ÓramdumpÉ豸¶ÁÈ¡Êý¾Ý
218***********************************************************************************/
219void *get_ramdump_from_dev(void *arg)
220//void *get_ramdump_from_dev(int fd)
221{
222
223 int read_len = -1;
224
225 printf("get_ramdump_from_dev * p_fd %d \n", ramdump_fd);
226
227 while(1)
228 {
229 read_len = read(ramdump_fd, gReadApplogBuffer, sizeof(gReadApplogBuffer));
230 if(read_len>0)
231 {
232 if(g_log_dir == ZCAT_MODE_AP_USB)
233 {
234 if(g_cp_trap_flag)
235 {
236 writefile(USB_RAMDUMP_FLAG_FILE_NAME, "1", 1);
237 printf("get_ramdump_from_dev: writefile");
238 }
239 else
240 {
241 send_ramdump_to_trap(gReadApplogBuffer, read_len);
242 printf("get_ramdump_from_dev: send_ramdump_to_trap");
243 }
244 }
245 else if(g_log_dir == ZCAT_MODE_AP_NET)
246 {
247 g_cpcrash_flag = TRUE;
248 T_AP_DEST_BUFFER_TYPE dest = {0};
249 T_AP_SRC_BUFFER_TYPE src = {0};
250 src.buf = gReadApplogBuffer;
251 src.beginIndex=0;
252 src.bufSize = sizeof(gReadApplogBuffer);
253 while (zCatAgt_Ap_ReadBuffer(&src , &dest))
254 {
255 unsigned char *p = dest.buf;
256 unsigned short len = dest.len;
257 //if(g_log_dir == ZCAT_MODE_AP_NET)
258 {
259 //»¥³â???
260 zTools_SendData(p, len, MDL_ZCAT, 1, SUBMDL_ZCATAGT);
261 }
262
263 memset(&dest, 0, sizeof(T_AP_DEST_BUFFER_TYPE));
264 }
265 }
266 else
267 {
268 printf("get_ramdump_from_dev g_log_dir error \n");
269 }
270 }
271 else
272 {
273 sleep(1);
274 //printf("get_ramdump_from_dev: sleep1");
275 }
276 }
277 return NULL;
278}
279
280//³õʼ»¯ramdump logÉ豸
281int init_ramdump_log_device()
282{
283 int ret_ramdump;
284 int fd;
285
286 fd = open(ramdump_dev, O_RDWR);
287 printf("init_ramdump_log_device fd %d \n", fd);
288 if (fd < 0)
289 {
290 printf("open ramdump_dev error!!!\n");
291 return -1;
292 }
293 ramdump_fd = fd;
294 ret_ramdump = pthread_create(&get_ramdump_thread, NULL, get_ramdump_from_dev, NULL);
295 if(ret_ramdump!=0)
296 {
297 printf("pthread_create getramdump_thread error\n");
298 return -1;
299 }
300
301 return fd;
302}
303
304static int tty_write(int fd, const char* buf, int size)
305{
306 int ret = 0;
307 ret = write(fd, buf, size);
308 if (ret != size) {
309 printf("%s failed, size=%d, ret=%d\n", __FUNCTION__, size, ret);
310 return -1;
311 }
312 return 0;
313}
314
315static int tty_read(int fd, char*buf, int size ,unsigned int delay_ms)
316{
317 int ret = -1;
318 int read_count = 0;
319 fd_set fds;
320 struct timeval tv;
321 if (buf == NULL)
322 return -1;
323 tv.tv_sec = delay_ms/1000;
324 tv.tv_usec = (delay_ms%1000) *1000;
325 FD_ZERO(&fds);
326 FD_SET(fd, &fds);
327 ret = select(fd +1, &fds, NULL, NULL, &tv);
328 if (ret > 0) {
329 read_count = read(fd, buf, size);
330 if (read_count <= 0) {
331 printf("%s read %d failed for ret=%d\n" , __FUNCTION__, read_count, ret);
332 return -1;
333 }
334 return read_count;
335 } else if (ret == 0) {
336 printf("select time out %dms\n" , delay_ms);
337 } else {
338 printf("select failed %s\n" , strerror(errno));
339 }
340 return -1;
341}
342static int mdp_send(const char*buf, int size)
343{
344 //mdp_print_array(,buf, size);
345 return tty_write(g_modem_fd, buf,size);
346}
347
348
349static int mdp_receive(char *buf, int size)
350{
351 int count = 0;
352 int length = size;
353 char *pbuffer = buf;
354 while ( length > 0) {
355 count = tty_read(g_modem_fd, pbuffer,size, RAMDUMP_DEFAULT_DELAY);
356 if (count < 0) {
357 return -1;
358 }
359 pbuffer += count;
360 length -= count;
361 }
362 //mdp_print_array(, buf, size);
363 return size;
364}
365static int mdp_send_command(unsigned int cmd, unsigned int argc,...)
366{
367 char buffer[CMD_BUFFER_LEN] = {0};
368 unsigned int i = 0;
369 unsigned int arg = 0;
370 UINT32 *pbuffer = (UINT32*)buffer;
371 *pbuffer = cmd;
372 va_list ap;
373 va_start(ap, argc);
374 for (i=0; i<argc; i++) {
375 arg = va_arg(ap, unsigned int);
376 *(++pbuffer) = arg;
377 }
378 va_end(ap);
379 return mdp_send(buffer, CMD_BUFFER_LEN);
380}
381static int mdp_receive_ack(unsigned int ack)
382{
383 int ret = 0;
384 unsigned int resp;
385 //char buffer[64] = {0};
386 ret = mdp_receive((char *)&resp, sizeof(unsigned int));
387 if (ret > 0) {
388 if (ack == resp)
389 return 0;
390 }
391 return -1;
392}
393static int init_devices(char* dev_path)
394{
395 int fd = -1;
396
397 fd = open(dev_path, O_RDWR);
398 if (fd < 0) {
399 printf("Can't open %s(%s)\n", dev_path, strerror(errno));
400 return -1;
401 }
402
403 return fd;
404}
405static int create_fold(char *fold)
406{
407 char buffer[256]= {0};
408 snprintf(buffer, 256, "mkdir -p %s\n", fold);
409 int ret = system(buffer);
410 if (ret < 0)
411 return -1;
412 return 0;
413}
414static int create_file(char* fold, char * path)
415{
416 int fd = -1;
417 DIR* pdir = NULL;
418 char file_name[FILENAME_MAX_LEN] = {0};
419 int ret = 0;
420 if ((fold==NULL) || (*fold=='\0'))
421 fold = g_ramdump_path;
422 if ( (path==NULL) || (*path=='\0')) {
423
424 return -1;
425 }
426 if ((pdir = opendir(fold)) == NULL) {
427 ret = create_fold(fold);
428 if (ret < 0) {
429 printf("%s create fold %d failed (%s)", fold, errno, strerror(errno));
430 return -1;
431 }
432 }
433 if (pdir != NULL)
434 closedir(pdir);
435 snprintf(file_name, FILENAME_MAX_LEN, "%s/%s", fold, path);
436 unlink(file_name);
437 printf("%s\n" , file_name);
438 fd = open(file_name, O_CREAT| O_RDWR, 0777);
439 if (fd < 0) {
440 printf("failed to create %s (%s)\n", path, strerror(errno));
441 }
442 return fd;
443
444}
445static int write_to_file(int fd, char *buffer, int size)
446{
447 int ret = 0;
448 if ((fd < 0) || (buffer==NULL) || (size<=0))
449 return -1;
450 ret = write(fd, buffer, size);
451 if (ret < size) {
452 printf("write to file failed, ret=%d, size=%d\n", ret, size);
453 return -1;
454 }
455 return 0;
456
457}
458
459static int mdp_receive_ack_and_info(int cmd, void *receive_info, int receive_size)
460{
461 int ret = -1;
462 char* buffer = NULL;
463 int rsp = -1;
464
465 buffer = malloc(receive_size + sizeof(rsp));
466 if(buffer == NULL)
467 goto exit;
468 ret = mdp_receive(buffer, receive_size + sizeof(rsp));
469 if (ret < sizeof(rsp) + receive_size) {
470 printf("failed to mdp_receive_ack_and_info\n");
471 goto exit;
472 }
473
474 memcpy(&rsp, buffer, sizeof(rsp));
475 if (rsp != cmd) {
476 printf("failed to receive cmd:%d\n", cmd);
477 goto exit;
478 }
479
480 memcpy(receive_info, buffer + sizeof(rsp), receive_size);
481exit:
482 free(buffer);
483 ret = 0;
484 return ret;
485}
486
487
488//»ñÈ¡µÚindex¸öramdumpÎļþ²¢Ð´Èët¿¨
489static int dump_file(int index, char *fold)
490{
491 int ret = 0;
492 //char path[256] = {0};
493 //char cmd_buffer[CMD_BUFFER_LEN] = {0};
494 file_info_t file_info = {{0}, 0};
495 int fd = 0;
496 int file_size , read_count, file_offset;
497
498 ret = mdp_send_command(DUMPFILE_FILE_REQ, 1, index);
499 if (ret < 0) {
500 return -1;
501 }
502
503 ret = mdp_receive_ack_and_info(DUMPFILE_FILE_RSP, (void*)(&file_info), sizeof(file_info));
504 /*
505 if (ret < 0) {
506 return -1;
507 }*/
508
509 fd = create_file(fold, file_info.file_name);
510 if (fd < 0) {
511 printf("failed to create file %s\n", file_info.file_name);
512 return -1;
513 }
514 printf("filename=%s\t size=%d\n", file_info.file_name, file_info.file_size);
515 file_size = file_info.file_size;
516 file_offset = read_count = 0;
517 while (file_size > 0) {
518 read_count = MIN(file_size, DATA_BLOCK_SIZE);
519 if (mdp_send_command(DUMPFILE_READ_REQ, 3, index, file_offset, read_count) < 0) {
520 ret = -1;
521 goto exit;
522 }
523 if (mdp_receive_ack(DUMPFILE_READ_RSP) < 0) {
524 ret = -1;
525 goto exit;
526 }
527 if (mdp_receive(g_ramdump_datablock_buffer, read_count) < 0) {
528 printf("failed to read file data\n");
529 ret = -1;
530 goto exit;
531 }
532 if (write_to_file(fd, g_ramdump_datablock_buffer, read_count)< 0) {
533 printf("failed to write file data\n");
534 ret = -1;
535 goto exit;
536 }
537 file_offset += read_count;
538 file_size -= read_count;
539 }
540 ret = 0;
541exit:
542 close(fd);
543 return ret;;
544}
545
546static int do_modem_ramdump( char* tty, char*path)
547{
548 int ret = -1;
549 int file_number = 0;
550 int i = 0;
551 int read_count = 0;
552 char read_buf[5] = {0};
553
554 g_modem_fd = init_devices(tty);
555 if (g_modem_fd < 0) {
556 printf("failed to open %s\n", tty);
557 return -1;
558 }
559 read_count = read(g_modem_fd, read_buf, sizeof(read_buf));
560 if (0 >= read_count )
561 {
562 printf("read_count %d\n", read_count);
563 goto exit;
564 }
565 ret = mdp_send_command(DUMPFILE_LINK_REQ, 0);
566 if (ret < 0) {
567 printf("Send DUMPFILE_LINK_REQ failed\n");
568 ret = -1;
569 goto exit;
570 }
571
572 ret = mdp_receive_ack_and_info(DUMPFILE_LINK_RSP, &file_number, sizeof(file_number));
573 /*
574 if (ret < 0) {
575 printf("mdp_receive_ack_and_filenum failed\n");
576 ret = -1;
577 goto exit;
578 }
579 */
580
581 printf("file_number = %d\n", file_number);
582 for (i=0; i<file_number; i++) {
583 printf("dump file index=%d ...\n", i);
584 ret = dump_file(i, path);
585 if (ret < 0) {
586 printf("dump file index=%d failed\n", i);
587 ret = -1;
588 goto exit;
589 }
590 printf("dump file index=%d success\n", i);
591 }
592 ret = mdp_send_command(DUMPFILE_END_REQ, 0);
593 if (ret < 0) {
594 printf("failed to send DUMPFILE_END_REQ\n");
595 ret = -1;
596 goto exit;
597 }
598 mdp_receive_ack(DUMPFILE_END_RSP);
599 ret = 0;
600exit:
601 if (g_modem_fd > 0)
602 close(g_modem_fd);
603 return ret;
604}
605
606void broadcast_ramdump_result(int success)
607{
608 char command[256];
609 sprintf(command, "am broadcast -a zte.com.cn.intent_modemramdump_finished --ez extra_success %s", (success == 0 ? "true" : "false"));
610 printf("%s\n" , command);
611 system(command);
612}
613#if 0
614static void compress_and_rm_fold(char *pfold, char *time_str)
615{
616 char buffer[512] = {0};
617 int ret = 0;
618 printf("%s %s %s\n", pfold, time_str);
619 snprintf(buffer, 512, "cd %s;tar -zcf %s.tgz %s/*\n", pfold, time_str, time_str);
620 printf("%s %s\n" , buffer);
621 ret = system(buffer);
622 if(ret != 0){
623 printf("compress failed, delete the unfinished compressed file\n");
624 snprintf(buffer, 512, "cd %s;rm -rvf %s.tgz \n", pfold, time_str);
625 }else {
626 printf("compress finished, delete the source fold\n");
627 snprintf(buffer, 512, "cd %s; rm -rvf %s\n", pfold, time_str);
628 }
629 printf("%s %s\n", buffer);
630 system(buffer);
631}
632#endif
633static int get_time_str(char*buf, size_t size)
634{
635 struct tm cur_tm;
636 time_t now = time(NULL);
637 if (NULL==buf || size<=0)
638 return -1;
639 localtime_r(&now, &cur_tm);
640 strftime(buf, size, "%Y_%m%d_%H%M%S", &cur_tm);
641 printf("%s\n", buf);
642 return 0;
643}
644static int get_ramdump_fold_name(char*ramdump_path, size_t size, char *time_buffer)
645{
646 if (ramdump_path==NULL || size<=0 || time_buffer==NULL)
647 {
648 return -1;
649 }
650 snprintf(ramdump_path, size, "%s/%s", g_ramdump_path, time_buffer);
651 printf("ramdump_path: %s\n", ramdump_path);
652 return 0;
653}
654
655/***********************************************************************************
656º¯Êý˵Ã÷£ºramdump²Ù×÷Ïß³Ì
657Êä È룺
658Êä ³ö£º
659·µ »Ø Öµ£º
660Æä Ëû£º
661***********************************************************************************/
662static void* ramdump_entry(void *arg)
663{
664 int ret = -1;
665 char dev_tty[256] = {0};
666 char dump_path[256] = {0};
667 char time_str[64] = {0};
668
669 // property_set("ctl.stop", "ztemodemlog");
670 /*
671 if(get_time_str(time_str, 64) < 0){
672 printf("Can't get the time str\n");
673 return NULL;
674 }
675 */
676 get_time_str(time_str, 64);
677 ret = get_ramdump_fold_name(dump_path, 256, time_str);
678 /*
679 if (ret < 0) {
680 printf("Can't get the ramdump fold path\n");
681 return NULL;
682 } */
683 memcpy(dev_tty, ramdump_dev, 256);
684
685 printf("try to get the ramdump data from %s\n", ramdump_dev);
686
687 ret = do_modem_ramdump(ramdump_dev,dump_path);
688 #if 0//for test
689 if (ret >= 0)
690 {
691 printf("get the ramdump data from %s success\n", dev_tty);
692 compress_and_rm_fold(dump_path, time_str);
693
694 }
695 else
696 {
697 printf("get the ramdump data from %s failed\n", dev_tty);
698 }
699 #endif
700 return NULL;
701}
702
703static void init_output_flash_mode()
704{
705 if(test_dir_exist(DEFAULT_RAMDUMP_PATH) < 0)
706 {
707 printf("test_dir_exist failed:%s. \n", DEFAULT_RAMDUMP_PATH);
708 return;
709 }
710 g_ramdump_path = DEFAULT_RAMDUMP_PATH;
711}
712
713static int init_output_u_mode()
714{
715 if(test_dir_exist(ZTE_LOG_PATH) < 0)//ÅжϸùĿ¼
716 {
717 printf("test_dir_exist failed:%s. \n", ZTE_LOG_PATH);
718 return -1;
719 }
720 if(test_dir_exist(RAMDUMP_LOG_PATH) < 0)//ÅжÏramdumpÈÕ־Ŀ¼ÊÇ·ñ´æÔÚ
721 {
722 printf("test_dir_exist failed:%s. \n", RAMDUMP_LOG_PATH);
723 return -1;
724 }
725 g_ramdump_path = RAMDUMP_LOG_PATH;
726 return 0;
727}
728
729int init_ramdump_output_dir(E_FLASH_MODE mode)
730{
731 int ret = -1;
732 pthread_t ramdump_thread = -1;
733
734 if(mode == FLASH_MODE_NOMAL)
735 {
736 init_output_flash_mode();
737 }
738 else if(mode == FLASH_MODE_U)
739 {
740 init_output_u_mode();
741 }
742 else
743 {
744 return -1;
745 }
746 ret = pthread_create(&ramdump_thread, NULL, ramdump_entry, NULL);
747 return ret;
748}
749
750static int send_ramdump_rules_to_cp(char *buf, int read_len)
751{
752 int write_len = write(ramdump_fd, buf, read_len);
753 if (write_len != read_len)
754 {
755 printf("modem_log, send_rule error, write_len = %d, len = %d. \n", write_len, read_len);
756 return -1;
757 }
758 return 0;
759}
760
761void *get_rule_from_ramdump_dev(void* args)
762{
763 char *pbuf = NULL;
764 int read_len = 0;
765 pbuf = (char *)malloc(8*1024);
766 if(pbuf == NULL)
767 {
768 return NULL;
769 }
770
771 while(1)
772 {
773 read_len = read(usb_ramdump_dev_fd, pbuf, 8*1024);
774
775 if(read_len > 0)
776 {
777 //send_rules_to_ramdump(pbuf, read_len);//Ïòramdump·¢Ë͹æÔò
778 send_ramdump_rules_to_cp(pbuf, read_len);
779
780 }
781 else
782 {
783 sleep(2);
784 }
785 }
786 return NULL;
787}
788
789static void* init_usb_trap_device(void* args)
790{
791 int ret_ramdump = -1;
792 int fd = -1;
793
794 while(TRUE)
795 {
796 //ret_ramdump = close(usb_ramdump_dev);
797 fd = open(usb_ramdump_dev, O_RDWR);
798 if (fd < 0)
799 {
800 sleep(1);
801 printf("usb_ramdump_dev open fail\n");
802 }
803 else
804 {
805 printf("usb_ramdump_dev open success\n");
806 break;
807 }
808 }
809 usb_ramdump_dev_fd = fd;
810
811 ret_ramdump=pthread_create(&usb_ramdump_dev_thread, NULL, get_rule_from_ramdump_dev, NULL);
812 if(ret_ramdump != 0)
813 {
814 printf("pthread_create cp_interact_thread error\n");
815 }
816
817 return NULL;
818}
819
820int init_ramdump_device()
821{
822 int ret = -1;
823 pthread_t trap_dev_thread = -1;
824
825 ret=pthread_create(&trap_dev_thread, NULL, init_usb_trap_device, NULL);
826 if(ret != 0)
827 {
828 printf("pthread_create init_ramdump_device error\n");
829 return -1;
830 }
831 g_cp_trap_flag = FALSE;
832 return ret;
833}
834
835int ramdump_trap_rev_close(VOID)
836{
837 int ret = -1;
838
839 ret = close(usb_ramdump_dev_fd);
840 if (ret < 0)
841 {
842 printf("open devtty GS0 error!!!\n");
843 return -1;
844 }
845 usb_ramdump_dev_fd = -1;
846 return ret;
847}
848
849#endif
850
851
852