[SSM] Change to make SSM work with Connectivity Abstraction
[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     switch (result)
76     {
77         case OC_STACK_OK:
78 #ifdef CA_INT
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));
82 #else
83             ret = OC::OCPlatform::findResource("",
84                                                "coap://" + hostAddress + "/oc/core?rt=SoftSensorManager.Sensor",
85                                                std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
86 #endif
87
88             if (ret != OC_STACK_OK)
89                 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
90
91             break;
92
93         case OC_STACK_PRESENCE_STOPPED:
94         case OC_STACK_PRESENCE_TIMEOUT:
95             if (m_mapResources.find(hostAddress) != m_mapResources.end())
96             {
97                 while (!m_mapResources[hostAddress].empty())
98                 {
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);
104                 }
105
106                 m_mapResources.erase(hostAddress);
107             }
108             break;
109
110         case OC_STACK_VIRTUAL_DO_NOT_HANDLE:
111             break;
112
113         default:
114             break;
115     }
116
117 CLEANUP:
118     ;
119 }
120
121 SSMRESULT CResourceFinder::startResourceFinder()
122 {
123     SSMRESULT res = SSM_E_FAIL;
124     OCStackResult ret = OC_STACK_ERROR;
125
126 #ifdef CA_INT
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));
130 #else
131     ret = OC::OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=SoftSensorManager.Sensor",
132                                        std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
133 #endif
134
135     if (ret != OC_STACK_OK)
136         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
137
138 #ifdef CA_INT
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));
143 #else
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));
148 #endif
149
150     if (ret != OC_STACK_OK)
151         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
152
153     res = SSM_S_OK;
154
155 CLEANUP:
156     return res;
157 }
158
159 SSMRESULT CResourceFinder::stopResourceFinder()
160 {
161     SSMRESULT res = SSM_E_FAIL;
162     OCStackResult ret = OC_STACK_ERROR;
163
164     ret = OC::OCPlatform::unsubscribePresence(m_multicastPresenceHandle);
165
166     if (ret != OC_STACK_OK)
167         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
168
169     m_multicastPresenceHandle = nullptr;
170
171     res = SSM_S_OK;
172
173 CLEANUP:
174     return res;
175 }
176
177 SSMRESULT CResourceFinder::startObserveResource(IN ISSMResource *pSensor, IN IEvent *pEvent)
178 {
179     return m_mapResourceHandler[pSensor->name]->startObserve(pEvent);
180 }
181
182 SSMRESULT CResourceFinder::stopObserveResource(IN ISSMResource *pSensor)
183 {
184     return m_mapResourceHandler[pSensor->name]->stopObserve();
185 }
186
187 void CResourceFinder::onExecute(IN void *pArg)
188 {
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;
196
197     std::string resourceHostAddress = "";
198     std::string resourceFullPath = "";
199
200     switch (pMessage[0])
201     {
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));
206
207             resourceFullPath = pResource->get()->host() + pResource->get()->uri();
208
209             resourceHostAddress = pResource->get()->host();
210             resourceHostAddress.erase(0, 7);        // erase 'coap://'
211
212             m_mapResourceHandler[resourceFullPath] = pResourceHandler;
213
214             m_mapResources[resourceHostAddress].push_back(resourceFullPath);
215
216             ret = pResource->get()->get(queryParams,
217                                         std::bind(&OICResourceHandler::onGetResourceProfile, pResourceHandler,
218                                                   std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
219
220             if (ret != OC_STACK_OK)
221                 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
222
223             break;
224
225         case RESOURCE_DISCOVER_INSTALL_RESOURCE:
226             if (m_mapResourcePresenceHandles.find(((ISSMResource *)pMessage[1])->ip) ==
227                 m_mapResourcePresenceHandles.end())
228             {
229 #ifdef CA_INT
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));
234 #else
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));
239 #endif
240
241                 if (ret != OC_STACK_OK)
242                     SSM_CLEANUP_ASSERT(SSM_E_FAIL);
243
244                 m_mapResourcePresenceHandles[((ISSMResource *)pMessage[1])->ip] = presenceHandle;
245             }
246
247             m_pResourceFinderEvent->onResourceFound((ISSMResource *) pMessage[1]);
248             break;
249
250         case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
251             m_pResourceFinderEvent->onResourceLost(&((OICResourceHandler *) pMessage[1])->m_SSMResource);
252
253             if (m_mapResourcePresenceHandles.find(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip) !=
254                 m_mapResourcePresenceHandles.end())
255             {
256                 ret = OC::OCPlatform::unsubscribePresence(
257                           m_mapResourcePresenceHandles[((OICResourceHandler *)pMessage[1])->m_SSMResource.ip]);
258
259                 if (ret != OC_STACK_OK)
260                     SSM_CLEANUP_ASSERT(SSM_E_FAIL);
261
262                 m_mapResourcePresenceHandles.erase(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip);
263             }
264
265             m_mapResourceHandler.erase(((OICResourceHandler *) pMessage[1])->m_SSMResource.name);
266             break;
267     }
268
269 CLEANUP:
270     ;
271 }
272
273 void CResourceFinder::onTerminate(IN void *pArg)
274 {
275     std::shared_ptr< OC::OCResource > *pResource = NULL;
276     intptr_t *pMessage = (intptr_t *)pArg;
277
278     switch (pMessage[0])
279     {
280         case RESOURCE_DISCOVER_REQUESTPROFILE:
281             pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
282             delete pResource;
283             break;
284
285         case RESOURCE_DISCOVER_INSTALL_RESOURCE:
286             break;
287
288         case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
289             break;
290     }
291
292     delete[] pMessage;
293 }