-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.cpp
399 lines (310 loc) · 9.46 KB
/
client.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//-----------------------------------------------------------------------------
/*!
\file
\brief Demo of file transfer server
Use Linux socat tool to create 2 virtual TTY devices, connected to each other.
E.g. socat -d -d pty,raw,echo=0 pty,raw,echo=0
Will create /dev/pts/1, and /dev/pts/2
*/
//-----------------------------------------------------------------------------
/* -- Includes ------------------------------------------------------------ */
#include <signal.h>
#include <unistd.h>
#include <iostream>
#include "slay2.h"
#include "slay2_linux.h"
#include "file_xfer.h"
#include "file_xfer_client.h"
/* -- Defines ------------------------------------------------------------- */
using namespace std;
#define CTRL_CHANNEL (5)
#define DATA_CHANNEL (6)
/* -- Types --------------------------------------------------------------- */
/* -- (Module) Global Variables ------------------------------------------- */
static int ctrlC;
/* -- Module Global Function Prototypes ----------------------------------- */
static void upload_alphabet(const unsigned char * const data, const unsigned int len,
Slay2Channel * dataChannel);
/* -- Implementation ------------------------------------------------------ */
static void m_signal_handler(int a)
{
ctrlC = 1;
}
class DummyClient : public FileXferClientApp
{
public:
static string statusText(int status)
{
if (status == 0)
{
return "NACK";
}
else
{
return "ACK";
}
}
static string errorText(int status)
{
if (status == 0)
{
return "OK";
}
else
{
return "FEHLER";
}
}
public:
void onPwdResponse(int status, const std::string& dir)
{
cout << "onPwdResponse: " << statusText(status) << endl;
cout << dir << endl;
cout << endl;
}
void onCdResponse(int status, const std::string& dir)
{
cout << "onCdResponse: " << statusText(status) << endl;
cout << dir << endl;
cout << endl;
}
void onLsResponse(int status, const std::string& dir)
{
cout << "onLsResponse: " << statusText(status) << endl;
cout << dir << endl;
cout << endl;
}
void onDirResponse(int status, const std::string& dir)
{
cout << "onDirResponse: " << statusText(status) << endl;
cout << dir << endl;
cout << endl;
}
void onMkdirResponse(int status)
{
cout << "onMkdirResponse: " << statusText(status) << endl;
cout << endl;
}
void onRmResponse(int status)
{
cout << "onRmResponse: " << statusText(status) << endl;
cout << endl;
}
void onDownloadResponse(int status)
{
cout << "onDownloadResponse: " << statusText(status) << endl;
cout << endl;
}
void onUploadResponse(int status)
{
cout << "onUploadResponse: " << statusText(status) << endl;
cout << endl;
}
void onQuitResponse(int status)
{
cout << "onQuitResponse: " << statusText(status) << endl;
cout << endl;
}
//file operation
bool openFileForRead(const std::string& file, FileHandle_t * handle)
{
cout << "openFileForRead: " << file << endl;
*handle = (FileHandle_t)1;
return true;
}
bool openFileForWrite(const std::string& file, FileHandle_t * handle)
{
cout << "openFileForWrite: " << file << endl;
*handle = (FileHandle_t)2;
return true;
}
size_t getFileSize(FileHandle_t file)
{
cout << "getFileSize=26" << endl;
return 26;
}
size_t readFromFile(FileHandle_t file, unsigned char * buffer, size_t bufferSize)
{
for (int i = 0; i < 26; ++i)
{
buffer[i] = 'a' + i;
}
cout << "readFromFile=26" << endl;
return 26;
}
size_t writeToFile(FileHandle_t file, const unsigned char * data, size_t length)
{
char * hack = (char *)data;
hack[length] = 0;
cout << "writeToFile: " << hack << endl;
return length;
}
FileHandle_t closeFile(FileHandle_t file)
{
if (file != FILE_XFER_CLIENT_INVALID_FILE_HANDLE)
{
cout << "closeFile" << endl;
}
return FILE_XFER_CLIENT_INVALID_FILE_HANDLE;
}
};
int main(int argc, char * argv[])
{
char buffer[512];
unsigned int count;
int status;
const char * path;
//check command line arguments
if (argc < 2)
{
cout << "Missing argument!" << endl;
cout << "Usage: ./fx_client <tty-dev>" << endl;
return -1;
}
//init communiction driver
Slay2Linux slay2; //serial layer 2 protocol driver
bool stat = slay2.init(argv[1], 115200);
if (stat == false)
{
cout << "Failed to open tty: " << argv[1] << endl;
return -2;
}
//open control channel
Slay2Channel * controlChannel = slay2.open(CTRL_CHANNEL);
if (controlChannel == NULL)
{
cout << "Failed to open control channel: " << CTRL_CHANNEL << endl;
slay2.shutdown();
return -3;
}
//open control channel
Slay2Channel * dataChannel = slay2.open(DATA_CHANNEL);
if (dataChannel == NULL)
{
cout << "Failed to open data channel: " << DATA_CHANNEL << endl;
slay2.close(controlChannel);
slay2.shutdown();
return -4;
}
//fxClient
DummyClient appClient;
FileXferClient fxClient(controlChannel, dataChannel, &appClient);
//start application
cout << "fx_client is using " << argv[1] << endl;
cout << "Control-Channel: " << CTRL_CHANNEL << endl;
cout << "Data-Channel: " << DATA_CHANNEL << endl;
cout << "Use CTRL+C to quit!" << endl << endl;
//register signal handler, to quit program usin CTRL+C
signal(SIGINT, &m_signal_handler);
buffer[0] = 0;
while (!ctrlC && (buffer[0] != 'X'))
{
cout << endl << "------------------------------" << endl;
cout << "Enter command" << endl;
cin >> buffer;
count = strlen(buffer);
buffer[count++] = 0; //add zero termination
switch (buffer[0])
{
case FILE_XFER_CMD_PWD:
status = fxClient.workingDirectory();
cout << "PWD" << endl;
cout << DummyClient::errorText(status) << endl;
break;
case FILE_XFER_CMD_CD:
path = (const char *)&buffer[1];
status = fxClient.changeDirectory(path);
cout << "CD " << path << endl;
cout << DummyClient::errorText(status) << endl;
break;
case FILE_XFER_CMD_LS:
status = fxClient.listDirectory();
cout << "LS" << endl;
cout << DummyClient::errorText(status) << endl;
break;
case FILE_XFER_CMD_DIR:
path = (const char *)&buffer[1];
status = fxClient.changeListDirectory(path);
cout << "DIR " << path << endl;
cout << DummyClient::errorText(status) << endl;
break;
case FILE_XFER_CMD_MKDIR:
path = (const char *)&buffer[1];
status = fxClient.makeDirectory(path);
cout << "MKDIR " << path << endl;
cout << DummyClient::errorText(status) << endl;
break;
case FILE_XFER_CMD_RM:
path = (const char *)&buffer[1];
status = fxClient.removeFile(path);
cout << "RM " << path << endl;
cout << DummyClient::errorText(status) << endl;
break;
case FILE_XFER_CMD_DOWNLOAD:
path = (const char *)&buffer[1];
status = fxClient.downloadFile(path, path);
cout << "DOWNLOAD" << endl;
cout << DummyClient::errorText(status) << endl;
break;
case FILE_XFER_CMD_UPLOAD:
path = (const char *)&buffer[1];
status = fxClient.uploadFile(path, path);
cout << "UPLOAD" << endl;
cout << DummyClient::errorText(status) << endl;
break;
case FILE_XFER_CMD_QUIT:
status = fxClient.quit();
cout << "QUIT" << endl;
cout << DummyClient::errorText(status) << endl;
break;
default:
break;
}
//run app
while (!fxClient.isIdle())
{
slay2.task();
fxClient.task(slay2.getTime1ms());
usleep(300); //5 chars @ 115200 bps takes about 300us
}
usleep(300); //5 chars @ 115200 bps takes about 300us
}
//shut down application
slay2.close(dataChannel);
slay2.close(controlChannel);
slay2.shutdown();
return 0;
}
#if 0
static void upload_alphabet(const unsigned char * const data, const unsigned int len,
Slay2Channel * dataChannel)
{
//ensure the given string is zero terminated
if (data[len - 1] == 0)
{
char * it = (char *)(data + 1); //i'm going to iterate through the string (and modifing it...)
const char * fileName = it; //first argument is upload file name
int fileSize = 0;
char c;
//search for KOMMA
while (((c = *it) != 0) && (c != ',')) ++it; //this loop ends, if "it" points to ZERO or KOMMA
*it = 0; //replace KOMMA by ZERO to terminate file name
//determien fileSize
if (c != 0) //did i found the KOMMA above
{
//yes - the data following, is the file size
fileSize = atoi(it + 1);
}
//schedule file upload
static const char * const alphabet = "abcdefghijklmnopqrstuvwxyz" \
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
unsigned int count = 0;
while (fileSize > 0)
{
dataChannel->send((const unsigned char *)&alphabet[count % (2*26)], 1, (fileSize != 1));
++count;
--fileSize;
}
}
}
#endif