libsysfs
3
HAL Library

# libsysfs

## Synopsis

    #define     SYSFS_FSTYPE_NAME
    #define     SYSFS_PROC_MNTS
    #define     SYSFS_BUS_DIR
    #define     SYSFS_BUS_NAME
    #define     SYSFS_CLASS_DIR
    #define     SYSFS_CLASS_NAME
    #define     SYSFS_BLOCK_DIR
    #define     SYSFS_BLOCK_NAME
    #define     SYSFS_DEVICES_DIR
    #define     SYSFS_DEVICES_NAME
    #define     SYSFS_DRIVERS_DIR
    #define     SYSFS_DRIVERS_NAME
    #define     SYSFS_NAME_ATTRIBUTE
    #define     SYSFS_UNKNOWN
    #define     SYSFS_PATH_ENV
    #define     SYSFS_PATH_MAX
    #define     SYSFS_NAME_LEN
    #define     SYSFS_BUS_ID_SIZE
    #define     SYSFS_METHOD_SHOW
    #define     SYSFS_METHOD_STORE
    struct      sysfs_attribute;
    struct      sysfs_link;
    struct      sysfs_directory;
    struct      sysfs_driver;
    struct      sysfs_device;
    struct      sysfs_root_device;
    struct      sysfs_bus;
    struct      sysfs_class_device;
    struct      sysfs_class;
    int         sysfs_get_mnt_path              (unsigned char *mnt_path,
                                                 size_t len);
    int         sysfs_get_name_from_path        (unsigned char *path,
                                                 unsigned char *name,
                                                 size_t len);
    int         sysfs_get_link                  (unsigned char *path,
                                                 unsigned char *target,
                                                 size_t len);
    struct dlist* sysfs_open_subsystem_list     (unsigned char *name);
    struct dlist* sysfs_open_bus_devices_list   (unsigned char *name);
    void        sysfs_close_list                (struct dlist *list);
    void        sysfs_close_attribute           (struct sysfs_attribute *sysattr);
    struct sysfs_attribute* sysfs_open_attribute
                                                (unsigned char *path);
    int         sysfs_read_attribute            (struct sysfs_attribute *sysattr);
    int         sysfs_read_attribute_value      (unsigned char *attrpath,
                                                 unsigned char *value,
                                                 size_t vsize);
    int         sysfs_write_attribute           (struct sysfs_attribute *sysattr,
                                                 unsigned char *new_value,
                                                 size_t len);
    unsigned char* sysfs_get_value_from_attributes
                                                (struct dlist *attr,
                                                 unsigned char *name);
    void        sysfs_close_directory           (struct sysfs_directory *sysdir);
    struct sysfs_directory* sysfs_open_directory
                                                (unsigned char *path);
    int         sysfs_read_directory            (struct sysfs_directory *sysdir);
    int         sysfs_read_all_subdirs          (struct sysfs_directory *sysdir);
    void        sysfs_close_link                (struct sysfs_link *ln);
    struct sysfs_link* sysfs_open_link          (unsigned char *lnpath);
    struct sysfs_link* sysfs_get_directory_link (struct sysfs_directory *dir,
                                                 unsigned char *linkname);
    void        sysfs_close_driver              (struct sysfs_driver *driver);
    struct sysfs_driver* sysfs_open_driver      (unsigned char *path);
    struct dlist* sysfs_get_driver_attributes   (struct sysfs_driver *driver);
    struct dlist* sysfs_get_driver_links        (struct sysfs_driver *driver);
    void        sysfs_close_driver_by_name      (struct sysfs_driver *driver);
    struct sysfs_attribute* sysfs_open_driver_attr
                                                (unsigned char *bus,
                                                 unsigned char *drv,
                                                 unsigned char *attrib);
    void        sysfs_close_root_device         (struct sysfs_root_device *root);
    void        sysfs_close_device              (struct sysfs_device *dev);
    struct sysfs_device* sysfs_open_device      (unsigned char *path);
    struct dlist* sysfs_get_device_attributes   (struct sysfs_device *device);
    struct sysfs_attribute* sysfs_open_device_attr
                                                (unsigned char *bus,
                                                 unsigned char *bus_id,
                                                 unsigned char *attrib);
    void        sysfs_close_bus                 (struct sysfs_bus *bus);
    struct sysfs_bus* sysfs_open_bus            (unsigned char *name);
    struct sysfs_device* sysfs_get_bus_device   (struct sysfs_bus *bus,
                                                 unsigned char *id);
    struct sysfs_driver* sysfs_get_bus_driver   (struct sysfs_bus *bus,
                                                 unsigned char *drvname);
    struct dlist* sysfs_get_bus_attributes      (struct sysfs_bus *bus);
    struct sysfs_attribute* sysfs_get_bus_attribute
                                                (struct sysfs_bus *bus,
                                                 unsigned char *attrname);
    struct sysfs_device* sysfs_open_bus_device  (unsigned char *busname,
                                                 unsigned char *dev_id);
    int         sysfs_find_driver_bus           (unsigned char *driver,
                                                 unsigned char *busname,
                                                 size_t bsize);
    void        sysfs_close_class_device        (struct sysfs_class_device *dev);
    void        sysfs_close_class               (struct sysfs_class *cls);
    struct sysfs_class* sysfs_open_class        (unsigned char *name);

