Fix bugs in things manager reported as defects
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / linux / configuration / FactorySetCollection.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 "FactorySetCollection.h"
32
33 using namespace OC;
34 using namespace std;
35
36 FactorySetResource::FactorySetResource()
37 {
38     m_configurationUri = "/factoryset"; // URI of the resource
39     m_configurationTypes.clear();
40     m_configurationTypes.push_back("factoryset"); // resource type name.
41     m_configurationRep.setUri(m_configurationUri);
42     m_configurationRep.setResourceTypes(m_configurationTypes);
43 }
44
45 FactorySetResource::~FactorySetResource(){}
46
47 /// This function internally calls registerResource API.
48 void FactorySetResource::createResources(ResourceEntityHandler callback)
49 {
50     using namespace OC::OCPlatform;
51
52     if (callback == NULL)
53     {
54         std::cout << "callback should be binded\t";
55         return;
56     }
57
58     // This will internally create and register the resource.
59     OCStackResult result = registerResource(m_configurationHandle, m_configurationUri,
60             m_configurationTypes[0], m_configurationInterfaces[0], callback,
61             OC_DISCOVERABLE | OC_OBSERVABLE);
62
63     if (OC_STACK_OK != result)
64     {
65         std::cout << "Resource creation (configuration) was unsuccessful\n";
66     }
67
68     std::cout << "FactorySet Resource is Created!\n";
69 }
70
71 void FactorySetResource::setFactorySetRepresentation(OCRepresentation& rep)
72 {
73     string value;
74
75     if (rep.getValue("n", value))
76     {
77         m_deviceName = value;
78         std::cout << "\t\t\t\t" << "m_deviceName: " << m_deviceName << std::endl;
79     }
80
81     if (rep.getValue("loc", value))
82     {
83         m_location = value;
84         std::cout << "\t\t\t\t" << "m_location: " << m_location << std::endl;
85     }
86
87     if (rep.getValue("locn", value))
88     {
89         m_locationName = value;
90         std::cout << "\t\t\t\t" << "m_locationName: " << m_locationName << std::endl;
91     }
92
93     if (rep.getValue("c", value))
94     {
95         m_currency = value;
96         std::cout << "\t\t\t\t" << "m_currency: " << m_currency << std::endl;
97     }
98
99     if (rep.getValue("r", value))
100     {
101         m_region = value;
102         std::cout << "\t\t\t\t" << "m_region: " << m_region << std::endl;
103     }
104 }
105
106 OCRepresentation FactorySetResource::getFactorySetRepresentation()
107 {
108     m_configurationRep.setValue("n", m_deviceName);
109     m_configurationRep.setValue("loc", m_location);
110     m_configurationRep.setValue("locn", m_locationName);
111     m_configurationRep.setValue("c", m_currency);
112     m_configurationRep.setValue("r", m_region);
113
114     return m_configurationRep;
115 }
116
117 std::string FactorySetResource::getUri()
118 {
119     return m_configurationUri;
120 }
121