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