iotivity 0.9.0
[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 namespace OIC
39 {
40
41     /// Declearation of Diagnostics Callback funtion type
42     typedef std::function<
43             void(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
44             > DiagnosticsCallback;
45
46     /**
47      *  @brief
48      *  The following class is used as a item stacking in request queue. The class stores a request
49      *  and referential information (e.g., a diagnostics name, a target resource object, a callback
50      *  function passed from the applications, and a update value). When the function for updating/
51      *  getting diagnostics value is called from applications, this class instance is created and
52      *  stored in the request queue. The queue is maintained in a std::map structure so if desiring
53      *  to find a specific request, you can find it by querying a diagnostics name.
54      */
55     class DiagnosticsRequestEntry
56     {
57     public:
58         DiagnosticsRequestEntry(std::string ID, DiagnosticsCallback callback,
59                 std::shared_ptr< OCResource > resource, std::string updateVal) :
60                 m_ID(ID), m_callback(callback), m_resource(resource), m_updateVal(updateVal)
61         {
62         }
63         ;
64
65         // Diagnostics Name (used in key value in std::map structure)
66         // e.g., reboot and factoryset
67         std::string m_ID;
68
69         // Reference callback pointer
70         DiagnosticsCallback m_callback;
71
72         // Reference resource object
73         std::shared_ptr< OCResource > m_resource;
74
75         // Update value only used for diagnostics update (always "true")
76         std::string m_updateVal;
77     };
78
79     /**
80      *  @brief
81      *  The following class is used to store providing diagnostics name and its relevant information
82      *  The relevant information includes a brief description, uri, and attribute key.
83      *  Note that a developer only specifies a diagnostics name, not URI nor attribute key, to
84      *  update a value to a remote. Thus, using diagnostics name, we convert it to more specific
85      *  information (i.e. uri and attribute key) to send a request. This class is reponsible to
86      *  storing these information.
87      */
88     class DiagnosticsUnitInfo
89     {
90     public:
91         DiagnosticsUnitInfo(std::string name, std::string description, std::string uri,
92                 std::string attribute) :
93                 m_name(name), m_description(description), m_uri(uri), m_attribute(attribute)
94         {
95         }
96         ;
97
98         std::string m_name;
99         std::string m_description;
100         std::string m_uri;
101         std::string m_attribute;
102
103         // If a developer wants to know a list of diagnostics names, gives it in JSON format.
104         std::string getJSON()
105         {
106             std::string res;
107
108             res = "{\"name\":\"" + m_name + "\",\"description\":\"" + m_description + "\"}";
109
110             return res;
111         }
112     };
113
114 #define NUMDIAGUNIT 3
115     typedef std::string DiagnosticsName;
116     typedef std::string DiagnosticsValue;
117
118     class ThingsDiagnostics
119     {
120     public:
121         /**
122          * Constructor for ThingsDiagnostics. Constructs a new ThingsDiagnostics
123          */
124         ThingsDiagnostics(void)
125         {
126             DiagnosticsUnitInfo unit[] =
127                     {
128                     { "reboot", "reboot", "/oic/diag/0/reboot", "value" },
129                             { "value",
130                                     "Collecting any device statistics",
131                                     "/oic/diag/0/startCollection", "value" },
132                             { "factoryreset", "restore all configuration values to default values",
133                                     "/oic/diag/0/factoryReset", "value" } };
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,
201                 const OCRepresentation& rep, const int eCode, std::string conf);
202         void onGetChildInfoForUpdate(const HeaderOptions& headerOptions,
203                 const OCRepresentation& rep, 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__*/