internalComm.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. /*
  2. * internalComm.c
  3. *
  4. * Created on: 2019年5月7日
  5. * Author: foluswen
  6. */
  7. #include <stdio.h> /*標準輸入輸出定義*/
  8. #include <stdlib.h> /*標準函數庫定義*/
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <termios.h>
  13. #include "internalComm.h"
  14. #include "../Config.h"
  15. #include "../Log/log.h"
  16. //------------------------------------------------------------------------------
  17. int tranceiveRelDelayTime(int fd, uint8_t *cmd, uint8_t cmd_len, uint8_t *rx, uint16_t _delay)
  18. {
  19. int len;
  20. //sleep(2); //required to make flush work, for some reason
  21. tcflush(fd, TCIOFLUSH);
  22. if (write(fd, cmd, cmd_len) >= cmd_len) {
  23. usleep(_delay * 1000);
  24. len = read(fd, rx, 512);
  25. } else {
  26. #ifdef SystemLogMessage
  27. DEBUG_ERROR("Serial command %s response fail.\n", cmd);
  28. #endif
  29. }
  30. return len;
  31. }
  32. int tranceive(int fd, uint8_t *cmd, uint8_t cmd_len, uint8_t *rx)
  33. {
  34. int len;
  35. //sleep(2); //required to make flush work, for some reason
  36. tcflush(fd, TCIOFLUSH);
  37. if (write(fd, cmd, cmd_len) >= cmd_len) {
  38. usleep(15000);
  39. len = read(fd, rx, 512);
  40. } else {
  41. #ifdef SystemLogMessage
  42. DEBUG_ERROR("Serial command %s response fail.\n", cmd);
  43. #endif
  44. }
  45. return len;
  46. }
  47. //------------------------------------------------------------------------------
  48. //===== Query =====
  49. //------------------------------------------------------------------------------
  50. int Query_FW_Ver(uint8_t fd, uint8_t targetAddr, Ver *Ret_Buf)
  51. {
  52. int result = FAIL;
  53. uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_FW_VER, 0x00, 0x00, 0x00};
  54. uint8_t rx[512];
  55. uint8_t chksum = 0x00;
  56. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  57. // for (int i = 0; i < 7; i++)
  58. // printf("tx = %x \n", tx[i]);
  59. // for (int i = 0; i < len; i++)
  60. // printf("rx = %x \n", rx[i]);
  61. if (len > 6) {
  62. if (len < 6 + (rx[4] | rx[5] << 8)) {
  63. return result;
  64. }
  65. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  66. chksum ^= rx[6 + idx];
  67. }
  68. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  69. (rx[2] == tx[1]) &&
  70. (rx[1] == tx[2]) &&
  71. (rx[3] == tx[3])) {
  72. memcpy(Ret_Buf->Version_FW, (char *)rx + 6, (rx[4] | rx[5] << 8));
  73. *(Ret_Buf->Version_FW + 8) = 0x00;
  74. result = PASS;
  75. }
  76. }
  77. return result;
  78. }
  79. int Query_HW_Ver(uint8_t fd, uint8_t targetAddr, Ver *Ret_Buf)
  80. {
  81. int result = FAIL;
  82. uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_HW_VER, 0x00, 0x00, 0x00};
  83. uint8_t rx[512];
  84. uint8_t chksum = 0x00;
  85. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  86. if (len > 6) {
  87. if (len < 6 + (rx[4] | rx[5] << 8)) {
  88. return result;
  89. }
  90. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  91. chksum ^= rx[6 + idx];
  92. }
  93. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  94. (rx[2] == tx[1]) &&
  95. (rx[1] == tx[2]) &&
  96. (rx[3] == tx[3])) {
  97. memcpy(Ret_Buf->Version_HW, (char *)rx + 6, (rx[4] | rx[5] << 8));
  98. *(Ret_Buf->Version_HW + 8) = 0x00;
  99. result = PASS;
  100. }
  101. }
  102. return result;
  103. }
  104. int Query_Present_InputVoltage(uint8_t fd, uint8_t targetAddr, PresentInputVoltage *Ret_Buf)
  105. {
  106. int result = FAIL;
  107. uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_PRESENT_IN_VOL, 0x00, 0x00, 0x00};
  108. uint8_t rx[512];
  109. uint8_t chksum = 0x00;
  110. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  111. if (len > 13) {
  112. if (len < 6 + (rx[4] | rx[5] << 8)) {
  113. return result;
  114. }
  115. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  116. chksum ^= rx[6 + idx];
  117. }
  118. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  119. (rx[2] == tx[1]) &&
  120. (rx[1] == tx[2]) &&
  121. (rx[3] == tx[3]) &&
  122. chksum != 0) {
  123. Ret_Buf->inputType = rx[6];
  124. Ret_Buf->L1N_L12 = (rx[7] | (rx[8] << 8)) / 10.0;
  125. Ret_Buf->L2N_L23 = (rx[9] | (rx[10] << 8)) / 10.0;
  126. Ret_Buf->L3N_L31 = (rx[11] | (rx[12] << 8)) / 10.0;
  127. if (Ret_Buf->L1N_L12 >= 320 ||
  128. Ret_Buf->L2N_L23 >= 320 ||
  129. Ret_Buf->L3N_L31 >= 320) {
  130. result = FAIL;
  131. } else {
  132. result = PASS;
  133. }
  134. }
  135. }
  136. return result;
  137. }
  138. int Query_Present_OutputVoltage(uint8_t fd, uint8_t targetAddr, PresentOutputVoltage *Ret_Buf)
  139. {
  140. int result = FAIL;
  141. uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_PRESENT_OUT_VOL, 0x00, 0x00, 0x00};
  142. uint8_t rx[512];
  143. uint8_t chksum = 0x00;
  144. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  145. if (len > 6) {
  146. if (len < 6 + (rx[4] | rx[5] << 8)) {
  147. return result;
  148. }
  149. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  150. chksum ^= rx[6 + idx];
  151. }
  152. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  153. (rx[2] == tx[1]) &&
  154. (rx[1] == tx[2]) &&
  155. (rx[3] == tx[3])) {
  156. Ret_Buf->behindFuse_Voltage_C1 = (rx[6] | (rx[7] << 8));
  157. Ret_Buf->behindRelay_Voltage_C1 = (rx[8] | (rx[9] << 8));
  158. if ((rx[4] | rx[5] << 8) > 4) {
  159. Ret_Buf->behindFuse_Voltage_C2 = (rx[10] | (rx[11] << 8));
  160. Ret_Buf->behindRelay_Voltage_C2 = (rx[12] | (rx[13] << 8));
  161. }
  162. result = PASS;
  163. }
  164. }
  165. return result;
  166. }
  167. int Query_Fan_Speed(uint8_t fd, uint8_t targetAddr, FanSpeed *Ret_Buf)
  168. {
  169. int result = FAIL;
  170. uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_FAN_SPEED, 0x00, 0x00, 0x00};
  171. uint8_t rx[512];
  172. uint8_t chksum = 0x00;
  173. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  174. if (len > 6) {
  175. if (len < 6 + (rx[4] | rx[5] << 8)) {
  176. return result;
  177. }
  178. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  179. chksum ^= rx[6 + idx];
  180. }
  181. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  182. (rx[2] == tx[1]) &&
  183. (rx[1] == tx[2]) &&
  184. (rx[3] == tx[3])) {
  185. for (int idx = 0; idx < 4; idx++) {
  186. Ret_Buf->speed[idx] = (rx[6 + (2 * idx)] | (rx[6 + (2 * idx) + 1] << 8));
  187. }
  188. result = PASS;
  189. }
  190. }
  191. return result;
  192. }
  193. int Query_Temperature(uint8_t fd, uint8_t targetAddr, Temperature *Ret_Buf)
  194. {
  195. int result = FAIL;
  196. uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_TEMPERATURE, 0x00, 0x00, 0x00};
  197. uint8_t rx[512];
  198. uint8_t chksum = 0x00;
  199. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  200. if (len > 6) {
  201. if (len < 6 + (rx[4] | rx[5] << 8)) {
  202. return result;
  203. }
  204. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  205. chksum ^= rx[6 + idx];
  206. }
  207. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  208. (rx[2] == tx[1]) &&
  209. (rx[1] == tx[2]) &&
  210. (rx[3] == tx[3])) {
  211. for (int idx = 0; idx < 4; idx++) {
  212. Ret_Buf->temperature[idx] = rx[6 + idx] - 60;
  213. }
  214. result = PASS;
  215. }
  216. }
  217. return result;
  218. }
  219. int Query_Aux_PowerVoltage(uint8_t fd, uint8_t targetAddr, AuxPower *Ret_Buf)
  220. {
  221. int result = FAIL;
  222. uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AUX_POWER_VOL, 0x00, 0x00, 0x00};
  223. uint8_t rx[512];
  224. uint8_t chksum = 0x00;
  225. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  226. if (len > 6) {
  227. if (len < 6 + (rx[4] | rx[5] << 8)) {
  228. return result;
  229. }
  230. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  231. chksum ^= rx[6 + idx];
  232. }
  233. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  234. (rx[2] == tx[1]) &&
  235. (rx[1] == tx[2]) &&
  236. (rx[3] == tx[3])) {
  237. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  238. Ret_Buf->voltage[idx] = rx[6 + idx];
  239. }
  240. result = PASS;
  241. }
  242. }
  243. return result;
  244. }
  245. int Query_Relay_Output(uint8_t fd, uint8_t targetAddr, Relay *Ret_Buf)
  246. {
  247. int result = FAIL;
  248. uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_RELAY_OUTPUT, 0x00, 0x00, 0x00};
  249. uint8_t rx[512];
  250. uint8_t chksum = 0x00;
  251. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  252. // for (int i = 0; i < 7; i++)
  253. // printf("tx = %x \n", tx[i]);
  254. // for (int i = 0; i < len; i++)
  255. // printf("rx = %x \n", rx[i]);
  256. if (len > 6) {
  257. if (len < 6 + (rx[4] | rx[5] << 8)) {
  258. return result;
  259. }
  260. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  261. chksum ^= rx[6 + idx];
  262. }
  263. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  264. (rx[2] == tx[1]) &&
  265. (rx[1] == tx[2]) &&
  266. (rx[3] == tx[3])) {
  267. Ret_Buf->relay_event.bits.AC_Contactor = (rx[6] >> 0) & 0x01;
  268. Ret_Buf->relay_event.bits.CCS_Precharge = (rx[6] >> 1) & 0x01;
  269. Ret_Buf->relay_event.bits.Gun1_N = (rx[7] >> 0) & 0x01;
  270. Ret_Buf->relay_event.bits.Gun1_P = (rx[7] >> 1) & 0x01;
  271. Ret_Buf->relay_event.bits.Gun1_Parallel_N = (rx[7] >> 2) & 0x01;
  272. Ret_Buf->relay_event.bits.Gun1_Parallel_P = (rx[7] >> 3) & 0x01;
  273. Ret_Buf->relay_event.bits.Gun2_N = (rx[8] >> 0) & 0x01;
  274. Ret_Buf->relay_event.bits.Gun2_P = (rx[8] >> 1) & 0x01;
  275. result = PASS;
  276. }
  277. }
  278. return result;
  279. }
  280. int Query_Gfd_Adc(uint8_t fd, uint8_t targetAddr, Gfd *Ret_Buf)
  281. {
  282. int result = FAIL;
  283. uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_GFD_ADC, 0x00, 0x00, 0x00};
  284. uint8_t rx[512];
  285. uint8_t chksum = 0x00;
  286. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  287. // for(int i = 0; i < 7; i++)
  288. // printf ("tx = %d \n", tx[i]);
  289. if (len > 6) {
  290. if (len < 6 + (rx[4] | rx[5] << 8)) {
  291. //printf("Query_Gfd_Adc fail \n");
  292. return result;
  293. }
  294. // for(int i = 0; i < len; i++)
  295. // printf ("rx = %d \n", rx[i]);
  296. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  297. chksum ^= rx[6 + idx];
  298. }
  299. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  300. (rx[2] == tx[1]) &&
  301. (rx[1] == tx[2]) &&
  302. (rx[3] == tx[3])) {
  303. Ret_Buf->Resister_conn1 = (rx[6] | (rx[7] << 8));
  304. Ret_Buf->voltage_conn1 = (rx[8] | (rx[9] << 8));
  305. Ret_Buf->result_conn1 = rx[10];
  306. Ret_Buf->rb_step_1 = rx[11];
  307. Ret_Buf->Resister_conn2 = (rx[12] | (rx[13] << 8));
  308. Ret_Buf->voltage_conn2 = (rx[14] | (rx[15] << 8));
  309. Ret_Buf->result_conn2 = rx[16];
  310. Ret_Buf->rb_step_2 = rx[17];
  311. result = PASS;
  312. }
  313. }
  314. return result;
  315. }
  316. int Query_Gpio_Input(uint8_t fd, uint8_t targetAddr, Gpio_in *Ret_Buf)
  317. {
  318. int result = FAIL;
  319. uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_GPIO_IN, 0x00, 0x00, 0x00};
  320. uint8_t rx[512];
  321. uint8_t chksum = 0x00;
  322. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  323. if (len > 6) {
  324. if (len < 6 + (rx[4] | rx[5] << 8)) {
  325. return result;
  326. }
  327. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  328. chksum ^= rx[6 + idx];
  329. }
  330. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  331. (rx[2] == tx[1]) &&
  332. (rx[1] == tx[2]) &&
  333. (rx[3] == tx[3])) {
  334. Ret_Buf->AC_Connector = (rx[6] >> 0) & 0x01;
  335. Ret_Buf->AC_MainBreaker = (rx[6] >> 1) & 0x01;
  336. Ret_Buf->SPD = (rx[6] >> 2) & 0x01;
  337. Ret_Buf->Door_Open = ((rx[6] >> 3) & 0x01);
  338. Ret_Buf->GFD[0] = (rx[6] >> 4) & 0x01;
  339. Ret_Buf->GFD[1] = (rx[6] >> 5) & 0x01;
  340. Ret_Buf->AC_Drop = (rx[6] >> 6) & 0x01;
  341. Ret_Buf->Emergency_IO = (rx[7] >> 0) & 0x01;
  342. Ret_Buf->Button_Emergency_Press = (rx[8] >> 0) & 0x01;
  343. Ret_Buf->Button_On_Press = (rx[8] >> 1) & 0x01;
  344. Ret_Buf->Button_Off_Press = (rx[8] >> 2) & 0x01;
  345. Ret_Buf->Key_1_Press = (rx[8] >> 3) & 0x01;
  346. Ret_Buf->Key_2_Press = (rx[8] >> 4) & 0x01;
  347. Ret_Buf->Key_3_Press = (rx[8] >> 5) & 0x01;
  348. Ret_Buf->Key_4_Press = (rx[8] >> 6) & 0x01;
  349. Ret_Buf->Button3 = (rx[8] >> 7) & 0x01;
  350. log_info("GPIO In: 0x%X\n",rx[8]);
  351. result = PASS;
  352. }
  353. }
  354. return result;
  355. }
  356. int Query_Model_Name(uint8_t fd, uint8_t targetAddr, uint8_t *modelname)
  357. {
  358. int result = FAIL;
  359. uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_MODEL_NAME, 0x00, 0x00, 0x00};
  360. uint8_t rx[512];
  361. uint8_t chksum = 0x00;
  362. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  363. if (len > 6) {
  364. if (len < 6 + (rx[4] | rx[5] << 8)) {
  365. return result;
  366. }
  367. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  368. chksum ^= rx[6 + idx];
  369. }
  370. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  371. (rx[2] == tx[1]) &&
  372. (rx[1] == tx[2]) &&
  373. (rx[3] == tx[3])) {
  374. strncpy((char *)modelname, (char *)(rx + 6), (rx[4] | rx[5] << 8));
  375. result = PASS;
  376. }
  377. }
  378. return result;
  379. }
  380. int Query_Charging_Current(uint8_t fd, uint8_t targetAddr, Ac_Charging_current *Ret_Buf)
  381. {
  382. int result = FAIL;
  383. uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_OUTPUT_CURRENT, 0x00, 0x00, 0x00};
  384. uint8_t rx[512];
  385. uint8_t chksum = 0x00;
  386. uint8_t len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);
  387. if (len > 6) {
  388. if (len < 6 + (rx[4] | rx[5] << 8)) {
  389. return result;
  390. }
  391. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  392. chksum ^= rx[6 + idx];
  393. }
  394. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  395. (rx[2] == tx[1]) &&
  396. (rx[1] == tx[2]) &&
  397. (rx[3] == tx[3])) {
  398. Ret_Buf->OuputCurrentL1 = rx[6] + (rx[7] << 8);
  399. Ret_Buf->OuputCurrentL2 = rx[8] + (rx[9] << 8);
  400. Ret_Buf->OuputCurrentL3 = rx[10] + (rx[11] << 8);
  401. result = PASS;
  402. }
  403. }
  404. return result;
  405. }
  406. int Query_AC_Status(uint8_t fd, uint8_t targetAddr, Ac_Status *Ret_Buf)
  407. {
  408. int result = FAIL;
  409. uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_STATUS, 0x00, 0x00, 0x00};
  410. uint8_t rx[512];
  411. uint8_t chksum = 0x00;
  412. uint8_t len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);
  413. if (len > 6) {
  414. if (len < 6 + (rx[4] | rx[5] << 8)) {
  415. return result;
  416. }
  417. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  418. chksum ^= rx[6 + idx];
  419. }
  420. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  421. (rx[2] == tx[1]) &&
  422. (rx[1] == tx[2]) &&
  423. (rx[3] == tx[3])) {
  424. Ret_Buf->CpStatus = rx[6];
  425. Ret_Buf->CurLimit = (rx[7] | (rx[8] << 8));
  426. Ret_Buf->PilotVol_P = (rx[9] | (rx[10] << 8));
  427. Ret_Buf->PilotVol_N = (rx[11] | (rx[12] << 8));
  428. Ret_Buf->LockStatus = rx[13];
  429. Ret_Buf->RelayStatus = rx[14];
  430. Ret_Buf->ShutterStatus = rx[15];
  431. Ret_Buf->MeterStatus = rx[16];
  432. Ret_Buf->PpStatus = rx[17];
  433. Ret_Buf->MaxCurrent = rx[18];
  434. Ret_Buf->RotateSwitchStatus = rx[19];
  435. //
  436. // Ret_Buf->AC_Connector = (rx[6] >> 0) & 0x01;
  437. // Ret_Buf->AC_MainBreaker = (rx[6] >> 1) & 0x01;
  438. // Ret_Buf->SPD = (rx[6] >> 2) & 0x01;
  439. // Ret_Buf->Door_Open = (rx[6] >> 3) & 0x01;
  440. // Ret_Buf->GFD[0] = (rx[6] >> 4) & 0x01;
  441. // Ret_Buf->GFD[1] = (rx[6] >> 5) & 0x01;
  442. // Ret_Buf->AC_Drop = (rx[6] >> 6) & 0x01;
  443. //
  444. // Ret_Buf->Emergency_IO = (rx[7] >> 0) & 0x01;
  445. //
  446. // Ret_Buf->Button_Emergency_Press = (rx[8] >> 0) & 0x01;
  447. // Ret_Buf->Button_On_Press = (rx[8] >> 1) & 0x01;
  448. // Ret_Buf->Button_Off_Press = (rx[8] >> 2) & 0x01;
  449. // Ret_Buf->Key_1_Press = (rx[8] >> 3) & 0x01;
  450. // Ret_Buf->Key_2_Press = (rx[8] >> 4) & 0x01;
  451. // Ret_Buf->Key_3_Press = (rx[8] >> 5) & 0x01;
  452. // Ret_Buf->Key_4_Press = (rx[8] >> 6) & 0x01;
  453. result = PASS;
  454. }
  455. }
  456. return result;
  457. }
  458. int Query_AC_Alarm_Code(uint8_t fd, uint8_t targetAddr, Ac_Alarm_code *Ret_Buf)
  459. {
  460. int result = FAIL;
  461. uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_ALARM_CODE, 0x00, 0x00};
  462. uint8_t rx[512];
  463. uint8_t chksum = 0x00;
  464. uint8_t len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);
  465. if (len > 6) {
  466. if (len < 6 + (rx[4] | rx[5] << 8)) {
  467. return result;
  468. }
  469. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  470. chksum ^= rx[6 + idx];
  471. }
  472. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  473. (rx[2] == tx[1]) &&
  474. (rx[1] == tx[2]) &&
  475. (rx[3] == tx[3])) {
  476. Ret_Buf->AcAlarmCode = rx[6] + (rx[7] << 8) + (rx[8] << 16) + (rx[9] << 24);
  477. result = PASS;
  478. }
  479. }
  480. return result;
  481. }
  482. int Query_Charging_Energy(uint8_t fd, uint8_t targetAddr, Ac_Charging_energy *Ret_Buf)
  483. {
  484. int result = FAIL;
  485. uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_OUTPUT_ENERGY, 0x00, 0x00, 0x00};
  486. uint8_t rx[512];
  487. uint8_t chksum = 0x00;
  488. uint8_t len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);
  489. if (len > 6) {
  490. if (len < 6 + (rx[4] | rx[5] << 8)) {
  491. return result;
  492. }
  493. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  494. chksum ^= rx[6 + idx];
  495. }
  496. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  497. (rx[2] == tx[1]) &&
  498. (rx[1] == tx[2]) &&
  499. (rx[3] == tx[3])) {
  500. Ret_Buf->Energy = rx[6] + (rx[7] << 8) + (rx[8] << 16) + (rx[9] << 24);
  501. result = PASS;
  502. }
  503. }
  504. return result;
  505. }
  506. //------------------------------------------------------------------------------
  507. //===== Configure =====
  508. //------------------------------------------------------------------------------
  509. int Config_Fan_Speed(uint8_t fd, uint8_t targetAddr, FanSpeed *Set_Buf)
  510. {
  511. int result = FAIL;
  512. uint8_t tx[15] = {0xaa, 0x00, targetAddr, CMD_CONFIG_FAN_SPEED, 0x08, 0x00, Set_Buf->speed[0] & 0xff, (Set_Buf->speed[0] >> 8) & 0xff, Set_Buf->speed[1] & 0xff, (Set_Buf->speed[1] >> 8) & 0xff, Set_Buf->speed[2] & 0xff, (Set_Buf->speed[2] >> 8) & 0xff, Set_Buf->speed[3] & 0xff, (Set_Buf->speed[3] >> 8) & 0xff, 0x00};
  513. uint8_t rx[512];
  514. uint8_t chksum = 0x00;
  515. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  516. chksum ^= tx[6 + idx];
  517. }
  518. tx[14] = chksum;
  519. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  520. if (len > 6) {
  521. if (len < 6 + (rx[4] | rx[5] << 8)) {
  522. return result;
  523. }
  524. chksum = 0x00;
  525. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  526. chksum ^= rx[6 + idx];
  527. }
  528. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  529. (rx[2] == tx[1]) &&
  530. (rx[1] == tx[2]) &&
  531. (rx[3] == tx[3]) &&
  532. rx[6] == PASS) {
  533. result = PASS;
  534. }
  535. }
  536. return result;
  537. }
  538. int Config_Model_Name(uint8_t fd, uint8_t targetAddr, uint8_t *modelname)
  539. {
  540. int result = FAIL;
  541. uint8_t tx[21] = {0xaa, 0x00, targetAddr, CMD_CONFIG_MODEL_NAME, 0x0E, 0x00,
  542. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  543. };
  544. uint8_t rx[512];
  545. uint8_t chksum = 0x00;
  546. memcpy(tx + 6, modelname, 14);
  547. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  548. chksum ^= tx[6 + idx];
  549. }
  550. tx[20] = chksum;
  551. // for(int i = 0; i < 21; i++)
  552. // printf ("tx = %x \n", tx[i]);
  553. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  554. // for(int i = 0; i < len; i++)
  555. // printf ("rx = %x \n", rx[i]);
  556. if (len > 6) {
  557. if (len < 6 + (rx[4] | rx[5] << 8)) {
  558. return result;
  559. }
  560. chksum = 0x00;
  561. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  562. chksum ^= rx[6 + idx];
  563. }
  564. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  565. (rx[2] == tx[1]) &&
  566. (rx[1] == tx[2]) &&
  567. (rx[3] == tx[3]) &&
  568. rx[6] == PASS) {
  569. result = PASS;
  570. }
  571. }
  572. return result;
  573. }
  574. int Config_Relay_Output(uint8_t fd, uint8_t targetAddr, Relay *Set_Buf)
  575. {
  576. int result = FAIL;
  577. uint8_t tx[10] = {0xaa, 0x00, targetAddr, CMD_CONFIG_RELAY_OUTPUT, 0x03, 0x00, Set_Buf->relay_event.relay_status[0], Set_Buf->relay_event.relay_status[1], Set_Buf->relay_event.relay_status[2]};
  578. uint8_t rx[512];
  579. uint8_t chksum = 0x00;
  580. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  581. chksum ^= tx[6 + idx];
  582. }
  583. tx[9] = chksum;
  584. // for (int i = 0; i < 10; i++)
  585. // printf("set relay cmd : tx = %x \n", tx[i]);
  586. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  587. if (len > 6) {
  588. if (len < 6 + (rx[4] | rx[5] << 8)) {
  589. return result;
  590. }
  591. // for (int i = 0; i < len; i++)
  592. // printf("set relay cmd : rx = %x \n", rx[i]);
  593. chksum = 0x00;
  594. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  595. chksum ^= rx[6 + idx];
  596. }
  597. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  598. (rx[2] == tx[1]) &&
  599. (rx[1] == tx[2]) &&
  600. (rx[3] == tx[3]) &&
  601. rx[6] == PASS) {
  602. result = PASS;
  603. }
  604. }
  605. return result;
  606. }
  607. int Config_Gpio_Output(uint8_t fd, uint8_t targetAddr, Gpio_out *Set_Buf)
  608. {
  609. int result = FAIL;
  610. uint8_t tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_GPIO_OUTPUT, 0x01, 0x00, 0x00, 0x00};
  611. uint8_t rx[512];
  612. uint8_t chksum = 0x00;
  613. tx[6] |= (Set_Buf->AC_Connector ? 0x01 : 0x00);
  614. for (int idx = 0; idx < 2; idx++) {
  615. tx[6] |= (Set_Buf->Button_LED[idx] ? 0x01 : 0x00) << (1 + idx);
  616. }
  617. for (int idx = 0; idx < 4; idx++) {
  618. tx[6] |= (Set_Buf->System_LED[idx] ? 0x01 : 0x00) << (3 + idx);
  619. }
  620. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  621. chksum ^= tx[6 + idx];
  622. }
  623. tx[14] = chksum;
  624. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  625. if (len > 6) {
  626. if (len < 6 + (rx[4] | rx[5] << 8)) {
  627. return result;
  628. }
  629. chksum = 0x00;
  630. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  631. chksum ^= rx[6 + idx];
  632. }
  633. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  634. (rx[2] == tx[1]) &&
  635. (rx[1] == tx[2]) &&
  636. (rx[3] == tx[3])) {
  637. result = PASS;
  638. }
  639. }
  640. return result;
  641. }
  642. int Config_Rtc_Data(uint8_t fd, uint8_t targetAddr, Rtc *Set_Buf)
  643. {
  644. int result = FAIL;
  645. uint8_t tx[21] = { 0xaa, 0x00, targetAddr, CMD_CONFIG_RTC_DATA, 0x0E, 0x00, Set_Buf->RtcData[0], Set_Buf->RtcData[1],
  646. Set_Buf->RtcData[2], Set_Buf->RtcData[3], Set_Buf->RtcData[4], Set_Buf->RtcData[5], Set_Buf->RtcData[6], Set_Buf->RtcData[7],
  647. Set_Buf->RtcData[8], Set_Buf->RtcData[9], Set_Buf->RtcData[10], Set_Buf->RtcData[11], Set_Buf->RtcData[12], Set_Buf->RtcData[13]
  648. };
  649. uint8_t rx[512];
  650. uint8_t chksum = 0x00;
  651. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  652. chksum ^= tx[6 + idx];
  653. }
  654. tx[20] = chksum;
  655. if (tranceive(fd, tx, sizeof(tx), rx) > 0) {
  656. chksum = 0x00;
  657. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  658. chksum ^= rx[6 + idx];
  659. }
  660. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  661. (rx[2] == tx[1]) &&
  662. (rx[1] == tx[2]) &&
  663. (rx[3] == tx[3]) &&
  664. rx[6] == PASS) {
  665. result = PASS;
  666. }
  667. }
  668. return result;
  669. }
  670. int Config_LED_Status(uint8_t fd, uint8_t targetAddr, Ac_Led_Status *Ret_Buf)
  671. {
  672. int result = FAIL;
  673. uint8_t tx[12] = {0xaa, 0x00, targetAddr, CMD_CONFIG_AC_LED_STATUS, 0x05, 0x00, Ret_Buf->ActionMode, (Ret_Buf->AcAlarmCode >> 0) & 0xFF,
  674. (Ret_Buf->AcAlarmCode >> 8) & 0xFF, (Ret_Buf->AcAlarmCode >> 16) & 0xFF, (Ret_Buf->AcAlarmCode >> 24) & 0xFF
  675. };
  676. uint8_t rx[512];
  677. uint8_t chksum = 0x00;
  678. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  679. chksum ^= tx[6 + idx];
  680. }
  681. tx[11] = chksum;
  682. if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0) {
  683. chksum = 0x00;
  684. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  685. chksum ^= rx[6 + idx];
  686. }
  687. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  688. (rx[2] == tx[1]) &&
  689. (rx[1] == tx[2]) &&
  690. (rx[3] == tx[3]) &&
  691. rx[6] == PASS) {
  692. result = PASS;
  693. }
  694. }
  695. return result;
  696. }
  697. int Config_Ac_Duty(uint8_t fd, uint8_t targetAddr, uint8_t _value)
  698. {
  699. int result = FAIL;
  700. uint8_t tx[8] = {0xaa, 0x00, targetAddr, CMD_CONFIG_AC_DUTY, 0x01, 0x00, _value};
  701. uint8_t rx[512];
  702. uint8_t chksum = 0x00;
  703. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  704. chksum ^= tx[6 + idx];
  705. }
  706. tx[7] = chksum;
  707. if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0) {
  708. chksum = 0x00;
  709. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  710. chksum ^= rx[6 + idx];
  711. }
  712. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  713. (rx[2] == tx[1]) &&
  714. (rx[1] == tx[2]) &&
  715. (rx[3] == tx[3]) &&
  716. rx[6] == PASS) {
  717. result = PASS;
  718. }
  719. }
  720. return result;
  721. }
  722. int Config_Legacy_Req(uint8_t fd, uint8_t targetAddr, uint8_t _switch)
  723. {
  724. int result = FAIL;
  725. uint8_t tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_LEGACY_REQ, 0x02, 0x00, _switch, 0x00};
  726. uint8_t rx[512];
  727. uint8_t chksum = 0x00;
  728. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  729. chksum ^= tx[6 + idx];
  730. }
  731. tx[8] = chksum;
  732. if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0) {
  733. chksum = 0x00;
  734. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  735. chksum ^= rx[6 + idx];
  736. }
  737. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  738. (rx[2] == tx[1]) &&
  739. (rx[1] == tx[2]) &&
  740. (rx[3] == tx[3]) &&
  741. rx[6] == PASS) {
  742. result = PASS;
  743. }
  744. }
  745. return result;
  746. }
  747. int Config_Reset_MCU(uint8_t fd, uint8_t targetAddr)
  748. {
  749. int result = FAIL;
  750. uint8_t tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_RESET_MCU, 0x02, 0x00, 0x01, 0x00};
  751. uint8_t rx[512];
  752. uint8_t chksum = 0x00;
  753. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  754. chksum ^= tx[6 + idx];
  755. }
  756. tx[7] = chksum;
  757. if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0) {
  758. chksum = 0x00;
  759. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  760. chksum ^= rx[6 + idx];
  761. }
  762. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  763. (rx[2] == tx[1]) &&
  764. (rx[1] == tx[2]) &&
  765. (rx[3] == tx[3]) &&
  766. rx[6] == PASS) {
  767. result = PASS;
  768. }
  769. }
  770. return result;
  771. }
  772. int Config_Gfd_Value(uint8_t fd, uint8_t targetAddr, Gfd_config *Set_Buf)
  773. {
  774. int result = FAIL;
  775. uint8_t tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_GDF_VALUE, 0x02, 0x00, 0x00, 0x00, 0x00};
  776. uint8_t rx[512];
  777. uint8_t chksum = 0x00;
  778. tx[6] = Set_Buf->index;
  779. tx[7] = Set_Buf->state;
  780. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  781. chksum ^= tx[6 + idx];
  782. }
  783. tx[8] = chksum;
  784. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  785. if (len > 6) {
  786. if (len < 6 + (rx[4] | rx[5] << 8)) {
  787. return result;
  788. }
  789. chksum = 0x00;
  790. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  791. chksum ^= rx[6 + idx];
  792. }
  793. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  794. (rx[2] == tx[1]) &&
  795. (rx[1] == tx[2]) &&
  796. (rx[3] == tx[3])) {
  797. result = PASS;
  798. }
  799. }
  800. return result;
  801. }
  802. int Config_CSU_Mode(uint8_t fd, uint8_t targetAddr)
  803. {
  804. int result = FAIL;
  805. uint8_t tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_CSU_MODE, 0x02, 0x00, 0x01, 0x00};
  806. uint8_t rx[512];
  807. uint8_t chksum = 0x00;
  808. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  809. chksum ^= tx[6 + idx];
  810. }
  811. tx[7] = chksum;
  812. if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0) {
  813. chksum = 0x00;
  814. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  815. chksum ^= rx[6 + idx];
  816. }
  817. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  818. (rx[2] == tx[1]) &&
  819. (rx[1] == tx[2]) &&
  820. (rx[3] == tx[3]) &&
  821. rx[6] == PASS) {
  822. result = PASS;
  823. }
  824. }
  825. return result;
  826. }
  827. int Config_Led_Color(uint8_t fd, uint8_t targetAddr, Led_Color *Ret_Buf)
  828. {
  829. int result = FAIL;
  830. uint8_t tx[13] = {0xaa,
  831. 0x00,
  832. targetAddr,
  833. CMD_CONFIG_LEN_COLOR,
  834. 0x06,
  835. 0x00,
  836. Ret_Buf->Connect_1_Red,
  837. Ret_Buf->Connect_1_Green,
  838. Ret_Buf->Connect_1_Blue,
  839. Ret_Buf->Connect_2_Red,
  840. Ret_Buf->Connect_2_Green,
  841. Ret_Buf->Connect_2_Blue
  842. };
  843. uint8_t rx[512];
  844. uint8_t chksum = 0x00;
  845. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  846. chksum ^= tx[6 + idx];
  847. }
  848. tx[12] = chksum;
  849. // for(int i = 0; i < 13; i++)
  850. // printf ("tx = %x \n", tx[i]);
  851. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  852. // for(int i = 0; i < len; i++)
  853. // printf ("rx = %x \n", rx[i]);
  854. if (len > 6) {
  855. if (len < 6 + (rx[4] | rx[5] << 8)) {
  856. return result;
  857. }
  858. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  859. chksum ^= rx[6 + idx];
  860. }
  861. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  862. (rx[2] == tx[1]) &&
  863. (rx[1] == tx[2]) &&
  864. (rx[3] == tx[3]) &&
  865. (rx[6] == PASS)) {
  866. result = PASS;
  867. }
  868. }
  869. return result;
  870. }
  871. //------------------------------------------------------------------------------
  872. //===== Update =====
  873. //------------------------------------------------------------------------------
  874. int Update_Start(uint8_t fd, uint8_t targetAddr, uint32_t crc32)
  875. {
  876. int result = FAIL;
  877. uint8_t tx[11] = {0xaa, 0x00, targetAddr, CMD_UPDATE_START, 0x04, 0x00, (crc32 >> 0) & 0xff, (crc32 >> 8) & 0xff, (crc32 >> 16) & 0xff, (crc32 >> 24) & 0xff, 0x00};
  878. uint8_t rx[512];
  879. uint8_t chksum = 0x00;
  880. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  881. chksum ^= tx[6 + idx];
  882. }
  883. tx[10] = chksum;
  884. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  885. if (len > 6) {
  886. if (len < 6 + (rx[4] | rx[5] << 8)) {
  887. return result;
  888. }
  889. chksum = 0x00;
  890. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  891. chksum ^= rx[6 + idx];
  892. }
  893. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  894. (rx[2] == tx[1]) &&
  895. (rx[1] == tx[2]) &&
  896. (rx[3] == tx[3]) &&
  897. (rx[6] == 0x00)) {
  898. result = PASS;
  899. }
  900. }
  901. return result;
  902. }
  903. int Update_Abord(uint8_t fd, uint8_t targetAddr)
  904. {
  905. int result = FAIL;
  906. uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_UPDATE_ABORT, 0x04, 0x00, 0x00};
  907. uint8_t rx[512];
  908. uint8_t chksum = 0x00;
  909. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  910. if (len > 6) {
  911. if (len < 6 + (rx[4] | rx[5] << 8)) {
  912. return result;
  913. }
  914. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  915. chksum ^= rx[6 + idx];
  916. }
  917. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  918. (rx[2] == tx[1]) &&
  919. (rx[1] == tx[2]) &&
  920. (rx[3] == tx[3]) &&
  921. (rx[6] == 0x00)) {
  922. result = PASS;
  923. }
  924. }
  925. return result;
  926. }
  927. int Update_Transfer(uint8_t fd, uint8_t targetAddr, uint32_t startAddr, uint8_t *data, uint16_t length)
  928. {
  929. int result = FAIL;
  930. uint8_t tx[11 + length];
  931. uint8_t rx[512];
  932. uint8_t chksum = 0x00;
  933. tx[0] = 0xaa;
  934. tx[1] = 0x00;
  935. tx[2] = targetAddr;
  936. tx[3] = CMD_UPDATE_TRANSFER;
  937. tx[4] = (4 + length) & 0xff;
  938. tx[5] = ((4 + length) >> 8) & 0xff;
  939. tx[6] = (startAddr >> 0) & 0xff;
  940. tx[7] = (startAddr >> 8) & 0xff;
  941. tx[8] = (startAddr >> 16) & 0xff;
  942. tx[9] = (startAddr >> 24) & 0xff;
  943. memcpy(tx + 10, data, length);
  944. for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
  945. chksum ^= tx[6 + idx];
  946. }
  947. tx[sizeof(tx) - 1] = chksum;
  948. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  949. if (len > 6) {
  950. if (len < 6 + (rx[4] | rx[5] << 8)) {
  951. return result;
  952. }
  953. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  954. chksum ^= rx[6 + idx];
  955. }
  956. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  957. (rx[2] == tx[1]) &&
  958. (rx[1] == tx[2]) &&
  959. (rx[3] == tx[3]) &&
  960. (rx[6] == 0x00)) {
  961. result = PASS;
  962. }
  963. }
  964. return result;
  965. }
  966. int Update_Finish(uint8_t fd, uint8_t targetAddr)
  967. {
  968. int result = FAIL;
  969. uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_UPDATE_FINISH, 0x04, 0x00, 0x00};
  970. uint8_t rx[512];
  971. uint8_t chksum = 0x00;
  972. uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
  973. if (len > 6) {
  974. if (len < 6 + (rx[4] | rx[5] << 8)) {
  975. return result;
  976. }
  977. for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
  978. chksum ^= rx[6 + idx];
  979. }
  980. if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
  981. (rx[2] == tx[1]) &&
  982. (rx[1] == tx[2]) &&
  983. (rx[3] == tx[3]) &&
  984. (rx[6] == 0x00)) {
  985. result = PASS;
  986. }
  987. }
  988. return result;
  989. }