WaspSD Class Reference

WaspSD Class. More...

#include <WaspSD.h>

Collaboration diagram for WaspSD:

Collaboration graph
[legend]

Public Member Functions

 WaspSD ()
 class constructor
uint16_t cleanFlags (void)
 It clears the flag.
uint8_t isSD ()
 It checks if there is a SD Card in the slot.
void ON ()
 It powers the SD card, initialize it and prepare it to work with.
void OFF ()
 It powers off the SD card and closes the pointers.
char * init ()
 It initializes the use of SD cards, looks into the root partition, opens the file system and initializes the public pointer that can be used to access the filesystem.
void close ()
 It closes the directory, filesystem and partition pointers.
void begin ()
 It sets switch and sd_present pin as output and input.
void setMode (uint8_t mode)
 It sets power mode.
char * print_disk_info ()
 It packs all the data about the disk into the buffer and returns it back. The buffer will then be available and offer the data to the developers as a human-readable encoded string.
offset_t getDiskSize ()
 It gets the total disk size.
offset_t getDiskFree ()
 It gets the free disk size.
uint8_t cd (const char *command)
 It changes the directory.
uint8_t cd (struct fat_dir_entry_struct subdir_entry)
 It changes the directory.
int8_t numFiles ()
 It gets the amount of files in a directory.
char * ls (void)
 It lists a directory.
char * ls (int offset)
 It lists a directory.
char * ls (int offset, int scope, uint8_t info)
 It lists a directory.
uint8_t find_file_in_dir (const char *name, struct fat_dir_entry_struct *dir_entry)
 It tests existence of files in the dd folder.
uint8_t mkdir (const char *dirname)
 It creates a directory.
uint8_t isDir (struct fat_dir_entry_struct file_entry)
 It checks if an entry is a file or a directory.
int8_t isDir (const char *dirname)
 It checks if an entry is a file or a directory.
uint8_t del (const char *name)
 It deletes a file or a directory.
uint8_t delDir (uint8_t depth)
 It deletes a directory.
uint8_t delFile (struct fat_dir_entry_struct file_entry)
 It deletes a file.
struct fat_file_structopenFile (const char *filename)
 It opens a file.
void closeFile (struct fat_file_struct *_fd)
 It closes a file.
int8_t isFile (const char *filename)
 It tests the existence of a file in the current folder.
int32_t numln (const char *filename)
 It gets the amount of lines in a file.
int32_t getFileSize (const char *name)
 It gets the file size for filename in the current folder.
char * getAttributes (const char *name)
 It gets the attributes for a directory or file entry.
char * cat (const char *filename, int32_t offset, uint16_t scope)
 It dumps into the buffer the amount of bytes in scope after offset coming from filename.
uint8_t * catBin (const char *filename, int32_t offset, uint16_t scope)
 It dumps into the bufferBin the amount of bytes in scope after offset coming from filename.
char * catln (const char *filename, uint32_t offset, uint16_t scope)
 It dumps into the buffer the amount of lines in scope after offset lines coming from filename.
int32_t indexOf (const char *filename, const char *pattern, uint32_t offset)
 It searches for first occurrence of a string in a file.
uint8_t create (const char *filename)
 It creates a file.
uint8_t writeSD (const char *filename, const char *str, int32_t offset)
 It writes strings to a file.
uint8_t writeSD (const char *filename, uint8_t *str, int32_t offset)
 It writes integer array to a file.
uint8_t writeSD (const char *filename, const char *str, int32_t offset, int16_t length)
 It writes strings to a file of a specific length.
uint8_t append (const char *filename, const char *str)
 It writes strings at the end of files.
uint8_t append (const char *filename, const char *str, uint16_t length)
 It writes strings at the end of files of a specific length.
uint8_t append (const char *filename, uint8_t *str)
 It writes integer arrays at the end of files.
uint8_t appendln (const char *filename, const char *str)
 It writes strings at the end of files adding an EOL.
uint8_t appendln (const char *filename, uint8_t *str)
 It writes integer arrays at the end of files adding an EOL.
const char * getLibVersion (void)
 It gets the library version.

Data Fields

char buffer [DOS_BUFFER_SIZE]
 Variable : buffer containing the information coming from the card used to avoid calls to UART functions inside the library. Beware, there could be data longer than the buffer size.
uint8_t bufferBin [BIN_BUFFER_SIZE]
 Variable : buffer containing the binary information coming from the card used to avoid calls to UART functions inside the library. Beware, there could be data longer than the buffer size.
uint16_t flag
 Variable : flag storing the state of the SD card during initialization and operation.
uint8_t _pwrMode
 Variable : It stores the SD power mode. Possible values are SD_ON or SD_OFF.
struct fat_fs_structfs
 Structure pointer : filesystem pointer.
struct partition_structpartition
 Structure pointer : partition pointer.
struct fat_dir_structdd
 Structure pointer : directory structure pointer.
struct fat_file_structfd
 Structure pointer : generic file pointer.
offset_t diskFree
 Variable : amount of free bytes in the drive.
offset_t diskSize
 Variable : total byte size of the drive.

Detailed Description

WaspSD Class.

WaspSD Class defines all the variables and functions used to manage the SD Card

Definition at line 174 of file WaspSD.h.


Constructor & Destructor Documentation

WaspSD::WaspSD (  ) 

class constructor

It does nothing

Parameters:
void 
Returns:
void

Definition at line 32 of file WaspSD.cpp.

00033 {
00034     // nothing to do
00035 }


Member Function Documentation

uint16_t WaspSD::cleanFlags ( void   ) 

It clears the flag.

It does nothing

Parameters:
void 
Returns:
'flag' variable

Definition at line 1749 of file WaspSD.cpp.

References flag.

01750 {
01751   flag = 0;
01752   return flag;
01753 }

uint8_t WaspSD::isSD (  ) 

It checks if there is a SD Card in the slot.

Parameters:
void 
Returns:
'1' if SD is present, '0' otherwise

Definition at line 218 of file WaspSD.cpp.

References close(), digitalRead(), and SD_PRESENT.

Referenced by cat(), catBin(), catln(), cd(), create(), del(), find_file_in_dir(), getAttributes(), getDiskFree(), getDiskSize(), getFileSize(), indexOf(), init(), ls(), mkdir(), numFiles(), numln(), openFile(), print_disk_info(), and writeSD().

00219 {
00220   if (digitalRead(SD_PRESENT))    return 1;
00221     
00222   // if the SD is not there, the best is to close all the pointers
00223   // just to avoid problems later one with calls to functions  
00224   close();
00225   return 0;
00226 }

Here is the call graph for this function:

Here is the caller graph for this function:

void WaspSD::ON ( void   ) 

It powers the SD card, initialize it and prepare it to work with.

Parameters:
void 
Returns:
void
See also:
close(), begin()

Definition at line 44 of file WaspSD.cpp.

References begin(), init(), SD_ON, and setMode().

00045 {
00046         begin();
00047         setMode(SD_ON);
00048         init();
00049 }

Here is the call graph for this function:

void WaspSD::OFF ( void   ) 

It powers off the SD card and closes the pointers.

Parameters:
void 
Returns:
void
See also:
close(), begin()

Definition at line 57 of file WaspSD.cpp.

References close(), SD_OFF, and setMode().

00058 {
00059         close();
00060         setMode(SD_OFF);
00061 }

Here is the call graph for this function:

char * WaspSD::init ( void   ) 

It initializes the use of SD cards, looks into the root partition, opens the file system and initializes the public pointer that can be used to access the filesystem.

Parameters:
void 
Returns:
human readable string indicating success or possible errors that can be printed by the user directly

Definition at line 112 of file WaspSD.cpp.

References CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, dd, fat_get_dir_entry_of_path(), fat_open(), fat_open_dir(), FILESYSTEM_FAILED, FILESYSTEM_FAILED_em, flag, fs, INIT_FAILED, INIT_FAILED_em, isSD(), NOTHING_FAILED, NOTHING_FAILED_em, partition, PARTITION_FAILED, PARTITION_FAILED_em, partition_open(), ROOT_DIR_FAILED, ROOT_DIR_FAILED_em, sd_raw_init(), sd_raw_read(), sd_raw_read_interval(), sd_raw_write(), and sd_raw_write_interval().

Referenced by ON(), WaspGPRS::readDataFTP(), and WaspGPRS::sendDataFTP().

00113 {
00114 
00115   // check if the card is there or not
00116   if (!isSD())
00117   {
00118     flag = CARD_NOT_PRESENT;
00119     return CARD_NOT_PRESENT_em;
00120   }
00121   
00122 
00123   // proceed with initialization
00124   if(!sd_raw_init())
00125   {
00126     flag = INIT_FAILED;
00127     return INIT_FAILED_em;
00128   }
00129   
00130 
00131   // open first partition
00132   partition = partition_open(sd_raw_read,
00133                             sd_raw_read_interval,
00134                             sd_raw_write,
00135                             sd_raw_write_interval,
00136                             0
00137                             );
00138 
00139 
00140   if(!partition)
00141   {
00142     // If the partition did not open, assume the storage device
00143     // is a "superfloppy", i.e. has no MBR.
00144     partition = partition_open(sd_raw_read,
00145                             sd_raw_read_interval,
00146                             sd_raw_write,
00147                             sd_raw_write_interval,
00148                             -1
00149                             );
00150     
00151 
00152     if(!partition)
00153     {
00154       flag = PARTITION_FAILED;
00155       return PARTITION_FAILED_em;
00156     }
00157   }
00158   
00159 
00160   // open file system 
00161   fs = fat_open(partition);
00162   if(!fs)
00163   {
00164     flag = FILESYSTEM_FAILED;
00165     return FILESYSTEM_FAILED_em;
00166   }
00167   
00168 
00169   // open root directory
00170   struct fat_dir_entry_struct directory;
00171   fat_get_dir_entry_of_path(fs, "/", &directory);
00172 
00173 
00174   dd = fat_open_dir(fs, &directory);
00175   if(!dd)
00176   {
00177     flag = ROOT_DIR_FAILED;
00178     return ROOT_DIR_FAILED_em;
00179   }
00180   
00181 
00182   flag = NOTHING_FAILED;
00183   return NOTHING_FAILED_em;
00184 }

Here is the call graph for this function:

Here is the caller graph for this function:

void WaspSD::close ( void   ) 

It closes the directory, filesystem and partition pointers.

This function closes all the pointers in use in the library, so that they can be reused again after a call to init(). It becomes very useful if e.g. the card is removed and inserted again

Parameters:
void 
Returns:
void

Definition at line 194 of file WaspSD.cpp.

References dd, fat_close(), fat_close_dir(), fs, INPUT, partition, partition_close(), pinMode(), SD_MISO, SD_MOSI, SD_SCK, and SD_SS.

Referenced by isSD(), OFF(), WaspGPRS::readDataFTP(), and WaspGPRS::sendDataFTP().

00195 {
00196   // close dir 
00197   fat_close_dir(dd);
00198 
00199   // close file system 
00200   fat_close(fs);
00201 
00202   // close partition 
00203   partition_close(partition);
00204   
00205   pinMode(SD_SS,INPUT);
00206   pinMode(SD_SCK,INPUT);
00207   pinMode(SD_MISO,INPUT);
00208   pinMode(SD_MOSI,INPUT);
00209 }

Here is the call graph for this function:

Here is the caller graph for this function:

void WaspSD::begin ( void   ) 

It sets switch and sd_present pin as output and input.

Parameters:
void 
Returns:
void

Definition at line 67 of file WaspSD.cpp.

References INPUT, MEM_PW, OUTPUT, pinMode(), and SD_PRESENT.

Referenced by ON(), WaspGPRS::readDataFTP(), and WaspGPRS::sendDataFTP().

00068 {
00069   // activate the power to the SD card
00070   pinMode(MEM_PW, OUTPUT);
00071   pinMode(SD_PRESENT, INPUT);
00072 }

Here is the call graph for this function:

Here is the caller graph for this function:

void WaspSD::setMode ( uint8_t  mode  ) 

It sets power mode.

Parameters:
uint8_t mode : SD_ON or SD_OFF
Returns:
void

Definition at line 80 of file WaspSD.cpp.

References _pwrMode, delay(), digitalWrite(), HIGH, LOW, MEM_PW, SD_OFF, and SD_ON.

Referenced by OFF(), ON(), WaspGPRS::readDataFTP(), and WaspGPRS::sendDataFTP().

00081 {
00082         _pwrMode=mode;
00083         switch(_pwrMode)
00084         {
00085                 case    SD_ON:
00086                         digitalWrite(MEM_PW, HIGH);
00087                         delay(10);
00088                         break;
00089                 case    SD_OFF:
00090                         digitalWrite(MEM_PW,LOW);
00091                         break;
00092         }
00093 }

Here is the call graph for this function:

Here is the caller graph for this function:

char * WaspSD::print_disk_info (  ) 

It packs all the data about the disk into the buffer and returns it back. The buffer will then be available and offer the data to the developers as a human-readable encoded string.

Parameters:
void 
Returns:
human readable string indicating success or possible errors that can be printed by the user directly

Definition at line 307 of file WaspSD.cpp.

References buffer, sd_raw_info::capacity, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, diskFree, diskSize, fat_get_fs_free(), fat_get_fs_size(), flag, sd_raw_info::flag_copy, sd_raw_info::flag_write_protect, sd_raw_info::flag_write_protect_temp, sd_raw_info::format, fs, isSD(), sd_raw_info::manufacturer, sd_raw_info::manufacturing_month, sd_raw_info::manufacturing_year, sd_raw_info::oem, sd_raw_info::product, sd_raw_info::revision, sd_raw_get_info(), and sd_raw_info::serial.

00308 {
00309     struct fat_fs_struct* _fs;
00310     _fs=fs;
00311     
00312     // check if the card is there or not
00313   if (!isSD())
00314   {
00315     flag = CARD_NOT_PRESENT;
00316     sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00317     return buffer;
00318   }
00319 
00320     if(!fs)
00321         return 0;
00322 
00323     struct sd_raw_info disk_info;
00324     if(!sd_raw_get_info(&disk_info))
00325         return 0;
00326 
00327     // update the publicly available size-related variables
00328     diskFree = fat_get_fs_free(_fs);
00329     diskSize = fat_get_fs_size(_fs);
00330  
00331     sprintf(buffer, "" \
00332     "manuf:  0x%x\n" \   
00333     "oem:    %s\n" \     
00334     "prod:   %s\n" \     
00335     "rev:    %x\n" \     
00336     "serial: 0x%lx\n" \   
00337     "date:   %u/%u\n" \  
00338     "size:   %u MB\n" \
00339     "free:   %lu/%lu\n" \
00340     "copy:   %u\n" \     
00341     "wr.pr.: %u/%u\n" \  
00342     "format: %u\n",   
00343     disk_info.manufacturer,          
00344     (char*) disk_info.oem,           
00345     (char*) disk_info.product,       
00346     disk_info.revision,              
00347     disk_info.serial,                
00348     disk_info.manufacturing_month,    
00349     disk_info.manufacturing_year,    
00350     disk_info.capacity / 1024 / 1024,                
00351     diskFree>>16,
00352     diskSize>>16,
00353     disk_info.flag_copy,             
00354     disk_info.flag_write_protect_temp,
00355     disk_info.flag_write_protect,    
00356     disk_info.format);            
00357     
00358     return buffer;
00359 }

Here is the call graph for this function:

offset_t WaspSD::getDiskSize (  ) 

It gets the total disk size.

Parameters:
void 
Returns:
the total size for the disk

Definition at line 235 of file WaspSD.cpp.

References buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, diskSize, fat_get_fs_size(), flag, fs, and isSD().

00236 {
00237     struct fat_fs_struct* _fs;            // filesystem pointer
00238     _fs=fs;
00239     
00240     // check if the card is there or not
00241     if (!isSD())
00242     {
00243         flag = CARD_NOT_PRESENT;
00244         sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00245         return 0;
00246     }
00247  
00248   // update the publicly available size-related variables
00249     diskSize = fat_get_fs_size(_fs);
00250     return diskSize;
00251 }

Here is the call graph for this function:

offset_t WaspSD::getDiskFree (  ) 

It gets the free disk size.

Parameters:
void 
Returns:
the total available space for the disk

Definition at line 260 of file WaspSD.cpp.

References buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, diskFree, fat_get_fs_free(), flag, fs, and isSD().

00261 {
00262     struct fat_fs_struct* _fs;
00263     _fs = fs;
00264   
00265     // check if the card is there or not
00266     if (!isSD())
00267     {
00268         flag = CARD_NOT_PRESENT;
00269         sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00270         return 0;
00271     }
00272 
00273   // update the publicly available size-related variables
00274     diskFree = fat_get_fs_free(_fs);
00275     return diskFree;
00276 }

Here is the call graph for this function:

uint8_t WaspSD::cd ( const char *  command  ) 

It changes the directory.

Parameters:
const char* command : the directory we want to change to
Returns:
'1' on success, '0' if error
See also:
cd(struct fat_dir_entry_struct subdir_entry)

Definition at line 394 of file WaspSD.cpp.

References buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, dd, fat_close_dir(), fat_open_dir(), find_file_in_dir(), flag, fs, and isSD().

Referenced by del(), numFiles(), WaspGPRS::readDataFTP(), and WaspGPRS::sendDataFTP().

