Subversion Repositories NaviCtrl

Rev

Rev 915 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
349 ingob 1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
396 ingob 5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
7
// + FOR NON COMMERCIAL USE ONLY
349 ingob 8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
396 ingob 10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
349 ingob 16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
396 ingob 17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
349 ingob 19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
396 ingob 20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
22
// + eindeutig als Ursprung verlinkt werden
349 ingob 23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
396 ingob 24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
349 ingob 27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
396 ingob 28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
34
// + this list of conditions and the following disclaimer.
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +     from this software without specific prior written permission.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
40
// +     with our written permission
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
42
// +     clearly linked as origin
43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
44
//
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
349 ingob 57
 
58
#include <stdio.h>
59
#include <stdarg.h>
60
#include <string.h>
61
#include "91x_lib.h"
62
#include "config.h"
63
#include "main.h"
64
#include "fat16.h"
65
#include "ftphelper.h"
66
#include "mkprotocol.h"
67
#include "debug.h"
68
#include "uart1.h"
69
 
70
 
71
typedef struct
72
{
73
  u8            Name[13];
74
  u8            Attribute;
75
  u32           Size;
76
} __attribute__((packed)) FTP_direntry_t;
77
 
78
FTP_direntry_t FTP_direntry;
79
 
396 ingob 80
char FTP_data[DATA_TRANSFER_SIZE+10];                             //  rx & tx buffer to avoid 2 buffers
349 ingob 81
 
915 holgerb 82
  #define KEYWORD_COUNT 57
427 killagreg 83
  // most used gpx tags can be compressed 
349 ingob 84
  const char keyword[KEYWORD_COUNT][16]=
85
                                        {"Altimeter>\0    \0",
435 ingob 86
                                         "MotorCurrent>\0 \0",
349 ingob 87
                                         "Current>\0      \0",
88
                                         "Variometer>\0   \0",
89
                                         "GroundSpeed>\0  \0",
90
                                         "VerticalSpeed>\0\0",
91
                                         "FlightTime>\0   \0",
92
                                         "Voltage>\0      \0",
93
                                         "Capacity>\0     \0",
94
                                         "RCQuality>\0    \0",
95
                                         "RCRSSI>\0       \0",
96
                                         "Compass>\0      \0",
97
                                         "NickAngle>\0    \0",
98
                                         "RollAngle>\0    \0",
99
                                         "NCFlag>\0       \0",
100
                                         "ErrorCode>\0    \0",
101
                                         "TargetBearing>\0\0",
102
                                         "RCSticks>\0     \0",
103
                                         "GPSSticks>\0    \0",
104
                                         "extensions>\0   \0",
105
                                         "Course>\0       \0",
106
                                         "trkpt\0         \0",
107
                                         "time>\0         \0",
108
                                         "sat>\0          \0",
109
                                         "ele>\0          \0",
110
                                         " lat=\0         \0",
111
                                         " lon=\0         \0",
435 ingob 112
                                         "0,0,0,0\0       \0",
434 holgerb 113
                                         "name>\0         \0",
114
                                         "FCFlags2>\0     \0",
115
                                         "MagnetField>\0  \0",
116
                                         "trkseg>\0       \0",
117
                                         "AnalogInputs>\0 \0",
118
                                         "Servo>\0        \0",
119
                                         "BL_State>\0     \0",
120
                                         "BL_MaxPWM>\0    \0",
121
                                         "MikroKopter\0   \0",
435 ingob 122
                                         "nter>000</\0    \0",
434 holgerb 123
                                         "metadata>\0     \0",
436 holgerb 124
                                         "Speak>\0        \0",
671 holgerb 125
                                         "MotorNOK>\0     \0",
126
                                         "Gas>\0          \0",
127
                                         "ShutterCnt>\0   \0",
128
                                         "ele_raw>\0      \0",
129
                                         "GPSInfo>\0      \0",
864 holgerb 130
                                         "Gimbal>\0       \0",
131
                                         "Laser>\0        \0",
865 holgerb 132
                                         "ExCtl>\0        \0",
434 holgerb 133
                                         "<WP>----,0,0,0<\0",
134
                                         "MagnetInclinati\0",
435 ingob 135
                                         "BL_Temperature>\0",
434 holgerb 136
                                         "AvaiableMotorPo\0",
435 ingob 137
                                         "FC_I2C_ErrorCou\0",    
138
                                         "FC_SPI_ErrorCou\0",
880 holgerb 139
                                         "TargetDistance>\0",
905 holgerb 140
                                         "LaserAltCorr>\0  ",
880 holgerb 141
                                         "_ErrorCounter>\0 "
671 holgerb 142
                                         };
143
//ACHTUNG: ggf. DATA_TRANSFER_SIZE vergrössern
434 holgerb 144
 
