Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SSMCore / src / SensorProcessor / ResourceFinder.h
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 #ifndef _ResourceFinder_H_
21 #define _ResourceFinder_H_
22
23 #include "SSMInterface/SSMCore.h"
24 #include "Common/PlatformLayer.h"
25 #include "Common/InternalInterface.h"
26 #include "OCPlatform.h"
27 #include "OCApi.h"
28
29 class CResourceFinder: public CObjectRoot< CObjectMultiThreadModel >,
30     public IResourceFinder,
31     public IThreadClient
32 {
33     public:
34         SSMRESULT queryInterface(const OID &objectID, IBase **ppObject)
35         {
36             if (ppObject == NULL)
37                 return SSM_E_POINTER;
38
39             if (IsEqualOID(objectID, OID_IResourceFinder))
40             {
41                 IBase *pBase = (IResourceFinder *) this;
42                 pBase->addRef();
43                 *ppObject = pBase;
44                 return SSM_S_OK;
45             }
46
47             return SSM_E_NOINTERFACE;
48         }
49
50         SSMRESULT finalConstruct();
51         void finalRelease();
52
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();
59
60         SSMRESULT startObserveResource(ISSMResource *pSensor, IEvent *pEvent);
61         SSMRESULT stopObserveResource(ISSMResource *pSensor);
62
63         void onExecute(void *pArg);
64         void onTerminate(void *pArg);
65
66     private:
67         class OICResourceHandler
68         {
69             public:
70                 ISSMResource m_SSMResource;
71
72                 OICResourceHandler()
73                 {
74                     m_pEvent = NULL;
75                     m_pResourceFinderClient = NULL;
76                 }
77
78                 SSMRESULT initHandler(std::shared_ptr< OC::OCResource > resource,
79                                       IThreadClient *pThreadClient)
80                 {
81                     SSMRESULT res = SSM_E_FAIL;
82
83                     SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_ITasker, (IBase **)&m_pTasker));
84                     m_pResource = resource;
85                     m_pResourceFinderClient = pThreadClient;
86
87 CLEANUP: return res;
88                 }
89
90                 SSMRESULT startObserve(IEvent *pEvent)
91                 {
92                     OC::QueryParamsMap queryParams;
93
94                     m_pEvent = pEvent;
95
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));
100
101                     return SSM_S_OK;
102                 }
103
104                 SSMRESULT stopObserve()
105                 {
106                     m_pResource.get()->cancelObserve();
107
108                     return SSM_S_OK;
109                 }
110
111                 void onResourceDataReceived(const OC::HeaderOptions headerOptions,
112                                             const OC::OCRepresentation &representation, const int &eCode,
113                                             const int &sequenceNumber)
114                 {
115                     if (eCode == 0)
116                     {
117                         std::vector< ContextData > lstCtxData;
118                         ContextData ctxData;
119                         std::map< std::string, std::string > outputProperty;
120
121                         //Bind data
122                         ctxData.rootName = m_pResource->uri().substr(1);
123
124                         //TODO: Temporally used for json parsing limitation
125                         ctxData.outputPropertyCount = representation.numberOfAttributes() / 3;
126
127                         if (ctxData.outputPropertyCount > 0)
128                         {
129                             for (int i = 0; i < ctxData.outputPropertyCount; i++)
130                             {
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);
135                             }
136
137                             /////////////////////////////////////////////////////
138                             lstCtxData.push_back(ctxData);
139                             m_pEvent->onEvent(m_pResource->uri().substr(1), SSM_REPEAT, lstCtxData);
140                         }
141                         else
142                         {
143                             ; //Payload is empty!!
144                         }
145                     }
146                 }
147
148                 void onGetResourceProfile(const OC::HeaderOptions &headerOptions,
149                                           const OC::OCRepresentation &representation, const int &eCode)
150                 {
151                     //unpack attributeMap
152
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();
159
160                     //bind default properties
161                     outputProperty["name"] = "lifetime";
162                     outputProperty["type"] = "int";
163                     outputProperty["value"] = "0";
164                     m_SSMResource.outputProperty.push_back(outputProperty);
165
166                     //bind default primitive sensor property, value to resource class for schema creating
167
168                     //TODO: Temporally used for json parsing limitation
169                     for (int i = 0; i < representation.numberOfAttributes() / 3; i++)
170                     {
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);
175                     }
176                     /////////////////////////////////////////////////////
177                     //
178                     intptr_t      *pMessage = new intptr_t [2];
179                     pMessage[0] = RESOURCE_DISCOVER_INSTALL_RESOURCE;
180                     pMessage[1] = reinterpret_cast<intptr_t>(&m_SSMResource);
181
182                     m_pTasker->addTask(m_pResourceFinderClient, (void *) pMessage);
183                 }
184
185             private:
186                 CObjectPtr< ITasker > m_pTasker;
187                 std::shared_ptr< OC::OCResource > m_pResource;
188                 IThreadClient *m_pResourceFinderClient;
189                 IEvent *m_pEvent;
190
191                 std::string toString(int t)
192                 {
193                     std::ostringstream os;
194                     os << t;
195                     return os.str();
196                 }
197         };
198
199         enum RESOURCE_DISCOVER_STATE
200         {
201             RESOURCE_DISCOVER_REQUESTPROFILE,
202             RESOURCE_DISCOVER_INSTALL_RESOURCE,
203             RESOURCE_DISCOVER_UNINSTALL_RESOURCE
204         };
205
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>
214 };
215
216 #endif