1 /******************************************************************
3 * Copyright 2015 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 ******************************************************************/
21 #include "configurationresource.h"
26 #include "OCPlatform.h"
32 ConfigurationResource::ConfigurationResource() :
33 m_deviceName(defaultDeviceName), m_location(defaultLocation),
34 m_locationName(defaultLocationName), m_currency(defaultCurrency),
35 m_region(defaultRegion)
37 m_configurationUri = defaultConURI; // URI of the resource
38 m_configurationTypes.push_back(defaultConResourceType); // resource type name
39 m_configurationInterfaces.push_back(DEFAULT_INTERFACE); // resource interface
40 m_configurationRep.setValue(DEFAULT_DEVICENAME, m_deviceName);
41 m_configurationRep.setValue(DEFAULT_LOCATION, m_location);
42 m_configurationRep.setValue(DEFAULT_LOCATIONNAME, m_locationName);
43 m_configurationRep.setValue(DEFAULT_CURRENCY, m_currency);
44 m_configurationRep.setValue(DEFAULT_REGION, m_region);
45 m_configurationRep.setUri(m_configurationUri);
46 m_configurationRep.setResourceTypes(m_configurationTypes);
47 m_configurationRep.setResourceInterfaces(m_configurationInterfaces);
48 m_configurationHandle = nullptr;
51 // Creates a ConfigurationResource
52 void ConfigurationResource::createResource(ResourceEntityHandler callback)
54 using namespace OC::OCPlatform;
58 dlog_print(DLOG_INFO, "ConfigurationResource", "#### Callback should be binded");
62 // This will internally create and register the resource
63 OCStackResult result = registerResource(m_configurationHandle, m_configurationUri,
64 m_configurationTypes[0], m_configurationInterfaces[0],
65 callback, OC_DISCOVERABLE | OC_OBSERVABLE);
67 if (OC_STACK_OK != result)
69 dlog_print(DLOG_INFO, "ConfigurationResource", "#### Resource creation"
70 "(configuration) was unsuccessful");
74 dlog_print(DLOG_INFO, "ConfigurationResource", "#### Configuration Resource is Created");
77 void ConfigurationResource::setConfigurationRepresentation(OCRepresentation &rep)
81 if (rep.getValue(DEFAULT_DEVICENAME, value))
84 dlog_print(DLOG_INFO, "ConfigurationResource", "#### m_deviceName: %s",
85 m_deviceName.c_str());
88 if (rep.getValue(DEFAULT_LOCATION, value))
91 dlog_print(DLOG_INFO, "ConfigurationResource", "#### m_location: %s",
95 if (rep.getValue(DEFAULT_LOCATIONNAME, value))
97 m_locationName = value;
98 dlog_print(DLOG_INFO, "ConfigurationResource", "#### m_locationName: %s",
99 m_locationName.c_str());
102 if (rep.getValue(DEFAULT_CURRENCY, value))
105 dlog_print(DLOG_INFO, "ConfigurationResource", "#### m_currency: %s",
109 if (rep.getValue(DEFAULT_REGION, value))
112 dlog_print(DLOG_INFO, "ConfigurationResource", "#### m_region: %s",
117 OCRepresentation ConfigurationResource::getConfigurationRepresentation()
119 m_configurationRep.setValue(DEFAULT_DEVICENAME, m_deviceName);
120 m_configurationRep.setValue(DEFAULT_LOCATION, m_location);
121 m_configurationRep.setValue(DEFAULT_LOCATIONNAME, m_locationName);
122 m_configurationRep.setValue(DEFAULT_CURRENCY, m_currency);
123 m_configurationRep.setValue(DEFAULT_REGION, m_region);
125 return m_configurationRep;
128 std::string ConfigurationResource::getUri()
130 return m_configurationUri;
133 // Assigns default values to all the attributes of the configuration resource
134 void ConfigurationResource::factoryReset()
136 m_deviceName = defaultDeviceName;
137 m_location = defaultLocation;
138 m_locationName = defaultLocationName;
139 m_currency = defaultCurrency;
140 m_region = defaultRegion;
143 // Deletes the configuration resource which has been created using createResource()
144 void ConfigurationResource::deleteResource()
146 // Unregister the resource
147 if (nullptr != m_configurationHandle)
149 OCPlatform::unregisterResource(m_configurationHandle);