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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 /// @file ThingsDiagnostics.h
23 /// @brief This file contains the declaration of classes and its members related to
24 /// ThingsDiagnostics.
26 #ifndef __OC_THINGSDIAGNOSTICS__
27 #define __OC_THINGSDIAGNOSTICS__
33 #include "OCPlatform.h"
35 #include "GroupManager.h"
39 /// Declearation of Diagnostics Callback funtion type
40 typedef std::function<
41 void(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) > DiagnosticsCallback;
45 * The following class is used as a item stacking in request queue. The class stores a request and
46 * referential information (e.g., a diagnostics name, a target resource object, a callback functi-
47 * on passed from the applications, and a update value). When the function for updating/getting
48 * diagnostics value is called from applications, this class instance is created and stored in the
49 * request queue. The queue is maintained in a std::map structure so if desiring to find a specific
50 * request, you can find it by querying a diagnostics name.
52 class DiagnosticsRequestEntry
55 DiagnosticsRequestEntry(std::string ID, DiagnosticsCallback callback,
56 std::shared_ptr< OCResource > resource, std::string updateVal) :
57 m_ID(ID), m_callback(callback), m_resource(resource), m_updateVal(updateVal)
62 // Diagnostics Name (used in key value in std::map structure)
63 // e.g., reboot and factoryset
66 // Reference callback pointer
67 DiagnosticsCallback m_callback;
69 // Reference resource object
70 std::shared_ptr< OCResource > m_resource;
72 // Update value only used for diagnostics update (always "true")
73 std::string m_updateVal;
78 * The following class is used to store providing diagnostics name and its relevant information
79 * The relevant information includes a brief description, uri, and attribute key.
80 * Note that a developer only specifies a diagnostics name, not URI nor attribute key, to
81 * update a value to a remote. Thus, using diagnostics name, we convert it to more specific
82 * information (i.e. uri and attribute key) to send a request. This class is reponsible to storing
85 class DiagnosticsUnitInfo
88 DiagnosticsUnitInfo(std::string name, std::string description, std::string uri,
89 std::string attribute) :
90 m_name(name), m_description(description), m_uri(uri), m_attribute(attribute)
96 std::string m_description;
98 std::string m_attribute;
100 // If a developer wants to know a list of diagnostics names, gives it in JSON format.
101 std::string getJSON()
105 res = "{\"name\":\"" + m_name + "\",\"description\":\"" + m_description + "\"}";
111 #define NUMDIAGUNIT 3
112 typedef std::string DiagnosticsName;
113 typedef std::string DiagnosticsValue;
115 class ThingsDiagnostics
119 * Constructor for ThingsDiagnostics. Constructs a new ThingsDiagnostics
121 ThingsDiagnostics(void)
123 DiagnosticsUnitInfo unit[] =
127 "/oic/diag", "reboot" },
129 "Toggles between collecting and not collecting any device statistics depending on the value being 0 or 1",
130 "/oic/diag", "startCollection" },
132 "restore all configuration values to default values",
133 "/oic/diag", "factoryReset" } };
135 for (int i = 0; i < NUMDIAGUNIT; i++)
136 DiagnosticsUnitTable.push_back(unit[i]);
143 ~ThingsDiagnostics(void)
148 static ThingsDiagnostics *thingsDiagnosticsInstance;
149 static ThingsDiagnostics* getInstance();
150 void deleteInstance();
152 void setGroupManager(GroupManager *groupmanager)
154 g_groupmanager = groupmanager;
159 * API to make things reboot
160 * Callback call when a response arrives.
162 * @param resource - resource pointer representing the target group
163 * @param callback - callback.
165 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
167 * NOTE: OCStackResult is defined in ocstack.h.
169 OCStackResult reboot(std::shared_ptr< OCResource > resource, DiagnosticsCallback callback);
172 * API for factory reset on device
173 * Callback call when a response arrives.
175 * @param resource - resource pointer representing the target group
176 * @param callback - callback.
178 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
180 * NOTE: OCStackResult is defined in ocstack.h.
183 OCStackResult factoryReset(std::shared_ptr< OCResource > resource,
184 DiagnosticsCallback callback);
187 * API to show a list of supported diagnostics units
188 * Callback call when a response arrives.
190 * @return the list in JSON format
192 std::string getListOfSupportedDiagnosticsUnits();
196 GroupManager *g_groupmanager;
198 std::vector< DiagnosticsUnitInfo > DiagnosticsUnitTable;
200 void onExecuteForGroupAction(const HeaderOptions& headerOptions, const OCRepresentation& rep,
201 const int eCode, std::string conf);
202 void onGetChildInfoForUpdate(const HeaderOptions& headerOptions, const OCRepresentation& rep,
203 const int eCode, std::string conf);
204 void onCreateActionSet(const HeaderOptions& headerOptions, const OCRepresentation& rep,
205 const int eCode, std::string conf);
206 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode,
208 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode,
211 std::shared_ptr< OCResource > getResource(std::string conf);
212 DiagnosticsCallback getCallback(std::string conf);
213 std::string getUpdateVal(std::string conf);
214 std::string getAttributeByDiagnosticsName(DiagnosticsName name);
215 std::string getUriByDiagnosticsName(DiagnosticsName name);
217 std::string getHostFromURI(std::string oldUri);
219 bool isSimpleResource(std::shared_ptr< OCResource > resource);
223 #endif /* __OC_THINGSCONFIGURATION__*/