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