00395 {
00396     struct fat_fs_struct* _fs;
00397     _fs=fs;
00398     
00399     uint8_t exit=2;
00400     while(exit>0)
00401     {
00402         // check if the card is there or not
00403         if (!isSD())
00404         {
00405             flag = CARD_NOT_PRESENT;
00406             sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00407             return 0;
00408         }
00409         struct fat_dir_entry_struct subdir_entry;
00410         if(find_file_in_dir(command, &subdir_entry))
00411         {
00412             struct fat_dir_struct* dd_new = fat_open_dir(_fs, &subdir_entry);
00413             if(dd_new)
00414             {
00415                 fat_close_dir(dd);
00416                 dd = dd_new;
00417                 return 1;
00418             }
00419         }
00420         exit--;
00421         if(!exit) return 0;
00422     }
00423 }

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t WaspSD::cd ( struct fat_dir_entry_struct  subdir_entry  ) 

It changes the directory.

Parameters:
struct fat_dir_entry_struct subdir_entry : the directory we want to change to
Returns:
'1' on success, '0' if error
See also:
cd(const char* command)

Definition at line 376 of file WaspSD.cpp.

References dd, fat_close_dir(), fat_open_dir(), and fs.

00377 {
00378     struct fat_fs_struct* _fs;
00379     _fs=fs;
00380     
00381     struct fat_dir_struct* dd_new = fat_open_dir(_fs, &subdir_entry);
00382     if(dd_new)
00383     {
00384         fat_close_dir(dd);
00385         dd = dd_new;
00386         return 1;
00387     }
00388     return 0;
00389 }

Here is the call graph for this function:

int8_t WaspSD::numFiles (  ) 

It gets the amount of files in a directory.

Parameters:
void 
Returns:
'0' if no files, a negative value if error and a possitive value indicating the amount of files

Definition at line 1425 of file WaspSD.cpp.

References buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, cd(), dd, fat_read_dir(), flag, and isSD().

01426 {
01427   // check if the card is there or not
01428   if (!isSD())
01429   {
01430     flag = CARD_NOT_PRESENT;
01431     sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01432     return -1;
01433   }
01434 
01435   uint8_t cont = -2;
01436   const char* command=".";
01437   struct fat_dir_entry_struct dir_entry;
01438   if(!(cd(command))) cont=0;
01439   while(fat_read_dir(dd, &dir_entry))
01440   {
01441     cont++;
01442   }
01443 
01444   return cont;
01445 }

Here is the call graph for this function:

char * WaspSD::ls ( void   ) 

It lists a directory.

Parameters:
void 
Returns:
'buffer' variable containing the corresponding listing
See also:
ls(int offset), ls(int offset, int scope, uint8_t info)

Definition at line 629 of file WaspSD.cpp.

References SIZES.

Referenced by ls(), and WaspGPRS::readDataFTP().

00630 {
00631   return ls(0,0, SIZES);    // calls DOS_ls to list files until filling the buffer
00632 }

Here is the caller graph for this function:

char * WaspSD::ls ( int  offset  ) 

It lists a directory.

Parameters:
int offset : it jumps over "offset" filenames in the list
Returns:
'buffer' variable containing the corresponding listing
See also:
ls(void), ls(int offset, int scope, uint8_t info)

Definition at line 597 of file WaspSD.cpp.

References ls(), and SIZES.

00598 {
00599   return ls(offset, 1, SIZES);  // call DOS_ls with just the offset parameter
00600 }

Here is the call graph for this function:

char * WaspSD::ls ( int  offset,
int  scope,
uint8_t  info = NAMES 
)

It lists a directory.

Parameters:
int offset : it jumps over "offset" filenames in the list
int scope : it includes a total of "scope" filenames in the buffer
int info : limits the amount of information to be sent back, ranges from NAMES, SIZES, to ATTRIBUTES
Returns:
'buffer' variable containing the corresponding listing
See also:
ls(void), ls(int offset)

Definition at line 457 of file WaspSD.cpp.

References ATTRIBUTES, fat_dir_entry_struct::attributes, buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, dd, DOS_BUFFER_SIZE, FAT_ATTRIB_DIR, FAT_ATTRIB_READONLY, fat_read_dir(), fat_dir_entry_struct::file_size, flag, fs, isSD(), fat_dir_entry_struct::long_name, NAMES, SIZES, and TRUNCATED_DATA.

00458 {
00459     struct fat_fs_struct* _fs;
00460     struct fat_dir_struct* _dd;
00461     _fs=fs;
00462     _dd=dd;
00463     
00464   // check if the card is there or not
00465   if (!isSD())
00466   {
00467     flag = CARD_NOT_PRESENT;
00468     sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00469     return buffer;
00470   }
00471     
00472   // if scope is zero, then we should make it 
00473   // ridiculously big, so that it goes on reading forever
00474   if (scope <= 0) scope = 1000;
00475   
00476   flag &= ~(TRUNCATED_DATA);
00477   struct fat_dir_entry_struct dir_entry;
00478   
00479   // clean the offset (directory listings before the one requested)
00480   if (offset-- > 0) while(fat_read_dir(_dd, &dir_entry) && offset > 0) 
00481   {
00482       offset--;
00483   }
00484 
00485   // declare the variables needed to iterate the listings
00486   int buff_count = 0, j = 0, buff_size = 30, BUFF_SIZE = 30, dos_buff_left = DOS_BUFFER_SIZE;
00487   
00488   // iterate the directory
00489   // if there is a directory entry, then create a string out of it
00490   // and add it to the buffer
00491   while(fat_read_dir(_dd, &dir_entry) && scope > 0)
00492   {
00493     buff_size = BUFF_SIZE;
00494     
00495     // create the line variable
00496     char line[BUFF_SIZE];
00497     
00498     // fill in the variable with the corresponding string
00499     switch (info)
00500     {
00501       case NAMES:
00502         sprintf(line, "%s\n", dir_entry.long_name);
00503         break;
00504       case SIZES:
00505         sprintf(line, "%s%c\t%lu\n", dir_entry.long_name, dir_entry.attributes & FAT_ATTRIB_DIR ? '/' : ' ', dir_entry.file_size);
00506         break;
00507       case ATTRIBUTES:
00508         sprintf(line, "%c%c %s\t%lu\n", dir_entry.attributes & FAT_ATTRIB_DIR ? 'd' : '-', dir_entry.attributes & FAT_ATTRIB_READONLY ? 'r' : 'w', dir_entry.long_name, dir_entry.file_size);
00509         break;
00510       default:
00511         // by default print only filenames, like the linux "ls" command
00512         sprintf(line, "%s\n", dir_entry.long_name);
00513         break;
00514     }
00515     
00516     // add the line to the buffer (this won't work with another sprintf, libc-avr goes bananas)
00517     for (j=0; j < BUFF_SIZE; j++) 
00518     {
00519       if (j + buff_count < DOS_BUFFER_SIZE - 4) 
00520       {
00521         if (line[j] != '\0') buffer[j + buff_count] = line[j];
00522       
00523         // in case you reach end of string, jump off the loop
00524         if (buffer[j + buff_count] == '\n' || line[j] == '\0') 
00525         {
00526           buff_size = j + buff_count + 1;
00527           //continue;
00528           j = BUFF_SIZE;  // dirty way of leaving the loop
00529         }
00530       } 
00531       else
00532       {
00533         // in case we filled up the whole buffer, we add a
00534         // visual end of buffer indicator and leave the loop
00535         buffer[DOS_BUFFER_SIZE - 4] = '>';
00536         buffer[DOS_BUFFER_SIZE - 3] = '>';
00537         buffer[DOS_BUFFER_SIZE - 2] = '\n';
00538         buffer[DOS_BUFFER_SIZE - 1] = '\0';
00539         flag |= TRUNCATED_DATA; 
00540 
00541         // go to the end of the directory
00542         while(fat_read_dir(_dd, &dir_entry)); 
00543         return buffer;  // leave the function here
00544       }
00545     }
00546     buff_count = buff_size;
00547     scope--;
00548   }
00549 
00550   // add an end of string to the buffer
00551   buffer[buff_count] = '\0';
00552   
00553   // go to the end of the directory
00554   while(fat_read_dir(_dd, &dir_entry)); 
00555 
00556   // return the buffer as long as there is any
00557   // data inside, zero if the directory was empty or
00558   // if there was an error
00559   if (buff_count > 0) return buffer;
00560   return 0;
00561 }

Here is the call graph for this function:

uint8_t WaspSD::find_file_in_dir ( const char *  name,
struct fat_dir_entry_struct dir_entry 
)

It tests existence of files in the dd folder.

Parameters:
const char* name : the file to find
struct fat_dir_entry_struct* dir_entry : the directory pointer to find the file in
Returns:
'1' if the file is availabe, '0' otherwise

Definition at line 643 of file WaspSD.cpp.

References buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, dd, fat_read_dir(), fat_reset_dir(), flag, isSD(), and fat_dir_entry_struct::long_name.

Referenced by cd(), del(), getAttributes(), getFileSize(), isDir(), isFile(), mkdir(), and openFile().

