a Go library for reading iridium short data bursts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
5.1 KiB

package main
import (
"bytes"
"encoding/binary"
"fmt"
"io/ioutil"
"os"
"time"
)
type IridiumSDB struct {
ProtocolRevision byte
Length uint16
Elements []interface{}
}
type MOHeader struct {
IEI byte
Length uint16
CDRReference uint32
IMEI [15]byte
Status byte
MOMSN uint16
MTMSN uint16
TimeSession uint32
}
func (h MOHeader) SessionTime() time.Time {
return time.Unix(int64(h.TimeSession), 0)
}
func (h MOHeader) StatusString() string {
switch h.Status {
case 0:
return "The SBD session completed successfully."
case 1:
return "The MO message transfer, if any, was successful. The MT message queued at the Iridium Gateway is too large to be transferred within a single SBD session."
case 2:
return "The MO message transfer, if any, was successful. The reported location was determined to be of unacceptable quality. This value is only applicable to IMEIs using SBD protocol revision 1."
case 10:
return "The SBD session timed out before session completion."
case 12:
return "The MO message being transferred by the IMEI is too large to be transferred within a single SBD session."
case 13:
return "An RF link loss occurred during the SBD session."
case 14:
return "An IMEI protocol anomaly occurred during SBD session."
case 15:
return "The IMEI is prohibited from accessing the Iridium Gateway."
default:
return ""
}
}
type MOPayload struct {
IEI byte
Length uint16
Payload []byte
}
type MOLocation struct {
IEI byte
Length uint16
LocationFlags byte
LatitudeDegrees uint8
LatitudeTHMinute uint16
LongitudeDegrees uint8
LongitudeTHMinute uint16
CEPRadius uint32
}
type MOConfirmation struct {
IEI byte
Length uint16
Status byte
}
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr,
"required argument missing\n")
os.Exit(1)
}
raw, err := ioutil.ReadFile(os.Args[1])
if err != nil {
fmt.Fprintf(os.Stderr,
"error reading \"%s\": %s\n",
os.Args[1], err.Error())
}
err = parseSDB(raw)
if err != nil {
fmt.Fprintf(os.Stderr,
"%s\n", err.Error())
os.Exit(1)
}
}
func parseSDB(raw []byte) error {
raw_len := len(raw)
if raw_len < 3 {
return fmt.Errorf("message structure too short: %d bytes",
len(raw))
}
sdb := IridiumSDB{}
sdb.ProtocolRevision = raw[0]
sdb.Length = binary.BigEndian.Uint16(raw[1:3])
if (int(sdb.Length) + 3) != raw_len {
return fmt.Errorf("invalid message length (expected %d, got %d)",
(sdb.Length + 3), raw_len)
}
for i := 3; i < raw_len; {
if (i + 3) > raw_len {
return fmt.Errorf("invalid element length (at %d)",
i)
}
el_iei := raw[i]
el_len := binary.BigEndian.Uint16(raw[i+1 : i+3])
if (i + 3 + int(el_len)) > raw_len {
return fmt.Errorf("invalid element length (at %d): %d",
i+1, el_len)
}
switch el_iei {
// MO Header IEI
case 1:
buf := bytes.NewReader(raw[i : i+3+int(el_len)])
moheader := MOHeader{}
err := binary.Read(buf, binary.BigEndian, &moheader)
if err != nil {
return fmt.Errorf("error reading MO header (at %d): %s",
i, err.Error())
}
sdb.Elements = append(sdb.Elements, moheader)
// MO Payload IEI
case 2:
mopayload := MOPayload{}
mopayload.IEI = el_iei
mopayload.Length = el_len
mopayload.Payload = raw[i+3 : i+3+int(el_len)]
sdb.Elements = append(sdb.Elements, mopayload)
// MO Location IEI
case 3:
buf := bytes.NewReader(raw[i : i+3+int(el_len)])
molocation := MOLocation{}
err := binary.Read(buf, binary.BigEndian, &molocation)
if err != nil {
return fmt.Errorf("error reading MO location (at %d): %s",
i, err.Error())
}
sdb.Elements = append(sdb.Elements, molocation)
// MO Confirmation IEI
case 5:
moconfirmation := MOConfirmation{}
moconfirmation.IEI = el_iei
moconfirmation.Length = el_len
moconfirmation.Status = raw[i+3]
sdb.Elements = append(sdb.Elements, moconfirmation)
default:
return fmt.Errorf("invalid element IEI (at %d): %d",
i, el_iei)
}
i += (3 + int(el_len))
}
fmt.Printf("Protocol Revision: %d\n",
sdb.ProtocolRevision)
fmt.Printf("Message Length: %d\n",
sdb.Length)
for c, i := range sdb.Elements {
if c > 0 {
fmt.Println()
}
switch v := i.(type) {
case MOHeader:
fmt.Printf("\tMO Header IEI: %d\n",
v.IEI)
fmt.Printf("\tMO Header Length: %d\n",
v.Length)
fmt.Printf("\tMO Header CDR Reference: %d\n",
v.CDRReference)
fmt.Printf("\tMO Header IMEI: %s\n",
v.IMEI)
fmt.Printf("\tMO Header Status: %s (%d)\n",
v.StatusString(), v.Status)
fmt.Printf("\tMO Header MOMSN: %d\n",
v.MOMSN)
fmt.Printf("\tMO Header MTMSN: %d\n",
v.MTMSN)
fmt.Printf("\tMO Header Session Time: %s\n",
v.SessionTime().String())
case MOPayload:
fmt.Printf("\tMO Payload IEI: %d\n",
v.IEI)
fmt.Printf("\tMO Payload Length: %d\n",
v.Length)
case MOConfirmation:
fmt.Printf("\tMO Confirmation IEI: %d\n",
v.IEI)
fmt.Printf("\tMO Confirmation Length: %d\n",
v.Length)
fmt.Printf("\tMO Confirmation Status: %d\n",
v.Status)
}
}
return nil
}