Soft Sensor Manager - Apply changed C.A. API
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SSMCore / src / SensorProcessor / ResourceFinder.cpp
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20 #include "ResourceFinder.h"
21
22 SSMRESULT CResourceFinder::finalConstruct()
23 {
24     SSMRESULT res = SSM_E_FAIL;
25
26     OC::PlatformConfig cfg(OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0,
27                            OC::QualityOfService::LowQos);
28
29     SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_ITasker, (IBase **)&m_pTasker));
30
31     OC::OCPlatform::Configure(cfg);
32
33     m_pResourceFinderEvent = NULL;
34
35     m_multicastPresenceHandle = nullptr;
36
37 CLEANUP:
38     return res;
39 }
40
41 void CResourceFinder::finalRelease()
42 {
43 }
44
45 SSMRESULT CResourceFinder::registerResourceFinderEvent(IN IResourceFinderEvent *pEvent)
46 {
47     m_pResourceFinderEvent = pEvent;
48     return SSM_S_OK;
49 }
50
51 void CResourceFinder::onResourceFound(std::shared_ptr< OC::OCResource > resource)
52 {
53     if (resource)
54     {
55         std::string path = resource->host() + resource->uri();
56
57         if (m_mapResourceHandler.find(path) != m_mapResourceHandler.end())
58             return;
59
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));
63
64         m_pTasker->addTask(this, pMessage);
65     }
66 }
67
68 void CResourceFinder::presenceHandler(OCStackResult result, const unsigned int nonce,
69                                       const std::string &hostAddress)
70 {
71     SSMRESULT res = SSM_E_FAIL;
72     OCStackResult ret = OC_STACK_ERROR;
73     intptr_t *pMessage = NULL;
74
75     std::ostringstream requestURI;
76
77     switch (result)
78     {
79         case OC_STACK_OK:
80             requestURI << "coap://" << hostAddress << ":" << OC_MULTICAST_PORT <<
81                        "/oc/core?rt=SoftSensorManager.Sensor";
82
83             ret = OC::OCPlatform::findResource("", requestURI.str(), OC_ETHERNET,
84                                                std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
85
86             if (ret != OC_STACK_OK)
87                 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
88
89             ret = OC::OCPlatform::findResource("", requestURI.str(), OC_WIFI,
90                                                std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
91
92             if (ret != OC_STACK_OK)
93                 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
94
95             break;
96
97         case OC_STACK_PRESENCE_STOPPED:
98         case OC_STACK_PRESENCE_TIMEOUT:
99             if (m_mapResources.find(hostAddress) != m_mapResources.end())
100             {
101                 while (!m_mapResources[hostAddress].empty())
102                 {
103                     pMessage = new intptr_t[2];
104                     pMessage[0] = RESOURCE_DISCOVER_UNINSTALL_RESOURCE;
105                     pMessage[1] = reinterpret_cast<intptr_t> (m_mapResourceHandler[m_mapResources[hostAddress].back()]);
106                     m_mapResources[hostAddress].pop_back();
107                     m_pTasker->addTask(this, pMessage);
108                 }
109
110                 m_mapResources.erase(hostAddress);
111             }
112             break;
113
114         case OC_STACK_VIRTUAL_DO_NOT_HANDLE:
115             break;
116
117         default:
118             break;
119     }
120
121 CLEANUP:
122     ;
123 }
124
125 SSMRESULT CResourceFinder::startResourceFinder()
126 {
127     SSMRESULT res = SSM_E_FAIL;
128     OCStackResult ret = OC_STACK_ERROR;
129
130     std::ostringstream requestURI;
131     requestURI << OC_WELL_KNOWN_QUERY << "?rt=SoftSensorManager.Sensor";
132
133     ret = OC::OCPlatform::findResource("", requestURI.str(), OC_ETHERNET,
134                                        std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
135
136     if (ret != OC_STACK_OK)
137         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
138
139     ret = OC::OCPlatform::findResource("", requestURI.str(), OC_WIFI,
140                                        std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
141
142     if (ret != OC_STACK_OK)
143         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
144
145     ret = OC::OCPlatform::subscribePresence(m_multicastPresenceHandle, OC_MULTICAST_IP,
146                                             "SoftSensorManager.Sensor", OC_ETHERNET, std::bind(&CResourceFinder::presenceHandler, this,
147                                                     std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
148
149     if (ret != OC_STACK_OK)
150         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
151
152     ret = OC::OCPlatform::subscribePresence(m_multicastPresenceHandle, OC_MULTICAST_IP,
153                                             "SoftSensorManager.Sensor", OC_WIFI, std::bind(&CResourceFinder::presenceHandler, this,
154                                                     std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
155
156     if (ret != OC_STACK_OK)
157         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
158
159     res = SSM_S_OK;
160
161 CLEANUP:
162     return res;
163 }
164
165 SSMRESULT CResourceFinder::stopResourceFinder()
166 {
167     SSMRESULT res = SSM_E_FAIL;
168     OCStackResult ret = OC_STACK_ERROR;
169
170     ret = OC::OCPlatform::unsubscribePresence(m_multicastPresenceHandle);
171
172     if (ret != OC_STACK_OK)
173         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
174
175     m_multicastPresenceHandle = nullptr;
176
177     res = SSM_S_OK;
178
179 CLEANUP:
180     return res;
181 }
182
183 SSMRESULT CResourceFinder::startObserveResource(IN ISSMResource *pSensor, IN IEvent *pEvent)
184 {
185     return m_mapResourceHandler[pSensor->name]->startObserve(pEvent);
186 }
187
188 SSMRESULT CResourceFinder::stopObserveResource(IN ISSMResource *pSensor)
189 {
190     return m_mapResourceHandler[pSensor->name]->stopObserve();
191 }
192
193 void CResourceFinder::onExecute(IN void *pArg)
194 {
195     SSMRESULT           res = SSM_E_FAIL;
196     OCStackResult       ret = OC_STACK_ERROR;
197     OC::QueryParamsMap  queryParams;
198     OICResourceHandler *pResourceHandler = NULL;
199     intptr_t           *pMessage =  reinterpret_cast<intptr_t *>(pArg);
200     std::shared_ptr< OC::OCResource > *pResource = NULL;
201     OC::OCPlatform::OCPresenceHandle   presenceHandle = NULL;
202
203     std::string resourceHostAddress = "";
204     std::string resourceFullPath = "";
205
206     switch (pMessage[0])
207     {
208         case RESOURCE_DISCOVER_REQUESTPROFILE:
209             pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
210             pResourceHandler = new OICResourceHandler();
211             SSM_CLEANUP_ASSERT(pResourceHandler->initHandler(*pResource, this));
212
213             resourceFullPath = pResource->get()->host() + pResource->get()->uri();
214
215             resourceHostAddress = pResource->get()->host();
216             resourceHostAddress.erase(0, 7);        // erase 'coap://'
217
218             m_mapResourceHandler[resourceFullPath] = pResourceHandler;
219
220             m_mapResources[resourceHostAddress].push_back(resourceFullPath);
221
222             ret = pResource->get()->get(queryParams, std::bind(&OICResourceHandler::onGetResourceProfile,
223                                         pResourceHandler, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
224
225             if (ret != OC_STACK_OK)
226                 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
227
228             break;
229
230         case RESOURCE_DISCOVER_INSTALL_RESOURCE:
231             if (m_mapResourcePresenceHandles.find(((ISSMResource *)pMessage[1])->ip) ==
232                 m_mapResourcePresenceHandles.end())
233             {
234                 ret = OC::OCPlatform::subscribePresence(presenceHandle, ((ISSMResource *)pMessage[1])->ip,
235                                                         "SoftSensorManager.Sensor", OC_ETHERNET, std::bind(&CResourceFinder::presenceHandler, this,
236                                                                 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
237
238                 if (ret != OC_STACK_OK)
239                     SSM_CLEANUP_ASSERT(SSM_E_FAIL);
240
241                 ret = OC::OCPlatform::subscribePresence(presenceHandle, ((ISSMResource *)pMessage[1])->ip,
242                                                         "SoftSensorManager.Sensor", OC_WIFI, std::bind(&CResourceFinder::presenceHandler, this,
243                                                                 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
244
245                 if (ret != OC_STACK_OK)
246                     SSM_CLEANUP_ASSERT(SSM_E_FAIL);
247
248                 m_mapResourcePresenceHandles[((ISSMResource *)pMessage[1])->ip] = presenceHandle;
249             }
250
251             m_pResourceFinderEvent->onResourceFound((ISSMResource *) pMessage[1]);
252             break;
253
254         case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
255             m_pResourceFinderEvent->onResourceLost(&((OICResourceHandler *) pMessage[1])->m_SSMResource);
256
257             if (m_mapResourcePresenceHandles.find(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip) !=
258                 m_mapResourcePresenceHandles.end())
259             {
260                 ret = OC::OCPlatform::unsubscribePresence(m_mapResourcePresenceHandles[((
261                             OICResourceHandler *)pMessage[1])->m_SSMResource.ip]);
262
263                 if (ret != OC_STACK_OK)
264                     SSM_CLEANUP_ASSERT(SSM_E_FAIL);
265
266                 m_mapResourcePresenceHandles.erase(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip);
267             }
268
269             m_mapResourceHandler.erase(((OICResourceHandler *) pMessage[1])->m_SSMResource.name);
270             break;
271     }
272
273 CLEANUP:
274     ;
275 }
276
277 void CResourceFinder::onTerminate(IN void *pArg)
278 {
279     std::shared_ptr< OC::OCResource > *pResource = NULL;
280     intptr_t *pMessage = (intptr_t *)pArg;
281
282     switch (pMessage[0])
283     {
284         case RESOURCE_DISCOVER_REQUESTPROFILE:
285             pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
286             delete pResource;
287             break;
288
289         case RESOURCE_DISCOVER_INSTALL_RESOURCE:
290             break;
291
292         case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
293             break;
294     }
295
296     delete[] pMessage;
297 }