diff --git a/openplotterMaiana/data/file.png b/openplotterMaiana/data/file.png new file mode 100644 index 0000000..c730601 Binary files /dev/null and b/openplotterMaiana/data/file.png differ diff --git a/openplotterMaiana/fwupdate.py b/openplotterMaiana/fwupdate.py new file mode 100644 index 0000000..0d64a74 --- /dev/null +++ b/openplotterMaiana/fwupdate.py @@ -0,0 +1,137 @@ +#!/usr/bin/env python3 + +import serial +import sys +import os +import time +import struct +import binascii +import subprocess +import os +from openplotterSettings import conf +from openplotterSettings import language + +conf2 = conf.Conf() +currentdir = os.path.dirname(os.path.abspath(__file__)) +currentLanguage = conf2.get('GENERAL', 'lang') +package = 'openplotter-maiana' +language.Language(currentdir, package, currentLanguage) + +port = None +file = None +filesize = 0 +crc32 = 0 + +def is_unit_running(): + print(_("Checking if unit is running")) + s = port.readline().strip().decode('utf-8') + if len(s) > 0: + #print s + tokens = s.split(',') + if len(tokens) > 3: + return True + return False + +def drain_port(): + s = port.readline() + while len(s) > 0: + s = port.readline() + +def enable_dfu(): + if is_unit_running(): + print(_("Unit is running, switching to DFU mode")) + port.write(b'dfu\r\n') + drain_port() + + for x in range(5): + port.write(b'\r\n') + s = port.readline().strip() + if s.find(b"MAIANA bootloader") >= 0: + return True + + return False + + + +def begin_transfer(): + command = "load {0} {1:x}\r\n".format(filesize, crc32).encode('utf-8') + port.write(command) + + s = port.readline().strip() + return s == b"READY" + +def startSK(): + subprocess.call(['systemctl', 'start', 'signalk.socket']) + subprocess.call(['systemctl', 'start', 'signalk.service']) + +if __name__ == '__main__': + try: + if len(sys.argv) < 3: + print(_("Usage: {0} port imagefile".format(sys.argv[0]))) + sys.exit(1) + + subprocess.call(['systemctl', 'stop', 'signalk.service']) + subprocess.call(['systemctl', 'stop', 'signalk.socket']) + time.sleep(1) + + portname = sys.argv[1] + filename = sys.argv[2] + + port = serial.Serial(portname, 38400, timeout=2) + if not port.is_open: + print(_("Unable to open serial port")) + startSK() + sys.exit(2) + + file = None + + try: + file = open(filename, "rb") + except: + print(_("Unable to open file")+"{0}".format(filename)) + startSK() + sys.exit(2) + + filesize = os.stat(filename).st_size + #print filesize + data = file.read() + crc32 = (binascii.crc32(data) & 0xFFFFFFFF) + file.seek(0) + + print(_("File size:")+" {0}, CRC32: 0x{1:x}".format(filesize, crc32)) + + + if not enable_dfu(): + print (_("Could not get unit into DFU mode")) + startSK() + sys.exit(3) + + print(_("Unit is in DFU mode")) + + + if not begin_transfer(): + print(_("Unable to begin transfer, restart the unit and retry")) + startSK() + sys.exit(3) + + print(_("Starting transfer")) + + bytes = file.read(2048) + resp = '' + while len(bytes) > 0: + port.write(bytes) + #print "Sent {0} bytes".format(len(bytes)) + sys.stdout.write('.') + sys.stdout.flush() + resp = port.readline().strip() + if resp.find(b"OK") < 0: + break + #print s + bytes = file.read(2048) + + print() + print(resp.decode('utf-8')) + startSK() + except Exception as e: + print(_('FAILED: ')+str(e)) + startSK() \ No newline at end of file diff --git a/openplotterMaiana/openplotterMaiana.py b/openplotterMaiana/openplotterMaiana.py index 3b343ca..f1fedf9 100644 --- a/openplotterMaiana/openplotterMaiana.py +++ b/openplotterMaiana/openplotterMaiana.py @@ -201,8 +201,8 @@ class MyFrame(wx.Frame): ser.write('station?\r\n'.encode("utf-8")) ser.write('tx?\r\n'.encode("utf-8")) time.sleep(0.5) - resp = requests.get(self.platform.http+'localhost:'+self.platform.skPort+'/signalk/v1/api/vessels/self/MAIANA/', verify=False) try: + resp = requests.get(self.platform.http+'localhost:'+self.platform.skPort+'/signalk/v1/api/vessels/self/MAIANA/', verify=False) data = ujson.loads(resp.content) except: data = {} @@ -546,7 +546,9 @@ class MyFrame(wx.Frame): self.toolbar2 = wx.ToolBar(self.firmware, style=wx.TB_TEXT) toolRefresh = self.toolbar2.AddTool(201, _('Refresh'), wx.Bitmap(self.currentdir+"/data/refresh.png")) self.Bind(wx.EVT_TOOL, self.OnToolRefresh, toolRefresh) - + self.toolbar2.AddSeparator() + toolFile= self.toolbar2.AddTool(202, _('Update firmware'), wx.Bitmap(self.currentdir+"/data/file.png")) + self.Bind(wx.EVT_TOOL, self.OnToolFile, toolFile) self.logger = rt.RichTextCtrl(self.firmware, style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_DONTWRAP|wx.LC_SORT_ASCENDING) self.logger.SetMargins((10,10)) @@ -556,6 +558,32 @@ class MyFrame(wx.Frame): self.firmware.SetSizer(vbox) + def OnToolFile(self,e): + file_path = False + dlg = wx.FileDialog(self, message=_('Choose a file'), defaultDir='~', defaultFile='', wildcard=_('bin files') + ' (*.bin)|*.bin|' + _('All files') + ' (*.*)|*.*', style=wx.FD_OPEN | wx.FD_CHANGE_DIR) + if dlg.ShowModal() == wx.ID_OK: + file_path = dlg.GetPath() + dlg.Destroy() + if file_path: + dlg = wx.MessageDialog(None, _( + 'Your MAIANA device firmware will be updated, please do not disconnect or tamper with it during the update.\n\nDo you want to go ahead?'), + _('Question'), wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION) + if dlg.ShowModal() == wx.ID_YES: + self.logger.Clear() + self.logger.WriteText(_("Stopping Signal K server")) + self.logger.Newline() + command = self.platform.admin+' python3 '+self.currentdir+'/fwupdate.py '+self.device+' '+file_path + popen = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, shell=True) + for line in popen.stdout: + if not 'Warning' in line and not 'WARNING' in line: + self.logger.WriteText(line) + self.ShowStatusBarYELLOW(_('Updating firmware, please wait... ')+line) + self.logger.ShowPosition(self.logger.GetLastPosition()) + self.logger.WriteText(_("Starting Signal K server")) + self.SetStatusText('') + dlg.Destroy() + + ################################################################################ def main():