8000 ... start on espnow · micropython/micropython@5926660 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5926660

Browse files
nickzoicshawwwn
authored andcommitted
... start on espnow
(cherry picked from commit 6177511a5a4c5cd0ea25e7bd6999fa8c7353372e)
1 parent 7fbad16 commit 5926660

File tree

5 files changed

+236
-9
lines changed

5 files changed

+236
-9
lines changed

ports/esp32/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,13 @@ SRC_C = \
171171
network_lan.c \
172172
modsocket.c \
173173
modesp.c \
174+
<<<<<<< HEAD
174175
esp32_ulp.c \
175176
modesp32.c \
177+
=======
178+
esp_espnow.c \
179+
moduhashlib.c \
180+
>>>>>>> 6177511a5... ... start on espnow
176181
espneopixel.c \
177182
machine_hw_spi.c \
178183
machine_wdt.c \
@@ -679,7 +684,11 @@ APP_LD_ARGS += -L$(dir $(LIBGCC_FILE_NAME)) -lgcc
679684
APP_LD_ARGS += -L$(dir $(LIBSTDCXX_FILE_NAME)) -lstdc++
680685
APP_LD_ARGS += $(LIBC_LIBM)
681686
APP_LD_ARGS += $(ESPCOMP)/esp32/libhal.a
687+
<<<<<<< HEAD
682688
APP_LD_ARGS += -L$(ESPCOMP)/esp32/lib -lcore -lmesh -lnet80211 -lphy -lrtc -lpp -lwpa -lsmartconfig -lcoexist -lwps -lwpa2
689+
=======
690+
APP_LD_ARGS += -L$(ESPCOMP)/esp32/lib -lcore -lnet80211 -lphy -lrtc -lpp -lwpa -lsmartconfig -lcoexist -lwps -lwpa2 -lespnow
691+
>>>>>>> 6177511a5... ... start on espnow
683692
APP_LD_ARGS += $(OBJ)
684693
APP_LD_ARGS += --end-group
685694

ports/esp32/esp_espnow.c

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 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+
28+
#include <stdio.h>
29+
#include <stdint.h>
30+
#include <string.h>
31+
32+
#include "esp_log.h"
33+
#include "esp_now.h"
34+
#include "esp_wifi.h"
35+
36+
#include "py/runtime.h"
37+
#include "py/mphal.h"
38+
#include "py/nlr.h"
39+
#include "py/objlist.h"
40+
#include "py/runtime.h"
41+
#include "py/mphal.h"
42+
#include "py/mperrno.h"
43+
44+
#include "modnetwork.h"
45+
46+
NORETURN void _esp_espnow_exceptions(esp_err_t e) {
47+
switch (e) {
48+
case ESP_ERR_ESPNOW_NOT_INIT:
49+
mp_raise_msg(&mp_type_OSError, "ESP-Now Not Initialized");
50+
case ESP_ERR_ESPNOW_ARG:
51+
mp_raise_msg(&mp_type_OSError, "ESP-Now Invalid Argument");
52+
case ESP_ERR_ESPNOW_NO_MEM:
53+
mp_raise_msg(&mp_type_OSError, "ESP-Now Out Of Mem");
54+
case ESP_ERR_ESPNOW_FULL:
55+
mp_raise_msg(&mp_type_OSError, "ESP-Now Peer List Full");
56+
case ESP_ERR_ESPNOW_NOT_FOUND:
57+
mp_raise_msg(&mp_type_OSError, "ESP-Now Peer Not Found");
58+
case ESP_ERR_ESPNOW_INTERNAL:
59+
mp_raise_msg(&mp_type_OSError, "ESP-Now Internal");
60+
case ESP_ERR_ESPNOW_EXIST:
61+
mp_raise_msg(&mp_type_OSError, "ESP-Now Peer Exists");
62+
default:
63+
nlr_raise(mp_obj_new_exception_msg_varg(
64+
&mp_type_RuntimeError, "ESP-Now Unknown Error 0x%04x", e
65+
));
66+
}
67+
}
68+
69+
static inline void esp_espnow_exceptions(esp_err_t e) {
70+
if (e != ESP_OK) _esp_espnow_exceptions(e);
71+
}
72+
73+
static inline void _get_bytes(mp_obj_t str, size_t len, uint8_t *dst) {
74+
size_t str_len;
75+
const char *data = mp_obj_str_get_data(str, &str_len);
76+
if (str_len != len) mp_raise_ValueError("bad len");
77+
memcpy(dst, data, len);
78+
}
79+
80+
// this is crap of course but lets try it
81+
82+
static int recv_buf_len = 0;
83+
uint8_t recv_mac[6];
84+
static uint8_t recv_buffer[250];
85+
86+
STATIC mp_obj_t espnow_recv() {
87+
if (recv_buf_len < 1) return mp_const_none;
88+
mp_obj_tuple_t *msg = mp_obj_new_tuple(2, NULL);
89+
msg->items[0] = mp_obj_new_bytes(recv_mac, sizeof(recv_mac));
90+
msg->items[1] = mp_obj_new_bytes(recv_buffer, recv_buf_len);
91+
recv_buf_len = 0;
92+
return msg;
93+
}
94+
95+
MP_DEFINE_CONST_FUN_OBJ_0(espnow_recv_obj, espnow_recv);
96+
97+
void simple_cb(const uint8_t *macaddr, const uint8_t *data, int len)
98+
{
99+
if (len < sizeof(recv_buffer)) {
100+
memcpy(recv_buffer, data, len);
101+
memcpy(recv_mac, macaddr, 6);
102+
recv_buf_len = len;
103+
}
104+
}
105+
106+
static int initialized = 0;
107+
108+
STATIC mp_obj_t espnow_init() {
109+
if (!initialized) {
110+
esp_now_init();
111+
initialized = 1;
112+
esp_now_register_recv_cb(simple_cb);
113+
}
114+
return mp_const_none;
115+
}
116+
MP_DEFINE_CONST_FUN_OBJ_0(espnow_init_obj, espnow_init);
117+
118+
STATIC mp_obj_t espnow_deinit() {
119+
if (initialized) {
120+
esp_now_deinit();
121+
initialized = 0;
122+
}
123+
return mp_const_none;
124+
}
125+
MP_DEFINE_CONST_FUN_OBJ_0(espnow_deinit_obj, espnow_deinit);
126+
127+
STATIC mp_obj_t espnow_set_pmk(mp_obj_t pmk) {
128+
uint8_t buf[ESP_NOW_ETH_ALEN];
129+
_get_bytes(pmk, ESP_NOW_ETH_ALEN, buf);
130+
esp_espnow_exceptions(esp_now_set_pmk(buf));
131+
return mp_const_none;
132+
}
133+
MP_DEFINE_CONST_FUN_OBJ_1(espnow_set_pmk_obj, espnow_set_pmk);
134+
135+
STATIC mp_obj_t espnow_add_peer(size_t n_args, const mp_obj_t *args) {
136+
esp_now_peer_info_t peer = {0};
137+
peer.ifidx = ((wlan_if_obj_t *)MP_OBJ_TO_PTR(args[0]))->if_id;
138+
_get_bytes(args[1], ESP_NOW_ETH_ALEN, peer.peer_addr);
139+
_get_bytes(args[2], ESP_NOW_KEY_LEN, peer.lmk);
140+
peer.encrypt = (n_args > 3 && mp_obj_is_true(args[3])) ? 1 : 0;
141+
// leaving channel as 0 for autodetect
142+
esp_espnow_exceptions(esp_now_add_peer(&peer));
143+
return mp_const_none;
144+
}
145+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espnow_add_peer_obj, 3, 4, espnow_add_peer);
146+
147+
STATIC mp_obj_t espnow_send(mp_obj_t addr, mp_obj_t msg) {
148+
mp_uint_t len1;
149+
const uint8_t *buf1 = (const uint8_t *)mp_obj_str_get_data(addr, &len1);
150+
mp_uint_t len2;
151+
const uint8_t *buf2 = (const uint8_t *)mp_obj_str_get_data(msg, &len2);
152+
if (len1 != ESP_NOW_ETH_ALEN) mp_raise_ValueError("addr invalid");
153+
if (len2 > ESP_NOW_MAX_DATA_LEN) mp_raise_ValueError("Msg too long");
154+
esp_espnow_exceptions(esp_now_send(buf1, buf2, len2));
155+
return mp_const_none;
156+
}
157+
MP_DEFINE_CONST_FUN_OBJ_2(espnow_send_obj, espnow_send);
158+
159+
STATIC mp_obj_t espnow_send_all(mp_obj_t msg) {
160+
mp_uint_t len;
161+
const uint8_t *buf = (const uint8_t *)mp_obj_str_get_data(msg, &len);
162+
if (len > ESP_NOW_MAX_DATA_LEN) mp_raise_ValueError("Msg too long");
163+
esp_espnow_exceptions(esp_now_send(NULL, buf, len));
164+
return mp_const_none;
165+
}
166+
MP_DEFINE_CONST_FUN_OBJ_1(espnow_send_all_obj, espnow_send_all);
167+
168+
STATIC const mp_rom_map_elem_t espnow_globals_dict_table[] = {
169+
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&espnow_init_obj) },
170+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&espnow_deinit_obj) },
171+
{ MP_ROM_QSTR(MP_QSTR_set_pmk), MP_ROM_PTR(&espnow_set_pmk_obj) },
172+
{ MP_ROM_QSTR(MP_QSTR_add_peer), MP_ROM_PTR(&espnow_add_peer_obj) },
173+
{ MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&espnow_send_obj) },
174+
{ MP_ROM_QSTR(MP_QSTR_send_all), MP_ROM_PTR(&espnow_send_all_obj) },
175+
{ MP_ROM_QSTR(MP_QSTR_recv), MP_ROM_PTR(&espnow_recv_obj) },
176+
};
177+
STATIC MP_DEFINE_CONST_DICT(espnow_globals_dict, espnow_globals_dict_table);
178+
179+
const mp_obj_module_t mp_module_esp_espnow = {
180+
.base = { &mp_type_module },
181+
.globals = (mp_obj_dict_t *)&espnow_globals_dict,
182+
};

ports/esp32/modesp.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ STATIC mp_obj_t esp_neopixel_write_(mp_obj_t pin, mp_obj_t buf, mp_obj_t timing)
122122
}
123123
STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp_neopixel_write_obj, esp_neopixel_write_);
124124

125+
extern const mp_obj_module_t mp_module_esp_espnow;
126+
125127
STATIC const mp_rom_map_elem_t esp_module_globals_table[] = {
126128
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_esp) },
127129

@@ -138,6 +140,7 @@ STATIC const mp_rom_map_elem_t esp_module_globals_table[] = {
138140

139141
{ MP_ROM_QSTR(MP_QSTR_neopixel_write), MP_ROM_PTR(&esp_neopixel_write_obj) },
140142
{ MP_ROM_QSTR(MP_QSTR_dht_readinto), MP_ROM_PTR(&dht_readinto_obj) },
143+
<<<<<<< HEAD
141144

142145
// Constants for second arg of osdebug()
143146
{ MP_ROM_QSTR(MP_QSTR_LOG_NONE), MP_ROM_INT((mp_uint_t)ESP_LOG_NONE)},
@@ -146,6 +149,9 @@ STATIC const mp_rom_map_elem_t esp_module_globals_table[] = {
146149
{ MP_ROM_QSTR(MP_QSTR_LOG_INFO), MP_ROM_INT((mp_uint_t)ESP_LOG_INFO)},
147150
{ MP_ROM_QSTR(MP_QSTR_LOG_DEBUG), MP_ROM_INT((mp_uint_t)ESP_LOG_DEBUG)},
148151
{ MP_ROM_QSTR(MP_QSTR_LOG_VERBOSE), MP_ROM_INT((mp_uint_t)ESP_LOG_VERBOSE)},
152+
=======
153+
{ MP_ROM_QSTR(MP_QSTR_espnow), MP_ROM_PTR(&mp_module_esp_espnow) },
154+
>>>>>>> 6177511a5... ... start on espnow
149155
};
150156

151157
STATIC MP_DEFINE_CONST_DICT(esp_module_globals, esp_module_globals_table);

ports/esp32/modnetwork.c

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
#define MODNETWORK_INCLUDE_CONSTANTS (1)
5555

56-
NORETURN void _esp_exceptions(esp_err_t e) {
56+
NORETURN void _esp_network_exceptions(esp_err_t e) {
5757
switch (e) {
5858
case ESP_ERR_WIFI_NOT_INIT:
5959
mp_raise_msg(&mp_type_OSError, "Wifi Not Initialized");
@@ -100,16 +100,11 @@ NORETURN void _esp_exceptions(esp_err_t e) {
100100
}
101101
}
102102

103-
static inline void esp_exceptions(esp_err_t e) {
104-
if (e != ESP_OK) _esp_exceptions(e);
103+
static inline void esp_network_exceptions(esp_err_t e) {
104+
if (e != ESP_OK) _esp_network_exceptions(e);
105105
}
106106

107-
#define ESP_EXCEPTIONS(x) do { esp_exceptions(x); } while (0);
108-
109-
typedef struct _wlan_if_obj_t {
110-
mp_obj_base_t base;
111-
int if_id;
112-
} wlan_if_obj_t;
107+
#define ESP_EXCEPTIONS(x) do { esp_network_exceptions(x); } while (0);
113108

114109
const mp_obj_type_t wlan_if_type;
115110
STATIC const wlan_if_obj_t wlan_sta_obj = {{&wlan_if_type}, WIFI_IF_STA};
@@ -491,12 +486,38 @@ STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) {
491486
ESP_EXCEPTIONS(tcpip_adapter_dhcps_start(WIFI_IF_AP));
492487
}
493488
} else {
489+
<<<<<<< HEAD
494490
// check for the correct string
495491
const char *mode = mp_obj_str_get_str(args[1]);
496492
if ((self->if_id != WIFI_IF_STA && self->if_id != ESP_IF_ETH) || strcmp("dhcp", mode)) {
497493
mp_raise_ValueError("invalid arguments");
498494
}
499495
ESP_EXCEPTIONS(tcpip_adapter_dhcpc_start(self->if_id));
496+
=======
497+
netutils_parse_ipv4_addr(items[1], (void*)&info.netmask, NETUTILS_BIG);
498+
}
499+
netutils_parse_ipv4_addr(items[2], (void*)&info.gw, NETUTILS_BIG);
500+
netutils_parse_ipv4_addr(items[3], (void*)&dns_info.ip, NETUTILS_BIG);
501+
// To set a static IP we have to disable DHCP first
502+
<<<<<<< HEAD
503+
if (self->if_id == WIFI_IF_STA || self->if_id == ESP_IF_ETH) {
504+
esp_err_t e = tcpip_adapter_dhcpc_stop(self->if_id);
505+
if (e != ESP_OK && e != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) _esp_exceptions(e);
506+
ESP_EXCEPTIONS(tcpip_adapter_set_ip_info(self->if_id, &info));
507+
ESP_EXCEPTIONS(tcpip_adapter_set_dns_info(self->if_id, TCPIP_ADAPTER_DNS_MAIN, &dns_info));
508+
=======
509+
if (self->if_id == WIFI_IF_STA) {
510+
esp_err_t e = tcpip_adapter_dhcpc_stop(WIFI_IF_STA);
511+
if (e != ESP_OK && e != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) _esp_network_exceptions(e);
512+
ESP_EXCEPTIONS(tcpip_adapter_set_ip_info(WIFI_IF_STA, &info));
513+
>>>>>>> ... start on espnow
514+
} else if (self->if_id == WIFI_IF_AP) {
515+
esp_err_t e = tcpip_adapter_dhcps_stop(WIFI_IF_AP);
516+
if (e != ESP_OK && e != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) _esp_network_exceptions(e);
517+
ESP_EXCEPTIONS(tcpip_adapter_set_ip_info(WIFI_IF_AP, &info));
518+
ESP_EXCEPTIONS(tcpip_adapter_set_dns_info(WIFI_IF_AP, TCPIP_ADAPTER_DNS_MAIN, &dns_info));
519+
ESP_EXCEPTIONS(tcpip_adapter_dhcps_start(WIFI_IF_AP));
520+
>>>>>>> 6177511a5... ... start on espnow
500521
}
501522
return mp_const_none;
502523
}

ports/esp32/modnetwork.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ enum { PHY_LAN8720, PHY_TLK110 };
3131
MP_DECLARE_CONST_FUN_OBJ_KW(get_lan_obj);
3232
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj);
3333

34+
<<<<<<< HEAD
3435
void usocket_events_deinit(void);
3536

3637
#endif
38+
=======
39+
typedef struct _wlan_if_obj_t {
40+
mp_obj_base_t base;
41+
int if_id;
42+
} wlan_if_obj_t;
43+
44+
#endif // MICROPY_INCLUDED_ESP32_MODESP_MODNETWORK_H
45+
>>>>>>> 6177511a5... ... start on espnow

0 commit comments

Comments
 (0)
0