1 #include "InterfaceHandler.h"
3 #include "OCResourceRequest.h"
5 #include "RCSRepresentation.h"
6 #include "RCSRequest.h"
7 #include "RCSResourceObject.h"
8 #include "RCSResourceAttributes.h"
9 #include "ResourceAttributesConverter.h"
11 #include <unordered_map>
15 using namespace OIC::Service;
17 RCSRepresentation toRepresentation(const RCSResourceObject& resource)
19 RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER };
21 return RCSRepresentation{ resource.getUri(), resource.getInterfaces(), resource.getTypes(),
22 resource.getAttributes() };
25 RCSRepresentation buildGetBaselineResponse(const RCSRequest&, const RCSResourceObject& resource)
27 return toRepresentation(resource);
30 RCSRepresentation buildSetBaselineResponse(const RCSRequest& rcsRequest,
31 const RCSResourceObject& resource)
33 return buildGetBaselineResponse(rcsRequest, resource);
36 RCSRepresentation buildGetRequestResponse(const RCSRequest&, const RCSResourceObject& resource)
38 RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER };
40 return RCSRepresentation{ resource.getAttributes() };
43 RCSRepresentation buildSetRequestResponse(const RCSRequest& rcsRequest,
44 const RCSResourceObject& resource)
46 auto requestAttr = ResourceAttributesConverter::fromOCRepresentation(
47 rcsRequest.getOCRequest()->getResourceRepresentation());
49 RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER };
51 const RCSResourceAttributes& updatedAttr = resource.getAttributes();
53 for (auto it = requestAttr.begin(); it != requestAttr.end();)
55 if(updatedAttr.contains(it->key()))
57 it->value() = updatedAttr.at(it->key());
62 it = requestAttr.erase(it);
66 return RCSRepresentation{ requestAttr };
69 RCSRepresentation buildGetBatchResponse(RCSRequest, const RCSResourceObject& resource)
71 RCSRepresentation rcsRep;
73 for (const auto& bound : resource.getBoundResources())
75 rcsRep.addChild(toRepresentation(*bound));
81 const std::unordered_map< std::string, InterfaceHandler > g_defaultHandlers {
84 InterfaceHandler(buildGetBaselineResponse, buildSetBaselineResponse) },
87 InterfaceHandler(buildGetRequestResponse, buildSetRequestResponse) },
90 InterfaceHandler(buildGetRequestResponse, nullptr) },
93 InterfaceHandler(buildGetBatchResponse, buildSetBaselineResponse) }
102 InterfaceHandler::InterfaceHandler(GetResponseBuilder getBuilder,
103 SetResponseBuilder setBuilder) :
104 m_getBuilder{ std::move(getBuilder) },
105 m_setBuilder{ std::move(setBuilder) }
109 bool InterfaceHandler::isGetSupported() const
111 return m_getBuilder != nullptr;
114 bool InterfaceHandler::isSetSupported() const
116 return m_setBuilder != nullptr;
119 InterfaceHandler::GetResponseBuilder InterfaceHandler::getGetResponseBuilder() const
124 InterfaceHandler::SetResponseBuilder InterfaceHandler::getSetResponseBuilder() const
129 InterfaceHandler getDefaultInterfaceHandler(const std::string& interfaceName,
130 const std::string& defaultInterfaceName)
132 auto it = g_defaultHandlers.find(interfaceName);
133 if (it != g_defaultHandlers.end()) return it->second;
135 it = g_defaultHandlers.find(defaultInterfaceName);
136 if (it != g_defaultHandlers.end()) return it->second;
138 return g_defaultHandlers.find(OIC::Service::BASELINE_INTERFACE)->second;