-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrestoretask.cpp
More file actions
129 lines (119 loc) · 3.45 KB
/
restoretask.cpp
File metadata and controls
129 lines (119 loc) · 3.45 KB
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
#include "restoretask.h"
#include "signetcliapplication.h"
extern "C" {
#include "signetdev/host/signetdev.h"
}
#include <iostream>
#include <string>
void restoreTask::help()
{
std::cout << "Restores a backup file onto the device" << std::endl
<< std::endl
<< "Usage: signet-cli restore <backup file name>" << std::endl << std::endl;
}
restoreTask::restoreTask(int argc, char *argv[])
{
if (argc > 0) {
m_file.setFileName(QString(argv[0]));
bool rc = m_file.open(QIODevice::ReadOnly);
if (!rc) {
std::cout << "Failed to open \"" << argv[0] << "\"" << std::endl;
} else {
if (m_file.size() != (NUM_STORAGE_BLOCKS * BLK_SIZE)) {
std::cout << "Backup file \"" << argv[0] << "\" is the wrong size" << std::endl;
m_file.close();
}
}
m_blockNum = 0;
} else {
std::cout << "No restore source file specified" << std::endl;
}
}
bool restoreTask::canStart()
{
return m_file.isOpen();
}
bool restoreTask::start()
{
int token;
::signetdev_get_device_state(NULL, &token);
return true;
}
void restoreTask::cmdResponse(void *cb_user_param, int cmd_token, int cmd, int end_device_state, int messages_remaining, int resp_code, void *resp_data)
{
Q_UNUSED(cb_user_param);
Q_UNUSED(cmd_token);
Q_UNUSED(messages_remaining);
Q_UNUSED(end_device_state);
int token;
switch(cmd) {
case SIGNETDEV_CMD_STARTUP: {
std::cout << "Press and HOLD the device button to begin restoring backup" << std::endl;
::signetdev_begin_device_restore(NULL, &token);
} break;
case SIGNETDEV_CMD_GET_DEVICE_STATE: {
switch (end_device_state) {
case LOGGED_OUT:
case UNINITIALIZED:
std::cout << "Press and HOLD the device button to begin restoring backup" << std::endl;
::signetdev_begin_device_restore(NULL, &token);
break;
case DISCONNECTED:
::signetdev_startup(NULL, &token);
break;
case LOGGED_IN:
std::cout << "Device is locked. The device must be unlocked to restore a backup" << std::endl;
QCoreApplication::quit();
break;
default:
std::cout << "The device must be unlocked to restore a backup" << std::endl;
QCoreApplication::quit();
break;
}
} break;
case SIGNETDEV_CMD_BEGIN_DEVICE_RESTORE: {
switch (resp_code) {
case OKAY: {
std::cout << "Restoring backup, this make take awhile..." << std::endl;
QByteArray block = m_file.read(BLK_SIZE);
if (block.size() != BLK_SIZE) {
std::cout << "Failed to read from backup file" << std::endl;
::signetdev_end_device_restore(NULL, &token);
} else {
::signetdev_write_block(NULL, &token, m_blockNum, block.data());
}
} break;
default:
std::cout << "Failed to start restoring backup up file" << std::endl;
QCoreApplication::quit();
}
} break;
case SIGNETDEV_CMD_WRITE_BLOCK: {
switch (resp_code) {
case OKAY: {
m_blockNum++;
if (m_blockNum == NUM_STORAGE_BLOCKS) {
std::cout << "Backup restoration complete" << std::endl;
::signetdev_end_device_restore(NULL, &token);
} else {
QByteArray block = m_file.read(BLK_SIZE);
if (block.size() != BLK_SIZE) {
std::cout << "Failed to read from backup file" << std::endl;
::signetdev_end_device_restore(NULL, &token);
} else {
::signetdev_write_block(NULL, &token, m_blockNum, block.data());
}
}
} break;
default:
std::cout << "Failed to write block while restoreing backup" << std::endl;
::signetdev_end_device_restore(NULL, &token);
break;
}
} break;
case SIGNETDEV_CMD_END_DEVICE_RESTORE:
m_file.close();
QCoreApplication::quit();
break;
}
}