ESP32/components/sd_card/sd_card.h

159 lines
4.7 KiB
C

/*
* sd_card.h
*
* Copyright (c) 2025 Umber Networks & Robert McMahon
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SD_CARD_H
#define SD_CARD_H
#include "esp_err.h"
#include <stdbool.h>
#include <stdint.h>
/**
* @brief Initialize SD card using SDIO interface
*
* @return ESP_OK on success, error code otherwise
*/
esp_err_t sd_card_init(void);
/**
* @brief Deinitialize SD card
*
* @return ESP_OK on success
*/
esp_err_t sd_card_deinit(void);
/**
* @brief Check if SD card is mounted and ready
*
* @return true if SD card is ready, false otherwise
*/
bool sd_card_is_ready(void);
/**
* @brief Check if Card Detect (CD) pin is configured and wired
*
* @return true if CD pin is available, false otherwise
*/
bool sd_card_cd_available(void);
/**
* @brief Check if a card is physically inserted (CD pin, polarity from Kconfig)
* Only meaningful when sd_card_cd_available() is true.
*
* @return true if CD indicates card inserted, false if removed or N/A
*/
bool sd_card_cd_is_inserted(void);
/**
* @brief Get raw CD pin level for diagnostics (0=LOW, 1=HIGH, -1=not configured)
*/
int sd_card_cd_get_level(void);
/**
* @brief Get SD card capacity information
*
* @param total_bytes Output parameter for total capacity in bytes
* @param free_bytes Output parameter for free space in bytes
* @return ESP_OK on success
*/
esp_err_t sd_card_get_info(uint64_t *total_bytes, uint64_t *free_bytes);
/**
* @brief Write data to a file on the SD card
*
* @param filename File path (e.g., "/sdcard/telemetry.json")
* @param data Data to write
* @param len Length of data in bytes
* @param append If true, append to file; if false, overwrite
* @return ESP_OK on success
*/
esp_err_t sd_card_write_file(const char *filename, const void *data, size_t len, bool append);
/**
* @brief Read data from a file on the SD card
*
* @param filename File path
* @param data Buffer to read into
* @param len Maximum length to read
* @param bytes_read Output parameter for actual bytes read
* @return ESP_OK on success
*/
esp_err_t sd_card_read_file(const char *filename, void *data, size_t len, size_t *bytes_read);
/**
* @brief Get size of a file in bytes
*
* @param filename File path
* @param size_bytes Output parameter for file size
* @return ESP_OK on success
*/
esp_err_t sd_card_get_file_size(const char *filename, size_t *size_bytes);
/**
* @brief Read a chunk of a file at given offset (for streaming)
*
* @param filename File path
* @param offset Byte offset from start
* @param data Buffer to read into
* @param len Maximum length to read
* @param bytes_read Output parameter for actual bytes read
* @return ESP_OK on success
*/
esp_err_t sd_card_read_file_at(const char *filename, size_t offset, void *data, size_t len, size_t *bytes_read);
/**
* @brief Check if a file exists on the SD card
*
* @param filename File path
* @return true if file exists, false otherwise
*/
bool sd_card_file_exists(const char *filename);
/**
* @brief List files in a directory on the SD card
*
* @param path Directory path (empty or "/" for root)
* @return ESP_OK on success
*/
esp_err_t sd_card_list_dir(const char *path);
/**
* @brief Delete a file from the SD card
*
* @param filename File path
* @return ESP_OK on success
*/
esp_err_t sd_card_delete_file(const char *filename);
#endif // SD_CARD_H