Rev 967 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
262 | giacomo | 1 | #ifndef __TFTP_H |
2 | #define __TFTP_H 1 |
||
3 | |||
4 | #include <string.h> |
||
5 | #include <stdlib.h> |
||
6 | #include <stdio.h> |
||
7 | #include <ll/sys/types.h> |
||
8 | #include <drivers/udpip.h> |
||
9 | |||
10 | #include "modules/sem.h" |
||
11 | |||
12 | #define BASE_X (40) |
||
13 | |||
14 | /* Real-time constraints for the sender and receiver tasks */ |
||
15 | #define TFTP_UPLOAD_SENDER_PERIOD (2000000) |
||
16 | #define TFTP_UPLOAD_SENDER_WCET (1000000) |
||
17 | #define TFTP_UPLOAD_SENDER_MET (500000) |
||
18 | |||
19 | /* opcodes options */ |
||
20 | #define TFTP_READ_REQUEST (1) |
||
21 | #define TFTP_WRITE_REQUEST (2) |
||
22 | #define TFTP_DATA (3) |
||
23 | #define TFTP_ACK (4) |
||
24 | #define TFTP_ERROR (5) |
||
25 | |||
26 | /* Default TFTP protocol error codes */ |
||
27 | #define TFTP_ERR_NOT_DEFINED (0) /* Not defined, see error message (if any) */ |
||
28 | #define TFTP_ERR_FILE_NOR_FOUND (1) /* File not found */ |
||
29 | #define TFTP_ERR_ACCESS_VIOLATION (2) /* Access violation */ |
||
30 | #define TFTP_ERR_DISK_FULL (3) /* Disk full or allocation exceeded */ |
||
31 | #define TFTP_ERR_ILLEGAL_OPERATION (4) /* Illegal TFTP operation */ |
||
32 | #define TFTP_ERR_UNKNOWN_ID (5) /* Unknown transfer ID */ |
||
33 | #define TFTP_ERR_FILE_EXISTS (6) /* File already exists */ |
||
34 | #define TFTP_ERR_NO_USER (7) /* No such user */ |
||
35 | |||
36 | /* Custom TFTP protocol error codes */ |
||
37 | #define TFTP_NO_ERROR (-1) |
||
38 | #define TFTP_ERR_TIMEOUT (-2) |
||
39 | |||
40 | /* TFTP connection modes */ |
||
41 | #define TFTP_NETASCII_MODE "netascii" |
||
42 | #define TFTP_OCTET_MODE "octet" |
||
43 | #define TFTP_MAIL_MODE "mail" |
||
44 | |||
45 | /* Status */ |
||
46 | #define TFTP_NOT_CONNECTED (-1) |
||
47 | #define TFTP_OPEN (-2) |
||
48 | #define TFTP_ACTIVE (-3) |
||
49 | #define TFTP_CONNECTION_REQUESTING (-4) |
||
50 | #define TFTP_STREAMING (-5) |
||
51 | #define TFTP_FLUSHING (-6) |
||
52 | #define TFTP_ERR (-100) |
||
53 | |||
54 | /* Mode options */ |
||
55 | //#define TFTP_UPLOAD (1) |
||
56 | //#define TFTP_DOWNLOAD (2) |
||
57 | |||
58 | /* Closing options */ |
||
59 | #define TFTP_STOP_NOW (1) |
||
60 | #define TFTP_FLUSH_BUFFER (2) |
||
61 | |||
62 | /* Base port address for socket communication */ |
||
63 | #define BASE_PORT (2000) |
||
64 | |||
65 | /* The standard size of the tftp data packet */ |
||
66 | #define TFTP_DATA_SIZE (512) |
||
67 | |||
68 | typedef struct tftp_packet { |
||
69 | WORD opcode; /* This is the opcode that represents operation to perform. */ |
||
70 | union { |
||
71 | struct { |
||
72 | BYTE filename[TFTP_DATA_SIZE]; /* This is the download data. */ |
||
73 | } request; |
||
74 | struct { |
||
75 | WORD block; /* The number of this block. */ |
||
76 | BYTE data[TFTP_DATA_SIZE]; /* This is the download data. */ |
||
77 | } data; |
||
78 | struct { |
||
79 | WORD block; /* This is the block number of the last data packet received. */ |
||
80 | } ack; |
||
81 | struct { |
||
82 | WORD errcode; /* This is the error code from the TFTP server. */ |
||
83 | BYTE errmsg[TFTP_DATA_SIZE]; /* This is the error message from the server. */ |
||
84 | } err; |
||
85 | } u; |
||
86 | } TFTP_PACKET; |
||
87 | |||
88 | #define MAX_CONCURRENT_STREAM (5) |
||
89 | #define MAX_BUFFER_SIZE (1000000) |
||
90 | #define TFTP_DEFAULT_TIMEOUT (6000000) |
||
91 | #define TFTP_DEFAULT_TIMEOUT_NUMBER (5) |
||
92 | |||
93 | typedef struct tftp_model { |
||
94 | int status; |
||
95 | int errcode; |
||
96 | char errmsg[200]; |
||
97 | int handle; |
||
98 | BYTE filename[80]; |
||
99 | PID sender_pid; |
||
100 | PID receiver_pid; |
||
101 | int socket; |
||
102 | UDP_ADDR host; |
||
103 | UDP_ADDR local; |
||
104 | unsigned int nblock; |
||
105 | TFTP_PACKET last_sent; |
||
106 | BYTE waiting_ack; |
||
107 | TIME timestamp; /* the time we sent the last packet (data or ack) */ |
||
108 | TIME timeout; /* in microseconds */ |
||
109 | int ntimeout; /* number of timeouts (when 0 -> error!) */ |
||
110 | } TFTP_MODEL; |
||
111 | |||
112 | typedef struct tftp_buffer { |
||
113 | BYTE *data; |
||
114 | unsigned long size; |
||
115 | unsigned long nbytes; |
||
116 | } TFTP_BUFFER; |
||
117 | |||
118 | /* Mutex constant */ |
||
119 | #define TFTP_PI (0) |
||
120 | #define TFTP_SRP (1) |
||
121 | #define TFTP_PC (2) |
||
122 | |||
123 | /* We need to know what kind of mutex the user wants to use to share data with the reading (writing) queue |
||
124 | */ |
||
125 | typedef struct tftp_mutex { |
||
126 | BYTE semtype; /* Semaphore type. */ |
||
127 | union { |
||
128 | struct { /* SRP */ |
||
129 | SRP_RES_MODEL r; |
||
130 | int pLevel; /* Preemption level */ |
||
131 | } srp; |
||
132 | struct { /* Priority ceiling */ |
||
133 | PC_RES_MODEL r; |
||
134 | int tPr; /* Task priority */ |
||
135 | } pc; |
||
136 | struct { /* Priority inheritance */ |
||
137 | PI_mutexattr_t a; |
||
138 | } pi; |
||
139 | } sem; |
||
140 | } TFTP_MUTEX; |
||
141 | |||
142 | /***** Tftp packet related routines *****/ |
||
143 | int tftp_get_opcode(TFTP_PACKET *pkt); |
||
144 | WORD tftp_get_data(TFTP_PACKET *pkt, BYTE *data, int n); |
||
145 | int tftp_get_ack_block(TFTP_PACKET *pkt); |
||
146 | int tftp_get_error(TFTP_PACKET *pkt, char *errmsg); |
||
147 | int tftp_fill_request(TFTP_PACKET *pkt, WORD opcode, const BYTE *filename, const BYTE *mode); |
||
148 | int tftp_fill_data(TFTP_PACKET *pkt, WORD nblock, BYTE *rawdata, WORD datasize); |
||
149 | int tftp_fill_ack(TFTP_PACKET *pkt, WORD nblock); |
||
150 | |||
151 | /***** Initialization routines *****/ |
||
152 | int tftp_init(); |
||
153 | int tftp_net_start(char *local_ip, char *host_ip, int init_net); |
||
154 | |||
155 | /***** Parameter setting routines *****/ |
||
156 | int tftp_set_timeout(int h, int sec); |
||
157 | int tftp_set_timeout_numbers(int h, int n); |
||
158 | |||
159 | /***** Connection routines *****/ |
||
160 | int tftp_open(char *fname); |
||
161 | int tftp_upload(int i, unsigned long buffsize, sem_t *mtx); |
||
162 | int tftp_download(int i, unsigned long buffsize, sem_t *mtx); |
||
163 | int tftp_close(int h, int hardness); |
||
164 | |||
165 | /* Buffer I/O routines */ |
||
166 | int tftp_put(int h, BYTE *rawdata, WORD n); |
||
167 | int tftp_get(int h, BYTE *rawdata, WORD n); |
||
168 | int tftp_getbuffersize(int h); |
||
169 | int tftp_usedbuffer(int h); |
||
170 | int tftp_freebuffer(int h); |
||
171 | |||
172 | int tftp_status(int h); |
||
173 | |||
174 | /* Debugger routines */ |
||
175 | int debug_setbuffer(int h, int size); |
||
176 | void debug_freebuffer(int h); |
||
177 | |||
178 | #endif /* tftp.h */ |