# HG changeset patch
# User Pavel Milanes (CO7WT) <co7wt@frcuba.co.cu>
# Date 1459198221 14400
#      Mon Mar 28 16:50:21 2016 -0400
# Node ID ac18b334d894df422cc27b9ea72d1d126add15a4
# Parent  4ac0e44caa6574ce43f2be6d7e5cf7a0f87fc104
[PATCH][BTECH] Simplified second ID proc, add QYT KT-8900, fixes #3015

- Simplified the procedure for the second ID of some radios in preparation
  to add the QYT KT-8900 radio.
- Added support for the QYT KT-8900.
- Fixing the variable naming for the QYT KT-UV980.

Also related to issue #2673 (support for the QYT KT-8900)

diff -r 4ac0e44caa65 -r ac18b334d894 chirp/drivers/btech.py
--- a/chirp/drivers/btech.py	Mon Mar 28 02:07:53 2016 -0400
+++ b/chirp/drivers/btech.py	Mon Mar 28 16:50:21 2016 -0400
@@ -161,18 +161,28 @@
 MINI8900_fp = "M28854"
 
 
-# QYT KT UV-890
-KTUV890_fp = "H28854"
+# QYT KT-UV980
+KTUV980_fp = "H28854"
+
+
+# QYT KT-8900
+KT8900_fp = "M29154"
+# this radio has an extra ID
+KT8900_id = "      303688"
+
 
 #### MAGICS
 # for the Waccom Mini-8900
 MSTRING_MINI8900 = "\x55\xA5\xB5\x45\x55\x45\x4d\x02"
 # for the B-TECH UV-2501+220 (including pre production ones)
 MSTRING_220 = "\x55\x20\x15\x12\x12\x01\x4d\x02"
+# for the QYT KT-8900
+MSTRING_KT8900 = "\x55\x20\x15\x09\x16\x45\x4D\x02"
 # magic string for all other models
 MSTRING = "\x55\x20\x15\x09\x20\x45\x4d\x02"
 
 
+
 def _clean_buffer(radio):
     """Cleaning the read serial buffer, hard timeout to survive an infinite
     data stream"""
@@ -351,10 +361,10 @@
 
     LOG.info("Positive ident, this is a %s" % radio.MODEL)
 
-    # the 2501+220 has one more check:
+    # the BTECH 2501+220 and the QYT KT-8900 have one more check:
     # reading the block 0x3DF0 to see if it's a code inside
-    if "+220" in radio.MODEL:
-        LOG.debug("This is a BTECH UV-2501+220, requesting the extra ID")
+    if "+220" in radio.MODEL or "KT-8900" in radio.MODEL:
+        LOG.debug("Requesting the extra ID")
         # send the read request
         _send(radio, _make_frame("S", 0x3DF0, 16))
         id2 = _rawrecv(radio, 20)
@@ -363,22 +373,16 @@
         # it seems that it's padded with \xff, \x20 and some times with \x00
         # we just care about the first 16, our magic string is in there
         if len(id2) < 16:
-            raise errors.RadioError("The extra UV-2501+220 ID is short.")
+            raise errors.RadioError("The extra ID is short.")
 
-        # ok, check for it, any of the correct ID must be in the received data
-        itis = False
-        for eid in radio._id2:
-            if eid in id2:
-                LOG.info("Confirmed, this is a BTECH UV-2501+220")
-                # set the flag and exit
-                itis = True
-                break
-
-        # It is a UV-2501+220?
-        if itis is False:
-            LOG.debug("Full *BAD* extra ID on the 2501+220 is: \n%s" %
-                      util.hexprint(id2))
-            raise errors.RadioError("The extra ident is wrong, aborting.")
+        # ok, the correct string must be in the received data
+        if radio._id2 in id2:
+            LOG.info("Confirmed, this is a %s %s" %
+                     (radio.VENDOR, radio.MODEL))
+        else:
+            LOG.debug("Full *BAD* extra ID on the %s is: \n%s" %
+                      (radio.MODEL, util.hexprint(id2)))
+            raise errors.RadioError("The extra ID is wrong, aborting.")
 
     return True
 
@@ -392,8 +396,9 @@
     # put radio in program mode and identify it
     _do_ident(radio, status)
 
-    # the first dummy packet for all model but the 2501+220
-    if not "+220" in radio.MODEL:
+    # the first dummy packet for all model but the
+    # BTECH 2501+220 & QYT KT-8900
+    if not ("+220" in radio.MODEL or "KT-8900" in radio.MODEL):
         # In the logs we have found that the first block is discarded
         # this is the \x05 in ack one, so we will simulate it here
         _send(radio, _make_frame("S", 0, BLOCK_SIZE))
@@ -401,7 +406,7 @@
 
         if debug is True:
             LOG.info("Dummy first block read done, got this:\n\n")
-            LOG.debug(util.hexprint(discard))
+            LOG.debug("\n" + util.hexprint(discard))
 
     # reset the progress bar in the UI
     status.max = MEM_SIZE / BLOCK_SIZE
@@ -916,7 +921,7 @@
     MODEL = "UV-2501+220"
     _magic = MSTRING_220
     _fileid = [UV2501_220_fp, UV2501_220pp_fp]
-    _id2 = [UV2501_220pp_id, ]
+    _id2 = UV2501_220pp_id
 
 
 @directory.register
@@ -943,4 +948,18 @@
     _vhf_range = (136000000, 175000000)
     _uhf_range = (400000000, 481000000)
     _magic = MSTRING_MINI8900
-    _fileid = [KTUV890_fp, ]
+    _fileid = [KTUV980_fp, ]
+
+
+@directory.register
+class KT9800(BTech):
+    """QYT KT-8900"""
+    VENDOR = "QYT"
+    MODEL = "KT-8900"
+    _vhf_range = (136000000, 175000000)
+    _uhf_range = (400000000, 481000000)
+    _magic = MSTRING_KT8900
+    _fileid = [KT8900_fp, ]
+    _id2 = KT8900_id
+
+
