Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / tizen / ConServerApp / src / diagnosticsresource.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 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 #include "diagnosticsresource.h"
22
23 #include <functional>
24 #include <dlog.h>
25
26 #include "conserverapp.h"
27 #include "OCPlatform.h"
28 #include "OCApi.h"
29 #include "ThingsManager.h"
30
31 extern string logMessage;
32
33 extern void *updateLog(void *);
34
35 using namespace OC;
36
37 // Constructor
38 DiagnosticsResource::DiagnosticsResource() :
39     m_factoryReset(defaultFactoryReset), m_reboot(defaultReboot),
40     m_startStatCollection(defaultStartStatCollection)
41 {
42     m_diagnosticsUri = "/oic/diag"; // URI of the resource
43     m_diagnosticsTypes.push_back("oic.diag"); // resource type name
44     m_diagnosticsInterfaces.push_back(DEFAULT_INTERFACE); // resource interface
45     m_diagnosticsRep.setValue("fr", m_factoryReset);
46     m_diagnosticsRep.setValue("rb", m_reboot);
47     m_diagnosticsRep.setValue("ssc", m_startStatCollection);
48     m_diagnosticsRep.setUri(m_diagnosticsUri);
49     m_diagnosticsRep.setResourceTypes(m_diagnosticsTypes);
50     m_diagnosticsRep.setResourceInterfaces(m_diagnosticsInterfaces);
51     m_diagnosticsHandle = NULL;
52 }
53
54 // Creates a DiagnosticResource
55 void DiagnosticsResource::createResource(ResourceEntityHandler callback)
56 {
57     using namespace OC::OCPlatform;
58
59     if (NULL == callback)
60     {
61         dlog_print(DLOG_INFO, "DiagnosticsResource", "#### Callback should be binded");
62         return;
63     }
64
65     // This will internally create and register the resource
66     OCStackResult result = registerResource(m_diagnosticsHandle, m_diagnosticsUri,
67                                             m_diagnosticsTypes[0], m_diagnosticsInterfaces[0],
68                                             callback, OC_DISCOVERABLE | OC_OBSERVABLE);
69
70     if (OC_STACK_OK != result)
71     {
72         dlog_print(DLOG_INFO, "DiagnosticsResource", "#### Resource creation"
73                    "(configuration) was unsuccessful");
74         return;
75     }
76
77     thread exec(
78         std::function< void(int second) >(
79             std::bind(&DiagnosticsResource::diagnosticsMonitor, this,
80                       std::placeholders::_1)), 1);
81     exec.detach();
82
83     dlog_print(DLOG_INFO, "DiagnosticsResource", "#### Diagnostics Resource is Created");
84 }
85
86 void DiagnosticsResource::setDiagnosticsRepresentation(OCRepresentation &rep)
87 {
88     string value;
89
90     if (rep.getValue("fr", value))
91     {
92         m_factoryReset = value;
93         dlog_print(DLOG_INFO, "DiagnosticsResource", "#### m_factoryReset: %s",
94                    m_factoryReset.c_str());
95     }
96
97     if (rep.getValue("rb", value))
98     {
99         m_reboot = value;
100         dlog_print(DLOG_INFO, "DiagnosticsResource", "#### m_reboot: %s", m_reboot.c_str());
101     }
102
103     if (rep.getValue("ssc", value))
104     {
105         m_startStatCollection = value;
106         dlog_print(DLOG_INFO, "DiagnosticsResource", "#### m_startStatCollection: %s",
107                    m_startStatCollection.c_str());
108     }
109 }
110
111 OCRepresentation DiagnosticsResource::getDiagnosticsRepresentation()
112 {
113     m_diagnosticsRep.setValue("fr", m_factoryReset);
114     m_diagnosticsRep.setValue("rb", m_reboot);
115     m_diagnosticsRep.setValue("ssc", m_startStatCollection);
116
117     return m_diagnosticsRep;
118 }
119
120 std::string DiagnosticsResource::getUri()
121 {
122     return m_diagnosticsUri;
123 }
124
125 // Handles the Reboot and FactoryReset request
126 void DiagnosticsResource::diagnosticsMonitor(int second)
127 {
128     while (1)
129     {
130         sleep(second);
131
132         if (m_reboot == "true")
133         {
134             int res;
135             dlog_print(DLOG_INFO, "DiagnosticsResource", "#### Reboot will be soon...");
136             m_reboot = defaultReboot;
137             res = system("sudo reboot"); // System reboot
138
139             dlog_print(DLOG_INFO, "DiagnosticsResource", "#### return: %d", res);
140
141             logMessage = "----------------------------<br>";
142             logMessage += "*** System Reboot Success ***<br>";
143
144             dlog_print(DLOG_INFO, LOG_TAG, "  %s", logMessage.c_str());
145             //Show the log
146             ecore_main_loop_thread_safe_call_sync(updateLog, &logMessage);
147
148         }
149         else if (m_factoryReset == "true")
150         {
151             dlog_print(DLOG_INFO, "DiagnosticsResource", "#### Factory Reset will be soon...");
152             m_factoryReset = defaultFactoryReset;
153             factoryReset();
154         }
155     }
156 }
157
158 // Deletes the diagnostic resource which has been created using createResource()
159 void DiagnosticsResource::deleteResource()
160 {
161     // Unregister the resource
162     if (NULL != m_diagnosticsHandle)
163     {
164         OCPlatform::unregisterResource(m_diagnosticsHandle);
165     }
166 }