gpio驱动-》gpioHal-》gpioJni-》gpioService-》gpioTest

来源:互联网 发布:科比生涯总数据 编辑:程序博客网 时间:2024/06/14 00:16

平台: A64 Android5.1

第一部分:驱动

#include <linux/interrupt.h>
#include <linux/input/matrix_keypad.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/async.h>
#include <linux/hrtimer.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/init-input.h>
#include <linux/gpio.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/kernel.h>
#include <asm/uaccess.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>



#define DEVICE_NAME "gpio_ctrl"

static long ococci_gpio_ioctl(struct file *filp, unsigned int gp, unsigned long Gcmd)
{
int data = -1;
unsigned int cmd=(unsigned int)Gcmd;
printk("cmd = %d  gp=%d \n",cmd,gp);
if(0 != gpio_request(gp, NULL)) {
printk("led_gpio gpio_request fail ! %d\n",gp);
return -1;
}

switch(cmd){
case 1 :
if (0 != gpio_direction_input(gp))
printk("gpio_direction_output fail !\n");
data = __gpio_get_value(gp);
gpio_free(gp);
return data;
case 2:
if (0 != gpio_direction_output(gp, 1))
printk("gpio_direction_output fail !\n");
break;
case 3 :
if (0 != gpio_direction_output(gp, 0))
printk("gpio_direction_output fail !\n");
break;
default:
printk("cmd is wrong \n");
return -1;
}
gpio_free(gp);
return 0;
}
static struct file_operations dev_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl= ococci_gpio_ioctl,

};


static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &dev_fops,
};


static int __init dev_init(void)
{
int ret;
  printk("-------------------->dev_init--------------------->\n");
ret = misc_register(&misc);

printk (DEVICE_NAME"\tinitialized\n");


return ret;
}


static void __exit dev_exit(void)
{
    misc_deregister(&misc);
}


module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ococci shane");


第二部分:gpioHal

#include <hardware/hardware.h>
#include <hardware/gpio.h>
#include <fcntl.h>
#include <errno.h>
#include <cutils/log.h>
#include <cutils/atomic.h>
#include <sys/ioctl.h>


#include <log/log.h>
#include <dlfcn.h>


#undef  LOG_TAG
#define LOG_TAG "gpioHalStub"






#define DEVICE_NAME "/dev/gpio_ctrl"
#define MODULE_NAME "gpio_ctrl"




static int gpio_device_close(struct hw_device_t* device);


/*static int port_to_index(int port, int port_num)
{
int index = (port - 1) * 32 + port_num;
return index;
}
*/
static int gpio_ioctl(struct gpio_device_t* dev, void *data){
int ret = 0;
ALOGI("gpioHandler in Hal methods .");
    struct gpioContrData *gpioData = (struct gpioContrData *)data;
ret = ioctl(dev->fd, gpioData->gpio, gpioData->cmd);
    if (ret < 0){
ALOGE("ioctl err in gpioHalStub\n");
   close(dev->fd);
            return -1;
    }
return ret;
}


static int gpio_device_open(const struct hw_module_t* module,const char* id,struct hw_device_t** device){

if(!strcmp(id,GPIO_HARDWARE_DEVICE_ID)){

struct gpio_device_t* dev;

dev = (struct gpio_device_t*)malloc(sizeof(struct gpio_device_t));
if(!dev){
ALOGE("Faild to alloc space for gpio_device_t.");
return -EFAULT;
}
memset(dev,0,sizeof(struct gpio_device_t));

dev->common.tag = HARDWARE_DEVICE_TAG;
dev->common.version = 0;
dev->common.module = (hw_module_t*)module;
dev->common.close = gpio_device_close;
dev->gpioControl = gpio_ioctl;


if((dev->fd = open(DEVICE_NAME,O_RDWR))==-1){
ALOGE("Failed to open device file /dev/gpio_ctrl -- %s .",strerror(errno));
free(dev);
return -EFAULT;
}

*device = &(dev->common);
ALOGI("Open device file /dev/gpio_ctrl  successfully .");
return 0;
}
return -EFAULT;
}


static int gpio_device_close(struct hw_device_t* device){
struct gpio_device_t* gpio_device = (struct gpio_device_t*)device;
if(gpio_device){
close(gpio_device->fd);
free(gpio_device);
}

return 0;
}
static struct hw_module_methods_t gpio_module_methods = {
.open=gpio_device_open,
};


struct gpio_module_t HAL_MODULE_INFO_SYM 
__attribute__ ((visibility ("default"))) = {
.common = {
.tag = HARDWARE_MODULE_TAG,
.version_major = 1,
.version_minor= 0,
.id = GPIO_HARDWARE_MODULE_ID,
.name = MODULE_NAME,
.reserved = {0},
.dso = NULL,
.author = "The Android Open gpio Project",
.methods = &gpio_module_methods,
},
};

第三部分:gpioJni


/*
 * Copyright (C) 2011 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.
 */


#define LOG_NDEBUG 0


#define LOG_TAG "GpioJNI"
#include <cutils/log.h>
//#include <cutiles/misc.h>
#include <hardware/hardware.h>
#include <hardware/gpio.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>


#include "jni.h"
#include "JNIHelp.h"


