Merge "Adding PCF() wrappers to OC_LOG() calls that were missing in the stack."
[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     SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_ITasker, (IBase **)&m_pTasker));
27     SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_IResourceConnectivity,
28                                             (IBase **)&m_pResourceConnectivity));
29
30     m_pResourceFinderEvent = NULL;
31     m_pPlatform = (OC::OCPlatform *)m_pResourceConnectivity->getPlatform();
32
33 CLEANUP:
34     return res;
35 }
36
37 void CResourceFinder::finalRelease()
38 {
39 }
40
41 SSMRESULT CResourceFinder::registerResourceFinderEvent(IN IResourceFinderEvent *pEvent)
42 {
43     m_pResourceFinderEvent = pEvent;
44     return SSM_S_OK;
45 }
46
47 void CResourceFinder::onResourceFound(std::shared_ptr<OC::OCResource> resource)
48 {
49     if (resource)
50     {
51         int     *pMessage = new int[2];
52         pMessage[0] = RESOURCE_DISCOVER_REQUESTPROFILE;
53         pMessage[1] = (int)new std::shared_ptr<OC::OCResource>(resource);
54
55         m_pTasker->addTask(this, pMessage);
56     }
57 }
58
59 SSMRESULT CResourceFinder::startResourceFinder()
60 {
61     //m_pPlatform->findResource("", "oc/core/service/SoftSensorManager/SoftSensor",
62     m_pPlatform->findResource("", "coap://224.0.1.187/oc/core?rt=SoftSensorManager.Sensor",
63                               std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
64
65     return SSM_S_OK;
66 }
67
68 SSMRESULT CResourceFinder::startObserveResource(IN ISSMResource *pSensor, IN IEvent *pEvent)
69 {
70     return m_mapResourceHandler[pSensor->name]->startObserve(pEvent);
71 }
72
73 SSMRESULT CResourceFinder::stopObserveResource(IN ISSMResource *pSensor)
74 {
75     return m_mapResourceHandler[pSensor->name]->stopObserve();
76 }
77
78 void CResourceFinder::onExecute(IN void *pArg)
79 {
80     SSMRESULT res = SSM_E_FAIL;
81     OC::QueryParamsMap  queryParams;
82     OICResourceHandler  *pResourceHandler = NULL;
83     int                 *pMessage = (int *)pArg;
84     std::shared_ptr<OC::OCResource> *pResource = NULL;
85
86     switch (pMessage[0])
87     {
88         case RESOURCE_DISCOVER_REQUESTPROFILE:
89             pResource = (std::shared_ptr<OC::OCResource> *)pMessage[1];
90             pResourceHandler = new OICResourceHandler();
91             SSM_CLEANUP_ASSERT(pResourceHandler->initHandler(*pResource, this));
92             m_mapResourceHandler[pResource->get()->host() + pResource->get()->uri()] = pResourceHandler;
93             pResource->get()->get(queryParams, std::bind(&OICResourceHandler::onGetResourceProfile,
94                                   pResourceHandler, std::placeholders::_1, std::placeholders::_2));
95             break;
96
97         case RESOURCE_DISCOVER_SETUP_RESOURCE:
98             m_pResourceFinderEvent->onResourceFound((ISSMResource *)pMessage[1]);
99             break;
100     }
101
102 CLEANUP:
103     ;
104 }
105
106 void CResourceFinder::onTerminate(IN void *pArg)
107 {
108     std::shared_ptr<OC::OCResource>     *pResource = NULL;
109     int                 *pMessage = (int *)pArg;
110
111     switch (pMessage[0])
112     {
113         case RESOURCE_DISCOVER_REQUESTPROFILE:
114             pResource = (std::shared_ptr<OC::OCResource> *)pMessage[1];
115             delete pResource;
116             break;
117
118         case RESOURCE_DISCOVER_SETUP_RESOURCE:
119             break;
120     }
121
122     delete[] pMessage;
123 }