protocol.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "protocol.h"
  2. #include "process.h"
  3. #include "cfg.h"
  4. #include "uart.h"
  5. #ifdef IS_BOOTLOADER
  6. uint32_t Firmware_Version[4] = {1, 0, 1, 20240129};
  7. #else
  8. uint32_t Firmware_Version[4] = {1, 1, 5, 20240125};
  9. #endif
  10. uint16_t Read_FirmwareVersion(uint8_t *pBuf, uint16_t buf_len)
  11. {
  12. int i;
  13. if( buf_len < 16){
  14. return 0;
  15. }
  16. for (i = 0; i < 4; ++i)
  17. {
  18. pBuf[i * 4] = (Firmware_Version[i] >> 24) & 0xff;
  19. pBuf[i * 4 + 1] = (Firmware_Version[i] >> 16) & 0xff;
  20. pBuf[i * 4 + 2] = (Firmware_Version[i] >> 8) & 0xff;
  21. pBuf[i * 4 + 3] = (Firmware_Version[i] >> 0) & 0xff;
  22. }
  23. return 16;
  24. }
  25. uint16_t Read_HardwareVersion(uint8_t *pBuf, uint16_t buf_len)
  26. {
  27. if( buf_len < 2){
  28. return 0;
  29. }
  30. pBuf[0] = (config->hw_version >> 8)&0xff;
  31. pBuf[1] = (config->hw_version >> 0)&0xff;
  32. return 2;
  33. }
  34. uint16_t Read_Deviceid(uint8_t *pBuf, uint16_t buf_len)
  35. {
  36. if( buf_len < 4){
  37. return 0;
  38. }
  39. pBuf[0] = (config->deviceid >> 24)&0xff;
  40. pBuf[1] = (config->deviceid >> 16)&0xff;
  41. pBuf[2] = (config->deviceid >> 8)&0xff;
  42. pBuf[3] = (config->deviceid >> 0)&0xff;
  43. return 4;
  44. }
  45. uint16_t Read_Devicetype(uint8_t *pBuf, uint16_t buf_len)
  46. {
  47. if( buf_len < 2){
  48. return 0;
  49. }
  50. pBuf[0] = (config->devicetype >> 8)&0xff;
  51. pBuf[1] = (config->devicetype >> 0)&0xff;
  52. return 2;
  53. }
  54. uint16_t Read_Addr(uint8_t *pBuf, uint16_t buf_len)
  55. {
  56. if( buf_len < 2){
  57. return 0;
  58. }
  59. pBuf[0] = 0x00;
  60. pBuf[1] = config->addr;
  61. return 2;
  62. }
  63. uint16_t Read_Baudrate(uint8_t *pBuf, uint16_t buf_len)
  64. {
  65. if( buf_len < 2){
  66. return 0;
  67. }
  68. pBuf[0] = 0x00;
  69. pBuf[1] = config->br_index;
  70. return 2;
  71. }
  72. uint16_t Read_BvStatus(uint8_t *pBuf, uint16_t buf_len)
  73. {
  74. if( buf_len < 2){
  75. return 0;
  76. }
  77. pBuf[0] = 0x00;
  78. pBuf[1] = g_bvopen;
  79. return 2;
  80. }
  81. /*=======================================================================================*/
  82. uint8_t Write_Addr(uint8_t *pdata, uint8_t len)
  83. {
  84. if(len == 2){
  85. config->addr = pdata[1];
  86. return RET_OK|RET_NEED_SAVE;
  87. }else{
  88. return RET_DATAINVALID;
  89. }
  90. }
  91. uint8_t Write_Baudrate(uint8_t *pdata, uint8_t len)
  92. {
  93. if(len == 2){
  94. if(pdata[1] >= BaudRate_4800 && pdata[1] <= BaudRate_230400){
  95. config->br_index = pdata[1];
  96. return RET_OK|RET_NEED_SAVE;
  97. }else{
  98. return RET_DATAINVALID;
  99. }
  100. }else{
  101. return RET_DATAINVALID;
  102. }
  103. }
  104. uint8_t Write_HardwareVersion(uint8_t *pdata, uint8_t len)
  105. {
  106. if(len == 2){
  107. config->hw_version = ((uint16_t)pdata[0]<<8) | pdata[1];
  108. return RET_OK|RET_NEED_SAVE;
  109. }else{
  110. return RET_DATAINVALID;
  111. }
  112. }
  113. uint8_t Write_Deviceid(uint8_t *pdata, uint8_t len)
  114. {
  115. if(len == 4){
  116. config->deviceid = ((uint32_t)pdata[0]<<24) | ((uint32_t)pdata[1]<<16)| ((uint32_t)pdata[2]<<8)| pdata[3];
  117. return RET_OK|RET_NEED_SAVE;
  118. }else{
  119. return RET_DATAINVALID;
  120. }
  121. }
  122. uint8_t Write_Devicetype(uint8_t *pdata, uint8_t len)
  123. {
  124. if(len == 2){
  125. config->devicetype = ((uint16_t)pdata[0]<<8) | pdata[1];
  126. return RET_OK|RET_NEED_SAVE;
  127. }else{
  128. return RET_DATAINVALID;
  129. }
  130. }
  131. //电磁阀控制
  132. uint8_t Control_SValve(uint8_t *pdata, uint8_t len)
  133. {
  134. if(len == 2){
  135. if(pdata[1] == 0x00){
  136. sv_close();
  137. }else{
  138. sv_open();
  139. }
  140. return RET_OK;
  141. }else{
  142. return RET_DATAINVALID;
  143. }
  144. }
  145. //语音输出
  146. uint8_t Trigger_VoicOutput(uint8_t *pdata, uint8_t len)
  147. {
  148. if(len == 2){
  149. g_trigger_vo = VO_COUNT_DFTT;
  150. return RET_OK;
  151. }else{
  152. return RET_DATAINVALID;
  153. }
  154. }
  155. //发送原始数据
  156. uint8_t Trigger_SendRaw(uint8_t *pdata, uint8_t len)
  157. {
  158. if(len == 2){
  159. if(pdata[1] == 0x00){
  160. g_send_raw = 0;
  161. }else{
  162. g_send_raw = 1;
  163. g_send_sequence = 0;
  164. }
  165. return RET_OK;
  166. }else{
  167. return RET_DATAINVALID;
  168. }
  169. }
  170. //发送滤波后数据
  171. uint8_t Trigger_SendFiltered(uint8_t *pdata, uint8_t len)
  172. {
  173. if(len == 2){
  174. if(pdata[1] == 0x00){
  175. g_send_filtered = 0;
  176. }else{
  177. g_send_filtered = 1;
  178. g_send_sequence = 0;
  179. }
  180. return RET_OK;
  181. }else{
  182. return RET_DATAINVALID;
  183. }
  184. }