Implement ServerID parsing and sample app
[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, uint8_t connectivityType,
144                                                 bool isObservable,
145                                                 const std::vector<std::string>& resourceTypes,
146                                                 const std::vector<std::string>& interfaces)
147     {
148         if(!m_client)
149         {
150             return std::shared_ptr<OCResource>();
151         }
152
153         return std::shared_ptr<OCResource>(new OCResource(m_client,
154                                             host,
155                                             uri, "", connectivityType,
156                                             isObservable,
157                                             resourceTypes,
158                                             interfaces));
159     }
160
161     OCStackResult OCPlatform_impl::findResource(const std::string& host,
162                                             const std::string& resourceName,
163                                             uint8_t connectivityType,
164                                             FindCallback resourceHandler)
165     {
166         return findResource(host, resourceName, connectivityType, resourceHandler, m_cfg.QoS);
167     }
168
169     OCStackResult OCPlatform_impl::findResource(const std::string& host,
170                                             const std::string& resourceName,
171                                             uint8_t connectivityType,
172                                             FindCallback resourceHandler, QualityOfService QoS)
173     {
174
175         return checked_guard(m_client, &IClientWrapper::ListenForResource,
176                              host, resourceName, connectivityType, resourceHandler, QoS);
177     }
178
179     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
180                                             const std::string& deviceURI,
181                                             uint8_t connectivityType,
182                                             FindDeviceCallback deviceInfoHandler)
183     {
184         return result_guard(getDeviceInfo(host, deviceURI, connectivityType,
185                deviceInfoHandler, m_cfg.QoS));
186     }
187
188     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
189                                             const std::string& deviceURI,
190                                             uint8_t connectivityType,
191                                             FindDeviceCallback deviceInfoHandler,
192                                             QualityOfService QoS)
193     {
194         return checked_guard(m_client, &IClientWrapper::ListenForDevice,
195                              host, deviceURI, connectivityType, deviceInfoHandler, QoS);
196     }
197
198 #else
199     OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
200                                                 const std::string& uri,
201                                                 bool isObservable,
202                                                 const std::vector<std::string>& resourceTypes,
203                                                 const std::vector<std::string>& interfaces)
204     {
205         if(!m_client)
206         {
207             return std::shared_ptr<OCResource>();
208         }
209
210         return std::shared_ptr<OCResource>(new OCResource(m_client,
211                                             host,
212                                             uri,
213                                             "", // 'created' Resources have no way of knowing their
214                                                 // server ID, so this has to be blank initially.
215                                             isObservable,
216                                             resourceTypes,
217                                             interfaces));
218     }
219
220     OCStackResult OCPlatform_impl::findResource(const std::string& host,
221                                             const std::string& resourceName,
222                                             FindCallback resourceHandler)
223     {
224         return findResource(host, resourceName, resourceHandler, m_cfg.QoS);
225     }
226
227     OCStackResult OCPlatform_impl::findResource(const std::string& host,
228                                             const std::string& resourceName,
229                                             FindCallback resourceHandler, QualityOfService QoS)
230     {
231
232         return checked_guard(m_client, &IClientWrapper::ListenForResource,
233                              host, resourceName, resourceHandler, QoS);
234     }
235
236     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
237                                             const std::string& deviceURI,
238                                             FindDeviceCallback deviceInfoHandler)
239     {
240         return result_guard(getDeviceInfo(host, deviceURI, deviceInfoHandler, m_cfg.QoS));
241     }
242
243     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
244                                             const std::string& deviceURI,
245                                             FindDeviceCallback deviceInfoHandler,
246                                             QualityOfService QoS)
247     {
248         return checked_guard(m_client, &IClientWrapper::ListenForDevice,
249                              host, deviceURI, deviceInfoHandler, QoS);
250     }
251
252 #endif
253
254     OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
255                                             std::string& resourceURI,
256                                             const std::string& resourceTypeName,
257                                             const std::string& resourceInterface,
258                                             EntityHandler entityHandler,
259                                             uint8_t resourceProperty)
260     {
261         return checked_guard(m_server, &IServerWrapper::registerResource,
262                              ref(resourceHandle), resourceURI, resourceTypeName,
263                              resourceInterface, entityHandler, resourceProperty);
264     }
265
266     OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
267     {
268         return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
269     }
270
271     OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
272                                             const std::shared_ptr< OCResource > resource)
273     {
274         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
275         std::vector<std::string> resourceTypes = resource->getResourceTypes();
276
277         return checked_guard(m_server, &IServerWrapper::registerResourceWithHost,
278                 ref(resourceHandle), resource->host(), resource->uri(), resourceTypes[0]/*"core.remote"*/, "oc.mi.def",
279                 (EntityHandler) nullptr, resourceProperty);
280     }
281
282     OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
283     {
284         return checked_guard(m_server, &IServerWrapper::unregisterResource,
285                              resourceHandle);
286     }
287
288     OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
289                                             OCResourceHandle resourceHandle)
290     {
291         return result_guard(OCUnBindResource(ref(collectionHandle), ref(resourceHandle)));
292     }
293
294     OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
295                                             const std::vector<OCResourceHandle>& resourceHandles)
296     {
297         for(const auto& h : resourceHandles)
298         {
299            OCStackResult r;
300
301            if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
302            {
303                return r;
304            }
305         }
306
307         return OC_STACK_OK;
308     }
309
310     OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
311                                             const OCResourceHandle resourceHandle)
312     {
313         return result_guard(OCBindResource(collectionHandle, resourceHandle));
314     }
315
316     OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
317                                             const std::vector<OCResourceHandle>& resourceHandles)
318     {
319         for(const auto& h : resourceHandles)
320         {
321            OCStackResult r;
322
323            if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
324            {
325                return r;
326            }
327         }
328
329         return OC_STACK_OK;
330     }
331
332     OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
333                                              const std::string& resourceTypeName) const
334     {
335         return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
336                              resourceHandle, resourceTypeName);
337     }
338
339     OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
340                                              const std::string& resourceInterfaceName) const
341     {
342         return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
343                              resourceHandle, resourceInterfaceName);
344     }
345
346     OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
347     {
348         return checked_guard(m_server, &IServerWrapper::startPresence,
349                              announceDurationSeconds);
350     }
351
352     OCStackResult OCPlatform_impl::stopPresence()
353     {
354         return checked_guard(m_server, &IServerWrapper::stopPresence);
355     }
356
357 #ifdef CA_INT
358     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
359                                             const std::string& host, uint8_t connectivityType,
360                                             SubscribeCallback presenceHandler)
361     {
362         return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
363     }
364
365
366     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
367                                             const std::string& host,
368                                             const std::string& resourceType,
369                                             uint8_t connectivityType,
370                                             SubscribeCallback presenceHandler)
371     {
372         return checked_guard(m_client, &IClientWrapper::SubscribePresence,
373                              &presenceHandle, host, resourceType, connectivityType,
374                              presenceHandler);
375     }
376 #else
377     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
378                                             const std::string& host,
379                                             SubscribeCallback presenceHandler)
380     {
381         return subscribePresence(presenceHandle, host, "", presenceHandler);
382     }
383
384     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
385                                             const std::string& host,
386                                             const std::string& resourceType,
387                                             SubscribeCallback presenceHandler)
388     {
389         return checked_guard(m_client, &IClientWrapper::SubscribePresence,
390                              &presenceHandle, host, resourceType,
391                              presenceHandler);
392     }
393 #endif
394
395     OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
396     {
397         return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
398                              ref(presenceHandle));
399     }
400
401     OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
402     {
403         return checked_guard(m_server, &IServerWrapper::sendResponse,
404                              pResponse);
405     }
406 } //namespace OC