全面编写hello驱动的HAL层代码到APP层

Android架构实例分析之编写hello驱动的HAL层代码通到APP

摘要:

HAL层中文名称又叫硬件抽象层,可以理解我Linux驱动的应用层。本文实现了一个简单的hello HAL的代码,衔接hello驱动和

Android标准架构实例分析之编写最简单的hello驱动

HAL层的代码会使用open read write等系统调用操作驱动层的文件系统(dev、sysfs、proc),同时它也有自己的数据结构,为上层提供接口,并导出和本体模块相关ID号。上层可以使用hw_get_module获得HAL中写的方法。其实如果不是为了避开Linux的GPL开源协议,HAL层可以不用,直接使用JNI层或者在Java代码中访问操作驱动层的文件系统(dev、sysfs、proc)也是可行的。 
详细原理可以参考:

http://blog.csdn.net/eliot_shao/article/details/50461738

HAL代码编写的基本步骤

1、编写HAL层头文件

2、填充相关的结构体

1、编写HAL层头文件

首先我们要知道关于HAL的三点要求:

1、 每一个hardware硬件模块都有一个ID;

2、 每一个hardware模块必须有一个继承struct hw_module_t common;的结构体;

3、 每一个hardware模块必须有一个继承struct hw_device_t common;的结构体;

struct hw_module_t的继承者担负了“联络员”的任务,在/system/lib/hw下面有若干hardware module,本地框架层通过ID找到对应的模块。

struct hw_device_t的继承者承担了对驱动操作方法的包装的任务。 
struct hw_module_t和struct hw_device_t的内容定义在:

hardware\libhardware\include\hardware\hardware.h

所以我们的hello.h内容如下:

 
  1. #ifndef ANDROID_HELLO_INTERFACE_H

  2. #define ANDROID_HELLO_INTERFACE_H

  3. #include <hardware/hardware.h>

  4.  
  5. __BEGIN_DECLS

  6. #define HELLO_HARDWARE_MODULE_ID "hello"//ID

  7. struct hello_module_t {

  8. struct hw_module_t common;

  9. };//hw_module_t的继承者

  10. struct hello_device_t {

  11. struct hw_device_t common;

  12. int fd;

  13. int (*set_val)(struct hello_device_t* dev, int val);

  14. int (*get_val)(struct hello_device_t* dev, int* val);

  15. };//hw_device_t的继承者

  16. __END_DECLS

  17.  
  18. #endif

2、填充相关的结构体

我们总结一下硬件具体的调用流程,也是hardware层的工作流程:

1、 通过ID找到硬件模块,struct hw_module_t common的结构体的继承者;

2、 通过硬件模块找到hw_module_methods_t,打开操作,获得设备的hw_device_t;

3、 调用hw_device_t中的各种操作硬件的方法;

4、 调用完成,通过hw_device_t的close关闭设备。

HAL规定了需要创一个名为HAL_MODULE_INFO_SYM结构体,如下:

 
  1. struct hello_module_t HAL_MODULE_INFO_SYM = {

  2. .common = {

  3. .tag= HARDWARE_MODULE_TAG,

  4. //.module_api_version = FINGERPRINT_MODULE_API_VERSION_2_0,

  5. .hal_api_version= HARDWARE_HAL_API_VERSION,

  6. .id = HELLO_HARDWARE_MODULE_ID,

  7. .name = "Demo shaomingliang hello HAL",

  8. .author = "The Android Open Source Project",

  9. .methods= &hello_module_methods,

  10. },

  11. };

填充struct hw_module_t common 并对其成员 methods进行赋值:

.methods= &hello_module_methods,

hello_module_methods的定义如下:

 
  1. static struct hw_module_methods_t hello_module_methods = {

  2. .open = hello_device_open,

  3. };

在 hello_device_open 对 struct hello_device_t 进行填充。