435 ingob 145
u16 CompressBuffer(char *buf, u16 size, u16 offset)
146
{
147
 
349 ingob 148
  u16 i, count = size;
149
  char *s = NULL, *start = buf+offset;
150
 
151
   for ( i = 0; i < KEYWORD_COUNT; i++)
152
   {
153
                s = strstr(start, keyword[i]);
154
                if (s != NULL)
155
                {       u8 keylen = strlen(keyword[i]);
156
                        *s= 27;  
157
                        *(s+1) = i+1;
158
                        count-= (keylen-2);
159
                        memcpy(s+2, s+keylen, count - (s-buf));
160
                        i--;
161
                }
162
        }
163
 
164
   return (count);
165
}
166
// --------------------------------------------------------------------
167
void CheckFTPCommand(u8 FTP_command)
168
{
427 killagreg 169
        static Find_t fe;
170
        static File_t *fp = NULL;
171
        static u8 blockindex, compressLevel = 0;
349 ingob 172
 
427 killagreg 173
        u32 filesize;
174
 
175
        switch (FTP_command)
176
        {
177
                case FTP_CMD_FINDFIRST:
178
                        FTP_direntry.Name[0] = 0;
179
                        // any file or directory except volume labels and hidden files 
180
                        if(findfirst_("*.*", (ATTR_ARCHIVE|ATTR_SUBDIRECTORY|ATTR_SYSTEM|ATTR_READONLY), &fe))
181
                        {
182
                                memcpy(&FTP_direntry.Name, &fe.name, 13);      
183
                                FTP_direntry.Attribute = fe.fp.Attribute;
184
                                FTP_direntry.Size = fe.fp.Size;
185
                        }
186
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &FTP_direntry, sizeof(FTP_direntry));
187
                        break;
349 ingob 188
 
427 killagreg 189
                case FTP_CMD_FINDNEXT:
190
                        FTP_direntry.Name[0] = 0;
191
                        if (findnext_(&fe))
192
                        {
193
                                memcpy(&FTP_direntry.Name, &fe.name, 13);      
194
                                FTP_direntry.Attribute = fe.fp.Attribute;
195
                                FTP_direntry.Size = fe.fp.Size;
196
                        }
197
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &FTP_direntry, sizeof(FTP_direntry));
198
                        break;
349 ingob 199
 
427 killagreg 200
                case FTP_CMD_GET_CWD: // get current working directory
201
                        {
202
                        char data_null = 0;
203
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 3, &FTP_command, 1, getcwd_(), strlen(getcwd_()), &data_null, 1 );
204
                        }
205
                        break;
349 ingob 206
 
427 killagreg 207
                case FTP_CMD_SET_CWD: // set current working directory
208
                        {
396 ingob 209
                        u8 cmd_successful;
210
                        cmd_successful = chdir_(FTP_data);
211
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &cmd_successful, 1);
427 killagreg 212
                        }
213
                        break;
396 ingob 214
 
427 killagreg 215
                case FTP_CMD_CDUP: // change dir up
216
                        {
217
                        u8 cmd_successful;
218
                        cmd_successful = chdir_("..");
219
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &cmd_successful, 1);
220
                        }
221
                        break;
396 ingob 222
 
427 killagreg 223
                case FTP_CMD_MKDIR:     // create directory 
224
                        {
225
                        u8 cmd_successful;
226
                    cmd_successful = mkdir_(&FTP_data[0]);
227
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &cmd_successful, 1);
228
                        }
229
                        break;
772 ingob 230
 
231
                case FTP_CMD_CHANGE_BAUDATE:    // change baudrate 
232
                        {
233
                         u8 cmd_successful;
234
                         u32 baudrate;
235
 
236
                        baudrate = FTP_data[0];
237
                        baudrate |= ((u32)FTP_data[1] * 0x100);
238
            baudrate *= 100;
239
 
240
                        UART1_Configure(baudrate);
241
 
775 holgerb 242
                        UART1_BaudrateFallbackTimeout = 700;    // set global var to check UART communication in polling
772 ingob 243
 
244
                        cmd_successful = 1;
245
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &cmd_successful, 1);
246
                        }
247
                        break;
248
 
427 killagreg 249
 
772 ingob 250
                case FTP_CMD_PING:      // ping
251
                        {
252
                        u8 cmd_successful = 1;
253
                    MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &cmd_successful, 1);
775 holgerb 254
                        UART1_BaudrateFallbackTimeout = 0;
772 ingob 255
                        }
256
                        break;
257
 
427 killagreg 258
                case FTP_CMD_RMDIR:     // delete directory 
259
                        {
260
                        u8 cmd_successful;
261
                    cmd_successful = rmdir_(&FTP_data[0]);
262
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &cmd_successful, 1);
263
                        }
264
                        break;
396 ingob 265
 
427 killagreg 266
                case FTP_CMD_DELETE_FILE:       // delete file 
267
                        {
268
                        u8 cmd_successful;
269
                cmd_successful = fdelete_(&FTP_data[0]);
270
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &cmd_successful, 1);
271
                        }
