Esempio n. 1
0
class BoardUtilsTests(unittest.TestCase):
    def setUp(self):
        self.__utils = BoardUtils(debug=False)
        # Override the default bof directory to work for unittests
        self.__utils._BoardUtils__bof_dir = '../data'

    def tearDown(self):
        #!!! Get rid of any extra bof files that may have failed to unload
        pass
        
    def testListAllBofs(self):
        # Success
        self.assertEqual('True', self.__utils.loadBof('fpga1.bof'))
        self.assertEqual(['fpga1.bof,r', 'fpga2.bof,a'],
                         self.__utils.listAllBofs())
        self.assertEqual('True', self.__utils.unloadBof('fpga1.bof'))
        # Failure

    def testListFreeBofs(self):
        # Success
        self.assertEqual('True', self.__utils.loadBof('fpga1.bof'))
        self.assertEqual(['fpga2.bof'], self.__utils.listFreeBofs())
        self.assertEqual('True', self.__utils.unloadBof('fpga1.bof'))
        # Failure

    def testListRegisters(self):
        #!!! No good way to test yet
        #!!! Depends on finding a directory in /proc/ on the fly
        pass

    def testListRunningBofs(self):
        # Success
        self.assertEqual('True', self.__utils.loadBof('fpga1.bof'))
        self.assertEqual(['fpga1.bof'], self.__utils.listRunningBofs())
        self.assertEqual('True', self.__utils.unloadBof('fpga1.bof'))
        # Failure

    def testLoadBof(self):
        # Success
        self.assertEqual('True', self.__utils.loadBof('/bin/ls'))
        # Failure
        self.assertEqual('False', self.__utils.loadBof('notABof'))

    def testReadRegister(self):
        #!!! No good way to test yet
        #!!! Depends on finding a directory in /proc/ on the fly
        pass

    def testUnloadBof(self):
        # Success
        self.assertEqual('True', self.__utils.loadBof('fpga1.bof'))
        self.assertEqual('True', self.__utils.unloadBof('fpga1.bof'))
        # Failure
        self.assertEqual('False', self.__utils.unloadBof('notABof'))

    def testWriteRegister(self):
        #!!! No good way to test yet
        #!!! Depends on finding a directory in /proc/ on the fly
        pass
Esempio n. 2
0
class Bee2Agent(Agent):
    """Agent for the BEE2.

    Handles incoming requests.
    """
    def __init__(self):
        self.__utils = BoardUtils()

    def get(self, keys = index):
        """Get all values of parameters matching keys in keys list.

        If keys is ['index'], return a list of all available parameters.
        i.e. return a list of everything the user can get.

        Keyword arguments:
        keys -- names of parameters to get (default index)
        """
        result = []
        if keys != index:
            result += [self.__utils.readRegister(reg) for reg in keys]
            # Keep hexlification of registers separate; preserve time coupling
            for i in range(len(result)):
                if result[i] != 'Error':
                    result[i] = b2a_hex(result[i])
            #result = [b2a_hex(r) for r in result]
        else:
            result += self.__utils.listRegisters()
        return result


    def load(self, keys = index):
        """Load profiles with names which match given keys list.

        If keys is ['index'], return a list of all available profiles to load.
        i.e. return a list of everything the user can load.

        Note that one cannot load a profile on a component in use.

        Keyword arguments:
        keys -- sequence of names of profiles to load (default index)
        """
        result = []
        if keys != index:
            result += [self.__utils.loadBof(proc) for proc in keys]
        else:
            result += self.__utils.listFreeBofs()
        return result


    def set(self, keys, values):
        """Set parameters with names which match keys, with supplied values.

        Keyword arguments:
        keys -- sequence of names of parameters to set
        values -- sequence of values of parameters to set
        """
        result = []
        if len(keys) > 0 and len(keys) == len(values) \
                and keys[0] and values[0]:
            # This relies on integer rounding in the division,
            # so in this case (n/8)*8 != n for some n
            values = [a2b_hex(zfill(str(value), 8*((len(value)+7)/8)))
                      for value in values]
            result += [self.__utils.writeRegister(reg, data)
                       for reg, data in zip(keys, values)]
        else:
            self.__utils.debug('set: key or value mismatch')
            result += ['False' for i in keys]
        return result
    

    def unload(self, keys = index):
        """Unload profiles with names which match given keys list.

        If keys is ['index'], return a list of all loaded profiles.
        i.e. return a list of everything the user can unload.

        Keyword arguments:
        keys -- sequence of names of profiles to unload (default index)
        """
        result = []
        if keys != index:
            result += [self.__utils.unloadBof(proc) for proc in keys]
        else:
            result += self.__utils.listRunningBofs()
        return result


    def profiles(self, keys = index):
        """Provide information on profiles, either given or found.
        
        If keys is ['index'], return a list of info on all found profiles,
        formatted ['profile_name,s', ...] where s is state of profile.

        See the agent module's 'states' property for more information.

        Keyword arguments:
        keys -- sequence of names of profiles for info (default index)
        """
        result = []
        profs = self.__utils.listAllBofs()
        if keys != index:
            bofs = [bof[:-2] for bof in profs]
            for key in keys:
                if key in bofs:
                    result.append(profs[bofs.index(key)])
                else:
                    result.append('Error')
        else:
            result += profs
        return result