app层通过aidl调用到注册在systemServer中的Service(这是framework的java部分),然后通过jni调用到cpp代码(可算是也可不算是framwork的),cpp代码加载hal模块并调用hal模块,算上hal模块一共四层app->service->native->hal,这是常见的hal层与上层交互的写法

mmm hardware/libhardware/modules/helloworld/
make snod
mmm frameworks/base/services/jni/
make snod
mmm frameworks/base/
mmm frameworks/base/services/java/      //previously this sentense is forgotten
make snod
mmm packages/apps/testhal/
make snod

make update-api (if change core file, this line should be execute before big compile)

make -j4

make adb //create single adb binary file, if there is environment it can be execute (after source build/envsetup.sh lunch 1 there will be environment maybe there are other ways)

even if ubuntu's start show error still it can build source code and do works

具体代码如下

//===========================================================================
step1
file creat:hardware/libhardware/include/hardware/helloworld.h

#define ANDROID_HELLOWORLD_INTERFACE_H
#include <hardware/hardware.h>

__BEGIN_DECLS

#define HELLOWORLD_HARDWARE_MODULE_ID "helloworld"

struct helloworld_module_t {
    struct hw_module_t common;

//not must
    char * description;
    int methodsNum;
};

struct helloworld_device_t{
    struct hw_device_t common;
    
    int (*helloworld_add)(struct helloworld_device_t * dev, int a ,int b, int *c);

int (*mychengfa)(struct helloworld_device_t * dev, int a ,int b, int *c);

};

__END_DECLS

file creat:hardware/libhardware/modules/helloworld/helloworld.c

#define LOG_TAG "HelloStub"

#include <hardware/hardware.h>
#include <fcntl.h>
#include <errno.h>
#include <cutils/log.h>
#include <hardware/helloworld.h>

#define MODULE_NAME "Helloworld"
#define MODULE_DES "Helloworld: Implement Add function asdf"
#define MODULE_AUTHOR "123456@qq.com"

static int helloworld_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device);
static int helloworld_close(struct hw_device_t* device);
static int helloworld_add(struct helloworld_device_t* dev, int a ,int b, int *c);
static int chengfa(struct helloworld_device_t* dev, int a ,int b, int *c);

static struct hw_module_methods_t helloworld_module_methods = {
    open: helloworld_open
};

//ta says: name must be HAL_MODULE_INFO_SYM,is it right?
struct helloworld_module_t HAL_MODULE_INFO_SYM = {
    common:{
    tag: HARDWARE_MODULE_TAG,
    version_major: 1,
    version_minor: 0,
    id: HELLOWORLD_HARDWARE_MODULE_ID,
    name: MODULE_NAME,
    author: MODULE_AUTHOR,
    methods: &helloworld_module_methods,
    },
    
    description: MODULE_DES,
    methodsNum: 3,
};

static int helloworld_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device){
    struct helloworld_device_t* dev;
    dev = (struct helloworld_device_t*)malloc(sizeof(struct helloworld_device_t));
    if(!dev){
        ALOGE("Helloworld open: failed to alloc device space asdf");
        return -EFAULT;
    }

memset(dev, 0, sizeof(struct helloworld_device_t));
    dev->common.tag = HARDWARE_DEVICE_TAG;
    dev->common.version = 0;
    dev->common.module = (hw_module_t*)module;
    dev->common.close = helloworld_close;
    dev->helloworld_add = helloworld_add;
    dev->mychengfa = chengfa;

*device = &(dev->common);
    ALOGI("Helloworld open: open driver file successfully asdf");

return 0;
}

static int helloworld_close(struct hw_device_t* device){
    struct helloworld_device_t* helloworld_device = (struct helloworld_device_t*)device;

if(helloworld_device){
        free(helloworld_device);
    }
    return 0;
}

static int helloworld_add(struct helloworld_device_t* dev, int a ,int b, int* c){
    ALOGI("Helloworld add: value %d + %d asdf", a ,b);
    *c = a + b;
    return 0;
}

static int chengfa(struct helloworld_device_t* dev, int a ,int b, int* c){
    ALOGI("Helloworld cheng:value %d x %d asdf", a ,b);
    *c = a * b;
    return 0;
}

file creat:hardware/libhardware/modules/helloworld/Android.mk

LOCAL_PATH := $(call my-dir)

# HAL module implemenation stored in
# hw/<OVERLAY_HARDWARE_MODULE_ID>.<ro.product.board>.so
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
LOCAL_SHARED_LIBRARIES := liblog

LOCAL_SRC_FILES :=     \
    helloworld.c
    
LOCAL_MODULE := helloworld.default
include $(BUILD_SHARED_LIBRARY)

mmm hardware/libhardware/modules/helloworld/
make snod

step2
file create:frameworks/base/services/jni/com_android_server_HelloWorldService.cpp

#define LOG_TAG "HelloWorldService"

#include "jni.h"
#include "JNIHelp.h"
#include "android_runtime/AndroidRuntime.h"
#include <utils/Log.h>
#include <utils/misc.h>
#include <hardware/hardware.h>
#include <hardware/helloworld.h>
#include <stdio.h>

namespace android{

struct helloworld_device_t* helloworld_device = NULL;

static jboolean helloworld_init(JNIEnv* env, jobject clazz){
    helloworld_module_t * module;
    //hw_get_module gu ding xie fa
    if(hw_get_module(HELLOWORLD_HARDWARE_MODULE_ID, (const struct hw_module_t **)&module) == 0){
        if(module->common.methods->open(&(module->common), HELLOWORLD_HARDWARE_MODULE_ID, (struct hw_device_t **)&helloworld_device)==0){
            return 0;        
        }
        return -1;
    }
    return -1;
}

static jint helloworld_Addval(JNIEnv* env, jobject clazz, jint a, jint b){
    int result;
    if(!helloworld_device){
        return -1;
    }
    helloworld_device->helloworld_add(helloworld_device, a, b, &result);
    return result;
}

static jint chengjiuwanle(JNIEnv* env, jobject clazz, jint a, jint b){
    int result;
    if(!helloworld_device){
        return -1;
    }
    helloworld_device->mychengfa(helloworld_device, a, b, &result);
    return result;
}

/*
 * JNI registration.
 */
static JNINativeMethod sMethods[] = {
    /* name, signature, funcPtr */
    { "init", "()Z", (void*) helloworld_init },
    { "addValue", "(II)I", (void*) helloworld_Addval },
    { "justChengfa", "(II)I", (void*) chengjiuwanle },
};

int register_android_server_HelloWorldService(JNIEnv* env)
{
    return jniRegisterNativeMethods(env, "com/android/server/HelloWorldService",
            sMethods, NELEM(sMethods));
}

};

file modify:frameworks/base/services/jni/Android.mk

com_android_server_HelloWorldService.cpp \

file modify:frameworks/base/services/jni/onload.cpp

int register_android_server_HelloWorldService(JNIEnv* env);

register_android_server_HelloWorldService(env);

mmm frameworks/base/services/jni/
make snod

step3
file create:frameworks/base/core/java/android/os/IHelloWorldService.aidl

package android.os;

interface IHelloWorldService
{
    int addVal(int a, int b);
    int chengcheng(int a, int b);
}

file create:frameworks/base/services/java/com/android/server/HelloWorldService.java

package com.android.server;

import android.content.Context;
import android.os.IHelloWorldService;
import android.util.Slog;

public class HelloWorldService extends IHelloWorldService.Stub{
    HelloWorldService(){
        init();
        Slog.v("HelloWorldService","HelloWorldService init asdf result=" + addVal(333,666));
    }

public int addVal(int a, int b){
        Slog.v("HelloWorldService","HelloWorldService addVal asdf" + a + "+" + b);
        return addValue(a,b);
    }

public int chengcheng(int a, int b){
        Slog.v("HelloWorldService","HelloWorldService chengcheng asdf" + a + "x" + b);
        return justChengfa(a,b);
    }

private static native boolean init();
    private static native int addValue(int a, int b);
    private static native int justChengfa(int a, int b);
}

file modify:frameworks/base/Android.mk

core/java/android/os/IHelloWorldService.aidl \

file modify:frameworks/base/services/java/com/android/server/SystemServer.java

try {
                Slog.i(TAG, "Notification Manager");
                notification = new NotificationManagerService(context, statusBar, lights);
                ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);
                networkPolicy.bindNotificationManager(notification);
            } catch (Throwable e) {
                reportWtf("starting Notification Manager", e);
            }