00644 {
00645     struct fat_dir_struct* _dd;
00646     _dd=dd;
00647   // check if the card is there or not
00648     if (!isSD())
00649     {
00650         flag = CARD_NOT_PRESENT;
00651         sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00652         return 0;
00653     }
00654 
00655     while(fat_read_dir(_dd, dir_entry))
00656     {
00657         if(strcmp(dir_entry->long_name, name) == 0)
00658         {
00659             fat_reset_dir(_dd);
00660             return 1;
00661         }
00662     }
00663 
00664     return 0;
00665 }

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t WaspSD::mkdir ( const char *  dirname  ) 

It creates a directory.

Parameters:
const char* dirname : the directory to create
Returns:
'1' on success, '0' otherwise

Definition at line 1396 of file WaspSD.cpp.

References buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, dd, DIR_CREATION_ERROR, fat_create_dir(), find_file_in_dir(), flag, and isSD().

01397 {
01398     struct fat_dir_struct* _dd;
01399     _dd=dd;
01400   // check if the card is there or not
01401   if (!isSD())
01402   {
01403     flag = CARD_NOT_PRESENT;
01404     flag |= DIR_CREATION_ERROR;
01405     sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01406     return 0;
01407   }
01408 
01409   flag &= ~(DIR_CREATION_ERROR);
01410   struct fat_dir_entry_struct dir_entry;
01411   if( (find_file_in_dir(dirname,&dir_entry)) || (!fat_create_dir(_dd, dirname, &dir_entry)) )
01412   {
01413       flag |= DIR_CREATION_ERROR;
01414       return 0;
01415   }
01416   return 1;
01417 }

Here is the call graph for this function:

uint8_t WaspSD::isDir ( struct fat_dir_entry_struct  file_entry  ) 

It checks if an entry is a file or a directory.

Parameters:
struct fat_dir_entry_struct file_entry : the entry to check
Returns:
'1' if it is a directory, '0' otherwise
See also:
isDir(const char* dirname)

Definition at line 1240 of file WaspSD.cpp.

References fat_dir_entry_struct::attributes, and FAT_ATTRIB_DIR.

Referenced by del(), and delDir().

01241 {
01242     return dir_entry.attributes & FAT_ATTRIB_DIR ? 1 : 0;
01243 }

Here is the caller graph for this function:

int8_t WaspSD::isDir ( const char *  dirname  ) 

It checks if an entry is a file or a directory.

Parameters:
const char* dirname : the entry to check
Returns:
'1' if it is a directory, '0' otherwise
See also:
isDir(struct fat_dir_entry_struct file_entry)

Definition at line 1252 of file WaspSD.cpp.

References fat_dir_entry_struct::attributes, FAT_ATTRIB_DIR, and find_file_in_dir().

01253 {
01254     struct fat_dir_entry_struct dir_entry;
01255     if(find_file_in_dir(dirname,&dir_entry)) 
01256     {
01257         return dir_entry.attributes & FAT_ATTRIB_DIR ? 1 : 0;
01258     }
01259     return -1;
01260 }

Here is the call graph for this function:

uint8_t WaspSD::del ( const char *  name  ) 

It deletes a file or a directory.

It allows only erasing depth one directories, thus if the user calls to erase a directory with subdirs, it will exit with error without erasing the directory . It also allows erasing current directory "." under the same premises: it should contain no subdirectories or it will exit with error. Thanks to this function, together with delFile, delDir and isDir it is possible to create more complex delete functions that could iterate through any directory structure

Parameters:
const char* name : the file or directory to delete
Returns:
'1' on success, '0' otherwise
See also:
delDir(uint8_t depth), delFile(struct fat_dir_entry_struct file_entry)

Definition at line 1321 of file WaspSD.cpp.

References buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, cd(), delDir(), delFile(), find_file_in_dir(), flag, isDir(), isSD(), WaspUtils::strCmp(), and Utils.

Referenced by WaspGPS::saveEphems().

01322 {
01323     
01324   // check if the card is there or not
01325   if (!isSD())
01326   {
01327     flag = CARD_NOT_PRESENT;
01328     sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01329     return 0;
01330   }
01331 
01332   // first case: erase the contents in current directory
01333   if (Utils.strCmp(name,".\0",2) == 0)
01334   {
01335     return delDir(0);
01336   }
01337   
01338   // second case: erase whatever else
01339   struct fat_dir_entry_struct file_entry;
01340   if(find_file_in_dir(name,&file_entry))
01341   {
01342     if (isDir(file_entry))
01343     {
01344       if(!(cd(file_entry))) return 0;
01345       uint8_t aux = delDir(0);
01346       if(!(cd(".."))) return 0;
01347       if (aux) 
01348       {
01349         aux &= delFile(file_entry);
01350       }
01351       return aux;
01352     }
01353     else
01354       return delFile(file_entry);
01355   }
01356   
01357   // if you made this far, it gotta be an error
01358   return 0;
01359 }

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t WaspSD::delDir ( uint8_t  depth  ) 

It deletes a directory.

It allows only erasing depth one directories, thus if the user calls to erase a directory with subdirs, it will exit with error without erasing the directory . It also allows erasing current directory "." under the same premises: it should contain no subdirectories or it will exit with error. Thanks to this function, together with delFile, del and isDir it is possible to create more complex delete functions that could iterate through any directory structure

Parameters:
uint8_t depth : is still not used, but function supports developers actualizations
Returns:
'1' on success, '0' otherwise
See also:
del(const char* name), delFile(struct fat_dir_entry_struct file_entry)

Definition at line 1283 of file WaspSD.cpp.

References dd, fat_delete_file(), fat_read_dir(), fs, isDir(), fat_dir_entry_struct::long_name, WaspUtils::strCmp(), and Utils.

Referenced by del().

01284 {
01285   uint8_t exit = 1;
01286   struct fat_dir_entry_struct dir_entry;
01287   struct fat_dir_struct* _dd;
01288   struct fat_fs_struct* _fs;
01289   _fs=fs;
01290   _dd = dd;
01291   while(fat_read_dir(_dd, &dir_entry))
01292   {
01293     if (isDir(dir_entry) && !(Utils.strCmp(dir_entry.long_name,".\0",2) == 0 || Utils.strCmp(dir_entry.long_name,"..\0",3) == 0)) exit = 0;
01294   }
01295   if (exit) while(fat_read_dir(_dd, &dir_entry))
01296   {
01297     if (!(Utils.strCmp(dir_entry.long_name,".\0",2) == 0 || Utils.strCmp(dir_entry.long_name,"..\0",3) == 0)) 
01298     {
01299       exit &= fat_delete_file(_fs, &dir_entry);
01300     }
01301   }
01302   return exit;
01303 }

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t WaspSD::delFile ( struct fat_dir_entry_struct  file_entry  ) 

It deletes a file.

Thanks to this function, together with delDir, del and isDir it is possible to create more complex delete functions that could iterate through any directory structure

Parameters:
struct fat_dir_entry_struct file_entry : the file to delete
Returns:
'1' on success, '0' otherwise
See also:
del(const char* name), delDir(uint8_t depth)

Definition at line 1268 of file WaspSD.cpp.

References fat_delete_file(), and fs.

Referenced by del().

01269 {
01270     struct fat_fs_struct* _fs;
01271     _fs=fs;
01272     if(fat_delete_file(_fs,&file_entry)) return 1;
01273     return 0;
01274 }

Here is the call graph for this function:

Here is the caller graph for this function:

struct fat_file_struct * WaspSD::openFile ( const char *  filename  )  [read]

It opens a file.

Parameters:
const char* filename : the file to open
Returns:
'0' if error, file pointer on success

Definition at line 696 of file WaspSD.cpp.

References buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, dd, fat_open_file(), fd, FILE_OPEN_ERROR, find_file_in_dir(), flag, fs, and isSD().

Referenced by cat(), catBin(), catln(), indexOf(), numln(), and writeSD().

00697 {
00698 
00699     struct fat_fs_struct* _fs;
00700     struct fat_dir_struct* _dd;
00701     _fs=fs;
00702     _dd=dd;
00703     struct fat_file_struct* _fd;
00704     _fd=fd;
00705     
00706   // check if the card is there or not
00707   if (!isSD())
00708   {
00709     flag = CARD_NOT_PRESENT;
00710     flag |= FILE_OPEN_ERROR;
00711     sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00712     return 0;
00713   }
00714 
00715   flag &= ~(FILE_OPEN_ERROR);
00716   struct fat_dir_entry_struct file_entry;
00717     if(!find_file_in_dir(filename,&file_entry))
00718     {
00719         flag |= FILE_OPEN_ERROR;
00720         return 0;
00721     }
00722 
00723     _fd=fat_open_file(_fs,&file_entry);
00724     if(!_fd) return fat_open_file(_fs,&file_entry);
00725     return _fd;
00726 }

Here is the call graph for this function:

Here is the caller graph for this function:

void WaspSD::closeFile ( struct fat_file_struct _fd  ) 

It closes a file.

Parameters:
struct fat_file_struct* _fd : the file to close
Returns:
void

Definition at line 734 of file WaspSD.cpp.

References fat_close_file().

00735 {
00736     fat_close_file(_fd);
00737 }

Here is the call graph for this function:

int8_t WaspSD::isFile ( const char *  filename  ) 

It tests the existence of a file in the current folder.

Parameters:
const char* filename : the file to check
Returns:
'1' on success, '0' if it is a directory, '-1' otherwise

Definition at line 677 of file WaspSD.cpp.

References fat_dir_entry_struct::attributes, FAT_ATTRIB_DIR, and find_file_in_dir().

Referenced by WaspGPS::saveEphems().

00678 {
00679     struct fat_dir_entry_struct file_entry;
00680     if(find_file_in_dir(filename,&file_entry)) 
00681     {
00682         return file_entry.attributes & FAT_ATTRIB_DIR ? 0 : 1;
00683     }
00684     return -1;
00685 }

Here is the call graph for this function:

Here is the caller graph for this function:

int32_t WaspSD::numln ( const char *  filename  ) 

It gets the amount of lines in a file.

Parameters:
const char* filename : the file to check
Returns:
number of lines on success, '-1' otherwise

Definition at line 1696 of file WaspSD.cpp.

References buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, fat_close_file(), fat_read_file(), fd, FILE_OPEN_ERROR, flag, isSD(), and openFile().

01697 {
01698     struct fat_file_struct* _fd;
01699     _fd=fd;
01700   // check if the card is there or not
01701   if (!isSD())
01702   {
01703     flag = CARD_NOT_PRESENT;
01704     flag |= FILE_OPEN_ERROR;
01705     sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01706     return -1;
01707   }
01708     
01709   flag &= ~(FILE_OPEN_ERROR);
01710 
01711   // search file in current directory and open it 
01712   // assign the file pointer to the general file pointer "fp"
01713   // exit if error and modify the general flag with FILE_OPEN_ERROR
01714   _fd = openFile(filename);
01715   if(!_fd)
01716   {
01717     sprintf(buffer, "error opening %s", filename);
01718     flag |= FILE_OPEN_ERROR;
01719     return -1;
01720   }
01721 
01722   byte maxBuffer = 1;  // size of the buffer to use when reading
01723   uint8_t bufferSD[maxBuffer];
01724   uint32_t cont = 0;
01725   
01726   // jump over offset lines
01727   uint8_t readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01728   while( readRet > 0)   
01729   {
01730     for(uint8_t i = 0; i < maxBuffer; ++i)
01731     {
01732       if (bufferSD[i] == '\n')
01733         cont++;
01734     }
01735     readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01736   }
01737 
01738   fat_close_file(_fd);
01739 
01740   return cont;
01741 
01742 }

Here is the call graph for this function:

int32_t WaspSD::getFileSize ( const char *  name  ) 

It gets the file size for filename in the current folder.

Parameters:
const char* name : the file to check
Returns:
file size on success, '-1' otherwise

Definition at line 747 of file WaspSD.cpp.

References buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, FILE_OPEN_ERROR, fat_dir_entry_struct::file_size, find_file_in_dir(), flag, and isSD().

Referenced by append(), and indexOf().

00748 {
00749   // check if the card is there or not
00750   if (!isSD())
00751   {
00752     flag = CARD_NOT_PRESENT;
00753     flag |= FILE_OPEN_ERROR;
00754     sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00755     return -1;
00756   }
00757 
00758   flag &= ~(FILE_OPEN_ERROR);
00759   struct fat_dir_entry_struct file_entry;
00760     if(!find_file_in_dir(name,&file_entry))
00761     {
00762         flag |= FILE_OPEN_ERROR;
00763         return -1;
00764     }
00765 
00766     return file_entry.file_size;
00767 }

Here is the call graph for this function:

Here is the caller graph for this function:

char * WaspSD::getAttributes ( const char *  name  ) 

It gets the attributes for a directory or file entry.

Parameters:
const char* name : the file or directory to check
Returns:
returns the attributes for a directory or file entry in the current directory. The attributes are encoded with two characters:
  • char #1: it is either "d" for a directory or "-" for a file entry
  • char #2: is either "r" for read only, and "w" if the file/directory is also writeable If the file or directory is not available in the folder, it will answer "--"

Definition at line 781 of file WaspSD.cpp.

References fat_dir_entry_struct::attributes, buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, FAT_ATTRIB_DIR, FAT_ATTRIB_READONLY, FILE_OPEN_ERROR, find_file_in_dir(), flag, and isSD().

00782 {
00783   // check if the card is there or not
00784   if (!isSD())
00785   {
00786     flag = CARD_NOT_PRESENT;
00787     flag |= FILE_OPEN_ERROR;
00788     sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00789     return buffer;
00790   }
00791 
00792   flag &= ~(FILE_OPEN_ERROR);
00793   struct fat_dir_entry_struct file_entry;
00794     if(!find_file_in_dir(name,&file_entry))
00795     {
00796         flag |= FILE_OPEN_ERROR;
00797         return "--";
00798     }
00799 
00800     char attrib[2];
00801     sprintf(attrib, "%c%c", file_entry.attributes & FAT_ATTRIB_DIR ? 'd' : '-', file_entry.attributes & FAT_ATTRIB_READONLY ? 'r' : 'w');
00802 
00803     return attrib;
00804 }

Here is the call graph for this function:

char * WaspSD::cat ( const char *  filename,
int32_t  offset,
uint16_t  scope 
)

It dumps into the buffer the amount of bytes in scope after offset coming from filename.

There is a limitation in size, due to DOS_BUFFER_SIZE. If the data read was bigger than that, the function will include the characters ">>" at the end and activate the TRUNCATED_DATA value in the DOS.flag. It is recommened to check this value to assure data integrity.

Parameters:
const char* filename : the file to get the data from
int32_t offset : amount of bytes to jump before start dumping the data to the buffer
uint16_t scope : amount of bytes for dumping to the buffer
Returns:
'buffer' variable where the data has been dumped
See also:
catBin (const char* filename, int32_t offset, uint16_t scope), catln (const char* filename, uint32_t offset, uint16_t scope)

Definition at line 832 of file WaspSD.cpp.

References buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, DOS_BUFFER_SIZE, fat_close_file(), fat_read_file(), fat_seek_file(), FAT_SEEK_SET, fd, FILE_OPEN_ERROR, flag, isSD(), openFile(), and TRUNCATED_DATA.

Referenced by WaspGPRS::sendDataFTP().

00833 {
00834     struct fat_file_struct* _fd;
00835     _fd=fd;
00836   // check if the card is there or not
00837   if (!isSD())
00838   {
00839     flag = CARD_NOT_PRESENT;
00840     flag |= FILE_OPEN_ERROR;
00841     sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00842     return buffer;
00843   }
00844     
00845   // if scope is zero, then we should make it 
00846   // ridiculously big, so that it goes on reading forever
00847   if (scope <= 0) scope = 1000;
00848   
00849   flag &= ~(TRUNCATED_DATA | FILE_OPEN_ERROR);
00850 
00851   // search file in current directory and open it 
00852   // assign the file pointer to the general file pointer "fp"
00853   // exit if error and modify the general flag with FILE_OPEN_ERROR
00854   _fd = openFile(filename);
00855   if(!_fd)
00856   {
00857     sprintf(buffer, "error opening %s", filename);
00858     flag |= FILE_OPEN_ERROR;
00859     return buffer;
00860   }
00861 
00862   // iterate through the file
00863   byte maxBuffer = 10;  // size of the buffer to use when reading
00864   uint8_t bufferSD[maxBuffer];
00865   uint32_t cont = 0;
00866   
00867   // first jump over the offset
00868   if(!fat_seek_file(_fd, &offset, FAT_SEEK_SET))
00869   {
00870           sprintf(buffer, "error seeking on: %s\n", filename);
00871           fat_close_file(_fd);
00872           return buffer;
00873   }
00874   
00875   uint8_t readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
00876 
00877   // second, read the data and store it in the DOS.buffer
00878   // as long as there is room in it
00879   while(readRet > 0 && scope > 0 && cont < DOS_BUFFER_SIZE)
00880   {
00881     for(uint8_t i = 0; i < readRet; ++i)
00882     {
00883       buffer[cont++] = bufferSD[i];
00884       scope--;
00885       if( scope <=0 ) break;
00886       if( cont>=DOS_BUFFER_SIZE ) break;
00887     }
00888     if( readRet<maxBuffer ) break;
00889     readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
00890   }
00891   if (cont < DOS_BUFFER_SIZE - 1) {
00892     buffer[cont++] = '\0';
00893   }
00894   else
00895   {
00896     // in case we filled up the whole buffer, we add a
00897     // visual end of buffer indicator and leave the loop
00898     buffer[DOS_BUFFER_SIZE - 4] = '>';
00899     buffer[DOS_BUFFER_SIZE - 3] = '>';
00900     buffer[DOS_BUFFER_SIZE - 2] = '\n';
00901     buffer[DOS_BUFFER_SIZE - 1] = '\0';
00902     flag |= TRUNCATED_DATA; 
00903   }
00904 
00905   fat_close_file(_fd);
00906 
00907   return buffer;
00908 
00909 }

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t * WaspSD::catBin ( const char *  filename,
int32_t  offset,
uint16_t  scope 
)

