Blame view

RIOT/sys/saul_reg/saul_reg.c 2.26 KB
fb11e647   vrobic   reseau statique a...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  /*
   * 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 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;
  }
  
  int saul_reg_rm(saul_reg_t *dev)
  {
      saul_reg_t *tmp = saul_reg;
  
      if (saul_reg == NULL || dev == NULL) {
          return -ENODEV;
      }
      if (saul_reg == dev) {
          saul_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_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;
  }
  
  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);
  }