Add Hardware abstraction Layer (HAL) module to Android to access Linux kernel driver in Ubuntu _android

Source: Internet
Author: User
Tags stub

In the context of the Ubuntu Android simple introduction to the Hardware Abstraction layer (HAL), we briefly describe the way in which the Android system writes drivers for the hardware. In simple terms, the hardware drivers are distributed in the Linux kernel and distributed in the hardware abstraction layer of the user space on the other hand. Then the Linux kernel driver implementation method on the Ubuntu Android system is illustrated with examples of how to write drivers in the Linux kernel. In this article, we will continue to introduce the other implementation of the Android hardware driver, which is how to add hardware modules to the hardware abstraction layer to interact with kernel drivers. In this article, we will also learn how to modify device file patterns using Linux-like Udev rules when creating device files on the Android system.

First , please prepare the sample kernel-driven sequence as shown in the article on the implementation of the Linux kernel driver in Ubuntu Android system . After completing this kernel driver, you can get three files in the Android system,/dev/hello,/sys/class/hello/hello/val, and/proc/hello. In this article, we will connect the hardware Abstraction Layer module and the Linux kernel driver module through the device file/dev/hello.

two. Enter into the Hardware/libhardware/include/hardware directory, create a new hello.h file:

user-name@machine-name:~/android$ CD Hardware/libhardware/include/hardware

user-name@machine-name:~/android/hardware/libhardware/include/hardware$ VI hello.h

The contents of the Hello.h file are as follows:

#ifndef android_hello_interface_h
#define ANDROID_HELLO_INTERFACE_H
#include  
 

Here, according to the requirements of the Android Hardware Abstraction Layer specification, the module ID, module structure and hardware interface structure are defined separately. In the hardware interface structure, FD represents the device file descriptor, which corresponds to the device file "/dev/hello" that we will process, set_val and get_val to the function interface provided on the HAL pair.

three. Go to the Hardware/libhardware/modules directory, create a new Hello directory, and add the hello.c file. the content of hello.c is more, we look at the paragraph.

The first is to include the related header file and define the related structure:

#define LOG_TAG "Hellostub" #include  

Here, the instance variable name must be Hal_module_info_sym,tag and must be Hardware_module_tag, as specified in the Android Hardware Abstraction Layer specification.

define the Hello_device_open function:

 static int Hello_device_open (const struct hw_module_t* module, const char* name, struct h
	
	w_device_t** device) {struct hello_device_t* Dev;dev = (struct hello_device_t*) malloc (sizeof (struct hello_device_t));
		if (!dev) {LOGE ("Hello stub:failed to Alloc spaces");
	Return-efault;
	} memset (Dev, 0, sizeof (struct hello_device_t));
	Dev->common.tag = Hardware_device_tag;
	dev->common.version = 0;
	Dev->common.module = (hw_module_t*) module;
	Dev->common.close = Hello_device_close;

	Dev->set_val = Hello_set_val;dev->get_val = Hello_get_val; if ((dev->fd = open (device_name, o_rdwr) = = = 1) {LOGE ("Hello stub:failed to Open/dev/hello--%s.", Strerror (errno
		), free (dev);
	Return-efault;
	} *device = & (Dev->common);

	Logi ("Hello Stub:open/dev/hello successfully."); 
return 0; 

Device_name is defined as "/dev/hello". Because the device files are created through device_create in the kernel driver, the device files created by Device_create are only read by the root user, and Hello_device_open is typically called by the upper app. These apps typically do not have root permissions, which results in the failure to open device files:

Hello stub:failed to Open/dev/hello-Permission denied.

The solution is similar to the Linux Udev rule, open the Android source code engineering directory, enter into the System/core/rootdir directory, which has a name ueventd.rc file, add a line to the inside:

/dev/hello 0666 root root

Define the three functions of Hello_device_close, Hello_set_val, and Hello_get_val:

static int hello_device_close (struct hw_device_t* device) {struct hello_device_t* hello_device
	= (struct hello_ device_t*) device;

	if (hello_device) {close
		(HELLO_DEVICE->FD);
		Free (hello_device);
	}
	
	return 0;
}

static int hello_set_val (struct hello_device_t* dev, int val) {
	logi ("Hello Stub:set value%d to device.", Val); 
   
    write (DEV->FD, &val, sizeof (Val));

	return 0;
}

static int hello_get_val (struct hello_device_t* dev, int* val) {
	if (!val) {
		LOGE ("Hello stub:error val Pointer" );
		Return-efault;
	}

	Read (DEV->FD, Val, sizeof (*val));

	Logi ("Hello stub:get value%d from device", *val);

	return 0;
}

   

Four. Continue the new android.mk file in the Hello directory:

Local_path: = $ (call My-dir)
Include $ (clear_vars)
Local_module_tags: = Optional
Local_prelink_module: = False
Local_module_path: = $ (target_out_shared_libraries)/HW
Local_shared_libraries: = Liblog
Local_src_files: = hello.c
Local_module: = Hello.default
Include $ (build_shared_library)

Note: Local_module's definition rule, hello followed by default,hello.default to ensure that our modules are always loaded with the hard image abstraction layer.

Five. Compile:

user-name@machine-name:~/android$ mmm Hardware/libhardware/modules/hello

After the compilation is successful, you can see the hello.default.so file in the OUT/TARGET/PRODUCT/GENERIC/SYSTEM/LIB/HW directory.

Six. Repackage the android system Mirror SYSTEM.IMG:

user-name@machine-name:~/android$ make Snod

After repackaging, SYSTEM.IMG contains our defined Hardware Abstraction Layer module HELLO.DEFAULT.

Although we have added a hardware Abstraction Layer module to our own hardware in the Android system, Java applications are not yet able to access our hardware. We also have to write JNI methods and add API interfaces to the Android application frameworks layer to get the upper application access to our hardware. In the following article, we will also complete this system process so that we can access our own customized hardware in a Java application.

This is the Android Hardware Abstraction Layer (HAL) module to access the Linux kernel driver data collation, follow-up continue to add, hope to help learn the Android code friends.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.