cfw_chset.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _CFW_CHSET_H_
  13. #define _CFW_CHSET_H_
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #include "quec_proj_config.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /**
  22. * @brief convert ASCII string to BCD
  23. *
  24. * Convert rules:
  25. * - 0-9:
  26. * - *: 0xa
  27. * - #: 0xb
  28. * - p/P: 0xc
  29. * - w/W: 0xd
  30. * - +: 0xe
  31. *
  32. * Example: 12345 -> 0x21 0x43 0xF5
  33. *
  34. * Caller should make sure there are enough space in output BCD array.
  35. * The output size is: (size + 1) >> 1
  36. *
  37. * @param s input ASCII string
  38. * @param size input ASCII string size
  39. * @param bsd output BCD array
  40. * @return
  41. * - -1: failed, due to invalid character in input string
  42. * - size of BCD array
  43. */
  44. int cfwDialStringToBcd(const void *src, size_t size, void *bcd);
  45. /**
  46. * @brief convert BCD to ASCII string
  47. *
  48. * Convert rules:
  49. * - 0-9:
  50. * - 0xa: *
  51. * - 0xb: #
  52. * - 0xc: p
  53. * - 0xd: w
  54. * - 0xe: +
  55. * 0xf can only appear as the higher 4-bits of the last byte of BCD array.
  56. *
  57. * Example: 0x21 0x43 0xF5 -> 12345
  58. *
  59. * Caller should make sure there are enough space in output BCD array.
  60. * Also, \\0 will be appended to the end of output ASCII string.
  61. * The output size can be: size*2 or size*2+1.
  62. *
  63. * @param bcd input BCD array
  64. * @param size input BCD array size
  65. * @param s output ASCII string
  66. * @return
  67. * - -1: failed, due to invalid input
  68. * - size of ASCII string, not including \\0
  69. */
  70. int cfwBcdToDialString(const void *src, size_t size, void *dst);
  71. /**
  72. * @brief convert HEX string to bytes
  73. *
  74. * Example:
  75. * - 123456 -> 0x12 0x34 0x56
  76. * - 1234567 -> 0x12 0x34 0x56 0x70
  77. *
  78. * Caller should make sure there are enough space in output byte array.
  79. * The output size is: (size + 1) >> 1
  80. *
  81. * @param src input HEX string
  82. * @param size input HEX string size
  83. * @param dst output byte array
  84. * @return
  85. * - -1: failed, due to invalid character in input string
  86. * - size of bytes
  87. */
  88. int cfwHexStrToBytes(const void *src, size_t size, void *dst);
  89. /**
  90. * @brief convert bytes to HEX string
  91. *
  92. * An extra \\0 will be appended to the output string.
  93. *
  94. * Caller should make sure there are enough space in output string.
  95. * The output size is: (size * 2 + 1)
  96. *
  97. * @param src input byte array
  98. * @param size input byte array size
  99. * @param dst output HEX string
  100. * @return
  101. * - -1: failed, due to NULL pointer
  102. * - size of string, , not including \\0
  103. */
  104. int cfwBytesToHexStr(const void *src, size_t size, void *dst);
  105. int cfwDtmfCharToTone(char c);
  106. int cfwToneToDtmfChar(char c);
  107. int cfwDecode7Bit(const void *src, void *dst, size_t size);
  108. int cfwEncode7Bit(const void *src, void *dst, size_t size);
  109. #ifdef CONFIG_QUEC_PROJECT_FEATURE
  110. uint8_t _BcdToDec(uint8_t val);
  111. #endif
  112. /**
  113. * @brief pack time and time zone
  114. *
  115. * Packed time is 7 bytes:
  116. * - [0]: BCD of (year - 2000)
  117. * - [1]: BCD of month (1-12)
  118. * - [2]: BCD of day in month (1-31)
  119. * - [3]: BCD of hour (0-23)
  120. * - [4]: BCD of minute (0-59)
  121. * - [5]: BCD of second (0-59)
  122. * - [6]: reversed BCD of time zone in quarter. WHen time zone is negative, it is
  123. * ored with 0x08
  124. *
  125. * For example:
  126. * - 2018-01-02 13:14:15 +8 will be packed into: 0x18 0x01 0x02 0x13 0x14 0x15 0x23
  127. * - 2018-01-02 13:14:15 -8 will be packed into: 0x18 0x01 0x02 0x13 0x14 0x15 0x2b
  128. *
  129. * The relationship between epoch time, local time and time zone:
  130. * local_time = epoch_time + time_zone
  131. *
  132. * @param pack packed time
  133. * @param seconds seconds from 1970-01-01 in local time
  134. * @param time_zone time zone offset in seconds
  135. */
  136. void cfwPackTime(void *pack, uint32_t seconds, int time_zone);
  137. /**
  138. * same to cfwPackTime
  139. */
  140. void cfwPackTimeV2(void *pack, uint32_t seconds, int time_zone);
  141. /**
  142. * @brief unpack time and time zone
  143. *
  144. * Packed time is described in \p cfwPackTime.
  145. *
  146. * @param pack packed time
  147. * @param seconds seconds from 1970-01-01 in local time
  148. * @param time_zone time zone offset in seconds
  149. */
  150. void cfwUnpackTime(const void *pack, uint32_t *seconds, int *time_zone);
  151. int cfwBcdToString(const void *src, size_t size, void *dst);
  152. int cfwStringToBcd(const void *src, size_t size, void *dst);
  153. #define SUL_AsciiToGsmBcd(src, size, dst) cfwDialStringToBcd(src, size, dst)
  154. #define SUL_gsmbcd2ascii(src, size, dst) cfwBcdToDialString(src, size, dst)
  155. #define SUL_GsmBcdToAscii(src, size, dst) cfwBcdToDialString(src, size, dst)
  156. #define SUL_hex2ascii(src, size, dst) cfwHexStrToBytes(src, size, dst)
  157. #define SUL_Decode7Bit(src, dst, size) cfwDecode7Bit(src, dst, size)
  158. #define SUL_Encode7Bit(src, dst, size) cfwEncode7Bit(src, dst, size)
  159. #ifdef __cplusplus
  160. }
  161. #endif
  162. #endif