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 catch(RAML::RamlException &e)
61 throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
65 std::vector<std::shared_ptr<SimulatorResource>> SimulatorManager::createResource(
66 const std::string &configPath, unsigned int count)
68 VALIDATE_INPUT(configPath.empty(), "Empty path!")
69 VALIDATE_INPUT(!count, "Count is zero!")
71 std::vector<std::shared_ptr<SimulatorResource>> resources;
74 resources = SimulatorResourceFactory::getInstance()->createResource(configPath, count);
76 catch(RAML::RamlException &e)
78 throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
80 if (!resources.size())
81 throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
85 std::shared_ptr<SimulatorSingleResource> SimulatorManager::createSingleResource(
86 const std::string &name, const std::string &uri, const std::string &resourceType)
88 VALIDATE_INPUT(name.empty(), "Empty resource name!")
89 VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
91 return SimulatorResourceFactory::getInstance()->createSingleResource(name, uri, resourceType);
94 std::shared_ptr<SimulatorCollectionResource> SimulatorManager::createCollectionResource(
95 const std::string &name, const std::string &uri, const std::string &resourceType)
97 VALIDATE_INPUT(name.empty(), "Empty resource name!")
98 VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
100 return SimulatorResourceFactory::getInstance()->createCollectionResource(name, uri, resourceType);
103 void SimulatorManager::findResource(ResourceFindCallback callback)
105 VALIDATE_CALLBACK(callback)
107 OC::FindCallback findCallback = std::bind(
108 [](std::shared_ptr<OC::OCResource> ocResource, ResourceFindCallback callback)
113 SimulatorRemoteResourceSP simulatorResource(new SimulatorRemoteResourceImpl(ocResource));
114 callback(simulatorResource);
115 }, std::placeholders::_1, callback);
117 typedef OCStackResult (*FindResource)(const std::string &, const std::string &,
118 OCConnectivityType, OC::FindCallback);
120 invokeocplatform(static_cast<FindResource>(OC::OCPlatform::findResource), "",
121 OC_MULTICAST_DISCOVERY_URI, CT_DEFAULT, findCallback);
124 void SimulatorManager::findResource(const std::string &resourceType,
125 ResourceFindCallback callback)
127 VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
128 VALIDATE_CALLBACK(callback)
130 OC::FindCallback findCallback = std::bind(
131 [](std::shared_ptr<OC::OCResource> ocResource, ResourceFindCallback callback)
136 SimulatorRemoteResourceSP simulatorResource(new SimulatorRemoteResourceImpl(ocResource));
137 callback(simulatorResource);
138 }, std::placeholders::_1, callback);
140 std::ostringstream query;
141 query << OC_MULTICAST_DISCOVERY_URI << "?rt=" << resourceType;
143 typedef OCStackResult (*FindResource)(const std::string &, const std::string &,
144 OCConnectivityType, OC::FindCallback);
146 invokeocplatform(static_cast<FindResource>(OC::OCPlatform::findResource), "", query.str(),
147 CT_DEFAULT, findCallback);
150 void SimulatorManager::getDeviceInfo(const std::string &host, DeviceInfoCallback callback)
152 VALIDATE_CALLBACK(callback)
154 OC::FindDeviceCallback deviceCallback = std::bind(
155 [](const OC::OCRepresentation & rep, const std::string & hostUri, DeviceInfoCallback callback)
157 std::string deviceName = rep.getValue<std::string>("n");
158 std::string deviceID = rep.getValue<std::string>("di");
159 std::string deviceSpecVersion = rep.getValue<std::string>("lcv");
160 std::string deviceDMV = rep.getValue<std::string>("dmv");
162 DeviceInfo deviceInfo(deviceName, deviceID, deviceSpecVersion, deviceDMV);
163 callback(hostUri, deviceInfo);
164 }, std::placeholders::_1, host, callback);
166 typedef OCStackResult (*GetDeviceInfo)(const std::string &, const std::string &,
167 OCConnectivityType, OC::FindDeviceCallback);
169 invokeocplatform(static_cast<GetDeviceInfo>(OC::OCPlatform::getDeviceInfo), host.c_str(),
170 "/oic/d", CT_DEFAULT, deviceCallback);
173 void SimulatorManager::setDeviceInfo(const std::string &deviceName)
175 VALIDATE_INPUT(deviceName.empty(), "Empty resource type!")
177 typedef OCStackResult (*RegisterDeviceInfo)(const OCDeviceInfo);
179 OCDeviceInfo ocDeviceInfo;
180 ocDeviceInfo.deviceName = const_cast<char *>(deviceName.c_str());
181 invokeocplatform(static_cast<RegisterDeviceInfo>(OC::OCPlatform::registerDeviceInfo),
185 void SimulatorManager::getPlatformInfo(const std::string &host, PlatformInfoCallback callback)
187 VALIDATE_CALLBACK(callback)
189 OC::FindPlatformCallback platformCallback = std::bind(
190 [](const OC::OCRepresentation & rep, const std::string & hostUri, PlatformInfoCallback callback)
192 PlatformInfo platformInfo;
193 platformInfo.setPlatformID(rep.getValue<std::string>("pi"));
194 platformInfo.setPlatformVersion(rep.getValue<std::string>("mnpv"));
195 platformInfo.setManufacturerName(rep.getValue<std::string>("mnmn"));
196 platformInfo.setManufacturerUrl(rep.getValue<std::string>("mnml"));
197 platformInfo.setModelNumber(rep.getValue<std::string>("mnmo"));
198 platformInfo.setDateOfManfacture(rep.getValue<std::string>("mndt"));
199 platformInfo.setOSVersion(rep.getValue<std::string>("mnos"));
200 platformInfo.setHardwareVersion(rep.getValue<std::string>("mnhw"));
201 platformInfo.setFirmwareVersion(rep.getValue<std::string>("mnfv"));
202 platformInfo.setSupportUrl(rep.getValue<std::string>("mnsl"));
203 platformInfo.setSystemTime(rep.getValue<std::string>("st"));
205 callback(hostUri, platformInfo);
206 }, std::placeholders::_1, host, callback);
208 typedef OCStackResult (*GetPlatformInfo)(const std::string &, const std::string &,
209 OCConnectivityType, OC::FindPlatformCallback);
211 invokeocplatform(static_cast<GetPlatformInfo>(OC::OCPlatform::getPlatformInfo), host.c_str(),
212 "/oic/p", CT_DEFAULT, platformCallback);
215 void SimulatorManager::setPlatformInfo(PlatformInfo &platformInfo)
217 OCPlatformInfo ocPlatformInfo;
218 ocPlatformInfo.platformID = const_cast<char *>(platformInfo.getPlatformID().c_str());
219 ocPlatformInfo.manufacturerName = const_cast<char *>(platformInfo.getManufacturerName().c_str());
220 ocPlatformInfo.manufacturerUrl = const_cast<char *>(platformInfo.getManufacturerUrl().c_str());
221 ocPlatformInfo.modelNumber = const_cast<char *>(platformInfo.getModelNumber().c_str());
222 ocPlatformInfo.dateOfManufacture = const_cast<char *>(platformInfo.getDateOfManfacture().c_str());
223 ocPlatformInfo.platformVersion = const_cast<char *>(platformInfo.getPlatformVersion().c_str());
224 ocPlatformInfo.operatingSystemVersion = const_cast<char *>(platformInfo.getOSVersion().c_str());
225 ocPlatformInfo.hardwareVersion = const_cast<char *>(platformInfo.getHardwareVersion().c_str());
226 ocPlatformInfo.firmwareVersion = const_cast<char *>(platformInfo.getFirmwareVersion().c_str());
227 ocPlatformInfo.supportUrl = const_cast<char *>(platformInfo.getSupportUrl().c_str());
228 ocPlatformInfo.systemTime = const_cast<char *>(platformInfo.getSystemTime().c_str());
230 typedef OCStackResult (*RegisterPlatformInfo)(const OCPlatformInfo);
231 invokeocplatform(static_cast<RegisterPlatformInfo>(OC::OCPlatform::registerPlatformInfo),
235 void SimulatorManager::setLogger(const std::shared_ptr<ILogger> &logger)
237 simLogger().setCustomTarget(logger);
240 bool SimulatorManager::setConsoleLogger()
242 return simLogger().setDefaultConsoleTarget();
245 bool SimulatorManager::setFileLogger(const std::string &path)
247 return simLogger().setDefaultFileTarget(path);