4b10c1451b703e8feffc23d6eb073918f6bda7cf
[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         SimulatorResourceFactory::getInstance()->createResource(configPath);
53     if (!resource)
54         throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
55     return resource;
56 }
57
58 std::vector<std::shared_ptr<SimulatorResource>> SimulatorManager::createResource(
59             const std::string &configPath, unsigned int count)
60 {
61     VALIDATE_INPUT(configPath.empty(), "Empty path!")
62     VALIDATE_INPUT(!count, "Count is zero!")
63
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!");
68     return resources;
69 }
70
71 std::shared_ptr<SimulatorSingleResource> SimulatorManager::createSingleResource(
72     const std::string &name, const std::string &uri, const std::string &resourceType)
73 {
74     VALIDATE_INPUT(name.empty(), "Empty resource name!")
75     VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
76
77     return SimulatorResourceFactory::getInstance()->createSingleResource(name, uri, resourceType);
78 }
79
80 std::shared_ptr<SimulatorCollectionResource> SimulatorManager::createCollectionResource(
81     const std::string &name, const std::string &uri, const std::string &resourceType)
82 {
83     VALIDATE_INPUT(name.empty(), "Empty resource name!")
84     VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
85
86     return SimulatorResourceFactory::getInstance()->createCollectionResource(name, uri, resourceType);
87 }
88
89 void SimulatorManager::findResource(ResourceFindCallback callback)
90 {
91     VALIDATE_CALLBACK(callback)
92
93     OC::FindCallback findCallback = std::bind(
94                                         [](std::shared_ptr<OC::OCResource> ocResource, ResourceFindCallback callback)
95     {
96         if (!ocResource)
97             return;
98
99         SimulatorRemoteResourceSP simulatorResource(new SimulatorRemoteResourceImpl(ocResource));
100         callback(simulatorResource);
101     }, std::placeholders::_1, callback);
102
103     typedef OCStackResult (*FindResource)(const std::string &, const std::string &,
104                                           OCConnectivityType, OC::FindCallback);
105
106     invokeocplatform(static_cast<FindResource>(OC::OCPlatform::findResource), "",
107                      OC_MULTICAST_DISCOVERY_URI, CT_DEFAULT, findCallback);
108 }
109
110 void SimulatorManager::findResource(const std::string &resourceType,
111                                     ResourceFindCallback callback)
112 {
113     VALIDATE_INPUT(resourceType.empty(), "Empty resource type!")
114     VALIDATE_CALLBACK(callback)
115
116     OC::FindCallback findCallback = std::bind(
117                                         [](std::shared_ptr<OC::OCResource> ocResource, ResourceFindCallback callback)
118     {
119         if (!ocResource)
120             return;
121
122         SimulatorRemoteResourceSP simulatorResource(new SimulatorRemoteResourceImpl(ocResource));
123         callback(simulatorResource);
124     }, std::placeholders::_1, callback);
125
126     std::ostringstream query;
127     query << OC_MULTICAST_DISCOVERY_URI << "?rt=" << resourceType;
128
129     typedef OCStackResult (*FindResource)(const std::string &, const std::string &,
130                                           OCConnectivityType, OC::FindCallback);
131
132     invokeocplatform(static_cast<FindResource>(OC::OCPlatform::findResource), "", query.str(),
133                      CT_DEFAULT, findCallback);
134 }
135
136 void SimulatorManager::getDeviceInfo(const std::string &host, DeviceInfoCallback callback)
137 {
138     VALIDATE_CALLBACK(callback)
139
140     OC::FindDeviceCallback deviceCallback = std::bind(
141             [](const OC::OCRepresentation & rep, DeviceInfoCallback callback)
142     {
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");
147
148         DeviceInfo deviceInfo(deviceName, deviceID, deviceSpecVersion, deviceDMV);
149         callback(deviceInfo);
150     }, std::placeholders::_1, callback);
151
152     typedef OCStackResult (*GetDeviceInfo)(const std::string &, const std::string &,
153                                            OCConnectivityType, OC::FindDeviceCallback);
154
155     invokeocplatform(static_cast<GetDeviceInfo>(OC::OCPlatform::getDeviceInfo), host.c_str(),
156                      "/oic/d", CT_DEFAULT, deviceCallback);
157 }
158
159 void SimulatorManager::setDeviceInfo(const std::string &deviceName)
160 {
161     VALIDATE_INPUT(deviceName.empty(), "Empty resource type!")
162
163     typedef OCStackResult (*RegisterDeviceInfo)(const OCDeviceInfo);
164
165     OCDeviceInfo ocDeviceInfo;
166     ocDeviceInfo.deviceName = const_cast<char *>(deviceName.c_str());
167     invokeocplatform(static_cast<RegisterDeviceInfo>(OC::OCPlatform::registerDeviceInfo),
168                      ocDeviceInfo);
169 }
170
171 void SimulatorManager::getPlatformInfo(const std::string &host, PlatformInfoCallback callback)
172 {
173     VALIDATE_CALLBACK(callback)
174
175     OC::FindPlatformCallback platformCallback = std::bind(
176                 [](const OC::OCRepresentation & rep, PlatformInfoCallback callback)
177     {
178         PlatformInfo platformInfo;
179         platformInfo.setPlatformID(rep.getValue<std::string>("pi"));
180         platformInfo.setPlatformVersion(rep.getValue<std::string>("mnpv"));
181         platformInfo.setManufacturerName(rep.getValue<std::string>("mnmn"));
182         platformInfo.setManufacturerUrl(rep.getValue<std::string>("mnml"));
183         platformInfo.setModelNumber(rep.getValue<std::string>("mnmo"));
184         platformInfo.setDateOfManfacture(rep.getValue<std::string>("mndt"));
185         platformInfo.setOSVersion(rep.getValue<std::string>("mnos"));
186         platformInfo.setHardwareVersion(rep.getValue<std::string>("mnhw"));
187         platformInfo.setFirmwareVersion(rep.getValue<std::string>("mnfv"));
188         platformInfo.setSupportUrl(rep.getValue<std::string>("mnsl"));
189         platformInfo.setSystemTime(rep.getValue<std::string>("st"));
190
191         callback(platformInfo);
192     }, std::placeholders::_1, callback);
193
194     typedef OCStackResult (*GetPlatformInfo)(const std::string &, const std::string &,
195             OCConnectivityType, OC::FindPlatformCallback);
196
197     invokeocplatform(static_cast<GetPlatformInfo>(OC::OCPlatform::getPlatformInfo), host.c_str(),
198                      "/oic/p", CT_DEFAULT, platformCallback);
199 }
200
201 void SimulatorManager::setPlatformInfo(PlatformInfo &platformInfo)
202 {
203     OCPlatformInfo ocPlatformInfo;
204     ocPlatformInfo.platformID = const_cast<char *>(platformInfo.getPlatformID().c_str());
205     ocPlatformInfo.manufacturerName = const_cast<char *>(platformInfo.getManufacturerName().c_str());
206     ocPlatformInfo.manufacturerUrl = const_cast<char *>(platformInfo.getManufacturerUrl().c_str());
207     ocPlatformInfo.modelNumber = const_cast<char *>(platformInfo.getModelNumber().c_str());
208     ocPlatformInfo.dateOfManufacture = const_cast<char *>(platformInfo.getDateOfManfacture().c_str());
209     ocPlatformInfo.platformVersion = const_cast<char *>(platformInfo.getPlatformVersion().c_str());
210     ocPlatformInfo.operatingSystemVersion = const_cast<char *>(platformInfo.getOSVersion().c_str());
211     ocPlatformInfo.hardwareVersion = const_cast<char *>(platformInfo.getHardwareVersion().c_str());
212     ocPlatformInfo.firmwareVersion = const_cast<char *>(platformInfo.getFirmwareVersion().c_str());
213     ocPlatformInfo.supportUrl = const_cast<char *>(platformInfo.getSupportUrl().c_str());
214     ocPlatformInfo.systemTime = const_cast<char *>(platformInfo.getSystemTime().c_str());
215
216     typedef OCStackResult (*RegisterPlatformInfo)(const OCPlatformInfo);
217     invokeocplatform(static_cast<RegisterPlatformInfo>(OC::OCPlatform::registerPlatformInfo),
218                      ocPlatformInfo);
219 }
220
221 void SimulatorManager::setLogger(const std::shared_ptr<ILogger> &logger)
222 {
223     simLogger().setCustomTarget(logger);
224 }
225
226 bool SimulatorManager::setConsoleLogger()
227 {
228     return simLogger().setDefaultConsoleTarget();
229 }
230
231 bool SimulatorManager::setFileLogger(const std::string &path)
232 {
233     return simLogger().setDefaultFileTarget(path);
234 }