Merge "Remove the dead code" 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     OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
142                                                 const std::string& uri,
143                                                 bool isObservable,
144                                                 const std::vector<std::string>& resourceTypes,
145                                                 const std::vector<std::string>& interfaces)
146     {
147         if(!m_client)
148         {
149             return std::shared_ptr<OCResource>();
150         }
151
152         return std::shared_ptr<OCResource>(new OCResource(m_client,
153                                             host,
154                                             uri,
155                                             isObservable,
156                                             resourceTypes,
157                                             interfaces));
158     }
159
160     OCStackResult OCPlatform_impl::findResource(const std::string& host,
161                                             const std::string& resourceName,
162                                             FindCallback resourceHandler)
163     {
164         return findResource(host, resourceName, resourceHandler, m_cfg.QoS);
165     }
166
167     OCStackResult OCPlatform_impl::findResource(const std::string& host,
168                                             const std::string& resourceName,
169                                             FindCallback resourceHandler, QualityOfService QoS)
170     {
171         return checked_guard(m_client, &IClientWrapper::ListenForResource,
172                              host, resourceName, resourceHandler, QoS);
173     }
174
175     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
176                                             const std::string& deviceURI,
177                                             FindDeviceCallback deviceInfoHandler)
178     {
179         return result_guard(getDeviceInfo(host, deviceURI, deviceInfoHandler, m_cfg.QoS));
180     }
181
182     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
183                                             const std::string& deviceURI,
184                                             FindDeviceCallback deviceInfoHandler,
185                                             QualityOfService QoS)
186     {
187         return checked_guard(m_client, &IClientWrapper::ListenForDevice,
188                              host, deviceURI, deviceInfoHandler, QoS);
189     }
190
191     OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
192                                             std::string& resourceURI,
193                                             const std::string& resourceTypeName,
194                                             const std::string& resourceInterface,
195                                             EntityHandler entityHandler,
196                                             uint8_t resourceProperty)
197     {
198         return checked_guard(m_server, &IServerWrapper::registerResource,
199                              ref(resourceHandle), resourceURI, resourceTypeName,
200                              resourceInterface, entityHandler, resourceProperty);
201     }
202
203     OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
204     {
205         return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
206     }
207
208     OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
209                                             const std::shared_ptr< OCResource > resource)
210     {
211         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
212         std::vector<std::string> resourceTypes = resource->getResourceTypes();
213
214         return checked_guard(m_server, &IServerWrapper::registerResourceWithHost,
215                 ref(resourceHandle), resource->host(), resource->uri(), resourceTypes[0]/*"core.remote"*/, "oc.mi.def",
216                 (EntityHandler) nullptr, resourceProperty);
217     }
218
219     OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
220     {
221         return checked_guard(m_server, &IServerWrapper::unregisterResource,
222                              resourceHandle);
223     }
224
225     OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
226                                             OCResourceHandle resourceHandle)
227     {
228         return result_guard(OCUnBindResource(ref(collectionHandle), ref(resourceHandle)));
229     }
230
231     OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
232                                             const std::vector<OCResourceHandle>& resourceHandles)
233     {
234         for(const auto& h : resourceHandles)
235         {
236            OCStackResult r;
237
238            if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
239            {
240                return r;
241            }
242         }
243
244         return OC_STACK_OK;
245     }
246
247     OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
248                                             const OCResourceHandle resourceHandle)
249     {
250         return result_guard(OCBindResource(collectionHandle, resourceHandle));
251     }
252
253     OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
254                                             const std::vector<OCResourceHandle>& resourceHandles)
255     {
256         for(const auto& h : resourceHandles)
257         {
258            OCStackResult r;
259
260            if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
261            {
262                return r;
263            }
264         }
265
266         return OC_STACK_OK;
267     }
268
269     OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
270                                              const std::string& resourceTypeName) const
271     {
272         return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
273                              resourceHandle, resourceTypeName);
274     }
275
276     OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
277                                              const std::string& resourceInterfaceName) const
278     {
279         return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
280                              resourceHandle, resourceInterfaceName);
281     }
282
283     OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
284     {
285         return checked_guard(m_server, &IServerWrapper::startPresence,
286                              announceDurationSeconds);
287     }
288
289     OCStackResult OCPlatform_impl::stopPresence()
290     {
291         return checked_guard(m_server, &IServerWrapper::stopPresence);
292     }
293
294     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
295                                             const std::string& host,
296                                             SubscribeCallback presenceHandler)
297     {
298         return subscribePresence(presenceHandle, host, "", presenceHandler);
299     }
300
301     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
302                                             const std::string& host,
303                                             const std::string& resourceType,
304                                             SubscribeCallback presenceHandler)
305     {
306         return checked_guard(m_client, &IClientWrapper::SubscribePresence,
307                              &presenceHandle, host, resourceType, presenceHandler);
308     }
309
310     OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
311     {
312         return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
313                              ref(presenceHandle));
314     }
315
316     OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
317     {
318         return checked_guard(m_server, &IServerWrapper::sendResponse,
319                              pResponse);
320     }
321 } //namespace OC