protocol.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. #include "protocol.h"
  2. #include "process.h"
  3. #include "cfg.h"
  4. #include "uart.h"
  5. #include "Motor.h"
  6. #include "AngleSensor.h"
  7. #include "Rtcx.h"
  8. #include "storage.h"
  9. static DataItem tmp_record;
  10. static uint8_t tmp_i=0;
  11. #ifdef IS_BOOTLOADER
  12. uint32_t Firmware_Version[4] = {1, 0, 0, 20240802};
  13. #else
  14. uint32_t Firmware_Version[4] = {1, 1, 1, 20240803};
  15. #endif
  16. uint16_t Read_FirmwareVersion(uint8_t *pBuf, uint16_t buf_len)
  17. {
  18. int i;
  19. if( buf_len < 16){
  20. return 0;
  21. }
  22. for (i = 0; i < 4; ++i)
  23. {
  24. pBuf[i * 4] = (Firmware_Version[i] >> 24) & 0xff;
  25. pBuf[i * 4 + 1] = (Firmware_Version[i] >> 16) & 0xff;
  26. pBuf[i * 4 + 2] = (Firmware_Version[i] >> 8) & 0xff;
  27. pBuf[i * 4 + 3] = (Firmware_Version[i] >> 0) & 0xff;
  28. }
  29. return 16;
  30. }
  31. uint16_t Read_HardwareVersion(uint8_t *pBuf, uint16_t buf_len)
  32. {
  33. if( buf_len < 2){
  34. return 0;
  35. }
  36. pBuf[0] = (config->hw_version >> 8)&0xff;
  37. pBuf[1] = (config->hw_version >> 0)&0xff;
  38. return 2;
  39. }
  40. uint16_t Read_Deviceid(uint8_t *pBuf, uint16_t buf_len)
  41. {
  42. if( buf_len < 4){
  43. return 0;
  44. }
  45. pBuf[0] = (config->deviceid >> 24)&0xff;
  46. pBuf[1] = (config->deviceid >> 16)&0xff;
  47. pBuf[2] = (config->deviceid >> 8)&0xff;
  48. pBuf[3] = (config->deviceid >> 0)&0xff;
  49. return 4;
  50. }
  51. uint16_t Read_Devicetype(uint8_t *pBuf, uint16_t buf_len)
  52. {
  53. if( buf_len < 2){
  54. return 0;
  55. }
  56. pBuf[0] = (config->devicetype >> 8)&0xff;
  57. pBuf[1] = (config->devicetype >> 0)&0xff;
  58. return 2;
  59. }
  60. uint16_t Read_Addr(uint8_t *pBuf, uint16_t buf_len)
  61. {
  62. if( buf_len < 2){
  63. return 0;
  64. }
  65. pBuf[0] = 0x00;
  66. pBuf[1] = config->addr;
  67. return 2;
  68. }
  69. uint16_t Read_Baudrate(uint8_t *pBuf, uint16_t buf_len)
  70. {
  71. if( buf_len < 2){
  72. return 0;
  73. }
  74. pBuf[0] = 0x00;
  75. pBuf[1] = config->br_index;
  76. return 2;
  77. }
  78. //读取锁状态
  79. uint16_t Read_LockStatus(uint8_t *pBuf, uint16_t buf_len)
  80. {
  81. if( buf_len < 2){
  82. return 0;
  83. }
  84. pBuf[0] = Process_GetLockStatus(); //锁状态
  85. pBuf[1] = Process_GetCoverStatus(); //上盖状态
  86. return 2;
  87. }
  88. //读取开锁、上锁阀值
  89. uint16_t Read_Threshold(uint8_t *pBuf, uint16_t buf_len)
  90. {
  91. if( buf_len < 8){
  92. return 0;
  93. }
  94. pBuf[0] = ((uint8_t*)(&config->unlock_threshold))[0];
  95. pBuf[1] = ((uint8_t*)(&config->unlock_threshold))[1];
  96. pBuf[2] = ((uint8_t*)(&config->unlock_threshold))[2];
  97. pBuf[3] = ((uint8_t*)(&config->unlock_threshold))[3];
  98. pBuf[4] = ((uint8_t*)(&config->lock_threshold))[0];
  99. pBuf[5] = ((uint8_t*)(&config->lock_threshold))[1];
  100. pBuf[6] = ((uint8_t*)(&config->lock_threshold))[2];
  101. pBuf[7] = ((uint8_t*)(&config->lock_threshold))[3];
  102. return 8;
  103. }
  104. //读取编码器角度值
  105. uint16_t Read_Angle(uint8_t *pBuf, uint16_t buf_len)
  106. {
  107. if( buf_len < 4){
  108. return 0;
  109. }
  110. float angle = AngleSensor_GetAngle();
  111. pBuf[0] = ((uint8_t*)(&angle))[0];
  112. pBuf[1] = ((uint8_t*)(&angle))[1];
  113. pBuf[2] = ((uint8_t*)(&angle))[2];
  114. pBuf[3] = ((uint8_t*)(&angle))[3];
  115. return 4;
  116. }
  117. //读取设备日期时间
  118. uint16_t Read_DateTime(uint8_t *pBuf, uint16_t buf_len)
  119. {
  120. if( buf_len < 8){
  121. return 0;
  122. }
  123. RTCx_UpdateTime();
  124. pBuf[0] = g_dateTime.year-2000;
  125. pBuf[1] = g_dateTime.month;
  126. pBuf[2] = g_dateTime.day;
  127. pBuf[3] = g_dateTime.hour;
  128. pBuf[4] = g_dateTime.minute;
  129. pBuf[5] = g_dateTime.second;
  130. pBuf[6] = g_dateTime.week;
  131. pBuf[7] = 0x00;
  132. return 8;
  133. }
  134. //读取设备历史记录条数
  135. uint16_t Read_RecordsNum(uint8_t *pBuf, uint16_t buf_len)
  136. {
  137. if( buf_len < 2){
  138. return 0;
  139. }
  140. uint16_t num = Storage_GetItemNum();
  141. pBuf[0] = (num>>8)&0xFF;
  142. pBuf[1] = num&0xFF;
  143. return 2;
  144. }
  145. //读取历史记录
  146. uint16_t Read_Records(uint8_t num, uint8_t *pBuf, uint16_t buf_len)
  147. {
  148. if((num > MAX_RECORDS_PERTIME) ||(buf_len < SIZE_RECORD*num)){
  149. return 0;
  150. }
  151. if(num > Storage_GetItemNum()){
  152. //return 0;
  153. num = Storage_GetItemNum();
  154. }
  155. for(tmp_i=0; tmp_i < num; tmp_i++){
  156. if(0 == Storage_GetItemFromOldest2(tmp_i, &tmp_record))
  157. {
  158. pBuf[tmp_i*SIZE_RECORD+0] = tmp_record.itemtype;
  159. pBuf[tmp_i*SIZE_RECORD+1] = tmp_record.eventtype;
  160. pBuf[tmp_i*SIZE_RECORD+2] = tmp_record.timestamp[0];
  161. pBuf[tmp_i*SIZE_RECORD+3] = tmp_record.timestamp[1];
  162. pBuf[tmp_i*SIZE_RECORD+4] = tmp_record.timestamp[2];
  163. pBuf[tmp_i*SIZE_RECORD+5] = tmp_record.timestamp[3];
  164. pBuf[tmp_i*SIZE_RECORD+6] = tmp_record.timestamp[4];
  165. pBuf[tmp_i*SIZE_RECORD+7] = tmp_record.timestamp[5];
  166. }else{
  167. break;
  168. }
  169. }
  170. Storage_WaitDelete(tmp_i);
  171. return tmp_i*SIZE_RECORD;
  172. }
  173. /*=======================================================================================*/
  174. uint8_t Write_Addr(uint8_t *pdata, uint8_t len)
  175. {
  176. if(len == 2){
  177. config->addr = pdata[1];
  178. return RET_OK|RET_NEED_SAVE;
  179. }else{
  180. return RET_DATAINVALID;
  181. }
  182. }
  183. uint8_t Write_Baudrate(uint8_t *pdata, uint8_t len)
  184. {
  185. if(len == 2){
  186. if(pdata[1] >= BaudRate_4800 && pdata[1] <= BaudRate_230400){
  187. config->br_index = pdata[1];
  188. return RET_OK|RET_NEED_SAVE;
  189. }else{
  190. return RET_DATAINVALID;
  191. }
  192. }else{
  193. return RET_DATAINVALID;
  194. }
  195. }
  196. uint8_t Write_HardwareVersion(uint8_t *pdata, uint8_t len)
  197. {
  198. if(len == 2){
  199. config->hw_version = ((uint16_t)pdata[0]<<8) | pdata[1];
  200. return RET_OK|RET_NEED_SAVE;
  201. }else{
  202. return RET_DATAINVALID;
  203. }
  204. }
  205. uint8_t Write_Deviceid(uint8_t *pdata, uint8_t len)
  206. {
  207. if(len == 4){
  208. config->deviceid = ((uint32_t)pdata[0]<<24) | ((uint32_t)pdata[1]<<16)| ((uint32_t)pdata[2]<<8)| pdata[3];
  209. return RET_OK|RET_NEED_SAVE;
  210. }else{
  211. return RET_DATAINVALID;
  212. }
  213. }
  214. uint8_t Write_Devicetype(uint8_t *pdata, uint8_t len)
  215. {
  216. if(len == 2){
  217. config->devicetype = ((uint16_t)pdata[0]<<8) | pdata[1];
  218. return RET_OK|RET_NEED_SAVE;
  219. }else{
  220. return RET_DATAINVALID;
  221. }
  222. }
  223. //开锁、上锁操作
  224. uint8_t Write_LockOp(uint8_t *pdata, uint8_t len)
  225. {
  226. if(len == 2){
  227. if(0x01 == pdata[0]){ //开锁
  228. Process_OpUnlock(pdata[1]);
  229. }else if(0x02 == pdata[0]){ //上锁
  230. Process_OpLock(pdata[1]);
  231. }else if(0x03 == pdata[0]){ //正转测试
  232. Motor_Positive(pdata[1]);
  233. }else if(0x04 == pdata[0]){ //反转测试
  234. Motor_Negative(pdata[1]);
  235. }
  236. return RET_OK;
  237. }else{
  238. return RET_DATAINVALID;
  239. }
  240. }
  241. //开锁阀值、上锁阀值标定
  242. uint8_t Write_ThresholdCalibration(uint8_t *pdata, uint8_t len)
  243. {
  244. if(len == 2){
  245. if(0x01 == pdata[1]){ //开锁
  246. Process_CalibrationThreshold(0);
  247. }else if(0x02 == pdata[1]){ //上锁
  248. Process_CalibrationThreshold(1);
  249. }
  250. return RET_OK|RET_NEED_SAVE;
  251. }else{
  252. return RET_DATAINVALID;
  253. }
  254. }
  255. //标定设备日期时间
  256. uint8_t Write_DateTime(uint8_t *pdata, uint8_t len)
  257. {
  258. static DateTime dateTime;
  259. if(len == 8){
  260. dateTime.year = pdata[0];
  261. dateTime.year += 2000;
  262. dateTime.month = pdata[1];
  263. dateTime.day = pdata[2];
  264. dateTime.hour = pdata[3];
  265. dateTime.minute = pdata[4];
  266. dateTime.second = pdata[5];
  267. dateTime.week = pdata[6];
  268. RTCx_SetTime(&dateTime);
  269. return RET_OK;
  270. }else{
  271. return RET_DATAINVALID;
  272. }
  273. }
  274. //删除已读取设备历史记录
  275. uint8_t Write_RecordsDelete(uint8_t *pdata, uint8_t len)
  276. {
  277. if(len == 2){
  278. Storage_DeleteConfirm();
  279. return RET_OK;
  280. }else{
  281. return RET_DATAINVALID;
  282. }
  283. }