Skip to content

Commit a2a5d89

Browse files
add common
1 parent f655b62 commit a2a5d89

File tree

3 files changed

+335
-54
lines changed

3 files changed

+335
-54
lines changed

inc/plugin.h

Lines changed: 151 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
//
2-
// Created by andrew on 2022/2/21.
3-
//
1+
/**
2+
* @file Plugin.h
3+
* @brief Definitions and interfaces for Plugin and related types.
4+
*
5+
* This file contains the enum definitions and class declarations
6+
* for Plugin and related components. The Plugin class provides
7+
* an interface for initializing, starting, stopping, resetting,
8+
* and communicating with plugins.
9+
*/
410

511
#ifndef DLMS_PLUGIN_H
612
#define DLMS_PLUGIN_H
@@ -13,72 +19,201 @@
1319
* 实际项目中随时可以替换成动态库插件形式,这里为demo代码采用直接链接编译的方式使用
1420
* */
1521

16-
// 服务函数根据类型进行参数和返回值的确认
22+
/**
23+
* @brief Enum representing the type of data a plugin service might handle.
24+
*
25+
* This enum is used to specify the type of input and output data
26+
* the service functions in a plugin can process.
27+
*/
1728
enum PluginServiceType {
1829
String = 0x1,
1930
JSON = 0x2,
2031
Binary = 0x4,
2132
Integer = 0x8,
2233
};
2334

35+
/**
36+
* @brief Enum representing different notification types.
37+
*
38+
* This enum is used to specify the type of notifications
39+
* that can be issued by the plugin.
40+
*/
2441
enum NotifyTypes {
2542
NetBroken = 0,
2643
Quit = 1,
2744
};
2845

2946
class IContext;
47+
/**
48+
* @brief Interface class for Plugin.
49+
*
50+
* This class provides an abstract interface for plugin management,
51+
* including initialization, starting, stopping, resetting, and
52+
* communication with plugins.
53+
*/
3054
class Plugin {
3155
public:
56+
/**
57+
* @brief Enum representing the status of a plugin.
58+
*
59+
* This enum is used to specify the current status of the plugin.
60+
*/
3261
enum PluginStatus : uint8_t {
33-
PLUGIN_UNKNOWN = 0,
34-
PLUGIN_INIT = 1,
35-
PLUGIN_RUNNING = 2,
36-
PLUGIN_STOP = 3,
37-
PLUGIN_Reset = 4,
62+
PLUGIN_UNKNOWN = 0, ///< Unknown status
63+
PLUGIN_INIT = 1, ///< Plugin initialized
64+
PLUGIN_RUNNING = 2, ///< Plugin is running
65+
PLUGIN_STOP = 3, ///< Plugin stopped
66+
PLUGIN_Reset = 4, ///< Plugin reset
3867
};
3968
//
4069

4170
public:
4271
virtual ~Plugin() = default;
4372

4473
/**
45-
* @brief 插件初始化
74+
* @brief Initialize the plugin.
4675
*
47-
* @param lpIDlms 主框架对象指针
76+
* This method performs the necessary initialization for the plugin.
77+
*
78+
* @param lpIDlms Pointer to the main framework object.
79+
* @param pluginName Name of the plugin.
80+
* @return Status code indicating success or failure.
4881
*/
4982
virtual Status Init(IContext *lpIDlms, std::string pluginName) = 0;
5083

84+
/**
85+
* @brief Start the plugin.
86+
*
87+
* This method starts the plugin execution.
88+
*
89+
* @return Status code indicating success or failure.
90+
*/
5191
virtual Status Start() = 0;
5292

93+
/**
94+
* @brief Stop the plugin.
95+
*
96+
* This method stops the plugin execution.
97+
*
98+
* @return Status code indicating success or failure.
99+
*/
53100
virtual Status Stop() = 0;
54101

102+
/**
103+
* @brief Reset the plugin.
104+
*
105+
* This method resets the plugin state.
106+
*
107+
* @return Status code indicating success or failure.
108+
*/
55109
virtual Status Reset() = 0;
56110

57-
// 获取插件名字
111+
/**
112+
* @brief Get the plugin name.
113+
*
114+
* This method returns the name of the plugin.
115+
*
116+
* @return The name of the plugin.
117+
*/
58118
virtual std::string GetName() = 0;
59119

120+
/**
121+
* @brief Send a synchronous message to the plugin.
122+
*
123+
* This method sends a synchronous message to the plugin and waits for a response.
124+
*
125+
* @param in Type of the input data.
126+
* @param lpInData Pointer to the input data.
127+
* @param out Type of the output data.
128+
* @param lpOutData Pointer to the output data.
129+
* @return Status code indicating success or failure.
130+
*/
60131
virtual Status SendSync(PluginServiceType in, void *lpInData, PluginServiceType out, void *lpOutData) = 0;
61132

133+
/**
134+
* @brief Send an asynchronous message to the plugin.
135+
*
136+
* This method sends an asynchronous message to the plugin without waiting for a response.
137+
*
138+
* @param in Type of the input data.
139+
* @param lpInData Pointer to the input data.
140+
* @return Status code indicating success or failure.
141+
*/
62142
virtual Status SendAsync(PluginServiceType in, void *lpInData) = 0;
63143

144+
/**
145+
* @brief Notify the plugin with input data.
146+
*
147+
* This method sends a notification to the plugin with the specified input data.
148+
*
149+
* @param in Type of the input data.
150+
* @param lpInData Pointer to the input data.
151+
* @return Status code indicating success or failure.
152+
*/
64153
virtual Status Notify(PluginServiceType in, void *lpInData) = 0;
65154

155+
/**
156+
* @brief Post a message to the plugin.
157+
*
158+
* This method posts a message to the plugin.
159+
*
160+
* @param in Type of the input data.
161+
* @param lpInData Pointer to the input data.
162+
* @return Status code indicating success or failure.
163+
*/
66164
virtual Status PostMessage(PluginServiceType in, void *lpInData) = 0;
67165

166+
/**
167+
* @brief Check if the plugin supports a certain service type.
168+
*
169+
* This method checks whether the plugin supports the specified service type.
170+
*
171+
* @param serviceType The service type to check.
172+
* @return true if the service type is supported, false otherwise.
173+
*/
68174
virtual bool Support(PluginServiceType serviceType) = 0;
69175

70176
protected:
71-
volatile PluginStatus pluginStatus{PLUGIN_UNKNOWN};
72-
std::string pluginType;
177+
volatile PluginStatus pluginStatus{PLUGIN_UNKNOWN}; ///< The current status of the plugin
178+
std::string pluginType; ///< The type of the plugin
73179
};
74180

75181
#ifdef __cplusplus
76182
extern "C" {
183+
184+
/**
185+
* @brief Get the version of the plugin framework.
186+
*
187+
* This function returns a string representing the version of the
188+
* plugin framework being used.
189+
*
190+
* @return A C-string containing the version information.
191+
*/
77192
const char *GetVersion();
193+
194+
/**
195+
* @brief Create a new plugin instance.
196+
*
197+
* This function creates a new instance of a plugin based on the provided
198+
* plugin type. The created plugin instance is returned as a pointer.
199+
*
200+
* @param pluginType The type of the plugin to be created.
201+
* @return A pointer to the newly created plugin instance.
202+
*/
78203
Plugin *CreatePlugin(const char *pluginType);
204+
205+
/**
206+
* @brief Delete an existing plugin instance.
207+
*
208+
* This function deletes an existing plugin instance, releasing any
209+
* resources associated with it.
210+
*
211+
* @param plugin The pointer to the plugin instance to be deleted.
212+
*/
79213
void DeletePlugin(Plugin *plugin);
80-
}
81214

82-
#endif
215+
} // extern "C"
216+
#endif // __cplusplus
217+
83218

84219
#endif //DLMS_PLUGIN_H

utils/json_proxy.h

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
//
2-
// Created by andrew on 2022/6/10.
3-
//
1+
/**
2+
* @file JsonProxy.h
3+
* @brief Definition of the JsonProxy class.
4+
*
5+
* This file contains the declaration of the JsonProxy class, which provides
6+
* various constructors for handling JSON objects and files.
7+
*/
48

59
#ifndef DLMS_JSON_PROXY_H
610
#define DLMS_JSON_PROXY_H
@@ -14,31 +18,92 @@
1418
#include "status.h"
1519

1620
using json = nlohmann::json;
17-
21+
/**
22+
* @brief A class to handle JSON objects.
23+
*
24+
* This class provides constructors to initialize a JSON object from various
25+
* sources such as another JSON object, a JSON file, or a string containing
26+
* the file path and filename.
27+
*/
1828
class JsonProxy {
1929
public:
30+
/**
31+
* @brief Default constructor.
32+
*
33+
* This constructor initializes a JsonProxy object with an empty JSON object.
34+
*/
2035
JsonProxy() = default;
2136

37+
/**
38+
* @brief Constructor to initialize with a JSON object.
39+
*
40+
* This constructor initializes a JsonProxy object with the given JSON object,
41+
* moving it into the internal JSON object.
42+
*
43+
* @param json The JSON object to initialize with.
44+
*/
2245
explicit JsonProxy(json json) : jsonObj(std::move(json)) {}
2346

47+
/**
48+
* @brief Constructor to initialize with a reference to a JSON object.
49+
*
50+
* This constructor initializes a JsonProxy object with a reference to the
51+
* provided JSON object.
52+
*
53+
* @param json The JSON object reference to initialize with.
54+
*/
2455
explicit JsonProxy(json &json) : jsonObj(json) {}
2556

57+
/**
58+
* @brief Constructor to initialize with an r-value reference to a JSON object.
59+
*
60+
* This constructor initializes a JsonProxy object with the given JSON object,
61+
* moving it into the internal JSON object.
62+
*
63+
* @param json The JSON object r-value reference to initialize with.
64+
*/
2665
explicit JsonProxy(json &&json) : jsonObj(json) {}
2766

67+
/**
68+
* @brief Constructor to initialize with a file path and filename.
69+
*
70+
* This constructor initializes a JsonProxy object by reading the specified
71+
* JSON file and parsing it into the internal JSON object.
72+
*
73+
* @param path The file path where the JSON file is located.
74+
* @param filename The name of the JSON file.
75+
*/
2876
JsonProxy(std::string &path, std::string &filename) {
2977
std::string file = path + "/" + filename;
3078
std::ifstream f(file.c_str());
3179
jsonObj = json::parse(f);
3280
}
3381

34-
explicit JsonProxy(std::string &filename) {}
82+
/**
83+
* @brief Constructor to initialize with a filename.
84+
*
85+
* This constructor initializes a JsonProxy object by reading the specified
86+
* JSON file and parsing it into the internal JSON object. (Implementation TODO)
87+
*
88+
* @param filename The name of the JSON file.
89+
*/
90+
explicit JsonProxy(std::string &filename) {
91+
// Implementation logic for this constructor could be added here
92+
}
3593

94+
/**
95+
* @brief Get the internal JSON object.
96+
*
97+
* This method returns a reference to the internal JSON object.
98+
*
99+
* @return A reference to the internal JSON object.
100+
*/
36101
json &GetJsonObj() {
37102
return jsonObj;
38103
}
39104

40105
private:
41-
json jsonObj;
106+
json jsonObj; ///< The internal JSON object managed by this proxy.
42107
};
43108

44109

0 commit comments

Comments
 (0)