Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
363 johboh 1
/*********************************************************************
2
 *
3
 *                  TFTP Client module for Microchip TCP/IP Stack
4
 *
5
 *********************************************************************
6
 * FileName:        TFTPc.h
7
 * Dependencies:    StackTsk.h
8
 * Processor:       PIC18
9
 * Complier:        MCC18 v1.00.50 or higher
10
 *                  HITECH PICC-18 V8.10PL1 or higher
11
 * Company:         Microchip Technology, Inc.
12
 *
13
 * Software License Agreement
14
 *
15
 * The software supplied herewith by Microchip Technology Incorporated
16
 * (the “Company”) for its PICmicro® Microcontroller is intended and
17
 * supplied to you, the Company’s customer, for use solely and
18
 * exclusively on Microchip PICmicro Microcontroller products. The
19
 * software is owned by the Company and/or its supplier, and is
20
 * protected under applicable copyright laws. All rights are reserved.
21
 * Any use in violation of the foregoing restrictions may subject the
22
 * user to criminal sanctions under applicable laws, as well as to
23
 * civil liability for the breach of the terms and conditions of this
24
 * license.
25
 *
26
 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
27
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
28
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
30
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
31
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
32
 *
33
 * Author               Date    Comment
34
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35
 * Nilesh Rajbharti     8/5/03  Original        (Rev 1.0)
36
 ********************************************************************/
37
#ifndef TFTPC_H
38
#define TFTPC_H
39
 
40
#include "..\Include\StackTsk.h"
41
#include "..\Include\UDP.h"
42
 
43
// Number of seconds to wait before declaring TIMEOUT error on Get.
44
#define TFTP_GET_TIMEOUT_VAL        (3 * TICKS_PER_SECOND)
45
 
46
// Number of seconds to wait before declaring TIMEOUT error on Put
47
#define TFTP_ARP_TIMEOUT_VAL        (3 * TICKS_PER_SECOND)
48
 
49
// Number of attempts before declaring TIMEOUT error.
50
#define TFTP_MAX_RETRIES            (3)
51
 
52
// Retry count must be 1 or more.
53
#if TFTP_MAX_RETRIES <= 0
54
#error Retry count must at least be 1
55
#endif
56
 
57
// Enum. of results returned by most of the TFTP functions.
58
typedef enum _TFTP_RESULT
59
{
60
    TFTP_OK = 0,
61
    TFTP_NOT_READY,
62
    TFTP_END_OF_FILE,
63
    TFTP_ERROR,
64
    TFTP_RETRY,
65
    TFTP_TIMEOUT
66
} TFTP_RESULT;
67
 
68
// File open mode as used by TFTPFileOpen().
69
typedef enum _TFTP_FILE_MODE
70
{
71
    TFTP_FILE_MODE_READ = 1,
72
    TFTP_FILE_MODE_WRITE = 2
73
} TFTP_FILE_MODE;
74
 
75
// Standard error codes as defined by TFTP spec.
76
// Use to decode value retuned by TFTPGetError().
77
typedef enum _TFTP_ACCESS_ERROR
78
{
79
    TFTP_ERROR_NOT_DEFINED = 0,
80
    TFTP_ERROR_FILE_NOT_FOUND,
81
    TFTP_ERROR_ACCESS_VIOLATION,
82
    TFTP_ERROR_DISK_FULL,
83
    TFTP_ERROR_INVALID_OPERATION,
84
    TFTP_ERROR_UNKNOWN_TID,
85
    TFTP_ERROR_FILE_EXISTS,
86
    TFTP_ERROR_NO_SUCH_USE
87
} TFTP_ACCESS_ERROR;
88
 
89
/*********************************************************************
90
 * Function:        void TFTPOpen(IP_ADDR *host)
91
 *
92
 * PreCondition:    UDP module is already initialized
93
 *                  and at least one UDP socket is available.
94
 *
95
 * Input:           host        - IP address of remote TFTP server
96
 *
97
 * Output:          None
98
 *
99
 * Side Effects:    None
100
 *
101
 * Overview:        Initiates ARP for given host and prepares
102
 *                  TFTP module for next sequence of function calls.
103
 *
104
 * Note:            Use TFTPIsOpened() to check if a connection was
105
 *                  successfully opened or not.
106
 *
107
 ********************************************************************/
108
void TFTPOpen(IP_ADDR *host);
109
 
110
 
111
/*********************************************************************
112
 * Function:        TFTP_RESULT TFTPIsOpened(void)
113
 *
114
 * PreCondition:    TFTPOpen() is already called.
115
 *
116
 * Input:           None
117
 *
118
 * Output:          TFTP_OK if previous call to TFTPOpen is complete
119
 *
120
 *                  TFTP_TIMEOUT if remote host did not respond to
121
 *                          previous ARP request.
122
 *
123
 *                  TFTP_NOT_READY if remote has still not responded
124
 *                          and timeout has not expired.
125
 *
126
 * Side Effects:    None
127
 *
128
 * Overview:        Waits for ARP reply and opens a UDP socket
129
 *                  to perform further TFTP operations.
130
 *
131
 * Note:            Once opened, application may keep TFTP socket
132
 *                  open and future TFTP operations.
133
 *                  If TFTPClose() is called to close the connection
134
 *                  TFTPOpen() must be called again before performing
135
 *                  any other TFTP operations.
136
 ********************************************************************/
137
TFTP_RESULT TFTPIsOpened(void);
138
 
139
 
140
/*********************************************************************
141
 * Macro:           void TFTPClose(void)
142
 *
143
 * PreCondition:    TFTPOpen is already called and TFTPIsOpened()
144
 *                  returned TFTP_OK.
145
 *
146
 * Input:           None
147
 *
148
 * Output:          None
149
 *
150
 * Side Effects:    None
151
 *
152
 * Overview:        Closes TFTP client socket.
153
 *
154
 * Note:            Once closed, application must do TFTPOpen to
155
 *                  perform any new TFTP operations.
156
 *
157
 *                  If TFTP server does not change during application
158
 *                  life-time, one may not need to call TFTPClose
159
 *                  and keep TFTP socket open.
160
 ********************************************************************/
161
#define TFTPClose(void)     UDPClose(_tftpSocket)
162
    extern UDP_SOCKET _tftpSocket;
163
 
164
 
165
 
166
/*********************************************************************
167
 * Macro:           BOOL TFTPIsFileOpenReady(void)
168
 *
169
 * PreCondition:    TFTPOpen is already called and TFTPIsOpened()
170
 *                  returned TFTP_OK.
171
 *
172
 * Input:           None
173
 *
174
 * Output:          TRUE, if it is ok to call TFTPOpenFile()
175
 *                  FALSE, if otherwise.
176
 *
177
 * Side Effects:    None
178
 *
179
 * Overview:        Checks to see if it is okay to send TFTP file
180
 *                  open request to remote server.
181
 *
182
 * Note:            None
183
 ********************************************************************/
184
#define TFTPIsFileOpenReady()       UDPIsPutReady(_tftpSocket)
185
 
186
 
187
 
188
/*********************************************************************
189
 * Function:        void TFTPOpenFile(char *fileName,
190
 *                                    TFTP_FILE_MODE mode)
191
 *
192
 * PreCondition:    TFPTIsFileOpenReady() = TRUE
193
 *
194
 * Input:           fileName        - File name that is to be opened.
195
 *                  mode            - Mode of file access
196
 *                                    Must be
197
 *                                      TFTP_FILE_MODE_READ for read
198
 *                                      TFTP_FILE_MODE_WRITE for write
199
 *
200
 * Output:          None
201
 *
202
 * Side Effects:    None
203
 *
204
 * Overview:        Prepares and sends TFTP file name and mode packet.
205
 *
206
 * Note:            By default, this funciton uses "octet" or binary
207
 *                  mode of file transfer.
208
 *                  Use TFTPIsFileOpened() to check if file is
209
 *                  ready to be read or written.
210
 ********************************************************************/
211
void TFTPOpenFile(char *fileName, TFTP_FILE_MODE mode);
212
 
213
 
