Skip to content

Commit 5ef9b10

Browse files
authored
Merge pull request #4685 from microDev1/authmode
Add AuthMode class
2 parents 5679eb4 + 1b972c5 commit 5ef9b10

File tree

4 files changed

+140
-14
lines changed

4 files changed

+140
-14
lines changed

ports/esp32s2/common-hal/wifi/Network.c

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "shared-bindings/wifi/Network.h"
28-
2927
#include <string.h>
3028

31-
#include "py/obj.h"
29+
#include "py/enum.h"
30+
#include "shared-bindings/wifi/Network.h"
31+
#include "shared-bindings/wifi/AuthMode.h"
3232

3333
mp_obj_t common_hal_wifi_network_get_ssid(wifi_network_obj_t *self) {
3434
const char *cstr = (const char *)self->record.ssid;
@@ -56,35 +56,42 @@ mp_obj_t common_hal_wifi_network_get_country(wifi_network_obj_t *self) {
5656
}
5757

5858
mp_obj_t common_hal_wifi_network_get_authmode(wifi_network_obj_t *self) {
59-
const char *authmode = "";
59+
uint8_t authmode_mask = 0;
6060
switch (self->record.authmode) {
6161
case WIFI_AUTH_OPEN:
62-
authmode = "OPEN";
62+
authmode_mask = (1 << AUTHMODE_OPEN);
6363
break;
6464
case WIFI_AUTH_WEP:
65-
authmode = "WEP";
65+
authmode_mask = (1 << AUTHMODE_WEP);
6666
break;
6767
case WIFI_AUTH_WPA_PSK:
68-
authmode = "WPA_PSK";
68+
authmode_mask = (1 << AUTHMODE_WPA) | (1 << AUTHMODE_PSK);
6969
break;
7070
case WIFI_AUTH_WPA2_PSK:
71-
authmode = "WPA2_PSK";
71+
authmode_mask = (1 << AUTHMODE_WPA2) | (1 << AUTHMODE_PSK);
7272
break;
7373
case WIFI_AUTH_WPA_WPA2_PSK:
74-
authmode = "WPA_WPA2_PSK";
74+
authmode_mask = (1 << AUTHMODE_WPA) | (1 << AUTHMODE_WPA2) | (1 << AUTHMODE_PSK);
7575
break;
7676
case WIFI_AUTH_WPA2_ENTERPRISE:
77-
authmode = "WPA2_ENTERPRISE";
77+
authmode_mask = (1 << AUTHMODE_WPA2) | (1 << AUTHMODE_ENTERPRISE);
7878
break;
7979
case WIFI_AUTH_WPA3_PSK:
80-
authmode = "WPA3_PSK";
80+
authmode_mask = (1 << AUTHMODE_WPA3) | (1 << AUTHMODE_PSK);
8181
break;
8282
case WIFI_AUTH_WPA2_WPA3_PSK:
83-
authmode = "WPA2_WPA3_PSK";
83+
authmode_mask = (1 << AUTHMODE_WPA2) | (1 << AUTHMODE_WPA3) | (1 << AUTHMODE_PSK);
8484
break;
8585
default:
86-
authmode = "UNKNOWN";
8786
break;
8887
}
89-
return mp_obj_new_str(authmode, strlen(authmode));
88+
mp_obj_t authmode_list = mp_obj_new_list(0, NULL);
89+
if (authmode_mask != 0) {
90+
for (uint8_t i = 0; i < 8; i++) {
91+
if ((authmode_mask >> i) & 1) {
92+
mp_obj_list_append(authmode_list, cp_enum_find(&wifi_authmode_type, i));
93+
}
94+
}
95+
}
96+
return authmode_list;
9097
}

py/circuitpy_defns.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ $(filter $(SRC_PATTERNS), \
451451
msgpack/__init__.c \
452452
msgpack/ExtType.c \
453453
supervisor/RunReason.c \
454+
wifi/AuthMode.c \
454455
)
455456

456457
SRC_BINDINGS_ENUMS += \

shared-bindings/wifi/AuthMode.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/enum.h"
28+
29+
#include "shared-bindings/wifi/AuthMode.h"
30+
31+
MAKE_ENUM_VALUE(wifi_authmode_type, authmode, OPEN, AUTHMODE_OPEN);
32+
MAKE_ENUM_VALUE(wifi_authmode_type, authmode, WEP, AUTHMODE_WEP);
33+
MAKE_ENUM_VALUE(wifi_authmode_type, authmode, WPA, AUTHMODE_WPA);
34+
MAKE_ENUM_VALUE(wifi_authmode_type, authmode, WPA2, AUTHMODE_WPA2);
35+
MAKE_ENUM_VALUE(wifi_authmode_type, authmode, WPA3, AUTHMODE_WPA3);
36+
MAKE_ENUM_VALUE(wifi_authmode_type, authmode, PSK, AUTHMODE_PSK);
37+
MAKE_ENUM_VALUE(wifi_authmode_type, authmode, ENTERPRISE, AUTHMODE_ENTERPRISE);
38+
39+
//| class AuthMode:
40+
//| """The authentication protocols used by WiFi."""
41+
//|
42+
//| OPEN: object
43+
//| """Open network. No authentication required."""
44+
//|
45+
//| WEP: object
46+
//| """Wired Equivalent Privacy."""
47+
//|
48+
//| WPA: object
49+
//| """Wireless Protected Access."""
50+
//|
51+
//| WPA2: object
52+
//| """Wireless Protected Access 2."""
53+
//|
54+
//| WPA3: object
55+
//| """Wireless Protected Access 3."""
56+
//|
57+
//| PSK: object
58+
//| """Pre-shared Key. (password)"""
59+
//|
60+
//| ENTERPRISE: object
61+
//| """Each user has a unique credential."""
62+
//|
63+
MAKE_ENUM_MAP(wifi_authmode) {
64+
MAKE_ENUM_MAP_ENTRY(authmode, OPEN),
65+
MAKE_ENUM_MAP_ENTRY(authmode, WEP),
66+
MAKE_ENUM_MAP_ENTRY(authmode, WPA),
67+
MAKE_ENUM_MAP_ENTRY(authmode, WPA2),
68+
MAKE_ENUM_MAP_ENTRY(authmode, WPA3),
69+
MAKE_ENUM_MAP_ENTRY(authmode, PSK),
70+
MAKE_ENUM_MAP_ENTRY(authmode, ENTERPRISE),
71+
};
72+
STATIC MP_DEFINE_CONST_DICT(wifi_authmode_locals_dict, wifi_authmode_locals_table);
73+
74+
MAKE_PRINTER(wifi, wifi_authmode);
75+
76+
MAKE_ENUM_TYPE(wifi, AuthMode, wifi_authmode);

shared-bindings/wifi/AuthMode.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_AUTHMODE_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_AUTHMODE_H
29+
30+
typedef enum {
31+
AUTHMODE_OPEN,
32+
AUTHMODE_WEP,
33+
AUTHMODE_WPA,
34+
AUTHMODE_WPA2,
35+
AUTHMODE_WPA3,
36+
AUTHMODE_PSK,
37+
AUTHMODE_ENTERPRISE
38+
} wifi_authmode_t;
39+
40+
extern const mp_obj_type_t wifi_authmode_type;
41+
42+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_AUTHMODE_H

0 commit comments

Comments
 (0)