@@ -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
101205extern GSMClass GSM;
0 commit comments