app_usbd_string_desc.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef APP_USBD_STRING_DESC_H__
  41. #define APP_USBD_STRING_DESC_H__
  42. #include <stdint.h>
  43. #include "sdk_common.h"
  44. #include "app_usbd.h"
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * @defgroup app_usbd_string_desc USBD string descriptors
  50. * @ingroup app_usbd
  51. *
  52. * @brief @tagAPI52840 USBD string descriptor management.
  53. * @{
  54. */
  55. /**
  56. * @brief USB Language identifier initialization.
  57. *
  58. * @param[in] lang Language identifier.
  59. */
  60. #define APP_USBD_LANG(lang) \
  61. ((app_usbd_langid_t) lang)
  62. /**
  63. * @brief USB Language identifier with sublanguage initialization.
  64. *
  65. * @param[in] lang Language identifier.
  66. * @param[in] sublang Sublanguage identifier.
  67. */
  68. #define APP_USBD_LANG_AND_SUBLANG(lang, sublang) \
  69. ((app_usbd_langid_t) lang | (app_usbd_langid_t) sublang)
  70. /**
  71. * @brief USB string initialization.
  72. *
  73. * Macro that creates initialization values for the USB string.
  74. * The string must be declared as a NULL-terminated string.
  75. *
  76. * @param[in] str NULL-terminated string.
  77. *
  78. * @return String descriptor initialization data.
  79. */
  80. #define APP_USBD_STRING_DESC(str) (const uint8_t *)(const char[]){str}
  81. /**
  82. * @brief USB raw 8-bit string initialization.
  83. *
  84. * Macro that creates header for raw values passed into descriptor.
  85. * Values must be of the uint8_t type and separated by commas.
  86. *
  87. * @param[in] ... comma-separated values.
  88. *
  89. * @return String descriptor initialization data.
  90. */
  91. #define APP_USBD_STRING_RAW8_DESC(...) (const uint8_t[]){ \
  92. 0x00, 0x00, /* NULL character at start to differentiate from normal string */ \
  93. (0xff & (sizeof((uint8_t[]){__VA_ARGS__}) + 2)), \
  94. (APP_USBD_DESCRIPTOR_STRING), \
  95. __VA_ARGS__ }
  96. /**
  97. * @brief USB raw 16-bit string initialization.
  98. *
  99. * Macro that creates header for raw values passed into descriptor.
  100. * Values must be of the uint16_t type and separated by commas.
  101. *
  102. * @param[in] ... comma-separated values.
  103. *
  104. * @return String descriptor initialization data.
  105. */
  106. #define APP_USBD_STRING_RAW16_DESC(...) (const uint8_t *) ((const uint16_t[]){ \
  107. 0x00, /* NULL character at start to differentiate from normal string */ \
  108. (0xff & (sizeof((uint16_t[]){__VA_ARGS__}) + 2)) | \
  109. ((uint16_t)APP_USBD_DESCRIPTOR_STRING) << 8, \
  110. __VA_ARGS__ })
  111. /**
  112. * @brief USB string descriptors IDs
  113. */
  114. typedef enum {
  115. APP_USBD_STRING_ID_LANGIDS = 0, /**< Supported language identifiers */
  116. /// Placeholders used only for alignement of user strings. Do not use or modify them.
  117. #if (APP_USBD_STRING_ID_MANUFACTURER != 0)
  118. APP_USBD_STRING_ID_MANUFACTURER_PLACEHOLDER = APP_USBD_STRING_ID_MANUFACTURER,
  119. #endif // (APP_USBD_STRING_ID_MANUFACTURER != 0)
  120. #if (APP_USBD_STRING_ID_PRODUCT != 0)
  121. APP_USBD_STRING_ID_PRODUCT_PLACEHOLDER = APP_USBD_STRING_ID_PRODUCT,
  122. #endif // (APP_USBD_STRING_ID_PRODUCT != 0)
  123. #if (APP_USBD_STRING_ID_SERIAL != 0)
  124. APP_USBD_STRING_ID_SERIAL_PLACEHOLDER = APP_USBD_STRING_ID_SERIAL,
  125. #endif // (APP_USBD_STRING_ID_SERIAL != 0)
  126. #if (APP_USBD_STRING_ID_CONFIGURATION != 0)
  127. APP_USBD_STRING_ID_CONFIGURATION_PLACEHOLDER = APP_USBD_STRING_ID_CONFIGURATION,
  128. #endif // (APP_USBD_STRING_ID_CONFIGURATION != 0)
  129. #define X(mnemonic, str_idx, ...) mnemonic str_idx,
  130. APP_USBD_STRINGS_USER
  131. #undef X
  132. APP_USBD_STRING_ID_CNT /**< Total number of identifiers. */
  133. } app_usbd_string_desc_idx_t;
  134. /**
  135. * @brief Get string descriptor.
  136. *
  137. * @param[in] idx String descriptor index.
  138. * @param[in] langid Selected language for the string.
  139. * @return String descriptor, or NULL if it does not exist.
  140. * */
  141. uint16_t const * app_usbd_string_desc_get(uint8_t idx, uint16_t langid);
  142. /**
  143. * @brief Get string length.
  144. *
  145. * Function for getting string length from descriptor (descriptor returned by @ref app_usbd_string_desc_get).
  146. *
  147. * @param[in] p_str String descriptor pointer.
  148. * @return Total descriptor length in bytes.
  149. */
  150. static inline size_t app_usbd_string_desc_length(uint16_t const * p_str)
  151. {
  152. return ((const app_usbd_descriptor_string_t *)p_str)->bLength;
  153. }
  154. /** @} */
  155. #ifdef __cplusplus
  156. }
  157. #endif
  158. #endif /* APP_USBD_STRING_DESC_H__ */