1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
20 #ifndef _ResourceFinder_H_
21 #define _ResourceFinder_H_
23 #include "SSMInterface/SSMCore.h"
24 #include "Common/PlatformLayer.h"
25 #include "Common/InternalInterface.h"
26 #include "OCPlatform.h"
29 class CResourceFinder: public CObjectRoot< CObjectMultiThreadModel >,
30 public IResourceFinder,
34 SSMRESULT queryInterface(const OID &objectID, IBase **ppObject)
39 if (IsEqualOID(objectID, OID_IResourceFinder))
41 IBase *pBase = (IResourceFinder *) this;
47 return SSM_E_NOINTERFACE;
50 SSMRESULT finalConstruct();
53 SSMRESULT registerResourceFinderEvent(IResourceFinderEvent *pEvent);
54 void onResourceFound(std::shared_ptr< OC::OCResource > resource);
55 void presenceHandler(OCStackResult result, const unsigned int nonce,
56 const std::string &hostAddress);
57 SSMRESULT startResourceFinder();
58 SSMRESULT stopResourceFinder();
60 SSMRESULT startObserveResource(ISSMResource *pSensor, IEvent *pEvent);
61 SSMRESULT stopObserveResource(ISSMResource *pSensor);
63 void onExecute(void *pArg);
64 void onTerminate(void *pArg);
67 class OICResourceHandler
70 ISSMResource m_SSMResource;
75 m_pResourceFinderClient = NULL;
78 SSMRESULT initHandler(std::shared_ptr< OC::OCResource > resource,
79 IThreadClient *pThreadClient)
81 SSMRESULT res = SSM_E_FAIL;
83 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_ITasker, (IBase **)&m_pTasker));
84 m_pResource = resource;
85 m_pResourceFinderClient = pThreadClient;
90 SSMRESULT startObserve(IEvent *pEvent)
92 OC::QueryParamsMap queryParams;
96 m_pResource.get()->observe(OC::ObserveType::Observe, queryParams,
97 std::bind(&OICResourceHandler::onResourceDataReceived, this,
98 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
99 std::placeholders::_4));
104 SSMRESULT stopObserve()
106 m_pResource.get()->cancelObserve();
111 void onResourceDataReceived(const OC::HeaderOptions headerOptions,
112 const OC::OCRepresentation &representation, const int &eCode,
113 const int &sequenceNumber)
117 std::vector< ContextData > lstCtxData;
119 std::map< std::string, std::string > outputProperty;
122 ctxData.rootName = m_pResource->uri().substr(1);
124 //TODO: Temporally used for json parsing limitation
125 ctxData.outputPropertyCount = representation.numberOfAttributes() / 3;
127 if (ctxData.outputPropertyCount > 0)
129 for (int i = 0; i < ctxData.outputPropertyCount; i++)
131 outputProperty["name"] = representation.getValue<std::string>(toString(i * 3));
132 outputProperty["type"] = representation.getValue<std::string>(toString(i * 3 + 1));
133 outputProperty["value"] = representation.getValue<std::string>(toString(i * 3 + 2));
134 ctxData.outputProperty.push_back(outputProperty);
137 /////////////////////////////////////////////////////
138 lstCtxData.push_back(ctxData);
139 m_pEvent->onEvent(m_pResource->uri().substr(1), SSM_REPEAT, lstCtxData);
143 ; //Payload is empty!!
148 void onGetResourceProfile(const OC::HeaderOptions &headerOptions,
149 const OC::OCRepresentation &representation, const int &eCode)
151 //unpack attributeMap
153 //Create SSMResource using OCResource attributeMap
154 std::map< std::string, std::string > outputProperty;
155 m_SSMResource.location = SENSOR_LOCATION_REMOTE;
156 m_SSMResource.name = m_pResource->host() + m_pResource->uri();
157 m_SSMResource.type = m_pResource->uri().substr(1);
158 m_SSMResource.ip = m_pResource->host();
160 //bind default properties
161 outputProperty["name"] = "lifetime";
162 outputProperty["type"] = "int";
163 outputProperty["value"] = "0";
164 m_SSMResource.outputProperty.push_back(outputProperty);
166 //bind default primitive sensor property, value to resource class for schema creating
168 //TODO: Temporally used for json parsing limitation
169 for (int i = 0; i < representation.numberOfAttributes() / 3; i++)
171 outputProperty["name"] = representation.getValue<std::string>(toString(i * 3));
172 outputProperty["type"] = representation.getValue<std::string>(toString(i * 3 + 1));
173 outputProperty["value"] = representation.getValue<std::string>(toString(i * 3 + 2));
174 m_SSMResource.outputProperty.push_back(outputProperty);
176 /////////////////////////////////////////////////////
178 intptr_t *pMessage = new intptr_t [2];
179 pMessage[0] = RESOURCE_DISCOVER_INSTALL_RESOURCE;
180 pMessage[1] = reinterpret_cast<intptr_t>(&m_SSMResource);
182 m_pTasker->addTask(m_pResourceFinderClient, (void *) pMessage);
186 CObjectPtr< ITasker > m_pTasker;
187 std::shared_ptr< OC::OCResource > m_pResource;
188 IThreadClient *m_pResourceFinderClient;
191 std::string toString(int t)
193 std::ostringstream os;
199 enum RESOURCE_DISCOVER_STATE
201 RESOURCE_DISCOVER_REQUESTPROFILE,
202 RESOURCE_DISCOVER_INSTALL_RESOURCE,
203 RESOURCE_DISCOVER_UNINSTALL_RESOURCE
206 IResourceFinderEvent *m_pResourceFinderEvent;
207 CObjectPtr< ITasker > m_pTasker;
208 OC::OCPlatform::OCPresenceHandle m_multicastPresenceHandle;
209 std::map< std::string, OICResourceHandler * > m_mapResourceHandler;
210 std::map< std::string, std::vector<std::string> >
211 m_mapResources; // <hostaddress, std::vector<resources> >
212 std::map< std::string, OC::OCPlatform::OCPresenceHandle >
213 m_mapResourcePresenceHandles; // <hostaddress, presence handler>