Skip to content

Commit

Permalink
Macro RETURN(): tracing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
JulStrat committed Sep 6, 2021
1 parent e551f44 commit 2de36e4
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 18 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ Compatible storage libraries:
- [SdFat - Adafruit fork](https://github.com/adafruit/SdFat)
- [Adafruit SPIFlash](https://github.com/adafruit/Adafruit_SPIFlash)

Simple tracing for CDB format/integrity and run time file operation errors.
```C++
#define TRACE_CDB
#include "uCDB.hpp"
```

## API

```C++
Expand Down
2 changes: 2 additions & 0 deletions examples/airports/airports.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
#include <SPI.h>
#include <SD.h>

#define TRACE_CDB
#include "uCDB.hpp"

uCDB<SDClass, File> ucdb(SD);
Expand Down
2 changes: 2 additions & 0 deletions examples/benchmark/benchmark.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#else
#include "SdFat.h"
#endif

#define TRACE_CDB
#include "uCDB.hpp"

#ifdef USE_SERIALFLASH_LIB
Expand Down
2 changes: 2 additions & 0 deletions examples/satcat/satcat.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include <SPI.h>
#include <SD.h>

#define TRACE_CDB
#include "uCDB.hpp"

uCDB<SDClass, File> ucdb(SD);
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=uCDB
version=0.5.0
version=0.5.1
author=Ioulianos Kakoulidis
maintainer=Ioulianos Kakoulidis <[email protected]>
sentence=API for querying Constant DataBase file store.
Expand Down
50 changes: 33 additions & 17 deletions src/uCDB.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@
@copyright Public Domain
*/

#ifdef TRACE_CDB
#ifndef TracePrinter
#define TracePrinter Serial
#endif
#define RETURN(statement, message) \
TracePrinter.print("[T]: "); \
TracePrinter.print(__FUNCTION__); \
TracePrinter.print(": "); \
TracePrinter.print(__LINE__); \
TracePrinter.print(", "); \
TracePrinter.println(#message); \
return (statement);
#else
#define RETURN(statement, message) return (statement);
#endif

/// uCDB result codes and states
enum cdbResult {
CDB_OK = 0,
Expand Down Expand Up @@ -69,11 +85,11 @@ class uCDB
Close CDB
*/
cdbResult close();

/**
uCDB destructor
*/
~uCDB();
~uCDB();

private:
TFileSystem& fs_;
Expand Down Expand Up @@ -142,7 +158,7 @@ cdbResult uCDB<TFileSystem, TFile>::open(const char *fileName, unsigned long (*u
// Close previously opened CDB file
// State - CDB_CLOSED
close();

if (!fs_.exists(fileName)) {
return CDB_NOT_FOUND;
}
Expand All @@ -156,15 +172,15 @@ cdbResult uCDB<TFileSystem, TFile>::open(const char *fileName, unsigned long (*u

// CDB file size must be at least HEADER_SIZE bytes
if (cdb.size() < CDB_HEADER_SIZE) {
return (state = CDB_ERROR);
RETURN(state = CDB_ERROR, CDB_ERROR);
}

dend = cdb.size();
snum = 0;

for (unsigned long pos = 0; pos < CDB_HEADER_SIZE; pos += CDB_DESCRIPTOR_SIZE) {
if (!readDescriptor(buff, pos)) {
return (state = CDB_ERROR); // File read error is critical here.
RETURN(state = CDB_ERROR, CDB_ERROR); // File read error is critical here.
}

htPos = unpack(buff);
Expand All @@ -174,10 +190,10 @@ cdbResult uCDB<TFileSystem, TFile>::open(const char *fileName, unsigned long (*u
continue; // Empty hash table
}
if ((htPos < CDB_HEADER_SIZE) || (htPos > cdb.size())) {
return (state = CDB_ERROR); // Critical CDB format or data integrity error
RETURN(state = CDB_ERROR, CDB_ERROR); // Critical CDB format or data integrity error
}
if (((cdb.size() - htPos) >> 3) < htSlotsNum) {
return (state = CDB_ERROR); // Critical CDB format or data integrity error
RETURN(state = CDB_ERROR, CDB_ERROR); // Critical CDB format or data integrity error
}

// Adjust data end position and total slots number
Expand All @@ -187,12 +203,12 @@ cdbResult uCDB<TFileSystem, TFile>::open(const char *fileName, unsigned long (*u
snum += htSlotsNum;

if (((cdb.size() - dend) >> 3) < snum) {
return (state = CDB_ERROR); // Critical CDB format or data integrity error
RETURN(state = CDB_ERROR, CDB_ERROR); // Critical CDB format or data integrity error
}
}
// Check total
if ((cdb.size() - dend) != 8 * snum){
return (state = CDB_ERROR); // Critical CDB format or data integrity error
RETURN(state = CDB_ERROR, CDB_ERROR); // Critical CDB format or data integrity error
}

dataEndPos = dend;
Expand Down Expand Up @@ -220,7 +236,7 @@ cdbResult uCDB<TFileSystem, TFile>::findKey(const void *key, unsigned long keyLe
keyLen_ = keyLen;

if (!readDescriptor(buff, (keyHash & 255) << 3)) {
return (state = FILE_ERROR);
RETURN(state = FILE_ERROR, FILE_ERROR);
}

hashTabStartPos = unpack(buff);
Expand Down Expand Up @@ -255,7 +271,7 @@ cdbResult uCDB<TFileSystem, TFile>::findNextValue() {
}

if (!rd) {
return (state = FILE_ERROR);
RETURN(state = FILE_ERROR, FILE_ERROR);
}

slotHash = unpack(buff);
Expand All @@ -268,12 +284,12 @@ cdbResult uCDB<TFileSystem, TFile>::findNextValue() {

// Check data position
if ((dataPos < CDB_HEADER_SIZE) || (dataPos > (dataEndPos - CDB_DESCRIPTOR_SIZE))) {
return (state = CDB_ERROR); // Critical CDB format or data integrity error
RETURN(state = CDB_ERROR, CDB_ERROR); // Critical CDB format or data integrity error
}

if (slotHash == keyHash) {
if (!readDescriptor(buff, dataPos)) {
return (state = FILE_ERROR);
RETURN(state = FILE_ERROR, FILE_ERROR);
}

dataKeyLen = unpack(buff);
Expand All @@ -282,11 +298,11 @@ cdbResult uCDB<TFileSystem, TFile>::findNextValue() {
//> key, value length check
unsigned long t = dataPos + CDB_DESCRIPTOR_SIZE;
if ((dataEndPos - t) < dataKeyLen) {
return (state = CDB_ERROR); // Critical CDB format or data integrity error
RETURN(state = CDB_ERROR, CDB_ERROR); // Critical CDB format or data integrity error
}
t += dataKeyLen;
if ((dataEndPos - t) < dataValueLen) {
return (state = CDB_ERROR); // Critical CDB format or data integrity error
RETURN(state = CDB_ERROR, CDB_ERROR); // Critical CDB format or data integrity error
}
//< key, value length check

Expand All @@ -296,7 +312,7 @@ cdbResult uCDB<TFileSystem, TFile>::findNextValue() {
valueBytesAvail = dataValueLen;
return (state = KEY_FOUND);
case FILE_ERROR:
return (state = FILE_ERROR);
RETURN(state = FILE_ERROR, FILE_ERROR);
default:
;
}
Expand Down Expand Up @@ -366,7 +382,7 @@ cdbResult uCDB<TFileSystem, TFile>::close() {

template <class TFileSystem, class TFile>
uCDB<TFileSystem, TFile>::~uCDB() {
close();
close();
}

// Private functions
Expand Down

0 comments on commit 2de36e4

Please sign in to comment.