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. Register radios and send text

Register radios and send text: ARS and TMS

On this page

This guide shows how a server reads the registration of a MOTOTRBO radio, answers it, and reads and sends text messages of the MOTOTRBO Text Messaging Service.

Goal

Act as the ARS and TMS server of a MOTOTRBO system: take each registration that a radio sends after power on, answer it, read the text messages that radios send, and send text with an acknowledgement.

What you need

ARS and TMS are UDP protocols. A radio sends each message in an IPv4 packet, and the IPv4 packet goes in a DMR data packet with the SAP IP based packet data. Motorola does not publish the layout of ARS or TMS. The packages follow open implementations and radio captures.

Run the examples from the top of the repository:

go test -C v2 -run Example -v ./mototrbo/ars ./mototrbo/tms

Steps

1. Read a registration and answer it

A radio sends a device registration to UDP port ip.PortARS when it powers on or changes channel, and again before its registration ends. ip.Decode takes the IPv4 and UDP headers off the user data of the packet. ars.DecodeMessage decodes the payload, and Message.DeviceID returns the radio ID.

ars.NewAck builds the answer with the refresh time, in units of 30 minutes. Send it back in an IPv4 packet with the addresses and ports of the registration swapped. ars.NewFailure builds a refusal, and ars.NewQuery requests that a radio register again.

The example first builds the registration as a radio does, with ars.NewRegistration and ip.AppendIPv4. ip.RadioAddress gives the IPv4 address of a radio ID.

func ExampleNewAck() {
    reg := ars.NewRegistration(ars.EventInitial)
    if _, err := reg.SetDeviceID(nil, 3002068); err != nil {
        fmt.Println(err)
        return
    }
    payload, _ := reg.AppendBinary(nil)
    radio := ip.RadioAddress(ip.DefaultNetwork, 3002068)
    server := ip.RadioAddress(ip.DefaultNetwork, 3002198)
    packet, _ := ip.AppendIPv4(nil, &ip.Datagram{Source: radio, Destination: server, SourcePort: ip.PortARS, DestinationPort: ip.PortARS, Payload: payload})

    d, err := ip.Decode(enums.SAPIPBasedPacketData, packet)
    if err != nil || d.DestinationPort != ip.PortARS {
        fmt.Println(d.DestinationPort, err)
        return
    }
    m, err := ars.DecodeMessage(d.Payload)
    if err != nil {
        fmt.Println(err)
        return
    }
    id, _ := m.DeviceID()
    fmt.Println(m.Header.Type, m.Registration.Event, "from", id, "at", ip.RadioID(d.Source))

    ack, _ := ars.NewAck(1)
    answer, _ := ack.AppendBinary(nil)
    reply, _ := ip.AppendIPv4(nil, &ip.Datagram{Source: d.Destination, Destination: d.Source, SourcePort: d.DestinationPort, DestinationPort: d.SourcePort, Payload: answer})
    fmt.Printf("answer %x in %d octets to %d\n", answer, len(reply), ip.RadioID(d.Source))
}

2. Read a text message

A text goes to UDP port ip.PortTMS. tms.DecodeMessage decodes it, and Message.TextUTF8 returns the text as UTF-8. The example reads a text that an AnyTone radio sent in its M-SMS mode.

When Header.AckRequired is set, answer with tms.NewAck and the sequence number of the text. The radio in the capture does not request an acknowledgement.

func ExampleDecodeMessage() {
    packet, _ := hex.DecodeString(appcapture.TMS)
    d, err := ip.Decode(enums.SAPIPBasedPacketData, packet)
    if err != nil || d.DestinationPort != ip.PortTMS {
        fmt.Println(d.DestinationPort, err)
        return
    }
    m, err := tms.DecodeMessage(d.Payload)
    if err != nil {
        fmt.Println(err)
        return
    }
    seq, _ := m.Sequence()
    fmt.Printf("%s %d from %d to %d: %q\n", m.Header.Type, seq, ip.RadioID(d.Source), ip.RadioID(d.Destination), m.TextUTF8(nil))
    if !m.Header.AckRequired {
        fmt.Println("no acknowledgement requested")
        return
    }
    ack, _ := tms.NewAck(seq)
    payload, _ := ack.AppendBinary(nil)
    reply, _ := ip.AppendIPv4(nil, &ip.Datagram{Source: d.Destination, Destination: d.Source, SourcePort: d.DestinationPort, DestinationPort: d.SourcePort, Payload: payload})
    fmt.Printf("ack %x in %d octets\n", payload, len(reply))
}

3. Send a text message

tms.NewText builds a text with a sequence number, and Message.SetTextUTF8 sets its text. Use a new sequence number for each text, so that the acknowledgement matches it. Put the encoded message in an IPv4 packet to port ip.PortTMS of the radio, as in step 1.

func ExampleNewText() {
    m, _ := tms.NewText(5, true)
    m.SetTextUTF8(nil, []byte("\r\nHello"))
    out, _ := m.AppendBinary(nil)
    fmt.Println(hex.EncodeToString(out))

    got, _ := tms.DecodeMessage(out)
    seq, _ := got.Sequence()
    ack, _ := tms.NewAck(seq)
    out, _ = ack.AppendBinary(nil)
    fmt.Printf("%q ack %x\n", got.TextUTF8(nil), out)
}

4. Carry the packets on a channel

Give the IPv4 packet to a data.Sender as the payload of a confirmed data message with the SAP IP based packet data and the radio ID of the radio as the destination LLID. On the receive side, a data.Receiver returns the payload of each data packet. Pass it to ip.Decode, and pick ARS or TMS by the destination port. The guide Send SMS and confirmed data with retries shows both machines.

To read text in every format that radios use, not only TMS, use sms.Decode. It calls tms.DecodeMessage for a packet to the TMS port.

Output

The registration and its answer:

Device Registration Initial from 3002068 at 3002068
answer 0002bf01 in 32 octets to 3002068

The text message from the capture:

Text 1 from 3009999 to 3002198: "\r\nMOTO TEST"
no acknowledgement requested

A text that requests an acknowledgement, and the acknowledgement:

0012e00085040d000a00480065006c006c006f00
"\r\nHello" ack 00039f0005

How it works

Each radio has an IPv4 address of the form network.x.y.z, where x.y.z is its 24-bit radio ID and the network ID is 12 unless the radio is set up with another one. A data packet with the SAP IP based packet data carries the IPv4 header, the UDP header and the payload. A packet with the SAP UDP/IP header compression carries a compressed header in place of both, and ip.Decode reads that form too. (ETSI TS 102 361-3 clauses 5.1, 5.5 and 5.6)

An ARS message starts with a 2-octet length, then one or two header octets: the PDU type, and for a registration the event, or for an answer the refresh time. A registration then holds the radio ID as decimal ASCII digits, the user identifier and the password. A TMS message has the same length field, a header octet with the PDU type and the acknowledgement request, an address field that is empty in radio to radio messages, two octets with the sequence number and the text encoding, and the text in UTF-16LE. Radios start the text with a CR LF. The packages do no I/O and allocate only to grow a slice of the caller.

The packets go on the channel as confirmed data. The state machines: data Sender and data Receiver. The csbk Preambles follows the preamble CSBKs that come before a data packet.

Edit this page on GitHub