1,framework层增加硬件访问服务
①文件操作
1)ISenrslService.aidl定义ISenrslService的接口
- senrsl@senrsl-ubuntu:~$ cd android/source/WORKING_DIRECTORY/
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY$ cd frameworks/base/core/java/android/os/
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/frameworks/base/core/java/android/os$ ll
- 总用量 1296
- drwxrwxr-x 3 senrsl senrsl 4096 3月 20 13:37 ./
- drwxrwxr-x 34 senrsl senrsl 4096 3月 20 13:37 ../
- -rw-rw-r-- 1 senrsl senrsl 1671 3月 20 13:37 AsyncResult.java
- -rw-rw-r-- 1 senrsl senrsl 26577 3月 20 13:37 AsyncTask.java
- -rw-rw-r-- 1 senrsl senrsl 1011 3月 20 13:37 BadParcelableException.java
- -rw-rw-r-- 1 senrsl senrsl 4769 3月 20 13:37 BatteryManager.java
- -rw-rw-r-- 1 senrsl senrsl 667 3月 20 13:37 BatteryProperties.aidl
- -rw-rw-r-- 1 senrsl senrsl 2909 3月 20 13:37 BatteryProperties.java
- -rw-rw-r-- 1 senrsl senrsl 103641 3月 20 13:37 BatteryStats.java
- -rw-rw-r-- 1 senrsl senrsl 17680 3月 20 13:37 Binder.java
- -rw-rw-r-- 1 senrsl senrsl 7154 3月 20 13:37 Broadcaster.java
- -rw-rw-r-- 1 senrsl senrsl 24561 3月 20 13:37 Build.java
- -rw-rw-r-- 1 senrsl senrsl 712 3月 20 13:37 Bundle.aidl
- -rw-rw-r-- 1 senrsl senrsl 54107 3月 20 13:37 Bundle.java
- -rw-rw-r-- 1 senrsl senrsl 6117 3月 20 13:37 CancellationSignal.java
- -rw-rw-r-- 1 senrsl senrsl 16852 3月 20 13:37 CommonClock.java
- -rw-rw-r-- 1 senrsl senrsl 18701 3月 20 13:37 CommonTimeConfig.java
- -rw-rw-r-- 1 senrsl senrsl 9487 3月 20 13:37 CommonTimeUtils.java
- -rw-rw-r-- 1 senrsl senrsl 3921 3月 20 13:37 ConditionVariable.java
- -rw-rw-r-- 1 senrsl senrsl 4300 3月 20 13:37 CountDownTimer.java
- -rw-rw-r-- 1 senrsl senrsl 893 3月 20 13:37 DeadObjectException.java
- -rw-rw-r-- 1 senrsl senrsl 58887 3月 20 13:37 Debug.java
- -rw-rw-r-- 1 senrsl senrsl 675 3月 20 13:37 DropBoxManager.aidl
- -rw-rw-r-- 1 senrsl senrsl 13133 3月 20 13:37 DropBoxManager.java
- -rw-rw-r-- 1 senrsl senrsl 32366 3月 20 13:37 Environment.java
- -rw-rw-r-- 1 senrsl senrsl 1156 3月 20 13:37 FactoryTest.java
- -rw-rw-r-- 1 senrsl senrsl 8176 3月 20 13:37 FileObserver.java
- -rw-rw-r-- 1 senrsl senrsl 12535 3月 20 13:37 FileUtils.java
- -rw-rw-r-- 1 senrsl senrsl 30954 3月 20 13:37 Handler.java
- -rw-rw-r-- 1 senrsl senrsl 4898 3月 20 13:37 HandlerThread.java
- -rw-rw-r-- 1 senrsl senrsl 806 3月 20 13:37 IBatteryPropertiesListener.aidl
- -rw-rw-r-- 1 senrsl senrsl 876 3月 20 13:37 IBatteryPropertiesRegistrar.aidl
- -rw-rw-r-- 1 senrsl senrsl 11656 3月 20 13:37 IBinder.java
- -rw-rw-r-- 1 senrsl senrsl 719 3月 20 13:37 ICancellationSignal.aidl
- -rw-rw-r-- 1 senrsl senrsl 804 3月 20 13:37 IHardwareService.aidl
- -rw-rw-r-- 1 senrsl senrsl 1013 3月 20 13:37 IInterface.java
- -rw-rw-r-- 1 senrsl senrsl 814 3月 20 13:37 IMessenger.aidl
- -rw-rw-r-- 1 senrsl senrsl 11835 3月 20 13:37 INetworkManagementService.aidl
- -rw-rw-r-- 1 senrsl senrsl 815 3月 20 13:37 IPermissionController.aidl
- -rw-rw-r-- 1 senrsl senrsl 2221 3月 20 13:37 IPowerManager.aidl
- -rw-rw-r-- 1 senrsl senrsl 824 3月 20 13:37 IRemoteCallback.aidl
- -rw-rw-r-- 1 senrsl senrsl 1093 3月 20 13:37 ISchedulingPolicyService.aidl
- -rw-rw-r-- 1 senrsl senrsl 2594 3月 20 13:37 IServiceManager.java
- -rw-rw-r-- 1 senrsl senrsl 849 3月 20 13:37 IUpdateLock.aidl
- -rw-rw-r-- 1 senrsl senrsl 1973 3月 20 13:37 IUserManager.aidl
- -rw-rw-r-- 1 senrsl senrsl 937 3月 20 13:37 IVibratorService.aidl
- -rw-rw-r-- 1 senrsl senrsl 10781 3月 20 13:37 Looper.java
- -rw-rw-r-- 1 senrsl senrsl 11164 3月 20 13:37 MemoryFile.java
- -rw-rw-r-- 1 senrsl senrsl 718 3月 20 13:37 Message.aidl
- -rw-rw-r-- 1 senrsl senrsl 16390 3月 20 13:37 Message.java
- -rw-rw-r-- 1 senrsl senrsl 18819 3月 20 13:37 MessageQueue.java
- -rw-rw-r-- 1 senrsl senrsl 720 3月 20 13:37 Messenger.aidl
- -rw-rw-r-- 1 senrsl senrsl 4671 3月 20 13:37 Messenger.java
- -rw-rw-r-- 1 senrsl senrsl 1230 3月 20 13:37 NetworkOnMainThreadException.java
- -rw-rw-r-- 1 senrsl senrsl 1678 3月 20 13:37 NullVibrator.java
- -rw-rw-r-- 1 senrsl senrsl 1039 3月 20 13:37 OperationCanceledException.java
- -rw-rw-r-- 1 senrsl senrsl 135 3月 20 13:37 package.html
- -rw-rw-r-- 1 senrsl senrsl 4874 3月 20 13:37 Parcelable.java
- -rw-rw-r-- 1 senrsl senrsl 2223 3月 20 13:37 ParcelableParcel.java
- -rw-rw-r-- 1 senrsl senrsl 740 3月 20 13:37 ParcelFileDescriptor.aidl
- -rw-rw-r-- 1 senrsl senrsl 36600 3月 20 13:37 ParcelFileDescriptor.java
- -rw-rw-r-- 1 senrsl senrsl 952 3月 20 13:37 ParcelFormatException.java
- -rw-rw-r-- 1 senrsl senrsl 81352 3月 20 13:37 Parcel.java
- -rw-rw-r-- 1 senrsl senrsl 660 3月 20 13:37 ParcelUuid.aidl
- -rw-rw-r-- 1 senrsl senrsl 3658 3月 20 13:37 ParcelUuid.java
- -rw-rw-r-- 1 senrsl senrsl 669 3月 20 13:37 PatternMatcher.aidl
- -rw-rw-r-- 1 senrsl senrsl 6515 3月 20 13:37 PatternMatcher.java
- -rw-rw-r-- 1 senrsl senrsl 23704 3月 20 13:37 PerformanceCollector.java
- -rw-rw-r-- 1 senrsl senrsl 31261 3月 20 13:37 PowerManager.java
- -rw-rw-r-- 1 senrsl senrsl 37308 3月 20 13:37 Process.java
- -rw-rw-r-- 1 senrsl senrsl 17354 3月 20 13:37 RecoverySystem.java
- -rw-rw-r-- 1 senrsl senrsl 2594 3月 20 13:37 Registrant.java
- -rw-rw-r-- 1 senrsl senrsl 3176 3月 20 13:37 RegistrantList.java
- -rw-rw-r-- 1 senrsl senrsl 672 3月 20 13:37 RemoteCallback.aidl
- -rw-rw-r-- 1 senrsl senrsl 3020 3月 20 13:37 RemoteCallback.java
- -rw-rw-r-- 1 senrsl senrsl 12275 3月 20 13:37 RemoteCallbackList.java
- -rw-rw-r-- 1 senrsl senrsl 936 3月 20 13:37 RemoteException.java
- -rw-rw-r-- 1 senrsl senrsl 828 3月 20 13:37 RemoteMailException.java
- -rw-rw-r-- 1 senrsl senrsl 734 3月 20 13:37 ResultReceiver.aidl
- -rw-rw-r-- 1 senrsl senrsl 4182 3月 20 13:37 ResultReceiver.java
- -rw-rw-r-- 1 senrsl senrsl 6662 3月 20 13:37 SELinux.java
- -rw-rw-r-- 1 senrsl senrsl 4392 3月 20 13:37 ServiceManager.java
- -rw-rw-r-- 1 senrsl senrsl 6637 3月 20 13:37 ServiceManagerNative.java
- -rw-rw-r-- 1 senrsl senrsl 4313 3月 20 13:37 StatFs.java
- drwxrwxr-x 2 senrsl senrsl 4096 3月 20 13:37 storage/
- -rw-rw-r-- 1 senrsl senrsl 84186 3月 20 13:37 StrictMode.java
- -rw-rw-r-- 1 senrsl senrsl 7464 3月 20 13:37 SystemClock.java
- -rw-rw-r-- 1 senrsl senrsl 5768 3月 20 13:37 SystemProperties.java
- -rw-rw-r-- 1 senrsl senrsl 4337 3月 20 13:37 SystemService.java
- -rw-rw-r-- 1 senrsl senrsl 3566 3月 20 13:37 SystemVibrator.java
- -rw-rw-r-- 1 senrsl senrsl 5978 3月 20 13:37 TokenWatcher.java
- -rw-rw-r-- 1 senrsl senrsl 10997 3月 20 13:37 Trace.java
- -rw-rw-r-- 1 senrsl senrsl 2969 3月 20 13:37 TransactionTooLargeException.java
- -rw-rw-r-- 1 senrsl senrsl 8321 3月 20 13:37 UEventObserver.java
- -rw-rw-r-- 1 senrsl senrsl 5161 3月 20 13:37 UpdateLock.java
- -rw-rw-r-- 1 senrsl senrsl 666 3月 20 13:37 UserHandle.aidl
- -rw-rw-r-- 1 senrsl senrsl 9783 3月 20 13:37 UserHandle.java
- -rw-rw-r-- 1 senrsl senrsl 25003 3月 20 13:37 UserManager.java
- -rw-rw-r-- 1 senrsl senrsl 3406 3月 20 13:37 Vibrator.java
- -rw-rw-r-- 1 senrsl senrsl 657 3月 20 13:37 WorkSource.aidl
- -rw-rw-r-- 1 senrsl senrsl 23097 3月 20 13:37 WorkSource.java
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/frameworks/base/core/java/android/os$ touch ISenrslService.aidl
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/frameworks/base/core/java/android/os$ gedit ISenrslService.aidl
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/frameworks/base/core/java/android/os$ cd ..
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/frameworks/base/core/java/android$ cd ..
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/frameworks/base/core/java$ cd ..
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/frameworks/base/core$ cd ..
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/frameworks/base$ gedit Android.mk
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/frameworks/base$ cd services/java/com/android/server/
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/frameworks/base/services/java/com/android/server$ touch SenrslService.java
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/frameworks/base/services/java/com/android/server$ gedit SenrslService.java
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/frameworks/base/services/java/com/android/server$ gedit SystemServer.java
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/frameworks/base/services/java/com/android/server$
2)/home/senrsl/android/source/WORKING_DIRECTORY/frameworks/base/Android.mk
- package android.os;
- /**
- *在Android系统中,硬件服务一般是运行在一个独立的进程中为各种应用程序提供服务。因此,调用这些硬件服 务的应用程序与这些硬件服务之间的通信需要通过代理来进行。为此,我们要先定义好通信接口。进入到 frameworks/base/core/java/android/os目录,新增ISenrslService.aidl 接口定义文件
- *ISenrslService接口主要提供了设备和获取硬件寄存器val的值的功能,分别通过setVal和 getVal两个函数来实现
- */
- interface ISenrslService {
- void setVal(int val);
- int getVal();
- }
在LOCAL_SRC_FILES += \中增加
3)SenrslService.java
- core/java/android/os/IVibratorService.aidl \
- core/java/android/os/ISenrslService.aidl \
- core/java/android/service/notification/INotificationListener.aidl \
4)在同目录的SystemServer.java中增加
- package com.android.server;
- import android.content.Context;
- import android.os.ISenrslService;
- import android.util.Slog;
- /**
- *SenrslService主要是通过调用JNI方法init_native、setVal_native和 getVal_native来提供硬件服务。
- */
- public class SenrslService extends ISenrslService.Stub {
- private static final String TAG = "SenrslService";
- SenrslService(Context context) {
- 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();
- };
line 733.
- try {
- Slog.i(TAG, "DiskStats Service");
- ServiceManager.addService("diskstats", new DiskStatsService(context));
- } catch (Throwable e) {
- reportWtf("starting DiskStats Service", e);
- }
- try {
- Slog.i(TAG, "Senrsl Service");
- ServiceManager.addService("senrsl", new SenrslService(context));
- } catch (Throwable e) {
- reportWtf("starting Senrsl Service", e);
- }
②编译
1)编译增加aidl
编译完后会生成
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY$ mmm frameworks/base
2)编译SenrslService
- /home/senrsl/android/source/WORKING_DIRECTORY/out/target/common/obj/JAVA_LIBRARIES/framework-base_intermediates/classes/android/os/ISenrslService.class
- /home/senrsl/android/source/WORKING_DIRECTORY/out/target/common/obj/JAVA_LIBRARIES/framework-base_intermediates/classes/android/os/ISenrslService$Stub.class
- /home/senrsl/android/source/WORKING_DIRECTORY/out/target/common/obj/JAVA_LIBRARIES/framework-base_intermediates/classes/android/os/ISenrslService$Stub$Proxy.class
③重新打包system.img
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY$ mmm frameworks/base/services/java
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY$ make snod
这样,重新打包后的system.img就在framework层包含了自定义的硬件服务SenrslService,并且会在系统启动时,自动 加载SenrslService.
应用程序可以通过java接口来访问senrsl硬件服务。
搞定2015年04月10日19:44:35
2,测试demo
开eclipse,新建android项目。
直接使用secondHallo项目。
①文件操作
1)布局文件activity_service.xml
2)strings.xml增加
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/value" >
- </TextView>
- <EditText
- android:id="@+id/edit_value"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="@string/hint" >
- </EditText>
- </LinearLayout>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:orientation="horizontal" >
- <Button
- android:id="@+id/button_read"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/read" >
- </Button>
- <Button
- android:id="@+id/button_write"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/write" >
- </Button>
- <Button
- android:id="@+id/button_clear"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/clear" >
- </Button>
- </LinearLayout>
- </LinearLayout>
3)SystemServiceActivity.java
- <!-- 系统服务访问 -->
- <string name="system_service">系统服务</string>
- <string name="value">Value</string>
- <string name="hint">请输入。。。。</string>
- <string name="read">Read</string>
- <string name="write">Write</string>
- <string name="clear">Clear</string>
4)mainfest.xml
- /**
- * Filename: SystemServiceActivity.java
- * Description:
- * Copyright: Copyright (c)2015
- * Company: SENRSL
- * @author: senRsl senRsl@163.com
- * @version: 1.0
- * Create at: 2015-4-10 下午7:58:41
- *
- * Modification History:
- * Date Author Version Description
- * ------------------------------------------------------------------
- * 2015-4-10 senRsl 1.0 1.0 Version
- */
- package dc.study;
- import android.app.Activity;
- import android.os.ServiceManager;
- import android.os.Bundle;
- import android.os.ISenrslService;
- import android.os.RemoteException;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- /**
- * 获取自定义系统服务SenrslService
- * 程序通过ServiceManager.getService("senrsl")来获得HelloService,接着通过 IHelloService.Stub.asInterface函数转换为IHelloService接口。
- * 其中,服务名字"senrsl"是系统启动时加载SenrslService时指定的,而ISenrslService接口定义在 android.os.ISenrslService中
- * 这个程序提供了简单的读定自定义硬件有寄存器val的值的功能,通过ISenrslService.getVal和 ISenrslService.setVal两个接口实现。
- *
- * @ClassName: SystemServiceActivity
- * @author senrsl
- *
- * @Package: dc.study
- * @CreateTime: 2015-4-10 下午7:58:41
- */
- public class SystemServiceActivity extends Activity implements View.OnClickListener{
- private final static String LOG_TAG = "TEST";
- private ISenrslService senrslService = null;
- private EditText valueText = null;
- private Button readButton = null;
- private Button writeButton = null;
- private Button clearButton = null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_service);
- senrslService = ISenrslService.Stub.asInterface(ServiceManager.getService("senrsl"));
- valueText = (EditText) findViewById(R.id.edit_value);
- readButton = (Button) findViewById(R.id.button_read);
- writeButton = (Button) findViewById(R.id.button_write);
- clearButton = (Button) findViewById(R.id.button_clear);
- readButton.setOnClickListener(this);
- writeButton.setOnClickListener(this);
- clearButton.setOnClickListener(this);
- Log.i(LOG_TAG, "获取系统服务的页面已打开");
- }
- @Override
- public void onClick(View v) {
- if (v.equals(readButton)) {
- try {
- int val = senrslService.getVal();
- String text = String.valueOf(val);
- valueText.setText(text);
- } catch (RemoteException e) {<activity android:name=".SystemServiceActivity" />
- Log.e(LOG_TAG,
- "Remote Exception while reading value from device.");
- }
- } else if (v.equals(writeButton)) {
- try {
- String text = valueText.getText().toString();
- int val = Integer.parseInt(text);
- senrslService.setVal(val);
- } catch (RemoteException e) {
- Log.e(LOG_TAG,
- "Remote Exception while writing value to device.");
- }
- } else if (v.equals(clearButton)) {
- String text = "";
- valueText.setText(text);
- }
- }
- }
5)根目录新建Android.mk
- <activity android:name=".SystemServiceActivity" />
②拷贝项目到/home/senrsl/android/source/WORKING_DIRECTORY/packages /experimental,删除eclipse相关文件
- LOCAL_PATH:= $(call my-dir)
- include $(CLEAR_VARS)
- LOCAL_MODULE_TAGS := optional
- LOCAL_SRC_FILES := $(call all-subdir-java-files)
- LOCAL_PACKAGE_NAME := secondHallo
- include $(BUILD_PACKAGE)
③编译apk
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/packages/experimental/secondHallo$ ll
- 总用量 32
- drwxrwxr-x 4 senrsl senrsl 4096 4月 10 20:20 ./
- drwxrwxr-x 19 senrsl senrsl 4096 4月 10 20:06 ../
- -rw-rw-r-- 1 senrsl senrsl 2151 4月 10 20:05 AndroidManifest.xml
- -rw-rw-r-- 1 senrsl senrsl 193 4月 10 20:10 Android.mk
- -rw-rw-r-- 1 senrsl senrsl 1248 12月 15 15:25 proguard.cfg
- -rw-rw-r-- 1 senrsl senrsl 361 12月 15 15:25 project.properties
- drwxrwxr-x 7 senrsl senrsl 4096 12月 15 15:25 res/
- drwxrwxr-x 3 senrsl senrsl 4096 12月 15 15:25 src/
编译完成后会生成/home/senrsl/android/source/WORKING_DIRECTORY/out/target /product/generic/system/app/secondHallo.apk
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY$ mmm packages/experimental/secondHallo/
④重新打包system.img
⑤启动测试
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY$ make snod
1)启动
2)点击读取,日志:
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY$ emulator -kernel /home/senrsl/android/source/kernel/goldfish/arch/arm/boot/zImage
- 04-10 08:48:40.630: I/SenrslStub(391): Senrsl Stub: get value 0 from device
- 04-10 08:48:40.630: I/SenrslService(391): Senrsl JNI: get value 0 from device.
- 04-10 08:48:40.690: I/Choreographer(550): Skipped 32 frames! The application may be doing too much work on its main thread.
- 04-10 08:48:40.740: I/LatinIME:LogUtils(524): Dictionary info: dictionary = contacts.zh_CN.dict ; version = ? ; date = ?
3)点击写,日志:
- 04-10 08:49:46.990: I/SenrslService(391): Senrsl JNI: set value 123 to device.
- 04-10 08:49:46.990: I/SenrslStub(391): Senrsl Stub: set value 123 to device.
E1)一开始启动后点读或写日志报设备打不开failed to open hello device
这个问题是因为之前改了个权限0666那个地方,一直都是mmm单模块来编译,没有把权限这个编译进去。
解决:整体重新编译一下就好了
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY$ make -j16
这个系列折腾了一整天,终于搞通了!
2015年04月10日20:54:14
--
senRsl
2015年04月10日17:09:22
2015年04月10日17:09:22
没有评论 :
发表评论