Blame view

RIOT/sys/net/gnrc/netif/gnrc_netif.c 2.09 KB
a752c7ab   elopes   add first test an...
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
  /*
   * Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
   * Copyright (C) 2015 INRIA
   *
   * 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.
   */
  
  /**
   * @{
   *
   * @file
   */
  
  #include <errno.h>
  #include "kernel_types.h"
  #include "net/gnrc/netif.h"
  
  #ifdef MODULE_GNRC_IPV6_NETIF
  #include "net/gnrc/ipv6/netif.h"
  #endif
  
  static gnrc_netif_handler_t if_handler[] = {
  #ifdef MODULE_GNRC_IPV6_NETIF
      { gnrc_ipv6_netif_add, gnrc_ipv6_netif_remove },
  #endif
      /* #ifdef MODULE_GNRC_IPV4_NETIF
       *  { ipv4_netif_add, ipv4_netif_remove },
       * #endif ... you get the idea
       */
      { NULL, NULL }
  };
  
  static kernel_pid_t ifs[GNRC_NETIF_NUMOF];
  
  void gnrc_netif_init(void)
  {
      for (int i = 0; i < GNRC_NETIF_NUMOF; i++) {
          ifs[i] = KERNEL_PID_UNDEF;
      }
  }
  
  int gnrc_netif_add(kernel_pid_t pid)
  {
      kernel_pid_t *free_entry = NULL;
  
      for (int i = 0; i < GNRC_NETIF_NUMOF; i++) {
          if (ifs[i] == pid) {
              return 0;
          }
          else if (ifs[i] == KERNEL_PID_UNDEF && !free_entry) {
              free_entry = &ifs[i];
          }
      }
  
      if (!free_entry) {
          return -ENOMEM;
      }
  
      *free_entry = pid;
  
      for (int j = 0; if_handler[j].add != NULL; j++) {
          if_handler[j].add(pid);
      }
  
      return 0;
  }
  
  void gnrc_netif_remove(kernel_pid_t pid)
  {
      int i;
  
      for (i = 0; i < GNRC_NETIF_NUMOF; i++) {
          if (ifs[i] == pid) {
              ifs[i] = KERNEL_PID_UNDEF;
  
              for (int j = 0; if_handler[j].remove != NULL; j++) {
                  if_handler[j].remove(pid);
              }
  
              return;
          }
      }
  }
  
  size_t gnrc_netif_get(kernel_pid_t *netifs)
  {
      size_t size = 0;
  
      for (int i = 0; i < GNRC_NETIF_NUMOF; i++) {
          if (ifs[i] != KERNEL_PID_UNDEF) {
              netifs[size++] = ifs[i];
          }
      }
  
      return size;
  }
  
  bool gnrc_netif_exist(kernel_pid_t pid)
  {
      for (int i = 0; i < GNRC_NETIF_NUMOF; i++) {
          if (ifs[i] == pid) {
              return true;
          }
      }
      return false;
  }
  
  /** @} */