Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / tizen / ConServerApp / src / maintenanceresource.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 "maintenanceresource.h"
22
23 #include <functional>
24 #include <dlog.h>
25 #include <thread>
26 #include <string>
27
28 #include "conserverapp.h"
29 #include "OCPlatform.h"
30 #include "OCApi.h"
31
32 using namespace OC;
33
34 extern std::string logMessage;
35
36 extern void *updateLog(void *);
37
38 static std::string defaultMntURI = "/oic/mnt";
39 static std::string defaultMntResourceType = "oic.wk.mnt";
40
41 // Constructor
42 MaintenanceResource::MaintenanceResource() :
43     m_factoryReset(defaultFactoryReset), m_reboot(defaultReboot),
44     m_startStatCollection(defaultStartStatCollection)
45 {
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;
56 }
57
58 // Creates a DiagnosticResource
59 void MaintenanceResource::createResource(ResourceEntityHandler callback)
60 {
61     using namespace OC::OCPlatform;
62
63     if (NULL == callback)
64     {
65         dlog_print(DLOG_INFO, "MaintenanceResource", "#### Callback should be binded");
66         return;
67     }
68
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);
73
74     if (OC_STACK_OK != result)
75     {
76         dlog_print(DLOG_INFO, "MaintenanceResource", "#### Resource creation"
77                    "(maintenance) was unsuccessful");
78         return;
79     }
80
81     std::thread exec(
82         std::function< void(int second) >(
83             std::bind(&MaintenanceResource::maintenanceMonitor, this,
84                       std::placeholders::_1)), 1);
85     exec.detach();
86
87     dlog_print(DLOG_INFO, "MaintenanceResource", "#### maintenance Resource is Created");
88 }
89
90 void MaintenanceResource::setMaintenanceRepresentation(OCRepresentation &rep)
91 {
92     std::string value;
93
94     if (rep.getValue(DEFAULT_FACTORYRESET, value))
95     {
96         m_factoryReset = value;
97         dlog_print(DLOG_INFO, "MaintenanceResource", "#### m_factoryReset: %s",
98                    m_factoryReset.c_str());
99     }
100
101     if (rep.getValue(DEFAULT_REBOOT, value))
102     {
103         m_reboot = value;
104         dlog_print(DLOG_INFO, "MaintenanceResource", "#### m_reboot: %s", m_reboot.c_str());
105     }
106
107     if (rep.getValue(DEFAULT_STARTCOLLECTION, value))
108     {
109         m_startStatCollection = value;
110         dlog_print(DLOG_INFO, "MaintenanceResource", "#### m_startStatCollection: %s",
111                    m_startStatCollection.c_str());
112     }
113 }
114
115 OCRepresentation MaintenanceResource::getMaintenanceRepresentation()
116 {
117     m_maintenanceRep.setValue(DEFAULT_FACTORYRESET, m_factoryReset);
118     m_maintenanceRep.setValue(DEFAULT_REBOOT, m_reboot);
119     m_maintenanceRep.setValue(DEFAULT_STARTCOLLECTION, m_startStatCollection);
120
121     return m_maintenanceRep;
122 }
123
124 std::string MaintenanceResource::getUri()
125 {
126     return m_maintenanceUri;
127 }
128
129 // Handles the Reboot and FactoryReset request
130 void MaintenanceResource::maintenanceMonitor(int second)
131 {
132     while (1)
133     {
134         sleep(second);
135
136         if (m_reboot == "true")
137         {
138             int res;
139             dlog_print(DLOG_INFO, "MaintenanceResource", "#### Reboot will be soon...");
140             m_reboot = defaultReboot;
141             res = system("sudo reboot"); // System reboot
142
143             dlog_print(DLOG_INFO, "MaintenanceResource", "#### return: %d", res);
144
145             logMessage = "----------------------------<br>";
146             logMessage += "*** System Reboot Success ***<br>";
147
148             dlog_print(DLOG_INFO, LOG_TAG, "  %s", logMessage.c_str());
149             //Show the log
150             ecore_main_loop_thread_safe_call_sync(updateLog, &logMessage);
151
152         }
153         else if (m_factoryReset == "true")
154         {
155             dlog_print(DLOG_INFO, "MaintenanceResource", "#### Factory Reset will be soon...");
156             m_factoryReset = defaultFactoryReset;
157             factoryReset();
158         }
159     }
160 }
161
162 // Deletes the diagnostic resource which has been created using createResource()
163 void MaintenanceResource::deleteResource()
164 {
165     // Unregister the resource
166     if (NULL != m_maintenanceHandle)
167     {
168         OCPlatform::unregisterResource(m_maintenanceHandle);
169     }
170 }