Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / things-manager / sdk / src / ThingsManager.cpp
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  */
25
26 #include "ThingsManager.h"
27 #include "GroupManager.h"
28 #include "GroupSynchronization.h"
29 #include "ThingsConfiguration.h"
30 #include "ThingsDiagnostics.h"
31 #include <algorithm>
32 #include <thread>
33
34 using namespace OC;
35 namespace OIC
36 {
37
38     GroupManager *g_groupManager;
39     GroupSynchronization *g_groupSync = NULL;
40     ThingsConfiguration *g_thingsConf = NULL;
41     ThingsDiagnostics *g_thingsDiag = NULL;
42
43     ThingsManager::ThingsManager(void)
44     {
45         g_groupManager = new GroupManager();
46         g_groupSync = GroupSynchronization::getInstance();
47         g_thingsConf = ThingsConfiguration::getInstance();
48         g_thingsDiag = ThingsDiagnostics::getInstance();
49         g_thingsConf->setGroupManager(g_groupManager);
50         g_thingsDiag->setGroupManager(g_groupManager);
51     }
52
53     /**
54      * Virtual destructor
55      */
56     ThingsManager::~ThingsManager(void)
57     {
58         delete g_groupManager;
59         g_groupSync->deleteInstance();
60         g_thingsConf->deleteInstance();
61         g_thingsDiag->deleteInstance();
62     }
63
64     OCStackResult ThingsManager::findCandidateResources(std::vector< std::string > resourceTypes,
65             std::function< void(std::vector< std::shared_ptr< OCResource > >) > callback,
66             int waitsec)
67     {
68         OCStackResult result = g_groupManager->findCandidateResources(resourceTypes, callback,
69                 waitsec);
70
71         return result;
72     }
73
74     OCStackResult ThingsManager::subscribeCollectionPresence(std::shared_ptr< OCResource > resource,
75             std::function< void(std::string, OCStackResult) > callback)
76     {
77         OCStackResult result = g_groupManager->subscribeCollectionPresence(resource, callback);
78
79         return result;
80     }
81
82     OCStackResult ThingsManager::bindResourceToGroup(OCResourceHandle& childHandle, std::shared_ptr< OCResource > resource, OCResourceHandle& collectionHandle)
83     {
84         OCStackResult result = g_groupManager->bindResourceToGroup(childHandle,resource,collectionHandle);
85
86         return result;
87     }
88
89     OCStackResult ThingsManager::findGroup(std::vector< std::string > collectionResourceTypes,
90             FindCallback callback)
91     {
92         OCStackResult result = g_groupSync->findGroup(collectionResourceTypes, callback);
93
94         return result;
95     }
96
97     OCStackResult ThingsManager::createGroup(std::string collectionResourceType)
98     {
99         OCStackResult result = g_groupSync->createGroup(collectionResourceType);
100
101         return result;
102     }
103
104     OCStackResult ThingsManager::joinGroup(std::string collectionResourceType,
105             OCResourceHandle resourceHandle)
106     {
107         OCStackResult result = g_groupSync->joinGroup(collectionResourceType, resourceHandle);
108
109         return result;
110     }
111
112     OCStackResult ThingsManager::joinGroup(const std::shared_ptr< OCResource > resource,
113             OCResourceHandle resourceHandle)
114     {
115         OCStackResult result = g_groupSync->joinGroup(resource, resourceHandle);
116
117         return result;
118     }
119
120     OCStackResult ThingsManager::leaveGroup(std::string collectionResourceType,
121             OCResourceHandle resourceHandle)
122     {
123         OCStackResult result = g_groupSync->leaveGroup(collectionResourceType, resourceHandle);
124
125         return result;
126     }
127
128     OCStackResult ThingsManager::leaveGroup(const std::shared_ptr< OCResource > resource,
129                             std::string collectionResourceType,
130                             OCResourceHandle resourceHandle)
131     {
132         return g_groupSync->leaveGroup(resource, collectionResourceType, resourceHandle);
133     }
134
135     void ThingsManager::deleteGroup(std::string collectionResourceType)
136     {
137         g_groupSync->deleteGroup(collectionResourceType);
138     }
139
140     std::map< std::string, OCResourceHandle > ThingsManager::getGroupList()
141     {
142         return g_groupSync->getGroupList();
143     }
144
145     OCStackResult ThingsManager::updateConfigurations(std::shared_ptr< OCResource > resource,
146             std::map< ConfigurationName, ConfigurationValue > configurations,
147             ConfigurationCallback callback)
148     {
149         return g_thingsConf->updateConfigurations(resource, configurations, callback);
150     }
151     OCStackResult ThingsManager::getConfigurations(std::shared_ptr< OCResource > resource,
152             std::vector< ConfigurationName > configurations, ConfigurationCallback callback)
153     {
154         return g_thingsConf->getConfigurations(resource, configurations, callback);
155     }
156     std::string ThingsManager::getListOfSupportedConfigurationUnits()
157     {
158         return g_thingsConf->getListOfSupportedConfigurationUnits();
159     }
160
161     OCStackResult ThingsManager::doBootstrap(ConfigurationCallback callback)
162     {
163         return g_thingsConf->doBootstrap(callback);
164     }
165
166     OCStackResult ThingsManager::reboot(std::shared_ptr< OCResource > resource,
167             ConfigurationCallback callback)
168     {
169         return g_thingsDiag->reboot(resource, callback);
170     }
171     OCStackResult ThingsManager::factoryReset(std::shared_ptr< OCResource > resource,
172             ConfigurationCallback callback)
173     {
174         return g_thingsDiag->factoryReset(resource, callback);
175     }
176
177     std::string ThingsManager::getStringFromActionSet(const ActionSet *newActionSet)
178     {
179         return g_groupManager->getStringFromActionSet(newActionSet);
180     }
181     ActionSet* ThingsManager::getActionSetfromString(std::string desc)
182     {
183         return g_groupManager->getActionSetfromString(desc);
184     }
185     OCStackResult ThingsManager::addActionSet(std::shared_ptr< OCResource > resource,
186             const ActionSet* newActionSet, PutCallback cb)
187     {
188         return g_groupManager->addActionSet(resource, newActionSet, cb);
189     }
190     OCStackResult ThingsManager::executeActionSet(std::shared_ptr< OCResource > resource,
191             std::string actionsetName, PostCallback cb)
192     {
193         return g_groupManager->executeActionSet(resource, actionsetName, cb);
194     }
195     OCStackResult ThingsManager::executeActionSet(std::shared_ptr< OCResource > resource,
196                     std::string actionsetName, long int delay, PostCallback cb)
197     {
198         return g_groupManager->executeActionSet(resource, actionsetName, delay, cb);
199     }
200     OCStackResult ThingsManager::cancelActionSet(std::shared_ptr< OCResource > resource,
201                     std::string actionsetName, PostCallback cb)
202     {
203         return g_groupManager->cancelActionSet(resource, actionsetName, cb);
204     }
205     OCStackResult ThingsManager::getActionSet(std::shared_ptr< OCResource > resource,
206             std::string actionsetName, GetCallback cb)
207     {
208         return g_groupManager->getActionSet(resource, actionsetName, cb);
209     }
210     OCStackResult ThingsManager::deleteActionSet(std::shared_ptr< OCResource > resource,
211             std::string actionsetName, PostCallback cb)
212     {
213         return g_groupManager->deleteActionSet(resource, actionsetName, cb);
214     }
215 }