PyDD

PyDD provides a Python module that implements the Device Detection protocol of the SoM/iRJ45. This allows the device configuration by other applications such as web base management software etc.

Detecting devices in the network

To detect devices in the network, instantiate DeviceDetectionProtocol providing the file containing the database of GOAL variables:

from pydd import GoalDb, DeviceDetectionProtocol # File containing the variable info data goal_db_file = 'goal_db.json' goal_db = GoalDb(goal_db_file) proto = DeviceDetectionProtocol(goal_db)

Devices can be detected by invoking the function scan of class DeviceDetectionProtocol providing the local IP to use as the source address for sending UDP packets and a timeout. The following code example searches for all devices in the network and prints the IP and MAC address of each device found:

from pydd import GoalDb, DeviceDetectionProtocol, Device # The local IP address localip = "192.168.0.200" # Default timeout in ms timeout = 2000 devices = proto.scan(localip, timeout) idx = 1 for device in devices: print("============ Device {id} ============".format(id = idx)) print("IP: " + device.ip) print("MAC: " + device.mac.hex(':').upper()) idx += 1 print()

As shown in the code snippet above, scan returns a list containing all devices found in the network represented by instances of Device.

Reading network parameters

The network parameters of a device can be read by invoking readnetworkparams of class DeviceDetectionProtocol. This function returns a tuple containing the IP settings of the device:

from pydd import GoalDb, DeviceDetectionProtocol, Device # The local IP address localip = "192.168.0.200" # Default timeout in ms timeout = 2000 print("============ Network Params ============") (ip, netmask, gateway, dns0, dns1, dhcp_enabled) = proto.readnetworkparams(device, localip, timeout) print("IP:.......... " + ip) print("Netmask:..... " + netmask) print("Gateway:..... " + gateway) print("DNS0:........ " + dns0) print("DNS1:........ " + dns1) print("DHCP Enabled: " + ("Yes" if dhcp_enabled >0 else "No"))

The function returns the IP address, the net mask the gateway the two DNS and whether DHCP is enabled.

Setting network parameters

The network parameters of a device can set by invoking writenetworkparams of class DeviceDetectionProtocol. The function returns true if the operation was successful otherwise false.

The following code example sets the IP address of each device in a list with consecutive IP addresses:

As shown in the code example above, writenetworkparams expects the following parameters:

  • device: The Device instance where the IP data shall be set

  • localip: The local IP to use as the source address

  • timeout: The timeout in ms

  • ipdata: A touple containing the IP address, the net mask, the gw, dns0, dns1 and whether DHCP shall be enabled. IP addresses are provided as a dotted string notation (e.g.”192.168.0.1”), dhcp_enabled is a boolean value.

  • activate: Set to True if the IP address settings shall be coming effective immediately. Otherwise, the settings are applied after the next reboot.

  • permanent: Set To True if the IP address settings shall be stored in flash. If False, the settings are lost after a reboot.