123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- #ifndef _FY_OS_H_
- #define _FY_OS_H_
- #include <dirent.h>
- #include <sys/stat.h>
- #include <sys/vfs.h>
- #include <stdio.h>
- #include <sys/reboot.h>
- #include <errno.h>
- #include <string.h>
- #include <string>
- #include <vector>
- #include <fstream>
- #include <sstream>
- #include <stdint.h>
- #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<typename T>
- inline void swap(T* a, T* b) {
- *a = (*a) ^ (*b);
- *b = (*a) ^ (*b);
- *a = (*a) ^ (*b);
- }
- template<typename T>
- 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<typename T>
- 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<std::string> ls(const std::string path, const std::string &sscanf_format, bool recursive) {
- // dir/*.cpp
- std::vector<std::string> 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<std::string> 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<std::string> available_serial_port() {
- std::vector<std::string> 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_ */
|