Properly defined URI & Resource Type values for Presence Advertisement.
[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::registerResource(OCResourceHandle& resourceHandle,
201                                             std::string& resourceURI,
202                                             const std::string& resourceTypeName,
203                                             const std::string& resourceInterface,
204                                             EntityHandler entityHandler,
205                                             uint8_t resourceProperty)
206     {
207         return checked_guard(m_server, &IServerWrapper::registerResource,
208                              std::ref(resourceHandle), resourceURI, resourceTypeName,
209                              resourceInterface, entityHandler, resourceProperty);
210     }
211
212     OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
213     {
214         return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
215     }
216
217     OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
218                                             const std::shared_ptr< OCResource > resource)
219     {
220         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
221         std::vector<std::string> resourceTypes = resource->getResourceTypes();
222
223         return checked_guard(m_server, &IServerWrapper::registerResourceWithHost,
224                 std::ref(resourceHandle), resource->host(), resource->uri(),
225                 resourceTypes[0]/*"core.remote"*/, DEFAULT_INTERFACE,
226                 (EntityHandler) nullptr, resourceProperty);
227     }
228
229     OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
230     {
231         return checked_guard(m_server, &IServerWrapper::unregisterResource,
232                              resourceHandle);
233     }
234
235     OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
236                                             OCResourceHandle resourceHandle)
237     {
238         return result_guard(OCUnBindResource(std::ref(collectionHandle), std::ref(resourceHandle)));
239     }
240
241     OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
242                                             const std::vector<OCResourceHandle>& resourceHandles)
243     {
244         for(const auto& h : resourceHandles)
245         {
246            OCStackResult r;
247
248            if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
249            {
250                return r;
251            }
252         }
253
254         return OC_STACK_OK;
255     }
256
257     OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
258                                             const OCResourceHandle resourceHandle)
259     {
260         return result_guard(OCBindResource(collectionHandle, resourceHandle));
261     }
262
263     OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
264                                             const std::vector<OCResourceHandle>& resourceHandles)
265     {
266         for(const auto& h : resourceHandles)
267         {
268            OCStackResult r;
269
270            if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
271            {
272                return r;
273            }
274         }
275
276         return OC_STACK_OK;
277     }
278
279     OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
280                                              const std::string& resourceTypeName) const
281     {
282         return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
283                              resourceHandle, resourceTypeName);
284     }
285
286     OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
287                                              const std::string& resourceInterfaceName) const
288     {
289         return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
290                              resourceHandle, resourceInterfaceName);
291     }
292
293     OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
294     {
295         return checked_guard(m_server, &IServerWrapper::startPresence,
296                              announceDurationSeconds);
297     }
298
299     OCStackResult OCPlatform_impl::stopPresence()
300     {
301         return checked_guard(m_server, &IServerWrapper::stopPresence);
302     }
303
304     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
305                                             const std::string& host,
306                                             OCConnectivityType connectivityType,
307                                             SubscribeCallback presenceHandler)
308     {
309         return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
310     }
311
312
313     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
314                                             const std::string& host,
315                                             const std::string& resourceType,
316                                             OCConnectivityType connectivityType,
317                                             SubscribeCallback presenceHandler)
318     {
319         return checked_guard(m_client, &IClientWrapper::SubscribePresence,
320                              &presenceHandle, host, resourceType, connectivityType,
321                              presenceHandler);
322     }
323
324     OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
325     {
326         return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
327                              std::ref(presenceHandle));
328     }
329
330     OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
331     {
332         return checked_guard(m_server, &IServerWrapper::sendResponse,
333                              pResponse);
334     }
335 } //namespace OC
336