• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "hdi_netlink_monitor.h"
17 #include <arpa/inet.h>
18 #include <cerrno>
19 #include <netinet/in.h>
20 #include <sys/socket.h>
21 #include <linux/netlink.h>
22 #include <linux/types.h>
23 #include "display_common.h"
24 #include "display_device.h"
25 #include "display_layer.h"
26 #include "hdi_session.h"
27 
28 namespace OHOS {
29 namespace HDI {
30 namespace DISPLAY {
HdiNetLinkMonitor()31 HdiNetLinkMonitor::HdiNetLinkMonitor()
32 {
33     DISPLAY_DEBUGLOG();
34 }
35 
Init()36 int HdiNetLinkMonitor::Init()
37 {
38     DISPLAY_DEBUGLOG();
39     mThread = std::make_unique<std::thread>(std::bind(&HdiNetLinkMonitor::MonitorThread, this));
40     mThread->detach();
41     mRunning = true;
42     return DISPLAY_SUCCESS;
43 }
44 
~HdiNetLinkMonitor()45 HdiNetLinkMonitor::~HdiNetLinkMonitor()
46 {
47     DISPLAY_DEBUGLOG();
48     if (mScoketFd >= 0) {
49         close(mScoketFd);
50     }
51 }
52 
ThreadInit()53 static int ThreadInit()
54 {
55     int ret;
56     int fd = -1;
57     const int32_t bufferSize = 1024;
58     struct sockaddr_nl snl;
59 
60     bzero(&snl, sizeof(struct sockaddr_nl));
61     snl.nl_family = AF_NETLINK;
62     snl.nl_pid = getpid();
63     snl.nl_groups = 1;
64     fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
65     DISPLAY_CHK_RETURN(fd < 0, DISPLAY_FAILURE, DISPLAY_LOGE("socket fail"));
66     ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
67     if (ret == -1) {
68         DISPLAY_LOGE("setsockopt fail");
69         close(fd);
70         return DISPLAY_FAILURE;
71     }
72     ret = bind(fd, (struct sockaddr *)&snl, sizeof(struct sockaddr_nl));
73     if (ret < 0) {
74         DISPLAY_LOGE("bind fail");
75         close(fd);
76         return DISPLAY_FAILURE;
77     }
78     return fd;
79 }
80 
ParseUeventMessage(const char * buf,uint32_t len)81 static void ParseUeventMessage(const char *buf, uint32_t len)
82 {
83     uint32_t num;
84 
85     for (num = 0; num < len;) {
86         const char *event = buf + num;
87         if (strcmp(event, "STATE=HDMI=0") == 0) {
88             HdiSession::GetInstance().HandleHotplug(false);
89             break;
90         } else if (strcmp(event, "STATE=HDMI=1") == 0) {
91             HdiSession::GetInstance().HandleHotplug(true);
92             break;
93         }
94         num += strlen(event) + 1;
95     }
96 }
97 
MonitorThread()98 void HdiNetLinkMonitor::MonitorThread()
99 {
100     constexpr int BUFFER_SIZE = 2048; /* buffer for the variables */
101     int len;
102     int fd = -1;
103 
104     fd = ThreadInit();
105     DISPLAY_CHK_RETURN_NOT_VALUE(fd < 0, DISPLAY_LOGE("socket fail"));
106     mScoketFd = fd;
107     while (mRunning) {
108         char buf[BUFFER_SIZE] = { 0 };
109         len = read(fd, &buf, sizeof(buf));
110         if (len < 0) {
111             DISPLAY_LOGE("read uevent message fail");
112             break;
113         }
114         ParseUeventMessage(buf, len);
115     }
116 }
117 } // DISPLAY
118 }  // HDI
119 }  // OHOS
120