Fix for issue of server updates its model on both PUT and POST requests even
[platform/upstream/iotivity.git] / service / simulator / src / simulator_manager.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 "simulator_manager.h"
22 #include "simulator_resource_factory.h"
23 #include "simulator_remote_resource_impl.h"
24 #include "simulator_utils.h"
25
26 SimulatorManager *SimulatorManager::getInstance()
27 {
28     static SimulatorManager s_instance;
29     return &s_instance;
30 }
31
32 SimulatorManager::SimulatorManager()
33 {
34     OC::PlatformConfig conf
35     {
36         OC::ServiceType::InProc,
37         OC::ModeType::Both,
38         "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
39         0,         // Uses randomly available port
40         OC::QualityOfService::LowQos
41     };
42
43     OC::OCPlatform::Configure(conf);
44 }
45
46 std::shared_ptr<SimulatorResource> SimulatorManager::createResource(
47     const std::string &configPath)
48 {
49     VALIDATE_INPUT(configPath.empty(), "Empty path!")
50
51     std::shared_ptr<SimulatorResource> resource =
52             SimulatorResourceFactory::getInstance()->createResource(configPath);
53     if (!resource)
54         throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
55     return resource;
56 }
57
58 std::vector<std::shared_ptr<SimulatorResource>> SimulatorManager::createResource(
59             const std::string &configPath, unsigned int count)
60 {
61     VALIDATE_INPUT(configPath.empty(), "Empty path!")
62     VALIDATE_INPUT(!count, "Count is zero!")
63
64     std::vector<std::shared_ptr<SimulatorResource>> resources =
65             SimulatorResourceFactory::getInstance()->createResource(configPath, count);
66     if (!resources.size())
67         throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
68     return resources;
69 }
70
71 std::shared_ptr<SimulatorSingleResource> SimulatorManager::createSingleResource(
72     const std::string &name, const std::string &uri, const std::string &resourceType)
73 {
74     VALIDATE_INPUT(name.empty(), "Empty resource name!")
75     VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
76
77     return SimulatorResourceFactory::getInstance()->createSingleResource(name, uri, resourceType);
78 }
79
80 std::shared_ptr<SimulatorCollectionResource> SimulatorManager::createCollectionResource(
81     const std::string &name, const std::string &uri, const std::string &resourceType)
82 {
83     VALIDATE_INPUT(name.empty(), "Empty resource name!")
84     VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
85
86     return SimulatorResourceFactory::getInstance()->createCollectionResource(name, uri, resourceType);
87 }
88
89 void SimulatorManager::findResource(ResourceFindCallback callback)
90 {
91     VALIDATE_CALLBACK(callback)
92
93     OC::FindCallback findCallback = std::bind(
94                                         [](std::shared_ptr<OC::OCResource> ocResource, ResourceFindCallback callback)
95     {
96         if (!ocResource)
97             return;
98
99         SimulatorRemoteResourceSP simulatorResource(new SimulatorRemoteResourceImpl(ocResource));
100         callback(simulatorResource);
101     }, std::placeholders::_1, callback);
102
103     typedef OCStackResult (*FindResource)(const std::string &, const std::string &,
104                                           OCConnectivityType, OC::FindCallback);
105
106     invokeocplatform(static_cast<FindResource>(OC::OCPlatform::findResource), "",
107                      OC_MULTICAST_DISCOVERY_URI, CT_DEFAULT, findCallback);
108 }
109
110 void SimulatorManager::findResource(const std::string &resourceType,
111                                     ResourceFindCallback callback)
112 {
113     VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
114     VALIDATE_CALLBACK(callback)
115
116     OC::FindCallback findCallback = std::bind(
117                                         [](std::shared_ptr<OC::OCResource> ocResource, ResourceFindCallback callback)
118     {
119         if (!ocResource)
120             return;
121
122         SimulatorRemoteResourceSP simulatorResource(new SimulatorRemoteResourceImpl(ocResource));
123         callback(simulatorResource);
124     }, std::placeholders::_1, callback);
125
126     std::ostringstream query;
127     query << OC_MULTICAST_DISCOVERY_URI << "?rt=" << resourceType;
128
129     typedef OCStackResult (*FindResource)(const std::string &, const std::string &,
130                                           OCConnectivityType, OC::FindCallback);
131
132     invokeocplatform(static_cast<FindResource>(OC::OCPlatform::findResource), "", query.str(),
133                      CT_DEFAULT, findCallback);
134 }
135
136 void SimulatorManager::getDeviceInfo(DeviceInfoCallback callback)
137 {
138     VALIDATE_CALLBACK(callback)
139
140     OC::FindDeviceCallback deviceCallback = std::bind(
141             [](const OC::OCRepresentation & rep, DeviceInfoCallback callback)
142     {
143         std::string deviceName = rep.getValue<std::string>("n");
144         std::string deviceID = rep.getValue<std::string>("di");
145         std::string deviceSpecVersion = rep.getValue<std::string>("lcv");
146         std::string deviceDMV = rep.getValue<std::string>("dmv");
147
148         DeviceInfo deviceInfo(deviceName, deviceID, deviceSpecVersion, deviceDMV);
149         callback(deviceInfo);
150     }, std::placeholders::_1, callback);
151
152     std::ostringstream uri;
153     uri << OC_MULTICAST_PREFIX << OC_RSRVD_DEVICE_URI;
154
155     typedef OCStackResult (*GetDeviceInfo)(const std::string &, const std::string &,
156                                            OCConnectivityType, OC::FindDeviceCallback);
157
158     invokeocplatform(static_cast<GetDeviceInfo>(OC::OCPlatform::getDeviceInfo), "",
159                      uri.str(), CT_DEFAULT, deviceCallback);
160 }
161
162 void SimulatorManager::setDeviceInfo(const std::string &deviceName)
163 {
164     VALIDATE_INPUT(deviceName.empty(), "Empty resource type!")
165
166     typedef OCStackResult (*RegisterDeviceInfo)(const OCDeviceInfo);
167
168     OCDeviceInfo ocDeviceInfo;
169     ocDeviceInfo.deviceName = const_cast<char *>(deviceName.c_str());
170     invokeocplatform(static_cast<RegisterDeviceInfo>(OC::OCPlatform::registerDeviceInfo),
171                      ocDeviceInfo);
172 }
173
174 void SimulatorManager::getPlatformInfo(PlatformInfoCallback callback)
175 {
176     VALIDATE_CALLBACK(callback)
177
178     OC::FindPlatformCallback platformCallback = std::bind(
179                 [](const OC::OCRepresentation & rep, PlatformInfoCallback callback)
180     {
181         PlatformInfo platformInfo;
182         platformInfo.setPlatformID(rep.getValue<std::string>("pi"));
183         platformInfo.setPlatformVersion(rep.getValue<std::string>("mnpv"));
184         platformInfo.setManufacturerName(rep.getValue<std::string>("mnmn"));
185         platformInfo.setManufacturerUrl(rep.getValue<std::string>("mnml"));
186         platformInfo.setModelNumber(rep.getValue<std::string>("mnmo"));
187         platformInfo.setDateOfManfacture(rep.getValue<std::string>("mndt"));
188         platformInfo.setOSVersion(rep.getValue<std::string>("mnos"));
189         platformInfo.setHardwareVersion(rep.getValue<std::string>("mnhw"));
190         platformInfo.setFirmwareVersion(rep.getValue<std::string>("mnfv"));
191         platformInfo.setSupportUrl(rep.getValue<std::string>("mnsl"));
192         platformInfo.setSystemTime(rep.getValue<std::string>("st"));
193
194         callback(platformInfo);
195     }, std::placeholders::_1, callback);
196
197     std::ostringstream uri;
198     uri << OC_MULTICAST_PREFIX << OC_RSRVD_PLATFORM_URI;
199
200     typedef OCStackResult (*GetPlatformInfo)(const std::string &, const std::string &,
201             OCConnectivityType, OC::FindPlatformCallback);
202
203     invokeocplatform(static_cast<GetPlatformInfo>(OC::OCPlatform::getPlatformInfo), "",
204                      uri.str(), CT_DEFAULT, platformCallback);
205 }
206
207 void SimulatorManager::setPlatformInfo(PlatformInfo &platformInfo)
208 {
209     OCPlatformInfo ocPlatformInfo;
210     ocPlatformInfo.platformID = const_cast<char *>(platformInfo.getPlatformID().c_str());
211     ocPlatformInfo.manufacturerName = const_cast<char *>(platformInfo.getManufacturerName().c_str());
212     ocPlatformInfo.manufacturerUrl = const_cast<char *>(platformInfo.getManufacturerUrl().c_str());
213     ocPlatformInfo.modelNumber = const_cast<char *>(platformInfo.getModelNumber().c_str());
214     ocPlatformInfo.dateOfManufacture = const_cast<char *>(platformInfo.getDateOfManfacture().c_str());
215     ocPlatformInfo.platformVersion = const_cast<char *>(platformInfo.getPlatformVersion().c_str());
216     ocPlatformInfo.operatingSystemVersion = const_cast<char *>(platformInfo.getOSVersion().c_str());
217     ocPlatformInfo.hardwareVersion = const_cast<char *>(platformInfo.getHardwareVersion().c_str());
218     ocPlatformInfo.firmwareVersion = const_cast<char *>(platformInfo.getFirmwareVersion().c_str());
219     ocPlatformInfo.supportUrl = const_cast<char *>(platformInfo.getSupportUrl().c_str());
220     ocPlatformInfo.systemTime = const_cast<char *>(platformInfo.getSystemTime().c_str());
221
222     typedef OCStackResult (*RegisterPlatformInfo)(const OCPlatformInfo);
223     invokeocplatform(static_cast<RegisterPlatformInfo>(OC::OCPlatform::registerPlatformInfo),
224                      ocPlatformInfo);
225 }
226
227 void SimulatorManager::setLogger(const std::shared_ptr<ILogger> &logger)
228 {
229     simLogger().setCustomTarget(logger);
230 }
231
232 bool SimulatorManager::setConsoleLogger()
233 {
234     return simLogger().setDefaultConsoleTarget();
235 }
236
237 bool SimulatorManager::setFileLogger(const std::string &path)
238 {
239     return simLogger().setDefaultFileTarget(path);
240 }