WIZlib Library API  ver 1.0
WIZlib Library API User Menual
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
util.c
Go to the documentation of this file.
1 
12 //#define FILE_LOG_SILENCE
13 #include "common/common.h"
14 //#include "common/util.h"
15 
16 #define MAX_TICK_ELAPSE 0x7FFFFFFF // Maximum elapse time you can set
17 
18 typedef struct alarm_node_t {
19  struct alarm_node_t *next;
20  uint32 time;
21  alarm_cbfunc cb;
22  int8 arg;
23 } alarm_node;
24 
25 
26 static alarm_node *alst = NULL;
27 
28 /*------ Base64 Encoding Table ------*/
29 static const int8 MimeBase64[] = {
30  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
31  'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
32  'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
33  'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
34  'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
35  'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
36  'w', 'x', 'y', 'z', '0', '1', '2', '3',
37  '4', '5', '6', '7', '8', '9', '+', '/'
38 };
39 
40 /*------ Base64 Decoding Table ------*/
41 static int32 DecodeMimeBase64[256] = {
42  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
43  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
44  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
45  52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
46  -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
47  15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
48  -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
49  41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
50  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
51  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
52  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
53  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
54  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
55  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
56  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
57  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
58 };
59 
78 int8 alarm_set(uint32 time, alarm_cbfunc cb, int8 arg)
79 {
80  alarm_node *aptr, **adbl;
81 
82  if(time > MAX_TICK_ELAPSE || cb == NULL) return RET_NOK;
83 
84  //if(time) DBGA("Set with delay: Time(%d), CB(%p), ARG(%d)", time, (void*)cb, arg);
85  time += wizpf_get_systick();
86 
87  adbl = &alst;
88  while(*adbl && (*adbl)->time <= time) {
89  adbl = &(*adbl)->next;
90  }
91 
92  aptr = *adbl;
93  *adbl = calloc(1, sizeof(alarm_node));
94  if(*adbl == NULL) {
95  *adbl = aptr;
96  ERRA("calloc fail - size(%d)", sizeof(alarm_node));
97  return RET_NOK;
98  }
99  (*adbl)->next = aptr;
100  (*adbl)->time = time;
101  (*adbl)->cb = cb;
102  (*adbl)->arg = arg;
103 
104  return RET_OK;
105 }
106 
119 int8 alarm_del(alarm_cbfunc cb, int8 arg)
120 {
121  int8 cnt = 0;
122  alarm_node *aptr, **adbl = &alst;
123 
124  while(*adbl) {
125  if( (cb == NULL || (cb != NULL && ((*adbl)->cb == cb))) &&
126  (arg == -1 || (arg >= 0 && ((*adbl)->arg == arg))) )
127  {
128  aptr = *adbl;
129  *adbl = aptr->next;
130  DBGA("Del: CB(%p),ARG(%d)", (void*)aptr->cb, aptr->arg);
131  free(aptr);
132  cnt++;
133  } else adbl = &(*adbl)->next;
134  }
135 
136  return cnt;
137 }
138 
151 int8 alarm_chk(alarm_cbfunc cb, int8 arg)
152 {
153  int8 cnt = 0;
154  alarm_node **adbl = &alst;
155 
156  while(*adbl) {
157  if( (cb == NULL || (cb != NULL && ((*adbl)->cb == cb))) &&
158  (arg == -1 || (arg >= 0 && ((*adbl)->arg == arg))) )
159  {
160  DBGA("Chk: CB(%p),ARG(%d)", (void*)(*adbl)->cb, (*adbl)->arg);
161  cnt++;
162  }
163  adbl = &(*adbl)->next;
164  }
165 
166  return cnt;
167 }
168 
173 void alarm_run(void)
174 {
175  uint32 cur = wizpf_get_systick(); //for DBG: static uint32 prt=999999;if(prt!=alst->time) {DBGA("cur(%d), time(%d)", wizpf_get_systick(), alst->time);prt = alst->time;}
176  if(wizpf_tick_elapse(alst->time) >= 0) {
177  alarm_node *aptr = alst; //for DBG: DBGA("cb call - cur(%d), time(%d), next(%d)", wizpf_get_systick(), alst->time, (alst->next)->time);
178  alst = alst->next;
179  aptr->cb(aptr->arg);
180  free(aptr);
181  }
182 }
183 
184 /* @} */
185 
186 
200 int8 digit_length(int32 dgt, int8 base)
201 {
202  int16 i, len = 0;
203 
204  if(dgt < 0) {
205  len++;
206  dgt *= -1;
207  }
208 
209  for(i=0; i<255; i++) {
210  len++;
211  dgt /= base;
212  if(dgt == 0) return len;
213  }
214 
215  return RET_NOK;
216 }
217 
233 int32 str_check(int (*method)(int), int8 *str)
234 {
235  if(method == NULL || str == NULL || *str == 0)
236  return RET_NOK;
237 
238  while(*str) {
239  if(!method((int)*str)) return RET_NOK;
240  str++;
241  }
242 
243  return RET_OK;
244 }
245 
257 int8 *strsep(register int8 **stringp, register const int8 *delim)
258 {
259  register int8 *s;
260  register const int8 *spanp;
261  register int32 c, sc;
262  int8 *tok;
263 
264  if ((s = *stringp) == NULL)
265  return (NULL);
266  for (tok = s;;) {
267  c = *s++;
268  spanp = delim;
269  do {
270  if ((sc = *spanp++) == c) {
271  if (c == 0)
272  s = NULL;
273  else
274  s[-1] = 0;
275  *stringp = s;
276  return (tok);
277  }
278  } while (sc != 0);
279  }
280  /* NOTREACHED */
281 }
282 
288 void print_dump(void *buf, uint16 len)
289 {
290  uint8 *tp = (uint8*)buf;
291  uint16 i;
292  uint16 line = len / 0x10;
293  uint16 left = len % 0x10;
294 
295  LOG("===========================================================");
296  LOG("-ADDR----0--1--2--3--4--5--6--7----8--9--A--B--C--D--E--F--");
297  for(i=0; i<line; i++) {
298  LOGA("0x%04x %02x %02x %02x %02x %02x %02x %02x %02x"
299  " %02x %02x %02x %02x %02x %02x %02x %02x", 0x10*i,
300  tp[0x10*i+0x0], tp[0x10*i+0x1], tp[0x10*i+0x2], tp[0x10*i+0x3],
301  tp[0x10*i+0x4], tp[0x10*i+0x5], tp[0x10*i+0x6], tp[0x10*i+0x7],
302  tp[0x10*i+0x8], tp[0x10*i+0x9], tp[0x10*i+0xA], tp[0x10*i+0xB],
303  tp[0x10*i+0xC], tp[0x10*i+0xD], tp[0x10*i+0xE], tp[0x10*i+0xF]);
304  }
305  if(left) {
306  LOGFA("0x%04x ", 0x10*line);
307  for(i=0; i<left; i++) LOGFA("%02x ", tp[0x10*line + i]);
308  NL1;
309  }
310  LOG("===========================================================");
311 }
312 
319 uint16 checksum(uint8 * src, uint32 len)
320 {
321  uint16 sum, tsum, i, j;
322  uint32 lsum;
323 
324  j = len >> 1;
325  lsum = 0;
326 
327  for(i = 0; i < j; i++) {
328  tsum = src[i*2];
329  tsum = tsum << 8;
330  tsum += src[i*2+1];
331  lsum += tsum;
332  }
333 
334  if(len % 2) {
335  tsum = src[i*2];
336  lsum += (tsum << 8);
337  }
338 
339  sum = lsum;
340  sum = ~(sum + (lsum >> 16));
341 
342  return (uint16)sum;
343 }
344 
345 /* @} */
346 
347 
348 
365 int32 base64_decode(int8 *text, uint8 *dst, int32 numBytes)
366 {
367  const int8* cp;
368  int32 space_idx = 0, phase;
369  int32 d, prev_d = 0;
370  uint8 c;
371 
372  space_idx = 0;
373  phase = 0;
374 
375  for ( cp = text; *cp != '\0'; ++cp ) {
376  d = DecodeMimeBase64[(int32) *cp];
377  if ( d != -1 ) {
378  switch ( phase ) {
379  case 0:
380  ++phase;
381  break;
382  case 1:
383  c = ( ( prev_d << 2 ) | ( ( d & 0x30 ) >> 4 ) );
384  if ( space_idx < numBytes )
385  dst[space_idx++] = c;
386  ++phase;
387  break;
388  case 2:
389  c = ( ( ( prev_d & 0xf ) << 4 ) | ( ( d & 0x3c ) >> 2 ) );
390  if ( space_idx < numBytes )
391  dst[space_idx++] = c;
392  ++phase;
393  break;
394  case 3:
395  c = ( ( ( prev_d & 0x03 ) << 6 ) | d );
396  if ( space_idx < numBytes )
397  dst[space_idx++] = c;
398  phase = 0;
399  break;
400  }
401  prev_d = d;
402  }
403  }
404 
405  return space_idx;
406 
407 }
408 
417 int32 base64_encode(int8 *text, int32 numBytes, int8 *encodedText)
418 {
419  uint8 input[3] = {0,0,0};
420  uint8 output[4] = {0,0,0,0};
421  int32 index, i, j;
422  int8 *p, *plen;
423 
424  plen = text + numBytes - 1;
425  j = 0;
426 
427  for (i = 0, p = text;p <= plen; i++, p++) {
428  index = i % 3;
429  input[index] = *p;
430 
431  if (index == 2 || p == plen) {
432  output[0] = ((input[0] & 0xFC) >> 2);
433  output[1] = ((input[0] & 0x3) << 4) | ((input[1] & 0xF0) >> 4);
434  output[2] = ((input[1] & 0xF) << 2) | ((input[2] & 0xC0) >> 6);
435  output[3] = (input[2] & 0x3F);
436 
437  encodedText[j++] = MimeBase64[output[0]];
438  encodedText[j++] = MimeBase64[output[1]];
439  encodedText[j++] = index == 0? '=' : MimeBase64[output[2]];
440  encodedText[j++] = index < 2? '=' : MimeBase64[output[3]];
441 
442  input[0] = input[1] = input[2] = 0;
443  }
444  }
445 
446  encodedText[j] = '\0';
447 
448  return 0;
449 }
450 
451 /* @} */
452 
453 #ifdef USE_FULL_ASSERT
454 /* NOT USE !!!
455  * Asset function which declared at stm32f10x_conf.h file
456  * If USE_FULL_ASSERT is defined this will be active.
457  * @param file File name in which asset occurred.
458  * @param line asserted line number
459  */
460 void assert_failed(uint8* file, uint32 line)
461 {
462  /* User can add his own implementation to report the file name and line number,
463  ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
464 
465  /* Infinite loop */
466  while (1)
467  {
468  }
469 }
470 #endif
471 
472 
473