当前位置:网站首页>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 :
边栏推荐
- js中订阅发布模式bus
- A little hesitant in the morning
- 一次简单的反射型XSS操作及思路
- 机器学习之卷积神经网络--CNN介绍
- A 24-year-old bald programmer teaches you how to continuously integrate and deliver microservice delivery. You can't learn how to cut me off
- Openharmony - detailed source code of Kernel Object Events
- IPDK — Overview
- 逆向调试入门-PE结构详解02/07
- leetcode:22. 括号生成
- 【MySQL】官网文档学习之查询语句sql注意事项
猜你喜欢

字节跳动数据平台技术揭秘:基于ClickHouse的复杂查询实现与优化

Etcd visualization tool: an introduction to kstone (I)

Openharmony - detailed source code of Kernel Object Events

开源技术交流丨一站式全自动化运维管家ChengYing入门介绍

Geoffrey Hinton:我的五十年深度学习生涯与研究心法

Soliciting articles and contributions - building a blog environment with a lightweight application server

关注35岁的坎:畏惧是因为你没有匹配这个年纪该有的能力

What useful supplier management systems are available

openGauss内核:SQL解析过程分析

全球陆续拥抱Web3.0,多国已明确开始抢占先机
随机推荐
Openharmony - detailed source code of Kernel Object Events
Kiss in the metauniverse! CMU launched VR head display plug-in, reproducing the vivid touch of lips
Convolutional neural network for machine learning uses cifar10 data set and alexnet network model to train classification model, install labelimg, and report error
【初学者必看】vlc实现的rtsp服务器及转储H264文件
wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe-Module
讲师征集令 | Apache DolphinScheduler Meetup分享嘉宾,期待你的议题和声音!
一台服务器最大并发 tcp 连接数多少?65535?
The Web3.0 era is coming. See how Tianyi cloud storage resources invigorate the system to enable new infrastructure (Part 1)
运维-- 统一网关非常必要
Convolutional neural networks for machine learning -- an introduction to CNN
[MySQL] official website document learning query statement SQL precautions
Knowing these commands allows you to master shell's own tools
昨日元宇宙|Meta “元宇宙”部门一季度亏损29.6亿美元,六福珠宝发行数字藏品
关注35岁的坎:畏惧是因为你没有匹配这个年纪该有的能力
[Spock] process non ASCII characters in an identifier
早晨有些犹豫
REDIS00_ Explain redis Conf configuration file
Big God explains open source buff gain strategy live lecture
Lenet5 training model of convolutional neural network for machine learning
面试官: 线程池是如何做到线程复用的?有了解过吗,说说看