1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
19212: shell/rtc: use rtc_tm_normalize() to sanitize input r=benpicco a=benpicco



19360: gcoap: make use coap_build_reply() in gcoap_resp_init() r=benpicco a=benpicco



19401: shell/cmds: add genfile command r=benpicco a=benpicco



19645: sys/isrpipe: Replace xtimer with ztimer_usec r=benpicco a=MrKevinWeiss



### Contribution description

Getting ready for the xtimer dep.


### Testing procedure

Green murdock, there is no explicit test for isrpipe but since it runs xtimer compat it should operate the same.

### Issues/PRs references



19720: tests: remove unnecessary use of floating point r=benpicco a=benpicco



Co-authored-by: Benjamin Valentin <benjamin.valentin@bht-berlin.de>
Co-authored-by: Benjamin Valentin <benjamin.valentin@ml-pa.com>
Co-authored-by: MrKevinWeiss <weiss.kevin604@gmail.com>
Co-authored-by: Benjamin Valentin <benpicco@beuth-hochschule.de>
This commit is contained in:
bors[bot] 2023-06-08 16:02:09 +00:00 committed by GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 136 additions and 48 deletions

View File

@ -444,6 +444,7 @@ PSEUDOMODULES += shell_cmd_coreclk
PSEUDOMODULES += shell_cmd_cryptoauthlib
PSEUDOMODULES += shell_cmd_dfplayer
PSEUDOMODULES += shell_cmd_fib
PSEUDOMODULES += shell_cmd_genfile
PSEUDOMODULES += shell_cmd_gnrc_icmpv6_echo
PSEUDOMODULES += shell_cmd_gnrc_ipv6_blacklist
PSEUDOMODULES += shell_cmd_gnrc_ipv6_frag_stats

View File

@ -362,7 +362,7 @@ endif
ifneq (,$(filter isrpipe_read_timeout,$(USEMODULE)))
USEMODULE += isrpipe
USEMODULE += xtimer
USEMODULE += ztimer_usec
endif
ifneq (,$(filter md5sum sha1sum sha256sum,$(USEMODULE)))

View File

@ -15,4 +15,4 @@ menuconfig MODULE_ISRPIPE
config MODULE_ISRPIPE_READ_TIMEOUT
bool "ISR Pipe read with timeout"
depends on MODULE_ISRPIPE
select MODULE_XTIMER
select ZTIMER_USEC

View File

@ -20,7 +20,7 @@
#include <errno.h>
#include "isrpipe/read_timeout.h"
#include "xtimer.h"
#include "ztimer.h"
typedef struct {
mutex_t *mutex;
@ -41,9 +41,9 @@ int isrpipe_read_timeout(isrpipe_t *isrpipe, uint8_t *buffer, size_t count, uint
_isrpipe_timeout_t _timeout = { .mutex = &isrpipe->mutex, .flag = 0 };
xtimer_t timer = { .callback = _cb, .arg = &_timeout };
ztimer_t timer = { .callback = _cb, .arg = &_timeout };
xtimer_set(&timer, timeout);
ztimer_set(ZTIMER_USEC, &timer, timeout);
while (!(res = tsrb_get(&isrpipe->tsrb, buffer, count))) {
mutex_lock(&isrpipe->mutex);
if (_timeout.flag) {
@ -52,7 +52,7 @@ int isrpipe_read_timeout(isrpipe_t *isrpipe, uint8_t *buffer, size_t count, uint
}
}
xtimer_remove(&timer);
ztimer_remove(ZTIMER_USEC, &timer);
return res;
}

View File

@ -1620,12 +1620,12 @@ ssize_t gcoap_req_send_tl(const uint8_t *buf, size_t len,
int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code)
{
if (coap_get_type(pdu) == COAP_TYPE_CON) {
coap_hdr_set_type(pdu->hdr, COAP_TYPE_ACK);
}
coap_hdr_set_code(pdu->hdr, code);
int header_len = coap_build_reply(pdu, code, buf, len, 0);
unsigned header_len = coap_get_total_hdr_len(pdu);
/* request contained no-response option or not enough space for response */
if (header_len <= 0) {
return -1;
}
pdu->options_len = 0;
pdu->payload = buf + header_len;

View File

@ -27,6 +27,7 @@
#include "container.h"
#include "periph/rtc.h"
#include "rtc_utils.h"
#include "shell.h"
static void _alarm_handler(void *arg)
@ -36,19 +37,6 @@ static void _alarm_handler(void *arg)
puts("The alarm rang");
}
static int dow(int year, int month, int day)
{
/* calculate the day of week using Tøndering's algorithm */
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
if (month < 1 || month > (int) ARRAY_SIZE(t)) {
/* This will be a wrong answer, but error handling is not this
* function's task (whereas memory safety is). */
return 7;
}
year -= month < 3;
return (year + year/4 - year/100 + year/400 + t[month-1] + day) % 7;
}
/** Read a ["YYYY-MM-DD", "hh:mm:ss"] formatted value from a string array.
*
* This performs no validation on the entered time -- that'd be trivial on some
@ -58,7 +46,7 @@ static int dow(int year, int month, int day)
*
* Invalid inputs merely lead to out-of-range values inside the time struct.
*/
static int _parse_time(char **argv, struct tm *time)
static void _parse_time(char **argv, struct tm *time)
{
short i;
char *end;
@ -81,10 +69,9 @@ static int _parse_time(char **argv, struct tm *time)
i = strtol(end + 1, &end, 10);
time->tm_sec = i;
time->tm_wday = dow(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday);
time->tm_isdst = -1; /* undefined */
return 0;
rtc_tm_normalize(time);
}
static int _print_time(struct tm *time)
@ -113,14 +100,14 @@ static int _rtc_setalarm(char **argv)
{
struct tm now;
if (_parse_time(argv, &now) == 0) {
if (rtc_set_alarm(&now, _alarm_handler, NULL) == -1) {
puts("rtc: error setting alarm");
return 1;
}
return 0;
_parse_time(argv, &now);
if (rtc_set_alarm(&now, _alarm_handler, NULL) < 0) {
puts("rtc: error setting alarm");
return 1;
}
return 1;
return 0;
}
static int _rtc_gettime(void)
@ -140,14 +127,14 @@ static int _rtc_settime(char **argv)
{
struct tm now;
if (_parse_time(argv, &now) == 0) {
if (rtc_set_time(&now) == -1) {
puts("rtc: error setting time");
return 1;
}
return 0;
_parse_time(argv, &now);
if (rtc_set_time(&now) < 0) {
puts("rtc: error setting time");
return 1;
}
return 1;
return 0;
}
static int _rtc_usage(void)

View File

@ -720,6 +720,97 @@ static int _vfs_handler(int argc, char **argv)
SHELL_COMMAND(vfs, "virtual file system operations", _vfs_handler);
#if MODULE_SHELL_CMD_GENFILE
static char _get_char(unsigned i)
{
i %= 62; /* a-z, A-Z, 0..9, -> 62 characters */
if (i < 10) {
return '0' + i;
}
i -= 10;
if (i <= 'z' - 'a') {
return 'a' + i;
}
i -= 1 + 'z' - 'a';
return 'A' + i;
}
static void _write_block(int fd, unsigned bs, unsigned i)
{
char block[bs];
char *buf = block;
buf += snprintf(buf, bs, "|%03u|", i);
memset(buf, _get_char(i), &block[bs] - buf);
block[bs - 1] = '\n';
vfs_write(fd, block, bs);
}
static int _vfs_genfile_cmd(int argc, char **argv)
{
unsigned blocksize = 64;
unsigned blocks = 32;
int fd = STDOUT_FILENO;
const char *cmdname = argv[0];
while (argc > 1 && argv[1][0] == '-') {
char *optarg = argc > 2 ? argv[2] : NULL;
char opt = argv[1][1];
if (optarg == NULL) {
printf("missing argument\n");
opt = '?';
}
switch (opt) {
case '?':
printf("usage: %s [-o <file>] [-b <block size>] [-n num blocks]\n",
cmdname);
return 0;
case 'o':
fd = vfs_open(optarg, O_CREAT | O_TRUNC | O_WRONLY, 0644);
if (fd < 0) {
printf("can't create %s\n", optarg);
return fd;
}
break;
case 'b':
blocksize = atoi(optarg);
break;
case 'n':
blocks = atoi(optarg);
break;
default:
printf("unknown option '%s'\n", argv[1]);
return 1;
}
argc -= 2;
argv += 2;
}
if (!blocksize || !blocks || argc > 1) {
printf("invalid argument\n");
return -EINVAL;
}
for (unsigned i = 0; i < blocks; ++i) {
_write_block(fd, blocksize, i);
}
if (fd != STDOUT_FILENO) {
vfs_close(fd);
printf("%u bytes written.\n", blocksize * blocks);
}
return 0;
}
SHELL_COMMAND(genfile, "generate dummy file", _vfs_genfile_cmd);
#endif
__attribute__((used)) /* only used if md5sum / sha1sum / sha256sum is used */
static inline void _print_digest(const uint8_t *digest, size_t len, const char *file)
{

View File

@ -107,13 +107,17 @@ int main(void)
printf("\n");
printf("%d elements probably in the filter.\n", in);
printf("%d elements not in the filter.\n", not_in);
double false_positive_rate = (double) in / (double) lenA;
unsigned false_positive_rate = (1000UL * in) / lenA;
/* Use 'fmt/print_float' to work on all platforms (atmega)
* Stdout should be flushed before to prevent garbled output. */
#if defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC)
fflush(stdout);
#endif
print_float(false_positive_rate, 6);
char buf[8];
int res = fmt_s32_dfp(buf, false_positive_rate, -3);
print(buf, res);
puts(" false positive rate.");
bloom_del(&bloom);

View File

@ -5,6 +5,7 @@ include ../Makefile.sys_common
USEMODULE += fido2_ctap_transport_hid
USEMODULE += usbus
USEMODULE += ztimer_sec
USEPKG += fido2_tests
USB_VID ?= $(USB_VID_TESTING)

View File

@ -5,6 +5,8 @@ CONFIG_MODULE_FIDO2=y
CONFIG_MODULE_FIDO2_CTAP=y
CONFIG_MODULE_FIDO2_CTAP_TRANSPORT=y
CONFIG_MODULE_FIDO2_CTAP_TRANSPORT_HID=y
CONFIG_MODULE_ZTIMER=y
CONFIG_MODULE_ZTIMER_SEC=y
CONFIG_PACKAGE_FIDO2_TESTS=y

View File

@ -23,7 +23,7 @@
#define ENABLE_DEBUG 0
#include "debug.h"
#include "xtimer.h"
#include "ztimer.h"
#include "fido2/ctap.h"
#include "fido2/ctap/transport/ctap_transport.h"
@ -31,6 +31,6 @@
int main(void)
{
/* sleep in order to see early DEBUG outputs */
xtimer_sleep(3);
ztimer_sleep(ZTIMER_SEC, 3);
fido2_ctap_transport_init();
}

View File

@ -3,6 +3,7 @@ BOARD ?= nrf52840dk
include ../Makefile.sys_common
USEMODULE += usbus_hid
USEMODULE += xtimer
DISABLE_MODULE += auto_init_usbus

View File

@ -4,6 +4,7 @@ USEMODULE += vfs_default
USEMODULE += vfs_auto_format
USEMODULE += ps
USEMODULE += shell_cmd_genfile
USEMODULE += shell_cmds_default
include $(RIOTBASE)/Makefile.include

View File

@ -63,7 +63,7 @@ static void test_bloom_based_on_dictionary_fixture(void)
{
int in = 0;
int not_in = 0;
double false_positive_rate = 0;
int false_positive_rate = 0;
load_dictionary_fixture();
@ -78,11 +78,11 @@ static void test_bloom_based_on_dictionary_fixture(void)
not_in++;
}
}
false_positive_rate = (double) in / (double) lenA;
false_positive_rate = (1000 * in) / lenA;
TEST_ASSERT_EQUAL_INT(TESTS_BLOOM_PROB_IN_FILTER, in);
TEST_ASSERT_EQUAL_INT(TESTS_BLOOM_NOT_IN_FILTER, not_in);
TEST_ASSERT(false_positive_rate < TESTS_BLOOM_FALSE_POS_RATE_THR);
TEST_ASSERT(false_positive_rate < TESTS_BLOOM_FALSE_POS_RATE_THR * 1000);
}
Test *tests_bloom_tests(void)