Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / linux / configuration / ConfigurationCollection.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 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 ///
22 /// This sample shows how one could create a resource (collection) with children.
23 ///
24
25 #include <functional>
26 #include <thread>
27 #include <string.h>
28
29 #include "OCPlatform.h"
30 #include "OCApi.h"
31 #include "ConfigurationCollection.h"
32
33 using namespace OC;
34 using namespace std;
35
36 /// This function internally calls registerResource API.
37 void ConfigurationResource::createResources(ResourceEntityHandler callback)
38 {
39     using namespace OC::OCPlatform;
40
41     if (callback == NULL)
42     {
43         std::cout << "callback should be binded\t";
44         return;
45     }
46
47     // This will internally create and register the resource.
48     OCStackResult result = registerResource(m_configurationHandle, m_configurationUri,
49             m_configurationTypes[0], m_configurationInterfaces[0], callback,
50             OC_DISCOVERABLE | OC_OBSERVABLE);
51
52     if (OC_STACK_OK != result)
53     {
54         std::cout << "Resource creation (configuration) was unsuccessful\n";
55     }
56
57     std::cout << "Configuration Resource is Created!(URI: " << m_configurationUri << ") \n";
58 }
59
60 void ConfigurationResource::setConfigurationRepresentation(OCRepresentation& rep)
61 {
62     string value;
63
64     if (rep.getValue("n", value))
65     {
66         m_deviceName = value;
67         std::cout << "\t\t\t\t" << "m_deviceName: " << m_deviceName << std::endl;
68     }
69
70     if (rep.getValue("loc", value))
71     {
72         m_location = value;
73         std::cout << "\t\t\t\t" << "m_location: " << m_location << std::endl;
74     }
75
76     if (rep.getValue("locn", value))
77     {
78         m_locationName = value;
79         std::cout << "\t\t\t\t" << "m_locationName: " << m_locationName << std::endl;
80     }
81
82     if (rep.getValue("c", value))
83     {
84         m_currency = value;
85         std::cout << "\t\t\t\t" << "m_currency: " << m_currency << std::endl;
86     }
87
88     if (rep.getValue("r", value))
89     {
90         m_region = value;
91         std::cout << "\t\t\t\t" << "m_region: " << m_region << std::endl;
92     }
93 }
94
95 OCRepresentation ConfigurationResource::getConfigurationRepresentation()
96 {
97     m_configurationRep.setValue("n", m_deviceName);
98     m_configurationRep.setValue("loc", m_location);
99     m_configurationRep.setValue("locn", m_locationName);
100     m_configurationRep.setValue("c", m_currency);
101     m_configurationRep.setValue("r", m_region);
102
103     return m_configurationRep;
104 }
105
106 std::string ConfigurationResource::getUri()
107 {
108     return m_configurationUri;
109 }
110
111 void ConfigurationResource::factoryReset()
112 {
113     m_deviceName = defaultDeviceName;
114     m_location = defaultLocation;
115     m_locationName = defaultLocationName;
116     m_currency = defaultCurrency;
117     m_region = defaultRegion;
118 }
119