Continued implementing WiFi functionality for RP2040 based devices (interface deactivation, AP stop, MicroPython config page). mpy_network

Mon, 20 Feb 2023 16:02:02 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 20 Feb 2023 16:02:02 +0100
branch
mpy_network
changeset 9782
67414f28db68
parent 9781
3112f77f722b
child 9783
5f84d5eeee9e

Continued implementing WiFi functionality for RP2040 based devices (interface deactivation, AP stop, MicroPython config page).

src/eric7/MicroPython/Devices/DeviceBase.py file | annotate | diff | comparison | revisions
src/eric7/MicroPython/Devices/RP2040Devices.py file | annotate | diff | comparison | revisions
src/eric7/MicroPython/WifiDialogs/WifiController.py file | annotate | diff | comparison | revisions
src/eric7/Preferences/ConfigurationPages/MicroPythonPage.py file | annotate | diff | comparison | revisions
src/eric7/Preferences/ConfigurationPages/MicroPythonPage.ui file | annotate | diff | comparison | revisions
src/eric7/Preferences/__init__.py file | annotate | diff | comparison | revisions
--- a/src/eric7/MicroPython/Devices/DeviceBase.py	Mon Feb 20 11:42:45 2023 +0100
+++ b/src/eric7/MicroPython/Devices/DeviceBase.py	Mon Feb 20 16:02:02 2023 +0100
@@ -1240,6 +1240,35 @@
         """
         return [], ""
 
+    def deactivateInterface(self, interface):
+        """
+        Public method to deactivate a given WiFi interface of the connected device.
+
+        @param interface designation of the interface to be deactivated (one of 'AP'
+            or 'STA')
+        @type str
+        @return tuple containg a flag indicating success and an error message
+        @rtype tuple of (bool, str)
+        """
+        return True, ""
+
+    def startAccessPoint(self):
+        """
+        Public method to start the access point interface.
+
+        @return tuple containing a flag indicating success and an error message
+        @rtype tuple of (bool, str)
+        """
+
+    def stopAccessPoint(self):
+        """
+        Public method to stop the access point interface.
+
+        @return tuple containg a flag indicating success and an error message
+        @rtype tuple of (bool, str)
+        """
+        return True, ""
+
     def addDeviceWifiEntries(self, menu):
         """
         Public method to add device specific entries to the given menu.
--- a/src/eric7/MicroPython/Devices/RP2040Devices.py	Mon Feb 20 11:42:45 2023 +0100
+++ b/src/eric7/MicroPython/Devices/RP2040Devices.py	Mon Feb 20 16:02:02 2023 +0100
@@ -661,7 +661,7 @@
             # TODO: not yet implemented
             pass
         else:
-            return super().checkInternet()
+            return super().scanNetworks()
 
         out, err = self._interface.execute(command, timeout=15000)
         if err:
@@ -683,6 +683,71 @@
 
         return networks, ""
 
+    def deactivateInterface(self, interface):
+        """
+        Public method to deactivate a given WiFi interface of the connected device.
+
+        @param interface designation of the interface to be deactivated (one of 'AP'
+            or 'STA')
+        @type str
+        @return tuple containg a flag indicating success and an error message
+        @rtype tuple of (bool, str)
+        @exception ValueError raised to indicate a wrong value for the interface type
+        """
+        if interface not in ("STA", "AP"):
+            raise ValueError(
+                "interface must be 'AP' or 'STA', got '{0}'".format(interface)
+            )
+
+        if self._deviceData["wifi_type"] == "picow":
+            command = """
+def deactivate():
+    import network
+    from time import sleep
+
+    wifi = network.WLAN(network.{0}_IF)
+    wifi.active(False)
+    sleep(0.1)
+    print(not wifi.active())
+
+deactivate()
+del deactivate
+""".format(interface)
+        elif self._deviceData["wifi_type"] == "pimoroni":
+            # TODO: not yet implemented
+            pass
+        else:
+            return super().deactivateInterface(interface)
+
+        out, err = self._interface.execute(command, timeout=15000)
+        if err:
+            return False, err
+        else:
+            return out.decode("utf-8").strip() == "True", ""
+
+    def startAccessPoint(self):
+        """
+        Public method to start the access point interface.
+
+        @return tuple containing a flag indicating success and an error message
+        @rtype tuple of (bool, str)
+        """
+
+    def stopAccessPoint(self):
+        """
+        Public method to stop the access point interface.
+
+        @return tuple containg a flag indicating success and an error message
+        @rtype tuple of (bool, str)
+        """
+        if self._deviceData["wifi_type"] == "picow":
+            return self.deactivateInterface("AP")
+        elif self._deviceData["wifi_type"] == "pimoroni":
+            # TODO: not yet implemented
+            pass
+        else:
+            return super().stopAccessPoint()
+
     ############################################################################
     ## RP2 only methods below
     ############################################################################
--- a/src/eric7/MicroPython/WifiDialogs/WifiController.py	Mon Feb 20 11:42:45 2023 +0100
+++ b/src/eric7/MicroPython/WifiDialogs/WifiController.py	Mon Feb 20 16:02:02 2023 +0100
@@ -214,8 +214,22 @@
         """
         Private slot to stop the Access Point interface of the connected device.
         """
-        # TODO: not implemented yet
-        pass
+        ok, err = self.__mpy.getDevice().stopAccessPoint()
+        if ok:
+            EricMessageBox.information(
+                None,
+                self.tr("Stop WiFi Access Point"),
+                self.tr("The WiFi Access Point interface was stopped successfully."),
+            )
+        else:
+            msg = self.tr("<p>The WiFi Access Point could not be stopped.</p>")
+            if err:
+                msg += self.tr("<p>Reason: {0}").format(err)
+            EricMessageBox.critical(
+                None,
+                self.tr("Stop WiFi Access Point"),
+                msg,
+            )
 
     @pyqtSlot()
     def __showConnectedClients(self):
@@ -234,5 +248,19 @@
             or 'STA')
         @type str
         """
-        # TODO: not implemented yet
-        pass
+        ok, err = self.__mpy.getDevice().deactivateInterface(interface)
+        if ok:
+            EricMessageBox.information(
+                None,
+                self.tr("Deactivate WiFi Interface"),
+                self.tr("The WiFi interface was deactivated successfully."),
+            )
+        else:
+            msg = self.tr("<p>The WiFi interface could not be deactivated.</p>")
+            if err:
+                msg += self.tr("<p>Reason: {0}").format(err)
+            EricMessageBox.critical(
+                None,
+                self.tr("Deactivate WiFi Interface"),
+                msg,
+            )
--- a/src/eric7/Preferences/ConfigurationPages/MicroPythonPage.py	Mon Feb 20 11:42:45 2023 +0100
+++ b/src/eric7/Preferences/ConfigurationPages/MicroPythonPage.py	Mon Feb 20 16:02:02 2023 +0100
@@ -42,6 +42,7 @@
         self.setObjectName("MicroPythonPage")
 
         self.showPasswordButton.setIcon(EricPixmapCache.getIcon("showPassword"))
+        self.apShowPasswordButton.setIcon(EricPixmapCache.getIcon("showPassword"))
 
         self.workspacePicker.setMode(EricPathPickerModes.DIRECTORY_MODE)
 
@@ -83,6 +84,16 @@
         self.dfuUtilPathPicker.setMode(EricPathPickerModes.OPEN_FILE_MODE)
         self.dfuUtilPathPicker.setFilters(self.tr("All Files (*)"))
 
+        # populate the WiFi security mode combo box
+        self.apSecurityComboBox.addItem(self.tr("open"), 0)
+        self.apSecurityComboBox.addItem("WEP", 1)
+        self.apSecurityComboBox.addItem("WPA", 2)
+        self.apSecurityComboBox.addItem("WPA2", 3)
+        self.apSecurityComboBox.addItem("WPA/WPA2", 4)
+        self.apSecurityComboBox.addItem("WPA2 (CCMP)", 5)
+        self.apSecurityComboBox.addItem("WPA3", 6)
+        self.apSecurityComboBox.addItem("WPA2/WPA3", 7)
+
         # set initial values
         # workspace
         self.workspacePicker.setText(
@@ -125,6 +136,12 @@
         self.passwordEdit.setText(Preferences.getMicroPython("WifiPassword"))
         self.apSsidEdit.setText(Preferences.getMicroPython("WifiApName"))
         self.apPasswordEdit.setText(Preferences.getMicroPython("WifiApPassword"))
+        index = self.apSecurityComboBox.findData(
+            Preferences.getMicroPython("WifiApAuthMode")
+        )
+        if index == -1:
+            index = 5  # default it to WPA/WPA2 in case of an issue
+        self.apSecurityComboBox.setCurrentIndex(index)
 
         # MPY Cross Compiler
         self.mpyCrossPicker.setText(Preferences.getMicroPython("MpyCrossCompiler"))
@@ -212,6 +229,9 @@
         Preferences.setMicroPython("WifiPassword", self.passwordEdit.text())
         Preferences.setMicroPython("WifiApName", self.apSsidEdit.text())
         Preferences.setMicroPython("WifiApPassword", self.apPasswordEdit.text())
+        Preferences.setMicroPython(
+            "WifiApAuthMode", self.apSecurityComboBox.currentData()
+        )
 
         # MPY Cross Compiler
         Preferences.setMicroPython("MpyCrossCompiler", self.mpyCrossPicker.text())
--- a/src/eric7/Preferences/ConfigurationPages/MicroPythonPage.ui	Mon Feb 20 11:42:45 2023 +0100
+++ b/src/eric7/Preferences/ConfigurationPages/MicroPythonPage.ui	Mon Feb 20 16:02:02 2023 +0100
@@ -7,7 +7,7 @@
     <x>0</x>
     <y>0</y>
     <width>541</width>
-    <height>1551</height>
+    <height>1617</height>
    </rect>
   </property>
   <layout class="QVBoxLayout" name="verticalLayout_3">
@@ -328,7 +328,7 @@
          <item row="0" column="1" colspan="2">
           <widget class="QLineEdit" name="apSsidEdit">
            <property name="toolTip">
-            <string>Enter the network name (SSID) to publih</string>
+            <string>Enter the network name (SSID) to publish</string>
            </property>
            <property name="clearButtonEnabled">
             <bool>true</bool>
@@ -365,6 +365,37 @@
            </property>
           </widget>
          </item>
+         <item row="2" column="0">
+          <widget class="QLabel" name="label_23">
+           <property name="text">
+            <string>Security:</string>
+           </property>
+          </widget>
+         </item>
+         <item row="2" column="1" colspan="2">
+          <layout class="QHBoxLayout" name="horizontalLayout_6">
+           <item>
+            <widget class="QComboBox" name="apSecurityComboBox">
+             <property name="toolTip">
+              <string>Select the security mode</string>
+             </property>
+            </widget>
+           </item>
+           <item>
+            <spacer name="horizontalSpacer_3">
+             <property name="orientation">
+              <enum>Qt::Horizontal</enum>
+             </property>
+             <property name="sizeHint" stdset="0">
+              <size>
+               <width>40</width>
+               <height>20</height>
+              </size>
+             </property>
+            </spacer>
+           </item>
+          </layout>
+         </item>
         </layout>
        </widget>
       </item>
@@ -725,6 +756,7 @@
   <tabstop>apSsidEdit</tabstop>
   <tabstop>apPasswordEdit</tabstop>
   <tabstop>apShowPasswordButton</tabstop>
+  <tabstop>apSecurityComboBox</tabstop>
   <tabstop>mpyCrossPicker</tabstop>
   <tabstop>dfuUtilPathPicker</tabstop>
   <tabstop>micropythonFirmwareUrlLineEdit</tabstop>
--- a/src/eric7/Preferences/__init__.py	Mon Feb 20 11:42:45 2023 +0100
+++ b/src/eric7/Preferences/__init__.py	Mon Feb 20 16:02:02 2023 +0100
@@ -1580,6 +1580,7 @@
         "WifiPassword": "",
         "WifiApName": "",
         "WifiApPassword": "",
+        "WifiApAuthMode": 4,  # WPA/WPA2
         "WifiCountry": "",
         # MicroPython URLs
         "MicroPythonDocuUrl": "https://docs.micropython.org/en/latest/",
@@ -3782,7 +3783,7 @@
     @param key the key of the value to get
     @return the requested MicroPython value
     """
-    if key in ("SerialTimeout", "ChartColorTheme"):
+    if key in ("SerialTimeout", "ChartColorTheme", "WifiApAuthMode"):
         return int(
             Prefs.settings.value("MicroPython/" + key, Prefs.microPythonDefaults[key])
         )

eric ide

mercurial