214
/*********************************************************************
215
 * Function:        TFTP_RESULT TFTPIsFileOpened(void)
216
 *
217
 * PreCondition:    TFTPOpenFile() is called.
218
 *
219
 * Input:           None
220
 *
221
 * Output:          TFTP_OK if file is ready to be read or written
222
 *
223
 *                  TFTP_RETRY if previous attempt was timed out
224
 *                  needs to be retried.
225
 *
226
 *                  TFTP_TIMEOUT if all attempts were exhausted.
227
 *
228
 *                  TFTP_NOT_ERROR if remote server responded with
229
 *                  error
230
 *
231
 *                  TFTP_NOT_READY if file is not yet opened.
232
 *
233
 * Side Effects:    None
234
 *
235
 * Overview:        Waits for remote server response regarding
236
 *                  previous attempt to open file.
237
 *                  If no response is received within specified
238
 *                  timeout, fnction returns with TFTP_RETRY
239
 *                  and application logic must issue another
240
 *                  TFTPFileOpen().
241
 *
242
 * Note:            None
243
 ********************************************************************/
244
TFTP_RESULT TFTPIsFileOpened(void);
245
 
246
/*********************************************************************
247
 * Function:        void TFTPCloseFile(void)
248
 *
249
 * PreCondition:    TFTPOpenFile() was called and TFTPIsFileOpened()
250
 *                  had returned with TFTP_OK.
251
 *
252
 * Input:           None
253
 *
254
 * Output:          None
255
 *
256
 * Side Effects:    None
257
 *
258
 * Overview:        If file is opened in read mode, it makes sure
259
 *                  that last ACK is sent to server
260
 *                  If file is opened in write mode, it makes sure
261
 *                  that last block is sent out to server and
262
 *                  waits for server to respond with ACK.
263
 *
264
 * Note:            TFTPIsFileClosed() must be called to confirm
265
 *                  if file was really closed.
266
 ********************************************************************/
267
void TFTPCloseFile(void);
268
 
269
 
270
/*********************************************************************
271
 * Function:        TFTP_RESULT TFPTIsFileClosed(void)
272
 *
273
 * PreCondition:    TFTPCloseFile() is already called.
274
 *
275
 * Input:           None
276
 *
277
 * Output:          TFTP_OK if file was successfully closdd
278
 *
279
 *                  TFTP_RETRY if file mode was Write and remote
280
 *                  server did not receive last packet.
281
 *                  Application must retry with last block.
282
 *
283
 *                  TFTP_TIMEOUT if all attempts were exhausted
284
 *                  in closing file.
285
 *
286
 *                  TFTP_ERROR if remote server sent an error
287
 *                  in response to last block.
288
 *                  Actual error code may be read by calling
289
 *                  TFTPGetError()
290
 *
291
 *                  TFTP_NOT_READY if file is not closed yet.
292
 *
293
 * Side Effects:    None
294
 *
295
 * Overview:        If file mode is Read, it simply makes that
296
 *                  last block is acknowledged.
297
 *                  If file mode is Write, it waits for server ack.
298
 *                  If no ack was received within specified timeout
299
 *                  instructs appliaction to resend last block.
300
 *                  It keeps track of retries and declares timeout
301
 *                  all attempts were exhausted.
302
 *
303
 * Note:            None
304
 ********************************************************************/
305
TFTP_RESULT TFTPIsFileClosed(void);
306
 
307
 
308
 
309
/*********************************************************************
310
 * Function:        TFTP_RESULT TFTPIsGetReady(void)
311
 *
312
 * PreCondition:    TFTPOpenFile() is called with TFTP_FILE_MODE_READ
313
 *                  and TFTPIsFileOpened() returned with TRUE.
314
 *
315
 * Input:           None
316
 *
317
 * Output:          TFTP_OK if it there is more data byte available
318
 *                  to read
319
 *
320
 *                  TFTP_TIMEOUT if timeout occurred waiting for
321
 *                  new data.
322
 *
323
 *                  TFTP_END_OF_FILE if end of file has reached.
324
 *
325
 *                  TFTP_ERROR if remote server returned ERROR.
326
 *                      Actual error code may be read by calling
327
 *                      TFTPGetError()
328
 *
329
 *                  TFTP_NOT_READY if still waiting for new data.
330
 *
331
 * Side Effects:    None
332
 *
333
 * Overview:        Waits for data block.  If data block does not
334
 *                  arrive within specified timeout, it automatically
335
 *                  sends out ack for previous block to remind
336
 *                  server to send next data block.
337
 *                  If all attempts are exhausted, it returns with
338
 *                  TFTP_TIMEOUT.
339
 *
340
 * Note:            By default, this funciton uses "octet" or binary
341
 *                  mode of file transfer.
342
 ********************************************************************/
