• 好记性不如烂笔头------按键灯jni使用(上层)


    上层使用jni(从java层到framework到JNI的用法过程)

    MTK平台如果在硬件上增加了按键灯,framework层想要调用的话,这里简单介绍一下我的使用过程。

    首先,需要增加KeyLed.c和头文件KeyLed.h两个接口函数:

    (hardwarelibhardware_legacykey_ledskey_leds.c)

    ( hardwarelibhardware_legacyincludehardware_legacyKeyLed.h)

    接下来需要一步步向上写接口了:

    1,给灯增加权限:

    进入到system/core/rootdir目录,里面有一个名为ueventd.rc文件,往里面添加一行:
          /sys/devices/platform/key_leds/red1_leds_onoff 0666 root root
         /sys/devices/platform/key_leds/red2_leds_onoff 0666 root root
         /sys/devices/platform/key_leds/red3_leds_onoff 0666 root root

    2,进入到frameworks/base/services/jni目录,新建com_android_server_KeyLedsService.cpp文件: 

    在com_android_server_KeyLedsService.cpp文件中,实现JNI方法。注意文件的命令方法,com_android_server前缀表示的是包名,表示硬件服务HelloService是放在frameworks/base/services/java目录下的com/android/server目录的,即存在一个命令为com.android.server.KeyLedsService的类。简单地说,KeyLedsService是一个提供Java接口的硬件访问服务类。

    //首先是包含相应的头文件:

    //在硬件抽象层中定义的硬件访问结构体,

    //JNI方法表

    //注册JNI方法

    3, 修改同目录下的onload.cpp文件,首先在namespace android增加register_android_server_KeyLedsService函数声明:

          namespace android {

          ..............................................................................................

          int register_android_server_KeyLedsService(JNIEnv *env);

          };

          在JNI_onLoad增加register_android_server_KeyLedsService函数调用:
          extern "C" jint JNI_onLoad(JavaVM* vm, void* reserved)
          {
           .................................................................................................
           register_android_server_KeyLedsService(env);
           .................................................................................................
          }
          这样,在Android系统初始化时,就会自动加载该JNI方法调用表。

    4, 修改同目录下的Android.mk文件,在LOCAL_SRC_FILES变量中增加一行:

          LOCAL_SRC_FILES:=
          .............................................................
          com_android_server_KeyLedsService.cpp
          onload.cpp

    此时编译代码,重新打包的system.img镜像文件就包含我们刚才编写的JNI方法了,也就是我们可以通过Android系统的Application Frameworks层提供的硬件服务KeyLedsService来调用这些JNI方法,进而调用低层的硬件抽象层接口去访问硬件了。

    数字科技日新月异的今天,软件和硬件的完美结合,造就了智能移动设备的流行。今天大家对iOS和Android系统的趋之若鹜,一定程度上是由于这两个系统上有着丰富多彩的各种应用软件。因此,软件和硬件的关系,在一定程度上可以说,硬件是为软件服务的。硬件工程师研发出一款硬件设备,自然少不了软件工程师为其编写驱动程序;而驱动程序的最终目的,是为了使得最上层的应用程序能够使用这些硬件提供的服务来为用户提供软件功能。对Android系统上的应用软件来说,就是要在系统的Application Frameworks层为其提供硬件服务。Linux内核层、硬件抽象层和运行时库层提供的自定义硬件服务接口,这些接口都是通过C或者C++语言来实现的。接下来,我们将学习如何在Android系统的Application Frameworks层提供Java接口的硬件服务。

    4.2,4.4,5.1等不同版本文件路径稍有不同,这里使用的是4.4版本

    1,增加IKeyLedsManager.aidl,(framework/base/core/java/android/app/IKeyLedsManager.aidl)

     1 package android.app;
     2 
     3 interface IKeyLedsManager
     4 {
     5     void red1_leds_exists();
     6     void red2_leds_exists();
     7     void red3_leds_exists();
     8     void blue_leds_exists();
     9     void green_leds_exists();
    10     void red1_leds_on();
    11     void red1_leds_off();
    12     void red2_leds_on();
    13     void red2_leds_off();
    14     void red3_leds_on();
    15     void red3_leds_off();
    16     void blue_leds_on();
    17     void blue_leds_off();
    18     void green_leds_on();
    19     void green_leds_off();
    20 }
    View Code

    主要定义了KeyLedsService的接口,IKeyLedsManager接口主要提供了设备和获取硬件寄存器val的值的功能,通过几个具体函数来实现。

    2, 返回到frameworks/base目录,打开Android.mk文件,修改LOCAL_SRC_FILES变量的值,增加IKeyLedsManager.aidl源文件:

     LOCAL_SRC_FILES += /

       ....................................................................

       core/java/android/os/IVibratorService.aidl /

       core/java/android/os/IKeyLedsManager.aidl /

       core/java/android/service/urlrenderer/IUrlRendererService.aidl /

       .....................................................................

    此时编译,就会根据IKeyLedsManager.aidl生成相应的IKeyLedsManager.Stub接口。

    3,增加KeyLedsManager.java,KeyLedsManager.java主要是通过调用JNI方法来提供硬件服务。

    (framework/base/core/java/android/app/KeyLedsManager.java)

      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.app;
     18 
     19 import android.content.Intent;
     20 import android.os.Handler;
     21 import android.os.HandlerThread;
     22 import android.os.IBinder;
     23 import android.os.Looper;
     24 import android.os.Message;
     25 import android.util.Log; 
     26 import android.os.RemoteException;
     27 import android.content.Context;
     28 
     29 public class KeyLedsManager {
     30     public static final String TAG = "KeyLedsManager";
     31     IKeyLedsManager mService;
     32     Context mContext;
     33     Handler mHandler;
     34     
     35     private final Runnable mOff = new Runnable() {
     36         public void run() {
     37             Log.d("__ngy-->", "mOff run , turn breath leds off");
     38             keyLedsAllOff();
     39         }
     40     };
     41 
     42     public KeyLedsManager(Context ctx, IKeyLedsManager service) {
     43         mContext = ctx;
     44         mService = service;
     45         Log.d(TAG, "mContext=" + mContext);
     46     }
     47 
     48     public void redKeyLedsTurnOn() {
     49         Log.d(TAG, "keyLedsTurnOn");
     50         red1_leds_on();
     51         red2_leds_on();
     52         red3_leds_on();
     53     }
     54     
     55     public void greenkeyLedsTurnOn() {
     56         Log.d(TAG, "keyLedsTurnOn");
     57         green_leds_on();
     58     }
     59 
     60     public void keyLedsAllOff(){
     61         Log.d(TAG, "keyLedsAllOff");
     62         Log.d(TAG, "turn key leds off");
     63         mHandler.removeCallbacks(mOff);
     64         
     65         red1_leds_off();
     66         red2_leds_off();
     67         red3_leds_off();
     68         blue_leds_off();
     69         green_leds_off();
     70     }
     71         
     72     public void red1_leds_exists(){  
     73         try {
     74             mService.red1_leds_exists();
     75         } catch(NullPointerException e){
     76             Log.e(TAG, "NullPointerException=" + e);
     77         } catch (RemoteException e) {
     78             Log.e(TAG, "[red1_leds_exists] RemoteException");
     79         }
     80     }
     81 
     82     public void red2_leds_exists(){  
     83         try {
     84             mService.red2_leds_exists();
     85         } catch(NullPointerException e){
     86             Log.e(TAG, "NullPointerException=" + e);
     87         } catch (RemoteException e) {
     88             Log.e(TAG, "[red2_leds_exists] RemoteException");
     89         }
     90     }
     91     
     92     。。。。。。。。。。。。。。。。。。。。。。。
     93 。。。。。。。。。。。。。。。。。。。。。。。。。。。
     94     
     95     public void green_leds_off(){  
     96         try {
     97             mService.green_leds_off();
     98         } catch(NullPointerException e){
     99             Log.e(TAG, "NullPointerException=" + e);
    100         } catch (RemoteException e) {
    101             Log.e(TAG, "[green_leds_off] RemoteException");
    102         }
    103     }
    104 }
    View Code

    Current.txt(framework/base/api/current.txt),这个修改后更新需要 make update-api

    ContextImpl.java(framework/base/core/java/android/app/ContextImpl.java)(编译后自动更新)

    4,进入到frameworks/base/services/java/com/android/server/目录,新增keyleds/KeyLedsService.java文件:

      KeyLedsService主要是通过调用JNI方法来控制不同状态(未读短信,未接电话等)下的灯的状态

    5,修改SystemServer.java文件,声明这个service,在初始化函数中增加加载KeyLedsService的代码:

       (frameworksaseservicesjavacomandroidserverSystemServer.java)

     KeyLedsService mKeyLedsService = null;

    mKeyLedsService = new KeyLedsService(context);
    ServiceManager.addService(Context.KEYLEDS_SERVICE, mKeyLedsService);

    在上层使用的时候,声明KeyLedsManager,调用方法即可;

    参考文档:http://blog.csdn.net/poltroon/article/details/7978264

  • 相关阅读:
    Golang 版本 支付宝支付SDK app支付接口2.0
    用 Certbot-auto 在 letsencrypt.org申请免费 SSL 证书实现 HTTPS
    Go类型断言demo
    签名算法
    golang cron定时任务简单实现
    Chapter Zero 0.2.2 内存
    Linux-系统目录结构
    Linux-关于Bash
    Chapter Zero 0.2.1 执行运算与判断的CPU
    Chapter Zero 0.1.4 计算机上常用的计算单位
  • 原文地址:https://www.cnblogs.com/zjqlogs/p/5175532.html
Copyright © 2020-2023  润新知