Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / simulator / inc / simulator_single_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_single_resource.h
23  *
24  * @brief   This file provides a class and API to access simulated resource and to perfrom auto
25  * update operations on simulated resource.
26  */
27
28 #ifndef SIMULATOR_SINGLE_RESOURCE_H_
29 #define SIMULATOR_SINGLE_RESOURCE_H_
30
31 #include "simulator_resource.h"
32
33 enum class AutoUpdateType
34 {
35     ONE_TIME,
36     REPEAT
37 };
38
39 /**
40  * @class   SimulatorSingleResource
41  * @brief   This class provides a set of APIs for handling simulated resource.
42  */
43 class SimulatorSingleResource : public SimulatorResource
44 {
45     public:
46
47         /**
48          * Callback method for receiving notifications when resource/attribute value updation
49          * completes.
50          *
51          * @param uri - URI of resource.
52          * @param id - Update automation identifier.
53          */
54         typedef std::function<void (const std::string &uri, const int id)>
55         AutoUpdateCompleteCallback;
56
57         /**
58          * API to get attribute from resource's resource model.
59          *
60          * @param attrName - Attribute's name.
61          * @param attribute - A attribute of resource's resource model.
62          *
63          * @return bool - true on success, otherwise false.
64          */
65         virtual bool getAttribute(const std::string &attrName,
66                                   SimulatorResourceAttribute &attribute) = 0;
67
68         /**
69          * API to get attribute from resource's resource model.
70          *
71          * @param attrName - Attribute's name.
72          * @param attribute - A attribute of resource's resource model.
73          *
74          * @return bool - true on success, otherwise false.
75          */
76         virtual std::map<std::string, SimulatorResourceAttribute> getAttributes() = 0;
77
78         /**
79          * API to add a new attribute to the resource model.
80          *
81          * @param attribute - Attribute to be add to model.
82          * @param notify - If value is true then notification will be send to observers on success.
83          *                             This flag is set to true by default.
84          *
85          * NOTE: API throws @SimulatorException exceptions on failure.
86          */
87         virtual bool addAttribute(const SimulatorResourceAttribute &attribute,
88                                   bool notify = true) = 0;
89
90         /**
91          * API to remove an attribute from the resource model.
92          *
93          * @param attrName - Name of the attribute to be removed.
94          * @param notify - If value is true then notification will be send to observers on success.
95          *                             This flag is set to true by default.
96          *
97          * @return bool - true on success, otherwise false.
98          */
99         virtual bool removeAttribute(const std::string &attrName, bool notify = true) = 0;
100
101         /**
102          * API to update the value of an attribute.
103          *
104          * @param attrName - Name of the attribute.
105          * @param value - Value of the attribute.
106          * @param notify - If value is true then notification will be send to observers on success.
107          *                             This flag is set to true by default.
108          */
109         template <typename T>
110         bool updateAttributeValue(const std::string &attrName, const T &value, bool notify = true)
111         {
112             SimulatorResourceAttribute attribute(attrName);
113             attribute.setValue(value);
114             return updateAttributeValue(attribute, notify);
115         }
116
117         /**
118          * API to update the value of an attribute.
119          *
120          * @param attribute - A resource model attribute.
121          * @param notify - If value is true then notification will be send to observers on success.
122          *                             This flag is set to true by default.
123          *
124          * @return bool - true on success, otherwise false.
125          */
126         virtual bool updateAttributeValue(const SimulatorResourceAttribute &attribute,
127                                           bool notify = true) = 0;
128
129         /**
130          * API to start the attribute value update automation for all attributes.
131          * Values for the attributes will be selected from their allowed range
132          * and the updated resource model will be notified to all the observers of the resource.
133          *
134          * @param type - Automation type.
135          * @param updateInterval - Time in milliseconds to be set as interval between updating
136          *                                              attribute values.
137          * @param callback - Callback to get notifiy when update automation is finished.
138          *
139          * @return ID representing update automation session.
140          * NOTE: API throws @InvalidArgsException, @SimulatorException exceptions.
141          */
142         virtual int startResourceUpdation(AutoUpdateType type, int updateInterval,
143                                           AutoUpdateCompleteCallback callback) = 0;
144
145         /**
146          * This method is used to start the attribute value update automation for
147          * specific attribute. Values for the attribute will be selected from its allowed range
148          * and the updated resource model will be notified to all the observers of the resource.
149          *
150          * @param attrName - Name of the attribute to be automated.
151          * @param type - Automation type.
152          * @param updateInterval - Time in milliseconds to be set as interval between updating
153          *                                              attribute values.
154          * @param callback - Callback to get notifiy when update automation is finished.
155          *
156          * @return ID representing update automation session.
157          * NOTE: API throws @InvalidArgsException, @SimulatorException exceptions.
158          */
159         virtual int startAttributeUpdation(const std::string &attrName, AutoUpdateType type,
160                                            int updateInterval, AutoUpdateCompleteCallback callback) = 0;
161
162         /**
163          * API to get the Ids of all ongoing resource update automation .
164          *
165          * @return vector of resource automation ids.
166          */
167         virtual std::vector<int> getResourceUpdations() = 0;
168
169         /**
170          * API to get the Ids of all ongoing attribute update automation .
171          *
172          * @return vector of attribute automation ids.
173          */
174         virtual std::vector<int> getAttributeUpdations() = 0;
175
176         /**
177          * API to stop the resource/attribute automation.
178          *
179          * @param id - Identifier for automation.
180          */
181         virtual void stopUpdation(int id) = 0;
182 };
183
184 typedef std::shared_ptr<SimulatorSingleResource> SimulatorSingleResourceSP;
185
186 #endif