1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
21 #include "simulator_manager.h"
22 #include "simulator_resource_factory.h"
23 #include "simulator_remote_resource_impl.h"
24 #include "simulator_utils.h"
26 SimulatorManager *SimulatorManager::getInstance()
28 static SimulatorManager s_instance;
32 SimulatorManager::SimulatorManager()
34 OC::PlatformConfig conf
36 OC::ServiceType::InProc,
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
43 OC::OCPlatform::Configure(conf);
46 std::shared_ptr<SimulatorResource> SimulatorManager::createResource(
47 const std::string &configPath)
49 VALIDATE_INPUT(configPath.empty(), "Empty path!")
51 std::shared_ptr<SimulatorResource> resource;
54 resource = SimulatorResourceFactory::getInstance()->createResource(configPath);
56 throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
58 catch (RAML::RamlException &e)
60 throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
66 std::vector<std::shared_ptr<SimulatorResource>> SimulatorManager::createResource(
67 const std::string &configPath, unsigned int count)
69 VALIDATE_INPUT(configPath.empty(), "Empty path!")
70 VALIDATE_INPUT(!count, "Count is zero!")
72 std::vector<std::shared_ptr<SimulatorResource>> resources;
75 resources = SimulatorResourceFactory::getInstance()->createResource(configPath, count);
76 if (!resources.size())
77 throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
79 catch (RAML::RamlException &e)
81 throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
87 std::shared_ptr<SimulatorSingleResource> SimulatorManager::createSingleResource(
88 const std::string &name, const std::string &uri, const std::string &resourceType)
90 VALIDATE_INPUT(name.empty(), "Empty resource name!")
91 VALIDATE_INPUT(uri.empty(), "Empty URI!")
92 VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
94 return SimulatorResourceFactory::getInstance()->createSingleResource(name, uri, resourceType);
97 std::shared_ptr<SimulatorCollectionResource> SimulatorManager::createCollectionResource(
98 const std::string &name, const std::string &uri, const std::string &resourceType)
100 VALIDATE_INPUT(name.empty(), "Empty resource name!")
101 VALIDATE_INPUT(uri.empty(), "Empty URI!")
102 VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
104 return SimulatorResourceFactory::getInstance()->createCollectionResource(name, uri, resourceType);
107 void SimulatorManager::findResource(ResourceFindCallback callback)
109 VALIDATE_CALLBACK(callback)
111 OC::FindCallback findCallback = std::bind(
112 [](std::shared_ptr<OC::OCResource> ocResource, ResourceFindCallback callback)
117 callback(std::make_shared<SimulatorRemoteResourceImpl>(ocResource));
118 }, std::placeholders::_1, callback);
120 typedef OCStackResult (*FindResource)(const std::string &, const std::string &,
121 OCConnectivityType, OC::FindCallback);
123 invokeocplatform(static_cast<FindResource>(OC::OCPlatform::findResource), "",
124 OC_MULTICAST_DISCOVERY_URI, CT_DEFAULT, findCallback);
127 void SimulatorManager::findResource(const std::string &resourceType,
128 ResourceFindCallback callback)
130 VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
131 VALIDATE_CALLBACK(callback)
133 OC::FindCallback findCallback = std::bind(
134 [](std::shared_ptr<OC::OCResource> ocResource, ResourceFindCallback callback)
139 callback(std::make_shared<SimulatorRemoteResourceImpl>(ocResource));
140 }, std::placeholders::_1, callback);
142 std::ostringstream query;
143 query << OC_MULTICAST_DISCOVERY_URI << "?rt=" << resourceType;
145 typedef OCStackResult (*FindResource)(const std::string &, const std::string &,
146 OCConnectivityType, OC::FindCallback);
148 invokeocplatform(static_cast<FindResource>(OC::OCPlatform::findResource), "", query.str(),
149 CT_DEFAULT, findCallback);
152 void SimulatorManager::getDeviceInfo(const std::string &host, DeviceInfoCallback callback)
154 VALIDATE_CALLBACK(callback)
156 OC::FindDeviceCallback deviceCallback = std::bind(
157 [](const OC::OCRepresentation & rep, const std::string & hostUri, DeviceInfoCallback callback)
159 std::string deviceName = rep.getValue<std::string>("n");
160 std::string deviceID = rep.getValue<std::string>("di");
161 std::string deviceSpecVersion = rep.getValue<std::string>("lcv");
162 std::string deviceDMV = rep.getValue<std::string>("dmv");
164 DeviceInfo deviceInfo(deviceName, deviceID, deviceSpecVersion, deviceDMV);
165 callback(hostUri, deviceInfo);
166 }, std::placeholders::_1, host, callback);
168 typedef OCStackResult (*GetDeviceInfo)(const std::string &, const std::string &,
169 OCConnectivityType, OC::FindDeviceCallback);
171 invokeocplatform(static_cast<GetDeviceInfo>(OC::OCPlatform::getDeviceInfo), host.c_str(),
172 "/oic/d", CT_DEFAULT, deviceCallback);
175 void SimulatorManager::setDeviceInfo(const std::string &deviceName)
177 VALIDATE_INPUT(deviceName.empty(), "Empty resource type!")
179 typedef OCStackResult (*RegisterDeviceInfo)(const OCDeviceInfo);
181 OCDeviceInfo ocDeviceInfo {nullptr, nullptr};
182 ocDeviceInfo.deviceName = const_cast<char *>(deviceName.c_str());
183 invokeocplatform(static_cast<RegisterDeviceInfo>(OC::OCPlatform::registerDeviceInfo),
187 void SimulatorManager::getPlatformInfo(const std::string &host, PlatformInfoCallback callback)
189 VALIDATE_CALLBACK(callback)
191 OC::FindPlatformCallback platformCallback = std::bind(
192 [](const OC::OCRepresentation & rep, const std::string & hostUri, PlatformInfoCallback callback)
194 PlatformInfo platformInfo;
195 platformInfo.setPlatformID(rep.getValue<std::string>("pi"));
196 platformInfo.setPlatformVersion(rep.getValue<std::string>("mnpv"));
197 platformInfo.setManufacturerName(rep.getValue<std::string>("mnmn"));
198 platformInfo.setManufacturerUrl(rep.getValue<std::string>("mnml"));
199 platformInfo.setModelNumber(rep.getValue<std::string>("mnmo"));
200 platformInfo.setDateOfManfacture(rep.getValue<std::string>("mndt"));
201 platformInfo.setOSVersion(rep.getValue<std::string>("mnos"));
202 platformInfo.setHardwareVersion(rep.getValue<std::string>("mnhw"));
203 platformInfo.setFirmwareVersion(rep.getValue<std::string>("mnfv"));
204 platformInfo.setSupportUrl(rep.getValue<std::string>("mnsl"));
205 platformInfo.setSystemTime(rep.getValue<std::string>("st"));
207 callback(hostUri, platformInfo);
208 }, std::placeholders::_1, host, callback);
210 typedef OCStackResult (*GetPlatformInfo)(const std::string &, const std::string &,
211 OCConnectivityType, OC::FindPlatformCallback);
213 invokeocplatform(static_cast<GetPlatformInfo>(OC::OCPlatform::getPlatformInfo), host.c_str(),
214 "/oic/p", CT_DEFAULT, platformCallback);
217 void SimulatorManager::setPlatformInfo(PlatformInfo &platformInfo)
219 OCPlatformInfo ocPlatformInfo;
220 ocPlatformInfo.platformID = const_cast<char *>(platformInfo.getPlatformID().c_str());
221 ocPlatformInfo.manufacturerName = const_cast<char *>(platformInfo.getManufacturerName().c_str());
222 ocPlatformInfo.manufacturerUrl = const_cast<char *>(platformInfo.getManufacturerUrl().c_str());
223 ocPlatformInfo.modelNumber = const_cast<char *>(platformInfo.getModelNumber().c_str());
224 ocPlatformInfo.dateOfManufacture = const_cast<char *>(platformInfo.getDateOfManfacture().c_str());
225 ocPlatformInfo.platformVersion = const_cast<char *>(platformInfo.getPlatformVersion().c_str());
226 ocPlatformInfo.operatingSystemVersion = const_cast<char *>(platformInfo.getOSVersion().c_str());
227 ocPlatformInfo.hardwareVersion = const_cast<char *>(platformInfo.getHardwareVersion().c_str());
228 ocPlatformInfo.firmwareVersion = const_cast<char *>(platformInfo.getFirmwareVersion().c_str());
229 ocPlatformInfo.supportUrl = const_cast<char *>(platformInfo.getSupportUrl().c_str());
230 ocPlatformInfo.systemTime = const_cast<char *>(platformInfo.getSystemTime().c_str());
232 typedef OCStackResult (*RegisterPlatformInfo)(const OCPlatformInfo);
233 invokeocplatform(static_cast<RegisterPlatformInfo>(OC::OCPlatform::registerPlatformInfo),
237 void SimulatorManager::setLogger(const std::shared_ptr<ILogger> &logger)
239 simLogger().setCustomTarget(logger);
242 bool SimulatorManager::setConsoleLogger()
244 return simLogger().setDefaultConsoleTarget();
247 bool SimulatorManager::setFileLogger(const std::string &path)
249 return simLogger().setDefaultFileTarget(path);