38e5269155e064b8bb3639a3bfc69d74316d66ba
[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 "resource_manager.h"
23 #include "simulator_client.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<SimulatorResourceServer> SimulatorManager::createResource(
47     const std::string &configPath,
48     SimulatorResourceServer::ResourceModelChangedCB callback)
49 {
50     return ResourceManager::getInstance()->createResource(configPath, callback);
51 }
52
53 std::vector<std::shared_ptr<SimulatorResourceServer>> SimulatorManager::createResource(
54             const std::string &configPath, unsigned short count,
55             SimulatorResourceServer::ResourceModelChangedCB callback)
56 {
57     return ResourceManager::getInstance()->createResource(configPath, count, callback);
58 }
59
60 std::vector<std::shared_ptr<SimulatorResourceServer>> SimulatorManager::getResources(
61             const std::string &resourceType)
62 {
63     return ResourceManager::getInstance()->getResources(resourceType);
64 }
65
66 void SimulatorManager::deleteResource(
67     const std::shared_ptr<SimulatorResourceServer> &resource)
68 {
69     ResourceManager::getInstance()->deleteResource(resource);
70 }
71
72 void SimulatorManager::deleteResource(const std::string &resourceType)
73 {
74     ResourceManager::getInstance()->deleteResources(resourceType);
75 }
76
77 void SimulatorManager::findResource(ResourceFindCallback callback)
78 {
79     SimulatorClient::getInstance()->findResources(callback);
80 }
81
82 void SimulatorManager::findResource(const std::string &resourceType,
83                                      ResourceFindCallback callback)
84 {
85     SimulatorClient::getInstance()->findResources(resourceType, callback);
86 }
87
88 void SimulatorManager::getDeviceInfo(DeviceInfoCallback callback)
89 {
90     if (!callback)
91         throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
92
93     OC::FindDeviceCallback deviceCallback = [this, callback](const OC::OCRepresentation & rep)
94     {
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");
99
100         DeviceInfo deviceInfo(deviceName, deviceID, deviceSpecVersion, deviceDMV);
101         callback(deviceInfo);
102     };
103
104     std::ostringstream uri;
105     uri << OC_MULTICAST_PREFIX << OC_RSRVD_DEVICE_URI;
106
107     typedef OCStackResult (*GetDeviceInfo)(const std::string &, const std::string &,
108                                            OCConnectivityType, OC::FindDeviceCallback);
109
110     invokeocplatform(static_cast<GetDeviceInfo>(OC::OCPlatform::getDeviceInfo), "",
111                      uri.str(),
112                      CT_DEFAULT,
113                      deviceCallback);
114 }
115
116 void SimulatorManager::setDeviceInfo(const std::string &deviceName)
117 {
118     if (deviceName.empty())
119         throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "Device name is empty!");
120
121
122     typedef OCStackResult (*RegisterDeviceInfo)(const OCDeviceInfo);
123
124     OCDeviceInfo ocDeviceInfo;
125     ocDeviceInfo.deviceName = const_cast<char *>(deviceName.c_str());
126     invokeocplatform(static_cast<RegisterDeviceInfo>(OC::OCPlatform::registerDeviceInfo),
127                      ocDeviceInfo);
128 }
129
130 void SimulatorManager::getPlatformInfo(PlatformInfoCallback callback)
131 {
132     if (!callback)
133         throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
134
135     OC::FindPlatformCallback platformCallback = [this, callback](const OC::OCRepresentation & rep)
136     {
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"));
149
150         callback(platformInfo);
151     };
152
153     std::ostringstream uri;
154     uri << OC_MULTICAST_PREFIX << OC_RSRVD_PLATFORM_URI;
155
156     typedef OCStackResult (*GetPlatformInfo)(const std::string &, const std::string &,
157             OCConnectivityType, OC::FindPlatformCallback);
158
159     invokeocplatform(static_cast<GetPlatformInfo>(OC::OCPlatform::getPlatformInfo), "",
160                      uri.str(),
161                      CT_DEFAULT,
162                      platformCallback);
163 }
164
165 void SimulatorManager::setPlatformInfo(PlatformInfo &platformInfo)
166 {
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());
179
180     typedef OCStackResult (*RegisterPlatformInfo)(const OCPlatformInfo);
181     invokeocplatform(static_cast<RegisterPlatformInfo>(OC::OCPlatform::registerPlatformInfo),
182                      ocPlatformInfo);
183 }
184
185 void SimulatorManager::setLogger(const std::shared_ptr<ILogger> &logger)
186 {
187     simLogger().setCustomTarget(logger);
188 }
189
190 bool SimulatorManager::setConsoleLogger()
191 {
192     return simLogger().setDefaultConsoleTarget();
193 }
194
195 bool SimulatorManager::setFileLogger(const std::string &path)
196 {
197     return simLogger().setDefaultFileTarget(path);
198 }