Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / tizen / ConServerApp / src / factorysetresource.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 "factorysetresource.h"
22
23 #include <functional>
24 #include <dlog.h>
25 #include <string.h>
26
27 #include "OCPlatform.h"
28 #include "OCApi.h"
29 #include "configurationresource.h"
30
31 using namespace OC;
32
33 FactorySetResource::FactorySetResource()
34 {
35     m_configurationUri = "/factoryset"; // URI of the resource
36     m_configurationTypes.clear();
37     m_configurationTypes.push_back("factoryset"); // resource type name.
38     m_configurationRep.setUri(m_configurationUri);
39     m_configurationRep.setResourceTypes(m_configurationTypes);
40 }
41
42 FactorySetResource::~FactorySetResource() {}
43
44 // This function internally calls registerResource API.
45 void FactorySetResource::createResource(ResourceEntityHandler callback)
46 {
47     using namespace OC::OCPlatform;
48
49     if (NULL == callback)
50     {
51         dlog_print(DLOG_INFO, "FactorySetResource", "#### Callback should be binded");
52         return;
53     }
54
55     // This will internally create and register the resource
56     OCStackResult result = registerResource(m_configurationHandle, m_configurationUri,
57                                             m_configurationTypes[0], m_configurationInterfaces[0],
58                                             callback, OC_DISCOVERABLE | OC_OBSERVABLE);
59
60     if (OC_STACK_OK != result)
61     {
62         dlog_print(DLOG_INFO, "FactorySetResource", "#### Resource creation"
63                    "(configuration) was unsuccessful");
64         return;
65     }
66
67     dlog_print(DLOG_INFO, "FactorySetResource", "#### Configuration Resource is Created");
68 }
69
70 void FactorySetResource::setFactorySetRepresentation(OCRepresentation &rep)
71 {
72     std::string value;
73
74     if (rep.getValue(DEFAULT_DEVICENAME, value))
75     {
76         m_deviceName = value;
77         dlog_print(DLOG_INFO, "FactorySetResource", "#### m_deviceName: %s",
78                    m_deviceName.c_str());
79     }
80
81     if (rep.getValue(DEFAULT_LOCATION, value))
82     {
83         m_location = value;
84         dlog_print(DLOG_INFO, "FactorySetResource", "#### m_location: %s",
85                    m_location.c_str());
86     }
87
88     if (rep.getValue(DEFAULT_LOCATIONNAME, value))
89     {
90         m_locationName = value;
91         dlog_print(DLOG_INFO, "FactorySetResource", "#### m_locationName: %s",
92                    m_locationName.c_str());
93     }
94
95     if (rep.getValue(DEFAULT_CURRENCY, value))
96     {
97         m_currency = value;
98         dlog_print(DLOG_INFO, "FactorySetResource", "#### m_currency: %s",
99                    m_currency.c_str());
100     }
101
102     if (rep.getValue(DEFAULT_REGION, value))
103     {
104         m_region = value;
105         dlog_print(DLOG_INFO, "FactorySetResource", "#### m_region: %s",
106                    m_region.c_str());
107     }
108 }
109
110 OCRepresentation FactorySetResource::getFactorySetRepresentation()
111 {
112     m_configurationRep.setValue(DEFAULT_DEVICENAME, m_deviceName);
113     m_configurationRep.setValue(DEFAULT_LOCATION, m_location);
114     m_configurationRep.setValue(DEFAULT_LOCATIONNAME, m_locationName);
115     m_configurationRep.setValue(DEFAULT_CURRENCY, m_currency);
116     m_configurationRep.setValue(DEFAULT_REGION, m_region);
117
118     return m_configurationRep;
119 }
120
121 std::string FactorySetResource::getUri()
122 {
123     return m_configurationUri;
124 }
125
126 // Deletes the factoryset resource which has been created using createResource()
127 void FactorySetResource::deleteResource()
128 {
129     // Unregister the Configuration resource
130     if (NULL != m_configurationHandle)
131     {
132         OCPlatform::unregisterResource(m_configurationHandle);
133     }
134 }