Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SSMCore / src / QueryProcessor / ContextModel.h
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 #ifndef _ContextModel_H_
21 #define _ContextModel_H_
22
23 #include "SSMInterface/SSMCore.h"
24 #include "Common/PlatformLayer.h"
25 #include "Common/InternalInterface.h"
26
27 /**
28 * @class    CContextModel
29 * @brief    CContextModel Interface
30 *            This class represents Context Model
31 *
32 * @see
33 */
34 class CContextModel :
35     public CObjectRoot<CObjectMultiThreadModel>
36     , public IContextModel
37     , public IThreadClient
38     , public IEvent
39 {
40     private:
41         CObjectPtr<ITasker>                     m_pTasker;
42         CObjectPtr<IEvaluationEngine>           m_pEvaluationEngine;
43         CObjectPtr<IContextModel>               m_pParentModel;
44         ModelPropertyVec                        m_modelProperties;
45         //std::vector<IConditionedModel*>           m_ConditionedModels;
46         int                                     m_modelId;
47         std::string                             m_modelName;
48         IContextModelEvent                      *m_pContextModelEvent;
49         CSimpleMutex                            m_mtxActivationCount;
50         int                                     m_activationCount;
51
52         std::map<std::string, std::pair<int, ISSMResource> >    m_mapSSM;
53         std::map<int, IntVec>                    m_mapDeviceDataIds;
54
55         //DeviceDataId, RefCount
56         std::map<int, int>                      m_mapSubscribedDevice;
57         std::map<int, int>                      m_mapGetDevice;
58
59         ConstructionType                        m_constructionType;
60
61         std::string                             m_secLifeTime;
62
63         void registerSSMResource(ActivationType activationType, int targetDeviceDataId,
64                                  ISSMResource *pSSMResource);
65
66         void unregisterSSMResource(ActivationType activationType, int targetDeviceDataId,
67                                    ISSMResource *pSSMResource);
68
69     public:
70         SSMRESULT finalConstruct();
71
72         void finalRelease();
73
74         /**
75         * @fn     create
76         * @brief Create new Context Model
77         *
78         * @param [in] ConstructionType constructionType - Construction Type that current model is created from external modules or internally created.
79         * @param [in] IContextModel *pParentModel - Parent Context Model that related to Model hierarchy.
80         * @param [in] std::string modelName - Represent model's name
81         * @param [in] ModelPropertyVec *pModelProperties - Represent model's properties that exposed to CQL
82         *
83         * @return SSMRESULT
84         * @warning
85         * @exception
86         * @see
87         */
88         SSMRESULT create(ConstructionType constructionType, IContextModel *pParentModel,
89                          std::string modelName, ModelPropertyVec *pModelProperties);
90
91         /**
92         * @fn     registerContextModelEvent
93         * @brief Register ContextModel's events like Activate or Deactivate
94         *
95         * @param [in] IContextModelEvent *pContextModelEvent - Event interface.
96         *
97         * @return SSMRESULT
98         * @warning
99         * @exception
100         * @see
101         */
102         SSMRESULT registerContextModelEvent(IContextModelEvent *pContextModelEvent);
103
104         void onExecute(void *pArg);
105
106         void onTerminate(void *pArg);
107
108         /**
109         * @fn     onEvent
110         * @brief Called when new context model data arrived
111         *
112         * @param [in] std::string deviceID - DeviceID that represents where the data comes.
113         * @param [in] TypeofEvent callType - Represent received data's type, streamed or single
114         * @param [in] std::vector<ContextData> ctxData - Context data
115         *
116         * @return int
117         * @warning
118         * @exception
119         * @see
120         */
121         int onEvent(std::string deviceID, TypeofEvent callType, std::vector<ContextData> ctxData);
122
123         SSMRESULT queryInterface(const OID &objectID, IBase **ppObject)
124         {
125             if (ppObject == NULL)
126                 return SSM_E_POINTER;
127
128             if (IsEqualOID(objectID, OID_IContextModel))
129             {
130                 IBase *pBase = (IContextModel *)this;
131                 pBase->addRef();
132                 *ppObject = pBase;
133                 return SSM_S_OK;
134             }
135
136             return SSM_E_NOINTERFACE;
137         }
138
139         /**
140         * @fn     getModelId
141         * @brief Get current model's id used for Evaluation Engine
142         *
143         * @param NONE
144         *
145         * @return int
146         * @warning
147         * @exception
148         * @see
149         */
150         int getModelId();
151
152         /**
153         * @fn     getModelName
154         * @brief Get current model's name
155         *
156         * @param NONE
157         *
158         * @return std::string
159         * @warning
160         * @exception
161         * @see
162         */
163         std::string getModelName();
164
165         /**
166         * @fn     getConstructionType
167         * @brief Get current model's constructionType defined when created
168         *
169         * @param NONE
170         *
171         * @return IContextModel::ConstructionType
172         * @warning
173         * @exception
174         * @see
175         */
176         ConstructionType getConstructionType();
177
178         /**
179         * @fn     getParentDataId
180         * @brief Get parent dataId related to model hierarchy
181         *
182         * @param [in] int dataId - DataId that current model's
183         * @param [in] IContextModel *pParentModel - Designated parent Context Model
184         * @param [out] int *pParentDataId - Designated parent Context Model's data's Id
185         *
186         * @return SSMRESULT
187         * @warning
188         * @exception
189         * @see
190         */
191         SSMRESULT getParentDataId(int dataId, IContextModel *pParentModel, int *pParentDataId);
192
193         /**
194         * @fn     getChildDataId
195         * @brief Get child dataId related to model hierarchy
196         *
197         * @param [in] int dataId - DataId that current model's
198         * @param [in] IContextModel *pChildModel - Designated child Context Model
199         * @param [out] int *pParentDataId - Designated child Context Model's data's Id
200         *
201         * @return SSMRESULT
202         * @warning
203         * @exception
204         * @see
205         */
206         SSMRESULT getChildDataId(int dataId, IContextModel *pChildModel, IntVec *pChildDataIds);
207
208         /**
209         * @fn     activate
210         * @brief Activate current model's working. Each call to activate will increase internal reference counter
211         *
212         * @param [in] ActivationType activationType - Specify current model's working type
213         * @param [in] int targetDeviceDataId - Specify target device's dataId
214         *
215         * @return SSMRESULT
216         * @warning
217         * @exception
218         * @see
219         */
220         SSMRESULT activate(ActivationType activationType, int targetDeviceDataId);
221
222         /**
223         * @fn     deactivate
224         * @brief Deactivate current model's working. Each call to deactivate will decrease internal reference counter\n
225         *        and finally deactivated when counter is 0
226         *
227         * @param [in] ActivationType activationType - Specify current model's working type
228         * @param [in] int targetDeviceDataId - Specify target device's dataId
229         *
230         * @return SSMRESULT
231         * @warning
232         * @exception
233         * @see
234         */
235         SSMRESULT deactivate(ActivationType activationType, int targetDeviceDataId);
236
237         //SSMRESULT GetModelSchema(ModelPropertyVec *pModelProperties);
238
239         /**
240         * @fn     addModelData
241         * @brief Add model data to Context Model
242         *
243         * @param [in] int parentDataId - Specify current model's parent Context Model's dataId
244         * @param [in] ModelPropertyVec *pData - Packed data for adding
245         * @param [out] int *pDataId - Returned dataId that newly added
246         *
247         * @return SSMRESULT
248         * @warning
249         * @exception
250         * @see
251         */
252         SSMRESULT addModelData(int parentDataId, ModelPropertyVec *pData, int *pDataId);
253
254         /**
255         * @fn     updateModelData
256         * @brief Update model data to Context Model
257         *
258         * @param [in] int dataId - DataId that provided AddModelData's execution
259         * @param [in] ModelPropertyVec *pData - Packed data for updating
260         *
261         * @return SSMRESULT
262         * @warning
263         * @exception
264         * @see
265         */
266         SSMRESULT updateModelData(int dataId, ModelPropertyVec *pData);
267
268         /**
269         * @fn     deleteModelData
270         * @brief Delete model data using given index
271         *
272         * @param [in] int dataId - DataId that provided AddModelData's execution
273         *
274         * @return SSMRESULT
275         * @warning
276         * @exception
277         * @see
278         */
279         SSMRESULT deleteModelData(int dataId);
280
281         /**
282         * @fn     getModelData
283         * @brief Get model data
284         *
285         * @param [in] int dataId - DataId that provided AddModelData's execution
286         * @param [out] ModelPropertyVec *pData - Packed data
287         *
288         * @return SSMRESULT
289         * @warning
290         * @exception
291         * @see
292         */
293         SSMRESULT getModelData(int dataId, ModelPropertyVec *pData);
294
295         /**
296         * @fn     getModelDataSet
297         * @brief Get model data set
298         *
299         * @param [in] int startIndex - Starting index of model data
300         * @param [in] int count - Number of data to retrieve
301         * @param [out] vector<ModelPropertyVec> *pData - Packed data
302         * @param [out] int *pLastIndex - Index of last data
303         *
304         * @return SSMRESULT
305         * @warning
306         * @exception
307         * @see
308         */
309         SSMRESULT getModelDataSet(int startIndex, int count,
310                                   std::vector<ModelPropertyVec> *pDataSet, int *pLastIndex);
311
312         /**
313         * @fn     createConditionedModel
314         * @brief Create Conditioned Model for data searching or triggering
315         *
316         * @param [in] ModelConditionVec *pModelConditionVec - Conditions that searching or triggering
317         * @param [out] IConditionedModel **ppConditionedModel - Created Conditioned Model interface
318         *
319         * @return SSMRESULT
320         * @warning
321         * @exception
322         * @see
323         */
324         SSMRESULT createConditionedModel(ModelConditionVec *pModelConditionVec,
325                                          IConditionedModel **ppConditionedModel);
326
327         //SSMRESULT CleanUpModelData();
328
329         /**
330         * @fn     addSSMResourceAndDeviceDataId
331         * @brief Add SSM resources with other informations - Internal use only
332         *
333         * @param [in] std::string deviceId - deviceId that used D2D communication
334         * @param [in] int deviceDataId - Device's dataId that where SSMResource come from
335         * @param [in] ISSMResource *pSSMResource - SSMResource for adding
336         *
337         * @return NONE
338         * @warning
339         * @exception
340         * @see
341         */
342         void addSSMResourceAndDeviceDataId(std::string deviceId, int deviceDataId,
343                                            ISSMResource *pSSMResource);
344
345         /**
346         * @fn     SetLifeTime
347         * @brief Sets current model data's lifeTime
348         *
349         * @param [in] int seconds - model data's lifeTime
350         *
351         * @return NONE
352         * @warning
353         * @exception
354         * @see
355         */
356         void setLifeTime(std::string seconds);
357 };
358 #endif