272
                        break;
396 ingob 273
 
427 killagreg 274
                case FTP_CMD_OPEN_FILE: // open the file for reading
275
                        {
276
                        u8 filefound;  
277
                        compressLevel = FTP_data[0];
278
                        fp = fopen_(&FTP_data[1], 'r');
279
                        if (fp != NULL)
280
                        {
281
                                filefound = 1;
282
                                filesize = fp->Size;
283
                                blockindex = 0;                         // reset index counter
284
                        }
285
                        else filefound = 0;                             // this means, no valid file found for transfer
286
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 3, &FTP_command, 1, &filefound, 1, &filesize, sizeof(filesize));
287
                        }
288
                        break;
349 ingob 289
 
427 killagreg 290
                case FTP_CMD_GET_FILE_DATA:
291
                        {
292
                        u16 size = DATA_TRANSFER_SIZE;
293
                        if (FTP_data[0] == blockindex + 1)                                      // next block is requested
294
                        {
295
                        blockindex++;                                  
296
                        }
297
                        else
298
                        {
299
                                u32 filepos;
300
                                blockindex = FTP_data[0];
301
                                filepos = FTP_data[1];
302
                                filepos |= ((u32)FTP_data[2] * 0x100);
303
                                filepos |= ((u32) FTP_data[3] * 0x10000L);
304
                                filepos |= ((u32) FTP_data[4] * 0x1000000L);
305
                        fseek_(fp, filepos, SEEK_SET);  // set filepointer to beginning of requested block
306
                        }
307
 
308
                        fread_(FTP_data, DATA_TRANSFER_SIZE, 1, fp);                            // read data block to buffer);
309
                        FTP_data[DATA_TRANSFER_SIZE] = 0;              
349 ingob 310
 
427 killagreg 311
                if (compressLevel)
312
                        {
313
                                size = CompressBuffer(FTP_data, DATA_TRANSFER_SIZE, 0);
349 ingob 314
 
427 killagreg 315
                                if (compressLevel > 1)
316
                                {
317
                                        if (size < DATA_TRANSFER_SIZE - 50)
318
                                        {              
435 ingob 319
                                                fread_(&FTP_data[size], DATA_TRANSFER_SIZE - size, 1, fp);                              // read data block to buffer);  
427 killagreg 320
                                                FTP_data[DATA_TRANSFER_SIZE] = 0;              
321
                                                size = CompressBuffer(FTP_data, DATA_TRANSFER_SIZE, size);
322
                                        }
323
                                }
324
                        }  
325
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 4, &FTP_command, 1, &blockindex, 1, &size, 2, &FTP_data, size);
326
                        }
327
                        break;
349 ingob 328
 
427 killagreg 329
                case FTP_CMD_CLOSE_FILE:
330
                        fclose_(fp);
331
                        Debug("ftp: CloseFile");               
332
                        break;
349 ingob 333
 
427 killagreg 334
                case FTP_CMD_CREATE_FILE:       // open the file for writing
335
                        {
336
                        u16 size = DATA_TRANSFER_SIZE;
337
                        //compressLevel = FTP_data[0];
338
                        // FTP_data[1..4] contains filesize in bytes
339
                        compressLevel = 0;                      // no compression for writing
340
                        fp = fopen_(&FTP_data[5], 'w');
341
                        if (fp != NULL)
342
                        {
343
                                blockindex = 0;                                                                 // reset index counter
344
                        }
345
                        else size = 0;                                                                          // this means, no valid file for transfer (maybe readonly)
346
                        //if (size == 0) Debug("ftp: create ERR ");
347
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &size, 2);
348
                        }
349
                        break;
349 ingob 350
 
427 killagreg 351
                case FTP_CMD_SEND_FILE_DATA:
352
                        {
353
                        u16 size = (u8)FTP_data[2];
354
                        size *= 0x100;
355
                        size += (u8) FTP_data[1];
356
                        //      if (FTP_data[0] == blockindex)                                          // next block is requested
357
                        {
358
                                blockindex++;                                  
359
                        fwrite_(&FTP_data[3], size, 1, fp);                             // write data block to buffer);
360
                        }      
361
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &blockindex, 1 );
362
                        }
363
                        break;
396 ingob 364
 
427 killagreg 365
                case FTP_CMD_NONE:
366
                default:
367
                        FTP_command = FTP_CMD_NONE;
368
                        Debug("ftp: CMD None");
369
                        break;
435 ingob 370
 
371
                case FTP_CMD_GET_KEYWORDS:
372
                        {
373
                        u8 count = KEYWORD_COUNT;
374
 
375
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 3, &FTP_command, 1, &count, 1, keyword, sizeof(keyword) );
376
                        }
377
                        break;
378
 
379
 
427 killagreg 380
        }
349 ingob 381
}
382
 
383