1e41f4b71Sopenharmony_ci# Hiview Development 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## Introduction 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci### Function Overview 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciHiview is a module that provides toolkits for device maintenance across different platforms. It consists of the plugin management platform and the service plugins running on the platform. Hiview works in event-driven mode. The core of Hiview is a collection of HiSysEvent stubs distributed in the system. Formatted events are reported to Hiview through the HiSysEvent API for processing. The following figure shows the data interaction process. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci**Figure 1** Data interaction between Hiview modules 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci1. The service process calls the event logging API to report logged event information and writes the information to the node file. 16e41f4b71Sopenharmony_ci2. SysEventSource of the Hiview process asynchronously reads event information from the node file and distributes the event to SysEventPipeline for processing. 17e41f4b71Sopenharmony_ci - The SysEventService plugin verifies events and flushes them to disks. 18e41f4b71Sopenharmony_ci - The Faultlogger plugin processes fault-related events. 19e41f4b71Sopenharmony_ci - The EventLogger plugin collects event-related log information. 20e41f4b71Sopenharmony_ci3. On completion of event processing, SysEventPipeline sends the events to the event subscription queue and then dispatches the events to the subscription plugin for processing. 21e41f4b71Sopenharmony_ci - FreezeDetector processes screen freezing events. 22e41f4b71Sopenharmony_ci - HiCollieCollector processes suspension events. 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci### Basic Concepts 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ciBefore you get started, familiarize yourself with the following concepts: 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci- Plug-in 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci An independent module running in the Hiview process. A plugin is delivered with the Hiview binary file to implement maintenance and fault management functions independently. Plug-ins can be disassembled independently during compilation, and can be hosted on the platform during running and dynamically configured. 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci- Pipeline 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci An ordered set of event processing plugins. Events entering the pipeline are processed by plugins on the pipeline in sequence. 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci- Event source 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci A special plugin that can produce events. Different from common plugins, a special plugin can be bound to a pipeline, and can produce events and distribute them to the pipeline. 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci- Pipeline group 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci A group of pipelines configured on the same event source. 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci### Working Principles 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ciHiview supports plugin development on the plugin management platform and provides the required plugin development capabilities. You can add plugins to the Hiview platform to implement HiSysEvent event processing. Before you get started, you're expected to have a basic understanding of plugin working principles. 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci#### Plug-in Registration 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ciA plugin can be registered in any of the following modes. 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci| Mode | Description | 53e41f4b71Sopenharmony_ci| ------------------ | ------------------------------------------------------------ | 54e41f4b71Sopenharmony_ci| Static registration | Use the **REGISTER(xxx);** macro to register the plugin. Such a plugin cannot be unloaded.| 55e41f4b71Sopenharmony_ci| Proxy registration | Use the **REGISTER_PROXY(xxx);** macro to register the plugin. Such a plugin is not loaded upon startup and can be loaded and unloaded dynamically during system running.| 56e41f4b71Sopenharmony_ci| Proxy registration and loading upon startup| Use the **REGISTER_PROXY_WITH_LOADED(xxx);** macro to register the plugin. Such a plugin is loaded upon startup and can be unloaded and loaded dynamically during system running.| 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci#### Plug-in Event-Driven Modes 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ciThere are two event-driven modes available for plugins: pipeline-driven and subscription-driven. The differences are as follows: 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci- Pipeline-driven plugins need to be configured on the pipeline. After an event is distributed from an event source to the pipeline, the event traverses the plugins configured on the pipeline in sequence for processing. 63e41f4b71Sopenharmony_ci- Subscription-driven plugins do not need to be configured on the pipeline. However, a listener needs to be registered with the Hiview platform upon plugin startup, and the plugin needs to implement the event listener function. 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci#### Plug-in Loading 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ciDepending on your service demand, you can compile all or some plugins into the Hiview binary file. 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ciMultiple plugins can be built into an independent plugin package and preset in the system as an independent **.so** file. One **.so** file corresponds to one **plugin_config** file. For example, **libxxx.z.so** corresponds to the** xxx_plugin_config** file. When the Hiview process starts, it scans for the plugin package (**.so** file) and the corresponding configuration file and loads the plugins in the plugin package. 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ciThe plugin package is described as follows: 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci1. The plugin package runs on the plugin management platform as an independent entity. The plugins, pipelines, or event sources in it provide the same functions as the plugins in the Hiview binary. 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci2. Plug-ins in the plugin package can be inserted into the Hiview binary pipeline. 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci3. Subscribers, wherever they are located, can receive events sent by the platform based on the subscription rules. 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci## Plug-in Development Guide 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci### When to Use 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ciYou can deploy a plugin on the Hiview platform if you want to perform specific service processing on the HiSysEvent events distributed from the event source. The following table describes the APIs used for plugin development. 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci### Available APIs 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ciThe following table lists the APIs related to plugin development. For details about the APIs, see the API Reference. 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ciTable 1 Description of Plugin APIs 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci| API | Description | 95e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | ------------------------------------------------------------ | 96e41f4b71Sopenharmony_ci| virtual void OnLoad() | Loads plugins. After plugins are loaded, you can call this API to initialize data.| 97e41f4b71Sopenharmony_ci| virtual void OnUnload() | Unloads plugins. Before plugins are unloaded, you can call this API to reclaim data. | 98e41f4b71Sopenharmony_ci| virtual bool ReadyToLoad() | Checks whether the current plugin can be loaded when the Hiview starts plugin loading. | 99e41f4b71Sopenharmony_ci| virtual bool OnEvent(std::shared_ptr\<Event\>& event) | Implements event processing. You can call this API to receive events distributed by the pipeline or platform perform service processing.| 100e41f4b71Sopenharmony_ci| virtual bool CanProcessEvent(std::shared_ptr\<Event\> event) | Checks whether an event can traverse backward throughout the entire pipeline. This function takes effect only when the plugin is the first one in the pipeline.| 101e41f4b71Sopenharmony_ci| HiviewContext* GetHiviewContext() | Obtains the context of the Hiview plugin management platform. | 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci**Table 2** Description of Event APIs 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci| API | Description | 106e41f4b71Sopenharmony_ci| ---------------------- | ---------------------------- | 107e41f4b71Sopenharmony_ci| domain_ | Event domain. | 108e41f4b71Sopenharmony_ci| eventName_ | Event name. | 109e41f4b71Sopenharmony_ci| happenTime_ | Time when an event occurs. | 110e41f4b71Sopenharmony_ci| jsonExtraInfo_ | Event data in JSON format. | 111e41f4b71Sopenharmony_ci| bool IsPipelineEvent() | Whether an event is a pipeline event. | 112e41f4b71Sopenharmony_ci| bool HasFinish() | Whether an event can continue to traverse backward.| 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci### How to Develop 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci1. Define a service plugin class, namely, **PluginExample**, which inherits from the **Plugin** class. 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci ```c++ 119e41f4b71Sopenharmony_ci #include "event.h" 120e41f4b71Sopenharmony_ci #include "plugin.h" 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci class PluginExample : public Plugin { 123e41f4b71Sopenharmony_ci public: 124e41f4b71Sopenharmony_ci bool OnEvent(std::shared_ptr<Event>& event) override; 125e41f4b71Sopenharmony_ci void OnLoad() override; 126e41f4b71Sopenharmony_ci void OnUnload() override; 127e41f4b71Sopenharmony_ci }; 128e41f4b71Sopenharmony_ci ``` 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci2. In the plugin class implementation code, register the plugin and overwrite the corresponding functions based on the service requirements. 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci ```c++ 133e41f4b71Sopenharmony_ci #include "plugin_factory.h" 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci // Register the plugin in static registration mode. 136e41f4b71Sopenharmony_ci REGISTER(PluginExample); 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci void PluginExample::OnLoad() 139e41f4b71Sopenharmony_ci { 140e41f4b71Sopenharmony_ci ... // Initialize plugin resources while the plugin is loaded. 141e41f4b71Sopenharmony_ci printf("PluginExample OnLoad \n"); 142e41f4b71Sopenharmony_ci } 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci void PluginExample::OnUnload() 145e41f4b71Sopenharmony_ci { 146e41f4b71Sopenharmony_ci ... // Release plugin resources while the plugin is unloaded. 147e41f4b71Sopenharmony_ci printf("PluginExample OnUnload \n"); 148e41f4b71Sopenharmony_ci } 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci bool PluginExample::OnEvent(std::shared_ptr<Event>& event) 151e41f4b71Sopenharmony_ci { 152e41f4b71Sopenharmony_ci ... // Perform specific service processing on the event using the event processing function. 153e41f4b71Sopenharmony_ci printf("PluginExample OnEvent \n"); 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci // If the plugin focuses only on events of a certain domain, log only the events of this domain. 156e41f4b71Sopenharmony_ci if (event->domain_ == "TEST_DOMAIN") { 157e41f4b71Sopenharmony_ci printf("The event data received is %s \n", event->jsonExtraInfo_); 158e41f4b71Sopenharmony_ci return true; 159e41f4b71Sopenharmony_ci } 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci return false; 162e41f4b71Sopenharmony_ci } 163e41f4b71Sopenharmony_ci ``` 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci3. Configure the plugin in the **plugin_build.json** file and compile the plugin with the Hiview binary file. 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci ```json 168e41f4b71Sopenharmony_ci { 169e41f4b71Sopenharmony_ci "plugins": { 170e41f4b71Sopenharmony_ci "PluginExample": { 171e41f4b71Sopenharmony_ci "path": "plugins/PluginExample", 172e41f4b71Sopenharmony_ci "name": "PluginExample" 173e41f4b71Sopenharmony_ci } 174e41f4b71Sopenharmony_ci }, 175e41f4b71Sopenharmony_ci "rules": [ 176e41f4b71Sopenharmony_ci { 177e41f4b71Sopenharmony_ci "info": { 178e41f4b71Sopenharmony_ci "loadorder": { 179e41f4b71Sopenharmony_ci "PluginExample": { 180e41f4b71Sopenharmony_ci "loadtime": 0 181e41f4b71Sopenharmony_ci } 182e41f4b71Sopenharmony_ci }, 183e41f4b71Sopenharmony_ci "pipelines": { 184e41f4b71Sopenharmony_ci "SysEventPipeline": [ 185e41f4b71Sopenharmony_ci PluginExample 186e41f4b71Sopenharmony_ci ] 187e41f4b71Sopenharmony_ci } 188e41f4b71Sopenharmony_ci } 189e41f4b71Sopenharmony_ci } 190e41f4b71Sopenharmony_ci ] 191e41f4b71Sopenharmony_ci } 192e41f4b71Sopenharmony_ci ``` 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci## Reference 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ciFor more information about the source code and usage of HiSysEvent, access the Hiview code repository(https://gitee.com/openharmony/hiviewdfx_hiview). 198