util.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2018 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #define _GNU_SOURCE
  14. #include <dirent.h>
  15. #include <ftw.h>
  16. #include <unistd.h>
  17. #include <stdio.h>
  18. #include <limits.h>
  19. #include <time.h>
  20. #include <sys/time.h>
  21. #include <ell/ell.h>
  22. #include "mesh/util.h"
  23. static bool debug_enabled;
  24. void print_packet(const char *label, const void *data, uint16_t size)
  25. {
  26. struct timeval pkt_time;
  27. if (!debug_enabled)
  28. return;
  29. gettimeofday(&pkt_time, NULL);
  30. if (size > 0) {
  31. char *str;
  32. str = l_util_hexstring(data, size);
  33. l_debug("%05d.%03d %s: %s",
  34. (uint32_t) pkt_time.tv_sec % 100000,
  35. (uint32_t) pkt_time.tv_usec/1000, label, str);
  36. l_free(str);
  37. } else
  38. l_debug("%05d.%03d %s: empty",
  39. (uint32_t) pkt_time.tv_sec % 100000,
  40. (uint32_t) pkt_time.tv_usec/1000, label);
  41. }
  42. uint32_t get_timestamp_secs(void)
  43. {
  44. struct timespec ts;
  45. clock_gettime(CLOCK_MONOTONIC, &ts);
  46. return ts.tv_sec;
  47. }
  48. bool str2hex(const char *str, uint16_t in_len, uint8_t *out,
  49. uint16_t out_len)
  50. {
  51. uint16_t i;
  52. if (in_len < out_len * 2)
  53. return false;
  54. for (i = 0; i < out_len; i++) {
  55. if (sscanf(&str[i * 2], "%02hhx", &out[i]) != 1)
  56. return false;
  57. }
  58. return true;
  59. }
  60. size_t hex2str(uint8_t *in, size_t in_len, char *out, size_t out_len)
  61. {
  62. static const char hexdigits[] = "0123456789abcdef";
  63. size_t i;
  64. if (in_len * 2 > (out_len - 1))
  65. return 0;
  66. for (i = 0; i < in_len; i++) {
  67. out[i * 2] = hexdigits[in[i] >> 4];
  68. out[i * 2 + 1] = hexdigits[in[i] & 0xf];
  69. }
  70. out[in_len * 2] = '\0';
  71. return i;
  72. }
  73. int create_dir(const char *dir_name)
  74. {
  75. struct stat st;
  76. char dir[PATH_MAX + 1], *prev, *next;
  77. int err;
  78. err = stat(dir_name, &st);
  79. if (!err && S_ISREG(st.st_mode))
  80. return 0;
  81. memset(dir, 0, PATH_MAX + 1);
  82. strcat(dir, "/");
  83. prev = strchr(dir_name, '/');
  84. while (prev) {
  85. next = strchr(prev + 1, '/');
  86. if (!next)
  87. break;
  88. if (next - prev == 1) {
  89. prev = next;
  90. continue;
  91. }
  92. strncat(dir, prev + 1, next - prev);
  93. mkdir(dir, 0755);
  94. prev = next;
  95. }
  96. mkdir(dir_name, 0755);
  97. return 0;
  98. }
  99. static int del_fobject(const char *fpath, const struct stat *sb, int typeflag,
  100. struct FTW *ftwbuf)
  101. {
  102. switch (typeflag) {
  103. case FTW_DP:
  104. rmdir(fpath);
  105. l_debug("RMDIR %s", fpath);
  106. break;
  107. case FTW_SL:
  108. default:
  109. remove(fpath);
  110. l_debug("RM %s", fpath);
  111. break;
  112. }
  113. return 0;
  114. }
  115. void del_path(const char *path)
  116. {
  117. nftw(path, del_fobject, 5, FTW_DEPTH | FTW_PHYS);
  118. }
  119. void enable_debug(void)
  120. {
  121. debug_enabled = true;
  122. l_debug_enable("*");
  123. }