ioLibrary for Cortex M series
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_t time;
22  int8_t arg;
23 } alarm_node;
24 
25 
26 static alarm_node *alst = NULL;
27 
28 /*------ Base64 Encoding Table ------*/
29 static const int8_t 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_t 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_t alarm_set(uint32_t time, alarm_cbfunc cb, int8_t 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_t alarm_del(alarm_cbfunc cb, int8_t arg)
120 {
121  int8_t 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_t alarm_chk(alarm_cbfunc cb, int8_t arg)
152 {
153  int8_t 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  if(wizpf_tick_elapse(alst->time) >= 0) {
176  alarm_node *aptr = alst; //for DBG: DBGA("cb call - cur(%d), time(%d), next(%d)", wizpf_get_systick(), alst->time, (alst->next)->time);
177  alst = alst->next;
178  aptr->cb(aptr->arg);
179  free(aptr);
180  }
181 }
182 
183 /* @} */
184 
185 
199 int8_t digit_length(int32_t dgt, int8_t base)
200 {
201  int16_t i, len = 0;
202 
203  if(dgt < 0) {
204  len++;
205  dgt *= -1;
206  }
207 
208  for(i=0; i<255; i++) {
209  len++;
210  dgt /= base;
211  if(dgt == 0) return len;
212  }
213 
214  return RET_NOK;
215 }
216 
232 int32_t str_check(int (*method)(int), int8_t *str)
233 {
234  if(method == NULL || str == NULL || *str == 0)
235  return RET_NOK;
236 
237  while(*str) {
238  if(!method((int)*str)) return RET_NOK;
239  str++;
240  }
241 
242  return RET_OK;
243 }
244 
256  /*
257 int8_t *strsep(int8_t **stringp, const int8_t *delim)
258 {
259  int8_t *s;
260  const int8_t *spanp;
261  int32_t c, sc;
262  int8_t *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 
281 }
282 */
288 void print_dump(void *buf, uint16_t len)
289 {
290  uint8_t *tp = (uint8_t*)buf;
291  uint16_t i;
292  uint16_t line = len / 0x10;
293  uint16_t left = len % 0x10;
294 
295  (void) tp; // Prevent warning message
296  LOG("===========================================================");
297  LOG("-ADDR----0--1--2--3--4--5--6--7----8--9--A--B--C--D--E--F--");
298  for(i=0; i<line; i++) {
299  LOGA("0x%04x %02x %02x %02x %02x %02x %02x %02x %02x"
300  " %02x %02x %02x %02x %02x %02x %02x %02x", 0x10*i,
301  tp[0x10*i+0x0], tp[0x10*i+0x1], tp[0x10*i+0x2], tp[0x10*i+0x3],
302  tp[0x10*i+0x4], tp[0x10*i+0x5], tp[0x10*i+0x6], tp[0x10*i+0x7],
303  tp[0x10*i+0x8], tp[0x10*i+0x9], tp[0x10*i+0xA], tp[0x10*i+0xB],
304  tp[0x10*i+0xC], tp[0x10*i+0xD], tp[0x10*i+0xE], tp[0x10*i+0xF]);
305  }
306  if(left) {
307  LOGFA("0x%04x ", 0x10*line);
308  for(i=0; i<left; i++) LOGFA("%02x ", tp[0x10*line + i]);
309  NL1;
310  }
311  LOG("===========================================================");
312 }
313 
320 uint16_t checksum(uint8_t * src, uint32_t len)
321 {
322  uint16_t sum, tsum, i, j;
323  uint32_t lsum;
324 
325  j = len >> 1;
326  lsum = 0;
327 
328  for(i = 0; i < j; i++) {
329  tsum = src[i*2];
330  tsum = tsum << 8;
331  tsum += src[i*2+1];
332  lsum += tsum;
333  }
334 
335  if(len % 2) {
336  tsum = src[i*2];
337  lsum += (tsum << 8);
338  }
339 
340  sum = lsum;
341  sum = ~(sum + (lsum >> 16));
342 
343  return (uint16_t)sum;
344 }
345 
346 /* @} */
347 
348 
349 
366 int32_t base64_decode(int8_t *text, uint8_t *dst, int32_t numBytes)
367 {
368  const int8_t* cp;
369  int32_t space_idx = 0, phase;
370  int32_t d, prev_d = 0;
371  uint8_t c;
372 
373  space_idx = 0;
374  phase = 0;
375 
376  for ( cp = text; *cp != '\0'; ++cp ) {
377  d = DecodeMimeBase64[(int32_t) *cp];
378  if ( d != -1 ) {
379  switch ( phase ) {
380  case 0:
381  ++phase;
382  break;
383  case 1:
384  c = ( ( prev_d << 2 ) | ( ( d & 0x30 ) >> 4 ) );
385  if ( space_idx < numBytes )
386  dst[space_idx++] = c;
387  ++phase;
388  break;
389  case 2:
390  c = ( ( ( prev_d & 0xf ) << 4 ) | ( ( d & 0x3c ) >> 2 ) );
391  if ( space_idx < numBytes )
392  dst[space_idx++] = c;
393  ++phase;
394  break;
395  case 3:
396  c = ( ( ( prev_d & 0x03 ) << 6 ) | d );
397  if ( space_idx < numBytes )
398  dst[space_idx++] = c;
399  phase = 0;
400  break;
401  }
402  prev_d = d;
403  }
404  }
405 
406  return space_idx;
407 
408 }
409 
418 int32_t base64_encode(int8_t *text, int32_t numBytes, int8_t *encodedText)
419 {
420  uint8_t input[3] = {0,0,0};
421  uint8_t output[4] = {0,0,0,0};
422  int32_t index, i, j;
423  int8_t *p, *plen;
424 
425  plen = text + numBytes - 1;
426  j = 0;
427 
428  for (i = 0, p = text;p <= plen; i++, p++) {
429  index = i % 3;
430  input[index] = *p;
431 
432  if (index == 2 || p == plen) {
433  output[0] = ((input[0] & 0xFC) >> 2);
434  output[1] = ((input[0] & 0x3) << 4) | ((input[1] & 0xF0) >> 4);
435  output[2] = ((input[1] & 0xF) << 2) | ((input[2] & 0xC0) >> 6);
436  output[3] = (input[2] & 0x3F);
437 
438  encodedText[j++] = MimeBase64[output[0]];
439  encodedText[j++] = MimeBase64[output[1]];
440  encodedText[j++] = index == 0? '=' : MimeBase64[output[2]];
441  encodedText[j++] = index < 2? '=' : MimeBase64[output[3]];
442 
443  input[0] = input[1] = input[2] = 0;
444  }
445  }
446 
447  encodedText[j] = '\0';
448 
449  return 0;
450 }
451 
452 /* @} */
453 
454 #ifdef USE_FULL_ASSERT
455 /* NOT USE !!!
456  * Asset function which declared at stm32f10x_conf.h file
457  * If USE_FULL_ASSERT is defined this will be active.
458  * @param file File name in which asset occurred.
459  * @param line asserted line number
460  */
461 void assert_failed(uint8_t* file, uint32_t line)
462 {
463  /* User can add his own implementation to report the file name and line number,
464  ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
465 
466  /* Infinite loop */
467  while (1)
468  {
469  }
470 }
471 #endif
472 
473 
474