CBOR rebase onto master for merge/review
[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         globalConfig() = config;
61     }
62
63     OCPlatform_impl& OCPlatform_impl::Instance()
64     {
65         static OCPlatform_impl platform(globalConfig());
66         return platform;
67     }
68
69     void OCPlatform_impl::init(const PlatformConfig& config)
70     {
71         switch(config.mode)
72         {
73             case ModeType::Server:
74                 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
75                 break;
76
77             case ModeType::Client:
78                 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
79                 break;
80
81             case ModeType::Both:
82                 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
83                 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
84                 break;
85          }
86     }
87
88     OCPlatform_impl::OCPlatform_impl(const PlatformConfig& config)
89      : m_cfg             { config },
90        m_WrapperInstance { make_unique<WrapperFactory>() },
91        m_csdkLock        { std::make_shared<std::recursive_mutex>() }
92     {
93         init(m_cfg);
94     }
95
96     OCPlatform_impl::~OCPlatform_impl(void)
97     {
98     }
99
100     OCStackResult OCPlatform_impl::setDefaultDeviceEntityHandler(EntityHandler entityHandler)
101     {
102         return checked_guard(m_server, &IServerWrapper::setDefaultDeviceEntityHandler,
103                              entityHandler);
104     }
105
106     OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle,
107                                                 QualityOfService QoS)
108     {
109         return result_guard(OCNotifyAllObservers(resourceHandle,
110                     static_cast<OCQualityOfService>(QoS)));
111     }
112
113     OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle)
114     {
115         return notifyAllObservers(resourceHandle, m_cfg.QoS);
116     }
117
118     OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
119                                        ObservationIds& observationIds,
120                                        const std::shared_ptr<OCResourceResponse> pResponse)
121     {
122         return notifyListOfObservers(resourceHandle, observationIds, pResponse, m_cfg.QoS);
123     }
124
125     OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
126                                        ObservationIds& observationIds,
127                                        const std::shared_ptr<OCResourceResponse> pResponse,
128                                        QualityOfService QoS)
129     {
130         if(!pResponse)
131         {
132          return result_guard(OC_STACK_ERROR);
133         }
134
135         OCRepPayload* pl = pResponse->getResourceRepresentation().getPayload();
136         OCStackResult result =
137                    OCNotifyListOfObservers(resourceHandle,
138                             &observationIds[0], observationIds.size(),
139                             pl,
140                             static_cast<OCQualityOfService>(QoS));
141         OCRepPayloadDestroy(pl);
142         return result_guard(result);
143     }
144
145     OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
146                                                 const std::string& uri,
147                                                 OCConnectivityType connectivityType,
148                                                 bool isObservable,
149                                                 const std::vector<std::string>& resourceTypes,
150                                                 const std::vector<std::string>& interfaces)
151     {
152         if(!m_client)
153         {
154             return std::shared_ptr<OCResource>();
155         }
156
157         return std::shared_ptr<OCResource>(new OCResource(m_client,
158                                             host,
159                                             uri, "", connectivityType,
160                                             isObservable,
161                                             resourceTypes,
162                                             interfaces));
163     }
164
165     OCStackResult OCPlatform_impl::findResource(const std::string& host,
166                                             const std::string& resourceName,
167                                             OCConnectivityType connectivityType,
168                                             FindCallback resourceHandler)
169     {
170         return findResource(host, resourceName, connectivityType, resourceHandler, m_cfg.QoS);
171     }
172
173     OCStackResult OCPlatform_impl::findResource(const std::string& host,
174                                             const std::string& resourceName,
175                                             OCConnectivityType connectivityType,
176                                             FindCallback resourceHandler,
177                                             QualityOfService QoS)
178     {
179         return checked_guard(m_client, &IClientWrapper::ListenForResource,
180                              host, resourceName, connectivityType, resourceHandler, QoS);
181     }
182
183     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
184                                             const std::string& deviceURI,
185                                             OCConnectivityType connectivityType,
186                                             FindDeviceCallback deviceInfoHandler)
187     {
188         return result_guard(getDeviceInfo(host, deviceURI, connectivityType,
189                deviceInfoHandler, m_cfg.QoS));
190     }
191
192     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
193                                             const std::string& deviceURI,
194                                             OCConnectivityType connectivityType,
195                                             FindDeviceCallback deviceInfoHandler,
196                                             QualityOfService QoS)
197     {
198         return checked_guard(m_client, &IClientWrapper::ListenForDevice,
199                              host, deviceURI, connectivityType, deviceInfoHandler, QoS);
200     }
201
202     OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
203                                             const std::string& platformURI,
204                                             OCConnectivityType connectivityType,
205                                             FindPlatformCallback platformInfoHandler)
206     {
207         return result_guard(getPlatformInfo(host, platformURI, connectivityType,
208                platformInfoHandler, m_cfg.QoS));
209     }
210
211     OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
212                                             const std::string& platformURI,
213                                             OCConnectivityType connectivityType,
214                                             FindPlatformCallback platformInfoHandler,
215                                             QualityOfService QoS)
216     {
217         return checked_guard(m_client, &IClientWrapper::ListenForDevice,
218                              host, platformURI, connectivityType, platformInfoHandler, QoS);
219     }
220
221     OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
222                                             std::string& resourceURI,
223                                             const std::string& resourceTypeName,
224                                             const std::string& resourceInterface,
225                                             EntityHandler entityHandler,
226                                             uint8_t resourceProperty)
227     {
228         return checked_guard(m_server, &IServerWrapper::registerResource,
229                              std::ref(resourceHandle), resourceURI, resourceTypeName,
230                              resourceInterface, entityHandler, resourceProperty);
231     }
232
233     OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
234     {
235         return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
236     }
237
238     OCStackResult OCPlatform_impl::registerPlatformInfo(const OCPlatformInfo platformInfo)
239     {
240         return checked_guard(m_server, &IServerWrapper::registerPlatformInfo, platformInfo);
241     }
242
243     OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
244                                             const std::shared_ptr< OCResource > resource)
245     {
246         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
247         std::vector<std::string> resourceTypes = resource->getResourceTypes();
248
249         return checked_guard(m_server, &IServerWrapper::registerResource,
250                 std::ref(resourceHandle), resource->host() + resource->uri(),
251                 resourceTypes[0]/*"core.remote"*/, DEFAULT_INTERFACE,
252                 (EntityHandler) nullptr, resourceProperty);
253     }
254
255     OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
256     {
257         return checked_guard(m_server, &IServerWrapper::unregisterResource,
258                              resourceHandle);
259     }
260
261     OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
262                                             OCResourceHandle resourceHandle)
263     {
264         return result_guard(OCUnBindResource(std::ref(collectionHandle), std::ref(resourceHandle)));
265     }
266
267     OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
268                                             const std::vector<OCResourceHandle>& resourceHandles)
269     {
270         for(const auto& h : resourceHandles)
271         {
272            OCStackResult r;
273
274            if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
275            {
276                return r;
277            }
278         }
279
280         return OC_STACK_OK;
281     }
282
283     OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
284                                             const OCResourceHandle resourceHandle)
285     {
286         return result_guard(OCBindResource(collectionHandle, resourceHandle));
287     }
288
289     OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
290                                             const std::vector<OCResourceHandle>& resourceHandles)
291     {
292         for(const auto& h : resourceHandles)
293         {
294            OCStackResult r;
295
296            if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
297            {
298                return r;
299            }
300         }
301
302         return OC_STACK_OK;
303     }
304
305     OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
306                                              const std::string& resourceTypeName) const
307     {
308         return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
309                              resourceHandle, resourceTypeName);
310     }
311
312     OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
313                                              const std::string& resourceInterfaceName) const
314     {
315         return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
316                              resourceHandle, resourceInterfaceName);
317     }
318
319     OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
320     {
321         return checked_guard(m_server, &IServerWrapper::startPresence,
322                              announceDurationSeconds);
323     }
324
325     OCStackResult OCPlatform_impl::stopPresence()
326     {
327         return checked_guard(m_server, &IServerWrapper::stopPresence);
328     }
329
330     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
331                                             const std::string& host,
332                                             OCConnectivityType connectivityType,
333                                             SubscribeCallback presenceHandler)
334     {
335         return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
336     }
337
338
339     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
340                                             const std::string& host,
341                                             const std::string& resourceType,
342                                             OCConnectivityType connectivityType,
343                                             SubscribeCallback presenceHandler)
344     {
345         return checked_guard(m_client, &IClientWrapper::SubscribePresence,
346                              &presenceHandle, host, resourceType, connectivityType,
347                              presenceHandler);
348     }
349
350     OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
351     {
352         return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
353                              std::ref(presenceHandle));
354     }
355
356     OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
357     {
358         return checked_guard(m_server, &IServerWrapper::sendResponse,
359                              pResponse);
360     }
361 } //namespace OC
362