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 "resource_manager.h"
23 #include "simulator_client.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<SimulatorResourceServer> SimulatorManager::createResource(
47 const std::string &configPath,
48 SimulatorResourceServer::ResourceModelChangedCB callback)
50 return ResourceManager::getInstance()->createResource(configPath, callback);
53 std::vector<std::shared_ptr<SimulatorResourceServer>> SimulatorManager::createResource(
54 const std::string &configPath, unsigned short count,
55 SimulatorResourceServer::ResourceModelChangedCB callback)
57 return ResourceManager::getInstance()->createResource(configPath, count, callback);
60 std::vector<std::shared_ptr<SimulatorResourceServer>> SimulatorManager::getResources(
61 const std::string &resourceType)
63 return ResourceManager::getInstance()->getResources(resourceType);
66 void SimulatorManager::deleteResource(
67 const std::shared_ptr<SimulatorResourceServer> &resource)
69 ResourceManager::getInstance()->deleteResource(resource);
72 void SimulatorManager::deleteResource(const std::string &resourceType)
74 ResourceManager::getInstance()->deleteResources(resourceType);
77 void SimulatorManager::findResource(ResourceFindCallback callback)
79 SimulatorClient::getInstance()->findResources(callback);
82 void SimulatorManager::findResource(const std::string &resourceType,
83 ResourceFindCallback callback)
85 SimulatorClient::getInstance()->findResources(resourceType, callback);
88 void SimulatorManager::getDeviceInfo(DeviceInfoCallback callback)
91 throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
93 OC::FindDeviceCallback deviceCallback = [this, callback](const OC::OCRepresentation & rep)
95 std::string deviceName = rep.getValue<std::string>("n");
96 std::string deviceID = rep.getValue<std::string>("di");
97 std::string deviceSpecVersion = rep.getValue<std::string>("lcv");
98 std::string deviceDMV = rep.getValue<std::string>("dmv");
100 DeviceInfo deviceInfo(deviceName, deviceID, deviceSpecVersion, deviceDMV);
101 callback(deviceInfo);
104 std::ostringstream uri;
105 uri << OC_MULTICAST_PREFIX << OC_RSRVD_DEVICE_URI;
107 typedef OCStackResult (*GetDeviceInfo)(const std::string &, const std::string &,
108 OCConnectivityType, OC::FindDeviceCallback);
110 invokeocplatform(static_cast<GetDeviceInfo>(OC::OCPlatform::getDeviceInfo), "",
116 void SimulatorManager::setDeviceInfo(const std::string &deviceName)
118 if (deviceName.empty())
119 throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "Device name is empty!");
122 typedef OCStackResult (*RegisterDeviceInfo)(const OCDeviceInfo);
124 OCDeviceInfo ocDeviceInfo;
125 ocDeviceInfo.deviceName = const_cast<char *>(deviceName.c_str());
126 invokeocplatform(static_cast<RegisterDeviceInfo>(OC::OCPlatform::registerDeviceInfo),
130 void SimulatorManager::getPlatformInfo(PlatformInfoCallback callback)
133 throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
135 OC::FindPlatformCallback platformCallback = [this, callback](const OC::OCRepresentation & rep)
137 PlatformInfo platformInfo;
138 platformInfo.setPlatformID(rep.getValue<std::string>("pi"));
139 platformInfo.setPlatformVersion(rep.getValue<std::string>("mnpv"));
140 platformInfo.setManufacturerName(rep.getValue<std::string>("mnmn"));
141 platformInfo.setManufacturerUrl(rep.getValue<std::string>("mnml"));
142 platformInfo.setModelNumber(rep.getValue<std::string>("mnmo"));
143 platformInfo.setDateOfManfacture(rep.getValue<std::string>("mndt"));
144 platformInfo.setOSVersion(rep.getValue<std::string>("mnos"));
145 platformInfo.setHardwareVersion(rep.getValue<std::string>("mnhw"));
146 platformInfo.setFirmwareVersion(rep.getValue<std::string>("mnfv"));
147 platformInfo.setSupportUrl(rep.getValue<std::string>("mnsl"));
148 platformInfo.setSystemTime(rep.getValue<std::string>("st"));
150 callback(platformInfo);
153 std::ostringstream uri;
154 uri << OC_MULTICAST_PREFIX << OC_RSRVD_PLATFORM_URI;
156 typedef OCStackResult (*GetPlatformInfo)(const std::string &, const std::string &,
157 OCConnectivityType, OC::FindPlatformCallback);
159 invokeocplatform(static_cast<GetPlatformInfo>(OC::OCPlatform::getPlatformInfo), "",
165 void SimulatorManager::setPlatformInfo(PlatformInfo &platformInfo)
167 OCPlatformInfo ocPlatformInfo;
168 ocPlatformInfo.platformID = const_cast<char *>(platformInfo.getPlatformID().c_str());
169 ocPlatformInfo.manufacturerName = const_cast<char *>(platformInfo.getManufacturerName().c_str());
170 ocPlatformInfo.manufacturerUrl = const_cast<char *>(platformInfo.getManufacturerUrl().c_str());
171 ocPlatformInfo.modelNumber = const_cast<char *>(platformInfo.getModelNumber().c_str());
172 ocPlatformInfo.dateOfManufacture = const_cast<char *>(platformInfo.getDateOfManfacture().c_str());
173 ocPlatformInfo.platformVersion = const_cast<char *>(platformInfo.getPlatformVersion().c_str());
174 ocPlatformInfo.operatingSystemVersion = const_cast<char *>(platformInfo.getOSVersion().c_str());
175 ocPlatformInfo.hardwareVersion = const_cast<char *>(platformInfo.getHardwareVersion().c_str());
176 ocPlatformInfo.firmwareVersion = const_cast<char *>(platformInfo.getFirmwareVersion().c_str());
177 ocPlatformInfo.supportUrl = const_cast<char *>(platformInfo.getSupportUrl().c_str());
178 ocPlatformInfo.systemTime = const_cast<char *>(platformInfo.getSystemTime().c_str());
180 typedef OCStackResult (*RegisterPlatformInfo)(const OCPlatformInfo);
181 invokeocplatform(static_cast<RegisterPlatformInfo>(OC::OCPlatform::registerPlatformInfo),
185 void SimulatorManager::setLogger(const std::shared_ptr<ILogger> &logger)
187 simLogger().setCustomTarget(logger);
190 bool SimulatorManager::setConsoleLogger()
192 return simLogger().setDefaultConsoleTarget();
195 bool SimulatorManager::setFileLogger(const std::string &path)
197 return simLogger().setDefaultFileTarget(path);