机顶盒的软件设计:
有了上面的硬件连接后,我们要进行如下修改(我们使用到的USB转串口线内置的转换芯片型号为CP2102,下面的Android对应linux kernel都是针对该款芯片配置):
a.内核配置
CONFIG_USB_SERIAL=y
CONFIG_USB_SERIAL_CONSOLE=y
CONFIG_USB_SERIAL_GENERIC=y
CONFIG_USB_SERIAL_CP210X=y
b.内核源码修改进行源码修改,主要是该USB转串口识别到的设备名改下名字,避免与其他类型设备冲突,主要将识别到ttyUSBx改名为ttyUSBDBGx(其中x为0-255之间的数字):
a).修改drivers/usb/serial/bus.c文件,在如下语句:
dev_info(&port->serial->dev->dev,“%s converter now attached to ttyUSB%d\n”,driver->description, minor);
前添加如下语句:
if (driver->driver.name == “cp210x”)
dev_info(&port->serial->dev->dev,“%s converter now attached to ttyUSBDBG%d\n”,driver->description, minor);
else
在如下语句:
dev_info(dev, “%s converter now disconnected from ttyUSB%d\n”,driver->description, minor);
前添加如下语句:
if (driver->driver.name == “cp210x”)
dev_info(dev, “%s converter now disconnected from ttyUSBDBG%d\ndriver->description, minor);
else
b).修改drivers/usb/serial/usb-serial.c文件,在如下语句:
dev_set_name(&port->dev, “ttyUSB%d”, port->number);
前添加如下语句:
if (type->driver.name == “cp210x”) {
usb_serial_tty_driver->name = “ttyUSBDBG”;dev_set_name(&port->dev, “ttyUSBDBG%d”, port->number);}
else
c.Android源码修改
a).在external目录下添加setdebugport目录,包含如下文件和内容:
Android.mk:
LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)
LOCAL_SRC_FILES:= setdebugport.c
LOCAL_MODULE:= setdebugport
include $(BUILD_EXECUTABLE)
setdebugport.c:
#include
#include
#include
int main(int argc, char **argv){
int fd;
if ((fd = open(argv[1], O_RDWR)) < 0) {
fd = open(“/dev/null”, O_RDWR);}
ioctl(fd, TIOCSCTTY, 0);
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
close(fd);
execl(“/system/bin/sh”, “/system/bin/sh”, NULL, NULL, NULL);
return 0;
}
该部分代码的核心在于将“标准输入0”、“标准输出1”、“标准错误输出2”都重定向到打开的串口设备fd,并且执行/system/bin/sh命令,此时该命令的所有输入和输出都会跟对应打开的串口关联起来,那么串口端就可以出现对应的sh交互界面,我们就可以进行调试了(该处参考了adb shell的实现)。
b).在device/{vendor}/{product}/device.mk添加如下语句:
PRODUCT_PACKAGES += setdebugport
c).在device/{vendor}/{product}/init.rc添加如下语句:
service exchgdbgport0 /system/bin/setdebugport /dev/ttyUSBDBG0
class main
user root
group system