实现自己的HAL-12 控制led(自定义的系统service的实现),从app 到 hal 到底层内核kernel层的驱动 的实现

Posted 杨斌并

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实现自己的HAL-12 控制led(自定义的系统service的实现),从app 到 hal 到底层内核kernel层的驱动 的实现相关的知识,希望对你有一定的参考价值。

控制led的自定义的系统service的实现

  • Z:\\itop-3399_8.1\\frameworks\\base\\core\\java\\android\\os 下创建 IHelloWorldService.aidl
  • IHelloWorldService.aidl 代码
/**
 * Copyright (c) 2007, 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.
 */

package android.os;
/** {@hide} */
interface IHelloWorldService {
    boolean ledClose();
	boolean initHW();
	boolean ledOpen();
	boolean openLedDevice();
	boolean closeLedDevice();
}
  • 将 IHelloWorldService.aidl 添加到Z:\\itop-3399_8.1\\frameworks\\base 下的 Android.mk 中 以便编译成java 类
LOCAL_SRC_FILES += \\
	core/java/android/accessibilityservice/IAccessibilityServiceConnection.aidl \\
	core/java/android/accessibilityservice/IAccessibilityServiceClient.aidl \\
	~~~~
	core/java/android/os/IHelloWorldService.aidl \\
	~~~~
  • 在Z:\\itop-3399_8.1\\frameworks\\base\\core\\java\\android\\content 所在的文件夹下的 Context.java 中添加自己的service 的名字
public abstract class Context {
	//添加到了这里
	public static final String HELLO_WORLD = "hello_world_service";
	
    /** @hide */
    @IntDef(flag = true, prefix = { "MODE_" }, value = {
            MODE_PRIVATE,
            MODE_WORLD_READABLE,
            MODE_WORLD_WRITEABLE,
            MODE_APPEND,
    })
    ~~~~~
  • Z:\\itop-3399_8.1\\frameworks\\base\\services\\core\\java\\com\\android\\server 下创建 HelloWorldService.java
  • HelloWorldService.java 代码
package com.android.server;
import android.util.Slog;
import android.os.IHelloWorldService;
public class HelloWorldService extends IHelloWorldService.Stub {
	
	private static final String TAG = "HelloWorldService" ;

    public HelloWorldService() {
       	boolean result = init();
		if(result){
			Slog.d(TAG, "init success");
		}else {
			Slog.d(TAG, "init error");
		}
    }
	
	@Override 
    public boolean ledClose() {
		boolean result = close_led();
        if(result){
			Slog.d(TAG, "ledClose is success");
		}else {
			Slog.d(TAG, "ledClose is error");
		}
        return result;
    }
	
	@Override 
	public boolean  initHW(){
		boolean result = init();
		if(result){
			Slog.d(TAG, "init success");
		}else {
			Slog.d(TAG, "init error");
		}
		return result;
	}
	
	@Override
	public boolean  ledOpen(){
		boolean result = open_led();
		if(result){
			Slog.d(TAG, "letOpen is success");
		}else {
			Slog.d(TAG, "letOpen is error");
		}
		
		return result;
	}
	
	@Override
	public boolean  openLedDevice(){
		boolean result = led_device_open();
		if(result){
			Slog.d(TAG, "openLedDevice is success");
		}else {
			Slog.d(TAG, "openLedDevice is error");
		}
		
		return result;
	}
	
	@Override
	public boolean  closeLedDevice(){
		boolean result = led_device_close();
		if(result){
			Slog.d(TAG, "closeLedDevice is success");
		}else {
			Slog.d(TAG, "closeLedDevice is error");
		}
		
		return result;
	}	

    private static native boolean init();
    private static native boolean close_led();
	private static native boolean open_led();
	private static native boolean led_device_open();
	private static native boolean led_device_close();
}
  • 将HelloWorldService 添加到Z:\\itop-3399_8.1\\frameworks\\base\\services\\java\\com\\android\\server 下的 SystemServer 中
 private void startOtherServices() {
			~~~~
            traceBeginAndSlog("StartKeyAttestationApplicationIdProviderService");
            ServiceManager.addService("sec_key_att_app_id_provider",
                    new KeyAttestationApplicationIdProviderService(context));
            traceEnd();
			///添加到了这里///
			traceBeginAndSlog("StartHelloWorldService");       
            ServiceManager.addService(Context.HELLO_WORLD, new HelloWorldService());             
			traceEnd();
			~~~~
}
  • 将HelloWorldService 服务注册到 Z:\\itop-3399_8.1\\frameworks\\base\\core\\java\\android\\app 下的 SystemServiceRegistry 中
final class SystemServiceRegistry {
    private static final String TAG = "SystemServiceRegistry";

    // Service registry information.
    // This information is never changed once static initialization has completed.
    private static final HashMap<Class<?>, String> SYSTEM_SERVICE_NAMES =
            new HashMap<Class<?>, String>();
    private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
            new HashMap<String, ServiceFetcher<?>>();
    private static int sServiceCacheSize;

    // Not instantiable.
    private SystemServiceRegistry() { }

    static {

			 registerService(Context.HELLO_WORLD, HelloWorldServiceManger.class,
		                new CachedServiceFetcher<HelloWorldServiceManger>() {
		            @Override
		            public HelloWorldServiceManger createService(ContextImpl ctx) throws ServiceNotFoundException{
						IBinder b = ServiceManager.getServiceOrThrow(Context.HELLO_WORLD);
						IHelloWorldService service = IHelloWorldService.Stub.asInterface(b);
		                return new HelloWorldServiceManger(service);
		            }});
    
      }
 }
  • 编译system.img 镜像

make systemimage -j8

  • 刷到开发版
  • 系统启动时成功打印如下说明成功

HelloWorldService 说明成功

以上是关于实现自己的HAL-12 控制led(自定义的系统service的实现),从app 到 hal 到底层内核kernel层的驱动 的实现的主要内容,如果未能解决你的问题,请参考以下文章

实现自己的HAL-15 控制led(Android 8.1 selinux 配置(自定义系统服务,以及节点的读写)),从app 到 hal 到底层内核kernel层的驱动 的实现

实现自己的HAL-15 控制led(Android 8.1 selinux 配置(自定义系统服务,以及节点的读写)),从app 到 hal 到底层内核kernel层的驱动 的实现

Android进阶——自定义View之自己绘制彩虹圆环调色板

esp32cam板载led是那个引脚

LED灯无线如何控制,利用了啥技术以及原理?

实现自己的HAL-10 控制led(内核驱动的hal),从app 到 hal 到底层内核kernel层的驱动 的实现