1f56a2e6e5a72b2b7b206879c79782dc47364e89
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / tizen / ConServerApp / src / configurationresource.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 "configurationresource.h"
22
23 #include <functional>
24 #include <dlog.h>
25
26 #include "OCPlatform.h"
27 #include "OCApi.h"
28
29 using namespace OC;
30
31 // Constructor
32 ConfigurationResource::ConfigurationResource() :
33     m_deviceName(defaultDeviceName), m_location(defaultLocation),
34     m_locationName(defaultLocationName), m_currency(defaultCurrency),
35     m_region(defaultRegion)
36 {
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;
49 }
50
51 // Creates a ConfigurationResource
52 void ConfigurationResource::createResource(ResourceEntityHandler callback)
53 {
54     using namespace OC::OCPlatform;
55
56     if (NULL == callback)
57     {
58         dlog_print(DLOG_INFO, "ConfigurationResource", "#### Callback should be binded");
59         return;
60     }
61
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);
66
67     if (OC_STACK_OK != result)
68     {
69         dlog_print(DLOG_INFO, "ConfigurationResource", "#### Resource creation"
70                    "(configuration) was unsuccessful");
71         return;
72     }
73
74     dlog_print(DLOG_INFO, "ConfigurationResource", "#### Configuration Resource is Created");
75 }
76
77 void ConfigurationResource::setConfigurationRepresentation(OCRepresentation &rep)
78 {
79     std::string value;
80
81     if (rep.getValue(DEFAULT_DEVICENAME, value))
82     {
83         m_deviceName = value;
84         dlog_print(DLOG_INFO, "ConfigurationResource", "#### m_deviceName: %s",
85                    m_deviceName.c_str());
86     }
87
88     if (rep.getValue(DEFAULT_LOCATION, value))
89     {
90         m_location = value;
91         dlog_print(DLOG_INFO, "ConfigurationResource", "#### m_location: %s",
92                    m_location.c_str());
93     }
94
95     if (rep.getValue(DEFAULT_LOCATIONNAME, value))
96     {
97         m_locationName = value;
98         dlog_print(DLOG_INFO, "ConfigurationResource", "#### m_locationName: %s",
99                    m_locationName.c_str());
100     }
101
102     if (rep.getValue(DEFAULT_CURRENCY, value))
103     {
104         m_currency = value;
105         dlog_print(DLOG_INFO, "ConfigurationResource", "#### m_currency: %s",
106                    m_currency.c_str());
107     }
108
109     if (rep.getValue(DEFAULT_REGION, value))
110     {
111         m_region = value;
112         dlog_print(DLOG_INFO, "ConfigurationResource", "#### m_region: %s",
113                    m_region.c_str());
114     }
115 }
116
117 OCRepresentation ConfigurationResource::getConfigurationRepresentation()
118 {
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);
124
125     return m_configurationRep;
126 }
127
128 std::string ConfigurationResource::getUri()
129 {
130     return m_configurationUri;
131 }
132
133 // Assigns default values to all the attributes of the configuration resource
134 void ConfigurationResource::factoryReset()
135 {
136     m_deviceName = defaultDeviceName;
137     m_location = defaultLocation;
138     m_locationName = defaultLocationName;
139     m_currency = defaultCurrency;
140     m_region = defaultRegion;
141 }
142
143 // Deletes the configuration resource which has been created using createResource()
144 void ConfigurationResource::deleteResource()
145 {
146     // Unregister the resource
147     if (nullptr != m_configurationHandle)
148     {
149         OCPlatform::unregisterResource(m_configurationHandle);
150     }
151 }