Fix for observe request handling issue.
[platform/upstream/iotivity.git] / service / simulator / src / simulator_resource.h
1 /******************************************************************
2  *
3  * Copyright 2015 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
21 /**
22  * @file   simulator_resource.h
23  *
24  * @brief   This file contains a class which represents a simulator resource that provides a set
25  *             of functions for operating a resource and performing automation on attribute values.
26  */
27
28 #ifndef SIMULATOR_RESOURCE_H_
29 #define SIMULATOR_RESOURCE_H_
30
31 #include "simulator_resource_model.h"
32 #include "simulator_attribute_automation.h"
33 #include "simulator_error_codes.h"
34
35 class ResourceManager;
36 /**
37  * @class   SimulatorResource
38  * @brief   This class provides a set of functions for operating and automating a resource.
39  */
40 class SimulatorResource
41 {
42         friend class ResourceManager;
43
44     public:
45         typedef std::function<void (const std::string &uri, const SimulatorResourceModel &resModel)>
46         ResourceModelChangedCB;
47
48         /**
49          * This method is used to set the resource URI.
50          *
51          * @param uri - Resource URI
52          *
53          * @return void
54          */
55         void setURI(const std::string &uri);
56
57         /**
58          * This method is used to get the resource URI.
59          *
60          * @return Resource URI
61          */
62         std::string getURI() const;
63
64         /**
65          * This method is used to set the resource type.
66          *
67          * @param resourceType - Resource Type
68          *
69          * @return void
70          */
71         void setResourceType(const std::string &resourceType);
72
73         /**
74          * This method is used to get the resource URI.
75          *
76          * @return Resource Type
77          */
78         std::string getResourceType() const;
79
80         /**
81          * This method is used to set the interface type of the resource.
82          *
83          * @param interfaceType - Interface Type of the resource
84          *
85          * @return void
86          */
87         void setInterfaceType(const std::string &interfaceType);
88
89         /**
90          * This method is used to get the interface type of the resource.
91          *
92          * @return Interface type of the resource
93          */
94         std::string getInterfaceType() const;
95
96         /**
97          * This method is used to set the name of the resource.
98          *
99          * @param name - Name of the resource
100          *
101          * @return void
102          */
103         void setName(const std::string &name);
104
105         /**
106          * This method is used to get the name of the resource.
107          *
108          * @return Resource name
109          */
110         std::string getName() const;
111
112         /**
113          * This method is used to start the attribute value automation for all attributes.
114          * Once started, values for the attributes will be selected randomly from their allowed range
115          * and the updated values will be notified to all the observers of the resource.
116          *
117          * @param id - Identifier for automation will be returned
118          *
119          * @return SimulatorResult
120          */
121         SimulatorResult startUpdateAutomation(AutomationType type, int &id);
122
123         /**
124          * This method is used to start the attribute value automation for a specific attribute.
125          * Once started, values for the attribute will be selected randomly from its allowed range
126          * and the updated value will be notified to all the observers of the resource.
127          *
128          * @param attrName - Name of the attribute to be automated
129          * @param id - Identifier for automation will be returned
130          *
131          * @return SimulatorResult
132          */
133         SimulatorResult startUpdateAutomation(const std::string &attrName, AutomationType type, int &id);
134
135         /**
136         * This method is used to stop the automation.
137         *
138         * @param id - Identifier for automation
139         */
140         void stopUpdateAutomation(const int id);
141
142         /**
143          * This method is used to add a new attribute to the resource model.
144          *
145          * @param attrName - Name of the attribute
146          * @param attrValue - Value of the attribute
147          *
148          * @return void
149          */
150         template <typename T>
151         void addAttribute(const std::string &attrName, const T &attrValue)
152         {
153             m_resModel.addAttribute(attrName, attrValue);
154             notifyListOfObservers();
155         }
156
157         /**
158          * This method is used to set the value range of an attribute.
159          * This method is intended to be used for attributes whose values are numbers only.
160          *
161          * @param attrName - Name of the attribute
162          * @param min - Minimum value of the range
163          * @param max - Maximum value of the range
164          *
165          * @return void
166          */
167         void setRange(const std::string &attrName, const int min, const int max);
168
169         /**
170          * This method is used to set the allowed values of an attribute.
171          *
172          * @param attrName - Name of the attribute
173          * @param values - Allowed values
174          *
175          * @return void
176          */
177         template <typename T>
178         void setAllowedValues(const std::string &attrName, const std::vector<T> &values)
179         {
180             m_resModel.setAllowedValues(attrName, values);
181         }
182
183         /**
184          * This method is used to set the update interval time for automation.
185          *
186          * @param attrName - Name of the attribute
187          * @param interval - Interval time in miliseconds for attribute value update automation
188          *
189          * @return void
190          */
191         void setUpdateInterval(const std::string &attrName, int interval);
192
193         /**
194          * This method is used to update the value of an attribute.
195          *
196          * @param attrName - Name of the attribute
197          * @param value - Value of the attribute
198          *
199          * @return void
200          */
201         template <typename T>
202         void updateAttribute(const std::string &attrName, const T &value)
203         {
204             m_resModel.updateAttribute(attrName, value);
205             notifyListOfObservers();
206         }
207
208         /**
209          * This method is used to update the attribute's value by taking the index of the value
210          * in the allowed values range.
211          *
212          * @param attrName - Name of the attribute
213          * @param allowedValueIndex - Index of the value in the allowed values range
214          *
215          * @return void
216          */
217         void updateAttributeFromAllowedValues(const std::string &attrName, const int allowedValueIndex);
218
219         /**
220           * This method is used to remove an attribute from the resource model.
221           *
222           * @param attName - Name of the attribute to be removed
223           *
224           * @return void
225           */
226         void removeAttribute(const std::string &attName);
227
228         /**
229          * This method is used to get the object of SimulatorResourceModel.
230          * Attributes of the resource are accessed using this object.
231          *
232          * @return SimulatorResourceModel - Resource model of the resource
233          */
234         SimulatorResourceModel getModel() const;
235
236         /**
237          * This method is used to set the callback for receiving the notifications when the
238          * resource model changes.
239          *
240          * @param callback - Callback to be set for receiving the notifications.
241          *
242          * @return SimulatorResourceModel - Resource model of the resource
243          */
244         void setModelChangeCallback(ResourceModelChangedCB callback);
245
246     private:
247         OCResourceHandle getHandle() const;
248         OC::OCRepresentation getOCRepresentation();
249         bool modifyResourceModel(OC::OCRepresentation &ocRep, SimulatorResourceModel::UpdateType type);
250         OCEntityHandlerResult entityHandler(std::shared_ptr<OC::OCResourceRequest> request);
251         void notifyListOfObservers ();
252
253         SimulatorResourceModel m_resModel;
254         OCResourceHandle m_resourceHandle;
255         std::string m_uri;
256         std::string m_resourceType;
257         std::string m_interfaceType;
258         std::string m_name;
259         OC::ObservationIds m_interestedObservers;
260         ResourceModelChangedCB m_callback;
261         UpdateAutomationManager m_updateAutomationMgr;
262 };
263
264 typedef std::shared_ptr<SimulatorResource> SimulatorResourcePtr;
265
266 #endif