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 OCConnectivityType g_connectivityType = CT_DEFAULT;
24 SSMRESULT CResourceFinder::finalConstruct()
26 SSMRESULT res = SSM_E_FAIL;
28 OC::PlatformConfig cfg(OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0,
29 OC::QualityOfService::LowQos);
31 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_ITasker, (IBase **)&m_pTasker));
33 OC::OCPlatform::Configure(cfg);
35 m_pResourceFinderEvent = NULL;
37 m_multicastPresenceHandle = nullptr;
43 void CResourceFinder::finalRelease()
47 SSMRESULT CResourceFinder::registerResourceFinderEvent(IResourceFinderEvent *pEvent)
49 m_pResourceFinderEvent = pEvent;
53 void CResourceFinder::onResourceFound(std::shared_ptr< OC::OCResource > resource)
57 std::string path = resource->host() + resource->uri();
59 if (m_mapResourceHandler.find(path) != m_mapResourceHandler.end())
62 intptr_t *pMessage = new intptr_t [2];
63 pMessage[0] = RESOURCE_DISCOVER_REQUESTPROFILE;
64 pMessage[1] = reinterpret_cast<intptr_t> (new std::shared_ptr<OC::OCResource>(resource));
66 m_pTasker->addTask(this, pMessage);
70 void CResourceFinder::presenceHandler(OCStackResult result, const unsigned int nonce,
71 const std::string &hostAddress)
73 SSMRESULT res = SSM_E_FAIL;
74 OCStackResult ret = OC_STACK_ERROR;
75 intptr_t *pMessage = NULL;
76 std::ostringstream requestURI;
81 requestURI << "coap://" << hostAddress << OC_RSRVD_WELL_KNOWN_URI << "?rt=SSManager.Sensor";
83 ret = OC::OCPlatform::findResource("", requestURI.str(), g_connectivityType,
84 std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
86 if (ret != OC_STACK_OK)
87 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
91 case OC_STACK_PRESENCE_STOPPED:
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_PRESENCE_TIMEOUT:
110 case OC_STACK_PRESENCE_DO_NOT_HANDLE:
121 SSMRESULT CResourceFinder::startResourceFinder()
123 SSMRESULT res = SSM_E_FAIL;
124 OCStackResult ret = OC_STACK_ERROR;
126 std::ostringstream requestURI;
127 requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=SSManager.Sensor";
129 std::ostringstream multicastPresenceURI;
130 multicastPresenceURI << "coap://" << OC_MULTICAST_PREFIX;
132 ret = OC::OCPlatform::findResource("", requestURI.str(), g_connectivityType,
133 std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
135 if (ret != OC_STACK_OK)
136 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
138 ret = OC::OCPlatform::subscribePresence(m_multicastPresenceHandle, multicastPresenceURI.str(),
139 "SSManager.Sensor", g_connectivityType, std::bind(&CResourceFinder::presenceHandler, this,
140 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
142 if (ret != OC_STACK_OK)
143 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
151 SSMRESULT CResourceFinder::stopResourceFinder()
153 SSMRESULT res = SSM_E_FAIL;
154 OCStackResult ret = OC_STACK_ERROR;
156 ret = OC::OCPlatform::unsubscribePresence(m_multicastPresenceHandle);
158 if (ret != OC_STACK_OK)
159 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
161 m_multicastPresenceHandle = nullptr;
169 SSMRESULT CResourceFinder::startObserveResource(ISSMResource *pSensor, IEvent *pEvent)
171 return m_mapResourceHandler[pSensor->name]->startObserve(pEvent);
174 SSMRESULT CResourceFinder::stopObserveResource(ISSMResource *pSensor)
176 return m_mapResourceHandler[pSensor->name]->stopObserve();
179 void CResourceFinder::onExecute(void *pArg)
181 SSMRESULT res = SSM_E_FAIL;
182 OCStackResult ret = OC_STACK_ERROR;
183 OC::QueryParamsMap queryParams;
184 OICResourceHandler *pResourceHandler = NULL;
185 intptr_t *pMessage = reinterpret_cast<intptr_t *>(pArg);
186 std::shared_ptr< OC::OCResource > *pResource = NULL;
187 OC::OCPlatform::OCPresenceHandle presenceHandle = NULL;
189 std::string resourceHostAddress = "";
190 std::string resourceFullPath = "";
194 case RESOURCE_DISCOVER_REQUESTPROFILE:
195 pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
196 pResourceHandler = new OICResourceHandler();
198 res = pResourceHandler->initHandler(*pResource, this);
201 SAFE_DELETE(pResourceHandler);
202 SSM_CLEANUP_ASSERT(res);
205 resourceFullPath = pResource->get()->host() + pResource->get()->uri();
207 resourceHostAddress = pResource->get()->host();
208 resourceHostAddress.erase(0, 7); // erase 'coap://'
210 m_mapResourceHandler[resourceFullPath] = pResourceHandler;
212 m_mapResources[resourceHostAddress].push_back(resourceFullPath);
214 ret = pResource->get()->get(queryParams, std::bind(&OICResourceHandler::onGetResourceProfile,
215 pResourceHandler, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
217 if (ret != OC_STACK_OK)
218 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
222 case RESOURCE_DISCOVER_INSTALL_RESOURCE:
223 if (m_mapResourcePresenceHandles.find(((ISSMResource *)pMessage[1])->ip) ==
224 m_mapResourcePresenceHandles.end())
226 ret = OC::OCPlatform::subscribePresence(presenceHandle, ((ISSMResource *)pMessage[1])->ip,
227 "SSManager.Sensor", (OCConnectivityType)(((ISSMResource *)pMessage[1])->connectivityType),
228 std::bind(&CResourceFinder::presenceHandler, this, std::placeholders::_1, std::placeholders::_2,
229 std::placeholders::_3));
231 if (ret != OC_STACK_OK)
232 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
234 m_mapResourcePresenceHandles[((ISSMResource *)pMessage[1])->ip] = presenceHandle;
237 m_pResourceFinderEvent->onResourceFound((ISSMResource *)pMessage[1]);
241 case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
242 m_pResourceFinderEvent->onResourceLost(&((OICResourceHandler *) pMessage[1])->m_SSMResource);
244 if (m_mapResourcePresenceHandles.find(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip) !=
245 m_mapResourcePresenceHandles.end())
247 ret = OC::OCPlatform::unsubscribePresence(m_mapResourcePresenceHandles[((
248 OICResourceHandler *)pMessage[1])->m_SSMResource.ip]);
250 if (ret != OC_STACK_OK)
251 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
253 m_mapResourcePresenceHandles.erase(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip);
256 delete m_mapResourceHandler[((OICResourceHandler *)pMessage[1])->m_SSMResource.name];
257 m_mapResourceHandler.erase(((OICResourceHandler *) pMessage[1])->m_SSMResource.name);
265 void CResourceFinder::onTerminate(void *pArg)
267 std::shared_ptr< OC::OCResource > *pResource = NULL;
268 intptr_t *pMessage = (intptr_t *)pArg;
272 case RESOURCE_DISCOVER_REQUESTPROFILE:
273 pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
277 case RESOURCE_DISCOVER_INSTALL_RESOURCE:
280 case RESOURCE_DISCOVER_UNINSTALL_RESOURCE: