These dmrgo docs are built from main and can describe changes that no release has yet. No release of dmrgo has these docs.

  1. DMRHub
  2. dmrgo
  3. Guides
  4. Use an MMDVM modem

Use an MMDVM modem

On this page

This guide shows how to talk to an MMDVM or MMDVM_HS modem over its serial port, as MMDVMHost does: read its version, send its config, switch it to DMR, and pass bursts in both directions.

Goal

Run the host side of the serial protocol of an MMDVM modem, so that your program receives the DMR bursts that the modem hears and sends bursts for it to transmit.

What you need

  • Go and a checkout of dmrgo. The example runs offline: a simulated modem in memory stands in for the serial port.
  • The package transport/mmdvm, and layer2 for the bursts.
  • For a real modem, a serial port package of your choice. Package mmdvm does no I/O: it builds and reads the octets that go over the port.

The serial protocol of a modem is not the MMDVM network protocol. The modem protocol links a host to its radio hardware; the network protocol links a host to a master, and the guide Connect to a network with the MMDVM network protocol or HBRP covers it.

Run the example from the top of the repository:

go test -C v2 -run '^Example$' -v ./transport/mmdvm

Steps

1. Send a frame to the modem

Every message on the port is a frame: a start octet, a length, a command and a payload. mmdvm.AppendFrame builds one around a payload. Write the frame to the serial port. The example gives the frame to its simulated modem in place of the port.

func toModem(fromModem []byte, c mmdvm.Command, payload []byte) ([]byte, error) {
    frame, err := mmdvm.AppendFrame(nil, c, payload)
    if err != nil {
        return fromModem, err
    }
    return modem(fromModem, frame), nil
}

2. Open the session

Request the version first. The reply gives the protocol version of the firmware: 1 for MMDVM_HS and 2 for MMDVM. mmdvm.DecodeVersion reads it, and Capabilities.Has shows whether the modem has DMR. Then send the config: ConfigV2 for protocol 2 and ConfigV1 for protocol 1. It enables the modes and sets the color code, the levels and the delays. SetMode switches the modem to DMR. The modem answers the config and the mode with an Ack, or with a Nak and a reason code.

func openSession() ([]byte, error) {
    fromModem, err := toModem(nil, mmdvm.CommandGetVersion, nil)
    if err != nil {
        return nil, err
    }
    cfg := mmdvm.ConfigV2{DMR: true, DMRColorCode: 7, State: mmdvm.ModeIdle, RXLevel: 50, DMRTXLevel: 50}
    payload, err := cfg.AppendBinary(nil)
    if err != nil {
        return nil, err
    }
    if fromModem, err = toModem(fromModem, mmdvm.CommandSetConfig, payload); err != nil {
        return nil, err
    }
    mode := mmdvm.SetMode{Mode: mmdvm.ModeDMR}
    if payload, err = mode.AppendBinary(nil); err != nil {
        return nil, err
    }
    return toModem(fromModem, mmdvm.CommandSetMode, payload)
}

3. Read the frames from the modem

Append the octets that you read from the port to a buffer, and call mmdvm.Next on it. NextFrame returns a whole frame and its length; drop that many octets. NextMore means the buffer ends inside a frame: read more. NextSkip gives the number of octets to drop that do not start a frame.

A DMR burst comes as a CommandDMRData1 or CommandDMRData2 frame. mmdvm.DecodeDMRData decodes it, and DMRData.Hint gives the layer2.Hint for the voice and data machines. A CommandDMRLost1 or CommandDMRLost2 frame shows that the modem lost the signal of the slot.

func printFrames(fromModem []byte) {
    for len(fromModem) > 0 {
        f, n, r := mmdvm.Next(fromModem)
        if r == mmdvm.NextMore {
            break
        }
        fromModem = fromModem[n:]
        if r == mmdvm.NextSkip {
            continue
        }
        switch f.Command {
        case mmdvm.CommandGetVersion:
            v, _ := mmdvm.DecodeVersion(f.Payload)
            fmt.Printf("version: protocol %d, DMR %v, %s\n", v.Protocol, v.Capabilities.Has(mmdvm.CapDMR), v.Description)
        case mmdvm.CommandAck:
            a, _ := mmdvm.DecodeAck(f.Payload)
            fmt.Println("ack:", a.Command)
        case mmdvm.CommandDMRData1, mmdvm.CommandDMRData2:
            d, err := mmdvm.DecodeDMRData(f)
            if err != nil {
                fmt.Println(err)
                continue
            }
            h := d.Hint()
            b, err := layer2.NewBurstFromBytes(d.Burst)
            if err != nil {
                fmt.Println(err)
                continue
            }
            fmt.Println("burst:", h.Slot, h.Kind, h.DataType, "color code", b.SlotType.ColorCode)
        default:
            fmt.Println("frame:", f.Command)
        }
    }
}

4. Send bursts to the modem

Put each burst that a machine returns in a DMRData. DMRData.SetHint sets the slot and the control octet from the Hint of the output, and DMRData.Command gives the command of the slot. Wrap the payload of DMRData.AppendBinary with AppendFrame. On a duplex modem, DMRStart starts and stops the transmitter, and DMRShortLC sets the Short LC of the CACH.

The simulated modem of the example builds its frames the same way:

func reply(out []byte, f mmdvm.Frame) []byte {
    switch f.Command {
    case mmdvm.CommandGetVersion:
        v := mmdvm.Version{Protocol: 2, Capabilities: mmdvm.CapDMR, CPU: mmdvm.CPUST, Description: []byte("MMDVM simulated")}
        payload, _ := v.AppendBinary(nil)
        out, _ = mmdvm.AppendFrame(out, mmdvm.CommandGetVersion, payload)
    case mmdvm.CommandSetConfig, mmdvm.CommandSetMode:
        a := mmdvm.Ack{Command: f.Command}
        payload, _ := a.AppendBinary(nil)
        out, _ = mmdvm.AppendFrame(out, mmdvm.CommandAck, payload)
        if f.Command == mmdvm.CommandSetMode {
            raw, _ := hex.DecodeString("21f40d58182015f8599012205c4d5d7f77fd757362300e202b9229044141c30391")
            d := mmdvm.DMRData{Slot: enums.Slot2, Control: mmdvm.ControlDataSync | mmdvm.Control(enums.DataTypeVoiceLCHeader)}
            copy(d.Burst[:], raw)
            payload, _ = d.AppendBinary(nil)
            out, _ = mmdvm.AppendFrame(out, d.Command(), payload)
        }
    default:
    }
    return out
}

Output

The version, the two Acks and the received burst:

version: protocol 2, DMR true, MMDVM simulated
ack: SetConfig
ack: SetMode
burst: TS2 Data Voice LC Header color code 7

The frames that MMDVMHost sends to a modem, from a capture:

GetStatus 0
DMRData2 34
TS2 MS-sourced data Voice LC Header

How it works

A frame starts with the octet 0xE0, then a length octet that counts the whole frame, then the command. A length of 0 is the long form, for frames of 256 octets or more: the next octet plus 255 is the length. A DMR data frame holds a control octet and the 33-octet burst, and from a modem built to send it, the RSSI after the burst. The control octet shows how the modem found the burst: a data SYNC with its data type, the voice SYNC of burst A, or the position of voice bursts B to F. MMDVMHost sends 0x00 toward the modem.

The decode functions never allocate and never panic. A field value that the firmware refuses, such as a color code above 15, gives a *dmrgo.FieldRangeError. (MMDVMHost 9384d68 Modem.cpp; MMDVM 046e1df SerialPort.cpp, DMRSlotRX.cpp, DMRIdleRX.cpp and DMRTX.cpp; MMDVM_HS bf66faf SerialPort.cpp)

The package encodes and decodes both roles, so a program can also act as the modem, as the example does. The bursts go to and from the service machines: bs Repeater, voice Receiver, voice Transmitter, data Receiver and data Sender.

Edit this page on GitHub