当前位置:网站首页>Summer Challenge ohos build custom service practice
Summer Challenge ohos build custom service practice
2022-06-28 16:17:00 【51CTO】
This article is participating in the starlight project 3.0– Summer Challenge
author : Caochangyan
Facing the whole scene 、 Full connection 、 Under the background of full intelligence era ,OpenHarmony It will be supported by more and more developers , In different scenes , Some non essential subsystems or components will be tailored according to actual requirements , New subsystems or components will also be added . If you want to add subsystems or add services / Components , I hope this article can give you some enlightenment .
1 Basic concepts
Before introducing custom services , First, a few concepts are briefly introduced :
① There are three basic concepts in Hongmeng system , They are Subsystem (subsystems), Components (components), function (features).
OpenHarmony Overall compliance Layered design , From bottom to top is : Kernel layer 、 System service layer 、 Framework layer and application layer .

The system functions according to “ System > Subsystem > Components ” Step by step . Subsystem is a logical concept , It consists of corresponding components . A component is a further breakdown of a subsystem , Reusable software units , It contains the source code 、 The configuration file 、 Resource files and compilation scripts ; Can build independently , Binary integration , It is a binary unit with independent verification capability .
for instance , Hongmeng ( System ) --> multimedia ( Subsystem ) --> Audio ( Components )–> collection ( function )
②IPC(Inter-Process Communication) Mechanism : Use Binder drive , Used for cross process communication within the device .IPC The client is usually used - The server (Client-Server) Model , Service requester (Client) Available service providers (Server) Agent for (Proxy), The data communication between processes is realized by reading and writing data through this agent .
Usually ,Server Will register the system capability first (System Ability) To the System Capability Manager (System Ability Manager, abbreviation saMgr) in ,saMgr Responsible for managing these SA And to Client Provide relevant interfaces .Client With a specific SA signal communication , You have to start with saMgr Get the SA Agent for , Then use agents and SA signal communication . In general use Proxy Indicates the service requestor ,Stub Indicates that the service provider .

2 target
Goal one : How to configure the new service , compile
Goal two : How to integrate a new service into OHOS in
Goal three : How to communicate with new services
3 Realization effect
3.1. Compile successfully
Once the configuration is complete , Code compiled successfully
With products rk3568-khdvk For example ,img File generation , Here's the picture

3.2. The new service runs
burn img File to development board , New services hello Run in the background of the development board as an independent process , Here's the picture

3.3. Communicate with new services
You can communicate with the server through command line mode or application startup trigger mode .
3.3.1 Command line communication
Client operation : perform myhello Executable program , Enter the command send, Send string "Hello,World".

Server response : stay service The layer received the sent string "Hello,World".

3.3.2 Application triggers communication
Application startup triggers :

Server response : Returns a string to print into the application form .

4 The code implements the directory structure
The whole is located in the directory foundation Next , namely foundation/mytest/hello.
5 Implementation process
5.1 How to configure and compile the new service
5.1.1 How to configure the new service
The configuration files of a subsystem of Hongmeng operating system mainly include the following four :
5.1.1.1 Module directory BUILD.gn file
Configure in the module directory BUILD.gn, Select the corresponding template according to the type .
Supported template types :
Example :
ohos_shared_library Example
ohos_executable Example :
ohos_executable Template properties and ohos_shared_library Almost the same
explain :
Executable modules ( namely ohos_executable Template defined ) The default is not to install , If you want to install , You need to specify the install_enable = true
ohos_prebuilt_etc Example :
explain :
To add a module to an existing part , It only needs to be in the module_list Add a new module in gn Compile target ; If this module is provided to other module interfaces , Need to be in inner_kits Add the corresponding configuration in ; If there are test cases for this module , Need to be added to test_list In the middle .
5.1.1.2 establish ohos.build file
Each subsystem has one ohos.build The configuration file ( Or there is bundle.json The configuration file ), In the root directory of the subsystem . Create in the folder corresponding to each component under the newly created subsystem directory ohos.build file , Define part information .
explain :
subsystem Defines the name of the subsystem ;parts Define the components included in the subsystem .
A part contains the part name , Modules included in the component module_list, Interfaces provided by components to other components inner_kits, Test cases for components test_list.
In the existing subsystem Add a new part , There are two ways :
a) In the original of this subsystem ohos.build Add the part to the file
b) Create a new one ohos.build file
Either way, you should ohos.build The files are in the folder of the corresponding subsystem
ohos.build The file contains two parts , The first part subsystem Describes the name of the subsystem ,parts Define the components included in the subsystem , To add a part , You need to add the corresponding content of this part to parts In the middle . When adding, you need to specify the modules included in the component module_list, If there are interfaces provided for other components , Need to be in inner_kits Description in , If there are test cases , Need to be in test_list Description in ,inner_kits And test_list If you don't have one, you can leave it blank .
5.1.1.3 subsystem_config.json file
Modify the system build In the catalog subsystem_config.json file
This file defines which subsystems exist and the folder path where these subsystems are located , When adding a subsystem, you need to describe the subsystem path And name, Represents subsystem path and subsystem name respectively .

5.1.1.4 Product profile {product_name}.json
stay productdefine/common/products The product configuration under the directory is as follows RK3568-KHDVK.json Add corresponding parts in , Add directly to the back of the original part .
Indicates the product name , Product manufacturers , Product equipment , edition , The type of system to compile , And the parts included in the product .

