Linux的uevent事件机制
作者:互联网
转自:https://blog.csdn.net/qq_30624591/article/details/92439991
uevent事件机制
Linux设计了一种uevent的机制。当有新的设备加入的时候,将设备的信息发送消息到用户态。而用户态有一个udev的进程监听这个信息。当收到信息后做一定的解析,根据解析到的结果和用户程序的配置做一些处理,也包括加载驱动程序。
内核部分,Uevent是Kobject的一部分,用于在Kobject状态发生改变时,例如增加、移除等,通知用户空间程序,主要涉及kobject.h和kobject_uevent.c两个文件,如下:
include/linux/kobject.h
lib/kobject_uevent.c
-
内核上报事件,调用kobject_uevent_env函数进行上报事件
-
int kobject_uevent_env(struct kobjectkobj, enum kobject_action action,charenvp_ext[])
static const char *kobject_actions[] ={
[KOBJ_ADD] = “add”,
[KOBJ_REMOVE] = “remove”,
[KOBJ_CHANGE] = “change”,
[KOBJ_MOVE] = “move”,
[KOBJ_ONLINE] = “online”,
[KOBJ_OFFLINE] = “offline”,
};
比如下面的一个HDMI插拔的uevent字符串
{SUBSYSTEM=extcon, SEQNUM=2706, ACTION=change, DEVTYPE=hdmi_audio, STATE=HDMI=1, DEVPATH=/devices/virtual/amhdmitx/amhdmitx0/hdmi_audio, NAME=hdmitx_extcon_audio}
android层面去读取uevent事件
frameworks/base/core/jni/android_os_UEventObserver.cpp
uevent_init() //open socket for UEventObserver
uevent_next_event(buffer, sizeof(buffer) //读取uevent事件
for (;;) {
int length = uevent_next_event(buffer, sizeof(buffer) - 1);
if (length <= 0) {
return NULL;
}
buffer[length] = '\0';
ALOGV("Received uevent message: %s", buffer);
if (isMatch(buffer, length)) {
// Assume the message is ASCII.
jchar message[length];
for (int i = 0; i < length; i++) {
message[i] = buffer[i];
}
return env->NewString(message, length);
}
}
frameworks/base/core/java/android/os/UEventObserver.java
创建一个UEventThread线程不停的读取消息
while (true) {
String message = nativeWaitForNextEvent();
if (message != null) {
if (DEBUG) {
Log.d(TAG, message);
}
sendEvent(message);
}
}
amlogic平台调试的例子
-
这里主要是添加了gpio按键的uevent事件上报给android,下面是一个上报的简单例子,主要是传输字符串,这里可以任意指定
char data[64];
char *envp[] = { data, NULL };
snprintf(data, sizeof(data), “code=%d,state=up”, key->code);
kobject_uevent_env(&kp->config_dev->kobj, KOBJ_CHANGE, envp); -
app里面的修改
mObserver.startObserving("DEVPATH=/devices/virtual/gpio_keyboard/gpio_keyboard"); mObserver.startObserving("DEVPATH=/devices/adc_keypad.13/input/input1"); private UEventObserver mObserver = new UEventObserver() { @Override public void onUEvent(UEventObserver.UEvent event) { Log.i("clei onUEvent","event:"+event +" code = "+event.get("code")); } };
-
注意点startObserving这个里面传入的是设备的驱动路径,必须填写正确,onUEvent接收到的就是回调的时间信息
09-11 00:08:59.942 7450 7474 I clei onUEvent: event:{DEVNAME=gpio_keyboard, SUBSYSTEM=gpio_keyboard, SEQNUM=4095, MAJOR=222, ACTION=change, DEVPATH=/devices/virtual/gpio_keyboard/gpio_keyboard, MINOR=0, code=473,state=down} code = 473,state=down 09-11 00:09:00.151 7450 7474 I clei onUEvent: event:{DEVNAME=gpio_keyboard, SUBSYSTEM=gpio_keyboard, SEQNUM=4096, MAJOR=222, ACTION=change, DEVPATH=/devices/virtual/gpio_keyboard/gpio_keyboard, MINOR=0, code=473,state=up} code = 473,state=up 09-11 00:08:55.147 7450 7474 I clei onUEvent: event:{PROP=0, SUBSYSTEM=input, PRODUCT=10/1/1/100, SEQNUM=4091, EV=100003, ACTION=change, DEVPATH=/devices/adc_keypad.13/input/input1, PHYS="adc_keypad/input0", KEY=3c000000 0 0 0 0 0 40000 0, NAME="adc_keypad", MODALIAS=input:b0010v0001p0001e0100-e0,1,14,k1DA,1DB,1DC,1DD,ramlsfw, code=477,state=down} code = 477,state=down 09-11 00:08:55.291 7450 7474 I clei onUEvent: event:{PROP=0, SUBSYSTEM=input, PRODUCT=10/1/1/100,
标签:code,uevent,Linux,kobject,keyboard,gpio,机制,event 来源: https://blog.csdn.net/wenjin359/article/details/100535680