Blame view

RIOT/sys/net/gnrc/netif/gnrc_netif_addr_to_str.c 1.02 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
  /*
   * Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
   *
   * 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 "net/gnrc/netif.h"
  
  static inline char _half_byte_to_char(uint8_t half_byte)
  {
      return (half_byte < 10) ? ('0' + half_byte) : ('a' + (half_byte - 10));
  }
  
  char *gnrc_netif_addr_to_str(char *out, size_t out_len, const uint8_t *addr,
                               size_t addr_len)
  {
      size_t i;
  
      if (out_len < (3 * addr_len)) { /* 2 for every byte, 1 for ':' or '\0' */
          return NULL;
      }
  
      out[0] = '\0';
  
      for (i = 0; i < addr_len; i++) {
          out[(3 * i)] = _half_byte_to_char(addr[i] >> 4);
          out[(3 * i) + 1] = _half_byte_to_char(addr[i] & 0xf);
  
          if (i != (addr_len - 1)) {
              out[(3 * i) + 2] = ':';
          }
          else {
              out[(3 * i) + 2] = '\0';
          }
      }
  
      return out;
  }
  
  /** @} */