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;
78 ret = OC::OCPlatform::findResource("",
79 "coap://" + hostAddress + ":6298" + "/oc/core?rt=SoftSensorManager.Sensor", OC_ALL,
80 std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
82 if (ret != OC_STACK_OK)
83 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
87 case OC_STACK_PRESENCE_STOPPED:
88 case OC_STACK_PRESENCE_TIMEOUT:
89 if (m_mapResources.find(hostAddress) != m_mapResources.end())
91 while (!m_mapResources[hostAddress].empty())
93 pMessage = new intptr_t[2];
94 pMessage[0] = RESOURCE_DISCOVER_UNINSTALL_RESOURCE;
95 pMessage[1] = reinterpret_cast<intptr_t> (m_mapResourceHandler[m_mapResources[hostAddress].back()]);
96 m_mapResources[hostAddress].pop_back();
97 m_pTasker->addTask(this, pMessage);
100 m_mapResources.erase(hostAddress);
104 case OC_STACK_VIRTUAL_DO_NOT_HANDLE:
115 SSMRESULT CResourceFinder::startResourceFinder()
117 SSMRESULT res = SSM_E_FAIL;
118 OCStackResult ret = OC_STACK_ERROR;
120 ret = OC::OCPlatform::findResource("",
121 "coap://224.0.1.187:6298/oc/core?rt=SoftSensorManager.Sensor", OC_ALL,
122 std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
124 if (ret != OC_STACK_OK)
125 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
127 ret = OC::OCPlatform::subscribePresence(m_multicastPresenceHandle, "coap://224.0.1.187",
128 "SoftSensorManager.Sensor", OC_ALL,
129 std::bind(&CResourceFinder::presenceHandler, this, std::placeholders::_1,
130 std::placeholders::_2, std::placeholders::_3));
132 if (ret != OC_STACK_OK)
133 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
141 SSMRESULT CResourceFinder::stopResourceFinder()
143 SSMRESULT res = SSM_E_FAIL;
144 OCStackResult ret = OC_STACK_ERROR;
146 ret = OC::OCPlatform::unsubscribePresence(m_multicastPresenceHandle);
148 if (ret != OC_STACK_OK)
149 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
151 m_multicastPresenceHandle = nullptr;
159 SSMRESULT CResourceFinder::startObserveResource(IN ISSMResource *pSensor, IN IEvent *pEvent)
161 return m_mapResourceHandler[pSensor->name]->startObserve(pEvent);
164 SSMRESULT CResourceFinder::stopObserveResource(IN ISSMResource *pSensor)
166 return m_mapResourceHandler[pSensor->name]->stopObserve();
169 void CResourceFinder::onExecute(IN void *pArg)
171 SSMRESULT res = SSM_E_FAIL;
172 OCStackResult ret = OC_STACK_ERROR;
173 OC::QueryParamsMap queryParams;
174 OICResourceHandler *pResourceHandler = NULL;
175 intptr_t *pMessage = reinterpret_cast<intptr_t *>(pArg);
176 std::shared_ptr< OC::OCResource > *pResource = NULL;
177 OC::OCPlatform::OCPresenceHandle presenceHandle = NULL;
179 std::string resourceHostAddress = "";
180 std::string resourceFullPath = "";
184 case RESOURCE_DISCOVER_REQUESTPROFILE:
185 pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
186 pResourceHandler = new OICResourceHandler();
187 SSM_CLEANUP_ASSERT(pResourceHandler->initHandler(*pResource, this));
189 resourceFullPath = pResource->get()->host() + pResource->get()->uri();
191 resourceHostAddress = pResource->get()->host();
192 resourceHostAddress.erase(0, 7); // erase 'coap://'
194 m_mapResourceHandler[resourceFullPath] = pResourceHandler;
196 m_mapResources[resourceHostAddress].push_back(resourceFullPath);
198 ret = pResource->get()->get(queryParams,
199 std::bind(&OICResourceHandler::onGetResourceProfile, pResourceHandler,
200 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
202 if (ret != OC_STACK_OK)
203 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
207 case RESOURCE_DISCOVER_INSTALL_RESOURCE:
208 if (m_mapResourcePresenceHandles.find(((ISSMResource *)pMessage[1])->ip) ==
209 m_mapResourcePresenceHandles.end())
211 ret = OC::OCPlatform::subscribePresence(presenceHandle, ((ISSMResource *)pMessage[1])->ip,
212 "SoftSensorManager.Sensor", OC_ALL,
213 std::bind(&CResourceFinder::presenceHandler, this, std::placeholders::_1,
214 std::placeholders::_2, std::placeholders::_3));
216 if (ret != OC_STACK_OK)
217 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
219 m_mapResourcePresenceHandles[((ISSMResource *)pMessage[1])->ip] = presenceHandle;
222 m_pResourceFinderEvent->onResourceFound((ISSMResource *) pMessage[1]);
225 case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
226 m_pResourceFinderEvent->onResourceLost(&((OICResourceHandler *) pMessage[1])->m_SSMResource);
228 if (m_mapResourcePresenceHandles.find(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip) !=
229 m_mapResourcePresenceHandles.end())
231 ret = OC::OCPlatform::unsubscribePresence(
232 m_mapResourcePresenceHandles[((OICResourceHandler *)pMessage[1])->m_SSMResource.ip]);
234 if (ret != OC_STACK_OK)
235 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
237 m_mapResourcePresenceHandles.erase(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip);
240 m_mapResourceHandler.erase(((OICResourceHandler *) pMessage[1])->m_SSMResource.name);
248 void CResourceFinder::onTerminate(IN void *pArg)
250 std::shared_ptr< OC::OCResource > *pResource = NULL;
251 intptr_t *pMessage = (intptr_t *)pArg;
255 case RESOURCE_DISCOVER_REQUESTPROFILE:
256 pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
260 case RESOURCE_DISCOVER_INSTALL_RESOURCE:
263 case RESOURCE_DISCOVER_UNINSTALL_RESOURCE: