Skip to content
This repository was archived by the owner on Oct 28, 2023. It is now read-only.

Commit 8227cf2

Browse files
authored
Merge pull request SHA2017-badge#121 from SHA2017-badge/basvs-cleanup-esp-rtcmem
cleanup esp-rtcmem code. throw out-of-range exceptions.
2 parents 9b1f023 + ad64d23 commit 8227cf2

File tree

3 files changed

+141
-40
lines changed

3 files changed

+141
-40
lines changed

esp32/esprtcmem.c

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,77 @@
2626
* THE SOFTWARE.
2727
*/
2828

29-
#include "py/mphal.h"
29+
#include <stdint.h>
30+
#include <string.h>
31+
32+
#include <py/mphal.h>
3033

3134
#include "esprtcmem.h"
3235

33-
static uint8_t RTC_DATA_ATTR rtcmemcontents[USER_RTC_MEM_SIZE] = {0};
36+
static uint8_t RTC_DATA_ATTR rtcmemcontents[USER_RTC_MEM_SIZE] = { 0 };
37+
38+
// @return 0..255 is ok; -1 is error
39+
int
40+
esp_rtcmem_read(uint32_t location)
41+
{
42+
if (location >= USER_RTC_MEM_SIZE)
43+
{
44+
return -1;
45+
}
3446

35-
const uint8_t esp_rtcmem_read(uint32_t location) {
36-
if (location<USER_RTC_MEM_SIZE) {
37-
return rtcmemcontents[location];
38-
} else {
39-
return 0;
40-
}
47+
return rtcmemcontents[location];
4148
}
4249

43-
void esp_rtcmem_read_string(uint32_t location, char *buffer) {
44-
sprintf(buffer, "%s", rtcmemcontents+location);
50+
// @return 0 is ok; -1 is error
51+
int
52+
esp_rtcmem_write(uint32_t location, uint8_t value)
53+
{
54+
if (location >= USER_RTC_MEM_SIZE)
55+
{
56+
return -1;
57+
}
58+
59+
rtcmemcontents[location] = value;
60+
61+
return 0;
4562
}
4663

47-
void esp_rtcmem_write_string(uint32_t location, const char *buffer) {
48-
sprintf((char *)rtcmemcontents+location, "%s", buffer);
64+
// @return 0 is ok; -1 is error
65+
int
66+
esp_rtcmem_read_string(uint32_t location, char *buffer, size_t *buf_len)
67+
{
68+
if (location >= USER_RTC_MEM_SIZE)
69+
{
70+
return -1;
71+
}
72+
73+
size_t maxlen = USER_RTC_MEM_SIZE - location;
74+
size_t len = strnlen((const char *) &rtcmemcontents[location], maxlen);
75+
if (len == maxlen)
76+
{
77+
return -1;
78+
}
79+
80+
if (*buf_len <= len)
81+
{
82+
return -1;
83+
}
84+
*buf_len = len + 1;
85+
memcpy(buffer, &rtcmemcontents[location], len + 1);
86+
87+
return 0;
4988
}
5089

51-
void esp_rtcmem_write(uint32_t location, uint8_t value) {
52-
if (location<USER_RTC_MEM_SIZE) rtcmemcontents[location] = value;
90+
// @return 0 is ok; -1 is error
91+
int
92+
esp_rtcmem_write_string(uint32_t location, const char *buffer)
93+
{
94+
if (location >= USER_RTC_MEM_SIZE || location + strlen(buffer) >= USER_RTC_MEM_SIZE)
95+
{
96+
return -1;
97+
}
98+
99+
memcpy(&rtcmemcontents[location], buffer, strlen(buffer)+1);
100+
101+
return 0;
53102
}

esp32/esprtcmem.h

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
11
#ifndef ESPRTCMEM_H
22
#define ESPRTCMEM_H
33

4-
#include "esp_attr.h"
5-
#include "rom/rtc.h"
4+
#include <stdint.h>
65

7-
#define USER_RTC_MEM_SIZE 256
6+
#include <rom/rtc.h>
7+
#include <esp_attr.h>
88

9-
const uint8_t esp_rtcmem_read(uint32_t location);
10-
void esp_rtcmem_write(uint32_t location, uint8_t value);
11-
void esp_rtcmem_read_string(uint32_t location, char *buffer);
12-
void esp_rtcmem_write_string(uint32_t location, const char *buffer);
9+
#define USER_RTC_MEM_SIZE 1024
1310

14-
#endif
11+
/**
12+
* Read byte from rtcmem on offset location.
13+
*
14+
* @param location The offset in the rtcmem.
15+
* @return 0..255 is ok; -1 is error
16+
*/
17+
extern int esp_rtcmem_read(uint32_t location);
18+
19+
/**
20+
* Write byte to rtcmem on offset location.
21+
*
22+
* @param location The offset in the rtcmem
23+
* @param value The value to write to this offset
24+
* @return 0 is ok; -1 is error
25+
*/
26+
extern int esp_rtcmem_write(uint32_t location, uint8_t value);
27+
28+
/**
29+
* Read zero-terminated string from rtcmem on offset location
30+
*
31+
* @param location The offset in the rtcmem
32+
* @param buffer The destination buffer
33+
* @param buf_len The length of the destination buffer
34+
* on input: the total buffer-length
35+
* on output: the used buffer-length
36+
* @return 0 is ok; -1 is error
37+
*/
38+
extern int esp_rtcmem_read_string(uint32_t location, char *buffer, size_t *buf_len);
39+
40+
/**
41+
* Write zero-terminated string to rtcmem on offfset location
42+
*
43+
* @param location The offset in the rtcmem
44+
* @param buffer The string to write to this offset
45+
* @return 0 is ok; -1 is error
46+
*/
47+
extern int esp_rtcmem_write_string(uint32_t location, const char *buffer);
48+
49+
#endif // ESPRTCMEM_H

esp32/modesp.c

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,38 +122,55 @@ STATIC IRAM_ATTR mp_obj_t esp_flash_user_start(void) {
122122
}
123123
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_flash_user_start_obj, esp_flash_user_start);
124124

125-
STATIC mp_obj_t esp_rtcmem_write_(mp_obj_t pos, mp_obj_t val) {
126-
esp_rtcmem_write(mp_obj_get_int(pos), mp_obj_get_int(val));
127-
return mp_const_none;
125+
STATIC mp_obj_t esp_rtcmem_write_(mp_obj_t _pos, mp_obj_t _val) {
126+
int pos = mp_obj_get_int(_pos);
127+
int val = mp_obj_get_int(_val);
128+
129+
if (val < 0 || val > 255) {
130+
mp_raise_msg(&mp_type_IndexError, "Value out of range");
131+
}
132+
int res = esp_rtcmem_write(pos, val);
133+
if (res < 0) {
134+
mp_raise_msg(&mp_type_IndexError, "Offset out of range");
135+
}
136+
return mp_const_none;
128137
}
129138
STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_rtcmem_write_obj, esp_rtcmem_write_);
130139

131-
STATIC mp_obj_t esp_rtcmem_read_(mp_obj_t pos) {
132-
uint8_t val = esp_rtcmem_read(mp_obj_get_int(pos));
133-
return mp_obj_new_int(val);
140+
STATIC mp_obj_t esp_rtcmem_read_(mp_obj_t _pos) {
141+
int pos = mp_obj_get_int(_pos);
142+
143+
int val = esp_rtcmem_read(pos);
144+
if (val < 0) {
145+
mp_raise_msg(&mp_type_IndexError, "Offset out of range");
146+
}
147+
return mp_obj_new_int(val);
134148
}
135149
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_rtcmem_read_obj, esp_rtcmem_read_);
136150

137151

138152
STATIC mp_obj_t esp_rtcmem_read_string_(mp_uint_t n_args, const mp_obj_t *args) {
139-
int pos = 2;
140-
if (n_args > 0){
141-
pos = mp_obj_get_int(args[0]);
153+
int pos = (n_args == 0) ? 2 : mp_obj_get_int(args[0]);
154+
155+
char str[256];
156+
size_t str_len = sizeof(str);
157+
int res = esp_rtcmem_read_string(pos, str, &str_len);
158+
if (res < 0) {
159+
mp_raise_msg(&mp_type_IndexError, "Offset out of range");
142160
}
143-
char words[USER_RTC_MEM_SIZE];
144-
esp_rtcmem_read_string(pos, words);
145-
return mp_obj_new_str(words, strlen(words), true);
161+
return mp_obj_new_str(str, str_len-1, true);
146162
}
147163
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_rtcmem_read_string_obj, 0, 1, esp_rtcmem_read_string_);
148164

149165
STATIC mp_obj_t esp_rtcmem_write_string_(mp_uint_t n_args, const mp_obj_t *args) {
150-
int pos = 2;
151-
if (n_args > 1){
152-
pos = mp_obj_get_int(args[1]);
166+
const char *str = mp_obj_str_get_str(args[0]);
167+
int pos = (n_args == 1) ? 2 : mp_obj_get_int(args[1]);
168+
169+
int res = esp_rtcmem_write_string(pos, str);
170+
if (res < 0) {
171+
mp_raise_msg(&mp_type_IndexError, "Offset out of range");
153172
}
154-
mp_uint_t len;
155-
esp_rtcmem_write_string(pos, mp_obj_str_get_data(args[0], &len));
156-
return mp_const_none;
173+
return mp_const_none;
157174
}
158175
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_rtcmem_write_string_obj, 1, 2, esp_rtcmem_write_string_);
159176

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy