3、 bootstrap_lite服务启动引导部件实现原理之bootstrap_service

在文件base\startup\bootstrap_lite\services\source\bootstrap_service.h中定义了2个宏函数INIT_APP_CALL和INIT_TEST_CALL,分别用来调用代码段&__zinitcall_app_XXX_start、&__zinitcall_app_XXX_end和&__zinitcall_test_start、&__zinitcall_test_end之间的初始化启动函数。bootstrap_service.h文件中的宏和base\startup\bootstrap_lite\services\source\core_main.h文件中的宏类似,不再一一分析。

3.1 结构体struct Bootstrap

在文件base\startup\bootstrap_lite\services\source\bootstrap_service.c中定义了结构体struct Bootstrap,如下代码⑵处。其中结构体中的INHERIT_SERVICE定义在文件foundation/distributedschedule/samgr_lite/interfaces/kits/samgr/service.h,见代码片段⑴处。

⑴   #define INHERIT_SERVICE                                          \
        const char *(*GetName)(Service * service);                   \
        BOOL (*Initialize)(Service * service, Identity identity);    \
        BOOL (*MessageHandle)(Service * service, Request * request); \
        TaskConfig (*GetTaskConfig)(Service * service)

    ......
⑵  typedef struct Bootstrap {
        INHERIT_SERVICE;
        Identity identity;
        uint8 flag;
    } Bootstrap;

 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

结构体Identity定义在文件foundation\distributedschedule\samgr_lite\interfaces\kits\samgr\message.h中,用于标识一个服务或特性。

/**
 * @brief Identifies a service and feature.
 *
 * You can use this structure to identity a {@link IUnknown} feature to which messages will be
 * sent through the asynchronous function of {@link IUnknown}. \n
 *
 */
struct Identity {
    /** Service ID */
    int16 serviceId;
    /** Feature ID */
    int16 featureId;
    /** Message queue ID */
    MQueueId queueId;
};

 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}"> class="hide-preCode-box">

3.2 Init(void)函数

讲解移植适配示例时,我们已经知道,bootstrap_lite部件会编译//base/startup/bootstrap_lite/services/source/bootstrap_service.c,该文件通过函数宏SYS_SERVICE_INIT将Init()函数符号灌段到__zinitcall_sys_service_start和__zinitcall_sys_service_end代码段中,代码片段如下。下文再详细分析GetName、Initialize、MessageHandle和GetTaskConfig函数。

  static const char *GetName(Service *service);
  static BOOL Initialize(Service *service, Identity identity);
  static TaskConfig GetTaskConfig(Service *service);
  static BOOL MessageHandle(Service *service, Request *request);

  static void Init(void)
  {
      static Bootstrap bootstrap;
      bootstrap.GetName = GetName;
      bootstrap.Initialize = Initialize;
      bootstrap.MessageHandle = MessageHandle;
      bootstrap.GetTaskConfig = GetTaskConfig;
      bootstrap.flag = FALSE;
      SAMGR_GetInstance()->RegisterService((Service *)&bootstrap);
  }
  SYS_SERVICE_INIT(Init);

 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}"> class="hide-preCode-box">

3.3 GetName和Initialize函数

GetName函数代码如下,其中BOOTSTRAP_SERVICE定义在文件foundation\distributedschedule\samgr_lite\interfaces\kits\samgr\samgr_lite.h中,取值为"Bootstrap",表示启动引导服务。

static const char *GetName(Service *service)
{
    (void)service;
    return BOOTSTRAP_SERVICE;
}

 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

Initialize函数定义如下,用于设置启动引导服务的标识信息。

static BOOL Initialize(Service *service, Identity identity)
{
    Bootstrap *bootstrap = (Bootstrap *)service;
    bootstrap->identity = identity;
    return TRUE;
}

 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

3.4 MessageHandle函数和GetTaskConfig函数

