本文是在上文3. 编写linux内核驱动程序(Android 10)的基础上进行的,虚拟机环境就是上文结束时的环境。本文将使用c可执行程序调用并测试上文写的驱动程序的read函数和write函数。
增加c可执行程序
在external目录下新建hello文件夹,在hello文件夹中新建hello.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define DEVICE_NAME "/dev/hello"
#define VAL_LENGTH 100
int main()
{
int fd = -1;
char val[VAL_LENGTH];
char new_val[VAL_LENGTH];
fd = open(DEVICE_NAME, O_RDWR);
if(fd == -1) {
printf("Failed to open device %s.\n", DEVICE_NAME);
return -1;
}
printf("Read original value:\n");
read(fd, val, VAL_LENGTH-1);
printf("%s.\n\n", val);
char val2[VAL_LENGTH] = "written_by_c";
printf("Write value %s to %s.\n\n", val2, DEVICE_NAME);
write(fd, val2, strlen(val2)+1);
printf("Read the value again:\n");
read(fd, new_val, VAL_LENGTH-1);
printf("%s.\n\n", new_val);
close(fd);
return 0;
}
在hello文件夹中新建Android.mk文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := hello
LOCAL_SRC_FILES := $(call all-subdir-c-files)
include $(BUILD_EXECUTABLE)
编译
编译hello模块
cd ~/Documents/aosp10
export TARGET_PREBUILT_KERNEL=/home/test/Documents/msm/arch/arm64/boot/Image.lz4-dtb
source build/envsetup.sh
lunch
aosp_walleye-userdebug
mmm ./external/hello
可以看到生成的hello可执行程序
重新打包system.img
time make snod -j16
time make -j16
重新刷入手机
sudo su
source build/envsetup.sh
lunch
aosp_walleye-userdebug
adb shell reboot bootloader
cd out/target/product/walleye/
fastboot flashall -w
测试
adb shell
su
cd system/bin
./hello
本文涉及到的知识主要和Android架构中的linux内核层、系统运行库层相关