Skip to content

Commit 48d7825

Browse files
committed
Validation of carrier available for every board.
1 parent 703a55b commit 48d7825

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

LiveObjects/hal.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import os
88
import time
9+
import sys
910

1011
import LiveObjects
1112

@@ -38,6 +39,11 @@ def mqtt_lib_import_str(lang):
3839
def get_security_level(self):
3940
pass
4041

42+
def check_network_capabilities(self, net_type):
43+
if net_type not in self._carrier_capability:
44+
print('Carrier not supported.')
45+
sys.exit()
46+
4147
def network_connect(self):
4248
pass
4349

@@ -53,12 +59,14 @@ class GPy(BoardsInterface):
5359
def __init__(self, net_type):
5460
self._lang = 'microPython'
5561
self._net_type = BoardsInterface.WIFI if net_type == BoardsInterface.DEFAULT_CARRIER else net_type
62+
self._carrier_capability = (BoardsInterface.WIFI, BoardsInterface.LTE)
5663
self._wifi_tls_capability = True
5764
self._lte_tls_capability = False
5865
self._mqtt_lib = super().mqtt_lib_import_str(self._lang)
5966
self._credentials = super().create_credentials(self._net_type)
6067

6168
def network_connect(self):
69+
super().check_network_capabilities(self._net_type)
6270
if self._net_type == BoardsInterface.WIFI:
6371
pycom_wifi_connect(self._credentials.get_creds()['ssid'], self._credentials.get_creds()['password'])
6472
elif self._net_type == BoardsInterface.LTE:
@@ -74,19 +82,19 @@ def get_security_level(self):
7482
class Esp8266(BoardsInterface):
7583
def __init__(self, net_type):
7684
self._lang = 'microPython'
77-
self._net_type = BoardsInterface.WIFI if net_type == BoardsInterface.DEFAULT_CARRIER else None
85+
self._net_type = BoardsInterface.WIFI if net_type == BoardsInterface.DEFAULT_CARRIER else net_type
86+
self._carrier_capability = (BoardsInterface.WIFI,)
7887
self._wifi_tls_capability = False
7988
self._wifi_lte_capability = False
8089
self._mqtt_lib = super().mqtt_lib_import_str(self._lang)
81-
self._credentials = super().create_credentials(BoardsInterface.WIFI)
90+
self._credentials = super().create_credentials(self._net_type)
8291

8392
def network_connect(self):
84-
if self._net_type == BoardsInterface.WIFI:
85-
wifi_connect(self._credentials.get_creds()['ssid'], self._credentials.get_creds()['password'])
93+
super().check_network_capabilities(self._net_type)
94+
wifi_connect(self._credentials.get_creds()['ssid'], self._credentials.get_creds()['password'])
8695

8796
def get_security_level(self):
88-
if self._net_type == BoardsInterface.WIFI:
89-
return LiveObjects.SSL if self._wifi_tls_capability else LiveObjects.NONE
97+
return LiveObjects.SSL if self._wifi_tls_capability else LiveObjects.NONE
9098

9199

92100
class Win32(BoardsInterface):
@@ -96,37 +104,37 @@ class Win32(BoardsInterface):
96104
class Esp32(BoardsInterface):
97105
def __init__(self, net_type):
98106
self._lang = 'microPython'
99-
self._net_type = BoardsInterface.WIFI if net_type == BoardsInterface.DEFAULT_CARRIER else None
107+
self._net_type = BoardsInterface.WIFI if net_type == BoardsInterface.DEFAULT_CARRIER else net_type
108+
self._carrier_capability = (BoardsInterface.WIFI,)
100109
self._wifi_tls_capability = True
101110
self._wifi_lte_capability = False
102111
self._mqtt_lib = super().mqtt_lib_import_str(self._lang)
103-
self._credentials = super().create_credentials(BoardsInterface.WIFI)
112+
self._credentials = super().create_credentials(self._net_type)
104113

105114
def network_connect(self):
106-
if self._net_type == BoardsInterface.WIFI:
107-
wifi_connect(self._credentials.get_creds()['ssid'], self._credentials.get_creds()['password'])
115+
super().check_network_capabilities(self._net_type)
116+
wifi_connect(self._credentials.get_creds()['ssid'], self._credentials.get_creds()['password'])
108117

109118
def get_security_level(self):
110-
if self._net_type == BoardsInterface.WIFI:
111-
return LiveObjects.SSL if self._wifi_tls_capability else LiveObjects.NONE
119+
return LiveObjects.SSL if self._wifi_tls_capability else LiveObjects.NONE
112120

113121

114122
class Linux(BoardsInterface):
115123
def __init__(self, net_type):
116124
self._lang = 'Python'
117-
self._net_type = BoardsInterface.EXISTING_NETWORK if net_type == BoardsInterface.DEFAULT_CARRIER else None
125+
self._net_type = BoardsInterface.EXISTING_NETWORK if net_type == BoardsInterface.DEFAULT_CARRIER else net_type
126+
self._carrier_capability = (BoardsInterface.EXISTING_NETWORK,)
118127
self._wifi_tls_capability = True
119128
self._wifi_lte_capability = False
120129
self._mqtt_lib = super().mqtt_lib_import_str(self._lang)
121130
self._credentials = super().create_credentials(self._net_type)
122131

123132
def network_connect(self):
124-
if self._net_type == BoardsInterface.EXISTING_NETWORK:
125-
use_existing_network_connection()
133+
super().check_network_capabilities(self._net_type)
134+
use_existing_network_connection()
126135

127136
def get_security_level(self):
128-
if self._net_type == BoardsInterface.EXISTING_NETWORK:
129-
return LiveObjects.SSL if self._wifi_tls_capability else LiveObjects.NONE
137+
return LiveObjects.SSL if self._wifi_tls_capability else LiveObjects.NONE
130138

131139

132140
class BoardsFactory:
@@ -159,7 +167,6 @@ def wifi_connect(ssid, password):
159167
CONN_TIMEOUT = 20
160168

161169

162-
# noinspection PyUnresolvedReferences
163170
def pycom_wifi_connect(ssid, password):
164171
from network import WLAN
165172

@@ -171,15 +178,14 @@ def pycom_wifi_connect(ssid, password):
171178
wlan.connect(ssid=ssid, auth=(WLAN.WPA2, password))
172179
time.sleep_ms(3000)
173180
if wlan.isconnected():
174-
print("WiFi connected succesfully")
181+
print("WiFi connected successfully")
175182
print('IPs:', wlan.ifconfig(), 'Channel:', wlan.channel())
176183
break
177184
elif time.time() - start_time > CONN_TIMEOUT:
178185
print("WiFi not connected. Stopped.")
179186
break
180187

181188

182-
# noinspection PyUnresolvedReferences
183189
def lte_connect(pin):
184190

185191
from network import LTE

0 commit comments

Comments
 (0)