common.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: Apache-2.0
  2. // Copyright 2015-2021 Espressif Systems (Shanghai) PTE LTD
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. /** Includes **/
  16. #include "common.h"
  17. #include "esp_log.h"
  18. #include <stdlib.h>
  19. #include <errno.h>
  20. #if 0
  21. DEFINE_LOG_TAG(utils);
  22. #endif
  23. /** Constants/Macros **/
  24. /** Exported variables **/
  25. /** Function declaration **/
  26. /** Exported Functions **/
  27. /**
  28. * @brief debug buffer print
  29. * @param buff - input buffer to print in hex
  30. * rx_len - buff len
  31. * human_str - helping string to describe about buffer
  32. * @retval None
  33. */
  34. #if DEBUG_HEX_STREAM_PRINT
  35. char print_buff[MAX_SPI_BUFFER_SIZE*3];
  36. #endif
  37. uint16_t hton_short (uint16_t x)
  38. {
  39. #if BYTE_ORDER == BIG_ENDIAN
  40. return x;
  41. #elif BYTE_ORDER == LITTLE_ENDIAN
  42. uint16_t val = 0;
  43. val = (x &0x00FF)<<8;
  44. val |= (x &0xFF00)>>8;
  45. return val;
  46. #else
  47. # error "not able to identify endianness"
  48. #endif
  49. }
  50. uint32_t hton_long (uint32_t x)
  51. {
  52. #if BYTE_ORDER == BIG_ENDIAN
  53. return x;
  54. #elif BYTE_ORDER == LITTLE_ENDIAN
  55. uint32_t val = (x&0xFF000000) >> 24;
  56. val |= (x&0x00FF0000) >> 8;
  57. val |= (x&0x0000FF00) << 8;
  58. val |= (x&0x000000FF) << 24;
  59. return val;
  60. #else
  61. # error "not able to identify endianness"
  62. #endif
  63. }
  64. /**
  65. * @brief Calculate minimum
  66. * @param x - number
  67. * y - number
  68. * @retval minimum
  69. */
  70. int min(int x, int y) {
  71. return (x < y) ? x : y;
  72. }
  73. #if 0
  74. /**
  75. * @brief get numbers from string
  76. * @param val - return integer value,
  77. * arg - input string
  78. * @retval STM_OK on success, else STM_FAIL
  79. */
  80. int get_num_from_string(int *val, char *arg)
  81. {
  82. int base = 10;
  83. char *endptr = NULL, *str = NULL;
  84. if (!arg || (arg[0]=='\0')) {
  85. ESP_LOGE(TAG, "No number Identified \n");
  86. return STM_FAIL;
  87. }
  88. if (!val) {
  89. ESP_LOGE(TAG, "No memory allocated \n");
  90. return STM_FAIL;
  91. }
  92. errno = 0;
  93. str = arg;
  94. *val = strtol(str, &endptr, base);
  95. if (endptr == str) {
  96. ESP_LOGE(TAG, "No digits found \n");
  97. *val = 0;
  98. return STM_FAIL;
  99. }
  100. if ((errno == ERANGE) && ((*val == INT32_MAX) || (*val == INT32_MIN))) {
  101. perror("strtol");
  102. *val = 0;
  103. return STM_FAIL;
  104. }
  105. return STM_OK;
  106. }
  107. #endif
  108. /** Local functions **/