下面是抽象层hello.c的详细代码:

 
  1. #define LOG_TAG "HelloStub"

  2. #include <hardware/hardware.h>

  3. #include <hardware/hello.h>

  4.  
  5. #include <sys/mman.h>

  6.  
  7. #include <dlfcn.h>

  8.  
  9. #include <cutils/ashmem.h>

  10. #include <cutils/log.h>

  11.  
  12. #include <fcntl.h>

  13. #include <errno.h>

  14. #include <sys/ioctl.h>

  15. #include <string.h>

  16. #include <stdlib.h>

  17.  
  18. #include <cutils/log.h>

  19. #include <cutils/atomic.h>

  20.  
  21. #define MODULE_NAME "Hello"

  22. char const * const device_name = "/dev/hello" ;

  23. static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device);

  24. static int hello_device_close(struct hw_device_t* device);

  25. static int hello_set_val(struct hello_device_t* dev, int val);

  26. static int hello_get_val(struct hello_device_t* dev, int* val);

  27.  
  28. static struct hw_module_methods_t hello_module_methods = {

  29. .open = hello_device_open,

  30. };

  31. static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device)

  32. {

  33. struct hello_device_t* dev;

  34. char name_[64];

  35. //pthread_mutex_t lock;

  36. dev = (struct hello_device_t*)malloc(sizeof(struct hello_device_t));

  37. if(!dev) {

  38. ALOGE("Hello Stub: failed to alloc space");

  39. return -EFAULT;

  40. }

  41. ALOGE("Hello Stub: hello_device_open eliot_shao");

  42. memset(dev, 0, sizeof(struct hello_device_t));

  43.  
  44. dev->common.tag = HARDWARE_DEVICE_TAG;

  45. dev->common.version = 0;

  46. dev->common.module = (hw_module_t*)module;

  47. dev->common.close = hello_device_close;

  48. dev->set_val = hello_set_val;

  49. dev->get_val = hello_get_val;

  50.  
  51. //pthread_mutex_lock(&lock);

  52. dev->fd = -1 ;

  53. snprintf(name_, 64, device_name, 0);

  54. dev->fd = open(name_, O_RDWR);

  55. if(dev->fd == -1) {

  56. ALOGE("Hello Stub:eliot failed to open %s !-- %s.", name_,strerror(errno));

  57. free(dev);

  58. return -EFAULT;

  59. }

  60. //pthread_mutex_unlock(&lock);

  61. *device = &(dev->common);

  62. ALOGI("Hello Stub: open HAL hello successfully.");

  63. return 0;

  64. }

  65.  
  66. static int hello_device_close(struct hw_device_t* device) {

  67. struct hello_device_t* hello_device = (struct hello_device_t*)device;

  68. if(hello_device) {

  69. close(hello_device->fd);

  70. free(hello_device);

  71. }

  72. return 0;

  73. }

  74. static int hello_set_val(struct hello_device_t* dev, int val) {

  75. ALOGI("Hello Stub: set value to device.");

  76. write(dev->fd, &val, sizeof(val));

  77. return 0;

  78. }

  79. static int hello_get_val(struct hello_device_t* dev, int* val) {

  80. if(!val) {

  81. ALOGE("Hello Stub: error val pointer");

  82. return -EFAULT;

  83. }

  84. read(dev->fd, val, sizeof(*val));

  85. ALOGI("Hello Stub: get value from device");

  86. return 0;

  87. }

  88.  
  89. struct hello_module_t HAL_MODULE_INFO_SYM = {

  90. .common = {

  91. .tag = HARDWARE_MODULE_TAG,

  92. //.module_api_version = FINGERPRINT_MODULE_API_VERSION_2_0,

  93. .hal_api_version = HARDWARE_HAL_API_VERSION,

  94. .id = HELLO_HARDWARE_MODULE_ID,

  95. .name = "Demo shaomingliang hello HAL",

  96. .author = "The Android Open Source Project",

  97. .methods = &hello_module_methods,

  98. },

  99. };

Android.mk 的详细代码如下:

 
  1.  
  2. LOCAL_PATH := $(call my-dir)

  3.  
  4. include $(CLEAR_VARS)

  5.  
  6. LOCAL_MODULE := hello.default

  7. LOCAL_MODULE_RELATIVE_PATH := hw

  8. LOCAL_SRC_FILES := hello.c

  9. LOCAL_SHARED_LIBRARIES := liblog

  10. LOCAL_MODULE_TAGS := optional

  11.  
  12. include $(BUILD_SHARED_LIBRARY)

  13.  

hello HAL代码的编译

将hal的代码放到如下位置:

hardware\libhardware\modules\hello\hello.c 
hardware\libhardware\modules\hello\Android.mk 
hardware\libhardware\include\hardware\hello.h

执行:mmm hardware\libhardware\modules\hello\

将会在system/lib/hw/下生成一个hello.default.so!

Android架构实例分析之注册hello HAL的JNI方法表

摘要:

Android JNI是一种技术,提供Java调用Android native代码或者native调用Java代码的一种机制,并不提供策略。本文实现了基于前面两篇文章:

Android标准架构实例分析之编写最简单的hello驱动 
http://blog.csdn.net/eliot_shao/article/details/51860229

Android架构实例分析之编写hello驱动的HAL层代码 
http://blog.csdn.net/eliot_shao/article/details/51861905

所实现的JNI案例。对HAL层提供的方法进行“翻译”,并转化成android runtime的JNI方法表,当Java代码调用时候可以正确的寻找到native的代码。

JNI原理分析

首先上一张Android JNI技术的工作原理图:

这里写图片描述

在图中主要描述Java-jni-native的一个调用流程。下面进行按步骤讲解: 
1、Java代码通过System.loadLibrary(“android_servers”); 加载JNI静态库; 
2、Android runtime自动调用JNI静态库中的JNI_OnLoad函数; 
3、JNI_OnLoad调用jniRegisterNativeMethods函数,注册多个或者一个JNINativeMethod到Android runtime的gMthods链表中; 
4、Java代码声明并调用相关的native代码; 
5、Android runtime查找gMthods链表找到前面注册的本地函数指针,然后执行。

注意:JNI和HAL的衔接是通过JNI的代码

hw_get_module(HELLO_HARDWARE_MODULE_ID, (const struct hw_module_t**)&module) == 0) 

实现的。

本文使用的hello的案例是以system_server进程上建立的,后面会创建一个hello的本地服务。下面是对system_server进程调用JNI过程进行分析。

1、frameworks\base\services\java\com\android\server\SystemServer.java

 
  1. // Initialize native services.

  2. System.loadLibrary("android_servers");

2、frameworks\base\services\core\jni\onload.cpp

 
  1. jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)

  2. register_android_server_HelloService(env);//add by eliot_shao

3、frameworks\base\services\core\jni\com_android_server_HelloService.cpp

 
  1. jniRegisterNativeMethods(env, "com/android/server/HelloService", method_table, NELEM(method_table));

  2. static const JNINativeMethod method_table[] = {

  3. {"init_native", "()Z", (void*)hello_init},

  4. {"setVal_native", "(I)V", (void*)hello_setVal},

  5. {"getVal_native", "()I", (void*)hello_getVal},

  6. };

4、frameworks\base\services\Android.mk

LOCAL_MODULE:= libandroid_servers
  • 1
  • 2

hello JNI实例

