/** * @brief Send data and wait for prompt from DCE * * @param dte Modem DTE object * @param data data buffer * @param length length of data to send * @param prompt pointer of specific prompt * @param timeout timeout value (unit: ms) * @return esp_err_t * ESP_OK on success * ESP_FAIL on error */ static esp_err_t esp_modem_dte_send_wait(modem_dte_t *dte, const char *data, uint32_t length, const char *prompt, uint32_t timeout) { MODEM_CHECK(data, "data is NULL", err_param); MODEM_CHECK(prompt, "prompt is NULL", err_param); esp_modem_dte_t *esp_dte = __containerof(dte, esp_modem_dte_t, parent); // We'd better disable pattern detection here for a moment in case prompt string contains the pattern character uart_disable_pattern_det_intr(esp_dte->uart_port); // uart_disable_rx_intr(esp_dte->uart_port); MODEM_CHECK(uart_write_bytes(esp_dte->uart_port, data, length) >= 0, "uart write bytes failed", err_write); uint32_t len = strlen(prompt); uint8_t *buffer = calloc(len + 1, sizeof(uint8_t)); int res = uart_read_bytes(esp_dte->uart_port, buffer, len, pdMS_TO_TICKS(timeout)); MODEM_CHECK(res >= len, "wait prompt [%s] timeout", err, prompt); MODEM_CHECK(!strncmp(prompt, (const char *)buffer, len), "get wrong prompt: %s", err, buffer); free(buffer); uart_enable_pattern_det_intr(esp_dte->uart_port, '\n', 1, MIN_PATTERN_INTERVAL, MIN_POST_IDLE, MIN_PRE_IDLE); return ESP_OK; err: free(buffer); err_write: uart_enable_pattern_det_intr(esp_dte->uart_port, '\n', 1, MIN_PATTERN_INTERVAL, MIN_POST_IDLE, MIN_PRE_IDLE); err_param: return ESP_FAIL; }
/** * @brief Send data to DCE * * @param dte Modem DTE object * @param data data buffer * @param length length of data to send * @return int actual length of data that has been send out */ static int esp_modem_dte_send_data(modem_dte_t *dte, const char *data, uint32_t length) { MODEM_CHECK(data, "data is NULL", err); esp_modem_dte_t *esp_dte = __containerof(dte, esp_modem_dte_t, parent); return uart_write_bytes(esp_dte->uart_port, data, length); err: return -1; }
void init() { const uart_config_t uart_config = { .baud_rate = 115200, .data_bits = UART_DATA_8_BITS, .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, .flow_ctrl = UART_HW_FLOWCTRL_DISABLE }; uart_param_config(UART_NUM_1, &uart_config); uart_set_pin(UART_NUM_1, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); // We won't use a buffer for sending data. uart_driver_install(UART_NUM_1, RX_BUF_SIZE * 2, 0, 0, NULL, 0); } int sendData(const char* logName, const char* data) { const int len = strlen(data); const int txBytes = uart_write_bytes(UART_NUM_1, data, len); ESP_LOGI(logName, "Wrote %d bytes", txBytes); return txBytes; } static void tx_task() { static const char *TX_TASK_TAG = "TX_TASK"; esp_log_level_set(TX_TASK_TAG, ESP_LOG_INFO); while (1) { sendData(TX_TASK_TAG, "Hello world"); vTaskDelay(2000 / portTICK_PERIOD_MS); } } static void rx_task() { static const char *RX_TASK_TAG = "RX_TASK"; esp_log_level_set(RX_TASK_TAG, ESP_LOG_INFO); uint8_t* data = (uint8_t*) malloc(RX_BUF_SIZE+1); while (1) { const int rxBytes = uart_read_bytes(UART_NUM_1, data, RX_BUF_SIZE, 1000 / portTICK_RATE_MS); if (rxBytes > 0) { data[rxBytes] = 0; ESP_LOGI(RX_TASK_TAG, "Read %d bytes: '%s'", rxBytes, data); ESP_LOG_BUFFER_HEXDUMP(RX_TASK_TAG, data, rxBytes, ESP_LOG_INFO); } } free(data); } void app_main() { init(); xTaskCreate(rx_task, "uart_rx_task", 1024*2, NULL, configMAX_PRIORITIES, NULL); xTaskCreate(tx_task, "uart_tx_task", 1024*2, NULL, configMAX_PRIORITIES-1, NULL); }
/** * @brief Send command to DCE * * @param dte Modem DTE object * @param command command string * @param timeout timeout value, unit: ms * @return esp_err_t * - ESP_OK on success * - ESP_FAIL on error */ static esp_err_t esp_modem_dte_send_cmd(modem_dte_t *dte, const char *command, uint32_t timeout) { esp_err_t ret = ESP_FAIL; modem_dce_t *dce = dte->dce; MODEM_CHECK(dce, "DTE has not yet bind with DCE", err); MODEM_CHECK(command, "command is NULL", err); esp_modem_dte_t *esp_dte = __containerof(dte, esp_modem_dte_t, parent); /* Calculate timeout clock tick */ /* Reset runtime information */ dce->state = MODEM_STATE_PROCESSING; /* Send command via UART */ uart_write_bytes(esp_dte->uart_port, command, strlen(command)); /* Check timeout */ MODEM_CHECK(xSemaphoreTake(esp_dte->process_sem, pdMS_TO_TICKS(timeout)) == pdTRUE, "process command timeout", err); ret = ESP_OK; err: dce->handle_line = NULL; return ret; }
static void uart_input_handler(const char *data, int len) { int i; if (len < 1) { return; } if (data[0] == 0x7f || data[0] == 0x08) { if (rcvMsg.pWritePos != rcvMsg.pRcvMsgBuff) { const char back = 0x08, space = ' '; uart_write_bytes(UART_NUM_0, &back, 1); uart_write_bytes(UART_NUM_0, &space, 1); //uart_write_bytes(UART_NUM_0, &back, 1); rcvMsg.pWritePos[0] = 0; rcvMsg.pWritePos -= 1; } else { // nothing to delete return; } } if (data[0] == '\r' || data[0] == '\n') { // enter key press rcvMsg.pWritePos[0] = 0; if (rcvMsg.pWritePos == rcvMsg.pRcvMsgBuff) { uart_write_bytes(UART_NUM_0, PROMPT, PROMPT_LEN); } else { int length = rcvMsg.pWritePos - rcvMsg.pRcvMsgBuff; uart_write_bytes(UART_NUM_0, PROMPT, PROMPT_LEN); if (status == INPUT_SSID) { memset(wifi_ssid, 0, WIFI_SSID_MAX_LEN); memcpy(wifi_ssid, rcvMsg.pRcvMsgBuff, length); status = NONE; ESP_LOGI(TAG, "you set ssid: %s", wifi_ssid); esp_err_t er = save_string(CMD_SSID, (char *)wifi_ssid); if (er != ESP_OK) ESP_LOGE(TAG, "save ssid error"); goto set_end; } else if (status == INPUT_PASS) { memset(wifi_pass, 0, WIFI_PASS_MAX_LEN); memcpy(wifi_pass, rcvMsg.pRcvMsgBuff, length); status = NONE; ESP_LOGI(TAG, "you set password: %s", wifi_pass); esp_err_t er = save_string(CMD_PASS, (char *)wifi_pass); if (er != ESP_OK) ESP_LOGE(TAG, "save pass error"); goto set_end; } else if (status == INPUT_IP) { memset(srv_ip, 0, SRV_IP_MAX_LEN); memcpy(srv_ip, rcvMsg.pRcvMsgBuff, length); status = NONE; ESP_LOGI(TAG, "you set server ip: %s", srv_ip); esp_err_t er = save_string(CMD_IP, (char *)srv_ip); if (er != ESP_OK) ESP_LOGE(TAG, "save ip error"); goto set_end; } else if (status == INPUT_PORT) { if (!is_valid_port((const char *)rcvMsg.pRcvMsgBuff, length)) { rcvMsg.pWritePos = rcvMsg.pRcvMsgBuff; ESP_LOGE(TAG, "invalid server port, please input again"); return; } srv_port = str2num((const char *)rcvMsg.pRcvMsgBuff, length); status = NONE; ESP_LOGI(TAG, "you set server port: %d", srv_port); //esp_err_t er = save_int32(CMD_PORT, srv_port); esp_err_t er = save_string(CMD_PORT, (char *)rcvMsg.pRcvMsgBuff); if (er != ESP_OK) ESP_LOGE(TAG, "save port error"); goto set_end; } if (strncmp((const char *)rcvMsg.pRcvMsgBuff, CMD_SSID, length) == 0) { ESP_LOGI(TAG, "input your ssid [less than 32byte]:"); status = INPUT_SSID; } else if (strncmp((const char *)rcvMsg.pRcvMsgBuff, CMD_PASS, length) == 0) { ESP_LOGI(TAG, "input your password [less than 32byte]:"); status = INPUT_PASS; } else if (strncmp((const char *)rcvMsg.pRcvMsgBuff, CMD_IP, length) == 0) { ESP_LOGI(TAG, "input server IP:"); status = INPUT_IP; } else if (strncmp((const char *)rcvMsg.pRcvMsgBuff, CMD_PORT, length) == 0) { ESP_LOGI(TAG, "input server port:"); status = INPUT_PORT; } else if (strncmp((const char *)rcvMsg.pRcvMsgBuff, CMD_HELP, length) == 0) { show_help_info(); } else if (strncmp((const char *)rcvMsg.pRcvMsgBuff, CMD_WIFI, length) == 0) { ESP_LOGI(TAG, "BLE scanning ......"); close_nvs(); ble_client_app_register(); //initialise_wifi(); } else if (strncmp((const char *)rcvMsg.pRcvMsgBuff, CMD_QUIT, length) == 0) { ESP_LOGI(TAG, "reset wifi info:"); clear_storage(); close_nvs(); system_restart(); } else { ESP_LOGI(TAG, "invalid command %s", rcvMsg.pRcvMsgBuff); show_help_info(); } } set_end: rcvMsg.pWritePos = rcvMsg.pRcvMsgBuff; memset(rcvMsg.pRcvMsgBuff, 0, RECV_BUF_SIZE); return; } *(rcvMsg.pWritePos) = data[0]; uart_write_bytes(UART_NUM_0, &data[0], 1); rcvMsg.pWritePos += 1; if (rcvMsg.pWritePos - rcvMsg.pRcvMsgBuff >= RECV_BUF_SIZE - 1) { rcvMsg.pWritePos = rcvMsg.pRcvMsgBuff; // overflow, restart ESP_LOGI(TAG, "overflow"); } }