Skip to content

Commit 9b36d33

Browse files
committed
move random mac address function into network module
1 parent 6e624b9 commit 9b36d33

File tree

3 files changed

+74
-15
lines changed

3 files changed

+74
-15
lines changed

shared-bindings/wiznet/wiznet5k.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
#include "shared-bindings/digitalio/DigitalInOut.h"
4141
#include "shared-bindings/digitalio/DriveMode.h"
4242
#include "shared-bindings/busio/SPI.h"
43-
#include "shared-bindings/random/__init__.h"
43+
44+
#include "shared-module/network/__init__.h"
4445

4546
#if MICROPY_PY_WIZNET5K
4647

@@ -336,19 +337,6 @@ STATIC mp_obj_t wiznet5k_socket_disconnect(mp_obj_t self_in) {
336337
}
337338
#endif
338339

339-
void create_random_mac_address(uint8_t *mac) {
340-
uint32_t rb1 = shared_modules_random_getrandbits(24);
341-
uint32_t rb2 = shared_modules_random_getrandbits(24);
342-
// first octet has multicast bit (0) cleared and local bit (1) set
343-
// everything else is just set randomly
344-
mac[0] = ((uint8_t)(rb1 >> 16) & 0xfe) | 0x02;
345-
mac[1] = (uint8_t)(rb1 >> 8);
346-
mac[2] = (uint8_t)(rb1);
347-
mac[3] = (uint8_t)(rb2 >> 16);
348-
mac[4] = (uint8_t)(rb2 >> 8);
349-
mac[5] = (uint8_t)(rb2);
350-
}
351-
352340
/******************************************************************************/
353341
// MicroPython bindings
354342

@@ -400,7 +388,7 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size
400388
.dns = {8, 8, 8, 8}, // Google public DNS
401389
.dhcp = NETINFO_STATIC,
402390
};
403-
create_random_mac_address(netinfo.mac);
391+
network_module_create_random_mac_address(netinfo.mac);
404392
ctlnetwork(CN_SET_NETINFO, (void*)&netinfo);
405393

406394
// seems we need a small delay after init

shared-module/network/__init__.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Nick Moore
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/mphal.h"
28+
29+
#include "shared-bindings/random/__init__.h"
30+
31+
void network_module_create_random_mac_address(uint8_t *mac) {
32+
uint32_t rb1 = shared_modules_random_getrandbits(24);
33+
uint32_t rb2 = shared_modules_random_getrandbits(24);
34+
// first octet has multicast bit (0) cleared and local bit (1) set
35+
// everything else is just set randomly
36+
mac[0] = ((uint8_t)(rb1 >> 16) & 0xfe) | 0x02;
37+
mac[1] = (uint8_t)(rb1 >> 8);
38+
mac[2] = (uint8_t)(rb1);
39+
mac[3] = (uint8_t)(rb2 >> 16);
40+
mac[4] = (uint8_t)(rb2 >> 8);
41+
mac[5] = (uint8_t)(rb2);
42+
}
43+

shared-module/network/__init__.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Nick Moore
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+
void network_module_create_random_mac_address(uint8_t *mac);
28+

0 commit comments

Comments
 (0)