5.1.2 How to compile the new service
Compile the entire open source Hongmeng system , The order is as follows :
here {product_name} In actual operation, it is changed to Product name , for example ,rk3566,rk3568 etc. . The files generated by compilation are archived in out/{device_name}/ Under the table of contents , The result is mirrored output at out/{device_name}/packages/phone/images/ Under the table of contents .
After the compilation , As for how to burn, please refer to the official website or other articles .
5.2 Integrate new services into ohos in
From the code structure, we can see , except 5.1 Configuration , It also needs to be :
5.2.1 establish sa_profile Contents and related documents
Create in the subsystem root directory sa_profile Catalog , Create services ID Prefixed xml Documents and BUILD.gn.
explain :
service ID Value is defined in foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include/system_ability_definition.h in , If not, create a new .

sa_profile Directory example :

9999.xml File example :
BUILD.gn Example :
5.2.2 establish etc Contents and related documents
Create in the subsystem root directory etc Catalog , Create the corresponding... Of the service process .rc file .
etc Directory example :

hello.cfg Configuration example :
5.3 How to communicate with new services
5.3.1 Through the command line and server End to end communication
establish main Function to test , The code is as follows :
Definition AbilityCliTool class , Use send Bound function cmdSendMessage, adopt main in execute Function call AbilityCliTool::cmdSendMessage, The code is as follows :
HelloClient The class implementation code is as follows :
It's going to be HelloProxy Object SendMessage function , The specific code is as follows :
HelloProxy Class is Proxy Server-side implementation , Inherit IRemoteProxy<IHello>, call SendRequest Interface to Stub End send request , Expose the capabilities provided by the server .HelloStub The specific code of the class is as follows :
explain :
This class is and IPC Framework related implementation , Need to inherit IRemoteStub<ITestAbility>.Stub The end is the end that receives the request , Need to rewrite OnRemoteRequest Method is used to receive client calls .
Next through HelloStubSendMessage The specific implementation class of the service side business function is called HelloService in SendMessage function , The specific code is as follows :
5.3.2 Trigger and... Through the application layer server End to end communication
In tools DevEcoStudio Create project , stay index.js The specific code in the document is as follows :
Generated after compilation hap package , Install on the development board , Execute the order as follows :
In addition to modification at the application layer , You also need to make the following changes in the framework layer :
thus , From the application layer, you can trigger the call when initializing the application hello namely SendMessage function , And return the string “Hello,World" Show... In the application .
6 summary
Go through the above steps , stay ohos Customize subsystems or services in the system , Compile and burn into the overall system through configuration , And then through cli The function is verified by the command line or application startup trigger , It has achieved the expected three goals , Master the process and architecture of building custom services .
For more original content, please pay attention to : Shenkaihong technical team
Beginner to master 、 Skills to cases , Systematic sharing HarmonyOS Development technology , Welcome to contribute and subscribe , Let's move forward hand in hand to build Hongmeng ecology .
Want to know more about open source , Please visit :
边栏推荐
- tablestore中可以使用sql查询可以查出表中所有的数据吗?
- 榜单首发——线控制动「新周期」,本土供应商市场竞争力TOP10
- The first place on the list - brake by wire "new cycle", the market competitiveness of local suppliers is TOP10
- 岛屿类问题通用解法与DFS框架
- Media data processing V2 Version (VPC) image zoom content analysis
- ID卡复制教程(使用T5577卡复制4100卡)
- CODING DevOps 助力中化信息打造新一代研效平台,驱动“线上中化”新未来
- 3. caller service call - dapr
- 如何根据多元索引查询最后一条数据,达到 sql order by desc limit 1的效果呢?
- Lenet5 training model of convolutional neural network for machine learning
猜你喜欢

简单介绍一下tensorflow与pytorch的相互转换(主要是tensorflow转pytorch)

Tiktok actual battle ~ list of bloggers I follow, follow and check

Coding Devops helps Sinochem information to build a new generation of research efficiency platform and drive the new future of "online Sinochem"

Slim GAIN(SGAIN)介绍及代码实现——基于生成对抗网络的缺失数据填补

【Hot100】1. 两数之和

10:00面试,10:02就出来了 ,问的实在是太...

Redmibook Pro 14 enhanced version cannot open delta software drastudio_ v1.00.07.52

【MySQL】表连接为什么比子查询快

24岁秃头程序员教你微服务交付下如何持续集成交付,学不会砍我

IPDK — Overview
随机推荐
Media data processing V2 Version (VPC) image zoom content analysis
Cross cluster deployment of helm applications using karmada
北京有哪些牛逼的中小型公司?
使用Karmada实现Helm应用的跨集群部署
10: 00 interview, came out at 10:02, the question is really too
Introduction to deep learning in machine learning
wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe-Module
10:00面试,10:02就出来了 ,问的实在是太...
10 years of testing experience, worthless in the face of the physiological age of 35
【MySQL】官网文档学习之查询语句sql注意事项
Briefly introduce the conversion between tensorflow and pytorch (mainly tensorflow to pytorch)
Ffmpeg forbidden output banner log (30)
早晨有些犹豫
Visual studio 2019 software installation package and installation tutorial
昨日元宇宙|Meta “元宇宙”部门一季度亏损29.6亿美元,六福珠宝发行数字藏品
[Spock] process non ASCII characters in an identifier
How can the sports app keep the end-to-side background alive to make the sports record more complete?
使用openpyxl操作Excel
REDIS00_详解redis.conf配置文件
超自动化与网络安全的未来