Skip to content

Commit b83af6a

Browse files
committed
CMUx idea and stub implementation
1 parent 0b8b045 commit b83af6a

File tree

2 files changed

+116
-2
lines changed

2 files changed

+116
-2
lines changed

libraries/GSM/src/GSM.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,20 @@
99
#define MAXRETRY 3
1010

1111

12-
mbed::CellularDevice *mbed::CellularDevice::get_default_instance()
12+
arduino::CMUXClass *mbed::CMUXClass::get_default_instance()
1313
{
14-
static mbed::BufferedSerial serial(MBED_CONF_GEMALTO_CINTERION_TX, MBED_CONF_GEMALTO_CINTERION_RX, 115200);
14+
// TODO: CHECK WHICH IS THE RIGHT ONE
15+
//static mbed::BufferedSerial serial(MBED_CONF_GEMALTO_CINTERION_TX, MBED_CONF_GEMALTO_CINTERION_RX, 115200);
16+
static mbed::UnbufferedSerial serial(MBED_CONF_GEMALTO_CINTERION_TX, MBED_CONF_GEMALTO_CINTERION_RX, 115200);
1517
serial.set_flow_control(mbed::SerialBase::RTSCTS_SW, MBED_CONF_GEMALTO_CINTERION_CTS, NC);
18+
static arduino::CMUXClass device(&serial);
19+
return &device;
20+
}
21+
22+
mbed::CellularDevice *mbed::CellularDevice::get_default_instance()
23+
{
24+
auto cmux = arduino::CMUXClass::get_default_instance();
25+
auto serial = cmux.get_serial(0);
1626
static mbed::GEMALTO_CINTERION device(&serial);
1727
return &device;
1828
}

libraries/GSM/src/GSM.h

+104
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,110 @@ class GSMClass : public MbedSocketClass {
9696
mbed::CellularDevice* _device = nullptr;
9797
};
9898

99+
100+
class PTYSerial: public FileHandle {
101+
public:
102+
PTYSerial(CMUXClass* parent) : _parent(parent) {};
103+
int populate_rx_buffer(char* buf, size_t sz) {
104+
// TO BE IMPLEMENTED
105+
}
106+
int write(char* buf, size_t sz) {
107+
_parent->populate_tx_buffer(buf, sz, this != _parent->get_serial(0));
108+
}
109+
}
110+
111+
class CMUXClass : {
112+
public:
113+
//CMUXClass(BufferedSerial* hw_serial)
114+
CMUXClass(FileHandle* hw_serial) : _hw_serial(hw_serial) {
115+
116+
_gsm_serial = new PTYSerial(this);
117+
_gps_serial = new PTYSerial(this);
118+
119+
reader_thd.start(mbed::callback(this, &CMUXClass::read));
120+
writer_thd.start(mbed::callback(this, &CMUXClass::write));
121+
_hw_serial.attach(mbed::callback(this, &CMUXClass::on_rx), mbed::SerialBase::RxIrq);
122+
}
123+
124+
void on_rx() {
125+
while(_hw_serial->readable()) {
126+
char c;
127+
core_util_critical_section_enter();
128+
_hw_serial->read(&c, 1);
129+
rx_buffer.push(c);
130+
}
131+
osSignalSet(reader_thd.get_id(), 0xA);
132+
}
133+
134+
void read() {
135+
while (1) {
136+
osSignalWait(0, osWaitForever);
137+
char temp_buf[256];
138+
size_t howMany = rx_buffer.size();
139+
rx_buffer.pop(temp_buf, howMany);
140+
size_t i = 0;
141+
while (i < howMany) {
142+
char payload[256];
143+
int frame_id;
144+
// cmux_handle_frame increments temp_buf
145+
auto ret = cmux_handle_frame(temp_buf, howMany, final_buf, &frame_id);
146+
if (ret <= 0) {
147+
// push again pop-ped data in rx_buffer and break
148+
rx_buffer.push(temp_buf, howMany - actualPosition);
149+
break;
150+
}
151+
if (frame_id == 0) {
152+
_gsm_serial->populate_rx_buffer(final_buf, ret);
153+
}
154+
if (frame_id == 1) {
155+
_gps_serial->populate_rx_buffer(final_buf, ret);
156+
}
157+
}
158+
}
159+
}
160+
161+
void write() {
162+
while (1) {
163+
auto ev = osSignalWait(0, osWaitForever);
164+
char payload[256];
165+
char frame[256];
166+
uint8_t frame_id = ev.value.v;
167+
size_t howMany = tx_buffer.size();
168+
tx_buffer.pop(temp_buf, howMany);
169+
int ret = cmux_write_buffer(payload, howMany, frame_id, frame);
170+
if (ret > 0) {
171+
_hw_serial->write(frame, ret);
172+
}
173+
}
174+
}
175+
176+
int populate_tx_buffer(char* buf, size_t sz, uint8_t id) {
177+
tx_buffer.push(buf, sz);
178+
osSignalSet(writer_thd.get_id(), id);
179+
}
180+
181+
static CMUXClass *get_default_instance();
182+
183+
FileHandle* get_serial(int index) {
184+
if (index == 0) {
185+
return _gsm_serial;
186+
}
187+
if (index == 1) {
188+
return _gps_serial;
189+
}
190+
return nullptr;
191+
}
192+
193+
private:
194+
FileHandle* _hw_serial = nullptr;
195+
FileHandle* _gsm_serial = nullptr;
196+
FileHandle* _gps_serial = nullptr;
197+
CircularBuffer<char, 1024> rx_buffer;
198+
CircularBuffer<char, 1024> tx_buffer;
199+
rtos::Thread reader_thd = rtos::Thread{osPriorityNormal, 4096, nullptr, "CMUXt1"};
200+
rtos::Thread writer_thd = rtos::Thread{osPriorityNormal, 4096, nullptr, "CMUXt2"};;
201+
};
202+
99203
}
100204

101205
extern GSMClass GSM;

0 commit comments

Comments
 (0)