Subversion Repositories NaviCtrl

Rev

Rev 397 | Rev 434 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 397 Rev 427
Line 71... Line 71...
71
typedef struct
71
typedef struct
72
{
72
{
73
  u8            Name[13];
73
  u8            Name[13];
74
  u8            Attribute;
74
  u8            Attribute;
75
  u32           Size;
75
  u32           Size;
76
 
-
 
77
} __attribute__((packed)) FTP_direntry_t;
76
} __attribute__((packed)) FTP_direntry_t;
Line 78... Line 77...
78
 
77
 
Line 79... Line 78...
79
FTP_direntry_t FTP_direntry;
78
FTP_direntry_t FTP_direntry;
Line 80... Line 79...
80
 
79
 
81
char FTP_data[DATA_TRANSFER_SIZE+10];                             //  rx & tx buffer to avoid 2 buffers
80
char FTP_data[DATA_TRANSFER_SIZE+10];                             //  rx & tx buffer to avoid 2 buffers
82
 
81
 
Line -... Line 82...
-
 
82
u16 CompressBuffer(char *buf, u16 size, u16 offset)
83
u16 CompressBuffer(char *buf, u16 size, u16 offset)
83
{
84
{
84
  #define KEYWORD_COUNT 27
85
  #define KEYWORD_COUNT 27
85
 
86
 
86
  // most used gpx tags can be compressed 
87
  const char keyword[KEYWORD_COUNT][16]=
87
  const char keyword[KEYWORD_COUNT][16]=
Line 131... Line 131...
131
   return (count);
131
   return (count);
132
}
132
}
133
// --------------------------------------------------------------------
133
// --------------------------------------------------------------------
134
void CheckFTPCommand(u8 FTP_command)
134
void CheckFTPCommand(u8 FTP_command)
135
{
135
{
136
/*
-
 
137
  static Find_t fe;
136
        static Find_t fe;
138
  static File_t *fp = NULL;
137
        static File_t *fp = NULL;
139
  static u8 blockindex, compressLevel = 0;
138
        static u8 blockindex, compressLevel = 0;
140
 
139
 
141
  u32 filesize;
140
        u32 filesize;
142
 
141
 
143
  switch (FTP_command)
142
        switch (FTP_command)
144
  {
143
        {
145
        case FTP_CMD_FINDFIRST:
144
                case FTP_CMD_FINDFIRST:
146
                FTP_direntry.Name[0] = 0;
145
                        FTP_direntry.Name[0] = 0;
-
 
146
                        // any file or directory except volume labels and hidden files 
147
                if (findfirst_("*.*", 0x35, &fe))
147
                        if(findfirst_("*.*", (ATTR_ARCHIVE|ATTR_SUBDIRECTORY|ATTR_SYSTEM|ATTR_READONLY), &fe))
148
                {
148
                        {
149
                  memcpy(&FTP_direntry.Name, &fe.name, 13);    
149
                                memcpy(&FTP_direntry.Name, &fe.name, 13);      
150
                  FTP_direntry.Attribute = fe.fp.Attribute;
150
                                FTP_direntry.Attribute = fe.fp.Attribute;
151
                  FTP_direntry.Size = fe.fp.Size;
151
                                FTP_direntry.Size = fe.fp.Size;
152
                 // Debug(FTP_direntry.Name);
-
 
153
                }
152
                        }
154
               
-
 
155
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &FTP_direntry, sizeof(FTP_direntry));
153
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &FTP_direntry, sizeof(FTP_direntry));
156
        break;
154
                        break;
Line 157... Line 155...
157
       
155
       
-
 
156
                case FTP_CMD_FINDNEXT:
-
 
157
                        FTP_direntry.Name[0] = 0;
-
 
158
                        if (findnext_(&fe))
-
 
159
                        {
-
 
160
                                memcpy(&FTP_direntry.Name, &fe.name, 13);      
-
 
161
                                FTP_direntry.Attribute = fe.fp.Attribute;
-
 
162
                                FTP_direntry.Size = fe.fp.Size;
-
 
163
                        }
-
 
164
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &FTP_direntry, sizeof(FTP_direntry));
Line 158... Line 165...
158
        case FTP_CMD_FINDNEXT:
165
                        break;
159
       
-
 
160
                FTP_direntry.Name[0] = 0;
-
 
161
 
166
       
162
                if (findnext_(&fe))
-
 
163
                {
-
 
164
                  memcpy(&FTP_direntry.Name, &fe.name, 13);    
167
                case FTP_CMD_GET_CWD: // get current working directory
165
                  FTP_direntry.Attribute = fe.fp.Attribute;
168
                        {
166
                  FTP_direntry.Size = fe.fp.Size;
169
                        char data_null = 0;
167
                 // Debug(FTP_direntry.Name);
-
 
168
                }
-
 
169
               
170
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 3, &FTP_command, 1, getcwd_(), strlen(getcwd_()), &data_null, 1 );
Line 170... Line -...
170
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &FTP_direntry, sizeof(FTP_direntry));
-
 
171
        break;
-
 
172
       
-
 
173
        case FTP_CMD_GET_CWD:
-
 
174
                {
-
 
175
                char data_null = 0;
-
 
176
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 3, &FTP_command, 1, GetPath(), strlen(GetPath()), &data_null, 1 );
-
 
177
                }
-
 
178
               
171
                        }
179
        break;
172
                        break;
180
       
173
       
181
        case FTP_CMD_SET_CWD:
-
 
182
                {
174
                case FTP_CMD_SET_CWD: // set current working directory
183
                        u8 cmd_successful;
175
                        {
184
 
176
                        u8 cmd_successful;
185
                        cmd_successful = chdir_(FTP_data);
177
                        cmd_successful = chdir_(FTP_data);
186
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &cmd_successful, 1);
-
 
187
                }
-
 
188
        break;
-
 
189
       
-
 
190
        case FTP_CMD_DELETE_FILE:       // delete file
-
 
191
                {
-
 
192
                u8 cmd_successful = 1;
-
 
193
 
-
 
Line 194... Line 178...
194
         Debug(&FTP_data[0]);
178
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &cmd_successful, 1);
195
 
179
                        }
196
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &cmd_successful, 1);
180
                        break;
-
 
181
 
-
 
182
                case FTP_CMD_CDUP: // change dir up
197
 
183
                        {
198
                if (fdelete_(&FTP_data[0]))
184
                        u8 cmd_successful;
Line -... Line 185...
-
 
185
                        cmd_successful = chdir_("..");
199
                {
186
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &cmd_successful, 1);
200
                  cmd_successful = 1;
187
                        }
201
                }
188
                        break;
-
 
189
 
202
                else cmd_successful = 0;                                                                       
190
                case FTP_CMD_MKDIR:     // create directory 
203
 
191
                        {
204
                }
192
                        u8 cmd_successful;
205
        break;
-
 
206
        case FTP_CMD_OPEN_FILE: // open the file for reading
193
                    cmd_successful = mkdir_(&FTP_data[0]);
207
                {
-
 
208
                u8 filefound;
194
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &cmd_successful, 1);
209
                       
195
                        }
210
                compressLevel = FTP_data[0];
196
                        break;
211
                fp = fopen_(&FTP_data[1], 'r');
197
       
212
                if (fp != NULL)
198
                case FTP_CMD_RMDIR:     // delete directory 
213
                {
-
 
-
 
199
                        {
Line -... Line 200...
-
 
200
                        u8 cmd_successful;
-
 
201
                    cmd_successful = rmdir_(&FTP_data[0]);
-
 
202
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &cmd_successful, 1);
-
 
203
                        }
-
 
204
                        break;
-
 
205
 
-
 
206
                case FTP_CMD_DELETE_FILE:       // delete file 
Line -... Line 207...
-
 
207
                        {
-
 
208
                        u8 cmd_successful;
-
 
209
                cmd_successful = fdelete_(&FTP_data[0]);
-
 
210
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &cmd_successful, 1);
-
 
211
                        }
-
 
212
                        break;
-
 
213
 
-
 
214
                case FTP_CMD_OPEN_FILE: // open the file for reading
-
 
215
                        {
-
 
216
                        u8 filefound;  
-
 
217
                        compressLevel = FTP_data[0];
-
 
218
                        fp = fopen_(&FTP_data[1], 'r');
214
                  filefound = 1;
219
                        if (fp != NULL)
215
                  filesize = fp->Size;
220
                        {
216
                  blockindex = 0;                                                                               // reset index counter
221
                                filefound = 1;
Line 217... Line 222...
217
                }
222
                                filesize = fp->Size;
218
                else filefound = 0;                                                                             // this means, no valid file found for transfer
223
                                blockindex = 0;                         // reset index counter
219
 
224
                        }
220
 
-
 
221
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 3, &FTP_command, 1, &filefound, 1, &filesize, sizeof(filesize));
225
                        else filefound = 0;                             // this means, no valid file found for transfer
222
                }
226
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 3, &FTP_command, 1, &filefound, 1, &filesize, sizeof(filesize));
223
        break;
227
                        }
224
 
228
                        break;
225
        case FTP_CMD_GET_FILE_DATA:
229
 
226
                {
230
                case FTP_CMD_GET_FILE_DATA:
-
 
231
                        {
-
 
232
                        u16 size = DATA_TRANSFER_SIZE;
-
 
233
                        if (FTP_data[0] == blockindex + 1)                                      // next block is requested
-
 
234
                        {
-
 
235
                        blockindex++;                                  
-
 
236
                        }
227
                  u16 size = DATA_TRANSFER_SIZE;
237
                        else
228
                 
238
                        {
229
                if (FTP_data[0] == blockindex + 1)                                      // next block is requested
-
 
230
                 {
239
                                u32 filepos;
231
                   blockindex++;                                       
240
                                blockindex = FTP_data[0];
232
                 }
241
                                filepos = FTP_data[1];
Line 233... Line 242...
233
                 else if (FTP_data[0] == blockindex)                                    // repeat last block
242
                                filepos |= ((u32)FTP_data[2] * 0x100);
234
                 {
243
                                filepos |= ((u32) FTP_data[3] * 0x10000L);
235
                    fseek_(fp, SEEK_CUR, -DATA_TRANSFER_SIZE);                  // set filepointer to beginning of last block
244
                                filepos |= ((u32) FTP_data[4] * 0x1000000L);
Line 236... Line 245...
236
                 }                                                                                                             
245
                        fseek_(fp, filepos, SEEK_SET);  // set filepointer to beginning of requested block
237
                 else blockindex = FTP_data[0]+ 10;                                             // ABORT transfer,  blockindex now out of valid range
246
                        }
238
         
247
               
239
                 fread_(FTP_data, DATA_TRANSFER_SIZE, 1, fp);                           // read data block to buffer);
248
                        fread_(FTP_data, DATA_TRANSFER_SIZE, 1, fp);                            // read data block to buffer);
240
                 FTP_data[DATA_TRANSFER_SIZE] = 0;             
249
                        FTP_data[DATA_TRANSFER_SIZE] = 0;              
241
           
250
           
242
             if (compressLevel)
251
                if (compressLevel)
243
                 {
252
                        {
244
                        size = CompressBuffer(FTP_data, DATA_TRANSFER_SIZE, 0);
253
                                size = CompressBuffer(FTP_data, DATA_TRANSFER_SIZE, 0);
245
                               
254
                               
246
                         if (compressLevel > 1)
-
 
247
                         {
255
                                if (compressLevel > 1)
248
                                if (size < DATA_TRANSFER_SIZE - 50)
256
                                {
249
                                 {              
257
                                        if (size < DATA_TRANSFER_SIZE - 50)
250
                                        fread_(&FTP_data[size], DATA_TRANSFER_SIZE - size, 1, fp);                              // read data block to buffer);         
258
                                        {              
251
                                        FTP_data[DATA_TRANSFER_SIZE] = 0;              
259
                                                fread_(&FTP_data[size], DATA_TRANSFER_SIZE - size, 1, fp);                              // read data block to buffer);          
252
                                   size = CompressBuffer(FTP_data, DATA_TRANSFER_SIZE, size);
260
                                                FTP_data[DATA_TRANSFER_SIZE] = 0;              
253
                                }
261
                                                size = CompressBuffer(FTP_data, DATA_TRANSFER_SIZE, size);
254
                         }
-
 
255
                 }
262
                                        }
256
                 
263
                                }
257
                 MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 4, &FTP_command, 1, &blockindex, 1, &size, 2, &FTP_data, size);
264
                        }  
258
                }
265
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 4, &FTP_command, 1, &blockindex, 1, &size, 2, &FTP_data, size);
259
        break;
266
                        }
260
 
-
 
261
        case FTP_CMD_CLOSE_FILE:
267
                        break;
-
 
268
 
262
                fclose_(fp);
269
                case FTP_CMD_CLOSE_FILE:
263
                Debug("ftp: CloseFile");               
-
 
264
 
-
 
265
        break;
270
                        fclose_(fp);
266
 
271
                        Debug("ftp: CloseFile");               
267
        case FTP_CMD_CREATE_FILE:       // open the file for writing
272
                        break;
268
        {
273
 
269
                u16 size = DATA_TRANSFER_SIZE;
274
                case FTP_CMD_CREATE_FILE:       // open the file for writing
270
                       
275
                        {
271
                //compressLevel = FTP_data[0];
-
 
272
                compressLevel = 0;                      // no compression for writing
276
                        u16 size = DATA_TRANSFER_SIZE;
273
               
-
 
274
 
277
                        //compressLevel = FTP_data[0];
275
                fp = fopen_(&FTP_data[5], 'w');
278
                        // FTP_data[1..4] contains filesize in bytes
276
                if (fp != NULL)
279
                        compressLevel = 0;                      // no compression for writing
277
                {
280
                        fp = fopen_(&FTP_data[5], 'w');
278
                  blockindex = 0;                                                                               // reset index counter
281
                        if (fp != NULL)
279
                }
282
                        {
280
                else size = 0;                                                                          // this means, no valid file for transfer (maybe readonly)
283
                                blockindex = 0;                                                                 // reset index counter
281
 
284
                        }
282
                 if (size == 0) Debug("ftp: create ERR ");
285
                        else size = 0;                                                                          // this means, no valid file for transfer (maybe readonly)
283
 
-
 
284
                MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &size, 2);
-
 
285
                }
286
                        //if (size == 0) Debug("ftp: create ERR ");
286
        break;
287
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &size, 2);
287
 
288
                        }
288
        case FTP_CMD_SEND_FILE_DATA:
289
                        break;
289
                {
290
 
290
                  u16 size = (u8)FTP_data[2];
291
                case FTP_CMD_SEND_FILE_DATA:
291
                 size *= 0x100;
292
                        {
292
                 size += (u8) FTP_data[1];
293
                        u16 size = (u8)FTP_data[2];
293
 
294
                        size *= 0x100;
294
 
295
                        size += (u8) FTP_data[1];
295
                if (FTP_data[0] == blockindex)                                          // next block is requested
296
                        //      if (FTP_data[0] == blockindex)                                          // next block is requested
296
                 {
297
                        {
297
                        blockindex++;                                  
298
                                blockindex++;                                  
298
                fwrite_(&FTP_data[3], size, 1, fp);                             // write data block to buffer);
299
                        fwrite_(&FTP_data[3], size, 1, fp);                             // write data block to buffer);
299
                 }
300
                        }      
300
                 MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &blockindex, 1 );
-
 
301
                }
301
                        MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'F', NC_ADDRESS, 2, &FTP_command, 1, &blockindex, 1 );
Line 302... Line -...
302
        break;
-