## Description

## Details

### SYSFS_FSTYPE_NAME

    #define SYSFS_FSTYPE_NAME	"sysfs"

### SYSFS_PROC_MNTS

    #define SYSFS_PROC_MNTS		"/proc/mounts"

### SYSFS_BUS_DIR

    #define SYSFS_BUS_DIR		"/bus"

### SYSFS_BUS_NAME

    #define SYSFS_BUS_NAME		"bus"

### SYSFS_CLASS_DIR

    #define SYSFS_CLASS_DIR		"/class"

### SYSFS_CLASS_NAME

    #define SYSFS_CLASS_NAME	"class"

### SYSFS_BLOCK_DIR

    #define SYSFS_BLOCK_DIR		"/block"

### SYSFS_BLOCK_NAME

    #define SYSFS_BLOCK_NAME	"block"

### SYSFS_DEVICES_DIR

    #define SYSFS_DEVICES_DIR	"/devices"

### SYSFS_DEVICES_NAME

    #define SYSFS_DEVICES_NAME	"devices"

### SYSFS_DRIVERS_DIR

    #define SYSFS_DRIVERS_DIR	"/drivers"

### SYSFS_DRIVERS_NAME

    #define SYSFS_DRIVERS_NAME	"drivers"

### SYSFS_NAME_ATTRIBUTE

    #define SYSFS_NAME_ATTRIBUTE	"name"

### SYSFS_UNKNOWN

    #define SYSFS_UNKNOWN		"unknown"

### SYSFS_PATH_ENV

    #define SYSFS_PATH_ENV		"SYSFS_PATH"

### SYSFS_PATH_MAX

    #define SYSFS_PATH_MAX		255

### SYSFS_NAME_LEN

    #define	SYSFS_NAME_LEN		50

### SYSFS_BUS_ID_SIZE

    #define SYSFS_BUS_ID_SIZE	20

### SYSFS_METHOD_SHOW

    #define SYSFS_METHOD_SHOW	0x01	/* attr can be read by user */

### SYSFS_METHOD_STORE

    #define SYSFS_METHOD_STORE	0x02	/* attr can be changed by user */

### struct sysfs_attribute

    struct sysfs_attribute {
    	unsigned char *value;
    	unsigned short len;		/* value length */
    	unsigned short method;		/* show and store */
    	unsigned char name[SYSFS_NAME_LEN];
    	unsigned char path[SYSFS_PATH_MAX];
    };

