diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a76d018 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "stdexcept": "cpp" + } +} \ No newline at end of file diff --git a/examples/usb_storage_example.cpp b/examples/usb_storage_example.cpp index 1e3837f..b027537 100644 --- a/examples/usb_storage_example.cpp +++ b/examples/usb_storage_example.cpp @@ -80,6 +80,8 @@ string storageStateToString(STORAGE_STATE state) { switch (state) { case STORAGE_INSERTED: return "INSERTED (not mounted)"; + case STORAGE_INSERTED_UNSUPPORTED: + return "INSERTED_UNSUPPORTED (unsupported filesystem)"; case STORAGE_MOUNTED: return "MOUNTED (ready for use)"; case STORAGE_UNSAFE_EJECT: diff --git a/includes/constants/StorageState.h b/includes/constants/StorageState.h index 11de9ec..c908432 100644 --- a/includes/constants/StorageState.h +++ b/includes/constants/StorageState.h @@ -16,7 +16,7 @@ namespace apra { enum STORAGE_STATE { - STORAGE_INSERTED, STORAGE_MOUNTED, STORAGE_UNSAFE_EJECT, STORAGE_SAFE_EJECT + STORAGE_INSERTED, STORAGE_INSERTED_UNMOUNTED, STORAGE_MOUNTED, STORAGE_UNSAFE_EJECT, STORAGE_SAFE_EJECT }; } diff --git a/includes/constants/StorageType.h b/includes/constants/StorageType.h index 1c847f9..ac41ec6 100644 --- a/includes/constants/StorageType.h +++ b/includes/constants/StorageType.h @@ -17,7 +17,7 @@ namespace apra { enum STORAGE_TYPE { - FAT32, NTFS, EXT4 + FAT32, NTFS, EXT4, UNSUPPORTED }; class STORAGE_TYPE_STRING diff --git a/includes/utils/StorageUSB.h b/includes/utils/StorageUSB.h index 601e211..206292e 100644 --- a/includes/utils/StorageUSB.h +++ b/includes/utils/StorageUSB.h @@ -51,6 +51,7 @@ class StorageUSB bool mountWithoutPrivilege(StorageMinimalInfo storageDevice); StorageMinimalInfo getHighCapacityPartition(std::string deviceNode); bool isDeviceNodeConnected(); + void checkDeviceNode(); bool m_shouldPrint; std::vector m_supportedTypes; @@ -60,6 +61,7 @@ class StorageUSB bool m_skipMount; STORAGE_STATE m_state; std::string m_manualPath; + int8_t m_retryCount; }; } /* namespace apra */ diff --git a/src/constants/StorageType.cpp b/src/constants/StorageType.cpp index fb9b671..189bd9d 100644 --- a/src/constants/StorageType.cpp +++ b/src/constants/StorageType.cpp @@ -24,6 +24,8 @@ std::string STORAGE_TYPE_STRING::getString(STORAGE_TYPE type) return "ntfs"; case EXT4: return "ext4"; + case UNSUPPORTED: + return ""; default: return ""; } @@ -39,10 +41,14 @@ STORAGE_TYPE STORAGE_TYPE_STRING::getEnum(std::string typeStr) { return EXT4; } - else + else if (typeStr == getString(FAT32)) { return FAT32; } + else + { + return UNSUPPORTED; + } } } /* namespace apra */ diff --git a/src/utils/GPIO.cpp b/src/utils/GPIO.cpp index b9decd6..0f30b26 100644 --- a/src/utils/GPIO.cpp +++ b/src/utils/GPIO.cpp @@ -283,7 +283,7 @@ int GPIO::ReadWithInterrupt(unsigned long uSecTout) } else if (rc == 0) { - printf("timeout \n"); + // printf("timeout \n"); return -1; } diff --git a/src/utils/StorageUSB.cpp b/src/utils/StorageUSB.cpp index 1e32c53..ac92f98 100644 --- a/src/utils/StorageUSB.cpp +++ b/src/utils/StorageUSB.cpp @@ -25,6 +25,8 @@ #include "utils/Utils.h" #include #include "utils/Macro.h" +#include +#include #define MAX_BUF_LEN 1024 @@ -36,7 +38,7 @@ StorageUSB::StorageUSB(string mountPath, vector supportedTypes, bool shouldPrint, bool skipMount) : m_shouldPrint(shouldPrint), m_supportedTypes(supportedTypes), m_mountPoint( mountPath), m_deviceNode(), m_partitionNode(), m_skipMount( - skipMount), m_state(STORAGE_SAFE_EJECT), m_manualPath(mountPath) + skipMount), m_state(STORAGE_SAFE_EJECT), m_manualPath(mountPath), m_retryCount(3) { string error; if (m_supportedTypes.empty()) @@ -147,6 +149,10 @@ string StorageUSB::insertCheck() m_state = STORAGE_INSERTED; m_deviceNode = devicePath; } + if (m_shouldPrint) + { + std::cout << "Device Node is ==========>>>" << m_deviceNode << std::endl; + } return devicePath; } return m_deviceNode; @@ -156,6 +162,7 @@ string StorageUSB::mountDevice() { string usbMountPath; string devNode = insertCheck(); + bool isSupportedFs = false; if (m_skipMount) { StorageMinimalInfo highPartition = getHighCapacityPartition(devNode); @@ -165,6 +172,12 @@ string StorageUSB::mountDevice() m_mountPoint = usbMountPath; m_partitionNode = highPartition.m_partition; } + STORAGE_TYPE type = STORAGE_TYPE_STRING::getEnum(highPartition.m_fsType); + auto it = std::find(m_supportedTypes.begin(), + m_supportedTypes.end(), + type); + isSupportedFs = (it != m_supportedTypes.end()); + m_retryCount--; } else { @@ -173,10 +186,19 @@ string StorageUSB::mountDevice() usbMountPath = m_mountPoint; } } - if (!usbMountPath.empty()) + if (!isSupportedFs) + { + m_state = STORAGE_INSERTED_UNMOUNTED; + } + else if (!usbMountPath.empty()) { m_state = STORAGE_MOUNTED; } + else if (!isSupportedFs && !m_retryCount) + { + m_state = STORAGE_INSERTED_UNMOUNTED; + m_retryCount = 0; + } else { if (m_shouldPrint) @@ -211,6 +233,8 @@ bool StorageUSB::isUnsafeEject() if (findMountDeviceBylsblk(m_partitionNode).empty()) { m_state = STORAGE_UNSAFE_EJECT; + m_deviceNode.clear(); + m_retryCount = 3; return true; } return false; @@ -231,6 +255,10 @@ StorageMinimalInfo StorageUSB::getHighCapacityPartition(std::string deviceNode) } } } + if (m_shouldPrint) + { + std::cout << "High Capacity Partition is " << highCapacityPartition.m_partition << " == " << highCapacityPartition.m_fsType << " === " << highCapacityPartition.m_size << std::endl; + } return highCapacityPartition; } @@ -496,22 +524,9 @@ bool StorageUSB::ejectDevice() { if (unMountUSBDevice()) { - string ejectCommand = "udisksctl power-off -b " + m_deviceNode; - try - { - string ejectResponse = Utils::exec(ejectCommand, m_shouldPrint); - if (Utils::caseInsensitiveSearch(ejectResponse, "error")) - { - printf("Eject error!\n%s\n", ejectResponse.c_str()); - return false; - } - } catch (std::runtime_error &e) - { - printf("eject error: %s\n", e.what()); - return false; - } + string ejectCommand = ""; m_state = STORAGE_SAFE_EJECT; - + m_retryCount = 3; m_mountPoint.clear(); m_deviceNode.clear(); m_partitionNode.clear(); @@ -528,6 +543,10 @@ string StorageUSB::findMountDeviceBylsblk(string devicePartitionNode) "lsblk " + devicePartitionNode + " --noheadings -o MOUNTPOINT", m_shouldPrint); string mountPath = Utils::trim(cmdResponse); + if (m_shouldPrint) + { + std::cout<<"\t mountPath ->" <