Example #1
0
    def test_protobufConversion(self):
        """Tests conversion between json and protobuf configuration representations."""

        # Convert json data to protobuf format
        with open (configFilePath, "r") as jsonFile:
            configData = json.load(jsonFile)
        #protobuf = self.nodeConfig.toProtoBuf(configData)
        protobuf = NodeConfig.toProtoBuf(configData)

        # Convert back to json representation
        #fromProtobuf = self.nodeConfig.fromProtoBuf(protobuf.SerializeToString())
        fromProtobuf = NodeConfig.fromProtoBuf(protobuf.SerializeToString())
        
        # Hash config instance from conversion and compare to initial configuration
        nc_protobuf = NodeConfig(configData=fromProtobuf)

        assert(self.nodeConfig.calculateHash() == nc_protobuf.calculateHash())
Example #2
0
    def test_loadConfig(self):
        """Test loadConfig method of NodeParams."""

        # Create new config for update
        newConfig = NodeConfig(configFilePath)
        newConfigHash = newConfig.calculateHash()
        with open(configFilePath, "r") as jsonFile:
            configData = json.load(jsonFile)
        newConfig_pb = NodeConfig.toProtoBuf(configData).SerializeToString()

        # Test update method
        badHash = hashlib.sha1()
        assert (self.nodeParams.loadConfig(newConfig_pb, badHash) == False
                )  # test rejection with bad hash
        assert (self.nodeParams.newConfig == None)
        assert (self.nodeParams.loadConfig(newConfig_pb, newConfigHash) == True
                )  # test acceptance
        assert (self.nodeParams.newConfig != None)
Example #3
0
    def test_updateConfig(self):
        """Test updateConfig method of NodeParams."""

        # Load valid new config
        newConfig = NodeConfig(configFilePath)
        newConfigHash = newConfig.calculateHash()
        with open(configFilePath, "r") as jsonFile:
            configData = json.load(jsonFile)
        newConfig_pb = NodeConfig.toProtoBuf(configData).SerializeToString()
        self.nodeParams.loadConfig(newConfig_pb, newConfigHash)

        # Test successful loading
        self.nodeParams.config.FCBaudrate = newConfig.FCBaudrate / 2.0  # change a parameter to verify loading of new config
        assert (self.nodeParams.config.FCBaudrate != newConfig.FCBaudrate)
        assert (self.nodeParams.updateConfig() == True)
        assert (self.nodeParams.config.FCBaudrate == newConfig.FCBaudrate)
        assert (self.nodeParams.newConfig == None)

        # Expect no load when config missing
        assert (self.nodeParams.updateConfig() == False)
Example #4
0
    def initMesh(self, currentTime=time.time()):
        """Initialize node mesh networks."""
        # Create tdma comm messages
        flooredStartTime = math.floor(self.commStartTime)
        self.tdmaCmds[TDMACmds['MeshStatus']] = Command(TDMACmds['MeshStatus'], {'commStartTimeSec': int(flooredStartTime), 'status': self.tdmaStatus, 'configHash': self.nodeParams.config.calculateHash()}, [TDMACmds['MeshStatus'], self.nodeParams.config.nodeId], self.nodeParams.config.commConfig['statusTxInterval'])

        self.tdmaCmds[TDMACmds['LinkStatus']] = Command(TDMACmds['LinkStatus'], {'linkStatus': self.nodeParams.linkStatus, 'nodeId': self.nodeParams.config.nodeId}, [TDMACmds['LinkStatus'], self.nodeParams.config.nodeId], self.nodeParams.config.commConfig['linksTxInterval'])
        
        if self.nodeParams.config.nodeId != 0: # stop ground node from broadcasting time offset
            self.tdmaCmds[TDMACmds['TimeOffset']] = Command(TDMACmds['TimeOffset'], {'nodeStatus': self.nodeParams.nodeStatus[self.nodeParams.config.nodeId-1]}, [TDMACmds['TimeOffset'], self.nodeParams.config.nodeId], self.nodeParams.config.commConfig['offsetTxInterval'])
        
        # Current network configuration message
        self.initialConfigTxTime = self.commStartTime + self.nodeParams.config.nodeId * self.nodeParams.config.commConfig['configTxInterval']
        configHash = self.nodeParams.config.calculateHash()
        config_pb = NodeConfig.toProtoBuf(self.nodeParams.config.rawConfig).SerializeToString()
        self.tdmaCmds[TDMACmds['CurrentConfig']] = Command(TDMACmds['CurrentConfig'], {'config': config_pb, 'configLength': len(config_pb), 'configHash': configHash, 'hashLength': self.nodeParams.config.hashSize}, [TDMACmds['CurrentConfig'], self.nodeParams.config.nodeId], self.maxNumSlots * self.nodeParams.config.commConfig['configTxInterval'])

        # Determine where in frame mesh network currently is
        self.syncTDMAFrame(currentTime)

        self.inited = True
        print("Node " + str(self.nodeParams.config.nodeId) + " - Initializing comm")
Example #5
0
testCmds.update({cmdId: cmd})

# TDMACmds['LinkStatus']
cmdId = TDMACmds['LinkStatus']
linkStatus = [[LinkStatus.NoLink for i in range(nodeParams.config.maxNumNodes)] for j in range(nodeParams.config.maxNumNodes)]
linkStatus[nodeId-1] = [LinkStatus.IndirectLink]*nodeParams.config.maxNumNodes
cmdData = {'linkStatus': linkStatus, 'nodeId': 1}
cmd = Command(cmdId, cmdData, [cmdId, nodeId])
testCmds.update({cmdId: cmd})

# TDMACmds['ConfigUpdate']
cmdId = TDMACmds['ConfigUpdate']
configHash = nodeParams.config.calculateHash()
with open(configFilePath, "r") as jsonFile:
    configData = json.load(jsonFile)
newConfig_pb = NodeConfig.toProtoBuf(configData).SerializeToString()
cmdData = {'destId': nodeParams.config.nodeId, 'config': newConfig_pb, 'configHash': configHash, 'hashLength': nodeParams.config.hashSize, 'configLength': len(newConfig_pb)}
cmd = Command(cmdId, cmdData, [cmdId, nodeId, nodeParams.get_cmdCounter()])
testCmds.update({cmdId: cmd})

# TDMACmds['NetworkRestart']
cmdId = TDMACmds['NetworkRestart']
destId = 4
restartTime = int(time.time() + 1000)
cmdData = {'destId': destId, 'restartTime': restartTime}
cmd = Command(cmdId, cmdData, [cmdId, nodeId, nodeParams.get_cmdCounter()])
testCmds.update({cmdId: cmd})

### GndCmds
# GndCmds['TimeOffsetSummary']
cmdId = GndCmds['TimeOffsetSummary']