在A20上演示老罗的Android硬件抽象层(HAL)概要介绍和学习计划4--关于JNI部分

来源:互联网 发布:mac怎么设置qq邮箱 编辑:程序博客网 时间:2024/05/16 15:10

在frameworks/base/services/jni

添加:

com_android_server_HelloService.cpp

内容如下:

/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#define LOG_TAG "HelloService"


#include "jni.h"
#include "JNIHelp.h"
#include "android_runtime/AndroidRuntime.h"


#include <utils/misc.h>
#include <utils/Log.h>
#include <hardware/hardware.h>
#include <hardware/hello.h>


#include <stdio.h>


namespace android
{


    struct hello_device_t* hello_device =NULL;
    static void hello_setVal(JNIEnv* evn, jobject clazz, jint value)
    {
        int val = value;
            ALOGI("Hello JNI: set value %d to device.", val);
            if(!hello_device) {  
                ALOGI("Hello JNI: device is not open.");  
                return;  
            }  
             hello_device->set_val(hello_device, val);  
    }


    static jint hello_getVal(JNIEnv* evn, jobject clazz)
    {
        int val = 0;
            if(!hello_device) {  
                ALOGI("Hello JNI: device is not open.");  
                return val;  
            }  
             hello_device->get_val(hello_device, &val);  


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


             return val;
    }
    /*通过硬件抽象层定义的硬件模块打开接口打开硬件设备*/  
    static inline int hello_device_open(const hw_module_t* module, struct hello_device_t** device) {  
        return module->methods->open(module, HELLO_HARDWARE_MODULE_ID, (struct hw_device_t**)device);  
    }  




    /*通过硬件模块ID来加载指定的硬件抽象层模块并打开硬件*/  
    static jboolean hello_init(JNIEnv* env, jclass clazz) {  
        hello_module_t* module;  


        ALOGI("Hello JNI: initializing......");  
        if(hw_get_module(HELLO_HARDWARE_MODULE_ID, (const struct hw_module_t**)&module) == 0) {  


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


            if(hello_device_open(&(module->common), &hello_device) == 0) {  
                ALOGI("Hello JNI: hello device is open.");  
                return 0;  
            }  
            ALOGE("Hello JNI: failed to open hello device.");  
            return -1;  
        }  
        ALOGE("Hello JNI: failed to get hello stub module.");  
        return -1;        
    }  


static JNINativeMethod method_table[] = {
    { "init_native", "()Z", (void*)hello_init },
    { "setVal_native", "(I)V", (void*)hello_setVal },
    { "getVal_native", "()I", (void*)hello_getVal },
};


int register_android_server_HelloService(JNIEnv *env)
{
    return jniRegisterNativeMethods(env, "com/android/server/HelloService",
            method_table, NELEM(method_table));
}


};







然后 在Android.mk:

diff --git a/services/jni/Android.mk b/services/jni/Android.mk
index 2c72f32..3c61a33 100755
--- a/services/jni/Android.mk
+++ b/services/jni/Android.mk
@@ -18,6 +18,7 @@ LOCAL_SRC_FILES:= \
     com_android_server_connectivity_Vpn.cpp \
     com_android_server_DisplayManagerServiceAw.cpp \
        com_android_server_BID.cpp \
+       com_android_server_HelloService.cpp  \
     onload.cpp
 
 LOCAL_C_INCLUDES += \


然后是onload.cpp:

index a4f99ca..53e0de6 100755
--- a/services/jni/onload.cpp
+++ b/services/jni/onload.cpp
@@ -36,6 +36,7 @@ int register_android_server_location_GpsLocationProvider(JNIEnv* env);
 int register_android_server_connectivity_Vpn(JNIEnv* env);
 int register_android_server_DisplayManagerServiceAw(JNIEnv *env);
 int register_android_server_BID(JNIEnv* env);
+int register_android_server_HelloService(JNIEnv *env);
 };
 
 using namespace android;
@@ -67,6 +68,7 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
     register_android_server_connectivity_Vpn(env);
        register_android_server_DisplayManagerServiceAw(env);
     register_android_server_BID(env);
+    register_android_server_HelloService(env);
        
     return JNI_VERSION_1_4;
 }


再就是:

frameworks/base/services/java/com/android/server添加HelloService.java:





package com.android.server;  
import android.content.Context;  
import android.os.IHelloService;  
import android.util.Slog;  
public class HelloService extends IHelloService.Stub {  
    private static final String TAG = "HelloService";  
    HelloService() {  
        init_native();  
    }  
    public void setVal(int val) {  
        setVal_native(val);  
    }     
    public int getVal() {  
        return getVal_native();  
    }  


    private static native boolean init_native();  
    private static native void setVal_native(int val);  
    private static native int getVal_native();  
};  


再修改SystemServer.java:

index a743c44..1dded42 100755
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -734,6 +734,14 @@ class ServerThread extends Thread {
             } catch (Throwable e) {
                 reportWtf("starting DiskStats Service", e);
             }
+            
+            try {
+                Slog.i(TAG, "HelloService");
+                ServiceManager.addService("hello", new HelloService());
+            } catch (Throwable e) {
+                Slog.e(TAG, "Failure starting Hello Service", e);
+            }
+
 
             try {
                 // need to add this service even if SamplingProfilerIntegration.isEnabled()


然后是在frameworks/base/core/java/android/os增加IHelloService:

package android.os;  
   
interface IHelloService {  
    void setVal(int val);  
    int getVal();  
}  


再就是在frameworks/base修改Android.mk:

diff --git a/Android.mk b/Android.mk
index bef18d1..cad9ad5 100755
--- a/Android.mk
+++ b/Android.mk
@@ -145,6 +145,7 @@ LOCAL_SRC_FILES += \
        core/java/android/os/IUpdateLock.aidl \
         core/java/android/os/IUserManager.aidl \
        core/java/android/os/IVibratorService.aidl \
+       core/java/android/os/IHelloService.aidl \
        core/java/android/service/dreams/IDreamManager.aidl \
        core/java/android/service/dreams/IDreamService.aidl \
        core/java/android/service/wallpaper/IWallpaperConnection.aidl \


其实 老罗在增加了类后一定要去修改api/current.txt,当然你update也是可以的,做过frameworks的人一般都会自己手动添加:

diff --git a/api/current.txt b/api/current.txt
index 7e99a58..9c820ae 100755
--- a/api/current.txt
+++ b/api/current.txt
@@ -16372,6 +16372,18 @@ package android.os {
     method public abstract void binderDied();
   }
 
+  public abstract interface IHelloService implements android.os.IInterface {
+    method public abstract int getVal() throws android.os.RemoteException;
+    method public abstract void setVal(int) throws android.os.RemoteException;
+  }
+
+  public static abstract class IHelloService.Stub extends android.os.Binder implements android.os.IHelloService {
+    ctor public IHelloService.Stub();
+    method public android.os.IBinder asBinder();
+    method public static android.os.IHelloService asInterface(android.os.IBinder);
+    method public boolean onTransact(int, android.os.Parcel, android.os.Parcel, int) throws android.os.RemoteException;
+  }
+
   public abstract interface IInterface {
     method public abstract android.os.IBinder asBinder();
   }


最后麻烦去/device/softwinner/common/hardware/hello把里面的hello.h复制到/hardware/libhardware/include/hardware吧。



原创粉丝点击