lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | * @file monitor.c |
| 4 | * @brief |
| 5 | * This file is part of ZCAT. |
| 6 | * zcatÓ¦Óòãlog_agent¼à²âusb/tf/netµÈÍâÉè²å°Îʼþ |
| 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.3 |
| 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 | * --------------------------------------------------------------------------- |
| 29 | * |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <sys/prctl.h> |
| 37 | #include <linux/types.h> |
| 38 | #include <linux/netlink.h> |
| 39 | #include <errno.h> |
| 40 | #include <unistd.h> |
| 41 | #include "../include/message.h" |
| 42 | #include "cfg_api.h" |
| 43 | #include "log_agent.h" |
| 44 | |
| 45 | #define BUFFER_SIZE 2048 |
| 46 | #define TIME_WAIT 30 /*³öÏÖÒì³£»Ö¸´Ê±¼ä*/ |
| 47 | |
| 48 | pthread_t monitor_hotplug_thread = 0; |
| 49 | |
| 50 | #define ZCAT_DETECT_USB_HOTREMOVE "offline@/devices/platform/zx29_hsotg.0/gadget/tty/ttyGS2" |
| 51 | #define ZCAT_DETECT_USB_HOTADD "online@/devices/platform/zx29_hsotg.0/gadget/tty/ttyGS2" |
| 52 | #define ZCAT_DETECT_USB_HOTREMOVE1 "offline@/devices/platform/zx29_hsotg.0/gadget/tty/ttyGS1" |
| 53 | #define ZCAT_DETECT_USB_HOTADD1 "online@/devices/platform/zx29_hsotg.0/gadget/tty/ttyGS1" |
| 54 | #define ZCAT_DETECT_HOTPLUG "@/devices/platform/zx29_hsotg.0/gadget/tty/" |
| 55 | |
| 56 | extern void get_usblog_tty_path(); |
| 57 | extern int close_usblog(); |
| 58 | extern int open_usblog(); |
| 59 | |
| 60 | extern BOOL g_cp_trap_flag; |
| 61 | extern int hb_flag; |
| 62 | extern char g_zcat_usblog[8]; |
| 63 | |
| 64 | static int g_hotplug_sock = -1; |
| 65 | |
| 66 | /** |
| 67 | * @brief ³õʼ»¯ÈȲå°Î¼àÊÓʼþ. |
| 68 | * @param[in] void |
| 69 | * @return ÎļþÃèÊö·û |
| 70 | * @note |
| 71 | * @see |
| 72 | */ |
| 73 | static int init_hotplug_sock() |
| 74 | { |
| 75 | const int buffersize = BUFFER_SIZE; |
| 76 | int ret; |
| 77 | |
| 78 | struct sockaddr_nl snl; |
| 79 | bzero(&snl, sizeof(struct sockaddr_nl)); |
| 80 | snl.nl_family = AF_NETLINK; |
| 81 | snl.nl_pid = getpid(); |
| 82 | snl.nl_groups = 1; |
| 83 | |
| 84 | int s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); |
| 85 | if (s == -1) |
| 86 | { |
| 87 | perror("socket"); |
| 88 | return -1; |
| 89 | } |
| 90 | ret = setsockopt(s, SOL_SOCKET, SO_RCVBUF, &buffersize, sizeof(buffersize)); |
| 91 | if(ret != 0) |
| 92 | { |
| 93 | perror("setsockopt"); |
| 94 | } |
| 95 | |
| 96 | ret = bind(s, (struct sockaddr *)&snl, sizeof(struct sockaddr_nl)); |
| 97 | if (ret < 0) |
| 98 | { |
| 99 | perror("bind"); |
| 100 | close(s); |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | return s; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @brief ÈȲå°Î¼àÊÓÏ̺߳¯Êý. |
| 109 | * @param[in] args Ï̺߳¯Êý²ÎÊý |
| 110 | * @return void |
| 111 | * @note |
| 112 | * @see |
| 113 | */ |
| 114 | static void *monitor_hotplug(void* args) |
| 115 | { |
| 116 | char buf[BUFFER_SIZE] = { 0 }; |
| 117 | int ret = 0; |
| 118 | |
| 119 | prctl(PR_SET_NAME, "monitor_hotplug"); |
| 120 | |
| 121 | while(1) |
| 122 | { |
| 123 | memset(buf, 0, sizeof(buf)); |
| 124 | |
| 125 | get_usblog_tty_path(); |
| 126 | |
| 127 | ret = recv(g_hotplug_sock, &buf, sizeof(buf) - 1, 0); |
| 128 | printf("[zcat] hotplug msg, ret = %d, msg = %s\n", ret, buf); |
| 129 | |
| 130 | if (strstr(buf, ZCAT_DETECT_HOTPLUG)) |
| 131 | { |
| 132 | if(strstr(buf, g_zcat_usblog)) |
| 133 | { |
| 134 | if(strstr(buf, "offline")) |
| 135 | { |
| 136 | hb_flag = 0; |
| 137 | close_usblog(); |
| 138 | continue; |
| 139 | } |
| 140 | |
| 141 | if(strstr(buf, "online")) |
| 142 | { |
| 143 | if(open_usblog() < 0) |
| 144 | { |
| 145 | printf("[zcat] open_usblog error!\n"); |
| 146 | continue; |
| 147 | //return -1; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @brief ³õʼ»¯ÈȲå°Î¼àÊÓÏß³Ì. |
| 158 | * @param[in] void |
| 159 | * @return 0 on success, errno otherwise |
| 160 | * @note |
| 161 | * @see |
| 162 | */ |
| 163 | int init_monitor_hotplug() |
| 164 | { |
| 165 | int ret = -1; |
| 166 | |
| 167 | g_hotplug_sock = init_hotplug_sock(); |
| 168 | printf("[zcat] init_hotplug_sock %d\n", g_hotplug_sock); |
| 169 | if(g_hotplug_sock < 0) |
| 170 | return -1; |
| 171 | |
| 172 | ret = pthread_create(&monitor_hotplug_thread, NULL, monitor_hotplug, NULL); |
| 173 | if(ret != 0) |
| 174 | { |
| 175 | printf("[zcat] create monitor_hotplug_thread error\n"); |
| 176 | return -1; |
| 177 | } |
| 178 | return ret; |
| 179 | } |