本文主要针对feature部分进行分析,这部分代码的分析囊括了interfaces\kits\samgr\feature.h、samgr\source\feature_impl.h和samgr\source\feature.c。
在feature.h中定义了feature的基类,分析如下:
struct Feature {
const char *(*GetName)(Feature *feature);//获取feature名
//@feature指向当前特性
//@parent指向特性所属的服务
//@identity系统调度特性的标识
//特性初始化,Samgr将任务分派给服务后,该服务在其自己的任务中调用此函数
void (*OnInitialize)(Feature *feature, Service *parent, Identity identity);
//停止一个特性
void (*OnStop)(Feature *feature, Identity identity);
//处理特性消息,用于处理调用者通过IUnknown发送的请求
BOOL (*OnMessage)(Feature *feature, Request *request);
};
在feature_impl.h中声明了feature的一些方法,分析如下:
//包含feature和iUnknown的结构体FeatureImpl
struct FeatureImpl {
Feature *feature;
IUnknown *iUnknown;
};
//inline--内联函数,在函数调用处展开,可以减少函数调用的开销
//判断指定的Feature是否有效
inline static BOOL IsInvalidFeature(Feature *feature)
{
return (feature == NULL || feature->GetName == NULL || feature->OnInitialize == NULL ||
feature->OnMessage == NULL || feature->OnStop == NULL);
}
//判断指定的IUnknown是否有效
inline static BOOL IsInvalidIUnknown(IUnknown *iUnknown)
{
return (iUnknown == NULL || iUnknown->QueryInterface == NULL || iUnknown->AddRef == NULL ||
iUnknown->Release == NULL);
}
//添加接口
BOOL SAMGR_AddInterface(FeatureImpl *featureImpl, IUnknown *iUnknown);
//删除接口
IUnknown *SAMGR_DelInterface(FeatureImpl *featureImpl);
//获取接口
IUnknown *SAMGR_GetInterface(FeatureImpl *featureImpl);
//判断featureImpl的接口是否为NULL
BOOL SAMGR_IsNoInterface(FeatureImpl *featureImpl);
//创建FeatureImpl对象
FeatureImpl *FEATURE_CreateInstance(Feature *feature);
在feature.c中对方法进行了实现,分析如下:
添加接口操作
/*
函数功能:为featureImpl添加接口
函数返回:添加成功返回true,添加失败返回false
*/
BOOL SAMGR_AddInterface(FeatureImpl *featureImpl, IUnknown *iUnknown)
{
//参数检查
if (featureImpl == NULL || iUnknown == NULL || featureImpl->iUnknown != NULL) {
return FALSE;
}
//将iUnknown添加到featureImpl中
featureImpl->iUnknown = iUnknown;
return TRUE;
}
删除接口
/*
函数功能:删除featureImpl的接口
函数返回:删除成功返回被删除的对象IUnknown,删除失败返回NULL
函数描述:删除指定featureImpl的接口,将featureImpl的iUnknown成员赋值为NULL,并返回iUnknown
*/
IUnknown *SAMGR_DelInterface(FeatureImpl *featureImpl)
{
//参数检查
if (featureImpl == NULL) {
return NULL;
}
//保存将被删除的iUnknown接口
IUnknown *iUnknown = featureImpl->iUnknown;
//置为NULL,删除的作用
featureImpl->iUnknown = NULL;
//返回被删除的接口对象
return iUnknown;
}
获取接口
//返回指定featureImpl的成员iUnknown
IUnknown *SAMGR_GetInterface(FeatureImpl *featureImpl)
{
//参数检查
if (featureImpl == NULL) {
return NULL;
}
//返回featureImpl的接口
return featureImpl->iUnknown;
}
判断接口是否存在
//判断featureImpl的接口是否为NULL,返回true or false
BOOL SAMGR_IsNoInterface(FeatureImpl *featureImpl)
{
return (BOOL)(featureImpl == NULL || featureImpl->iUnknown == NULL);
}
创建featureimpl实例
//为指定的feature创建一个FeatureImpl实例
FeatureImpl *FEATURE_CreateInstance(Feature *feature)
{
if (feature == NULL) {
return NULL;
}
//分配内存
FeatureImpl *featureImpl = (FeatureImpl *)SAMGR_Malloc(sizeof(FeatureImpl));
if (featureImpl == NULL) {
//内存分配失败
return NULL;
}
//赋值,默认iUnknown为NULL
featureImpl->feature = feature;
featureImpl->iUnknown = NULL;
return featureImpl;
}
总是有很多小伙伴反馈说:OpenHarmony开发不知道学习哪些技术?不知道需要重点掌握哪些OpenHarmony开发知识点? 为了解决大家这些学习烦恼。在这准备了一份很实用的鸿蒙全栈开发学习路线与学习文档给大家用来跟着学习。
针对一些列因素,整理了一套纯血版鸿蒙(HarmonyOS Next)全栈开发技术的学习路线,包含了鸿蒙开发必掌握的核心知识要点,内容有(OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、OpenHarmony驱动开发、系统定制移植……等)技术知识点。
OpenHarmony 开发环境搭建:https://gitcode.com/HarmonyOS_MN/733GH/overview
《OpenHarmony源码解析》
搭建开发环境
系统架构分析
- 构建子系统
- 启动流程
- 子系统
- 分布式任务调度子系统
- 分布式通信子系统
- 驱动子系统
- ……