# pyOCD debugger
# Copyright (c) 2006-2019 Arm Limited
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .board import Board
from .board_ids import BOARD_ID_TO_INFO
import logging

LOG = logging.getLogger(__name__)

class MbedBoard(Board):
    """! @brief Mbed board class.
    
    This class inherits from Board and is specific to mbed boards. Particularly, this class
    will dynamically determine the type of connected board based on the board ID encoded in
    the debug probe's serial number.
    """
    def __init__(self, session, target=None):
        """! @brief Constructor.
        
        This constructor attempts to use the board ID from the serial number to determine
        the target type. See #BOARD_ID_TO_INFO.
        """
        target = session.options.get('target_override', target)
        unique_id = session.probe.unique_id
        try:
            board_id = unique_id[0:4]
            board_info = BOARD_ID_TO_INFO[board_id]
            self._name = board_info.name
            self.native_target = board_info.target
        except KeyError:
            board_info = None
            self._name = "Unknown Board"
            self.native_target = None

        # Unless overridden use the native target
        if target is None:
            target = self.native_target

        if target is None:
            LOG.warning("Board ID %s is not recognized; you will be able to use pyOCD but not program flash.", board_id)
            target = "cortex_m"

        super(MbedBoard, self).__init__(session, target)

        # Set test binary if not already set.
        if (board_info is not None) and (self._test_binary is None):
            self._test_binary = board_info.binary

    @property
    def name(self):
        """! @brief Return board name."""
        return self._name

    @property
    def description(self):
        """! @brief Return description of the board."""
        return self.name + " [" + self.target_type + "]"


@@ -1032,16 +1032,134 @@
  number.
+ If the board ID is all %220%22 characters, it indicates the%0A    firmware is generic and doesn't have an associated board.
 %0A    %22%22%22
@@ -1517,25 +1517,8 @@
     
-try:%0A            
 boar
@@ -1539,16 +1539,350 @@
 id%5B0:4%5D%0A
+        %0A        # Check for null board ID. This indicates a standalone probe or generic firmware.%0A        if board_id == %220000%22:%0A            board_info = None%0A            self._name = %22Generic Board%22%0A            self.native_target = None%0A        else:%0A            # Attempt to look up the board ID in our table.%0A            try:%0A    
         
@@ -1929,32 +1929,36 @@
 id%5D%0A            
+ 
+   
 self._name = boa
@@ -1970,16 +1970,20 @@
 fo.name%0A
+    
         
@@ -2033,16 +2033,20 @@
         
+  
+  
 except K
@@ -2054,16 +2054,20 @@
 yError:%0A
+    
         
@@ -2092,32 +2092,36 @@
 one%0A            
+   
+ 
 self._name = %22Un
@@ -2137,32 +2137,36 @@
 rd%22%0A            
+    
 self.native_targ
@@ -2176,16 +2176,20 @@
 = None%0A%0A
+    
         
@@ -2230,32 +2230,36 @@
  target%0A        
+    
 if target is Non
@@ -2253,32 +2253,36 @@
 target is None:%0A
+    
             targ
@@ -2306,16 +2306,190 @@
 target%0A%0A
+            # If there still isn't a known target, tell the user about it. Leaving target%0A            # set to None will cause cortex_m to be selected by the Board ctor.%0A    
         
@@ -2499,32 +2499,36 @@
 target is None:%0A
+    
             LOG.
@@ -2635,40 +2635,8 @@
 _id)
-%0A            target = %22cortex_m%22
 %0A%0A  

Make MbedBoard aware that a board ID of "0000" means generic firmware.