Android工程模式下需要一个方便的途径去访问底层的硬件信息,以方便软件和测试同事更加简单的知道硬件信息;Android 从底层本质上说是 Linux,因此可以把 Android 当作 Linux 来访问,从而达到访问系统信息的目的;
参考文档:http://blog.csdn.net/bingqingsuimeng/article/details/7919397
proc 文件系统是Linux内核开发人员为了减少系统调用的复杂性而引入的一种特殊文件系统。
有了这种文件系统,用户只需要像查看文本文件一样就能够读取内核中的各种数据,给开发人员和系统管理员带来很大的便利。
很多Linux常用命令都是依靠分析/proc目录下的文件来运行的,比如 ls,ps 等。
1、读取CPU和内存等信息。
分析 /proc/cpuinfo 这个文件,就能获得 CPU信息。
adb shell
# cat /proc/cpuinfo
分析 /proc/meminfo ,就能获得内存信息
adb shell
# cat /proc/meminfo
adb shell
#cat proc/kmsg 抓取kernel抛出的调试信息
注:用 Android SDK 的 ActivityManager.getMemoryInfo(ActivityManager.MemoryInfo) 也能获得当前可用内存的大小
你如果使用Linux,可以看到 /proc目录还有很多文件,你可以用 man proc 命令来学习 proc 文件系统。
linux 2.6 内核中引入了 sysfs 文件系统,是用户空间与内核空间进行交互的一个媒介。
比起古老的 proc 文件系统,它是基于内核的数据结构,因此组织结构上更加严密。
它的设计使内核的信息更易获取,而且更加清晰。内核空间与用户空间的映射关系如下表所示:
内核空间(internel) ——->用户空间(externel)
内核对象(kernel objects) ——->目录(directories)
对象属性(object attributes) ——->普通文件(regular files)
对象关系(object relationshiops) ——->符号链接(symbolic links)
先使用kobject_create_and_add() 創建目錄
再透過sysfs_create_group(example_kobj, &attr_group) 創建目录下的属性文件组, 其中 attr_group 是我们自己定义的属性相关文件,
static int __init example_init(void)
{
int retval;
/*
* Create a simple kobject with the name of “kobject_example",* located under /sys/kernel/
** As this is a simple directory, no uevent will be sent to userspace. That is why this function should not be used for any type of dynamic kobjects,
where the name and number are* not known ahead of time.
*/
example_kobj = kobject_create_and_add(“kobject_example", kernel_kobj);
if (!example_kobj)
return -ENOMEM;
/* Create the files associated with this kobject */
retval = sysfs_create_group(example_kobj, &attr_group);
if (retval)
kobject_put(example_kobj);
return retval;
}
通过它,我们描述了目录里具有的属性文件,以及各文件的 show 和 store 函数。
对于本例来说,可用一个属性数组 attrs 描述这一属性组 attr_group。
#ifdef CONFIG_GET_HARDWARE_IC_TYPE_SUPPORT struct kobject *hwinfo_kobj; static char main_buf[10]; static char sub_buf[10]; static ssize_t main_camera_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { return sprintf(buf, "%s\n", main_buf); //printk("@fantasy --camera-- set main camera id %s!\n",&main_buf); } static ssize_t sub_camera_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { return sprintf(buf, "%s\n", sub_buf); //printk("@fantasy --camera-- set sub camera id %s!\n",&sub_buf); } static struct kobj_attribute main_attr = { .attr = { .name = "main_camera", .mode = S_IRUGO, }, .show = &main_camera_show, }; static struct kobj_attribute sub_attr = { .attr = { .name = "sub_camera", .mode = S_IRUGO, }, .show = &sub_camera_show, }; static struct attribute *camera_attrs[] = { &main_attr.attr, &sub_attr.attr, NULL }; static struct attribute_group camera_attr_group = { .attrs = camera_attrs, }; static void camera_get_ic_type_init(void) { int ret; hwinfo_kobj = kobject_create_and_add("hw_info", NULL); if (hwinfo_kobj) ret = sysfs_create_group(hwinfo_kobj, &camera_attr_group); if (!hwinfo_kobj || ret) pr_err("failed to create hw info kobj\n"); } #endif //end of CONFIG_GET_HARDWARE_IC_TYPE_SUPPORT在摄像头驱动identify的过程中给文件节点赋值,然后在应用层读取硬件参数的文件节点:
//Main Camera File MCamera_TYPE_FILE = new File("/sys/hw_info/main_camera"); String MCamera_type_string = null; if (MCamera_TYPE_FILE.exists()) { try { FileReader command = new FileReader(MCamera_TYPE_FILE); BufferedReader reader = new BufferedReader(command); MCamera_type_string = reader.readLine(); if(null == MCamera_type_string){ MCamera_type_string = "UNKNOWN"; } command.close(); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else{ MCamera_type_string = "UNKNOWN"; } //Sub Camera File SCamera_TYPE_FILE = new File("/sys/hw_info/sub_camera"); String SCamera_type_string = null; if (SCamera_TYPE_FILE.exists()) { try { FileReader command = new FileReader(SCamera_TYPE_FILE); BufferedReader reader = new BufferedReader(command); SCamera_type_string = reader.readLine(); if(null == SCamera_type_string){ SCamera_type_string = "UNKNOWN"; } command.close(); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else{ SCamera_type_string = "UNKNOWN"; }
Android工程模式下读取硬件信息,布布扣,bubuko.com
原文:http://blog.csdn.net/fantasyhujian/article/details/23562559