2020-10-17 23:33:09 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2020-11-01 00:11:12 +01:00
|
|
|
"crypto/rand"
|
2020-10-23 23:41:56 +02:00
|
|
|
"encoding/binary"
|
2020-10-18 11:15:31 +02:00
|
|
|
"errors"
|
2020-11-03 14:08:14 +01:00
|
|
|
"fmt"
|
2020-11-03 22:10:51 +01:00
|
|
|
"os/exec"
|
2020-10-17 23:33:09 +02:00
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2020-10-26 10:33:24 +01:00
|
|
|
const controlStreamPort = 50001
|
|
|
|
|
const serialStreamPort = 50002
|
|
|
|
|
const audioStreamPort = 50003
|
|
|
|
|
|
2020-10-18 10:33:47 +02:00
|
|
|
type controlStream struct {
|
2020-10-23 14:00:59 +02:00
|
|
|
common streamCommon
|
|
|
|
|
serial serialStream
|
|
|
|
|
audio audioStream
|
|
|
|
|
|
|
|
|
|
deinitNeededChan chan bool
|
|
|
|
|
deinitFinishedChan chan bool
|
|
|
|
|
|
2020-10-18 13:02:41 +02:00
|
|
|
authInnerSendSeq uint16
|
|
|
|
|
authID [6]byte
|
2020-10-29 16:29:41 +01:00
|
|
|
gotAuthID bool
|
|
|
|
|
authOk bool
|
|
|
|
|
|
|
|
|
|
a8replyID [16]byte
|
|
|
|
|
gotA8ReplyID bool
|
2020-10-18 13:02:41 +02:00
|
|
|
|
2020-10-23 23:42:40 +02:00
|
|
|
serialAndAudioStreamOpened bool
|
|
|
|
|
deinitializing bool
|
2020-10-23 14:00:59 +02:00
|
|
|
|
2020-10-18 11:15:31 +02:00
|
|
|
requestSerialAndAudioTimeout *time.Timer
|
2020-10-26 11:32:21 +01:00
|
|
|
reauthTimeoutTimer *time.Timer
|
2020-10-17 23:33:09 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-23 16:20:09 +02:00
|
|
|
func (s *controlStream) sendPktLogin() error {
|
2020-10-23 22:52:14 +02:00
|
|
|
// The reply to the auth packet will contain a 6 bytes long auth ID with the first 2 bytes set to our ID.
|
2020-11-01 00:11:12 +01:00
|
|
|
var authStartID [2]byte
|
|
|
|
|
if _, err := rand.Read(authStartID[:]); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2020-10-25 20:18:24 +01:00
|
|
|
p := []byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2020-10-18 11:01:53 +02:00
|
|
|
byte(s.common.localSID >> 24), byte(s.common.localSID >> 16), byte(s.common.localSID >> 8), byte(s.common.localSID),
|
|
|
|
|
byte(s.common.remoteSID >> 24), byte(s.common.remoteSID >> 16), byte(s.common.remoteSID >> 8), byte(s.common.remoteSID),
|
|
|
|
|
0x00, 0x00, 0x00, 0x70, 0x01, 0x00, 0x00, byte(s.authInnerSendSeq),
|
2020-10-23 22:52:14 +02:00
|
|
|
byte(s.authInnerSendSeq >> 8), 0x00, authStartID[0], authStartID[1], 0x00, 0x00, 0x00, 0x00,
|
2020-10-17 23:33:09 +02:00
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
0x2b, 0x3f, 0x55, 0x5c, 0x00, 0x00, 0x00, 0x00, // username: beer
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
0x2b, 0x3f, 0x55, 0x5c, 0x3f, 0x25, 0x77, 0x58, // pass: beerbeer
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
0x69, 0x63, 0x6f, 0x6d, 0x2d, 0x70, 0x63, 0x00, // icom-pc in plain text
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2020-10-21 15:05:28 +02:00
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
2020-10-25 20:18:24 +01:00
|
|
|
if err := s.common.pkt0.sendTrackedPacket(&s.common, p); err != nil {
|
2020-10-23 14:00:59 +02:00
|
|
|
return err
|
|
|
|
|
}
|
2020-10-18 12:50:09 +02:00
|
|
|
|
2020-10-18 11:01:53 +02:00
|
|
|
s.authInnerSendSeq++
|
2020-10-23 14:00:59 +02:00
|
|
|
return nil
|
2020-10-17 23:33:09 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-23 23:18:37 +02:00
|
|
|
func (s *controlStream) sendPktAuth(magic byte) error {
|
2020-10-17 23:33:09 +02:00
|
|
|
// Example request from PC: 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0x00,
|
|
|
|
|
// 0xbb, 0x41, 0x3f, 0x2b, 0xe6, 0xb2, 0x7b, 0x7b,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x30, 0x01, 0x05, 0x00, 0x02,
|
|
|
|
|
// 0x00, 0x00, 0x5d, 0x37, 0x12, 0x82, 0x3b, 0xde,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
|
|
|
|
// Example reply from radio: 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00,
|
|
|
|
|
// 0xe6, 0xb2, 0x7b, 0x7b, 0xbb, 0x41, 0x3f, 0x2b,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x30, 0x02, 0x05, 0x00, 0x02,
|
|
|
|
|
// 0x00, 0x00, 0x5d, 0x37, 0x12, 0x82, 0x3b, 0xde,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
2020-10-25 10:53:46 +01:00
|
|
|
|
2020-10-25 20:18:24 +01:00
|
|
|
p := []byte{0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2020-10-18 11:01:53 +02:00
|
|
|
byte(s.common.localSID >> 24), byte(s.common.localSID >> 16), byte(s.common.localSID >> 8), byte(s.common.localSID),
|
|
|
|
|
byte(s.common.remoteSID >> 24), byte(s.common.remoteSID >> 16), byte(s.common.remoteSID >> 8), byte(s.common.remoteSID),
|
|
|
|
|
0x00, 0x00, 0x00, 0x30, 0x01, magic, 0x00, byte(s.authInnerSendSeq),
|
|
|
|
|
byte(s.authInnerSendSeq >> 8), 0x00, s.authID[0], s.authID[1], s.authID[2], s.authID[3], s.authID[4], s.authID[5],
|
2020-10-17 23:33:09 +02:00
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2020-10-21 15:05:28 +02:00
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
2020-10-25 20:18:24 +01:00
|
|
|
if err := s.common.pkt0.sendTrackedPacket(&s.common, p); err != nil {
|
2020-10-23 14:00:59 +02:00
|
|
|
return err
|
|
|
|
|
}
|
2020-10-18 11:01:53 +02:00
|
|
|
s.authInnerSendSeq++
|
2020-10-23 14:00:59 +02:00
|
|
|
return nil
|
2020-10-17 23:33:09 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-23 14:00:59 +02:00
|
|
|
func (s *controlStream) sendRequestSerialAndAudio() error {
|
2020-10-26 09:01:39 +01:00
|
|
|
log.Debug("requesting serial and audio stream")
|
2020-10-26 10:33:24 +01:00
|
|
|
|
2020-10-29 15:55:21 +01:00
|
|
|
txSeqBufLengthMs := uint16(txSeqBufLength.Milliseconds())
|
|
|
|
|
|
2020-10-25 20:18:24 +01:00
|
|
|
p := []byte{0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2020-10-18 11:01:53 +02:00
|
|
|
byte(s.common.localSID >> 24), byte(s.common.localSID >> 16), byte(s.common.localSID >> 8), byte(s.common.localSID),
|
|
|
|
|
byte(s.common.remoteSID >> 24), byte(s.common.remoteSID >> 16), byte(s.common.remoteSID >> 8), byte(s.common.remoteSID),
|
|
|
|
|
0x00, 0x00, 0x00, 0x80, 0x01, 0x03, 0x00, byte(s.authInnerSendSeq),
|
|
|
|
|
byte(s.authInnerSendSeq >> 8), 0x00, s.authID[0], s.authID[1], s.authID[2], s.authID[3], s.authID[4], s.authID[5],
|
2020-10-29 16:29:41 +01:00
|
|
|
s.a8replyID[0], s.a8replyID[1], s.a8replyID[2], s.a8replyID[3], s.a8replyID[4], s.a8replyID[5], s.a8replyID[6], s.a8replyID[7],
|
|
|
|
|
s.a8replyID[8], s.a8replyID[9], s.a8replyID[10], s.a8replyID[11], s.a8replyID[12], s.a8replyID[13], s.a8replyID[14], s.a8replyID[15],
|
2020-10-17 23:33:09 +02:00
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
0x49, 0x43, 0x2d, 0x37, 0x30, 0x35, 0x00, 0x00, // IC-705 in plain text
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
0x2b, 0x3f, 0x55, 0x5c, 0x00, 0x00, 0x00, 0x00, // username: beer
|
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2020-10-26 10:33:24 +01:00
|
|
|
0x01, 0x01, 0x04, 0x04, 0x00, 0x00, byte(audioSampleRate >> 8), byte(audioSampleRate & 0xff),
|
|
|
|
|
0x00, 0x00, byte(audioSampleRate >> 8), byte(audioSampleRate & 0xff),
|
|
|
|
|
0x00, 0x00, byte(serialStreamPort >> 8), byte(serialStreamPort & 0xff),
|
2020-10-29 15:55:21 +01:00
|
|
|
0x00, 0x00, byte(audioStreamPort >> 8), byte(audioStreamPort & 0xff), 0x00, 0x00,
|
|
|
|
|
byte(txSeqBufLengthMs >> 8), byte(txSeqBufLengthMs & 0xff), 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
2020-10-25 20:18:24 +01:00
|
|
|
if err := s.common.pkt0.sendTrackedPacket(&s.common, p); err != nil {
|
2020-10-23 14:00:59 +02:00
|
|
|
return err
|
|
|
|
|
}
|
2020-10-18 12:50:09 +02:00
|
|
|
|
2020-10-18 11:01:53 +02:00
|
|
|
s.authInnerSendSeq++
|
2020-10-18 11:15:31 +02:00
|
|
|
|
2020-10-23 14:00:59 +02:00
|
|
|
return nil
|
2020-10-17 23:33:09 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-29 16:29:41 +01:00
|
|
|
func (s *controlStream) sendRequestSerialAndAudioIfPossible() {
|
|
|
|
|
if !s.serialAndAudioStreamOpened && s.authOk && s.gotA8ReplyID {
|
2020-11-01 00:11:12 +01:00
|
|
|
if err := s.sendRequestSerialAndAudio(); err != nil {
|
|
|
|
|
reportError(err)
|
|
|
|
|
}
|
2020-10-29 16:29:41 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 14:00:59 +02:00
|
|
|
func (s *controlStream) handleRead(r []byte) error {
|
2020-10-18 10:53:16 +02:00
|
|
|
switch len(r) {
|
2020-10-29 16:29:41 +01:00
|
|
|
case 168:
|
|
|
|
|
if bytes.Equal(r[:6], []byte{0xa8, 0x00, 0x00, 0x00, 0x00, 0x00}) {
|
|
|
|
|
// Example answer from radio:
|
|
|
|
|
// 0xa8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,
|
|
|
|
|
// 0x01, 0x13, 0x11, 0x18, 0x38, 0xff, 0x55, 0x7d,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x98, 0x02, 0x02, 0x00, 0x07,
|
|
|
|
|
// 0x00, 0x00, 0x7f, 0x91, 0x00, 0x00, 0x4f, 0x0d,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x01, 0x93, 0x8a, 0x01, 0x24, 0x17, 0x64,
|
|
|
|
|
// 0xbc, 0x4b, 0xa3, 0xa0, 0x13, 0x58, 0x41, 0x04,
|
|
|
|
|
// 0x58, 0x2d, 0x49, 0x43, 0x2d, 0x37, 0x30, 0x35,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x49, 0x43, 0x4f, 0x4d, 0x5f, 0x56,
|
|
|
|
|
// 0x41, 0x55, 0x44, 0x49, 0x4f, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x3f, 0x3f, 0xa4, 0x01, 0xff, 0x01,
|
|
|
|
|
// 0xff, 0x01, 0x01, 0x01, 0x00, 0x00, 0x4b, 0x00,
|
|
|
|
|
// 0x01, 0x50, 0x00, 0xb8, 0x0b, 0x00, 0x00, 0x00
|
|
|
|
|
copy(s.a8replyID[:], r[66:82])
|
|
|
|
|
s.gotA8ReplyID = true
|
|
|
|
|
}
|
2020-10-23 16:20:09 +02:00
|
|
|
case 64:
|
|
|
|
|
if bytes.Equal(r[:6], []byte{0x40, 0x00, 0x00, 0x00, 0x00, 0x00}) {
|
|
|
|
|
// Example answer from radio: 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
|
|
|
|
|
// 0x33, 0x60, 0xd4, 0xe5, 0xf4, 0x67, 0x86, 0xe1,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x30, 0x02, 0x05, 0x00, 0x02,
|
|
|
|
|
// 0x00, 0x00, 0x35, 0x34, 0x76, 0x11, 0xb9, 0xd0,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
|
|
|
|
|
2020-10-26 11:32:21 +01:00
|
|
|
s.reauthTimeoutTimer.Stop()
|
|
|
|
|
|
2020-10-26 09:01:39 +01:00
|
|
|
log.Debug("auth ok")
|
2020-10-23 16:20:09 +02:00
|
|
|
|
2020-10-29 16:29:41 +01:00
|
|
|
if r[21] == 0x05 { // Answer for our second auth?
|
|
|
|
|
s.authOk = true
|
|
|
|
|
s.sendRequestSerialAndAudioIfPossible()
|
2020-10-23 14:00:59 +02:00
|
|
|
}
|
2020-10-17 23:33:09 +02:00
|
|
|
}
|
2020-10-18 10:53:16 +02:00
|
|
|
case 80:
|
2020-10-25 10:55:05 +01:00
|
|
|
if bytes.Equal(r[:6], []byte{0x50, 0x00, 0x00, 0x00, 0x00, 0x00}) {
|
2020-10-17 23:33:09 +02:00
|
|
|
// Example answer from radio: 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00,
|
|
|
|
|
// 0x86, 0x1f, 0x2f, 0xcc, 0x03, 0x03, 0x89, 0x29,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x40, 0x02, 0x03, 0x00, 0x52,
|
|
|
|
|
// 0x00, 0x00, 0xf8, 0xad, 0x06, 0x8d, 0xda, 0x7b,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
|
|
|
|
|
// 0x80, 0x00, 0x00, 0x90, 0xc7, 0x0e, 0x86, 0x01,
|
|
|
|
|
// 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
|
|
|
|
|
2020-10-25 10:55:05 +01:00
|
|
|
if bytes.Equal(r[48:51], []byte{0xff, 0xff, 0xff}) {
|
2020-10-28 22:13:29 +01:00
|
|
|
if !s.serialAndAudioStreamOpened {
|
|
|
|
|
return errors.New("auth failed, try rebooting the radio")
|
|
|
|
|
}
|
2020-10-30 10:04:23 +01:00
|
|
|
return errors.New("auth failed")
|
2020-10-25 10:55:05 +01:00
|
|
|
}
|
|
|
|
|
if bytes.Equal(r[48:51], []byte{0x00, 0x00, 0x00}) && r[64] == 0x01 {
|
|
|
|
|
return errors.New("got radio disconnected")
|
|
|
|
|
}
|
2020-10-17 23:33:09 +02:00
|
|
|
}
|
2020-10-18 10:53:16 +02:00
|
|
|
case 144:
|
2020-10-18 12:50:09 +02:00
|
|
|
if !s.serialAndAudioStreamOpened && bytes.Equal(r[:6], []byte{0x90, 0x00, 0x00, 0x00, 0x00, 0x00}) && r[96] == 1 {
|
2020-10-17 23:33:09 +02:00
|
|
|
// Example answer:
|
|
|
|
|
// 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x00,
|
|
|
|
|
// 0xc6, 0x5f, 0x6f, 0x0c, 0x5f, 0x8b, 0x1e, 0x89,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x80, 0x03, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x31, 0x30, 0x31, 0x47, 0x39, 0x07,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
|
|
|
|
|
// 0x80, 0x00, 0x00, 0x90, 0xc7, 0x0e, 0x86, 0x01,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2020-10-26 10:33:24 +01:00
|
|
|
// 0x49, 0x43, 0x2d, 0x37, 0x30, 0x35, 0x00, 0x00, // IC-705 in plain text
|
2020-10-17 23:33:09 +02:00
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x01, 0x00, 0x00, 0x00, 0x69, 0x63, 0x6f, 0x6d,
|
|
|
|
|
// 0x2d, 0x70, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x03, 0x03,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
2020-10-23 18:36:47 +02:00
|
|
|
|
2020-10-23 23:41:56 +02:00
|
|
|
s.requestSerialAndAudioTimeout.Stop()
|
|
|
|
|
|
2020-10-27 09:25:36 +01:00
|
|
|
devName := parseNullTerminatedString(r[64:])
|
2020-10-23 22:52:14 +02:00
|
|
|
log.Print("got serial and audio request success, device name: ", devName)
|
|
|
|
|
|
2020-10-23 23:41:56 +02:00
|
|
|
// Stuff can change in the meantime because of a previous login...
|
|
|
|
|
s.common.remoteSID = binary.BigEndian.Uint32(r[8:12])
|
|
|
|
|
s.common.localSID = binary.BigEndian.Uint32(r[12:16])
|
2020-10-23 22:52:14 +02:00
|
|
|
copy(s.authID[:], r[26:32])
|
2020-10-24 09:29:56 +02:00
|
|
|
s.gotAuthID = true
|
2020-10-23 14:00:59 +02:00
|
|
|
|
2020-10-26 09:13:30 +01:00
|
|
|
if err := s.serial.init(devName); err != nil {
|
2020-10-23 23:41:56 +02:00
|
|
|
return errors.New("serial/" + err.Error())
|
2020-10-23 14:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-26 09:13:30 +01:00
|
|
|
if err := s.audio.init(devName); err != nil {
|
2020-10-23 23:41:56 +02:00
|
|
|
return errors.New("audio/" + err.Error())
|
2020-10-23 14:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-18 12:50:09 +02:00
|
|
|
s.serialAndAudioStreamOpened = true
|
2020-10-28 10:15:13 +01:00
|
|
|
statusLog.startPeriodicPrint()
|
2020-10-29 20:28:17 +01:00
|
|
|
|
2020-11-03 14:08:14 +01:00
|
|
|
runCmdRunner.startIfNeeded(runCmd)
|
|
|
|
|
if enableSerialDevice {
|
|
|
|
|
serialCmdRunner.startIfNeeded(runCmdOnSerialPortCreated)
|
|
|
|
|
}
|
|
|
|
|
if !disableRigctld {
|
2020-11-03 22:10:51 +01:00
|
|
|
if _, err := exec.LookPath("rigctld"); err != nil {
|
|
|
|
|
log.Error("can't start rigctld: ", err)
|
|
|
|
|
} else {
|
|
|
|
|
rigctldRunner.startIfNeeded(fmt.Sprint("rigctld -m ", rigctldModel, " -r :", serialTCPPort))
|
|
|
|
|
}
|
2020-11-03 14:08:14 +01:00
|
|
|
}
|
2020-10-17 23:33:09 +02:00
|
|
|
}
|
2020-10-18 10:53:16 +02:00
|
|
|
}
|
2020-10-23 14:00:59 +02:00
|
|
|
return nil
|
2020-10-18 10:53:16 +02:00
|
|
|
}
|
2020-10-17 23:33:09 +02:00
|
|
|
|
2020-10-23 14:00:59 +02:00
|
|
|
func (s *controlStream) loop() {
|
2020-10-28 18:03:35 +01:00
|
|
|
netstat.reset()
|
2020-10-23 16:20:09 +02:00
|
|
|
|
2020-10-26 11:32:21 +01:00
|
|
|
s.reauthTimeoutTimer = time.NewTimer(0)
|
|
|
|
|
<-s.reauthTimeoutTimer.C
|
|
|
|
|
|
2020-10-26 11:33:42 +01:00
|
|
|
reauthTicker := time.NewTicker(25 * time.Second)
|
2020-10-23 14:00:59 +02:00
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
select {
|
2020-10-23 16:20:09 +02:00
|
|
|
case r := <-s.common.readChan:
|
|
|
|
|
if !s.deinitializing {
|
|
|
|
|
if err := s.handleRead(r); err != nil {
|
|
|
|
|
reportError(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-23 14:00:59 +02:00
|
|
|
case <-reauthTicker.C:
|
2020-10-26 09:01:39 +01:00
|
|
|
log.Debug("sending auth")
|
2020-10-26 11:32:21 +01:00
|
|
|
s.reauthTimeoutTimer.Reset(3 * time.Second)
|
2020-10-23 23:18:37 +02:00
|
|
|
if err := s.sendPktAuth(0x05); err != nil {
|
2020-10-23 14:00:59 +02:00
|
|
|
reportError(err)
|
|
|
|
|
}
|
2020-11-04 10:31:58 +01:00
|
|
|
if err := s.sendPktAuth(0x05); err != nil {
|
|
|
|
|
reportError(err)
|
|
|
|
|
}
|
|
|
|
|
if err := s.sendPktAuth(0x05); err != nil {
|
|
|
|
|
reportError(err)
|
|
|
|
|
}
|
2020-10-26 11:32:21 +01:00
|
|
|
case <-s.reauthTimeoutTimer.C:
|
|
|
|
|
log.Error("auth timeout, audio/serial stream may stop")
|
2020-10-23 14:00:59 +02:00
|
|
|
case <-s.deinitNeededChan:
|
|
|
|
|
s.deinitFinishedChan <- true
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-18 18:34:22 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-26 09:13:30 +01:00
|
|
|
func (s *controlStream) init() error {
|
|
|
|
|
log.Debug("init")
|
|
|
|
|
|
2020-10-26 10:33:24 +01:00
|
|
|
if err := s.common.init("control", controlStreamPort); err != nil {
|
2020-10-23 14:00:59 +02:00
|
|
|
return err
|
|
|
|
|
}
|
2020-10-18 10:53:16 +02:00
|
|
|
|
2020-10-26 08:56:30 +01:00
|
|
|
if err := s.common.start(); err != nil {
|
2020-10-23 14:00:59 +02:00
|
|
|
return err
|
|
|
|
|
}
|
2020-10-18 10:53:16 +02:00
|
|
|
|
2020-10-28 09:47:18 +01:00
|
|
|
s.common.pkt0.init(&s.common)
|
2020-10-23 16:20:09 +02:00
|
|
|
if err := s.sendPktLogin(); err != nil {
|
2020-10-23 14:00:59 +02:00
|
|
|
return err
|
|
|
|
|
}
|
2020-10-18 10:53:16 +02:00
|
|
|
|
2020-10-23 16:20:09 +02:00
|
|
|
log.Debug("expecting login answer")
|
2020-10-18 10:53:16 +02:00
|
|
|
// Example success auth packet: 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
|
|
|
|
|
// 0xe6, 0xb2, 0x7b, 0x7b, 0xbb, 0x41, 0x3f, 0x2b,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x50, 0x02, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x5d, 0x37, 0x12, 0x82, 0x3b, 0xde,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x46, 0x54, 0x54, 0x48, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
|
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
2020-10-23 14:00:59 +02:00
|
|
|
r, err := s.common.expect(96, []byte{0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2020-10-18 10:53:16 +02:00
|
|
|
if bytes.Equal(r[48:52], []byte{0xff, 0xff, 0xff, 0xfe}) {
|
2020-10-23 14:00:59 +02:00
|
|
|
return errors.New("invalid user/password")
|
2020-10-18 10:53:16 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-31 14:07:02 +01:00
|
|
|
s.common.pkt7.startPeriodicSend(&s.common, 2, false)
|
|
|
|
|
|
2020-10-18 11:01:53 +02:00
|
|
|
copy(s.authID[:], r[26:32])
|
2020-10-24 09:29:56 +02:00
|
|
|
s.gotAuthID = true
|
2020-10-23 23:18:37 +02:00
|
|
|
if err := s.sendPktAuth(0x02); err != nil {
|
2020-10-26 09:26:21 +01:00
|
|
|
return err
|
2020-10-23 16:20:09 +02:00
|
|
|
}
|
2020-10-26 09:01:39 +01:00
|
|
|
log.Debug("login ok, first auth sent...")
|
2020-10-18 10:53:16 +02:00
|
|
|
|
2020-10-28 09:47:18 +01:00
|
|
|
s.common.pkt0.startPeriodicSend(&s.common)
|
|
|
|
|
|
2020-11-01 00:11:12 +01:00
|
|
|
if err := s.sendPktAuth(0x05); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
log.Debug("second auth sent...")
|
|
|
|
|
|
2020-10-23 18:36:47 +02:00
|
|
|
s.requestSerialAndAudioTimeout = time.AfterFunc(5*time.Second, func() {
|
|
|
|
|
reportError(errors.New("login/serial/audio request timeout"))
|
|
|
|
|
})
|
|
|
|
|
|
2020-10-23 14:00:59 +02:00
|
|
|
s.deinitNeededChan = make(chan bool)
|
|
|
|
|
s.deinitFinishedChan = make(chan bool)
|
|
|
|
|
go s.loop()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2020-10-18 10:53:16 +02:00
|
|
|
|
2020-10-23 14:00:59 +02:00
|
|
|
func (s *controlStream) deinit() {
|
2020-10-23 16:20:09 +02:00
|
|
|
s.deinitializing = true
|
|
|
|
|
s.serialAndAudioStreamOpened = false
|
2020-10-28 10:15:13 +01:00
|
|
|
statusLog.stopPeriodicPrint()
|
2020-10-23 16:20:09 +02:00
|
|
|
|
2020-10-23 14:00:59 +02:00
|
|
|
if s.deinitNeededChan != nil {
|
|
|
|
|
s.deinitNeededChan <- true
|
|
|
|
|
<-s.deinitFinishedChan
|
|
|
|
|
}
|
|
|
|
|
if s.requestSerialAndAudioTimeout != nil {
|
|
|
|
|
s.requestSerialAndAudioTimeout.Stop()
|
|
|
|
|
s.requestSerialAndAudioTimeout = nil
|
2020-10-17 23:33:09 +02:00
|
|
|
}
|
2020-10-24 09:29:56 +02:00
|
|
|
|
|
|
|
|
if s.gotAuthID && s.common.gotRemoteSID && s.common.conn != nil {
|
2020-10-26 09:01:39 +01:00
|
|
|
log.Debug("sending deauth")
|
2020-10-24 09:29:56 +02:00
|
|
|
_ = s.sendPktAuth(0x01)
|
2020-10-29 09:20:55 +01:00
|
|
|
// Waiting a little bit to make sure the radio can send retransmit requests.
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2020-10-24 09:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-23 14:00:59 +02:00
|
|
|
s.common.deinit()
|
2020-10-23 18:36:47 +02:00
|
|
|
s.serial.deinit()
|
2020-10-25 15:39:41 +01:00
|
|
|
s.audio.deinit()
|
2020-10-17 23:33:09 +02:00
|
|
|
}
|