StateFS
 All Classes Files Functions Variables Typedefs Enumerations Groups Pages
Classes | Macros | Functions | Variables
Fake Power provider example

Example provider - provides fake power supply properties. More...

Classes

struct  statefs_power_prop
 
struct  power_handle
 

Macros

#define iv_max_size   (sizeof(max_iv_fmt) - 1)
 
#define is_low_max_len   sizeof(is_low_fmt)
 

Functions

static void * control_thread (void *)
 
static int power_init ()
 
static int read_voltage (char *dst, statefs_size_t len)
 
static int read_current (char *dst, statefs_size_t len)
 
static bool update_is_low ()
 
static int read_is_low (char *dst, statefs_size_t len)
 
static struct statefs_power_proppower_prop (struct statefs_property *p)
 
static struct statefs_nodeprop_find (struct statefs_branch const *self, char const *name)
 
static void prop_next (struct statefs_branch const *self, statefs_handle_t *idx_ptr)
 
static struct statefs_nodeprop_get (struct statefs_branch const *self, statefs_handle_t idx)
 
static statefs_handle_t prop_first (struct statefs_branch const *self)
 
static struct statefs_nodens_find (struct statefs_branch const *self, char const *name)
 
static struct statefs_nodens_get (struct statefs_branch const *self, statefs_handle_t p)
 
static statefs_handle_t ns_first (struct statefs_branch const *self)
 
static void power_release (struct statefs_node *node)
 
static bool power_connect (struct statefs_property *p, struct statefs_slot *slot)
 
static void power_disconnect (struct statefs_property *p)
 
static int power_getattr (struct statefs_property const *p)
 
static statefs_ssize_t power_size (struct statefs_property const *p)
 
static statefs_handle_t power_open (struct statefs_property *p, int mode)
 
static int power_read (statefs_handle_t h, char *dst, statefs_size_t len, statefs_off_t off)
 
static void power_close (statefs_handle_t h)
 

Variables

static pthread_mutex_t power_mutex = PTHREAD_MUTEX_INITIALIZER
 
static bool is_running = true
 
static pthread_t tid
 
static double voltage_now = 3.6
 
static double dvoltage = 0.1
 
static const char max_iv_fmt [] = "1.0000"
 
static bool power_is_initialized = false
 
static const char is_low_fmt [] = "0"
 
static bool is_low = false
 
static struct statefs_slotis_low_slot = NULL
 
static struct statefs_power_prop props []
 
static struct statefs_namespace battery_ns
 
static struct statefs_provider provider
 

Detailed Description

Example provider - provides fake power supply properties.

Provider root is provider (returned by statefs_provider_get()).

There is one namespace and array of properties. There are 3 properties: continuous voltage and current, and discrete is_low.

On first properties access separate thread is run starting to generate monotonous fake voltage values changes each second and updating is_low property when voltage value crosses "low voltage" barrier. Current (I, A) values are randomly generated. All values are formatted into fixed-size fields. Provider structures are statically defined and theoretically functionality can be taken out into framework but it was not done. Maybe later...

Access to any node is serialized by server, so power_mutex is used only to synchronize with voltage values generation thread

Function Documentation

static int power_getattr ( struct statefs_property const *  p)
static

all example properties are read-only

is_low property is discrete and this is hardcoded here and also in power_connect() and power_disconnect() for simplicity

static statefs_handle_t power_open ( struct statefs_property p,
int  mode 
)
static

Allocates power_handle structure to be used as a handle.

On first run also execute initialization of provider including starting of voltage values generation thread

static int power_read ( statefs_handle_t  h,
char *  dst,
statefs_size_t  len,
statefs_off_t  off 
)
static

if reading starting from non-zero offset it is considered read operation is continued, so previous cached result is used if exists

static statefs_ssize_t power_size ( struct statefs_property const *  p)
static

just return maximum size of the property

Variable Documentation

struct statefs_namespace battery_ns
static
Initial value:
= {
.node = {
.type = statefs_node_ns,
.name = "battery",
},
.branch = {
.find = prop_find,
.first = prop_first,
.next = prop_next,
.get = prop_get,
}
}

provider main (and the only) namespace

pthread_mutex_t power_mutex = PTHREAD_MUTEX_INITIALIZER
static

sync generation thread and property access

struct statefs_power_prop props[]
static
Initial value:
= {
{
.prop = {
.node = {
.type = statefs_node_prop,
.name = "voltage"
},
.default_value = STATEFS_REAL(3.8)
},
.read = read_voltage,
.max_size = iv_max_size
},
{
.prop = {
.node = {
.type = statefs_node_prop,
.name = "current"
},
.default_value = STATEFS_REAL(0),
},
.read = read_current,
.max_size = iv_max_size
},
{
.prop = {
.node = {
.type = statefs_node_prop,
.name = "is_low"
},
.default_value = STATEFS_BOOL(false)
},
.read = read_is_low,
.max_size = is_low_max_len
}
}

array describing properties

struct statefs_provider provider
static
Initial value:
= {
.version = STATEFS_CURRENT_VERSION,
.root = {
.node = {
.type = statefs_node_root,
.name = "power",
.release = &power_release
},
.branch = {
.find = ns_find,
.first = &ns_first,
.get = &ns_get
}
},
.io = {
.getattr = power_getattr,
.open = power_open,
.read = power_read,
.size = power_size,
.close = power_close,
.connect = power_connect,
.disconnect = power_disconnect
}
}

provider root structure