1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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 ******************************************************************/
20 #include "ContextRepository.h"
21 #include "rapidxml/rapidxml.hpp"
23 static inline std::string trim_both(const std::string &str)
26 int npos = str.find_first_not_of(" \t\v\n");
31 unsigned int n = (unsigned int)npos;
32 std::string tempString = n == std::string::npos ? str : str.substr(n, str.length());
34 n = tempString.find_last_not_of(" \t\v\n");
35 return n == std::string::npos ? tempString : tempString.substr(0, n + 1);
38 SSMRESULT CContextRepository::finalConstruct()
40 SSMRESULT res = SSM_E_FAIL;
42 SSM_CLEANUP_ASSERT(CreateInstance(OID_IResourceFinder, (IBase **)&m_resourceFinder));
43 SSM_CLEANUP_ASSERT(m_resourceFinder->registerResourceFinderEvent(this));
49 SSMRESULT CContextRepository::startResourceFinder()
51 SSMRESULT res = SSM_E_FAIL;
53 SSM_CLEANUP_ASSERT(m_resourceFinder->startResourceFinder());
59 SSMRESULT CContextRepository::stopResourceFinder()
61 SSMRESULT res = SSM_E_FAIL;
63 SSM_CLEANUP_ASSERT(m_resourceFinder->stopResourceFinder());
69 SSMRESULT CContextRepository::registerResourceFinderEvent(IResourceEvent *pResourceEvent)
71 m_resourceEvents.push_back(pResourceEvent);
75 void CContextRepository::finalRelease()
79 SSMRESULT CContextRepository::initRepository(std::string name, std::string type,
80 std::string pathSoftSensors, std::string pathDescription)
82 SSMRESULT res = SSM_E_FAIL;
84 std::vector<DictionaryData> dict;
89 if (pathSoftSensors.length() == 0)
91 SSM_CLEANUP_ASSERT(getCurrentPath(&m_pathSoftSensors));
92 m_pathSoftSensors.append("/");
96 m_pathSoftSensors = pathSoftSensors;
99 if (pathDescription.length() == 0)
101 SSM_CLEANUP_ASSERT(getCurrentPath(&m_pathSoftSensorsDescription));
102 m_pathSoftSensorsDescription.append("/");
103 m_pathSoftSensorsDescription.append(DEFAULT_PATH_SOFT_SENSORS);
107 m_pathSoftSensorsDescription = pathDescription;
111 SSM_CLEANUP_ASSERT(loadXMLFromFile(m_pathSoftSensorsDescription.c_str(), &dict));
112 SSM_CLEANUP_ASSERT(makeSSMResourceListForDictionaryData(dict, &m_lstSoftSensor));
118 SSMRESULT CContextRepository::loadXMLFromString(char *xmlData,
119 std::vector<DictionaryData> *dataList)
121 // use rapidxml-----------------------
122 SSMRESULT res = SSM_E_INVALIDXML;
123 rapidxml::xml_document< char > xmlDoc;
124 //xmlDoc.parse< 0 >( &xmlData.front() );
127 std::string valueStr;
129 rapidxml::xml_node< char > *item;
130 rapidxml::xml_node< char > *subItem;
131 rapidxml::xml_node< char > *subItem2;
132 rapidxml::xml_node< char > *subItem3;
134 rapidxml::xml_node< char > *root;
138 xmlDoc.parse< 0 >(xmlData);
141 root = xmlDoc.first_node();
145 throw rapidxml::parse_error("No Root Element", 0);
148 for (item = root->first_node(); item; item = item->next_sibling())
150 DictionaryData dictionaryData;
151 for (subItem = item->first_node(); subItem; subItem = subItem->next_sibling())
154 keyStr = subItem->name(); // key
155 valueStr = subItem->value(); // value
157 if (!keyStr.compare("name"))
159 dictionaryData.rootName = trim_both(valueStr);
161 ////std::cout<<keyStr << " : " << subItem->value() <<std::endl<<std::endl; //root_name
162 for (subItem2 = subItem->first_node(); subItem2; subItem2 = subItem2->next_sibling())
164 std::map<std::string, std::string> propertyMap;
165 std::vector<std::string> enterconditionVector;
167 keyStr = subItem2->name(); // key
168 valueStr = subItem2->value(); // value
170 if (!keyStr.compare("input"))
172 dictionaryData.inputs.push_back(trim_both(valueStr));
174 ////std::cout<<name << " :: " << subItem2->value() <<std::endl<<std::endl;
175 for (subItem3 = subItem2->first_node(); subItem3; subItem3 = subItem3->next_sibling())
177 std::string newKeyStr = subItem3->name(); // key
178 valueStr = subItem3->value(); // value
180 if (!keyStr.compare("attribute") || !keyStr.compare("output"))
182 propertyMap.insert(std::make_pair(trim_both(newKeyStr), trim_both(valueStr)));
185 if (!keyStr.compare("attribute"))
187 dictionaryData.attributeProperty.push_back(propertyMap);
189 else if (!keyStr.compare("output"))
191 dictionaryData.outputProperty.push_back(propertyMap);
197 dictionaryData.app_input_count = std::to_string((long long)dictionaryData.app_inputs.size());
198 dictionaryData.input_count = std::to_string((long long)dictionaryData.inputs.size());
199 dictionaryData.attribute_property_count = std::to_string((long long)dictionaryData.attribute_property.size());
200 dictionaryData.output_property_count = std::to_string((long long)dictionaryData.output_property.size());
203 dataList->push_back(dictionaryData);
208 catch (rapidxml::parse_error &e)
210 SSM_CLEANUP_ASSERT(SSM_E_INVALIDXML);
217 SSMRESULT CContextRepository::loadXMLFromFile(std::string descriptionFilePath,
218 std::vector<DictionaryData> *dataList)
220 SSMRESULT res = SSM_E_FAIL;
221 std::basic_ifstream< char > xmlFile(descriptionFilePath.c_str());
226 xmlFile.seekg(0, std::ios::end);
227 unsigned int size = (unsigned int)xmlFile.tellg();
230 std::vector< char > xmlData(size + 1);
233 xmlFile.read(&xmlData.front(), (std::streamsize)size);
235 SSM_CLEANUP_ASSERT(loadXMLFromString(&xmlData.front(), dataList));
239 //let work with no soft sensor manager
247 SSMRESULT CContextRepository::getSoftSensorList(std::vector<ISSMResource *> *pSoftSensorList)
249 for (size_t i = 0; i < m_lstSoftSensor.size(); i++)
251 pSoftSensorList->push_back(m_lstSoftSensor.at(i));
257 SSMRESULT CContextRepository::getPrimitiveSensorList(std::vector<ISSMResource *>
258 *pPrimitiveSensorList)
260 for (size_t i = 0; i < m_lstPrimitiveSensor.size(); i++)
262 pPrimitiveSensorList->push_back(m_lstPrimitiveSensor.at(i));
268 SSMRESULT CContextRepository::onResourceFound(ISSMResource *pSensor)
270 m_lstPrimitiveSensor.push_back(pSensor);
272 for (size_t i = 0; i < m_resourceEvents.size(); i++)
274 m_resourceEvents.at(i)->onResourceEvent(SSM_EVENT_ADDED, pSensor, "");
280 SSMRESULT CContextRepository::onResourceLost(ISSMResource *pSensor)
282 std::vector<ISSMResource *>::iterator itor;
283 itor = std::find(m_lstPrimitiveSensor.begin(), m_lstPrimitiveSensor.end(), pSensor);
285 if (itor != m_lstPrimitiveSensor.end())
287 m_lstPrimitiveSensor.erase(itor);
294 SSMRESULT CContextRepository::startObserveResource(ISSMResource *pSensor, IEvent *pEvent)
296 return m_resourceFinder->startObserveResource(pSensor, pEvent);
299 SSMRESULT CContextRepository::stopObserveResource(ISSMResource *pSensor)
301 return m_resourceFinder->stopObserveResource(pSensor);
305 SSMRESULT CContextRepository::makeSSMResourceListForDictionaryData(std::vector<DictionaryData>
306 dataList, std::vector<ISSMResource *> *pList)
308 SSMRESULT res = SSM_E_FAIL;
310 for (unsigned int i = 0 ; i < dataList.size() ; ++i)
312 ISSMResource *pResource = new ISSMResource();
313 pResource->location = SENSOR_LOCATION_LOCAL;
314 pResource->type = dataList[i].rootName;
315 pResource->name = std::string("coap://127.0.0.1/") + dataList[i].rootName;
316 pResource->ip = "coap://127.0.0.1/";
318 pResource->inputList = dataList[i].inputs;
320 for (unsigned int j = 0; j < dataList[i].outputProperty.size(); ++j)
322 pResource->outputProperty.push_back(dataList[i].outputProperty[j]);
324 for (unsigned int j = 0; j < dataList[i].attributeProperty.size(); ++j)
326 pResource->outputProperty.push_back(dataList[i].attributeProperty[j]);
328 pList->push_back(pResource);
336 SSMRESULT CContextRepository::loadSoftSensor(std::string softSensorName, ICtxDelegate *pDelegate,
339 std::stringstream sstream;
340 SSMRESULT res = SSM_E_FAIL;
342 typedef void(*InitContext)(ICtxDelegate *);
343 InitContext InitializeContextFunction = NULL;
348 sstream << m_pathSoftSensors << softSensorName.c_str() << ".dll" << std::ends;
350 HINSTANCE hModule = NULL;
351 hModule = LoadLibraryA(sstream.str().c_str());
355 InitializeContextFunction = (InitContext)GetProcAddress(hModule, "InitializeContext");
358 //sstream << "/data/data/com.example.javaproject/lib/lib" << modelName <<".so" << std::ends;
359 sstream << m_pathSoftSensors << "lib" << softSensorName.c_str() << ".so" << std::ends;
361 void *hModule = NULL;
362 hModule = dlopen(sstream.str().c_str(), RTLD_LOCAL | RTLD_LAZY);
366 InitializeContextFunction = (InitContext)dlsym(hModule, "InitializeContext");
371 //load library failed. raise error
372 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
375 if (InitializeContextFunction != NULL)
377 InitializeContextFunction(pDelegate);
378 *hSoftSensor = hModule;
383 //Unload module and return error
384 SSM_CLEANUP_ASSERT(unloadSoftSensor(hModule));
385 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
392 SSMRESULT CContextRepository::unloadSoftSensor(void *hSoftSensor)
394 SSMRESULT res = SSM_E_FAIL;
397 SSM_CLEANUP_COND_ASSERT(FreeLibrary((HINSTANCE)hSoftSensor), TRUE, "FreeLibrary failed");
399 SSM_CLEANUP_COND_ASSERT(dlclose(hSoftSensor), 0, "dlclose failed");
408 SSMRESULT CContextRepository::getCurrentPath(std::string *path)
411 SSMRESULT res = SSM_E_FAIL;
413 DWORD length = GetModuleFileNameA(NULL, buffer, 2047);
417 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
420 buffer[length] = '\0';
421 if (PathRemoveFileSpecA(buffer) == 0)
423 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
427 char *strPath = NULL;
428 int length = ::readlink("/proc/self/exe", buffer, 2047);
432 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
435 buffer[length] = '\0';
437 strPath = strrchr(buffer, '/');
441 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
447 path->append(buffer);