1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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 #include "diagnosticsresource.h"
26 #include "conserverapp.h"
27 #include "OCPlatform.h"
29 #include "ThingsManager.h"
31 extern string logMessage;
33 extern void *updateLog(void *);
38 DiagnosticsResource::DiagnosticsResource() :
39 m_factoryReset(defaultFactoryReset), m_reboot(defaultReboot),
40 m_startStatCollection(defaultStartStatCollection)
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;
54 // Creates a DiagnosticResource
55 void DiagnosticsResource::createResource(ResourceEntityHandler callback)
57 using namespace OC::OCPlatform;
61 dlog_print(DLOG_INFO, "DiagnosticsResource", "#### Callback should be binded");
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);
70 if (OC_STACK_OK != result)
72 dlog_print(DLOG_INFO, "DiagnosticsResource", "#### Resource creation"
73 "(configuration) was unsuccessful");
78 std::function< void(int second) >(
79 std::bind(&DiagnosticsResource::diagnosticsMonitor, this,
80 std::placeholders::_1)), 1);
83 dlog_print(DLOG_INFO, "DiagnosticsResource", "#### Diagnostics Resource is Created");
86 void DiagnosticsResource::setDiagnosticsRepresentation(OCRepresentation &rep)
90 if (rep.getValue("fr", value))
92 m_factoryReset = value;
93 dlog_print(DLOG_INFO, "DiagnosticsResource", "#### m_factoryReset: %s",
94 m_factoryReset.c_str());
97 if (rep.getValue("rb", value))
100 dlog_print(DLOG_INFO, "DiagnosticsResource", "#### m_reboot: %s", m_reboot.c_str());
103 if (rep.getValue("ssc", value))
105 m_startStatCollection = value;
106 dlog_print(DLOG_INFO, "DiagnosticsResource", "#### m_startStatCollection: %s",
107 m_startStatCollection.c_str());
111 OCRepresentation DiagnosticsResource::getDiagnosticsRepresentation()
113 m_diagnosticsRep.setValue("fr", m_factoryReset);
114 m_diagnosticsRep.setValue("rb", m_reboot);
115 m_diagnosticsRep.setValue("ssc", m_startStatCollection);
117 return m_diagnosticsRep;
120 std::string DiagnosticsResource::getUri()
122 return m_diagnosticsUri;
125 // Handles the Reboot and FactoryReset request
126 void DiagnosticsResource::diagnosticsMonitor(int second)
132 if (m_reboot == "true")
135 dlog_print(DLOG_INFO, "DiagnosticsResource", "#### Reboot will be soon...");
136 m_reboot = defaultReboot;
137 res = system("sudo reboot"); // System reboot
139 dlog_print(DLOG_INFO, "DiagnosticsResource", "#### return: %d", res);
141 logMessage = "----------------------------<br>";
142 logMessage += "*** System Reboot Success ***<br>";
144 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
146 ecore_main_loop_thread_safe_call_sync(updateLog, &logMessage);
149 else if (m_factoryReset == "true")
151 dlog_print(DLOG_INFO, "DiagnosticsResource", "#### Factory Reset will be soon...");
152 m_factoryReset = defaultFactoryReset;
158 // Deletes the diagnostic resource which has been created using createResource()
159 void DiagnosticsResource::deleteResource()
161 // Unregister the resource
162 if (NULL != m_diagnosticsHandle)
164 OCPlatform::unregisterResource(m_diagnosticsHandle);