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.JNIWrapper;
008
009/**
010 * CAN API HAL JNI Functions.
011 *
012 * @see "hal/CAN.h"
013 */
014@SuppressWarnings("MethodName")
015public class CANJNI extends JNIWrapper {
016  /** Flag for sending a CAN message once. */
017  public static final int CAN_SEND_PERIOD_NO_REPEAT = 0;
018
019  /** Flag for stopping periodic CAN message sends. */
020  public static final int CAN_SEND_PERIOD_STOP_REPEATING = -1;
021
022  /** Mask for "is frame remote" in message ID. */
023  public static final int CAN_IS_FRAME_REMOTE = 0x40000000;
024
025  /** Mask for "is frame 11 bits" in message ID. */
026  public static final int CAN_IS_FRAME_11BIT = 0x80000000;
027
028  /** Default constructor. */
029  public CANJNI() {}
030
031  /**
032   * Sends a CAN message.
033   *
034   * @param busId The bus ID.
035   * @param messageId The ID of the CAN message.
036   * @param data the data to send.
037   * @param dataLength the length of data to send
038   * @param flags the message flags
039   * @param periodMs The period in milliseconds at which to send the message, use {@link
040   *     #CAN_SEND_PERIOD_NO_REPEAT} for a single send.
041   * @return send status, 0 on success.
042   */
043  public static native int sendMessage(
044      int busId, int messageId, byte[] data, int dataLength, int flags, int periodMs);
045
046  /**
047   * Receives a CAN message.
048   *
049   * @param busId The bus ID.
050   * @param messageId message id to look for.
051   * @param message The message.
052   * @return receive status, 0 on success.
053   */
054  public static native int receiveMessage(int busId, int messageId, CANReceiveMessage message);
055
056  /**
057   * Retrieves the current status of the CAN bus.
058   *
059   * @param busId The bus ID.
060   * @param status The CANStatus object to hold the retrieved status.
061   */
062  public static native void getCANStatus(int busId, CANStatus status);
063
064  /**
065   * Opens a new CAN stream session for receiving CAN messages with specified filters.
066   *
067   * @param busId The bus ID.
068   * @param messageId The CAN messageId to match against. The bits of the messageId are bitwise
069   *     ANDed with the messageIDMask.
070   * @param messageIDMask The CAN messageIDMask is a bit-wise mask of bits in the messageId to match
071   *     against. This allows matching against multiple frames. For example, providing an messageId
072   *     of 0x2050001 and a mask of 0x1FFF003F would match all REV motor controller frames for a
073   *     device with CAN ID 1. Providing a mask of 0x1FFFFFFF means that only the exact messageId
074   *     will be matched. Providing a mask of 0 would match any frame of any type.
075   * @param maxMessages The maximum number of messages that can be buffered in the session.
076   * @return The handle to the opened CAN stream session.
077   */
078  public static native int openCANStreamSession(
079      int busId, int messageId, int messageIDMask, int maxMessages);
080
081  /**
082   * Closes a CAN stream session.
083   *
084   * @param sessionHandle The handle of the CAN stream session to be closed.
085   */
086  public static native void closeCANStreamSession(int sessionHandle);
087
088  /**
089   * Reads messages from a CAN stream session.
090   *
091   * @param sessionHandle The handle of the CAN stream session.
092   * @param messages An array to hold the CANStreamMessage objects (output parameter).
093   * @param messagesToRead The number of messages to read from the session.
094   * @return The number of messages read into the buffer
095   * @throws CANStreamOverflowException If the number of messages to read exceeds the capacity of
096   *     the provided messages array.
097   */
098  public static native int readCANStreamSession(
099      int sessionHandle, CANStreamMessage[] messages, int messagesToRead)
100      throws CANStreamOverflowException;
101}