Imported Upstream version 0.9.1
[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 #include "ThingsManager.h"
29
30 using namespace OC;
31
32 // Constructor
33 ConfigurationResource::ConfigurationResource() :
34     m_location(defaultLocation), m_systemTime(defaultSystemTime), m_currency(
35         defaultCurrency), m_region(defaultRegion)
36 {
37     m_configurationUri = "/oic/con"; // URI of the resource
38     m_configurationTypes.push_back("oic.con"); // resource type name
39     m_configurationInterfaces.push_back(DEFAULT_INTERFACE); // resource interface
40     m_configurationRep.setValue("loc", m_location);
41     m_configurationRep.setValue("st", m_systemTime);
42     m_configurationRep.setValue("c", m_currency);
43     m_configurationRep.setValue("r", m_region);
44     m_configurationRep.setUri(m_configurationUri);
45     m_configurationRep.setResourceTypes(m_configurationTypes);
46     m_configurationRep.setResourceInterfaces(m_configurationInterfaces);
47     m_configurationHandle = NULL;
48 }
49
50 // Creates a ConfigurationResource
51 void ConfigurationResource::createResource(ResourceEntityHandler callback)
52 {
53     using namespace OC::OCPlatform;
54
55     if (NULL == callback)
56     {
57         dlog_print(DLOG_INFO, "ConfigurationResource", "#### Callback should be binded");
58         return;
59     }
60
61     // This will internally create and register the resource
62     OCStackResult result = registerResource(m_configurationHandle, m_configurationUri,
63                                             m_configurationTypes[0], m_configurationInterfaces[0],
64                                             callback, OC_DISCOVERABLE | OC_OBSERVABLE);
65
66     if (OC_STACK_OK != result)
67     {
68         dlog_print(DLOG_INFO, "ConfigurationResource", "#### Resource creation"
69                    "(configuration) was unsuccessful");
70         return;
71     }
72
73     dlog_print(DLOG_INFO, "ConfigurationResource", "#### Configuration Resource is Created");
74 }
75
76 void ConfigurationResource::setConfigurationRepresentation(OCRepresentation &rep)
77 {
78     string value;
79
80     if (rep.getValue("loc", value))
81     {
82         m_location = value;
83         dlog_print(DLOG_INFO, "ConfigurationResource", "#### m_location: %s",
84                    m_location.c_str());
85     }
86
87     if (rep.getValue("st", value))
88     {
89         dlog_print(DLOG_INFO, "ConfigurationResource", "#### SystemTime is not"
90                    "allowed to be written");
91     }
92
93     if (rep.getValue("c", value))
94     {
95         m_currency = value;
96         dlog_print(DLOG_INFO, "ConfigurationResource", "#### m_currency: %s",
97                    m_currency.c_str());
98     }
99
100     if (rep.getValue("r", value))
101     {
102         m_region = value;
103         dlog_print(DLOG_INFO, "ConfigurationResource", "#### m_region: %s",
104                    m_region.c_str());
105     }
106 }
107
108 OCRepresentation ConfigurationResource::getConfigurationRepresentation()
109 {
110     m_configurationRep.setValue("loc", m_location);
111     m_configurationRep.setValue("st", m_systemTime);
112     m_configurationRep.setValue("c", m_currency);
113     m_configurationRep.setValue("r", m_region);
114
115     return m_configurationRep;
116 }
117
118 std::string ConfigurationResource::getUri()
119 {
120     return m_configurationUri;
121 }
122
123 // Assigns default values to all the attributes of the configuration resource
124 void ConfigurationResource::factoryReset()
125 {
126     m_location = defaultLocation;
127     m_systemTime = defaultSystemTime;
128     m_currency = defaultCurrency;
129     m_region = defaultRegion;
130 }
131
132 // Deletes the configuration resource which has been created using createResource()
133 void ConfigurationResource::deleteResource()
134 {
135     // Unregister the resource
136     if (NULL != m_configurationHandle)
137     {
138         OCPlatform::unregisterResource(m_configurationHandle);
139     }
140 }