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);
128114STATIC 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);
151137STATIC 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 ;
0 commit comments