1 package org.metricshub.ipmi.core.coding.commands.fru;
2
3 /*-
4 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5 * IPMI Java Client
6 * ჻჻჻჻჻჻
7 * Copyright 2023 Verax Systems, MetricsHub
8 * ჻჻჻჻჻჻
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Lesser Public License for more details.
18 *
19 * You should have received a copy of the GNU General Lesser Public
20 * License along with this program. If not, see
21 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
23 */
24
25 /**
26 * Represents unit which is used to access FRU
27 */
28 public enum BaseUnit {
29 /** The device is accessed by bytes. */
30 Bytes(BaseUnit.BYTES),
31 /** The device is accessed by 16-bit words: Read FRU Data takes its offset in words. */
32 Words(BaseUnit.WORDS),;
33
34 private static final int BYTES = 0;
35 private static final int WORDS = 1;
36 private static final int BYTESIZE = 1;
37 private static final int WORDSIZE = 2;
38
39 private int code;
40
41 BaseUnit(int code) {
42 this.code = code;
43 }
44
45 /**
46 * Returns the code of the unit.
47 *
48 * @return the access type bit of Get FRU Inventory Area Info: 0 for bytes, 1 for words
49 */
50 public int getCode() {
51 return code;
52 }
53
54 /**
55 * Returns the unit of an access type bit.
56 *
57 * @param value the access type bit of Get FRU Inventory Area Info
58 * @return the matching unit
59 * @throws IllegalArgumentException when the value is neither 0 nor 1
60 */
61 public static BaseUnit parseInt(int value) {
62 switch (value) {
63 case BYTES:
64 return Bytes;
65 case WORDS:
66 return Words;
67 default:
68 throw new IllegalArgumentException("Invalid value: " + value);
69 }
70 }
71
72 /**
73 * Returns the size of the unit in bytes.
74 *
75 * @return 1 for {@link #Bytes}, 2 for {@link #Words}
76 */
77 public int getSize() {
78 switch (this) {
79 case Bytes:
80 return BYTESIZE;
81 case Words:
82 return WORDSIZE;
83 default:
84 throw new IllegalArgumentException("Invalid value: " + this);
85 }
86 }
87 }