carlink_utils.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include "carlink_utils.h"
  6. #include <FreeRTOS.h>
  7. #ifndef min
  8. #define min(x,y) ((x)<(y)?(x):(y))
  9. #endif
  10. void hex2str(const uint8_t *input, uint16_t input_len, char *output)
  11. {
  12. char *hexEncode = "0123456789ABCDEF";
  13. int i = 0, j = 0;
  14. for (i = 0; i < input_len; i++) {
  15. output[j++] = hexEncode[(input[i] >> 4) & 0xf];
  16. output[j++] = hexEncode[(input[i]) & 0xf];
  17. }
  18. }
  19. #if 1
  20. #define isDigit(c) (((c) <= '9' && (c) >= '0') ? (1) : (0))
  21. static uint8_t hex2dec(char hex)
  22. {
  23. if (isDigit(hex)) {
  24. return (hex - '0');
  25. }
  26. if (hex >= 'a' && hex <= 'f') {
  27. return (hex - 'a' + 10);
  28. }
  29. if (hex >= 'A' && hex <= 'F') {
  30. return (hex - 'A' + 10);
  31. }
  32. return 0;
  33. }
  34. int string2hex(char *input, int input_len, char *output, int max_len)
  35. {
  36. int i = 0;
  37. uint8_t ch0, ch1;
  38. if (input_len % 2 != 0) {
  39. return -1;
  40. }
  41. while (i < input_len / 2 && i < max_len) {
  42. ch0 = hex2dec((char)input[2 * i]);
  43. ch1 = hex2dec((char)input[2 * i + 1]);
  44. output[i] = (ch0 << 4 | ch1);
  45. i++;
  46. }
  47. return i;
  48. }
  49. #endif
  50. void str2asiistr(const char *input, int input_len, char *output, int max_len)
  51. {
  52. int i = 0;
  53. if (max_len < 2 *input_len)
  54. return;
  55. while (i < input_len) {
  56. sprintf(output, "%02x", input[i]);
  57. output += 2;
  58. i++;
  59. }
  60. }
  61. bool is_power_of_2(uint32_t n)
  62. {
  63. return (n != 0 && ((n & (n -1)) == 0));
  64. }
  65. // buffer_size musb be pow of 2
  66. int ring_buffer_init( ring_buffer_t *ctx, int buffer_size)
  67. {
  68. void *base = NULL;
  69. if (ctx == NULL || !is_power_of_2(buffer_size))
  70. return -1;
  71. base = pvPortMalloc(buffer_size);
  72. ctx->buffer = (uint8_t *)base;
  73. //ctx->end = (uint8_t *)base + buffer_size;
  74. ctx->size = (uint32_t) buffer_size;
  75. ctx->mask = (uint32_t)( buffer_size - 1 );
  76. ctx->read_offset = 0;
  77. ctx->write_offset = 0;
  78. return 0;
  79. }
  80. void ring_buffer_free( ring_buffer_t *ctx )
  81. {
  82. if (ctx == NULL || NULL == ctx->buffer)
  83. return;
  84. vPortFree(ctx->buffer);
  85. }
  86. uint32_t ring_buffer_write(ring_buffer_t *ctx, uint8_t *buffer, uint32_t len)
  87. {
  88. uint32_t l;
  89. len = min(len, ctx->size - ctx->write_offset + ctx->read_offset);
  90. l = min(len, ctx->size - (ctx->write_offset & (ctx->mask)));
  91. memcpy(ctx->buffer + (ctx->write_offset & (ctx->mask)), buffer, l);
  92. memcpy(ctx->buffer, buffer + l, len - l);
  93. ctx->write_offset += len;
  94. return len;
  95. }
  96. uint32_t ring_buffer_read(ring_buffer_t *ctx, uint8_t *buffer, uint32_t len)
  97. {
  98. uint32_t l;
  99. len = min(len, ctx->write_offset - ctx->read_offset);
  100. l = min(len, ctx->size - (ctx->read_offset & (ctx->mask)));
  101. memcpy(buffer, ctx->buffer + (ctx->read_offset & (ctx->mask)), l);
  102. memcpy(buffer + l, ctx->buffer, len - l);
  103. ctx->read_offset += len;
  104. return len;
  105. }