def test_checkCmdCounter(self): """Test checkCmdCounter method.""" # Check that command relay buffer is empty assert (self.comm.cmdRelayBuffer == bytearray()) ### Test method with command that includes a command counter checkCmdCounter(self.comm, testCmds[NodeCmds['GCSCmd']].header['header'], testCmds[NodeCmds['GCSCmd']].serialize(), self.comm) encodedMsg = createEncodedMsg(testCmds[NodeCmds['GCSCmd']].serialize()) assert (self.comm.cmdRelayBuffer == encodedMsg) # Resend with new counter to check cmd is appended to buffer #cmdCounter = testCmds[NodeCmds['GCSCmd']].header['header']['cmdCounter'] #header, cmdMsg = self.updateCmdCounterValue(cmdCounter+1, deepcopy(testCmds[NodeCmds['GCSCmd']].header), testCmds[NodeCmds['GCSCmd']].body) testCmds[NodeCmds['GCSCmd']].header['header']['cmdCounter'] += 1 checkCmdCounter(self.comm, testCmds[NodeCmds['GCSCmd']].header['header'], testCmds[NodeCmds['GCSCmd']].serialize(), self.comm) #checkCmdCounter(self.comm, header['header'], cmdMsg, self.comm) encodedMsg2 = createEncodedMsg( testCmds[NodeCmds['GCSCmd']].serialize()) assert (self.comm.cmdRelayBuffer == encodedMsg + encodedMsg2) ## Test various counter values to test acceptance behavior # Command counter == 1, stored counter == 0 self.comm.cmdRelayBuffer = bytearray() self.nodeParams.cmdHistory.append( 0) # place known cmd counter value in history testCmds[NodeCmds['GCSCmd']].header['header']['cmdCounter'] = 1 checkCmdCounter(self.comm, testCmds[NodeCmds['GCSCmd']].header['header'], testCmds[NodeCmds['GCSCmd']].serialize(), self.comm) assert (len(self.comm.cmdRelayBuffer) > 0) # cmd put in relay buffer # Command counter == 1, stored counter == 1 self.comm.cmdRelayBuffer = bytearray() checkCmdCounter(self.comm, testCmds[NodeCmds['GCSCmd']].header['header'], testCmds[NodeCmds['GCSCmd']].serialize(), self.comm) assert (len(self.comm.cmdRelayBuffer) == 0 ) # cmd not put in relay buffer ### Send command that should not be relayed self.comm.cmdRelayBuffer = bytearray() self.nodeParams.cmdHistory = deque(maxlen=50) # clear command history checkCmdCounter(self.comm, testCmds[NodeCmds['NoOp']].header['header'], testCmds[NodeCmds['NoOp']].serialize(), self.comm) assert (len(self.comm.cmdRelayBuffer) == 0 ) # cmd not put in relay buffer
def test_parseMsgs(self): """Test parseMsgs method of SerialComm.""" # Place multiple messages in rx buffer truthMsg = createEncodedMsg(testMsg) truthMsg2 = createEncodedMsg(testMsg + b'999') self.serialComm.radio.rxBuffer[0:len(truthMsg)] = truthMsg self.serialComm.radio.rxBuffer[len(truthMsg):len(truthMsg) + len(truthMsg2)] = truthMsg2 self.serialComm.radio.bytesInRxBuffer = len(truthMsg) + len(truthMsg2) # Parse messages self.serialComm.parseMsgs() assert (len(self.serialComm.msgParser.parsedMsgs) == 2) assert (self.serialComm.msgParser.parsedMsgs[0] == testMsg ) # confirm parsed message matches original assert (self.serialComm.msgParser.parsedMsgs[1] == testMsg + b'999' ) # confirm parsed message matches original
def test_bufferTxMsg(self): """Test bufferTxMsg method of SerialComm.""" assert (len(self.serialComm.radio.txBuffer) == 0 ) # confirm empty tx buffer self.serialComm.bufferTxMsg(testMsg) truthMsg = createEncodedMsg(testMsg) assert (self.serialComm.radio.txBuffer == truthMsg ) # confirm message placed in tx buffer
def test_sendMsg(self): """Test sendMsg method of SerialComm.""" # Send test message msgBytes = testMsg self.serialPort.read(100) # clear serial buffer self.serialComm.sendMsg(testMsg) time.sleep(0.1) readBytes = self.serialPort.read(100) # Encode test message and compare to bytes read truthMsg = createEncodedMsg(testMsg) assert (readBytes == truthMsg)
def test_sendBuffer(self): """Test sendBuffer method of SerialComm.""" # Place message in tx buffer self.serialComm.bufferTxMsg(testMsg) assert (len(self.serialComm.radio.txBuffer) > 0) # Send message and compare received bytes to sent self.serialComm.sendBuffer() assert (len(self.serialComm.radio.txBuffer) == 0) time.sleep(0.1) self.serialComm.readBytes() truthMsg = createEncodedMsg(testMsg) serBytes = self.serialComm.radio.getRxBytes() assert (serBytes == truthMsg)
def test_checkCmdCounter(self): """Test checkCmdCounter method.""" # Check that command relay buffer is empty assert (self.comm.cmdRelayBuffer == bytearray()) ### Test method with command that includes a command counter cmdMsg = packHeader(testCmds[PixhawkCmds['GCSCmd']].header) + testCmds[ PixhawkCmds['GCSCmd']].body checkCmdCounter(self.commProcessor, testCmds[PixhawkCmds['GCSCmd']].header['header'], cmdMsg, self.comm) encodedMsg = createEncodedMsg(cmdMsg) assert (self.comm.cmdRelayBuffer == encodedMsg) # Resend with new counter to check cmd is appended to buffer cmdCounter = testCmds[ PixhawkCmds['GCSCmd']].header['header']['cmdCounter'] header, cmdMsg = self.updateCmdCounterValue( cmdCounter + 1, deepcopy(testCmds[PixhawkCmds['GCSCmd']].header), testCmds[PixhawkCmds['GCSCmd']].body) checkCmdCounter(self.commProcessor, header['header'], cmdMsg, self.comm) encodedMsg2 = createEncodedMsg(cmdMsg) assert (self.comm.cmdRelayBuffer == encodedMsg + encodedMsg2) ## Test various counter values to test acceptance behavior # Command counter == 1, stored counter == 0 self.comm.cmdRelayBuffer = bytearray() self.nodeParams.cmdHistory.append( 0) # place known cmd counter value in history header, cmdMsg = self.updateCmdCounterValue( 1, deepcopy(testCmds[PixhawkCmds['GCSCmd']].header), testCmds[PixhawkCmds['GCSCmd']].body) checkCmdCounter(self.commProcessor, header['header'], cmdMsg, self.comm) assert (len(self.comm.cmdRelayBuffer) > 0) # cmd put in relay buffer # Command counter == 1, stored counter == 1 self.comm.cmdRelayBuffer = bytearray() checkCmdCounter(self.commProcessor, header['header'], cmdMsg, self.comm) assert (len(self.comm.cmdRelayBuffer) == 0 ) # cmd not put in relay buffer # Command counter == 255, stored counter == 1 self.comm.cmdRelayBuffer = bytearray() header, cmdMsg = self.updateCmdCounterValue( 255, deepcopy(testCmds[PixhawkCmds['GCSCmd']].header), testCmds[PixhawkCmds['GCSCmd']].body) checkCmdCounter(self.commProcessor, header['header'], cmdMsg, self.comm) assert (len(self.comm.cmdRelayBuffer) > 0) # cmd put in relay buffer # Command counter == 1, stored counter == 255 header, cmdMsg = self.updateCmdCounterValue( 1, deepcopy(testCmds[PixhawkCmds['GCSCmd']].header), testCmds[PixhawkCmds['GCSCmd']].body) checkCmdCounter(self.commProcessor, header['header'], cmdMsg, self.comm) assert (len(self.comm.cmdRelayBuffer) > 0) # cmd put in relay buffer # Command counter == cmdCounterThreshold, storedcounter == 255 (DEPRECATED) #self.nodeParams.set_cmdCounter(255) #self.comm.cmdRelayBuffer = bytearray() #header, cmdMsg = self.updateCmdCounterValue(self.nodeParams.cmdCounterThreshold, deepcopy(testCmds[PixhawkCmds['GCSCmd']].header), testCmds[PixhawkCmds['GCSCmd']].body) #assert(len(self.comm.cmdRelayBuffer) == 0) # cmd not put in relay buffer ### Send command that should not be relayed self.comm.cmdRelayBuffer = bytearray() self.nodeParams._cmdCounter = 0 cmdMsg = packHeader(testCmds[PixhawkCmds['FormationCmd']].header ) + testCmds[PixhawkCmds['FormationCmd']].body checkCmdCounter(self.commProcessor, testCmds[PixhawkCmds['FormationCmd']].header['header'], cmdMsg, self.comm) assert (len(self.comm.cmdRelayBuffer) == 0 ) # cmd not put in relay buffer