protocol.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 "Ds1302.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, 20240711};
  13. #else
  14. uint32_t Firmware_Version[4] = {1, 1, 1, 20240711};
  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 < 6){
  121. return 0;
  122. }
  123. DS1302_UpdateTime();
  124. pBuf[0] = g_dateTime.year;
  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. return 6;
  131. }
  132. //读取设备历史记录条数
  133. uint16_t Read_RecordsNum(uint8_t *pBuf, uint16_t buf_len)
  134. {
  135. if( buf_len < 2){
  136. return 0;
  137. }
  138. uint16_t num = Storage_GetItemNum();
  139. pBuf[0] = (num>>8)&0xFF;
  140. pBuf[1] = num&0xFF;
  141. return 2;
  142. }
  143. //读取历史记录
  144. uint16_t Read_Records(uint8_t num, uint8_t *pBuf, uint16_t buf_len)
  145. {
  146. if((num > MAX_RECORDS_PERTIME) ||(buf_len < SIZE_RECORD*num)){
  147. return 0;
  148. }
  149. if(num > Storage_GetItemNum()){
  150. //return 0;
  151. num = Storage_GetItemNum();
  152. }
  153. for(tmp_i=0; tmp_i < num; tmp_i++){
  154. if(0 == Storage_GetItemFromOldest2(tmp_i, &tmp_record))
  155. {
  156. pBuf[tmp_i*SIZE_RECORD+0] = tmp_record.itemtype;
  157. pBuf[tmp_i*SIZE_RECORD+1] = tmp_record.eventtype;
  158. pBuf[tmp_i*SIZE_RECORD+2] = tmp_record.timestamp[0];
  159. pBuf[tmp_i*SIZE_RECORD+3] = tmp_record.timestamp[1];
  160. pBuf[tmp_i*SIZE_RECORD+4] = tmp_record.timestamp[2];
  161. pBuf[tmp_i*SIZE_RECORD+5] = tmp_record.timestamp[3];
  162. pBuf[tmp_i*SIZE_RECORD+6] = tmp_record.timestamp[4];
  163. pBuf[tmp_i*SIZE_RECORD+7] = tmp_record.timestamp[5];
  164. }else{
  165. break;
  166. }
  167. }
  168. Storage_WaitDelete(tmp_i);
  169. return tmp_i*SIZE_RECORD;
  170. }
  171. /*=======================================================================================*/
  172. uint8_t Write_Addr(uint8_t *pdata, uint8_t len)
  173. {
  174. if(len == 2){
  175. config->addr = pdata[1];
  176. return RET_OK|RET_NEED_SAVE;
  177. }else{
  178. return RET_DATAINVALID;
  179. }
  180. }
  181. uint8_t Write_Baudrate(uint8_t *pdata, uint8_t len)
  182. {
  183. if(len == 2){
  184. if(pdata[1] >= BaudRate_4800 && pdata[1] <= BaudRate_230400){
  185. config->br_index = pdata[1];
  186. return RET_OK|RET_NEED_SAVE;
  187. }else{
  188. return RET_DATAINVALID;
  189. }
  190. }else{
  191. return RET_DATAINVALID;
  192. }
  193. }
  194. uint8_t Write_HardwareVersion(uint8_t *pdata, uint8_t len)
  195. {
  196. if(len == 2){
  197. config->hw_version = ((uint16_t)pdata[0]<<8) | pdata[1];
  198. return RET_OK|RET_NEED_SAVE;
  199. }else{
  200. return RET_DATAINVALID;
  201. }
  202. }
  203. uint8_t Write_Deviceid(uint8_t *pdata, uint8_t len)
  204. {
  205. if(len == 4){
  206. config->deviceid = ((uint32_t)pdata[0]<<24) | ((uint32_t)pdata[1]<<16)| ((uint32_t)pdata[2]<<8)| pdata[3];
  207. return RET_OK|RET_NEED_SAVE;
  208. }else{
  209. return RET_DATAINVALID;
  210. }
  211. }
  212. uint8_t Write_Devicetype(uint8_t *pdata, uint8_t len)
  213. {
  214. if(len == 2){
  215. config->devicetype = ((uint16_t)pdata[0]<<8) | pdata[1];
  216. return RET_OK|RET_NEED_SAVE;
  217. }else{
  218. return RET_DATAINVALID;
  219. }
  220. }
  221. //开锁、上锁操作
  222. uint8_t Write_LockOp(uint8_t *pdata, uint8_t len)
  223. {
  224. if(len == 2){
  225. if(0x01 == pdata[0]){ //开锁
  226. Process_OpUnlock(pdata[1]);
  227. }else if(0x02 == pdata[0]){ //上锁
  228. Process_OpLock(pdata[1]);
  229. }else if(0x03 == pdata[0]){ //正转测试
  230. Motor_Positive(pdata[1]);
  231. }else if(0x04 == pdata[0]){ //反转测试
  232. Motor_Negative(pdata[1]);
  233. }
  234. return RET_OK;
  235. }else{
  236. return RET_DATAINVALID;
  237. }
  238. }
  239. //开锁阀值、上锁阀值标定
  240. uint8_t Write_ThresholdCalibration(uint8_t *pdata, uint8_t len)
  241. {
  242. if(len == 2){
  243. if(0x01 == pdata[1]){ //开锁
  244. Process_CalibrationThreshold(0);
  245. }else if(0x02 == pdata[1]){ //上锁
  246. Process_CalibrationThreshold(1);
  247. }
  248. return RET_OK|RET_NEED_SAVE;
  249. }else{
  250. return RET_DATAINVALID;
  251. }
  252. }
  253. //标定设备日期时间
  254. uint8_t Write_DateTime(uint8_t *pdata, uint8_t len)
  255. {
  256. if(len == 6){
  257. g_dateTime.year = pdata[0];
  258. g_dateTime.month = pdata[1];
  259. g_dateTime.day = pdata[2];
  260. g_dateTime.hour = pdata[3];
  261. g_dateTime.minute = pdata[4];
  262. g_dateTime.second = pdata[5];
  263. return RET_OK;
  264. }else{
  265. return RET_DATAINVALID;
  266. }
  267. }
  268. //删除已读取设备历史记录
  269. uint8_t Write_RecordsDelete(uint8_t *pdata, uint8_t len)
  270. {
  271. if(len == 2){
  272. Storage_DeleteConfirm();
  273. return RET_OK;
  274. }else{
  275. return RET_DATAINVALID;
  276. }
  277. }