221c305fad170e6d875cef4739d477f45f321f03
[platform/upstream/iotivity.git] / service / protocol-plugin / plugin-manager / src / CpluffAdapter.h
1 //******************************************************************
2 //
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 /// @file CpluffAdapter.h
22
23 /// @brief
24
25 #ifndef __CPLUFFADAPTER_H
26 #define __CPLUFFADAPTER_H
27 #ifndef DLOPEN_POSIX
28 #define DLOPEN_POSIX
29 #endif
30
31 #include <vector>
32 #include <dirent.h>
33 #include <sys/stat.h>
34 #include <errno.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37 #include <internal.h>
38
39 #include "Plugin.h"
40 #include "Config.h"
41
42 #define BUF_LEN     (int)( 1024 * ( EVENT_SIZE + 16 ) )
43
44 namespace OIC
45 {
46     /**
47     * @brief    After installing a plug-in in a directory, each plug-ins can be managed by this class.
48     *
49     *
50     */
51     class CpluffAdapter
52     {
53         public:
54             /**
55             * A function to register pluins in the path.
56             * This function will load plugins in plugin manager table.
57             *
58             * @param path plugin file path to be registered.
59             * @return int, 1 is success, 0 is fail.
60             *
61             * NOTE:
62             *
63             */
64             int registerPlugin(const std::string path);
65
66
67             /**
68             * A function to register pluins in the path.
69             * This function will load plugins in plugin manager table.
70             *
71             * @param path plugin file path to be registered.
72             * recursive load plugins sub folders recursively.
73             * @return int, 1 is success, 0 is fail.
74             *
75             * NOTE:
76             *
77             */
78             int registerAllPlugin(const std::string path);
79
80
81             /**
82             * Unregister plugin.
83             *
84             * @param plugin plugin object to be unregistered.
85             * @return int, 1 is success, 0 is fail.
86             */
87             int unregisterPlugin(Plugin *const plugin);
88
89
90             /**
91             * Unregister All plugin.
92             *
93             * @return int, 1 is success, 0 is fail.
94             */
95             int unregisterAllPlugin(void);
96
97
98             /**
99             * get all plugins which currently registered.
100             *
101             * @return vector of currently registered plugins
102             *
103             */
104             std::vector<Plugin> &getAllPlugins(void);
105
106
107             /**
108             * find plugins which have the key and value
109             *
110             * @return vector of currently registered plugins
111             */
112             std::vector<Plugin> *findPlugins(const std::string key, const std::string value);
113
114
115             /**
116             * Get plugin which has the id
117             *
118             * @param pluginID plugin id to find
119             * @return Plugin instance
120             */
121             //Plugin *getPlugin(const std::string pluginID);
122
123             /**
124             * Start plugin
125             * This function will load dynamic plugin library on memory and call start function of plugin to be initialized.
126             *
127             * @param Plugin
128             * @param Platform pointer.
129             * @return int, 1 is success, 0 is fail.
130             */
131             int start(Plugin *const plugin, void *const arg);
132
133
134             /**
135             * Stop Plugin.
136             * This function will call stop function of plugin and unload dynamic plugin library from memory.
137             *
138             * @param Plugin
139             * @return int, 1 is success, 0 is fail.
140             */
141             int stop(Plugin *const plugin);
142
143
144             /**
145             * Check whether the plugin started or not
146             *
147             * @param Plugin to identify the Starting.
148             * @return true if started, false is stop.
149             *
150             */
151             bool isStarted(Plugin *plugin);
152
153
154             /**
155             * Get Plugin state.
156             *
157             * @param Plugin ID
158             * @return Plugin state.
159             */
160             const std::string getState(const std::string plugID);
161
162             /**
163             * print whole plugin info.
164             *
165             * @param cpluff plugins
166             */
167             void printPluginList();
168
169
170             /**
171             *
172             * new Singleton pattern instance.
173             *
174             * @return OICPluginManager pointer Address.
175             */
176             static CpluffAdapter *Getinstance(void *args = NULL)
177             {
178                 if (NULL == s_pinstance)
179                 {
180                     s_pinstance = new CpluffAdapter(args);
181                 }
182
183                 return s_pinstance;
184             }
185
186
187
188         private:
189
190             Config *config;
191             typedef std::map<std::string, bool> File_list;
192             std::vector<Plugin> m_plugins;
193             cp_context_t *m_context;
194             cp_status_t m_status;
195             cp_plugin_info_t **m_cp_plugins;
196             cp_plugin_info_t *m_plugin;
197             static CpluffAdapter *s_pinstance;
198
199             /**
200             * Constructor for CpluffAdapter.
201             * During construction time, all plugins under the root plugin path will be loaded.
202             *
203             */
204             CpluffAdapter(void *args = NULL);
205
206             /**
207             * Virtual destructor
208             */
209             virtual ~CpluffAdapter(void);
210
211             /**
212             * delete Singleton pattern instance.
213             */
214             static void deleteinstance()
215             {
216                 if (NULL != s_pinstance)
217                 {
218                     delete s_pinstance;
219                     s_pinstance = NULL;
220                 }
221             }
222
223             /**
224             * detect plugins(add, delete, move)
225             *
226             * @param plugin file path.
227             * @return void
228             */
229             //void observePluginPath(void *);
230
231
232             /**
233             * change Number to String.
234             *
235             * @param int.
236             * @return State String.
237             */
238             const char *state_to_string(int state);
239
240             /**
241             * Get whole "SO" file list.
242             *
243             * @param OUT, SO file list.
244             * @param Root path.
245             * @return true or false.
246             */
247             bool getFileList(File_list &list, const std::string strDir);
248
249             /**
250             * install plugin using c-pluff.
251             *
252             * @param Root path.
253             * @return int, 1 is success, 0 is fail.
254             */
255             int installPlugin(const std::string path);
256
257
258             /**
259             * find Plugin and install plugin.(Recursive)
260             *
261             * @param Root path.
262             * @return int, 1 is success, 0 is fail.
263             */
264             int findPluginRecursive(const std::string path);
265
266             /**
267             * load Plugin information to PluginManager table.
268             *
269             * @param path to observe
270             * @return int, 1 is success, 0 is fail.
271             */
272             int loadPluginInfoToManager(const std::string path);
273     };
274 }
275
276 #endif //__CPLUFFADAPTER_H