replace : iotivity -> iotivity-sec
[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 "logger.h"
48 #include "oc_logger.hpp"
49
50 #define TAG "OIC_PLATFORM"
51
52 namespace OC
53 {
54
55     PlatformConfig& OCPlatform_impl::globalConfig()
56     {
57         static PlatformConfig s_config;
58         return s_config;
59     }
60
61     OCStackResult OCPlatform_impl::Configure(const PlatformConfig& config)
62     {
63         OIC_LOG_V(INFO, TAG, "Configure : %d %p", config.transportType, config.ps);
64         OCStackResult res = OC_STACK_OK;
65
66         res = OCSetSecurePSI(config.key, config.ps, config.psEnc, config.psRescue);
67         if (OC_STACK_OK == res && config.psEnc)
68         {
69             OIC_LOG(INFO, TAG, "It uses psEnc for svr db.");
70             res = OCRegisterPersistentStorageHandler(config.psEnc);
71         } else
72         {
73             OIC_LOG(INFO, TAG, "It uses ps for svr db.");
74             res = OCRegisterPersistentStorageHandler(config.ps);
75         }
76
77         globalConfig() = config;
78         return res;
79     }
80
81     OCPlatform_impl& OCPlatform_impl::Instance()
82     {
83         static OCPlatform_impl platform(globalConfig());
84         return platform;
85     }
86
87     OCStackResult OCPlatform_impl::start()
88     {
89         OIC_LOG(INFO, TAG, "start");
90
91         OCStackResult res = OC_STACK_OK;
92         if (OC_CLIENT == m_modeType)
93         {
94             if (OC_STACK_OK != checked_guard(m_client, &IClientWrapper::start))
95             {
96                 res = OC_STACK_ERROR;
97             }
98         }
99         else if (OC_SERVER == m_modeType)
100         {
101             if (OC_STACK_OK != checked_guard(m_server, &IServerWrapper::start))
102             {
103                 res = OC_STACK_ERROR;
104             }
105         }
106         else if (OC_CLIENT_SERVER == m_modeType || OC_GATEWAY == m_modeType)
107         {
108             if (OC_STACK_OK != checked_guard(m_client, &IClientWrapper::start))
109             {
110                 res = OC_STACK_ERROR;
111             }
112
113             if (OC_STACK_OK != checked_guard(m_server, &IServerWrapper::start))
114             {
115                 res = OC_STACK_ERROR;
116             }
117         }
118         else
119         {
120             res = OC_STACK_ERROR;
121         }
122
123         return res;
124     }
125
126     OCStackResult OCPlatform_impl::stop()
127     {
128         OIC_LOG(INFO, TAG, "stop");
129
130         OCStackResult res = OC_STACK_OK;
131         if (OC_CLIENT == m_modeType)
132         {
133             if (OC_STACK_OK != checked_guard(m_client, &IClientWrapper::stop))
134             {
135                 res = OC_STACK_ERROR;
136             }
137         }
138         else if (OC_SERVER == m_modeType)
139         {
140             if (OC_STACK_OK != checked_guard(m_server, &IServerWrapper::stop))
141             {
142                 res = OC_STACK_ERROR;
143             }
144         }
145         else if (OC_CLIENT_SERVER == m_modeType)
146         {
147             if (OC_STACK_OK != checked_guard(m_client, &IClientWrapper::stop))
148             {
149                 res = OC_STACK_ERROR;
150             }
151
152             if (OC_STACK_OK != checked_guard(m_server, &IServerWrapper::stop))
153             {
154                 res = OC_STACK_ERROR;
155             }
156         }
157         else
158         {
159             res = OC_STACK_ERROR;
160         }
161
162         return res;
163     }
164
165     void OCPlatform_impl::init(const PlatformConfig& config)
166     {
167         OIC_LOG(INFO, TAG, "init");
168
169         switch(config.mode)
170         {
171             case ModeType::Server:
172                 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
173                 m_modeType = OC_SERVER;
174                 break;
175
176             case ModeType::Client:
177                 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
178                 m_modeType = OC_CLIENT;
179                 break;
180
181             case ModeType::Both:
182             case ModeType::Gateway:
183                 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
184                 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
185                 m_modeType = OC_CLIENT_SERVER;
186                 break;
187          }
188     }
189
190     OCPlatform_impl::OCPlatform_impl(const PlatformConfig& config)
191      : m_cfg             { config },
192        m_WrapperInstance { make_unique<WrapperFactory>() },
193        m_csdkLock        { std::make_shared<std::recursive_mutex>() }
194     {
195         init(m_cfg);
196     }
197
198     OCPlatform_impl::~OCPlatform_impl(void)
199     {
200     }
201
202     OCStackResult OCPlatform_impl::setDefaultDeviceEntityHandler(EntityHandler entityHandler)
203     {
204         return checked_guard(m_server, &IServerWrapper::setDefaultDeviceEntityHandler,
205                              entityHandler);
206     }
207
208     OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle,
209                                                 QualityOfService QoS)
210     {
211         return checked_guard(m_server, &IServerWrapper::notifyAllObservers, resourceHandle, QoS);
212     }
213
214     OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle)
215     {
216         return notifyAllObservers(resourceHandle, m_cfg.QoS);
217     }
218
219     OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
220                                        ObservationIds& observationIds,
221                                        const std::shared_ptr<OCResourceResponse> pResponse)
222     {
223         return notifyListOfObservers(resourceHandle, observationIds, pResponse, m_cfg.QoS);
224     }
225
226     OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
227                                        ObservationIds& observationIds,
228                                        const std::shared_ptr<OCResourceResponse> pResponse,
229                                        QualityOfService QoS)
230     {
231         return checked_guard(m_server, &IServerWrapper::notifyListOfObservers, resourceHandle,
232                              observationIds, pResponse, QoS);
233     }
234
235     OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
236                                                 const std::string& uri,
237                                                 OCConnectivityType connectivityType,
238                                                 bool isObservable,
239                                                 const std::vector<std::string>& resourceTypes,
240                                                 const std::vector<std::string>& interfaces)
241     {
242         if(!m_client)
243         {
244             return std::shared_ptr<OCResource>();
245         }
246
247         uint8_t resourceProperty = 0;
248         if (isObservable)
249         {
250             resourceProperty = (resourceProperty | OC_OBSERVABLE);
251         }
252         return std::shared_ptr<OCResource>(new OCResource(m_client,
253                                             host,
254                                             uri, "", connectivityType,
255                                             resourceProperty,
256                                             resourceTypes,
257                                             interfaces, ""));
258     }
259
260     OCStackResult OCPlatform_impl::findResource(const std::string& host,
261                                             const std::string& resourceName,
262                                             OCConnectivityType connectivityType,
263                                             FindCallback resourceHandler)
264     {
265         return findResource(host, resourceName, connectivityType, resourceHandler, m_cfg.QoS);
266     }
267
268     OCStackResult OCPlatform_impl::findResource(const std::string& host,
269                                             const std::string& resourceName,
270                                             OCConnectivityType connectivityType,
271                                             FindCallback resourceHandler,
272                                             QualityOfService QoS)
273     {
274         return checked_guard(m_client, &IClientWrapper::ListenForResource,
275                              host, resourceName, connectivityType, resourceHandler, QoS);
276     }
277
278     OCStackResult OCPlatform_impl::findResource(const std::string& host,
279                                             const std::string& resourceName,
280                                             OCConnectivityType connectivityType,
281                                             FindCallback resourceHandler,
282                                             FindErrorCallback errorHandler)
283     {
284         return findResource(host, resourceName, connectivityType, resourceHandler,
285                             errorHandler, m_cfg.QoS);
286     }
287
288     OCStackResult OCPlatform_impl::findResource(const std::string& host,
289                                             const std::string& resourceName,
290                                             OCConnectivityType connectivityType,
291                                             FindCallback resourceHandler,
292                                             FindErrorCallback errorHandler,
293                                             QualityOfService QoS)
294     {
295         return checked_guard(m_client, &IClientWrapper::ListenErrorForResource,
296                              host, resourceName, connectivityType, resourceHandler,
297                              errorHandler, QoS);
298     }
299
300     OCStackResult OCPlatform_impl::findResourceList(const std::string& host,
301                                             const std::string& resourceName,
302                                             OCConnectivityType connectivityType,
303                                             FindResListCallback resourceHandler,
304                                             QualityOfService QoS)
305     {
306         return checked_guard(m_client, &IClientWrapper::ListenForResourceList,
307                              host, resourceName, connectivityType, resourceHandler, QoS);
308     }
309
310     OCStackResult OCPlatform_impl::findResourceList(const std::string& host,
311                                             const std::string& resourceName,
312                                             OCConnectivityType connectivityType,
313                                             FindResListCallback resourceHandler,
314                                             FindErrorCallback errorHandler,
315                                             QualityOfService Qos)
316     {
317         return checked_guard(m_client, &IClientWrapper::ListenForResourceListWithError,
318                              host, resourceName, connectivityType, resourceHandler,
319                              errorHandler, Qos);
320     }
321
322     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
323                                             const std::string& deviceURI,
324                                             OCConnectivityType connectivityType,
325                                             FindDeviceCallback deviceInfoHandler)
326     {
327         return result_guard(getDeviceInfo(host, deviceURI, connectivityType,
328                deviceInfoHandler, m_cfg.QoS));
329     }
330
331     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
332                                             const std::string& deviceURI,
333                                             OCConnectivityType connectivityType,
334                                             FindDeviceCallback deviceInfoHandler,
335                                             QualityOfService QoS)
336     {
337         return checked_guard(m_client, &IClientWrapper::ListenForDevice,
338                              host, deviceURI, connectivityType, deviceInfoHandler, QoS);
339     }
340
341     OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
342                                             const std::string& platformURI,
343                                             OCConnectivityType connectivityType,
344                                             FindPlatformCallback platformInfoHandler)
345     {
346         return result_guard(getPlatformInfo(host, platformURI, connectivityType,
347                platformInfoHandler, m_cfg.QoS));
348     }
349
350     OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
351                                             const std::string& platformURI,
352                                             OCConnectivityType connectivityType,
353                                             FindPlatformCallback platformInfoHandler,
354                                             QualityOfService QoS)
355     {
356         return checked_guard(m_client, &IClientWrapper::ListenForDevice,
357                              host, platformURI, connectivityType, platformInfoHandler, QoS);
358     }
359
360     OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
361                                             std::string& resourceURI,
362                                             const std::string& resourceTypeName,
363                                             const std::string& resourceInterface,
364                                             EntityHandler entityHandler,
365                                             uint8_t resourceProperty)
366     {
367         return checked_guard(m_server, &IServerWrapper::registerResource,
368                              std::ref(resourceHandle), resourceURI, resourceTypeName,
369                              resourceInterface, entityHandler, resourceProperty);
370     }
371
372     OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
373     {
374         return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
375     }
376
377     OCStackResult OCPlatform_impl::registerPlatformInfo(const OCPlatformInfo platformInfo)
378     {
379         return checked_guard(m_server, &IServerWrapper::registerPlatformInfo, platformInfo);
380     }
381
382     OCStackResult OCPlatform_impl::setPropertyValue(OCPayloadType type, const std::string& tag, const std::string& value)
383     {
384
385         return checked_guard(m_server, &IServerWrapper::setPropertyValue, type, tag, value);
386     }
387
388     OCStackResult OCPlatform_impl::setPropertyValue(OCPayloadType type, const std::string& tag, const std::vector<std::string>& value)
389     {
390         for (const auto& h : value)
391         {
392            OCStackResult r;
393
394            if (OC_STACK_OK != (r = result_guard(setPropertyValue(type, tag, h))))
395            {
396                return r;
397            }
398         }
399
400         return OC_STACK_OK;
401     }
402
403     OCStackResult OCPlatform_impl::getPropertyValue(OCPayloadType type, const std::string& tag, std::string& value)
404     {
405         return checked_guard(m_server, &IServerWrapper::getPropertyValue, type, tag, value);
406     }
407
408     OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
409                                             const std::shared_ptr< OCResource > resource)
410     {
411         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
412         std::vector<std::string> resourceTypes = resource->getResourceTypes();
413
414         return checked_guard(m_server, &IServerWrapper::registerResource,
415                 std::ref(resourceHandle), resource->host() + resource->uri(),
416                 resourceTypes[0]/*"core.remote"*/, DEFAULT_INTERFACE,
417                 (EntityHandler) nullptr, resourceProperty);
418     }
419
420     OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
421     {
422         return checked_guard(m_server, &IServerWrapper::unregisterResource,
423                              resourceHandle);
424     }
425
426     OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
427                                             OCResourceHandle resourceHandle)
428     {
429         return result_guard(OCUnBindResource(std::ref(collectionHandle), std::ref(resourceHandle)));
430     }
431
432     OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
433                                             const std::vector<OCResourceHandle>& resourceHandles)
434     {
435         for(const auto& h : resourceHandles)
436         {
437            OCStackResult r;
438
439            if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
440            {
441                return r;
442            }
443         }
444
445         return OC_STACK_OK;
446     }
447
448     OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
449                                             const OCResourceHandle resourceHandle)
450     {
451         return result_guard(OCBindResource(collectionHandle, resourceHandle));
452     }
453
454     OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
455                                             const std::vector<OCResourceHandle>& resourceHandles)
456     {
457         for(const auto& h : resourceHandles)
458         {
459            OCStackResult r;
460
461            if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
462            {
463                return r;
464            }
465         }
466
467         return OC_STACK_OK;
468     }
469
470     OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
471                                              const std::string& resourceTypeName) const
472     {
473         return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
474                              resourceHandle, resourceTypeName);
475     }
476
477     OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
478                                              const std::string& resourceInterfaceName) const
479     {
480         return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
481                              resourceHandle, resourceInterfaceName);
482     }
483
484     OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
485     {
486         return checked_guard(m_server, &IServerWrapper::startPresence,
487                              announceDurationSeconds);
488     }
489
490     OCStackResult OCPlatform_impl::stopPresence()
491     {
492         return checked_guard(m_server, &IServerWrapper::stopPresence);
493     }
494
495     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
496                                             const std::string& host,
497                                             OCConnectivityType connectivityType,
498                                             SubscribeCallback presenceHandler)
499     {
500         return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
501     }
502
503
504     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
505                                             const std::string& host,
506                                             const std::string& resourceType,
507                                             OCConnectivityType connectivityType,
508                                             SubscribeCallback presenceHandler)
509     {
510         return checked_guard(m_client, &IClientWrapper::SubscribePresence,
511                              &presenceHandle, host, resourceType, connectivityType,
512                              presenceHandler);
513     }
514
515     OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
516     {
517         return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
518                              std::ref(presenceHandle));
519     }
520
521 #ifdef WITH_CLOUD
522     OCStackResult OCPlatform_impl::subscribeDevicePresence(OCPresenceHandle& presenceHandle,
523                                                            const std::string& host,
524                                                            const std::vector<std::string>& di,
525                                                            OCConnectivityType connectivityType,
526                                                            ObserveCallback callback)
527     {
528         return checked_guard(m_client, &IClientWrapper::SubscribeDevicePresence,
529                              &presenceHandle, host, di, connectivityType, callback);
530     }
531 #endif
532
533     OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
534     {
535         return checked_guard(m_server, &IServerWrapper::sendResponse,
536                              pResponse);
537     }
538     std::weak_ptr<std::recursive_mutex> OCPlatform_impl::csdkLock()
539     {
540         return m_csdkLock;
541     }
542
543     OCStackResult OCPlatform_impl::findDirectPairingDevices(unsigned short waittime,
544                              GetDirectPairedCallback directPairingHandler)
545     {
546         return checked_guard(m_client, &IClientWrapper::FindDirectPairingDevices,
547                              waittime, directPairingHandler);
548
549     }
550
551     OCStackResult OCPlatform_impl::getDirectPairedDevices(
552                              GetDirectPairedCallback directPairingHandler)
553     {
554
555         return checked_guard(m_client, &IClientWrapper::GetDirectPairedDevices,
556                              directPairingHandler);
557     }
558
559     OCStackResult OCPlatform_impl::doDirectPairing(std::shared_ptr<OCDirectPairing> peer,
560                              OCPrm_t pmSel,
561                              const std::string& pinNumber,
562                              DirectPairingCallback resultCallback)
563     {
564         return checked_guard(m_client, &IClientWrapper::DoDirectPairing,
565                              peer, pmSel, pinNumber, resultCallback);
566     }
567 #ifdef WITH_CLOUD
568     OCAccountManager::Ptr OCPlatform_impl::constructAccountManagerObject(const std::string& host,
569                                                             OCConnectivityType connectivityType)
570     {
571         if (!m_client)
572         {
573             return std::shared_ptr<OCAccountManager>();
574         }
575
576         return std::shared_ptr<OCAccountManager>(new OCAccountManager(m_client,
577                                                                       host,
578                                                                       connectivityType));
579     }
580 #endif // WITH_CLOUD
581 #ifdef TCP_ADAPTER
582     OCStackResult OCPlatform_impl::findKeepAliveResource(std::string host, KeepAliveCallback resultCallback)
583     {
584         return checked_guard(m_client, &IClientWrapper::findKeepAliveResource, host, resultCallback);
585     }
586
587     OCStackResult OCPlatform_impl::sendKeepAliveRequest(std::string host, const OCRepresentation& rep,
588                                                         KeepAliveCallback resultCallback)
589     {
590         return checked_guard(m_client, &IClientWrapper::sendKeepAliveRequest, host,
591                              rep, resultCallback);
592     }
593 #endif
594     OCStackResult OCPlatform_impl::getDeviceId(OCUUIdentity *myUuid)
595     {
596         return OCGetDeviceId(myUuid);
597     }
598
599     OCStackResult OCPlatform_impl::setDeviceId(const OCUUIdentity *myUuid)
600     {
601         return OCSetDeviceId(myUuid);
602     }
603 } //namespace OC