/ firmware / src / console / session.cpp
session.cpp
 1  #include "session.h"
 2  
 3  void programs::shell::session::reset(RingBuffer *ring) {
 4    if (!ring) return;
 5    ring->head.store(0, std::memory_order_relaxed);
 6    ring->tail.store(0, std::memory_order_relaxed);
 7  }
 8  
 9  bool programs::shell::session::push(RingBuffer *ring, char ch) {
10    if (!ring || !ring->data || ring->capacity == 0) return false;
11    uint16_t head = ring->head.load(std::memory_order_relaxed);
12    uint16_t tail = ring->tail.load(std::memory_order_acquire);
13    uint16_t next = (head + 1) % ring->capacity;
14    if (next == tail) return false;
15    ring->data[head] = ch;
16    ring->head.store(next, std::memory_order_release);
17    return true;
18  }
19  
20  int programs::shell::session::pop(RingBuffer *ring, char *ch) {
21    if (!ring || !ch || !ring->data) return 0;
22    uint16_t tail = ring->tail.load(std::memory_order_relaxed);
23    uint16_t head = ring->head.load(std::memory_order_acquire);
24    if (head == tail) return 0;
25    *ch = ring->data[tail];
26    ring->tail.store((tail + 1) % ring->capacity, std::memory_order_release);
27    return 1;
28  }
29  
30  void programs::shell::session::reset(WriteBuffer *buffer) {
31    if (!buffer) return;
32    buffer->position = 0;
33  }
34  
35  bool programs::shell::session::push(WriteBuffer *buffer, char ch) {
36    if (!buffer || !buffer->data || buffer->capacity == 0) return false;
37    if (buffer->position >= buffer->capacity) return false;
38    buffer->data[buffer->position++] = ch;
39    return true;
40  }