moved device setup into a single function

By the way, --verbose is not implemented yet. We will need to wait until
systemd journal or some other form of logging is implemented.
This commit is contained in:
Pranav Jerry 2021-08-05 12:11:31 +05:30
parent df73f94018
commit 2e4149d943
Signed by: pranav
GPG Key ID: F1DCDC4FED0A0C5B
2 changed files with 50 additions and 43 deletions

View File

@ -16,4 +16,4 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
__version__ = "0.2.0a2"
__version__ = "0.2.0a3"

View File

@ -55,6 +55,51 @@ def copy_files():
copy(i, dest)
def setup_devices(args):
iwd = IWD()
devices = iwd.get_devices()
adhoc_devices = []
ap_devices = []
# Find devices supporting ad-hoc and ap
for i in devices:
device = Device(i)
adapter = Adapter(device.adapter)
if adapter.supports_mode("ad-hoc"):
adhoc_devices.append(i)
if adapter.supports_mode("ap"):
ap_devices.append(i)
if len(adhoc_devices) != 0:
# Start ad-hoc on first device supporting ad-hoc
adhoc_device = Device(adhoc_devices.pop())
# The same device is likely to have ap support too.
# But we can't start ad-hoc and ap on the same interface.
# Remove adhoc_device from ap_devices if it exists there
if adhoc_device.name in ap_devices:
ap_devices.remove(adhoc_device.name)
print("Working on ad-hoc")
# Turn on adapter if it is off
# See issue #9
adhoc_adapter = Adapter(adhoc_device.adapter)
if not adhoc_adapter.is_powered_on():
adhoc_adapter.power_on()
adhoc_device.reload()
adhoc_device.start_adhoc_open(args.adhoc_name)
# Start Access point if ap_device is not empty,
# ie, we have more devices
if len(ap_devices) != 0:
print("Working on AP")
ap_device = Device(ap_devices.pop())
# Turn on adapter if it is off
# See issue #9
ap_adapter = Adapter(ap_device.adapter)
if not ap_adapter.is_powered_on():
ap_adapter.power_on()
ap_adapter.reload()
ap_device.start_ap(args.ap_ssid, args.ap_passwd)
def here_be_dragons():
"""
This function is run every time you
@ -69,47 +114,7 @@ def here_be_dragons():
# Now, the iwd part
try:
iwd = IWD()
devices = iwd.get_devices()
adhoc_devices = []
ap_devices = []
for i in devices:
device = Device(i)
adapter = Adapter(device.adapter)
if adapter.supports_mode("ad-hoc"):
adhoc_devices.append(i)
if adapter.supports_mode("ap"):
ap_devices.append(i)
if len(adhoc_devices) != 0:
# Start ad-hoc on first device supporting ad-hoc
adhoc_device = Device(adhoc_devices.pop())
# The same device is likely to have ap support too.
# But we can't start ad-hoc and ap on the same interface.
# Remove adhoc_device from ap_devices if it exists there
if adhoc_device.name in ap_devices:
ap_devices.remove(adhoc_device.name)
print("Working on ad-hoc")
# Turn on adapter if it is off
# See issue #9
adhoc_adapter = Adapter(adhoc_device.adapter)
if not adhoc_adapter.is_powered_on():
adhoc_adapter.power_on()
adhoc_device.reload()
adhoc_device.start_adhoc_open(args.adhoc_name)
# Start Access point if ap_device is not empty,
# ie, we have more devices
if len(ap_devices) != 0:
print("Working on AP")
ap_device = Device(ap_devices.pop())
# Turn on adapter if it is off
# See issue #9
ap_adapter = Adapter(ap_device.adapter)
if not ap_adapter.is_powered_on():
ap_adapter.power_on()
ap_adapter.reload()
ap_device.start_ap(args.ap_ssid, args.ap_passwd)
setup_devices(args)
except DBusError as error:
print(error)
sys.exit("An error occured while communicating with iwd")
@ -124,19 +129,21 @@ def parse_args():
)
parser.add_argument(
"--ap-ssid",
"-n",
type=str,
help="SSID of the WiFi AP",
default=AP_SSID,
)
parser.add_argument(
"--ap-passwd",
"-p",
"--ap-password",
type=str,
help="password of the WiFi AP",
default=AP_PASSWD,
)
parser.add_argument(
"--adhoc-name", type=str, default=ADHOC_NAME, help="name of adhoc network"
"--adhoc-name", "-a", type=str, default=ADHOC_NAME, help="name of adhoc network"
)
parser.add_argument(
"-v",