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