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 "maintenanceresource.h"
28 #include "conserverapp.h"
29 #include "OCPlatform.h"
34 extern std::string logMessage;
36 extern void *updateLog(void *);
38 static std::string defaultMntURI = "/oic/mnt";
39 static std::string defaultMntResourceType = "oic.wk.mnt";
42 MaintenanceResource::MaintenanceResource() :
43 m_factoryReset(defaultFactoryReset), m_reboot(defaultReboot),
44 m_startStatCollection(defaultStartStatCollection)
46 m_maintenanceUri = defaultMntURI; // URI of the resource
47 m_maintenanceTypes.push_back(defaultMntResourceType); // resource type name
48 m_maintenanceInterfaces.push_back(DEFAULT_INTERFACE); // resource interface
49 m_maintenanceRep.setValue(DEFAULT_FACTORYRESET, m_factoryReset);
50 m_maintenanceRep.setValue(DEFAULT_REBOOT, m_reboot);
51 m_maintenanceRep.setValue(DEFAULT_STARTCOLLECTION, m_startStatCollection);
52 m_maintenanceRep.setUri(m_maintenanceUri);
53 m_maintenanceRep.setResourceTypes(m_maintenanceTypes);
54 m_maintenanceRep.setResourceInterfaces(m_maintenanceInterfaces);
55 m_maintenanceHandle = NULL;
58 // Creates a DiagnosticResource
59 void MaintenanceResource::createResource(ResourceEntityHandler callback)
61 using namespace OC::OCPlatform;
65 dlog_print(DLOG_INFO, "MaintenanceResource", "#### Callback should be binded");
69 // This will internally create and register the resource
70 OCStackResult result = registerResource(m_maintenanceHandle, m_maintenanceUri,
71 m_maintenanceTypes[0], m_maintenanceInterfaces[0],
72 callback, OC_DISCOVERABLE | OC_OBSERVABLE);
74 if (OC_STACK_OK != result)
76 dlog_print(DLOG_INFO, "MaintenanceResource", "#### Resource creation"
77 "(maintenance) was unsuccessful");
82 std::function< void(int second) >(
83 std::bind(&MaintenanceResource::maintenanceMonitor, this,
84 std::placeholders::_1)), 1);
87 dlog_print(DLOG_INFO, "MaintenanceResource", "#### maintenance Resource is Created");
90 void MaintenanceResource::setMaintenanceRepresentation(OCRepresentation &rep)
94 if (rep.getValue(DEFAULT_FACTORYRESET, value))
96 m_factoryReset = value;
97 dlog_print(DLOG_INFO, "MaintenanceResource", "#### m_factoryReset: %s",
98 m_factoryReset.c_str());
101 if (rep.getValue(DEFAULT_REBOOT, value))
104 dlog_print(DLOG_INFO, "MaintenanceResource", "#### m_reboot: %s", m_reboot.c_str());
107 if (rep.getValue(DEFAULT_STARTCOLLECTION, value))
109 m_startStatCollection = value;
110 dlog_print(DLOG_INFO, "MaintenanceResource", "#### m_startStatCollection: %s",
111 m_startStatCollection.c_str());
115 OCRepresentation MaintenanceResource::getMaintenanceRepresentation()
117 m_maintenanceRep.setValue(DEFAULT_FACTORYRESET, m_factoryReset);
118 m_maintenanceRep.setValue(DEFAULT_REBOOT, m_reboot);
119 m_maintenanceRep.setValue(DEFAULT_STARTCOLLECTION, m_startStatCollection);
121 return m_maintenanceRep;
124 std::string MaintenanceResource::getUri()
126 return m_maintenanceUri;
129 // Handles the Reboot and FactoryReset request
130 void MaintenanceResource::maintenanceMonitor(int second)
136 if (m_reboot == "true")
139 dlog_print(DLOG_INFO, "MaintenanceResource", "#### Reboot will be soon...");
140 m_reboot = defaultReboot;
141 res = system("sudo reboot"); // System reboot
143 dlog_print(DLOG_INFO, "MaintenanceResource", "#### return: %d", res);
145 logMessage = "----------------------------<br>";
146 logMessage += "*** System Reboot Success ***<br>";
148 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
150 ecore_main_loop_thread_safe_call_sync(updateLog, &logMessage);
153 else if (m_factoryReset == "true")
155 dlog_print(DLOG_INFO, "MaintenanceResource", "#### Factory Reset will be soon...");
156 m_factoryReset = defaultFactoryReset;
162 // Deletes the diagnostic resource which has been created using createResource()
163 void MaintenanceResource::deleteResource()
165 // Unregister the resource
166 if (NULL != m_maintenanceHandle)
168 OCPlatform::unregisterResource(m_maintenanceHandle);