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()
34 m_context = cp_create_context(&m_status);
35 m_cp_plugins = nullptr;
38 config = Config::Getinstance();
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);
214 //printf("plugin size = %d\n",m_plugins.size());
220 int CpluffAdapter::registerPlugin(const std::string path)
222 //printf("CpluffAdapter register\n");
227 printf("CpluffAdapter path == null\n");
232 if (installPlugin(path))
234 flag = loadPluginInfoToManager(path);
239 int CpluffAdapter::registerAllPlugin(const std::string path)
241 //printf("CpluffAdapter register\n");
245 printf("CpluffAdapter path == null\n");
250 if (findPluginRecursive(path))
252 flag = loadPluginInfoToManager(path);
257 int CpluffAdapter::unregisterPlugin(Plugin *const plugin)
260 char *argv = nullptr;
263 for (unsigned int i = 0; i < m_plugins.size(); i++)
266 if (*plugin == m_plugins[i])
268 id = m_plugins[i].getID().c_str();
269 argv = const_cast<char *>(id.c_str());
270 if ((status = cp_uninstall_plugin(m_context, argv)) != 0)
272 printf("unregister failed\n");
275 m_plugins.erase(m_plugins.begin() + i);
281 int CpluffAdapter::unregisterAllPlugin(void)
284 char *argv = nullptr;
289 id = m_plugins[0].getID().c_str();
290 argv = const_cast<char *>(id.c_str());
291 if ((status = cp_uninstall_plugin(m_context, argv)) != 0)
293 printf("unregister failed\n");
296 m_plugins.erase(m_plugins.begin());
297 if (m_plugins.size() == 0)
306 std::vector<Plugin> &CpluffAdapter::getAllPlugins(void)
311 std::vector<Plugin> *CpluffAdapter::findPlugins(const std::string key, const std::string value)
313 std::vector<Plugin> *re_plugins;
315 re_plugins = new std::vector<Plugin>;
317 for (unsigned int i = 0; i < m_plugins.size(); i++)
319 if (!m_plugins[i].getValueByAttribute(key).compare(value))
321 re_plugins->push_back(m_plugins[i]);
328 Plugin *CpluffAdapter::getPlugin(const std::string plugID)
330 for (unsigned int i = 0; i < m_plugins.size(); i++)
332 if (!(m_plugins[i].getID().compare(plugID)))
334 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)
453 id = plugin->getID();
454 for (unsigned int i = 0 ; i < m_plugins.size(); i++)
456 if (*plugin == m_plugins[i])
458 ctx = cpi_new_context((cp_plugin_t *)hnode_get(hash_lookup(m_context->env->plugins, id.c_str())),
459 m_context->env, &status);
460 //cp_define_symbol(ctx, "START_ARGUMENT", arg);
461 //printf("start ocplatform address : %x\n", arg);
463 if ((status = cp_start_plugin(m_context, (char *)id.c_str()) ) != CP_OK)
465 printf("API function CpluffAdapter::start() faild with error code.\n");
470 printf("start plug-in %s.\n", (char *)id.c_str());
479 int CpluffAdapter::stop(Plugin *const plugin)
484 id = plugin->getID();
485 printf("stop plugin id = %s\n", id.c_str());
486 if ((status = cp_stop_plugin(m_context, (char *)id.c_str())) != CP_OK)
488 printf("API function CpluffAdapter::stop() faild with error code.\n");
493 printf("Stop plug-in %s.\n", (char *)id.c_str());
499 bool CpluffAdapter::isStarted(Plugin *plugin)
502 cp_plugin_state_t state;
504 id = plugin->getID();
506 state = cp_get_plugin_state(m_context, (char *)id.c_str());
507 if (state == CP_PLUGIN_STARTING || state == CP_PLUGIN_ACTIVE)
513 //Auto plugin detection is disabled
515 void CpluffAdapter::observePluginPath(void *str)
517 //printf("start observePluginPath\n");
522 char *str1 = (char *)str;
523 std::string original_path(str1);
524 //printf("Directory is %s\n",(char*)str1));
525 char buffer[BUF_LEN];
531 printf("inotify_init\n");
534 wd = inotify_add_watch( fd, str1,
535 IN_MODIFY | IN_CREATE | IN_DELETE | IN_DELETE_SELF
536 | IN_MOVE_SELF | IN_MOVED_FROM | IN_MOVED_TO);
540 //printf("read start\n");
542 length = read( fd, buffer, BUF_LEN );
546 printf("observePluginPath read\n");
548 std::string filepath = original_path;
549 //printf("filepath = %s\n",filepath.c_str());
552 if (i < (signed)(BUF_LEN - ( sizeof( struct inotify_event) + 16)) && i > -1)
554 struct inotify_event *event = ( struct inotify_event *) &buffer[ i ];
556 if ((event->mask & IN_CREATE) || (event->mask & IN_MODIFY) || (event->mask & IN_MOVED_TO))
558 registerAllPlugin(str1);
563 //filepath += std::string(event->name);
564 std::vector<Plugin> *resource_plugin = findPlugins("Path", filepath.c_str()); //add foldername
566 //printf("plugin size is %d\n",resource_plugin->size());
567 //printf("plugin file path is %s\n",resource_plugin->());
568 if (resource_plugin->size() == 1)
570 unregisterPlugin(&(resource_plugin->at(0)));
574 registerAllPlugin(str1);
576 delete(resource_plugin);
577 resource_plugin = nullptr;
579 //printf("observePluginPath path = %s \n",str1);
580 //printf("observePluginPath directory name = %s \n",event->name);
581 i += EVENT_SIZE + event->len;
587 ( void ) inotify_rm_watch( fd, wd );
588 ( void ) close( fd );
589 //printf("observePluginPath end\n");
592 const std::string CpluffAdapter::getState(const std::string plugID)
594 return state_to_string(cp_get_plugin_state(m_context, plugID.c_str()));