Added commentary to the public API referring to new functionality of
[platform/upstream/iotivity.git] / src / OCPlatform.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.cpp
24 //
25 // Description: Implementation of the OCPlatform.
26 //
27 //
28 //
29 //*********************************************************************
30
31 #include <random>
32 #include <utility>
33 #include <functional>
34
35 #include "ocstack.h"
36
37 #include "OCPlatform.h"
38 #include "OCApi.h"
39 #include "OCException.h"
40 #include "OCUtilities.h"
41
42 #include "oc_logger.hpp"
43
44 namespace OC
45 {
46 void OCPlatform::init(const PlatformConfig& config)
47 {
48     switch(config.mode)
49      {
50        case ModeType::Server:
51                        m_server = m_WrapperInstance->CreateServerWrapper(*this, m_csdkLock, config);
52                        break;
53
54        case ModeType::Client:
55                       m_client = m_WrapperInstance->CreateClientWrapper(*this, m_csdkLock, config);
56                       break;
57
58        case ModeType::Both:
59                        m_server = m_WrapperInstance->CreateServerWrapper(*this, m_csdkLock, config);
60                        m_client = m_WrapperInstance->CreateClientWrapper(*this, m_csdkLock, config);
61                        break;
62      }
63 }
64
65 OCPlatform::OCPlatform(const PlatformConfig& config)
66  : m_cfg             { config },
67    m_WrapperInstance { make_unique<WrapperFactory>() },
68    m_csdkLock        { make_shared<std::recursive_mutex>() }
69 {
70     init(m_cfg);
71 }
72
73 OCPlatform::~OCPlatform(void)
74 {
75 }
76
77 OCStackResult OCPlatform::setDefaultDeviceEntityHandler(EntityHandler entityHandler)
78 {
79     return checked_guard(m_server, &IServerWrapper::setDefaultDeviceEntityHandler,
80                          entityHandler);
81 }
82
83 OCStackResult OCPlatform::notifyAllObservers(OCResourceHandle resourceHandle, QualityOfService QoS)
84 {
85     return result_guard(OCNotifyAllObservers(resourceHandle, static_cast<OCQualityOfService>(QoS)));
86 }
87
88 OCStackResult OCPlatform::notifyAllObservers(OCResourceHandle resourceHandle)
89 {
90     return notifyAllObservers(resourceHandle, m_cfg.QoS);
91 }
92
93 OCStackResult OCPlatform::notifyListOfObservers(OCResourceHandle resourceHandle,
94                                             ObservationIds& observationIds,
95                                             const std::shared_ptr<OCResourceResponse> pResponse)
96 {
97     return notifyListOfObservers(resourceHandle, observationIds, pResponse, m_cfg.QoS);
98 }
99
100 OCStackResult OCPlatform::notifyListOfObservers(OCResourceHandle resourceHandle,
101                                             ObservationIds& observationIds,
102                                             const std::shared_ptr<OCResourceResponse> pResponse,
103                                             QualityOfService QoS)
104 {
105     if(!pResponse)
106     {
107      return result_guard(OC_STACK_ERROR);
108     }
109
110     std::string payload(pResponse->getPayload());
111
112     return result_guard(
113                OCNotifyListOfObservers(resourceHandle,
114                                        &observationIds[0], observationIds.size(),
115                                        reinterpret_cast<unsigned char *>(const_cast<char *>(payload.c_str())),
116                                        static_cast<OCQualityOfService>(QoS)));
117 }
118
119 OCResource::Ptr OCPlatform::constructResourceObject(const std::string& host, const std::string& uri,
120                                                     bool isObservable, const std::vector<std::string>& resourceTypes,
121                                                     const std::vector<std::string>& interfaces)
122 {
123     if(!m_client)
124     {
125         return std::shared_ptr<OCResource>();
126     }
127
128     return std::shared_ptr<OCResource>(new OCResource(m_client, host, uri, isObservable, resourceTypes, interfaces));
129 }
130
131 OCStackResult OCPlatform::findResource(const std::string& host, const std::string& resourceName,
132                                        FindCallback resourceHandler)
133 {
134     return findResource(host, resourceName, resourceHandler, m_cfg.QoS);
135 }
136
137 OCStackResult OCPlatform::findResource(const std::string& host, const std::string& resourceName,
138                                        FindCallback resourceHandler, QualityOfService QoS)
139 {
140     return checked_guard(m_client, &IClientWrapper::ListenForResource,
141                          host, resourceName, resourceHandler, QoS);
142 }
143
144
145 OCStackResult OCPlatform::registerResource(OCResourceHandle& resourceHandle,
146                                            std::string& resourceURI,
147                                            const std::string& resourceTypeName,
148                                            const std::string& resourceInterface,
149                                            EntityHandler entityHandler,
150                                            uint8_t resourceProperty)
151 {
152     return checked_guard(m_server, &IServerWrapper::registerResource,
153                          ref(resourceHandle), resourceURI, resourceTypeName,
154                          resourceInterface, entityHandler, resourceProperty);
155 }
156
157 OCStackResult OCPlatform::unregisterResource(const OCResourceHandle& resourceHandle) const
158 {
159     return checked_guard(m_server, &IServerWrapper::unregisterResource,
160                          resourceHandle);
161 }
162
163 OCStackResult OCPlatform::unbindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle)
164 {
165     return result_guard(OCUnBindResource(ref(collectionHandle), ref(resourceHandle)));
166 }
167
168 OCStackResult OCPlatform::unbindResources(const OCResourceHandle collectionHandle, const std::vector<OCResourceHandle>& resourceHandles)
169 {
170     for(const auto& h : resourceHandles)
171     {
172        OCStackResult r;
173
174        if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
175        {
176            return r;
177        }
178     }
179
180     return OC_STACK_OK;
181 }
182
183 OCStackResult OCPlatform::bindResource(const OCResourceHandle collectionHandle, const OCResourceHandle resourceHandle)
184 {
185     return result_guard(OCBindResource(collectionHandle, resourceHandle));
186 }
187
188 OCStackResult OCPlatform::bindResources(const OCResourceHandle collectionHandle, const std::vector<OCResourceHandle>& resourceHandles)
189 {
190     for(const auto& h : resourceHandles)
191     {
192        OCStackResult r;
193
194        if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
195        {
196            return r;
197        }
198     }
199
200     return OC_STACK_OK;
201 }
202
203 OCStackResult OCPlatform::bindTypeToResource(const OCResourceHandle& resourceHandle,
204                                              const std::string& resourceTypeName) const
205 {
206     return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
207                          resourceHandle, resourceTypeName);
208 }
209
210 OCStackResult OCPlatform::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
211                                                   const std::string& resourceInterfaceName) const
212 {
213     return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
214                          resourceHandle, resourceInterfaceName);
215 }
216
217 OCStackResult OCPlatform::startPresence(const unsigned int announceDurationSeconds)
218 {
219     return checked_guard(m_server, &IServerWrapper::startPresence,
220                          announceDurationSeconds);
221 }
222
223 OCStackResult OCPlatform::stopPresence()
224 {
225     return checked_guard(m_server, &IServerWrapper::stopPresence);
226 }
227
228 OCStackResult OCPlatform::subscribePresence(OCPresenceHandle& presenceHandle, const std::string& host,
229                                             SubscribeCallback presenceHandler)
230 {
231     return checked_guard(m_client, &IClientWrapper::SubscribePresence,
232                          &presenceHandle, host, presenceHandler);
233 }
234
235 OCStackResult OCPlatform::unsubscribePresence(OCPresenceHandle presenceHandle)
236 {
237     return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
238                          ref(presenceHandle));
239 }
240
241 } //namespace OC