input_mac.cc
Go to the documentation of this file.
1 /*
2  * Roboception GmbH
3  * Munich, Germany
4  * www.roboception.com
5  *
6  * Copyright (c) 2024 Roboception GmbH
7  * All rights reserved
8  *
9  * Author: Heiko Hirschmueller
10  */
11 
12 #include "input_mac.h"
13 
14 #include <sstream>
15 
16 InputMAC::InputMAC(int x, int y, int w, int h, const char *label) : Fl_Input(x, y, w, h, label)
17 {
18  cb=0;
19  user=0;
20 
21  tooltip("Format example: 01:23:45:67:89:ab");
22 }
23 
24 uint64_t InputMAC::getMAC()
25 {
26  std::istringstream in(value());
27 
28  // read formated
29 
30  uint64_t v[6]={256, 256, 256, 256, 256, 256};
31  char s[5]={' ', ' ', ' ', ' ', ' '};
32  in >> std::hex >> v[0] >> s[0] >> v[1] >> s[1] >> v[2] >> s[2] >> v[3] >> s[3] >> v[4] >>
33  s[4] >> v[5];
34 
35  // all separators must be ':'
36 
37  for (int i=0; i<5; i++)
38  {
39  if (s[i] != ':') return 0;
40  }
41 
42  // its invalid if there is unparsed characters left
43 
44  if (!in.eof())
45  {
46  return 0;
47  }
48 
49  // check validity of individual numbers
50 
51  for (int i=0; i<6; i++)
52  {
53  if (v[i] > 255) return 0;
54  }
55 
56  // return MAC as integer
57 
58  return ((v[0]<<40) | (v[1]<<32) | (v[2]<<24) | (v[3]<<16) | (v[4]<<8) | v[5]);
59 }
60 
61 int InputMAC::handle(int event)
62 {
63  if (event == FL_KEYBOARD && !(Fl::event_key() == FL_Tab))
64  {
65  char ascii=Fl::event_text()[0];
66 
67  if (std::isprint(ascii) && !isxdigit(ascii) && ascii != ':')
68  {
69  return 1; // ignore all printable characters except accepted ones
70  }
71  }
72 
73  std::string v=value();
74 
75  // handle event in superclass
76 
77  int ret=Fl_Input::handle(event);
78 
79  // call callback if value has changed
80 
81  if (cb && v != value())
82  {
83  cb(this, user);
84  }
85 
86  return ret;
87 }
input_mac.h
InputMAC::getMAC
uint64_t getMAC()
Definition: input_mac.cc:24
InputMAC::cb
Fl_Callback * cb
Definition: input_mac.h:36
InputMAC::handle
int handle(int event)
Definition: input_mac.cc:61
InputMAC::user
void * user
Definition: input_mac.h:37
InputMAC::InputMAC
InputMAC(int x, int y, int w, int h, const char *label)
Definition: input_mac.cc:16


rcdiscover
Author(s): Heiko Hirschmueller , Raphael Schaller
autogenerated on Thu Aug 1 2024 02:55:56