//============================
            try {
                Slog.i(TAG, "Helloworld Service");
                ServiceManager.addService("helloworld",
                        new HelloWorldService());
            } catch (Throwable e) {
                reportWtf("starting HelloWorld service", e);
            }
//============================

try {
                Slog.i(TAG, "Device Storage Monitor");
                ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
                        new DeviceStorageMonitorService(context));
            } catch (Throwable e) {
                reportWtf("starting DeviceStorageMonitor service", e);
            }

mmm frameworks/base/
mmm frameworks/base/services/java/      //previously this sentense is forgotten
make snod

step4
packages/apps/testhal/Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES := $(call all-java-files-under, src)

#LOCAL_SDK_VERSION := current
#LOCAL_PRIVATE_PLATFORM_APIS := true

LOCAL_PACKAGE_NAME := testhal
LOCAL_CERTIFICATE := platform

include $(BUILD_PACKAGE)

# Use the folloing include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))

packages/apps/testhal/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.testhal">

<original-package android:name="com.testhal" />

<application android:label="@string/app_name" android:icon="@mipmap/ic_launcher_calculator">
        <activity android:name="MainActivity" 
                  android:windowSoftInputMode="stateAlwaysHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

packages/apps/testhal/src/com/testhal/MainActivity.java

package com.testhal;

import android.app.Activity;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import android.os.IBinder;
import android.os.Build;
import android.os.IHelloWorldService;
import android.os.ServiceManager;

public class MainActivity extends Activity  {
    TextView tv;
    Button btn;
    Button btn2;
    int inta = 5;
    int intb = 7;

@Override
    public void onCreate(Bundle state) {
        super.onCreate(state);
        setContentView(R.layout.main);
    tv = (TextView)findViewById(R.id.tv);      
    btn = (Button)findViewById(R.id.btn);      
    btn2 = (Button)findViewById(R.id.btn2);  
    
    btn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
        Toast.makeText(MainActivity.this, "click", Toast.LENGTH_SHORT).show();

try{
            IBinder b = ServiceManager.getService("helloworld");
            IHelloWorldService service = IHelloWorldService.Stub.asInterface(b);
            int result = service.addVal(inta,intb);
            tv.setText("result:"+result);
        }catch(Exception e){

}

}});
    btn2.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
        Toast.makeText(MainActivity.this, "click2", Toast.LENGTH_SHORT).show();

try{
            IBinder b = ServiceManager.getService("helloworld");
            IHelloWorldService service = IHelloWorldService.Stub.asInterface(b);
            int result = service.chengcheng(inta,intb);
            tv.setText("result:"+result);
        }catch(Exception e){

}

}});    
    
    }
}

packages/apps/testhal/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              >

<TextView
        android:id="@+id/tv"
        android:text="@string/asdf"
        android:layout_width="match_parent"
              android:layout_height="wrap_content"
    />

<Button
        android:id="@+id/btn"
        android:text="@string/btn1"
        android:layout_width="match_parent"
              android:layout_height="wrap_content"
    />

<Button
        android:id="@+id/btn2"
        android:text="@string/btn2"
        android:layout_width="match_parent"
              android:layout_height="wrap_content"
    />
    
</LinearLayout>

and a pic file and a strings.xml

mmm packages/apps/testhal/
make snod

//==============================================================
big compile need to modify files(maybe)
hardware/libhardware/modules/Android.mk

hardware_modules := gralloc hwcomposer audio nfc nfc-nci local_time \
    power usbaudio audio_remote_submix camera consumerir helloworld

build/target/product/sdk.mk

LegacyCamera \
    testhal

end

