1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 //******************************************************************
25 // Description: Implementation of the OCPlatform.
29 //*********************************************************************
33 #include "OCPlatform.h"
35 #include "OCException.h"
39 // Constructor. Internally calls private init function
40 OCPlatform::OCPlatform(const PlatformConfig& config)
46 OCPlatform::~OCPlatform(void)
48 std::cout << "platform destructor called" << std::endl;
51 OCStackResult OCPlatform::notifyAllObservers(OCResourceHandle resourceHandle)
53 return OCNotifyAllObservers(resourceHandle);
56 OCStackResult OCPlatform::notifyListOfObservers(
57 OCResourceHandle resourceHandle,
58 ObservationIds& observationIds,
59 const std::shared_ptr<OCResourceResponse> pResponse)
61 OCStackResult result = OC_STACK_ERROR;
67 std::string payload = pResponse->getPayload();
68 unsigned char *pBuffer = new unsigned char[payload.length()+1];
69 strncpy((char*)pBuffer, payload.c_str(), payload.length() + 1);
72 printf("\tGoing from stack for List of Observers: Payload: %s\n", (char*)pBuffer);
74 result = OCNotifyListOfObservers(resourceHandle, &observationIds[0],
75 observationIds.size(), pBuffer);
79 catch(std::exception e) // TODO : define our own exception
88 void OCPlatform::init(const PlatformConfig& config)
90 m_csdkLock = make_shared<std::mutex>();
91 std::unique_ptr<WrapperFactory> wrapperInstance(new WrapperFactory());
92 m_WrapperInstance = std::move(wrapperInstance);
94 if(config.mode == ModeType::Server)
96 // Call server wrapper init
97 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
99 else if(config.mode == ModeType::Client)
101 // Call client wrapper init
102 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
106 // This must be both server and client
107 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
108 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
112 OCResource::Ptr OCPlatform::constructResourceObject(const std::string& host, const std::string& uri,
113 bool isObservable, const std::vector<std::string>& resourceTypes,
114 const std::vector<std::string>& interfaces)
118 return std::shared_ptr<OCResource>(new OCResource(m_client, host, uri, isObservable, resourceTypes, interfaces));
123 return std::shared_ptr<OCResource>();
127 OCStackResult OCPlatform::findResource(const std::string& host, const std::string& resourceName,
128 FindCallback resourceHandler)
132 return m_client->ListenForResource(host, resourceName, resourceHandler);
134 return OC_STACK_ERROR;
138 OCStackResult OCPlatform::registerResource(OCResourceHandle& resourceHandle,
139 std::string& resourceURI,
140 const std::string& resourceTypeName,
141 const std::string& resourceInterface,
142 RegisterCallback entityHandler,
143 uint8_t resourceProperty)
145 OCStackResult result = OC_STACK_OK;
150 result = m_server->registerResource(resourceHandle, resourceURI, resourceTypeName, resourceInterface, entityHandler, resourceProperty);
152 catch(std::exception e) // TODO: define our own expception.
162 OCStackResult OCPlatform::unregisterResource(const OCResourceHandle& resourceHandle) const
164 OCStackResult result = OC_STACK_ERROR;
168 result = m_server->unregisterResource(resourceHandle);
175 OCStackResult OCPlatform::unbindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle)
177 OCStackResult result = OC_STACK_OK;
180 result = OCUnBindResource(collectionHandle, resourceHandle);
182 catch(std::exception e) // TODO: define our own expception.
190 OCStackResult OCPlatform::unbindResources(const OCResourceHandle collectionHandle, const std::vector<OCResourceHandle>& resourceHandles)
192 OCStackResult result = OC_STACK_OK;
196 for(const OCResourceHandle& handle : resourceHandles)
198 result = OCUnBindResource(collectionHandle, handle);
200 if(result != OC_STACK_OK)
202 // TODO Should we unbind the previous successful ones?
203 // TODO should we return which are succesful
204 // Currently just returns with any failure
209 catch(std::exception e) // TODO : define our own exception
217 OCStackResult OCPlatform::bindResource(const OCResourceHandle collectionHandle, const OCResourceHandle resourceHandle)
219 OCStackResult result = OC_STACK_OK;
222 result = OCBindResource(collectionHandle, resourceHandle);
224 catch(std::exception e) // TODO : define our own exception
232 OCStackResult OCPlatform::bindResources(const OCResourceHandle collectionHandle, const std::vector<OCResourceHandle>& resourceHandles)
234 OCStackResult result = OC_STACK_OK;
238 for(const OCResourceHandle& handle : resourceHandles)
240 result = OCBindResource(collectionHandle, handle);
242 if(result != OC_STACK_OK)
244 // TODO Should we unbind the previous successful ones?
245 // TODO should we return which are succesful
246 // Currently just returns with any failure
251 catch(std::exception e) // TODO : define our own exception
259 OCStackResult OCPlatform::bindTypeToResource(const OCResourceHandle& resourceHandle,
260 const std::string& resourceTypeName) const
262 OCStackResult result = OC_STACK_ERROR;
267 result = m_server->bindTypeToResource(resourceHandle, resourceTypeName);
269 catch (OCException& e)
271 cout << "Caught an exception..." << endl;
272 cout << "\tMessage: " << e.what() << endl;
273 cout << "\t Reason: " << e.reason() << endl;
279 OCStackResult OCPlatform::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
280 const std::string& resourceInterfaceName) const
282 OCStackResult result = OC_STACK_ERROR;
287 result = m_server->bindInterfaceToResource(resourceHandle, resourceInterfaceName);
289 catch (OCException& e)
291 cout << "Caught an exception..." << endl;
292 cout << "\tMessage: " << e.what() << endl;
293 cout << "\t Reason: " << e.reason() << endl;
300 OCStackResult OCPlatform::startPresence(const unsigned int announceDurationSeconds)
304 return m_server->startPresence(announceDurationSeconds);
308 return OC_STACK_ERROR;
312 OCStackResult OCPlatform::stopPresence()
316 return m_server->stopPresence();
320 return OC_STACK_ERROR;
324 OCStackResult OCPlatform::subscribePresence(OCPresenceHandle& presenceHandle, const std::string& host,
325 SubscribeCallback presenceHandler)
329 return m_client->SubscribePresence(&presenceHandle, host, presenceHandler);
333 return OC_STACK_ERROR;
337 OCStackResult OCPlatform::unsubscribePresence(OCPresenceHandle presenceHandle)
341 return m_client->UnsubscribePresence(presenceHandle);
345 return OC_STACK_ERROR;