yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /**
|
| 2 | *
|
| 3 | * @file log_agent_sdcard.c
|
| 4 | * @brief
|
| 5 | * This file is part of ZCAT.
|
| 6 | * zcatÓ¦Óòãlog_agent´¦Àíͨ¹ýsd¿¨µÄÊý¾ÝÁ÷
|
| 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.2
|
| 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/02/02 1.2 jiang.fenglin ÐÞ¸Ä×¢ÊÍ·½Ê½Îªdoxygen
|
| 27 | * 2019/07/08 1.3 jiang.fenglin Ôö¼ÓAPFS/CPFSģʽ
|
| 28 | * ---------------------------------------------------------------------------
|
| 29 | *
|
| 30 | *
|
| 31 | */
|
| 32 |
|
| 33 | #include <string.h>
|
| 34 | #include <pthread.h>
|
| 35 | #include <sys/prctl.h>
|
| 36 | #include <sys/statfs.h>
|
| 37 | #include "log_agent.h"
|
| 38 | #include "cfg_api.h"
|
| 39 |
|
| 40 | #if 0
|
| 41 | #define DEFAULT_LOG_ROOT "/sdcard"
|
| 42 | #define DEFAULT_LOG_ROOT_PARENT "/ZTELog"
|
| 43 | #define CONFIG_STORAGE_FILE_PATH "/data/misc/rild/log_storage.cfg"
|
| 44 | #endif
|
| 45 |
|
| 46 | #define USB_DEV_SDCARD_PATH "/dev/mmcblk0p1"
|
| 47 | #define USB_DEV_SDCARD_PATH_BACK "/dev/mmcblk0"
|
| 48 |
|
| 49 | #define ZCAT_SDCARD_MOUNT_PATH "/tmp/udisk"
|
| 50 |
|
| 51 | #define ZCAT_SDCARD_RULE_PATH "/tmp/udisk/log.rule"
|
| 52 | #define ZCAT_SDCARD_LOG_PATH "/tmp/udisk/zcat_log"
|
| 53 |
|
| 54 | #define ZCAT_FS_RULE_PATH "/var/zcat/log.rule"
|
| 55 | #define ZCAT_FS_PATH "/var/zcat"
|
| 56 |
|
| 57 |
|
| 58 | static T_LOG_SDCARD_PARA g_log_sdcard_para = { {0} };
|
| 59 | pthread_t rule_heartbeat_thread = 0;
|
| 60 |
|
| 61 | extern int deal_with_encoded_data(unsigned char *buffer,int buf_len);
|
| 62 |
|
| 63 | typedef struct
|
| 64 | {
|
| 65 | char head1[16];
|
| 66 | int heartbeat_pos;
|
| 67 | int heartbeat_len;
|
| 68 | int rule_pos;
|
| 69 | int rule_len;
|
| 70 | int ap_rule_pos;
|
| 71 | int ap_rule_len;
|
| 72 | int log_file_size;
|
| 73 | int log_file_cnt;
|
| 74 | }rule_struct;
|
| 75 |
|
| 76 | char g_heartbeat_buff[35] = {
|
| 77 | 0x01,0xaa,0xaa,0xaa,0x01,0x55,0x73,0x01,
|
| 78 | 0x14,0x00,0x00,0x00,0x06,0x01,0xbb,0xbb,
|
| 79 | 0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
| 80 | 0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,
|
| 81 | 0xe6,0x7a,0x7e
|
| 82 | };
|
| 83 |
|
| 84 | /* É豸¾ä±ú */
|
| 85 | extern BOOL bSDSet;
|
| 86 | extern int cplog_fd;
|
| 87 |
|
| 88 |
|
| 89 | /**
|
| 90 | * @brief ÅжÏĿ¼ÊÇ·ñ´æÔÚ£¬Èç¹û²»´æÔÚÔò´´½¨Ò»¸öеÄĿ¼
|
| 91 | * @param[in] dir_path Ŀ¼·¾¶
|
| 92 | * @return 0 on success, -1 error.
|
| 93 | * @note
|
| 94 | * @see
|
| 95 | */
|
| 96 | int test_dir_exist(char* dir_path)
|
| 97 | {
|
| 98 | DIR *p_dir = NULL;
|
| 99 |
|
| 100 | if ((p_dir = opendir(dir_path)) == NULL)
|
| 101 | {
|
| 102 | printf("[zcat] opendir failed, mkdir %s\n", dir_path);
|
| 103 | if (mkdir(dir_path, 0777) != 0)
|
| 104 | {
|
| 105 | printf("[zcat] mkdir %s error, error info: %s\n", dir_path, strerror(errno));
|
| 106 | return -1;
|
| 107 | }
|
| 108 | }
|
| 109 | else
|
| 110 | {
|
| 111 | closedir(p_dir);
|
| 112 | p_dir = NULL;
|
| 113 | }
|
| 114 | return 0;
|
| 115 | }
|
| 116 |
|
| 117 | /**
|
| 118 | * @brief ³õʼ»¯sd¿¨´ælogµÄÅäÖòÎÊý
|
| 119 | * @return void
|
| 120 | * @note
|
| 121 | * @see
|
| 122 | */
|
| 123 | static void init_sdcard_para()
|
| 124 | {
|
| 125 | g_log_sdcard_para.max_file_size = 1024 * 1024 * 100;
|
| 126 | //g_log_sdcard_para.max_file_num = 8;
|
| 127 | g_log_sdcard_para.log_overwrite = 1;
|
| 128 | g_log_sdcard_para.compress_log_files = 1;
|
| 129 | g_log_sdcard_para.file_counts = 0;
|
| 130 | g_log_sdcard_para.output_fd = -1;
|
| 131 | g_log_sdcard_para.free_space_limit = 1024 * 1024 * 1;
|
| 132 |
|
| 133 | memset(g_log_sdcard_para.rule_path, 0, sizeof(g_log_sdcard_para.rule_path));
|
| 134 | strcpy(g_log_sdcard_para.rule_path, ZCAT_SDCARD_RULE_PATH);
|
| 135 | }
|
| 136 |
|
| 137 | /**
|
| 138 | * @brief ³õʼ»¯sd¿¨normalģʽ²ÎÊý
|
| 139 | * @return void
|
| 140 | * @note
|
| 141 | * @see
|
| 142 | */
|
| 143 | static int init_sdcard_flash_mode()
|
| 144 | {
|
| 145 | memset(g_log_sdcard_para.log_dir, 0, sizeof(g_log_sdcard_para.log_dir));
|
| 146 | strcpy(g_log_sdcard_para.log_dir, ZCAT_SDCARD_LOG_PATH);
|
| 147 |
|
| 148 | if(test_dir_exist(g_log_sdcard_para.log_dir) < 0)//´´½¨ÈÕÖ¾Îļþ¼Ð
|
| 149 | {
|
| 150 | printf("[zcat] test %s dir exist failed. \n", g_log_sdcard_para.log_dir);
|
| 151 | return -1;
|
| 152 | }
|
| 153 | return 0;
|
| 154 | }
|
| 155 |
|
| 156 | /**
|
| 157 | * @brief ³õʼ»¯sd¿¨uÅÌģʽ²ÎÊý
|
| 158 | * @return void
|
| 159 | * @note
|
| 160 | * @see
|
| 161 | */
|
| 162 | static int init_sdcard_u_mode()
|
| 163 | {
|
| 164 | memset(g_log_sdcard_para.log_dir, 0, sizeof(g_log_sdcard_para.log_dir));
|
| 165 | strcpy(g_log_sdcard_para.log_dir, ZCAT_SDCARD_LOG_PATH);
|
| 166 | if(test_dir_exist(ZCAT_SDCARD_LOG_PATH) < 0)//ÅжÏËùÓÐÈÕ־Ŀ¼ÊÇ·ñ´æÔÚ
|
| 167 | {
|
| 168 | printf("[zcat] test_dir_exist failed:%s. \n", ZCAT_SDCARD_LOG_PATH);
|
| 169 | return -1;
|
| 170 | }
|
| 171 | /*if(test_dir_exist(MODEM_LOG_PATH) < 0)//ÅжÏcpÈÕ־Ŀ¼ÊÇ·ñ´æÔÚ
|
| 172 | {
|
| 173 | printf("test_dir_exist failed:%s. \n", MODEM_LOG_PATH);
|
| 174 | return -1;
|
| 175 | }*/
|
| 176 | return 0;
|
| 177 | }
|
| 178 |
|
| 179 | int zCat_MountSd()
|
| 180 | {
|
| 181 | char cmd[100] = { 0 };
|
| 182 |
|
| 183 | if(test_dir_exist(ZCAT_SDCARD_MOUNT_PATH) < 0) // ´´½¨¹ÒÔØµÄÁÙʱÎļþ¼Ð
|
| 184 | {
|
| 185 | printf("[zcat] create %s fail.\n", ZCAT_SDCARD_MOUNT_PATH);
|
| 186 | return -1;
|
| 187 | }
|
| 188 |
|
| 189 | printf("[zcat] init sd output dir successs.\n");
|
| 190 |
|
| 191 | memset(cmd, 0, sizeof(cmd));
|
| 192 | sprintf(cmd, "mount -t vfat %s %s", USB_DEV_SDCARD_PATH, ZCAT_SDCARD_MOUNT_PATH);
|
| 193 | if(0 != zxic_system(cmd))
|
| 194 | {
|
| 195 | printf("[zcat] cmd %s fail!\n", cmd);
|
| 196 |
|
| 197 | memset(cmd, 0, sizeof(cmd));
|
| 198 | sprintf(cmd, "mount -t vfat %s %s", USB_DEV_SDCARD_PATH_BACK, ZCAT_SDCARD_MOUNT_PATH);
|
| 199 | if(0 != system(cmd))
|
| 200 | {
|
| 201 | printf("[zcat] cmd %s fail, mount error!\n", cmd);
|
| 202 | return -1;
|
| 203 | }
|
| 204 | }
|
| 205 |
|
| 206 | return 0;
|
| 207 | }
|
| 208 |
|
| 209 | /**
|
| 210 | * @brief ɾ³ýĿ¼ÏÂÔçÆÚ´´½¨µÄÎļþ\n
|
| 211 | * ¸ù¾ÝÎļþÃûÅжϴ´½¨Ê±¼ä£¬É¾³ý×îÏÈ´´½¨µÄÄǸöÎļþ
|
| 212 | * @param[in] dir_path ÎÄĿ¼·¾¶
|
| 213 | * @return void
|
| 214 | * @note
|
| 215 | * @see
|
| 216 | */
|
| 217 | static void delete_oldest_file(char* dir_path)
|
| 218 | {
|
| 219 | DIR *p_dir = NULL;
|
| 220 | struct dirent *p_entry = NULL;
|
| 221 | char oldest_file[64] = "99999999999999"; //ÈÕÆÚµÄ×î´óÖµ
|
| 222 | char file_name[MAX_STRING_LENGTH] = {0};
|
| 223 | p_dir = opendir(dir_path);
|
| 224 |
|
| 225 | if(p_dir)
|
| 226 | {
|
| 227 | while ((p_entry = readdir(p_dir)) != NULL)
|
| 228 | {
|
| 229 | if (strncmp(p_entry->d_name, ".", 1) == 0)
|
| 230 | continue;
|
| 231 |
|
| 232 | if (strcmp(p_entry->d_name, oldest_file) < 0)
|
| 233 | {
|
| 234 | strncpy(oldest_file, p_entry->d_name, 64 - 1);
|
| 235 | }
|
| 236 | }
|
| 237 |
|
| 238 | snprintf(file_name, MAX_STRING_LENGTH, "%s/%s", dir_path, oldest_file);
|
| 239 | if(remove(file_name) != 0)
|
| 240 | {
|
| 241 | printf("[zcat] remove %s fail!errno=%d(%s)\n",file_name,errno,strerror(errno));
|
| 242 | }
|
| 243 | closedir(p_dir);
|
| 244 | }
|
| 245 | }
|
| 246 |
|
| 247 |
|
| 248 | /**
|
| 249 | * @brief ÔÚdir_pathĿ¼Ï´ò¿ªlogÊä³öÎļþ²¢·µ»ØÎļþ¾ä±ú,\n
|
| 250 | * ÒÔµ±Ç°Ê±¼äΪÎļþÃû£¬´´½¨logÊä³öÎļþ¡£logÎļþ×î´óÊýÁ¿ÓÉLOG_FILE_MAX_NO¾ö¶¨£¬
|
| 251 | * Èôµ±Ç°ÎļþÊýÒÑ´ïÉÏÏÞ£¬É¾³ý×îÏÈ´´½¨µÄÎļþ,ÔÙÒÔµ±Ç°Ê±¼äΪÎļþÃû´´½¨Îļþ
|
| 252 | * @param[in] log_sdcard_para ÈÕÖ¾´æT¿¨²ÎÊý
|
| 253 | * @return ´ò¿ªµÄlogÎļþÃèÊö·û
|
| 254 | * @note
|
| 255 | * @see
|
| 256 | */
|
| 257 | static int open_log_file(T_LOG_SDCARD_PARA* log_sdcard_para)
|
| 258 | {
|
| 259 | int fd = -1;
|
| 260 | char file_name[MAX_STRING_LENGTH] = {0};
|
| 261 | struct tm *cur_time;
|
| 262 | time_t now;
|
| 263 |
|
| 264 | now = time(NULL);
|
| 265 | cur_time = localtime(&now);
|
| 266 |
|
| 267 | if(cur_time != NULL)
|
| 268 | snprintf(file_name, MAX_STRING_LENGTH, "%s/%d_%02d%02d_%02d%02d%02d%s", log_sdcard_para->log_dir, cur_time->tm_year + 1900,
|
| 269 | cur_time->tm_mon + 1, cur_time->tm_mday, cur_time->tm_hour, cur_time->tm_min,
|
| 270 | cur_time->tm_sec, ".log");
|
| 271 |
|
| 272 | if (log_sdcard_para->log_overwrite)
|
| 273 | {
|
| 274 | log_sdcard_para->file_counts++;
|
| 275 |
|
| 276 | struct statfs diskInfo;
|
| 277 | statfs(g_log_sdcard_para.log_dir, &diskInfo);
|
| 278 |
|
| 279 | if (diskInfo.f_bfree * diskInfo.f_bsize < log_sdcard_para->free_space_limit)
|
| 280 | //if (log_sdcard_para->file_counts > log_sdcard_para->max_file_num)
|
| 281 | {
|
| 282 | delete_oldest_file(log_sdcard_para->log_dir);
|
| 283 | log_sdcard_para->file_counts--;
|
| 284 | }
|
| 285 | }
|
| 286 |
|
| 287 | fd = open(file_name, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
|
| 288 |
|
| 289 | if (fd >= 0)
|
| 290 | {
|
| 291 | fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP |S_IWGRP | S_IROTH |S_IWOTH);
|
| 292 | }
|
| 293 | strncpy(log_sdcard_para->log_name, file_name, MAX_STRING_LENGTH-1);
|
| 294 |
|
| 295 | return fd;
|
| 296 | }
|
| 297 |
|
| 298 | /**
|
| 299 | * @brief ³õʼ»¯logµÄĿ¼
|
| 300 | * @param[in] n/a
|
| 301 | * @return 0 on success, errno otherwise
|
| 302 | * @note
|
| 303 | * @see
|
| 304 | */
|
| 305 | int init_fs_log_dir()
|
| 306 | {
|
| 307 | struct statfs diskInfo;
|
| 308 |
|
| 309 | (void)(diskInfo);
|
| 310 |
|
| 311 | g_log_sdcard_para.max_file_size = 1024 * 1024 * 10;
|
| 312 | //g_log_sdcard_para.max_file_num = 5;
|
| 313 | g_log_sdcard_para.log_overwrite = 1;
|
| 314 | g_log_sdcard_para.compress_log_files = 1;
|
| 315 | g_log_sdcard_para.file_counts = 0;
|
| 316 | g_log_sdcard_para.output_fd = -1;
|
| 317 | g_log_sdcard_para.free_space_limit = 1024 * 1024 * 1;
|
| 318 |
|
| 319 | memset(g_log_sdcard_para.rule_path, 0, sizeof(g_log_sdcard_para.rule_path));
|
| 320 | strcpy(g_log_sdcard_para.rule_path, ZCAT_FS_RULE_PATH);
|
| 321 |
|
| 322 | memset(g_log_sdcard_para.log_dir, 0, sizeof(g_log_sdcard_para.log_dir));
|
| 323 | strcpy(g_log_sdcard_para.log_dir, ZCAT_FS_PATH);
|
| 324 |
|
| 325 | if(test_dir_exist(g_log_sdcard_para.log_dir) < 0)//´´½¨ÈÕÖ¾Îļþ¼Ð
|
| 326 | {
|
| 327 | printf("[zcat] test %s dir exist failed. \n", g_log_sdcard_para.log_dir);
|
| 328 | return -1;
|
| 329 | }
|
| 330 |
|
| 331 | g_log_sdcard_para.output_fd = open_log_file(&g_log_sdcard_para);//´ò¿ªÈÕÖ¾Îļþ
|
| 332 | if (g_log_sdcard_para.output_fd == -1)
|
| 333 | {
|
| 334 | printf("[zcat] failed to open log file!\n");
|
| 335 | return -1;
|
| 336 | }
|
| 337 | return 0;
|
| 338 | }
|
| 339 |
|
| 340 | /**
|
| 341 | * @brief ³õʼ»¯sd¿¨´ælogµÄĿ¼
|
| 342 | * @param[in] mode normalģʽ/uÅÌģʽ
|
| 343 | * @return 0 on success, errno otherwise
|
| 344 | * @note
|
| 345 | * @see
|
| 346 | */
|
| 347 | int init_sdcard_log_dir(E_FLASH_MODE mode)
|
| 348 | {
|
| 349 | init_sdcard_para();
|
| 350 |
|
| 351 | if(mode == FLASH_MODE_U)
|
| 352 | {
|
| 353 | if(init_sdcard_u_mode() < 0)
|
| 354 | {
|
| 355 | printf("[zcat] init_sdcard_u_mode failed!\n");
|
| 356 | return -1;
|
| 357 | }
|
| 358 | }
|
| 359 | else
|
| 360 | {
|
| 361 | if(init_sdcard_flash_mode() < 0)
|
| 362 | {
|
| 363 | printf("[zcat] init_sdcard_flash_mode failed!\n");
|
| 364 | return -1;
|
| 365 | }
|
| 366 | }
|
| 367 |
|
| 368 | g_log_sdcard_para.output_fd = open_log_file(&g_log_sdcard_para);//´ò¿ªÈÕÖ¾Îļþ
|
| 369 | if (g_log_sdcard_para.output_fd == -1)
|
| 370 | {
|
| 371 | printf("[zcat] failed to open log file!\n");
|
| 372 | return -1;
|
| 373 | }
|
| 374 | return 0;
|
| 375 | }
|
| 376 |
|
| 377 | /**
|
| 378 | * @brief дlogµ½SD¿¨.\n
|
| 379 | * @param[in] fd ÉÏÒ»´ÎдÈësd¿¨ÖеÄzlfÎļþÃèÊö·û
|
| 380 | * @param[in] buf ´ýдÈëµÄÊý¾ÝÖ¸Õë
|
| 381 | * @param[in] len ´ýдÈëµÄÊý¾ÝÖ¸³¤¶È
|
| 382 | * @param[in] log_sdcard_para sd¿¨²ÎÊý
|
| 383 | * @return ʵ¼ÊдÈëµÄ³¤¶È
|
| 384 | * @note
|
| 385 | * @see
|
| 386 | */
|
| 387 | static int output_log_to_sdcard(int fd, unsigned char *buf, int len, T_LOG_SDCARD_PARA* log_sdcard_para)
|
| 388 | {
|
| 389 | int output_fd = fd;
|
| 390 | int write_len = 0;
|
| 391 |
|
| 392 | //static int file_size = 0;
|
| 393 | log_sdcard_para->file_size += len;
|
| 394 |
|
| 395 | // Èç¹ûÎļþ´óС´ïµ½×î´óÎļþ³ß´ç£¬Ôòн¨Ò»¸öÎļþ
|
| 396 | if (log_sdcard_para->file_size > log_sdcard_para->max_file_size)
|
| 397 | {
|
| 398 | close(fd);
|
| 399 |
|
| 400 | if (log_sdcard_para->compress_log_files)
|
| 401 | {
|
| 402 | //compress_log_files(g_log_dir);
|
| 403 | }
|
| 404 |
|
| 405 | output_fd = open_log_file(log_sdcard_para);
|
| 406 | if (output_fd == -1)
|
| 407 | {
|
| 408 | return -1;
|
| 409 | }
|
| 410 | log_sdcard_para->output_fd = output_fd;
|
| 411 | log_sdcard_para->file_size = len;
|
| 412 | }
|
| 413 |
|
| 414 | write_len = write(output_fd, buf, len);
|
| 415 | if(write_len != len) // Èç¹ûT¿¨Ð´Âú£¬Ôòɾ³ý֮ǰµÄlogÎļþ
|
| 416 | {
|
| 417 | delete_oldest_file(log_sdcard_para->log_dir);
|
| 418 | log_sdcard_para->file_counts--;
|
| 419 | write_len = write(output_fd, buf, len);
|
| 420 | if(write_len != len)
|
| 421 | {
|
| 422 | return -1;
|
| 423 | }
|
| 424 | }
|
| 425 |
|
| 426 | return write_len;
|
| 427 | }
|
| 428 |
|
| 429 | /**
|
| 430 | * @brief bufferÄÚÈÝдÈëµ½T¿¨
|
| 431 | * @param[in] buffer ´ýдÈëÊý¾ÝÖ¸Õë
|
| 432 | * @param[in] buffer_len ´ýдÈëÊý¾Ý³¤¶È
|
| 433 | * @return ʵ¼ÊдÈëµÄ³¤¶È
|
| 434 | * @note
|
| 435 | * @see
|
| 436 | */
|
| 437 | int send_message_to_sdcard(unsigned char* buf, int len)
|
| 438 | {
|
| 439 | return output_log_to_sdcard(g_log_sdcard_para.output_fd, buf, len, &g_log_sdcard_para);
|
| 440 | }
|
| 441 |
|
| 442 | /**
|
| 443 | * @brief ·¢Ë͹æÔòÎļþµ½cp.\n
|
| 444 | * @param[in] fd ¹æÔòÎļþÃèÊö·û
|
| 445 | * @param[in] offset ÎļþÆ«ÒÆ
|
| 446 | * @param[in] length ·¢ËÍÊý¾Ý³¤¶È
|
| 447 | * @return 0 on success, -1 otherwise
|
| 448 | * @note
|
| 449 | * @see
|
| 450 | */
|
| 451 | static int send_rules_to_cp(int fd, int offset, int length)
|
| 452 | {
|
| 453 | char buf[1024] = {0};
|
| 454 | int left_len = 0;
|
| 455 | int next_buf_len = 0;
|
| 456 | int read_len = 0;
|
| 457 |
|
| 458 | if(lseek(fd, offset, SEEK_SET) == -1)
|
| 459 | {
|
| 460 | printf("[zcat] rules file lseek to pos %d failed (%s)\n", offset, strerror(errno));
|
| 461 | return -1;
|
| 462 | }
|
| 463 |
|
| 464 | left_len = length;
|
| 465 | next_buf_len = sizeof(buf) < left_len ? sizeof(buf) : left_len;
|
| 466 | while((read_len = read(fd, buf, next_buf_len)) > 0)
|
| 467 | {
|
| 468 | int write_len = write(cplog_fd, buf, read_len);
|
| 469 | if (write_len != read_len)
|
| 470 | {
|
| 471 | printf("[zcat] send_rule error, write_len = %d, len = %d.\n", write_len, read_len);
|
| 472 | return -1;
|
| 473 | }
|
| 474 | left_len -= read_len;
|
| 475 | next_buf_len = sizeof(buf) < left_len ? sizeof(buf) : left_len;
|
| 476 | }
|
| 477 | return 0;
|
| 478 | }
|
| 479 |
|
| 480 |
|
| 481 | /**
|
| 482 | * @brief ·¢Ë͹æÔòÎļþµ½ap, app devºÍkernel dev.\n
|
| 483 | * @param[in] fd ¹æÔòÎļþÃèÊö·û
|
| 484 | * @param[in] offset ÎļþÆ«ÒÆ
|
| 485 | * @param[in] length ·¢ËÍÊý¾Ý³¤¶È
|
| 486 | * @return 0 on success, -1 otherwise
|
| 487 | * @note
|
| 488 | * @see
|
| 489 | */
|
| 490 | static int send_rules_to_ap(int fd, int offset, int length)
|
| 491 | {
|
| 492 | unsigned char buf[1024] = {0};
|
| 493 | int left_len = 0;
|
| 494 | int next_buf_len = 0;
|
| 495 | int read_len = 0;
|
| 496 |
|
| 497 | if(lseek(fd, offset, SEEK_SET) == -1)
|
| 498 | {
|
| 499 | printf("[zcat] rules file lseek to pos %d failed (%s)\n", offset, strerror(errno));
|
| 500 | return -1;
|
| 501 | }
|
| 502 |
|
| 503 | left_len = length;
|
| 504 | next_buf_len = sizeof(buf) < left_len ? sizeof(buf) : left_len;
|
| 505 | while((read_len = read(fd, buf, next_buf_len)) > 0)
|
| 506 | {
|
| 507 | deal_with_encoded_data(buf, read_len);
|
| 508 | next_buf_len = sizeof(buf) < left_len ? sizeof(buf) : left_len;
|
| 509 | }
|
| 510 | return 0;
|
| 511 | }
|
| 512 |
|
| 513 | /**
|
| 514 | * @brief ¼ì¹æÔòÎļþºÏ·¨ÐÔ£¬¶ÁȡժҪÐÅÏ¢.\n
|
| 515 | * @param[in] fd ¹æÔòÎļþÃèÊö·û
|
| 516 | * @param[out] rule_info ¹æÔòÎļþ¸ÅÒªÐÅÏ¢
|
| 517 | * @return ¹æÔòÎļþµÄ´óС
|
| 518 | * @note
|
| 519 | * @see
|
| 520 | */
|
| 521 | static int get_rule(int fd, rule_struct *rule_info)
|
| 522 | {
|
| 523 | int len = 0;
|
| 524 |
|
| 525 | len = read(fd, rule_info, sizeof(rule_struct));
|
| 526 | if(len <= 0)
|
| 527 | {
|
| 528 | printf("[zcat] rule file len = %d\n", len);
|
| 529 | return len;
|
| 530 | }
|
| 531 | printf("[zcat] rule_info\n\theartbeat_pos : %d\n\theartbeat_len : %d\n\trule_pos : %d\n\trule_len : %d\n",
|
| 532 | rule_info->heartbeat_pos,
|
| 533 | rule_info->heartbeat_len,
|
| 534 | rule_info->rule_pos,
|
| 535 | rule_info->rule_len
|
| 536 | );
|
| 537 |
|
| 538 | if(rule_info->heartbeat_len > sizeof(g_heartbeat_buff))
|
| 539 | {
|
| 540 | printf("[zcat] check_rule, heartbeat voerload %d error.\n", sizeof(g_heartbeat_buff));
|
| 541 | return -1;
|
| 542 | }
|
| 543 |
|
| 544 | // ¶ÁÈ¡¹æÔòÎļþÖеÄÐÄÌø°üÊý¾Ý£¬Ì滻ĬÈÏÐÄÌøÊý¾Ý
|
| 545 | if(lseek(fd, rule_info->heartbeat_pos, SEEK_SET) == -1)
|
| 546 | {
|
| 547 | printf("[zcat] check_rule, lseek to heartbeat_pos error.\n");
|
| 548 | return -1;
|
| 549 | }
|
| 550 | if(read(fd, g_heartbeat_buff, rule_info->heartbeat_len) <= 0)
|
| 551 | {
|
| 552 | printf("[zcat] check_rule, read heartbeat data error.\n");
|
| 553 | return -1;
|
| 554 | }
|
| 555 |
|
| 556 | return len;
|
| 557 | }
|
| 558 |
|
| 559 | /**
|
| 560 | * @brief ¹æÔòºÍÐÄÌø·¢ËÍỊ̈߳¬\n
|
| 561 | * ¶ÁÈ¡sd¿¨ÖеĹæÔòÎļþ·¢Ë͸øpc£¬ÖÜÆÚÐԵķ¢ËÍÐÄÌø.
|
| 562 | * @param[in] arg sd¿¨ÎļþÃèÊö·û
|
| 563 | * @return void
|
| 564 | * @note
|
| 565 | * @see
|
| 566 | */
|
| 567 | static void rule_heartbeat_entry(void *arg)
|
| 568 | {
|
| 569 | BOOL bSendRule = FALSE;
|
| 570 | int rule_file_fd = 0;
|
| 571 | int rule_len = 0;
|
| 572 | rule_struct rule_info = { {0} };
|
| 573 | char* heartbeat_buf = g_heartbeat_buff;
|
| 574 | int heartbeat_len = sizeof(g_heartbeat_buff);
|
| 575 |
|
| 576 | prctl(PR_SET_NAME, "heartbeat");
|
| 577 |
|
| 578 | while(TRUE)
|
| 579 | {
|
| 580 | int write_len = write(cplog_fd, heartbeat_buf, heartbeat_len);
|
| 581 | if (write_len != heartbeat_len)
|
| 582 | {
|
| 583 | printf("[zcat] send heartbeat failed! %d, %d, %d\n", cplog_fd, heartbeat_len, write_len);
|
| 584 | }
|
| 585 |
|
| 586 | sleep(2);
|
| 587 | if(bSDSet == TRUE && bSendRule == FALSE)
|
| 588 | {
|
| 589 | rule_file_fd = open(g_log_sdcard_para.rule_path, O_RDONLY, S_IRUSR);
|
| 590 | if (rule_file_fd < 0)
|
| 591 | {
|
| 592 | printf("[zcat] failed to open rule file %s.\n", g_log_sdcard_para.rule_path);
|
| 593 | return;
|
| 594 | }
|
| 595 |
|
| 596 | // ¼ì²é¹æÔòÎļþºÏ·¨ÐÔ
|
| 597 | rule_len = get_rule(rule_file_fd, &rule_info);
|
| 598 | if(rule_len <= 0)
|
| 599 | {
|
| 600 | close(rule_file_fd);
|
| 601 | return;
|
| 602 | }
|
| 603 | printf("[zcat] check rule success,len is %d\n", rule_len);
|
| 604 |
|
| 605 | // ·¢ËÍcp¹æÔò
|
| 606 | send_rules_to_cp(rule_file_fd, rule_info.rule_pos, rule_info.rule_len);
|
| 607 | printf("[zcat] send rules to cp %d bytes,position %d\n",
|
| 608 | rule_info.rule_len,
|
| 609 | rule_info.rule_pos);
|
| 610 | // ·¢ËÍap¹æÔò
|
| 611 | send_rules_to_cp(rule_file_fd, rule_info.ap_rule_pos, rule_info.ap_rule_len);
|
| 612 | printf("[zcat] send rules to cp %d bytes,position %d\n",
|
| 613 | rule_info.ap_rule_len,
|
| 614 | rule_info.ap_rule_pos);
|
| 615 |
|
| 616 | close(rule_file_fd);
|
| 617 | bSendRule = TRUE;
|
| 618 | }
|
| 619 | }
|
| 620 | }
|
| 621 |
|
| 622 | #if 0
|
| 623 | /**
|
| 624 | * @brief »ñÈ¡sdcardģʽ
|
| 625 | * @param[in] void
|
| 626 | * @return sdcardģʽ
|
| 627 | * @note
|
| 628 | * @see
|
| 629 | */
|
| 630 | static E_FLASH_MODE get_flash_mode()
|
| 631 | {
|
| 632 | E_FLASH_MODE flash_mode = FLASH_MODE_NOMAL;
|
| 633 |
|
| 634 | return flash_mode;
|
| 635 | }
|
| 636 | #endif
|
| 637 |
|
| 638 | /**
|
| 639 | * @brief ³õʼ»¯sdcardģʽ£¬´´½¨¹æÔòºÍÐÄÌø·¢ËÍÏß³Ì.
|
| 640 | * @param[in] void
|
| 641 | * @return 0 on success, errno otherwise
|
| 642 | * @note
|
| 643 | * @see
|
| 644 | */
|
| 645 | int init_sdcard_mode()
|
| 646 | {
|
| 647 | //E_FLASH_MODE flash_mode = FLASH_MODE_NOMAL;
|
| 648 |
|
| 649 | //flash_mode = get_flash_mode();
|
| 650 | pthread_create(&rule_heartbeat_thread, NULL, (void*)rule_heartbeat_entry, NULL);
|
| 651 |
|
| 652 | return 0;
|
| 653 | }
|