Merge "[SSM] Reflect current OIC api style and add Tizen Inproc Sample"
[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         std::string path = resource->host() + resource->uri();
58
59         m_pTasker->addTask(this, pMessage);
60     }
61 }
62
63 SSMRESULT CResourceFinder::startResourceFinder()
64 {
65     OCStackResult res = OC_STACK_ERROR;
66
67     res = OC::OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=SoftSensorManager.Sensor",
68                                        std::bind(&CResourceFinder::onResourceFound, this, std::placeholders::_1));
69
70     if (res != OC_STACK_OK)
71         return SSM_E_FAIL;
72
73     return SSM_S_OK;
74 }
75
76 SSMRESULT CResourceFinder::startObserveResource(IN ISSMResource *pSensor, IN IEvent *pEvent)
77 {
78     return m_mapResourceHandler[pSensor->name]->startObserve(pEvent);
79 }
80
81 SSMRESULT CResourceFinder::stopObserveResource(IN ISSMResource *pSensor)
82 {
83     return m_mapResourceHandler[pSensor->name]->stopObserve();
84 }
85
86 void CResourceFinder::onExecute(IN void *pArg)
87 {
88     SSMRESULT res = SSM_E_FAIL;
89     OC::QueryParamsMap  queryParams;
90     OICResourceHandler  *pResourceHandler = NULL;
91     int                 *pMessage = (int *)pArg;
92     std::shared_ptr<OC::OCResource> *pResource = NULL;
93
94     switch (pMessage[0])
95     {
96         case RESOURCE_DISCOVER_REQUESTPROFILE:
97             pResource = (std::shared_ptr<OC::OCResource> *)pMessage[1];
98             pResourceHandler = new OICResourceHandler();
99             SSM_CLEANUP_ASSERT(pResourceHandler->initHandler(*pResource, this));
100             m_mapResourceHandler[pResource->get()->host() + pResource->get()->uri()] = pResourceHandler;
101             pResource->get()->get(queryParams, std::bind(&OICResourceHandler::onGetResourceProfile,
102                                   pResourceHandler, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
103             break;
104
105         case RESOURCE_DISCOVER_SETUP_RESOURCE:
106             m_pResourceFinderEvent->onResourceFound((ISSMResource *)pMessage[1]);
107             break;
108     }
109
110 CLEANUP:
111     ;
112 }
113
114 void CResourceFinder::onTerminate(IN void *pArg)
115 {
116     std::shared_ptr<OC::OCResource>     *pResource = NULL;
117     int                 *pMessage = (int *)pArg;
118
119     switch (pMessage[0])
120     {
121         case RESOURCE_DISCOVER_REQUESTPROFILE:
122             pResource = (std::shared_ptr<OC::OCResource> *)pMessage[1];
123             delete pResource;
124             break;
125
126         case RESOURCE_DISCOVER_SETUP_RESOURCE:
127             break;
128     }
129
130     delete[] pMessage;
131 }