iotivity 0.9.0
[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 <unistd.h>
35 #include <boost/thread.hpp>
36 #include <boost/bind.hpp>
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 FelixAdapter
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             /**
164             *
165             * new Singleton pattern instance.
166             *
167             * @return OICPluginManager pointer Address.
168             */
169             static FelixAdapter *Getinstance()
170             {
171                 if (NULL == s_pinstance)
172                 {
173                     s_pinstance = new FelixAdapter();
174                 }
175
176                 return s_pinstance;
177             }
178
179
180
181         private:
182             Config *config;
183             typedef std::map<std::string, bool> File_list;
184             std::vector<Plugin> m_plugins;
185             boost::thread m_file_detect_thread;
186             /*
187             cp_context_t *m_context;
188             cp_status_t m_status;
189             cp_plugin_info_t **m_cp_plugins;
190             cp_plugin_info_t *m_plugin;
191             boost::thread_group m_thread_g;
192             std::string m_path;
193             */
194             static FelixAdapter *s_pinstance;
195
196             /**
197             * Constructor for FelixAdapter.
198             * During construction time, all plugins under the root plugin path will be loaded.
199             *
200             */
201             FelixAdapter();
202
203             /**
204             * Virtual destructor
205             */
206             virtual ~FelixAdapter(void);
207
208             /**
209             * delete Singleton pattern instance.
210             */
211             static void deleteinstance()
212             {
213                 if (NULL != s_pinstance)
214                 {
215                     delete s_pinstance;
216                     s_pinstance = NULL;
217                 }
218             }
219
220             /**
221             * detect plugins(add, delete, move)
222             *
223             * @param plugin file path.
224             * @return void
225             */
226             //void observePluginPath(void *);
227
228             /**
229             * Get whole "SO" file list.
230             *
231             * @param OUT, SO file list.
232             * @param Root path.
233             * @return true or false.
234             */
235             bool getFileList(File_list &list, const std::string strDir);
236
237             /**
238             * print whole plugin info.
239             *
240             * @param Felix plugins
241             */
242             void printPluginList(cp_plugin_info_t **plugins);
243
244             /**
245             * install plugin using c-pluff.
246             *
247             * @param Root path.
248             * @return int, 1 is success, 0 is fail.
249             */
250             int installPlugin(const std::string path);
251
252
253             /**
254             * find Plugin and install plugin.(Recursive)
255             *
256             * @param Root path.
257             * @return int, 1 is success, 0 is fail.
258             */
259             int findPluginRecursive(const std::string path);
260
261             /**
262             * load Plugin information to PluginManager table.
263             *
264             * @param path to observe
265             * @return int, 1 is success, 0 is fail.
266             */
267             int loadPluginInfoToManager(const std::string path);
268     };
269 }
270
271 #endif //__FELIXADAPTER_H__