Updating JNI modules of service provider
[platform/upstream/iotivity.git] / service / simulator / src / simulator_resource.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 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
21 #include "simulator_resource.h"
22 #include "simulator_attribute_automation.h"
23
24 void SimulatorResource::setRange(const std::string &attrName, const int min, const int max)
25 {
26     m_resModel.setRange(attrName, min, max);
27 }
28
29 void SimulatorResource::setUpdateInterval(const std::string &attrName, int interval)
30 {
31     m_resModel.setUpdateInterval(attrName, interval);
32 }
33
34 void SimulatorResource::removeAttribute(const std::string &attrName)
35 {
36     m_resModel.removeAttribute(attrName);
37     notifyListOfObservers();
38 }
39
40 void SimulatorResource::setURI(const std::string &uri)
41 {
42     m_uri = uri;
43 }
44
45 std::string SimulatorResource::getURI() const
46 {
47     return m_uri;
48 }
49
50 void SimulatorResource::setResourceType(const std::string &resourceType)
51 {
52     m_resourceType = resourceType;
53 }
54
55 std::string SimulatorResource::getResourceType() const
56 {
57     return m_resourceType;
58 }
59
60 void SimulatorResource::setInterfaceType(const std::string &interfaceType)
61 {
62     m_interfaceType = interfaceType;
63 }
64
65 std::string SimulatorResource::getInterfaceType() const
66 {
67     return m_interfaceType;
68 }
69
70 OCResourceHandle SimulatorResource::getHandle() const
71 {
72     return m_resourceHandle;
73 }
74
75 void SimulatorResource::setName(const std::string &name)
76 {
77     m_name = name;
78 }
79
80 std::string SimulatorResource::getName() const
81 {
82     return m_name;
83 }
84
85 void SimulatorResource::updateAttributeFromAllowedValues(const std::string &attrName,
86         const int allowedValueIndex)
87 {
88     m_resModel.updateAttributeFromAllowedValues(attrName, allowedValueIndex);
89     notifyListOfObservers();
90 }
91
92 SimulatorResourceModel SimulatorResource::getModel() const
93 {
94     return m_resModel;
95 }
96
97 void SimulatorResource::setModelChangeCallback(ResourceModelChangedCB callback)
98 {
99     m_callback = callback;
100 }
101
102 SimulatorResult SimulatorResource::startUpdateAutomation(AutomationType type, int &id)
103 {
104     return m_updateAutomationMgr.startResourceAutomation(this, id, type);
105 }
106
107 SimulatorResult SimulatorResource::startUpdateAutomation(const std::string &attrName,
108         AutomationType type, int &id)
109 {
110     return m_updateAutomationMgr.startAttributeAutomation(this, attrName, id, type);
111 }
112
113 void SimulatorResource::stopUpdateAutomation(const int id)
114 {
115     m_updateAutomationMgr.stop(id);
116 }
117
118 OC::OCRepresentation SimulatorResource::getOCRepresentation()
119 {
120     return m_resModel.getOCRepresentation();
121 }
122
123 bool SimulatorResource::modifyResourceModel(OC::OCRepresentation &ocRep,
124         SimulatorResourceModel::UpdateType type)
125 {
126     return m_resModel.update(ocRep, type);
127 }
128
129 OCEntityHandlerResult SimulatorResource::entityHandler(std::shared_ptr<OC::OCResourceRequest>
130         request)
131 {
132     OCEntityHandlerResult errCode = OC_EH_ERROR;
133     if (!request)
134     {
135         return OC_EH_ERROR;
136     }
137
138     if (OC::RequestHandlerFlag::RequestFlag & request->getRequestHandlerFlag())
139     {
140         auto response = std::make_shared<OC::OCResourceResponse>();
141         response->setRequestHandle(request->getRequestHandle());
142         response->setResourceHandle(request->getResourceHandle());
143
144         if ("GET" == request->getRequestType())
145         {
146             response->setErrorCode(200);
147             response->setResponseResult(OC_EH_OK);
148             response->setResourceRepresentation(getOCRepresentation());
149
150             if (OC_STACK_OK == OC::OCPlatform::sendResponse(response))
151             {
152                 errCode = OC_EH_OK;
153             }
154         }
155         else if ("PUT" == request->getRequestType())
156         {
157             OC::OCRepresentation rep = request->getResourceRepresentation();
158             if (true == modifyResourceModel(rep, SimulatorResourceModel::UpdateType::PUT))
159             {
160                 response->setErrorCode(200);
161                 response->setResponseResult(OC_EH_OK);
162                 response->setResourceRepresentation(getOCRepresentation());
163             }
164             else
165             {
166                 response->setErrorCode(400);
167                 response->setResponseResult(OC_EH_ERROR);
168             }
169
170             if (OC_STACK_OK == OC::OCPlatform::sendResponse(response))
171             {
172                 errCode = OC_EH_OK;
173             }
174         }
175         else if ("POST" == request->getRequestType())
176         {
177             OC::OCRepresentation rep = request->getResourceRepresentation();
178             if (true == modifyResourceModel(rep, SimulatorResourceModel::UpdateType::POST))
179             {
180                 response->setErrorCode(200);
181                 response->setResponseResult(OC_EH_OK);
182                 response->setResourceRepresentation(getOCRepresentation());
183             }
184             else
185             {
186                 response->setErrorCode(400);
187                 response->setResponseResult(OC_EH_ERROR);
188             }
189
190             if (OC_STACK_OK == OC::OCPlatform::sendResponse(response))
191             {
192                 errCode = OC_EH_OK;
193             }
194         }
195         else if ("DELETE" == request->getRequestType())
196         {
197             OC::OCRepresentation rep = request->getResourceRepresentation();
198             if (true == modifyResourceModel(rep, SimulatorResourceModel::UpdateType::DELETE))
199             {
200                 response->setErrorCode(200);
201                 response->setResponseResult(OC_EH_OK);
202                 response->setResourceRepresentation(getOCRepresentation());
203             }
204             else
205             {
206                 response->setErrorCode(400);
207                 response->setResponseResult(OC_EH_ERROR);
208             }
209
210             if (OC_STACK_OK == OC::OCPlatform::sendResponse(response))
211             {
212                 errCode = OC_EH_OK;
213             }
214         }
215         else
216         {
217             response->setResponseResult(OC_EH_ERROR);
218             OC::OCPlatform::sendResponse(response);
219             errCode = OC_EH_ERROR;
220         }
221     }
222
223     if (OC::RequestHandlerFlag::ObserverFlag & request->getRequestHandlerFlag())
224     {
225         OC::ObservationInfo observationInfo = request->getObservationInfo();
226         if (OC::ObserveAction::ObserveRegister == observationInfo.action)
227         {
228             m_interestedObservers.push_back(observationInfo.obsId);
229         }
230         else if (OC::ObserveAction::ObserveUnregister == observationInfo.action)
231         {
232             m_interestedObservers.erase(std::remove(m_interestedObservers.begin(),
233                                                     m_interestedObservers.end(),
234                                                     observationInfo.obsId),
235                                         m_interestedObservers.end());
236         }
237         errCode = OC_EH_OK;
238     }
239
240     return errCode;
241 }
242
243 void SimulatorResource::notifyListOfObservers ()
244 {
245     if(getHandle())
246     {
247         if (m_interestedObservers.size())
248         {
249             std::shared_ptr<OC::OCResourceResponse> resourceResponse =
250             {std::make_shared<OC::OCResourceResponse>()};
251
252             resourceResponse->setErrorCode(200);
253             resourceResponse->setResponseResult(OC_EH_OK);
254             resourceResponse->setResourceRepresentation(getOCRepresentation(), OC::DEFAULT_INTERFACE);
255
256             OC::OCPlatform::notifyListOfObservers(getHandle(), m_interestedObservers, resourceResponse);
257         }
258         if(m_callback)
259         {
260             m_callback(getURI(),getModel());
261         }
262     }
263 }