在前面内核驱动加载成功后就可以尝试用程序的方式测试该驱动了。
1、进入android源代码路径(注意不是内核源代码路径)
2、cd external
3、mkdir hello
4、cd hello
5、创建hello.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #define DEVICE_NAME "/dev/hello"
- int main(int argc, char** argv)
- {
- int fd = -1;
- int val = 0;
- 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, sizeof(val));
- printf("%d.\n\n", val);
- val = 5;
- printf("Write value %d to %s.\n\n", val, DEVICE_NAME);
- write(fd, &val, sizeof(val));
-
- printf("Read the value again:\n");
- read(fd, &val, sizeof(val));
- printf("%d.\n\n", val);
- close(fd);
- return 0;
- }
6、创建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)
7、回到android源代码根路径
8、make hello
9、make snod
10、cd 安卓源代码路径/out/target/product/generic
11、emulator -image system.img -data userdata.img -ramdisk ramdisk.img -sdcard dcard.img -kernel 安卓内核源代码路径/kernel/goldfish/arch/arm/boot/zImage -avd a_avd&
12、验证结果
- adb shell
- cd /system/bin
- ./hello
阅读(1636) | 评论(0) | 转发(0) |