fb11e647
vrobic
reseau statique a...
|
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
|
/*
* Copyright (C) 2015 Engineering-Spirit
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup examples
* @{
*
* @file
* @brief Demonstrating the sending and receiving of data via the TFTP server
*
* @author Nick van IJzendoorn <nijzendoorn@engineering-spirit.nl>
*
* @}
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
#include "thread.h"
#include "net/gnrc/tftp.h"
/* the message queues */
#define TFTP_QUEUE_SIZE (4)
static msg_t _tftp_msg_queue[TFTP_QUEUE_SIZE];
/* allocate the stack */
char _tftp_stack[THREAD_STACKSIZE_MAIN + THREAD_EXTRA_STACKSIZE_PRINTF];
/* default server text which can be received */
static const char _tftp_server_hello[] = "Hello world,\n"
"\n"
"Welcome to the wonderful world of IoT and embedded systems.\n"
"\n"
"Enjoy the RIOT-OS\n";
static tftp_action_t _tftp_action;
/**
* @brief called at every transcation start
*/
static bool _tftp_server_start_cb(tftp_action_t action, tftp_mode_t mode,
const char *file_name, size_t *len)
{
/* translate the mode */
const char *str_mode = "ascii";
if (mode == TTM_OCTET) {
str_mode = "bin";
}
else if (mode == TTM_MAIL) {
str_mode = "mail";
}
/* translate the action */
const char *str_action = "read";
if (action == TFTP_WRITE) {
str_action = "write";
}
/* display the action being performed */
printf("tftp_server: %s %s %s:%lu\n", str_mode, str_action, file_name, (unsigned long)*len);
/* return the length of the text, if this is an read action */
if (action == TFTP_READ) {
*len = sizeof(_tftp_server_hello);
}
/* remember the action of the current transfer */
_tftp_action = action;
/* we accept the transfer to take place so we return true */
return true;
}
/**
* @brief called to get or put data, depending on the mode received by `_tftp_start_cb(action, ...)`
*/
static int _tftp_server_data_cb(uint32_t offset, void *data, size_t data_len)
{
char *c = (char *) data;
/* if we are reading return the part of the data that is being requested */
if (_tftp_action == TFTP_READ) {
/* calculate the length of the data block to transfer */
if (offset + data_len > sizeof(_tftp_server_hello)) {
data_len -= (offset + data_len) - sizeof(_tftp_server_hello);
}
/* copy the block to the output buffer */
memcpy(data, _tftp_server_hello + offset, data_len);
}
else {
/* we received a data block which we output to the console */
printf("\n -- SERVER DATA --\n%.*s\n -- SERVER DATA --\n", (int)data_len, c);
}
/* return the length of the data block */
return data_len;
}
/**
* @brief the transfer has stopped, see the event argument to determined if it was successful
* or not.
*/
static void _tftp_server_stop_cb(tftp_event_t event, const char *msg)
{
/* decode the stop event received */
const char *cause = "UNKOWN";
if (event == TFTP_SUCCESS) {
cause = "SUCCESS";
}
else if (event == TFTP_PEER_ERROR) {
cause = "ERROR From Client";
}
else if (event == TFTP_INTERN_ERROR) {
cause = "ERROR Internal Server Error";
}
/* print the transfer result to the console */
printf("tftp_server: %s: %s\n", cause, (msg == NULL) ? "NULL" : msg);
}
/**
* @brief the TFTP server thread
*/
void *tftp_server_wrapper(void *arg)
{
(void)arg;
/* A message queue is needed to register for incoming packets */
msg_init_queue(_tftp_msg_queue, TFTP_QUEUE_SIZE);
/* inform the user */
puts("tftp_server: Starting TFTP service at port 69");
/* run the TFTP server */
gnrc_tftp_server(_tftp_server_data_cb, _tftp_server_start_cb, _tftp_server_stop_cb, true);
/* the TFTP server has been stopped */
puts("tftp_server: Stopped TFTP service");
return NULL;
}
/**
* @brief start the TFTP server by creating a thread
*/
void tftp_server_start(void)
{
thread_create(_tftp_stack, sizeof(_tftp_stack),
1, THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST,
tftp_server_wrapper, NULL, "TFTP Server");
}
/**
* @brief stop the TFTP server by sending a message to the thread
*/
void tftp_server_stop(void)
{
gnrc_tftp_server_stop();
}
int tftp_server_cmd(int argc, char * *argv)
{
switch (argc) {
case 2:
if (strcmp(argv[1], "start") == 0) {
tftp_server_start();
return 0;
}
else if (strcmp(argv[1], "stop") == 0) {
tftp_server_stop();
return 0;
}
/* no break */
default:
printf("usage: %s [start|stop]\n", argv[0]);
return 0;
}
return 0;
}
|