1. Develop the Android hardware abstraction layer code
~ Android-2.3_r1/hardware/libhardware
---- Include
---- Hardware
---- Freg. h
---- Hareware. h
----- Modules
---- Freg
---- Freg. cpp
---- Android. mk
Hareware. h
.........#ifndef ANDROID_INCLUDE_HARDWARE_HARDWARE_H#define ANDROID_INCLUDE_HARDWARE_HARDWARE_H#include
#include
#include
__BEGIN_DECLS/* * Value for the hw_module_t.tag field */#define MAKE_TAG_CONSTANT(A,B,C,D) (((A) << 24) | ((B) << 16) | ((C) << 8) | (D))#define HARDWARE_MODULE_TAG MAKE_TAG_CONSTANT('H', 'W', 'M', 'T')#define HARDWARE_DEVICE_TAG MAKE_TAG_CONSTANT('H', 'W', 'D', 'T')struct hw_module_t;struct hw_module_methods_t;struct hw_device_t;/** * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM * and the fields of this data structure must begin with hw_module_t * followed by module specific information. */typedef struct hw_module_t { /** tag must be initialized to HARDWARE_MODULE_TAG */ uint32_t tag; /** major version number for the module */ uint16_t version_major; /** minor version number of the module */ uint16_t version_minor; /** Identifier of module */ const char *id; /** Name of this module */ const char *name; /** Author/owner/implementor of the module */ const char *author; /** Modules methods */ struct hw_module_methods_t* methods; /** module's dso */ void* dso; /** padding to 128 bytes, reserved for future use */ uint32_t reserved[32-7];} hw_module_t;typedef struct hw_module_methods_t { /** Open a specific device */ int (*open)(const struct hw_module_t* module, const char* id, struct hw_device_t** device);} hw_module_methods_t;/** * Every device data structure must begin with hw_device_t * followed by module specific public methods and attributes. */typedef struct hw_device_t { /** tag must be initialized to HARDWARE_DEVICE_TAG */ uint32_t tag; /** version number for hw_device_t */ uint32_t version; /** reference to the module this device belongs to */ struct hw_module_t* module; /** padding reserved for future use */ uint32_t reserved[12]; /** Close this device */ int (*close)(struct hw_device_t* device);} hw_device_t;/** * Name of the hal_module_info */#define HAL_MODULE_INFO_SYM HMI/** * Name of the hal_module_info as a string */#define HAL_MODULE_INFO_SYM_AS_STR "HMI"/** * Get the module info associated with a module by id. * @return: 0 == success, <0 == error and *pHmi == NULL */int hw_get_module(const char *id, const struct hw_module_t **module);........__END_DECLS#endif /* ANDROID_INCLUDE_HARDWARE_HARDWARE_H */
Hw_module_t struct:
(1) There is a comment before the definition of the struct hw_module_t, which means that each module in the hardware abstraction layer must customize a module structure in the hardware abstraction layer, the type of its first member variable must be hw_module_t.
(2) Each module in the hardware abstraction layer must have an export symbol HAL_MODULE_IFNO_SYM, that is, "HMI", which points to a custom module structure in the hardware abstraction layer.
(3) The value of the member variable tag of the struct hw_module_t must be set to HARWARE_MODULE_TAG, that is, it is set to a constant value ('H' <24 | 'W' <16 | 'M' <8 | 'T '), this is a module structure in the hardware abstraction layer.
(4) the dso member variable of the hw_module_t struct is used to save the handle value obtained after loading the hardware abstraction layer module.
(5) The member variable methods of the hw_module_t struct defines a list of operation methods for the hardware abstraction layer module. Its type is hw_module_methods_t. Next we will introduce its definition.
Hw_module_methods_t struct:
(1) There is only one member variable. It is a function pointer used to open hardware devices in the hardware abstraction layer module. The parameter module indicates the module of the hardware device to be opened. The parameter id indicates the ID of the hardware device to be opened. The parameter device is an output parameter, used to describe an opened hardware device. A hardware abstraction layer module may contain multiple hardware devices. Therefore, when you call the open member variable of the hw_module_methods_t struct to open a hardware device, we need to specify its ID.
Hw_device_t struct:
(1) Each hardware device in the hardware abstraction layer module must customize a hardware device structure, and its first member variable type must be hw_device_t.
(2) The value of the member variable tag of the struct hw_device_t must be set to HARDWARE_DEVICE_TAG, that is, it is set to a constant value ('H' <24 | 'W' <16 | 'D' <8 | 'T '), indicates the hardware device structure in the hardware abstraction layer.
(3) The close member variable of the hw_device_t struct is a function pointer used to close a hardware device.
At this point, the specification for Writing module interfaces at the hardware abstraction layer has been introduced.
Freg. h
#ifndef ANDROID_FREG_INTERFACE_H#define ANDROID_FREG_INTERFACE_H#include
__BEGIN_DECLS/** * The id of this module */#define FREG_HARDWARE_MODULE_ID "freg"/** * The id of this device */#define FREG_HARDWARE_DEVICE_ID "freg"struct freg_module_t {struct hw_module_t common;};struct freg_device_t {struct hw_device_t common;int fd;int (*set_val)(struct freg_device_t* dev, int val);int (*get_val)(struct freg_device_t* dev, int* val);};__END_DECLS#endif
The macro FREG_HARDWARE_MODULE_ID and FREG_HARDWARE_DEVICE_ID are used to describe the module ID and device ID respectively. The struct freg_module_t is used to describe the custom module struct. Its first member variable type is hw_module_t. The structure freg_device_t is used to describe the Virtual Hardware Device freg. The type of its first member variable is freg_device_t. In addition, the structure freg_device_t defines three other member variables. The member variable fd is a file descriptor used to describe the opened Device File/dev/freg, the member variables set_val and get_val are function pointers used to write and read the val contents of the freg register of the Virtual Hardware Device respectively.
Freg. cpp
# Define LOG_TAG "FregHALStub" # include
# Include
# Include
# Include
# Include
# Include
# Define DEVICE_NAME "/dev/freg" # define MODULE_NAME "Freg" # define MODULE_AUTHOR "shyluo@gmail.com" // because these functions are used before implementation, so first define static int freg_device_open (const struct hw_module_t * module, const char * id, struct hw_device_t ** device); static int freg_device_close (struct hw_device_t * device ); static int freg_set_val (struct freg_device_t * dev, int val); static int freg_get_val (struct freg_device_t * dev, int * va L); static struct functions freg_module_methods = {open: freg_device_open}; struct freg_module_t HAL_MODULE_INFO_SYM = {// A Hardware Abstraction Layer module must export a common: {tag: HARDWARE_MODULE_TAG, // The tag value must be set to Priority: 1, version_minor: 0, id: Role, name: MODULE_NAME, author: MODULE_AUTHOR, methods: & freg_module_methods ,}}; static int freg_device_open (con St struct hw_module_t * module, const char * id, struct hw_device_t ** device) {if (! Strcmp (id, FREG_HARDWARE_DEVICE_ID) {// match IDstruct freg_device_t * dev; dev = (struct freg_device_t *) malloc (sizeof (struct freg_device_t )); // assign the freg_device_t struct if (! Dev) {LOGE ("Failed to alloc space for freg_device_t. "); return-EFAULT;} memset (dev, 0, sizeof (struct freg_device_t); dev-> common. tag = HARDWARE_DEVICE_TAG; // It must be initialized to HARDWARE_DEVICE_TAGdev-> common. version = 0; dev-> common. module = (hw_module_t *) module; dev-> common. close = freg_device_close; // close the function dev-> set_val = freg_set_val; dev-> get_val = freg_get_val; if (dev-> fd = open (DEVICE_NAME, O_RDWR )) =-1) {LOGE ("Failed to open device file/dev/freg -- % s. ", strerror (errno); free (dev); return-EFAULT;} * device = & (dev-> common ); LOGI ("Open device file/dev/freg successfully. "); return 0;} return-EFAULT;} static int freg_device_close (struct hw_device_t * device) {struct freg_device_t * freg_device = (struct freg_device_t *) device; if (freg_device) {close (freg_device-> fd); free (freg_device);} return 0;} static int freg_s Et_val (struct freg_device_t * dev, int val) {if (! Dev) {LOGE ("Null dev pointer. "); return-EFAULT;} LOGI (" Set value % d to device file/dev/freg. ", val); write (dev-> fd, & val, sizeof (val); return 0;} static int freg_get_val (struct freg_device_t * dev, int * val) {if (! Dev) {LOGE ("Null dev pointer."); return-EFAULT;} if (! Val) {LOGE ("Null val pointer. "); return-EFAULT;} read (dev-> fd, val, sizeof (* val); LOGI (" Get value % d from device file/dev/freg. ", * val); return 0 ;}
In this Code, the most noteworthy is the definition of the module variable HAL_MODULE_INFO_SYM. According to the specification written by the hardware abstraction layer module, each Hardware Abstraction Layer module must export a symbol named HAL_MODULE_INFO_SYM, which points to a custom hardware abstraction layer module structure, in addition, the tag value of the member variable whose first type is hw_module_t must be set to HARDWARE_MODULE_TAG. In addition, the version number, ID, name, author, and Operation Method list of the module structure in the hardware abstraction layer are initialized.
Android. mk
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_PRELINK_MODULE := falseLOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hwLOCAL_SHARED_LIBRARIES := liblogLOCAL_SRC_FILES := freg.cppLOCAL_MODULE := freg.defaultinclude $(BUILD_SHARED_LIBRARY)
Include $ (BUILD_SHARED_LIBRARY) indicates to compile the hardware abstraction layer module into a dynamic link library file named freg. default.
2. Compile
Compile the Hardware Abstraction Layer:
The generated dynamic link library file, named freg. default. so, is saved in out/target/product/generic Libraries? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vc3lzdGVtL2xpYi9od8S/release + c1_vcd4kpha + ICAgICAgtPKw/release =" http://www.2cto.com/uploadfile/Collfiles/20140609/20140609091226145.png "alt =" n blocks of limbs Gu Yu have been missing? Http://www.bkjia.com/kf/yidong/Android/ "target =" _ blank "class =" keylink "> Android system image file system. img.