diff --git a/driver/new_uart.c b/driver/new_uart.c index bb382a5..dde2a24 100644 --- a/driver/new_uart.c +++ b/driver/new_uart.c @@ -23,8 +23,8 @@ #include "os_type.h" #ifdef _ENABLE_RING_BUFFER - static ring_buffer_t *rxBuff; - static ring_buffer_t *txBuff; + static ringbuf_t rxBuff; + static ringbuf_t txBuff; #endif #ifdef _ENABLE_CONSOLE_INTEGRATION @@ -58,8 +58,8 @@ void ICACHE_FLASH_ATTR UART_init(UartBautRate uart0_br, UartBautRate uart1_br, u #if _ENABLE_CONSOLE_INTEGRATION == 1 void ICACHE_FLASH_ATTR UART_init_console(UartBautRate uart0_br, uint8 recv_task_priority, - ring_buffer_t *rxbuffer, - ring_buffer_t *txBuffer) + ringbuf_t rxbuffer, + ringbuf_t txBuffer) { /* Set the task which should receive the signals once the data is received */ uart_recvTaskPrio = recv_task_priority; @@ -96,7 +96,7 @@ void ICACHE_FLASH_ATTR UART_init(UartBautRate uart0_br, UartBautRate uart1_br, u #if _ENABLE_CONSOLE_INTEGRATION == 0 #if _ENABLE_RING_BUFFER == 1 - rxBuff = ring_buffer_init(RX_RING_BUFFER_SIZE); + rxBuff = ringbuf_new(RX_RING_BUFFER_SIZE); #endif #endif } @@ -193,17 +193,14 @@ int UART_Recv(uint8 uart_no, char *buffer, int max_buf_len) #if _ENABLE_RING_BUFFER == 1 /* If the ring buffer is enabled, then unload from Rx Ring Buffer */ - uint8 bytes_ringbuffer = rxBuff->data_present; + uint8 bytes_ringbuffer = ringbuf_bytes_used(rxBuff); //ring_buffer_dump(rxBuff); if (bytes_ringbuffer) { max_unload = (bytes_ringbufferbuffer_size = max_size; - rbuff->buffer = (uint8_t *) os_malloc(rbuff->buffer_size); - os_memset(rbuff->buffer, 0 , rbuff->buffer_size); - rbuff->write_ptr = rbuff->read_ptr = rbuff->buffer; - rbuff->end_ptr = rbuff->buffer + rbuff->buffer_size - 1; - rbuff->rb_empty = 1; - rbuff->rb_full = 0; - rbuff->data_present = 0; - } - - return rbuff; -} - -void ring_buffer_dump(ring_buffer_t *rbuff) -{ - int index = 0; - uint8_t *temp; - -#ifdef VERBOSE_DUMP - printf("Dumping contents of the ring buffer [Data Present: %3d]\n", rbuff->data_present); - printf("Buffer Start: %p, End: %p\n", rbuff->buffer, rbuff->end_ptr); - printf("Data Read Start: %p, Data write Start: %p\n", rbuff->read_ptr, rbuff->write_ptr); - for (index = 0; index < rbuff->buffer_size; index ++) - { - char *tmp = rbuff->buffer+index; - - printf("\t[%p] %3d : %c", tmp, index, *tmp); - - if (tmp == rbuff->read_ptr) printf("\t[R]"); - if (tmp == rbuff->write_ptr) printf("\t[W]"); - - printf("\n"); - } -#else - //printf("Dumping contents of the ring buffer [Data Present: %3d]\n\t", rbuff->data_present); - - os_printf("\t"); - temp = rbuff->read_ptr; - for (index = 0; index < rbuff->data_present; index ++) - { - os_printf(" %x ", *temp); - - if (temp != rbuff->end_ptr) temp ++; - else temp = rbuff->buffer; - } - os_printf("\n"); - -#endif -} - -int ring_buffer_enqueue(ring_buffer_t *rbuff, uint8_t ch) -{ - int temp_flag = 0; - if (rbuff == NULL) return -1; // Check for valid structure - if (rbuff->buffer == NULL) return -1; // Check if it in initialized - - if (rbuff->data_present == rbuff->buffer_size) - { -#ifdef RB_ENABLE_DISCARD -// DBG("RingBuffer is full - discarding %c to add %c\n", *rbuff->read_ptr, ch); - // Discard the oldest data and add the new data - if (rbuff->read_ptr != rbuff->end_ptr) rbuff->read_ptr ++; - else rbuff->read_ptr = rbuff->buffer; - rbuff->data_present --; - - temp_flag = 1; -#else - // Ring Buffer is full and latest data will be discarded - DBG("RingBuffer is full - not adding %c\n", ch); - return -1; -#endif - } - - *(rbuff->write_ptr) = ch; - rbuff->data_present ++; - - if (rbuff->write_ptr != rbuff->end_ptr) rbuff->write_ptr ++; - else rbuff->write_ptr = rbuff->buffer; - - if (temp_flag) ring_buffer_dump(rbuff); - -} - -#ifdef RB_ENABLE_DISCARD -int ring_buffer_enqueue_bulk(ring_buffer_t *rbuff, uint8_t *inp, int len) -{ - int temp_flag = 0; - if (rbuff == NULL) return -1; // Check for valid structure - if (rbuff->buffer == NULL) return -1; // Check if it in initialized - - if (rbuff->data_present + len > rbuff->buffer_size) return -1; // too large - discard all - - if (rbuff->end_ptr - rbuff->write_ptr > len) { - os_memcpy(rbuff->write_ptr, inp, len); - rbuff->write_ptr += len; - rbuff->data_present += len; - } else { - int i; - for (i=0; ibuffer == NULL) return -1; // Check if it in initialized - - if (rbuff->data_present == 0) - { - DBG("\nDeQueuuing from RingBuffer - No Data in ring buffer [%d] \n", rbuff->data_present); - return -1; - } - else - { - ch = *rbuff->read_ptr; - *rbuff->read_ptr = ' '; - if (rbuff->read_ptr != rbuff->end_ptr) rbuff->read_ptr ++; - else rbuff->read_ptr = rbuff->buffer; - rbuff->data_present --; - } - - //DBG("DeQueuuing from RingBuffer %c [%3d]\n", ch, rbuff->data_present); - - return (int)ch; -} - -uint16_t ring_buffer_data(ring_buffer_t *rbuff) -{ - DBG("\nData in ring buffer: %d\n", rbuff->data_present); - return (rbuff->data_present); -} diff --git a/user/ring_buffer.h b/user/ring_buffer.h deleted file mode 100644 index 2dcd446..0000000 --- a/user/ring_buffer.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef _RING_BUFFER_H_ -#define _RING_BUFFER_H_ - -#include "ets_sys.h" -#include "osapi.h" -#include "osapi.h" -#include "mem.h" -#include "os_type.h" - -//typedef unsigned char uint8_t; -// typedef unsigned int uint16_t; - -typedef struct ring_buffer -{ - uint8_t *buffer; - uint8_t *write_ptr, *read_ptr, *end_ptr; - uint8_t rb_empty, rb_full; - uint16_t buffer_size, data_present; -} ring_buffer_t; - - -ring_buffer_t * ring_buffer_init(int max_size); -int ring_buffer_enqueue(ring_buffer_t *rbuff, uint8_t ch); -int ring_buffer_enqueue_bulk(ring_buffer_t *rbuff, uint8_t *inp, int len); -int ring_buffer_dequeue(ring_buffer_t *rbuff); -uint16_t ring_buffer_data(ring_buffer_t *rbuff); -uint16_t ring_buffer_clear(ring_buffer_t *rbuff); -#endif // _RING_BUFFER_H_ diff --git a/user/ringbuf.c b/user/ringbuf.c new file mode 100644 index 0000000..7668b5e --- /dev/null +++ b/user/ringbuf.c @@ -0,0 +1,250 @@ +/* + * ringbuf.c - C ring buffer (FIFO) implementation. + * + * Written in 2011 by Drew Hess . + * + * To the extent possible under law, the author(s) have dedicated all + * copyright and related and neighboring rights to this software to + * the public domain worldwide. This software is distributed without + * any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication + * along with this software. If not, see + * . + */ + +#include "ringbuf.h" + +#include +#include +#include +#include +#include +#include + +#include "osapi.h" +#include "mem.h" + +#define assert(x) + +/* + * The code is written for clarity, not cleverness or performance, and + * contains many assert()s to enforce invariant assumptions and catch + * bugs. Feel free to optimize the code and to remove asserts for use + * in your own projects, once you're comfortable that it functions as + * intended. + */ + +struct ringbuf_t +{ + uint8_t *buf; + uint8_t *head, *tail; + size_t size; +}; + +ringbuf_t +ringbuf_new(size_t capacity) +{ + ringbuf_t rb = (ringbuf_t)os_malloc(sizeof(struct ringbuf_t)); + if (rb) { + + /* One byte is used for detecting the full condition. */ + rb->size = capacity + 1; + rb->buf = (uint8_t *)os_malloc(rb->size); + if (rb->buf) + ringbuf_reset(rb); + else { + os_free(rb); + return 0; + } + } + return rb; +} + +size_t +ringbuf_buffer_size(const struct ringbuf_t *rb) +{ + return rb->size; +} + +void +ringbuf_reset(ringbuf_t rb) +{ + rb->head = rb->tail = rb->buf; +} + +void +ringbuf_free(ringbuf_t *rb) +{ + assert(rb && *rb); + os_free((*rb)->buf); + os_free(*rb); + *rb = 0; +} + +size_t +ringbuf_capacity(const struct ringbuf_t *rb) +{ + return ringbuf_buffer_size(rb) - 1; +} + +/* + * Return a pointer to one-past-the-end of the ring buffer's + * contiguous buffer. You shouldn't normally need to use this function + * unless you're writing a new ringbuf_* function. + */ +static const uint8_t * +ringbuf_end(const struct ringbuf_t *rb) +{ + return rb->buf + ringbuf_buffer_size(rb); +} + +size_t +ringbuf_bytes_free(const struct ringbuf_t *rb) +{ + if (rb->head >= rb->tail) + return ringbuf_capacity(rb) - (rb->head - rb->tail); + else + return rb->tail - rb->head - 1; +} + +size_t +ringbuf_bytes_used(const struct ringbuf_t *rb) +{ + return ringbuf_capacity(rb) - ringbuf_bytes_free(rb); +} + +int +ringbuf_is_full(const struct ringbuf_t *rb) +{ + return ringbuf_bytes_free(rb) == 0; +} + +int +ringbuf_is_empty(const struct ringbuf_t *rb) +{ + return ringbuf_bytes_free(rb) == ringbuf_capacity(rb); +} + +const void * +ringbuf_tail(const struct ringbuf_t *rb) +{ + return rb->tail; +} + +const void * +ringbuf_head(const struct ringbuf_t *rb) +{ + return rb->head; +} + +/* + * Given a ring buffer rb and a pointer to a location within its + * contiguous buffer, return the a pointer to the next logical + * location in the ring buffer. + */ +static uint8_t * +ringbuf_nextp(ringbuf_t rb, const uint8_t *p) +{ + /* + * The assert guarantees the expression (++p - rb->buf) is + * non-negative; therefore, the modulus operation is safe and + * portable. + */ + assert((p >= rb->buf) && (p < ringbuf_end(rb))); + return rb->buf + ((++p - rb->buf) % ringbuf_buffer_size(rb)); +} + +void * +ringbuf_memcpy_into(ringbuf_t dst, const void *src, size_t count) +{ + const uint8_t *u8src = src; + const uint8_t *bufend = ringbuf_end(dst); + int overflow = count > ringbuf_bytes_free(dst); + size_t nread = 0; + + while (nread != count) { + /* don't copy beyond the end of the buffer */ + assert(bufend > dst->head); + size_t n = MIN(bufend - dst->head, count - nread); + os_memcpy(dst->head, u8src + nread, n); + dst->head += n; + nread += n; + + /* wrap? */ + if (dst->head == bufend) + dst->head = dst->buf; + } + + if (overflow) { + dst->tail = ringbuf_nextp(dst, dst->head); + assert(ringbuf_is_full(dst)); + } + + return dst->head; +} + +void * +ringbuf_memcpy_from(void *dst, ringbuf_t src, size_t count) +{ + size_t bytes_used = ringbuf_bytes_used(src); + if (count > bytes_used) + return 0; + + uint8_t *u8dst = dst; + const uint8_t *bufend = ringbuf_end(src); + size_t nwritten = 0; + while (nwritten != count) { + assert(bufend > src->tail); + size_t n = MIN(bufend - src->tail, count - nwritten); + os_memcpy(u8dst + nwritten, src->tail, n); + src->tail += n; + nwritten += n; + + /* wrap ? */ + if (src->tail == bufend) + src->tail = src->buf; + } + + assert(count + ringbuf_bytes_used(src) == bytes_used); + return src->tail; +} + +void * +ringbuf_copy(ringbuf_t dst, ringbuf_t src, size_t count) +{ + size_t src_bytes_used = ringbuf_bytes_used(src); + if (count > src_bytes_used) + return 0; + int overflow = count > ringbuf_bytes_free(dst); + + const uint8_t *src_bufend = ringbuf_end(src); + const uint8_t *dst_bufend = ringbuf_end(dst); + size_t ncopied = 0; + while (ncopied != count) { + assert(src_bufend > src->tail); + size_t nsrc = MIN(src_bufend - src->tail, count - ncopied); + assert(dst_bufend > dst->head); + size_t n = MIN(dst_bufend - dst->head, nsrc); + os_memcpy(dst->head, src->tail, n); + src->tail += n; + dst->head += n; + ncopied += n; + + /* wrap ? */ + if (src->tail == src_bufend) + src->tail = src->buf; + if (dst->head == dst_bufend) + dst->head = dst->buf; + } + + assert(count + ringbuf_bytes_used(src) == src_bytes_used); + + if (overflow) { + dst->tail = ringbuf_nextp(dst, dst->head); + assert(ringbuf_is_full(dst)); + } + + return dst->head; +} + diff --git a/user/ringbuf.h b/user/ringbuf.h new file mode 100644 index 0000000..c032b9e --- /dev/null +++ b/user/ringbuf.h @@ -0,0 +1,167 @@ +#ifndef INCLUDED_RINGBUF_H +#define INCLUDED_RINGBUF_H + +/* + * ringbuf.h - C ring buffer (FIFO) interface. + * + * Written in 2011 by Drew Hess . + * + * To the extent possible under law, the author(s) have dedicated all + * copyright and related and neighboring rights to this software to + * the public domain worldwide. This software is distributed without + * any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication + * along with this software. If not, see + * . + */ + +/* + * A byte-addressable ring buffer FIFO implementation. + * + * The ring buffer's head pointer points to the starting location + * where data should be written when copying data *into* the buffer + * (e.g., with ringbuf_read). The ring buffer's tail pointer points to + * the starting location where data should be read when copying data + * *from* the buffer (e.g., with ringbuf_write). + */ + +#include +#include + +typedef struct ringbuf_t *ringbuf_t; + +/* + * Create a new ring buffer with the given capacity (usable + * bytes). Note that the actual internal buffer size may be one or + * more bytes larger than the usable capacity, for bookkeeping. + * + * Returns the new ring buffer object, or 0 if there's not enough + * memory to fulfill the request for the given capacity. + */ +ringbuf_t +ringbuf_new(size_t capacity); + +/* + * The size of the internal buffer, in bytes. One or more bytes may be + * unusable in order to distinguish the "buffer full" state from the + * "buffer empty" state. + * + * For the usable capacity of the ring buffer, use the + * ringbuf_capacity function. + */ +size_t +ringbuf_buffer_size(const struct ringbuf_t *rb); + +/* + * Deallocate a ring buffer, and, as a side effect, set the pointer to + * 0. + */ +void +ringbuf_free(ringbuf_t *rb); + +/* + * Reset a ring buffer to its initial state (empty). + */ +void +ringbuf_reset(ringbuf_t rb); + +/* + * The usable capacity of the ring buffer, in bytes. Note that this + * value may be less than the ring buffer's internal buffer size, as + * returned by ringbuf_buffer_size. + */ +size_t +ringbuf_capacity(const struct ringbuf_t *rb); + +/* + * The number of free/available bytes in the ring buffer. This value + * is never larger than the ring buffer's usable capacity. + */ +size_t +ringbuf_bytes_free(const struct ringbuf_t *rb); + +/* + * The number of bytes currently being used in the ring buffer. This + * value is never larger than the ring buffer's usable capacity. + */ +size_t +ringbuf_bytes_used(const struct ringbuf_t *rb); + +int +ringbuf_is_full(const struct ringbuf_t *rb); + +int +ringbuf_is_empty(const struct ringbuf_t *rb); + +/* + * Const access to the head and tail pointers of the ring buffer. + */ +const void * +ringbuf_tail(const struct ringbuf_t *rb); + +const void * +ringbuf_head(const struct ringbuf_t *rb); + +/* + * Copy n bytes from a contiguous memory area src into the ring buffer + * dst. Returns the ring buffer's new head pointer. + * + * It is possible to copy more data from src than is available in the + * buffer; i.e., it's possible to overflow the ring buffer using this + * function. When an overflow occurs, the state of the ring buffer is + * guaranteed to be consistent, including the head and tail pointers; + * old data will simply be overwritten in FIFO fashion, as + * needed. However, note that, if calling the function results in an + * overflow, the value of the ring buffer's tail pointer may be + * different than it was before the function was called. + */ +void * +ringbuf_memcpy_into(ringbuf_t dst, const void *src, size_t count); + +/* + * Copy n bytes from the ring buffer src, starting from its tail + * pointer, into a contiguous memory area dst. Returns the value of + * src's tail pointer after the copy is finished. + * + * Note that this copy is destructive with respect to the ring buffer: + * the n bytes copied from the ring buffer are no longer available in + * the ring buffer after the copy is complete, and the ring buffer + * will have n more free bytes than it did before the function was + * called. + * + * This function will *not* allow the ring buffer to underflow. If + * count is greater than the number of bytes used in the ring buffer, + * no bytes are copied, and the function will return 0. + */ +void * +ringbuf_memcpy_from(void *dst, ringbuf_t src, size_t count); + +/* + * Copy count bytes from ring buffer src, starting from its tail + * pointer, into ring buffer dst. Returns dst's new head pointer after + * the copy is finished. + * + * Note that this copy is destructive with respect to the ring buffer + * src: any bytes copied from src into dst are no longer available in + * src after the copy is complete, and src will have 'count' more free + * bytes than it did before the function was called. + * + * It is possible to copy more data from src than is available in dst; + * i.e., it's possible to overflow dst using this function. When an + * overflow occurs, the state of dst is guaranteed to be consistent, + * including the head and tail pointers; old data will simply be + * overwritten in FIFO fashion, as needed. However, note that, if + * calling the function results in an overflow, the value dst's tail + * pointer may be different than it was before the function was + * called. + * + * It is *not* possible to underflow src; if count is greater than the + * number of bytes used in src, no bytes are copied, and the function + * returns 0. + */ +void * +ringbuf_copy(ringbuf_t dst, ringbuf_t src, size_t count); + +#endif /* INCLUDED_RINGBUF_H */ + diff --git a/user/user_config.h b/user/user_config.h index 128f58a..b864877 100644 --- a/user/user_config.h +++ b/user/user_config.h @@ -1,7 +1,7 @@ #ifndef _USER_CONFIG_ #define _USER_CONFIG_ -typedef enum {SIG_DO_NOTHING=0, SIG_START_SERVER=1, SIG_SEND_DATA, SIG_UART0, SIG_CONSOLE_RX, SIG_CONSOLE_TX } USER_SIGNALS; +typedef enum {SIG_DO_NOTHING=0, SIG_START_SERVER=1, SIG_SEND_DATA, SIG_UART0, SIG_CONSOLE_RX, SIG_CONSOLE_TX, SIG_PACKET } USER_SIGNALS; #define WIFI_SSID "ssid" #define WIFI_PASSWORD "password" @@ -20,11 +20,18 @@ typedef enum {SIG_DO_NOTHING=0, SIG_START_SERVER=1, SIG_SEND_DATA, SIG_UART0, SI // // Define this if you want to offer monitoring access to all transmitted data between the soft AP and all STAs. -// Data is mirrored in pcap format to the given port +// Packets are mirrored in pcap format to the given port. // CAUTION: this might be a privacy issue!!! // //#define REMOTE_MONITORING 1 + #define MONITOR_SERVER_PORT 8888 +#define MONITOR_BUFFER_SIZE 0x4000 + +// Define this if you want to cut packets short in case of too high data rate +#define MONITOR_BUFFER_TIGHT 0x1000 + +// Define this if you want to silently drop any packet that cannot be send to the monitor //#define DROP_PACKET_IF_NOT_RECORDED 1 #endif diff --git a/user/user_main.c b/user/user_main.c index 99aa74f..c403021 100644 --- a/user/user_main.c +++ b/user/user_main.c @@ -15,8 +15,8 @@ #include "string.h" #include "driver/uart.h" +#include "ringbuf.h" #include "user_config.h" -#include "ring_buffer.h" #include "config_flash.h" #ifdef REMOTE_MONITORING @@ -35,21 +35,23 @@ sysconfig_t config; os_event_t user_procTaskQueue[user_procTaskQueueLen]; static void user_procTask(os_event_t *events); -static ring_buffer_t *console_rx_buffer, *console_tx_buffer; +static ringbuf_t console_rx_buffer, console_tx_buffer; struct netif *netif_ap; static netif_input_fn orig_input_ap; static netif_linkoutput_fn orig_output_ap; #ifdef REMOTE_MONITORING -static uint8_t monitoring_on = 0; -static ring_buffer_t *pcap_buffer; +static uint8_t monitoring_on; +static ringbuf_t pcap_buffer; struct espconn *cur_mon_conn; -static uint8_t monitoring_send_ongoing = 0; +static uint8_t monitoring_send_ongoing; + static void ICACHE_FLASH_ATTR tcp_monitor_sent_cb(void *arg) { - uint16_t len, remainder; + uint16_t len; + static uint8_t tbuf[1400]; struct espconn *pespconn = (struct espconn *)arg; //os_printf("tcp_client_sent_cb(): Data sent to monitor\n"); @@ -57,23 +59,18 @@ static void ICACHE_FLASH_ATTR tcp_monitor_sent_cb(void *arg) monitoring_send_ongoing = 0; if (!monitoring_on) return; - len = pcap_buffer->data_present; + len = ringbuf_bytes_used(pcap_buffer); if (len > 0) { if (len > 1400) len = 1400; - remainder = pcap_buffer->end_ptr - pcap_buffer->read_ptr + 1; - if (remainder < len) - len = remainder; - + ringbuf_memcpy_from(tbuf, pcap_buffer, len); //os_printf("tcp_monitor_sent_cb(): %d Bytes sent to monitor\n", len); - espconn_sent(pespconn, pcap_buffer->read_ptr, len); + if (espconn_sent(pespconn, tbuf, len) != 0) { + os_printf("TCP send error\r\n"); + return; + } monitoring_send_ongoing = 1; - - pcap_buffer->data_present -= len; - pcap_buffer->read_ptr += len; - if (pcap_buffer->read_ptr == pcap_buffer->end_ptr + 1) - pcap_buffer->read_ptr = pcap_buffer->buffer; } } @@ -83,8 +80,6 @@ static void ICACHE_FLASH_ATTR tcp_monitor_discon_cb(void *arg) struct espconn *pespconn = (struct espconn *)arg; monitoring_on = 0; - while (pcap_buffer->data_present) - ring_buffer_dequeue(pcap_buffer); } @@ -96,11 +91,7 @@ static void ICACHE_FLASH_ATTR tcp_monitor_connected_cb(void *arg) os_printf("tcp_monitor_connected_cb(): Client connected\r\n"); - pcap_buffer->write_ptr = pcap_buffer->read_ptr = pcap_buffer->buffer; - pcap_buffer->end_ptr = pcap_buffer->buffer + pcap_buffer->buffer_size - 1; - pcap_buffer->rb_empty = 1; - pcap_buffer->rb_full = 0; - pcap_buffer->data_present = 0; + ringbuf_reset(pcap_buffer); cur_mon_conn = pespconn; @@ -124,21 +115,29 @@ static void ICACHE_FLASH_ATTR tcp_monitor_connected_cb(void *arg) } -int ICACHE_FLASH_ATTR put_packet_to_ringbuf(ring_buffer_t *buf, struct pbuf *p) { +int ICACHE_FLASH_ATTR put_packet_to_ringbuf(struct pbuf *p) { struct pcap_pkthdr pcap_phdr; uint32_t t_usecs; + uint32_t len = p->len; - if (buf->buffer_size-buf->data_present >= sizeof(pcap_phdr)+p->len) { +#ifdef MONITOR_BUFFER_TIGHT + if (ringbuf_bytes_free(pcap_buffer) < MONITOR_BUFFER_TIGHT) { + if (len > 60) { + len = 60; + //os_printf("Packet cut\n"); + } + } +#endif + + if (ringbuf_bytes_free(pcap_buffer) >= sizeof(pcap_phdr)+len) { //os_printf("Put %d Bytes into RingBuff\r\n", sizeof(pcap_phdr)+p->len); - t_usecs = system_get_time(); + t_usecs = system_get_time(); pcap_phdr.ts_sec = t_usecs/1000000; pcap_phdr.ts_usec = t_usecs%1000000; - pcap_phdr.caplen = p->len; + pcap_phdr.caplen = len; pcap_phdr.len = p->tot_len; - ring_buffer_enqueue_bulk(buf, (uint8_t*)&pcap_phdr, sizeof(pcap_phdr)); - if (p->len != p->tot_len) - os_printf(">>>Len %d != TotalLen %d!\r\n", p->len, p->tot_len); - ring_buffer_enqueue_bulk(buf, p->payload, p->len); + ringbuf_memcpy_into(pcap_buffer, (uint8_t*)&pcap_phdr, sizeof(pcap_phdr)); + ringbuf_memcpy_into(pcap_buffer, p->payload, len); } else { //os_printf("Packet with %d Bytes discarded\r\n", p->len); return -1; @@ -147,6 +146,7 @@ int ICACHE_FLASH_ATTR put_packet_to_ringbuf(ring_buffer_t *buf, struct pbuf *p) } #endif +static uint8_t columns = 0; err_t ICACHE_FLASH_ATTR my_input_ap (struct pbuf *p, struct netif *inp) { //os_printf("Got packet from STA\r\n"); @@ -154,15 +154,17 @@ err_t ICACHE_FLASH_ATTR my_input_ap (struct pbuf *p, struct netif *inp) { #ifdef REMOTE_MONITORING if (monitoring_on) { - if (put_packet_to_ringbuf(pcap_buffer, p) == 0) { - if (!monitoring_send_ongoing) - tcp_monitor_sent_cb(cur_mon_conn); - } else { + system_os_post(user_procTaskPrio, SIG_PACKET, 0 ); + if (put_packet_to_ringbuf(p) != 0) { #ifdef DROP_PACKET_IF_NOT_RECORDED pbuf_free(p); return; #else - os_printf(".", p->len); + os_printf("x"); + if (++columns > 40) { + os_printf("\r\n"); + columns = 0; + } #endif } } @@ -178,15 +180,17 @@ err_t ICACHE_FLASH_ATTR my_output_ap (struct netif *outp, struct pbuf *p) { #ifdef REMOTE_MONITORING if (monitoring_on) { - if (put_packet_to_ringbuf(pcap_buffer, p) == 0) { - if (!monitoring_send_ongoing) - tcp_monitor_sent_cb(cur_mon_conn); - } else { + system_os_post(user_procTaskPrio, SIG_PACKET, 0 ); + if (put_packet_to_ringbuf(p) != 0) { #ifdef DROP_PACKET_IF_NOT_RECORDED pbuf_free(p); return; #else - os_printf(".", p->len); + os_printf("x"); + if (++columns > 40) { + os_printf("\r\n"); + columns = 0; + } #endif } } @@ -195,39 +199,6 @@ err_t ICACHE_FLASH_ATTR my_output_ap (struct netif *outp, struct pbuf *p) { orig_output_ap (outp, p); } -int ICACHE_FLASH_ATTR fputs_into_ringbuff(ring_buffer_t *buffer, uint8_t *strbuffer, int length) -{ - uint8_t index = 0, ch; - for (index = 0; index < length; index++) - { - ch = *(strbuffer+index); - ring_buffer_enqueue(buffer, ch); - } - - return index; -} - -int ICACHE_FLASH_ATTR fgets_from_ringbuff(ring_buffer_t *buffer, uint8_t *strbuffer, int max_length) -{ - uint8_t max_unload, i, index = 0; - uint8_t bytes_ringbuffer = buffer->data_present; - uint8_t *str = strbuffer; - - if (bytes_ringbuffer) // If data is available in the ringbuffer - { - max_unload = (bytes_ringbufferdata_present; - max_length = sizeof(payload); + uint8_t bytes_ringbuffer = ringbuf_bytes_used(console_tx_buffer); + max_length = sizeof(payload)-1; if (bytes_ringbuffer) // If data is available in the ringbuffer { max_unload = (bytes_ringbuffer", payload); //os_printf("Payload: %s\n", payload); @@ -326,7 +294,9 @@ void console_handle_command(struct espconn *pespconn) int bytes_count, nTokens, i; - bytes_count = fgets_from_ringbuff(console_rx_buffer, cmd_line, 255); + bytes_count = ringbuf_bytes_used(console_rx_buffer); + ringbuf_memcpy_from(cmd_line, console_rx_buffer, bytes_count); + cmd_line[bytes_count] = 0; nTokens = parse_str_into_tokens(cmd_line, tokens, 5); @@ -334,14 +304,14 @@ void console_handle_command(struct espconn *pespconn) { config_save(0, &config); os_sprintf(response, "Done!\r\n"); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); goto command_handled; } if (strcmp(tokens[0], "help") == 0) { os_sprintf(response, "save|reset|set [ssid|password|auto_connect|ap_ssid|ap_password|ap_open] |lock|unlock \r\n"); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); goto command_handled; } @@ -351,25 +321,25 @@ void console_handle_command(struct espconn *pespconn) os_sprintf(response, "STA: SSID %s PW:****** [AutoConnect: %2d] \r\n", config.ssid, config.auto_connect); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); os_sprintf(response, "AP: SSID %s PW:****** [Open: %2d] \r\n", config.ap_ssid, config.ap_open); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); goto command_handled; } else { os_sprintf(response, "STA: SSID %s PW:%s [AutoConnect: %2d] \r\n", config.ssid, config.password, config.auto_connect); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); os_sprintf(response, "AP: SSID %s PW:%s [Open: %2d] \r\n", config.ap_ssid, config.ap_password, config.ap_open); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); os_sprintf(response, "Bytes in %d Bytes out %d\r\n", Bytes_in, Bytes_out); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); goto command_handled; } } @@ -377,7 +347,7 @@ void console_handle_command(struct espconn *pespconn) if (strcmp(tokens[0], "reset") == 0) { os_sprintf("Restarting ... \r\n"); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); system_restart(); goto command_handled; @@ -387,7 +357,7 @@ void console_handle_command(struct espconn *pespconn) { config.locked = 1; os_sprintf(response, "Config locked. Please save the configuration using save command\r\n"); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); goto command_handled; } @@ -396,15 +366,15 @@ void console_handle_command(struct espconn *pespconn) if (nTokens != 2) { os_sprintf(response, "Invalid number of arguments\r\n"); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); } else if (strcmp(tokens[1],config.password) == 0) { config.locked = 0; os_sprintf(response, "Config unlocked. Please save the configuration using save command\r\n"); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); } else { os_sprintf(response, "Unlock failed. Invalid password\r\n"); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); } goto command_handled; } @@ -415,7 +385,7 @@ void console_handle_command(struct espconn *pespconn) if (config.locked) { os_sprintf(response, "Invalid set command. Config locked\r\n"); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); goto command_handled; } @@ -426,7 +396,7 @@ void console_handle_command(struct espconn *pespconn) if (nTokens < 3) { os_sprintf(response, "Invalid number of arguments\r\n"); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); goto command_handled; } else @@ -436,7 +406,7 @@ void console_handle_command(struct espconn *pespconn) { os_sprintf(config.ssid, "%s", tokens[2]); os_sprintf(response, "SSID Set. Please save the configuration using save command\r\n"); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); goto command_handled; } @@ -444,7 +414,7 @@ void console_handle_command(struct espconn *pespconn) { os_sprintf(config.password, "%s", tokens[2]); os_sprintf(response, "Password Set. Please save the configuration using save command\r\n"); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); goto command_handled; } @@ -452,7 +422,7 @@ void console_handle_command(struct espconn *pespconn) { config.auto_connect = atoi(tokens[2]); os_sprintf(response, "Auto Connect Set. Please save the configuration using save command\r\n"); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); goto command_handled; } @@ -460,7 +430,7 @@ void console_handle_command(struct espconn *pespconn) { os_sprintf(config.ap_ssid, "%s", tokens[2]); os_sprintf(response, "AP SSID Set. Please save the configuration using save command\r\n"); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); goto command_handled; } @@ -468,7 +438,7 @@ void console_handle_command(struct espconn *pespconn) { os_sprintf(config.ap_password, "%s", tokens[2]); os_sprintf(response, "AP Password Set. Please save the configuration using save command\r\n"); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); goto command_handled; } @@ -476,7 +446,7 @@ void console_handle_command(struct espconn *pespconn) { config.ap_open = atoi(tokens[2]); os_sprintf(response, "Open Auth Set. Please save the configuration using save command\r\n"); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); goto command_handled; } @@ -485,7 +455,7 @@ void console_handle_command(struct espconn *pespconn) /* Control comes here only if the tokens[0] command is not handled */ os_sprintf(response, "\r\nInvalid Command\r\n"); - fputs_into_ringbuff(console_tx_buffer, response, os_strlen(response)); + ringbuf_memcpy_into(console_tx_buffer, response, os_strlen(response)); command_handled: system_os_post(0, SIG_CONSOLE_TX, (ETSParam) pespconn); @@ -504,7 +474,7 @@ static void ICACHE_FLASH_ATTR tcp_client_recv_cb(void *arg, for (index=0; index