### struct sysfs_link

    struct sysfs_link {
    	unsigned char name[SYSFS_NAME_LEN];
    	unsigned char path[SYSFS_PATH_MAX];
    	unsigned char target[SYSFS_PATH_MAX];
    };

### struct sysfs_directory

    struct sysfs_directory {
    	struct dlist *subdirs;
    	struct dlist *links;
    	struct dlist *attributes;
    	unsigned char name[SYSFS_NAME_LEN];
    	unsigned char path[SYSFS_PATH_MAX];
    };

### struct sysfs_driver

    struct sysfs_driver {
    	struct dlist *devices;
    	unsigned char name[SYSFS_NAME_LEN];
    	unsigned char path[SYSFS_PATH_MAX];

    	/* for internal use only */
    	struct sysfs_directory *directory;
    };

### struct sysfs_device

    struct sysfs_device {
    	struct sysfs_device *parent;
    	struct dlist *children;
    	unsigned char name[SYSFS_NAME_LEN];
    	unsigned char bus_id[SYSFS_NAME_LEN];
    	unsigned char bus[SYSFS_NAME_LEN];
    	unsigned char driver_name[SYSFS_NAME_LEN];
    	unsigned char path[SYSFS_PATH_MAX];

    	/* for internal use only */
    	struct sysfs_directory *directory;
    };

### struct sysfs_root_device

    struct sysfs_root_device {
    	struct dlist *devices;
    	unsigned char name[SYSFS_NAME_LEN];
    	unsigned char path[SYSFS_PATH_MAX];

    	/* for internal use only */
    	struct sysfs_directory *directory;
    };

### struct sysfs_bus

    struct sysfs_bus {
    	struct dlist *drivers;
    	struct dlist *devices;
    	unsigned char name[SYSFS_NAME_LEN];
    	unsigned char path[SYSFS_PATH_MAX];

    	/* internal use only */
    	struct sysfs_directory *directory;
    };

### struct sysfs_class_device

    struct sysfs_class_device {
    	struct sysfs_device *sysdevice;		/* NULL if virtual */
    	struct sysfs_driver *driver;		/* NULL if not implemented */
    	unsigned char name[SYSFS_NAME_LEN];
    	unsigned char classname[SYSFS_NAME_LEN];
    	unsigned char path[SYSFS_PATH_MAX];

    	/* for internal use only */
    	struct sysfs_directory *directory;
    };

### struct sysfs_class

    struct sysfs_class {
    	struct dlist *devices;
    	unsigned char name[SYSFS_NAME_LEN];
    	unsigned char path[SYSFS_PATH_MAX];

    	/* for internal use only */
    	struct sysfs_directory *directory;
    };

### sysfs_get_mnt_path ()

    int         sysfs_get_mnt_path              (unsigned char *mnt_path,
                                                 size_t len);

`mnt_path` :

  place to put the retrieved mount path

`len` :

  size of mnt_path returns 0 with success and -1 with error.

Returns :

### sysfs_get_name_from_path ()

    int         sysfs_get_name_from_path        (unsigned char *path,
                                                 unsigned char *name,
                                                 size_t len);

`path` :

  path to get name from

`name` :

  where to put name

`len` :

  size of name

Returns :

### sysfs_get_link ()

    int         sysfs_get_link                  (unsigned char *path,
                                                 unsigned char *target,
                                                 size_t len);

`path` :

  symbolic link's path

`target` :

  where to put name

`len` :

  size of name

Returns :

### sysfs_open_subsystem_list ()

    struct dlist* sysfs_open_subsystem_list     (unsigned char *name);

`name` :

  name of the subsystem, eg., "bus", "class", "devices" Returns a dlist of supported names or NULL if subsystem not supported

Returns :

### sysfs_open_bus_devices_list ()

    struct dlist* sysfs_open_bus_devices_list   (unsigned char *name);

`name` :

  name of the subsystem, eg., "pci", "scsi", "usb" Returns a dlist of supported names or NULL if subsystem not supported

Returns :

### sysfs_close_list ()

    void        sysfs_close_list                (struct dlist *list);

