Skip to content

Commit f0e55dd

Browse files
author
BiffoBear
committed
Added a leading _ to top level constants in DHCP.
1 parent bdd23c7 commit f0e55dd

File tree

1 file changed

+92
-92
lines changed

1 file changed

+92
-92
lines changed

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

Lines changed: 92 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -33,57 +33,57 @@
3333

3434

3535
# DHCP State Machine
36-
STATE_DHCP_START = const(0x00)
37-
STATE_DHCP_DISCOVER = const(0x01)
38-
STATE_DHCP_REQUEST = const(0x02)
39-
STATE_DHCP_LEASED = const(0x03)
40-
STATE_DHCP_REREQUEST = const(0x04)
41-
STATE_DHCP_RELEASE = const(0x05)
42-
STATE_DHCP_WAIT = const(0x06)
43-
STATE_DHCP_DISCONN = const(0x07)
36+
_STATE_DHCP_START = const(0x00)
37+
_STATE_DHCP_DISCOVER = const(0x01)
38+
_STATE_DHCP_REQUEST = const(0x02)
39+
_STATE_DHCP_LEASED = const(0x03)
40+
_STATE_DHCP_REREQUEST = const(0x04)
41+
_STATE_DHCP_RELEASE = const(0x05)
42+
_STATE_DHCP_WAIT = const(0x06)
43+
_STATE_DHCP_DISCONN = const(0x07)
4444

4545
# DHCP wait time between attempts
46-
DHCP_WAIT_TIME = const(60)
46+
_DHCP_WAIT_TIME = const(60)
4747

4848
# DHCP Message Types
49-
DHCP_DISCOVER = const(1)
50-
DHCP_OFFER = const(2)
51-
DHCP_REQUEST = const(3)
52-
DHCP_DECLINE = const(4)
53-
DHCP_ACK = const(5)
54-
DHCP_NAK = const(6)
55-
DHCP_RELEASE = const(7)
56-
DHCP_INFORM = const(8)
49+
_DHCP_DISCOVER = const(1)
50+
_DHCP_OFFER = const(2)
51+
_DHCP_REQUEST = const(3)
52+
_DHCP_DECLINE = const(4)
53+
_DHCP_ACK = const(5)
54+
_DHCP_NAK = const(6)
55+
_DHCP_RELEASE = const(7)
56+
_DHCP_INFORM = const(8)
5757

5858
# DHCP Message OP Codes
59-
DHCP_BOOT_REQUEST = const(0x01)
60-
DHCP_BOOT_REPLY = const(0x02)
59+
_DHCP_BOOT_REQUEST = const(0x01)
60+
_DHCP_BOOT_REPLY = const(0x02)
6161

62-
DHCP_HTYPE10MB = const(0x01)
63-
DHCP_HTYPE100MB = const(0x02)
62+
_DHCP_HTYPE10MB = const(0x01)
63+
_DHCP_HTYPE100MB = const(0x02)
6464

65-
DHCP_HLENETHERNET = const(0x06)
66-
DHCP_HOPS = const(0x00)
65+
_DHCP_HLENETHERNET = const(0x06)
66+
_DHCP_HOPS = const(0x00)
6767

68-
MAGIC_COOKIE = const(0x63825363)
69-
MAX_DHCP_OPT = const(0x10)
68+
_MAGIC_COOKIE = const(0x63825363)
69+
_MAX_DHCP_OPT = const(0x10)
7070

7171
# Default DHCP Server port
72-
DHCP_SERVER_PORT = const(67)
72+
_DHCP_SERVER_PORT = const(67)
7373
# DHCP Lease Time, in seconds
74-
DEFAULT_LEASE_TIME = const(900)
75-
BROADCAST_SERVER_ADDR = (255, 255, 255, 255)
74+
_DEFAULT_LEASE_TIME = const(900)
75+
_BROADCAST_SERVER_ADDR = (255, 255, 255, 255)
7676

