Skip to content

Commit 2f360d5

Browse files
author
Brendan Whitfield
committed
added command validation function, check for mode 06 over non-CAN
1 parent eec3bb5 commit 2f360d5

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

‎obd/async.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ def watch(self, c, callback=None, force=False):
136136
logger.warning("Can't watch() while running, please use stop()")
137137
else:
138138

139-
if not force and not self.supports(c):
140-
logger.warning("'%s' is not supported" % str(c))
139+
if not force and not self.test_cmd(c):
140+
# self.test_cmd() will print warnings
141141
return
142142

143143
# new command being watched, store the command

‎obd/obd.py‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,24 @@ def supports(self, cmd):
215215
return cmd in self.supported_commands
216216

217217

218+
def test_cmd(self, cmd):
219+
"""
220+
Returns a boolean for whether a command will
221+
be sent without using force=True.
222+
"""
223+
# test if the command is supported
224+
if not self.supports(cmd):
225+
logger.warning("'%s' is not supported" % str(cmd))
226+
return False
227+
228+
# mode 06 is only implemented for the CAN protocols
229+
if cmd.mode == 6 and self.port.protocol_id() not in ["6", "7", "8", "9"]:
230+
logger.warning("Mode 06 commands are only supported over CAN protocols")
231+
return False
232+
233+
return True
234+
235+
218236
def query(self, cmd, force=False):
219237
"""
220238
primary API function. Sends commands to the car, and
@@ -225,11 +243,10 @@ def query(self, cmd, force=False):
225243
logger.warning("Query failed, no connection available")
226244
return OBDResponse()
227245

228-
if not force and not self.supports(cmd):
229-
logger.warning("'%s' is not supported" % str(cmd))
246+
# if the user forces, skip all checks
247+
if not force and not self.test_cmd(cmd):
230248
return OBDResponse()
231249

232-
233250
# send command and retrieve message
234251
logger.info("Sending command: %s" % str(cmd))
235252
cmd_string = self.__build_command_string(cmd)

0 commit comments

Comments
 (0)