It dumps into the bufferBin the amount of bytes in scope after offset coming from filename.

Parameters:
const char* filename : the file to get the data from
int32_t offset : amount of bytes to jump before start dumping the data to the buffer
uint16_t scope : amount of bytes for dumping to the buffer
Returns:
'bufferBin' variable where the data has been dumped
See also:
cat (const char* filename, int32_t offset, uint16_t scope), catln (const char* filename, uint32_t offset, uint16_t scope)

Definition at line 937 of file WaspSD.cpp.

References BIN_BUFFER_SIZE, buffer, bufferBin, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, fat_close_file(), fat_read_file(), fat_seek_file(), FAT_SEEK_SET, fd, FILE_OPEN_ERROR, flag, isSD(), openFile(), and TRUNCATED_DATA.

Referenced by WaspGPS::loadEphems().

00938 {
00939         struct fat_file_struct* _fd;
00940         _fd=fd;
00941   // check if the card is there or not
00942         if (!isSD())
00943         {
00944                 flag = CARD_NOT_PRESENT;
00945                 flag |= FILE_OPEN_ERROR;
00946                 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
00947                 return bufferBin;
00948         }
00949     
00950   // if scope is zero, then we should make it 
00951   // ridiculously big, so that it goes on reading forever
00952         if (scope <= 0) scope = 1000;
00953   
00954         flag &= ~(TRUNCATED_DATA | FILE_OPEN_ERROR);
00955 
00956   // search file in current directory and open it 
00957   // assign the file pointer to the general file pointer "fp"
00958   // exit if error and modify the general flag with FILE_OPEN_ERROR
00959         _fd = openFile(filename);
00960         if(!_fd)
00961         {
00962                 sprintf(buffer, "error opening %s", filename);
00963                 flag |= FILE_OPEN_ERROR;
00964                 return bufferBin;
00965         }
00966 
00967   // iterate through the file
00968         byte maxBuffer = 1;  // size of the buffer to use when reading
00969         uint8_t bufferSD[maxBuffer];
00970         uint32_t cont = 0;
00971   
00972   // first jump over the offset
00973         if(!fat_seek_file(_fd, &offset, FAT_SEEK_SET))
00974         {
00975                 sprintf(buffer, "error seeking on: %s\n", filename);
00976                 fat_close_file(_fd);
00977                 return bufferBin;
00978         }
00979   
00980         uint8_t readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
00981 
00982   // second, read the data and store it in the DOS.buffer
00983   // as long as there is room in it
00984         while(readRet > 0 && scope > 0 && cont < BIN_BUFFER_SIZE)
00985         {
00986                 for(uint8_t i = 0; i < maxBuffer; ++i)
00987                 {
00988                         bufferBin[cont++] = bufferSD[i];
00989                         scope--;
00990                         readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
00991                 }
00992         }
00993 
00994         fat_close_file(_fd);
00995 
00996         return bufferBin;
00997 
00998 }

Here is the call graph for this function:

Here is the caller graph for this function:

char * WaspSD::catln ( const char *  filename,
uint32_t  offset,
uint16_t  scope 
)

It dumps into the buffer the amount of lines in scope after offset lines coming from filename.

There is a limitation in size, due to DOS_BUFFER_SIZE. If the data read was bigger than that, the function will include the characters ">>" at the end and activate the TRUNCATED_DATA value in the DOS.flag. It is recommened to check this value to assure data integrity.

Parameters:
const char* filename : the file to get the data from
uint32_t offset : amount of lines to jump before start dumping the data to the buffer
uint16_t scope : amount of lines for dumping to the buffer
Returns:
'buffer' variable where the data has been dumped
See also:
cat (const char* filename, int32_t offset, uint16_t scope), catBin (const char* filename, int32_t offset, uint16_t scope)

Definition at line 1027 of file WaspSD.cpp.

References buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, DOS_BUFFER_SIZE, fat_close_file(), fat_read_file(), fd, FILE_OPEN_ERROR, flag, isSD(), openFile(), and TRUNCATED_DATA.

01028 {
01029     struct fat_file_struct* _fd;
01030     _fd=fd;
01031   // check if the card is there or not
01032   if (!isSD())
01033   {
01034     flag = CARD_NOT_PRESENT;
01035     flag |= FILE_OPEN_ERROR;
01036     sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01037     return buffer;
01038   }
01039     
01040   // if scope is zero, then we should make it 
01041   // ridiculously big, so that it goes on reading forever
01042   if (scope <= 0) scope = 1000;
01043   
01044   flag &= ~(TRUNCATED_DATA | FILE_OPEN_ERROR);
01045 
01046   // search file in current directory and open it 
01047   // assign the file pointer to the general file pointer "fp"
01048   // exit if error and modify the general flag with FILE_OPEN_ERROR
01049   _fd = openFile(filename);
01050   if(!_fd)
01051   {
01052     sprintf(buffer, "error opening %s", filename);
01053     flag |= FILE_OPEN_ERROR;
01054     return buffer;
01055   }
01056 
01057   byte maxBuffer = 1;  // size of the buffer to use when reading
01058   uint8_t bufferSD[maxBuffer];
01059   uint32_t cont = 0;
01060   
01061   // jump over offset lines
01062   uint8_t readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01063   if (offset > 0) 
01064   {
01065     while( readRet > 0 && offset > 0)   
01066     {
01067       for(uint8_t i = 0; i < maxBuffer; ++i)
01068       {
01069         if (bufferSD[i] == '\n')
01070           offset--;
01071       }
01072       readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01073     }
01074 
01075     // do not forget the first buffer  
01076     for(uint8_t i = 0; i < maxBuffer; ++i)
01077     {
01078       buffer[cont++] = bufferSD[i];
01079       if (bufferSD[i] == '\n')
01080         scope--;
01081     }
01082 
01083     readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01084   }
01085   
01086   // add to buffer scope lines
01087   while(readRet > 0 && scope > 0 && cont < DOS_BUFFER_SIZE)
01088   {
01089     for(uint8_t i = 0; i < maxBuffer; ++i)
01090     {
01091       buffer[cont++] = bufferSD[i];
01092       if (bufferSD[i] == '\n')
01093         scope--;
01094     }
01095     readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01096   }
01097 
01098   // are we at the end of the buffer yet?
01099   if (cont < DOS_BUFFER_SIZE - 1) {
01100     buffer[cont++] = '\0';
01101   }
01102   else
01103   {
01104     // in case we filled up the whole buffer, we add a
01105     // visual end of buffer indicator and leave the loop
01106     buffer[DOS_BUFFER_SIZE - 4] = '>';
01107     buffer[DOS_BUFFER_SIZE - 3] = '>';
01108     buffer[DOS_BUFFER_SIZE - 2] = '\n';
01109     buffer[DOS_BUFFER_SIZE - 1] = '\0';
01110     flag |= TRUNCATED_DATA; 
01111   }
01112 
01113   fat_close_file(_fd);
01114 
01115   return buffer;
01116 
01117 }

Here is the call graph for this function:

int32_t WaspSD::indexOf ( const char *  filename,
const char *  pattern,
uint32_t  offset 
)

It searches for first occurrence of a string in a file.

Parameters:
const char* filename : the file where looking for the pattern
const char* pattern : pattern to find
uint32_t offset : amount of bytes to jump before start looking for the pattern
Returns:
the amount of bytes to the pattern from the offset

Definition at line 1145 of file WaspSD.cpp.

References buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, fat_close_file(), fat_read_file(), fd, FILE_OPEN_ERROR, flag, getFileSize(), isSD(), openFile(), WaspUtils::sizeOf(), WaspUtils::strCmp(), and Utils.

01146 {
01147     struct fat_file_struct* _fd;
01148     _fd=fd;
01149   // check if the card is there or not
01150   if (!isSD())
01151   {
01152     flag = CARD_NOT_PRESENT;
01153     flag |= FILE_OPEN_ERROR;
01154     sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01155     return -1;
01156   }
01157 
01158   flag &= ~(FILE_OPEN_ERROR);
01159 
01160   // search file in current directory and open it 
01161   // assign the file pointer to the general file pointer "fp"
01162   // exit if error and modify the general flag with FILE_OPEN_ERROR
01163   _fd = openFile(filename);
01164   if(!_fd)
01165   {
01166     sprintf(buffer, "error opening %s", filename);
01167     flag |= FILE_OPEN_ERROR;
01168     return -1;
01169   }
01170 
01171   uint32_t limitSize = getFileSize(filename);
01172 
01173   uint8_t limitPattern = Utils.sizeOf(pattern);
01174   uint8_t bufferSD[1];
01175   uint8_t cmpPattern[limitPattern];
01176   uint8_t exit = 0, contPattern = 0;
01177   uint32_t cont = 0;
01178 
01179   // jump over the offset
01180   uint8_t readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01181   if (offset > 0) while(readRet > 0 && offset > 0) 
01182   { 
01183     readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01184     offset--; 
01185     //cont++;  // we don't wanna count until we are done with the offset
01186   }
01187 
01188   // fill in the first buffer
01189   for(int j=0; j < limitPattern - 1; j++)
01190     if (readRet > 0)
01191     {
01192       cmpPattern[j] = bufferSD[0];
01193       readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01194     }
01195     else
01196       exit = 1;  // exit if the filesize is smaller than the pattern
01197 
01198 //to debug the code in the library it is possible to use the buffer together with sprintf, e.g.
01199 //sprintf(buffer,"%s - %c%c%c%c - %lu - %u -%lu", pattern, cmpPattern[0], cmpPattern[1], cmpPattern[2], bufferSD[0], cont, limitPattern, limitSize);
01200 
01201   // run inside the buffer
01202   while(readRet > 0 && cont < limitSize && !exit)
01203   {
01204     // take the last position in the buffer
01205     cmpPattern[limitPattern-1] = bufferSD[0];
01206     
01207     // compare the strings, exit if found
01208 //    if(strcmp((const char*) pattern, (const char*) cmpPattern) == 0)
01209     if(Utils.strCmp((const char*) pattern, (const char*) cmpPattern, limitPattern) == 0)
01210       exit = 1;
01211       
01212     // if not exit, increment counter
01213     if (!exit) 
01214       cont++;
01215       
01216     // shift cmpPattern to the left one position
01217     for(int j = 0; j < limitPattern - 1; j++) cmpPattern[j] = cmpPattern[j+1];
01218     
01219     // read the next buffer
01220     readRet = fat_read_file(_fd, bufferSD, sizeof(bufferSD));
01221   }
01222 
01223   fat_close_file(_fd);
01224 
01225   // in case we checked the whole buffer, we return error
01226   if (cont >= limitSize - 1) 
01227     return -1; 
01228 
01229   // otherwise we return the pattern's location
01230   return cont;
01231 
01232 }

Here is the call graph for this function:

uint8_t WaspSD::create ( const char *  filename  ) 

It creates a file.

Parameters:
const char* filename : the file to create
Returns:
'1' on success, '0' otherwise

Definition at line 1367 of file WaspSD.cpp.

References buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, dd, fat_create_file(), FILE_CREATION_ERROR, flag, and isSD().

Referenced by WaspGPRS::readDataFTP(), and WaspGPS::saveEphems().

01368 {
01369     struct fat_dir_struct* _dd;
01370     _dd=dd;
01371   // check if the card is there or not
01372   if (!isSD())
01373   {
01374     flag = CARD_NOT_PRESENT;
01375     flag |= FILE_CREATION_ERROR;
01376     sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01377     return 0;
01378   }
01379 
01380   flag &= ~(FILE_CREATION_ERROR);
01381   struct fat_dir_entry_struct file_entry;
01382   if(!fat_create_file(_dd, filename, &file_entry))
01383   {
01384       flag |= FILE_CREATION_ERROR;
01385       return 0;
01386   }
01387   return 1;
01388 }

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t WaspSD::writeSD ( const char *  filename,
const char *  str,
int32_t  offset 
)

It writes strings to a file.

Parameters:
const char* filename : the file to write to
const char* str : the string to write into the file
int32_t offset : amount of bytes to jump before start writing the string
Returns:
'1' on success, '0' otherwise
See also:
writeSD(const char* filename, uint8_t* str, int32_t offset), append(const char* filename, const char* str), append(const char* filename, uint8_t* str), appendln(const char* filename, const char* str), appendln(const char* filename, uint8_t* str)

Definition at line 1535 of file WaspSD.cpp.

References buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, fat_close_file(), fat_seek_file(), FAT_SEEK_SET, fat_write_file(), fd, FILE_WRITING_ERROR, flag, isSD(), openFile(), WaspUtils::sizeOf(), and Utils.

Referenced by append(), and WaspGPS::saveEphems().

01536 {
01537     struct fat_file_struct* _fd;
01538     _fd=fd;
01539   // check if the card is there or not
01540     if (!isSD())
01541     {
01542         flag = CARD_NOT_PRESENT;
01543         flag |= FILE_WRITING_ERROR;
01544         sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01545         return 0;
01546     }
01547 
01548     flag &= ~(FILE_WRITING_ERROR);
01549     uint8_t exit = 0;
01550   // search file in current directory and open it
01551     _fd = openFile(filename);
01552     if(!_fd)
01553     {
01554         sprintf(buffer, "error opening: %s\n", filename);
01555         exit = 1;
01556     }
01557 
01558     if(!exit) if(!fat_seek_file(_fd, &offset, FAT_SEEK_SET))
01559     {
01560         sprintf(buffer, "error seeking on: %s\n", filename);
01561         fat_close_file(_fd);
01562         exit = 1;
01563     }
01564 
01565     uint16_t data_len = Utils.sizeOf(str);
01566 
01567   // write text to file
01568     if(!exit) if(fat_write_file(_fd, (uint8_t*) str, data_len) != data_len)
01569     {
01570         sprintf(buffer, "error writing to: %s\n", filename);
01571         fat_close_file(_fd);
01572         exit = 1;
01573     }
01574 
01575     if(!exit) fat_close_file(_fd);
01576     else flag |= FILE_WRITING_ERROR;
01577     return !exit;
01578 }

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t WaspSD::writeSD ( const char *  filename,
uint8_t *  str,
int32_t  offset 
)

It writes integer array to a file.

Parameters:
const char* filename : the file to write to
uint8_t* str : the integer array to write into the file
int32_t offset : amount of bytes to jump before start writing the string
Returns:
'1' on success, '0' otherwise
See also:
writeSD(const char* filename, const char* str, int32_t offset), append(const char* filename, const char* str), append(const char* filename, uint8_t* str), appendln(const char* filename, const char* str), appendln(const char* filename, uint8_t* str)

Definition at line 1588 of file WaspSD.cpp.

References buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, fat_close_file(), fat_seek_file(), FAT_SEEK_SET, fat_write_file(), fd, FILE_WRITING_ERROR, flag, isSD(), and openFile().

01589 {
01590         struct fat_file_struct* _fd;
01591         _fd=fd;
01592   // check if the card is there or not
01593         if (!isSD())
01594         {
01595                 flag = CARD_NOT_PRESENT;
01596                 flag |= FILE_WRITING_ERROR;
01597                 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01598                 return 0;
01599         }
01600 
01601         flag &= ~(FILE_WRITING_ERROR);
01602         uint8_t exit = 0;
01603   // search file in current directory and open it
01604         _fd = openFile(filename);
01605         if(!_fd)
01606         {
01607                 sprintf(buffer, "error opening: %s\n", filename);
01608                 exit = 1;
01609         }
01610 
01611         if(!exit) if(!fat_seek_file(_fd, &offset, FAT_SEEK_SET))
01612         {
01613                 sprintf(buffer, "error seeking on: %s\n", filename);
01614                 fat_close_file(_fd);
01615                 exit = 1;
01616         }
01617 
01618         uint16_t data_len = 0;
01619         while( (str[data_len]!=0xAA) || (str[data_len+1]!=0xAA) ) data_len++;
01620 
01621   // write text to file
01622         if(!exit) if(fat_write_file(_fd, str, data_len) != data_len)
01623         {
01624                 sprintf(buffer, "error writing to: %s\n", filename);
01625                 fat_close_file(_fd);
01626                 exit = 1;
01627         }
01628 
01629         if(!exit) fat_close_file(_fd);
01630         else flag |= FILE_WRITING_ERROR;
01631         return !exit;
01632 }

Here is the call graph for this function:

uint8_t WaspSD::writeSD ( const char *  filename,
const char *  str,
int32_t  offset,
int16_t  length 
)

It writes strings to a file of a specific length.

Parameters:
const char* filename : the file to write to
const char* str : the string to write into the file
int32_t offset : amount of bytes to jump before start writing the string
int16_t length : amount of bytes to write to the file
Returns:
'1' on success, '0' otherwise
See also:
writeSD(const char* filename, uint8_t* str, int32_t offset), append(const char* filename, const char* str), append(const char* filename, uint8_t* str), appendln(const char* filename, const char* str), appendln(const char* filename, uint8_t* str)

Definition at line 1643 of file WaspSD.cpp.

