android11 屏蔽usb通过otg转接口外接鼠标设备

硬件平台:QCS6125

软件平台:Android11

需求:Android设备通过接usb转接线连接鼠标功能屏蔽。

考虑到屏蔽的层面可以从两个层面去做,一个是驱动层面不识别,一个就是Android系统层面不识别加载,本篇只讲后者。

1、分析接入鼠标时候的日志:

cpp 复制代码
07-17 11:49:31.326124     0     0 I msm-dwc3 4e00000.ssusb: DWC3 exited from low power mode 
--------- switch to main 
07-17 11:49:31.519494  3131  3310 E AntiCheck: [SECSDK] pid 3309, TracePID is 0 
--------- switch to kernel
07-17 11:49:31.557661     0     0 I usb 1-1 : new low-speed USB device number 2 using xhci-hcd
07-17 11:49:31.715120     0     0 I usb 1-1 : New USB device found, idVendor=046d, idProduct=c077
07-17 11:49:31.715145     0     0 I usb 1-1 : New USB device strings: Mfr=1, Product=2, SerialNumber=0
07-17 11:49:31.715162     0     0 I usb 1-1 : Product: USB Optical Mouse
07-17 11:49:31.715179     0     0 I usb 1-1 : Manufacturer: Logitech
07-17 11:49:31.740283     0     0 I input   : Logitech USB Optical Mouse as /devices/platform/soc/4e00000.ssusb/4e00000.dwc3/xhci-hcd.0.auto/usb1/1-1/1-1:1.0/0111:0003:046D:C077.0001/input/input6
07-17 11:49:31.740748     0     0 I hid-generic 0111: 0003:046D:C077.0001: input: USB HID v1.11 Mouse [Logitech USB Optical Mouse] on usb-xhci-hcd.0.auto-1/input0
--------- switch to main 
07-17 11:49:31.868222  1445  1557 D EventHub: No input device configuration file found for device 'Logitech USB Optical Mouse'.
07-17 11:49:31.869328  1445  1557 I EventHub: usingClockIoctl=true
07-17 11:49:31.869380  1445  1557 I EventHub: New device: id=7, fd=306, path='/dev/input/event6', name='Logitech USB Optical Mouse', classes=0x80000008, configuration='', keyLayout='', keyCharacterMap='', builtinKeyboard=false, 
07-17 11:49:31.871511  1445  1557 I InputReader: Device added: id=8, eventHubId=7, name='Logitech USB Optical Mouse', descriptor='45dd693031521900f038547baa8709cf60c2611f',sources=0x00002002
07-17 11:49:31.874726  3131  3151 I ez.app.launche: Background young concurrent copying GC freed 777596(44MB) AllocSpace objects, 0(0B) LOS objects, 20% free, 91MB/115MB, paused 202us total 119.147ms

可以看到,底层驱动识别后,EventHub也识别了设备一些类型属性,

New device: id=7, fd=306, path='/dev/input/event6', name='Logitech USB Optical Mouse', classes=0x80000008, configuration='', keyLayout='', keyCharacterMap='', builtinKeyboard=false,

遂计划从这里入手:

在EventHub::openDeviceLocked方法中,有对input设备类型的判断和加载,日志显示的是上文标红的类型,查看EventHub.h中设备类型的定义,发现该类型就是鼠标的类型:

/* The input device is a cursor device such as a trackball or mouse. */

INPUT_DEVICE_CLASS_CURSOR = 0x00000008,

2、跟代码逻辑,屏蔽识别该类型的代码段,具体修改如下:

java 复制代码
diff --git a/services/inputflinger/reader/EventHub.cpp b/services/inputflinger/reader/EventHub.cpp
index a1514af66..f726979be 100644
--- a/services/inputflinger/reader/EventHub.cpp
+++ b/services/inputflinger/reader/EventHub.cpp
@@ -67,6 +67,9 @@ using android::base::StringPrintf;
 
 namespace android {
 
+// class type we do not want to support
+static uint32_t NOT_SUPPORT_CLASS_TYPE = 0;
+
 static constexpr bool DEBUG = false;
 
 static const char* DEVICE_PATH = "/dev/input";
@@ -1331,7 +1334,8 @@ status_t EventHub::openDeviceLocked(const char* devicePath) {
     // See if this is a cursor device such as a trackball or mouse.
     if (test_bit(BTN_MOUSE, device->keyBitmask) && test_bit(REL_X, device->relBitmask) &&
         test_bit(REL_Y, device->relBitmask)) {
-        device->classes |= INPUT_DEVICE_CLASS_CURSOR;
+        //device->classes |= INPUT_DEVICE_CLASS_CURSOR;
+        device->classes &= NOT_SUPPORT_CLASS_TYPE;
     }
 
     // See if this is a rotary encoder type device.

将设备类型或的标志位标为不识别的一个类型即可,后文代码识别到class为0,会不走加载流程:

java 复制代码
    // If the device isn't recognized as something we handle, don't monitor it.
    if (device->classes == 0) { 
        ALOGV("Dropping device: id=%d, path='%s', name='%s'", deviceId, devicePath,
              device->identifier.name.c_str());
        delete device;
        return -1;
    }    

从而不挂载,完成。

Mark~~~

相关推荐
Libraeking1 小时前
破壁行动:在旧项目中丝滑嵌入 Compose(混合开发实战)
android·经验分享·android jetpack
市场部需要一个软件开发岗位2 小时前
JAVA开发常见安全问题:Cookie 中明文存储用户名、密码
android·java·安全
JMchen1234 小时前
Android后台服务与网络保活:WorkManager的实战应用
android·java·网络·kotlin·php·android-studio
crmscs4 小时前
剪映永久解锁版/电脑版永久会员VIP/安卓SVIP手机永久版下载
android·智能手机·电脑
localbob4 小时前
杀戮尖塔 v6 MOD整合版(Slay the Spire)安卓+PC端免安装中文版分享 卡牌肉鸽神作!杀戮尖塔中文版,电脑和手机都能玩!杀戮尖塔.exe 杀戮尖塔.apk
android·杀戮尖塔apk·杀戮尖塔exe·游戏分享
机建狂魔4 小时前
手机秒变电影机:Blackmagic Camera + LUT滤镜包的专业级视频解决方案
android·拍照·摄影·lut滤镜·拍摄·摄像·录像
hudawei9964 小时前
flutter和Android动画的对比
android·flutter·动画
lxysbly6 小时前
md模拟器安卓版带金手指2026
android
儿歌八万首7 小时前
硬核春节:用 Compose 打造“赛博鞭炮”
android·kotlin·compose·春节
消失的旧时光-194310 小时前
从 Kotlin 到 Dart:为什么 sealed 是处理「多种返回结果」的最佳方式?
android·开发语言·flutter·架构·kotlin·sealed