[ATFT] Make file backward compatible for v2.0.

Add logic to check config file version, if config file version is
v2.0 and the software version is v3.0, we would upgrade the config
file to v3.0. User could upgrade from v2.0 to v3.0 and keep all the
configurations including mapping status by copying the config file.

Test: 1. Verify config version upgrades from v2.0 to v3.0.
2. Verify that config version stays the same even if it is invalid.
3. Verify that config files with wrong version (not v2.0 or v3.0)
would be rejected and the config file version would not be touched.
Bug: None
Change-Id: Id1a4c82b35a31eaaccd88b2ec002f41833b6e1ad
diff --git a/at-factory-tool/atft.py b/at-factory-tool/atft.py
index 7b70d42..bd541ff 100644
--- a/at-factory-tool/atft.py
+++ b/at-factory-tool/atft.py
@@ -59,6 +59,9 @@
   from serialmapperwin import SerialMapper
 
 
+# The current software version.
+VERSION = 3.0
+
 # colors
 COLOR_WHITE = wx.Colour(255, 255, 255)
 COLOR_RED = wx.Colour(192, 40, 40)
@@ -274,8 +277,9 @@
         'Failed to create log!',
         '无法创建日志文件!'][index]
     self.ALERT_FAIL_TO_PARSE_CONFIG = [
-        'Failed to find or parse config file!',
-        '无法找到或解析配置文件!'][index]
+        'Failed to find or parse config file, please check your config file'
+        ' version!',
+        '无法找到或解析配置文件,请确认你的配置文件与软件版本一致!'][index]
     self.ALERT_NO_DEVICE = [
         'No devices found!',
         '无设备!'][index]
@@ -1457,7 +1461,7 @@
 
     self.SetLanguage()
 
-    self.atft_string.TITLE += ' ' + self.atft_version
+    self.atft_string.TITLE += ' %s' % self.atft_version
 
     # The atft_manager instance to manage various operations.
     self.atft_manager = self.CreateAtftManager()
@@ -1515,6 +1519,10 @@
 
     self.InitializeUI()
 
+    if self.configs == None:
+      self.ShowAlert(self.atft_string.ALERT_FAIL_TO_PARSE_CONFIG)
+      sys.exit(0)
+
     self.CreateShortCuts()
 
     self.log = self.CreateAtftLog()
@@ -1524,10 +1532,6 @@
     self.key_handler = self.CreateAtftKeyHandler()
     self.key_handler.StartProcessKey()
 
-    if self.configs == None:
-      self.ShowAlert(self.atft_string.ALERT_FAIL_TO_PARSE_CONFIG)
-      sys.exit(0)
-
     if not self.log.log_dir_file:
       self._SendAlertEvent(self.atft_string.ALERT_FAIL_TO_CREATE_LOG)
 
@@ -1787,7 +1791,7 @@
       The parsed configuration map.
     """
     # Give default values
-    self.atft_version = 'v0.0'
+    self.atft_version = ''
     self.compatible_atfa_version = '0'
     self.device_refresh_interval = 1.0
     self.default_key_threshold_1 = None
@@ -1825,6 +1829,14 @@
 
     try:
       self.atft_version = str(configs['ATFT_VERSION'])
+      if self.atft_version != "v%.1f" % VERSION:
+        # Config file version mismatch.
+        if VERSION == 3.0 and self.atft_version == 'v2.0':
+          # 3.0 is compatible with v2.0 config file. Update the config version.
+          self.atft_version = 'v3.0'
+        else:
+          return None
+
       self.compatible_atfa_version = str(configs['COMPATIBLE_ATFA_VERSION'])
       self.device_refresh_interval = float(configs['DEVICE_REFRESH_INTERVAL'])
       if 'DEFAULT_KEY_THRESHOLD_1' in configs:
@@ -1958,6 +1970,8 @@
     self.configs['DEVICE_USB_LOCATIONS'] = self.device_usb_locations
     self.configs['PASSWORD_HASH'] = self.password_hash
     self.configs['LANGUAGE'] = self.language
+    if self.atft_version:
+      self.configs['ATFT_VERSION'] = self.atft_version
     config_file_path = os.path.join(self._GetCurrentPath(), self.CONFIG_FILE)
     with open(config_file_path, 'w') as config_file:
       config_file.write(json.dumps(self.configs, sort_keys=True, indent=4))