iotivity 0.9.0
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SSMCore / src / SensorProcessor / ContextExecutor.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 _ContextExecutor_H_
21 #define _ContextExecutor_H_
22
23 #include "SSMInterface/SSMCore.h"
24 #include "SSMInterface/SSMModelDefinition.h"
25 #include "Common/PlatformLayer.h"
26 #include "Common/InternalInterface.h"
27
28 #include "ContextRepository.h"
29
30 /**
31  * @class    CContextExecutor
32  * @brief    Class for execute context model.
33  *           Delegate requested context to lower layer.
34  *           Execute context model.
35  *
36  * @see
37  */
38 class CContextExecutor :
39     public CObjectRoot<CObjectMultiThreadModel>
40     , public IContextExecutor
41     , public IResourceEvent
42     , public IThreadClient
43 {
44     private:
45         CObjectPtr<ITasker>             m_pTasker;
46
47         CObjectPtr<IContextRepository>  m_pContextRepository;
48
49         CObjectPtr<IContextDataReader>  m_pContextDataReader;
50
51         /**
52         * @brief Sensor data from primitive sensors
53         */
54         std::map<std::string, std::vector<ContextData> >        m_storedPrimitiveSensorData;
55
56         /**
57         * @brief key = primitive, soft sensor / values = soft sensors that has key in the input list
58         */
59         std::map<std::string, std::vector<std::string> >        m_relatedSoftSensor;
60
61         /**
62         * @brief requested high layer's callback data.(IEvent instance, deviceId, call type)
63         */
64         std::map<std::string, CallbackData>                 m_requestedContextCallback;
65
66         /**
67         * @brief registered high layer's context resource data.
68         */
69         std::map<std::string, ISSMResource *>               m_registeredResources;
70
71         /**
72         * @brief context model event of context model library
73         */
74         std::map<std::string, ICtxEvent *>                   m_ctxEventList;
75
76         /**
77         * @brief context model library list
78         */
79         std::map<std::string, void *>                        m_libraryList;
80
81         ICtxEvent                                           *ctxEvent;
82         //CSimpleMutex                                      m_mtxRequestedContextCallback;
83
84         CSimpleMutex                                        m_mtxLibraryIO, m_mtxUserDataIO;
85
86         std::map<std::string, std::vector<CallbackData> >   m_mapResourceLookup;
87
88     public:
89
90         SSMRESULT finalConstruct();
91         void finalRelease();
92
93         SSMRESULT queryInterface(const OID &objectID, IBase **ppObject)
94         {
95             if (ppObject == NULL)
96                 return SSM_E_POINTER;
97
98             if (IsEqualOID(objectID, OID_IContextExecutor))
99             {
100                 IBase *pBase = (IContextExecutor *)this;
101                 pBase->addRef();
102                 *ppObject = pBase;
103                 return SSM_S_OK;
104             }
105
106             return SSM_E_NOINTERFACE;
107         }
108
109         /**
110         * @fn           registerCallback
111         * @brief        ICtxDelegate Interface.
112         *               If context model call this function, then get model's instance
113         *
114         * @param        [in]  ICtxEvent *pEvent -  context model's ICtxEvent instance.
115         *
116         * @return       void
117         *
118         * @warning
119         * @exception
120         * @see
121         */
122         void registerCallback(IN ICtxEvent *pEvent);
123
124         /**
125         * @fn           addOutput
126         * @brief        ICtxDelegate Interface.
127         *               Get context model's result and delegate to upper layer.
128         *
129         * @param        [in]  std::vector<ContextData> contextData -  context model's result
130         * @return       void
131         *
132         * @warning
133         * @exception
134         * @see
135         */
136         void addOutput(IN std::vector<ContextData> contextData);
137
138         /**
139         * @fn           getDataFromDatabase
140         * @brief        ICtxDelegate Interface.
141         *               Get context model's historical data
142         *
143         * @param        [in] std::string modelName - Model name that looking for
144         * @param        [in] int startIndex - Starting index of model data
145         * @param        [in] int count - Number of data to retrieve
146         * @param        [out] std::vector<ContextData> *data   -  data from database
147         * @param        [out] int *pLastIndex - Index of last data
148         * @return       void
149         *
150         * @warning
151         * @exception
152         * @see
153         */
154         void getDataFromDatabase(IN std::string modelName, IN int startIndex, IN int count,
155                                  OUT std::vector<ContextData> *data, OUT int *pLastIndex);
156
157         /**
158         * @fn           onEvent
159         * @brief        IEvent Interface.
160         *               Call from lower layer when lower context data generated.
161         *
162         * @param        [in]  std::string deviceID -  caller device UUID.
163         * @param        [in]  TypeofEvent callType -  context event type.
164         * @param        [in]  std::vector<ContextData> ctxData -  context data
165         *
166         * @return       int - function result status
167         *
168         * @warning
169         * @exception
170         * @see
171         */
172         int onEvent(IN std::string deviceID, IN TypeofEvent callType, IN std::vector<ContextData> ctxData);
173
174         /**
175         * @fn           registerContext
176         * @brief        Register context model request.
177         *
178         * @param        [in]  TypeofEvent callType -  Type of event. SSM_ONCE or SSM_REPEAT
179         * @param        [in]  ISSMResource *pSSMResource -  Requested context model resource.
180         * @param        [in]  IEvent *pEvent -  IEvent class for callback.
181         *
182         * @return       void
183         *
184         * @warning
185         * @exception
186         * @see
187         */
188         void registerContext(IN TypeofEvent callType, IN ISSMResource *pSSMResouce, IN IEvent *pEvent);
189
190         /**
191         * @fn           unregisterContext
192         * @brief        Unregister context model request.
193         *
194         * @param        [in]  TypeofEvent callType -  Type of event. SSM_ONCE or SSM_REPEAT
195         * @param        [in]  ISSMResource *pSSMResource -  Requested context model resource.
196         * @param        [in]  IEvent *pEvent -  IEvent class for callback.
197         *
198         * @return       void
199         *
200         * @warning
201         * @exception
202         * @see
203         */
204         void  unregisterContext(IN TypeofEvent callType, IN ISSMResource *pSSMResource, IN IEvent *pEvent);
205
206         void onExecute(void *pArg);
207         void onTerminate(void *pArg);
208         int onResourceEvent(IN RESOURCE_EVENT_TYPE eventType, IN ISSMResource *pSSMResource,
209                             IN std::string info);
210
211     private:
212         SSMRESULT findString(IN std::vector<ISSMResource *> *sList, IN const std::string str,
213                              OUT ISSMResource **ppResource);
214         std::map<std::string, std::vector<ContextData> >  getPreparedContextList(
215             IN std::string primitiveSensor);
216         void runLogic(IN std::vector<ContextData> inputData, IN std::string softSensor);
217 };
218
219 #endif