Merge "Fixed seg fault in simpleclient sample app (Bug IOT-130)"
[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             ret = OC::OCPlatform::findResource("",
79                                                "coap://" + hostAddress + "/oc/core?rt=SoftSensorManager.Sensor",
80                                                std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
81
82             if (ret != OC_STACK_OK)
83                 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
84
85             break;
86
87         case OC_STACK_PRESENCE_STOPPED:
88         case OC_STACK_PRESENCE_TIMEOUT:
89             if (m_mapResources.find(hostAddress) != m_mapResources.end())
90             {
91                 while (!m_mapResources[hostAddress].empty())
92                 {
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);
98                 }
99
100                 m_mapResources.erase(hostAddress);
101             }
102             break;
103
104         case OC_STACK_VIRTUAL_DO_NOT_HANDLE:
105             break;
106
107         default:
108             break;
109     }
110
111 CLEANUP:
112     ;
113 }
114
115 SSMRESULT CResourceFinder::startResourceFinder()
116 {
117     SSMRESULT res = SSM_E_FAIL;
118     OCStackResult ret = OC_STACK_ERROR;
119
120     ret = OC::OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=SoftSensorManager.Sensor",
121                                        std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
122
123     if (ret != OC_STACK_OK)
124         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
125
126     ret = OC::OCPlatform::subscribePresence(m_multicastPresenceHandle, "coap://224.0.1.187",
127                                             "SoftSensorManager.Sensor",
128                                             std::bind(&CResourceFinder::presenceHandler, this, std::placeholders::_1,
129                                                     std::placeholders::_2, std::placeholders::_3));
130
131     if (ret != OC_STACK_OK)
132         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
133
134     res = SSM_S_OK;
135
136 CLEANUP:
137     return res;
138 }
139
140 SSMRESULT CResourceFinder::stopResourceFinder()
141 {
142     SSMRESULT res = SSM_E_FAIL;
143     OCStackResult ret = OC_STACK_ERROR;
144
145     ret = OC::OCPlatform::unsubscribePresence(m_multicastPresenceHandle);
146
147     if (ret != OC_STACK_OK)
148         SSM_CLEANUP_ASSERT(SSM_E_FAIL);
149
150     m_multicastPresenceHandle = nullptr;
151
152     res = SSM_S_OK;
153
154 CLEANUP:
155     return res;
156 }
157
158 SSMRESULT CResourceFinder::startObserveResource(IN ISSMResource *pSensor, IN IEvent *pEvent)
159 {
160     return m_mapResourceHandler[pSensor->name]->startObserve(pEvent);
161 }
162
163 SSMRESULT CResourceFinder::stopObserveResource(IN ISSMResource *pSensor)
164 {
165     return m_mapResourceHandler[pSensor->name]->stopObserve();
166 }
167
168 void CResourceFinder::onExecute(IN void *pArg)
169 {
170     SSMRESULT res = SSM_E_FAIL;
171     OCStackResult ret = OC_STACK_ERROR;
172     OC::QueryParamsMap queryParams;
173     OICResourceHandler *pResourceHandler = NULL;
174     intptr_t                 *pMessage =  reinterpret_cast<intptr_t *>(pArg);
175     std::shared_ptr< OC::OCResource > *pResource = NULL;
176     OC::OCPlatform::OCPresenceHandle presenceHandle = NULL;
177
178     std::string resourceHostAddress = "";
179     std::string resourceFullPath = "";
180
181     switch (pMessage[0])
182     {
183         case RESOURCE_DISCOVER_REQUESTPROFILE:
184             pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
185             pResourceHandler = new OICResourceHandler();
186             SSM_CLEANUP_ASSERT(pResourceHandler->initHandler(*pResource, this));
187
188             resourceFullPath = pResource->get()->host() + pResource->get()->uri();
189
190             resourceHostAddress = pResource->get()->host();
191             resourceHostAddress.erase(0, 7);        // erase 'coap://'
192
193             m_mapResourceHandler[resourceFullPath] = pResourceHandler;
194
195             m_mapResources[resourceHostAddress].push_back(resourceFullPath);
196
197             ret = pResource->get()->get(queryParams,
198                                         std::bind(&OICResourceHandler::onGetResourceProfile, pResourceHandler,
199                                                   std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
200
201             if (ret != OC_STACK_OK)
202                 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
203
204             break;
205
206         case RESOURCE_DISCOVER_INSTALL_RESOURCE:
207             if (m_mapResourcePresenceHandles.find(((ISSMResource *)pMessage[1])->ip) ==
208                 m_mapResourcePresenceHandles.end())
209             {
210                 ret = OC::OCPlatform::subscribePresence(presenceHandle, ((ISSMResource *)pMessage[1])->ip,
211                                                         "SoftSensorManager.Sensor",
212                                                         std::bind(&CResourceFinder::presenceHandler, this, std::placeholders::_1,
213                                                                 std::placeholders::_2, std::placeholders::_3));
214
215                 if (ret != OC_STACK_OK)
216                     SSM_CLEANUP_ASSERT(SSM_E_FAIL);
217
218                 m_mapResourcePresenceHandles[((ISSMResource *)pMessage[1])->ip] = presenceHandle;
219             }
220
221             m_pResourceFinderEvent->onResourceFound((ISSMResource *) pMessage[1]);
222             break;
223
224         case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
225             m_pResourceFinderEvent->onResourceLost(&((OICResourceHandler *) pMessage[1])->m_SSMResource);
226
227             if (m_mapResourcePresenceHandles.find(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip) !=
228                 m_mapResourcePresenceHandles.end())
229             {
230                 ret = OC::OCPlatform::unsubscribePresence(
231                           m_mapResourcePresenceHandles[((OICResourceHandler *)pMessage[1])->m_SSMResource.ip]);
232
233                 if (ret != OC_STACK_OK)
234                     SSM_CLEANUP_ASSERT(SSM_E_FAIL);
235
236                 m_mapResourcePresenceHandles.erase(((OICResourceHandler *)pMessage[1])->m_SSMResource.ip);
237             }
238
239             m_mapResourceHandler.erase(((OICResourceHandler *) pMessage[1])->m_SSMResource.name);
240             break;
241     }
242
243 CLEANUP:
244     ;
245 }
246
247 void CResourceFinder::onTerminate(IN void *pArg)
248 {
249     std::shared_ptr< OC::OCResource > *pResource = NULL;
250     intptr_t *pMessage = (intptr_t *)pArg;
251
252     switch (pMessage[0])
253     {
254         case RESOURCE_DISCOVER_REQUESTPROFILE:
255             pResource = (std::shared_ptr< OC::OCResource > *) pMessage[1];
256             delete pResource;
257             break;
258
259         case RESOURCE_DISCOVER_INSTALL_RESOURCE:
260             break;
261
262         case RESOURCE_DISCOVER_UNINSTALL_RESOURCE:
263             break;
264     }
265
266     delete[] pMessage;
267 }