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