Imported Upstream version 1.1.0
[platform/upstream/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             throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
57     }
58     catch (RAML::RamlException &e)
59     {
60         throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
61     }
62
63     return resource;
64 }
65
66 std::vector<std::shared_ptr<SimulatorResource>> SimulatorManager::createResource(
67             const std::string &configPath, unsigned int count)
68 {
69     VALIDATE_INPUT(configPath.empty(), "Empty path!")
70     VALIDATE_INPUT(!count, "Count is zero!")
71
72     std::vector<std::shared_ptr<SimulatorResource>> resources;
73     try
74     {
75         resources = SimulatorResourceFactory::getInstance()->createResource(configPath, count);
76         if (!resources.size())
77             throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
78     }
79     catch (RAML::RamlException &e)
80     {
81         throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
82     }
83
84     return resources;
85 }
86
87 std::shared_ptr<SimulatorSingleResource> SimulatorManager::createSingleResource(
88     const std::string &name, const std::string &uri, const std::string &resourceType)
89 {
90     VALIDATE_INPUT(name.empty(), "Empty resource name!")
91     VALIDATE_INPUT(uri.empty(), "Empty URI!")
92     VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
93
94     return SimulatorResourceFactory::getInstance()->createSingleResource(name, uri, resourceType);
95 }
96
97 std::shared_ptr<SimulatorCollectionResource> SimulatorManager::createCollectionResource(
98     const std::string &name, const std::string &uri, const std::string &resourceType)
99 {
100     VALIDATE_INPUT(name.empty(), "Empty resource name!")
101     VALIDATE_INPUT(uri.empty(), "Empty URI!")
102     VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
103
104     return SimulatorResourceFactory::getInstance()->createCollectionResource(name, uri, resourceType);
105 }
106
107 void SimulatorManager::findResource(ResourceFindCallback callback)
108 {
109     VALIDATE_CALLBACK(callback)
110
111     OC::FindCallback findCallback = std::bind(
112                                         [](std::shared_ptr<OC::OCResource> ocResource, ResourceFindCallback callback)
113     {
114         if (!ocResource)
115             return;
116
117         callback(std::make_shared<SimulatorRemoteResourceImpl>(ocResource));
118     }, std::placeholders::_1, callback);
119
120     typedef OCStackResult (*FindResource)(const std::string &, const std::string &,
121                                           OCConnectivityType, OC::FindCallback);
122
123     invokeocplatform(static_cast<FindResource>(OC::OCPlatform::findResource), "",
124                      OC_MULTICAST_DISCOVERY_URI, CT_DEFAULT, findCallback);
125 }
126
127 void SimulatorManager::findResource(const std::string &resourceType,
128                                     ResourceFindCallback callback)
129 {
130     VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
131     VALIDATE_CALLBACK(callback)
132
133     OC::FindCallback findCallback = std::bind(
134                                         [](std::shared_ptr<OC::OCResource> ocResource, ResourceFindCallback callback)
135     {
136         if (!ocResource)
137             return;
138
139         callback(std::make_shared<SimulatorRemoteResourceImpl>(ocResource));
140     }, std::placeholders::_1, callback);
141
142     std::ostringstream query;
143     query << OC_MULTICAST_DISCOVERY_URI << "?rt=" << resourceType;
144
145     typedef OCStackResult (*FindResource)(const std::string &, const std::string &,
146                                           OCConnectivityType, OC::FindCallback);
147
148     invokeocplatform(static_cast<FindResource>(OC::OCPlatform::findResource), "", query.str(),
149                      CT_DEFAULT, findCallback);
150 }
151
152 void SimulatorManager::getDeviceInfo(const std::string &host, DeviceInfoCallback callback)
153 {
154     VALIDATE_CALLBACK(callback)
155
156     OC::FindDeviceCallback deviceCallback = std::bind(
157             [](const OC::OCRepresentation & rep, const std::string & hostUri, DeviceInfoCallback callback)
158     {
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");
163
164         DeviceInfo deviceInfo(deviceName, deviceID, deviceSpecVersion, deviceDMV);
165         callback(hostUri, deviceInfo);
166     }, std::placeholders::_1, host, callback);
167
168     typedef OCStackResult (*GetDeviceInfo)(const std::string &, const std::string &,
169                                            OCConnectivityType, OC::FindDeviceCallback);
170
171     invokeocplatform(static_cast<GetDeviceInfo>(OC::OCPlatform::getDeviceInfo), host.c_str(),
172                      "/oic/d", CT_DEFAULT, deviceCallback);
173 }
174
175 void SimulatorManager::setDeviceInfo(const std::string &deviceName)
176 {
177     VALIDATE_INPUT(deviceName.empty(), "Empty resource type!")
178
179     typedef OCStackResult (*RegisterDeviceInfo)(const OCDeviceInfo);
180
181     OCDeviceInfo ocDeviceInfo {nullptr, nullptr};
182     ocDeviceInfo.deviceName = const_cast<char *>(deviceName.c_str());
183     invokeocplatform(static_cast<RegisterDeviceInfo>(OC::OCPlatform::registerDeviceInfo),
184                      ocDeviceInfo);
185 }
186
187 void SimulatorManager::getPlatformInfo(const std::string &host, PlatformInfoCallback callback)
188 {
189     VALIDATE_CALLBACK(callback)
190
191     OC::FindPlatformCallback platformCallback = std::bind(
192                 [](const OC::OCRepresentation & rep, const std::string & hostUri, PlatformInfoCallback callback)
193     {
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"));
206
207         callback(hostUri, platformInfo);
208     }, std::placeholders::_1, host, callback);
209
210     typedef OCStackResult (*GetPlatformInfo)(const std::string &, const std::string &,
211             OCConnectivityType, OC::FindPlatformCallback);
212
213     invokeocplatform(static_cast<GetPlatformInfo>(OC::OCPlatform::getPlatformInfo), host.c_str(),
214                      "/oic/p", CT_DEFAULT, platformCallback);
215 }
216
217 void SimulatorManager::setPlatformInfo(PlatformInfo &platformInfo)
218 {
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());
231
232     typedef OCStackResult (*RegisterPlatformInfo)(const OCPlatformInfo);
233     invokeocplatform(static_cast<RegisterPlatformInfo>(OC::OCPlatform::registerPlatformInfo),
234                      ocPlatformInfo);
235 }
236
237 void SimulatorManager::setLogger(const std::shared_ptr<ILogger> &logger)
238 {
239     simLogger().setCustomTarget(logger);
240 }
241
242 bool SimulatorManager::setConsoleLogger()
243 {
244     return simLogger().setDefaultConsoleTarget();
245 }
246
247 bool SimulatorManager::setFileLogger(const std::string &path)
248 {
249     return simLogger().setDefaultFileTarget(path);
250 }