本文均属自己阅读源码的点滴总结,转账请注明出处谢谢。
欢迎和大家交流。qq:1037701636 email:[email protected]
Android源码版本Version:4.2.2; 硬件平台 全志A31
在介绍FrameWork是不得不提的是HAL(硬件抽象层)一般是用来和特点的硬件平台进行交互的,所以不同的android平台主要的区别也就是在这个部分,HAL的好处在于一个FrameWork可以调用不同的HAL,只需要相关的HAL满足一定接口规范即可。另一方面HAL的好处是可以屏蔽相关对底层硬件操作的应用代码。
网上对HAL的介绍内容已经很多,这里就简单和大家分享我所深入去了解的HAL层的相关规范。
核心的几个API:hw_get_module(), hw_get_module_by_class(), load()就这么简单,核心在hw_get_module_by_class(),来看其实现的过程代码:
int hw_get_module_by_class(const char *class_id, const char *inst, const struct hw_module_t **module){ int status; int i; const struct hw_module_t *hmi = NULL; char prop[PATH_MAX]; char path[PATH_MAX]; char name[PATH_MAX]; if (inst) snprintf(name, PATH_MAX, "%s.%s", class_id, inst); else strlcpy(name, class_id, PATH_MAX); /* * Here we rely on the fact that calling dlopen multiple times on * the same .so will simply increment a refcount (and not load * a new copy of the library). * We also assume that dlopen() is thread-safe. */ /* Loop through the configuration variants looking for a module */ for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) { if (i < HAL_VARIANT_KEYS_COUNT) { if (property_get(variant_keys[i], prop, NULL) == 0) { continue; } snprintf(path, sizeof(path), "%s/%s.%s.so", HAL_LIBRARY_PATH2, name, prop); if (access(path, R_OK) == 0) break;//设备厂商特定的HAL库所在目录/vendor/lib/hw snprintf(path, sizeof(path), "%s/%s.%s.so", HAL_LIBRARY_PATH1, name, prop);//通用的HAL库/system/lib/hw if (access(path, R_OK) == 0) break; } else { snprintf(path, sizeof(path), "%s/%s.default.so", HAL_LIBRARY_PATH1, name);//默认库 if (access(path, R_OK) == 0) break; } } status = -ENOENT; if (i < HAL_VARIANT_KEYS_COUNT+1) { /* load the module, if this fails, we're doomed, and we should not try * to load a different variant. */ status = load(class_id, path, module); } return status;}
step1:关注变量variant_keys, 通过系统属性获取函数property_get()来获取下面4个属性变量在内存的系统属性中维护的数值(其中系统属性的创建和维护主要由init进程来完成)
static const char *variant_keys[] = { "ro.hardware", /* This goes first so that it can pick up a different file on the emulator. */ "ro.product.board", "ro.board.platform", "ro.arch"};
先来看ro.hardware,它一般是指定了系统板级的硬件如我这里的sun6i, 那这个属性变量在何处设置呢,首先出现的地方是在init进程里面,依次调用如下:
1.get_hardware_name()——> fd = open("/proc/cpuinfo", O_RDONLY):搜索cpuinfo里面的hardware字段,有的话就保存在hardware中
2.process_kernel_cmdline——>export_kernel_boot_props():这里会对boot启动时设置的属性值查询,对hardware有如下代码:
/* if this was given on kernel command line, override what we read * before (e.g. from /proc/cpuinfo), if anything */ pval = property_get("ro.boot.hardware"); if (pval) strlcpy(hardware, pval, sizeof(hardware)); property_set("ro.hardware", hardware);
如果在boot时设置了ro.boot.hardware那么hardware就重载从/proc/cpuinfo里面读取的数值,这样整个ro.hardware的硬件系统属性值就配置好了。
对于"ro.product.board"、 "ro.board.platform"、"ro.arch"这3个属性变量,就要集中到android的属性文件上来了,android系统集中的属性文件有以下几个:
#define PROP_PATH_SYSTEM_BUILD "/system/build.prop"
#define PROP_PATH_SYSTEM_DEFAULT "/system/default.prop"
#define PROP_PATH_LOCAL_OVERRIDE "/data/local.prop"
queue_builtin_action(property_service_init_action, "property_service_init");//属性服务的初始化
void start_property_service(void){ int fd; load_properties_from_file(PROP_PATH_SYSTEM_BUILD); load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT);可以看到会解析不同路径下的prop属性文件,包括下面要介绍的这个buil.prop
3 ro.build.id=JDQ39 4 ro.build.display.id=fiber_3g-eng 4.2.2 JDQ39 20140110 test-keys 5 ro.build.version.incremental=20140110 6 ro.build.version.sdk=17 7 ro.build.version.codename=REL 8 ro.build.version.release=4.2.2 9 ro.build.date=2014年 01月 10日 星期五 16:03:07 CST 10 ro.build.date.utc=1389340987 11 ro.build.type=eng 12 ro.build.user=root 13 ro.build.host=linux 14 ro.build.tags=test-keys 15 ro.product.model=Softwinner 16 ro.product.brand=Softwinner 17 ro.product.name=fiber_3g 18 ro.product.device=fiber-3g 19 ro.product.board=exdroid 20 ro.product.cpu.abi=armeabi-v7a 21 ro.product.cpu.abi2=armeabi 22 ro.product.manufacturer=unknown 23 ro.product.locale.language=en 24 ro.product.locale.region=US 25 ro.wifi.channels= 26 ro.board.platform=fiber....
#define HAL_LIBRARY_PATH1 "/system/lib/hw"
#define HAL_LIBRARY_PATH2 "/vendor/lib/hw"
handle = dlopen(path, RTLD_NOW);