IOT-2539 Fix -Wmissing-field-initializers
[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     try
53     {
54         resource = SimulatorResourceFactory::getInstance()->createResource(configPath);
55         if (!resource)
56         {
57             throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
58         }
59     }
60     catch (RAML::RamlException &e)
61     {
62         throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
63     }
64
65     return resource;
66 }
67
68 std::vector<std::shared_ptr<SimulatorResource>> SimulatorManager::createResource(
69             const std::string &configPath, unsigned int count)
70 {
71     VALIDATE_INPUT(configPath.empty(), "Empty path!")
72     VALIDATE_INPUT(!count, "Count is zero!")
73
74     std::vector<std::shared_ptr<SimulatorResource>> resources;
75     try
76     {
77         resources = SimulatorResourceFactory::getInstance()->createResource(configPath, count);
78         if (!resources.size())
79         {
80             throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
81         }
82     }
83     catch (RAML::RamlException &e)
84     {
85         throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
86     }
87
88     return resources;
89 }
90
91 std::shared_ptr<SimulatorSingleResource> SimulatorManager::createSingleResource(
92     const std::string &name, const std::string &uri, const std::string &resourceType)
93 {
94     VALIDATE_INPUT(name.empty(), "Empty resource name!")
95     VALIDATE_INPUT(uri.empty(), "Empty URI!")
96     VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
97
98     return SimulatorResourceFactory::getInstance()->createSingleResource(name, uri, resourceType);
99 }
100
101 std::shared_ptr<SimulatorCollectionResource> SimulatorManager::createCollectionResource(
102     const std::string &name, const std::string &uri, const std::string &resourceType)
103 {
104     VALIDATE_INPUT(name.empty(), "Empty resource name!")
105     VALIDATE_INPUT(uri.empty(), "Empty URI!")
106     VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
107
108     return SimulatorResourceFactory::getInstance()->createCollectionResource(name, uri, resourceType);
109 }
110
111 void SimulatorManager::findResource(ResourceFindCallback callback)
112 {
113     VALIDATE_CALLBACK(callback)
114
115     OC::FindCallback findCallback = std::bind(
116                                         [](std::shared_ptr<OC::OCResource> ocResource, ResourceFindCallback callback)
117     {
118         if (!ocResource)
119         {
120             return;
121         }
122
123         callback(std::make_shared<SimulatorRemoteResourceImpl>(ocResource));
124     }, std::placeholders::_1, callback);
125
126     typedef OCStackResult (*FindResource)(const std::string &, const std::string &,
127                                           OCConnectivityType, OC::FindCallback);
128
129     invokeocplatform(static_cast<FindResource>(OC::OCPlatform::findResource), "",
130                      OC_MULTICAST_DISCOVERY_URI, CT_DEFAULT, findCallback);
131 }
132
133 void SimulatorManager::findResource(const std::string &resourceType,
134                                     ResourceFindCallback callback)
135 {
136     VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
137     VALIDATE_CALLBACK(callback)
138
139     OC::FindCallback findCallback = std::bind(
140                                         [](std::shared_ptr<OC::OCResource> ocResource, ResourceFindCallback callback)
141     {
142         if (!ocResource)
143         {
144             return;
145         }
146
147         callback(std::make_shared<SimulatorRemoteResourceImpl>(ocResource));
148     }, std::placeholders::_1, callback);
149
150     std::ostringstream query;
151     query << OC_MULTICAST_DISCOVERY_URI << "?rt=" << resourceType;
152
153     typedef OCStackResult (*FindResource)(const std::string &, const std::string &,
154                                           OCConnectivityType, OC::FindCallback);
155
156     invokeocplatform(static_cast<FindResource>(OC::OCPlatform::findResource), "", query.str(),
157                      CT_DEFAULT, findCallback);
158 }
159
160 void SimulatorManager::getDeviceInfo(const std::string &host, DeviceInfoCallback callback)
161 {
162     VALIDATE_CALLBACK(callback)
163
164     OC::FindDeviceCallback deviceCallback = std::bind(
165             [](const OC::OCRepresentation & rep, const std::string & hostUri, DeviceInfoCallback callback)
166     {
167         std::string deviceName = rep.getValue<std::string>(OC_RSRVD_DEVICE_NAME);
168         std::string deviceID = rep.getValue<std::string>(OC_RSRVD_DEVICE_ID);
169         std::string deviceSpecVersion = rep.getValue<std::string>(OC_RSRVD_SPEC_VERSION);
170         std::string deviceDMV = rep.getValue<std::string>(OC_RSRVD_DATA_MODEL_VERSION);
171
172         DeviceInfo deviceInfo(deviceName, deviceID, deviceSpecVersion, deviceDMV);
173         callback(hostUri, deviceInfo);
174     }, std::placeholders::_1, host, callback);
175
176     typedef OCStackResult (*GetDeviceInfo)(const std::string &, const std::string &,
177                                            OCConnectivityType, OC::FindDeviceCallback);
178
179     invokeocplatform(static_cast<GetDeviceInfo>(OC::OCPlatform::getDeviceInfo), host.c_str(),
180                      OC_RSRVD_DEVICE_URI, CT_DEFAULT, deviceCallback);
181 }
182
183 void SimulatorManager::setDeviceInfo(const std::string &deviceName)
184 {
185     VALIDATE_INPUT(deviceName.empty(), "Empty resource type!")
186
187     typedef OCStackResult (*RegisterDeviceInfo)(const OCDeviceInfo);
188
189     OCDeviceInfo ocDeviceInfo {nullptr, nullptr, nullptr, nullptr};
190     ocDeviceInfo.deviceName = const_cast<char *>(deviceName.c_str());
191     invokeocplatform(static_cast<RegisterDeviceInfo>(OC::OCPlatform::registerDeviceInfo),
192                      ocDeviceInfo);
193 }
194
195 void SimulatorManager::setDeviceInfo(const std::string &deviceName, const std::string &protocolIndependentID)
196 {
197     setDeviceInfo(deviceName);
198
199     typedef OCStackResult (*SetPropertyValue)(OCPayloadType, const std::string&,
200                                               const std::string&);
201
202     invokeocplatform(static_cast<SetPropertyValue>(OC::OCPlatform::setPropertyValue),
203                      PAYLOAD_TYPE_DEVICE,
204                      OC_RSRVD_PROTOCOL_INDEPENDENT_ID,
205                      protocolIndependentID.c_str());
206 }
207
208 void SimulatorManager::getPlatformInfo(const std::string &host, PlatformInfoCallback callback)
209 {
210     VALIDATE_CALLBACK(callback)
211
212     OC::FindPlatformCallback platformCallback = std::bind(
213                 [](const OC::OCRepresentation & rep, const std::string & hostUri, PlatformInfoCallback callback)
214     {
215         PlatformInfo platformInfo;
216         platformInfo.setPlatformID(rep.getValue<std::string>(OC_RSRVD_PLATFORM_ID));
217         platformInfo.setPlatformVersion(rep.getValue<std::string>(OC_RSRVD_PLATFORM_VERSION));
218         platformInfo.setManufacturerName(rep.getValue<std::string>(OC_RSRVD_MFG_NAME));
219         platformInfo.setManufacturerUrl(rep.getValue<std::string>(OC_RSRVD_MFG_URL));
220         platformInfo.setModelNumber(rep.getValue<std::string>(OC_RSRVD_MODEL_NUM));
221         platformInfo.setDateOfManfacture(rep.getValue<std::string>(OC_RSRVD_MFG_DATE));
222         platformInfo.setOSVersion(rep.getValue<std::string>(OC_RSRVD_OS_VERSION));
223         platformInfo.setHardwareVersion(rep.getValue<std::string>(OC_RSRVD_HARDWARE_VERSION));
224         platformInfo.setFirmwareVersion(rep.getValue<std::string>(OC_RSRVD_FIRMWARE_VERSION));
225         platformInfo.setSupportUrl(rep.getValue<std::string>(OC_RSRVD_SUPPORT_URL));
226         platformInfo.setSystemTime(rep.getValue<std::string>(OC_RSRVD_SYSTEM_TIME));
227
228         callback(hostUri, platformInfo);
229     }, std::placeholders::_1, host, callback);
230
231     typedef OCStackResult (*GetPlatformInfo)(const std::string &, const std::string &,
232             OCConnectivityType, OC::FindPlatformCallback);
233
234     invokeocplatform(static_cast<GetPlatformInfo>(OC::OCPlatform::getPlatformInfo), host.c_str(),
235                      OC_RSRVD_PLATFORM_URI, CT_DEFAULT, platformCallback);
236 }
237
238 void SimulatorManager::setPlatformInfo(PlatformInfo &platformInfo)
239 {
240     OCPlatformInfo ocPlatformInfo;
241     ocPlatformInfo.platformID = const_cast<char *>(platformInfo.getPlatformID().c_str());
242     ocPlatformInfo.manufacturerName = const_cast<char *>(platformInfo.getManufacturerName().c_str());
243     ocPlatformInfo.manufacturerUrl = const_cast<char *>(platformInfo.getManufacturerUrl().c_str());
244     ocPlatformInfo.modelNumber = const_cast<char *>(platformInfo.getModelNumber().c_str());
245     ocPlatformInfo.dateOfManufacture = const_cast<char *>(platformInfo.getDateOfManfacture().c_str());
246     ocPlatformInfo.platformVersion = const_cast<char *>(platformInfo.getPlatformVersion().c_str());
247     ocPlatformInfo.operatingSystemVersion = const_cast<char *>(platformInfo.getOSVersion().c_str());
248     ocPlatformInfo.hardwareVersion = const_cast<char *>(platformInfo.getHardwareVersion().c_str());
249     ocPlatformInfo.firmwareVersion = const_cast<char *>(platformInfo.getFirmwareVersion().c_str());
250     ocPlatformInfo.supportUrl = const_cast<char *>(platformInfo.getSupportUrl().c_str());
251     ocPlatformInfo.systemTime = const_cast<char *>(platformInfo.getSystemTime().c_str());
252
253     typedef OCStackResult (*RegisterPlatformInfo)(const OCPlatformInfo);
254     invokeocplatform(static_cast<RegisterPlatformInfo>(OC::OCPlatform::registerPlatformInfo),
255                      ocPlatformInfo);
256 }
257
258 void SimulatorManager::setLogger(const std::shared_ptr<ILogger> &logger)
259 {
260     simLogger().setCustomTarget(logger);
261 }
262
263 bool SimulatorManager::setConsoleLogger()
264 {
265     return simLogger().setDefaultConsoleTarget();
266 }
267
268 bool SimulatorManager::setFileLogger(const std::string &path)
269 {
270     return simLogger().setDefaultFileTarget(path);
271 }