Code cleanup

This commit is contained in:
Christopher Ramey 2019-10-01 08:35:28 -08:00 committed by Christopher Ramey
parent 86d509ca74
commit d2f04934ee

148
sdb.go
View File

@ -9,6 +9,71 @@ import (
"time"
)
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())
}
sdb, err := parseSDB(raw)
if err != nil {
fmt.Fprintf(os.Stderr,
"%s\n", err.Error())
os.Exit(1)
}
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)
}
}
}
type IridiumSDB struct {
ProtocolRevision byte
Length uint16
@ -76,32 +141,10 @@ type MOConfirmation struct {
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 {
func parseSDB(raw []byte) (*IridiumSDB, error) {
raw_len := len(raw)
if raw_len < 3 {
return fmt.Errorf("message structure too short: %d bytes",
return nil, fmt.Errorf("message structure too short: %d bytes",
len(raw))
}
@ -110,20 +153,20 @@ func parseSDB(raw []byte) error {
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)",
return nil, 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)",
return nil, 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",
return nil, fmt.Errorf("invalid element length (at %d): %d",
i+1, el_len)
}
@ -134,7 +177,7 @@ func parseSDB(raw []byte) error {
moheader := MOHeader{}
err := binary.Read(buf, binary.BigEndian, &moheader)
if err != nil {
return fmt.Errorf("error reading MO header (at %d): %s",
return nil, fmt.Errorf("error reading MO header (at %d): %s",
i, err.Error())
}
sdb.Elements = append(sdb.Elements, moheader)
@ -153,7 +196,7 @@ func parseSDB(raw []byte) error {
molocation := MOLocation{}
err := binary.Read(buf, binary.BigEndian, &molocation)
if err != nil {
return fmt.Errorf("error reading MO location (at %d): %s",
return nil, fmt.Errorf("error reading MO location (at %d): %s",
i, err.Error())
}
sdb.Elements = append(sdb.Elements, molocation)
@ -167,55 +210,12 @@ func parseSDB(raw []byte) error {
sdb.Elements = append(sdb.Elements, moconfirmation)
default:
return fmt.Errorf("invalid element IEI (at %d): %d",
return nil, 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
return &sdb, nil
}