refactor common espnow functions
This commit is contained in:
parent
e30126e335
commit
cac90a6969
@ -36,25 +36,11 @@
|
||||
|
||||
#include "shared-bindings/util.h"
|
||||
|
||||
#include "common-hal/espnow/__init__.h"
|
||||
#include "common-hal/espnow/ESPNow.h"
|
||||
|
||||
#include "esp_now.h"
|
||||
|
||||
// Return C pointer to byte memory string/bytes/bytearray in obj.
|
||||
// Raise ValueError if the length does not match expected len.
|
||||
static const uint8_t *_get_bytes_len(mp_obj_t obj, size_t len, mp_uint_t rw) {
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(obj, &bufinfo, rw);
|
||||
mp_arg_validate_length(bufinfo.len, len, MP_QSTR_buffer);
|
||||
return (uint8_t *)bufinfo.buf;
|
||||
}
|
||||
|
||||
// Return C pointer to the MAC address.
|
||||
// Raise ValueError if mac is wrong type or is not 6 bytes long.
|
||||
static const uint8_t *_get_peer_addr(mp_obj_t mac) {
|
||||
return mp_obj_is_true(mac) ? _get_bytes_len(mac, ESP_NOW_ETH_ALEN, MP_BUFFER_READ) : NULL;
|
||||
}
|
||||
|
||||
// --- Initialisation and Config functions ---
|
||||
|
||||
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
|
||||
//| ...
|
||||
STATIC mp_obj_t espnow_set_pmk(mp_obj_t self_in, mp_obj_t key) {
|
||||
espnow_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
common_hal_espnow_set_pmk(self, _get_bytes_len(key, ESP_NOW_KEY_LEN, MP_BUFFER_READ));
|
||||
common_hal_espnow_set_pmk(self, common_hal_espnow_get_bytes_len(key, ESP_NOW_KEY_LEN));
|
||||
return mp_const_none;
|
||||
}
|
||||
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
|
||||
espnow_obj_t *self = pos_args[0];
|
||||
check_for_deinit(self);
|
||||
|
||||
const uint8_t *peer_addr = _get_peer_addr(args[ARG_mac].u_obj);
|
||||
const uint8_t *peer_addr = common_hal_espnow_get_bytes_len(args[ARG_mac].u_obj, ESP_NOW_ETH_ALEN);
|
||||
|
||||
// Get a pointer to the data buffer of the message
|
||||
mp_buffer_info_t message;
|
||||
|
@ -29,25 +29,11 @@
|
||||
#include "py/runtime.h"
|
||||
|
||||
#include "bindings/espnow/Peer.h"
|
||||
#include "common-hal/espnow/__init__.h"
|
||||
|
||||
// TODO: check peer already exist
|
||||
// TODO: check peer dosen't exist
|
||||
|
||||
// Return C pointer to the ReadableBuffer.
|
||||
// Raise ValueError if the length does not match expected len.
|
||||
static const uint8_t *_get_bytes_len(mp_obj_t obj, size_t len, mp_uint_t rw) {
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(obj, &bufinfo, rw);
|
||||
mp_arg_validate_length(bufinfo.len, len, MP_QSTR_buffer);
|
||||
return (uint8_t *)bufinfo.buf;
|
||||
}
|
||||
|
||||
// Return C pointer to the MAC address.
|
||||
// Raise ValueError if mac is wrong type or is not 6 bytes long.
|
||||
static const uint8_t *_get_peer_addr(mp_obj_t mac) {
|
||||
return mp_obj_is_true(mac) ? _get_bytes_len(mac, ESP_NOW_ETH_ALEN, MP_BUFFER_READ) : NULL;
|
||||
}
|
||||
|
||||
//| class Peer:
|
||||
//| """A data class to store parameters specific to a peer."""
|
||||
//|
|
||||
@ -89,7 +75,7 @@ STATIC mp_obj_t espnow_peer_make_new(const mp_obj_type_t *type, size_t n_args, s
|
||||
.encrypt = false
|
||||
};
|
||||
|
||||
memcpy(self->peer_info.peer_addr, _get_peer_addr(args[ARG_mac].u_obj), ESP_NOW_ETH_ALEN);
|
||||
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);
|
||||
|
||||
const mp_obj_t channel = args[ARG_channel].u_obj;
|
||||
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
|
||||
|
||||
const mp_obj_t lmk = args[ARG_lmk].u_obj;
|
||||
if (lmk != mp_const_none) {
|
||||
memcpy(self->peer_info.lmk, _get_bytes_len(lmk, ESP_NOW_KEY_LEN, MP_BUFFER_READ), ESP_NOW_KEY_LEN);
|
||||
memcpy(self->peer_info.lmk, common_hal_espnow_get_bytes_len(lmk, ESP_NOW_KEY_LEN), ESP_NOW_KEY_LEN);
|
||||
} else if (self->peer_info.encrypt && !self->peer_info.lmk) {
|
||||
mp_raise_ValueError_varg(translate("%q is %q"), MP_QSTR_lmk, MP_QSTR_None);
|
||||
}
|
||||
@ -128,7 +114,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(espnow_peer_get_mac_obj, espnow_peer_get_mac);
|
||||
STATIC mp_obj_t espnow_peer_set_mac(const mp_obj_t self_in, const mp_obj_t value) {
|
||||
espnow_peer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
|
||||
memcpy(self->peer_info.peer_addr, _get_peer_addr(value), ESP_NOW_ETH_ALEN);
|
||||
memcpy(self->peer_info.peer_addr, common_hal_espnow_get_bytes_len(value, ESP_NOW_ETH_ALEN), ESP_NOW_ETH_ALEN);
|
||||
esp_now_mod_peer(&self->peer_info);
|
||||
|
||||
return mp_const_none;
|
||||
@ -151,7 +137,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(espnow_peer_get_lmk_obj, espnow_peer_get_lmk);
|
||||
STATIC mp_obj_t espnow_peer_set_lmk(const mp_obj_t self_in, const mp_obj_t value) {
|
||||
espnow_peer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
|
||||
memcpy(self->peer_info.lmk, _get_bytes_len(value, ESP_NOW_KEY_LEN, MP_BUFFER_READ), ESP_NOW_KEY_LEN);
|
||||
memcpy(self->peer_info.lmk, common_hal_espnow_get_bytes_len(value, ESP_NOW_KEY_LEN), ESP_NOW_KEY_LEN);
|
||||
esp_now_mod_peer(&self->peer_info);
|
||||
|
||||
return mp_const_none;
|
||||
|
37
ports/espressif/common-hal/espnow/__init__.c
Normal file
37
ports/espressif/common-hal/espnow/__init__.c
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2023 MicroDev
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "common-hal/espnow/__init__.h"
|
||||
#include "py/runtime.h"
|
||||
|
||||
// Return C pointer to byte memory string/bytes/bytearray in obj.
|
||||
// Raise ValueError if the length does not match expected len.
|
||||
const uint8_t *common_hal_espnow_get_bytes_len(mp_obj_t obj, size_t len) {
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(obj, &bufinfo, MP_BUFFER_READ);
|
||||
mp_arg_validate_length(bufinfo.len, len, MP_QSTR_buffer);
|
||||
return (uint8_t *)bufinfo.buf;
|
||||
}
|
30
ports/espressif/common-hal/espnow/__init__.h
Normal file
30
ports/espressif/common-hal/espnow/__init__.h
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2023 MicroDev
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "py/obj.h"
|
||||
extern const uint8_t *common_hal_espnow_get_bytes_len(mp_obj_t obj, size_t len);
|
Loading…
Reference in New Issue
Block a user