All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.yamcs.tctm.CcsdsSeqCountFiller Maven / Gradle / Ivy

There is a newer version: 5.10.9
Show newest version
package org.yamcs.tctm;

import java.util.HashMap;
import java.util.Map;

import org.yamcs.utils.ByteArrayUtils;

/**
 * Fills in the time, seq and checksum
 * 
 * @author nm
 *
 */
public class CcsdsSeqCountFiller {
    static Map seqCounts = new HashMap();

    /**
     * generate a new ccsds primary header sequence count for the given apid
     * 
     * @param apid
     * @return
     */
    private synchronized int getSeqCount(int apid) {
        int seqCount = 0;
        if (seqCounts.containsKey(apid)) {
            seqCount = seqCounts.get(apid);
        }
        seqCount = (seqCount + 1) % (1 << 14);
        seqCounts.put(apid, seqCount);
        return seqCount;
    }

    /**
     * generates a sequence count and fills it in
     * 
     * @param packet
     * @return  returns the generated sequence count
     */
    public int fill(byte[] packet) {
        int apidseqcount = ByteArrayUtils.decodeInt(packet, 0);
        
        int apid = (apidseqcount >> 16) & 0x07FF;
        int seqFlags = apidseqcount >>> 14;
        
        int seqCount = getSeqCount(apid);

        ByteArrayUtils.encodeUnsignedShort((short) ((seqFlags << 14) | seqCount), packet, 2);

        return seqCount;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy