Skip to content

Commit cac90a6

Browse files
committed
refactor common espnow functions
1 parent e30126e commit cac90a6

File tree

4 files changed

+75
-36
lines changed

4 files changed

+75
-36
lines changed

ports/espressif/bindings/espnow/ESPNow.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,11 @@
3636

3737
#include "shared-bindings/util.h"
3838

39+
#include "common-hal/espnow/__init__.h"
3940
#include "common-hal/espnow/ESPNow.h"
4041

4142
#include "esp_now.h"
4243

43-
// Return C pointer to byte memory string/bytes/bytearray in obj.
44-
// Raise ValueError if the length does not match expected len.
45-
static const uint8_t *_get_bytes_len(mp_obj_t obj, size_t len, mp_uint_t rw) {
46-
mp_buffer_info_t bufinfo;
47-
mp_get_buffer_raise(obj, &bufinfo, rw);
48-
mp_arg_validate_length(bufinfo.len, len, MP_QSTR_buffer);
49-
return (uint8_t *)bufinfo.buf;
50-
}
51-
52-
// Return C pointer to the MAC address.
53-
// Raise ValueError if mac is wrong type or is not 6 bytes long.
54-
static const uint8_t *_get_peer_addr(mp_obj_t mac) {
55-
return mp_obj_is_true(mac) ? _get_bytes_len(mac, ESP_NOW_ETH_ALEN, MP_BUFFER_READ) : NULL;
56-
}
57-
5844
// --- Initialisation and Config functions ---
5945

