-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDlgDevicesMemory.cpp
More file actions
341 lines (305 loc) · 8.22 KB
/
DlgDevicesMemory.cpp
File metadata and controls
341 lines (305 loc) · 8.22 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "stdafx.h"
#include "KRS.h"
#include "DlgDevicesMemory.h"
#include "DlgHardCfg.h"
#include "src\LOG_V0.h"
#include <io.h>
#include <sys/stat.h>
#define WM_MEM_MSG (WM_USER+1)
IMPLEMENT_DYNAMIC(DlgDevicesMemory, CDialog)
CString DlgDevicesMemory::m_cfg_name;
DlgDevicesMemory::DlgDevicesMemory(bool download, CWnd* pParent):
CDialog(DlgDevicesMemory::IDD, pParent), m_download(download)
{
}
DlgDevicesMemory::~DlgDevicesMemory()
{
}
void DlgDevicesMemory::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_PROGRESS_DEVICES, m_progress_devices);
DDX_Control(pDX, IDC_PROGRESS_MEMORY, m_progress_memory);
DDX_Control(pDX, IDC_LIST_LOG, m_list_log);
}
BEGIN_MESSAGE_MAP(DlgDevicesMemory, CDialog)
ON_MESSAGE(WM_MEM_MSG, OnMessage)
ON_WM_TIMER()
END_MESSAGE_MAP()
byte g_mem[7*256 + 256];
int g_devices_counter;
CString g_cfg_path;
SERV_Device* g_prev_device = NULL;
bool g_errors_occured, g_cancel_as_abort;
int g_num_of_OK_devices;
BOOL DlgDevicesMemory::OnInitDialog()
{
CDialog::OnInitDialog();
SetWindowText(m_download?" Ïîëó÷åíèå äàííûõ îò óñòðîéñòâ":" Âîññòàíîâëåíèå êîíôèãóðàöèè óñòðîéñòâ");
g_prev_device = NULL;
g_errors_occured = false;
g_num_of_OK_devices = 0;
g_cancel_as_abort = true;
if (m_download)
{
char str[2048];
GetCurrentDirectory(2048, str);
CString prev_dir = str;
SetCurrentDirectory(KRS_root_path);
CreateDirectory("CFG", NULL);
SetCurrentDirectory(KRS_root_path + "\\CFG");
CreateDirectory("tmp.files", NULL);
g_cfg_path = KRS_root_path + "\\CFG\\tmp.files";
SetCurrentDirectory(g_cfg_path);
WIN32_FIND_DATA file_data;
HANDLE file_handle = FindFirstFile("*.*", &file_data);
while (file_handle != INVALID_HANDLE_VALUE)
{
if ((file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
{
_chmod(file_data.cFileName, _S_IREAD | _S_IWRITE); // äåëàåì ôàéë íå READ_ONLY
remove(file_data.cFileName);
}
if (FindNextFile(file_handle, &file_data) == 0)
file_handle = INVALID_HANDLE_VALUE;
}
SetCurrentDirectory(prev_dir);
AddLog("Çàãðóçêà êîíôèãóðàöèè èç óñòðîéñòâ:");
}
else
{
g_cfg_path = KRS_root_path + "\\CFG\\" + m_cfg_name + ".files";
AddLog(CString("Âîññòàíîâëåíèå êîíôèãóðàöèè \"") + m_cfg_name + "\":");
}
g_devices_counter = 0;
m_progress_memory.SetRange32(0, 2047);
MutexWrap cfg_access(SERV_current_device_cfg_mutex);
int count = SERV_current_device_cfg->GetDevicesCount();
if (count > 0)
{
m_progress_devices.SetRange32(0, count);
StartDevice(SERV_current_device_cfg->GetDevicesBegin()->second);
SetTimer(1, 100, NULL);
}
else
{
AddLog("Âíóòðåííÿÿ êîíôèãóðàöèÿ ïóñòà!");
MessageBox("Âíóòðåííÿÿ êîíôèãóðàöèÿ ïóñòà, ðàáîòà ñ óñòðîéñòâàìè íåâîçìîæíà", "Íåò íè îäíîãî óñòðîéñòâà");
EndDialog(IDCANCEL);
}
return TRUE;
}
void DlgDevicesMemory::OnOK()
{
}
void DlgDevicesMemory::OnCancel()
{
if (!g_cancel_as_abort)
CDialog::OnCancel();
else
if (MessageBox("Ïðåðâàòü ïðîöåññ îáìåíà äàííûìè ñ óñòðîéñòâàìè?", " Ïðåðûâàíèå", MB_YESNO) == IDYES)
{
if (::IsWindow(m_hWnd))
{
SERV_StopMemoryHandling(true);
CDialog::OnCancel();
}
}
}
void DlgDevicesMemory::StartDevice(SERV_Device* device)
{
g_prev_device = device;
m_progress_devices.SetPos(g_devices_counter);
m_progress_memory.SetPos(0);
SetDlgItemText(IDC_STATIC_DEVICE_INFO, device->GetInfo());
SetDlgItemText(IDC_STATIC_MEMORY_INFO, "0/2048");
if (m_download)
SERV_RequestMemory(m_hWnd, WM_MEM_MSG, device->GetAddr(), g_mem);
else
{
CString name = g_cfg_path + (TXT("\\dev_%02X") << device->GetAddr());
FILE *f = fopen(name, "rb");
if (f)
{
fread(g_mem, 7*256, 1, f);
fclose(f);
}
else
{
g_errors_occured = true;
AddNegativeLog(CString("-- Îøèáêà ÷òåíèÿ ñ äèñêà êîíôèãóðàöèè óñòðîéñòâà \"") + device->GetInfo() + "\"!");
StartNext();
return;
}
SERV_UploadMemory(m_hWnd, WM_MEM_MSG, device->GetAddr(), g_mem);
}
}
LRESULT DlgDevicesMemory::OnMessage(WPARAM wp, LPARAM lp)
{
if (SERV_current_device_cfg == NULL)
{
EndDialog(IDCANCEL);
return -1;
}
if (wp == 0)
{
MutexWrap cfg_access(SERV_current_device_cfg_mutex);
if (g_prev_device != NULL)
{
if (m_download)
{
AddLog(CString("+ Êîíôèãóðàöèÿ óñòðîéñòâà \"") + g_prev_device->GetInfo() + "\" ïîëó÷åíà");
CString name = g_cfg_path + (TXT("\\dev_%02X") << g_prev_device->GetAddr());
FILE *f = fopen(name, "wb");
if (f)
{
fwrite(g_mem, 7*256, 1, f);
fclose(f);
_chmod(name, _S_IREAD);
g_num_of_OK_devices++;
}
else
{
AddNegativeLog(CString("-- Îøèáêà çàïèñè íà äèñê êîíôèãóðàöèè óñòðîéñòâà \"") + g_prev_device->GetInfo() + "\"!");
g_errors_occured = true;
}
}
else
{
AddLog(CString("+ Êîíôèãóðàöèÿ óñòðîéñòâà \"") + g_prev_device->GetInfo() + "\" âîññòàíîâëåíà");
g_num_of_OK_devices++;
}
}
StartNext();
}
return +1;
}
bool SaveCfg(CString cfg_name)
{
bool errors_occure = false;
char str[2048];
GetCurrentDirectory(2048, str);
CString prev_dir = str;
CString src_path = KRS_root_path + "\\CFG\\tmp.files";
CString dst_path = KRS_root_path + "\\CFG\\" + cfg_name + ".files";
SetCurrentDirectory(KRS_root_path + "\\CFG");
CreateDirectory(cfg_name + ".files", NULL);
SetCurrentDirectory(src_path);
CString file_name;
WIN32_FIND_DATA file_data;
HANDLE file_handle = FindFirstFile("*.*", &file_data);
while (file_handle != INVALID_HANDLE_VALUE)
{
if ((file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
{
file_name = file_data.cFileName;
if (!CopyFile(src_path + "\\" + file_name, dst_path + "\\" + file_name, TRUE))
{
MessageBox(NULL, src_path + "\\" + file_name, "Îøèáêà êîïèðîâàíèÿ ôàéëà", MB_OK);
errors_occure = true;
}
}
if (FindNextFile(file_handle, &file_data) == 0)
file_handle = INVALID_HANDLE_VALUE;
}
file_handle = CreateFile(KRS_root_path + "\\CFG\\" + cfg_name,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY,
NULL);
CloseHandle(file_handle);
SetCurrentDirectory(prev_dir);
return !errors_occure;
}
void DlgDevicesMemory::StartNext()
{
SERV_StopMemoryHandling(false);
g_devices_counter++;
if (SERV_current_device_cfg->GetDevicesCount() <= g_devices_counter)
{
KillTimer(1);
m_progress_devices.SetPos(g_devices_counter);
m_progress_memory.SetPos(2048);
SetDlgItemText(IDC_STATIC_DEVICE_INFO, "îïðîñ çàâåðøåí");
SetDlgItemText(IDC_STATIC_MEMORY_INFO, "ðàáîòà çàâåðøåíà");
if (m_download && g_num_of_OK_devices > 0)
{
DlgHardCfg dlg(true);
if (dlg.DoModal() == IDOK)
{
AddLog(CString("Ñîõðàíåíèå êîíôèãóðàöèè \"") + dlg.m_cfg_name + "\":");
if (SaveCfg(dlg.m_cfg_name))
AddLog("+ Êîíôèãóðàöèÿ ñîõðàíåíà óñïåøíî");
else
{
g_errors_occured = true;
AddLog("-- Îøèáêà ñîõðàíåíèÿ êîíôèãóðàöèè");
}
}
else
{
AddLog("Ñîõðàíåíèå êîíôèãóðàöèè:");
g_errors_occured = true;
AddLog("-- Êîíôèãóðàöèÿ íå áûëà ñîõðàíåíà");
}
}
//if (g_errors_occured)
{
SetDlgItemText(IDCANCEL, "Çàêðûòü");
g_cancel_as_abort = false;
}
//else
// EndDialog(IDOK);
}
else
{
SERV_DevicesMap::iterator current = SERV_current_device_cfg->GetDevicesBegin();
for (int i = 0; i < g_devices_counter; i++)
current++;
StartDevice(current->second);
}
}
void DlgDevicesMemory::AddLog(CString log)
{
m_list_log.AddString(log);
LOG_V0_AddMessage(LOG_V0_MESSAGE, log, true, true);
}
void DlgDevicesMemory::AddNegativeLog(CString log)
{
AddLog(log);
AddLog("-- Êîíôèãóðàöèÿ äëÿ \"" + g_prev_device->GetInfo() + "\" íå " + ((m_download)?"ñîõðàíåíà":"âîññòàíîâëåíà"));
}
void DlgDevicesMemory::OnTimer(UINT nIDEvent)
{
if (nIDEvent == 1)
{
int addr = SERV_GetCurrentMemoryAddr();
if (addr == 0xFFFF)
addr = 0;
m_progress_memory.SetPos(addr);
SetDlgItemText(IDC_STATIC_MEMORY_INFO, (CString)(TXT("%d/2048")<<addr));
if (SERV_GetRepeatOfLastCommand() >= 3)
{
g_errors_occured = true;
AddNegativeLog(CString("-- Óñòðîéñòâî \"") + g_prev_device->GetInfo() + "\" íå îòâå÷àåò");
StartNext();
}
}
}
void DlgDevicesMemory_UploadMem()
{
DlgHardCfg dlg1(false);
if (dlg1.DoModal() == IDOK)
{
DlgDevicesMemory::m_cfg_name = dlg1.m_cfg_name;
DlgDevicesMemory dlg2(false);
dlg2.DoModal();
}
}
void DlgDevicesMemory_DownloadMem()
{
DlgDevicesMemory dlg(true);
dlg.DoModal();
}