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;
79 ret = OC::OCPlatform::findResource("",
80 "coap://" + hostAddress + ":5298" + "/oc/core?rt=SoftSensorManager.Sensor", OC_ALL,
81 std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
83 ret = OC::OCPlatform::findResource("",
84 "coap://" + hostAddress + "/oc/core?rt=SoftSensorManager.Sensor",
85 std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
88 if (ret != OC_STACK_OK)
89 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
93 case OC_STACK_PRESENCE_STOPPED:
94 case OC_STACK_PRESENCE_TIMEOUT:
95 if (m_mapResources.find(hostAddress) != m_mapResources.end())
97 while (!m_mapResources[hostAddress].empty())
99 pMessage = new intptr_t[2];
100 pMessage[0] = RESOURCE_DISCOVER_UNINSTALL_RESOURCE;
101 pMessage[1] = reinterpret_cast<intptr_t> (m_mapResourceHandler[m_mapResources[hostAddress].back()]);
102 m_mapResources[hostAddress].pop_back();
103 m_pTasker->addTask(this, pMessage);
106 m_mapResources.erase(hostAddress);
110 case OC_STACK_VIRTUAL_DO_NOT_HANDLE:
121 SSMRESULT CResourceFinder::startResourceFinder()
123 SSMRESULT res = SSM_E_FAIL;
124 OCStackResult ret = OC_STACK_ERROR;
127 ret = OC::OCPlatform::findResource("",
128 "coap://224.0.1.187:5298/oc/core?rt=SoftSensorManager.Sensor", OC_ALL,
129 std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
131 ret = OC::OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=SoftSensorManager.Sensor",
132 std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
135 if (ret != OC_STACK_OK)
136 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
139 ret = OC::OCPlatform::subscribePresence(m_multicastPresenceHandle, "coap://224.0.1.187",
140 "SoftSensorManager.Sensor", OC_ALL,
141 std::bind(&CResourceFinder::presenceHandler, this, std::placeholders::_1,
142 std::placeholders::_2, std::placeholders::_3));
144 ret = OC::OCPlatform::subscribePresence(m_multicastPresenceHandle, "coap://224.0.1.187",
145 "SoftSensorManager.Sensor",
146 std::bind(&CResourceFinder::presenceHandler, this, std::placeholders::_1,
147 std::placeholders::_2, std::placeholders::_3));
150 if (ret != OC_STACK_OK)
151 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
159 SSMRESULT CResourceFinder::stopResourceFinder()
161 SSMRESULT res = SSM_E_FAIL;
162 OCStackResult ret = OC_STACK_ERROR;
164 ret = OC::OCPlatform::unsubscribePresence(m_multicastPresenceHandle);
166 if (ret != OC_STACK_OK)
167 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
169 m_multicastPresenceHandle = nullptr;
177 SSMRESULT CResourceFinder::startObserveResource(IN ISSMResource *pSensor, IN IEvent *pEvent)
179 return m_mapResourceHandler[pSensor->name]->startObserve(pEvent);
182 SSMRESULT CResourceFinder::stopObserveResource(IN ISSMResource *pSensor)
184 return m_mapResourceHandler[pSensor->name]->stopObserve();
187 void CResourceFinder::onExecute(IN void *pArg)
189 SSMRESULT res = SSM_E_FAIL;
190 OCStackResult ret = OC_STACK_ERROR;
191 OC::QueryParamsMap queryParams;
192 OICResourceHandler *pResourceHandler = NULL;
193 intptr_t *pMessage = reinterpret_cast<intptr_t *>(pArg);
194 std::shared_ptr< OC::OCResource > *pResource = NULL;
195 OC::OCPlatform::OCPresenceHandle presenceHandle = NULL;
197 std::string resourceHostAddress = "";
198 std::string resourceFullPath = "";
202 case RESOURCE_DISCOVER_REQUESTPROFILE:
203 pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
204 pResourceHandler = new OICResourceHandler();
205 SSM_CLEANUP_ASSERT(pResourceHandler->initHandler(*pResource, this));
207 resourceFullPath = pResource->get()->host() + pResource->get()->uri();
209 resourceHostAddress = pResource->get()->host();
210 resourceHostAddress.erase(0, 7); // erase 'coap://'
212 m_mapResourceHandler[resourceFullPath] = pResourceHandler;
214 m_mapResources[resourceHostAddress].push_back(resourceFullPath);
216 ret = pResource->get()->get(queryParams,
217 std::bind(&OICResourceHandler::onGetResourceProfile, pResourceHandler,
218 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
220 if (ret != OC_STACK_OK)
221 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
225 case RESOURCE_DISCOVER_INSTALL_RESOURCE:
226 if (m_mapResourcePresenceHandles.find(((ISSMResource *)pMessage[1])->ip) ==
227 m_mapResourcePresenceHandles.end())
230 ret = OC::OCPlatform::subscribePresence(presenceHandle, ((ISSMResource *)pMessage[1])->ip,
231 "SoftSensorManager.Sensor", OC_ALL,
232 std::bind(&CResourceFinder::presenceHandler, this, std::placeholders::_1,
233 std::placeholders::_2, std::placeholders::_3));
235 ret = OC::OCPlatform::subscribePresence(presenceHandle, ((ISSMResource *)pMessage[1])->ip,
236 "SoftSensorManager.Sensor",
237 std::bind(&CResourceFinder::presenceHandler, this, std::placeholders::_1,
238 std::placeholders::_2, std::placeholders::_3));
241 if (ret != OC_STACK_OK)
242 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
244 m_mapResourcePresenceHandles[((ISSMResource *)pMessage[1])->ip] = presenceHandle;
247 m_pResourceFinderEvent->onResourceFound((ISSMResource *) pMessage[1]);
250 case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
251 m_pResourceFinderEvent->onResourceLost(&((OICResourceHandler *) pMessage[1])->m_SSMResource);
253 if (m_mapResourcePresenceHandles.find(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip) !=
254 m_mapResourcePresenceHandles.end())
256 ret = OC::OCPlatform::unsubscribePresence(
257 m_mapResourcePresenceHandles[((OICResourceHandler *)pMessage[1])->m_SSMResource.ip]);
259 if (ret != OC_STACK_OK)
260 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
262 m_mapResourcePresenceHandles.erase(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip);
265 m_mapResourceHandler.erase(((OICResourceHandler *) pMessage[1])->m_SSMResource.name);
273 void CResourceFinder::onTerminate(IN void *pArg)
275 std::shared_ptr< OC::OCResource > *pResource = NULL;
276 intptr_t *pMessage = (intptr_t *)pArg;
280 case RESOURCE_DISCOVER_REQUESTPROFILE:
281 pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
285 case RESOURCE_DISCOVER_INSTALL_RESOURCE:
288 case RESOURCE_DISCOVER_UNINSTALL_RESOURCE: