From 7ba8ffa0a3352af0d6b62505f744fc4748b28620 Mon Sep 17 00:00:00 2001
From: Greg Albrecht <gba@onbeep.com>
Date: Fri, 13 Sep 2013 23:26:18 -0700
Subject: [PATCH] OMG KISS WORKS. DONT TOUCH ANYTHING.

---
 kiss/classes.py   | 12 ++++++++++++
 kiss/constants.py |  2 +-
 kiss/util.py      | 10 ++++++++++
 3 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/kiss/classes.py b/kiss/classes.py
index 55f9599..e6882f1 100644
--- a/kiss/classes.py
+++ b/kiss/classes.py
@@ -34,6 +34,17 @@ class KISS(object):
         self.speed = speed
         self.serial_int = None  # TODO Potentially very f*cking unsafe.
 
+    def __enter__(self):
+        return self
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        if self.serial_int and self.serial_int.isOpen():
+            self.serial_int.close()
+
+    def __del__(self):
+        if self.serial_int and self.serial_int.isOpen():
+            self.serial_int.close()
+
     def start(self):
         """
         Initializes the KISS device and commits configuration.
@@ -68,6 +79,7 @@ class KISS(object):
 
         :param callback: Callback to call with decoded data.
         """
+        self.logger.debug('callback=%s', callback)
         read_buffer = ''
         while 1:
             read_data = self.serial_int.read(kiss.constants.READ_BYTES)
diff --git a/kiss/constants.py b/kiss/constants.py
index 974e610..d0513ca 100644
--- a/kiss/constants.py
+++ b/kiss/constants.py
@@ -11,7 +11,7 @@ __license__ = 'Apache 2.0'
 import logging
 
 
-LOG_LEVEL = logging.INFO
+LOG_LEVEL = logging.DEBUG
 LOG_FORMAT = ('%(asctime)s %(levelname)s %(name)s.%(funcName)s:%(lineno)d'
               ' - %(message)s')
 
diff --git a/kiss/util.py b/kiss/util.py
index 5e24874..3ff7f11 100644
--- a/kiss/util.py
+++ b/kiss/util.py
@@ -39,3 +39,13 @@ def escape_special_codes(raw_codes):
         kiss.constants.FEND,
         kiss.constants.FESC_TFEND
     )
+
+
+def unescape_special_codes(codes):
+    return codes.replace(
+        chr(0xc0),
+        chr(0xdb) + chr(0xdc)
+    ).replace(
+        chr(0xc0),
+        chr(0xdb) + chr(0xdd)
+    )
-- 
GitLab