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;
39 void CResourceFinder::finalRelease()
43 SSMRESULT CResourceFinder::registerResourceFinderEvent(IN IResourceFinderEvent *pEvent)
45 m_pResourceFinderEvent = pEvent;
49 void CResourceFinder::onResourceFound(std::shared_ptr< OC::OCResource > resource)
53 std::string path = resource->host() + resource->uri();
55 if (m_mapResourceHandler.find(path) != m_mapResourceHandler.end())
58 intptr_t *pMessage = new intptr_t [2];
59 pMessage[0] = RESOURCE_DISCOVER_REQUESTPROFILE;
60 pMessage[1] = reinterpret_cast<intptr_t> (new std::shared_ptr<OC::OCResource>(resource));
62 std::cout << "Resource Found !! >> " << path << std::endl;
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 + "/oc/core?rt=SoftSensorManager.Sensor",
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 OC::OCPlatform::OCPresenceHandle presenceHandle = nullptr;
122 ret = OC::OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=SoftSensorManager.Sensor",
123 std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
125 if (ret != OC_STACK_OK)
126 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
128 ret = OC::OCPlatform::subscribePresence(presenceHandle, "coap://224.0.1.187",
129 "SoftSensorManager.Sensor",
130 std::bind(&CResourceFinder::presenceHandler, this, std::placeholders::_1,
131 std::placeholders::_2, std::placeholders::_3));
133 if (ret != OC_STACK_OK)
134 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
142 SSMRESULT CResourceFinder::startObserveResource(IN ISSMResource *pSensor, IN IEvent *pEvent)
144 return m_mapResourceHandler[pSensor->name]->startObserve(pEvent);
147 SSMRESULT CResourceFinder::stopObserveResource(IN ISSMResource *pSensor)
149 return m_mapResourceHandler[pSensor->name]->stopObserve();
152 void CResourceFinder::onExecute(IN void *pArg)
154 SSMRESULT res = SSM_E_FAIL;
155 OCStackResult ret = OC_STACK_ERROR;
156 OC::QueryParamsMap queryParams;
157 OICResourceHandler *pResourceHandler = NULL;
158 intptr_t *pMessage = reinterpret_cast<intptr_t *>(pArg);
159 std::shared_ptr< OC::OCResource > *pResource = NULL;
160 OC::OCPlatform::OCPresenceHandle presenceHandle = NULL;
162 std::string resourceHostAddress = "";
163 std::string resourceFullPath = "";
167 case RESOURCE_DISCOVER_REQUESTPROFILE:
168 pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
169 pResourceHandler = new OICResourceHandler();
170 SSM_CLEANUP_ASSERT(pResourceHandler->initHandler(*pResource, this));
172 resourceFullPath = pResource->get()->host() + pResource->get()->uri();
174 resourceHostAddress = pResource->get()->host();
175 resourceHostAddress.erase(0, 7); // erase 'coap://'
177 m_mapResourceHandler[resourceFullPath] = pResourceHandler;
179 m_mapResources[resourceHostAddress].push_back(resourceFullPath);
181 ret = pResource->get()->get(queryParams,
182 std::bind(&OICResourceHandler::onGetResourceProfile, pResourceHandler,
183 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
185 if (ret != OC_STACK_OK)
186 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
190 case RESOURCE_DISCOVER_INSTALL_RESOURCE:
191 if (m_mapResourcePresenceHandles.find(((ISSMResource *)pMessage[1])->ip) ==
192 m_mapResourcePresenceHandles.end())
194 ret = OC::OCPlatform::subscribePresence(presenceHandle, ((ISSMResource *)pMessage[1])->ip,
195 "SoftSensorManager.Sensor",
196 std::bind(&CResourceFinder::presenceHandler, this, std::placeholders::_1,
197 std::placeholders::_2, std::placeholders::_3));
199 if (ret != OC_STACK_OK)
200 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
202 m_mapResourcePresenceHandles[((ISSMResource *)pMessage[1])->ip] = presenceHandle;
205 m_pResourceFinderEvent->onResourceFound((ISSMResource *) pMessage[1]);
208 case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
209 m_pResourceFinderEvent->onResourceLost(&((OICResourceHandler *) pMessage[1])->m_SSMResource);
211 if (m_mapResourcePresenceHandles.find(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip) !=
212 m_mapResourcePresenceHandles.end())
214 ret = OC::OCPlatform::unsubscribePresence(
215 m_mapResourcePresenceHandles[((OICResourceHandler *)pMessage[1])->m_SSMResource.ip]);
217 if (ret != OC_STACK_OK)
218 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
220 m_mapResourcePresenceHandles.erase(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip);
223 m_mapResourceHandler.erase(((OICResourceHandler *) pMessage[1])->m_SSMResource.name);
231 void CResourceFinder::onTerminate(IN void *pArg)
233 std::shared_ptr< OC::OCResource > *pResource = NULL;
234 intptr_t *pMessage = (intptr_t *)pArg;
238 case RESOURCE_DISCOVER_REQUESTPROFILE:
239 pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
243 case RESOURCE_DISCOVER_INSTALL_RESOURCE:
246 case RESOURCE_DISCOVER_UNINSTALL_RESOURCE: