diff --git a/AppKit/fifo.c b/AppKit/fifo.c index 1c6f4ab..b632666 100644 --- a/AppKit/fifo.c +++ b/AppKit/fifo.c @@ -141,7 +141,7 @@ os_size_t fifo_read_fast(fifo_t* rb, uint8_t * data, os_size_t len){ // w r if(write_idx > read_idx){ - memcpy(&data, &rb->buffer[read_idx], len); + memcpy(data, &rb->buffer[read_idx], len); }else{ os_size_t first_part = size - read_idx; if(first_part >= len){ @@ -203,7 +203,7 @@ os_size_t fifo_peek_bulk(fifo_t* rb, os_size_t offset, uint8_t* data, os_size_t // w r if(write_idx > read_idx){ - memcpy(&data, &rb->buffer[read_idx], len); + memcpy(data, &rb->buffer[read_idx], len); }else{ os_size_t first_part = size - read_idx; if(first_part >= len){ @@ -249,16 +249,16 @@ int fifo_memcmp(fifo_t *rb, os_size_t offset, const void *ptr, os_size_t len) { */ os_size_t fifo_find(fifo_t *rb, os_size_t offset, const void *pattern, os_size_t pattern_len) { if (!rb || !pattern || pattern_len == 0) { - return -1u; + return FIFO_INVALID_POS; } os_size_t data_size = fifo_count(rb); if(offset > data_size){ - return -1u; // 开始查找位置错误了 + return FIFO_INVALID_POS; // 开始查找位置错误了 } if (pattern_len > (data_size - offset) ) { - return -1u; // 模式长度超过缓冲区数据量,不可能找到 + return FIFO_INVALID_POS; // 模式长度超过缓冲区数据量,不可能找到 } // 遍历缓冲区中所有可能的起始位置 @@ -290,7 +290,7 @@ os_size_t fifo_find(fifo_t *rb, os_size_t offset, const void *pattern, os_size_t } } - return -1u; + return FIFO_INVALID_POS; } unsigned long fifo_strtoul(fifo_t* self, os_size_t* endptr /*从哪里开始转换*/, int base) { @@ -397,3 +397,29 @@ unsigned long fifo_strtoul(fifo_t* self, os_size_t* endptr /*从哪里开始转 return acc; } +// Returns the virtual index relative to tail where the match starts, or -1 if not found +os_size_t fifo_strstr(fifo_t *self, const char *search) { + os_size_t search_len = strlen(search); + os_size_t count = fifo_count(self); + if (search_len == 0 || count < search_len) return FIFO_INVALID_POS; + + // Loop through every possible starting position in the ring buffer + for (os_size_t i = 0; i <= (count - search_len); i++) { + bool match = true; + + // Check if the substring matches starting at virtual position i + for (os_size_t j = 0; j < search_len; j++) { + os_size_t rd_index = (self->read_idx + i + j) % self->buffer_size; + if (self->buffer[rd_index] != search[j]) { + match = false; + break; + } + } + + if (match) { + return i; // Return the position relative to the current tail + } + } + return FIFO_INVALID_POS; +} + diff --git a/AppKit/fifo.h b/AppKit/fifo.h index 1d68fc6..576caf5 100644 --- a/AppKit/fifo.h +++ b/AppKit/fifo.h @@ -26,6 +26,8 @@ typedef struct fifo_t{ os_size_t read_idx; }fifo_t; +#define FIFO_INVALID_POS ((os_size_t)(-1)) + /* ==================================================================================================== */ /* 接口 */ @@ -139,4 +141,6 @@ os_size_t fifo_find(fifo_t *rb, os_size_t offset, const void *pattern, os_size_t unsigned long fifo_strtoul(fifo_t* self, os_size_t* endptr /*从哪里开始转换, 可以为空*/, int base /*0, 8, 10, 16 进制*/); +os_size_t fifo_strstr(fifo_t *self, const char *search); + #endif /*INCLUDED_FIFO_H*/ diff --git a/Kernel/SingleCore/os_sem.h b/Kernel/SingleCore/os_sem.h index 8f78a03..6e2c241 100644 --- a/Kernel/SingleCore/os_sem.h +++ b/Kernel/SingleCore/os_sem.h @@ -85,4 +85,14 @@ void os_sem_notify_one_in_isr(os_sem_t* self){ os_waitobject_notify_one_in_isr(&self->wait_obj); } +OS_STATIC_FORCE_INLINE +void os_sem_reset(os_sem_t* self){ + os_critical_enter(); + self->value = 0; + os_waitobject_init(&self->wait_obj); + os_critical_leave(); +} + + + #endif /*INCLUDED_OS_SEM_H*/