2.Android硬件访问服务编写系统代码【转】
本文转载自:https://blog.csdn.net/qq_33443989/article/details/76696772
版权声明:本文为博主(Tower)自学笔记,欢迎转载! :-) https://blog.csdn.net/qq_33443989/article/details/76696772
1>. 实现用硬件访问服务硬件的代码-没有HAL层
1<. AIDL: Android Interface Definition Language,即Android接口定义语言
AIDL_百度解释
2<. 编写代码
1<. ILedService.aidl
1<. 路径: android_system_code/frameworks/base/core/java/android/os/
2<. 作用: 生成我们所需要的接口文件
3<. 实现
1<. 代码内容
2<. 上传 :Android_Soruce/franeworks/base/core/java/android/os
3<. 修改 :Android_Soruce/franeworks/base/Android.mk文件
+ core/java/android/os/ILedService.aidl
[前期准备]:在Android源码目录
. setenv
lunch
mmm .[dir]
4<.[出现问题]:
make: Entering directory /work/android-5.0.2'
Aidl: framework <= frameworks/base/./core/java/android/os/ILedService.aidl
unable to open out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/os/ILedService.java for write
make: *** [out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/os/ILedService.java] Error 1
make: Leaving directory/work/android-5.0.2’
[解决方法]:
su : 改为root用户
5<. 最终得到了 ILedService.java
1<.路径: android_system_code\out\target\common\obj\JAVA_LIBRARIES\
framework_intermediates\src\core\java\android\os
2<. 解析IXxxxxService.java
1<. 会得到一个Stub抽象类和一个Proxy静态内部类
2<. asInterface() : 转换函数一枚
3<. ledCtrl() : 已经实现了binder进程通信的内部函数,并且可以覆写
3<. 如何使用IXxxxxService.java
1<. 初始化
ILedService iLedService;
iLedService = iLedService.stub.asInterface(ServiceManager.getService(“led”));
2<. 后期使用
iLedService.ledCtrl():直接使用,这样使用并不会直接操作我们的HAL层,而是发给相应的ledService进程,使其处理
2<. LedService.java
1<. 路径: android_system_code\frameworks\base\services\core\java\com\android\server\
2<. 作用: 用于向系统注册该服务,提供了APP访问硬件的桥梁
3<. 实现:
1<. 定义类 : 继承于aidl文件接口.抽象类Stub
public class LedService extends ILedService.Stub
2<. 实现aidl接口定义的函数
public int ledCtrl(int which, int status) throws android.os.RemoteException
3<. 实现构造方法:例如打开设备 native_ledOpen();
/* 以下是修改其他文件,使其注册LedService.java */
4<. systemServer.java
1<. 路径: android_system_code\frameworks\base\services\java\com\android\server\
SystemServer.java
2<. startOtherServices()
1.添加 LedService Led = null;
2.添加下面代码
/* 2017.08.04 Tower Modify*/
Slog.i(TAG, “Led Service”);
Led = new LedService();
ServiceManager.addService(“Led”, Led);
/* Modift End */
3<. 实现com_android_server_LedService.cpp
1<. 路径: android_system_code\frameworks\base\services\core\jni\
2<. 作用: 属于JNI层,注册本地方法,供LedService.java使用
3<. 实现: 同其他的JNI一样, 只是多了一个注册函数,给onload.cpp使用
1<. int register_android_server_LedService(JNIEnv *env)
/* 以下是修改其他文件,使其注册LedService.java */
4<. onload.cpp
1<. 路径: android_system_code\frameworks\base\services\core\jni\onload.cpp
2<. JNI_OnLoad()
register_android_server_LedService(env);
3<. 编译:
1<. 自己写的文件:
File_name Full_Dir
com_android_server_LedService.cpp android_system_code/frameworks/base/services/core/jni/
LedService.java android_system_code/frameworks/base/services/core/java/com/android/server/
ILedService.aidl android_system_code/frameworks/base/core/java/android/os/
ILedService.java android_system_code/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/os/
2<. 修改的文件
File_name Full_Dir
onload.cpp android_system_code/frameworks/base/services/core/jni/
systemServer.java android_system_code/frameworks/base/services/java/com/android/server/
3<. 同时修改对应目录下的Android.mk
1<. 编译 android_system_code/frameworks/base/services
mmm.
[为什么]:因为android_system_code/frameworks/base/services/Android.mk已经包含
1<. include (wildcard(wildcard(LOCAL_PATH)/*/jni/Android.mk)
2<. include (patsubst(patsubst(LOCAL_PATH)/%/Android.mk,$(services))
[所以]:
1<. com_android_server_LedService.cpp
android_system_code/frameworks/base/services/core/jni/Android.mk 需要修改
2<. LedService.java
android_system_code/frameworks/base/services/core/Android.mk 不需要修改
[补充]:android_system_code/frameworks/base/services/android.mk里面
LOCAL_MODULE:= libandroid_servers : 这个库指的就是onload.cpp
4<. make snod : 会在out目录下创建image
5<. ./gen-img.sh : 可以直接在源码目录下得到image
4<. 烧写内核:
示例代码
1<. LedService.java –APP层
package com.android.server;
import android.os.ILedService;
//import android.os.Vibrator;
/*
import android.os.IBinder;
*/
public class LedService extends ILedService.Stub {
private static final String TAG = "LedService";
/* call native c function to access hardware */
public int ledCtrl(int which, int status) throws android.os.RemoteException {
return native_ledCtrl(which, status);
}
public LedService() {
native_ledOpen();
}
/* declaration native method*/
public static native int native_ledCtrl(int which, int status);
public static native int native_ledOpen();
public static native void native_ledClose();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2<. SystemServer.java –SYS层
package com.android.server;
...
import android.app.ActivityManagerNative;
import android.app.ActivityThread;
import android.app.IAlarmManager;
import android.app.INotificationManager;
import android.app.usage.UsageStatsManagerInternal;
import android.bluetooth.BluetoothAdapter;
...
public final class SystemServer {
...
private void startOtherServices() {
VibratorService vibrator = null;
LedService leds = null;
IAlarmManager alarm = null;
MountService mountService = null;
NetworkManagementService networkManagement = null;
NetworkStatsService networkStats = null;
NetworkPolicyManagerService networkPolicy = null;
ConnectivityService connectivity = null;
...
try {
Slog.i(TAG, "Reading configuration...");
SystemConfig.getInstance();
try {
...
Slog.i(TAG, "Vibrator Service");
vibrator = new VibratorService(context);
ServiceManager.addService("vibrator", vibrator);
/* 2017.08.04 Tower Modify*/
Slog.i(TAG, "Led Service");
leds = new LedService();
ServiceManager.addService("leds", leds);
/* Modift End */
}
}
...
}
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
3<. onload.cpp –JNI层
namespace android {
int register_android_server_location_GpsLocationProvider(JNIEnv* env);
int register_android_server_location_FlpHardwareProvider(JNIEnv* env);
int register_android_server_connectivity_Vpn(JNIEnv* env);
int register_android_server_hdmi_HdmiCecController(JNIEnv* env);
int register_android_server_tv_TvInputHal(JNIEnv* env);
int register_android_server_PersistentDataBlockService(JNIEnv* env);
int register_android_server_fingerprint_FingerprintService(JNIEnv* env);
int register_android_server_Watchdog(JNIEnv* env);
int register_android_server_LedService(JNIEnv *env);
...
}
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv* env = NULL;
jint result = -1;
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
ALOGE("GetEnv failed!");
return result;
}
register_android_server_LightsService(env);
register_android_server_AlarmManagerService(env);
register_android_server_UsbDeviceManager(env);
register_android_server_UsbHostManager(env);
register_android_server_VibratorService(env);
register_android_server_SystemServer(env);
register_android_server_location_GpsLocationProvider(env);
register_android_server_location_FlpHardwareProvider(env);
register_android_server_connectivity_Vpn(env);
...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
4<. ILedService.aidl –JNI层
package android.os;
/** {@hide} */
interface ILedService
{
int ledCtrl(int which, int status);
}
1
2
3
4
5
6
7
5<. com_android_server_LedService.cpp –JNI层
/*
* Copyright (C) 2017 The Android Open Source Project
* Author : Fogost路Tower
* Time : 2017.08.04
*/
#define LOG_TAG "LedService"
#include "jni.h"
#include "JNIHelp.h"
#include <utils/misc.h>
#include <utils/Log.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
namespace android
{
static jint g_iFd;
jint ledOpen(JNIEnv *env, jobject cls)
{
g_iFd = open("/dev/leds", O_RDWR);
ALOGI("native ledopen.. %d", g_iFd);
if (g_iFd >= 0) {
return 0;
}else {
return -1;
}
}
void ledClose(JNIEnv *env, jobject cls)
{
ALOGI("native ledClose..");
close(g_iFd);
}
jint ledCtrl(JNIEnv *env, jobject cls, jint which, jint status)
{
int ret = ioctl(g_iFd, status, which);
ALOGI("native ledCtrl.. which = %d status = %d ret = %d: ", which, status, ret);
return ret;
}
static const JNINativeMethod method_table[] = {
{ "native_ledOpen" , "()I" , (void*)ledOpen},
{ "native_ledClose", "()V" , (void*)ledClose},
{ "native_ledCtrl" , "(II)I", (void*)ledCtrl},
};
int register_android_server_LedService(JNIEnv *env)
{
return jniRegisterNativeMethods(env, "com/android/server/LedService",
method_table, NELEM(method_table));
}
};
2.Android硬件访问服务编写系统代码【转】的更多相关文章
- 6.2、Android硬件访问服务编写系统代码
1.实现接口文件给App使用,接口文件是应用程序查询获得服务时获得 使用AIDL(Android接口定义语言)来实现ILedService.java接口 定义ILedService.aidl inte ...
- 6.4 Android硬件访问服务编写HAL代码
JNI向上提供本地函数,向下加载HAL文件,并调用HAL的函数: HAL负责访问驱动程序执行硬件操作 JNI和HAL都是用c语言或者C++语言编写的,JNI加载HAL的实质就是使用dlopen加载动态 ...
- 6.1、Android硬件访问服务之框架
1.通过前面led点亮的例子,其流程如下 Android app(java)(通过loadLibrary)——>C library(C库做如下事情)——>1.JNI_Onload 2.jn ...
- 6.3 Android硬件访问服务APP代码
以下步骤是操作MainActivity类 1.导入包 import android.os.ILedService 2.添加成员变量 private ILedService iLedService = ...
- 6.5 Android硬件访问服务使用反射
1.前面的例子中App为了能够范问ILedService接口,把classes.jar导入到应用程序中,但是我们不想把classes编进apk包里面去,这样导致我们的apk程序会很大(解压缩apk会发 ...
- Android硬件访问服务中的HAL-查看打印的信息
JNI 向上提供本地函数,向下加载HAL文件并调用HAL的函数 HAL 负责访问驱动程序执行硬件操作. external\chromium_org\third_party\hwcplus\src\h ...
- 硬件访问服务学习笔记_WDS
1.Android驱动框架App1 App2 App3 App4-------------------硬件访问服务-------------------JNI-------------------C库 ...
- LED硬件访问服务(2)——JNI/HAL
一.系统编程 1.SystemServer.java类中提供了main()方法,说明它是以一个进程的方式存在的,启动后直接执行其run() 2.注册服务ServiceManager.addServic ...
- 在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务(老罗学习笔记5)
在数字科技日新月异的今天,软件和硬件的完美结合,造就了智能移动设备的流行.今天大家对iOS和Android系统的趋之若鹜,一定程度上是由于这两个系统上有着丰富多彩的各种应用软件.因此,软件和硬件的关系 ...
随机推荐
- python windows 安装sklearn
- hbase-java-api001
package api; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfig ...
- [17]Windows的启动过程
一.内核的引导 在intel x86系统上,windows操作系统获得控制首先从硬盘的主引导记录(MBR,Master Boot Record)开始,windows setup程序在安装windows ...
- 《大话设计模式》c++实现 工厂模式
工厂模式 工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 在工厂模式中,我们在创建对象时不会对客户端 ...
- Object-C-自定义类型归档
对自定义类型的对象进行本地化保存,那么该类型必须实现NSCoding协议! NSCoding 协议中只有两个方法,都是require的方法,一个是把本身的类型进行编码,一个是解码成类对象,返回一个对象 ...
- SpringMVC探究-----从HelloWorld开始
1.SpringMVC简介 Spring MVC框架是有一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离. 它的设计是围绕Dispatch ...
- 20155228 获取技能的成功经验和关于C语言学习的调查
内容提要 你有什么技能比大多人(超过90%以上)更好?针对这个技能的获取你有什么成功的经验?与老师博客中的学习经验有什么共通之处? 有关C语言学习的调查 你是怎么学习C语言的?(作业,实验,教材,其他 ...
- python 使用json.dumps() 的indent 参数,获得漂亮的格式化字符串后输出
想获得漂亮的格式化字符串后输出,可以使用json.dumps() 的indent 参数.它会使得输出和pprint() 函数效果类似 >>> data {'age': 4, 'nam ...
- Oracle 数据库实现数据合并:merge
1.使用update进行数据更新 1)最简单的更新 update tablea a set a.price=1.00 2)带条件的数据更新 update tablea a set a.price = ...
- 前端框架VUE----webpack打包工具的使用
在这里我仅仅的是对webpack做个讲解,webpack这个工具非常强大,解决了我们前端很繁琐的一些工具流程繁琐的事情.如果感兴趣的同学,还是看官网吧. 中文链接地址:https://www.webp ...