6046
static void check_for_deinit(espnow_obj_t *self) {
@@ -136,7 +122,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espnow___exit___obj, 4, 4, espnow_obj
136122
//| ...
137123
STATIC mp_obj_t espnow_set_pmk(mp_obj_t self_in, mp_obj_t key) {
138124
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
139-
common_hal_espnow_set_pmk(self, _get_bytes_len(key, ESP_NOW_KEY_LEN, MP_BUFFER_READ));
125+
common_hal_espnow_set_pmk(self, common_hal_espnow_get_bytes_len(key, ESP_NOW_KEY_LEN));
140126
return mp_const_none;
141127
}
142128
STATIC MP_DEFINE_CONST_FUN_OBJ_2(espnow_set_pmk_obj, espnow_set_pmk);
@@ -226,7 +212,7 @@ STATIC mp_obj_t espnow_send(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
226212
espnow_obj_t *self = pos_args[0];
227213
check_for_deinit(self);
228214

229-
const uint8_t *peer_addr = _get_peer_addr(args[ARG_mac].u_obj);
215+
const uint8_t *peer_addr = common_hal_espnow_get_bytes_len(args[ARG_mac].u_obj, ESP_NOW_ETH_ALEN);
230216

231217
// Get a pointer to the data buffer of the message
232218
mp_buffer_info_t message;

ports/espressif/bindings/espnow/Peer.c

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,11 @@
2929
#include "py/runtime.h"
3030

3131
#include "bindings/espnow/Peer.h"
32+
#include "common-hal/espnow/__init__.h"
3233

3334
// TODO: check peer already exist
3435
// TODO: check peer dosen't exist
3536

36-
// Return C pointer to the ReadableBuffer.
37-
// Raise ValueError if the length does not match expected len.
38-
static const uint8_t *_get_bytes_len(mp_obj_t obj, size_t len, mp_uint_t rw) {
39-
mp_buffer_info_t bufinfo;
40-
mp_get_buffer_raise(obj, &bufinfo, rw);
41-
mp_arg_validate_length(bufinfo.len, len, MP_QSTR_buffer);
42-
return (uint8_t *)bufinfo.buf;
43-
}
44-
45-
// Return C pointer to the MAC address.
46-
// Raise ValueError if mac is wrong type or is not 6 bytes long.
47-
static const uint8_t *_get_peer_addr(mp_obj_t mac) {
48-
return mp_obj_is_true(mac) ? _get_bytes_len(mac, ESP_NOW_ETH_ALEN, MP_BUFFER_READ) : NULL;
49-
}
50-
5137
//| class Peer:
5238
//| """A data class to store parameters specific to a peer."""
5339
//|
@@ -89,7 +75,7 @@ STATIC mp_obj_t espnow_peer_make_new(const mp_obj_type_t *type, size_t n_args, s
8975
.encrypt = false
9076
};
9177

92-
memcpy(self->peer_info.peer_addr, _get_peer_addr(args[ARG_mac].u_obj), ESP_NOW_ETH_ALEN);
78+
memcpy(self->peer_info.peer_addr, common_hal_espnow_get_bytes_len(args[ARG_mac].u_obj, ESP_NOW_ETH_ALEN), ESP_NOW_ETH_ALEN);
9379

9480
const mp_obj_t channel = args[ARG_channel].u_obj;
9581
if (channel != mp_const_none) {
@@ -108,7 +94,7 @@ STATIC mp_obj_t espnow_peer_make_new(const mp_obj_type_t *type, size_t n_args, s
10894

10995
const mp_obj_t lmk = args[ARG_lmk].u_obj;
11096
if (lmk != mp_const_none) {
111-
memcpy(self->peer_info.lmk, _get_bytes_len(lmk, ESP_NOW_KEY_LEN, MP_BUFFER_READ), ESP_NOW_KEY_LEN);
97+
memcpy(self->peer_info.lmk, common_hal_espnow_get_bytes_len(lmk, ESP_NOW_KEY_LEN), ESP_NOW_KEY_LEN);
11298
} else if (self->peer_info.encrypt && !self->peer_info.lmk) {
11399
mp_raise_ValueError_varg(translate("%q is %q"), MP_QSTR_lmk, MP_QSTR_None);
114100
}
@@ -128,7 +114,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(espnow_peer_get_mac_obj, espnow_peer_get_mac);
128114
STATIC mp_obj_t espnow_peer_set_mac(const mp_obj_t self_in, const mp_obj_t value) {
129115
espnow_peer_obj_t *self = MP_OBJ_TO_PTR(self_in);
130116

131-
memcpy(self->peer_info.peer_addr, _get_peer_addr(value), ESP_NOW_ETH_ALEN);
117+
memcpy(self->peer_info.peer_addr, common_hal_espnow_get_bytes_len(value, ESP_NOW_ETH_ALEN), ESP_NOW_ETH_ALEN);
132118
esp_now_mod_peer(&self->peer_info);
133119

134120
return mp_const_none;
@@ -151,7 +137,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(espnow_peer_get_lmk_obj, espnow_peer_get_lmk);
151137
STATIC mp_obj_t espnow_peer_set_lmk(const mp_obj_t self_in, const mp_obj_t value) {
152138
espnow_peer_obj_t *self = MP_OBJ_TO_PTR(self_in);
153139

154-
memcpy(self->peer_info.lmk, _get_bytes_len(value, ESP_NOW_KEY_LEN, MP_BUFFER_READ), ESP_NOW_KEY_LEN);
140+
memcpy(self->peer_info.lmk, common_hal_espnow_get_bytes_len(value, ESP_NOW_KEY_LEN), ESP_NOW_KEY_LEN);
155141
esp_now_mod_peer(&self->peer_info);
156142

157143
return mp_const_none;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2023 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 "common-hal/espnow/__init__.h"
28+
#include "py/runtime.h"
29+
30+
// Return C pointer to byte memory string/bytes/bytearray in obj.
31+
// Raise ValueError if the length does not match expected len.
32+
const uint8_t *common_hal_espnow_get_bytes_len(mp_obj_t obj, size_t len) {
33+
mp_buffer_info_t bufinfo;
34+
mp_get_buffer_raise(obj, &bufinfo, MP_BUFFER_READ);
35+
mp_arg_validate_length(bufinfo.len, len, MP_QSTR_buffer);
36+
return (uint8_t *)bufinfo.buf;
37+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2023 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+
#pragma once
28+
29+
#include "py/obj.h"
30+
extern const uint8_t *common_hal_espnow_get_bytes_len(mp_obj_t obj, size_t len);

0 commit comments

Comments
 (0)