mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
16f859dafd
This makes life easier when calling e.g. `saul_reg_write()` with data stored in flash. As now the signatures for reading and writing differ (in that `const` qualifier only), `saul_notsup()` is split into `saul_write_notsup()` and `saul_read_notsup()`. However, one is implemented as a symbol alias of the other, so that ROM consumption remains unchanged.
120 lines
2.2 KiB
C
120 lines
2.2 KiB
C
/*
|
|
* 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>
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#include "saul_reg.h"
|
|
|
|
/**
|
|
* @brief Keep the head of the device list as global variable
|
|
*/
|
|
saul_reg_t *saul_reg = NULL;
|
|
|
|
int saul_reg_add(saul_reg_t *dev)
|
|
{
|
|
saul_reg_t *tmp = saul_reg;
|
|
|
|
if (dev == NULL) {
|
|
return -ENODEV;
|
|
}
|
|
|
|
/* prepare new entry */
|
|
dev->next = NULL;
|
|
/* add to registry */
|
|
if (saul_reg == NULL) {
|
|
saul_reg = dev;
|
|
}
|
|
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;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
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;
|
|
}
|
|
|
|
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)
|
|
{
|
|
if (dev == NULL) {
|
|
return -ENODEV;
|
|
}
|
|
return dev->driver->write(dev->dev, data);
|
|
}
|