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 =
52 SimulatorResourceFactory::getInstance()->createResource(configPath);
54 throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
58 std::vector<std::shared_ptr<SimulatorResource>> SimulatorManager::createResource(
59 const std::string &configPath, unsigned int count)
61 VALIDATE_INPUT(configPath.empty(), "Empty path!")
62 VALIDATE_INPUT(!count, "Count is zero!")
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!");
71 std::shared_ptr<SimulatorSingleResource> SimulatorManager::createSingleResource(
72 const std::string &name, const std::string &uri, const std::string &resourceType)
74 VALIDATE_INPUT(name.empty(), "Empty resource name!")
75 VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
77 return SimulatorResourceFactory::getInstance()->createSingleResource(name, uri, resourceType);
80 std::shared_ptr<SimulatorCollectionResource> SimulatorManager::createCollectionResource(
81 const std::string &name, const std::string &uri, const std::string &resourceType)
83 VALIDATE_INPUT(name.empty(), "Empty resource name!")
84 VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
86 return SimulatorResourceFactory::getInstance()->createCollectionResource(name, uri, resourceType);
89 void SimulatorManager::findResource(ResourceFindCallback callback)
91 VALIDATE_CALLBACK(callback)
93 OC::FindCallback findCallback = std::bind(
94 [](std::shared_ptr<OC::OCResource> ocResource, ResourceFindCallback callback)
99 SimulatorRemoteResourceSP simulatorResource(new SimulatorRemoteResourceImpl(ocResource));
100 callback(simulatorResource);
101 }, std::placeholders::_1, callback);
103 typedef OCStackResult (*FindResource)(const std::string &, const std::string &,
104 OCConnectivityType, OC::FindCallback);
106 invokeocplatform(static_cast<FindResource>(OC::OCPlatform::findResource), "",
107 OC_MULTICAST_DISCOVERY_URI, CT_DEFAULT, findCallback);
110 void SimulatorManager::findResource(const std::string &resourceType,
111 ResourceFindCallback callback)
113 VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
114 VALIDATE_CALLBACK(callback)
116 OC::FindCallback findCallback = std::bind(
117 [](std::shared_ptr<OC::OCResource> ocResource, ResourceFindCallback callback)
122 SimulatorRemoteResourceSP simulatorResource(new SimulatorRemoteResourceImpl(ocResource));
123 callback(simulatorResource);
124 }, std::placeholders::_1, callback);
126 std::ostringstream query;
127 query << OC_MULTICAST_DISCOVERY_URI << "?rt=" << resourceType;
129 typedef OCStackResult (*FindResource)(const std::string &, const std::string &,
130 OCConnectivityType, OC::FindCallback);
132 invokeocplatform(static_cast<FindResource>(OC::OCPlatform::findResource), "", query.str(),
133 CT_DEFAULT, findCallback);
136 void SimulatorManager::getDeviceInfo(DeviceInfoCallback callback)
138 VALIDATE_CALLBACK(callback)
140 OC::FindDeviceCallback deviceCallback = std::bind(
141 [](const OC::OCRepresentation & rep, DeviceInfoCallback callback)
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");
148 DeviceInfo deviceInfo(deviceName, deviceID, deviceSpecVersion, deviceDMV);
149 callback(deviceInfo);
150 }, std::placeholders::_1, callback);
152 std::ostringstream uri;
153 uri << OC_MULTICAST_PREFIX << OC_RSRVD_DEVICE_URI;
155 typedef OCStackResult (*GetDeviceInfo)(const std::string &, const std::string &,
156 OCConnectivityType, OC::FindDeviceCallback);
158 invokeocplatform(static_cast<GetDeviceInfo>(OC::OCPlatform::getDeviceInfo), "",
159 uri.str(), CT_DEFAULT, deviceCallback);
162 void SimulatorManager::setDeviceInfo(const std::string &deviceName)
164 VALIDATE_INPUT(deviceName.empty(), "Empty resource type!")
166 typedef OCStackResult (*RegisterDeviceInfo)(const OCDeviceInfo);
168 OCDeviceInfo ocDeviceInfo;
169 ocDeviceInfo.deviceName = const_cast<char *>(deviceName.c_str());
170 invokeocplatform(static_cast<RegisterDeviceInfo>(OC::OCPlatform::registerDeviceInfo),
174 void SimulatorManager::getPlatformInfo(PlatformInfoCallback callback)
176 VALIDATE_CALLBACK(callback)
178 OC::FindPlatformCallback platformCallback = std::bind(
179 [](const OC::OCRepresentation & rep, PlatformInfoCallback callback)
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"));
194 callback(platformInfo);
195 }, std::placeholders::_1, callback);
197 std::ostringstream uri;
198 uri << OC_MULTICAST_PREFIX << OC_RSRVD_PLATFORM_URI;
200 typedef OCStackResult (*GetPlatformInfo)(const std::string &, const std::string &,
201 OCConnectivityType, OC::FindPlatformCallback);
203 invokeocplatform(static_cast<GetPlatformInfo>(OC::OCPlatform::getPlatformInfo), "",
204 uri.str(), CT_DEFAULT, platformCallback);
207 void SimulatorManager::setPlatformInfo(PlatformInfo &platformInfo)
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());
222 typedef OCStackResult (*RegisterPlatformInfo)(const OCPlatformInfo);
223 invokeocplatform(static_cast<RegisterPlatformInfo>(OC::OCPlatform::registerPlatformInfo),
227 void SimulatorManager::setLogger(const std::shared_ptr<ILogger> &logger)
229 simLogger().setCustomTarget(logger);
232 bool SimulatorManager::setConsoleLogger()
234 return simLogger().setDefaultConsoleTarget();
237 bool SimulatorManager::setFileLogger(const std::string &path)
239 return simLogger().setDefaultFileTarget(path);