unsigned char ringbuf_remove(struct ring_buffer *buffer) { unsigned char c = ringbuf_peek(buffer); buffer->tail = (buffer->tail + 1); if (buffer->tail >= SERIAL_BUFFER_SIZE) buffer->tail -= SERIAL_BUFFER_SIZE; return c; }
size_t ringbuf_copy(struct ringbuf *to, struct ringbuf *from, size_t size) { size_t tocopy = MIN(ringbuf_space(to), size); uint8_t *cbuf = XCALLOC(MTYPE_TMP, tocopy); tocopy = ringbuf_peek(from, 0, cbuf, tocopy); size_t put = ringbuf_put(to, cbuf, tocopy); XFREE(MTYPE_TMP, cbuf); return put; }
static void process_input(struct hfp_gw *hfp) { char *str, *ptr; size_t len, count; bool free_ptr = false; bool read_again; do { str = ringbuf_peek(hfp->read_buf, 0, &len); if (!str) return; ptr = memchr(str, '\r', len); if (!ptr) { char *str2; size_t len2; /* * If there is no more data in ringbuffer, * it's just an incomplete command. */ if (len == ringbuf_len(hfp->read_buf)) return; str2 = ringbuf_peek(hfp->read_buf, len, &len2); if (!str2) return; ptr = memchr(str2, '\r', len2); if (!ptr) return; *ptr = '\0'; count = len2 + len; ptr = malloc(count); if (!ptr) return; memcpy(ptr, str, len); memcpy(ptr + len, str2, len2); free_ptr = true; str = ptr; } else { count = ptr - str; *ptr = '\0'; } if (!handle_at_command(hfp, str)) /* * Command is not handled that means that was some * trash. Let's skip that and keep reading from ring * buffer. */ read_again = true; else /* * Command has been handled. If we are waiting for a * result from upper layer, we can stop reading. If we * already reply i.e. ERROR on unknown command, then we * can keep reading ring buffer. Actually ring buffer * should be empty but lets just look there. */ read_again = !hfp->result_pending; ringbuf_drain(hfp->read_buf, count + 1); if (free_ptr) free(ptr); } while (read_again); }
static void hf_process_input(struct hfp_hf *hfp) { char *str, *ptr, *str2, *tmp; size_t len, count, offset, len2; bool free_tmp = false; str = ringbuf_peek(hfp->read_buf, 0, &len); if (!str) return; offset = 0; ptr = find_cr_lf(str, len); while (ptr) { count = ptr - (str + offset); if (count == 0) { /* 2 is for <cr><lf> */ offset += 2; } else { *ptr = '\0'; hf_call_prefix_handler(hfp, str + offset); offset += count + 2; } ptr = find_cr_lf(str + offset, len - offset); } /* * Just check if there is no wrapped data in ring buffer. * Should not happen too often */ if (len == ringbuf_len(hfp->read_buf)) goto done; str2 = ringbuf_peek(hfp->read_buf, len, &len2); if (!str2) goto done; ptr = find_cr_lf(str2, len2); if (!ptr) { /* Might happen that we wrap between \r and \n */ ptr = memchr(str2, '\n', len2); if (!ptr) goto done; } count = ptr - str2; if (count) { *ptr = '\0'; tmp = malloc(len + count); if (!tmp) goto done; /* "str" here is not a string so we need to use memcpy */ memcpy(tmp, str, len); memcpy(tmp + len, str2, count); free_tmp = true; } else { str[len-1] = '\0'; tmp = str; } hf_call_prefix_handler(hfp, tmp); offset += count; done: ringbuf_drain(hfp->read_buf, offset); if (free_tmp) free(tmp); }