Lte3000MessageDecoder.java 3.96 KB
package com.example.mina.box.lte3000;

import com.example.mina.util.CommandHelper;
import com.example.mina.util.Lte3000CommandHelper;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;


public class Lte3000MessageDecoder implements ProtocolDecoder {

    private static final byte[] Response_46;// = "Fv7.46 Pv2.15 LTE2250/032X032".getBytes();
    private static final byte[] Response_53 = new byte[]{0x06, 0x30, 0x30, 0x53, 0x03, 0x56};
    private static final byte[] Response_58 = new byte[]{0x06, 0x30, 0x30, 0x58, 0x47, 0x4D, 0x4F, 0x03, 0x18};

    static {
        byte[] bs = "Fv7.46 Pv2.15 XRM2250/032X064".getBytes();
        Response_46 = new byte[bs.length + 6];
        System.arraycopy(bs, 0, Response_46, 4, bs.length);
        Response_46[0] = 0x06;
        Response_46[1] = 0x30;
        Response_46[2] = 0x30;
        Response_46[3] = 0x46;
        Response_46[bs.length + 4] = 0x03;
        Lte3000CommandHelper.setCHK(Response_46);
    }

    private int getBcdPort(byte a, byte b, byte c) {
        return (CommandHelper.BCD_REV[a] - 30) * 100 + (CommandHelper.BCD_REV[b] - 30) * 10 + (CommandHelper.BCD_REV[c] - 30);
    }
    private int getBcdAttn(byte a, byte b) {
        return (CommandHelper.BCD_REV[a] - 30) * 10 + (CommandHelper.BCD_REV[b] - 30);
    }

    
    
    @Override
    public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
        //byte[] bytes = in.array();
        byte[] bytes = new byte[in.limit()];
        int len = bytes.length;
        in.get(bytes);
        String type = null;
        Lte3000RequestMessage requestMessage = Lte3000RequestMessage.builder()
                .cmd(new String(bytes))
                .bytes(bytes)
                .len(len)
                .response46(Response_46)
                .response53(Response_53)
                .response58(Response_58)
                .build();

        //F: Firmware Version / Unit ID         P.82
        //0x46
        if (bytes[3] == 'F') {
            requestMessage.setType("Firmware Version");
        }

        //O:Query Output Channel             P.106
        //0x4F
        if (bytes[3] == 'O') {
            int col = this.getBcdPort(bytes[4], bytes[5], bytes[6]);
            int row = 0;
            requestMessage.setRow(row);
            requestMessage.setCol(col);
            requestMessage.setType("Query Output Channel");

        }

        //S: Set Crosspoint             P.115
        //0x53
        if (bytes[3] == 'S') {
            int row = this.getBcdPort(bytes[7], bytes[8], bytes[9]);
            int col = this.getBcdPort(bytes[4], bytes[5], bytes[6]);
            requestMessage.setRow(row);
            requestMessage.setCol(col);
            requestMessage.setType("Set Crosspoint");
        }

        //XGMO: Set Gain Control to Manual Mode - Output p.137
        //0x58 0x47 0x4D 0x4F
        if (bytes[3] == 'X' && bytes[4] == 'G' && bytes[5] == 'M' && bytes[6] == 'O' ) {
            int col = this.getBcdPort(bytes[7], bytes[8], bytes[9]);
            int attn = this.getBcdAttn(bytes[12], bytes[13]);
            if (bytes[11] == 0x2D) {
                
            }
            requestMessage.setAttn(attn);
            requestMessage.setCol(col);
            requestMessage.setType("Set Gain Control to Manual Mode");
        }

        //XGRO: Gain Control Read Status - Output P.139
        if (bytes[3] == 'X' && bytes[4] == 'G' && bytes[5] == 'R' && bytes[6] == 'O' ) {
            int col = this.getBcdPort(bytes[7], bytes[8], bytes[9]);
            requestMessage.setCol(col);
            requestMessage.setType("Gain Control Read Status");
        }
        out.write(requestMessage);
    }

    @Override
    public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws Exception {

    }

    @Override
    public void dispose(IoSession session) throws Exception {

    }
}