mirror of https://github.com/Desuuuu/klipper.git
serialhdl: Update SendWithRetry to use reactor completions
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
233da97b19
commit
4f6982c4d0
|
@ -438,11 +438,10 @@ class CommandWrapper:
|
||||||
if minclock:
|
if minclock:
|
||||||
minsystime = self._clocksync.estimate_clock_systime(minclock)
|
minsystime = self._clocksync.estimate_clock_systime(minclock)
|
||||||
cmd = self._cmd.encode(data)
|
cmd = self._cmd.encode(data)
|
||||||
|
src = serialhdl.SerialRetryCommand(self._serial, response, response_oid)
|
||||||
try:
|
try:
|
||||||
src = serialhdl.SerialRetryCommand(
|
return src.get_response([cmd], self._cmd_queue,
|
||||||
self._serial, [cmd], self._cmd_queue, response, response_oid,
|
minclock=minclock, minsystime=minsystime)
|
||||||
minclock=minclock, minsystime=minsystime)
|
|
||||||
return src.get_response()
|
|
||||||
except serialhdl.error as e:
|
except serialhdl.error as e:
|
||||||
raise error(str(e))
|
raise error(str(e))
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# Serial port management for firmware communication
|
# Serial port management for firmware communication
|
||||||
#
|
#
|
||||||
# Copyright (C) 2016,2017 Kevin O'Connor <kevin@koconnor.net>
|
# Copyright (C) 2016-2019 Kevin O'Connor <kevin@koconnor.net>
|
||||||
#
|
#
|
||||||
# This file may be distributed under the terms of the GNU GPLv3 license.
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
||||||
import logging, threading
|
import logging, threading
|
||||||
|
@ -152,8 +152,8 @@ class SerialReader:
|
||||||
self.raw_send(cmd, minclock, reqclock, self.default_cmd_queue)
|
self.raw_send(cmd, minclock, reqclock, self.default_cmd_queue)
|
||||||
def send_with_response(self, msg, response):
|
def send_with_response(self, msg, response):
|
||||||
cmd = self.msgparser.create_command(msg)
|
cmd = self.msgparser.create_command(msg)
|
||||||
src = SerialRetryCommand(self, [cmd], self.default_cmd_queue, response)
|
src = SerialRetryCommand(self, response)
|
||||||
return src.get_response()
|
return src.get_response([cmd], self.default_cmd_queue)
|
||||||
def alloc_command_queue(self):
|
def alloc_command_queue(self):
|
||||||
return self.ffi_main.gc(self.ffi_lib.serialqueue_alloc_commandqueue(),
|
return self.ffi_main.gc(self.ffi_lib.serialqueue_alloc_commandqueue(),
|
||||||
self.ffi_lib.serialqueue_free_commandqueue)
|
self.ffi_lib.serialqueue_free_commandqueue)
|
||||||
|
@ -199,52 +199,30 @@ class SerialReader:
|
||||||
class SerialRetryCommand:
|
class SerialRetryCommand:
|
||||||
TIMEOUT_TIME = 5.0
|
TIMEOUT_TIME = 5.0
|
||||||
RETRY_TIME = 0.500
|
RETRY_TIME = 0.500
|
||||||
def __init__(self, serial, cmds, cmd_queue, name, oid=None,
|
def __init__(self, serial, name, oid=None):
|
||||||
minclock=0, minsystime=0.):
|
|
||||||
self.serial = serial
|
self.serial = serial
|
||||||
self.cmds = cmds
|
|
||||||
self.cmd_queue = cmd_queue
|
|
||||||
self.name = name
|
self.name = name
|
||||||
self.oid = oid
|
self.oid = oid
|
||||||
self.minclock = minclock
|
self.completion = serial.reactor.completion()
|
||||||
self.response = None
|
self.min_query_time = serial.reactor.monotonic()
|
||||||
reactor = self.serial.reactor
|
self.serial.register_response(self.handle_callback, name, oid)
|
||||||
self.mutex = reactor.mutex(is_locked=True)
|
|
||||||
self.min_query_time = self.serial.reactor.monotonic()
|
|
||||||
self.first_query_time = max(self.min_query_time, minsystime)
|
|
||||||
self.serial.register_response(self.handle_callback, self.name, self.oid)
|
|
||||||
self.send_event(self.min_query_time)
|
|
||||||
retry_time = self.first_query_time + self.RETRY_TIME
|
|
||||||
self.send_timer = reactor.register_timer(self.send_event, retry_time)
|
|
||||||
def unregister(self):
|
|
||||||
self.serial.register_response(None, self.name, self.oid)
|
|
||||||
self.serial.reactor.unregister_timer(self.send_timer)
|
|
||||||
def send_event(self, eventtime):
|
|
||||||
if self.response is not None:
|
|
||||||
return self.serial.reactor.NEVER
|
|
||||||
if eventtime > self.first_query_time + self.TIMEOUT_TIME:
|
|
||||||
self.unregister()
|
|
||||||
if self.response is None:
|
|
||||||
self.mutex.unlock()
|
|
||||||
return self.serial.reactor.NEVER
|
|
||||||
for cmd in self.cmds:
|
|
||||||
self.serial.raw_send(cmd, self.minclock, self.minclock,
|
|
||||||
self.cmd_queue)
|
|
||||||
return eventtime + self.RETRY_TIME
|
|
||||||
def handle_callback(self, params):
|
def handle_callback(self, params):
|
||||||
last_sent_time = params['#sent_time']
|
if params['#sent_time'] >= self.min_query_time:
|
||||||
if last_sent_time >= self.min_query_time and self.response is None:
|
self.min_query_time = self.serial.reactor.NEVER
|
||||||
self.response = params
|
self.serial.reactor.async_complete(self.completion, params)
|
||||||
self.serial.reactor.register_async_callback(self.do_wake)
|
def get_response(self, cmds, cmd_queue, minclock=0, minsystime=0.):
|
||||||
def do_wake(self, eventtime):
|
first_query_time = query_time = max(self.min_query_time, minsystime)
|
||||||
self.mutex.unlock()
|
while 1:
|
||||||
def get_response(self):
|
for cmd in cmds:
|
||||||
with self.mutex:
|
self.serial.raw_send(cmd, minclock, minclock, cmd_queue)
|
||||||
pass
|
params = self.completion.wait(query_time + self.RETRY_TIME)
|
||||||
if self.response is None:
|
if params is not None:
|
||||||
raise error("Timeout on wait for '%s' response" % (self.name,))
|
self.serial.register_response(None, self.name, self.oid)
|
||||||
self.unregister()
|
return params
|
||||||
return self.response
|
query_time = self.serial.reactor.monotonic()
|
||||||
|
if query_time > first_query_time + self.TIMEOUT_TIME:
|
||||||
|
self.serial.register_response(None, self.name, self.oid)
|
||||||
|
raise error("Timeout on wait for '%s' response" % (self.name,))
|
||||||
|
|
||||||
# Attempt to place an AVR stk500v2 style programmer into normal mode
|
# Attempt to place an AVR stk500v2 style programmer into normal mode
|
||||||
def stk500v2_leave(ser, reactor):
|
def stk500v2_leave(ser, reactor):
|
||||||
|
|
Loading…
Reference in New Issue