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