`list` :

  dlist to free Returns nothing

### sysfs_close_attribute ()

    void        sysfs_close_attribute           (struct sysfs_attribute *sysattr);

`sysattr` :

  attribute to close.

### sysfs_open_attribute ()

    struct sysfs_attribute* sysfs_open_attribute
                                                (unsigned char *path);

`path` :

  path to attribute. returns sysfs_attribute struct with success and NULL with error.

Returns :

### sysfs_read_attribute ()

    int         sysfs_read_attribute            (struct sysfs_attribute *sysattr);

`sysattr` :

  attribute to read returns 0 with success and -1 with error.

Returns :

### sysfs_read_attribute_value ()

    int         sysfs_read_attribute_value      (unsigned char *attrpath,
                                                 unsigned char *value,
                                                 size_t vsize);

`attrpath` :

  sysfs path to attribute

`value` :

  buffer to put value

`vsize` :

  size of value buffer returns 0 with success and -1 with error.

Returns :

### sysfs_write_attribute ()

    int         sysfs_write_attribute           (struct sysfs_attribute *sysattr,
                                                 unsigned char *new_value,
                                                 size_t len);

`sysattr` :

  attribute to write

`new_value` :

  value to write

`len` :

  length of "new_value" returns 0 with success and -1 with error.

Returns :

### sysfs_get_value_from_attributes ()

    unsigned char* sysfs_get_value_from_attributes
                                                (struct dlist *attr,
                                                 unsigned char *name);

`attr` :

`name` :

Returns :

### sysfs_close_directory ()

    void        sysfs_close_directory           (struct sysfs_directory *sysdir);

`sysdir` :

  sysfs_directory to close

### sysfs_open_directory ()

    struct sysfs_directory* sysfs_open_directory
                                                (unsigned char *path);

`path` :

  path of directory to open. returns: struct sysfs_directory * with success and NULL on error.

Returns :

### sysfs_read_directory ()

    int         sysfs_read_directory            (struct sysfs_directory *sysdir);

`sysdir` :

  sysfs directory to open returns 0 with success and -1 with error.

Returns :

### sysfs_read_all_subdirs ()

    int         sysfs_read_all_subdirs          (struct sysfs_directory *sysdir);

`sysdir` :

  directory whose subdirs need reading. returns 0 with success and -1 with error.

Returns :

### sysfs_close_link ()

    void        sysfs_close_link                (struct sysfs_link *ln);

`ln` :

  link to close.

### sysfs_open_link ()

    struct sysfs_link* sysfs_open_link          (unsigned char *lnpath);

`lnpath` :

Returns :

### sysfs_get_directory_link ()

    struct sysfs_link* sysfs_get_directory_link (struct sysfs_directory *dir,
                                                 unsigned char *linkname);

`dir` :

  directory to retrieve link from

`linkname` :

  name of link to look for returns reference to sysfs_link if found and NULL if not found

Returns :

### sysfs_close_driver ()

    void        sysfs_close_driver              (struct sysfs_driver *driver);

`driver` :

  driver to close

### sysfs_open_driver ()

    struct sysfs_driver* sysfs_open_driver      (unsigned char *path);

`path` :

  path to driver directory returns struct sysfs_driver with success and NULL with error

Returns :

### sysfs_get_driver_attributes ()

    struct dlist* sysfs_get_driver_attributes   (struct sysfs_driver *driver);

`driver` :

  sysfs_driver for which attributes are required returns a dlist of attributes corresponding to the driver if present NULL otherwise

Returns :

### sysfs_get_driver_links ()

    struct dlist* sysfs_get_driver_links        (struct sysfs_driver *driver);

`driver` :

  sysfs_driver for which links list is required returns a dlist of links corresponding to the driver if present NULL otherwise

Returns :

### sysfs_close_driver_by_name ()

    void        sysfs_close_driver_by_name      (struct sysfs_driver *driver);

`driver` :

  driver to close