MessageHandle函数和GetTaskConfig函数代码如下,MessageHandle函数用于处理各种请求,请求消息编号定义request->msgId定义在文件foundation\distributedschedule\samgr_lite\interfaces\kits\samgr\samgr_lite.h中的枚举enum BootMessage,如下⑴处所示。在MessageHandle函数中,当请求的消息编号为BOOT_SYS_COMPLETED系统服务初始化完成时,检查应用服务是否加载。当应用没有加载时,执行INIT_APP_CALL宏函数调用zInit代码段上的应用服务和应用特性初始化函数。然后执行⑶,调用函数SAMGR_SendResponseByIdentity进行响应。GetTaskConfig函数用于获取任务配置。

⑴  typedef enum BootMessage {
        /** Message indicating that the core system service is initialized */
        /** 标识核心系统服务初始化完成 */
        BOOT_SYS_COMPLETED,
        /** Message indicating that the system and application-layer services are initialized */
        /** 标识应用层服务初始化完成 */
        BOOT_APP_COMPLETED,
        /** Message indicating service registration during running */
        /** 标识运行时的服务注册 */
        BOOT_REG_SERVICE,
        /** Maximum number of message IDs */
        /** 标识消息最大值,butt是烟蒂;屁股;枪托的意思,表示尾部吧 */
        BOOTSTRAP_BUTT
    } BootMessage;

    ......

  static BOOL MessageHandle(Service *service, Request *request)
  {
      Bootstrap *bootstrap = (Bootstrap *)service;
      switch (request->msgId) {
          case BOOT_SYS_COMPLETED:
⑵            if ((bootstrap->flag & LOAD_FLAG) != LOAD_FLAG) {
                  INIT_APP_CALL(service);
                  INIT_APP_CALL(feature);
                  bootstrap->flag |= LOAD_FLAG;
              }
⑶            (void)SAMGR_SendResponseByIdentity(&bootstrap->identity, request, NULL);
              break;

          case BOOT_APP_COMPLETED:
              (void)SAMGR_SendResponseByIdentity(&bootstrap->identity, request, NULL);
              break;

          case BOOT_REG_SERVICE:
              (void)SAMGR_SendResponseByIdentity(&bootstrap->identity, request, NULL);
              break;

          default:
              break;
      }
      return TRUE;
  }

  static TaskConfig GetTaskConfig(Service *service)
  {
      (void)service;
      // The bootstrap service uses a stack of 2 KB (0x800) in size and a queue of 20 elements.
      // You can adjust it according to the actual situation.
      TaskConfig config = {LEVEL_HIGH, PRI_NORMAL, 0x800, 20, SHARED_TASK};
      return config;
  }

 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}"> class="hide-preCode-box">

小结

本文介绍了startup子系统之bootstrap_lite服务启动引导部件的移植适配案例及原理。

如果大家想更加深入的学习 OpenHarmony 开发的内容,不妨可以参考以下相关学习文档进行学习,助你快速提升自己:

OpenHarmony 开发环境搭建:https://qr18.cn/CgxrRy

《OpenHarmony源码解析》:https://qr18.cn/CgxrRy

系统架构分析:https://qr18.cn/CgxrRy

OpenHarmony 设备开发学习手册:https://qr18.cn/CgxrRy

在这里插入图片描述

OpenHarmony面试题(内含参考答案):https://qr18.cn/CgxrRy

写在最后

data-report-view="{"mod":"1585297308_001","spm":"1001.2101.3001.6548","dest":"https://blog.csdn.net/maniuT/article/details/139773308","extend1":"pc","ab":"new"}">> id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"> class="blog_extension blog_extension_type2" id="blog_extension"> class="extension_official" data-report-click="{"spm":"1001.2101.3001.6471"}" data-report-view="{"spm":"1001.2101.3001.6471"}"> class="blog_extension_card_left"> class="blog_extension_card_cont"> 鸿蒙开发学习资料领取!!! class="blog_extension_card_cont_r"> 微信名片
注:本文转载自blog.csdn.net的沧海一笑-dj的文章"https://blog.csdn.net/dengjin20104042056/article/details/96195535"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接

评论记录:

未查询到任何数据!