iotivity 0.9.0
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SSMCore / src / SSMInterface / SoftSensorManager.cpp
1 /******************************************************************
2 *
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
4 *
5 *
6 *
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
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
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"
30
31 inline bool operator<( const OID &lhs, const OID &rhs )
32 {
33     int ret = memcmp( &lhs, &rhs, sizeof(OID));
34     return (ret < 0 ? true : false );
35 }
36
37 SSMRESULT CSoftSensorManager::finalConstruct()
38 {
39     return SSM_S_OK;
40 }
41
42 void CSoftSensorManager::finalRelease()
43 {
44 }
45
46 SSMRESULT CSoftSensorManager::initializeCore(IN std::string xmlDescription)
47 {
48     SSMRESULT                   res = SSM_E_FAIL;
49     rapidxml::xml_document<>    xmlDoc;
50     std::string                 strKey;
51     std::string                 strValue;
52     rapidxml::xml_node<>        *root = NULL;
53     rapidxml::xml_node<>        *itemSSMCore = NULL;
54     rapidxml::xml_node<>        *itemDevice = NULL;
55
56     std::string                 name;
57     std::string                 type;
58     std::string                 pathSoftSensors;
59     std::string                 pathDescription;
60
61     xmlDoc.parse<0>((char *)xmlDescription.c_str());
62
63     root = xmlDoc.first_node();
64
65     strKey = root->name();
66
67     if (strKey != "SSMCore")
68     {
69         return SSM_E_FAIL;
70     }
71
72     for (itemSSMCore = root->first_node(); itemSSMCore; itemSSMCore = itemSSMCore->next_sibling())
73     {
74         strKey = itemSSMCore->name();
75
76         if (strKey == "Device")
77         {
78             for (itemDevice = itemSSMCore->first_node(); itemDevice; itemDevice = itemDevice->next_sibling())
79             {
80                 strKey = itemDevice->name();
81
82                 if (strKey == "Name")
83                 {
84                     name = itemDevice->value();
85                 }
86                 else if (strKey == "Type")
87                 {
88                     type = itemDevice->value();
89                 }
90                 else
91                 {
92                     ;/*NULL*/
93                 }
94             }
95         }
96         else if (strKey == "Config")
97         {
98             for (itemDevice = itemSSMCore->first_node(); itemDevice; itemDevice = itemDevice->next_sibling())
99             {
100                 strKey = itemDevice->name();
101
102                 if (strKey == "SoftSensorRepository")
103                 {
104                     pathSoftSensors = itemDevice->value();
105                 }
106                 else if (strKey == "SoftSensorDescription")
107                 {
108                     pathDescription = itemDevice->value();
109                 }
110                 else
111                 {
112                     ;/*NULL*/
113                 }
114             }
115         }
116         else
117         {
118             ;/*NULL*/
119         }
120     }
121
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,
125                        pathDescription));
126
127     SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_IPropagationEngine, (IBase **)&m_pPropagationEngine));
128
129 CLEANUP:
130     if (res != SSM_S_OK)
131     {
132         terminateCore(false);
133     }
134
135     return res;
136 }
137
138 SSMRESULT CSoftSensorManager::startCore()
139 {
140     //m_pSharingLayer->Start();
141     return SSM_S_OK;
142 }
143
144 SSMRESULT CSoftSensorManager::stopCore()
145 {
146     //m_pSharingLayer->Stop();
147     return SSM_S_OK;
148 }
149
150 SSMRESULT CSoftSensorManager::terminateCore(bool factoryResetFlag)
151 {
152     return SSM_S_OK;
153 }
154
155 SSMRESULT CSoftSensorManager::createQueryEngine(OUT IQueryEngine **ppQueryEngine)
156 {
157     SSMRESULT res = SSM_E_FAIL;
158     IQueryEngineInternal    *pQueryEngineInternal = NULL;
159     SSM_CLEANUP_ASSERT(CreateInstance(OID_IQueryEngineInternal, (IBase **)&pQueryEngineInternal));
160     *ppQueryEngine = (IQueryEngine *)pQueryEngineInternal;
161
162 CLEANUP:
163     return res;
164 }
165
166 unsigned long CSoftSensorManager::releaseQueryEngine(IN IQueryEngine *pQueryEngine)
167 {
168     IQueryEngineInternal    *pQueryEngineInternal = NULL;
169     pQueryEngineInternal = (IQueryEngineInternal *)(CQueryEngine *)pQueryEngine;
170
171     return pQueryEngineInternal->release();
172 }
173
174 SSMRESULT CSoftSensorManager::getInstalledModelList(OUT std::vector<ISSMResource *> *pList)
175 {
176     m_pSensingEngine->getList(pList);
177
178     return SSM_S_OK;
179 }
180
181 CSimpleMutex                *g_mtxGlobalInstance = NULL;
182 std::map<OID, IBase *>       *g_globalInstance = NULL;
183 IThreadPool                 *g_pThreadPool = NULL;
184
185 SSMRESULT CreateGlobalInstance(IN const OID &objectID, OUT IBase **ppvObject)
186 {
187     SSMRESULT res = SSM_E_NOINTERFACE;
188
189     if (ppvObject == NULL)
190     {
191         return SSM_E_POINTER;
192     }
193
194     *ppvObject = NULL;
195
196     g_mtxGlobalInstance->lock();
197     res = SSM_S_FALSE;
198
199     if (IsEqualOID(OID_ITasker, objectID))
200     {
201         if (g_globalInstance->find(OID_ITasker) == g_globalInstance->end())
202         {
203             SSM_CLEANUP_ASSERT(CreateInstance(OID_ITasker, ppvObject));
204         }
205     }
206     else if (IsEqualOID(OID_IThreadPool, objectID))
207     {
208         if (g_globalInstance->find(OID_IThreadPool) == g_globalInstance->end())
209         {
210             SSM_CLEANUP_ASSERT(CreateInstance(OID_IThreadPool, ppvObject));
211         }
212     }
213     else if (IsEqualOID(OID_IEvaluationEngine, objectID))
214     {
215         if (g_globalInstance->find(OID_IEvaluationEngine) == g_globalInstance->end())
216         {
217             SSM_CLEANUP_ASSERT(CreateInstance(OID_IEvaluationEngine, ppvObject));
218         }
219     }
220     else if (IsEqualOID(OID_IPropagationEngine, objectID))
221     {
222         if (g_globalInstance->find(OID_IPropagationEngine) == g_globalInstance->end())
223         {
224             SSM_CLEANUP_ASSERT(CreateInstance(OID_IPropagationEngine, ppvObject));
225         }
226     }
227     else if (IsEqualOID(OID_IContextRepository, objectID))
228     {
229         if (g_globalInstance->find(OID_IContextRepository) == g_globalInstance->end())
230         {
231             SSM_CLEANUP_ASSERT(CreateInstance(OID_IContextRepository, ppvObject));
232         }
233     }
234     else if (IsEqualOID(OID_IContextDataReader, objectID))
235     {
236         if (g_globalInstance->find(OID_IContextDataReader) == g_globalInstance->end())
237         {
238             SSM_CLEANUP_ASSERT(CreateInstance(OID_IContextDataReader, ppvObject));
239         }
240     }
241     else if (IsEqualOID(OID_ISensingEngine, objectID))
242     {
243         if (g_globalInstance->find(OID_ISensingEngine) == g_globalInstance->end())
244         {
245             SSM_CLEANUP_ASSERT(CreateInstance(OID_ISensingEngine, ppvObject));
246         }
247     }
248     else
249     {
250         res = SSM_E_NOINTERFACE;
251     }
252
253     switch (res)
254     {
255         case SSM_S_OK:
256             (*g_globalInstance)[objectID] = *ppvObject;
257             break;
258
259         case SSM_S_FALSE:
260             (*g_globalInstance)[objectID]->addRef();
261             *ppvObject = (*g_globalInstance)[objectID];
262             res = SSM_S_OK;
263             break;
264
265         default:
266             goto CLEANUP;
267     }
268
269 CLEANUP:
270     g_mtxGlobalInstance->unlock();
271     return res;
272 }
273
274 SSMRESULT CreateInstance(IN const OID &objectID, OUT IBase **ppObject)
275 {
276     SSMRESULT res = SSM_E_NOINTERFACE;
277
278     if (ppObject == NULL)
279     {
280         return SSM_E_POINTER;
281     }
282
283     *ppObject = NULL;
284
285     if (IsEqualOID(OID_ITasker, objectID))
286     {
287         SSM_CLEANUP_ASSERT(CreateNewObject<CTasker>(objectID, ppObject));
288     }
289     else if (IsEqualOID(OID_IWorkerThread, objectID))
290     {
291         SSM_CLEANUP_ASSERT(CreateNewObject<CWorkerThread>(objectID, ppObject));
292     }
293     else if (IsEqualOID(OID_IThreadPool, objectID))
294     {
295         SSM_CLEANUP_ASSERT(CreateNewObject<CThreadPool>(objectID, ppObject));
296     }
297     else if (IsEqualOID(OID_IEvaluationEngine, objectID))
298     {
299         SSM_CLEANUP_ASSERT(CreateNewObject<CEvaluationEngine>(objectID, ppObject));
300     }
301     else if (IsEqualOID(OID_IPropagationEngine, objectID))
302     {
303         SSM_CLEANUP_ASSERT(CreateNewObject<CPropagationEngine>(objectID, ppObject));
304     }
305     else if (IsEqualOID(OID_IContextRepository, objectID))
306     {
307         SSM_CLEANUP_ASSERT(CreateNewObject<CContextRepository>(objectID, ppObject));
308     }
309     else if (IsEqualOID(OID_ISensingEngine, objectID))
310     {
311         SSM_CLEANUP_ASSERT(CreateNewObject<CSensingEngine>(objectID, ppObject));
312     }
313     else if (IsEqualOID(OID_IContextExecutor, objectID))
314     {
315         SSM_CLEANUP_ASSERT(CreateNewObject<CContextExecutor>(objectID, ppObject));
316     }
317     else if (IsEqualOID(OID_IContextModel, objectID))
318     {
319         SSM_CLEANUP_ASSERT(CreateNewObject<CContextModel>(objectID, ppObject));
320     }
321     else if (IsEqualOID(OID_IConditionedModel, objectID))
322     {
323         SSM_CLEANUP_ASSERT(CreateNewObject<CConditionedModel>(objectID, ppObject));
324     }
325     else if (IsEqualOID(OID_IConditionedQueryResult, objectID))
326     {
327         SSM_CLEANUP_ASSERT(CreateNewObject<CConditionedQueryResult>(objectID, ppObject));
328     }
329     else if (IsEqualOID(OID_IConditionedQuery, objectID))
330     {
331         SSM_CLEANUP_ASSERT(CreateNewObject<CConditionedQuery>(objectID, ppObject));
332     }
333     else if (IsEqualOID(OID_IResourceFinder, objectID))
334     {
335         SSM_CLEANUP_ASSERT(CreateNewObject<CResourceFinder>(objectID, ppObject));
336     }
337     else if (IsEqualOID(OID_IQueryEngineInternal, objectID))
338     {
339         SSM_CLEANUP_ASSERT(CreateNewObject<CQueryEngine>(objectID, ppObject));
340     }
341     else if (IsEqualOID(OID_ISoftSensorManager, objectID))
342     {
343         SSM_CLEANUP_ASSERT(CreateNewObject<CSoftSensorManager>(objectID, ppObject));
344     }
345     else if (IsEqualOID(OID_IContextDataReader, objectID))
346     {
347         SSM_CLEANUP_ASSERT(CreateNewObject<CContextDataReader>(objectID, ppObject));
348     }
349
350 CLEANUP:
351     return res;
352 }
353
354 SSMRESULT CreateGlobalInstanceRepo()
355 {
356     SSMRESULT res = SSM_E_FAIL;
357
358     if (g_mtxGlobalInstance != NULL)
359         SSM_CLEANUP_ASSERT(SSM_E_OUTOFMEMORY);
360
361     if (g_globalInstance != NULL)
362         SSM_CLEANUP_ASSERT(SSM_E_OUTOFMEMORY);
363
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);
368
369     SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_IThreadPool, (IBase **)&g_pThreadPool));
370
371 CLEANUP:
372     return res;
373 }
374
375 SSMRESULT DestroyGlobalInstanceRepo()
376 {
377     SSMRESULT res = SSM_E_FAIL;
378
379     SSM_CLEANUP_ASSERT(g_pThreadPool->destroyThreadPool());
380
381     SAFE_RELEASE(g_pThreadPool);
382     SAFE_DELETE(g_mtxGlobalInstance);
383     SAFE_DELETE(g_globalInstance);
384
385 CLEANUP:
386     return res;
387 }