YARP
Yet Another Robot Platform
QosStyle.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include <yarp/os/QosStyle.h>
10 #include <yarp/os/Vocab.h>
11 
12 #include <cstdlib>
13 #include <string>
14 
15 
17  threadPriority(-1),
18  threadPolicy(-1),
19  packetPriority(-1)
20 {
21 }
22 
24 {
25  if (dscp != DSCP_Invalid && dscp != DSCP_Undefined) {
26  packetPriority = ((int)dscp) << 2;
27  } else {
28  packetPriority = -1;
29  }
30 }
31 
32 
34 {
35  if (level != PacketPriorityInvalid && level != PacketPriorityUndefined) {
36  packetPriority = ((int)level) << 2;
37  } else {
38  packetPriority = -1;
39  }
40 }
41 
42 bool yarp::os::QosStyle::setPacketPriority(const std::string& priority)
43 {
44  size_t p = priority.find(':');
45  if (p == std::string::npos) {
46  return false;
47  }
48 
49  std::string key = priority.substr(0, p);
50  std::string value = priority.substr(p + 1);
51  if (key.length() <= 0 || value.length() <= 0) {
52  return false;
53  }
54 
55  if (key == "LEVEL") {
56  PacketPriorityLevel level = getLevelByVocab(yarp::os::Vocab::encode(value));
57  if (level != PacketPriorityInvalid && level != PacketPriorityUndefined) {
58  setPacketPriorityByLevel(level);
59  return true;
60  }
61  } else if (key == "DSCP") {
62  PacketPriorityDSCP dscp = getDSCPByVocab(yarp::os::Vocab::encode(value));
63  if (dscp != DSCP_Invalid && dscp != DSCP_Undefined) {
64  setPacketPriorityByDscp(dscp);
65  return true;
66  }
67  } else if (key == "TOS") {
68  char* p;
69  int tos = strtol(value.c_str(), &p, 10);
70  if (p == nullptr && tos >= 0) {
71  setPacketPrioritybyTOS(tos);
72  return true;
73  }
74  }
75 
76  packetPriority = -1;
77  return false;
78 }
79 
81 {
82  switch (packetPriority) {
83  case -1:
84  return DSCP_Invalid;
85  case (0 << 2):
86  return DSCP_CS0;
87  case (8 << 2):
88  return DSCP_CS1;
89  case (16 << 2):
90  return DSCP_CS2;
91  case (24 << 2):
92  return DSCP_CS3;
93  case (32 << 2):
94  return DSCP_CS4;
95  case (40 << 2):
96  return DSCP_CS5;
97  case (48 << 2):
98  return DSCP_CS6;
99  case (56 << 2):
100  return DSCP_CS7;
101  case (10 << 2):
102  return DSCP_AF11;
103  case (12 << 2):
104  return DSCP_AF12;
105  case (14 << 2):
106  return DSCP_AF13;
107  case (18 << 2):
108  return DSCP_AF21;
109  case (20 << 2):
110  return DSCP_AF22;
111  case (22 << 2):
112  return DSCP_AF23;
113  case (26 << 2):
114  return DSCP_AF31;
115  case (28 << 2):
116  return DSCP_AF32;
117  case (30 << 2):
118  return DSCP_AF33;
119  case (34 << 2):
120  return DSCP_AF41;
121  case (36 << 2):
122  return DSCP_AF42;
123  case (38 << 2):
124  return DSCP_AF43;
125  case (44 << 2):
126  return DSCP_VA;
127  case (46 << 2):
128  return DSCP_EF;
129  default:
130  return DSCP_Undefined;
131  }
132 }
133 
135 {
136  switch (packetPriority) {
137  case -1:
138  return PacketPriorityInvalid;
139  case (0 << 2):
140  return PacketPriorityNormal;
141  case (10 << 2):
142  return PacketPriorityLow;
143  case (36 << 2):
144  return PacketPriorityHigh;
145  case (44 << 2):
146  return PacketPriorityCritical;
147  default:
148  return PacketPriorityUndefined;
149  }
150 }
151 
152 
153 // | | Class 1 | Class 2 | Class 3 | Class 4 |
154 // |-----------|----------------|----------------|----------------|----------------|
155 // | Low Drop | AF11 (DSCP 10) | AF21 (DSCP 18) | AF31 (DSCP 26) | AF41 (DSCP 34) |
156 // | Med Drop | AF12 (DSCP 12) | AF22 (DSCP 20) | AF32 (DSCP 28) | AF42 (DSCP 36) |
157 // | High Drop | AF13 (DSCP 14) | AF23 (DSCP 22) | AF33 (DSCP 30) | AF43 (DSCP 38) |
159 {
160  switch (vocab) {
161  case yarp::os::createVocab('C', 'S', '0'):
162  return DSCP_CS0;
163  case yarp::os::createVocab('C', 'S', '1'):
164  return DSCP_CS1;
165  case yarp::os::createVocab('C', 'S', '2'):
166  return DSCP_CS2;
167  case yarp::os::createVocab('C', 'S', '3'):
168  return DSCP_CS3;
169  case yarp::os::createVocab('C', 'S', '4'):
170  return DSCP_CS4;
171  case yarp::os::createVocab('C', 'S', '5'):
172  return DSCP_CS5;
173  case yarp::os::createVocab('C', 'S', '6'):
174  return DSCP_CS6;
175  case yarp::os::createVocab('C', 'S', '7'):
176  return DSCP_CS7;
177  case yarp::os::createVocab('A', 'F', '1', '1'):
178  return DSCP_AF11;
179  case yarp::os::createVocab('A', 'F', '1', '2'):
180  return DSCP_AF12;
181  case yarp::os::createVocab('A', 'F', '1', '3'):
182  return DSCP_AF13;
183  case yarp::os::createVocab('A', 'F', '2', '1'):
184  return DSCP_AF21;
185  case yarp::os::createVocab('A', 'F', '2', '2'):
186  return DSCP_AF22;
187  case yarp::os::createVocab('A', 'F', '2', '3'):
188  return DSCP_AF23;
189  case yarp::os::createVocab('A', 'F', '3', '1'):
190  return DSCP_AF31;
191  case yarp::os::createVocab('A', 'F', '3', '2'):
192  return DSCP_AF32;
193  case yarp::os::createVocab('A', 'F', '3', '3'):
194  return DSCP_AF33;
195  case yarp::os::createVocab('A', 'F', '4', '1'):
196  return DSCP_AF41;
197  case yarp::os::createVocab('A', 'F', '4', '2'):
198  return DSCP_AF42;
199  case yarp::os::createVocab('A', 'F', '4', '3'):
200  return DSCP_AF43;
201  case yarp::os::createVocab('V', 'A'):
202  return DSCP_VA;
203  case yarp::os::createVocab('E', 'F'):
204  return DSCP_EF;
205  default:
206  return DSCP_Invalid;
207  };
208 }
209 
210 
212 {
213  switch (vocab) {
214  case yarp::os::createVocab('N', 'O', 'R', 'M'):
215  return PacketPriorityNormal;
216  case yarp::os::createVocab('L', 'O', 'W'):
217  return PacketPriorityLow;
218  case yarp::os::createVocab('H', 'I', 'G', 'H'):
219  return PacketPriorityHigh;
220  case yarp::os::createVocab('C', 'R', 'I', 'T'):
221  return PacketPriorityCritical;
222  default:
223  return PacketPriorityInvalid;
224  }
225 }
yarp::os::createVocab
constexpr yarp::conf::vocab32_t createVocab(char a, char b=0, char c=0, char d=0)
Definition: Vocab.h:22
yarp::os::QosStyle::getDSCPByVocab
static PacketPriorityDSCP getDSCPByVocab(int vocab)
returns the IPV4/6 DSCP value given as DSCP code
Definition: QosStyle.cpp:158
yarp::os::QosStyle::setPacketPriority
bool setPacketPriority(const std::string &priority)
sets the packet priority from a string.
Definition: QosStyle.cpp:42
yarp::os::QosStyle::getPacketPriorityAsLevel
PacketPriorityLevel getPacketPriorityAsLevel() const
returns the packet TOS value
Definition: QosStyle.cpp:134
yarp::os::QosStyle::getLevelByVocab
static PacketPriorityLevel getLevelByVocab(int vocab)
returns the priority level
Definition: QosStyle.cpp:211
yarp::os::QosStyle::setPacketPriorityByLevel
void setPacketPriorityByLevel(PacketPriorityLevel level)
sets the packet priority given as PacketPriorityLevel
Definition: QosStyle.cpp:33
yarp::os::QosStyle::getPacketPriorityAsDSCP
PacketPriorityDSCP getPacketPriorityAsDSCP() const
returns the packet DSCP class
Definition: QosStyle.cpp:80
QosStyle.h
yarp::os::QosStyle::QosStyle
QosStyle()
Constructor.
Definition: QosStyle.cpp:16
yarp::os::Vocab::encode
NetInt32 encode(const std::string &str)
Convert a string into a vocabulary identifier.
Definition: Vocab.cpp:14
yarp::os::QosStyle::PacketPriorityDSCP
PacketPriorityDSCP
The PacketPriorityDSCP defines the packets quality of service (priority) using DSCP.
Definition: QosStyle.h:48
Vocab.h
yarp::os::QosStyle::PacketPriorityLevel
PacketPriorityLevel
The PacketPriorityLevel defines the packets quality of service (priority) levels.
Definition: QosStyle.h:33
yarp::os::QosStyle::setPacketPriorityByDscp
void setPacketPriorityByDscp(PacketPriorityDSCP dscp)
sets the packet priority given as DSCP class
Definition: QosStyle.cpp:23