Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / service / simulator / inc / 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_server_types.h"
32 #include "simulator_resource_model.h"
33 #include "simulator_exceptions.h"
34
35 enum class ObservationStatus : unsigned char
36 {
37     OBSERVE_REGISTER,
38     OBSERVE_UNREGISTER
39 };
40
41 typedef struct
42 {
43     uint8_t id;
44     std::string address;
45     uint16_t port;
46 } ObserverInfo;
47
48 /**
49  * @class   SimulatorResourceServer
50  * @brief   This class provides a set of functions for operating and automating a resource.
51  */
52 class SimulatorResourceServer
53 {
54     public:
55         /**
56          * Callback method for receiving notifications when resource model gets changed.
57          *
58          * @param uri - Resource URI
59          * @param resModel - Resource model
60          */
61         typedef std::function<void (const std::string &uri, const SimulatorResourceModel &resModel)>
62         ResourceModelChangedCB;
63
64         /**
65          * Callback method for receiving notifications when observer is registered/unregistered
66          * with resource.
67          *
68          * @param uri - Resource URI
69          * @param state - OBSERVE_REGISTER if observer is registered, otherwise OBSERVE_UNREGISTER.
70          * @param observerInfo - Information about observer.
71          */
72         typedef std::function<void (const std::string &uri, ObservationStatus state, const ObserverInfo &observerInfo)>
73         ObserverCB;
74
75         SimulatorResourceServer();
76
77         virtual ~SimulatorResourceServer() {};
78
79         /**
80          * API to get the resource URI.
81          *
82          * @return Resource URI
83          */
84         std::string getURI() const;
85
86         /**
87          * API to get the resource URI.
88          *
89          * @return Resource Type
90          */
91         std::string getResourceType() const;
92
93         /**
94          * API to get the interface type of the resource.
95          *
96          * @return Interface type of the resource
97          */
98         std::string getInterfaceType() const;
99
100         /**
101          * API to get the name of the resource.
102          *
103          * @return Resource name
104          */
105         std::string getName() const;
106
107         /**
108          * API to add a new attribute to the resource model.
109          *
110          * @param attribute - Attribute to be add to model.
111          */
112         void addAttribute(SimulatorResourceModel::Attribute &attribute);
113
114         /**
115          * API to set the value range of an attribute.
116          * This method is intended to be used for attributes whose values are numbers only.
117          *
118          * @param attrName - Name of the attribute
119          * @param min - Minimum value of the range
120          * @param max - Maximum value of the range
121          */
122         void setRange(const std::string &attrName, const int min, const int max);
123
124         /**
125          * API to set the allowed values of an attribute.
126          *
127          * @param attrName - Name of the attribute
128          * @param values - Allowed values
129          */
130         template <typename T>
131         void setAllowedValues(const std::string &attrName, const std::vector<T> &values)
132         {
133             m_resModel.setAllowedValues(attrName, values);
134         }
135
136         /**
137          * API to set the update interval time for automation.
138          *
139          * @param attrName - Name of the attribute
140          * @param interval - Interval time in miliseconds for attribute value update automation
141          */
142         void setUpdateInterval(const std::string &attrName, int interval);
143
144         /**
145          * API to update the value of an attribute.
146          *
147          * @param attrName - Name of the attribute
148          * @param value - Value of the attribute
149          */
150         template <typename T>
151         void updateAttributeValue(const std::string &attrName, const T &value)
152         {
153             m_resModel.updateAttribute(attrName, value);
154
155             // Notify all the subscribers
156             notifyAll();
157         }
158
159         /**
160          * API to update the attribute's value by taking the index of the value
161          * in the allowed values range.
162          *
163          * @param attrName - Name of the attribute
164          * @param allowedValueIndex - Index of the value in the allowed values range
165          */
166         void updateFromAllowedValues(const std::string &attrName, unsigned int index);
167
168         /**
169           * API to remove an attribute from the resource model.
170           *
171           * @param attName - Name of the attribute to be removed
172           */
173         void removeAttribute(const std::string &attName);
174
175         /**
176          * API to get the object of SimulatorResourceModel.
177          * Attributes of the resource are accessed using this object.
178          *
179          * @return Resource model of the resource.
180          */
181         SimulatorResourceModel getModel() const;
182
183         /**
184          * API to get the observable state of resource.
185          *
186          * @return bool - true if resource is observable, otherwise false.
187          */
188         virtual bool isObservable() const = 0;
189
190         /**
191          * API to start the attribute value automation for all attributes.
192          * Once started, values for the attributes will be selected randomly from their allowed range
193          * and the updated values will be notified to all the observers of the resource.
194          *
195          * @param type - Automation type.
196          * @param callback - Callback to get notifiy when update automation is finished.
197          * @param id - Identifier for automation.
198          *
199          * @return ID representing update automation session.
200          * NOTE: API throws @InvalidArgsException, @SimulatorException exceptions.
201          */
202         virtual int startUpdateAutomation(AutomationType type, int updateInterval,
203                                           updateCompleteCallback callback) = 0;
204
205         /**
206          * This method is used to start the attribute value automation for a specific attribute.
207          * Once started, values for the attribute will be selected randomly from its allowed range
208          * and the updated value will be notified to all the observers of the resource.
209          *
210          * @param attrName - Name of the attribute to be automated.
211          * @param type - Automation type.
212          * @param updateInterval -Interval time in milliseconds for attribute value update automation.
213          * @param callback - Callback to get notifiy when update automation is finished.
214          * @param id - Identifier for automation.
215          *
216          * @return ID representing update automation session.
217          * NOTE: API throws @InvalidArgsException, @SimulatorException exceptions.
218          */
219         virtual int startUpdateAutomation(const std::string &attrName, AutomationType type,
220                                           int updateInterval, updateCompleteCallback callback) = 0;
221
222         /**
223          * API to get the Ids of all ongoing resource update automation .
224          *
225          * @return vector of resource automation ids.
226          */
227         virtual std::vector<int> getResourceAutomationIds() = 0;
228
229         /**
230          * API to get the Ids of all ongoing attribute update automation .
231          *
232          * @return vector of attribute automation ids.
233          */
234         virtual std::vector<int> getAttributeAutomationIds() = 0;
235
236         /**
237         * API to stop the resource/attribute automation.
238         *
239         * @param id - Identifier for automation.
240         */
241         virtual void stopUpdateAutomation(const int id) = 0;
242
243         /**
244          * API to set the callback for receiving the notifications when the
245          * resource model changes.
246          *
247          * @param callback - Callback to be set for receiving the notifications.
248          */
249         virtual void setModelChangeCallback(ResourceModelChangedCB callback) = 0;
250
251         /**
252          * API to set the callback for receiving the notifications when
253          * observer is registered or unregistered with resource.
254          *
255          * @param callback - Callback to be set for receiving the notifications.
256          */
257         virtual void setObserverCallback(ObserverCB callback) = 0;
258
259         /**
260          * API to get observers which are registered with resource.
261          *
262          * @return vector of ObserverInfo.
263          */
264         virtual std::vector<ObserverInfo> getObserversList() = 0;
265
266         /**
267          * API to notify current resource model to specific observer.
268          *
269          * NOTE: API throws @SimulatorException exception.
270          */
271         virtual void notify(uint8_t id) = 0;
272
273         /**
274          * API to notify all registered observers.
275          *
276          * NOTE: API throws @SimulatorException exception.
277          */
278         virtual void notifyAll() = 0;
279
280     protected:
281         std::string m_name;
282         std::string m_uri;
283         std::string m_resourceType;
284         std::string m_interfaceType;
285         SimulatorResourceModel m_resModel;
286 };
287
288 typedef std::shared_ptr<SimulatorResourceServer> SimulatorResourceServerSP;
289
290 #endif