/ src / libserver / common.cpp
common.cpp
 1  /*
 2      EIBD eib bus access and management daemon
 3      Copyright (C) 2005-2011 Martin Koegler <mkoegler@auto.tuwien.ac.at>
 4  
 5      This program is free software; you can redistribute it and/or modify
 6      it under the terms of the GNU General Public License as published by
 7      the Free Software Foundation; either version 2 of the License, or
 8      (at your option) any later version.
 9  
10      This program is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13      GNU General Public License for more details.
14  
15      You should have received a copy of the GNU General Public License
16      along with this program; if not, write to the Free Software
17      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19  
20  #include "common.h"
21  
22  #include <cstdio>
23  #ifdef HAVE_SYS_TIME_H
24  #include <sys/time.h>
25  #endif
26  
27  timestamp_t
28  getTime ()
29  {
30    struct timeval t;
31    gettimeofday (&t, 0);
32    return ((timestamp_t) t.tv_sec) * 1000000 + ((timestamp_t) t.tv_usec);
33  }
34  
35  std::string
36  FormatEIBAddr (eibaddr_t addr)
37  {
38    char buf[255];
39    sprintf (buf, "%d.%d.%d", (addr >> 12) & 0xf, (addr >> 8) & 0xf,
40             (addr) & 0xff);
41    return buf;
42  }
43  
44  std::string
45  FormatGroupAddr (eibaddr_t addr)
46  {
47    char buf[255];
48    sprintf (buf, "%d/%d/%d", (addr >> 11) & 0x1f, (addr >> 8) & 0x7,
49             (addr) & 0xff);
50    return buf;
51  }
52  
53  std::string
54  FormatDomainAddr (domainaddr_t addr)
55  {
56    char buf[255];
57    sprintf (buf, "%04X", addr);
58    return buf;
59  }
60  
61  std::string
62  FormatEIBKey (eibkey_type key)
63  {
64    char buf[255];
65    sprintf (buf, "%08X", key);
66    return buf;
67  }
68  
69  void
70  addHex (std::string & s, const uint8_t c)
71  {
72    char buf[4];
73    sprintf (buf, "%02X ", c);
74    s += buf;
75  }
76  
77  void
78  add16Hex (std::string & s, const uint16_t c)
79  {
80    char buf[6];
81    sprintf (buf, "%04X ", c);
82    s += buf;
83  }
84