1 //******************************************************************
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
27 #include <OCPlatform.h>
30 #include "ThingsDiagnostics.h"
36 std::map< std::string, DiagnosticsRequestEntry > diagnosticsRequestTable;
37 ThingsDiagnostics* ThingsDiagnostics::thingsDiagnosticsInstance = NULL;
39 DiagnosticsRequestEntry::DiagnosticsRequestEntry(std::string ID, DiagnosticsCallback callback,
40 std::shared_ptr< OCResource > resource, std::string updateVal)
43 m_callback = callback;
44 m_resource = resource;
45 m_updateVal = updateVal;
48 DiagnosticsUnitInfo::DiagnosticsUnitInfo(std::string name,
49 std::string attribute,
53 m_attribute = attribute;
57 std::string DiagnosticsUnitInfo::getJSON()
61 res = "{\"name\":\"" + m_name + "\",\"attribute\":\"" + m_attribute + "\"}";
66 ThingsDiagnostics::ThingsDiagnostics()
68 DiagnosticsUnitInfo unit[] =
70 { "rb", "Reboot", "/oic/diag"},
71 { "ssc", "StartStatCollection", "/oic/diag"},
72 { "fr", "Factory Reset", "/oic/diag" } };
74 for (int i = 0; i < NUMDIAGUNIT; i++)
75 DiagnosticsUnitTable.push_back(unit[i]);
78 ThingsDiagnostics::~ThingsDiagnostics()
82 void ThingsDiagnostics::setGroupManager(GroupManager *groupmanager)
84 g_groupmanager = groupmanager;
87 ThingsDiagnostics* ThingsDiagnostics::getInstance()
89 if (thingsDiagnosticsInstance == NULL)
91 thingsDiagnosticsInstance = new ThingsDiagnostics();
93 return thingsDiagnosticsInstance;
96 void ThingsDiagnostics::deleteInstance()
98 if (thingsDiagnosticsInstance)
100 delete thingsDiagnosticsInstance;
101 thingsDiagnosticsInstance = NULL;
105 std::string ThingsDiagnostics::getAttributeByDiagnosticsName(DiagnosticsName name)
107 for (auto it = DiagnosticsUnitTable.begin(); DiagnosticsUnitTable.end() != it; it++)
109 if ((*it).m_name == name)
110 return (*it).m_attribute;
116 std::string ThingsDiagnostics::getUriByDiagnosticsName(DiagnosticsName name)
118 for (auto it = DiagnosticsUnitTable.begin(); DiagnosticsUnitTable.end() != it; it++)
120 if ((*it).m_name == name)
127 std::string ThingsDiagnostics::getUpdateVal(std::string diag)
129 std::map< std::string, DiagnosticsRequestEntry >::iterator it =
130 diagnosticsRequestTable.find(diag);
132 if (it == diagnosticsRequestTable.end())
135 return it->second.m_updateVal;
138 std::shared_ptr< OCResource > ThingsDiagnostics::getResource(std::string diag)
140 std::map< std::string, DiagnosticsRequestEntry >::iterator it =
141 diagnosticsRequestTable.find(diag);
143 if (it == diagnosticsRequestTable.end())
146 return it->second.m_resource;
149 DiagnosticsCallback ThingsDiagnostics::getCallback(std::string diag)
151 std::map< std::string, DiagnosticsRequestEntry >::iterator it =
152 diagnosticsRequestTable.find(diag);
154 if (it == diagnosticsRequestTable.end())
157 return it->second.m_callback;
160 std::string ThingsDiagnostics::getHostFromURI(std::string oldUri)
165 if ((f = oldUri.find("/factoryset/oic/")) != string::npos)
166 newUri = oldUri.replace(f, oldUri.size(), "");
167 else if ((f = oldUri.find("/oic/")) != string::npos)
168 newUri = oldUri.replace(f, oldUri.size(), "");
173 std::string ThingsDiagnostics::getListOfSupportedDiagnosticsUnits()
177 res = "{\"Diagnostics Units\":[";
179 auto it = DiagnosticsUnitTable.begin();
182 res = res + (*it).getJSON();
185 if (it == DiagnosticsUnitTable.end())
196 void ThingsDiagnostics::onGetChildInfoForUpdate(const HeaderOptions& headerOptions,
197 const OCRepresentation& rep, const int eCode, std::string diag)
199 if (eCode != OC_STACK_OK)
201 std::cout << "onGet Response error: " << eCode << std::endl;
202 getCallback(diag)(headerOptions, rep, eCode);
206 std::cout << "GET request was successful" << std::endl;
208 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
210 std::vector < OCRepresentation > children = rep.getChildren();
211 for (auto oit = children.begin(); oit != children.end(); ++oit)
213 std::cout << "\t\tChild Resource URI: " << oit->getUri() << std::endl;
216 // Get information by using diagnostics name(diag)
217 std::shared_ptr < OCResource > resource = getResource(diag);
218 std::string actionstring = diag;
219 std::string uri = getUriByDiagnosticsName(diag);
220 std::string attrKey = diag;
227 // In this nest, we create a new action set of which name is the dignostics name.
228 // Required information consists of a host address, URI, attribute key, and
230 ActionSet *newActionSet = new ActionSet();
231 newActionSet->actionsetName = diag;
233 for (auto oit = children.begin(); oit != children.end(); ++oit)
235 Action *newAction = new Action();
237 // oit->getUri() includes a host address as well as URI.
238 // We should split these to each other and only use the host address to create
239 // a child resource's URI. Note that the collection resource and its child
240 // resource are located in same host.
241 newAction->target = getHostFromURI(oit->getUri()) + uri;
243 Capability *newCapability = new Capability();
244 newCapability->capability = attrKey;
245 newCapability->status = getUpdateVal(diag);
247 newAction->listOfCapability.push_back(newCapability);
248 newActionSet->listOfAction.push_back(newAction);
251 // Request to create a new action set by using the above actionSet
252 g_groupmanager->addActionSet(resource, newActionSet,
254 void(const HeaderOptions& headerOptions,
255 const OCRepresentation& rep, const int eCode) >(
256 std::bind(&ThingsDiagnostics::onCreateActionSet, this,
257 std::placeholders::_1, std::placeholders::_2,
258 std::placeholders::_3, diag)));
260 delete(newActionSet);
265 void ThingsDiagnostics::onCreateActionSet(const HeaderOptions& headerOptions,
266 const OCRepresentation& rep, const int eCode, std::string diag)
268 if (eCode != OC_STACK_OK)
270 std::cout << "onPut Response error: " << eCode << std::endl;
271 getCallback(diag)(headerOptions, rep, eCode);
275 std::cout << "PUT request was successful" << std::endl;
277 std::shared_ptr < OCResource > resource = getResource(diag);
280 // Now, it is time to execute the action set.
281 g_groupmanager->executeActionSet(resource, diag,
283 void(const HeaderOptions& headerOptions,
284 const OCRepresentation& rep, const int eCode) >(
285 std::bind(&ThingsDiagnostics::onExecuteForGroupAction, this,
286 std::placeholders::_1, std::placeholders::_2,
287 std::placeholders::_3, diag)));
291 void ThingsDiagnostics::onExecuteForGroupAction(const HeaderOptions& headerOptions,
292 const OCRepresentation& rep, const int eCode, std::string diag)
294 if (eCode != OC_STACK_OK)
296 std::cout << "onPut Response error: " << eCode << std::endl;
297 getCallback(diag)(headerOptions, rep, eCode);
301 std::cout << "PUT request was successful" << std::endl;
302 getCallback(diag)(headerOptions, rep, eCode);
304 // Delete the created actionset
305 std::shared_ptr < OCResource > resource = getResource(diag);
308 g_groupmanager->deleteActionSet(resource, diag,
310 void(const HeaderOptions& headerOptions,
311 const OCRepresentation& rep, const int eCode) >(
312 std::bind(&ThingsDiagnostics::onDeleteGroupAction, this,
313 std::placeholders::_1, std::placeholders::_2,
314 std::placeholders::_3, diag)));
318 void ThingsDiagnostics::onDeleteGroupAction(const HeaderOptions& headerOptions,
319 const OCRepresentation& rep, const int eCode, std::string diag)
321 if (eCode != OC_STACK_OK)
323 std::cout << "Delete actionset returned with error: " << eCode << diag << std::endl;
327 std::cout << "Deleted the actionset created!" << diag<< std::endl;
330 void ThingsDiagnostics::onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep,
331 const int eCode, std::string diag)
333 if (eCode != OC_STACK_OK)
335 std::cout << "onPut Response error: " << eCode << std::endl;
336 getCallback(diag)(headerOptions, rep, eCode);
340 std::cout << "PUT request was successful" << std::endl;
342 getCallback(diag)(headerOptions, rep, eCode);
346 bool ThingsDiagnostics::isSimpleResource(std::shared_ptr< OCResource > resource)
348 for (unsigned int i = 0; i < resource->getResourceTypes().size(); ++i)
350 if (resource->getResourceTypes().at(0).find(".resourceset", 0) != std::string::npos)
357 OCStackResult ThingsDiagnostics::reboot(std::shared_ptr< OCResource > resource,
358 DiagnosticsCallback callback)
362 std::cout << "resource is NULL\n";
363 return OC_STACK_ERROR;
366 std::string diag = "rb";
368 // Check the request queue if a previous request is still left. If so, remove it.
369 std::map< std::string, DiagnosticsRequestEntry >::iterator iter =
370 diagnosticsRequestTable.find(diag);
371 if (iter != diagnosticsRequestTable.end())
372 diagnosticsRequestTable.erase(iter);
374 // Create new request entry stored in the queue
375 DiagnosticsRequestEntry newCallback(diag, callback, resource, "true");
376 diagnosticsRequestTable.insert(std::make_pair(diag, newCallback));
378 QueryParamsMap query;
379 OCRepresentation rep;
381 if (isSimpleResource(resource))
383 // This resource is a simple resource. Just send a PUT message
384 OCRepresentation rep;
385 rep.setValue < std::string > (diag, "true");
387 return resource->put(resource->getResourceTypes().at(0), DEFAULT_INTERFACE, rep, query,
389 void(const HeaderOptions& headerOptions, const OCRepresentation& rep,
391 std::bind(&ThingsDiagnostics::onPut, this, std::placeholders::_1,
392 std::placeholders::_2, std::placeholders::_3, diag)));
396 // This resource is a collection resource. It just acquires child resource's URI and
397 // send GET massages to the child resources in turn.
398 // First, request the child resources's URI.
399 // TODO: Add a deletion of actionset
400 return resource->get(resource->getResourceTypes().at(0), DEFAULT_INTERFACE, query,
402 void(const HeaderOptions& headerOptions, const OCRepresentation& rep,
404 std::bind(&ThingsDiagnostics::onGetChildInfoForUpdate, this,
405 std::placeholders::_1, std::placeholders::_2,
406 std::placeholders::_3, diag)));
410 OCStackResult ThingsDiagnostics::factoryReset(std::shared_ptr< OCResource > resource,
411 DiagnosticsCallback callback)
415 std::cout << "resource is NULL\n";
416 return OC_STACK_ERROR;
419 std::string diag = "fr";
421 // Check the request queue if a previous request is still left. If so, remove it.
422 std::map< std::string, DiagnosticsRequestEntry >::iterator iter =
423 diagnosticsRequestTable.find(diag);
424 if (iter != diagnosticsRequestTable.end())
425 diagnosticsRequestTable.erase(iter);
427 // Create new request entry stored in the queue
428 DiagnosticsRequestEntry newCallback(diag, callback, resource, "true");
429 diagnosticsRequestTable.insert(std::make_pair(diag, newCallback));
431 QueryParamsMap query;
432 OCRepresentation rep;
434 if (isSimpleResource(resource))
436 // This resource is a simple resource. Just send a PUT message
437 OCRepresentation rep;
438 rep.setValue < std::string > ("value", "true");
440 return resource->put(resource->getResourceTypes().at(0), DEFAULT_INTERFACE, rep, query,
442 void(const HeaderOptions& headerOptions, const OCRepresentation& rep,
444 std::bind(&ThingsDiagnostics::onPut, this, std::placeholders::_1,
445 std::placeholders::_2, std::placeholders::_3, diag)));
449 // This resource is a collection resource. It just acquires child resource's URI and
450 // send GET massages to the child resources in turn.
451 // First, request the child resources's URI.
452 // TODO: Add a deletion of actionset
453 return resource->get(resource->getResourceTypes().at(0), DEFAULT_INTERFACE, query,
455 void(const HeaderOptions& headerOptions, const OCRepresentation& rep,
457 std::bind(&ThingsDiagnostics::onGetChildInfoForUpdate, this,
458 std::placeholders::_1, std::placeholders::_2,
459 std::placeholders::_3, diag)));