From 7c99bd226757b9fe74e63a9b835ff8840a6c9796 Mon Sep 17 00:00:00 2001
From: Paul McMillan <paul.mcmillan@nebula.com>
Date: Thu, 6 Mar 2014 14:15:24 +0000
Subject: [PATCH] Allow initial configuration of a KISS device

Make configuration work, and include a set of default sane values.
---
 kiss/classes.py   | 36 ++++++++++++++++++++++++------------
 kiss/constants.py | 18 ++++++++++++++++++
 2 files changed, 42 insertions(+), 12 deletions(-)

diff --git a/kiss/classes.py b/kiss/classes.py
index e19b3f9..2380ce2 100644
--- a/kiss/classes.py
+++ b/kiss/classes.py
@@ -45,34 +45,46 @@ class KISS(object):
         if self.serial_int and self.serial_int.isOpen():
             self.serial_int.close()
 
-    def start(self):
+    def start(self, **kwargs):
         """
         Initializes the KISS device and commits configuration.
+
+        See http://en.wikipedia.org/wiki/KISS_(TNC)#Command_codes
+        for configuration names.
+
+        :param **kwargs: name/value pairs to use as initial config values.
         """
         self.logger.debug('start()')
         self.serial_int = serial.Serial(self.port, self.speed)
         self.serial_int.timeout = kiss.constants.SERIAL_TIMEOUT
 
-        # TODO: Configuration section is incomplete - ampledata.
-        # http://en.wikipedia.org/wiki/KISS_(TNC)#Command_Codes
-        kiss_config = {}
-        for setting in ['TX_DELAY', 'PERSISTENCE', 'SLOT_TIME', 'TX_TAIL',
-                        'FULL_DUPLEX']:
-            if kiss_config.get(setting):
-                self.write_setting(kiss_config[setting])
+        # If no settings specified, default to config values similar
+        # to those that ship with xastir.
+        if not kwargs:
+            kwargs = kiss.constants.DEFAULT_KISS_CONFIG_VALUES
+
+        for name, value in kwargs.items():
+            self.write_setting(name, value)
 
-    def write_setting(self, setting):
+    def write_setting(self, name, value):
         """
         Writes KISS Command Codes to attached device.
 
         http://en.wikipedia.org/wiki/KISS_(TNC)#Command_Codes
 
-        :param setting: KISS Command Code to write.
+        :param name: KISS Command Code Name as a string.
+        :param value: KISS Command Code value to write.
         """
+        self.logger.debug('Configuring %s = %s', name, repr(value))
+
+        # Do the reasonable thing if a user passes an int
+        if type(value) == int:
+            value = chr(value)
+
         return self.serial_int.write(
             kiss.constants.FEND +
-            kiss.constants.FULL_DUPLEX +
-            kiss.util.escape_special_codes(setting) +
+            getattr(kiss.constants, name.upper()) +
+            kiss.util.escape_special_codes(value) +
             kiss.constants.FEND
         )
 
diff --git a/kiss/constants.py b/kiss/constants.py
index 7ee2e38..7bc24c9 100644
--- a/kiss/constants.py
+++ b/kiss/constants.py
@@ -41,3 +41,21 @@ TX_TAIL = chr(0x04)
 FULL_DUPLEX = chr(0x05)
 SET_HARDWARE = chr(0x06)
 RETURN = chr(0xFF)
+
+# Alternate convenience spellings for Command Codes
+# (these more closely match the names in the spec)
+DATAFRAME = DATA_FRAME
+TXDELAY = TX_DELAY
+P = PERSISTENCE
+SLOTTIME = SLOT_TIME
+TXTAIL = TX_TAIL
+FULLDUPLEX = FULL_DUPLEX
+SETHARDWARE = SET_HARDWARE
+
+DEFAULT_KISS_CONFIG_VALUES = {
+    'TX_DELAY': 40,
+    'PERSISTENCE': 63,
+    'SLOT_TIME': 20,
+    'TX_TAIL': 30,
+    'FULL_DUPLEX': 0,
+    }
-- 
GitLab