liyujie
2025-08-28 d9927380ed7c8366f762049be9f3fee225860833
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <linux/netlink.h>
 
#include <cutils/log.h>
#include <sysutils/NetlinkEvent.h>
#include "hotplug_monitor.h"
 
HotplugMonitor::HotplugMonitor(const Callback *cb, int sockfd)
  : NetlinkListener(sockfd, NetlinkListener::NETLINK_FORMAT_ASCII),
    mSocket(sockfd),
    mCallback(cb)
{
    this->startListener();
}
 
HotplugMonitor::~HotplugMonitor()
{
    this->stopListener();
    close(mSocket);
}
 
void
HotplugMonitor::onEvent(NetlinkEvent *event)
{
    const char *subsys = event->getSubsystem();
    if (!subsys) {
        ALOGE("No subsystem found in netlink event");
        return;
    }
#ifndef SUNXI_USE_LEGACY_KERNEL
    const char* prefix[] = {"extcon", "NAME", "STATE"};
#else
    const char* prefix[] = {"switch", "SWITCH_NAME", "SWITCH_STATE"};
#endif
 
    if (!strcmp(subsys, prefix[0])) {
        if (event->getAction() == NetlinkEvent::Action::kChange) {
            const char *name  = event->findParam(prefix[1]);
            const char *state = event->findParam(prefix[2]);
            if((NULL == name) || (NULL == state)) {
                ALOGE("null point: name=%s, state=%s.", name, state);
                return;
            }
            ALOGD("hotplug change: name=%s, state=%s ", name, state);
            bool plug = (strstr(state, "1") != NULL);
            notifyHotplugChanged(name, plug);
        } else {
            event->dump();
        }
    }
    return;
}
 
#define NEMLEN(_array) \
    (sizeof(_array) / sizeof(_array[0]))
 
static int name2type(const char *name)
{
    struct deviceTypeMap {
        const char *name; const int type;
    };
    const deviceTypeMap nameTypeArray[] = {
        {"hdmi", 4},
        {"cvbs", 2},
    };
 
    for (size_t i = 0; i < NEMLEN(nameTypeArray); i++) {
        if (strcmp(name, nameTypeArray[i].name) == 0)
            return nameTypeArray[i].type;
    }
    return -1;
}
 
void
HotplugMonitor::notifyHotplugChanged(const char *name, int connected)
{
    ALOGD("HotplugMonitor: %s connected=%d", name, connected);
    if (mCallback)
        mCallback->hotplug(mCallback, name2type(name), connected);
    return;
}
 
int
HotplugMonitor::createUeventSocket(void)
{
    struct sockaddr_nl addr;
    int sz = 64*1024;
    int on = 1;
    int sockfd;
 
    memset(&addr, 0, sizeof(addr));
    addr.nl_family = AF_NETLINK;
    addr.nl_pid = ~getpid();
    addr.nl_groups = 0xffffffff;
 
    sockfd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
    if(sockfd < 0) {
        ALOGE("create socket error: %s(%d)", strerror(errno), errno);
        return -1;
    }
    if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz)) < 0) {
        ALOGE("setsockopt(SO_RCVBUF) error: %s(%d)", strerror(errno), errno);
        close(sockfd);
        return -1;
    }
    if (setsockopt(sockfd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
        ALOGE("setsockopt(SO_PASSCRED) error: %s(%d)", strerror(errno), errno);
        close(sockfd);
        return -1;
    }
    if(bind(sockfd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
        ALOGE("bind error: %s(%d)", strerror(errno), errno);
        close(sockfd);
        return 0;
    }
 
    return sockfd;
}