a14447072daf79eaaaad41815ca92a66bfffbdfd
[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 /**
22  * @file
23  *
24  * This file contains the declaration of classes and its members related to
25  * ThingsDiagnostics.
26  */
27
28 #ifndef __OC_THINGSDIAGNOSTICS__
29 #define __OC_THINGSDIAGNOSTICS__
30
31 #include <string>
32 #include <vector>
33 #include <map>
34 #include <cstdlib>
35 #include "OCPlatform.h"
36 #include "OCApi.h"
37 #include "GroupManager.h"
38
39 using namespace OC;
40 namespace OIC
41 {
42     /// Declearation of Diagnostics Callback funtion type
43     typedef std::function<
44             void(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
45             > DiagnosticsCallback;
46
47     /**
48      *  @brief
49      *  The following class is used as a item stacking in request queue. The class stores a request
50      *  and referential information (e.g., a diagnostics name, a target resource object, a callback
51      *  function passed from the applications, and a update value). When the function for updating/
52      *  getting diagnostics value is called from applications, this class instance is created and
53      *  stored in the request queue. The queue is maintained in a std::map structure so if desiring
54      *  to find a specific request, you can find it by querying a diagnostics name.
55      */
56     class DiagnosticsRequestEntry
57     {
58     public:
59         DiagnosticsRequestEntry(std::string ID, DiagnosticsCallback callback,
60                 std::shared_ptr< OCResource > resource, std::string updateVal);
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
83      *  storing these information.
84      */
85     class DiagnosticsUnitInfo
86     {
87     public:
88         std::string m_name;
89         std::string m_attribute;
90         std::string m_uri;
91
92         DiagnosticsUnitInfo(std::string name, std::string attribute, std::string uri);
93
94         // If a developer wants to know a list of configuration names, gives it in JSON format.
95         std::string getJSON();
96     };
97
98 #define NUMDIAGUNIT 3
99     typedef std::string DiagnosticsName;
100     typedef std::string DiagnosticsValue;
101
102     class ThingsDiagnostics
103     {
104     public:
105         /**
106          * Constructor for ThingsDiagnostics. Constructs a new ThingsDiagnostics
107          */
108         ThingsDiagnostics(void);
109
110         /**
111          * Virtual destructor
112          */
113         ~ThingsDiagnostics(void);
114
115         static ThingsDiagnostics *thingsDiagnosticsInstance;
116         static ThingsDiagnostics* getInstance();
117         void deleteInstance();
118
119         void setGroupManager(GroupManager *groupmanager);
120
121         /**
122          * API to make things reboot
123          * Callback call when a response arrives.
124          *
125          * @param resource - resource pointer representing the target group
126          * @param callback - callback.
127          *
128          * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
129          *
130          * NOTE: OCStackResult is defined in ocstack.h.
131          */
132         OCStackResult reboot(std::shared_ptr< OCResource > resource, DiagnosticsCallback callback);
133
134         /**
135          * API for factory reset on device
136          * Callback call when a response arrives.
137          *
138          * @param resource - resource pointer representing the target group
139          * @param callback - callback.
140          *
141          * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
142          *
143          * NOTE: OCStackResult is defined in ocstack.h.
144          */
145
146         OCStackResult factoryReset(std::shared_ptr< OCResource > resource,
147                 DiagnosticsCallback callback);
148
149         /**
150          * API to show a list of supported diagnostics units
151          * Callback call when a response arrives.
152          *
153          * @return the list in JSON format
154          */
155         std::string getListOfSupportedDiagnosticsUnits();
156
157     private:
158
159         GroupManager *g_groupmanager;
160
161         std::vector< DiagnosticsUnitInfo > DiagnosticsUnitTable;
162
163         void onExecuteForGroupAction(const HeaderOptions& headerOptions,
164                 const OCRepresentation& rep, const int eCode, std::string conf);
165         void onDeleteGroupAction(const HeaderOptions& headerOptions,
166                 const OCRepresentation& rep, const int eCode, std::string conf);
167         void onGetChildInfoForUpdate(const HeaderOptions& headerOptions,
168                 const OCRepresentation& rep, const int eCode, std::string conf);
169         void onCreateActionSet(const HeaderOptions& headerOptions, const OCRepresentation& rep,
170                 const int eCode, std::string conf);
171         void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode,
172                 std::string conf);
173         void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode,
174                 std::string conf);
175
176         std::shared_ptr< OCResource > getResource(std::string conf);
177         DiagnosticsCallback getCallback(std::string conf);
178         std::string getUpdateVal(std::string conf);
179         std::string getAttributeByDiagnosticsName(DiagnosticsName name);
180         std::string getUriByDiagnosticsName(DiagnosticsName name);
181
182         std::string getHostFromURI(std::string oldUri);
183
184         bool isSimpleResource(std::shared_ptr< OCResource > resource);
185
186     };
187 }
188 #endif  /* __OC_THINGSCONFIGURATION__*/