### sysfs_open_driver_attr ()

    struct sysfs_attribute* sysfs_open_driver_attr
                                                (unsigned char *bus,
                                                 unsigned char *drv,
                                                 unsigned char *attrib);

NOTE: A call to `sysfs_close_attribute()` is required to close the attribute returned and to free memory

`bus` :

  bus on which to look

`drv` :

  driver whose attribute has to be read

`attrib` :

  Attribute to be read Returns struct sysfs_attribute on success and NULL on failure

Returns :

### sysfs_close_root_device ()

    void        sysfs_close_root_device         (struct sysfs_root_device *root);

`root` :

  root device to close

### sysfs_close_device ()

    void        sysfs_close_device              (struct sysfs_device *dev);

`dev` :

### sysfs_open_device ()

    struct sysfs_device* sysfs_open_device      (unsigned char *path);

`path` :

  path to device, this is the /sys/devices/ path returns sysfs_device structure with success or NULL with error

Returns :

### sysfs_get_device_attributes ()

    struct dlist* sysfs_get_device_attributes   (struct sysfs_device *device);

`device` :

  struct sysfs_device * for which attributes are to be returned

Returns :

### sysfs_open_device_attr ()

    struct sysfs_attribute* sysfs_open_device_attr
                                                (unsigned char *bus,
                                                 unsigned char *bus_id,
                                                 unsigned char *attrib);

`bus` :

`bus_id` :

`attrib` :

Returns :

### sysfs_close_bus ()

    void        sysfs_close_bus                 (struct sysfs_bus *bus);

`bus` :

  bus structure

### sysfs_open_bus ()

    struct sysfs_bus* sysfs_open_bus            (unsigned char *name);

`name` :

Returns :

### sysfs_get_bus_device ()

    struct sysfs_device* sysfs_get_bus_device   (struct sysfs_bus *bus,
                                                 unsigned char *id);

`bus` :

  bus to find device on

`id` :

  bus_id for device returns struct sysfs_device reference or NULL if not found.

Returns :

### sysfs_get_bus_driver ()

    struct sysfs_driver* sysfs_get_bus_driver   (struct sysfs_bus *bus,
                                                 unsigned char *drvname);

`bus` :

  bus to find driver on

`drvname` :

  name of driver returns struct sysfs_driver reference or NULL if not found.

Returns :

### sysfs_get_bus_attributes ()

    struct dlist* sysfs_get_bus_attributes      (struct sysfs_bus *bus);

`bus` :

  bus to get attributes for. returns dlist of attributes or NULL if there aren't any.

Returns :

### sysfs_get_bus_attribute ()

    struct sysfs_attribute* sysfs_get_bus_attribute
                                                (struct sysfs_bus *bus,
                                                 unsigned char *attrname);

`bus` :

  bus to retrieve attribute from

`attrname` :

  attribute name to retrieve returns reference to sysfs_attribute if found or NULL if not found

Returns :

### sysfs_open_bus_device ()

    struct sysfs_device* sysfs_open_bus_device  (unsigned char *busname,
                                                 unsigned char *dev_id);

`busname` :

  Name of bus to search

`dev_id` :

  Id of device on bus. returns sysfs_device if found or NULL if not.

Returns :

### sysfs_find_driver_bus ()

    int         sysfs_find_driver_bus           (unsigned char *driver,
                                                 unsigned char *busname,
                                                 size_t bsize);

`driver` :

  name of the driver to locate

`busname` :

  buffer to copy name to

`bsize` :

  buffer size returns 0 with success, -1 with error

Returns :

### sysfs_close_class_device ()

    void        sysfs_close_class_device        (struct sysfs_class_device *dev);

`dev` :

  class device to close.

### sysfs_close_class ()

    void        sysfs_close_class               (struct sysfs_class *cls);

`cls` :

### sysfs_open_class ()

    struct sysfs_class* sysfs_open_class        (unsigned char *name);

`name` :

Returns :