diag.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 _DIAG_H_
  13. #define _DIAG_H_
  14. #include "diag_config.h"
  15. #include "drv_debug_port.h"
  16. #include "osi_compiler.h"
  17. OSI_EXTERN_C_BEGIN
  18. #define LOG_TAG_DIAG OSI_MAKE_LOG_TAG('D', 'I', 'A', 'G')
  19. #define DIAG_MCU_READ_MAX_SIZE 2048
  20. #define DIAG_ENC_DATA_STATIC_SIZE (DIAG_MCU_READ_MAX_SIZE << 1)
  21. /**
  22. * @brief diag command header
  23. *
  24. * The diag command header is common. The data after the heade is
  25. * called diag command data. The diag command header and the followed
  26. * command data is called command packet. \p len is the total length
  27. * of diag packet.
  28. *
  29. * When pointer of \p diagMsgHead_t is used ad parameter, it can be safely
  30. * assumed that the pointer is 4 bytes aligned. The memory from the
  31. * beginning of command header with size of \p diagMsgHead_t.len is
  32. * accessible.
  33. */
  34. typedef struct diagMsgHead
  35. {
  36. uint32_t seq_num; ///< Message sequence number, used for flow control
  37. uint16_t len; ///< The totoal size of the packet
  38. uint8_t type; ///< Main command type
  39. uint8_t subtype; ///< Sub command type
  40. } diagMsgHead_t;
  41. /**
  42. * @brief diag command handle function type
  43. *
  44. * @param cmd the diag command packet
  45. * @param ctx caller context
  46. * @return
  47. * - true if the command is handled
  48. * - false if the command isn't handled
  49. */
  50. typedef bool (*diagCmdHandle_t)(const diagMsgHead_t *cmd, void *ctx);
  51. /**
  52. * \brief diag device type
  53. */
  54. typedef enum
  55. {
  56. DIAG_DEVICE_UNKNOWN = 0, ///< unknown
  57. DIAG_DEVICE_UART, ///< uart
  58. DIAG_DEVICE_USERIAL, ///< usb serial
  59. } diagDevType_t;
  60. /**
  61. * @brief get diag command data from command header
  62. *
  63. * @param cmd the diag command packet
  64. * @return the diag command data
  65. */
  66. static inline const void *diagCmdData(const diagMsgHead_t *cmd) { return cmd + 1; }
  67. /**
  68. * @brief get diag command data size from command header
  69. *
  70. * @param cmd the diag command packet
  71. * @return the diag command data size
  72. */
  73. static inline unsigned diagCmdDataSize(const diagMsgHead_t *cmd) { return cmd->len - sizeof(diagMsgHead_t); }
  74. /**
  75. * \brief diag device name
  76. *
  77. * It may be affected by configuration and sysnv.
  78. *
  79. * \return diag device name
  80. */
  81. unsigned diagDeviceName(void);
  82. /**
  83. * @brief initialize diag module
  84. */
  85. void diagInit(drvDebugPort_t *port);
  86. /**
  87. * @brief register diag command handler by type
  88. *
  89. * @param type main command type
  90. * @param handler diag command handler
  91. * @param ctx caller context
  92. * @return
  93. * - true on success
  94. * - false if \p type is invalid
  95. */
  96. bool diagRegisterCmdHandle(uint8_t type, diagCmdHandle_t handler, void *ctx);
  97. /**
  98. * @brief unregister diag command type handler
  99. *
  100. * @param type main command type
  101. * @return
  102. * - true on success
  103. * - false if \p type is invalid
  104. */
  105. bool diagUnregisterCmdHandle(uint8_t type);
  106. /**
  107. * @brief output diag packet
  108. *
  109. * \p data should be a complete diag packet.
  110. *
  111. * @param data diag packet pointer
  112. * @param size diag packet size
  113. * @return
  114. * - true on success
  115. * - false if \p data is NULL or size is zero
  116. */
  117. bool diagOutputPacket(const void *data, unsigned size);
  118. /**
  119. * @brief output diag packet, header followed by data
  120. *
  121. * \p size is permitted to be zero. That means there are no data.
  122. *
  123. * This just to make diag output easier. When there are data following
  124. * command header, it is not needed to allocate memory for the whole
  125. * packet, and concate the header and data together.
  126. *
  127. * \p len in the header will be changed to \code sizeof(diagMsgHead_t) + size \endcode.
  128. *
  129. * @param cmd diag command header, must be valid
  130. * @param data diag data following header, must be valid when \p size if not zero
  131. * @param size diag data size
  132. * @return
  133. * - true on success
  134. * - false if \p head is NULL
  135. */
  136. bool diagOutputPacket2(const diagMsgHead_t *cmd, const void *data, unsigned size);
  137. /**
  138. * @brief output diag packet, header followed by sub-header and data
  139. *
  140. * \p sub_header_size is permitted to be zero. That means there are no sub-header.
  141. *
  142. * \p size is permitted to be zero. That means there are no data.
  143. *
  144. * This just to make diag output easier. When there are sub-header and data
  145. * following command header, it is not needed to concate them together.
  146. *
  147. * \p len in the header will be changed to
  148. * \code sizeof(diagMsgHead_t) + sub_header_size + size \endcode.
  149. *
  150. * @param cmd diag command header, must be valid
  151. * @param sub_header sub-header, must be valid when \p sub_header_size if not zero
  152. * @param sub_header_size sub-header size
  153. * @param size diag data size
  154. * @param data diag data following sub-header, must be valid when \p size if not zero
  155. * @param size diag data size
  156. * @return
  157. * - true on success
  158. * - false if \p head is NULL
  159. */
  160. bool diagOutputPacket3(const diagMsgHead_t *cmd, const void *sub_header, unsigned sub_header_size, const void *data, unsigned size);
  161. /**
  162. * @brief output response for bad command
  163. *
  164. * @param cmd diag command header, must be valid
  165. */
  166. void diagBadCommand(const diagMsgHead_t *cmd);
  167. OSI_EXTERN_C_END
  168. #endif