internalComm.c 35 KB

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