23e9f85dd535f40bfeee1ac3171f8848aff64a96
[platform/upstream/iotivity.git] / service / resource-container / bundle-api / include / BundleResource.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 #ifndef BUNDLERESOURCE_H_
22 #define BUNDLERESOURCE_H_
23
24 #include <list>
25 #include <string>
26 #include <map>
27 #include <vector>
28 #include <memory>
29 #include <mutex>
30
31
32 #include "NotificationReceiver.h"
33 #include "RCSResourceAttributes.h"
34
35 namespace OIC
36 {
37     namespace Service
38     {
39
40         /**
41         * @class    BundleResource
42         * @brief    This class represents Basic bundle resource template
43         *               to be registered in the container and make resource server
44         *
45         */
46         class BundleResource
47         {
48             public:
49                 typedef std::shared_ptr< BundleResource > Ptr;
50
51                 /**
52                 * Constructor for BundleResource
53                 */
54                 BundleResource();
55
56                 /**
57                 * Virtual destructor for BundleResource
58                 */
59                 virtual ~BundleResource();
60
61                 /**
62                 * Return the list of attribute names of the resource
63                 *
64                 * @return List of the attribute names
65                 */
66                 std::list<std::string> getAttributeNames();
67
68                 /**
69                 * Initialize attributes of the resource
70                 *
71                 * @return void
72                 */
73                 virtual void initAttributes() = 0;
74
75                 /**
76                 * Register notification receiver(resource container) to notify for the
77                 *     changes of attributes
78                 *
79                 * @param pNotiReceiver Notification Receiver to get notification from
80                 * bundle resource
81                 *
82                 * @return void
83                 */
84                 void registerObserver(NotificationReceiver* pNotiReceiver);
85
86                 /**
87                 * Return all attributes of the resource
88                 *
89                 * @return Attributes of the resource
90                 */
91                 const RCSResourceAttributes getAttributes();
92
93                 /**
94                 * Set attributes of the resource
95                 *
96                 * @param attrs Attributes to set
97                 *
98                 * @return void
99                 */
100                 void setAttributes(const RCSResourceAttributes &attrs);
101
102                 void setAttributes(const RCSResourceAttributes &attrs, bool notify);
103
104                 /**
105                 * Return the value of an attribute
106                 *
107                 * @param key Key of attribute to get
108                 *
109                 * @return Value of the attribute
110                 */
111                 RCSResourceAttributes::Value getAttribute(const std::string &key);
112
113                 /**
114                 * Sets the value of an attribute
115                 *
116                 * @param key Name of attribute to set
117                 *
118                 * @param value Value of attribute to set
119                 *
120                 * @param notify Flag to indicate if OIC clients should be notified about an update
121                 *
122                 * @return void
123                 */
124                 void setAttribute(const std::string &key, RCSResourceAttributes::Value &&value,
125                                   bool notify);
126
127                 /**
128                 * Sets the value of an attribute
129                 *
130                 * @param key Name of attribute to set
131                 *
132                 * @param value Value of attribute to set
133                 *
134                 * @param notify Flag to indicate if OIC clients should be notified about an update
135                 *
136                 * @return void
137                 */
138                 void setAttribute(const std::string &key, RCSResourceAttributes::Value &value,
139                                   bool notify);
140
141                 /**
142                 * Sets the value of an attribute
143                 *
144                 * @param key Name of attribute to set
145                 *
146                 * @param value Value of attribute to set
147                 *
148                 * @return void
149                 */
150                 void setAttribute(const std::string &key, RCSResourceAttributes::Value &&value);
151
152                 /**
153                 * Sets the value of an attribute
154                 *
155                 * @param key Name of attribute to set
156                 *
157                 * @param value Value of attribute to set
158                 *
159                 * @return void
160                 */
161                 void setAttribute(const std::string &key, RCSResourceAttributes::Value &value);
162
163                 /**
164                 * This function should be implemented by the according bundle resource
165                 * and execute the according business logic (e.g., light switch or sensor resource)
166                 * to retrieve a sensor value. If a new sensor value is retrieved, the
167                 * setAttribute data should be called to update the value.
168                 * The implementor of the function can decide weather to notify OIC clients
169                 * about the changed state or not.
170                 *
171                 * @return All attributes
172                 */
173                 virtual RCSResourceAttributes handleGetAttributesRequest() = 0;
174
175                 /**
176                 * This function should be implemented by the according bundle resource
177                 * and execute the according business logic (e.g., light switch or sensor resource)
178                 * and write either on soft sensor values or external bridged devices.
179                 *
180                 * The call of this method could for example trigger a HTTP PUT request on
181                 * an external APIs. This method is responsible to update the resource internal
182                 * data and call the setAttribute method.
183                 *
184                 * The implementor of the function can decide weather to notify OIC clients
185                 * about the changed state or not.
186                 *
187                 * @param attrs Attributes to set
188                 *
189                 * @return void
190                 */
191                 virtual void handleSetAttributesRequest(const RCSResourceAttributes &attrs) = 0;
192             private:
193
194                 void sendNotification(NotificationReceiver *notficiationRecevier, std::string uri);
195
196             public:
197                 std::string m_bundleId;
198                 std::string m_name, m_uri, m_resourceType, m_interface, m_address;
199                 std::map< std::string,
200                 std::vector< std::map< std::string, std::string > > > m_mapResourceProperty;
201
202             private:
203                 NotificationReceiver* m_pNotiReceiver;
204                 RCSResourceAttributes m_resourceAttributes;
205                 std::mutex m_resourceAttributes_mutex;
206         };
207     }
208 }
209
210 #endif