1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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 ******************************************************************/
20 #include "ResourceFinder.h"
22 SSMRESULT CResourceFinder::finalConstruct()
24 SSMRESULT res = SSM_E_FAIL;
26 OC::PlatformConfig cfg(OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0,
27 OC::QualityOfService::LowQos);
29 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_ITasker, (IBase **)&m_pTasker));
31 OC::OCPlatform::Configure(cfg);
33 m_pResourceFinderEvent = NULL;
35 m_multicastPresenceHandle = nullptr;
41 void CResourceFinder::finalRelease()
45 SSMRESULT CResourceFinder::registerResourceFinderEvent(IN IResourceFinderEvent *pEvent)
47 m_pResourceFinderEvent = pEvent;
51 void CResourceFinder::onResourceFound(std::shared_ptr< OC::OCResource > resource)
55 std::string path = resource->host() + resource->uri();
57 if (m_mapResourceHandler.find(path) != m_mapResourceHandler.end())
60 intptr_t *pMessage = new intptr_t [2];
61 pMessage[0] = RESOURCE_DISCOVER_REQUESTPROFILE;
62 pMessage[1] = reinterpret_cast<intptr_t> (new std::shared_ptr<OC::OCResource>(resource));
64 m_pTasker->addTask(this, pMessage);
68 void CResourceFinder::presenceHandler(OCStackResult result, const unsigned int nonce,
69 const std::string &hostAddress)
71 SSMRESULT res = SSM_E_FAIL;
72 OCStackResult ret = OC_STACK_ERROR;
73 intptr_t *pMessage = NULL;
74 std::ostringstream requestURI;
79 requestURI << "coap://" << hostAddress << ":" << OC_MULTICAST_PORT <<
80 "/oc/core?rt=SoftSensorManager.Sensor";
82 ret = OC::OCPlatform::findResource("", requestURI.str(), OC_WIFI,
83 std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
85 if (ret != OC_STACK_OK)
86 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
90 case OC_STACK_PRESENCE_STOPPED:
91 case OC_STACK_PRESENCE_TIMEOUT:
92 if (m_mapResources.find(hostAddress) != m_mapResources.end())
94 while (!m_mapResources[hostAddress].empty())
96 pMessage = new intptr_t[2];
97 pMessage[0] = RESOURCE_DISCOVER_UNINSTALL_RESOURCE;
98 pMessage[1] = reinterpret_cast<intptr_t> (m_mapResourceHandler[m_mapResources[hostAddress].back()]);
99 m_mapResources[hostAddress].pop_back();
100 m_pTasker->addTask(this, pMessage);
103 m_mapResources.erase(hostAddress);
107 case OC_STACK_VIRTUAL_DO_NOT_HANDLE:
118 SSMRESULT CResourceFinder::startResourceFinder()
120 SSMRESULT res = SSM_E_FAIL;
121 OCStackResult ret = OC_STACK_ERROR;
123 std::ostringstream requestURI;
124 requestURI << OC_WELL_KNOWN_QUERY << "?rt=SoftSensorManager.Sensor";
126 ret = OC::OCPlatform::findResource("", requestURI.str(), OC_WIFI,
127 std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
129 if (ret != OC_STACK_OK)
130 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
132 ret = OC::OCPlatform::subscribePresence(m_multicastPresenceHandle, OC_MULTICAST_IP,
133 "SoftSensorManager.Sensor", OC_WIFI, std::bind(&CResourceFinder::presenceHandler, this,
134 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
136 if (ret != OC_STACK_OK)
137 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
145 SSMRESULT CResourceFinder::stopResourceFinder()
147 SSMRESULT res = SSM_E_FAIL;
148 OCStackResult ret = OC_STACK_ERROR;
150 ret = OC::OCPlatform::unsubscribePresence(m_multicastPresenceHandle);
152 if (ret != OC_STACK_OK)
153 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
155 m_multicastPresenceHandle = nullptr;
163 SSMRESULT CResourceFinder::startObserveResource(IN ISSMResource *pSensor, IN IEvent *pEvent)
165 return m_mapResourceHandler[pSensor->name]->startObserve(pEvent);
168 SSMRESULT CResourceFinder::stopObserveResource(IN ISSMResource *pSensor)
170 return m_mapResourceHandler[pSensor->name]->stopObserve();
173 void CResourceFinder::onExecute(IN void *pArg)
175 SSMRESULT res = SSM_E_FAIL;
176 OCStackResult ret = OC_STACK_ERROR;
177 OC::QueryParamsMap queryParams;
178 OICResourceHandler *pResourceHandler = NULL;
179 intptr_t *pMessage = reinterpret_cast<intptr_t *>(pArg);
180 std::shared_ptr< OC::OCResource > *pResource = NULL;
181 OC::OCPlatform::OCPresenceHandle presenceHandle = NULL;
183 std::string resourceHostAddress = "";
184 std::string resourceFullPath = "";
188 case RESOURCE_DISCOVER_REQUESTPROFILE:
189 pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
190 pResourceHandler = new OICResourceHandler();
191 SSM_CLEANUP_ASSERT(pResourceHandler->initHandler(*pResource, this));
193 resourceFullPath = pResource->get()->host() + pResource->get()->uri();
195 resourceHostAddress = pResource->get()->host();
196 resourceHostAddress.erase(0, 7); // erase 'coap://'
198 m_mapResourceHandler[resourceFullPath] = pResourceHandler;
200 m_mapResources[resourceHostAddress].push_back(resourceFullPath);
202 ret = pResource->get()->get(queryParams, std::bind(&OICResourceHandler::onGetResourceProfile,
203 pResourceHandler, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
205 if (ret != OC_STACK_OK)
206 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
210 case RESOURCE_DISCOVER_INSTALL_RESOURCE:
211 if (m_mapResourcePresenceHandles.find(((ISSMResource *)pMessage[1])->ip) ==
212 m_mapResourcePresenceHandles.end())
214 ret = OC::OCPlatform::subscribePresence(presenceHandle, ((ISSMResource *)pMessage[1])->ip,
215 "SoftSensorManager.Sensor", OC_WIFI, std::bind(&CResourceFinder::presenceHandler, this,
216 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
218 if (ret != OC_STACK_OK)
219 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
221 m_mapResourcePresenceHandles[((ISSMResource *)pMessage[1])->ip] = presenceHandle;
224 m_pResourceFinderEvent->onResourceFound((ISSMResource *) pMessage[1]);
227 case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
228 m_pResourceFinderEvent->onResourceLost(&((OICResourceHandler *) pMessage[1])->m_SSMResource);
230 if (m_mapResourcePresenceHandles.find(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip) !=
231 m_mapResourcePresenceHandles.end())
233 ret = OC::OCPlatform::unsubscribePresence(m_mapResourcePresenceHandles[((
234 OICResourceHandler *)pMessage[1])->m_SSMResource.ip]);
236 if (ret != OC_STACK_OK)
237 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
239 m_mapResourcePresenceHandles.erase(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip);
242 delete m_mapResourceHandler[((OICResourceHandler *)pMessage[1])->m_SSMResource.name];
243 m_mapResourceHandler.erase(((OICResourceHandler *) pMessage[1])->m_SSMResource.name);
251 void CResourceFinder::onTerminate(IN void *pArg)
253 std::shared_ptr< OC::OCResource > *pResource = NULL;
254 intptr_t *pMessage = (intptr_t *)pArg;
258 case RESOURCE_DISCOVER_REQUESTPROFILE:
259 pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
263 case RESOURCE_DISCOVER_INSTALL_RESOURCE:
266 case RESOURCE_DISCOVER_UNINSTALL_RESOURCE: