1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "InterfaceHandler.h"
23 #include "OCResourceRequest.h"
25 #include "RCSRepresentation.h"
26 #include "RCSRequest.h"
27 #include "RCSResourceObject.h"
28 #include "RCSResourceAttributes.h"
29 #include "ResourceAttributesConverter.h"
31 #include <unordered_map>
35 using namespace OIC::Service;
37 RCSRepresentation toRepresentation(const RCSResourceObject& resource)
39 RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER };
41 return RCSRepresentation{ resource.getUri(), resource.getInterfaces(), resource.getTypes(),
42 resource.getAttributes() };
45 RCSRepresentation buildGetBaselineResponse(const RCSRequest&, const RCSResourceObject& resource)
47 return toRepresentation(resource);
50 RCSRepresentation buildSetBaselineResponse(const RCSRequest& rcsRequest,
51 const RCSResourceObject& resource)
53 return buildGetBaselineResponse(rcsRequest, resource);
56 RCSRepresentation buildGetRequestResponse(const RCSRequest&, const RCSResourceObject& resource)
58 RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER };
60 return RCSRepresentation{ resource.getAttributes() };
63 RCSRepresentation buildSetRequestResponse(const RCSRequest& rcsRequest,
64 const RCSResourceObject& resource)
66 auto requestAttr = ResourceAttributesConverter::fromOCRepresentation(
67 rcsRequest.getOCRequest()->getResourceRepresentation());
69 RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER };
71 const RCSResourceAttributes& updatedAttr = resource.getAttributes();
73 for (auto it = requestAttr.begin(); it != requestAttr.end();)
75 if(updatedAttr.contains(it->key()))
77 it->value() = updatedAttr.at(it->key());
82 it = requestAttr.erase(it);
86 return RCSRepresentation{ requestAttr };
89 RCSRepresentation buildGetBatchResponse(RCSRequest, const RCSResourceObject& resource)
91 RCSRepresentation rcsRep;
93 RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER };
94 rcsRep.setAttributes(resource.getAttributes());
96 for (const auto& bound : resource.getBoundResources())
98 rcsRep.addChild(toRepresentation(*bound));
104 const std::unordered_map< std::string, InterfaceHandler > g_defaultHandlers {
106 { BASELINE_INTERFACE,
107 InterfaceHandler(buildGetBaselineResponse, buildSetBaselineResponse) },
109 { ACTUATOR_INTERFACE,
110 InterfaceHandler(buildGetRequestResponse, buildSetRequestResponse) },
113 InterfaceHandler(buildGetRequestResponse, nullptr) },
116 InterfaceHandler(buildGetBatchResponse, buildSetBaselineResponse) }
125 InterfaceHandler::InterfaceHandler(GetResponseBuilder getBuilder,
126 SetResponseBuilder setBuilder) :
127 m_getBuilder{ std::move(getBuilder) },
128 m_setBuilder{ std::move(setBuilder) }
132 bool InterfaceHandler::isGetSupported() const
134 return m_getBuilder != nullptr;
137 bool InterfaceHandler::isSetSupported() const
139 return m_setBuilder != nullptr;
142 InterfaceHandler::GetResponseBuilder InterfaceHandler::getGetResponseBuilder() const
147 InterfaceHandler::SetResponseBuilder InterfaceHandler::getSetResponseBuilder() const
152 InterfaceHandler getDefaultInterfaceHandler(const std::string& interfaceName,
153 const std::string& defaultInterfaceName)
155 auto it = g_defaultHandlers.find(interfaceName);
156 if (it != g_defaultHandlers.end()) return it->second;
158 it = g_defaultHandlers.find(defaultInterfaceName);
159 if (it != g_defaultHandlers.end()) return it->second;
161 return g_defaultHandlers.find(OIC::Service::BASELINE_INTERFACE)->second;