You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

133 lines
2.2 KiB

/*
* Copyright (C) 2015 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup sys_saul_reg
* @{
*
* @file
* @brief SAUL registry implementation
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "saul_reg.h"
/**
* @brief Keep the head of the device list
*/
static saul_reg_t *reg = NULL;
int saul_reg_add(saul_reg_t *dev)
{
saul_reg_t *tmp = reg;
if (dev == NULL) {
return -ENODEV;
}
/* prepare new entry */
dev->next = NULL;
/* add to registry */
if (reg == NULL) {
reg = dev;
}
else {
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = dev;
}
return 0;
}
int saul_reg_rm(saul_reg_t *dev)
{
saul_reg_t *tmp = reg;
if (reg == NULL || dev == NULL) {
return -EINVAL;
}
if (reg == dev) {
reg = dev->next;
}
while (tmp->next && (tmp->next != dev)) {
tmp = tmp->next;
}
if (tmp->next == dev) {
tmp->next = dev->next;
}
else {
return -ENODEV;
}
return 0;
}
saul_reg_t *saul_reg_get(void)
{
return reg;
}
saul_reg_t *saul_reg_find_nth(int pos)
{
saul_reg_t *tmp = reg;
for (int i = 0; (i < pos) && tmp; i++) {
tmp = tmp->next;
}
return tmp;
}
saul_reg_t *saul_reg_find_type(uint8_t type)
{
saul_reg_t *tmp = reg;
while (tmp) {
if (tmp->driver->type == type) {
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
saul_reg_t *saul_reg_find_name(const char *name)
{
saul_reg_t *tmp = reg;
while (tmp) {
if (strcmp(tmp->name, name) == 0) {
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
int saul_reg_read(saul_reg_t *dev, phydat_t *res)
{
if (dev == NULL) {
return -ENODEV;
}
return dev->driver->read(dev->dev, res);
}
int saul_reg_write(saul_reg_t *dev, phydat_t *data)
{
if (dev == NULL) {
return -ENODEV;
}
return dev->driver->write(dev->dev, data);
}