协能can协议
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /**
  2. * @content:基本类型定义
  3. * @time:2016-8-23
  4. * @author:Mr_zhu
  5. * @version: V1.0
  6. * @describe:
  7. * 1#2016-8-23#V1.0#首次生成
  8. */
  9. #ifndef TYPE_H
  10. #define TYPE_H
  11. #define LOG_TAG "can_dexn"
  12. #include <elog.h>
  13. #include <errno.h>
  14. #include <math.h>
  15. #include <sys/select.h>
  16. #include <sys/time.h>
  17. #include <cstdio>
  18. #include <ctime>
  19. #include <iostream>
  20. #include <iterator>
  21. #include <string>
  22. #include <vector>
  23. typedef long HRESULT;
  24. typedef unsigned long ULONG;
  25. typedef char BYTE;
  26. typedef char* PBYTE;
  27. class IUnknown;
  28. #ifndef NULL
  29. #define NULL 0
  30. #endif
  31. typedef struct _GUID
  32. {
  33. unsigned int Data1;
  34. unsigned short Data2;
  35. unsigned short Data3;
  36. unsigned char Data4[8];
  37. std::string toString()
  38. {
  39. char t[256];
  40. snprintf(t, 256, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", Data1, Data2, Data3, Data4[0], Data4[1], Data4[2], Data4[3], Data4[4], Data4[5],
  41. Data4[6], Data4[7]);
  42. return t;
  43. }
  44. } GUID;
  45. typedef GUID IID;
  46. typedef std::string ProgID;
  47. #define interface struct
  48. #define CLSID IID //----组件标识符
  49. #define OBJID IID //----对象标识符
  50. #define E_NOINTERFACE 0x80004002L
  51. #define S_OK 0
  52. #define S_FALSE 1
  53. #define SUCCEEDED(x) ((x)==S_OK)
  54. #define READ_O 0x00000001
  55. #define READ_WAIT 0x00000002
  56. #define READ_TIMEOUT 0x00000004
  57. #define WRITE_O 0x00000008
  58. #define WRITE_WAIT 0x00000010
  59. #define WRITE_TIMEOUT 0x00000020
  60. #define WRITE_FAIL 0x00000040
  61. #define HEX 16
  62. typedef IUnknown* (*CreateFuncPTR)();
  63. inline BYTE HByte(unsigned int x)
  64. {
  65. return (((x) >> 8) & 0x00ff);
  66. }
  67. inline BYTE LByte(unsigned int x)
  68. {
  69. return (((x)) & 0x00ff);
  70. }
  71. inline BYTE HHByte(unsigned int x)
  72. {
  73. return (((x) >> 24) & 0x000000ff);
  74. }
  75. inline BYTE HLByte(unsigned int x)
  76. {
  77. return (((x) >> 16) & 0x000000ff);
  78. }
  79. inline BYTE LHByte(unsigned int x)
  80. {
  81. return (((x) >> 8) & 0x000000ff);
  82. }
  83. inline BYTE LLByte(unsigned int x)
  84. {
  85. return (((x)) & 0x000000ff);
  86. }
  87. inline BYTE HHByte(long int x)
  88. {
  89. return (((x) >> 24) & 0x000000ff);
  90. }
  91. inline BYTE HLByte(long int x)
  92. {
  93. return (((x) >> 16) & 0x000000ff);
  94. }
  95. inline BYTE LHByte(long int x)
  96. {
  97. return (((x) >> 8) & 0x000000ff);
  98. }
  99. inline BYTE LLByte(long int x)
  100. {
  101. return (((x)) & 0x000000ff);
  102. }
  103. inline BYTE HHByte(int x)
  104. {
  105. return (((x) >> 24) & 0x000000ff);
  106. }
  107. inline BYTE HLByte(int x)
  108. {
  109. return (((x) >> 16) & 0x000000ff);
  110. }
  111. inline BYTE LHByte(int x)
  112. {
  113. return (((x) >> 8) & 0x000000ff);
  114. }
  115. inline BYTE LLByte(int x)
  116. {
  117. return (((x)) & 0x000000ff);
  118. }
  119. /**
  120. * @content:输出调试信息至stdout
  121. * @time:2016-8-26
  122. * @author:Mr_zhu
  123. * @param: const ProgID(组件中文标识符),const char*(待输出信息)
  124. * @return: void
  125. * @decribe
  126. * 1#2016-8-26#V1.0#首次生成
  127. */
  128. inline void trace(const ProgID pid, const char* msg)
  129. {
  130. struct timeval tv;
  131. struct timezone tz;
  132. struct tm *p;
  133. gettimeofday(&tv, &tz);
  134. // printf("tv_sec:%ld\n", tv.tv_sec);
  135. // printf("tv_usec:%ld\n", tv.tv_usec);
  136. // printf("tz_minuteswest:%d\n", tz.tz_minuteswest);
  137. // printf("tz_dsttime:%d\n", tz.tz_dsttime);
  138. p = localtime(&tv.tv_sec);
  139. char t[255];
  140. snprintf(t, 255, "time_now:%04d-%02d-%02d %02d-%02d-%02d,%ld", 1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec);
  141. // std::cout << __FILE__ << "," << __LINE__ << "[" << t << "], "<< pid << ":" << msg
  142. // << std::endl;
  143. std::cout << "[" << t << "], " << pid << ":" << msg << std::endl;
  144. }
  145. /**
  146. * @content:将String转为Hex
  147. * @time:2016-10-1
  148. * @author:Mr_zhu
  149. * @param: std::string(待转字符串)
  150. * @return: unsigned int(返回值)
  151. * @decribe
  152. * 1#2016-10-1#V1.0#首次生成
  153. */
  154. inline unsigned int hexstringtoInt(std::string str)
  155. {
  156. unsigned int result = 0;
  157. for (unsigned int i = 0; i < str.length(); i++)
  158. {
  159. if ((str.at(i) >= '0') && (str.at(i) <= '9'))
  160. {
  161. result += ((str.at(i) - 48) * pow(16, str.length() - 1 - i));
  162. }
  163. else if ((str.at(i) >= 'a') && (str.at(i) <= 'f'))
  164. {
  165. result += ((str.at(i) - 97 + 10) * pow(16, str.length() - 1 - i));
  166. }
  167. }
  168. return result;
  169. }
  170. /**
  171. * @content:将String转为Hex
  172. * @time:2016-10-1
  173. * @author:Mr_zhu
  174. * @param: std::string(待转字符串)
  175. * @return: unsigned short(返回值)
  176. * @decribe
  177. * 1#2016-10-1#V1.0#首次生成
  178. */
  179. inline unsigned short hexstringtoShort(std::string str)
  180. {
  181. unsigned short result = 0;
  182. for (unsigned int i = 0; i < str.length(); i++)
  183. {
  184. if ((str.at(i) >= '0') && (str.at(i) <= '9'))
  185. {
  186. result += (str.at(i) - 48) * pow(16, str.length() - 1 - i);
  187. }
  188. if ((str.at(i) >= 'a') && (str.at(i) <= 'f'))
  189. {
  190. result += (str.at(i) - 97 + 10) * pow(16, str.length() - 1 - i);
  191. }
  192. }
  193. return result;
  194. }
  195. /**
  196. * @content:将String转为Hex
  197. * @time:2016-10-1
  198. * @author:Mr_zhu
  199. * @param: std::string(待转字符串)
  200. * @return: unsigned char(返回值)
  201. * @decribe
  202. * 1#2016-10-1#V1.0#首次生成
  203. */
  204. inline unsigned char hexstringtoChar(std::string str)
  205. {
  206. unsigned char result = 0;
  207. for (unsigned int i = 0; i < str.length(); i++)
  208. {
  209. if ((str.at(i) >= '0') && (str.at(i) <= '9'))
  210. {
  211. result += (str.at(i) - 48) * pow(16, str.length() - 1 - i);
  212. }
  213. if ((str.at(i) >= 'a') && (str.at(i) <= 'f'))
  214. {
  215. result += (str.at(i) - 97 + 10) * pow(16, str.length() - 1 - i);
  216. }
  217. }
  218. return result;
  219. }
  220. /**
  221. * @content:将String转为GUID
  222. * @time:2016-10-1
  223. * @author:Mr_zhu
  224. * @param: std::string(待转字符串)
  225. * @return: GUID(返回的GUID)
  226. * @decribe
  227. * 1#2016-10-1#V1.0#首次生成
  228. */
  229. inline GUID stringtoGUID(std::string str)
  230. {
  231. GUID t_guid =
  232. { 0, 0, 0,
  233. { 0, 0, 0, 0, 0, 0, 0, 0 } };
  234. if (!str.empty())
  235. {
  236. std::string t_str = str;
  237. std::vector<std::string> t_vstr;
  238. // int index = t_str.find('{');
  239. // int index1 = t_str.find('}');
  240. int index = 0;
  241. int index1 = 0;
  242. if ((index != -1) && (index1 != -1))
  243. {
  244. t_str = t_str.substr(0);
  245. int index = t_str.find('-');
  246. while (index != -1)
  247. {
  248. t_vstr.push_back(t_str.substr(0, index));
  249. t_str = t_str.substr(index + 1);
  250. index = t_str.find('-');
  251. if (index == -1)
  252. {
  253. break;
  254. }
  255. }
  256. t_vstr.push_back(t_str);
  257. }
  258. if (t_vstr.size() == 5)
  259. {
  260. t_guid.Data1 = hexstringtoInt(t_vstr.at(0));
  261. t_guid.Data2 = hexstringtoShort(t_vstr.at(1));
  262. t_guid.Data3 = hexstringtoShort(t_vstr.at(2));
  263. t_guid.Data4[0] = hexstringtoChar(t_vstr.at(3).substr(0, 2));
  264. t_guid.Data4[1] = hexstringtoChar(t_vstr.at(3).substr(2));
  265. std::string sstr = t_vstr.at(4);
  266. t_guid.Data4[2] = hexstringtoChar(sstr.substr(0, 2));
  267. t_guid.Data4[3] = hexstringtoChar(sstr.substr(2, 2));
  268. t_guid.Data4[4] = hexstringtoChar(sstr.substr(4, 2));
  269. t_guid.Data4[5] = hexstringtoChar(sstr.substr(6, 2));
  270. t_guid.Data4[6] = hexstringtoChar(sstr.substr(8, 2));
  271. t_guid.Data4[7] = hexstringtoChar(sstr.substr(10, 2));
  272. }
  273. }
  274. return t_guid;
  275. }
  276. /**
  277. * @content: 比较两个标识符是否相等
  278. * @time:2016-8-26
  279. * @author:Mr_zhu
  280. * @param: const IID&(待比较标识符1),const IID2&(待比较标识符2)
  281. * @return: void
  282. * @decribe
  283. * 1#2016-8-26#V1.0#首次生成
  284. */
  285. inline bool operator==(const IID& guid1, const IID& guid2)
  286. {
  287. if (guid1.Data1 != guid2.Data1)
  288. return false;
  289. if (guid1.Data2 != guid2.Data2)
  290. return false;
  291. if (guid1.Data3 != guid2.Data3)
  292. return false;
  293. for (int i = 0; i < 8; i++)
  294. {
  295. if (guid1.Data4[i] != guid2.Data4[i])
  296. return false;
  297. }
  298. return true;
  299. }
  300. /**
  301. * @content: 比较标识符1是否小于标识符2
  302. * @time:2016-8-26
  303. * @author:Mr_zhu
  304. * @param: const IID&(待比较标识符1),const IID2&(待比较标识符2)
  305. * @return: void
  306. * @decribe
  307. * 1#2016-8-26#V1.0#首次生成
  308. */
  309. inline bool operator<(const IID& guid1, const IID& guid2)
  310. {
  311. if (guid1.Data1 >= guid2.Data1)
  312. return false;
  313. if (guid1.Data2 >= guid2.Data2)
  314. return false;
  315. if (guid1.Data3 >= guid2.Data3)
  316. return false;
  317. for (int i = 0; i < 8; i++)
  318. {
  319. if (guid1.Data4[i] >= guid2.Data4[i])
  320. return false;
  321. }
  322. return true;
  323. }
  324. inline bool isGUIDEqual(const IID& guid1, const IID& guid2)
  325. {
  326. return ((guid1.Data1 == guid2.Data1) && (guid1.Data2 == guid2.Data2) && (guid1.Data3 == guid2.Data3) && (guid1.Data4[0] == guid2.Data4[0])
  327. && (guid1.Data4[1] == guid2.Data4[1]) && (guid1.Data4[2] == guid2.Data4[2]) && (guid1.Data4[3] == guid2.Data4[3])
  328. && (guid1.Data4[4] == guid2.Data4[4]) && (guid1.Data4[5] == guid2.Data4[5]) && (guid1.Data4[6] == guid2.Data4[6])
  329. && (guid1.Data4[7] == guid2.Data4[7]));
  330. }
  331. inline void secondsSleep(unsigned int seconds)
  332. {
  333. struct timeval tv;
  334. tv.tv_sec = seconds;
  335. tv.tv_usec = 0;
  336. int err;
  337. do
  338. {
  339. err = select(0, NULL, NULL, NULL, &tv);
  340. } while (err < 0 && errno == EINTR);
  341. }
  342. inline void milisecondsSleep(unsigned int mSeconds)
  343. {
  344. struct timeval tv;
  345. tv.tv_sec = mSeconds / 1000;
  346. tv.tv_usec = (mSeconds % 1000) * 1000;
  347. int err;
  348. do
  349. {
  350. err = select(0, NULL, NULL, NULL, &tv);
  351. } while (err < 0 && errno == EINTR);
  352. }
  353. inline void microssecondsSleep(unsigned int uSeconds)
  354. {
  355. struct timeval tv;
  356. tv.tv_sec = uSeconds / 1000000;
  357. tv.tv_usec = uSeconds % 1000000;
  358. int err;
  359. do
  360. {
  361. err = select(0, NULL, NULL, NULL, &tv);
  362. } while (err < 0 && errno == EINTR);
  363. }
  364. /**
  365. * DEBUG等级
  366. */
  367. typedef enum
  368. {
  369. DEBUG = 0, INFO, WARN, ERROR, FATAL
  370. } LEVEL;
  371. #endif