@@ -65,6 +65,10 @@ void DTE::set_command_callbacks()
6565{
6666 primary_term->set_read_cb ([this ](uint8_t *data, size_t len) {
6767 Scoped<Lock> l (command_cb.line_lock );
68+ #ifdef CONFIG_ESP_MODEM_URC_HANDLER
69+ // Update buffer state when new data arrives
70+ update_buffer_state (len);
71+ #endif
6872#ifndef CONFIG_ESP_MODEM_URC_HANDLER
6973 if (command_cb.got_line == nullptr || command_cb.result != command_result::TIMEOUT) {
7074 return false ; // this line has been processed already (got OK or FAIL previously)
@@ -80,7 +84,7 @@ void DTE::set_command_callbacks()
8084 std::memcpy (inflatable.current (), data, len);
8185 data = inflatable.begin ();
8286 }
83- if (command_cb.process_line (data, inflatable.consumed , len)) {
87+ if (command_cb.process_line (data, inflatable.consumed , len, this )) {
8488 return true ;
8589 }
8690 // at this point we're sure that the data processing hasn't finished,
@@ -92,7 +96,7 @@ void DTE::set_command_callbacks()
9296 inflatable.consumed += len;
9397 return false ;
9498#else
95- if (command_cb.process_line (data, 0 , len)) {
99+ if (command_cb.process_line (data, 0 , len, this )) {
96100 return true ;
97101 }
98102 // cannot inflate and the processing hasn't finishes in the first iteration, but continue
@@ -105,7 +109,7 @@ void DTE::set_command_callbacks()
105109 if (buffer.size > buffer.consumed ) {
106110 data = buffer.get ();
107111 len = primary_term->read (data + buffer.consumed , buffer.size - buffer.consumed );
108- if (command_cb.process_line (data, buffer.consumed , len)) {
112+ if (command_cb.process_line (data, buffer.consumed , len, this )) {
109113 return true ;
110114 }
111115 buffer.consumed += len;
@@ -121,7 +125,7 @@ void DTE::set_command_callbacks()
121125 inflatable.grow (inflatable.consumed + len);
122126 }
123127 len = primary_term->read (inflatable.current (), len);
124- if (command_cb.process_line (inflatable.begin (), inflatable.consumed , len)) {
128+ if (command_cb.process_line (inflatable.begin (), inflatable.consumed , len, this )) {
125129 return true ;
126130 }
127131 inflatable.consumed += len;
@@ -150,10 +154,19 @@ void DTE::set_command_callbacks()
150154command_result DTE::command (const std::string &command, got_line_cb got_line, uint32_t time_ms, const char separator)
151155{
152156 Scoped<Lock> l1 (internal_lock);
157+ #ifdef CONFIG_ESP_MODEM_URC_HANDLER
158+ // Track command start
159+ buffer_state.command_waiting = true ;
160+ buffer_state.command_start_offset = buffer_state.total_processed ;
161+ #endif
153162 command_cb.set (got_line, separator);
154163 primary_term->write ((uint8_t *)command.c_str (), command.length ());
155164 command_cb.wait_for_line (time_ms);
156165 command_cb.set (nullptr );
166+ #ifdef CONFIG_ESP_MODEM_URC_HANDLER
167+ // Track command end
168+ buffer_state.command_waiting = false ;
169+ #endif
157170 buffer.consumed = 0 ;
158171#ifdef CONFIG_ESP_MODEM_USE_INFLATABLE_BUFFER_IF_NEEDED
159172 inflatable.deflate ();
@@ -365,18 +378,54 @@ void DTE::on_read(got_line_cb on_read_cb)
365378 });
366379}
367380
368- bool DTE::command_cb::process_line (uint8_t *data, size_t consumed, size_t len)
381+ bool DTE::command_cb::process_line (uint8_t *data, size_t consumed, size_t len, DTE* dte )
369382{
370383 // returning true indicates that the processing finished and lower layers can destroy the accumulated buffer
371384#ifdef CONFIG_ESP_MODEM_URC_HANDLER
372- bool consume_buffer = false ;
385+ // Call enhanced URC handler if registered
386+ if (enhanced_urc_handler && dte) {
387+ // Create buffer info for enhanced URC handler
388+ UrcBufferInfo buffer_info = dte->create_urc_info (data, consumed, len);
389+
390+ // Call enhanced URC handler
391+ UrcConsumeInfo consume_info = enhanced_urc_handler (buffer_info);
392+
393+ // Handle consumption control
394+ switch (consume_info.result ) {
395+ case UrcConsumeResult::CONSUME_NONE:
396+ // Don't consume anything, continue with command processing
397+ break ;
398+
399+ case UrcConsumeResult::CONSUME_PARTIAL:
400+ // Consume only specified amount
401+ dte->buffer_state .last_urc_processed += consume_info.consume_size ;
402+ // Adjust data pointers for command processing
403+ data += consume_info.consume_size ;
404+ consumed = (consumed + len) - consume_info.consume_size ;
405+ len = 0 ;
406+ break ;
407+
408+ case UrcConsumeResult::CONSUME_ALL:
409+ // Consume entire buffer
410+ dte->buffer_state .last_urc_processed = consumed + len;
411+ return true ; // Signal buffer consumption
412+ }
413+ }
414+
415+ // Fallback to legacy URC handler if enhanced handler not set
373416 if (urc_handler) {
374- consume_buffer = urc_handler (data, consumed + len) != command_result::TIMEOUT;
417+ bool consume_buffer = urc_handler (data, consumed + len) != command_result::TIMEOUT;
418+ if (result != command_result::TIMEOUT || got_line == nullptr ) {
419+ return consume_buffer; // this line has been processed already (got OK or FAIL previously)
420+ }
375421 }
422+ #endif
423+
424+ // Continue with normal command processing
376425 if (result != command_result::TIMEOUT || got_line == nullptr ) {
377- return consume_buffer ; // this line has been processed already (got OK or FAIL previously)
426+ return false ; // Command processing continues
378427 }
379- # endif
428+
380429 if (memchr (data + consumed, separator, len)) {
381430 result = got_line (data, consumed + len);
382431 if (result == command_result::OK || result == command_result::FAIL) {
@@ -423,3 +472,22 @@ void DTE::extra_buffer::grow(size_t need_size)
423472 */
424473unique_buffer::unique_buffer (size_t size):
425474 data(std::make_unique<uint8_t []>(size)), size(size), consumed(0 ) {}
475+
476+ #ifdef CONFIG_ESP_MODEM_URC_HANDLER
477+ void DTE::update_buffer_state (size_t new_data_size)
478+ {
479+ buffer_state.total_processed += new_data_size;
480+ }
481+
482+ DTE::UrcBufferInfo DTE::create_urc_info (uint8_t * data, size_t consumed, size_t len)
483+ {
484+ return {
485+ .buffer_start = data,
486+ .buffer_total_size = consumed + len,
487+ .processed_offset = buffer_state.last_urc_processed ,
488+ .new_data_size = (consumed + len) - buffer_state.last_urc_processed ,
489+ .new_data_start = data + buffer_state.last_urc_processed ,
490+ .is_command_active = buffer_state.command_waiting
491+ };
492+ }
493+ #endif
0 commit comments