Abstract: Based on the Huawei LiteOS system, this article will take you into the component development guide from the component definition.

This article is from the Huawei LiteOS Component Development Guide in one Minute, written by Lionlace.

Do you feel like you don’t know where to start when you’re dealing with a huge amount of code?

Today, we bring you a developer productivity tool: components

Based on the Huawei LiteOS system, this article will take you through the component development guide starting from the component definition.

Component definition

Components are part of the Huawei LiteOS system and belong to the Huawei LiteOS application. At the system level, except for the kernel, build scripts, auxiliary tools and system function library, the software packages that comprise the functions of Huawei LiteOS system can be called components. This document describes component composition, specifications, and component development process in detail to help component developers develop components. Huawei LiteOS components are divided into online components and offline components. The component source code of the online component is not in the Huawei LiteOS code warehouse, so it needs to be downloaded from the Internet before use. The source code for offline components is stored in the Huawei LiteOS code repository. Offline components are basic components, such as networks and file systems.

components

The following takes the online component CURL as an example to explain the component composition and component management of Huawei LiteOS in detail.

The directory structure

Adding a component involves creating and modifying files/directories as shown below. Curl under Components, curl under Demos and the files they contain are newly added directories and files. Other files are common files of each component or demos, and generally need to be modified when new components are added.

Instructions:

To add a component or demo, you need to create its own Kconfig, Makefile, and. Mk files. This document is about adding a component.

Curl -7.54.1, curl-7.54.1.zip should be deleted when submitting to LiteOS warehouse.

Origin. Patch, patch.sha256, and src.sha256 are generated during development and must be submitted to the LiteOS_Components repository.

online_components

The source code for the online component needs to be downloaded from the web, and the download information is recorded in the online_components file. This file holds information (or parameters) about each online component in a specific format. As shown below, each component has four parameters, separated by &#&.

$curl- $curl- $curl- $curl/Utility / $curl $curl = $curl https://github.com/curl/curl/archive/refs/tags/curl-7_54_1.zipCopy the code

• Parameter 1: component source name (commonly named: source name-version number).

Note: this name is equivalent to the source code renaming, and directly click on the web page to download the name may not be the same. Go directly to github.com/curl/curl/a… , the downloaded file is curl-curl-7_54_1.zip, and the decompressed file is curl-curl-7_54_1. Sh, however, names the downloaded file curl-7.54.1.zip (name method: Parameter 1 + download type in Parameter 4) based on parameter 1 and parameter 4. The obtained file is curl-7.54.1, that is, parameter 1. Therefore, during the development process, all operations on the source code must be based on the name of parameter 1. If the downloaded source code name is inconsistent with that of parameter 1, rename the source code according to the preceding instructions and then proceed with subsequent development.

• Parameter 2: directory where the component is located (download the component source code to this directory).

• Parameter 3: component enable flag (used to determine whether to download the component source code, which is the configuration item in the component Kconfig file).

• Parameter 4: address for downloading component source code or command for obtaining component source code (source code verification is involved later. Therefore, it is recommended to download the source code compression package first to facilitate verification.)

Note: Online components can be downloaded in the following ways:

• Download compressed packages: Only. Zip and. Tar. gz compressed packages are supported, for example:

https://github.com/curl/curl/archive/refs/tags/curl-7_54_1.zip
https://github.com/curl/curl/archive/refs/tags/curl-7_54_1.tar.gz
Copy the code

• Use Git clone, for example:

Git clone - b curl - 7.54.1 https://github.com/curl/curl.gitCopy the code

• If you cannot obtain the source code using the preceding methods, you can write a shell script to download the source code and return the download result (success/failure). Parameter 4 specifies the method to invoke the script.

download.sh

Download the source code and patch file of the online component, perform sha256sum verification, and input the patch file into the source code after the verification is successful.

components/utility/curl

The curl directory contains the source code of the curl component and its related files. When adding a component, create a component directory (including language component, media component, utility component, and security component) in the components directory based on component functions. Curl is a utility component, so it is in the Utility directory.

Naming conventions: Use the same name as the component source code (cannot be abbreviated, such as libpng cannot be written as PNG) or refer to the industry common name.

The curl/curl – 7.54.1

Component source, directory name must be consistent with the parameters in the online_components file. The online components are downloaded from the Internet by download.sh. There is no need to upload the source code of the components to the Huawei LiteOS code storehouse. Only the source code of the offline components needs to be uploaded.

src.sha256

The file contains the SHA256 verification code of the component source code. To ensure normal use of components, ensure that the source code downloaded from the Internet is the same as the source code used during component development. LiteOS ensures that the downloaded source code is identical by performing sha256 checks. Therefore, when developing components, you need to compute the SHA256 checksum of the generated source file and copy it to the src.sha256 file. The src.sha256 file for the curl component reads as follows:

7 eec2c7f863c17d8586e94255cd0c2934822f1ae3cb40283a892b46e44f93bdf curl - 7.54.1. ZipCopy the code

To generate the SHA256 verification code, run the following command:

• If the downloaded source package is compressed:

Zip > src.sha256 # sha256sum curl-7.54.1.zip > src.sha256Copy the code

• If the source package is not compressed:

Find a -type parameters f -print0 | xargs 0 sha256sum > SRC. Sha256 # find curl - 7.54.1 -type f -print0 | xargs 0 sha256sum > src.sha256Copy the code

Note: If you need to run the preceding commands in Windows, you can install Git and run them using the Git terminal. Git config –global core. Autocrat lf input

Note:

The verification code file is named src.sha256, and the file name cannot be modified.

When developing components, download the source file using parameter 4 in the online_Components file.

src.sha256

Source code patch file. The file is named Origin. Patch and cannot be modified. To develop components on Huawei LiteOS, it is recommended to avoid direct modifications to open source if unavoidable (e.g. If the source code cannot be compiled successfully without modification, or the source code cannot be directly run on Huawei LiteOS, it needs to be adapted.), the modified content should be generated into a patch file origin. Patch should be adopted to modify the source code in the subsequent use. Patch is installed in patch-P1 mode. Pay attention to patch file generation.

You can use one of the following methods to generate the origin. Patch file:

• Create directories A and B, copy the source code of unmodified components to directory A, and copy the source code of modified components to directory B. The directory structure is as follows:

• Run the following command

Diff -Nur a/curl- 7.54.1b /curl-7.54.1 > orinig. PatchCopy the code

Note: Make sure the origin. Patch file is in Unix format before local verification. This file is automatically converted to Unix format when it is uploaded to the LiteOS_Components repository. If the local verification is not in Unix format, the download. Sh execution of the online component fails.

patch.sha256

Source patch file Origin. Patch Check code file. The file is named patch.sha256 and cannot be modified. It ensures that the downloaded patch file is correct and the file format is consistent with src.patch.

Kconfig

components/utility/curl/Kconfig

Component configuration files. The components can be incorporated into the menu item interface of LiteOS through this file, and can be modified by referring to the Kconfig file of the existing components when writing. You can search for the basic syntax of Kconfig or refer to the official document kconfig-language. TXT.

The components/utility/curl/Kconfig file content and specification is as follows:

Config LOSCFG_COMPONENTS_CURL # configitem "LOSCFG_COMPONENTS_ "bool "Enable Curl" # bool: configitem type; Enable Curl: Configuration item Prompt select LOSCFG_COMPONENTS_NET_LWIP # Dependencies: Specify dependencies for other components. Select LOSCFG_COMPONENTS_FS_FATFS default n # Whether the component is enabled by default: Help Answer y to enable curl is disabled by defaultCopy the code

• If a component contains other components, you can name configuration items flexibly. For example, LOSCFG_COMPONENTS_ Directory name _XXX, but not too long.

• The configuration item prompt is the information displayed in the Menuconfig menu item and can be understood as the alias of the configuration item. Capitalize the first letter of each word and lower case the rest (except proper nouns).

• Uppercase the first letter of the help message, lower case the rest (except proper nouns), and end it.

• Use a space instead of the TAB key for indentation.

Kconfig in the parent directory

When adding a component, you need to create the Kconfig file in the local component directory and modify the Kconfig file in the upper-level directory. This file is used to call Kconfig in the next-level subdirectory and can be modified by referring to other components in this file. Content is as follows, the components/utility/Kconfig files contains the utility all components Kconfig file directory.

Menu "Utility" # menu item source "components/Utility/bidireference/Kconfig" source "components/Utility/curl/Kconfig" # The content of this new source "components/utility/freetype/Kconfig" source "components/utility/iconv/Kconfig" source "components/utility/iniparser/Kconfig" source "components/utility/json-c/Kconfig" source "components/utility/libxml2/Kconfig" endmenuCopy the code

The mk file

The. Mk file is used to export the external header file of a component for other components or modules to call the API of this component. To avoid confusion in header files, Huawei LiteOS uses the. Mk file to manage and limit the external header files of components, and exports the external interfaces of components layer by layer through the. Mk files in each directory. Finally, all header files of components are provided externally through components.

The mk files are as follows:

• Create the. Mk file of the component in the new component directory. For details, see the. Mk file of an existing component.

• Modify the. Mk file in the upper-level directory. For details, see other components in the file.

Such as curl. Mk and Utility. Mk:

# # # # # # # # # # # # # # # # # # # # # # # curl. The content of the mk as # # # # # # # # # # # # # # # # # # # # # # # # # CURL_VERSION = curl - 7.54.1 component version COMPONENTS_CURL_INCLUDE: =  \ -I $(LITEOSTOPDIR)/components/utility/curl/$(CURL_VERSION)/include \ -I $(LITEOSTOPDIR)/components/utility/curl / $(CURL_VERSION)/SRC # # # # # # # # # # # # # # # # # # # # # utility. Mk content is as follows # # # # # # # # # # # # # # # # # # # # # # # ifeq ($(LOSCFG_COMPONENTS_CURL), y) # only component has been enabled, To perform the following actions include $(LITEOSTOPDIR)/components/utility/curl curl. Mk # call subdirectory of other components. Mk file COMPONENTS_UTILITY_INCLUDE + = $(COMPONENTS_CURL_INCLUDE) # export the component's open header file endifCopy the code

Document specification and Description:

• CURL_VERSION: component version macro definition, usually named source name _VERSION.

• COMPONENTS_CURL_INCLUDE: header macro definition, usually named COMPONENTS_ source name _INCLUDE.

• In the. Mk file in the parent directory of a component, the. Mk file of a specific component can be invoked only when the component is enabled.

• To indent, use four Spaces. Do not use the TAB key.

Makefile

The components/utility/curl/Makefile componentization compiled files. Use this file to add components to the component compilation framework of Huawei LiteOS. When adding a component, you need to create this file in the component directory to compile the component. For details, see the Makefile of an existing component. The components/utility/curl/Makefile file content is as follows:

include $(LITEOSTOPDIR)/config.mk include $(LITEOSTOPDIR)/components/net/lwip/lwip.mk include $(LITEOSTOPDIR)/components/utility/curl curl. Mk MODULE_NAME: = $(notdir $(CURDIR)) # $(notdir $(CURDIR)) is equivalent to the curl LOCAL_SRCS_y := LOCAL_DIRS_y := LOCAL_DIRS_y += $(CURL_VERSION)/lib LOCAL_DIRS_y += $(CURL_VERSION)/lib/vauth LOCAL_DIRS_y += $(CURL_VERSION)/lib/vtls LOCAL_DIRS_y += $(CURL_VERSION)/src LOCAL_INCLUDE := $(LWIP_INCLUDE) LOCAL_INCLUDE += \ -I $(LITEOSTOPDIR)/components/utility/curl/$(CURL_VERSION)/lib \ -I $(LITEOSTOPDIR)/components/utility/curl/$(CURL_VERSION)/include \ -I $(LITEOSTOPDIR)/components/utility/curl/$(CURL_VERSION)/src LOCAL_SRCS_y += $(foreach dir, $(LOCAL_DIRS_y), $(wildcard $(dir)/*.c)) LOCAL_SRCS = $(LOCAL_SRCS_y) LOCAL_EXT_FLAG := -Wno-error -Wno-implicit-function-declaration -Wno-unused-variable CURL_DEFS = \ -D HAVE_CONFIG_H \ -D BUILDING_LIBCURL LOCAL_FLAGS := $(LOCAL_INCLUDE) $(CURL_DEFS) $(LOCAL_EXT_FLAG) include $(MODULE)Copy the code

Here are some important parameters (some parameters are not required for every Makefile, please choose to write/not write as required) :

• Include $(LITEOSTOPDIR)/config.mk: introduces the macro definition of the Huawei LiteOS system. All makefiles need to reference this file.

• MODULE_NAME: module name. The name of the static linked library generated by compilation is the module name. For example, if the module name is curl, the generated static link library is libcurl. For links to the library file of new components to be added in the Bsp. mk file, see links to existing components in the Bsp. mk file to modify the library file.

** Note: ** If the component name itself starts with lib, lib is not needed in the module name. For example, libpng should be written as MODULE_NAME := PNG instead of MODULE_NAME := libpng, avoiding the generated static library file named liblibpnp.a.

• LOCAL_INCLUDE: a collection of header files used for compilation.

• LOCAL_SRCS: This module requires all source files except header files that are involved in compiling.

• LOCAL_EXT_FLAG: set of compilation parameters for this module (optional).

• CURL_DEFS: optional collection of macro definitions required for compilation of this module.

• To indent, use four Spaces. Do not use the TAB key.

Makefile in the parent directory

The file used to invoke the next level subdirectories of Makefile, components/utility/Makefile file content is as follows:

include $(LITEOSTOPDIR)/config.mk MODULE_$(LOSCFG_COMPONENTS_BIDIREFERENCE) += bidireference MODULE_$(LOSCFG_COMPONENTS_CURL) += curl # MODULE_$(LOSCFG_COMPONENTS_ICONV) += iconv MODULE_$(LOSCFG_COMPONENTS_INIPARSER) += iniparser MODULE_$(LOSCFG_COMPONENTS_JSON_C) += json-c MODULE_$(LOSCFG_COMPONENTS_LIBXML2) += libxml2 include $(MODULE)Copy the code

LOSCFG_COMPONENTS_CURL is a configuration item in the component Kconfig file. MODULE_$(LOSCFG_COMPONENTS_CURL) indicates that the curl component is compiled only after the component is enabled.

Components in the demo

In principle, each component should provide a reference demo, which must be developed by itself. It is forbidden to copy code directly from the network. Each demo can provide only one external interface, which is called in the demo_entry.c file.

bsp.mk

Huawei LiteOS uses a component-based compilation framework. After each component or demo is compiled, a corresponding static library file will be generated in the OUT directory. Since the Windows platform cannot automatically link static library files, manual linking is required in the Bsp.mk file. Add link curl component and demo static library file:

ifneq ($(OS), Linux) ifeq ($(LOSCFG_COMPONENTS_CURL), Y) LITEOS_BASELIB += -lcurl endif ifeq ($(LOSCFG_DEMOS_CURL), Y) LITEOS_BASELIB += -lcurl_demo # Curl_demo is the module name of component Demo endif endifCopy the code

demo_entry.c

Entry to all demos. After the demo is complete, it is called in the DemoEntry function of the file.

#ifdef LOSCFG_DEMOS_CURL
#include "curl_demo.h"
#endif
VOID DemoEntry(VOID)
{
#ifdef LOSCFG_DEMOS_CURL
    CurlDemoTask();
#endif
}
Copy the code

Note: functions must be called as #include headers. Extern is not allowed.

Online component download process

Linux platform

Download the LiteOS code repository. After you run the make Menuconfig command in the Huawei LiteOS source directory, the download.sh script is automatically invoked. The script reads the contents of the online_Components file to get component download information and determine whether the component is enabled. If the component is enabled, continue to determine whether the component source exists. Patch, src.sha256, and patch.sha256. Use the. Sha256 file to check whether the downloaded source code and the Origin. If correct, enter patch to complete the downloading process of online components.

Note: If you want to re-download the component source code and patches, you need to delete the existing source code and patch files locally. In addition, if the script is interrupted by external signals, such as CTRLZ and CTRLC, the download process may fail. You need to clear the remaining files in the download in time.

The Windows platform

In the HUAWEI LiteOS Studio development tool, the download.sh file is automatically invoked after the project configuration — > component configuration is complete. The rest of the logic is consistent with that of the Linux platform.

The test component

After all the component development is complete, you need to test the component to ensure that the online component download process succeeds.

Submit code

After the component test is passed, the code can be submitted to each code warehouse of Huawei LiteOS. The requirements for code submission are as follows:

• Online components do not need to submit component source code.

• Origon. patch, src.sha256, and patch.sha256 are submitted to the corresponding directory in the LiteOS_Components repository. (The directory structure must be the same as the directory structure during development. Curl files should be placed in the Components/Utility /curl directory of the repository. For offline components, the src.sha256 file is not required.

• Remaining documents are submitted to LiteOS warehouse.

Note: Please refer to the LiteOS Code & Document Contribution Guide for the code submission process.

Gitee.com/LiteOS/Lite…

conclusion

We will continue to add more components, development boards, architectures, features and more in the future.

Thank you for reading, have any questions, suggestions, please leave a message to us, let us progress together:

Gitee.com/LiteOS/Lite…

To make it easier to find the “LiteOS” code store, visit gitee.com/LiteOS/Lite… , follow “Watch”, like “Star”, and “Fork” to your own account, as shown below.

Click to follow, the first time to learn about Huawei cloud fresh technology ~