7777
# DHCP Response Options
78-
MSG_TYPE = 53
79-
SUBNET_MASK = 1
80-
ROUTERS_ON_SUBNET = 3
81-
DNS_SERVERS = 6
82-
DHCP_SERVER_ID = 54
83-
T1_VAL = 58
84-
T2_VAL = 59
85-
LEASE_TIME = 51
86-
OPT_END = 255
78+
_MSG_TYPE = 53
79+
_SUBNET_MASK = 1
80+
_ROUTERS_ON_SUBNET = 3
81+
_DNS_SERVERS = 6
82+
_DHCP_SERVER_ID = 54
83+
_T1_VAL = 58
84+
_T2_VAL = 59
85+
_LEASE_TIME = 51
86+
_OPT_END = 255
8787

8888
# Packet buffer
8989
_BUFF = bytearray(318)
@@ -123,13 +123,13 @@ def __init__(
123123
self._sock = None
124124

125125
# DHCP state machine
126-
self._dhcp_state = STATE_DHCP_START
126+
self._dhcp_state = _STATE_DHCP_START
127127
self._initial_xid = 0
128128
self._transaction_id = 0
129129
self._start_time = 0
130130

131131
# DHCP server configuration
132-
self.dhcp_server_ip = BROADCAST_SERVER_ADDR
132+
self.dhcp_server_ip = _BROADCAST_SERVER_ADDR
133133
self.local_ip = 0
134134
self.gateway_ip = 0
135135
self.subnet_mask = 0
@@ -168,13 +168,13 @@ def send_dhcp_message(
168168
"""
169169
_BUFF[:] = b"\x00" * len(_BUFF)
170170
# OP
171-
_BUFF[0] = DHCP_BOOT_REQUEST
171+
_BUFF[0] = _DHCP_BOOT_REQUEST
172172
# HTYPE
173-
_BUFF[1] = DHCP_HTYPE10MB
173+
_BUFF[1] = _DHCP_HTYPE10MB
174174
# HLEN
175-
_BUFF[2] = DHCP_HLENETHERNET
175+
_BUFF[2] = _DHCP_HLENETHERNET
176176
# HOPS
177-
_BUFF[3] = DHCP_HOPS
177+
_BUFF[3] = _DHCP_HOPS
178178

179179
# Transaction ID (xid)
180180
self._initial_xid = htonl(self._transaction_id)
@@ -203,10 +203,10 @@ def send_dhcp_message(
203203
# NOTE: 192 octets of 0's, BOOTP legacy
204204

205205
# Magic Cookie
206-
_BUFF[236] = (MAGIC_COOKIE >> 24) & 0xFF
207-
_BUFF[237] = (MAGIC_COOKIE >> 16) & 0xFF
208-
_BUFF[238] = (MAGIC_COOKIE >> 8) & 0xFF
209-
_BUFF[239] = MAGIC_COOKIE & 0xFF
206+
_BUFF[236] = (_MAGIC_COOKIE >> 24) & 0xFF
207+
_BUFF[237] = (_MAGIC_COOKIE >> 16) & 0xFF
208+
_BUFF[238] = (_MAGIC_COOKIE >> 8) & 0xFF
209+
_BUFF[239] = _MAGIC_COOKIE & 0xFF
210210

211211
# Option - DHCP Message Type
212212
_BUFF[240] = 53
@@ -230,7 +230,7 @@ def send_dhcp_message(
230230
_BUFF[253] = hostname_len
231231
_BUFF[254:after_hostname] = self._hostname
232232

233-
if state == DHCP_REQUEST and not renew:
233+
if state == _DHCP_REQUEST and not renew:
234234
# Set the parsed local IP addr
235235
_BUFF[after_hostname] = 50
236236
_BUFF[after_hostname + 1] = 0x04
@@ -275,7 +275,7 @@ def parse_dhcp_response(
275275
# -- Parse Packet, FIXED -- #
276276
# Validate OP
277277
assert (
278-
_BUFF[0] == DHCP_BOOT_REPLY
278+
_BUFF[0] == _DHCP_BOOT_REPLY
279279
), "Malformed Packet - \
280280
DHCP message OP is not expected BOOT Reply."
281281

@@ -288,55 +288,55 @@ def parse_dhcp_response(
288288
if _BUFF[28:34] == 0:
289289
return 0, 0
290290

291-
if int.from_bytes(_BUFF[235:240], "big") != MAGIC_COOKIE:
291+
if int.from_bytes(_BUFF[235:240], "big") != _MAGIC_COOKIE:
292292
return 0, 0
293293

294294
# -- Parse Packet, VARIABLE -- #
295295
ptr = 240
296-
while _BUFF[ptr] != OPT_END:
297-
if _BUFF[ptr] == MSG_TYPE:
296+
while _BUFF[ptr] != _OPT_END:
297+
if _BUFF[ptr] == _MSG_TYPE:
298298
ptr += 1
299299
opt_len = _BUFF[ptr]
300300
ptr += opt_len
301301
msg_type = _BUFF[ptr]
302302
ptr += 1
303-
elif _BUFF[ptr] == SUBNET_MASK:
303+
elif _BUFF[ptr] == _SUBNET_MASK:
304304
ptr += 1
305305
opt_len = _BUFF[ptr]
306306
ptr += 1
307307
self.subnet_mask = tuple(_BUFF[ptr : ptr + opt_len])
308308
ptr += opt_len
309-
elif _BUFF[ptr] == DHCP_SERVER_ID:
309+
elif _BUFF[ptr] == _DHCP_SERVER_ID:
310310
ptr += 1
311311
opt_len = _BUFF[ptr]
312312
ptr += 1
313313
self.dhcp_server_ip = tuple(_BUFF[ptr : ptr + opt_len])
314314
ptr += opt_len
315-
elif _BUFF[ptr] == LEASE_TIME:
315+
elif _BUFF[ptr] == _LEASE_TIME:
316316
ptr += 1
317317
opt_len = _BUFF[ptr]
318318
ptr += 1
319319
self._lease_time = int.from_bytes(_BUFF[ptr : ptr + opt_len], "big")
320320
ptr += opt_len
321-
elif _BUFF[ptr] == ROUTERS_ON_SUBNET:
321+
elif _BUFF[ptr] == _ROUTERS_ON_SUBNET:
322322
ptr += 1
323323
opt_len = _BUFF[ptr]
324324
ptr += 1
325325
self.gateway_ip = tuple(_BUFF[ptr : ptr + opt_len])
326326
ptr += opt_len
327-
elif _BUFF[ptr] == DNS_SERVERS:
327+
elif _BUFF[ptr] == _DNS_SERVERS:
328328
ptr += 1
329329
opt_len = _BUFF[ptr]
330330
ptr += 1
331331
self.dns_server_ip = tuple(_BUFF[ptr : ptr + 4])
332332
ptr += opt_len # still increment even though we only read 1 addr.
333-
elif _BUFF[ptr] == T1_VAL:
333+
elif _BUFF[ptr] == _T1_VAL:
334334
ptr += 1
335335
opt_len = _BUFF[ptr]
336336
ptr += 1
337337
self._t1 = int.from_bytes(_BUFF[ptr : ptr + opt_len], "big")
338338
ptr += opt_len
339-
elif _BUFF[ptr] == T2_VAL:
339+
elif _BUFF[ptr] == _T2_VAL:
340340
ptr += 1
341341
opt_len = _BUFF[ptr]
342342
ptr += 1
@@ -379,55 +379,55 @@ def _dhcp_state_machine(self) -> None:
379379
the non-blocking DHCP maintenance function.
380380
"""
381381
if self._eth.link_status:
382-
if self._dhcp_state == STATE_DHCP_DISCONN:
383-
self._dhcp_state = STATE_DHCP_START
382+
if self._dhcp_state == _STATE_DHCP_DISCONN:
383+
self._dhcp_state = _STATE_DHCP_START
384384
else:
385-
if self._dhcp_state != STATE_DHCP_DISCONN:
386-
self._dhcp_state = STATE_DHCP_DISCONN
387-
self.dhcp_server_ip = BROADCAST_SERVER_ADDR
385+
if self._dhcp_state != _STATE_DHCP_DISCONN:
386+
self._dhcp_state = _STATE_DHCP_DISCONN
387+
self.dhcp_server_ip = _BROADCAST_SERVER_ADDR
388388
self._last_lease_time = 0
389389
reset_ip = (0, 0, 0, 0)
390390
self._eth.ifconfig = (reset_ip, reset_ip, reset_ip, reset_ip)
391391
if self._sock is not None:
392392
self._sock.close()
393393
self._sock = None
394394

395-
if self._dhcp_state == STATE_DHCP_START:
395+
if self._dhcp_state == _STATE_DHCP_START:
396396
self._start_time = time.monotonic()
397397
self._transaction_id = (self._transaction_id + 1) & 0x7FFFFFFF
398398
try:
399399
self._sock = socket.socket(type=socket.SOCK_DGRAM)
400400
except RuntimeError:
401401
if self._debug:
402402
print("* DHCP: Failed to allocate socket")
403-
self._dhcp_state = STATE_DHCP_WAIT
403+
self._dhcp_state = _STATE_DHCP_WAIT
404404
else:
405405
self._sock.settimeout(self._response_timeout)
406406
self._sock.bind((None, 68))
407-
self._sock.connect((self.dhcp_server_ip, DHCP_SERVER_PORT))
407+
self._sock.connect((self.dhcp_server_ip, _DHCP_SERVER_PORT))
408408
if self._last_lease_time == 0 or time.monotonic() > (
409409
self._last_lease_time + self._lease_time
410410
):
411411
if self._debug:
412412
print("* DHCP: Send discover to {}".format(self.dhcp_server_ip))
413413
self.send_dhcp_message(
414-
STATE_DHCP_DISCOVER, (time.monotonic() - self._start_time)
414+
_STATE_DHCP_DISCOVER, (time.monotonic() - self._start_time)
415415
)
416-
self._dhcp_state = STATE_DHCP_DISCOVER
416+
self._dhcp_state = _STATE_DHCP_DISCOVER
417417
else:
418418
if self._debug:
419419
print("* DHCP: Send request to {}".format(self.dhcp_server_ip))
420420
self.send_dhcp_message(
421-
DHCP_REQUEST, (time.monotonic() - self._start_time), True
421+
_DHCP_REQUEST, (time.monotonic() - self._start_time), True
422422
)
423-
self._dhcp_state = STATE_DHCP_REQUEST
423+
self._dhcp_state = _STATE_DHCP_REQUEST
424424

425-
elif self._dhcp_state == STATE_DHCP_DISCOVER:
425+
elif self._dhcp_state == _STATE_DHCP_DISCOVER:
426426
if self._sock.available():
427427
if self._debug:
428428
print("* DHCP: Parsing OFFER")
429429
msg_type, xid = self.parse_dhcp_response()
430-
if msg_type == DHCP_OFFER:
430+
if msg_type == _DHCP_OFFER:
431431
# Check if transaction ID matches, otherwise it may be an offer
432432
# for another device
433433
if htonl(self._transaction_id) == int.from_bytes(xid, "big"):
@@ -437,33 +437,33 @@ def _dhcp_state_machine(self) -> None:
437437
)
438438
self._transaction_id = (self._transaction_id + 1) & 0x7FFFFFFF
439439
self.send_dhcp_message(
440-
DHCP_REQUEST, (time.monotonic() - self._start_time)
440+
_DHCP_REQUEST, (time.monotonic() - self._start_time)
441441
)
442-
self._dhcp_state = STATE_DHCP_REQUEST
442+
self._dhcp_state = _STATE_DHCP_REQUEST
443443
else:
444444
if self._debug:
445445
print("* DHCP: Received OFFER with non-matching xid")
446446
else:
447447
if self._debug:
448448
print("* DHCP: Received DHCP Message is not OFFER")
449449

450-
elif self._dhcp_state == STATE_DHCP_REQUEST:
450+
elif self._dhcp_state == _STATE_DHCP_REQUEST:
451451
if self._sock.available():
452452
if self._debug:
453453
print("* DHCP: Parsing ACK")
454454
msg_type, xid = self.parse_dhcp_response()
455455
# Check if transaction ID matches, otherwise it may be
456456
# for another device
457457
if htonl(self._transaction_id) == int.from_bytes(xid, "big"):
458-
if msg_type == DHCP_ACK:
458+
if msg_type == _DHCP_ACK:
459459
if self._debug:
460460
print("* DHCP: Successful lease")
461461
self._sock.close()
462462
self._sock = None
463-
self._dhcp_state = STATE_DHCP_LEASED
463+
self._dhcp_state = _STATE_DHCP_LEASED
464464
self._last_lease_time = self._start_time
465465
if self._lease_time == 0:
466-
self._lease_time = DEFAULT_LEASE_TIME
466+
self._lease_time = _DEFAULT_LEASE_TIME
467467
if self._t1 == 0:
468468
# T1 is 50% of _lease_time
469469
self._t1 = self._lease_time >> 1
@@ -486,41 +486,41 @@ def _dhcp_state_machine(self) -> None:
486486
if self._debug:
487487
print("* DHCP: Received non-matching xid")
488488

489-
elif self._dhcp_state == STATE_DHCP_WAIT:
490-
if time.monotonic() > (self._start_time + DHCP_WAIT_TIME):
489+
elif self._dhcp_state == _STATE_DHCP_WAIT:
490+
if time.monotonic() > (self._start_time + _DHCP_WAIT_TIME):
491491
if self._debug:
492492
print("* DHCP: Begin retry")
493-
self._dhcp_state = STATE_DHCP_START
493+
self._dhcp_state = _STATE_DHCP_START
494494
if time.monotonic() > (self._last_lease_time + self._rebind_in_sec):
495-
self.dhcp_server_ip = BROADCAST_SERVER_ADDR
495+
self.dhcp_server_ip = _BROADCAST_SERVER_ADDR
496496
if time.monotonic() > (self._last_lease_time + self._lease_time):
497497
reset_ip = (0, 0, 0, 0)
498498
self._eth.ifconfig = (reset_ip, reset_ip, reset_ip, reset_ip)
499499

500-
elif self._dhcp_state == STATE_DHCP_LEASED:
500+
elif self._dhcp_state == _STATE_DHCP_LEASED:
501501
if time.monotonic() > (self._last_lease_time + self._renew_in_sec):
502-
self._dhcp_state = STATE_DHCP_START
502+
self._dhcp_state = _STATE_DHCP_START
503503
if self._debug:
504504
print("* DHCP: Time to renew lease")
505505

506506
if self._dhcp_state in (
507-
STATE_DHCP_DISCOVER,
508-
STATE_DHCP_REQUEST,
507+
_STATE_DHCP_DISCOVER,
508+
_STATE_DHCP_REQUEST,
509509
) and time.monotonic() > (self._start_time + self._response_timeout):
510-
self._dhcp_state = STATE_DHCP_WAIT
510+
self._dhcp_state = _STATE_DHCP_WAIT
511511
if self._sock is not None:
512512
self._sock.close()
513513
self._sock = None
514514

515515
def request_dhcp_lease(self) -> bool:
516516
"""Request to renew or acquire a DHCP lease."""
517-
if self._dhcp_state in (STATE_DHCP_LEASED, STATE_DHCP_WAIT):
518-
self._dhcp_state = STATE_DHCP_START
517+
if self._dhcp_state in (_STATE_DHCP_LEASED, _STATE_DHCP_WAIT):
518+
self._dhcp_state = _STATE_DHCP_START
519519

520-
while self._dhcp_state not in (STATE_DHCP_LEASED, STATE_DHCP_WAIT):
520+
while self._dhcp_state not in (_STATE_DHCP_LEASED, _STATE_DHCP_WAIT):
521521
self._dhcp_state_machine()
522522

523-
return self._dhcp_state == STATE_DHCP_LEASED
523+
return self._dhcp_state == _STATE_DHCP_LEASED
524524

525525
def maintain_dhcp_lease(self) -> None:
526526
"""Maintain DHCP lease"""

0 commit comments

Comments
 (0)