rameworks\base\services\core\jni\com_android_server_HelloService.cpp

 
  1. #define LOG_TAG "HelloService"

  2. #include "jni.h"

  3. #include "JNIHelp.h"

  4. #include "android_runtime/AndroidRuntime.h"

  5. #include <utils/misc.h>

  6. #include <utils/Log.h>

  7. #include <hardware/hardware.h>

  8. #include <hardware/hello.h>

  9. #include <stdio.h>

  10. namespace android

  11. {

  12. struct hello_device_t* hello_device = NULL;

  13. static void hello_setVal(JNIEnv* env, jobject clazz, jint value) {

  14. int val = value;

  15. ALOGI("Hello JNI: set value %d to device.", val);

  16. if(!hello_device) {

  17. ALOGI("Hello JNI: device is not open.");

  18. return;

  19. }

  20. hello_device->set_val(hello_device, val);

  21. }

  22. static jint hello_getVal(JNIEnv* env, jobject clazz) {

  23. int val = 0;

  24. if(!hello_device) {

  25. ALOGI("Hello JNI: device is not open.");

  26. return val;

  27. }

  28. hello_device->get_val(hello_device, &val);

  29.  
  30. ALOGI("Hello JNI: get value %d from device.", val);

  31.  
  32. return val;

  33. }

  34. static inline int hello_device_open(const hw_module_t* module, struct hello_device_t** device) {

  35. return module->methods->open(module, HELLO_HARDWARE_MODULE_ID, (struct hw_device_t**)device);

  36.  
  37. }

  38. static jboolean hello_init(JNIEnv* env, jclass clazz) {

  39. hello_module_t* module;

  40.  
  41. ALOGI("Hello JNI: initializing......");

  42. if(hw_get_module(HELLO_HARDWARE_MODULE_ID, (const struct hw_module_t**)&module) == 0) {

  43. ALOGI("Hello JNI: hello Stub found.");

  44. if(hello_device_open(&(module->common), &hello_device) == 0) {

  45. ALOGI("Hello JNI: hello device is open.");

  46. return 0;

  47. }

  48. ALOGE("Hello JNI: failed to open hello device.");

  49. return -1;

  50. }

  51. ALOGE("Hello JNI: failed to get hello stub module.");

  52. return -1;

  53. }

  54. static const JNINativeMethod method_table[] = {

  55. {"init_native", "()Z", (void*)hello_init},

  56. {"setVal_native", "(I)V", (void*)hello_setVal},

  57. {"getVal_native", "()I", (void*)hello_getVal},

  58. };

  59. int register_android_server_HelloService(JNIEnv *env) {

  60. return jniRegisterNativeMethods(env, "com/android/server/HelloService", method_table, NELEM(method_table));

  61. }

  62. };

