blob: 8c1893b155d737178d7bcfe248268a06f6372236 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#include "hotplug.h"
2
3
4static hotplug_parse_func hotplug_parse_ptr[DEVICE_TYPE_MAX] = {NULL};
5static hotplug_proc_func hotplug_proc_ptr[DEVICE_TYPE_MAX] = {NULL};
6
7#define RTC_ALARM_TIMEOUT_MSG "PMIC RTC ALARM IRQ COME"
8#define RTC_TIMER_TIMEOUT_MSG "PMIC RTC TIMER IRQ COME"
9
10static const char *hotplug_action_str[] = {
11 [KOBJ_ADD] = "add",
12 [KOBJ_REMOVE] = "remove",
13 [KOBJ_CHANGE] = "change",
14 [KOBJ_MOVE] = "move",
15 [KOBJ_ONLINE] = "online",
16 [KOBJ_OFFLINE] = "offline",
17};
18
19static int get_action_type(const char *buf, int count)
20{
21 int action;
22
23 if (count && (buf[count - 1] == '\n' || buf[count - 1] == '\0'))
24 count--;
25
26 if (!count)
27 goto err_out;
28
29 for (action = 0; action < KOBJ_MAX; action++) {
30 if (0 == strncmp(hotplug_action_str[action], buf, count)) {
31 return action;
32 }
33 }
34
35err_out:
36 return -1;
37}
38
39
40/*½«UEVENTÖеÄ'\0' Ìæ»»³É'\n'£¬·ÀÖ¹»ñÈ¡²»µ½'\0'ºóÃæµÄÄÚÈÝ*/
41void char_replace(char*buf, int count, char orig_char, char replace_char)
42{
43 int index = 0;
44
45 if (!buf || count <= 0) {
46 slog(NET_PRINT, SLOG_ERR,"[%s][%d]\n", __func__, __LINE__);
47 return;
48 }
49
50 for (index = 0; index < count; ++index) {
51 if (buf[index] == orig_char) {
52 buf[index] = replace_char;
53 }
54 }
55}
56
57/*×¢²ádeviceµÄueventÏûÏ¢½âÎöº¯Êý*/
58int hotplug_parse_register(int device_type, hotplug_parse_func func)
59{
60 if (device_type < DEVICE_TYPE_MAX && device_type >= 0) {
61 hotplug_parse_ptr[device_type] = func;
62 }
63
64 return 0;
65}
66
67/*×¢²ádeviceµÄueventÏûÏ¢½âÎöº¯Êý*/
68int hotplug_proc_register(int device_type, hotplug_proc_func func)
69{
70 if (device_type < DEVICE_TYPE_MAX && device_type >= 0) {
71 hotplug_proc_ptr[device_type] = func;
72 }
73
74 return 0;
75}
76
77
78/*ÈȰβåÏûÏ¢½âÎö*/
79static int hotplug_msg_parse(const char *msg, int msglen, struct hotplug_event *event)
80{
81 int i = 0;
82 int ret = 0;
83 int act_type = 0;
84 char *act_ptr = NULL;
85 char *p = NULL;
86
87 if (NULL == msg || NULL == event) {
88 slog(NET_PRINT, SLOG_ERR,"[%s][%d]\n", __func__, __LINE__);
89 return -1;
90 }
91
92 memset(event, 0, sizeof(struct hotplug_event));
93
94 slog(NET_PRINT, SLOG_ERR, "[%s][%d]uevent message = %s, %d, %d\n", __func__, __LINE__, msg, msglen, strlen(msg));
95
96
97 //´¦ÀíÇý¶¯ÈȲå°ÎÉϱ¨ÏûÏ¢
98 act_ptr = strchr(msg, '@');
99 if (NULL == act_ptr) {
100 slog(NET_PRINT, SLOG_ERR, "hotplug msg parse failed!\n");
101 return -1;
102 }
103
104 act_type = get_action_type(msg, act_ptr - msg);
105
106 if (act_type < 0) {
107 slog(NET_PRINT, SLOG_ERR, "hotplug get action type failed!\n");
108 return -1;
109 }
110 event->action = act_type;
111
112 slog(NET_PRINT, SLOG_NORMAL, "[%s][%d]action = %s\n", __func__, __LINE__, hotplug_action_str[act_type]);
113
114 for (i = 0; i < DEVICE_TYPE_MAX; i++) {
115 if (hotplug_parse_ptr[i]) {
116 ret = hotplug_parse_ptr[i](msg, msglen, event);
117 if (0 == ret){
118 if (hotplug_proc_ptr[event->type])
119 hotplug_proc_ptr[event->type](event);
120 }
121 }
122 }
123
124 return -1;
125}
126
127
128/*ÈȰβåÌ×½Ó×Ö³õʼ»¯*/
129int hotplug_sock_init()
130{
131 int sockfd = 0;
132 int ret;
133 struct sockaddr_nl snl;
134
135 bzero(&snl, sizeof(struct sockaddr_nl));
136 snl.nl_family = AF_NETLINK;
137 snl.nl_pid = getpid();
138 snl.nl_groups = 1;
139
140 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
141 perror("signal");
142
143 sockfd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
144 if (sockfd < 0) {
145 slog(NET_PRINT, SLOG_ERR, "create hotplug socket failed!\n");
146 return -1;
147 }
148// setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &buffersize, sizeof(buffersize));
149
150 ret = bind(sockfd, (struct sockaddr *)&snl, sizeof(struct sockaddr_nl));
151 if (ret < 0) {
152 slog(NET_PRINT, SLOG_ERR, "hotplug socket bind fail!\n");
153 close(sockfd);
154 return -1;
155 }
156
157 return sockfd;
158}
159
160
161void hotplug_init()
162{
163 /*ÍøÂçÉ豸³õʼ»¯*/
164 netdev_hotplug_init();
165 /*Çý¶¯É豸³õʼ»¯*/
166 drv_hotplug_init();
167}
168
169
170int zte_hotplug_main(int argc, char * argv[])
171{
172 int hotplug_fd = 0;
173 fd_set readfds;
174 int maxfd = 0;
175 char buf[UEVENT_BUFFER_SIZE] = {0};
176 int len = 0;
177 int ret = 0;
178 struct hotplug_event hot_event;
179 prctl(PR_SET_NAME, "hotplug", 0, 0, 0);
180 //¸ù¾ÝNV³õʼ»¯´òÓ¡¼¶±ð£¬²¢×¢²á¶¯Ì¬µ÷Õû´òÓ¡¼¶±ðÐźÅÁ¿
181 loglevel_init();
182
183 hotplug_fd = hotplug_sock_init();
184 if (hotplug_fd < 0) {
185 slog(NET_PRINT, SLOG_ERR, "hotplug socket init fail!\n");
186 return -1;
187 }
188
189 /*ÈȰβåÉ豸µÄ³õʼ»¯*/
190 hotplug_init();
191
192 maxfd = hotplug_fd;
193 while (1) {
194 FD_ZERO(&readfds);
195 FD_SET(hotplug_fd, &readfds);
196
197 ret = select(maxfd + 1, &readfds, NULL, NULL, NULL);
198 if (ret == -1 && errno == EINTR)
199 continue;
200
201 if (FD_ISSET(hotplug_fd, &readfds)) {
202 memset(buf, 0, sizeof(buf));
203 len = recv(hotplug_fd, &buf, sizeof(buf), 0);
204 if (len <= 0) {
205 slog(NET_PRINT, SLOG_ERR, "hotplug recv msg fail!\n");
206 continue;
207 }
208
209 /*½øÐÐueventÏûÏ¢½âÎö£¬·µ»Ø½âÎö½á¹û*/
210 ret = hotplug_msg_parse(buf, len, &hot_event);
211#if 0 //klocwork
212 if (ret < 0)
213 continue;
214
215 if (hot_event.type >= DEVICE_TYPE_MAX || hot_event.type < 0)
216 continue;
217#endif
218 /*É豸ueventʼþ´¦Àí
219 if (hotplug_proc_ptr[hot_event.type])
220 hotplug_proc_ptr[hot_event.type](&hot_event);*/
221 }
222 }
223
224 return 0;
225}
226
227