1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
21 #include "RCSResourceObject.h"
22 #include "OCPlatform.h"
24 using namespace OC::OCPlatform;
25 using namespace OIC::Service;
29 constexpr int RESOURCE_LIGHT = 1;
31 constexpr int DEFALUT_SERVER = 1;
32 constexpr int CUSTOM_SERVER = 2;
34 constexpr int INCREASE = 1;
35 constexpr int DECREASE = 2;
37 typedef void (*DisplayControlMenuFunc)();
38 typedef std::function<void()> Run;
41 bool g_isPresenceStarted = false;
43 RCSResourceObject::Ptr g_resource;
45 int processUserInput(int min, int max)
55 if (input == max + 1) throw CloseApp();
56 if (min <= input) return input;
60 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
62 throw std::runtime_error("Invalid Input, please try again");
65 void displayControlLightMenu()
67 std::cout << "========================================================\n";
68 std::cout << INCREASE << ". Increase Brightness by 1 stage \n";
69 std::cout << DECREASE << ". Decrease Brightness by 1 stage \n";
70 std::cout << DECREASE + 1 << ". Quit \n";
71 std::cout << "========================================================\n";
74 void printAttributes(const RCSResourceAttributes &attrs)
76 for (const auto &attr : attrs)
78 std::cout << "\tkey : " << attr.key() << "\n\tvalue : "
79 << attr.value().toString() << std::endl;
83 RCSGetResponse requestHandlerForGet(const RCSRequest &, RCSResourceAttributes &attrs)
85 std::cout << "Received a Get request from Client" << std::endl;
86 printAttributes(attrs);
89 RCSResourceObject::LockGuard lock(g_resource);
90 std::cout << "\nSending response to Client : " << std::endl;
91 printAttributes(g_resource->getAttributes());
94 return RCSGetResponse::defaultAction();
97 RCSSetResponse requestHandlerForSet(const RCSRequest &, RCSResourceAttributes &attrs)
99 std::cout << "Received a Set request from Client" << std::endl;
100 printAttributes(attrs);
102 return RCSSetResponse::defaultAction();
105 void initServer(const std::string &resourceUri, const std::string &resourceType,
106 const std::string &attrKey)
109 g_resource = RCSResourceObject::Builder(resourceUri, resourceType, "oic.if.baseline").
110 setDiscoverable(true).setObservable(true).setSecureFlag(true).build();
112 g_resource->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
113 g_resource->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER);
114 g_resource->setAttribute(attrKey, 0);
117 void updateAttribute(const std::string &attrKey, int control)
119 const int diff = control == INCREASE ? 1 : - 1;
122 RCSResourceObject::LockGuard lock(g_resource);
123 auto &attrs = g_resource->getAttributes();
124 attrs[attrKey] = attrs[attrKey].get<int>() + diff;
127 if (control == INCREASE)
129 std::cout << attrKey << " increased." << std::endl;
133 std::cout << attrKey << " decreased." << std::endl;
135 std::cout << "\nCurrent " << attrKey << ": "
136 << g_resource->getAttributeValue(attrKey).get<int>() << std::endl;
139 void runResourceControl(DisplayControlMenuFunc displayMenuFunc, const std::string &attrKey)
142 updateAttribute(attrKey, processUserInput(INCREASE, DECREASE));
145 void runResourceTypeSelection(int resourceMode)
147 std::cout << "========================================================\n";
148 std::cout << RESOURCE_LIGHT << ". Create a Secure Light Resource \n";
149 std::cout << RESOURCE_LIGHT + 1 << ". Quit \n";
150 std::cout << "========================================================\n";
152 int resourceType = processUserInput(RESOURCE_LIGHT, RESOURCE_LIGHT);
153 DisplayControlMenuFunc displayMenuFunc;
156 switch (resourceType)
159 attrKey = "Brightness";
160 initServer("/a/light", "oic.r.light", attrKey);
162 displayMenuFunc = displayControlLightMenu;
166 if (resourceMode == CUSTOM_SERVER)
168 g_resource->setGetRequestHandler(requestHandlerForGet);
169 g_resource->setSetRequestHandler(requestHandlerForSet);
172 g_currentRun = std::bind(runResourceControl, displayMenuFunc, std::move(attrKey));
175 void runResourceModeSelection()
177 std::cout << "======================================================== \n";
178 std::cout << DEFALUT_SERVER << ". Creation of Secure Resource Without Handlers \n";
179 std::cout << CUSTOM_SERVER << ". Creation of Secure Resource With Set and Get Handlers \n";
180 std::cout << CUSTOM_SERVER + 1 << ". Quit \n";
181 std::cout << "======================================================== \n";
183 g_currentRun = std::bind(runResourceTypeSelection,
184 processUserInput(DEFALUT_SERVER, CUSTOM_SERVER));
187 void runPresenceSelection()
189 constexpr int PRESENCE_ON = 1;
190 constexpr int PRESENCE_OFF = 2;
192 std::cout << "========================================================\n";
193 std::cout << PRESENCE_ON << ". Presence On \n";
194 std::cout << PRESENCE_OFF << ". Presence Off \n";
195 std::cout << PRESENCE_OFF + 1 << ". Quit \n";
196 std::cout << "========================================================\n";
198 if (processUserInput(PRESENCE_ON, PRESENCE_OFF) == PRESENCE_ON)
200 g_isPresenceStarted = true;
204 g_currentRun = runResourceModeSelection;
207 static FILE *client_open(const char * /*path*/, const char *mode)
209 return fopen("./oic_svr_db_server.json", mode);
214 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
216 OC::PlatformConfig cfg
218 OC::ServiceType::InProc, OC::ModeType::Server, "0.0.0.0", 0,
219 OC::QualityOfService::LowQos, &ps
222 OC::OCPlatform::Configure(cfg);
224 g_currentRun = runPresenceSelection;
232 catch (const std::exception &e)
234 std::cout << e.what() << std::endl;
236 catch (const CloseApp &)
241 std::cout << "Stopping the server" << std::endl;
245 if (g_isPresenceStarted)