Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-lithionics.c
blob5bb8e231066794af9600922419b532ebd184c54f
1 /* packet-lithionics.c
2 * Routines for Lithionics NeverDie Battery Management System (BMS)
3 * By Michael Mann <Michael.Mann@jbtc.com>
4 * Copyright 2018 Michael Mann
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
12 * From https://lithionicsbattery.com/wp-content/uploads/2018/06/NeverDie-BMS-Advanced-RS232-UART-serial-protocol-Rev7.15.pdf
15 #include "config.h"
17 #include <epan/packet.h>
18 #include <epan/tfs.h>
19 #include <epan/unit_strings.h>
20 #include <wsutil/strtoi.h>
22 void proto_register_lithionics(void);
23 void proto_reg_handoff_lithionics(void);
25 static dissector_handle_t lithionics_handle;
27 static int proto_lithionics;
29 static int hf_lithionics_battery_address;
30 static int hf_lithionics_amp_hours_remain;
31 static int hf_lithionics_volts;
32 static int hf_lithionics_bat_gauge;
33 static int hf_lithionics_soc;
34 static int hf_lithionics_direction;
35 static int hf_lithionics_amps;
36 static int hf_lithionics_watts;
37 static int hf_lithionics_temperature;
38 static int hf_lithionics_system_status;
39 static int hf_lithionics_system_status_high_voltage_state;
40 static int hf_lithionics_system_status_charge_source_detected;
41 static int hf_lithionics_system_status_neverdie_reserve_state;
42 static int hf_lithionics_system_status_optoloop_cell_open;
43 static int hf_lithionics_system_status_reserve_voltage_range;
44 static int hf_lithionics_system_status_low_voltage_state;
45 static int hf_lithionics_system_status_battery_protection_state;
46 static int hf_lithionics_system_status_power_off_state;
47 static int hf_lithionics_system_status_aux_contacts_state;
48 static int hf_lithionics_system_status_aux_contacts_error;
49 static int hf_lithionics_system_status_precharge_error;
50 static int hf_lithionics_system_status_contactor_flutter;
51 static int hf_lithionics_system_status_ac_power_present;
52 static int hf_lithionics_system_status_tsm_charger_present;
53 static int hf_lithionics_system_status_tsm_charger_error;
54 static int hf_lithionics_system_status_external_temp_sensor_error;
55 static int hf_lithionics_system_status_agsr_state;
56 static int hf_lithionics_system_status_high_temperature_state;
57 static int hf_lithionics_system_status_low_temperature_state;
58 static int hf_lithionics_system_status_aux_input1_state;
59 static int hf_lithionics_system_status_charge_disable_state;
60 static int hf_lithionics_system_status_overcurrent_state;
61 static int hf_lithionics_system_status_reserved;
62 static int hf_lithionics_temination;
64 static int ett_lithionics;
65 static int ett_lithionics_system_status;
67 static int* const system_status_flags[] = {
68 &hf_lithionics_system_status_high_voltage_state,
69 &hf_lithionics_system_status_charge_source_detected,
70 &hf_lithionics_system_status_neverdie_reserve_state,
71 &hf_lithionics_system_status_optoloop_cell_open,
72 &hf_lithionics_system_status_reserve_voltage_range,
73 &hf_lithionics_system_status_low_voltage_state,
74 &hf_lithionics_system_status_battery_protection_state,
75 &hf_lithionics_system_status_power_off_state,
76 &hf_lithionics_system_status_aux_contacts_state,
77 &hf_lithionics_system_status_aux_contacts_error,
78 &hf_lithionics_system_status_precharge_error,
79 &hf_lithionics_system_status_contactor_flutter,
80 &hf_lithionics_system_status_ac_power_present,
81 &hf_lithionics_system_status_tsm_charger_present,
82 &hf_lithionics_system_status_tsm_charger_error,
83 &hf_lithionics_system_status_external_temp_sensor_error,
84 &hf_lithionics_system_status_agsr_state,
85 &hf_lithionics_system_status_high_temperature_state,
86 &hf_lithionics_system_status_low_temperature_state,
87 &hf_lithionics_system_status_aux_input1_state,
88 &hf_lithionics_system_status_charge_disable_state,
89 &hf_lithionics_system_status_overcurrent_state,
90 &hf_lithionics_system_status_reserved,
91 NULL
94 static const value_string lithionics_direction_vals[] = {
95 { 0, "Discharging" },
96 { 1, "Charging" },
97 { 0, NULL }
100 static const true_false_string tfs_lithionics_high_voltage_state = { "Above HVC", "Below HVC" };
101 static const true_false_string tfs_lithionics_reserve_voltage_range = { "Below RVC", "Above RVC" };
102 static const true_false_string tfs_lithionics_low_voltage_state = { "Below LVC", "Above LVC" };
103 static const true_false_string tfs_lithionics_battery_protection_state = { "Recovering from abnormal event", "Normal" };
104 static const true_false_string tfs_lithionics_power_off_state = { "Command", "Button" };
105 static const true_false_string tfs_lithionics_high_temperature_state = { "Exceeds allowed threshold", "Normal" };
106 static const true_false_string tfs_lithionics_low_temperature_state = { "Below allowed threshold", "Normal" };
108 static int
109 dissect_lithionics(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
111 proto_tree *lithionics_tree, *status_tree;
112 proto_item *ti;
113 int offset = 0;
114 char* str;
115 float f;
116 uint32_t value;
118 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Lithionics");
119 col_clear(pinfo->cinfo, COL_INFO);
121 ti = proto_tree_add_item(tree, proto_lithionics, tvb, 0, -1, ENC_NA);
122 lithionics_tree = proto_item_add_subtree(ti, ett_lithionics);
124 //just put the whole packet string (minus newlines) in the Info column
125 col_set_str(pinfo->cinfo, COL_INFO, (const char*)tvb_get_string_enc(pinfo->pool, tvb, offset, tvb_reported_length_remaining(tvb, offset)-2, ENC_ASCII));
127 str = (char*)tvb_get_string_enc(pinfo->pool, tvb, offset + 1, 1, ENC_ASCII);
128 if (!ws_strtou32(str, NULL, &value))
129 proto_tree_add_uint_format_value(lithionics_tree, hf_lithionics_battery_address, tvb, offset, 2, 0, "<Invalid value \"%s\">", str);
130 else
131 proto_tree_add_uint(lithionics_tree, hf_lithionics_battery_address, tvb, offset, 2, value);
132 offset += 2;
134 str = (char*)tvb_get_string_enc(pinfo->pool, tvb, offset + 1, 5, ENC_ASCII);
135 if (!ws_strtou32(str, NULL, &value))
136 proto_tree_add_float_format_value(lithionics_tree, hf_lithionics_amp_hours_remain, tvb, offset, 6, 0.0, "<Invalid value \"%s\">", str);
137 else {
138 f = (float)(value*.1);
139 proto_tree_add_float_format_value(lithionics_tree, hf_lithionics_amp_hours_remain, tvb, offset, 6, f, "%0.1fAh", f);
141 offset += 6;
143 str = (char*)tvb_get_string_enc(pinfo->pool, tvb, offset + 1, 4, ENC_ASCII);
144 if (!ws_strtou32(str, NULL, &value))
145 proto_tree_add_float_format_value(lithionics_tree, hf_lithionics_volts, tvb, offset, 5, 0.0, "<Invalid value \"%s\">", str);
146 else {
147 f = (float)(value*.1);
148 proto_tree_add_float_format_value(lithionics_tree, hf_lithionics_volts, tvb, offset, 5, f, "%0.1fV", f);
150 offset += 5;
152 str = (char*)tvb_get_string_enc(pinfo->pool, tvb, offset + 1, 3, ENC_ASCII);
153 if (!ws_strtou32(str, NULL, &value))
154 proto_tree_add_uint_format_value(lithionics_tree, hf_lithionics_bat_gauge, tvb, offset, 4, 0, "<Invalid value \"%s\">", str);
155 else
156 proto_tree_add_uint(lithionics_tree, hf_lithionics_bat_gauge, tvb, offset, 4, value);
157 offset += 4;
159 str = (char*)tvb_get_string_enc(pinfo->pool, tvb, offset + 1, 3, ENC_ASCII);
160 if (!ws_strtou32(str, NULL, &value))
161 proto_tree_add_uint_format_value(lithionics_tree, hf_lithionics_soc, tvb, offset, 4, 0, "<Invalid value \"%s\">", str);
162 else
163 proto_tree_add_uint(lithionics_tree, hf_lithionics_soc, tvb, offset, 4, value);
164 offset += 4;
166 str = (char*)tvb_get_string_enc(pinfo->pool, tvb, offset + 1, 1, ENC_ASCII);
167 if (!ws_strtou32(str, NULL, &value))
168 proto_tree_add_uint_format_value(lithionics_tree, hf_lithionics_direction, tvb, offset, 2, 0, "<Invalid value \"%s\">", str);
169 else
170 proto_tree_add_uint(lithionics_tree, hf_lithionics_direction, tvb, offset, 2, value);
171 offset += 2;
173 str = (char*)tvb_get_string_enc(pinfo->pool, tvb, offset + 1, 5, ENC_ASCII);
174 if (!ws_strtou32(str, NULL, &value))
175 proto_tree_add_float_format_value(lithionics_tree, hf_lithionics_amps, tvb, offset, 6, 0.0, "<Invalid value \"%s\">", str);
176 else {
177 f = (float)(value*.1);
178 proto_tree_add_float_format_value(lithionics_tree, hf_lithionics_amps, tvb, offset, 6, f, "%0.1fAmp", f);
180 offset += 6;
182 str = (char*)tvb_get_string_enc(pinfo->pool, tvb, offset + 1, 6, ENC_ASCII);
183 if (!ws_strtou32(str, NULL, &value))
184 proto_tree_add_uint_format_value(lithionics_tree, hf_lithionics_watts, tvb, offset, 7, 0, "<Invalid value \"%s\">", str);
185 else
186 proto_tree_add_uint(lithionics_tree, hf_lithionics_watts, tvb, offset, 7, value);
187 offset += 7;
189 str = (char*)tvb_get_string_enc(pinfo->pool, tvb, offset + 1, 3, ENC_ASCII);
190 if (!ws_strtou32(str, NULL, &value))
191 proto_tree_add_uint_format_value(lithionics_tree, hf_lithionics_temperature, tvb, offset, 4, 0, "<Invalid value \"%s\">", str);
192 else
193 proto_tree_add_uint(lithionics_tree, hf_lithionics_temperature, tvb, offset, 4, value);
194 offset += 4;
196 str = (char*)tvb_get_string_enc(pinfo->pool, tvb, offset + 1, 6, ENC_ASCII);
197 //do this over proto_tree_add_bitmask_value to get better field highlighting
198 if (!ws_hexstrtou32(str, NULL, &value))
199 proto_tree_add_uint_format_value(lithionics_tree, hf_lithionics_system_status, tvb, offset, 7, 0, "<Invalid value \"%s\">", str);
200 else {
201 ti = proto_tree_add_uint(lithionics_tree, hf_lithionics_system_status, tvb, offset, 7, value);
202 status_tree = proto_item_add_subtree(ti, ett_lithionics_system_status);
203 proto_tree_add_bitmask_list_value(status_tree, tvb, offset, 7, system_status_flags, value);
205 offset += 7;
207 proto_tree_add_item(lithionics_tree, hf_lithionics_temination, tvb, offset, 2, ENC_NA);
208 offset += 2;
210 return offset;
213 void
214 proto_register_lithionics(void)
217 static hf_register_info hf[] = {
218 { &hf_lithionics_battery_address,
219 { "Battery address", "lithionics_bms.battery_address", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL } },
220 { &hf_lithionics_amp_hours_remain,
221 { "Amp Hours Remaining", "lithionics_bms.amp_hours_remain", FT_FLOAT, BASE_NONE, NULL, 0x0, NULL, HFILL } },
222 { &hf_lithionics_volts,
223 { "Volts", "lithionics_bms.volts", FT_FLOAT, BASE_NONE, NULL, 0x0, NULL, HFILL } },
224 { &hf_lithionics_bat_gauge,
225 { "Bat gauge", "lithionics_bms.bat_gauge", FT_UINT32, BASE_DEC|BASE_UNIT_STRING, UNS(&units_percent), 0x0, NULL, HFILL } },
226 { &hf_lithionics_soc,
227 { "SoC", "lithionics_bms.soc", FT_UINT32, BASE_DEC|BASE_UNIT_STRING, UNS(&units_percent), 0x0, NULL, HFILL } },
228 { &hf_lithionics_direction,
229 { "Direction", "lithionics_bms.direction", FT_UINT16, BASE_DEC, VALS(lithionics_direction_vals), 0x0, NULL, HFILL } },
230 { &hf_lithionics_amps,
231 { "Amps", "lithionics_bms.amps", FT_FLOAT, BASE_NONE, NULL, 0x0, NULL, HFILL } },
232 { &hf_lithionics_watts,
233 { "Watts", "lithionics_bms.watts", FT_UINT32, BASE_DEC|BASE_UNIT_STRING, UNS(&units_watt), 0x0, NULL, HFILL } },
234 { &hf_lithionics_temperature,
235 { "Temperature", "lithionics_bms.temperature", FT_UINT32, BASE_DEC|BASE_UNIT_STRING, UNS(&units_degree_degrees), 0x0, NULL, HFILL } },
236 { &hf_lithionics_temination,
237 { "Newline Termination", "lithionics_bms.termination", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL } },
238 { &hf_lithionics_system_status,
239 { "System Status", "lithionics_bms.system_status", FT_UINT24, BASE_HEX, NULL, 0x0, NULL, HFILL } },
240 { &hf_lithionics_system_status_high_voltage_state,
241 { "High Voltage State", "lithionics_bms.system_status.high_voltage_state", FT_BOOLEAN, 24, TFS(&tfs_lithionics_high_voltage_state), 0x000001, NULL, HFILL } },
242 { &hf_lithionics_system_status_charge_source_detected,
243 { "Charge Source Detected", "lithionics_bms.system_status.charge_source_detected", FT_BOOLEAN, 24, NULL, 0x000002, NULL, HFILL } },
244 { &hf_lithionics_system_status_neverdie_reserve_state,
245 { "NeverDie Reserve State", "lithionics_bms.system_status.neverdie_reserve_state", FT_BOOLEAN, 24, NULL, 0x000004, NULL, HFILL } },
246 { &hf_lithionics_system_status_optoloop_cell_open,
247 { "OptoLoop Cell Loop is open", "lithionics_bms.system_status.optoloop_cell_open", FT_BOOLEAN, 24, NULL, 0x000008, NULL, HFILL } },
248 { &hf_lithionics_system_status_reserve_voltage_range,
249 { "Reserve Voltage Range", "lithionics_bms.system_status.reserve_voltage_range", FT_BOOLEAN, 24, TFS(&tfs_lithionics_reserve_voltage_range), 0x000010, NULL, HFILL } },
250 { &hf_lithionics_system_status_low_voltage_state,
251 { "Low Voltage State", "lithionics_bms.system_status.low_voltage_state", FT_BOOLEAN, 24, TFS(&tfs_lithionics_low_voltage_state), 0x000020, NULL, HFILL } },
252 { &hf_lithionics_system_status_battery_protection_state,
253 { "Battery Protection State", "lithionics_bms.system_status.battery_protection_state", FT_BOOLEAN, 24, TFS(&tfs_lithionics_battery_protection_state), 0x000040, NULL, HFILL } },
254 { &hf_lithionics_system_status_power_off_state,
255 { "Power Off State", "lithionics_bms.system_status.power_off_state", FT_BOOLEAN, 24, TFS(&tfs_lithionics_power_off_state), 0x000080, NULL, HFILL } },
256 { &hf_lithionics_system_status_aux_contacts_state,
257 { "AUX Contacts State", "lithionics_bms.system_status.aux_contacts_state", FT_BOOLEAN, 24, NULL, 0x000100, NULL, HFILL } },
258 { &hf_lithionics_system_status_aux_contacts_error,
259 { "AUX Contacts Error", "lithionics_bms.system_status.aux_contacts_error", FT_BOOLEAN, 24, NULL, 0x000200, NULL, HFILL } },
260 { &hf_lithionics_system_status_precharge_error,
261 { "Pre-charge Error", "lithionics_bms.system_status.precharge_error", FT_BOOLEAN, 24, NULL, 0x000400, NULL, HFILL } },
262 { &hf_lithionics_system_status_contactor_flutter,
263 { "Contactor Flutter", "lithionics_bms.system_status.contactor_flutter", FT_BOOLEAN, 24, NULL, 0x000800, NULL, HFILL } },
264 { &hf_lithionics_system_status_ac_power_present,
265 { "AC Power Present", "lithionics_bms.system_status.ac_power_present", FT_BOOLEAN, 24, NULL, 0x001000, NULL, HFILL } },
266 { &hf_lithionics_system_status_tsm_charger_present,
267 { "TSM Charger Present", "lithionics_bms.system_status.tsm_charger_present", FT_BOOLEAN, 24, NULL, 0x002000, NULL, HFILL } },
268 { &hf_lithionics_system_status_tsm_charger_error,
269 { "TSM Charger Error", "lithionics_bms.system_status.tsm_charger_error", FT_BOOLEAN, 24, NULL, 0x004000, NULL, HFILL } },
270 { &hf_lithionics_system_status_external_temp_sensor_error,
271 { "External Temp Sensor Error", "lithionics_bms.system_status.external_temp_sensor_error", FT_BOOLEAN, 24, NULL, 0x008000, NULL, HFILL } },
272 { &hf_lithionics_system_status_agsr_state,
273 { "AGSR State", "lithionics_bms.system_status.agsr_state", FT_BOOLEAN, 24, NULL, 0x010000, NULL, HFILL } },
274 { &hf_lithionics_system_status_high_temperature_state,
275 { "High Temperature State", "lithionics_bms.system_status.high_temperature_state", FT_BOOLEAN, 24, TFS(&tfs_lithionics_high_temperature_state), 0x020000, NULL, HFILL } },
276 { &hf_lithionics_system_status_low_temperature_state,
277 { "Low Temperature State", "lithionics_bms.system_status.low_temperature_state", FT_BOOLEAN, 24, TFS(&tfs_lithionics_low_temperature_state), 0x040000, NULL, HFILL } },
278 { &hf_lithionics_system_status_aux_input1_state,
279 { "Auxiliary Input 1 State", "lithionics_bms.system_status.aux_input1_state", FT_BOOLEAN, 24, NULL, 0x080000, NULL, HFILL } },
280 { &hf_lithionics_system_status_charge_disable_state,
281 { "Charge Disable State", "lithionics_bms.system_status.charge_disable_state", FT_BOOLEAN, 24, NULL, 0x100000, NULL, HFILL } },
282 { &hf_lithionics_system_status_overcurrent_state,
283 { "Overcurrent State", "lithionics_bms.system_status.overcurrent_state", FT_BOOLEAN, 24, NULL, 0x200000, NULL, HFILL } },
284 { &hf_lithionics_system_status_reserved,
285 { "Reserved", "lithionics_bms.system_status.reserved", FT_UINT24, BASE_HEX, NULL, 0xC00000, NULL, HFILL } },
289 static int *ett[] = {
290 &ett_lithionics,
291 &ett_lithionics_system_status,
294 proto_lithionics = proto_register_protocol("Lithionics Battery Management System", "Lithionics BMS", "lithionics_bms");
295 proto_register_field_array(proto_lithionics, hf, array_length(hf));
296 proto_register_subtree_array(ett, array_length(ett));
298 lithionics_handle = register_dissector("lithionics_bms", dissect_lithionics, proto_lithionics);
301 void
302 proto_reg_handoff_lithionics(void)
304 dissector_add_for_decode_as("udp.port", lithionics_handle);
308 * Editor modelines - http://www.wireshark.org/tools/modelines.html
310 * Local variables:
311 * c-basic-offset: 4
312 * tab-width: 8
313 * indent-tabs-mode: t
314 * End:
316 * vi: set shiftwidth=4 tabstop=8 expandtab:
317 * :indentSize=4:tabSize=8:noTabs=false: