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 "SSMInterface/SoftSensorManager.h"
21 #include "QueryProcessor/QueryEngine.h"
22 #include "QueryProcessor/ContextModel.h"
23 #include "QueryProcessor/ConditionedModel.h"
24 #include "QueryProcessor/EvaluationEngine.h"
25 #include "QueryProcessor/PropagationEngine.h"
26 #include "SensorProcessor/SensingEngine.h"
27 #include "SensorProcessor/ContextExecutor.h"
28 #include "SensorProcessor/ContextDataReader.h"
29 #include "SensorProcessor/ResourceFinder.h"
31 inline bool operator<( const OID &lhs, const OID &rhs )
33 int ret = memcmp( &lhs, &rhs, sizeof(OID));
34 return (ret < 0 ? true : false );
37 SSMRESULT CSoftSensorManager::finalConstruct()
42 void CSoftSensorManager::finalRelease()
46 SSMRESULT CSoftSensorManager::initializeCore(IN std::string xmlDescription)
48 SSMRESULT res = SSM_E_FAIL;
49 rapidxml::xml_document<> xmlDoc;
52 rapidxml::xml_node<> *root = NULL;
53 rapidxml::xml_node<> *itemSSMCore = NULL;
54 rapidxml::xml_node<> *itemDevice = NULL;
58 std::string pathSoftSensors;
59 std::string pathDescription;
61 xmlDoc.parse<0>((char *)xmlDescription.c_str());
63 root = xmlDoc.first_node();
65 strKey = root->name();
67 if (strKey != "SSMCore")
72 for (itemSSMCore = root->first_node(); itemSSMCore; itemSSMCore = itemSSMCore->next_sibling())
74 strKey = itemSSMCore->name();
76 if (strKey == "Device")
78 for (itemDevice = itemSSMCore->first_node(); itemDevice; itemDevice = itemDevice->next_sibling())
80 strKey = itemDevice->name();
84 name = itemDevice->value();
86 else if (strKey == "Type")
88 type = itemDevice->value();
96 else if (strKey == "Config")
98 for (itemDevice = itemSSMCore->first_node(); itemDevice; itemDevice = itemDevice->next_sibling())
100 strKey = itemDevice->name();
102 if (strKey == "SoftSensorRepository")
104 pathSoftSensors = itemDevice->value();
106 else if (strKey == "SoftSensorDescription")
108 pathDescription = itemDevice->value();
122 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_IContextRepository, (IBase **)&m_pContextRepository));
123 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_ISensingEngine, (IBase **)&m_pSensingEngine));
124 SSM_CLEANUP_ASSERT(m_pContextRepository->initRepository(name, type, pathSoftSensors,
127 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_IPropagationEngine, (IBase **)&m_pPropagationEngine));
132 terminateCore(false);
138 SSMRESULT CSoftSensorManager::startCore()
140 //m_pSharingLayer->Start();
144 SSMRESULT CSoftSensorManager::stopCore()
146 //m_pSharingLayer->Stop();
150 SSMRESULT CSoftSensorManager::terminateCore(bool factoryResetFlag)
155 SSMRESULT CSoftSensorManager::createQueryEngine(OUT IQueryEngine **ppQueryEngine)
157 SSMRESULT res = SSM_E_FAIL;
158 IQueryEngineInternal *pQueryEngineInternal = NULL;
159 SSM_CLEANUP_ASSERT(CreateInstance(OID_IQueryEngineInternal, (IBase **)&pQueryEngineInternal));
160 *ppQueryEngine = (IQueryEngine *)pQueryEngineInternal;
166 unsigned long CSoftSensorManager::releaseQueryEngine(IN IQueryEngine *pQueryEngine)
168 IQueryEngineInternal *pQueryEngineInternal = NULL;
169 pQueryEngineInternal = (IQueryEngineInternal *)(CQueryEngine *)pQueryEngine;
171 return pQueryEngineInternal->release();
174 SSMRESULT CSoftSensorManager::getInstalledModelList(OUT std::vector<ISSMResource *> *pList)
176 m_pSensingEngine->getList(pList);
181 CSimpleMutex *g_mtxGlobalInstance = NULL;
182 std::map<OID, IBase *> *g_globalInstance = NULL;
183 IThreadPool *g_pThreadPool = NULL;
185 SSMRESULT CreateGlobalInstance(IN const OID &objectID, OUT IBase **ppvObject)
187 SSMRESULT res = SSM_E_NOINTERFACE;
189 if (ppvObject == NULL)
191 return SSM_E_POINTER;
196 g_mtxGlobalInstance->lock();
199 if (IsEqualOID(OID_ITasker, objectID))
201 if (g_globalInstance->find(OID_ITasker) == g_globalInstance->end())
203 SSM_CLEANUP_ASSERT(CreateInstance(OID_ITasker, ppvObject));
206 else if (IsEqualOID(OID_IThreadPool, objectID))
208 if (g_globalInstance->find(OID_IThreadPool) == g_globalInstance->end())
210 SSM_CLEANUP_ASSERT(CreateInstance(OID_IThreadPool, ppvObject));
213 else if (IsEqualOID(OID_IEvaluationEngine, objectID))
215 if (g_globalInstance->find(OID_IEvaluationEngine) == g_globalInstance->end())
217 SSM_CLEANUP_ASSERT(CreateInstance(OID_IEvaluationEngine, ppvObject));
220 else if (IsEqualOID(OID_IPropagationEngine, objectID))
222 if (g_globalInstance->find(OID_IPropagationEngine) == g_globalInstance->end())
224 SSM_CLEANUP_ASSERT(CreateInstance(OID_IPropagationEngine, ppvObject));
227 else if (IsEqualOID(OID_IContextRepository, objectID))
229 if (g_globalInstance->find(OID_IContextRepository) == g_globalInstance->end())
231 SSM_CLEANUP_ASSERT(CreateInstance(OID_IContextRepository, ppvObject));
234 else if (IsEqualOID(OID_IContextDataReader, objectID))
236 if (g_globalInstance->find(OID_IContextDataReader) == g_globalInstance->end())
238 SSM_CLEANUP_ASSERT(CreateInstance(OID_IContextDataReader, ppvObject));
241 else if (IsEqualOID(OID_ISensingEngine, objectID))
243 if (g_globalInstance->find(OID_ISensingEngine) == g_globalInstance->end())
245 SSM_CLEANUP_ASSERT(CreateInstance(OID_ISensingEngine, ppvObject));
250 res = SSM_E_NOINTERFACE;
256 (*g_globalInstance)[objectID] = *ppvObject;
260 (*g_globalInstance)[objectID]->addRef();
261 *ppvObject = (*g_globalInstance)[objectID];
270 g_mtxGlobalInstance->unlock();
274 SSMRESULT CreateInstance(IN const OID &objectID, OUT IBase **ppObject)
276 SSMRESULT res = SSM_E_NOINTERFACE;
278 if (ppObject == NULL)
280 return SSM_E_POINTER;
285 if (IsEqualOID(OID_ITasker, objectID))
287 SSM_CLEANUP_ASSERT(CreateNewObject<CTasker>(objectID, ppObject));
289 else if (IsEqualOID(OID_IWorkerThread, objectID))
291 SSM_CLEANUP_ASSERT(CreateNewObject<CWorkerThread>(objectID, ppObject));
293 else if (IsEqualOID(OID_IThreadPool, objectID))
295 SSM_CLEANUP_ASSERT(CreateNewObject<CThreadPool>(objectID, ppObject));
297 else if (IsEqualOID(OID_IEvaluationEngine, objectID))
299 SSM_CLEANUP_ASSERT(CreateNewObject<CEvaluationEngine>(objectID, ppObject));
301 else if (IsEqualOID(OID_IPropagationEngine, objectID))
303 SSM_CLEANUP_ASSERT(CreateNewObject<CPropagationEngine>(objectID, ppObject));
305 else if (IsEqualOID(OID_IContextRepository, objectID))
307 SSM_CLEANUP_ASSERT(CreateNewObject<CContextRepository>(objectID, ppObject));
309 else if (IsEqualOID(OID_ISensingEngine, objectID))
311 SSM_CLEANUP_ASSERT(CreateNewObject<CSensingEngine>(objectID, ppObject));
313 else if (IsEqualOID(OID_IContextExecutor, objectID))
315 SSM_CLEANUP_ASSERT(CreateNewObject<CContextExecutor>(objectID, ppObject));
317 else if (IsEqualOID(OID_IContextModel, objectID))
319 SSM_CLEANUP_ASSERT(CreateNewObject<CContextModel>(objectID, ppObject));
321 else if (IsEqualOID(OID_IConditionedModel, objectID))
323 SSM_CLEANUP_ASSERT(CreateNewObject<CConditionedModel>(objectID, ppObject));
325 else if (IsEqualOID(OID_IConditionedQueryResult, objectID))
327 SSM_CLEANUP_ASSERT(CreateNewObject<CConditionedQueryResult>(objectID, ppObject));
329 else if (IsEqualOID(OID_IConditionedQuery, objectID))
331 SSM_CLEANUP_ASSERT(CreateNewObject<CConditionedQuery>(objectID, ppObject));
333 else if (IsEqualOID(OID_IResourceFinder, objectID))
335 SSM_CLEANUP_ASSERT(CreateNewObject<CResourceFinder>(objectID, ppObject));
337 else if (IsEqualOID(OID_IQueryEngineInternal, objectID))
339 SSM_CLEANUP_ASSERT(CreateNewObject<CQueryEngine>(objectID, ppObject));
341 else if (IsEqualOID(OID_ISoftSensorManager, objectID))
343 SSM_CLEANUP_ASSERT(CreateNewObject<CSoftSensorManager>(objectID, ppObject));
345 else if (IsEqualOID(OID_IContextDataReader, objectID))
347 SSM_CLEANUP_ASSERT(CreateNewObject<CContextDataReader>(objectID, ppObject));
354 SSMRESULT CreateGlobalInstanceRepo()
356 SSMRESULT res = SSM_E_FAIL;
358 if (g_mtxGlobalInstance != NULL)
359 SSM_CLEANUP_ASSERT(SSM_E_OUTOFMEMORY);
361 if (g_globalInstance != NULL)
362 SSM_CLEANUP_ASSERT(SSM_E_OUTOFMEMORY);
364 g_mtxGlobalInstance = new CSimpleMutex();
365 SSM_CLEANUP_NULL_ASSERT(g_mtxGlobalInstance);
366 g_globalInstance = new std::map<OID, IBase *>();
367 SSM_CLEANUP_NULL_ASSERT(g_globalInstance);
369 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_IThreadPool, (IBase **)&g_pThreadPool));
375 SSMRESULT DestroyGlobalInstanceRepo()
377 SSMRESULT res = SSM_E_FAIL;
379 SSM_CLEANUP_ASSERT(g_pThreadPool->destroyThreadPool());
381 SAFE_RELEASE(g_pThreadPool);
382 SAFE_DELETE(g_mtxGlobalInstance);
383 SAFE_DELETE(g_globalInstance);