343
TFTP_RESULT TFTPIsGetReady(void);
344
 
345
 
346
/*********************************************************************
347
 * Function:        BYTE TFTPGet(void)
348
 *
349
 * PreCondition:    TFTPOpenFile() is called with TFTP_FILE_MODE_READ
350
 *                  and TFTPIsGetReady() = TRUE
351
 *
352
 * Input:           None
353
 *
354
 * Output:          data byte as received from remote server.
355
 *
356
 * Side Effects:    None
357
 *
358
 * Overview:        Fetches next data byte from TFTP socket.
359
 *                  If end of data block is reached, it issues
360
 *                  ack to server so that next data block can be
361
 *                  received.
362
 *
363
 * Note:            Use this function to read file from server.
364
 ********************************************************************/
365
BYTE TFTPGet(void);
366
 
367
 
368
/*********************************************************************
369
 * Function:        TFTP_RESULT TFTPIsPutReady(void)
370
 *
371
 * PreCondition:    TFTPOpenFile() is called with TFTP_FILE_MODE_WRITE
372
 *                  and TFTPIsFileOpened() returned with TRUE.
373
 *
374
 * Input:           None
375
 *
376
 * Output:          TFTP_OK if it is okay to write more data byte.
377
 *
378
 *                  TFTP_TIMEOUT if timeout occurred waiting for
379
 *                  ack from server
380
 *
381
 *                  TFTP_RETRY if all server did not send ack
382
 *                  on time and application needs to resend
383
 *                  last block.
384
 *
385
 *                  TFTP_ERROR if remote server returned ERROR.
386
 *                  Actual error code may be read by calling
387
 *                  TFTPGetError()
388
 *
389
 *                  TFTP_NOT_READY if still waiting...
390
 *
391
 * Side Effects:    None
392
 *
393
 * Overview:        Waits for ack from server.  If ack does not
394
 *                  arrive within specified timeout, it it instructs
395
 *                  application to retry last block by returning
396
 *                  TFTP_RETRY.
397
 *
398
 *                  If all attempts are exhausted, it returns with
399
 *                  TFTP_TIMEOUT.
400
 *
401
 * Note:            None
402
 ********************************************************************/
403
TFTP_RESULT TFTPIsPutReady(void);
404
 
405
 
406
/*********************************************************************
407
 * Function:        void TFTPPut(BYTE c)
408
 *
409
 * PreCondition:    TFTPOpenFile() is called with TFTP_FILE_MODE_WRITE
410
 *                  and TFTPIsPutReady() = TRUE
411
 *
412
 * Input:           c       - Data byte that is to be written
413
 *
414
 * Output:          None
415
 *
416
 * Side Effects:    None
417
 *
418
 * Overview:        Puts given data byte into TFTP socket.
419
 *                  If end of data block is reached, it
420
 *                  transmits entire block.
421
 *
422
 * Note:            Use this function to write file to server.
423
 ********************************************************************/
424
void TFTPPut(BYTE c);
425
 
426
 
427
/*********************************************************************
428
 * Macro:           WORD TFTPGetError(void)
429
 *
430
 * PreCondition:    One of the TFTP function returned with
431
 *                  TFTP_ERROR result.
432
 *
433
 * Input:           None
434
 *
435
 * Output:          Error code as returned by remote server.
436
 *                  Application may use TFTP_ACCESS_ERROR enum. to
437
 *                  decode standard error code.
438
 *
439
 * Side Effects:    None
440
 *
441
 * Overview:        Returns previously saved error code.
442
 *
443
 * Note:            None
444
 ********************************************************************/
445
#define TFTPGetError()      (_tftpError)
446
    extern WORD _tftpError;
447
 
448
 
449
#endif