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