added error callback for findResource() in stack
[platform/upstream/iotivity.git] / resource / src / OCPlatform_impl.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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 name:
23 //     OCPlatform_impl.cpp
24 //
25 // Description: Implementation of the OCPlatform functionality.  It contains
26 // a singleton interface that is used only by the OCPlatform namespace and is the
27 // central entrance to the stack.
28 //
29 //
30 //
31 //*********************************************************************
32
33 #include "OCPlatform_impl.h"
34
35 #include <random>
36 #include <utility>
37 #include <functional>
38
39 #include "ocstack.h"
40
41 #include "OCPlatform.h"
42 #include "OCApi.h"
43 #include "OCException.h"
44 #include "OCUtilities.h"
45 #include "ocpayload.h"
46
47 #include "oc_logger.hpp"
48
49 namespace OC
50 {
51
52     PlatformConfig& OCPlatform_impl::globalConfig()
53     {
54         static PlatformConfig s_config;
55         return s_config;
56     }
57
58     void OCPlatform_impl::Configure(const PlatformConfig& config)
59     {
60         OCRegisterPersistentStorageHandler(config.ps);
61         globalConfig() = config;
62     }
63
64     OCPlatform_impl& OCPlatform_impl::Instance()
65     {
66         static OCPlatform_impl platform(globalConfig());
67         return platform;
68     }
69
70     void OCPlatform_impl::init(const PlatformConfig& config)
71     {
72         switch(config.mode)
73         {
74             case ModeType::Server:
75                 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
76                 break;
77
78             case ModeType::Client:
79                 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
80                 break;
81
82             case ModeType::Both:
83             case ModeType::Gateway:
84                 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
85                 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
86                 break;
87          }
88     }
89
90     OCPlatform_impl::OCPlatform_impl(const PlatformConfig& config)
91      : m_cfg             { config },
92        m_WrapperInstance { make_unique<WrapperFactory>() },
93        m_csdkLock        { std::make_shared<std::recursive_mutex>() }
94     {
95         init(m_cfg);
96     }
97
98     OCPlatform_impl::~OCPlatform_impl(void)
99     {
100     }
101
102     OCStackResult OCPlatform_impl::setDefaultDeviceEntityHandler(EntityHandler entityHandler)
103     {
104         return checked_guard(m_server, &IServerWrapper::setDefaultDeviceEntityHandler,
105                              entityHandler);
106     }
107
108     OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle,
109                                                 QualityOfService QoS)
110     {
111         return result_guard(OCNotifyAllObservers(resourceHandle,
112                     static_cast<OCQualityOfService>(QoS)));
113     }
114
115     OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle)
116     {
117         return notifyAllObservers(resourceHandle, m_cfg.QoS);
118     }
119
120     OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
121                                        ObservationIds& observationIds,
122                                        const std::shared_ptr<OCResourceResponse> pResponse)
123     {
124         return notifyListOfObservers(resourceHandle, observationIds, pResponse, m_cfg.QoS);
125     }
126
127     OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
128                                        ObservationIds& observationIds,
129                                        const std::shared_ptr<OCResourceResponse> pResponse,
130                                        QualityOfService QoS)
131     {
132         if(!pResponse)
133         {
134          return result_guard(OC_STACK_ERROR);
135         }
136
137         OCRepPayload* pl = pResponse->getResourceRepresentation().getPayload();
138         OCStackResult result =
139                    OCNotifyListOfObservers(resourceHandle,
140                             &observationIds[0], observationIds.size(),
141                             pl,
142                             static_cast<OCQualityOfService>(QoS));
143         OCRepPayloadDestroy(pl);
144         return result_guard(result);
145     }
146
147     OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
148                                                 const std::string& uri,
149                                                 OCConnectivityType connectivityType,
150                                                 bool isObservable,
151                                                 const std::vector<std::string>& resourceTypes,
152                                                 const std::vector<std::string>& interfaces)
153     {
154         if(!m_client)
155         {
156             return std::shared_ptr<OCResource>();
157         }
158
159         return std::shared_ptr<OCResource>(new OCResource(m_client,
160                                             host,
161                                             uri, "", connectivityType,
162                                             isObservable,
163                                             resourceTypes,
164                                             interfaces));
165     }
166
167     OCStackResult OCPlatform_impl::findResource(const std::string& host,
168                                             const std::string& resourceName,
169                                             OCConnectivityType connectivityType,
170                                             FindCallback resourceHandler)
171     {
172         return findResource(host, resourceName, connectivityType, resourceHandler, m_cfg.QoS);
173     }
174
175     OCStackResult OCPlatform_impl::findResource(const std::string& host,
176                                             const std::string& resourceName,
177                                             OCConnectivityType connectivityType,
178                                             FindCallback resourceHandler,
179                                             QualityOfService QoS)
180     {
181         return checked_guard(m_client, &IClientWrapper::ListenForResource,
182                              host, resourceName, connectivityType, resourceHandler, QoS);
183     }
184
185     OCStackResult OCPlatform_impl::findResource(const std::string& host,
186                                             const std::string& resourceName,
187                                             OCConnectivityType connectivityType,
188                                             FindCallback resourceHandler,
189                                             FindErrorCallback errorHandler)
190     {
191         return findResource(host, resourceName, connectivityType, resourceHandler,
192                             errorHandler, m_cfg.QoS);
193     }
194
195     OCStackResult OCPlatform_impl::findResource(const std::string& host,
196                                             const std::string& resourceName,
197                                             OCConnectivityType connectivityType,
198                                             FindCallback resourceHandler,
199                                             FindErrorCallback errorHandler,
200                                             QualityOfService QoS)
201     {
202         return checked_guard(m_client, &IClientWrapper::ListenErrorForResource,
203                              host, resourceName, connectivityType, resourceHandler,
204                              errorHandler, QoS);
205     }
206
207     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
208                                             const std::string& deviceURI,
209                                             OCConnectivityType connectivityType,
210                                             FindDeviceCallback deviceInfoHandler)
211     {
212         return result_guard(getDeviceInfo(host, deviceURI, connectivityType,
213                deviceInfoHandler, m_cfg.QoS));
214     }
215
216     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
217                                             const std::string& deviceURI,
218                                             OCConnectivityType connectivityType,
219                                             FindDeviceCallback deviceInfoHandler,
220                                             QualityOfService QoS)
221     {
222         return checked_guard(m_client, &IClientWrapper::ListenForDevice,
223                              host, deviceURI, connectivityType, deviceInfoHandler, QoS);
224     }
225
226     OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
227                                             const std::string& platformURI,
228                                             OCConnectivityType connectivityType,
229                                             FindPlatformCallback platformInfoHandler)
230     {
231         return result_guard(getPlatformInfo(host, platformURI, connectivityType,
232                platformInfoHandler, m_cfg.QoS));
233     }
234
235     OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
236                                             const std::string& platformURI,
237                                             OCConnectivityType connectivityType,
238                                             FindPlatformCallback platformInfoHandler,
239                                             QualityOfService QoS)
240     {
241         return checked_guard(m_client, &IClientWrapper::ListenForDevice,
242                              host, platformURI, connectivityType, platformInfoHandler, QoS);
243     }
244
245     OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
246                                             std::string& resourceURI,
247                                             const std::string& resourceTypeName,
248                                             const std::string& resourceInterface,
249                                             EntityHandler entityHandler,
250                                             uint8_t resourceProperty)
251     {
252         return checked_guard(m_server, &IServerWrapper::registerResource,
253                              std::ref(resourceHandle), resourceURI, resourceTypeName,
254                              resourceInterface, entityHandler, resourceProperty);
255     }
256
257     OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
258     {
259         return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
260     }
261
262     OCStackResult OCPlatform_impl::registerPlatformInfo(const OCPlatformInfo platformInfo)
263     {
264         return checked_guard(m_server, &IServerWrapper::registerPlatformInfo, platformInfo);
265     }
266
267     OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
268                                             const std::shared_ptr< OCResource > resource)
269     {
270         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
271         std::vector<std::string> resourceTypes = resource->getResourceTypes();
272
273         return checked_guard(m_server, &IServerWrapper::registerResource,
274                 std::ref(resourceHandle), resource->host() + resource->uri(),
275                 resourceTypes[0]/*"core.remote"*/, DEFAULT_INTERFACE,
276                 (EntityHandler) nullptr, resourceProperty);
277     }
278
279     OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
280     {
281         return checked_guard(m_server, &IServerWrapper::unregisterResource,
282                              resourceHandle);
283     }
284
285     OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
286                                             OCResourceHandle resourceHandle)
287     {
288         return result_guard(OCUnBindResource(std::ref(collectionHandle), std::ref(resourceHandle)));
289     }
290
291     OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
292                                             const std::vector<OCResourceHandle>& resourceHandles)
293     {
294         for(const auto& h : resourceHandles)
295         {
296            OCStackResult r;
297
298            if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
299            {
300                return r;
301            }
302         }
303
304         return OC_STACK_OK;
305     }
306
307     OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
308                                             const OCResourceHandle resourceHandle)
309     {
310         return result_guard(OCBindResource(collectionHandle, resourceHandle));
311     }
312
313     OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
314                                             const std::vector<OCResourceHandle>& resourceHandles)
315     {
316         for(const auto& h : resourceHandles)
317         {
318            OCStackResult r;
319
320            if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
321            {
322                return r;
323            }
324         }
325
326         return OC_STACK_OK;
327     }
328
329     OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
330                                              const std::string& resourceTypeName) const
331     {
332         return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
333                              resourceHandle, resourceTypeName);
334     }
335
336     OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
337                                              const std::string& resourceInterfaceName) const
338     {
339         return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
340                              resourceHandle, resourceInterfaceName);
341     }
342
343     OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
344     {
345         return checked_guard(m_server, &IServerWrapper::startPresence,
346                              announceDurationSeconds);
347     }
348
349     OCStackResult OCPlatform_impl::stopPresence()
350     {
351         return checked_guard(m_server, &IServerWrapper::stopPresence);
352     }
353
354     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
355                                             const std::string& host,
356                                             OCConnectivityType connectivityType,
357                                             SubscribeCallback presenceHandler)
358     {
359         return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
360     }
361
362
363     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
364                                             const std::string& host,
365                                             const std::string& resourceType,
366                                             OCConnectivityType connectivityType,
367                                             SubscribeCallback presenceHandler)
368     {
369         return checked_guard(m_client, &IClientWrapper::SubscribePresence,
370                              &presenceHandle, host, resourceType, connectivityType,
371                              presenceHandler);
372     }
373
374     OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
375     {
376         return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
377                              std::ref(presenceHandle));
378     }
379
380     OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
381     {
382         return checked_guard(m_server, &IServerWrapper::sendResponse,
383                              pResponse);
384     }
385
386     std::weak_ptr<std::recursive_mutex> OCPlatform_impl::csdkLock()
387     {
388         return m_csdkLock;
389     }
390 } //namespace OC
391