106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci
1606f6ba60Sopenharmony_ci#ifndef PLUGIN_MODULE_API_H
1706f6ba60Sopenharmony_ci#define PLUGIN_MODULE_API_H
1806f6ba60Sopenharmony_ci
1906f6ba60Sopenharmony_ci#include <stddef.h>
2006f6ba60Sopenharmony_ci#include <stdint.h>
2106f6ba60Sopenharmony_ci#include <climits>
2206f6ba60Sopenharmony_ci
2306f6ba60Sopenharmony_ci#ifdef __cplusplus
2406f6ba60Sopenharmony_ciextern "C" {
2506f6ba60Sopenharmony_ci#endif
2606f6ba60Sopenharmony_ci
2706f6ba60Sopenharmony_ci/**
2806f6ba60Sopenharmony_ci * @brief interface type of plugin session starting
2906f6ba60Sopenharmony_ci *
3006f6ba60Sopenharmony_ci * It will be called when the plugin session starts, and the binary data pointed
3106f6ba60Sopenharmony_ci * by the parameter is the protobuffer binary data filled by the IDE side;
3206f6ba60Sopenharmony_ci *
3306f6ba60Sopenharmony_ci * The implementation part —— need to call the deserialization code generated by
3406f6ba60Sopenharmony_ci * protobuffer to restore it to the data type generated by proto;
3506f6ba60Sopenharmony_ci *
3606f6ba60Sopenharmony_ci * For the third-party developers, you can also not use the protobuffer as a
3706f6ba60Sopenharmony_ci * serialization tool, only need to adapt to the Java code.
3806f6ba60Sopenharmony_ci *
3906f6ba60Sopenharmony_ci * @param ConfigData configure the starting address of information memory block
4006f6ba60Sopenharmony_ci * @param ConfigSize configure the byte counts of information memory block
4106f6ba60Sopenharmony_ci * @return Return 0 for success and -1 for failure
4206f6ba60Sopenharmony_ci */
4306f6ba60Sopenharmony_citypedef int (*PluginSessionStartCallback)(const uint8_t* configData, uint32_t configSize);
4406f6ba60Sopenharmony_ci
4506f6ba60Sopenharmony_ci/**
4606f6ba60Sopenharmony_ci * @brief interface type of Plugin result reporting interface type
4706f6ba60Sopenharmony_ci * The incoming memory block is a large memory pre allocated by the plugin management module,
4806f6ba60Sopenharmony_ci * The implementation part —— need to define the specific result type object of proto and serialize it to this memory,
4906f6ba60Sopenharmony_ci * The framework will transfer the original data to the PC as it is.
5006f6ba60Sopenharmony_ci * @param bufferData: the starting address of the memory buffer where the result is stored
5106f6ba60Sopenharmony_ci * @param bufferSize: The byte counts in the memory buffer where the result is stored
5206f6ba60Sopenharmony_ci * @return The return value greater than 0 indicates the number of bytes filled in memory,
5306f6ba60Sopenharmony_ci * and 0 indicates that nothing is filled,
5406f6ba60Sopenharmony_ci * return value less than 0 indicates failure, and -1 is a general error
5506f6ba60Sopenharmony_ci * If the absolute value of the return value is greater than buffersize, the number of bytes in the buffer
5606f6ba60Sopenharmony_ci * is insufficient to store the result. The framework will try to get the result again with a larger cache.
5706f6ba60Sopenharmony_ci */
5806f6ba60Sopenharmony_citypedef int (*PluginReportResultCallback)(uint8_t* bufferData, uint32_t bufferSize);
5906f6ba60Sopenharmony_ci
6006f6ba60Sopenharmony_cistruct RandomWriteCtx;
6106f6ba60Sopenharmony_ci
6206f6ba60Sopenharmony_ci/**
6306f6ba60Sopenharmony_ci * @brief get memory of the specified length
6406f6ba60Sopenharmony_ci * @param ctx : writing context
6506f6ba60Sopenharmony_ci * @param size : The byte counts to be used
6606f6ba60Sopenharmony_ci * @param memory : output current target memory pointer
6706f6ba60Sopenharmony_ci * @param offset : output current offset relative to the head of writing
6806f6ba60Sopenharmony_ci * @return : Return true for success and false for failure
6906f6ba60Sopenharmony_ci */
7006f6ba60Sopenharmony_citypedef bool (*GetMemoryFunc)(RandomWriteCtx* ctx, uint32_t size, uint8_t** memory, uint32_t* offset);
7106f6ba60Sopenharmony_ci
7206f6ba60Sopenharmony_ci/**
7306f6ba60Sopenharmony_ci * @brief seek to the specified offset for writing
7406f6ba60Sopenharmony_ci * @param ctx : writing context
7506f6ba60Sopenharmony_ci * @param offset : offset relative to the head of writing
7606f6ba60Sopenharmony_ci * @return : Return true for success and false for failure
7706f6ba60Sopenharmony_ci */
7806f6ba60Sopenharmony_citypedef bool (*SeekFunc)(RandomWriteCtx* ctx, uint32_t offset);
7906f6ba60Sopenharmony_ci
8006f6ba60Sopenharmony_ci/**
8106f6ba60Sopenharmony_ci * RandomWriteCtx : type definition, random write context.
8206f6ba60Sopenharmony_ci */
8306f6ba60Sopenharmony_cistruct RandomWriteCtx {
8406f6ba60Sopenharmony_ci    /**
8506f6ba60Sopenharmony_ci     * get the memory and current offset.
8606f6ba60Sopenharmony_ci     */
8706f6ba60Sopenharmony_ci    GetMemoryFunc getMemory;
8806f6ba60Sopenharmony_ci
8906f6ba60Sopenharmony_ci    /**
9006f6ba60Sopenharmony_ci     * seek for writing.
9106f6ba60Sopenharmony_ci     */
9206f6ba60Sopenharmony_ci    SeekFunc seek;
9306f6ba60Sopenharmony_ci};
9406f6ba60Sopenharmony_ci
9506f6ba60Sopenharmony_ci/**
9606f6ba60Sopenharmony_ci * @brief interface type of Plugin result reporting interface type
9706f6ba60Sopenharmony_ci * The plugin reports the bytes data by randomWrite() directly,
9806f6ba60Sopenharmony_ci * or constructor proto_encoder message by randomWrite(),
9906f6ba60Sopenharmony_ci * the message class code is generated by protoc using proto file.
10006f6ba60Sopenharmony_ci * @param randomWrite: random write context
10106f6ba60Sopenharmony_ci * @return size that has been reproted, -1 when reprot faild.
10206f6ba60Sopenharmony_ci */
10306f6ba60Sopenharmony_citypedef int32_t (*PluginReportResultOptimizeCallback)(RandomWriteCtx* randomWrite);
10406f6ba60Sopenharmony_ci
10506f6ba60Sopenharmony_ci/**
10606f6ba60Sopenharmony_ci * @brief interface type of plugin session stop,
10706f6ba60Sopenharmony_ci * Called when stopping plugin sessions
10806f6ba60Sopenharmony_ci * @return Return 0 for success and -1 for failure.
10906f6ba60Sopenharmony_ci */
11006f6ba60Sopenharmony_citypedef int (*PluginSessionStopCallback)(void);
11106f6ba60Sopenharmony_ci
11206f6ba60Sopenharmony_ci/**
11306f6ba60Sopenharmony_ci * @brief interface type of plugin session stop,
11406f6ba60Sopenharmony_ci * Called when stopping plugin sessions
11506f6ba60Sopenharmony_ci * @return Return 0 for success and -1 for failure.
11606f6ba60Sopenharmony_ci */
11706f6ba60Sopenharmony_citypedef int (*PluginReportBasicDataCallback)(void);
11806f6ba60Sopenharmony_ci
11906f6ba60Sopenharmony_ci/**
12006f6ba60Sopenharmony_ci * WriterStruct type forward declaration
12106f6ba60Sopenharmony_ci */
12206f6ba60Sopenharmony_citypedef struct WriterStruct WriterStruct;
12306f6ba60Sopenharmony_ci
12406f6ba60Sopenharmony_ci/**
12506f6ba60Sopenharmony_ci * @brief write : interface type
12606f6ba60Sopenharmony_ci * @param writer : pointer
12706f6ba60Sopenharmony_ci * @param data : the first byte pointer of data buffer
12806f6ba60Sopenharmony_ci * @param size : The byte counts in the data buffer
12906f6ba60Sopenharmony_ci * @return : Return the number of bytes written for success, and returns -1 for failure
13006f6ba60Sopenharmony_ci */
13106f6ba60Sopenharmony_citypedef long (*WriteFuncPtr)(WriterStruct* writer, const void* data, size_t size);
13206f6ba60Sopenharmony_ci
13306f6ba60Sopenharmony_ci/**
13406f6ba60Sopenharmony_ci * @brief flush : interface type
13506f6ba60Sopenharmony_ci * @return Return true for success and false for failure;
13606f6ba60Sopenharmony_ci */
13706f6ba60Sopenharmony_citypedef bool (*FlushFuncPtr)(WriterStruct* writer);
13806f6ba60Sopenharmony_ci
13906f6ba60Sopenharmony_ci/**
14006f6ba60Sopenharmony_ci * @brief StartReport interface type
14106f6ba60Sopenharmony_ci * @return : Return RandomWriteCtx for success, null for failure
14206f6ba60Sopenharmony_ci *           The plugin reports the bytes data by RandomWriteCtx directly,
14306f6ba60Sopenharmony_ci *           or constructor proto_encoder message by RandomWriteCtx,
14406f6ba60Sopenharmony_ci *           the message class code is generated by protoc using proto file.
14506f6ba60Sopenharmony_ci */
14606f6ba60Sopenharmony_citypedef RandomWriteCtx* (*StartReportFuncPtr)(WriterStruct* writer);
14706f6ba60Sopenharmony_ci
14806f6ba60Sopenharmony_ci/**
14906f6ba60Sopenharmony_ci * @brief FinishReport interface type
15006f6ba60Sopenharmony_ci * @param size : The byte counts of data reported
15106f6ba60Sopenharmony_ci * @return
15206f6ba60Sopenharmony_ci */
15306f6ba60Sopenharmony_citypedef void (*FinishReportFuncPtr)(WriterStruct* writer, int32_t size);
15406f6ba60Sopenharmony_ci
15506f6ba60Sopenharmony_ci/**
15606f6ba60Sopenharmony_ci * WriterStruct : type definition.
15706f6ba60Sopenharmony_ci */
15806f6ba60Sopenharmony_cistruct WriterStruct {
15906f6ba60Sopenharmony_ci    /**
16006f6ba60Sopenharmony_ci     * write : function pointer,point to the actual write function.
16106f6ba60Sopenharmony_ci     */
16206f6ba60Sopenharmony_ci    WriteFuncPtr write = nullptr;
16306f6ba60Sopenharmony_ci
16406f6ba60Sopenharmony_ci    /**
16506f6ba60Sopenharmony_ci     * flush function pointer,point to the actual flush function.
16606f6ba60Sopenharmony_ci     */
16706f6ba60Sopenharmony_ci    FlushFuncPtr flush = nullptr;
16806f6ba60Sopenharmony_ci
16906f6ba60Sopenharmony_ci    /**
17006f6ba60Sopenharmony_ci     * startMessage function pointer, point to the actual startMessage function.
17106f6ba60Sopenharmony_ci     */
17206f6ba60Sopenharmony_ci    StartReportFuncPtr startReport = nullptr;
17306f6ba60Sopenharmony_ci
17406f6ba60Sopenharmony_ci    /**
17506f6ba60Sopenharmony_ci     * finishMessage function pointer, point to the actual finishMessage function.
17606f6ba60Sopenharmony_ci     */
17706f6ba60Sopenharmony_ci    FinishReportFuncPtr finishReport = nullptr;
17806f6ba60Sopenharmony_ci
17906f6ba60Sopenharmony_ci    /**
18006f6ba60Sopenharmony_ci     * data encoding method, true is protobuf, false is protoencoder, default is true for UT.
18106f6ba60Sopenharmony_ci     */
18206f6ba60Sopenharmony_ci    bool isProtobufSerialize = true;
18306f6ba60Sopenharmony_ci};
18406f6ba60Sopenharmony_ci
18506f6ba60Sopenharmony_ci/**
18606f6ba60Sopenharmony_ci * @brief : register writer interface type
18706f6ba60Sopenharmony_ci * @param : writer, writer pointer
18806f6ba60Sopenharmony_ci * @return : Return 0 for success and -1 for failure
18906f6ba60Sopenharmony_ci */
19006f6ba60Sopenharmony_citypedef int (*RegisterWriterStructCallback)(const WriterStruct* writer);
19106f6ba60Sopenharmony_ci
19206f6ba60Sopenharmony_ci/**
19306f6ba60Sopenharmony_ci * PluginModuleCallbacks : type forward declaration
19406f6ba60Sopenharmony_ci */
19506f6ba60Sopenharmony_citypedef struct PluginModuleCallbacks PluginModuleCallbacks;
19606f6ba60Sopenharmony_ci
19706f6ba60Sopenharmony_ci/**
19806f6ba60Sopenharmony_ci * @brief Plugin callback function table
19906f6ba60Sopenharmony_ci */
20006f6ba60Sopenharmony_cistruct PluginModuleCallbacks {
20106f6ba60Sopenharmony_ci    /**
20206f6ba60Sopenharmony_ci     * Session start callback
20306f6ba60Sopenharmony_ci     */
20406f6ba60Sopenharmony_ci    PluginSessionStartCallback onPluginSessionStart;
20506f6ba60Sopenharmony_ci
20606f6ba60Sopenharmony_ci    /**
20706f6ba60Sopenharmony_ci     * Data reporting callback and it is used to connect the
20806f6ba60Sopenharmony_ci     * polling reporting results of plugin management framework.
20906f6ba60Sopenharmony_ci     */
21006f6ba60Sopenharmony_ci    PluginReportResultCallback onPluginReportResult;
21106f6ba60Sopenharmony_ci
21206f6ba60Sopenharmony_ci    /**
21306f6ba60Sopenharmony_ci     * Session stop callback
21406f6ba60Sopenharmony_ci     */
21506f6ba60Sopenharmony_ci    PluginSessionStopCallback onPluginSessionStop;
21606f6ba60Sopenharmony_ci
21706f6ba60Sopenharmony_ci    /**
21806f6ba60Sopenharmony_ci     * Register the writer callback, which is used to connect the
21906f6ba60Sopenharmony_ci     * flow reporting results of plugin management framework
22006f6ba60Sopenharmony_ci     */
22106f6ba60Sopenharmony_ci    RegisterWriterStructCallback onRegisterWriterStruct;
22206f6ba60Sopenharmony_ci
22306f6ba60Sopenharmony_ci    /**
22406f6ba60Sopenharmony_ci     * report plugin basic data
22506f6ba60Sopenharmony_ci     */
22606f6ba60Sopenharmony_ci    PluginReportBasicDataCallback onReportBasicDataCallback = 0;
22706f6ba60Sopenharmony_ci
22806f6ba60Sopenharmony_ci    /**
22906f6ba60Sopenharmony_ci     * Data reporting callback optimized and it is used to connect the
23006f6ba60Sopenharmony_ci     * polling reporting results of plugin management framework.
23106f6ba60Sopenharmony_ci     */
23206f6ba60Sopenharmony_ci    PluginReportResultOptimizeCallback onPluginReportResultOptimize;
23306f6ba60Sopenharmony_ci};
23406f6ba60Sopenharmony_ci
23506f6ba60Sopenharmony_ci/**
23606f6ba60Sopenharmony_ci * @brief The maximum length of the plugin name and it does not contain the ending character(\0)
23706f6ba60Sopenharmony_ci */
23806f6ba60Sopenharmony_ci#define PLUGIN_MODULE_NAME_MAX 127
23906f6ba60Sopenharmony_ci
24006f6ba60Sopenharmony_ci/**
24106f6ba60Sopenharmony_ci * @brief The maximum length of the plugin version and it does not contain the ending character(\0)
24206f6ba60Sopenharmony_ci */
24306f6ba60Sopenharmony_ci#define PLUGIN_MODULE_VERSION_MAX 7
24406f6ba60Sopenharmony_ci
24506f6ba60Sopenharmony_ci/**
24606f6ba60Sopenharmony_ci * PluginModuleStruct type forward declaration
24706f6ba60Sopenharmony_ci */
24806f6ba60Sopenharmony_citypedef struct PluginModuleStruct PluginModuleStruct;
24906f6ba60Sopenharmony_ci
25006f6ba60Sopenharmony_cistruct PluginModuleStruct {
25106f6ba60Sopenharmony_ci    /**
25206f6ba60Sopenharmony_ci     * Callback function table
25306f6ba60Sopenharmony_ci     */
25406f6ba60Sopenharmony_ci    PluginModuleCallbacks* callbacks;
25506f6ba60Sopenharmony_ci
25606f6ba60Sopenharmony_ci    /**
25706f6ba60Sopenharmony_ci     * Plugin name
25806f6ba60Sopenharmony_ci     */
25906f6ba60Sopenharmony_ci    char name[PLUGIN_MODULE_NAME_MAX + 1];
26006f6ba60Sopenharmony_ci
26106f6ba60Sopenharmony_ci    /**
26206f6ba60Sopenharmony_ci     * Plugin version
26306f6ba60Sopenharmony_ci     */
26406f6ba60Sopenharmony_ci    char version[PLUGIN_MODULE_VERSION_MAX + 1];
26506f6ba60Sopenharmony_ci
26606f6ba60Sopenharmony_ci    /**
26706f6ba60Sopenharmony_ci     * (polling type needed)result buffer byte number prompt, used to prompt the plugin management
26806f6ba60Sopenharmony_ci     * module to call the data reporting interface to use the memory buffer byte number
26906f6ba60Sopenharmony_ci     */
27006f6ba60Sopenharmony_ci    uint32_t resultBufferSizeHint;
27106f6ba60Sopenharmony_ci
27206f6ba60Sopenharmony_ci    bool isStandaloneFileData = false;
27306f6ba60Sopenharmony_ci
27406f6ba60Sopenharmony_ci    char outFileName[PATH_MAX + 1];
27506f6ba60Sopenharmony_ci};
27606f6ba60Sopenharmony_ci
27706f6ba60Sopenharmony_ci/**
27806f6ba60Sopenharmony_ci * Plugin module registration information;
27906f6ba60Sopenharmony_ci
28006f6ba60Sopenharmony_ci * The plugin management module will search the symbol from the dynamic object file according to the name,
28106f6ba60Sopenharmony_ci * and then retrieve the basic information and callback function table.
28206f6ba60Sopenharmony_ci *
28306f6ba60Sopenharmony_ci * If Callback function table of onPluginReportResult is filled with non null values,
28406f6ba60Sopenharmony_ci * the plugin will be regarded as a "polling" plugin by the plugin management framework,
28506f6ba60Sopenharmony_ci * and the management framework will call this interface in the module regularly.
28606f6ba60Sopenharmony_ci
28706f6ba60Sopenharmony_ci * If Callback function table of onRegisterWriterStruct is filled with non null values,
28806f6ba60Sopenharmony_ci * The plugin will be regarded as a "streaming" plugin by the plugin management framework,
28906f6ba60Sopenharmony_ci * and the management framework will not call this interface in the module regularly.
29006f6ba60Sopenharmony_ci
29106f6ba60Sopenharmony_ci * The plugin module needs to save the writer pointer when the onregisterwriterstruct interface is called,
29206f6ba60Sopenharmony_ci * and create a thread when the onpluginsessionstart interface is called;
29306f6ba60Sopenharmony_ci
29406f6ba60Sopenharmony_ci * At the right time, the thread calls the writer's write() interface to write data to
29506f6ba60Sopenharmony_ci * the shared memory area, and calls the writer's flush ();
29606f6ba60Sopenharmony_ci
29706f6ba60Sopenharmony_ci * The interface notifies the service process to take the data from the shared memory area.
29806f6ba60Sopenharmony_ci */
29906f6ba60Sopenharmony_ciextern PluginModuleStruct g_pluginModule;
30006f6ba60Sopenharmony_ci
30106f6ba60Sopenharmony_ci#ifdef __cplusplus
30206f6ba60Sopenharmony_ci}
30306f6ba60Sopenharmony_ci#endif
30406f6ba60Sopenharmony_ci
30506f6ba60Sopenharmony_ci#endif // PLUGIN_MODULE_API_H
306