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