001package jmri.jmrix.can;
002
003/**
004 * Base interface for mutable messages in a CANbus based message/reply protocol.
005 * <p>
006 * It is expected that any CAN based system will be based upon basic CAN
007 * concepts such as ID/header (standard or extended), Normal and RTR frames and
008 * a data field.
009 * <p>
010 * "header" refers to the full 11 or 29 bit header; which mode is separately set
011 * via the "extended" parameter
012 *
013 * @author Bob Jacobsen Copyright (C) 2008, 2009, 2010
014 */
015public interface CanMutableFrame extends CanFrame {
016
017    /**
018     * Set the CAN Frame header.
019     * @param h new header value
020     */
021    void setHeader(int h);
022
023    /**
024     * Set if the CAN Frame header is extended.
025     * @param b true if extended, else false
026     */
027    void setExtended(boolean b);
028
029    /**
030     * Set if the CAN Frame is an RTR Frame.
031     * @param b true if RTR, else false
032     */
033    void setRtr(boolean b);
034
035    /**
036     * Set the number of data elements in the main CAN Frame body.
037     * @param n true number of data bytes, 0-8
038     */
039    void setNumDataElements(int n);
040
041    /**
042     * Get a single data byte in the frame.
043     * @param n the index, 0-7
044     * @param v the new value, 0-255
045     */
046    void setElement(int n, int v);
047    
048    /**
049     * Set the CAN Frame data elements by int array.
050     *
051     * @param d array of CAN Frame data bytes, max 8
052     */
053    default void setData(int[] d) {
054        int len = (d.length <= 8) ? d.length : 8;
055        for (int i = 0; i < len; i++) {
056            setElement(i,d[i]);
057        }
058    }
059    
060    /**
061     * Set the CAN Frame data elements by byte array.
062     *
063     * @param d array of CAN Frame data bytes, max 8
064     */
065    default void setData(byte[] d) {
066        int len = (d.length <= 8) ? d.length : 8;
067        for (int i = 0; i < len; i++) {
068            setElement(i,d[i] & 0xFF);
069        }
070    }
071
072}