namespace android
{


static inline int gpio_device_open(const hw_module_t* module,struct gpio_device_t** device){

return module->methods->open(module,GPIO_HARDWARE_DEVICE_ID,(struct hw_device_t** )device);
}


static jlong gpio_init(JNIEnv* env,jclass clazz){
gpio_module_t* module;
gpio_device_t* device;
ALOGD("Initalizing HAL stub gpio ....");

if(hw_get_module(GPIO_HARDWARE_MODULE_ID,(const struct hw_module_t**)&module)==0){
ALOGD("Device gpio found");

if(gpio_device_open(&(module->common),&device)==0){
ALOGD("Device gpio was opened");
return (jlong)device;
}
ALOGE("Failed to open Device gpio");
return -1;
}
ALOGE("Failed to find Device gpio");
return -1;
}
//--------------------------------------------------------------------------------------------
static int port_to_index(int port, int port_num)
{
if(port == -1|| port_num == -1)
return -1;
int index = (port - 1) * 32 + port_num;


return index;
}
static int getPort(const char *str)
{
if(str == NULL)
return -1;
if(*str != 'p' &&  *str != 'P')
return -1;

if( !(( *(str + 1)>='a' && *(str+1) <='z') ||(*(str+1) >='A' && *(str +1) <='Z')))
return -1;
return *(str+1) -'A' + 1;
}
static int myAtoi(const char *str)
{
int num = 0;
if(str == NULL)
return -1;
while(*str)
{
if(*str < '0' || *str >'9')
return -1;
num *= 10;
num += (*(str++)-'0');
}
return num;
}
static int getPortNum(const char *str)
{
char num[3] = {0};
if(str == NULL)
return -1;
memcpy(num,str+2,2);
return myAtoi(num);
}


//--------------------------------------------------------------------------------------------
static int gpioHandleHAL(struct gpio_device_t* device,const char* gpio,int cmd){
ALOGE("GpioHandler in JNI static local methods .");
struct gpioContrData data;
int port_num = getPortNum(gpio);
int port = getPort(gpio);
data.gpio = port_to_index(port,port_num);
if(data.gpio<0){
ALOGE("port_to_index err .");
return -1;
}
data.cmd = (char)cmd;


return device->gpioControl(device,&data);
}
static jint gpioHandleNative(JNIEnv *env, jobject thiz, jstring jGpio, jlong ptr, jint jCmd){
const char* gpio =NULL;
int state=-1;
ALOGE("GpioHandler in JNI public local methods .");
gpio_device_t* device = (gpio_device_t *)ptr;
if(!device){
ALOGE("Device gpio is not open");
state = -1;
goto err;
}

gpio = jGpio ? env->GetStringUTFChars(jGpio, NULL) : NULL;
    if (!gpio) {
        jniThrowNullPointerException(env, "gpio");
        state = -1;
goto err;
    }
if(1==jCmd||2==jCmd||3==jCmd){
state = gpioHandleHAL(device,gpio,jCmd);
}else{
ALOGE("This is a wrong command for gpio driver!");
state=-1;
goto err;
}
err:
if(gpio){
        env->ReleaseStringUTFChars(jGpio, gpio);
}
return state;
}
//------------------------------------------------------------------------------


static JNINativeMethod gMethods[] = {
    {"nativeInit", "()J", (void *)gpio_init},
    {"nativeGpioHandle", "(Ljava/lang/String;JI)I", (void *)gpioHandleNative},
};

int register_android_server_GpioService(JNIEnv *env){

    return jniRegisterNativeMethods(env, "com/android/server/GpioService",
            gMethods, NELEM(gMethods));
}


};


第四部分:gpioService

package com.android.server;


import android.content.Context;
import android.os.IGpioService;
import android.util.Slog;
public class GpioService extends IGpioService.Stub{
private static final String TAG = "GpioService";

private long mPtr = 0;
public GpioService() {
mPtr = nativeInit();

if(mPtr == 0){
Slog.e(TAG,"Faild to initialize gpio service");
}
}

public int gpioHandle(String gpio,int cmd){
Slog.i(TAG,"GpioHandler in GpioService methods .");
if(mPtr == 0){
Slog.e(TAG,"Freg service is not initialized.");
return -1;
}
//return 0;
return nativeGpioHandle(gpio,mPtr,cmd);
}


private static native long nativeInit();
private static native int nativeGpioHandle(String gpio,long mPtr,int cmd);





}

第五部分:IGpioService

package android.os;
interface IGpioService{
int gpioHandle(String gpio,int cmd);
}

第六部分:gpioTest


package android.led;
import android.app.Activity;
import android.os.ServiceManager;
import android.os.IGpioService;
import java.io.IOException;
import android.app.Activity;
import android.led.R;
import android.os.Bundle;
import android.os.RemoteException;
import android.view.View;
import android.widget.Button;
import android.util.Log;
public class MainMenu extends Activity {
private static final String LOG_TAG = "COM.LED";
private IGpioService gpioService=null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
int ret=0;
final int  cmd = 2;
    gpioService=IGpioService.Stub.asInterface(ServiceManager.getService("GpioService"));
Log.w(LOG_TAG, "gpioHandle in app ");
    try {
ret = gpioService.gpioHandle("PL10", cmd);
if(ret<0){
Log.w(LOG_TAG,"gpioHandle faild in apk");
}
if(cmd==1){
Log.w(LOG_TAG,"gpio's state is:%d"+ret);
}
} catch (RemoteException e1) {
Log.w(LOG_TAG,"gpioHandle faild in apk catch Exception--mrzhang");
e1.printStackTrace();
}
       
        setContentView(R.layout.main);
        
    /* Open the serial port */

    }
    
    @Override
    protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
   
    }
}

0 0
原创粉丝点击