1
0

Utility.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 = {0};
  179. static const char monthNames[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
  180. sscanf(__DATE__, "%s %d %d", month, &dd, &yyyy);
  181. MM = (strstr(monthNames, month) - monthNames) / 3;
  182. t.tm_year = yyyy - 1900;
  183. t.tm_mday = dd;
  184. t.tm_mon = MM;
  185. return QDateTime::fromTime_t((uint)mktime(&t)).toString("yyyyMMdd");
  186. }
  187. QString osVersion()
  188. {
  189. QString version/*("#210 Mon Jun 12 11:40:05 CST 2017")*/;
  190. struct utsname kernelInfo;
  191. if (uname(&kernelInfo) != -1) {
  192. version = QString(kernelInfo.version);
  193. }
  194. QString year, month, day("%1"),time;
  195. QStringList stringList = version.split(QChar(' '));
  196. for (int i = 0; i < stringList.size(); ++i) {
  197. if (3 == i) {
  198. month = stringList.at(i);
  199. if (month.contains("Jan")) {
  200. month = QString("01");
  201. } else if (month.contains("Feb")) {
  202. month = QString("02");
  203. } else if (month.contains("Mar")) {
  204. month = QString("03");
  205. } else if (month.contains("Apr")) {
  206. month = QString("04");
  207. } else if (month.contains("May")) {
  208. month = QString("05");
  209. } else if (month.contains("Jun")) {
  210. month = QString("06");
  211. } else if (month.contains("Jul")) {
  212. month = QString("07");
  213. } else if (month.contains("Aug")) {
  214. month = QString("08");
  215. } else if (month.contains("Sep")) {
  216. month = QString("09");
  217. } else if (month.contains("Oct")) {
  218. month = QString("10");
  219. } else if (month.contains("Nov")) {
  220. month = QString("11");
  221. } else /*if (month.contains("Dec"))*/ {
  222. month = QString("12");
  223. }
  224. }else if (4 == i) {
  225. day = day.arg(stringList.at(i).toInt(), 2, 10, QChar('0'));
  226. } else if (5 == i) {
  227. time = stringList.at(i);
  228. QStringList timeList = time.split(":");
  229. if(timeList.size() >= 3)
  230. {
  231. time = QString("-")+timeList.at(0)+QString(":")+timeList.at(1);
  232. }
  233. } else if (i == stringList.size() - 1) {
  234. year = stringList.at(i);
  235. }
  236. }
  237. return year + month + day + time;
  238. }
  239. bool setDateTime(const int year, const int month, const int day, const int hour, const int minute, const int second)
  240. {
  241. struct tm tm;
  242. tm.tm_year = year - 1900;
  243. tm.tm_mon = month;
  244. tm.tm_mday = day;
  245. tm.tm_hour = hour;
  246. tm.tm_min = minute;
  247. tm.tm_sec = second;
  248. tm.tm_isdst = -1;
  249. time_t time = mktime(&tm);
  250. bool flag(false);
  251. if (0 == stime(&time)) {
  252. if (0 == system("hwclock -w")) {
  253. flag = true;
  254. }
  255. }
  256. return flag;
  257. }
  258. bool memallocload()
  259. {
  260. static bool load = false;
  261. if (!load) {
  262. if (0 != system("/etc/memalloc_load.sh")) {
  263. printf("execute hw decoder memory /etc/memalloc_load.sh failed!");
  264. } else {
  265. load = true;
  266. }
  267. }
  268. return load;
  269. }
  270. bool driverload()
  271. {
  272. static bool load = false;
  273. if (!load) {
  274. if (0 != system("/etc/driver_load.sh")) {
  275. printf("execute hw decoder /etc/driver_load.sh failed!");
  276. } else {
  277. load = true;
  278. }
  279. }
  280. return load;
  281. }
  282. bool capacitiveScreen()
  283. {
  284. bool flag = true;
  285. if (NULL == getenv("QWS_ARK_MT_DEVICE")) {
  286. flag = false;
  287. }
  288. return flag;
  289. }