Trond Enger / d7a_1x

Fork of d7a_1x by WizziLab

include/d7a_fs.h

Committer:
Jeej
Date:
2016-05-25
Revision:
30:d775c1409849
Parent:
29:8e7c5c1e9aab
Child:
33:f9a542d3efaa

File content as of revision 30:d775c1409849:

#ifndef _D7A_FS_H_
#define _D7A_FS_H_

#include "mbed.h"
#include "rtos.h"
#include "d7a_common.h"
#include "d7a_com.h"

#define TO_FS       (5000)
#define FS_MAX_CONCURRENCY  8 // Maximum number of concurrent access



// PROP Byte
#define FS_BFO_STORAGE_CLASS    0
#define FS_BFO_ACT_COND         4
#define FS_BFO_ACT_EN           7
#define FS_BFS_STORAGE_CLASS    2
#define FS_BFS_ACT_COND         3
#define FS_BFS_ACT_EN           1

//======================================================================
// Attributes and macros
//======================================================================
// "Piped" File: rd/wr are not stored but just sent on IF
#define FS_TRANSIENT         (TRANSIENT  << FS_BFO_STORAGE_CLASS)
// "RAM" File: rd/wr to a volatile buffer.
#define FS_VOLATILE          (VOLATILE   << FS_BFO_STORAGE_CLASS)
// "Mirrored" File: loaded from NVM, cached/used in RAM. Flushable to NVM.
#define FS_RESTORABLE        (RESTORABLE << FS_BFO_STORAGE_CLASS)
// "Normal" File: rd/wr from/to NVM
#define FS_PERMANENT         (PERMANENT  << FS_BFO_STORAGE_CLASS)
#define FS_ACT_COND(c)       ((c & 0x7) << FS_BFO_ACT_COND)
#define FS_ACT_EN            (1  << FS_BFO_ACT_EN)

#define FS_RUNNABLE          (1  << FS_BFO_RUNNABLE)
#define FS_ENCRYPTED         (1  << FS_BFO_ENCRYPTED)

// XXX Old ALP Hack
#define NACK                 FS_RUNNABLE
#define NOTIF                FS_ENCRYPTED

// D7AactP Enabled File properties
#if 0 // Condition is not used (nor usable)
    #define FS_TRANSIENT_NOTIF(c) (FS_TRANSIENT  | FS_ACT_EN | FS_ACT_COND(c))
    #define FS_VOLATILE_NOTIF(c)  (FS_VOLATILE   | FS_ACT_EN | FS_ACT_COND(c))
    #define FS_RESTORABLE_NOTIF(c)(FS_RESTORABLE | FS_ACT_EN | FS_ACT_COND(c))
    #define FS_PERMANENT_NOTIF(c) (FS_PERMANENT  | FS_ACT_EN | FS_ACT_COND(c))
#else
    #define FS_TRANSIENT_NOTIF    (FS_TRANSIENT  | FS_ACT_EN)
    #define FS_VOLATILE_NOTIF     (FS_VOLATILE   | FS_ACT_EN)
    #define FS_RESTORABLE_NOTIF   (FS_RESTORABLE | FS_ACT_EN)
    #define FS_PERMANENT_NOTIF    (FS_PERMANENT  | FS_ACT_EN)
#endif

#define RWX_RWX             0b00111111
#define RW_ALL              0b00110110
#define RW_RW               0b00110110
#define RW_STANDARD         0b00110100
#define RW_R                0b00110100
#define R_R                 0b00100100


// COM-FS Operation-codes. Mimics ALP ones.
#define FS_OP_NULL      0
#define FS_OP_RD        1
#define FS_OP_WR        4
#define FS_OP_TOUCH     7  // Not in ALP but '7' is still in 'write' actions area
#define FS_OP_STAT      16
#define FS_OP_DSTAT     30 // Not in ALP: Variant of STAT for distant files
#define FS_OP_CREATE    17
#define FS_OP_FLUSH     20
#define FS_OP_EXECUTE   31
#define FS_OP_RETSTAT   32
#define FS_OP_RETDATA   34



// FS/COM-FS Status-codes. Mimics ALP ones.
enum {
    FS_STAT_PENDING                =  1,
    FS_STAT_OK                     =  0,
    FS_STAT_ERR_FID_NOEXIST        = -1,    // FF
    FS_STAT_ERR_FID_ALREADYEXIST   = -2,    // FE
    FS_STAT_ERR_NOTRESTORABLE      = -3,    // FD
    FS_STAT_ERR_PERMISSION         = -4,    // FC
    FS_STAT_ERR_LENGTH_OVFL        = -5,    // FB
    FS_STAT_ERR_ALLOC_OVFL         = -6,    // FA
    FS_STAT_ERR_START_OFFSET_OVFL  = -7,    // F9
    FS_STAT_ERR_WRITE_OVFL         = -8,    // F8
    FS_STAT_ERR_WRITE_PROTECTED    = -9,    // F7
    FS_STAT_ERR_UNKNOWN_OPERATION  = -10,   // F6
    FS_STAT_ERR_INCOMPLETE_OPERAND = -11,   // F5
    FS_STAT_ERR_WRONG_OPERAND      = -12,   // F4
    FS_STAT_ERR_UNKNOWN            = -127,  // 80
};


