2021-09-06 21:54:24 +05:30
|
|
|
# This file is part of naxalnet.
|
|
|
|
# Copyright (C) 2021 The naxalnet Authors
|
|
|
|
|
|
|
|
# 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
daemon.py
|
|
|
|
---------
|
|
|
|
|
|
|
|
The daemon part. This is currently under construction.
|
|
|
|
"""
|
|
|
|
|
2021-09-07 11:19:14 +05:30
|
|
|
from dasbus.loop import EventLoop
|
|
|
|
from naxalnet.iwd import IWD, IWD_DEVICE_INTERFACE
|
2021-09-07 18:45:11 +05:30
|
|
|
from naxalnet.log import logger
|
2021-09-06 21:54:24 +05:30
|
|
|
|
|
|
|
|
|
|
|
class Daemon:
|
|
|
|
"""implements the daemon part"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2021-09-07 11:19:14 +05:30
|
|
|
self.loop = EventLoop()
|
|
|
|
self.iwd = IWD()
|
2021-09-27 22:30:07 +05:30
|
|
|
self.callback = None
|
2021-09-06 21:54:24 +05:30
|
|
|
|
2021-09-07 11:19:14 +05:30
|
|
|
def on_device_add(self, path, data):
|
2021-09-06 21:54:24 +05:30
|
|
|
"""
|
2021-09-07 11:19:14 +05:30
|
|
|
this function will be run every time a device is added
|
2021-09-06 21:54:24 +05:30
|
|
|
"""
|
2021-09-07 11:19:14 +05:30
|
|
|
if IWD_DEVICE_INTERFACE in data:
|
|
|
|
logger.debug("New device %s found", str(data[IWD_DEVICE_INTERFACE]["Name"]))
|
|
|
|
logger.info("Reloading")
|
|
|
|
self.callback()
|
2021-09-06 21:54:24 +05:30
|
|
|
|
2021-09-07 11:19:14 +05:30
|
|
|
def on_device_remove(self, path, data):
|
2021-09-06 21:54:24 +05:30
|
|
|
"""
|
2021-09-07 11:19:14 +05:30
|
|
|
this function will be run every time a device is removed
|
2021-09-06 21:54:24 +05:30
|
|
|
"""
|
2021-09-07 11:19:14 +05:30
|
|
|
if IWD_DEVICE_INTERFACE in data:
|
|
|
|
logger.debug("A device was removed")
|
|
|
|
logger.info("Reloading")
|
|
|
|
self.callback()
|
2021-09-06 21:54:24 +05:30
|
|
|
|
2021-09-07 11:19:14 +05:30
|
|
|
def add_callback(self, callback):
|
2021-09-06 21:54:24 +05:30
|
|
|
"""
|
2021-09-07 11:19:14 +05:30
|
|
|
register the callback with D-Bus so that callback is
|
|
|
|
run every time a device is added or removed
|
2021-09-06 21:54:24 +05:30
|
|
|
"""
|
2021-09-07 11:19:14 +05:30
|
|
|
self.callback = callback
|
|
|
|
proxy = self.iwd._proxy
|
|
|
|
proxy.InterfacesAdded.connect(self.on_device_add)
|
|
|
|
proxy.InterfacesRemoved.connect(self.on_device_remove)
|
2021-09-06 21:54:24 +05:30
|
|
|
|
|
|
|
def start(self):
|
|
|
|
"""
|
|
|
|
start the daemon
|
|
|
|
"""
|
2021-09-07 11:19:14 +05:30
|
|
|
logger.debug("Starting daemon")
|
|
|
|
self.loop.run()
|