Implemented C++/Android API for OCRegisterPersistentStorageHandler()
[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         OCRegisterPersistentStorageHandler(config.ps);
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         std::string payload(pResponse->getResourceRepresentation().getJSONRepresentation());
136
137         return result_guard(
138                    OCNotifyListOfObservers(resourceHandle,
139                             &observationIds[0], observationIds.size(),
140                             payload.c_str(),
141                             static_cast<OCQualityOfService>(QoS)));
142     }
143
144     OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
145                                                 const std::string& uri,
146                                                 OCConnectivityType connectivityType,
147                                                 bool isObservable,
148                                                 const std::vector<std::string>& resourceTypes,
149                                                 const std::vector<std::string>& interfaces)
150     {
151         if(!m_client)
152         {
153             return std::shared_ptr<OCResource>();
154         }
155
156         return std::shared_ptr<OCResource>(new OCResource(m_client,
157                                             host,
158                                             uri, "", connectivityType,
159                                             isObservable,
160                                             resourceTypes,
161                                             interfaces));
162     }
163
164     OCStackResult OCPlatform_impl::findResource(const std::string& host,
165                                             const std::string& resourceName,
166                                             OCConnectivityType connectivityType,
167                                             FindCallback resourceHandler)
168     {
169         return findResource(host, resourceName, connectivityType, resourceHandler, m_cfg.QoS);
170     }
171
172     OCStackResult OCPlatform_impl::findResource(const std::string& host,
173                                             const std::string& resourceName,
174                                             OCConnectivityType connectivityType,
175                                             FindCallback resourceHandler,
176                                             QualityOfService QoS)
177     {
178         return checked_guard(m_client, &IClientWrapper::ListenForResource,
179                              host, resourceName, connectivityType, resourceHandler, QoS);
180     }
181
182     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
183                                             const std::string& deviceURI,
184                                             OCConnectivityType connectivityType,
185                                             FindDeviceCallback deviceInfoHandler)
186     {
187         return result_guard(getDeviceInfo(host, deviceURI, connectivityType,
188                deviceInfoHandler, m_cfg.QoS));
189     }
190
191     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
192                                             const std::string& deviceURI,
193                                             OCConnectivityType connectivityType,
194                                             FindDeviceCallback deviceInfoHandler,
195                                             QualityOfService QoS)
196     {
197         return checked_guard(m_client, &IClientWrapper::ListenForDevice,
198                              host, deviceURI, connectivityType, deviceInfoHandler, QoS);
199     }
200
201     OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
202                                             const std::string& platformURI,
203                                             OCConnectivityType connectivityType,
204                                             FindPlatformCallback platformInfoHandler)
205     {
206         return result_guard(getPlatformInfo(host, platformURI, connectivityType,
207                platformInfoHandler, m_cfg.QoS));
208     }
209
210     OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
211                                             const std::string& platformURI,
212                                             OCConnectivityType connectivityType,
213                                             FindPlatformCallback platformInfoHandler,
214                                             QualityOfService QoS)
215     {
216         return checked_guard(m_client, &IClientWrapper::ListenForDevice,
217                              host, platformURI, connectivityType, platformInfoHandler, QoS);
218     }
219
220     OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
221                                             std::string& resourceURI,
222                                             const std::string& resourceTypeName,
223                                             const std::string& resourceInterface,
224                                             EntityHandler entityHandler,
225                                             uint8_t resourceProperty)
226     {
227         return checked_guard(m_server, &IServerWrapper::registerResource,
228                              std::ref(resourceHandle), resourceURI, resourceTypeName,
229                              resourceInterface, entityHandler, resourceProperty);
230     }
231
232     OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
233     {
234         return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
235     }
236
237     OCStackResult OCPlatform_impl::registerPlatformInfo(const OCPlatformInfo platformInfo)
238     {
239         return checked_guard(m_server, &IServerWrapper::registerPlatformInfo, platformInfo);
240     }
241
242     OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
243                                             const std::shared_ptr< OCResource > resource)
244     {
245         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
246         std::vector<std::string> resourceTypes = resource->getResourceTypes();
247
248         return checked_guard(m_server, &IServerWrapper::registerResource,
249                 std::ref(resourceHandle), resource->uri(),
250                 resourceTypes[0]/*"core.remote"*/, DEFAULT_INTERFACE,
251                 (EntityHandler) nullptr, resourceProperty);
252     }
253
254     OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
255     {
256         return checked_guard(m_server, &IServerWrapper::unregisterResource,
257                              resourceHandle);
258     }
259
260     OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
261                                             OCResourceHandle resourceHandle)
262     {
263         return result_guard(OCUnBindResource(std::ref(collectionHandle), std::ref(resourceHandle)));
264     }
265
266     OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
267                                             const std::vector<OCResourceHandle>& resourceHandles)
268     {
269         for(const auto& h : resourceHandles)
270         {
271            OCStackResult r;
272
273            if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
274            {
275                return r;
276            }
277         }
278
279         return OC_STACK_OK;
280     }
281
282     OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
283                                             const OCResourceHandle resourceHandle)
284     {
285         return result_guard(OCBindResource(collectionHandle, resourceHandle));
286     }
287
288     OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
289                                             const std::vector<OCResourceHandle>& resourceHandles)
290     {
291         for(const auto& h : resourceHandles)
292         {
293            OCStackResult r;
294
295            if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
296            {
297                return r;
298            }
299         }
300
301         return OC_STACK_OK;
302     }
303
304     OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
305                                              const std::string& resourceTypeName) const
306     {
307         return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
308                              resourceHandle, resourceTypeName);
309     }
310
311     OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
312                                              const std::string& resourceInterfaceName) const
313     {
314         return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
315                              resourceHandle, resourceInterfaceName);
316     }
317
318     OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
319     {
320         return checked_guard(m_server, &IServerWrapper::startPresence,
321                              announceDurationSeconds);
322     }
323
324     OCStackResult OCPlatform_impl::stopPresence()
325     {
326         return checked_guard(m_server, &IServerWrapper::stopPresence);
327     }
328
329     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
330                                             const std::string& host,
331                                             OCConnectivityType connectivityType,
332                                             SubscribeCallback presenceHandler)
333     {
334         return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
335     }
336
337
338     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
339                                             const std::string& host,
340                                             const std::string& resourceType,
341                                             OCConnectivityType connectivityType,
342                                             SubscribeCallback presenceHandler)
343     {
344         return checked_guard(m_client, &IClientWrapper::SubscribePresence,
345                              &presenceHandle, host, resourceType, connectivityType,
346                              presenceHandler);
347     }
348
349     OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
350     {
351         return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
352                              std::ref(presenceHandle));
353     }
354
355     OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
356     {
357         return checked_guard(m_server, &IServerWrapper::sendResponse,
358                              pResponse);
359     }
360 } //namespace OC
361