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"
22 static inline std::string trim_both(const std::string &str)
25 int npos = str.find_first_not_of(" \t\v\n");
30 unsigned int n = (unsigned int)npos;
31 std::string tempString = n == std::string::npos ? str : str.substr(n, str.length());
33 n = tempString.find_last_not_of(" \t\v\n");
34 return n == std::string::npos ? tempString : tempString.substr(0, n + 1);
37 SSMRESULT CContextRepository::finalConstruct()
39 SSMRESULT res = SSM_E_FAIL;
41 SSM_CLEANUP_ASSERT(CreateInstance(OID_IResourceFinder, (IBase **)&m_resourceFinder));
42 SSM_CLEANUP_ASSERT(m_resourceFinder->registerResourceFinderEvent(this));
48 SSMRESULT CContextRepository::startResourceFinder()
50 SSMRESULT res = SSM_E_FAIL;
52 SSM_CLEANUP_ASSERT(m_resourceFinder->startResourceFinder());
58 SSMRESULT CContextRepository::registerResourceFinderEvent(IN IResourceEvent *pResourceEvent)
60 m_resourceEvents.push_back(pResourceEvent);
64 void CContextRepository::finalRelease()
68 SSMRESULT CContextRepository::initRepository(IN std::string name, IN std::string type,
69 IN std::string pathSoftSensors, IN std::string pathDescription)
71 SSMRESULT res = SSM_E_FAIL;
73 std::vector<DictionaryData> dict;
78 if (pathSoftSensors.length() == 0)
80 SSM_CLEANUP_ASSERT(GetCurrentPath(&m_pathSoftSensors));
81 m_pathSoftSensors.append("/");
85 m_pathSoftSensors = pathSoftSensors;
88 if (pathDescription.length() == 0)
90 SSM_CLEANUP_ASSERT(GetCurrentPath(&m_pathSoftSensorsDescription));
91 m_pathSoftSensorsDescription.append("/");
92 m_pathSoftSensorsDescription.append(DEFAULT_PATH_SOFT_SENSORS);
96 m_pathSoftSensorsDescription = pathDescription;
100 SSM_CLEANUP_ASSERT(loadXMLFromFile(m_pathSoftSensorsDescription.c_str(), &dict));
101 SSM_CLEANUP_ASSERT(makeSSMResourceListForDictionaryData(dict, &m_lstSoftSensor));
107 SSMRESULT CContextRepository::loadXMLFromString(char *xmlData,
108 std::vector<DictionaryData> *dataList)
110 // use rapidxml-----------------------
111 SSMRESULT res = SSM_E_FAIL;
112 rapidxml::xml_document< char > xmlDoc;
113 //xmlDoc.parse< 0 >( &xmlData.front() );
114 xmlDoc.parse< 0 >(xmlData);
117 std::string valueStr;
119 rapidxml::xml_node< char > *item;
120 rapidxml::xml_node< char > *subItem;
121 rapidxml::xml_node< char > *subItem2;
122 rapidxml::xml_node< char > *subItem3;
125 rapidxml::xml_node< char > *root = xmlDoc.first_node();
129 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
132 for ( item = root->first_node(); item; item = item->next_sibling() )
134 DictionaryData dictionaryData;
135 for ( subItem = item->first_node(); subItem ; subItem = subItem->next_sibling() )
138 keyStr = subItem->name(); // key
139 valueStr = subItem->value(); // value
141 if (!keyStr.compare("name"))
143 dictionaryData.rootName = trim_both(valueStr);
145 ////std::cout<<keyStr << " : " << subItem->value() <<std::endl<<std::endl; //root_name
146 for (subItem2 = subItem->first_node(); subItem2 ; subItem2 = subItem2->next_sibling())
148 std::map<std::string, std::string> propertyMap;
149 std::vector<std::string> enterconditionVector;
151 keyStr = subItem2->name(); // key
152 valueStr = subItem2->value(); // value
154 if (!keyStr.compare("input"))
156 dictionaryData.inputs.push_back(trim_both(valueStr));
158 ////std::cout<<name << " :: " << subItem2->value() <<std::endl<<std::endl;
159 for (subItem3 = subItem2->first_node(); subItem3 ; subItem3 = subItem3->next_sibling())
161 std::string newKeyStr = subItem3->name(); // key
162 valueStr = subItem3->value(); // value
164 if (!keyStr.compare("attribute") || !keyStr.compare("output") )
166 propertyMap.insert(std::make_pair(trim_both(newKeyStr), trim_both(valueStr)));
169 if (!keyStr.compare("attribute"))
171 dictionaryData.attributeProperty.push_back(propertyMap);
173 else if (!keyStr.compare("output"))
175 dictionaryData.outputProperty.push_back(propertyMap);
181 dictionaryData.app_input_count = std::to_string((long long)dictionaryData.app_inputs.size());
182 dictionaryData.input_count = std::to_string((long long)dictionaryData.inputs.size());
183 dictionaryData.attribute_property_count = std::to_string((long long)dictionaryData.attribute_property.size());
184 dictionaryData.output_property_count = std::to_string((long long)dictionaryData.output_property.size());
187 dataList->push_back(dictionaryData);
196 SSMRESULT CContextRepository::loadXMLFromFile(std::string descriptionFilePath,
197 std::vector<DictionaryData> *dataList)
199 SSMRESULT res = SSM_E_FAIL;
200 std::basic_ifstream< char > xmlFile(descriptionFilePath.c_str());
205 xmlFile.seekg(0, std::ios::end);
206 unsigned int size = (unsigned int)xmlFile.tellg();
209 std::vector< char > xmlData(size + 1);
212 xmlFile.read(&xmlData.front(), (std::streamsize)size);
214 SSM_CLEANUP_ASSERT(loadXMLFromString(&xmlData.front(), dataList));
218 //let work with no soft sensor manager
226 SSMRESULT CContextRepository::getSoftSensorList(OUT std::vector<ISSMResource *> *pSoftSensorList)
228 for (size_t i = 0; i < m_lstSoftSensor.size(); i++)
230 pSoftSensorList->push_back(m_lstSoftSensor.at(i));
236 SSMRESULT CContextRepository::getPrimitiveSensorList(OUT std::vector<ISSMResource *>
237 *pPrimitiveSensorList)
239 for (size_t i = 0; i < m_lstPrimitiveSensor.size(); i++)
241 pPrimitiveSensorList->push_back(m_lstPrimitiveSensor.at(i));
247 SSMRESULT CContextRepository::onResourceFound(IN ISSMResource *pSensor)
249 m_lstPrimitiveSensor.push_back(pSensor);
251 for (size_t i = 0; i < m_resourceEvents.size(); i++)
253 m_resourceEvents.at(i)->onResourceEvent(SSM_EVENT_ADDED, pSensor, "");
259 SSMRESULT CContextRepository::onResourceLost(IN ISSMResource *pSensor)
261 std::vector<ISSMResource *>::iterator itor;
262 itor = std::find(m_lstPrimitiveSensor.begin(), m_lstPrimitiveSensor.end(), pSensor);
264 if (itor != m_lstPrimitiveSensor.end())
266 m_lstPrimitiveSensor.erase(itor);
273 SSMRESULT CContextRepository::startObserveResource(IN ISSMResource *pSensor, IN IEvent *pEvent)
275 return m_resourceFinder->startObserveResource(pSensor, pEvent);
278 SSMRESULT CContextRepository::stopObserveResource(IN ISSMResource *pSensor)
280 return m_resourceFinder->stopObserveResource(pSensor);
284 SSMRESULT CContextRepository::makeSSMResourceListForDictionaryData(std::vector<DictionaryData>
285 dataList, std::vector<ISSMResource *> *pList)
287 SSMRESULT res = SSM_E_FAIL;
289 for (unsigned int i = 0 ; i < dataList.size() ; ++i)
291 ISSMResource *pResource = new ISSMResource();
292 pResource->location = SENSOR_LOCATION_LOCAL;
293 pResource->type = dataList[i].rootName;
294 pResource->name = std::string("coap://127.0.0.1/") + dataList[i].rootName;
295 pResource->ip = "coap://127.0.0.1/";
297 pResource->inputList = dataList[i].inputs;
299 for (unsigned int j = 0; j < dataList[i].outputProperty.size(); ++j)
301 pResource->outputProperty.push_back(dataList[i].outputProperty[j]);
303 for (unsigned int j = 0; j < dataList[i].attributeProperty.size(); ++j)
305 pResource->outputProperty.push_back(dataList[i].attributeProperty[j]);
307 pList->push_back(pResource);
315 SSMRESULT CContextRepository::loadSoftSensor(std::string softSensorName, ICtxDelegate *pDelegate,
318 std::stringstream sstream;
319 SSMRESULT res = SSM_E_FAIL;
321 typedef void(*InitContext)(IN ICtxDelegate *);
322 InitContext InitializeContextFunction = NULL;
327 sstream << m_pathSoftSensors << softSensorName.c_str() << ".dll" << std::ends;
329 HINSTANCE hModule = NULL;
330 hModule = LoadLibraryA(sstream.str().c_str());
334 InitializeContextFunction = (InitContext)GetProcAddress(hModule, "InitializeContext");
337 //sstream << "/data/data/com.example.javaproject/lib/lib" << modelName <<".so" << std::ends;
338 sstream << m_pathSoftSensors << "lib" << softSensorName.c_str() << ".so" << std::ends;
340 void *hModule = NULL;
341 hModule = dlopen(sstream.str().c_str(), RTLD_LOCAL | RTLD_LAZY);
345 InitializeContextFunction = (InitContext)dlsym(hModule, "InitializeContext");
350 //load library failed. raise error
351 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
354 if (InitializeContextFunction != NULL)
356 InitializeContextFunction(pDelegate);
357 *hSoftSensor = hModule;
362 //Unload module and return error
363 SSM_CLEANUP_ASSERT(unloadSoftSensor(hModule));
364 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
371 SSMRESULT CContextRepository::unloadSoftSensor(void *hSoftSensor)
373 SSMRESULT res = SSM_E_FAIL;
376 SSM_CLEANUP_COND_ASSERT(FreeLibrary((HINSTANCE)hSoftSensor), TRUE, "FreeLibrary failed");
378 SSM_CLEANUP_COND_ASSERT(dlclose(hSoftSensor), 0, "dlclose failed");
387 SSMRESULT CContextRepository::GetCurrentPath(std::string *path)
390 SSMRESULT res = SSM_E_FAIL;
392 DWORD length = GetModuleFileNameA(NULL, buffer, 2047);
396 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
399 buffer[length] = '\0';
400 if (PathRemoveFileSpecA(buffer) == 0)
402 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
406 char *strPath = NULL;
407 int length = ::readlink("/proc/self/exe", buffer, 2047);
411 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
414 buffer[length] = '\0';
416 strPath = strrchr(buffer, '/');
420 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
426 path->append(buffer);