Updating JNI modules of service provider
[platform/upstream/iotivity.git] / service / simulator / src / simulator_resource_server.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_server.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_SERVER_H_
29 #define SIMULATOR_RESOURCE_SERVER_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   SimulatorResourceServer
38  * @brief   This class provides a set of functions for operating and automating a resource.
39  */
40 class SimulatorResourceServer
41 {
42         friend class ResourceManager;
43
44     public:
45         typedef std::function<void (const std::string &uri, const SimulatorResourceModel &resModel)>
46         ResourceModelChangedCB;
47
48         SimulatorResourceServer();
49
50         /**
51          * This method is used to set the resource URI.
52          *
53          * @param uri - Resource URI
54          *
55          * @return SimulatorResult
56          */
57         SimulatorResult setURI(const std::string &uri);
58
59         /**
60          * This method is used to get the resource URI.
61          *
62          * @return Resource URI
63          */
64         std::string getURI() const;
65
66         /**
67          * This method is used to set the resource type.
68          *
69          * @param resourceType - Resource Type
70          *
71          * @return SimulatorResult
72          */
73         SimulatorResult setResourceType(const std::string &resourceType);
74
75         /**
76          * This method is used to get the resource URI.
77          *
78          * @return Resource Type
79          */
80         std::string getResourceType() const;
81
82         /**
83          * This method is used to set the interface type of the resource.
84          *
85          * @param interfaceType - Interface Type of the resource
86          *
87          * @return SimulatorResult
88          */
89         SimulatorResult setInterfaceType(const std::string &interfaceType);
90
91         /**
92          * This method is used to get the interface type of the resource.
93          *
94          * @return Interface type of the resource
95          */
96         std::string getInterfaceType() const;
97
98         /**
99          * This method is used to set the name of the resource.
100          *
101          * @param name - Name of the resource
102          *
103          * @return void
104          */
105         void setName(const std::string &name);
106
107         /**
108          * This method is used to get the name of the resource.
109          *
110          * @return Resource name
111          */
112         std::string getName() const;
113
114         /**
115          * This method is used to set whether resource can be observable or not.
116          *
117          * @param state - true for resource observable, otherwise false.
118          *
119          * @return SimulatorResult
120          */
121         SimulatorResult setObservable(bool state);
122
123         /**
124          * This method is used to get the observable state of resource.
125          *
126          * @return bool - true if resource is observable, otherwise false.
127          */
128         bool isObservable() const;
129
130         /**
131          * This method is used to start the attribute value automation for all attributes.
132          * Once started, values for the attributes will be selected randomly from their allowed range
133          * and the updated values will be notified to all the observers of the resource.
134          *
135          * @param id - Identifier for automation will be returned
136          *
137          * @return SimulatorResult
138          */
139         SimulatorResult startUpdateAutomation(AutomationType type,
140                                               updateCompleteCallback callback, int &id);
141
142         /**
143          * This method is used to start the attribute value automation for a specific attribute.
144          * Once started, values for the attribute will be selected randomly from its allowed range
145          * and the updated value will be notified to all the observers of the resource.
146          *
147          * @param attrName - Name of the attribute to be automated
148          * @param id - Identifier for automation will be returned
149          *
150          * @return SimulatorResult
151          */
152         SimulatorResult startUpdateAutomation(const std::string &attrName, AutomationType type,
153                                               updateCompleteCallback callback, int &id);
154
155         /**
156          * This method is used to get the Ids of all ongoing resource update automation .
157          *
158          * @return Ids as vector of int
159          */
160         std::vector<int> getResourceAutomationIds();
161
162         /**
163          * This method is used to get the Ids of all ongoing attribute update automation .
164          *
165          * @return Ids as vector of int
166          */
167         std::vector<int> getAttributeAutomationIds();
168
169         /**
170         * This method is used to stop the automation.
171         *
172         * @param id - Identifier for automation
173         */
174         void stopUpdateAutomation(const int id);
175
176         /**
177          * This method is used to add a new attribute to the resource model.
178          *
179          * @param attrName - Name of the attribute
180          * @param attrValue - Value of the attribute
181          *
182          * @return void
183          */
184         template <typename T>
185         void addAttribute(const std::string &attrName, const T &attrValue)
186         {
187             m_resModel.addAttribute(attrName, attrValue);
188             notifyListOfObservers();
189         }
190
191         /**
192          * This method is used to set the value range of an attribute.
193          * This method is intended to be used for attributes whose values are numbers only.
194          *
195          * @param attrName - Name of the attribute
196          * @param min - Minimum value of the range
197          * @param max - Maximum value of the range
198          *
199          * @return void
200          */
201         void setRange(const std::string &attrName, const int min, const int max);
202
203         /**
204          * This method is used to set the allowed values of an attribute.
205          *
206          * @param attrName - Name of the attribute
207          * @param values - Allowed values
208          *
209          * @return void
210          */
211         template <typename T>
212         void setAllowedValues(const std::string &attrName, const std::vector<T> &values)
213         {
214             m_resModel.setAllowedValues(attrName, values);
215         }
216
217         /**
218          * This method is used to set the update interval time for automation.
219          *
220          * @param attrName - Name of the attribute
221          * @param interval - Interval time in miliseconds for attribute value update automation
222          *
223          * @return void
224          */
225         void setUpdateInterval(const std::string &attrName, int interval);
226
227         /**
228          * This method is used to update the value of an attribute.
229          *
230          * @param attrName - Name of the attribute
231          * @param value - Value of the attribute
232          *
233          * @return void
234          */
235         template <typename T>
236         void updateAttribute(const std::string &attrName, const T &value)
237         {
238             m_resModel.updateAttribute(attrName, value);
239             notifyListOfObservers();
240         }
241
242         /**
243          * This method is used to update the attribute's value by taking the index of the value
244          * in the allowed values range.
245          *
246          * @param attrName - Name of the attribute
247          * @param allowedValueIndex - Index of the value in the allowed values range
248          *
249          * @return void
250          */
251         void updateAttributeFromAllowedValues(const std::string &attrName, const int allowedValueIndex);
252
253         /**
254           * This method is used to remove an attribute from the resource model.
255           *
256           * @param attName - Name of the attribute to be removed
257           *
258           * @return void
259           */
260         void removeAttribute(const std::string &attName);
261
262         /**
263          * This method is used to get the object of SimulatorResourceModel.
264          * Attributes of the resource are accessed using this object.
265          *
266          * @return SimulatorResourceModel - Resource model of the resource
267          */
268         SimulatorResourceModel getModel() const;
269
270         /**
271          * This method is used to set the callback for receiving the notifications when the
272          * resource model changes.
273          *
274          * @param callback - Callback to be set for receiving the notifications.
275          *
276          * @return SimulatorResourceModel - Resource model of the resource
277          */
278         void setModelChangeCallback(ResourceModelChangedCB callback);
279
280     private:
281         SimulatorResult start();
282         SimulatorResult stop();
283         OC::OCRepresentation getOCRepresentation();
284         bool modifyResourceModel(OC::OCRepresentation &ocRep);
285         OCEntityHandlerResult entityHandler(std::shared_ptr<OC::OCResourceRequest> request);
286         void notifyListOfObservers ();
287
288         SimulatorResourceModel m_resModel;
289         std::string m_uri;
290         std::string m_resourceType;
291         std::string m_interfaceType;
292         std::string m_name;
293         ResourceModelChangedCB m_callback;
294         UpdateAutomationManager m_updateAutomationMgr;
295
296         OCResourceHandle m_resourceHandle;
297         OCResourceProperty m_property;
298         OC::ObservationIds m_interestedObservers;
299 };
300
301 typedef std::shared_ptr<SimulatorResourceServer> SimulatorResourceServerPtr;
302
303 #endif