kitkat(KRT16S)从hal层到app层写法相关推荐

  1. aidl android 怎么mk编译,使用Android的HIDL+AIDL方式编写从HAL层到APP层的程序

    先实现HIDL,打通从HAL到framework层 可以把自己的HIDL模块建立在hardware/interfaces/.frameworks/hardware/interfaces/. syste ...

  2. android系统底层驱动多个物理按键上报同一个键值给app层,app层如何区分

    如果设备有多个按键上报同一个键值给app层,app通过getScanCode()可以区分是哪个物理按键,得到的值就是linux驱动层的扫描码

  3. android l camera no panorama,Android Camera从App层到framework层到HAL层的初始化过程

    Android camera 从上到下能够分为四个部分: Application层. framework层. HAL(hardware abstract layer)层. Kernel层 通常面向开发 ...

  4. Android Hal层回调APP应用接口

    前面文章讲述了Android APP怎么调用hal层的方法,和Android系统自带的灯光子系统类似:本文将讲述hal层如何回调APP应用接口,和android系统振动传感器数据上报类似,同时本文也会 ...

  5. java 通信层_Android native进程间通信实例-binder篇之——HAL层访问JAVA层的服务

    有一天在群里聊天的时候,有人提出一个问题,怎样才能做到HAL层访问JAVA层的接口?刚好我不会,所以做了一点研究. 之前的文章末尾部分说过了service call 可以用来调试系统的binder服务 ...

  6. Android App层通过JNI从驱动获取Input Event

    1 概述 尝试在App层直接读取驱动的Input Event,获取触屏事件(本文获取的是电磁笔触屏事件),而不通过Android的Input Framework. 2 架构 3 实现 3.1 JNI层 ...

  7. android源生Browser分析---APP层基本架构

    App层的功能主要分几块: 使用WebView的浏览器页面主体 除WebView之外的UI 页面的相关功能,如页内查找,前进,后退 设置 事件 多窗口管理 书签/历史记录 首先看构成主体框架的几个类 ...

  8. android源生Browser分析(二)---APP层基本架构

    App层的功能主要分几块: 使用WebView的浏览器页面主体 除WebView之外的UI 页面的相关功能,如页内查找,前进,后退 设置 事件 多窗口管理 书签/历史记录 首先看构成主体框架的几个类 ...

  9. 论HAL层、FDL层、FTL层的参数传递

    HAL层.FDL层.FTL层的共用参数传递方式: 共用头文件 添加开发环境配置选项卡的宏来做控制,并各层公共定义这个宏 共用RAM区存共用参数

  10. 是男人就下100层【第一层】——高仿微信界面(4)

    上一篇<是男人就下100层[第一层]--高仿微信界面(3)>中我们完成了登录,这一篇看完成登录后的一个短暂加载和引导界面. 加载界面: <RelativeLayout xmlns:a ...

最新文章

  1. MongoDB学习笔记(一:常见问题汇总)
  2. windows10系统下MongoDB的安装及环境配置
  3. Python和java二选一该学啥
  4. java同步锁实例_Java同步锁全息详解
  5. 使用作业自动清理数据库日志文件
  6. 打开并读取npy文件,查看文件内容
  7. cas:1092775-62-6 ; (ir[dfcf3ppy]2(bpy))pf6热延迟荧光材料TADF
  8. 安装vue环境,并新建Vue项目
  9. 使用VNC连接树莓派4b如何全屏1080p分辨率,一次更改永久有效!
  10. 黑盒测试、白盒测试与灰盒测试方法
  11. Javascript数组部分
  12. 计算机专业本科考教资可以考哪些,高中教师资格证计算机专业考什么内容
  13. Elsevier期刊模板2(官方要求+文章结构)
  14. 聚乙烯原料最新价格 聚丙烯价格多少钱一吨?
  15. 基于联邦学习的多源异构数据融合算法
  16. Deep learning—Yann LeCun, Yoshua Bengio Geoffrey Hintonxi
  17. wordpress最佳架构_应用程序的21个最佳WordPress主题(2020)
  18. 初中计算机认识ppt课程教案,初中信息技术《PPT模板的制作》教案
  19. excel数据汇总统计表格全部数据
  20. [k8s]gosu使用

热门文章

  1. 用计算机打开软件,电脑上如何打开软件?
  2. ascii码表的使用
  3. 短视频聚合直播app源码/软件系统开发方案
  4. AIX 系统默认ftp
  5. 小程序毕设作品之微信小程序点餐系统毕业设计(6)开题答辩PPT
  6. 【知识科普】嵌入式软件开发是什么?
  7. idea在mac版怎么配置svn_Mac安装svn客户端
  8. ecshop index.php,]ECSHOP 源码分析(install/index.php1)
  9. wordpress博客加载缓慢解决:去除Open Sans和Lato 字体
  10. Fiddler 抓包工具总结