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));
}

};

posted @ 2019-01-09 10:08  请给我倒杯茶  阅读(331)  评论(0编辑  收藏  举报