Removed execute permissions from non-executable files.
[platform/upstream/iotivity.git] / service / things-manager / sdk / src / ThingsDiagnostics.h
1 //******************************************************************
2 //
3 // Copyright 2014 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 /// @file   ThingsDiagnostics.h
22
23 /// @brief  This file contains the declaration of classes and its members related to
24 ///         ThingsDiagnostics.
25
26 #ifndef __OC_THINGSDIAGNOSTICS__
27 #define __OC_THINGSDIAGNOSTICS__
28
29 #include <string>
30 #include <vector>
31 #include <map>
32 #include <cstdlib>
33 #include "OCPlatform.h"
34 #include "OCApi.h"
35 #include "GroupManager.h"
36
37 using namespace OC;
38
39 /// Declearation of Diagnostics Callback funtion type
40 typedef std::function<
41         void(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) > DiagnosticsCallback;
42
43 /**
44  *  @brief
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.
51  */
52 class DiagnosticsRequestEntry
53 {
54 public:
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)
58     {
59     }
60     ;
61
62     // Diagnostics Name (used in key value in std::map structure)
63     // e.g., reboot and factoryset
64     std::string m_ID;
65
66     // Reference callback pointer
67     DiagnosticsCallback m_callback;
68
69     // Reference resource object
70     std::shared_ptr< OCResource > m_resource;
71
72     // Update value only used for diagnostics update (always "true")
73     std::string m_updateVal;
74 };
75
76 /**
77  *  @brief
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
83  *  these information.
84  */
85 class DiagnosticsUnitInfo
86 {
87 public:
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)
91     {
92     }
93     ;
94
95     std::string m_name;
96     std::string m_description;
97     std::string m_uri;
98     std::string m_attribute;
99
100     // If a developer wants to know a list of diagnostics names, gives it in JSON format.
101     std::string getJSON()
102     {
103         std::string res;
104
105         res = "{\"name\":\"" + m_name + "\",\"description\":\"" + m_description + "\"}";
106
107         return res;
108     }
109 };
110
111 #define NUMDIAGUNIT 3
112 typedef std::string DiagnosticsName;
113 typedef std::string DiagnosticsValue;
114
115 class ThingsDiagnostics
116 {
117 public:
118     /**
119      * Constructor for ThingsDiagnostics. Constructs a new ThingsDiagnostics
120      */
121     ThingsDiagnostics(void)
122     {
123         DiagnosticsUnitInfo unit[] =
124         {
125             { "reboot",
126                 "reboot",
127                 "/oic/diag", "reboot" },
128             { "startcollection",
129                 "Toggles between collecting and not collecting any device statistics depending on the value being 0 or 1",
130                 "/oic/diag", "startCollection" },
131             { "factoryreset",
132                 "restore all configuration values to default values",
133                 "/oic/diag", "factoryReset" } };
134
135         for (int i = 0; i < NUMDIAGUNIT; i++)
136             DiagnosticsUnitTable.push_back(unit[i]);
137     }
138     ;
139
140     /**
141      * Virtual destructor
142      */
143     ~ThingsDiagnostics(void)
144     {
145     }
146     ;
147
148     static ThingsDiagnostics *thingsDiagnosticsInstance;
149     static ThingsDiagnostics* getInstance();
150     void deleteInstance();
151
152     void setGroupManager(GroupManager *groupmanager)
153     {
154         g_groupmanager = groupmanager;
155     }
156     ;
157
158     /**
159      * API to make things reboot
160      * Callback call when a response arrives.
161      *
162      * @param resource - resource pointer representing the target group
163      * @param callback - callback.
164      *
165      * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
166      *
167      * NOTE: OCStackResult is defined in ocstack.h.
168      */
169     OCStackResult reboot(std::shared_ptr< OCResource > resource, DiagnosticsCallback callback);
170
171     /**
172      * API for factory reset on device
173      * Callback call when a response arrives.
174      *
175      * @param resource - resource pointer representing the target group
176      * @param callback - callback.
177      *
178      * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
179      *
180      * NOTE: OCStackResult is defined in ocstack.h.
181      */
182
183     OCStackResult factoryReset(std::shared_ptr< OCResource > resource,
184             DiagnosticsCallback callback);
185
186     /**
187      * API to show a list of supported diagnostics units
188      * Callback call when a response arrives.
189      *
190      * @return the list in JSON format
191      */
192     std::string getListOfSupportedDiagnosticsUnits();
193
194 private:
195
196     GroupManager *g_groupmanager;
197
198     std::vector< DiagnosticsUnitInfo > DiagnosticsUnitTable;
199
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,
207             std::string conf);
208     void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode,
209             std::string conf);
210
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);
216
217     std::string getHostFromURI(std::string oldUri);
218
219     bool isSimpleResource(std::shared_ptr< OCResource > resource);
220
221 };
222
223 #endif  /* __OC_THINGSCONFIGURATION__*/
224