1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/sys/saul_reg/saul_reg.c

120 lines
2.2 KiB
C
Raw Normal View History

2015-11-18 16:07:44 +01:00
/*
* 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 <stddef.h>
2015-11-18 16:07:44 +01:00
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "saul_reg.h"
/**
* @brief Keep the head of the device list as global variable
2015-11-18 16:07:44 +01:00
*/
saul_reg_t *saul_reg = NULL;
2015-11-18 16:07:44 +01:00
int saul_reg_add(saul_reg_t *dev)
{
saul_reg_t *tmp = saul_reg;
2015-11-18 16:07:44 +01:00
if (dev == NULL) {
return -ENODEV;
}
/* prepare new entry */
dev->next = NULL;
/* add to registry */
if (saul_reg == NULL) {
saul_reg = dev;
2015-11-18 16:07:44 +01:00
}
else {
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = dev;
}
return 0;
}
saul_reg_t *saul_reg_find_nth(int pos)
{
saul_reg_t *tmp = saul_reg;
2015-11-18 16:07:44 +01:00
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 = saul_reg;
2015-11-18 16:07:44 +01:00
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 = saul_reg;
2015-11-18 16:07:44 +01:00
while (tmp) {
if (strcmp(tmp->name, name) == 0) {
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
saul_reg_t *saul_reg_find_type_and_name(uint8_t type, const char *name)
{
saul_reg_t *tmp = saul_reg;
while (tmp) {
if (tmp->driver->type == type && strcmp(tmp->name, name) == 0) {
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
2015-11-18 16:07:44 +01:00
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, const phydat_t *data)
2015-11-18 16:07:44 +01:00
{
if (dev == NULL) {
return -ENODEV;
}
return dev->driver->write(dev->dev, data);
}