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