-
Notifications
You must be signed in to change notification settings - Fork 122
/
IShell.h
391 lines (314 loc) · 14.6 KB
/
IShell.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2020 Metrological
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __ISHELL_H__
#define __ISHELL_H__
#include "IPlugin.h"
#include "ISubSystem.h"
#include <com/ICOM.h>
namespace WPEFramework {
namespace RPC {
class Object;
}
namespace PluginHost {
struct EXTERNAL IShell : virtual public Core::IUnknown {
enum { ID = RPC::ID_SHELL };
// This interface is only returned if the IShell is accessed in the main process. The interface can
// be used to instantiate new objects (COM objects) in a new process, or monitor the state of such a process.
// If this interface is requested outside of the main process, it will return a nullptr.
/* @stubgen:omit */
struct EXTERNAL ICOMLink {
struct INotification : virtual public Core::IUnknown {
virtual ~INotification() = default;
virtual void Dangling(const Core::IUnknown* source, const uint32_t interfaceId) = 0;
virtual void Revoked(const Core::IUnknown* remote, const uint32_t interfaceId) = 0;
};
virtual ~ICOMLink() = default;
virtual void Register(RPC::IRemoteConnection::INotification* sink) = 0;
virtual void Unregister(const RPC::IRemoteConnection::INotification* sink) = 0;
virtual void Register(INotification* sink) = 0;
virtual void Unregister(INotification* sink) = 0;
virtual RPC::IRemoteConnection* RemoteConnection(const uint32_t connectionId) = 0;
virtual void* Instantiate(const RPC::Object& object, const uint32_t waitTime, uint32_t& connectionId) = 0;
};
enum class startup : uint8_t {
UNAVAILABLE,
DEACTIVATED,
ACTIVATED
};
// State of the IPlugin interface associated with this shell.
enum state : uint8_t {
UNAVAILABLE,
DEACTIVATED,
DEACTIVATION,
ACTIVATED,
ACTIVATION,
PRECONDITION,
HIBERNATED,
DESTROYED
};
enum reason : uint8_t {
REQUESTED,
AUTOMATIC,
FAILURE,
MEMORY_EXCEEDED,
STARTUP,
SHUTDOWN,
CONDITIONS,
WATCHDOG_EXPIRED,
INITIALIZATION_FAILED
};
/* @stubgen:omit */
class EXTERNAL Job : public Core::IDispatch {
protected:
Job(IShell* shell, IShell::state toState, IShell::reason why)
: _shell(shell)
, _state(toState)
, _reason(why)
{
if (shell != nullptr) {
shell->AddRef();
}
}
public:
Job() = delete;
Job(const Job&) = delete;
Job& operator=(const Job&) = delete;
~Job() override
{
if (_shell != nullptr) {
_shell->Release();
}
}
public:
static Core::ProxyType<Core::IDispatch> Create(IShell* shell, IShell::state toState, IShell::reason why);
//
// Core::IDispatch implementation
// -------------------------------------------------------------------------------
void Dispatch() override
{
ASSERT(_shell != nullptr);
if (_shell != nullptr) {
switch (_state) {
case ACTIVATED:
_shell->Activate(_reason);
break;
case DEACTIVATED:
_shell->Deactivate(_reason);
break;
default:
ASSERT(false);
break;
}
}
}
private:
IShell* const _shell;
const state _state;
const reason _reason;
};
//! @{
//! =========================== ACCESSOR TO THE SHELL AROUND THE PLUGIN ===============================
//! This interface allows access to the shell that scontrolls the lifetimeof the Plugin. It's access
//! and responses is based on configuration applicable to the plugin e.g. enabling a WebServer is
//! enabling a webserver on a path, applicable to the plugin being initialized..
//! The full network URL looks like: http://<accessor>/<webprefix>/<callsign> for accessing the plugin.
//!
//! Methods to enable/disable a webserver. URLPath is "added" to the end of the path that reaches this
//! plugin, based on the DataPath.
//! URL: http://<accessor>/<webprefix>/<callsign>/<URLPath> for accessing the webpages on
//! <DataPath>/<fileSystemPath>.
//! @}
virtual void EnableWebServer(const string& URLPath, const string& fileSystemPath) = 0;
virtual void DisableWebServer() = 0;
//! Model: Returns a Human Readable name for the platform it is running on.
virtual string Model() const = 0;
//! Background: If enabled, the PluginHost is running in daemon mode
virtual bool Background() const = 0;
//! Accessor: Identifier that can be used for Core:NodeId to connect to the webbridge.
virtual string Accessor() const = 0;
//! WebPrefix: First part of the pathname in the HTTP request to select the webbridge components.
virtual string WebPrefix() const = 0;
//! Locator: The name of the binary (so) that holds the given ClassName code.
virtual string Locator() const = 0;
//! ClassName: Name of the class to be instantiated for this IShell
virtual string ClassName() const = 0;
//! Versions: Returns a JSON Array of versions (JSONRPC interfaces) supported by this plugin.
virtual string Versions() const = 0;
//! Callsign: Instantiation name of this specific plugin. It is the name given in the config for the classname.
virtual string Callsign() const = 0;
//! PersistentPath: <config:persistentpath>/<plugin:callsign>/
virtual string PersistentPath() const = 0;
//! VolatilePath: <config:volatilepath>/<plugin:callsign>/
virtual string VolatilePath() const = 0;
//! DataPath: <config:datapath>/<plugin:classname>/
virtual string DataPath() const = 0;
//! ProxyStubPath: <config:proxystubpath>/
virtual string ProxyStubPath() const = 0;
//! SystemPath: <config:systempath>/
virtual string SystemPath() const = 0;
//! SystemPath: <config:apppath>/Plugins/
virtual string PluginPath() const = 0;
//! SystemPath: <config:systemrootpath>/
virtual string SystemRootPath() const = 0;
//! SystemRootPath: Set <config:systemrootpath>/
virtual Core::hresult SystemRootPath(const string& systemRootPath) = 0;
//! Startup: <config:startup>/
virtual PluginHost::IShell::startup Startup() const = 0;
//! Startup: Set<startup,autostart,resumed states>/
virtual Core::hresult Startup(const startup value) = 0;
//! Substituted Config value
virtual string Substitute(const string& input) const = 0;
virtual bool Resumed() const = 0;
virtual Core::hresult Resumed(const bool value) = 0;
virtual string HashKey() const = 0;
virtual string ConfigLine() const = 0;
virtual Core::hresult ConfigLine(const string& config) = 0;
virtual Core::hresult Metadata(string& info /* @out */) const = 0;
//! Return whether the given version is supported by this IShell instance.
virtual bool IsSupported(const uint8_t version) const = 0;
// Get access to the SubSystems and their corrresponding information. Information can be set or get to see what the
// status of the sub systems is.
virtual ISubSystem* SubSystems() = 0;
// Notify all subscribers of this service with the given string.
// It is expected to be JSON formatted strings as it is assumed that this is for reaching websockets clients living in
// the web world that have build in functionality to parse JSON structs.
virtual void Notify(const string& message) = 0;
// Allow access to the Shells, configured for the different Plugins found in the configuration.
// Calling the QueryInterfaceByCallsign with an empty callsign will query for interfaces located
// on the controller.
virtual void Register(IPlugin::INotification* sink) = 0;
virtual void Unregister(IPlugin::INotification* sink) = 0;
virtual state State() const = 0;
virtual void* /* @interface:id */ QueryInterfaceByCallsign(const uint32_t id, const string& name) = 0;
// Methods to Activate/Deactivate and Unavailable the aggregated Plugin to this shell.
// NOTE: These are Blocking calls!!!!!
virtual Core::hresult Activate(const reason) = 0;
virtual Core::hresult Deactivate(const reason) = 0;
virtual Core::hresult Unavailable(const reason) = 0;
virtual Core::hresult Hibernate(const uint32_t timeout) = 0;
virtual reason Reason() const = 0;
// Method to access, in the main process space, the channel factory to submit JSON objects to be send.
// This method will return a error if it is NOT in the main process.
/* @stubgen:stub */
virtual uint32_t Submit(const uint32_t Id, const Core::ProxyType<Core::JSON::IElement>& response) = 0;
// Method to access, in the main space, a COM factory to instantiate objects out-of-process.
// This method will return a nullptr if it is NOT in the main process.
/* @stubgen:stub */
virtual ICOMLink* COMLink() = 0;
inline void Register(RPC::IRemoteConnection::INotification* sink)
{
ICOMLink* handler(COMLink());
// This method can only be used in the main process. Only this process, can instantiate a new process
ASSERT(handler != nullptr);
if (handler != nullptr) {
handler->Register(sink);
}
}
inline void Unregister(const RPC::IRemoteConnection::INotification* sink)
{
ICOMLink* handler(COMLink());
// This method can only be used in the main process. Only this process, can instantiate a new process
ASSERT(handler != nullptr);
if (handler != nullptr) {
handler->Unregister(sink);
}
}
inline void Register(ICOMLink::INotification* sink)
{
ICOMLink* handler(COMLink());
ASSERT(handler != nullptr);
if (handler != nullptr) {
handler->Register(sink);
}
}
inline void Unregister(ICOMLink::INotification* sink)
{
ICOMLink* handler(COMLink());
ASSERT(handler != nullptr);
if (handler != nullptr) {
handler->Unregister(sink);
}
}
inline RPC::IRemoteConnection* RemoteConnection(const uint32_t connectionId)
{
ICOMLink* handler(COMLink());
// This method can only be used in the main process. Only this process, can instantiate a new process
ASSERT(handler != nullptr);
return (handler == nullptr ? nullptr : handler->RemoteConnection(connectionId));
}
inline uint32_t EnablePersistentStorage(uint32_t permission = 0, const string& user = {}, const string& group = {})
{
return (EnableStoragePath(PersistentPath(), permission, user, group));
}
inline uint32_t EnableVolatileStorage(uint32_t permission = 0, const string& user = {}, const string& group = {})
{
return (EnableStoragePath(VolatilePath(), permission, user, group));
}
template <typename REQUESTEDINTERFACE>
REQUESTEDINTERFACE* Root(uint32_t& pid, const uint32_t waitTime, const string className, const uint32_t version = ~0)
{
void* baseInterface (Root(pid, waitTime, className, REQUESTEDINTERFACE::ID, version));
if (baseInterface != nullptr) {
return (reinterpret_cast<REQUESTEDINTERFACE*>(baseInterface));
}
return (nullptr);
}
template <typename REQUESTEDINTERFACE>
REQUESTEDINTERFACE* QueryInterfaceByCallsign(const string& name)
{
void* baseInterface(QueryInterfaceByCallsign(REQUESTEDINTERFACE::ID, name));
if (baseInterface != nullptr) {
return (reinterpret_cast<REQUESTEDINTERFACE*>(baseInterface));
}
return (nullptr);
}
private:
inline uint32_t EnableStoragePath(const string& storagePath, uint32_t permission, const string& user, const string& group)
{
uint32_t result = Core::ERROR_NONE;
// Make sure there is a path to the persitent infmration
Core::File path(storagePath);
if (path.IsDirectory() == false) {
if (Core::Directory(storagePath.c_str()).Create() != true) {
result = Core::ERROR_BAD_REQUEST;
}
}
if (result == Core::ERROR_NONE) {
if (permission) {
path.Permission(permission);
}
if (user.empty() != true) {
path.User(user);
}
if (group.empty() != true) {
path.Group(group);
}
}
return (result);
}
void* Root(uint32_t& pid, const uint32_t waitTime, const string className, const uint32_t interface, const uint32_t version = ~0);
/* @stubgen:omit */
virtual std::vector<string> GetLibrarySearchPaths(const string&) const
{
return std::vector<string> {};
}
};
} // namespace PluginHost
}
#endif //__ISHELL_H__