-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalaos_update.cpp
182 lines (153 loc) · 5.58 KB
/
calaos_update.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/******************************************************************************
** Copyright (c) 2007-2008, Calaos. All Rights Reserved.
**
** This file is part of Calaos Home.
**
** Calaos Home is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**
** Calaos Home is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Foobar; if not, write to the Free Software
** Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**
******************************************************************************/
//
// Check firmware version and update it if needed.
//
#include <Utils.h>
#include <EcoreTimer.h>
#include <Firmwares.h>
static Firmwares firmwares;
static EcoreTimer *timer = NULL;
static void checkUpdate();
static void checkUpdate_cb(string msg);
static void downloadUpdate();
static void downloadUpdate_cb(string msg, FileProgress *progress);
static void installUpdate();
static void installUpdate_cb(string msg);
static void checkUpdate()
{
if (timer)
{
delete timer;
timer = NULL;
}
firmwares.checkUpdate(sigc::ptr_fun(checkUpdate_cb));
}
static void checkUpdate_cb(string msg)
{
if (msg == "failed")
{
Utils::logger("root") << Priority::ERROR
<< "Failed to check update"
<< log4cpp::eol;
ecore_main_loop_quit();
}
else if (msg == "no_update")
{
Utils::logger("root") << Priority::INFO
<< "System is up to date"
<< log4cpp::eol;
ecore_main_loop_quit();
}
else
{
//update needed
Utils::logger("root") << Priority::INFO
<< "Update available: " << msg
<< log4cpp::eol;
downloadUpdate();
}
}
static double counter;
static void downloadUpdate()
{
counter = ecore_time_get();
firmwares.downloadFirmware(sigc::ptr_fun(downloadUpdate_cb));
}
static int old_pourcent = -1;
static void downloadUpdate_cb(string msg, FileProgress *progress)
{
if (msg == "failed")
{
Utils::logger("root") << Priority::ERROR
<< "Failed to download update"
<< log4cpp::eol;
ecore_main_loop_quit();
}
else if (msg == "progress,update" && progress)
{
if (progress->dltotal == 0.0) progress->dltotal = 1.0;
int pourcent = (int)((progress->dlnow / progress->dltotal) * 100.0);
int elapsed = (int)(ecore_time_get() - counter);
static int old_elapsed = 0;
static string tps = "waiting...";
if (elapsed != old_elapsed)
{
double length = ((double)elapsed * 100) / (double)pourcent;
length -= elapsed;
if (progress->dlnow == 0)
tps = "Connexion en cours...";
else
tps = "Temps restant: " + Utils::time2string((long)length);
old_elapsed = elapsed;
}
stringstream size;
progress->dlnow = progress->dlnow / 1000000;
progress->dltotal = progress->dltotal / 1000000;
if (progress->dltotal < 10) progress->dltotal = 0;
size << setprecision(2) << progress->dlnow << " / " << progress->dltotal << " Mo";
if (pourcent != old_pourcent)
{
Utils::logger("root") << Priority::INFO
<< "Downloading: " << size.str() << " (" << pourcent << "%) - " << tps
<< log4cpp::eol;
old_pourcent = pourcent;
}
}
else if (msg == "done")
{
Utils::logger("root") << Priority::INFO
<< "Download done."
<< log4cpp::eol;
installUpdate();
}
}
static void installUpdate()
{
firmwares.installFirmware(sigc::ptr_fun(installUpdate_cb));
}
static void installUpdate_cb(string msg)
{
if (msg == "done")
{
Utils::logger("root") << Priority::INFO
<< "Install done."
<< log4cpp::eol;
}
else
{
Utils::logger("root") << Priority::INFO
<< "Install Failed !"
<< log4cpp::eol;
}
ecore_main_loop_quit();
}
int main (int argc, char **argv)
{
ecore_init();
ecore_con_init();
Utils::InitLoggingSystem(string(DEFAULT_CONFIG_PATH) + "calaos_console_log.conf");
timer = new EcoreTimer(0.0, (sigc::slot<void>)sigc::ptr_fun(checkUpdate));
ecore_main_loop_begin();
ecore_con_shutdown();
ecore_shutdown();
return 0;
}