// Memory "TYPE"
#define FS_TYPE_DISTANT 0x80
enum {  NOFILE  = 0,
        PFLASH,
        EEPROM,
        RAM,

        HOST    = 0x80 + 0x10, // Distant HST_COM Files

        ERASED  = 0xff
};
// File "Storage Class"
enum { TRANSIENT=0, VOLATILE, RESTORABLE, PERMANENT};
// D7AactP "trigger" TODO: is it really applicable ?
enum { LIST=0, READ, WRITE, WRITE_FLUSH};

// =======================================================================
// d7a_fs_header_t
// -----------------------------------------------------------------------
/// Header Structure
// =======================================================================
TYPEDEF_STRUCT_PACKED{
    uint32_t addr         : 32;

    uint32_t type         : 8;

    // RAM Mirroring
    uint32_t mirror_idx   : 7;
    uint32_t mirrored     : 1;

    uint32_t fid          : 8;

    uint32_t afid         : 8;

    /// Storage Class
    uint32_t storage_class : 2;
    /// RFU
    uint32_t rfu_2         : 1;
    uint32_t rfu_3         : 1;
    /// D7AactP condition
    uint32_t act_cond      : 3;
    /// D7AactP is enabled
    uint32_t act_en        : 1;

    /// Guest permissions
    uint32_t guest_x       : 1;
    uint32_t guest_w       : 1;
    uint32_t guest_r       : 1;
    /// User permissions
    uint32_t user_x        : 1;
    uint32_t user_w        : 1;
    uint32_t user_r        : 1;
    /// File is executable
    uint32_t runable       : 1;
    /// File is encrypted
    uint32_t encrypted     : 1;


    uint32_t length        : 24;
    uint32_t alloc         : 24;

    uint32_t ifid          : 8;
} d7a_fs_header_bf_t;

TYPEDEF_STRUCT_PACKED{
    uint32_t addr;
    uint8_t  type;
    uint8_t  mirror;
    uint8_t  fid;

    uint8_t  afid;
    uint8_t  prop;
    uint8_t  perm;

    uint8_t  length[3];
    uint8_t  alloc[3];
    uint8_t  ifid;
} d7a_fs_header_byte_t;

typedef union
{
    d7a_fs_header_bf_t bf;
    d7a_fs_header_byte_t byte;

} d7a_fs_header_t;


typedef enum {
    KAL_FS_PROP_SIZE,
    KAL_FS_PROP_ALLOC,
    KAL_FS_PROP_TYPE,
    KAL_FS_PROP_ADDR,
    KAL_FS_PROP_AFID,
    KAL_FS_PROP_IFID,
    KAL_FS_PROP_PROP,
    KAL_FS_PROP_PERM,

    KAL_FS_PROP_ALL,
} d7a_fs_property_t;

typedef struct{
    uint32_t addr;
    uint8_t  type;
    uint8_t  afid;
    uint8_t  ifid;
    uint8_t  prop;
    uint8_t  perm;
    uint32_t length;
    uint32_t alloc;
} d7a_fs_properties_t;


typedef d7a_fs_header_t* (*GetStatFileFunction)(const uint8_t file_id);

typedef bool (*SetStatFileFunction)(const uint8_t file_id, const d7a_fs_header_t* hdr);

typedef uint32_t (*WriteFileFunction)(  const uint8_t file_id,
                                        const uint16_t offset,
                                        const uint16_t size,
                                        const uint8_t * const content);
                                            
typedef uint32_t (*ReadFileFunction)(   const uint8_t file_id,
                                        const uint16_t offset,
                                        const uint16_t size,
                                        uint8_t* buf);

typedef uint32_t (*TouchFileFunction)(  const uint8_t file_id,
                                        const uint16_t offset,
                                        const uint16_t size);
                                        
typedef struct {
    GetStatFileFunction get_stat;
    SetStatFileFunction set_stat;
    WriteFileFunction   write_file;
    ReadFileFunction    read_file;
    TouchFileFunction   touch_file;
} d7a_fs_callbacks_t;

void d7a_fs_open(const d7a_fs_callbacks_t* config);
void* d7a_fs_wait_done(uint32_t millisec = osWaitForever);
void d7a_fs_new_pkt(d7a_com_rx_msg_t* pkt);
d7a_com_rx_msg_t* d7a_fs_wait_pkt( uint32_t millisec = osWaitForever );
uint32_t d7a_fs_get_properties(uint8_t fid, d7a_fs_property_t prop, d7a_fs_properties_t* props);
void d7a_fs_distant_stat(uint8_t fid, int* length);
int d7a_fs_distant_create(uint8_t fid, d7a_fs_properties_t* props);
int d7a_fs_read(uint8_t fid, void *data, uint32_t offset, uint32_t length);


#endif