Android模拟器学framework和driver之传感器篇6(Android 通过JNI连接驱动层和framework)

之前,我们已经实现了android HAL层,在android模拟器上移植了一个虚拟的temperature sensor,我之前在模拟器上也移植了backlight,RTC等驱动,都能在应用层得到需要的数据,其实自己想学点东西,给自己布置点任务还是不错的,通过模拟器也可以来学习linux 中的device driver,这部分在今后的博客中我会涉及到,这篇blog我主要是想在之前所做的东西的基础上来实现android 的另外一种架构层的实现。

driver--->framework JNI(server)---->framework JAVA(server)

我们的任务分为如下几个步骤:

=====================================================================

||  1.在temperature sensor驱动中添加sysfs中添加一个只读的文件让JNI 读数据。                    

||  2.在temperature sensor驱动中添加uevent通知user space数据发生了改变需要读取。        

||  3.在android framework中添加jni server读取底层数据,封装操作函数。                                  

||  4.在android framework中添加uevent observer来监听uevent事件的变化。                              

||  5.(可选)在java server中添加系统ACTION,然后编写android apk来接收ACTION。          

======================================================================

下面会根据这个步骤来展开我们的学习!!!!!!!


之前的代码可以参照之前的文章。

1.在temperature sensor驱动中添加sysfs中添加一个只读的文件让JNI 读数据。      

首先来看的还是驱动,我们在之前的驱动中加入如下代码:

首先是添加value文件节点,可以参照name是如何做的 

static int tempValue;

static ssize_t temperature_show_value(struct device *dev,
	struct device_attribute *attr, char *buf)
{	
	return sprintf(buf, "%d\n", tempValue);
}

static IIO_DEVICE_ATTR(name, S_IRUGO, temperature_show_name, NULL,0);
static IIO_DEVICE_ATTR(value, S_IRUGO, temperature_show_value, NULL,0);

static struct attribute *temperature_attributes[] = {
	&iio_dev_attr_name.dev_attr.attr,
	&iio_dev_attr_value.dev_attr.attr,
	NULL
};

这样的话会生成/sys/bus/iio/devices/device0/value 可以read 这个节点来得到温度值。

2.在temperature sensor驱动中添加uevent通知user space数据发生了改变需要读取。

接下来是uevent的添加,这边先把代码贴上,我们在分析uvent的代码。

static void temperature_dev_poll(struct input_polled_dev *dev)
{
	//++++add uevent in driver
	char *buf;
	char *envp[3];
	sysfs_notify(&dev->input->dev.kobj,NULL, "value");
	buf = kmalloc(32,GFP_ATOMIC);
	if(!buf){
		printk(KERN_ERR "%s kmalloc failed\n", __func__);
		return;
	}	
	envp[0] = "NAME=temperature";
	snprintf(buf , 32 , "TEMPERATURE=%d",tempValue);
	envp[1] = buf;
	envp[2] =NULL;
	kobject_uevent_env(&dev->input->dev.kobj,KOBJ_CHANGE,envp);
	kfree(buf);
	//----------
	printk(KERN_INFO "Current Temperature: %d\n",tempValue);
	if((tempValue++)==100)
		tempValue=0;
	input_event(dev->input,EV_ABS,ABS_PRESSURE,tempValue);
	input_sync(dev->input);
}




其实大家可以看到这边最主要的就是kobject_uevent_env函数,这个函数会通知user space 环境变量发生了变化,然后user space可以通过监听uevent来知道这个时候需要读数据,好,来分析下这个函数:


/**
 * kobject_uevent_env - send an uevent with environmental data
 *
 * @action: action that is happening
 * @kobj: struct kobject that the action is happening to
 * @envp_ext: pointer to environmental data
 *
 * Returns 0 if kobject_uevent() is completed with success or the
 * corresponding error when it fails.
 */
