Single

hotplug.json解析

由于产测需要用到hotplug,而dfu设备不会触发hotplug,所以这里需要更改hotplug.json这个文件,这个文件在/etc/hotplug.json
要看懂这个文件,要先知道这些词语
eq 等于
neq 不等于
gt 大于
egt 大于等于
lt 小于
elt 小于等于
like LIKE
between BETWEEN
代入进去就知道是什么意思了
例子一:

[
	[ "case", "ACTION", {
		"add": [
			[ "if",
				[ "and",
					[ "has", "MAJOR" ],
					[ "has", "MINOR" ]
				],
				[...]
			]
		]
	} ],
]

换成能看的懂的语言就是

select action 
{
case "add":
     if has(marjor) && has(mior) {...}
}

例子二:

[ "if",
    [ "eq", "DEVNAME",
        [ "null", "full", "ptmx", "zero", "tty", "net", "random", "urandom" ]
    ],
    [
        [ "makedev", "/dev/%DEVNAME%", "0666" ],
        [ "return" ]
    ]
]

换成能看懂的语言就是

if devname == null full ptmx zero tty net random  {
	makedev /dev/%devname% 
	return
}

相信只要能看懂这个,就能看懂下面正文并能够修改了

正文

root@OpenWrt:~# cat /etc/hotplug.json 

[
        [ "case", "ACTION", {
                "add": [
                        [ "if",
                                [ "and",
                                        [ "has", "MAJOR" ],
                                        [ "has", "MINOR" ],
                                ],
                                [
                                        [ "if",
                                                [ "or",
                                                        [ "eq", "DEVNAME",
                                                                [ "null", "full", "ptmx", "zero" ],
                                                        ],
                                                        [ "regex", "DEVNAME",
                                                                [ "^gpio", "^hvc" ],
                                                        ],
                                                ],
                                                [
                                                        [ "makedev", "/dev/%DEVNAME%", "0666" ],
                                                        [ "return" ],
                                                ]
                                        ],
                                        [ "if",
                                                [ "or",
                                                        [ "eq", "DEVNAME", "mapper/control" ],
                                                        [ "regex", "DEVPATH", "^ppp" ],
                                                ],
                                                [
                                                        [ "makedev", "/dev/%DEVNAME%", "0600" ],
                                                        [ "return" ],
                                                ],
                                        ],
                                        [ "if",
                                                [ "has", "DEVNAME" ],
                                                [ "makedev", "/dev/%DEVNAME%", "0644" ],
                                        ],
                                ],
                        ],
                        [ "if",
                                [ "has", "FIRMWARE" ],
                                [
                                        [ "exec", "/sbin/hotplug-call", "%SUBSYSTEM%" ],
                                        [ "load-firmware", "/lib/firmware" ],
                                        [ "return" ]
                                ]
                        ],
                ],
                "remove" : [
                        [ "if",
                                [ "and",
                                        [ "has", "DEVNAME" ],
                                        [ "has", "MAJOR" ],
                                        [ "has", "MINOR" ],
                                ],
                                [ "rm", "/dev/%DEVNAME%" ]
                        ]
                ]
        } ],
        [ "if",
                [ "eq", "SUBSYSTEM", "platform" ],
                [ "exec", "/sbin/hotplug-call", "%SUBSYSTEM%" ]
        ],
        [ "if",
                [ "and",
                        [ "has", "BUTTON" ],
                        [ "eq", "SUBSYSTEM", "button" ],
                ],
                [ "exec", "/etc/rc.button/%BUTTON%" ]
        ],
        [ "if",
                [ "eq", "SUBSYSTEM",
                        [ "net", "input", "usb", "ieee1394", "block", "atm", "zaptel", "tty", "button" ]
                ],
                [ "exec", "/sbin/hotplug-call", "%SUBSYSTEM%" ]
        ],
        [ "if",
                [ "and",
                        [ "eq", "SUBSYSTEM", "usb-serial" ],
                        [ "regex", "DEVNAME",
                                [ "^ttyUSB", "^ttyACM" ]
                        ],
                ],
                [ "exec", "/sbin/hotplug-call", "tty" ]
        ]
]

可以看到,这个配置文件主要是
1、定义add相应动作
2、定义remove相应动作
3、如果出现键盘事件并执行的脚本位置
4、如果出现usb串口事件并执行的脚本位置
其中我们之前用到的BPI:Bit以及BPI:smart烧录脚本都是用到了下面这一行

        
[ "if",
    [ "and",
        [ "eq", "SUBSYSTEM", "usb-serial" ],
        [ "regex", "DEVNAME",
            [ "^ttyUSB", "^ttyACM" ]
        ],
    ],
    [ "exec", "/sbin/hotplug-call", "tty" ]
]

这几行代码的意思就是
如果SUBSYSTEM=usb-serial,且DEVNAME已ttyUSB或者ttyACM开头(regex就是执行正则表达式),就执行/sbin/hotplug-call脚本(exec是执行脚本的命令)。而脚本内容如下

root@OpenWrt:~# cat /sbin/hotplug-call
#!/bin/sh
# Copyright (C) 2006-2016 OpenWrt.org

export HOTPLUG_TYPE="$1"

. /lib/functions.sh

PATH="/usr/sbin:/usr/bin:/sbin:/bin"
LOGNAME=root
USER=root
export PATH LOGNAME USER
export DEVICENAME="${DEVPATH##*/}"

[ \! -z "$1" -a -d /etc/hotplug.d/$1 ] && {
        for script in $(ls /etc/hotplug.d/$1/* 2>&-); do (
                [ -f $script ] && . $script
        ); done
}

可以看到,会自动执行/etc/hotplug.d/$1/下面所有脚本($1就是前面的tty),如果tty设备插入后,要执行某个脚本,只需把脚本放入/etc/hotplug.d/tty/即可

暂无评论

发表评论

zh_CNChinese