References buffer, CARD_NOT_PRESENT, CARD_NOT_PRESENT_em, fat_close_file(), fat_seek_file(), FAT_SEEK_SET, fat_write_file(), fd, FILE_WRITING_ERROR, flag, isSD(), and openFile().

01644 {
01645         struct fat_file_struct* _fd;
01646         _fd=fd;
01647   // check if the card is there or not
01648         if (!isSD())
01649         {
01650                 flag = CARD_NOT_PRESENT;
01651                 flag |= FILE_WRITING_ERROR;
01652                 sprintf(buffer,"%s", CARD_NOT_PRESENT_em);
01653                 return 0;
01654         }
01655 
01656         flag &= ~(FILE_WRITING_ERROR);
01657         uint8_t exit = 0;
01658   // search file in current directory and open it
01659         _fd = openFile(filename);
01660         if(!_fd)
01661         {
01662                 sprintf(buffer, "error opening: %s\n", filename);
01663                 exit = 1;
01664         }
01665 
01666         if(!exit) if(!fat_seek_file(_fd, &offset, FAT_SEEK_SET))
01667         {
01668                 sprintf(buffer, "error seeking on: %s\n", filename);
01669                 fat_close_file(_fd);
01670                 exit = 1;
01671         }
01672 
01673   // write text to file
01674         if(!exit) if(fat_write_file(_fd, (uint8_t*) str, length) != length)
01675         {
01676                 sprintf(buffer, "error writing to: %s\n", filename);
01677                 fat_close_file(_fd);
01678                 exit = 1;
01679         }
01680 
01681         if(!exit) fat_close_file(_fd);
01682         else flag |= FILE_WRITING_ERROR;
01683         return !exit;
01684 }

Here is the call graph for this function:

uint8_t WaspSD::append ( const char *  filename,
const char *  str 
)

It writes strings at the end of files.

Parameters:
const char* filename : the file to write to
const char* str : the string to write into the file
Returns:
'1' on success, '0' otherwise
See also:
writeSD(const char* filename, const char* str, int32_t offset), writeSD(const char* filename, uint8_t* str, int32_t offset), append(const char* filename, uint8_t* str), appendln(const char* filename, const char* str), appendln(const char* filename, uint8_t* str)

Definition at line 1455 of file WaspSD.cpp.

References getFileSize(), and writeSD().

Referenced by appendln(), and WaspGPRS::readDataFTP().

01456 {
01457     return writeSD(filename, str, getFileSize(filename));
01458 }

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t WaspSD::append ( const char *  filename,
const char *  str,
uint16_t  length 
)

It writes strings at the end of files of a specific length.

Parameters:
const char* filename : the file to write to
const char* str : the string to write into the file
uint16_t length : the length to write
Returns:
'1' on success, '0' otherwise
See also:
writeSD(const char* filename, const char* str, int32_t offset), writeSD(const char* filename, uint8_t* str, int32_t offset), append(const char* filename, uint8_t* str), appendln(const char* filename, const char* str), appendln(const char* filename, uint8_t* str)

Definition at line 1468 of file WaspSD.cpp.

References getFileSize(), and writeSD().

01469 {
01470         return writeSD(filename, str, getFileSize(filename), length);
01471 }

Here is the call graph for this function:

uint8_t WaspSD::append ( const char *  filename,
uint8_t *  str 
)

It writes integer arrays at the end of files.

Parameters:
const char* filename : the file to write to
uint8_t* str : the integer array to write into the file
Returns:
'1' on success, '0' otherwise
See also:
writeSD(const char* filename, const char* str, int32_t offset), writeSD(const char* filename, uint8_t* str, int32_t offset), append(const char* filename, const char* str), appendln(const char* filename, const char* str), appendln(const char* filename, uint8_t* str)

Definition at line 1482 of file WaspSD.cpp.

References getFileSize(), and writeSD().

01483 {
01484         return writeSD(filename, str, getFileSize(filename));
01485 }

Here is the call graph for this function:

uint8_t WaspSD::appendln ( const char *  filename,
const char *  str 
)

It writes strings at the end of files adding an EOL.

Parameters:
const char* filename : the file to write to
const char* str : the string to write into the file
Returns:
'1' on success, '0' otherwise
See also:
writeSD(const char* filename, const char* str, int32_t offset), writeSD(const char* filename, uint8_t* str, int32_t offset), append(const char* filename, const char* str), append(const char* filename, uint8_t* str), appendln(const char* filename, uint8_t* str)

Definition at line 1496 of file WaspSD.cpp.

References append().

01497 {
01498     uint8_t exit = 0;
01499     exit = append(filename, str);
01500 #ifndef FILESYSTEM_LINUX
01501     if (exit) exit &= append(filename, "\r");
01502 #endif
01503     if (exit) exit &= append(filename, "\n");
01504     return exit;
01505 }

Here is the call graph for this function:

uint8_t WaspSD::appendln ( const char *  filename,
uint8_t *  str 
)

It writes integer arrays at the end of files adding an EOL.

Parameters:
const char* filename : the file to write to
uint8_t* str : the integer array to write into the file
Returns:
'1' on success, '0' otherwise
See also:
writeSD(const char* filename, const char* str, int32_t offset), writeSD(const char* filename, uint8_t* str, int32_t offset), append(const char* filename, const char* str), append(const char* filename, uint8_t* str), appendln(const char* filename, const char* str)

Definition at line 1516 of file WaspSD.cpp.

References append().

01517 {
01518         uint8_t exit = 0;
01519         exit = append(filename, str);
01520 #ifndef FILESYSTEM_LINUX
01521         if (exit) exit &= append(filename, "\r");
01522 #endif
01523         if (exit) exit &= append(filename, "\n");
01524         return exit;
01525 }

Here is the call graph for this function:

const char* WaspSD::getLibVersion ( void   )  [inline]

It gets the library version.

Parameters:
void 
Returns:
the library version

Definition at line 610 of file WaspSD.h.

References VERSION.

00610 {return VERSION;};


Field Documentation

char WaspSD::buffer[DOS_BUFFER_SIZE]

Variable : buffer containing the information coming from the card used to avoid calls to UART functions inside the library. Beware, there could be data longer than the buffer size.

Definition at line 183 of file WaspSD.h.

Referenced by cat(), catBin(), catln(), cd(), create(), del(), find_file_in_dir(), getAttributes(), getDiskFree(), getDiskSize(), getFileSize(), indexOf(), ls(), mkdir(), numFiles(), numln(), openFile(), print_disk_info(), WaspGPRS::sendDataFTP(), and writeSD().

uint8_t WaspSD::bufferBin[BIN_BUFFER_SIZE]

Variable : buffer containing the binary information coming from the card used to avoid calls to UART functions inside the library. Beware, there could be data longer than the buffer size.

Definition at line 188 of file WaspSD.h.

Referenced by catBin(), and WaspGPS::loadEphems().

uint16_t WaspSD::flag

Variable : flag storing the state of the SD card during initialization and operation.

Definition at line 193 of file WaspSD.h.

Referenced by cat(), catBin(), catln(), cd(), cleanFlags(), create(), del(), find_file_in_dir(), getAttributes(), getDiskFree(), getDiskSize(), getFileSize(), indexOf(), init(), WaspGPS::loadEphems(), ls(), mkdir(), numFiles(), numln(), openFile(), print_disk_info(), WaspGPS::saveEphems(), and writeSD().

Variable : It stores the SD power mode. Possible values are SD_ON or SD_OFF.

Definition at line 198 of file WaspSD.h.

Referenced by setMode().

struct fat_fs_struct* WaspSD::fs [read]

Structure pointer : filesystem pointer.

Definition at line 204 of file WaspSD.h.

Referenced by cd(), close(), delDir(), delFile(), getDiskFree(), getDiskSize(), init(), ls(), openFile(), and print_disk_info().

Structure pointer : partition pointer.

Definition at line 209 of file WaspSD.h.

Referenced by close(), and init().

struct fat_dir_struct* WaspSD::dd [read]

Structure pointer : directory structure pointer.

Definition at line 214 of file WaspSD.h.

Referenced by cd(), close(), create(), delDir(), find_file_in_dir(), init(), ls(), mkdir(), numFiles(), and openFile().

struct fat_file_struct* WaspSD::fd [read]

Structure pointer : generic file pointer.

Definition at line 219 of file WaspSD.h.

Referenced by cat(), catBin(), catln(), indexOf(), numln(), openFile(), and writeSD().

Variable : amount of free bytes in the drive.

Definition at line 224 of file WaspSD.h.

Referenced by getDiskFree(), and print_disk_info().

Variable : total byte size of the drive.

Definition at line 229 of file WaspSD.h.

Referenced by getDiskSize(), and print_disk_info().


The documentation for this class was generated from the following files:

Generated on Tue Jul 20 09:31:03 2010 for WaspmoteAPI by  doxygen 1.5.6