Support HighQoS and Cancel Observe in CA and RI
[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         default:
115             break;
116     }
117
118 CLEANUP:
119     ;
120 }
121
122 SSMRESULT CResourceFinder::startResourceFinder()
123 {
124     SSMRESULT res = SSM_E_FAIL;
125     OCStackResult ret = OC_STACK_ERROR;
126
127     std::ostringstream requestURI;
128     requestURI << OC_WELL_KNOWN_QUERY << "?rt=SoftSensorManager.Sensor";
129
130     ret = OC::OCPlatform::findResource("", requestURI.str(), OC_ETHERNET,
131                                        std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
132
133     if (ret != OC_STACK_OK)
134         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
135
136     ret = OC::OCPlatform::findResource("", requestURI.str(), OC_WIFI,
137                                        std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
138
139     if (ret != OC_STACK_OK)
140         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
141
142     ret = OC::OCPlatform::subscribePresence(m_multicastPresenceHandle, OC_MULTICAST_IP,
143                                             "SoftSensorManager.Sensor", OC_ETHERNET, std::bind(&CResourceFinder::presenceHandler, this,
144                                                     std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
145
146     if (ret != OC_STACK_OK)
147         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
148
149     ret = OC::OCPlatform::subscribePresence(m_multicastPresenceHandle, OC_MULTICAST_IP,
150                                             "SoftSensorManager.Sensor", OC_WIFI, std::bind(&CResourceFinder::presenceHandler, this,
151                                                     std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
152
153     if (ret != OC_STACK_OK)
154         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
155
156     res = SSM_S_OK;
157
158 CLEANUP:
159     return res;
160 }
161
162 SSMRESULT CResourceFinder::stopResourceFinder()
163 {
164     SSMRESULT res = SSM_E_FAIL;
165     OCStackResult ret = OC_STACK_ERROR;
166
167     ret = OC::OCPlatform::unsubscribePresence(m_multicastPresenceHandle);
168
169     if (ret != OC_STACK_OK)
170         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
171
172     m_multicastPresenceHandle = nullptr;
173
174     res = SSM_S_OK;
175
176 CLEANUP:
177     return res;
178 }
179
180 SSMRESULT CResourceFinder::startObserveResource(IN ISSMResource *pSensor, IN IEvent *pEvent)
181 {
182     return m_mapResourceHandler[pSensor->name]->startObserve(pEvent);
183 }
184
185 SSMRESULT CResourceFinder::stopObserveResource(IN ISSMResource *pSensor)
186 {
187     return m_mapResourceHandler[pSensor->name]->stopObserve();
188 }
189
190 void CResourceFinder::onExecute(IN void *pArg)
191 {
192     SSMRESULT           res = SSM_E_FAIL;
193     OCStackResult       ret = OC_STACK_ERROR;
194     OC::QueryParamsMap  queryParams;
195     OICResourceHandler *pResourceHandler = NULL;
196     intptr_t           *pMessage =  reinterpret_cast<intptr_t *>(pArg);
197     std::shared_ptr< OC::OCResource > *pResource = NULL;
198     OC::OCPlatform::OCPresenceHandle   presenceHandle = NULL;
199
200     std::string resourceHostAddress = "";
201     std::string resourceFullPath = "";
202
203     switch (pMessage[0])
204     {
205         case RESOURCE_DISCOVER_REQUESTPROFILE:
206             pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
207             pResourceHandler = new OICResourceHandler();
208             SSM_CLEANUP_ASSERT(pResourceHandler->initHandler(*pResource, this));
209
210             resourceFullPath = pResource->get()->host() + pResource->get()->uri();
211
212             resourceHostAddress = pResource->get()->host();
213             resourceHostAddress.erase(0, 7);        // erase 'coap://'
214
215             m_mapResourceHandler[resourceFullPath] = pResourceHandler;
216
217             m_mapResources[resourceHostAddress].push_back(resourceFullPath);
218
219             ret = pResource->get()->get(queryParams, std::bind(&OICResourceHandler::onGetResourceProfile,
220                                         pResourceHandler, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
221
222             if (ret != OC_STACK_OK)
223                 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
224
225             break;
226
227         case RESOURCE_DISCOVER_INSTALL_RESOURCE:
228             if (m_mapResourcePresenceHandles.find(((ISSMResource *)pMessage[1])->ip) ==
229                 m_mapResourcePresenceHandles.end())
230             {
231                 ret = OC::OCPlatform::subscribePresence(presenceHandle, ((ISSMResource *)pMessage[1])->ip,
232                                                         "SoftSensorManager.Sensor", OC_ETHERNET, std::bind(&CResourceFinder::presenceHandler, this,
233                                                                 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
234
235                 if (ret != OC_STACK_OK)
236                     SSM_CLEANUP_ASSERT(SSM_E_FAIL);
237
238                 ret = OC::OCPlatform::subscribePresence(presenceHandle, ((ISSMResource *)pMessage[1])->ip,
239                                                         "SoftSensorManager.Sensor", OC_WIFI, std::bind(&CResourceFinder::presenceHandler, this,
240                                                                 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
241
242                 if (ret != OC_STACK_OK)
243                     SSM_CLEANUP_ASSERT(SSM_E_FAIL);
244
245                 m_mapResourcePresenceHandles[((ISSMResource *)pMessage[1])->ip] = presenceHandle;
246             }
247
248             m_pResourceFinderEvent->onResourceFound((ISSMResource *) pMessage[1]);
249             break;
250
251         case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
252             m_pResourceFinderEvent->onResourceLost(&((OICResourceHandler *) pMessage[1])->m_SSMResource);
253
254             if (m_mapResourcePresenceHandles.find(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip) !=
255                 m_mapResourcePresenceHandles.end())
256             {
257                 ret = OC::OCPlatform::unsubscribePresence(m_mapResourcePresenceHandles[((
258                             OICResourceHandler *)pMessage[1])->m_SSMResource.ip]);
259
260                 if (ret != OC_STACK_OK)
261                     SSM_CLEANUP_ASSERT(SSM_E_FAIL);
262
263                 m_mapResourcePresenceHandles.erase(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip);
264             }
265
266             m_mapResourceHandler.erase(((OICResourceHandler *) pMessage[1])->m_SSMResource.name);
267             break;
268     }
269
270 CLEANUP:
271     ;
272 }
273
274 void CResourceFinder::onTerminate(IN void *pArg)
275 {
276     std::shared_ptr< OC::OCResource > *pResource = NULL;
277     intptr_t *pMessage = (intptr_t *)pArg;
278
279     switch (pMessage[0])
280     {
281         case RESOURCE_DISCOVER_REQUESTPROFILE:
282             pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
283             delete pResource;
284             break;
285
286         case RESOURCE_DISCOVER_INSTALL_RESOURCE:
287             break;
288
289         case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
290             break;
291     }
292
293     delete[] pMessage;
294 }