转自:http://blog.csdn.net/zhangjie201412/article/details/7238202
之前,我们已经实现了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的代码。
- <pre name="code" class="cpp">static void temperature_dev_poll(struct input_polled_dev *dev)
- {
-
- 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来知道这个时候需要读数据,好,来分析下这个函数:
-
-
-
-
-
-
-
-
-
-
- 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__);
-
-
- top_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;
-
-
- if (uevent_ops && uevent_ops->filter)
- if (!uevent_ops->filter(kset, kobj)) {
- pr_debug("kobject: '%s' (%p): %s: filter function "
- "caused the event to drop!\n",
- kobject_name(kobj), kobj, __func__);
- return 0;
- }
-
-
- 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;
- }
-
-
-
- env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
- if (!env)
- return -ENOMEM;
-
-
-
- devpath = kobject_get_path(kobj, GFP_KERNEL);
- if (!devpath) {
- retval = -ENOENT;
- goto exit;
- }
-
-
-
- retval = add_uevent_var(env, "ACTION=%s", action_string);
- if (retval)
- goto exit;
-
- retval = add_uevent_var(env, "DEVPATH=%s", devpath);
- if (retval)
- goto exit;
-
- retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
- if (retval)
- goto exit;
-
-
- if (envp_ext) {
- for (i = 0; envp_ext[i]; i++) {
- retval = add_uevent_var(env, "%s", envp_ext[i]);
- if (retval)
- goto exit;
- }
- }
-
-
-
- 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;
- }
- }
-
-
-
-
-
-
-
- if (action == KOBJ_ADD)
- kobj->state_add_uevent_sent = 1;
- else if (action == KOBJ_REMOVE)
- kobj->state_remove_uevent_sent = 1;
-
-
-
- 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;
-
- #if defined(CONFIG_NET)
-
- if (uevent_sock) {
- struct sk_buff *skb;
- size_t len;
-
-
- len = strlen(action_string) + strlen(devpath) + 2;
- skb = alloc_skb(len + env->buflen, GFP_KERNEL);
- if (skb) {
- char *scratch;
-
-
- scratch = skb_put(skb, len);
- sprintf(scratch, "%s@%s", action_string, devpath);
-
-
- 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
-
-
-
- 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;
-
-
- 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);
-
- Slog.v(TAG,"------Struct function....");
- / startObserving("EVENT=temperature");
- startObserving(TEMPERATURE_UEVENT_MATCH);
- / init();
- }
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