001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.hal.can;
006
007import edu.wpi.first.hal.communication.NIRioStatus;
008import edu.wpi.first.hal.util.UncleanStatusException;
009
010public final class CANExceptionFactory {
011  // FRC Error codes
012  static final int ERR_CANSessionMux_InvalidBuffer = -44086;
013  static final int ERR_CANSessionMux_MessageNotFound = -44087;
014  static final int ERR_CANSessionMux_NotAllowed = -44088;
015  static final int ERR_CANSessionMux_NotInitialized = -44089;
016
017  /**
018   * Checks the status of a CAN message with the given message ID.
019   *
020   * @param status The CAN status.
021   * @param messageID The CAN message ID.
022   * @throws CANInvalidBufferException if the buffer is invalid.
023   * @throws CANMessageNotAllowedException if the message isn't allowed.
024   * @throws CANNotInitializedException if the CAN bus isn't initialized.
025   * @throws UncleanStatusException if the status code passed in reports an error.
026   */
027  public static void checkStatus(int status, int messageID) {
028    switch (status) {
029      case NIRioStatus.kRioStatusSuccess:
030        // Everything is ok... don't throw.
031        return;
032      case ERR_CANSessionMux_InvalidBuffer:
033      case NIRioStatus.kRIOStatusBufferInvalidSize:
034        throw new CANInvalidBufferException();
035      case ERR_CANSessionMux_MessageNotFound:
036      case NIRioStatus.kRIOStatusOperationTimedOut:
037        throw new CANMessageNotFoundException();
038      case ERR_CANSessionMux_NotAllowed:
039      case NIRioStatus.kRIOStatusFeatureNotSupported:
040        throw new CANMessageNotAllowedException("MessageID = " + messageID);
041      case ERR_CANSessionMux_NotInitialized:
042      case NIRioStatus.kRIOStatusResourceNotInitialized:
043        throw new CANNotInitializedException();
044      default:
045        throw new UncleanStatusException("Fatal status code detected:  " + status);
046    }
047  }
048
049  private CANExceptionFactory() {}
050}