-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlasherServerDlg.cpp
360 lines (306 loc) · 10.8 KB
/
FlasherServerDlg.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// FlasherServerDlg.cpp : fichier d'implémentation
//
#include "pch.h"
#include "framework.h"
#include "FlasherServer.h"
#include "FlasherServerDlg.h"
#include "afxdialogex.h"
#include "CEnumCOM.h"
#include "cServer.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// Fonction de Gestion du Thread de transfert des fichiers pour flash
UINT Flash(LPVOID pParam);
// boîte de dialogue CAboutDlg utilisée pour la boîte de dialogue 'À propos de' pour votre application
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// Données de boîte de dialogue
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // Prise en charge de DDX/DDV
// Implémentation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// boîte de dialogue de CFlasherServerDlg
CFlasherServerDlg::CFlasherServerDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_FLASHERSERVER_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}
void CFlasherServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST_FILE_TITRE, m_TitreListeFiles);
DDX_Control(pDX, IDC_LIST_COM_TITRE, m_TireListeCom);
DDX_Control(pDX, IDC_LIST_FILE, m_ListeFiles);
DDX_Control(pDX, IDC_LIST_COM, m_ListeCOM);
DDX_Control(pDX, IDC_PROGRESS1, m_Progress);
DDX_Control(pDX, IDC_EDIT1, m_Edit);
DDX_Control(pDX, IDC_FLASH, m_ButtonFlash);
}
BEGIN_MESSAGE_MAP(CFlasherServerDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ADD_FILE, &CFlasherServerDlg::OnBnClickedAddFile)
ON_BN_CLICKED(IDC_DELETE_FILE, &CFlasherServerDlg::OnBnClickedDeleteFile)
ON_BN_CLICKED(IDC_FLASH, &CFlasherServerDlg::OnBnClickedFlash)
ON_BN_CLICKED(IDC_REFRESH, &CFlasherServerDlg::OnBnClickedRefresh)
ON_LBN_SELCHANGE(IDC_LIST_COM, &CFlasherServerDlg::OnLbnSelchangeListCom)
END_MESSAGE_MAP()
// gestionnaires de messages de CFlasherServerDlg
BOOL CFlasherServerDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Ajouter l'élément de menu "À propos de..." au menu Système.
// IDM_ABOUTBOX doit se trouver dans la plage des commandes système.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Définir l'icône de cette boîte de dialogue. L'infrastructure effectue cela automatiquement
// lorsque la fenêtre principale de l'application n'est pas une boîte de dialogue
SetIcon(m_hIcon, TRUE); // Définir une grande icône
SetIcon(m_hIcon, FALSE); // Définir une petite icône
// TODO: ajoutez ici une initialisation supplémentaire
OnBnClickedRefresh();
m_Edit.SetWindowText(L"Choose :\r\n - a communication port,\r\n - the files to be transferred to\r\n daisy's QSPI Flash memory.\r\n\r\nClick Flash");
return TRUE; // retourne TRUE, sauf si vous avez défini le focus sur un contrôle
}
void CFlasherServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// Si vous ajoutez un bouton Réduire à votre boîte de dialogue, vous devez utiliser le code ci-dessous
// pour dessiner l'icône. Pour les applications MFC utilisant le modèle Document/Vue,
// cela est fait automatiquement par l'infrastructure.
void CFlasherServerDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // contexte de périphérique pour la peinture
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Centrer l'icône dans le rectangle client
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Dessiner l'icône
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
// Le système appelle cette fonction pour obtenir le curseur à afficher lorsque l'utilisateur fait glisser
// la fenêtre réduite.
HCURSOR CFlasherServerDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CFlasherServerDlg::OnBnClickedAddFile()
{
// TODO: ajoutez ici le code de votre gestionnaire de notification de contrôle
int NbFiles = m_ListeFiles.GetCount();
if (NbFiles >= DIR_FILE_COUNT) {
MessageBox(L"Maximum file number size reached", L"Error Add File", MB_OK | MB_ICONWARNING);
return;
}
CFileDialog FileDialog(true);
if (IDOK == FileDialog.DoModal()) {
CString NomFichier = FileDialog.GetPathName();
m_ListeFiles.InsertString(NbFiles, NomFichier);
}
}
void CFlasherServerDlg::OnBnClickedDeleteFile()
{
// TODO: ajoutez ici le code de votre gestionnaire de notification de contrôle
int Select = m_ListeFiles.GetCurSel();
CString NomFichier;
if (Select != -1) {
m_ListeFiles.GetText(Select, NomFichier);
if (IDYES == MessageBox(CString(L"Delete : " + NomFichier + L" ?"), L"Delete file in the list", MB_YESNO | MB_ICONWARNING)) {
m_ListeFiles.DeleteString(Select);
}
}
}
void CFlasherServerDlg::OnBnClickedFlash()
{
// TODO: ajoutez ici le code de votre gestionnaire de notification de contrôle
if (m_pFlashThread) {
// Vérifie si le thread précédent est terminé
DWORD result = WaitForSingleObject(m_pFlashThread->m_hThread, 0);
if (result == WAIT_TIMEOUT) {
return;
}
}
m_ButtonFlash.EnableWindow(FALSE);
m_pFlashThread = AfxBeginThread(
Flash, // Fonction de contrôle du thread
this, // Paramètre à passer à la fonction
THREAD_PRIORITY_HIGHEST, // Priorité du thread (par défaut normale)
0);
}
void CFlasherServerDlg::OnBnClickedRefresh()
{
// TODO: ajoutez ici le code de votre gestionnaire de notification de contrôle
Dad::cEnumCommPorts EnumPort;
EnumPort.UpdateCommPorts();
m_ListeCOM.ResetContent();
uint16_t NbCOM = EnumPort.getNbPortsFound();
for (uint16_t Index = 0; Index < NbCOM; Index++) {
uint16_t PortNum = EnumPort.getNumComm(Index+1);
CString NomPort;
NomPort.Format(L"COM%d",PortNum);
m_ListeCOM.InsertString(Index, NomPort);
m_ListeCOM.SetItemData(Index, PortNum);
}
}
// Fonction de Gestion du Thread de transfert des fichiers pour flash
UINT Flash(LPVOID pParam) {
CFlasherServerDlg* pDialog = (CFlasherServerDlg *) pParam;
Dad::cServer Server;
CString NomFichier;
CString PathFichier;
HCURSOR hMemCurseur = GetCursor();
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
int NbFiles = pDialog->m_ListeFiles.GetCount();
if (NbFiles == 0) {
MessageBox(GetActiveWindow(), L"Please add a file", L"No file selected", MB_OK | MB_ICONWARNING);
pDialog->m_ButtonFlash.EnableWindow(TRUE);
SetCursor(hMemCurseur);
return -1;
}
// Lecture du port sélectionné
int Select = pDialog->m_ListeCOM.GetCurSel();
if (Select == -1) {
MessageBox(GetActiveWindow(), L"Please select a COM port", L"No COM port selected", MB_OK | MB_ICONWARNING);
pDialog->m_ButtonFlash.EnableWindow(TRUE);
SetCursor(hMemCurseur);
return -1;
}
uint8_t NumPort = pDialog->m_ListeCOM.GetItemData(Select);
Server.Init(NumPort, QSPI_SIZE);
// Mise en mémoire des fichiers
for (int Index = 0; Index < NbFiles; Index++) {
pDialog->m_ListeFiles.GetText(Index, PathFichier);
NomFichier = PathFindFileName(PathFichier);
CW2A asciiPathFichier(PathFichier);
const char* cPathFichier = asciiPathFichier;
CW2A asciiNomFichier(NomFichier);
const char* cNomFichier = asciiNomFichier;
if (false == Server.addFile(std::string(cPathFichier), std::string(cNomFichier))) {
MessageBox(GetActiveWindow(), CString(L"Error loading " + NomFichier + L" file"), L"File loading error", MB_OK | MB_ICONERROR);
pDialog->m_Edit.SetWindowText(L"File loading error");
pDialog->m_ButtonFlash.EnableWindow(TRUE);
SetCursor(hMemCurseur);
return -1;
}
}
// Transfert des données
int16_t NbBloc = Server.getNbBlocs();
pDialog->m_Progress.SetRange(0, NbBloc);
pDialog->m_Progress.SetPos(0);
for (int16_t IndexBloc = 0; IndexBloc < NbBloc; IndexBloc++) {
// Synchronisation avec Daisy
int16_t SynchroResult = Server.Synchronize();
if (-1 == SynchroResult) {
MessageBox(GetActiveWindow(), L"Synchronization with Daisy impossible (Maybe Change COM Port?)", L"Transfer error", MB_OK | MB_ICONERROR);
pDialog->m_Edit.SetWindowText(L"Unable to communicate with Daisy");
pDialog->m_ButtonFlash.EnableWindow(TRUE);
SetCursor(hMemCurseur);
return -1;
}
if (IndexBloc != SynchroResult) {
MessageBox(GetActiveWindow(), L"Communication problem detected, flashing in progress stopped.", L"Transfer error", MB_OK | MB_ICONERROR);
pDialog->m_Edit.SetWindowText(L"File transfer or flash\r\nprocedure failed");
pDialog->m_ButtonFlash.EnableWindow(TRUE);
SetCursor(hMemCurseur);
return -1;
}
// Transmission d'un bloc
uint8_t EndTrans = IndexBloc == (NbBloc - 1) ? 1 : 0;
if (!Server.TransBloc(IndexBloc, EndTrans)) {
MessageBox(GetActiveWindow(), L"Communication problem detected (COM port inaccessible), flashing in progress stopped.", L"Transfer error", MB_OK | MB_ICONERROR);
pDialog->m_Edit.SetWindowText(L"File transfer or flash\r\nprocedure failed");
pDialog->m_ButtonFlash.EnableWindow(TRUE);
SetCursor(hMemCurseur);
return -1;
}
pDialog->m_Progress.SetPos(IndexBloc + 1);
CString strMessage;
int PourCent = ((IndexBloc+1) * 100) / NbBloc;
strMessage.Format(_T("Transfer in progress \r\n%d%% completed"), PourCent);
pDialog->m_Edit.SetWindowText(strMessage);
}
pDialog->m_Edit.SetWindowText(L"Transfer and flash completed");
pDialog->m_ButtonFlash.EnableWindow(TRUE);
SetCursor(hMemCurseur);
return 0;
}
void CFlasherServerDlg::OnLbnSelchangeListCom()
{
// TODO: ajoutez ici le code de votre gestionnaire de notification de contrôle
HCURSOR hMemCurseur = GetCursor();
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
int Select = m_ListeCOM.GetCurSel();
if (Select != -1) {
Dad::cServer Server;
uint8_t NumPort = m_ListeCOM.GetItemData(Select);
if (true == Server.Init(NumPort)) {
if (-1 == Server.Synchronize()) {
CString Msg;
Msg.Format(L"Daisy not detected on COM%d", NumPort);
m_Edit.SetWindowText(Msg);
}
else {
CString Msg;
Msg.Format(L"OK : Daisy is detected\r\non COM%d", NumPort);
m_Edit.SetWindowText(Msg);
}
}
else {
CString Msg;
Msg.Format(L"COM%d is not accessible", NumPort);
MessageBox(Msg, L"COM Error", MB_OK | MB_ICONERROR);
}
}
SetCursor(hMemCurseur);
}