diff options
author | Jiří Klimeš <jklimes@redhat.com> | 2015-02-02 17:22:05 +0100 |
---|---|---|
committer | Jiří Klimeš <jklimes@redhat.com> | 2015-02-09 10:11:12 +0100 |
commit | 3d991b8e3216d38af45e8f36d2d69d2b50ebd648 (patch) | |
tree | db34ec9b278fddaf91667346e2d8c5256f352628 /examples | |
parent | 9a2c0451a48090399c0fc9f6fae34824e3b42841 (diff) | |
download | NetworkManager-3d991b8e3216d38af45e8f36d2d69d2b50ebd648.tar.gz |
examples: add a Lua example for getting Wi-Fi access points
Diffstat (limited to 'examples')
-rw-r--r-- | examples/lua/lgi/Makefile.am | 3 | ||||
-rwxr-xr-x | examples/lua/lgi/show-wifi-networks.lua | 79 |
2 files changed, 81 insertions, 1 deletions
diff --git a/examples/lua/lgi/Makefile.am b/examples/lua/lgi/Makefile.am index 1b16368495..6a3d068278 100644 --- a/examples/lua/lgi/Makefile.am +++ b/examples/lua/lgi/Makefile.am @@ -1,4 +1,5 @@ EXTRA_DIST = \ add-connection.lua \ list-connections.lua \ - list-devices.lua + list-devices.lua \ + show-wifi-networks.lua diff --git a/examples/lua/lgi/show-wifi-networks.lua b/examples/lua/lgi/show-wifi-networks.lua new file mode 100755 index 0000000000..69a065c67b --- /dev/null +++ b/examples/lua/lgi/show-wifi-networks.lua @@ -0,0 +1,79 @@ +#!/usr/bin/env lua +-- -*- Mode: Lua; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- +-- vim: ft=lua ts=2 sts=2 sw=2 et ai +-- +-- This program is free software; you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation; either version 2 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License along +-- with this program; if not, write to the Free Software Foundation, Inc., +-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +-- +-- Copyright 2015 Red Hat, Inc. +-- +-- +-- This example lists Wi-Fi access points NetworkManager scanned on Wi-Fi devices. +-- The example uses libnm library using GObject introspection via Lua lgi module. +-- Most distribution ship the module as lua-lgi package. +-- libnm guide: https://developer.gnome.org/libnm/1.0/ +-- Lua-lgi guide: https://github.com/pavouk/lgi/blob/master/docs/guide.md +-- + +local lgi = require 'lgi' +local NM = lgi.NM + +function ssid_to_utf8(ap) + local ssid = ap:get_ssid() + if not ssid then return "" end + return NM.utils_ssid_to_utf8(ssid:get_data()) +end + +function print_device_info(device) + local active_ap = device:get_active_access_point() + if active_ap then ssid = ssid_to_utf8(active_ap) end + local info = string.format("Device: %s | Driver: %s | Active AP: %s", + device:get_iface(), device:get_driver(), ssid) + print(info) + print(string.rep("=", info:len())) +end + +function print_ap_info(ap) + local strength = ap:get_strength() + local frequency = ap:get_frequency() + print("SSID: ", ssid_to_utf8(ap)) + print("BSSID: ", ap:get_bssid()) + print("Frequency: ", frequency) + print("Channel: ", NM.utils_wifi_freq_to_channel(frequency)) + print(string.format("Strength: %s %s%%", NM.utils_wifi_strength_bars(strength), strength)) + print("") +end + + +--------------------------- +-- Main code starts here -- +--------------------------- +-- Call setlocale() else NM.utils_wifi_strength_bars() will think the locale +-- is ASCII-only, and return the fallback characters rather than the unicode bars +os.setlocale('') + +-- get all devices +client = NM.Client.new() +devs = client:get_devices() + +-- print APs for all Wi-Fi devices +for _, dev in ipairs(devs) do + if dev:get_device_type() == "WIFI" then + print_device_info(dev) + for _, ap in ipairs(dev:get_access_points()) do + print_ap_info(ap) + end + end +end + |