2013年5月2日 星期四

Andorid HAL & system service testing / example code for TI AM335xevm JB BSP.




A. APP . "AndroInt_ext8_native"
  We start from the demo apk. This apk is very simple , create two text input , and one button. Input the number at text_1 & text_2 , the button click will adding two numbers from each text, and show result on text of Button.


The IAndroIntServie is java interface , create by .aidl file in frameworks direct.
In this interface only have one function "AndroInt_add()".

So this using ServiceManage.getServie() to get system service.
This Service will implemnet in frameworks .

Now we go through to next step " modify frameworks ".

diff -Nura TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/packages/apps/AndroInt_ext8_native/src/com/example/androInt_ext8_native/MainActivity.java TI-Android-JB-4.1.2_AM335x_4.0.1/packages/apps/AndroInt_ext8_native/src/com/example/androInt_ext8_native/MainActivity.java
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/packages/apps/AndroInt_ext8_native/src/com/example/androInt_ext8_native/MainActivity.java    1970-01-01 08:00:00.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/packages/apps/AndroInt_ext8_native/src/com/example/androInt_ext8_native/MainActivity.java    2013-05-02 16:58:39.860415713 +0800
@@ -0,0 +1,87 @@
+package com.example.androInt_ext8_native;
+
+import android.os.Bundle;
+import android.os.IBinder;
+import android.os.IAndroIntService;
+import android.os.ServiceManager;
+import android.os.RemoteException;
+
+//import android.os.Parcel;
+
+import android.util.Log;
+import android.view.Menu;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.Button;
+import android.widget.EditText;
+import android.app.Activity;
+
+//import android.content.Context;
+
+
+public class MainActivity extends Activity implements OnClickListener
+{
+
+    private static final String TAG = "MainActivity";
+    private EditText mEditText2;
+    private EditText mEditText1;
+    private Button mButton;
+//    private AndroIntService mAndroIntService = null ;
+
+    IAndroIntService om = IAndroIntService.Stub.asInterface(ServiceManager.getService("androint"));
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState)
+    {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_main);
+
+        mEditText1 =(EditText) this.findViewById(R.id.editText1);
+        mEditText2 = (EditText) this.findViewById(R.id.editText2);
+        mButton = (Button) this.findViewById(R.id.button1);
+
+        mButton.setOnClickListener((android.view.View.OnClickListener) this);
+//        mAndroIntService = new AndroIntService(this);
+
+    }
+
+    @Override
+    public boolean onCreateOptionsMenu(Menu menu)
+    {
+        // Inflate the menu; this adds items to the action bar if it is present.
+        getMenuInflater().inflate(R.menu.main, menu);
+        return true;
+    }
+
+
+    @Override
+    public void onClick(View v)
+    {
+        // TODO Auto-generated method stub
+        int result = 0 ;
+        int a = Integer.parseInt(mEditText1.getText().toString());
+        int b = Integer.parseInt(mEditText2.getText().toString());
+
+        Log.i(TAG, "a:" + a + " ,b:" + b );
+        Log.i(TAG, "om:" + om);
+
+        try
+        {
+//            result = a + b ;
+            result = om.AndroInt_add(a, b);
+            Log.i(TAG, "result:" + result);
+        }
+        catch (RemoteException e)
+        {
+            // TODO Auto-generated catch block
+            Log.i(TAG, "Fail.... ");
+        }
+
+        mButton.setText(String.valueOf("A+B="+result ));
+
+    }
+
+}
+
+
+
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/packages/apps/AndroInt_ext8_native/Android.mk    1970-01-01 08:00:00.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/packages/apps/AndroInt_ext8_native/Android.mk    2013-05-02 11:22:09.705661044 +0800
@@ -0,0 +1,10 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_PACKAGE_NAME := AndroInt_ext8_native
+
+include $(BUILD_PACKAGE)



B. Modify Frameworks .

    1. Add new "IAndroIngService.aidl " into Android.mk file.

diff -Nura TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/frameworks/base/Android.mk TI-Android-JB-4.1.2_AM335x_4.0.1/frameworks/base/Android.mk
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/frameworks/base/Android.mk    2012-12-17 15:18:22.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/frameworks/base/Android.mk    2013-04-30 16:25:22.118205204 +0800
@@ -126,6 +126,7 @@
     core/java/android/nfc/INfcTag.aidl \
     core/java/android/os/ICancellationSignal.aidl \
     core/java/android/os/IHardwareService.aidl \
+    core/java/android/os/IAndroIntService.aidl \
     core/java/android/os/IMessenger.aidl \
     core/java/android/os/INetworkManagementService.aidl \
     core/java/android/os/IPermissionController.aidl \






   2.Caeate  "IAndroIntService.aidl " file. This interface only have one function.
      "AndroInt_add()"


diff -Nura TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/frameworks/base/core/java/android/os/IAndroIntService.aidl TI-Android-JB-4.1.2_AM335x_4.0.1/frameworks/base/core/java/android/os/IAndroIntService.aidl
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/frameworks/base/core/java/android/os/IAndroIntService.aidl    1970-01-01 08:00:00.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/frameworks/base/core/java/android/os/IAndroIntService.aidl    2013-04-30 16:25:15.269146567 +0800
@@ -0,0 +1,32 @@
+/*
+ * IAndroIntService.aidl
+ *
+ * Copyright 2013 Jeff Hsieh <jeff@localhost.localdomain>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ *
+ */
+
+package android.os;
+
+/** {@hide} */
+interface IAndroIntService
+{
+    // obsolete flashlight support
+    int AndroInt_add(int a, int b);
+}
+
 


3.Implement AndroIntService.java file.
   In this class , implement AndroInt_add(); function and
   addService "androint" into system service.
   In AndroInt_add() function , we call JNI function add_native().
   The add_native() will access kernel driver.



diff -Nura TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/frameworks/base/services/java/com/android/server/AndroIntService.java TI-Android-JB-4.1.2_AM335x_4.0.1/frameworks/base/services/java/com/android/server/AndroIntService.java
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/frameworks/base/services/java/com/android/server/AndroIntService.java    1970-01-01 08:00:00.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/frameworks/base/services/java/com/android/server/AndroIntService.java    2013-05-02 13:58:21.110380956 +0800
@@ -0,0 +1,75 @@
+/*
+ * AndroIntService.java
+ *
+ * Copyright 2013 Jeff Hsieh <jeff@localhost.localdomain>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ *
+ */
+
+
+package com.android.server;
+
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.os.Handler;
+import android.os.IAndroIntService;
+import android.os.ServiceManager;
+import android.os.Message;
+import android.util.Slog;
+import android.util.Log;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+
+public class AndroIntService
+{
+
+    private static final String LOG_TAG = "AndroIntService";
+
+    private static native int init_native();
+    private static native int add_native(int a,int b);
+
+    private final Context mContext;
+
+    AndroIntService(Context context)
+    {
+        Log.i(LOG_TAG, "AndroIntService.");
+        init_native();
+        mContext = context;
+
+        Log.i(LOG_TAG, "addService.");
+        ServiceManager.addService("androint", mAndroInt_Bind);
+    }
+
+    private final IAndroIntService.Stub mAndroInt_Bind = new IAndroIntService.Stub()
+    {
+        public int AndroInt_add(int a , int b)
+        {
+            int result;
+            Log.i(LOG_TAG, "mAndroInt_Bind.AndroInt_add.");
+            result = add_native(a,b);
+            return result;
+        }
+
+    };
+
+
+
+
+
+}




4.Modif SystemService.java file .
   When system service started, lunch AndroIntService Class and then
   add service "androint" into system service.
 


diff -Nura TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/frameworks/base/services/java/com/android/server/SystemServer.java TI-Android-JB-4.1.2_AM335x_4.0.1/frameworks/base/services/java/com/android/server/SystemServer.java
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/frameworks/base/services/java/com/android/server/SystemServer.java    2012-12-17 15:18:57.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/frameworks/base/services/java/com/android/server/SystemServer.java    2013-05-02 12:41:16.244678767 +0800
@@ -117,6 +117,7 @@
         AccountManagerService accountManager = null;
         ContentService contentService = null;
         LightsService lights = null;
