001/*
002 * Copyright (C) Cross The Road Electronics.  All rights reserved.
003 * License information can be found in CTRE_LICENSE.txt
004 * For support and suggestions contact support@ctr-electronics.com or file
005 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
006 */
007package com.ctre.phoenix6;
008
009import com.ctre.phoenix6.jni.CANBusJNI;
010
011/**
012 * Static class for getting information about available CAN buses.
013 */
014public class CANBus {
015    /**
016     * Contains status information about a CAN bus.
017     */
018    public static class CANBusStatus {
019        /**
020         * Status code response of getting the data
021         */
022        public StatusCode Status;
023
024        /**
025         * CAN bus utilization, from 0.0 to 1.0
026         */
027        public float BusUtilization;
028        /**
029         * Bus off count
030         */
031        public int BusOffCount;
032        /**
033         * Transmit buffer full count
034         */
035        public int TxFullCount;
036        /**
037         * Receive Error Counter (REC)
038         */
039        public int REC;
040        /**
041         * Transmit Error Counter (TEC)
042         */
043        public int TEC;
044    }
045
046    /**
047     * Gets whether the network is CAN FD.
048     * 
049     * @param canbus Name of the CAN bus
050     * @return True if the network is CAN FD
051     */
052    public static boolean isNetworkFD(String canbus) {
053        return CANBusJNI.JNI_IsNetworkFD(canbus);
054    }
055    /**
056     * Gets the status of the CAN bus, including the
057     * bus utilization and the error counters.
058     * <p>
059     * This function returns a new object every call.
060     * As a result, we recommend that this is not called
061     * inside a tight loop.
062     *
063     * @param canbus Name of the CAN bus
064     * @return Status of the CAN bus
065     */
066    public static CANBusStatus getStatus(String canbus) {
067        var jni = new CANBusJNI();
068        var err = jni.JNI_GetStatus(canbus);
069
070        var status = new CANBusStatus();
071        status.BusUtilization = jni.busUtilization;
072        status.BusOffCount = jni.busOffCount;
073        status.TxFullCount = jni.txFullCount;
074        status.REC = jni.rec;
075        status.TEC = jni.tec;
076
077        if (err != 0) {
078            status.Status = StatusCode.InvalidNetwork;
079        } else {
080            status.Status = StatusCode.OK;
081        }
082        return status;
083    }
084}