frameworks\base\services\core\jni\onload.cpp

 
  1. /*

  2. * Copyright (C) 2014 MediaTek Inc.

  3. * Modification based on code covered by the mentioned copyright

  4. * and/or permission notice(s).

  5. */

  6. /*

  7. * Copyright (C) 2009 The Android Open Source Project

  8. *

  9. * Licensed under the Apache License, Version 2.0 (the "License");

  10. * you may not use this file except in compliance with the License.

  11. * You may obtain a copy of the License at

  12. *

  13. * http://www.apache.org/licenses/LICENSE-2.0

  14. *

  15. * Unless required by applicable law or agreed to in writing, software

  16. * distributed under the License is distributed on an "AS IS" BASIS,

  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  18. * See the License for the specific language governing permissions and

  19. * limitations under the License.

  20. */

  21.  
  22. #include "JNIHelp.h"

  23. #include "jni.h"

  24. #include "utils/Log.h"

  25. #include "utils/misc.h"

  26.  
  27. namespace android {

  28. int register_android_server_AlarmManagerService(JNIEnv* env);

  29. int register_android_server_AssetAtlasService(JNIEnv* env);

  30. int register_android_server_BatteryStatsService(JNIEnv* env);

  31. int register_android_server_ConsumerIrService(JNIEnv *env);

  32. int register_android_server_InputApplicationHandle(JNIEnv* env);

  33. int register_android_server_InputWindowHandle(JNIEnv* env);

  34. int register_android_server_InputManager(JNIEnv* env);

  35. int register_android_server_LightsService(JNIEnv* env);

  36. int register_android_server_PowerManagerService(JNIEnv* env);

  37. int register_android_server_SerialService(JNIEnv* env);

  38. int register_android_server_SystemServer(JNIEnv* env);

  39. int register_android_server_UsbDeviceManager(JNIEnv* env);

  40. int register_android_server_UsbMidiDevice(JNIEnv* env);

  41. int register_android_server_UsbHostManager(JNIEnv* env);

  42. int register_android_server_VibratorService(JNIEnv* env);

  43. int register_android_server_location_GpsLocationProvider(JNIEnv* env);

  44. int register_android_server_location_FlpHardwareProvider(JNIEnv* env);

  45. int register_android_server_connectivity_Vpn(JNIEnv* env);

  46. int register_android_server_hdmi_HdmiCecController(JNIEnv* env);

  47. int register_android_server_tv_TvInputHal(JNIEnv* env);

  48. int register_android_server_PersistentDataBlockService(JNIEnv* env);

  49. int register_android_server_Watchdog(JNIEnv* env);

  50. int register_com_mediatek_perfservice_PerfServiceManager(JNIEnv* env);

  51. #if defined (MTK_HDMI_SUPPORT)

  52. int register_com_mediatek_hdmi_MtkHdmiManagerService(JNIEnv* env);

  53. #endif

  54. // Mediatek AAL support

  55. int register_android_server_display_DisplayPowerController(JNIEnv* env);

  56. #ifndef MTK_BSP_PACKAGE

  57. int register_com_android_internal_app_ShutdownManager(JNIEnv *env);

  58. #endif

  59.  
  60. int register_android_server_HelloService(JNIEnv *env);//add by eliot_shao

  61. };

  62.  
  63. using namespace android;

  64.  
  65. extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)

  66. {

  67. JNIEnv* env = NULL;

  68. jint result = -1;

  69.  
  70. if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {

  71. ALOGE("GetEnv failed!");

  72. return result;

  73. }

  74. ALOG_ASSERT(env, "Could not retrieve the env!");

  75.  
  76. register_android_server_PowerManagerService(env);

  77. register_android_server_SerialService(env);

  78. register_android_server_InputApplicationHandle(env);

  79. register_android_server_InputWindowHandle(env);

  80. register_android_server_InputManager(env);

  81. register_android_server_LightsService(env);

  82. register_android_server_AlarmManagerService(env);

  83. register_android_server_UsbDeviceManager(env);

  84. register_android_server_UsbMidiDevice(env);

  85. register_android_server_UsbHostManager(env);

  86. register_android_server_VibratorService(env);

  87. register_android_server_SystemServer(env);

  88. register_android_server_location_GpsLocationProvider(env);

  89. register_android_server_location_FlpHardwareProvider(env);

  90. register_android_server_connectivity_Vpn(env);

  91. register_android_server_AssetAtlasService(env);

  92. register_android_server_ConsumerIrService(env);

  93. register_android_server_BatteryStatsService(env);

  94. register_android_server_hdmi_HdmiCecController(env);

  95. register_android_server_tv_TvInputHal(env);

  96. register_android_server_PersistentDataBlockService(env);

  97. register_android_server_Watchdog(env);

  98. register_com_mediatek_perfservice_PerfServiceManager(env);

  99. #if defined (MTK_HDMI_SUPPORT)

  100. register_com_mediatek_hdmi_MtkHdmiManagerService(env);

  101. #endif

  102. // Mediatek AAL support

  103. register_android_server_display_DisplayPowerController(env);

  104. #ifndef MTK_BSP_PACKAGE

  105. register_com_android_internal_app_ShutdownManager(env);

  106. #endif

  107. register_android_server_HelloService(env);//add by eliot_shao

  108. return JNI_VERSION_1_4;

  109. }

编译:

android 2.3 和android 6.0版本的JNI位置发生了一点变化。 
将com_android_server_HelloService.cpp放入frameworks\base\services\core\jni\ 
修改: LOGI–>ALOGI 
LOGE–>ALOGE 
修改onload.cpp 和 Android.mk 
编译:mmm frameworks/base/services/

生成system/lib/libandroid_servers.so

 

摘要:

在上一文中介绍了hello驱动的JNI方法,最终更新在android runtime中的java-native函数表。本文将介绍java的世界中如何通过调用JNI定义的java函数实现hello系统服务进程,为应用程序提供系统服务。

 

通信代理AIDL

java的世界,硬件服务一般是运行在一个独立的进程中为各种应用程序提供服务。因此,调用这些硬件服务的应用程序与这些硬件服务之间的通信需要通过代理来进行。

AIDL (Android Interface DefinitionLanguage) 是一种IDL 语言,用于生成可以在Android设备上两个进程之间进行进程间通信(interprocess communication, IPC)的代码。如果在一个进程中(例如Activity)要调用另一个进程中(例如Service)对象的操作,就可以使用AIDL生成可序列化的参数。

如何实现

 

如上图,在system_server中注册很多系统服务,这些系统服务通过servicemanager进程进行管理,如果应用程序(client)需要使用某个服务就通过servicemanager来申请使用。

基于这种思路我们需要实现hello系统服务可以这样做:

1、建立aidl通信接口;

2、在system_server中注册hello_service到servicemanager;

3、实现hello_service,对应aidl中的接口函数。

4、client向servicemanager请求hello_service,成功后,调用aidl接口函数,建立client进程和hello_service进程的通信关系。

注意:关于android L 版本以后需要添加selinux策略,否则第二步向system_server中注册hello_service到servicemanager不成功,会出现:ServiceManager add_service SELinux Permission Denied !详细解决方案请参考:

http://blog.csdn.net/eliot_shao/article/details/51770558

 

下面是具体实现流程和测试过程,主要参考罗升阳的hello代码,感谢!。

 

 

1、建立aidl通信接口;

aidl 记得在 frameworks/base/Android.mk 加上 core\java\android\os\IHelloService.aidl

建立frameworks\base\core\java\android\os\IHelloService.aidl

内容:

 
  1. package android.os;

  2.  
  3. interface IHelloService {

  4. void setVal(int val);

  5. int getVal();

  6. }

 

2、在system_server中注册hello_service到servicemanager;

修改frameworks\base\services\java\com\android\server\SystemServer.java

添加如下代码:

 
  1. //add by eliot start

  2. try {

  3.  
  4. Slog.i(TAG, "Hello Service");

  5.  
  6. ServiceManager.addService("hello", new HelloService());//

  7.  
  8. } catch (Throwable e) {

  9.  
  10. Slog.e(TAG, "Failure starting Hello Service", e);

  11.  
  12. }

  13.  
  14. //add by eliot end

 

3、实现hello_service

创建frameworks\base\services\core\java\com\android\server\HelloService.java

实现内容:

 
  1. package com.android.server;

  2. import android.content.Context;

  3. import android.os.IHelloService;

  4.  
  5.  
  6. public class HelloService extends IHelloService.Stub {

  7. private static final String TAG = "HelloService";

  8.  
  9. HelloService() {

  10. init_native();

  11. }

  12.  
  13. public void setVal(int val) {

  14. setVal_native(val);

  15. }

  16.  
  17. public int getVal() {

  18. return getVal_native();

  19. }

  20.  
  21. private static native boolean init_native();

  22. private static native void setVal_native(int val);

  23. private static native int getVal_native();

  24. };

 

4、client使用hello_service

 

在app中使用hello_service的大致流程如下:

 
  1. import android.os.IHelloService;//导入通信接口

  2. //请求hello_service

  3. private IHelloService helloService = null;

  4. helloService = IHelloService.Stub.asInterface(

  5. ServiceManager.getService("hello"));

  6.  
  7. //使用hello_service并显示

  8. int val = helloService.getVal();

  9. String text = String.valueOf(val);

  10. valueText.setText(text);


测试

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">在第三步后,就可以进行编译测试。</span>

mmm frameworks/base

打包下载到机器,使用service list查看是否有hello service:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值