1 //******************************************************************
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 /// @file CpluffAdapter.cpp
25 #include "CpluffAdapter.h"
29 CpluffAdapter *CpluffAdapter::s_pinstance;
31 CpluffAdapter::CpluffAdapter(void *args)
34 m_context = cp_create_context(&m_status);
35 m_cp_plugins = nullptr;
38 config = Config::Getinstance(args);
39 std::string pluginpath = config->getPluginPath();
42 printf("Current path is %s\n", pluginpath.c_str());
46 fprintf(stderr, "Pluing path does not exist\n");
49 registerAllPlugin(pluginpath);
52 CpluffAdapter::~CpluffAdapter(void)
54 cp_release_info(m_context, m_cp_plugins);
55 //Auto plugin detection is disabled
56 //m_thread_g.interrupt_all();
57 //m_thread_g.join_all();
58 s_pinstance->deleteinstance();
59 s_pinstance = nullptr;
62 int CpluffAdapter::installPlugin(const std::string path)
64 char *argv = const_cast<char *>(path.c_str());
66 if ((m_plugin = cp_load_plugin_descriptor(m_context, argv, &m_status)) == nullptr)
68 printf("cp_load_plugin_descriptor failed\n");
71 else if ((m_status = cp_install_plugin(m_context, m_plugin)) != 0)
73 if (m_status == CP_ERR_CONFLICT)
75 printf("Plugin is already installed or conflicts symbol.\n");
77 printf("cp_install_plugin failed : %d \n" , m_status);
85 int CpluffAdapter::findPluginRecursive(const std::string path)
87 ////////FILELIST////////////
89 getFileList(file_list, path);
90 File_list::iterator itr;
92 for (itr = file_list.begin(); itr != file_list.end(); itr++)
94 if (itr->second == true)
96 //printf("[Direcotry] %s\n",itr->first.c_str());
100 //printf("[FILE] = %s\n" , itr->first.c_str());
101 std::string filepath = itr->first.c_str();
103 for (unsigned int i = 0 ; i < filepath.size(); i++)
105 if (filepath.at(i) == '/')
110 std::string filename = filepath.substr(0, count );
111 if (!installPlugin(filename))
113 printf("installPlugin failed path : %s \n", filename.c_str());
121 int CpluffAdapter::loadPluginInfoToManager(const std::string path)
123 //get plugins information.
124 if ((m_cp_plugins = cp_get_plugins_info(m_context, &m_status, nullptr)) == nullptr)
126 printf("cp_get_plugins_infor failed\n");
134 for (int i = 0 ; m_cp_plugins[i] != nullptr; i++)
136 Plugin *plugin = new Plugin;
137 plugin->setValue("Path", m_cp_plugins[i]->plugin_path);
138 plugin->setValue("Language", "CPP");
139 //printf("add filepath %s\n",m_cp_plugins[i]->plugin_path);
140 if (m_cp_plugins[i]->identifier != nullptr)
142 plugin->setValue("Id", m_cp_plugins[i]->identifier );
146 plugin->setValue("Id", "");
149 if (m_cp_plugins[i]->url != nullptr)
151 plugin->setValue("Url", m_cp_plugins[i]->url);
155 plugin->setValue("Url", "");
158 if (m_cp_plugins[i]->name != nullptr)
160 plugin->setValue("Name", m_cp_plugins[i]->name);
164 plugin->setValue("Name", "");
167 if (m_cp_plugins[i]->resourcetype != nullptr)
169 plugin->setValue("ResourceType", m_cp_plugins[i]->resourcetype);
173 plugin->setValue("ResourceType", "");
176 if (m_cp_plugins[i]->version != nullptr)
178 plugin->setValue("Version", m_cp_plugins[i]->version);
182 plugin->setValue("Version", "");
185 bool plugin_compare_flag = true;
186 for (unsigned int i = 0 ; i < m_plugins.size(); i++)
188 if (*plugin == m_plugins[i])
191 plugin_compare_flag = false;
195 if (plugin_compare_flag)
197 //Auto plugin detection is disabled
201 boost::thread *t = new boost::thread(boost::bind(&CpluffAdapter::observePluginPath,
202 //this, (void *)path.c_str()));
203 this, (void *)m_cp_plugins[i]->plugin_path));
204 m_thread_g.add_thread(t);
208 printf("thread throw exception\n");
211 m_plugins.push_back(*plugin);
219 int CpluffAdapter::registerPlugin(const std::string path)
225 printf("CpluffAdapter path == null\n");
230 if (installPlugin(path))
232 flag = loadPluginInfoToManager(path);
237 int CpluffAdapter::registerAllPlugin(const std::string path)
242 printf("CpluffAdapter path == null\n");
247 if (findPluginRecursive(path))
249 flag = loadPluginInfoToManager(path);
254 int CpluffAdapter::unregisterPlugin(Plugin *const plugin)
257 char *argv = nullptr;
260 for (unsigned int i = 0; i < m_plugins.size(); i++)
263 if (*plugin == m_plugins[i])
265 id = m_plugins[i].getID().c_str();
266 argv = const_cast<char *>(id.c_str());
267 if ((status = cp_uninstall_plugin(m_context, argv)) != 0)
269 printf("unregister failed\n");
272 m_plugins.erase(m_plugins.begin() + i);
278 int CpluffAdapter::unregisterAllPlugin(void)
281 char *argv = nullptr;
286 id = m_plugins[0].getID().c_str();
287 argv = const_cast<char *>(id.c_str());
288 if ((status = cp_uninstall_plugin(m_context, argv)) != 0)
290 printf("unregister failed\n");
293 m_plugins.erase(m_plugins.begin());
294 if (m_plugins.size() == 0)
303 std::vector<Plugin> &CpluffAdapter::getAllPlugins(void)
308 std::vector<Plugin> *CpluffAdapter::findPlugins(const std::string key, const std::string value)
310 std::vector<Plugin> *re_plugins;
312 re_plugins = new std::vector<Plugin>;
314 for (unsigned int i = 0; i < m_plugins.size(); i++)
316 std::string attributeValue = m_plugins[i].getValueByAttribute(key);
317 if (!attributeValue.empty() && !attributeValue.compare(value))
319 re_plugins->push_back(m_plugins[i]);
327 Plugin *CpluffAdapter::getPlugin(const std::string plugID)
329 for (unsigned int i = 0; i < m_plugins.size(); i++)
331 if (!(m_plugins[i].getID().compare(plugID)))
333 return &(m_plugins[i]);
341 bool CpluffAdapter::getFileList(File_list &list, const std::string strDir)
343 struct stat statinfo;
344 memset(&statinfo, 0, sizeof(statinfo));
345 std::string path = strDir;
346 if (0 != lstat(path.c_str(), &statinfo))
348 printf("OICPlugManager lstat is 0\n");
351 if (!S_ISDIR(statinfo.st_mode))
353 printf("%s is not directory", strDir.c_str());
358 struct dirent *entry;
360 if ((dir = opendir(strDir.c_str())) == nullptr)
362 printf("%s open error", strDir.c_str());
366 while ((entry = readdir(dir)) != nullptr)
368 memset(&statinfo, 0, sizeof(statinfo));
369 std::string strFilePath = strDir + "/" + entry->d_name;
370 while (strFilePath.find("//") != std::string::npos)
371 strFilePath.replace(strFilePath.find("//"), 2, "/");
373 if (0 != lstat(strFilePath.c_str(), &statinfo))
375 printf("OICPlugManager lstat is 0\n");
380 if (S_ISDIR(statinfo.st_mode))
382 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
385 list.insert(std::pair<std::string, bool>(strFilePath, true));
386 std::string strSubDir = strDir + "/" + entry->d_name;
387 getFileList(list, strSubDir);
391 int size = strFilePath.size();
392 std::string so = strFilePath.substr(size - 2, size - 1);
395 list.insert(std::pair<std::string, bool>(strFilePath, false));
404 const char *CpluffAdapter::state_to_string(int state)
425 void CpluffAdapter::printPluginList()
427 const char format[] = " %-30s %-15s %-15s %-20s\n";
428 printf("\n====== Plugins List ======\n");
435 for (int i = 0; m_cp_plugins[i] != nullptr; i++)
438 m_cp_plugins[i]->identifier,
439 m_cp_plugins[i]->name != nullptr ? m_cp_plugins[i]->name : "",
440 state_to_string(cp_get_plugin_state(m_context, m_cp_plugins[i]->identifier)),
441 m_cp_plugins[i]->resourcetype != nullptr ? m_cp_plugins[i]->resourcetype : "");
446 int CpluffAdapter::start(Plugin *const plugin, void *const arg)
451 id = plugin->getID();
452 for (unsigned int i = 0 ; i < m_plugins.size(); i++)
454 if (*plugin == m_plugins[i])
456 if ((status = cp_start_plugin(m_context, (char *)id.c_str()) ) != CP_OK)
458 printf("API function CpluffAdapter::start() faild with error code.\n");
463 printf("start plug-in %s.\n", (char *)id.c_str());
472 int CpluffAdapter::stop(Plugin *const plugin)
477 id = plugin->getID();
478 printf("stop plugin id = %s\n", id.c_str());
479 if ((status = cp_stop_plugin(m_context, (char *)id.c_str())) != CP_OK)
481 printf("API function CpluffAdapter::stop() faild with error code.\n");
486 printf("Stop plug-in %s.\n", (char *)id.c_str());
492 bool CpluffAdapter::isStarted(Plugin *plugin)
495 cp_plugin_state_t state;
497 id = plugin->getID();
499 state = cp_get_plugin_state(m_context, (char *)id.c_str());
500 if (state == CP_PLUGIN_STARTING || state == CP_PLUGIN_ACTIVE)
507 // Auto plugin detection is disabled
509 void CpluffAdapter::observePluginPath(void *str)
515 char *str1 = (char *)str;
516 std::string original_path(str1);
517 char buffer[BUF_LEN];
523 printf("inotify_init\n");
526 wd = inotify_add_watch( fd, str1,
527 IN_MODIFY | IN_CREATE | IN_DELETE | IN_DELETE_SELF
528 | IN_MOVE_SELF | IN_MOVED_FROM | IN_MOVED_TO);
532 //printf("read start\n");
534 length = read( fd, buffer, BUF_LEN );
538 printf("observePluginPath read\n");
540 std::string filepath = original_path;
543 if (i < (signed)(BUF_LEN - ( sizeof( struct inotify_event) + 16)) && i > -1)
545 struct inotify_event *event = ( struct inotify_event *) &buffer[ i ];
547 if ((event->mask & IN_CREATE) || (event->mask & IN_MODIFY) || (event->mask & IN_MOVED_TO))
549 registerAllPlugin(str1);
553 std::vector<Plugin> *resource_plugin = findPlugins("Path", filepath.c_str()); //add foldername
555 if (resource_plugin->size() == 1)
557 unregisterPlugin(&(resource_plugin->at(0)));
561 registerAllPlugin(str1);
563 delete(resource_plugin);
564 resource_plugin = nullptr;
566 i += EVENT_SIZE + event->len;
572 ( void ) inotify_rm_watch( fd, wd );
573 ( void ) close( fd );
577 const std::string CpluffAdapter::getState(const std::string plugID)
579 return state_to_string(cp_get_plugin_state(m_context, plugID.c_str()));