Utility.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include "Utility.h"
  2. #include <sys/fcntl.h>
  3. #include <string.h>
  4. #include <iostream>
  5. #include <string>
  6. #include <sstream>
  7. #include <unistd.h>
  8. #include <cstdlib>
  9. #include <fcntl.h>
  10. #include <stdio.h>
  11. #include <time.h>
  12. #include <QDateTime>
  13. #include <sys/utsname.h>
  14. #include <time.h>
  15. #include <QStringList>
  16. #include <sys/ioctl.h>
  17. #include <QDebug>
  18. using std::string;
  19. using std::stringstream;
  20. using std::cout;
  21. using std::endl;
  22. #define CARBACK_IOCTL_BASE 0x9A
  23. #define CARBACK_IOCTL_SET_APP_READY _IO(CARBACK_IOCTL_BASE, 0)
  24. #define CARBACK_IOCTL_APP_ENTER_DONE _IO(CARBACK_IOCTL_BASE, 1)
  25. #define CARBACK_IOCTL_APP_EXIT_DONE _IO(CARBACK_IOCTL_BASE, 2)
  26. #define CARBACK_IOCTL_GET_STATUS _IOR(CARBACK_IOCTL_BASE, 3, int)
  27. static string appFilePathSuffix()
  28. {
  29. static const unsigned short s_MaxCount = 1024;
  30. char buf[s_MaxCount] = {'\0'};
  31. int ret = readlink("/proc/self/exe", buf, s_MaxCount);
  32. string suffix("");
  33. if ((ret < 0) || (ret > s_MaxCount)) {
  34. suffix = string("");
  35. } else {
  36. string tmp(buf);
  37. int pos = tmp.find_last_of(char('/'));
  38. if (-1 == pos) {
  39. suffix = string("");
  40. }
  41. suffix = tmp.substr(pos + 1, tmp.length() - 1);
  42. }
  43. return suffix;
  44. }
  45. bool singleApplication(const bool acquire, const std::string &key, const bool block)
  46. {
  47. string app;
  48. if (0 == key.size()) {
  49. app = appFilePathSuffix();
  50. } else {
  51. app = key;
  52. }
  53. bool ret = true;
  54. string error;
  55. if (acquire) {
  56. error = "acquire";
  57. } else {
  58. error = "release";
  59. }
  60. if (app.empty()) {
  61. error = string("app is Empty!");
  62. ret = false;
  63. } else {
  64. string tmp = app;
  65. // stringstream pidStr;
  66. // pidStr << getpid();
  67. // string pid;
  68. // pidStr >> pid;
  69. tmp = string("/tmp/") + tmp;
  70. int fd = open(tmp.c_str(), O_RDWR | O_CREAT, 0666);
  71. if (fd < 0) {
  72. error = string("open ") + tmp + string(" error!");
  73. ret = false;
  74. } else {
  75. struct flock lock;
  76. if (!acquire) {
  77. lock.l_type = F_RDLCK;
  78. }
  79. lock.l_whence = SEEK_SET;
  80. lock.l_start = 0;
  81. lock.l_len = 0;
  82. if ((!acquire)
  83. && (0 == fcntl(fd, F_GETLK, &lock))) {
  84. if (F_WRLCK == lock.l_type) {
  85. ret = false;
  86. }
  87. } else {
  88. if (acquire) {
  89. lock.l_type = F_WRLCK;
  90. } else {
  91. lock.l_type = F_UNLCK;
  92. }
  93. lock.l_pid = getpid();
  94. int cmd = F_SETLKW;
  95. if (!block) {
  96. cmd = F_SETLK;
  97. }
  98. if(fcntl(fd, cmd, &lock) < 0) {
  99. close(fd);
  100. fd = -1;
  101. error = string("application already acquire!");
  102. ret = false;
  103. }
  104. }
  105. }
  106. }
  107. if (ret) {
  108. error += " success!";
  109. } else {
  110. error += " failed!";
  111. }
  112. cout << " singleApplication: " << app << " " << error << endl;
  113. return ret;
  114. }
  115. bool acquireApplication(const std::string &key, const bool block)
  116. {
  117. return singleApplication(true, key, block);
  118. }
  119. bool releaseApplication(const std::string &key)
  120. {
  121. return singleApplication(false, key, false);
  122. }
  123. static HANDLER m_Callback(NULL);
  124. static void* m_Parameter(NULL);
  125. HANDLER acquirePreemptiveResource(HANDLER callback, void *parameter)
  126. {
  127. HANDLER lastHandler = m_Callback;
  128. if ((NULL != m_Callback) && (callback != m_Callback)) {
  129. m_Callback(m_Parameter);
  130. }
  131. m_Callback = callback;
  132. m_Parameter = parameter;
  133. return lastHandler;
  134. }
  135. void clearOwner()
  136. {
  137. m_Callback = NULL;
  138. m_Parameter = NULL;
  139. }
  140. bool initializeArkVideoResources()
  141. {
  142. static bool flag(false);
  143. if (!flag) {
  144. int ret = 0;
  145. #ifndef gcc
  146. ret = system("/etc/memalloc_load.sh");
  147. ret = (0 == ret) && system("/etc/driver_load.sh");
  148. ret = (0 == ret) && system("/etc/itu656_load.sh");
  149. #endif
  150. flag = (0 == ret);
  151. }
  152. return flag;
  153. }
  154. #include <fcntl.h>
  155. bool hideArkStartupLogo()
  156. {
  157. static bool flag(false);
  158. if (!flag) {
  159. int ret = 0;
  160. #ifndef gcc
  161. int fd = open("/proc/display", O_WRONLY);
  162. if (-1 != fd) {
  163. const char str[] = "23";
  164. ret = write(fd, str, sizeof(str));
  165. close(fd);
  166. fd = -1;
  167. }
  168. // ret = system("echo 23 > /proc/display");
  169. #endif
  170. flag = (3 == ret);
  171. }
  172. return flag;
  173. }
  174. QString compilerDate()
  175. {
  176. char month[4] = {0};
  177. int MM, dd, yyyy;
  178. struct tm t;
  179. memset(&t, 0, sizeof(t));
  180. static const char monthNames[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
  181. sscanf(__DATE__, "%s %d %d", month, &dd, &yyyy);
  182. MM = (strstr(monthNames, month) - monthNames) / 3;
  183. t.tm_year = yyyy - 1900;
  184. t.tm_mday = dd;
  185. t.tm_mon = MM;
  186. return QDateTime::fromTime_t((uint)mktime(&t)).toString("yyyyMMdd");
  187. }
  188. QString osVersion()
  189. {
  190. QString version/*("#210 Mon Jun 12 11:40:05 CST 2017")*/;
  191. struct utsname kernelInfo;
  192. if (uname(&kernelInfo) != -1) {
  193. version = QString(kernelInfo.version);
  194. }
  195. QString year, month, day("%1"),time;
  196. QStringList stringList = version.split(QChar(' '));
  197. for (int i = 0; i < stringList.size(); ++i) {
  198. if (3 == i) {
  199. month = stringList.at(i);
  200. if (month.contains("Jan")) {
  201. month = QString("01");
  202. } else if (month.contains("Feb")) {
  203. month = QString("02");
  204. } else if (month.contains("Mar")) {
  205. month = QString("03");
  206. } else if (month.contains("Apr")) {
  207. month = QString("04");
  208. } else if (month.contains("May")) {
  209. month = QString("05");
  210. } else if (month.contains("Jun")) {
  211. month = QString("06");
  212. } else if (month.contains("Jul")) {
  213. month = QString("07");
  214. } else if (month.contains("Aug")) {
  215. month = QString("08");
  216. } else if (month.contains("Sep")) {
  217. month = QString("09");
  218. } else if (month.contains("Oct")) {
  219. month = QString("10");
  220. } else if (month.contains("Nov")) {
  221. month = QString("11");
  222. } else /*if (month.contains("Dec"))*/ {
  223. month = QString("12");
  224. }
  225. }else if (4 == i) {
  226. day = day.arg(stringList.at(i).toInt(), 2, 10, QChar('0'));
  227. } else if (5 == i) {
  228. time = stringList.at(i);
  229. QStringList timeList = time.split(":");
  230. if(timeList.size() >= 3)
  231. {
  232. time = QString("-")+timeList.at(0)+QString(":")+timeList.at(1);
  233. }
  234. } else if (i == stringList.size() - 1) {
  235. year = stringList.at(i);
  236. }
  237. }
  238. return year + month + day + time;
  239. }
  240. bool setDateTime(const int year, const int month, const int day, const int hour, const int minute, const int second)
  241. {
  242. struct tm tm;
  243. tm.tm_year = year - 1900;
  244. tm.tm_mon = month;
  245. tm.tm_mday = day;
  246. tm.tm_hour = hour;
  247. tm.tm_min = minute;
  248. tm.tm_sec = second;
  249. tm.tm_isdst = -1;
  250. time_t time = mktime(&tm);
  251. // 填充timespec
  252. struct timespec ts;
  253. ts.tv_sec = time;
  254. ts.tv_nsec = 0;
  255. bool flag(false);
  256. if (0 == clock_settime(CLOCK_REALTIME, &ts)) {
  257. if (0 == system("hwclock -w")) {
  258. flag = true;
  259. }
  260. }
  261. return flag;
  262. }
  263. bool memallocload()
  264. {
  265. static bool load = false;
  266. if (!load) {
  267. if (0 != system("/etc/memalloc_load.sh")) {
  268. printf("execute hw decoder memory /etc/memalloc_load.sh failed!");
  269. } else {
  270. load = true;
  271. }
  272. }
  273. return load;
  274. }
  275. bool driverload()
  276. {
  277. static bool load = false;
  278. if (!load) {
  279. if (0 != system("/etc/driver_load.sh")) {
  280. printf("execute hw decoder /etc/driver_load.sh failed!");
  281. } else {
  282. load = true;
  283. }
  284. }
  285. return load;
  286. }
  287. bool capacitiveScreen()
  288. {
  289. bool flag = true;
  290. if (NULL == getenv("QWS_ARK_MT_DEVICE")) {
  291. flag = false;
  292. }
  293. return flag;
  294. }