35b2075bb20ee88efd6f2c1e7be76b939dc1de64
[platform/upstream/iotivity.git] / service / things-manager / sdk / inc / GroupManager.h
1 //******************************************************************
2 //
3 // Copyright 2014 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
23  *
24  * This file contains the declaration of classes and its members related to
25  * GroupManager.
26  */
27
28 #ifndef __OC_GROUPMANAGER__
29 #define __OC_GROUPMANAGER__
30
31 #include <string>
32 #include <vector>
33 #include <map>
34 #include <cstdlib>
35 #include <ActionSet.h>
36 #include "OCPlatform.h"
37 #include "OCApi.h"
38
39 using namespace OC;
40
41 namespace OIC
42 {
43 typedef std::function< void(std::vector< std::shared_ptr< OCResource > >) > CandidateCallback;
44 typedef std::function< void(std::string, OCStackResult) > CollectionPresenceCallback;
45
46 typedef std::function< void(const HeaderOptions&, const OCRepresentation&, const int) > GetCallback;
47 typedef std::function< void(const HeaderOptions&, const OCRepresentation&, const int) > PostCallback;
48 typedef std::function< void(const HeaderOptions&, const OCRepresentation&, const int) > PutCallback;
49
50 /**
51  * @class GroupManager
52  * @brief
53  * This APIs provide functions for application to find appropriate devices (i.e. things) in network,
54  * create a group of the devices, check a presence of member devices in the group, and actuate a
55  * group action in a more convenient way.
56  */
57 class GroupManager
58 {
59 public:
60     /**
61      * Constructor for GroupManager. Constructs a new GroupManager
62      */
63     GroupManager(void);
64
65     /**
66      * Virtual destructor
67      */
68     ~GroupManager(void);
69
70     /**
71      * API for candidate resources discovery.
72      * Callback only call when all resource types found.
73      *
74      * @param resourceTypes - required resource types(called "candidate")
75      * @param candidateCallback - callback. OCResource vector.
76      *
77      * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
78      *
79      * NOTE: OCStackResult is defined in ocstack.h.
80      */
81     OCStackResult findCandidateResources(std::vector< std::string > resourceTypes,
82             CandidateCallback callback, int waitsec = -1);
83
84     /**
85      * API for Collection member's state subscribe.
86      *
87      * NOTE: NOT IMPLEMENT YET
88      */
89     OCStackResult subscribeCollectionPresence(std::shared_ptr< OCResource > resource,
90             CollectionPresenceCallback);
91
92     OCStackResult bindResourceToGroup(OCResourceHandle& childHandle,
93             std::shared_ptr< OCResource > resource, OCResourceHandle& collectionHandle);
94
95 private:
96
97     void onFoundResource(std::shared_ptr< OCResource > resource, int waitsec);
98     void findPreparedRequest(std::map< std::vector< std::string >, CandidateCallback > &request);
99     void lazyCallback(int second);
100
101     void onGetForPresence(const HeaderOptions& headerOptions, const OCRepresentation& rep,
102             const int eCode, CollectionPresenceCallback callback);
103     void checkCollectionRepresentation(const OCRepresentation& rep,
104             CollectionPresenceCallback callback);
105     void collectionPresenceHandler(OCStackResult result, const unsigned int nonce,
106             const std::string& hostAddress, std::string host, std::string uri);
107
108     /**
109      *   API for Collection(Group) action.
110      */
111
112 public:
113     /**
114      * API for extracting an action set string from the ActionSet class instance
115      *
116      * @param newActionSet pointer of ActionSet class instance
117      *
118      * @return std::string return value of this API.
119      *                     It returns an action set String.
120      * @note OCStackResult is defined in ocstack.h.
121      */
122     std::string getStringFromActionSet(const ActionSet *newActionSet);
123
124     /**
125      * API for extrracting ActionSet class instance from an action set string.
126      *
127      * @param desc description of an action set string
128      *
129      * @return ActionSet* return value of this API.
130      *                      It returns pointer of ActionSet.
131      */
132     ActionSet* getActionSetfromString(std::string desc);
133
134     /**
135      * API for adding an action set.
136      * Callback is called when the response of PUT operation arrives.
137      *
138      * @param resource resource pointer of the group resource
139      * @param newActionSet pointer of ActionSet class instance
140      * @param callback callback for PUT operation.
141      *
142      * @return Returns ::OC_STACK_OK if success.
143      *
144      * @note OCStackResult is defined in ocstack.h.
145      */
146     OCStackResult addActionSet(std::shared_ptr< OCResource > resource,
147             const ActionSet* newActionSet, PutCallback cb);
148
149     /**
150      * API for executing an existing action set.
151      * Callback is called when the response of  POST operation arrives.
152      *
153      * @param resource resource pointer of the group resource
154      * @param actionsetName the action set name for executing the action set
155      * @param callback callback for POST operation.
156      *
157      * @return Returns ::OC_STACK_OK if success.
158      * @note OCStackResult is defined in ocstack.h.
159      */
160     OCStackResult executeActionSet(std::shared_ptr< OCResource > resource,
161             std::string actionsetName, PostCallback cb);
162
163     /**
164      * API for executing an existing action set.
165      * Callback is called when the response of  POST operation arrives.
166      *
167      * @param resource resource pointer of the group resource
168      * @param actionsetName the action set name for executing the action set
169      * @param delay waiting time for until the action set run.
170      * @param callback callback for POST operation.
171      *
172      * @return Returns ::OC_STACK_OK if success.
173      * @note OCStackResult is defined in ocstack.h.
174      */
175     OCStackResult executeActionSet(std::shared_ptr< OCResource > resource,
176             std::string actionsetName, long int delay, PostCallback cb);
177
178     /**
179      * API for canceling an existing action set.
180      * Callback is called when the response of POST operation arrives.
181      *
182      * @param resource resource pointer of the group resource
183      * @param actionsetName the action set name for executing the action set
184      * @param callback callback for POST operation.
185      *
186      * @return Returns ::OC_STACK_OK if success.
187      * @note OCStackResult is defined in ocstack.h.
188      */
189     OCStackResult cancelActionSet(std::shared_ptr< OCResource > resource,
190             std::string actionsetName, PostCallback cb);
191
192     /**
193      * API for reading an existing action set.
194      * Callback is called when the response of GET operation arrives.
195      *
196      * @param resource resource pointer of the group resource
197      * @param actionsetName the action set name for reading the action set
198      * @param callback callback for GET operation.
199      *
200      * @return Returns ::OC_STACK_OK if success.
201      * @note OCStackResult is defined in ocstack.h.
202      */
203     OCStackResult getActionSet(std::shared_ptr< OCResource > resource, std::string actionsetName,
204             PostCallback cb);
205
206     /**
207      * API for removing an existing action set.
208      * Callback is called when the response of  POST operation arrives.
209      *
210      * @param resource resource pointer of the group resource
211      * @param actionsetName the action set name for removing the action set
212      * @param callback callback for POST operation.
213      *
214      * @return Returns ::OC_STACK_OK if success.
215      * @note OCStackResult is defined in ocstack.h.
216      */
217     OCStackResult deleteActionSet(std::shared_ptr< OCResource > resource, std::string actionsetName,
218             PostCallback);
219 };
220 }
221 #endif  /* __OC_GROUPMANAGER__*/