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

在Android系统中,实现并集成HelloService,包括文件修改、JNI接口定义、类实现及系统服务注册。

在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吧。



内容概要:本文介绍了一个基于冠豪猪优化算法(CPO)的无人机三维路径规划项目,利用Python实现了在复杂三维环境中为无人机规划安全、高效、低能耗飞行路径的完整解决方案。项目涵盖空间环境建模、无人机动力学约束、路径编码、多目标代价函数设计以及CPO算法的核心实现。通过体素网格建模、动态障碍物处理、路径平滑技术多约束融合机制,系统能够在高维、密集障碍环境下快速搜索出满足飞行可行性、安全性与能效最优的路径,并支持在线重规划以适应动态环境变化。文中还提供了关键模块的代码示例,包括环境建模、路径评估CPO优化流程。; 适合人群:具备一定Python编程基础优化算法基础知识,从事无人机、智能机器人、路径规划或智能优化算法研究的相关科研人员与工程技术人员,尤其适合研究生及有一定工作经验的研发工程师。; 使用场景及目标:①应用于复杂三维环境下的无人机自主导航与避障;②研究智能优化算法(如CPO)在路径规划中的实际部署与性能优化;③实现多目标(路径最短、能耗最低、安全性最高)耦合条件下的工程化路径求解;④构建可扩展的智能无人系统决策框架。; 阅读建议:建议结合文中模型架构与代码示例进行实践运行,重点关注目标函数设计、CPO算法改进策略与约束处理机制,宜在仿真环境中测试不同场景以深入理解算法行为与系统鲁棒性。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值