os.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #ifndef _FY_OS_H_
  2. #define _FY_OS_H_
  3. #include <dirent.h>
  4. #include <sys/stat.h>
  5. #include <sys/vfs.h>
  6. #include <stdio.h>
  7. #include <sys/reboot.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <string>
  11. #include <vector>
  12. #include <fstream>
  13. #include <sstream>
  14. #include <stdint.h>
  15. #include "strings.hpp"
  16. #include "entry/EasyUIContext.h"
  17. namespace base {
  18. enum endians {
  19. endian_big,
  20. endian_little,
  21. };
  22. /**
  23. * 小端返回true,大端false
  24. */
  25. inline bool is_little_endian() {
  26. union {
  27. int a;
  28. char b;
  29. } c;
  30. c.a = 1;
  31. return (c.b == 1);
  32. }
  33. template<typename T>
  34. inline void swap(T* a, T* b) {
  35. *a = (*a) ^ (*b);
  36. *b = (*a) ^ (*b);
  37. *a = (*a) ^ (*b);
  38. }
  39. template<typename T>
  40. inline void reverse_endian(T* v) {
  41. typedef unsigned char byte;
  42. byte* b = (byte*) v;
  43. for (int i = 0; i < (int)(sizeof(T)) / 2; ++i) {
  44. swap(&(b[i]), &(b[sizeof(T) - 1 - i]));
  45. }
  46. }
  47. template<typename T>
  48. inline T from_endian(T val, bool le) {
  49. static bool e = is_little_endian();
  50. if (e != le) {
  51. reverse_endian(&val);
  52. return val;
  53. }
  54. return val;
  55. }
  56. inline ::size_t to_uint16(const char* begin, endians endian) {
  57. if (endian_big == endian) {
  58. return (::size_t(begin[0]) << 8) | ::size_t(begin[1]);
  59. } else {
  60. return (::size_t(begin[1]) << 8) | ::size_t(begin[0]);
  61. }
  62. }
  63. inline ::size_t to_big_u16(const char* begin) {
  64. return to_uint16(begin, endian_big);
  65. }
  66. inline ::size_t to_little_u16(const char* begin) {
  67. return to_uint16(begin, endian_little);
  68. }
  69. inline uint32_t to_big_u32(const char* begin) {
  70. return (::size_t(begin[0]) << 24)
  71. | (::size_t(begin[1]) << 16)
  72. | (::size_t(begin[2]) << 8)
  73. | (::size_t(begin[3]));
  74. }
  75. inline std::string system(const std::string &command) {
  76. std::string result;
  77. FILE *p = popen(command.c_str(), "r");
  78. if (p) {
  79. while (!feof(p)) {
  80. char buffer[1024] = { 0 };
  81. char *line = fgets(buffer, sizeof(buffer), p);
  82. if (feof(p) || ferror(p)) {
  83. break;
  84. }
  85. if (line) {
  86. result += buffer;
  87. }
  88. }
  89. fclose(p);
  90. } else {
  91. char buf[256] = {0};
  92. snprintf(buf, (::size_t)sizeof(buf), "open failed ,errno=%d", errno);
  93. return buf;
  94. }
  95. return result;
  96. }
  97. inline void reboot() {
  98. ::reboot(RB_AUTOBOOT);
  99. }
  100. class statfs {
  101. public:
  102. explicit statfs(const char* mount_point_path) {
  103. memset(&stat_, 0, sizeof(stat_));
  104. if ((error_ = ::statfs(mount_point_path, &stat_)) != 0) {
  105. error_ = errno;
  106. }
  107. }
  108. uint64_t block_count() {
  109. return stat_.f_blocks;
  110. }
  111. uint64_t block_size() {
  112. return stat_.f_bsize;
  113. }
  114. uint64_t free_blocks() {
  115. return stat_.f_bfree;
  116. }
  117. uint64_t total_bytes() {
  118. return stat_.f_blocks * stat_.f_bsize;
  119. }
  120. uint64_t available_bytes() {
  121. return stat_.f_bavail * stat_.f_bsize;
  122. }
  123. // 0 ok, other error
  124. int error() {
  125. return error_;
  126. }
  127. private:
  128. struct ::statfs stat_;
  129. int error_;
  130. };
  131. namespace path {
  132. //取路径前缀
  133. inline std::string prefix(const std::string &path) {
  134. size_t pos = path.find_last_of(".");
  135. if (pos == std::string::npos) {
  136. return path;
  137. }
  138. return path.substr(0, pos);
  139. }
  140. //取文件后缀
  141. inline std::string suffix(const std::string &path) {
  142. size_t pos = path.find_last_of(".");
  143. if (pos == std::string::npos) {
  144. return path;
  145. }
  146. if ((path.size() - 1) == pos) {
  147. return "";
  148. }
  149. return path.substr(pos + 1);
  150. }
  151. inline std::string join(const std::string &a, const std::string &b) {
  152. if (a.empty()) {
  153. return b;
  154. }
  155. if (a[a.size() - 1] == '/') {
  156. return a + b;
  157. }
  158. return a + "/" + b;
  159. }
  160. inline std::string dir(const std::string &path) {
  161. ::size_t pos = path.find_last_of("/");
  162. if (pos == 0) {
  163. return "/";
  164. }
  165. if (pos != std::string::npos) {
  166. return path.substr(0, pos);
  167. }
  168. return "";
  169. }
  170. inline std::string base(const std::string &path) {
  171. ::size_t pos = path.find_last_of("/");
  172. if (pos != std::string::npos) {
  173. return path.substr(pos + 1);
  174. }
  175. return path;
  176. }
  177. inline std::vector<std::string> ls(const std::string path, const std::string &sscanf_format, bool recursive) {
  178. // dir/*.cpp
  179. std::vector<std::string> ret;
  180. DIR *d;
  181. struct dirent *entry;
  182. if ((d = opendir(path.c_str())) == NULL) {
  183. //can't open dir
  184. return ret;
  185. }
  186. while ((entry = readdir(d)) != NULL) {
  187. if ((strcmp(".", entry->d_name) == 0) || (strcmp("..", entry->d_name) == 0)) {
  188. continue;
  189. }
  190. if ((entry->d_type == DT_DIR) && recursive) {
  191. std::vector<std::string> sub = ls(join(path, entry->d_name), sscanf_format, recursive);
  192. ret.insert(ret.end(), sub.begin(), sub.end());
  193. } else {
  194. std::string str = join(path, entry->d_name);
  195. ret.push_back(str);
  196. }
  197. }
  198. closedir(d);
  199. return ret;
  200. }
  201. inline std::vector<std::string> available_serial_port() {
  202. std::vector<std::string> v;
  203. auto list = ls("/dev", "", false);
  204. for (auto i : list) {
  205. if (i.find("ttyS") != std::string::npos) {
  206. v.push_back(std::string("/dev/") + i);
  207. }
  208. }
  209. return v;
  210. }
  211. } // namespace path
  212. inline std::string _get_conf_str(const std::string& all,
  213. const std::string& key) {
  214. std::string::size_type start = all.find(key);
  215. if (start == std::string::npos) {
  216. return "";
  217. }
  218. std::string::size_type end_line = all.find("\n", start + key.size());
  219. if (end_line == std::string::npos) {
  220. return "";
  221. }
  222. return all.substr(std::string::size_type(start + key.size()),
  223. std::string::size_type(end_line - start - key.size() - 1));
  224. }
  225. inline bool get_wpa_ssid_psk(std::string* ssid, std::string* psk) {
  226. std::ifstream ifs("/data/misc/wifi/wpa_supplicant.conf",
  227. std::ios::in | std::ios::binary);
  228. std::stringstream ss;
  229. ss << ifs.rdbuf();
  230. std::string content(ss.str());
  231. ifs.close();
  232. *ssid = _get_conf_str(content, "\tssid=\"");
  233. *psk = _get_conf_str(content, "\tpsk=\"");
  234. return ssid->size() > 0 || psk->size() > 0;
  235. }
  236. class screensaver_guard {
  237. public:
  238. screensaver_guard() {
  239. enable_ = EASYUICONTEXT->isScreensaverEnable();
  240. EASYUICONTEXT->setScreensaverEnable(false);
  241. }
  242. ~screensaver_guard() {
  243. EASYUICONTEXT->setScreensaverEnable(enable_);
  244. }
  245. private:
  246. bool enable_;
  247. };
  248. } // namespace fy
  249. #endif /* _FY_OS_H_ */