Implemented additional checks. Add force flag to flash firmware even if modem is in BIOS mode.
This commit is contained in:
@@ -14,22 +14,25 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// Size of a "firmware packet" over the serial line
|
||||
const CHUNKSIZE = 256
|
||||
|
||||
type modem struct {
|
||||
port serial.Port
|
||||
}
|
||||
|
||||
func initmodem(serport string, baudrate int) (*modem, error) {
|
||||
// initmodem: opens the serial line, returns instance of serial.Port
|
||||
func initmodem(serport string, baudrate int, sertimeout int) (*modem, error) {
|
||||
p, err := serial.Open(serport,
|
||||
serial.WithBaudrate(baudrate),
|
||||
serial.WithDataBits(8),
|
||||
serial.WithParity(serial.NoParity),
|
||||
serial.WithStopBits(serial.OneStopBit),
|
||||
serial.WithReadTimeout(70),
|
||||
serial.WithWriteTimeout(70)) // 50 was not stable, bump to 70
|
||||
serial.WithReadTimeout(sertimeout),
|
||||
serial.WithWriteTimeout(sertimeout))
|
||||
if err != nil {
|
||||
fmt.Printf("ERROR: File %s could not be opened\n", serport)
|
||||
return nil, err
|
||||
@@ -38,9 +41,12 @@ func initmodem(serport string, baudrate int) (*modem, error) {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// write: writes to the serial ports, returns answer and logs output
|
||||
func (m *modem) write(out string, nolog ...bool) ([]byte, error) {
|
||||
if len(nolog) == 0 {
|
||||
log.Debug("Sending " + out)
|
||||
log.Debug("Sending " + strings.TrimFunc(out, func(r rune) bool {
|
||||
return !unicode.IsGraphic(r)
|
||||
}))
|
||||
} else {
|
||||
if len(out) < 200 { // in other cases it's the firmware!
|
||||
log.Debugf("Sending %d byte(s)", len(out))
|
||||
@@ -67,6 +73,7 @@ func (m *modem) write(out string, nolog ...bool) ([]byte, error) {
|
||||
|
||||
}
|
||||
|
||||
// getSerNum: tries to find out the serial number of the modem.
|
||||
func (m *modem) getSerNum() (string, error) {
|
||||
answer, err := m.write("\rsys sern\r")
|
||||
if err != nil {
|
||||
@@ -77,6 +84,7 @@ func (m *modem) getSerNum() (string, error) {
|
||||
return sn, nil
|
||||
}
|
||||
|
||||
// setDateTime: sets the current date and time (taken from the host) to the modem
|
||||
func (m *modem) setDateTime() error {
|
||||
ct := time.Now()
|
||||
answer, err := m.write("DATE " + ct.Format("020106\r"))
|
||||
@@ -93,6 +101,7 @@ func (m *modem) setDateTime() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// writeUpdate: main flash utility. Writes the firmware taken from fwfile to the modem. Write progress bar.
|
||||
func (m *modem) writeUpdate(fwfile string) error {
|
||||
_, err := m.write("UPDATE\r")
|
||||
if err != nil {
|
||||
@@ -174,6 +183,7 @@ func (m *modem) writeUpdate(fwfile string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkFirmwareFits: checks the type of the modem and compares the file extension.
|
||||
func (m *modem) checkFirmwareFits(fwfile string) error {
|
||||
answer, err := m.write("ver ##\r")
|
||||
if err != nil {
|
||||
@@ -215,8 +225,9 @@ func (m *modem) checkFirmwareFits(fwfile string) error {
|
||||
|
||||
}
|
||||
|
||||
func Update(serport string, baudrate int, fwfile string) error {
|
||||
m, err := initmodem(serport, baudrate)
|
||||
// Update: main firmware update utility. Runs pre-checks and calls main flashing function writeUpdate
|
||||
func Update(serport string, baudrate int, sertimeout int, fwfile string, force bool) error {
|
||||
m, err := initmodem(serport, baudrate, sertimeout)
|
||||
defer m.port.Close()
|
||||
if err != nil {
|
||||
return errors.New("FEHLER in Update")
|
||||
@@ -232,8 +243,12 @@ func Update(serport string, baudrate int, fwfile string) error {
|
||||
if m.setDateTime() != nil {
|
||||
return err
|
||||
}
|
||||
if m.checkFirmwareFits(fwfile) != nil {
|
||||
return err
|
||||
if force == false {
|
||||
if m.checkFirmwareFits(fwfile) != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
log.Noticef("Skipping checks if firmware fits modem due to force flag!")
|
||||
}
|
||||
if m.writeUpdate(fwfile) != nil {
|
||||
return err
|
||||
|
Reference in New Issue
Block a user