#ifndef _FY_OS_H_ #define _FY_OS_H_ #include #include #include #include #include #include #include #include #include #include #include #include #include "strings.hpp" #include "entry/EasyUIContext.h" namespace base { enum endians { endian_big, endian_little, }; /** * 小端返回true,大端false */ inline bool is_little_endian() { union { int a; char b; } c; c.a = 1; return (c.b == 1); } template inline void swap(T* a, T* b) { *a = (*a) ^ (*b); *b = (*a) ^ (*b); *a = (*a) ^ (*b); } template inline void reverse_endian(T* v) { typedef unsigned char byte; byte* b = (byte*) v; for (int i = 0; i < (int)(sizeof(T)) / 2; ++i) { swap(&(b[i]), &(b[sizeof(T) - 1 - i])); } } template inline T from_endian(T val, bool le) { static bool e = is_little_endian(); if (e != le) { reverse_endian(&val); return val; } return val; } inline ::size_t to_uint16(const char* begin, endians endian) { if (endian_big == endian) { return (::size_t(begin[0]) << 8) | ::size_t(begin[1]); } else { return (::size_t(begin[1]) << 8) | ::size_t(begin[0]); } } inline ::size_t to_big_u16(const char* begin) { return to_uint16(begin, endian_big); } inline ::size_t to_little_u16(const char* begin) { return to_uint16(begin, endian_little); } inline uint32_t to_big_u32(const char* begin) { return (::size_t(begin[0]) << 24) | (::size_t(begin[1]) << 16) | (::size_t(begin[2]) << 8) | (::size_t(begin[3])); } inline std::string system(const std::string &command) { std::string result; FILE *p = popen(command.c_str(), "r"); if (p) { while (!feof(p)) { char buffer[1024] = { 0 }; char *line = fgets(buffer, sizeof(buffer), p); if (feof(p) || ferror(p)) { break; } if (line) { result += buffer; } } fclose(p); } else { char buf[256] = {0}; snprintf(buf, (::size_t)sizeof(buf), "open failed ,errno=%d", errno); return buf; } return result; } inline void reboot() { ::reboot(RB_AUTOBOOT); } class statfs { public: explicit statfs(const char* mount_point_path) { memset(&stat_, 0, sizeof(stat_)); if ((error_ = ::statfs(mount_point_path, &stat_)) != 0) { error_ = errno; } } uint64_t block_count() { return stat_.f_blocks; } uint64_t block_size() { return stat_.f_bsize; } uint64_t free_blocks() { return stat_.f_bfree; } uint64_t total_bytes() { return stat_.f_blocks * stat_.f_bsize; } uint64_t available_bytes() { return stat_.f_bavail * stat_.f_bsize; } // 0 ok, other error int error() { return error_; } private: struct ::statfs stat_; int error_; }; namespace path { //取路径前缀 inline std::string prefix(const std::string &path) { size_t pos = path.find_last_of("."); if (pos == std::string::npos) { return path; } return path.substr(0, pos); } //取文件后缀 inline std::string suffix(const std::string &path) { size_t pos = path.find_last_of("."); if (pos == std::string::npos) { return path; } if ((path.size() - 1) == pos) { return ""; } return path.substr(pos + 1); } inline std::string join(const std::string &a, const std::string &b) { if (a.empty()) { return b; } if (a[a.size() - 1] == '/') { return a + b; } return a + "/" + b; } inline std::string dir(const std::string &path) { ::size_t pos = path.find_last_of("/"); if (pos == 0) { return "/"; } if (pos != std::string::npos) { return path.substr(0, pos); } return ""; } inline std::string base(const std::string &path) { ::size_t pos = path.find_last_of("/"); if (pos != std::string::npos) { return path.substr(pos + 1); } return path; } inline std::vector ls(const std::string path, const std::string &sscanf_format, bool recursive) { // dir/*.cpp std::vector ret; DIR *d; struct dirent *entry; if ((d = opendir(path.c_str())) == NULL) { //can't open dir return ret; } while ((entry = readdir(d)) != NULL) { if ((strcmp(".", entry->d_name) == 0) || (strcmp("..", entry->d_name) == 0)) { continue; } if ((entry->d_type == DT_DIR) && recursive) { std::vector sub = ls(join(path, entry->d_name), sscanf_format, recursive); ret.insert(ret.end(), sub.begin(), sub.end()); } else { std::string str = join(path, entry->d_name); ret.push_back(str); } } closedir(d); return ret; } inline std::vector available_serial_port() { std::vector v; auto list = ls("/dev", "", false); for (auto i : list) { if (i.find("ttyS") != std::string::npos) { v.push_back(std::string("/dev/") + i); } } return v; } } // namespace path inline std::string _get_conf_str(const std::string& all, const std::string& key) { std::string::size_type start = all.find(key); if (start == std::string::npos) { return ""; } std::string::size_type end_line = all.find("\n", start + key.size()); if (end_line == std::string::npos) { return ""; } return all.substr(std::string::size_type(start + key.size()), std::string::size_type(end_line - start - key.size() - 1)); } inline bool get_wpa_ssid_psk(std::string* ssid, std::string* psk) { std::ifstream ifs("/data/misc/wifi/wpa_supplicant.conf", std::ios::in | std::ios::binary); std::stringstream ss; ss << ifs.rdbuf(); std::string content(ss.str()); ifs.close(); *ssid = _get_conf_str(content, "\tssid=\""); *psk = _get_conf_str(content, "\tpsk=\""); return ssid->size() > 0 || psk->size() > 0; } class screensaver_guard { public: screensaver_guard() { enable_ = EASYUICONTEXT->isScreensaverEnable(); EASYUICONTEXT->setScreensaverEnable(false); } ~screensaver_guard() { EASYUICONTEXT->setScreensaverEnable(enable_); } private: bool enable_; }; } // namespace fy #endif /* _FY_OS_H_ */