int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
		       char *envp_ext[])
{
	struct kobj_uevent_env *env;
	const char *action_string = kobject_actions[action];
	const char *devpath = NULL;
	const char *subsystem;
	struct kobject *top_kobj;
	struct kset *kset;
	struct kset_uevent_ops *uevent_ops;
	u64 seq;
	int i = 0;
	int retval = 0;

	pr_debug("kobject: '%s' (%p): %s\n",
		 kobject_name(kobj), kobj, __func__);

	/* search the kset we belong to */
	top_kobj = kobj;
	 /*找到其所属的kset容器,如果没找到就从其父kobj找,一直继续下去,直到父kobj不存在*/
	while (!top_kobj->kset && top_kobj->parent)
		top_kobj = top_kobj->parent;

	if (!top_kobj->kset) {
		pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
			 "without kset!\n", kobject_name(kobj), kobj,
			 __func__);
		return -EINVAL;
	}

	kset = top_kobj->kset;
	uevent_ops = kset->uevent_ops;
	/*回调uevent_ops->filter()函数,我们这边是dev_uevent_filter()函数,主要是检查是否是uevent_suppress*/
	/* skip the event, if the filter returns zero. */
	if (uevent_ops && uevent_ops->filter)
		if (!uevent_ops->filter(kset, kobj)) {	//如果不成功,就是uevent_suppress,则直接返回
			pr_debug("kobject: '%s' (%p): %s: filter function "
				 "caused the event to drop!\n",
				 kobject_name(kobj), kobj, __func__);
			return 0;
		}
	/*回调uevent_ops->name(),来获取bus或者class的名字,这边是iio bus,所以bus的名字是“iio”*/
	/* originating subsystem */
	if (uevent_ops && uevent_ops->name)
		subsystem = uevent_ops->name(kset, kobj);
	else
		subsystem = kobject_name(&kset->kobj);
	if (!subsystem) {
		pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
			 "event to drop!\n", kobject_name(kobj), kobj,
			 __func__);
		return 0;
	}
	
	/*获得用于存放环境变量的buffer*/
	/* environment buffer */
	env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
	if (!env)
		return -ENOMEM;
	
	/*获取该kobj在sysfs的路径,通过遍历其父kobj来获得,这边是/sys/devices/platform/android-temperature*/
	/* complete object path */
	devpath = kobject_get_path(kobj, GFP_KERNEL);
	if (!devpath) {
		retval = -ENOENT;
		goto exit;
	}

	/*添加ACTION环境变量,这边是“change” 命令,这里提供了add,remove,change,move,online,offline六种命令*/
	/* default keys */
	retval = add_uevent_var(env, "ACTION=%s", action_string);
	if (retval)
		goto exit;
	/*添加DEVPATH环境变量,这边是/sys/devices/platform/android-temperature 之后会用到*/
	retval = add_uevent_var(env, "DEVPATH=%s", devpath);
	if (retval)
		goto exit;
	/*添加subsystem环境变量,这里是input*/
	retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
	if (retval)
		goto exit;

	/* keys passed in from the caller */
	if (envp_ext) {
		for (i = 0; envp_ext[i]; i++) {
			retval = add_uevent_var(env, "%s", envp_ext[i]);
			if (retval)
				goto exit;
		}
	}

	//回调uevent_ops->event(),输出一些环境变量
	/* let the kset specific function add its stuff */
	if (uevent_ops && uevent_ops->uevent) {
		retval = uevent_ops->uevent(kset, kobj, env);
		if (retval) {
			pr_debug("kobject: '%s' (%p): %s: uevent() returned "
				 "%d\n", kobject_name(kobj), kobj,
				 __func__, retval);
			goto exit;
		}
	}

	/*
	 * Mark "add" and "remove" events in the object to ensure proper
	 * events to userspace during automatic cleanup. If the object did
	 * send an "add" event, "remove" will automatically generated by
	 * the core, if not already done by the caller.
	 */
	if (action == KOBJ_ADD)
		kobj->state_add_uevent_sent = 1;
	else if (action == KOBJ_REMOVE)
		kobj->state_remove_uevent_sent = 1;

	/* we will send an event, so request a new sequence number */
	/*增加event序列号的值,并输出到环境变量的buffer。该序列号可以从sys/kernel/uevent_seqnum属性文件读取*/
	spin_lock(&sequence_lock);
	seq = ++uevent_seqnum;
	spin_unlock(&sequence_lock);
	retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq);
	if (retval)
		goto exit;
/*如果配置了网络,那么就会通过netlink socket向用户控件发送环境变量,而用户控件则通过netlink socket接收,然后采取一系列的动作。这种机制目前在udev中,也就是pc机系统中,我们这边不是PC机所以不在分析*/
#if defined(CONFIG_NET)
	/* send netlink message */
	if (uevent_sock) {
		struct sk_buff *skb;
		size_t len;

		/* allocate message with the maximum possible size */
		len = strlen(action_string) + strlen(devpath) + 2;
		skb = alloc_skb(len + env->buflen, GFP_KERNEL);
		if (skb) {
			char *scratch;

			/* add header */
			scratch = skb_put(skb, len);
			sprintf(scratch, "%s@%s", action_string, devpath);

			/* copy keys to our continuous event payload buffer */
			for (i = 0; i < env->envp_idx; i++) {
				len = strlen(env->envp[i]) + 1;
				scratch = skb_put(skb, len);
				strcpy(scratch, env->envp[i]);
			}

			NETLINK_CB(skb).dst_group = 1;
			retval = netlink_broadcast(uevent_sock, skb, 0, 1,
						   GFP_KERNEL);
		} else
			retval = -ENOMEM;
	}
#endif
/*对于嵌入式系统来说,busybox采用的是mdev,在系统启动脚本rcS中会使用echo /sbin/mdev > /proc/sys/kernelhotplug命令。。。。*/

	/* call uevent_helper, usually only enabled during early boot */
	if (uevent_helper[0]) {
		char *argv [3];

		argv [0] = uevent_helper;
		argv [1] = (char *)subsystem;
		argv [2] = NULL;
		retval = add_uevent_var(env, "HOME=/");
		if (retval)
			goto exit;
		retval = add_uevent_var(env,
					"PATH=/sbin:/bin:/usr/sbin:/usr/bin");
		if (retval)
			goto exit;
		
		//呼叫应用程序来处理,UMH_WAIT_EXEC表明等待应用程序处理完
		retval = call_usermodehelper(argv[0], argv,
					     env->envp, UMH_WAIT_EXEC);
	}

exit:
	kfree(devpath);
	kfree(env);
	return retval;
}
EXPORT_SYMBOL_GPL(kobject_uevent_env);



这边我们要关注的就是ACTION还有就是环境变量参数,注意,我们这边使用的kobject是input->dev.kobj,所以这边的DEVPATH和SUBSYSTEM都是跟input subsystem有关的。

这里把uevent传到user space了,下面来看user space是怎么做的,涉及的代码有:

hardware/libhardware_legacy/uevent
frameworks/base/core/jni/android_os_UEventObserver.cpp
frameworks/base/core/java/android/os/UEventObserver.java
frameworks/base/services/java/com/android/server/SystemServer.java

这边我提最重要的,就是hardware/libhardware_legacy/uevent/uevent.c 发送一个socket广播把这个uevent事件发送出去

    s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
    if(s < 0)
        return 0;

    setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));

    if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
        close(s);
        return 0;
    }

然后就是在framework中通过jni进行封装,使得java中也能通过sokect来监听uevent。


3.在android framework中添加jni server读取底层数据,封装操作函数。                 

下面我们来实现在jni中读取文件系统中的节点来获得温度的值。

下面是2个文件的下载地址,一个是jni一个是java(com_android_server_TemperatureObserver.cpp,TemperatureObserver.java)

http://download.csdn.net/detail/zhangjie201412/4049098

其实在这边用到的东西也不是很多,最多就是jni中的一些规则:

在jni中获得java中的域,在jni中注册method函数,在jni中注册等方法,这边很简单,就是open节点,然后读数据,获得java中的变量的fieldID 然后把数据传给java中的变量,最后封装native函数,然后再java中调用。

1.获得java中的域:

	jclass clazz = env->FindClass("com/android/server/TemperatureObserver");
	if(clazz == NULL)
	{
		LOGE("Can't find com/android/server/TemperatureObserver");
		return -1;
	}
	
	mTemperatureValueID = 
		env->GetFieldID(clazz , "mTemperatureValue" , "I");


2.open节点读数据,再把数据传到java的变量中:

	jclass clazz = env->FindClass("com/android/server/TemperatureObserver");
	if(clazz == NULL)
	{
		LOGE("Can't find com/android/server/TemperatureObserver");
		return -1;
	}
	
	mTemperatureValueID = 
		env->GetFieldID(clazz , "mTemperatureValue" , "I");


4.在android framework中添加uevent observer来监听uevent事件的变化。  

3.在java中注册uevent监听

	public TemperatureObserver(Context context) {
		mContext = context;
		PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
	        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TemperatureObserver");
	        mWakeLock.setReferenceCounted(false);
		//++++add log
		Slog.v(TAG,"------Struct function....");
//	        startObserving("EVENT=temperature");
		startObserving(TEMPERATURE_UEVENT_MATCH);
//        	init();  // set initial status
	}

4.在java中实现onUEvent,并且调用java原生接口

	@Override
	public void onUEvent(UEventObserver.UEvent event) {
		if (LOG) Slog.v(TAG, "Temperature UEVENT: " + event.toString());

	        try {
        		update(event.get("NAME"), Integer.parseInt(event.get("TEMPERATURE")));
	        } catch (NumberFormatException e) {
        		Slog.e(TAG, "Could not parse switch state from event " + event);
	        }
	}

	private native void native_temperature_update();

	private synchronized final void update(String newName, int newValue){
		if(LOG)
			Slog.v(TAG,"NAME: "+newName+"   VALUE: "+newValue);
		native_temperature_update();
		Slog.v(TAG,"-----value-----"+mTemperatureValue);
	}


至此,我们的分析就结束了,关于jni的用法我就不多说了,提供一些链接,大家可以拿作参考:

http://blog.csdn.net/thl789/article/details/7212822

http://blog.csdn.net/furongkang/article/details/6857610


这一部分到此结束,希望写的这些对大家有点帮助。




   

你可能感兴趣的:(java,android,struct,socket,jni,action)