a8e8fd36eb65a97f7e1b4a4b7eee5fd23c958686
[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         uint8_t resourceProperty = 0;
160         if (isObservable)
161         {
162             resourceProperty = (resourceProperty | OC_OBSERVABLE);
163         }
164         return std::shared_ptr<OCResource>(new OCResource(m_client,
165                                             host,
166                                             uri, "", connectivityType,
167                                             resourceProperty,
168                                             resourceTypes,
169                                             interfaces));
170     }
171
172     OCStackResult OCPlatform_impl::findResource(const std::string& host,
173                                             const std::string& resourceName,
174                                             OCConnectivityType connectivityType,
175                                             FindCallback resourceHandler)
176     {
177         return findResource(host, resourceName, connectivityType, resourceHandler, m_cfg.QoS);
178     }
179
180     OCStackResult OCPlatform_impl::findResource(const std::string& host,
181                                             const std::string& resourceName,
182                                             OCConnectivityType connectivityType,
183                                             FindCallback resourceHandler,
184                                             QualityOfService QoS)
185     {
186         return checked_guard(m_client, &IClientWrapper::ListenForResource,
187                              host, resourceName, connectivityType, resourceHandler, QoS);
188     }
189
190     OCStackResult OCPlatform_impl::findResource(const std::string& host,
191                                             const std::string& resourceName,
192                                             OCConnectivityType connectivityType,
193                                             FindCallback resourceHandler,
194                                             FindErrorCallback errorHandler)
195     {
196         return findResource(host, resourceName, connectivityType, resourceHandler,
197                             errorHandler, m_cfg.QoS);
198     }
199
200     OCStackResult OCPlatform_impl::findResource(const std::string& host,
201                                             const std::string& resourceName,
202                                             OCConnectivityType connectivityType,
203                                             FindCallback resourceHandler,
204                                             FindErrorCallback errorHandler,
205                                             QualityOfService QoS)
206     {
207         return checked_guard(m_client, &IClientWrapper::ListenErrorForResource,
208                              host, resourceName, connectivityType, resourceHandler,
209                              errorHandler, QoS);
210     }
211
212     OCStackResult OCPlatform_impl::findResourceList(const std::string& host,
213                                             const std::string& resourceName,
214                                             OCConnectivityType connectivityType,
215                                             FindResListCallback resourceHandler,
216                                             QualityOfService QoS)
217     {
218         return checked_guard(m_client, &IClientWrapper::ListenForResource2,
219                              host, resourceName, connectivityType, resourceHandler, QoS);
220     }
221
222     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
223                                             const std::string& deviceURI,
224                                             OCConnectivityType connectivityType,
225                                             FindDeviceCallback deviceInfoHandler)
226     {
227         return result_guard(getDeviceInfo(host, deviceURI, connectivityType,
228                deviceInfoHandler, m_cfg.QoS));
229     }
230
231     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
232                                             const std::string& deviceURI,
233                                             OCConnectivityType connectivityType,
234                                             FindDeviceCallback deviceInfoHandler,
235                                             QualityOfService QoS)
236     {
237         return checked_guard(m_client, &IClientWrapper::ListenForDevice,
238                              host, deviceURI, connectivityType, deviceInfoHandler, QoS);
239     }
240
241     OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
242                                             const std::string& platformURI,
243                                             OCConnectivityType connectivityType,
244                                             FindPlatformCallback platformInfoHandler)
245     {
246         return result_guard(getPlatformInfo(host, platformURI, connectivityType,
247                platformInfoHandler, m_cfg.QoS));
248     }
249
250     OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
251                                             const std::string& platformURI,
252                                             OCConnectivityType connectivityType,
253                                             FindPlatformCallback platformInfoHandler,
254                                             QualityOfService QoS)
255     {
256         return checked_guard(m_client, &IClientWrapper::ListenForDevice,
257                              host, platformURI, connectivityType, platformInfoHandler, QoS);
258     }
259
260     OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
261                                             std::string& resourceURI,
262                                             const std::string& resourceTypeName,
263                                             const std::string& resourceInterface,
264                                             EntityHandler entityHandler,
265                                             uint8_t resourceProperty)
266     {
267         return checked_guard(m_server, &IServerWrapper::registerResource,
268                              std::ref(resourceHandle), resourceURI, resourceTypeName,
269                              resourceInterface, entityHandler, resourceProperty);
270     }
271
272     OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
273     {
274         return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
275     }
276
277     OCStackResult OCPlatform_impl::registerPlatformInfo(const OCPlatformInfo platformInfo)
278     {
279         return checked_guard(m_server, &IServerWrapper::registerPlatformInfo, platformInfo);
280     }
281
282     OCStackResult OCPlatform_impl::setPropertyValue(OCPayloadType type, const std::string& tag, const std::string& value)
283     {
284
285         return checked_guard(m_server, &IServerWrapper::setPropertyValue, type, tag, value);
286     }
287
288     OCStackResult OCPlatform_impl::setPropertyValue(OCPayloadType type, const std::string& tag, const std::vector<std::string>& value)
289     {
290         for (const auto& h : value)
291         {
292            OCStackResult r;
293
294            if (OC_STACK_OK != (r = result_guard(setPropertyValue(type, tag, h))))
295            {
296                return r;
297            }
298         }
299
300         return OC_STACK_OK;
301     }
302
303     OCStackResult OCPlatform_impl::getPropertyValue(OCPayloadType type, const std::string& tag, std::string& value)
304     {
305         return checked_guard(m_server, &IServerWrapper::getPropertyValue, type, tag, value);
306     }
307
308     OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
309                                             const std::shared_ptr< OCResource > resource)
310     {
311         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
312         std::vector<std::string> resourceTypes = resource->getResourceTypes();
313
314         return checked_guard(m_server, &IServerWrapper::registerResource,
315                 std::ref(resourceHandle), resource->host() + resource->uri(),
316                 resourceTypes[0]/*"core.remote"*/, DEFAULT_INTERFACE,
317                 (EntityHandler) nullptr, resourceProperty);
318     }
319
320     OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
321     {
322         return checked_guard(m_server, &IServerWrapper::unregisterResource,
323                              resourceHandle);
324     }
325
326     OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
327                                             OCResourceHandle resourceHandle)
328     {
329         return result_guard(OCUnBindResource(std::ref(collectionHandle), std::ref(resourceHandle)));
330     }
331
332     OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
333                                             const std::vector<OCResourceHandle>& resourceHandles)
334     {
335         for(const auto& h : resourceHandles)
336         {
337            OCStackResult r;
338
339            if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
340            {
341                return r;
342            }
343         }
344
345         return OC_STACK_OK;
346     }
347
348     OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
349                                             const OCResourceHandle resourceHandle)
350     {
351         return result_guard(OCBindResource(collectionHandle, resourceHandle));
352     }
353
354     OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
355                                             const std::vector<OCResourceHandle>& resourceHandles)
356     {
357         for(const auto& h : resourceHandles)
358         {
359            OCStackResult r;
360
361            if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
362            {
363                return r;
364            }
365         }
366
367         return OC_STACK_OK;
368     }
369
370     OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
371                                              const std::string& resourceTypeName) const
372     {
373         return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
374                              resourceHandle, resourceTypeName);
375     }
376
377     OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
378                                              const std::string& resourceInterfaceName) const
379     {
380         return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
381                              resourceHandle, resourceInterfaceName);
382     }
383
384     OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
385     {
386         return checked_guard(m_server, &IServerWrapper::startPresence,
387                              announceDurationSeconds);
388     }
389
390     OCStackResult OCPlatform_impl::stopPresence()
391     {
392         return checked_guard(m_server, &IServerWrapper::stopPresence);
393     }
394
395     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
396                                             const std::string& host,
397                                             OCConnectivityType connectivityType,
398                                             SubscribeCallback presenceHandler)
399     {
400         return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
401     }
402
403
404     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
405                                             const std::string& host,
406                                             const std::string& resourceType,
407                                             OCConnectivityType connectivityType,
408                                             SubscribeCallback presenceHandler)
409     {
410         return checked_guard(m_client, &IClientWrapper::SubscribePresence,
411                              &presenceHandle, host, resourceType, connectivityType,
412                              presenceHandler);
413     }
414
415     OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
416     {
417         return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
418                              std::ref(presenceHandle));
419     }
420
421 #ifdef WITH_CLOUD
422     OCStackResult OCPlatform_impl::subscribeDevicePresence(OCPresenceHandle& presenceHandle,
423                                                            const std::string& host,
424                                                            const std::vector<std::string>& di,
425                                                            OCConnectivityType connectivityType,
426                                                            ObserveCallback callback)
427     {
428         return checked_guard(m_client, &IClientWrapper::SubscribeDevicePresence,
429                              &presenceHandle, host, di, connectivityType, callback);
430     }
431 #endif
432
433     OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
434     {
435         return checked_guard(m_server, &IServerWrapper::sendResponse,
436                              pResponse);
437     }
438     std::weak_ptr<std::recursive_mutex> OCPlatform_impl::csdkLock()
439     {
440         return m_csdkLock;
441     }
442
443     OCStackResult OCPlatform_impl::findDirectPairingDevices(unsigned short waittime,
444                              GetDirectPairedCallback directPairingHandler)
445     {
446         return checked_guard(m_client, &IClientWrapper::FindDirectPairingDevices,
447                              waittime, directPairingHandler);
448
449     }
450
451     OCStackResult OCPlatform_impl::getDirectPairedDevices(
452                              GetDirectPairedCallback directPairingHandler)
453     {
454
455         return checked_guard(m_client, &IClientWrapper::GetDirectPairedDevices,
456                              directPairingHandler);
457     }
458
459     OCStackResult OCPlatform_impl::doDirectPairing(std::shared_ptr<OCDirectPairing> peer,
460                              OCPrm_t pmSel,
461                              const std::string& pinNumber,
462                              DirectPairingCallback resultCallback)
463     {
464         return checked_guard(m_client, &IClientWrapper::DoDirectPairing,
465                              peer, pmSel, pinNumber, resultCallback);
466     }
467 #ifdef WITH_CLOUD
468     OCAccountManager::Ptr OCPlatform_impl::constructAccountManagerObject(const std::string& host,
469                                                             OCConnectivityType connectivityType)
470     {
471         if (!m_client)
472         {
473             return std::shared_ptr<OCAccountManager>();
474         }
475
476         return std::shared_ptr<OCAccountManager>(new OCAccountManager(m_client,
477                                                                       host,
478                                                                       connectivityType));
479     }
480 #endif // WITH_CLOUD
481
482     OCStackResult OCPlatform_impl::getDeviceId(OCUUIdentity *myUuid)
483     {
484         return OCGetDeviceId(myUuid);
485     }
486
487     OCStackResult OCPlatform_impl::setDeviceId(const OCUUIdentity *myUuid)
488     {
489         return OCSetDeviceId(myUuid);
490     }
491 } //namespace OC