+        AndroIntService mAndroIntService = null;
         PowerManagerService power = null;
         BatteryService battery = null;
         VibratorService vibrator = null;
@@ -208,6 +209,9 @@
             Slog.i(TAG, "Lights Service");
             lights = new LightsService(context);

+            Slog.i(TAG, "Testing Service");
+            mAndroIntService = new AndroIntService(context);
+
             Slog.i(TAG, "Battery Service");
             battery = new BatteryService(context, lights);
             ServiceManager.addService("battery", battery);
@@ -657,7 +661,7 @@
             } catch (Throwable e) {
                 reportWtf("starting CertBlacklister", e);
             }
-           
+
             if (context.getResources().getBoolean(
                     com.android.internal.R.bool.config_enableDreams)) {
                 try {


5.Add jni cpp file into Android.mk file.



diff -Nura TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/frameworks/base/services/jni/Android.mk TI-Android-JB-4.1.2_AM335x_4.0.1/frameworks/base/services/jni/Android.mk
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/frameworks/base/services/jni/Android.mk    2012-12-17 15:18:57.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/frameworks/base/services/jni/Android.mk    2013-04-30 16:25:20.077187730 +0800
@@ -8,6 +8,7 @@
     com_android_server_input_InputManagerService.cpp \
     com_android_server_input_InputWindowHandle.cpp \
     com_android_server_LightsService.cpp \
+    com_android_server_AndroIntService.cpp \
     com_android_server_PowerManagerService.cpp \
     com_android_server_SerialService.cpp \
     com_android_server_SystemServer.cpp \



 6.Implement com_android_server_AndroIntService.cpp file.
   There have two JNI function.

   add_native(): write data into kernle char driver. And do add in kernel driver,
   then read result from kernel char driver.

   init_native(): get HAL module device data , and open kernel char device.



diff -Nura TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/frameworks/base/services/jni/com_android_server_AndroIntService.cpp TI-Android-JB-4.1.2_AM335x_4.0.1/frameworks/base/services/jni/com_android_server_AndroIntService.cpp
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/frameworks/base/services/jni/com_android_server_AndroIntService.cpp    1970-01-01 08:00:00.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/frameworks/base/services/jni/com_android_server_AndroIntService.cpp    2013-05-02 15:14:32.380259488 +0800
@@ -0,0 +1,98 @@
+/*
+ * com_android_server_AndroIntService.cpp
+ *
+ * Copyright 2013 Jeff Hsieh <jeff@localhost.localdomain>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ *
+ */
+
+
+#define LOG_TAG "AndroIntService_JNI"
+
+#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/androint.h>
+
+#include <stdio.h>
+
+namespace android
+{
+static    hw_module_t        *module;
+static     hw_device_t        *device ;
+
+    static jint add_native(JNIEnv *env, jobject clazz,int a , int b)
+    {
+    int result;
+    struct androint_device_t *int_dev;
+
+        ALOGI("[%s]:%d \n",__FUNCTION__,__LINE__);
+        if ( device == NULL )
+        {
+            ALOGI("[%s]:%d:Device is null \n",__FUNCTION__,__LINE__);
+            return (jint)-1;
+        }
+        else
+        {
+            int_dev = (struct androint_device_t *)device;
+            int_dev->write_add(int_dev,a,b);
+            result = int_dev->read_add(int_dev);
+            ALOGI("[%s]:%d \n",__FUNCTION__,__LINE__);
+
+        }
+        return (jint)result;
+    }
+
+    static jint init_native(JNIEnv *env, jobject clazz)
+    {
+    int err;
+
+        ALOGI("[%s]:%d \n",__FUNCTION__,__LINE__);
+        err = hw_get_module(ANDROINT_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
+        if (err == 0)
+        {
+            err = module->methods->open(module, ANDROINT_HARDWARE_MODULE_ID, &device);
+            ALOGI("get module success! \n" );
+        }
+        else
+        {
+            device = NULL;
+            ALOGI("get module fail:%d! \n",err );
+        }
+
+        return (jint)err;
+    }
+
+    static JNINativeMethod method_table[] =
+    {
+        { "add_native", "(II)I", (void*)add_native },
+        { "init_native", "()I", (void*)init_native },
+    };
+
+    int register_android_server_AndroIntService(JNIEnv *env)
+    {
+        ALOGI("[%s]:%d \n",__FUNCTION__,__LINE__);
+        return jniRegisterNativeMethods(env, "com/android/server/AndroIntService",
+                method_table, NELEM(method_table));
+    }
+
+};



  7.Modify onload.cpp file .
    when system load .so module file. the "JNI_OnLoad"  is first doing function.
    In this function , we so register Natiye Metheods .



diff -Nura TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/frameworks/base/services/jni/onload.cpp TI-Android-JB-4.1.2_AM335x_4.0.1/frameworks/base/services/jni/onload.cpp
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/frameworks/base/services/jni/onload.cpp    2012-12-17 15:18:57.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/frameworks/base/services/jni/onload.cpp    2013-04-30 19:31:08.255261707 +0800
@@ -19,6 +19,8 @@
 #include "utils/Log.h"
 #include "utils/misc.h"

+#define LOG_TAG "onload"
+
 namespace android {
 int register_android_server_AlarmManagerService(JNIEnv* env);
 int register_android_server_BatteryService(JNIEnv* env);
@@ -34,6 +36,9 @@
 int register_android_server_SystemServer(JNIEnv* env);
 int register_android_server_location_GpsLocationProvider(JNIEnv* env);
 int register_android_server_connectivity_Vpn(JNIEnv* env);
+
+int register_android_server_AndroIntService(JNIEnv* env);
+
 };

 using namespace android;
@@ -64,5 +69,8 @@
     register_android_server_location_GpsLocationProvider(env);
     register_android_server_connectivity_Vpn(env);

+    ALOGI("register_android_server_AndroIntService...... ");
+    register_android_server_AndroIntService(env);
+
     return JNI_VERSION_1_4;
 }




C. Hard device side (HAL)


1. Create androint.h file.
    In this file , define driver using stauct , called "androint_device_t"
    And define HARDWARE_MODULE_ID "androint"


diff -Nura TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/hardware/libhardware/include/hardware/androint.h TI-Android-JB-4.1.2_AM335x_4.0.1/hardware/libhardware/include/hardware/androint.h
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/hardware/libhardware/include/hardware/androint.h    1970-01-01 08:00:00.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/hardware/libhardware/include/hardware/androint.h    2013-05-02 14:36:20.091463495 +0800
@@ -0,0 +1,54 @@
+/*
+ * androint.h
+ *
+ * Copyright 2013 Jeff Hsieh <jeff@localhost.localdomain>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ *
+ */
+
+#ifndef ANDROIDINT_INTERFACE_H
+#define ANDROIDINT_INTERFACE_H
+
+#include <stdint.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+#include <hardware/hardware.h>
+
+__BEGIN_DECLS
+
+/**
+ * The id of this module
+ */
+#define ANDROINT_HARDWARE_MODULE_ID        "androint"
+
+struct androint_device_t
+{
+    struct hw_device_t hw_dev;
+
+    int fd;
+    int (*write_add)(struct androint_device_t* dev,int a , int b );
+    int (*read_add)(struct androint_device_t* dev);
+};
+
+
+
+__END_DECLS
+
+#endif  // ANDROID_LIGHTS_INTERFACE_H
+




2. Modify am335xevm_sd.mk file .
    Add androin.$(TARGET_PRODUCE) .
    ### importent ###
         androin.$(TARGET_PRODUCE) .
         must same as HARDWARE_MODULE_ID
  
   the HAL hw_get_module() will searce /system/lib/hw/ <id.product.board>
   the id is HARDWARE_MODULE_ID.


diff -Nura TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/device/ti/am335xevm_sk/am335xevm_sk.mk TI-Android-JB-4.1.2_AM335x_4.0.1/device/ti/am335xevm_sk/am335xevm_sk.mk
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/device/ti/am335xevm_sk/am335xevm_sk.mk    2012-12-17 15:17:46.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/device/ti/am335xevm_sk/am335xevm_sk.mk    2013-05-02 16:42:49.562787996 +0800
@@ -28,7 +28,8 @@
         MagicSmokeWallpapers \
         VisualizationWallpapers \
         librs_jni \
-        lights.$(TARGET_PRODUCT)
+        lights.$(TARGET_PRODUCT) \
+        androint.$(TARGET_PRODUCT)

 PRODUCT_PROPERTY_OVERRIDES := \
         net.dns1=8.8.8.8 \


3.Modify device.mk file .




diff -Nura TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/device/ti/am335xevm_sk/device.mk TI-Android-JB-4.1.2_AM335x_4.0.1/device/ti/am335xevm_sk/device.mk
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/device/ti/am335xevm_sk/device.mk    2012-12-17 15:17:46.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/device/ti/am335xevm_sk/device.mk    2013-05-02 16:42:51.695805279 +0800
@@ -112,6 +112,9 @@
     hcitool

 PRODUCT_PACKAGES += \
+    androint.am335xevm_sk
+
+PRODUCT_PACKAGES += \
     lights.am335xevm_sk

 PRODUCT_PACKAGES += \



 4.Implement add.c file.
    This file is driver access function.

    open_add(): open device file of /dev/xxxxx . and setting read_add / write_add
                        function point into androint_device_t  struct.

     write_add()/read_add() : Direct access kernel device .


diff -Nura TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/device/ti/am335xevm_sk/libintadd/add.c TI-Android-JB-4.1.2_AM335x_4.0.1/device/ti/am335xevm_sk/libintadd/add.c
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/device/ti/am335xevm_sk/libintadd/add.c    1970-01-01 08:00:00.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/device/ti/am335xevm_sk/libintadd/add.c    2013-05-02 16:46:04.784374408 +0800
@@ -0,0 +1,161 @@
+/*
+ * add.c
+ *
+ * Copyright 2013 Jeff Hsieh <jeff@localhost.localdomain>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ *
+ */
+
+
+#define LOG_TAG "HAL_add"
+
+#include <cutils/log.h>
+
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+
+#include <sys/ioctl.h>
+#include <sys/types.h>
+
+#include <hardware/androint.h>
+
+
+#define DEVFILE "/dev/androint-1"
+
+
+static pthread_once_t g_init = PTHREAD_ONCE_INIT;
+static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
+
+static void init_globals(void)
+{
+    /* init the mutex */
+    pthread_mutex_init(&g_lock, NULL);
+}
+
+static int write_add(struct androint_device_t *dev, int a , int b )
+{
+int        err;
+int     buf[2];
+
+    ALOGI("Device:%s not open !!\n", DEVFILE);
+    err = -1;
+    if (dev->fd < 0  )
+    {
+        ALOGI("Device:%s not open !!\n", DEVFILE);
+        return err ;
+    }
+    pthread_mutex_lock(&g_lock);
+
+    buf[0] = a;
+    buf[1] = b;
+
+    err = write(dev->fd,buf,sizeof(int)*2);
+
+    pthread_mutex_unlock(&g_lock);
+
+    return err ;
+
+}
+
+static int read_add(struct androint_device_t* dev)
+{
+int        err;
+int     result;
+
+    err = -1;
+    if (dev->fd < 0  )
+    {
+        ALOGI("Device:%s not open !!\n", DEVFILE);
+        return err ;
+    }
+
+    pthread_mutex_lock(&g_lock);
+
+    read(dev->fd, &result,sizeof(int));
+
+    pthread_mutex_unlock(&g_lock);
+
+    return result;
+}
+
+static int close_add(struct androint_device_t *dev)
+{
+int    err;
+    err = -1;
+    if (dev->fd > 0 )
+    {
+        ALOGI("Close %s\n", DEVFILE);
+        close(dev->fd);
+        dev->fd = -1 ;
+        err = 0;
+    }
+
+    return err;
+}
+
+static int open_add(const struct hw_module_t *module, char const *name, struct hw_device_t **device)
+{
+struct androint_device_t *dev;
+
+    pthread_once(&g_init, init_globals);
+
+    dev = malloc(sizeof(struct androint_device_t));
+    memset(dev, 0, sizeof(*dev));
+
+    dev->hw_dev.tag = HARDWARE_DEVICE_TAG;
+    dev->hw_dev.version = 0;
+    dev->hw_dev.module = (struct hw_module_t *)module;
+    dev->hw_dev.close =  (int (*)(struct hw_device_t *))close_add;
+
+    dev->fd = open(DEVFILE,O_RDWR);
+    dev->write_add = write_add;
+    dev->read_add = read_add;
+
+
+    ALOGI("Open %s\n", DEVFILE);
+
+    *device = (struct hw_device_t *)dev;
+
+    return 0;
+}
+
+static struct hw_module_methods_t add_module_methods =
+{
+    .open =  open_add,
+};
+
+struct hw_module_t HAL_MODULE_INFO_SYM =
+{
+    .tag = HARDWARE_MODULE_TAG,
+    .version_major = 1,
+    .version_minor = 0,
+    .id = ANDROINT_HARDWARE_MODULE_ID,
+    .name = "Instant testing Module",
+    .author = "Instant, Inc.",
+    .methods = &add_module_methods,
+};
+
+
+
+
+
 


  5.Create Andorid.mk file for build add.c .


diff -Nura TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/device/ti/am335xevm_sk/libintadd/Android.mk TI-Android-JB-4.1.2_AM335x_4.0.1/device/ti/am335xevm_sk/libintadd/Android.mk
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/device/ti/am335xevm_sk/libintadd/Android.mk    1970-01-01 08:00:00.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/device/ti/am335xevm_sk/libintadd/Android.mk    2013-05-02 16:58:00.966105273 +0800
@@ -0,0 +1,40 @@
+#  Android.mk
+#
+#  Copyright 2013 Jeff Hsieh <jeff@localhost.localdomain>
+#
+#  This program is free software; you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation; either version 2 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software
+#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+#  MA 02110-1301, USA.
+#
+#
+
+LOCAL_PATH:= $(call my-dir)
+
+ifeq ($(TARGET_PRODUCT),am335xevm_sk)
+# HAL module implemenation, not prelinked and stored in
+# hw/<COPYPIX_HARDWARE_MODULE_ID>.<ro.board.platform>.so
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := add.c
+
+LOCAL_PRELINK_MODULE := false
+LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
+
+LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE := androint.$(TARGET_PRODUCT)
+
+include $(BUILD_SHARED_LIBRARY)
+
+endif
 



 6.Modify init.am335xevm.rc file . to insmod kernel driver .ko file.



diff -Nura TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/device/ti/am335xevm_sk/init.am335xevm.rc TI-Android-JB-4.1.2_AM335x_4.0.1/device/ti/am335xevm_sk/init.am335xevm.rc
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/device/ti/am335xevm_sk/init.am335xevm.rc    2012-12-17 15:17:46.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/device/ti/am335xevm_sk/init.am335xevm.rc    2013-05-02 15:02:15.438483092 +0800
@@ -43,6 +43,8 @@
     insmod /system/lib/modules/cfg80211.ko
     insmod /system/lib/modules/mac80211.ko
     insmod /system/lib/modules/wl12xx.ko
+    insmod /system/lib/modules/androint_module.ko
+

 on fs
     # Backlight



 7.Modify uevnetd.am335xevm.rc file.
    Change /dev/xxxxx group and owner.


diff -Nura TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/device/ti/am335xevm_sk/ueventd.am335xevm.rc TI-Android-JB-4.1.2_AM335x_4.0.1/device/ti/am335xevm_sk/ueventd.am335xevm.rc
--- TI-Android-JB-4.1.2_AM335x_4.0.1-orignal/device/ti/am335xevm_sk/ueventd.am335xevm.rc    2012-12-17 15:17:46.000000000 +0800
+++ TI-Android-JB-4.1.2_AM335x_4.0.1/device/ti/am335xevm_sk/ueventd.am335xevm.rc    2013-05-02 15:04:22.641516874 +0800
@@ -1 +1,2 @@
 /dev/video0         0666  root       root
+/dev/androint-1        0660  system     system


D. Kernel Dirver

I am lazy to explain it, if have some question please contact me.


1.Makefile

# ./kernel/hardware/driver_test/Makefile
#
# vi: set ts=4 :


##=======================================
## Setting Compiler FLAGS.
##=======================================

export CFLAGS           :=

export CPPFLAGS         :=

export LDFLAGS          :=

##==========================================
## Specital Make Rules.
##==========================================
KERNELDIR                 ?= $(PRJ_KERNEL_DIR)/_version
PWD                       := $(shell pwd)

ifeq ($(KERNELRELEASE),)

all:
    @if [ -L '$(KERNELDIR)' ] ;then \
        INSTALL_MOD_PATH=$(PRJ_IMG_DIR) $(MAKE) -C $(KERNELDIR) M=$(PWD) LDDINC=$(PWD) modules modules_install ;\
    else \
        $(MKMATE) color -fred "## Unknow kernel dir '$(KERNELDIR)', Please build kernel first...." ; \
        exit 1 ; \
    fi

distclean clean:
    @rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions [mM]odule*

else

obj-m   := androint_module.o

endif






2.Driver code.

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>

#include <asm/uaccess.h>
#include <asm/system.h>

#define DEV_MAJOR 48
#define DEV_NAME "androint"

struct androint_dev
{
    int             data[2];
    struct cdev     cdev;
    struct class     *sysfs_class;
    dev_t             dev_num;
};
struct androint_dev androint_device;


ssize_t androint_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
ssize_t retval;
int sum ;

    retval = 0;
    sum = androint_device.data[0] + androint_device.data[1];
    if (count != sizeof (int))
    {
        printk(KERN_INFO "Read error: buffer size not same !\n");
        goto out;
    }

    retval = copy_to_user(buf,&sum , sizeof(int));

    if ( retval)
        retval = -EFAULT;

out:
    return retval;
}

ssize_t androint_write(struct file *filp, const char *buf, size_t count,
            loff_t *f_pos)
{
ssize_t retval;

    retval = 0;

    if (count != sizeof(int) * 2 )
    {
        printk(KERN_INFO "Write error: buffer size not same !\n");
        goto out;
    }

    retval = copy_from_user(androint_device.data , buf, count);

    if ( retval)
        retval = -EFAULT;
out:
    return retval;
}

int androint_open(struct inode *inode, struct file *filp)
{
    int num = MINOR(inode->i_rdev);

    printk("androint_open -> minor : %d\n", num);

    return 0;
}

int androint_release(struct inode *inode, struct file *filp)
{
    printk("androint_release \n");
    return 0;
}

struct file_operations androint_fops =
{
    .owner  = THIS_MODULE,
    .read       = androint_read,
    .write      = androint_write,
    .open       = androint_open,
    .release    = androint_release,
};


static int __init androint_init(void)
{
int        result;

    printk(KERN_INFO "Module inti starts !\n");

    androint_device.dev_num = MKDEV(DEV_MAJOR,0);
    result = register_chrdev_region(androint_device.dev_num,1, DEV_NAME);
    if (result < 0)
    {
        printk( KERN_ALERT "androint: cannot register device region of %s\n",DEV_NAME);
        return result;
    }

    cdev_init(&androint_device.cdev, &androint_fops);
    androint_device.cdev.owner = THIS_MODULE;

    result = cdev_add(&androint_device.cdev,androint_device.dev_num,1);
    if (result)
    {
        printk(KERN_INFO "Can not add device %s \n ", DEV_NAME);
        goto fail_addcdev;
    }

//    populate sysfs entry
    androint_device.sysfs_class = class_create(THIS_MODULE, DEV_NAME);
    device_create(androint_device.sysfs_class, NULL,
                        androint_device.dev_num, NULL, DEV_NAME"-%d", 1);

    printk(KERN_INFO "Module inti done !\n");


fail_addcdev:
    unregister_chrdev_region(androint_device.dev_num,1);

    return result;


}

void androint_exit(void)
{

// remove the cdev
    cdev_del(&androint_device.cdev);

// Freeing the major number
    unregister_chrdev_region(androint_device.dev_num, 1);

    device_destroy(androint_device.sysfs_class, androint_device.dev_num);

    class_destroy(androint_device.sysfs_class);

    printk(KERN_INFO "Module release done !\n");
}


module_init(androint_init);
module_exit(androint_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Instnat");






沒有留言:

張貼留言