-
Notifications
You must be signed in to change notification settings - Fork 143
/
BaudRateCommand.java
78 lines (65 loc) · 2.15 KB
/
BaudRateCommand.java
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
/*
* Copyright (C) 2010 Archie L. Cobbs. All rights reserved.
*
* $Id: BaudRateCommand.java 39 2011-03-22 17:21:53Z archie.cobbs $
*/
package gnu.io.rfc2217;
import static gnu.io.rfc2217.RFC2217.COM_PORT_OPTION;
import static gnu.io.rfc2217.RFC2217.SERVER_OFFSET;
import static gnu.io.rfc2217.RFC2217.SET_BAUDRATE;
/**
* RFC 2217 {@code SET-BAUDRATE} command.
*
* @see <a href="http://tools.ietf.org/html/rfc2217">RFC 2217</a>
*/
public class BaudRateCommand extends ComPortCommand {
private int baudRate;
/**
* Decoding constructor.
*
* @param bytes encoded option starting with the {@code COM-PORT-OPTION} byte
* @throws NullPointerException if {@code bytes} is null
* @throws IllegalArgumentException if {@code bytes} has length that is too short or too long
* @throws IllegalArgumentException if {@code bytes[0]} is not {@link RFC2217#COM_PORT_OPTION}
* @throws IllegalArgumentException if {@code bytes[1]} is not {@link RFC2217#SET_BAUDRATE} (client or server)
*/
public BaudRateCommand(int[] bytes) {
super("SET-BAUDRATE", SET_BAUDRATE, bytes);
this.baudRate = ((bytes[2] & 0xff) << 24) | ((bytes[3] & 0xff) << 16) | ((bytes[4] & 0xff) << 8) | (bytes[5] & 0xff);
}
/**
* Encoding constructor.
*
* @param baudRate baud rate
* @param client true for the client-to-server command, false for the server-to-client command
*/
public BaudRateCommand(boolean client, int baudRate) {
this(new int[] {
COM_PORT_OPTION,
client ? SET_BAUDRATE : SET_BAUDRATE + SERVER_OFFSET,
(baudRate >> 24) & 0xff,
(baudRate >> 16) & 0xff,
(baudRate >> 8) & 0xff,
baudRate & 0xff,
});
}
@Override
public String toString() {
return this.getName() + " " + this.baudRate;
}
@Override
public void visit(ComPortCommandSwitch sw) {
sw.caseBaudRate(this);
}
public int getBaudRate() {
return this.baudRate;
}
@Override
int getMinPayloadLength() {
return 4;
}
@Override
int getMaxPayloadLength() {
return 4;
}
}