linux/esptun.c
static bool esp_read_responses(int fd, bool early_terminate)
{
    bool finished = false;
    bool retval = false;
    /** TODO handle timeouts. */
    while (!finished) {
        ssize_t n = read(fd, &esp_buffer[esp_end], ESP_BUFSIZE - esp_end);
        if (n <= 0) {
            my_err("Reading from UART (%s)", strerror(errno));
        }
        esp_end += n;

        size_t ptr = 0;
        while (ptr < esp_end) {
            const uint8_t *resp = &esp_buffer[ptr];
            struct packet_info pkt_info = {};
            size_t len = esp_response_is_complete(resp, esp_end - ptr, &pkt_info);
            if (len) {
                logprintf(DEBUG, "<-: ");
                debug_response(resp, len);
                logprintf(DEBUG, "\n");
                /* Check for final response */
                if (is_prefix(resp, len, S("OK"))
                 || is_prefix(resp, len, S("SEND OK"))) {
                    finished = true;
                    retval = true;
                }
                if (is_prefix(resp, len, S("FAIL"))
                  || is_prefix(resp, len, S("SEND FAIL"))
                  || is_prefix(resp, len, S("ERROR"))
                  || is_prefix(resp, len, S("ALREADY CONNECTED"))) {
                    finished = true;
                    retval = false;
                }
                /* Log interesting info responses */
                if (is_prefix(resp, len, S("+CIPSTA_CUR"))) {
                    logprintf(INFO2, "  %.*s\n", (int)(len - 11 - 2), resp + 11);
                }
                if (is_prefix(resp, len, S("WIFI "))) {
                    logprintf(INFO2, "  %.*s\n", (int)(len - 2), resp);
                }
                /* Did we receive a packet? */
                if (is_prefix(resp, len, pktprefix, sizeof(pktprefix)) && tun_fd != -1) {
                    logprintf(DEBUG, "NET2TUN %lu: Read %ld bytes from the esp interface\n", net2tap, pkt_info.payload_len);
                    net2tap++;
                    tun_tx_packet(tun_fd, pkt_info.payload, pkt_info.payload_len);
                }

                /* On non-final response, keep reading. */
            } else {
                break;
            }
            ptr += len;
        }

        if (ptr == 0 && esp_end == ESP_BUFSIZE) {
            /* Buffer full there was no progress processing responses - this isn't good,
             * exit to prevent looping forever. */
            my_err("Buffer full with unterminated command");
        }

        /* Remove processed responses from esp_buffer by shifting bytes. */
        memmove(esp_buffer, &esp_buffer[ptr], esp_end - ptr);
        esp_end -= ptr;

        if (early_terminate && esp_end == 0) {
            /* All responses processed, back to select so that packets from tun get a chance. */
            finished = true;
            retval = true;
        }
    }
    return retval;
}
/** Get OK/FAIL status after running a command on the ESP. */
Get OK/FAIL status after running a command on the ESP.
Get OK / FAIL status after running a command on the ESP .
static bool esp_read_responses(int fd, bool early_terminate)
{
    bool finished = false;
    bool retval = false;
    while (!finished) {
        ssize_t n = read(fd, &esp_buffer[esp_end], ESP_BUFSIZE - esp_end);
        if (n <= 0) {
            my_err("Reading from UART (%s)", strerror(errno));
        }
        esp_end += n;
        size_t ptr = 0;
        while (ptr < esp_end) {
            const uint8_t *resp = &esp_buffer[ptr];
            struct packet_info pkt_info = {};
            size_t len = esp_response_is_complete(resp, esp_end - ptr, &pkt_info);
            if (len) {
                logprintf(DEBUG, "<-: ");
                debug_response(resp, len);
                logprintf(DEBUG, "\n");
                if (is_prefix(resp, len, S("OK"))
                 || is_prefix(resp, len, S("SEND OK"))) {
                    finished = true;
                    retval = true;
                }
                if (is_prefix(resp, len, S("FAIL"))
                  || is_prefix(resp, len, S("SEND FAIL"))
                  || is_prefix(resp, len, S("ERROR"))
                  || is_prefix(resp, len, S("ALREADY CONNECTED"))) {
                    finished = true;
                    retval = false;
                }
                if (is_prefix(resp, len, S("+CIPSTA_CUR"))) {
                    logprintf(INFO2, "  %.*s\n", (int)(len - 11 - 2), resp + 11);
                }
                if (is_prefix(resp, len, S("WIFI "))) {
                    logprintf(INFO2, "  %.*s\n", (int)(len - 2), resp);
                }
                if (is_prefix(resp, len, pktprefix, sizeof(pktprefix)) && tun_fd != -1) {
                    logprintf(DEBUG, "NET2TUN %lu: Read %ld bytes from the esp interface\n", net2tap, pkt_info.payload_len);
                    net2tap++;
                    tun_tx_packet(tun_fd, pkt_info.payload, pkt_info.payload_len);
                }
            } else {
                break;
            }
            ptr += len;
        }
        if (ptr == 0 && esp_end == ESP_BUFSIZE) {
            my_err("Buffer full with unterminated command");
        }
        memmove(esp_buffer, &esp_buffer[ptr], esp_end - ptr);
        esp_end -= ptr;
        if (early_terminate && esp_end == 0) {
            finished = true;
            retval = true;
        }
    }
    return retval;
}
static bool esp_read_responses ( int fd , bool early_terminate ) { bool finished = false ; bool retval = false ; while ( ! finished ) { ssize_t n = read ( fd , & esp_buffer [ esp_end ] , ESP_BUFSIZE - esp_end ) ; if ( n <= 0 ) { my_err ( " " , strerror ( errno ) ) ; } esp_end += n ; size_t ptr = 0 ; while ( ptr < esp_end ) { const uint8_t * resp = & esp_buffer [ ptr ] ; struct packet_info pkt_info = { } ; size_t len = esp_response_is_complete ( resp , esp_end - ptr , & pkt_info ) ; if ( len ) { logprintf ( DEBUG , " " ) ; debug_response ( resp , len ) ; logprintf ( DEBUG , " \n " ) ; if ( is_prefix ( resp , len , S ( " " ) ) || is_prefix ( resp , len , S ( " " ) ) ) { finished = true ; retval = true ; } if ( is_prefix ( resp , len , S ( " " ) ) || is_prefix ( resp , len , S ( " " ) ) || is_prefix ( resp , len , S ( " " ) ) || is_prefix ( resp , len , S ( " " ) ) ) { finished = true ; retval = false ; } if ( is_prefix ( resp , len , S ( " " ) ) ) { logprintf ( INFO2 , " \n " , ( int ) ( len - 11 - 2 ) , resp + 11 ) ; } if ( is_prefix ( resp , len , S ( " " ) ) ) { logprintf ( INFO2 , " \n " , ( int ) ( len - 2 ) , resp ) ; } if ( is_prefix ( resp , len , pktprefix , sizeof ( pktprefix ) ) && tun_fd != -1 ) { logprintf ( DEBUG , " \n " , net2tap , pkt_info . payload_len ) ; net2tap ++ ; tun_tx_packet ( tun_fd , pkt_info . payload , pkt_info . payload_len ) ; } } else { break ; } ptr += len ; } if ( ptr == 0 && esp_end == ESP_BUFSIZE ) { my_err ( " " ) ; } memmove ( esp_buffer , & esp_buffer [ ptr ] , esp_end - ptr ) ; esp_end -= ptr ; if ( early_terminate && esp_end == 0 ) { finished = true ; retval = true ; } } return retval ; }
Get OK/FAIL status after running a command on the ESP.
Get OK / FAIL status after running a command on the ESP .
/** TODO handle timeouts. */ /* Check for final response */ /* Log interesting info responses */ /* Did we receive a packet? */ /* On non-final response, keep reading. */ /* Buffer full there was no progress processing responses - this isn't good,
             * exit to prevent looping forever. */ /* Remove processed responses from esp_buffer by shifting bytes. */ /* All responses processed, back to select so that packets from tun get a chance. */