#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Copyright (C) 2011 - 2012 Red Hat, Inc.
#
# This example lists basic information about network interfaces known to NM.
# It finds the devices via GetDevices() D-Bus call and then gets properties of
# each device.

NM_SERVICE_NAME="org.freedesktop.NetworkManager"
NM_OBJECT_PATH="/org/freedesktop/NetworkManager"
DEVICE_IFACE="org.freedesktop.NetworkManager.Device"
DEVICE_WIFI_IFACE="org.freedesktop.NetworkManager.Device.Wireless"
ACCESS_POINT_IFACE="org.freedesktop.NetworkManager.AccessPoint"
NM_GET_DEVICES="org.freedesktop.NetworkManager.GetDevices"
DBUS_PROPERTIES_GET="org.freedesktop.DBus.Properties.Get"
OBJECT_PATH="/org/freedesktop/NetworkManager/Settings"
METHOD="org.freedesktop.DBus.Properties.Get"

# for the types see include/NetworkManager.h
devtype_to_name() {
  case "$1" in
    1) echo "Ethernet" ;;
    2) echo "Wi-Fi" ;;
    5) echo "Bluetooth" ;;
    6) echo "OLPC" ;;
    7) echo "WiMAX" ;;
    8) echo "Modem" ;;
    9) echo "InfiniBand" ;;
    10) echo "Bond" ;;
    11) echo "VLAN" ;;
    12) echo "ADSL" ;;
    13) echo "Bridge" ;;
    14) echo "Generic" ;;
    15) echo "Team" ;;
    16) echo "TUN" ;;
    17) echo "IPTunnel" ;;
    18) echo "MACVLAN" ;;
    19) echo "VXLAN" ;;
    20) echo "Veth" ;;
    *) echo "Unknown" ;;
  esac
}

state_to_name() {
  case "$1" in
    10) echo "Unmanaged" ;;
    20) echo "Unavailable" ;;
    30) echo "Disconnected" ;;
    40) echo "Prepare" ;;
    50) echo "Config" ;;
    60) echo "Need Auth" ;;
    70) echo "IP Config" ;;
    80) echo "IP Check" ;;
    90) echo "Secondaries" ;;
    100) echo "Activated" ;;
    110) echo "Deactivating" ;;
    120) echo "Failed" ;;
    *) echo "Unknown" ;;
  esac
}

get_devices() {
  dbus-send --system --print-reply --dest="${NM_SERVICE_NAME}" \
    "${NM_OBJECT_PATH}" "${NM_GET_DEVICES}" \
    | grep "object path" | cut -d '"' -f 2
}

get_device_property() {
  # first arg:  device object path
  # second arg: property name
  # returns:    property value
  dbus-send --system --print-reply --dest="${NM_SERVICE_NAME}" \
    "$1" "${DBUS_PROPERTIES_GET}" string:"${DEVICE_IFACE}" string:"$2" \
    | grep "variant" | awk '{ print $3 }' | sed 's/"//g'
}

# shellcheck disable=SC2312
list_devices_details() {
  for device in $(get_devices); do
    DEV_INTERFACE=$(get_device_property "${device}" "Interface")
    DEV_TYPE=$(get_device_property "${device}" "DeviceType")
    DEV_DRIVER=$(get_device_property "${device}" "Driver")
    DEV_STATE=$(get_device_property "${device}" "State")

    echo "============================"
    echo "Interface: ${DEV_INTERFACE}"
    echo "Type: $(devtype_to_name "${DEV_TYPE}")"
    echo "Driver: ${DEV_DRIVER}"
    echo "State: $(state_to_name "${DEV_STATE}")"
  done
}

get_wifi_devices() {
  dbus-send --system --print-reply --dest="${NM_SERVICE_NAME}" \
    "${NM_OBJECT_PATH}" "${NM_GET_DEVICES}" \
    | grep "object path" | cut -d '"' -f 2
}

get_wifi_property() {
  # first arg:  object path
  # second arg: interface
  # third arg:  property name
  # returns:    property value
  dbus-send --system --print-reply --dest="${NM_SERVICE_NAME}" \
    "$1" "${DBUS_PROPERTIES_GET}" string:"$2" string:"$3" \
    | grep "variant" \
    | awk -F '"' '{ if (NF == 1) {FS=" "; n=split($0,a); print a[n];} else print $(NF-1); }'
}

is_wifi_device() {
  DEV_TYPE=$(get_wifi_property "${device}" "${DEVICE_IFACE}" "DeviceType")

  if [ "${DEV_TYPE}" -eq 2 ]; then
    return 0
  else
    return 1
  fi
}

show_active_ssids() {
  for device in $(get_wifi_devices); do
    INTERFACE=$(get_wifi_property "${device}" "${DEVICE_IFACE}" "Interface")

    if is_wifi_device; then
      ACTIVE_AP=$(get_wifi_property "${device}" "${DEVICE_WIFI_IFACE}" "ActiveAccessPoint")
      if [ "${ACTIVE_AP}" != "/" ]; then
        SSID=$(get_wifi_property "${ACTIVE_AP}" "${ACCESS_POINT_IFACE}" "Ssid")
        BSSID=$(get_wifi_property "${ACTIVE_AP}" "${ACCESS_POINT_IFACE}" "HwAddress")
        echo "Device '${INTERFACE}' is connected to '${SSID}' (BSSID=${BSSID})"
      else
        echo "No active AP on device '${INTERFACE}'" >&2
      fi
    fi
  done
}

get_hostname() {
  dbus-send --system --print-reply --dest="${NM_SERVICE_NAME}" \
    "${OBJECT_PATH}" "${METHOD}" \
    string:"org.freedesktop.NetworkManager.Settings" string:"Hostname" \
    | sed -n 's/.*"\([^"]*\)".*/\1/p'
}

get_hostname

# --- main program ---
# print currently connected SSID on all Wi-Fi devices
show_active_ssids

# print devices details
list_devices_details
