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
010/**
011 * Checks the status of a CAN message and throws an exception of the appropriate type if necessary.
012 */
013public final class CANExceptionFactory {
014  // FRC Error codes
015  static final int ERR_CANSessionMux_InvalidBuffer = -44086;
016  static final int ERR_CANSessionMux_MessageNotFound = -44087;
017  static final int ERR_CANSessionMux_NotAllowed = -44088;
018  static final int ERR_CANSessionMux_NotInitialized = -44089;
019
020  /**
021   * Checks the status of a CAN message with the given message ID.
022   *
023   * @param status The CAN status.
024   * @param messageID The CAN message ID.
025   * @throws CANInvalidBufferException if the buffer is invalid.
026   * @throws CANMessageNotAllowedException if the message isn't allowed.
027   * @throws CANNotInitializedException if the CAN bus isn't initialized.
028   * @throws UncleanStatusException if the status code passed in reports an error.
029   */
030  public static void checkStatus(int status, int messageID) {
031    switch (status) {
032      case NIRioStatus.kRioStatusSuccess:
033        // Everything is ok... don't throw.
034        return;
035      case ERR_CANSessionMux_InvalidBuffer:
036      case NIRioStatus.kRIOStatusBufferInvalidSize:
037        throw new CANInvalidBufferException();
038      case ERR_CANSessionMux_MessageNotFound:
039      case NIRioStatus.kRIOStatusOperationTimedOut:
040        throw new CANMessageNotFoundException();
041      case ERR_CANSessionMux_NotAllowed:
042      case NIRioStatus.kRIOStatusFeatureNotSupported:
043        throw new CANMessageNotAllowedException("MessageID = " + messageID);
044      case ERR_CANSessionMux_NotInitialized:
045      case NIRioStatus.kRIOStatusResourceNotInitialized:
046        throw new CANNotInitializedException();
047      default:
048        throw new UncleanStatusException("Fatal status code detected:  " + status);
049    }
050  }
051
052  private CANExceptionFactory() {}
053}