utils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /**************************************************************************
  2. * File Name : utils.c
  3. * Author :
  4. * Version : 1.0
  5. * Date :
  6. * Description :
  7. *
  8. * Copyright (c) 2014 Winner Microelectronics Co., Ltd.
  9. * All rights reserved.
  10. *
  11. ***************************************************************************/
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdarg.h>
  16. #include "wm_mem.h"
  17. #include "tls_common.h"
  18. #include "wm_debug.h"
  19. #include "wm_sockets.h"
  20. #include "utils.h"
  21. static const u8 crc8_tbl[256] = {
  22. 0x00,0x91,0xe3,0x72,0x07,0x96,0xe4,0x75,
  23. 0x0e,0x9f,0xed,0x7c,0x09,0x98,0xea,0x7b,
  24. 0x1c,0x8d,0xff,0x6e,0x1b,0x8a,0xf8,0x69,
  25. 0x12,0x83,0xf1,0x60,0x15,0x84,0xf6,0x67,
  26. 0x38,0xa9,0xdb,0x4a,0x3f,0xae,0xdc,0x4d,
  27. 0x36,0xa7,0xd5,0x44,0x31,0xa0,0xd2,0x43,
  28. 0x24,0xb5,0xc7,0x56,0x23,0xb2,0xc0,0x51,
  29. 0x2a,0xbb,0xc9,0x58,0x2d,0xbc,0xce,0x5f,
  30. 0x70,0xe1,0x93,0x02,0x77,0xe6,0x94,0x05,
  31. 0x7e,0xef,0x9d,0x0c,0x79,0xe8,0x9a,0x0b,
  32. 0x6c,0xfd,0x8f,0x1e,0x6b,0xfa,0x88,0x19,
  33. 0x62,0xf3,0x81,0x10,0x65,0xf4,0x86,0x17,
  34. 0x48,0xd9,0xab,0x3a,0x4f,0xde,0xac,0x3d,
  35. 0x46,0xd7,0xa5,0x34,0x41,0xd0,0xa2,0x33,
  36. 0x54,0xc5,0xb7,0x26,0x53,0xc2,0xb0,0x21,
  37. 0x5a,0xcb,0xb9,0x28,0x5d,0xcc,0xbe,0x2f,
  38. 0xe0,0x71,0x03,0x92,0xe7,0x76,0x04,0x95,
  39. 0xee,0x7f,0x0d,0x9c,0xe9,0x78,0x0a,0x9b,
  40. 0xfc,0x6d,0x1f,0x8e,0xfb,0x6a,0x18,0x89,
  41. 0xf2,0x63,0x11,0x80,0xf5,0x64,0x16,0x87,
  42. 0xd8,0x49,0x3b,0xaa,0xdf,0x4e,0x3c,0xad,
  43. 0xd6,0x47,0x35,0xa4,0xd1,0x40,0x32,0xa3,
  44. 0xc4,0x55,0x27,0xb6,0xc3,0x52,0x20,0xb1,
  45. 0xca,0x5b,0x29,0xb8,0xcd,0x5c,0x2e,0xbf,
  46. 0x90,0x01,0x73,0xe2,0x97,0x06,0x74,0xe5,
  47. 0x9e,0x0f,0x7d,0xec,0x99,0x08,0x7a,0xeb,
  48. 0x8c,0x1d,0x6f,0xfe,0x8b,0x1a,0x68,0xf9,
  49. 0x82,0x13,0x61,0xf0,0x85,0x14,0x66,0xf7,
  50. 0xa8,0x39,0x4b,0xda,0xaf,0x3e,0x4c,0xdd,
  51. 0xa6,0x37,0x45,0xd4,0xa1,0x30,0x42,0xd3,
  52. 0xb4,0x25,0x57,0xc6,0xb3,0x22,0x50,0xc1,
  53. 0xba,0x2b,0x59,0xc8,0xbd,0x2c,0x5e,0xcf
  54. };
  55. #ifndef isdigit
  56. #define in_range(c, lo, up) ((u8)c >= lo && (u8)c <= up)
  57. #define isdigit(c) in_range(c, '0', '9')
  58. #endif
  59. int chk_crc8(u8 *ptr, u32 len)
  60. {
  61. u8 crc8;
  62. u8 data;
  63. crc8=0;
  64. while (len--!=0) {
  65. data = *ptr++;
  66. crc8 = crc8_tbl[crc8^data];
  67. }
  68. if(crc8==0x00) {return 0;}
  69. else {return -1;}
  70. }
  71. //#ifndef TLS_CONFIG_FPGA
  72. u8 get_crc8(u8 *ptr, u32 len)
  73. {
  74. u8 crc8;
  75. u8 data;
  76. crc8=0;
  77. while (len--!=0) {
  78. data = *ptr++;
  79. crc8 = crc8_tbl[crc8^data];
  80. }
  81. return crc8;
  82. }
  83. //#endif
  84. u8 calculate_crc8(u8 crc8, u8 *ptr, u32 len)
  85. {
  86. u8 data;
  87. while (len--!=0) {
  88. data = *ptr++;
  89. crc8 = crc8_tbl[crc8^data];
  90. }
  91. return crc8;
  92. }
  93. static u32 _cal_crc32(u32 crc_result, u8 data_8)
  94. {
  95. u8 crc_out[32];
  96. u8 crc_buf[32];
  97. u8 in_data_buf[8];
  98. u32 i;
  99. u32 flag;
  100. flag = 0x01;
  101. for (i = 0; i < 32; i++) {
  102. crc_out[i] = 0;
  103. }
  104. for (i = 0; i < 8; i++) {
  105. in_data_buf[i] = (data_8 >> i) & flag;
  106. }
  107. for (i = 0; i < 32; i++) {
  108. crc_buf[i] = (unsigned char)(crc_result >> i) & flag;
  109. }
  110. crc_out[0] = in_data_buf[1]^in_data_buf[7]^crc_buf[30]^crc_buf[24];
  111. crc_out[1] = in_data_buf[0]^in_data_buf[1]^in_data_buf[6]^in_data_buf[7]^crc_buf[31]^crc_buf[30]^crc_buf[25]^crc_buf[24];
  112. crc_out[2] = in_data_buf[0]^in_data_buf[1]^in_data_buf[5]^in_data_buf[6]^in_data_buf[7]^crc_buf[31]^crc_buf[30]^crc_buf[26]^crc_buf[25]^crc_buf[24];
  113. crc_out[3] = in_data_buf[0]^in_data_buf[4]^in_data_buf[5]^in_data_buf[6]^crc_buf[31]^crc_buf[27]^crc_buf[26]^crc_buf[25];
  114. crc_out[4] = in_data_buf[1]^in_data_buf[3]^in_data_buf[4]^in_data_buf[5]^in_data_buf[7]^crc_buf[30]^crc_buf[28]^crc_buf[27]^crc_buf[26]^crc_buf[24];
  115. crc_out[5] = in_data_buf[0]^in_data_buf[1]^in_data_buf[2]^in_data_buf[3]^in_data_buf[4]^in_data_buf[6]^in_data_buf[7]^
  116. crc_buf[31]^crc_buf[30]^crc_buf[29]^crc_buf[28]^crc_buf[27]^crc_buf[25]^crc_buf[24];
  117. crc_out[6] = in_data_buf[0]^in_data_buf[1]^in_data_buf[2]^in_data_buf[3]^in_data_buf[5]^in_data_buf[6]^
  118. crc_buf[31]^crc_buf[30]^crc_buf[29]^crc_buf[28]^crc_buf[26]^crc_buf[25];
  119. crc_out[7] = in_data_buf[0]^in_data_buf[2]^in_data_buf[4]^in_data_buf[5]^in_data_buf[7]^crc_buf[31]^crc_buf[29]^crc_buf[27]^crc_buf[26]^crc_buf[24];
  120. crc_out[8] = in_data_buf[3]^in_data_buf[4]^in_data_buf[6]^in_data_buf[7]^crc_buf[28]^crc_buf[27]^crc_buf[25]^crc_buf[24]^crc_buf[0];
  121. crc_out[9] = in_data_buf[2]^in_data_buf[3]^in_data_buf[5]^in_data_buf[6]^crc_buf[29]^crc_buf[28]^crc_buf[26]^crc_buf[25]^crc_buf[1];
  122. crc_out[10] = in_data_buf[2]^in_data_buf[4]^in_data_buf[5]^in_data_buf[7]^crc_buf[29]^crc_buf[27]^crc_buf[26]^crc_buf[24]^crc_buf[2];
  123. crc_out[11] = in_data_buf[3]^in_data_buf[4]^in_data_buf[6]^in_data_buf[7]^crc_buf[28]^crc_buf[27]^crc_buf[25]^crc_buf[24]^crc_buf[3];
  124. crc_out[12] = in_data_buf[1]^in_data_buf[2]^in_data_buf[3]^in_data_buf[5]^in_data_buf[6]^in_data_buf[7]^
  125. crc_buf[30]^crc_buf[29]^crc_buf[28]^crc_buf[26]^crc_buf[25]^crc_buf[24]^crc_buf[4];
  126. crc_out[13] = in_data_buf[0]^in_data_buf[1]^in_data_buf[2]^in_data_buf[4]^in_data_buf[5]^in_data_buf[6]^
  127. crc_buf[31]^crc_buf[30]^crc_buf[29]^crc_buf[27]^crc_buf[26]^crc_buf[25]^crc_buf[5];
  128. crc_out[14] = in_data_buf[0]^in_data_buf[1]^in_data_buf[3]^in_data_buf[4]^in_data_buf[5]^crc_buf[31]^crc_buf[30]^crc_buf[28]^crc_buf[27]^crc_buf[26]^crc_buf[6];
  129. crc_out[15] = in_data_buf[0]^in_data_buf[2]^in_data_buf[3]^in_data_buf[4]^crc_buf[31]^crc_buf[29]^crc_buf[28]^crc_buf[27]^crc_buf[7];
  130. crc_out[16] = in_data_buf[2]^in_data_buf[3]^in_data_buf[7]^crc_buf[29]^crc_buf[28]^crc_buf[24]^crc_buf[8];
  131. crc_out[17] = in_data_buf[1]^in_data_buf[2]^in_data_buf[6]^crc_buf[30]^crc_buf[29]^crc_buf[25]^crc_buf[9];
  132. crc_out[18] = in_data_buf[0]^in_data_buf[1]^in_data_buf[5]^crc_buf[31]^crc_buf[30]^crc_buf[26]^crc_buf[10];
  133. crc_out[19] = in_data_buf[0]^in_data_buf[4]^crc_buf[31]^crc_buf[27]^crc_buf[11];
  134. crc_out[20] = in_data_buf[3]^crc_buf[28]^crc_buf[12];
  135. crc_out[21] = in_data_buf[2]^crc_buf[29]^crc_buf[13];
  136. crc_out[22] = in_data_buf[7]^crc_buf[24]^crc_buf[14];
  137. crc_out[23] = in_data_buf[1]^in_data_buf[6]^in_data_buf[7]^crc_buf[30]^crc_buf[25]^crc_buf[24]^crc_buf[15];
  138. crc_out[24] = in_data_buf[0]^in_data_buf[5]^in_data_buf[6]^crc_buf[31]^crc_buf[26]^crc_buf[25]^crc_buf[16];
  139. crc_out[25] = in_data_buf[4]^in_data_buf[5]^crc_buf[27]^crc_buf[26]^crc_buf[17];
  140. crc_out[26] = in_data_buf[1]^in_data_buf[3]^in_data_buf[4]^in_data_buf[7]^crc_buf[30]^crc_buf[28]^crc_buf[27]^crc_buf[24]^crc_buf[18];
  141. crc_out[27] = in_data_buf[0]^in_data_buf[2]^in_data_buf[3]^in_data_buf[6]^crc_buf[31]^crc_buf[29]^crc_buf[28]^crc_buf[25]^crc_buf[19];
  142. crc_out[28] = in_data_buf[1]^in_data_buf[2]^in_data_buf[5]^crc_buf[30]^crc_buf[29]^crc_buf[26]^crc_buf[20];
  143. crc_out[29] = in_data_buf[0]^in_data_buf[1]^in_data_buf[4]^crc_buf[31]^crc_buf[30]^crc_buf[27]^crc_buf[21];
  144. crc_out[30] = in_data_buf[0]^in_data_buf[3]^crc_buf[31]^crc_buf[28]^crc_buf[22];
  145. crc_out[31] = in_data_buf[2]^crc_buf[23]^crc_buf[29];
  146. crc_result = 0;
  147. for (i = 0; i < 32; i++) {
  148. if (crc_out[i]) {crc_result |= (1<<i);}
  149. }
  150. return crc_result;
  151. }
  152. //#ifndef TLS_CONFIG_FPGA
  153. u32 get_crc32(u8 *data, u32 data_size)
  154. {
  155. u32 i;
  156. u32 val;
  157. int crc_result = 0xffffffff;
  158. for (i = 0; i < data_size; i++) {
  159. crc_result = _cal_crc32(crc_result, data[i]);
  160. }
  161. val = 0;
  162. for (i = 0; i < 32; i++) {
  163. if ((crc_result>>i) & 0x1) {val |= (1<<(31-i));}
  164. }
  165. TLS_DBGPRT_INFO("calculate crc -0x%x .\n", ~val);
  166. return ~val;
  167. }
  168. //#endif
  169. u32 checksum(u32 *data, u32 length, u32 init)
  170. {
  171. static long long sum = 0;
  172. u32 checksum;
  173. u32 i;
  174. /*
  175. Calculate the checksum.
  176. */
  177. if (!init) {sum = 0;}
  178. for (i = 0; i < length; i++) {sum+=*(data + i);}
  179. checksum = ~((u32)(sum>>32)+(u32)sum);
  180. return checksum;
  181. }
  182. int atodec(char ch)
  183. {
  184. int dec = -1;
  185. if ((ch >= '0') && (ch <= '9')) {dec = ch - '0';}
  186. return dec;
  187. }
  188. int strtodec(int *dec, char *str)
  189. {
  190. int i;
  191. int dd;
  192. int sign;
  193. i = -1;
  194. dd = 0;
  195. sign = 1;
  196. if (*str == '-') {
  197. str++;
  198. sign = -1;
  199. }
  200. while (*str) {
  201. i = atodec(*str++);
  202. if (i < 0) {return -1;}
  203. dd = dd*10 + i;
  204. }
  205. *dec = dd*sign;
  206. return ((i < 0) ? -1 : 0);
  207. }
  208. int atohex(char ch)
  209. {
  210. int hex;
  211. hex = -1;
  212. if ((ch >= '0') && (ch <= '9')) {hex = ch - '0';}
  213. else if ((ch >= 'a') && (ch <= 'f')) {hex = ch - 'a' + 0xa;}
  214. else if ((ch >= 'A') && (ch <= 'F')) {hex = ch - 'A' + 0xa;}
  215. return hex;
  216. }
  217. int strtohex(u32 *hex, char *str)
  218. {
  219. int n;
  220. int i;
  221. u32 dd;
  222. n = -1;
  223. i = 0;
  224. dd = 0;
  225. while(*str){
  226. n = atohex(*str++);
  227. if (n < 0) {return -1;}
  228. dd = (dd<<4) + n;
  229. if (++i > 8){return -1;}
  230. }
  231. *hex = dd;
  232. return (n<0?-1:0);
  233. }
  234. int strtohexarray(u8 array[], int cnt, char *str)
  235. {
  236. int hex;
  237. u8 tmp;
  238. u8 *des;
  239. des = array;
  240. while (cnt-- > 0) {
  241. hex = atohex(*str++);
  242. if (hex < 0) {return -1;}
  243. else {tmp = (hex << 4) & 0xf0;}
  244. hex = atohex(*str++);
  245. if (hex < 0) {return -1;}
  246. else {tmp = tmp | (hex & 0x0f);}
  247. *des++ = (u8) tmp;
  248. }
  249. return ((*str==0) ? 0 : -1);
  250. }
  251. int strtoip(u32 *ipadr, char * str)
  252. {
  253. int n;
  254. u32 i;
  255. u32 ip;
  256. char *head;
  257. char *tail;
  258. ip = 0;
  259. head = str;
  260. tail = str;
  261. for (i = 0; i < 3; ) {
  262. if (*tail == '.') {
  263. i++;
  264. *tail = 0;
  265. ip <<= 8;
  266. if (strtodec(&n, head) < 0) {return -1;}
  267. if ((n < 0) || (n > 255)) {return -1;}
  268. ip += n;
  269. *tail = '.';
  270. head = tail + 1;
  271. }
  272. tail++;
  273. }
  274. if (i < 3) {return -1;}
  275. ip <<= 8;
  276. if (strtodec(&n, head) < 0) {return -1;}
  277. if ((n < 0) || (n > 255)) {return -1;}
  278. ip += n;
  279. *ipadr = ip;
  280. return ((ip == 0) ? -1 : 0);
  281. }
  282. void iptostr(u32 ip, char *str)
  283. {
  284. sprintf(str, "%d.%d.%d.%d",
  285. ((ip >> 24) & 0xff),((ip >> 16) & 0xff),\
  286. ((ip >> 8) & 0xff), ((ip >> 0) & 0xff));
  287. }
  288. void mactostr(u8 mac[], char *str)
  289. {
  290. sprintf(str, "%02x%02x%02x%02x%02x%02x",
  291. mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
  292. }
  293. int hex_to_digit(int c)
  294. {
  295. if( '0' <= c && c <= '9' )
  296. return c - '0';
  297. if( 'A' <= c && c <= 'F' )
  298. return c - ('A' - 10);
  299. if( 'a' <= c && c <= 'f' )
  300. return c - ('a' - 10);
  301. return -1;
  302. }
  303. int digit_to_hex(int c)
  304. {
  305. if( 0 <= c && c <= 9 )
  306. return c + '0';
  307. if( 0xA <= c && c <= 0xF )
  308. return c - 0xA + 'A' ;
  309. return -1;
  310. }
  311. int hexstr_to_unit(char *buf, u32 *d)
  312. {
  313. int i;
  314. int len = strlen(buf);
  315. int c;
  316. *d = 0;
  317. if (len > 8)
  318. return -1;
  319. for (i=0; i<len; i++) {
  320. c = hex_to_digit(buf[i]);
  321. if (c < 0)
  322. return -1;
  323. *d = (u8)c | (*d << 4);
  324. }
  325. return 0;
  326. }
  327. int string_to_uint(char *buf, u32 *d)
  328. {
  329. int i;
  330. int len = strlen(buf);
  331. if (len > 11 || len == 0)
  332. return -1;
  333. for(i=0; i<len; i++) {
  334. if (!isdigit(buf[i]))
  335. return -1;
  336. }
  337. *d = atoi(buf);
  338. return 0;
  339. }
  340. int string_to_ipaddr(const char *buf, u8 *addr)
  341. {
  342. int count = 0, rc = 0;
  343. int in[4];
  344. char c;
  345. rc = sscanf(buf, "%u.%u.%u.%u%c",
  346. &in[0], &in[1], &in[2], &in[3], &c);
  347. if (rc != 4 && (rc != 5 || c != '\n'))
  348. return -1;
  349. for (count = 0; count < 4; count++) {
  350. if (in[count] > 255)
  351. return -1;
  352. addr[count] = in[count];
  353. }
  354. return 0;
  355. }
  356. char * strdup(const char *s)
  357. {
  358. char * ret;
  359. int len;
  360. //if(s == NULL)
  361. // return NULL;
  362. len = strlen(s) + 1;
  363. ret = tls_mem_alloc(len);
  364. if(ret == NULL)
  365. return NULL;
  366. memset(ret, 0, len);
  367. memcpy(ret, s, len-1);
  368. return ret;
  369. }
  370. char * strndup(const char *s, size_t len)
  371. {
  372. char * ret;
  373. //if(s == NULL)
  374. // return NULL;
  375. ret = tls_mem_alloc(len + 1);
  376. if(ret == NULL)
  377. return NULL;
  378. memset(ret, 0, len + 1);
  379. memcpy(ret, s, len);
  380. return ret;
  381. }
  382. #if 1
  383. int gettimeofday(struct timeval *tv, struct timezone *tz)
  384. {
  385. int ret = 0;
  386. u32 current_tick;
  387. current_tick = tls_os_get_time();//OSTimeGet();
  388. tv->tv_sec = (current_tick) / 100;
  389. tv->tv_usec = 10000 * (current_tick % 100);
  390. return ret;
  391. }
  392. #endif
  393. void delay_cnt(int count)
  394. {
  395. #ifdef TLS_CONFIG_CPU_XT804
  396. volatile int delay = count;
  397. #else
  398. int delay = count;
  399. #endif
  400. while(delay--)
  401. ;
  402. }
  403. void dumpBuffer(char *name, char* buffer, int len)
  404. {
  405. #if 1
  406. int i = 0;
  407. printf("%s:\n", name);
  408. for(; i < len; i++)
  409. {
  410. printf("%02X, ", buffer[i]);
  411. if((i + 1) % 16 == 0)
  412. {
  413. printf("\n");
  414. }
  415. }
  416. printf("\n");
  417. #endif
  418. }
  419. void dumpUint32(char *name, uint32_t* buffer, int len)
  420. {
  421. int i = 0;
  422. printf("%s:\n", name);
  423. for(; i < len; i++)
  424. {
  425. printf("%08x ", buffer[i]);
  426. if((i + 1) % 8 == 0)
  427. {
  428. printf("\n");
  429. }
  430. }
  431. printf("\n");
  432. }
  433. int strcasecmp(const char *s1, const char *s2)
  434. {
  435. char a, b;
  436. while (*s1 && *s2) {
  437. a = *s1++;
  438. b = *s2++;
  439. if (a == b)
  440. continue;
  441. if (a >= 'a' && a <= 'z')
  442. a -= 'a' - 'A';
  443. if (b >= 'a' && b <= 'z')
  444. b -= 'a' - 'A';
  445. if (a != b)
  446. return 1;
  447. }
  448. return 0;
  449. }