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