Module_LcmControl_Wistron.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. /*
  2. * Module_LcmControl_Wistron.c
  3. *
  4. * Created on: 2021/11/6
  5. * Author: folus
  6. */
  7. #include "Module_LcmControl_Wistron.h"
  8. #include "define.h"
  9. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  10. struct StatusCodeData *ShmStatusCodeData;
  11. struct OCPP16Data *ShmOCPP16Data;
  12. struct OCPP20Data *ShmOCPP20Data;
  13. ParsingRatedCur modelnameInfo = {0};
  14. uint8_t gunType[4] = {0};
  15. struct timespec timer[TMR_IDX_CNT];
  16. /**
  17. *
  18. * @param fmt
  19. * @return
  20. */
  21. int StoreLogMsg(const char *fmt, ...)
  22. {
  23. char Buf[4096+256];
  24. char buffer[4096];
  25. time_t CurrentTime;
  26. struct tm *tm;
  27. struct timeval tv;
  28. va_list args;
  29. va_start(args, fmt);
  30. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  31. va_end(args);
  32. memset(Buf,0,sizeof(Buf));
  33. CurrentTime = time(NULL);
  34. tm=localtime(&CurrentTime);
  35. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  36. sprintf(Buf,"echo -n \'[%04d.%02d.%02d %02d:%02d:%02d.%03ld]%s\' >> /Storage/SystemLog/[%04d.%02d]Module_LcmControl_Wistron_Log",
  37. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,(tv.tv_usec/1000),
  38. buffer,
  39. tm->tm_year+1900,tm->tm_mon+1);
  40. #ifdef SystemLogMessage
  41. system((const char*)Buf);
  42. #endif
  43. #ifdef ConsloePrintLog
  44. printf("[%04d.%02d.%02d %02d:%02d:%02d.%03ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,(tv.tv_usec/1000), buffer);
  45. #endif
  46. return rc;
  47. }
  48. /**
  49. *
  50. * @param data
  51. * @param length
  52. * @return
  53. */
  54. uint32_t crc32(uint8_t *data, uint32_t length)
  55. {
  56. uint32_t crc=0xFFFFFFFF;
  57. for(size_t i=0;i<length;i++)
  58. {
  59. char ch=data[i];
  60. for(size_t j=0;j<8;j++)
  61. {
  62. uint32_t b=(ch^crc)&1;
  63. crc>>=1;
  64. if(b) crc=crc^0xEDB88320;
  65. ch>>=1;
  66. }
  67. }
  68. return ~crc;
  69. }
  70. /**
  71. *
  72. * @param filename
  73. * @return
  74. */
  75. uint32_t getFileCrc32(char *filename)
  76. {
  77. uint32_t result = 0;
  78. int fd = open(filename, O_RDONLY);
  79. if(fd < 0)
  80. {
  81. DEBUG_ERROR("Can not open file %s\n", filename);
  82. }
  83. else
  84. {
  85. struct stat st;
  86. stat(filename, &st);
  87. uint8_t data[st.st_size];
  88. if(read(fd,data,st.st_size) == st.st_size)
  89. {
  90. result = crc32(data, st.st_size);
  91. }
  92. close(fd);
  93. }
  94. return result;
  95. }
  96. /**
  97. *
  98. * @param timer
  99. */
  100. void refreshStartTimer(struct timespec *timer)
  101. {
  102. clock_gettime(CLOCK_MONOTONIC, timer);
  103. }
  104. /**
  105. *
  106. * @param timer
  107. * @return
  108. */
  109. int getDiffSecNow(struct timespec timer)
  110. {
  111. struct timespec timerNow;
  112. clock_gettime(CLOCK_MONOTONIC, &timerNow);
  113. return (int)((((unsigned long)(timerNow.tv_sec - timer.tv_sec) * 1000) + ((unsigned long)((timerNow.tv_nsec / 1000000) - (timer.tv_nsec / 1000000))))/1000);
  114. }
  115. /**
  116. *
  117. * @return
  118. */
  119. int InitShareMemory()
  120. {
  121. int result = PASS;
  122. int MeterSMId;
  123. //Initial ShmSysConfigAndInfo
  124. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  125. {
  126. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  127. result = FAIL;
  128. }
  129. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  130. {
  131. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  132. result = FAIL;
  133. }
  134. else
  135. {}
  136. // Initial ShmStatusCodeData
  137. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  138. {
  139. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  140. result = FAIL;
  141. }
  142. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  143. {
  144. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  145. result = FAIL;
  146. }
  147. else
  148. {}
  149. // Initial ShmOCPP16Data
  150. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  151. {
  152. DEBUG_ERROR("shmget ShmOCPP16Data NG\n");
  153. result = FAIL;
  154. }
  155. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  156. {
  157. DEBUG_ERROR("shmat ShmOCPP16Data NG\n");
  158. result = FAIL;
  159. }
  160. else
  161. {}
  162. // Initial ShmOCPP20Data
  163. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), 0777)) < 0)
  164. {
  165. DEBUG_ERROR("shmget ShmOCPP20Data NG\n");
  166. result = FAIL;
  167. }
  168. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  169. {
  170. DEBUG_ERROR("shmat ShmOCPP20Data NG\n");
  171. result = FAIL;
  172. }
  173. else
  174. {}
  175. // Parsing model name to get related info about charger
  176. if(RatedCurrentParsing((char*)ShmSysConfigAndInfo->SysConfig.ModelName, &modelnameInfo) != -1)
  177. {
  178. DEBUG_INFO("Model name rated power: %d\n", modelnameInfo.ratedPower);
  179. if((ShmSysConfigAndInfo->SysConfig.ModelName[0]=='D') &&
  180. ((ShmSysConfigAndInfo->SysConfig.ModelName[1]=='B') ||
  181. (ShmSysConfigAndInfo->SysConfig.ModelName[1]=='K') ||
  182. (ShmSysConfigAndInfo->SysConfig.ModelName[1]=='O'))
  183. ) // 'D' means DC
  184. {
  185. // DO series
  186. for(int gun_index=0; gun_index<GENERAL_GUN_QUANTITY ; gun_index++)
  187. {
  188. gunType[gun_index] = GUN_TYPE_DO;
  189. switch(modelnameInfo.ParsingInfo[gun_index].GunType)
  190. {
  191. case Gun_Type_Chademo:
  192. DEBUG_INFO("Gun-%02d type: Cabinet CHAdeMO\n", gun_index);
  193. break;
  194. case Gun_Type_CCS_2:
  195. DEBUG_INFO("Gun-%02d type: Cabinet CCS\n", gun_index);
  196. break;
  197. case Gun_Type_GB:
  198. DEBUG_INFO("Gun-%02d type: Cabinet GBT\n", gun_index);
  199. break;
  200. case Gun_Type_AC:
  201. DEBUG_INFO("Gun-%02d type: Cabinet AC\n", gun_index);
  202. break;
  203. default:
  204. DEBUG_WARN("Gun-%02d type: Cabinet unknown\n", gun_index);
  205. break;
  206. }
  207. }
  208. }
  209. else
  210. {
  211. for(int gun_index=0;gun_index<modelnameInfo.GetGunCount;gun_index++)
  212. {
  213. switch(modelnameInfo.ParsingInfo[gun_index].GunType)
  214. {
  215. case Gun_Type_Chademo:
  216. gunType[gun_index] = GUN_TYPE_CHAdeMO;
  217. DEBUG_INFO("Gun-%02d type: CHAdeMO\n", gun_index);
  218. break;
  219. case Gun_Type_CCS_2:
  220. gunType[gun_index] = GUN_TYPE_CCS;
  221. DEBUG_INFO("Gun-%02d type: CCS\n", gun_index);
  222. break;
  223. case Gun_Type_GB:
  224. gunType[gun_index] = GUN_TYPE_GBT;
  225. DEBUG_INFO("Gun-%02d type: GBT\n", gun_index);
  226. break;
  227. case Gun_Type_AC:
  228. gunType[gun_index] = GUN_TYPE_AC;
  229. DEBUG_INFO("Gun-%02d type: AC\n", gun_index);
  230. break;
  231. default:
  232. DEBUG_WARN("Gun-%02d type: Unknown\n", gun_index);
  233. break;
  234. }
  235. }
  236. }
  237. }
  238. else
  239. {
  240. DEBUG_ERROR("Model name parsing fail.\n");
  241. result = FAIL;
  242. }
  243. // Initial all timer
  244. for(uint8_t idxTmr=0;idxTmr<TMR_IDX_CNT;idxTmr++)
  245. {
  246. refreshStartTimer(&timer[idxTmr]);
  247. }
  248. return result;
  249. }
  250. /**
  251. *
  252. * @param mosq
  253. * @param userdata
  254. * @param msg
  255. * @return
  256. */
  257. int on_message(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *msg)
  258. {
  259. json_object *payload;
  260. payload = json_tokener_parse(msg->payload);
  261. if(!is_error(payload))
  262. {
  263. /*
  264. * TODO:
  265. * 1. Process message depend on receive topic
  266. * 2. Message maybe could define as JSON format
  267. */
  268. if(strstr(msg->topic, "client/connect"))
  269. {
  270. DEBUG_INFO("HW_version: %s\n", json_object_get_string(json_object_object_get(payload, "HW_version")));
  271. DEBUG_INFO("FW_version_OS: %s\n", json_object_get_string(json_object_object_get(payload, "FW_version_OS")));
  272. DEBUG_INFO("FW_version_APK: %s\n", json_object_get_string(json_object_object_get(payload, "FW_version_APK")));
  273. DEBUG_INFO("FW_version_UI: %s\n", json_object_get_string(json_object_object_get(payload, "FW_version_UI")));
  274. DEBUG_INFO("MAC_addr: %s\n", json_object_get_string(json_object_object_get(payload, "MAC_addr")));
  275. DEBUG_INFO("Orientation: %s\n", json_object_get_string(json_object_object_get(payload, "Orientation")));
  276. }
  277. else if(strstr(msg->topic, "client/touch"))
  278. {
  279. if(json_object_object_get(payload, "touchlist") != NULL)
  280. {
  281. for(int idx=0;idx<json_object_array_length(json_object_object_get(payload, "touchlist"));idx++)
  282. {
  283. DEBUG_INFO("Touch point-%d(%s-%s)=> x: %s, y: %s\n", idx,
  284. json_object_get_string(json_object_object_get(json_object_array_get_idx(json_object_object_get(payload, "touchlist"), idx), "obj_type")),
  285. json_object_get_string(json_object_object_get(json_object_array_get_idx(json_object_object_get(payload, "touchlist"), idx), "obj_onClick")),
  286. json_object_get_string(json_object_object_get(json_object_array_get_idx(json_object_object_get(payload, "touchlist"), idx), "touch_point_x")),
  287. json_object_get_string(json_object_object_get(json_object_array_get_idx(json_object_object_get(payload, "touchlist"), idx), "touch_point_y")));
  288. }
  289. }
  290. }
  291. else if(strstr(msg->topic, "client/info"))
  292. {
  293. if(json_object_object_get(payload, "status") != NULL)
  294. {
  295. switch(atoi(json_object_get_string(json_object_object_get(payload, "status"))))
  296. {
  297. case 1:
  298. DEBUG_INFO("Status normal.\n");
  299. break;
  300. case 2:
  301. DEBUG_INFO("Status downloading.\n");
  302. break;
  303. case 3:
  304. DEBUG_INFO("Status uploading.\n");
  305. break;
  306. case 4:
  307. DEBUG_INFO("Status reboot.\n");
  308. break;
  309. case 5:
  310. DEBUG_INFO("Status reset.\n");
  311. break;
  312. default:
  313. DEBUG_INFO("Unknown status.\n");
  314. break;
  315. }
  316. }
  317. }
  318. else if(strstr(msg->topic, "client/profile") ||
  319. strstr(msg->topic, "client/command") ||
  320. strstr(msg->topic, "client/timesync") ||
  321. strstr(msg->topic, "client/layout/textview") ||
  322. strstr(msg->topic, "client/layout/imageview") ||
  323. strstr(msg->topic, "client/layout/videoview") ||
  324. strstr(msg->topic, "client/layout/remove"))
  325. {
  326. // Skip topic publish from CSU
  327. }
  328. else
  329. {
  330. DEBUG_INFO("Unknown topic: %s\n", msg->topic);
  331. DEBUG_INFO("Unknown payload: %s\n", msg->payload);
  332. }
  333. }
  334. else
  335. {
  336. DEBUG_WARN("Payload is not JSON format.\n");
  337. }
  338. json_object_put(payload);
  339. return 0;
  340. }
  341. /**
  342. *
  343. * @param mosq
  344. * @param obj
  345. * @param reason_code
  346. */
  347. void on_connect(struct mosquitto *mosq, void *obj, int reason_code)
  348. {
  349. DEBUG_INFO("on_connect: %s\n", mosquitto_connack_string(reason_code));
  350. if(reason_code != 0)mosquitto_disconnect(mosq);
  351. }
  352. /**
  353. *
  354. * @param mosq
  355. * @param obj
  356. * @param reason_code
  357. */
  358. void on_disconnect(struct mosquitto *mosq, void *obj, int reason_code)
  359. {
  360. DEBUG_INFO("on_disconnect: %s\n", mosquitto_connack_string(reason_code));
  361. if(reason_code != 0)mosquitto_disconnect(mosq);
  362. }
  363. /**
  364. *
  365. * @param mosq
  366. * @param obj
  367. * @param mid
  368. */
  369. void on_publish(struct mosquitto *mosq, void *obj, int mid)
  370. {}
  371. //==========================================================================
  372. // Page routine
  373. //==========================================================================
  374. /**
  375. *
  376. * @param targetChargingInfoData
  377. */
  378. void page_Booting(struct ChargingInfoData *targetChargingInfoData)
  379. {
  380. /*
  381. * TODO:
  382. * 1. Page content
  383. */
  384. }
  385. /**
  386. *
  387. * @param targetChargingInfoData
  388. */
  389. void page_Idle(struct ChargingInfoData *targetChargingInfoData)
  390. {
  391. /*
  392. * TODO:
  393. * 1. Page content
  394. */
  395. }
  396. /**
  397. *
  398. * @param targetChargingInfoData
  399. */
  400. void page_Authorizing(struct ChargingInfoData *targetChargingInfoData)
  401. {
  402. /*
  403. * TODO:
  404. * 1. Page content
  405. */
  406. }
  407. /**
  408. *
  409. * @param targetChargingInfoData
  410. */
  411. void page_Preparing(struct ChargingInfoData *targetChargingInfoData)
  412. {
  413. /*
  414. * TODO:
  415. * 1. Page content
  416. */
  417. }
  418. /**
  419. *
  420. * @param targetChargingInfoData
  421. */
  422. void page_Charging(struct ChargingInfoData *targetChargingInfoData)
  423. {
  424. /*
  425. * TODO:
  426. * 1. Page content
  427. */
  428. }
  429. /**
  430. *
  431. * @param targetChargingInfoData
  432. */
  433. void page_Terminating(struct ChargingInfoData *targetChargingInfoData)
  434. {
  435. /*
  436. * TODO:
  437. * 1. Page content
  438. */
  439. }
  440. /**
  441. *
  442. * @param targetChargingInfoData
  443. */
  444. void page_Complete(struct ChargingInfoData *targetChargingInfoData)
  445. {
  446. /*
  447. * TODO:
  448. * 1. Page content
  449. */
  450. }
  451. /**
  452. *
  453. * @param targetChargingInfoData
  454. */
  455. void page_Reservation(struct ChargingInfoData *targetChargingInfoData)
  456. {
  457. /*
  458. * TODO:
  459. * 1. Page content
  460. */
  461. }
  462. /**
  463. *
  464. * @param targetChargingInfoData
  465. */
  466. void page_Maintain(struct ChargingInfoData *targetChargingInfoData)
  467. {
  468. /*
  469. * TODO:
  470. * 1. Page content
  471. */
  472. }
  473. /**
  474. *
  475. * @param targetChargingInfoData
  476. */
  477. void page_Update(struct ChargingInfoData *targetChargingInfoData)
  478. {
  479. /*
  480. * TODO:
  481. * 1. Page content
  482. */
  483. }
  484. /**
  485. *
  486. * @param targetChargingInfoData
  487. */
  488. void page_AlarmFault(struct ChargingInfoData *targetChargingInfoData)
  489. {
  490. /*
  491. * TODO:
  492. * 1. Page content
  493. */
  494. }
  495. /**
  496. *
  497. * @param targetChargingInfoData
  498. */
  499. void page_Unknown(struct ChargingInfoData *targetChargingInfoData)
  500. {
  501. /*
  502. * TODO:
  503. * 1. Page content
  504. */
  505. }
  506. //==========================================================================
  507. // Command publish sample routine
  508. //==========================================================================
  509. /**
  510. *
  511. * @param mosq
  512. */
  513. int publish_profile_sample(struct mosquitto *mosq)
  514. {
  515. return publish_profile(mosq, PROFILE_CONNECT_TIMEOUT, "10000");
  516. }
  517. /**
  518. *
  519. * @param mosq
  520. */
  521. int publish_upgrade_sample(struct mosquitto *mosq)
  522. {
  523. char url[128];
  524. char chkCrc32[16];
  525. sprintf(url, "ftp://ftpuser:ftppasswd@%s/mnt/dummy_Image.zip", BROKER_ADDRESS);
  526. sprintf(chkCrc32, "0x%08X", getFileCrc32("/root/main"));
  527. return publish_upgrade(mosq, OTA_TYPE_UI, url, chkCrc32, "V0.01");
  528. }
  529. /**
  530. *
  531. * @param mosq
  532. */
  533. int publish_restart_sample(struct mosquitto *mosq)
  534. {
  535. return publish_restart(mosq, TRUE);
  536. }
  537. /**
  538. *
  539. * @param mosq
  540. */
  541. int publish_power_saving_sample(struct mosquitto *mosq)
  542. {
  543. return publish_power_saving(mosq, TRUE);
  544. }
  545. /**
  546. *
  547. * @param mosq
  548. */
  549. int publish_back_dimming_sample(struct mosquitto *mosq)
  550. {
  551. return publish_back_dimming(mosq, BRIGHTNESS_LEVEL_AUTO);
  552. }
  553. /**
  554. *
  555. * @param mosq
  556. */
  557. int publish_timesync_sample(struct mosquitto *mosq)
  558. {
  559. return publish_timesync(mosq);
  560. }
  561. /**
  562. *
  563. * @param mosq
  564. */
  565. int publish_textview_add_sample(struct mosquitto *mosq)
  566. {
  567. Text_List textList[2] = {0};
  568. for(int idx=0;idx<ARRAY_SIZE(textList);idx++)
  569. {
  570. textList[idx].textviewIndex = idx;
  571. sprintf(textList[idx].textString, "%08d", (rand()%99999999+10000000));
  572. sprintf(textList[idx].textFont, FONT_ARIAL);
  573. sprintf(textList[idx].textStyle, FONT_STYLE_NORMAL);
  574. textList[idx].textSize = FONT_SIZE_20PX;
  575. textList[idx].layout_x = (rand()%800+100);
  576. textList[idx].layout_y = (rand()%800+100);
  577. }
  578. return publish_textview_add(mosq, textList, ARRAY_SIZE(textList));
  579. }
  580. /**
  581. *
  582. * @param mosq
  583. */
  584. int publish_imageview_add_sample(struct mosquitto *mosq)
  585. {
  586. Image_List imageList[2] = {0};
  587. for(int idx=0;idx<ARRAY_SIZE(imageList);idx++)
  588. {
  589. imageList[idx].imageviewIndex = idx;
  590. imageList[idx].imgsrc_addr = (rand()%10+0);
  591. imageList[idx].layout_x = (rand()%800+100);
  592. imageList[idx].layout_y = (rand()%800+100);
  593. imageList[idx].width = (rand()%800+100);
  594. imageList[idx].height = (rand()%800+100);
  595. }
  596. return publish_imageview_add(mosq, imageList, ARRAY_SIZE(imageList));
  597. }
  598. /**
  599. *
  600. * @param mosq
  601. */
  602. int publish_videoview_add_sample(struct mosquitto *mosq)
  603. {
  604. Video_List videoList[2] = {0};
  605. for(int idx=0;idx<ARRAY_SIZE(videoList);idx++)
  606. {
  607. videoList[idx].videoviewIndex = idx;
  608. videoList[idx].videosrc_addr = (rand()%10+0);
  609. videoList[idx].layout_x = (rand()%800+100);
  610. videoList[idx].layout_y = (rand()%800+100);
  611. videoList[idx].width = (rand()%800+100);
  612. videoList[idx].height = (rand()%800+100);
  613. }
  614. return publish_videoview_add(mosq, videoList, ARRAY_SIZE(videoList));
  615. }
  616. /**
  617. *
  618. * @param mosq
  619. */
  620. int publish_view_remove_sample(struct mosquitto *mosq)
  621. {
  622. Text_List textList[2] = {0};
  623. Image_List imageList[2] = {0};
  624. Video_List videoList[2] = {0};
  625. // Remove text view
  626. for(int idx=0;idx<ARRAY_SIZE(textList);idx++)
  627. {
  628. textList[idx].textviewIndex = idx;
  629. }
  630. // Remove image view
  631. for(int idx=0;idx<ARRAY_SIZE(imageList);idx++)
  632. {
  633. imageList[idx].imageviewIndex = idx;
  634. }
  635. // Remove video view
  636. for(int idx=0;idx<ARRAY_SIZE(videoList);idx++)
  637. {
  638. videoList[idx].videoviewIndex = idx;
  639. }
  640. return publish_view_remove(mosq, textList, ARRAY_SIZE(textList), imageList, ARRAY_SIZE(imageList), videoList, ARRAY_SIZE(videoList));
  641. }
  642. /**
  643. *
  644. * @param mosq
  645. */
  646. int publish_clear_screen_sample(struct mosquitto *mosq)
  647. {
  648. return publish_clear_screen(mosq);
  649. }
  650. /**
  651. *
  652. */
  653. int publish_audio_volume_sample(struct mosquitto *mosq)
  654. {
  655. return publish_audio_volume(mosq, 50);
  656. }
  657. /**
  658. *
  659. * @param mosq
  660. * @return
  661. */
  662. int publish_trigger_report_status_sample(struct mosquitto *mosq)
  663. {
  664. return publish_trigger_report_status(mosq);
  665. }
  666. /**
  667. *
  668. * @return
  669. */
  670. int main(void)
  671. {
  672. struct mosquitto *mosq;
  673. int result;
  674. //===========================================
  675. // Initial share memory
  676. //===========================================
  677. if(InitShareMemory() == FAIL)
  678. {
  679. DEBUG_ERROR("InitShareMemory NG\n");
  680. sleep(5);
  681. return FAIL;
  682. }
  683. else
  684. {
  685. DEBUG_INFO("Share memory initial OK.\n");
  686. }
  687. //===========================================
  688. // Start mosquitto broker
  689. //===========================================
  690. system("killall mosquitto");
  691. sleep(1);
  692. system("/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d");
  693. DEBUG_INFO("Broker target: %s:%d\n", BROKER_ADDRESS, BROKER_PORT);
  694. //===========================================
  695. // Initialize mosquitto library
  696. //===========================================
  697. mosquitto_lib_init();
  698. //===========================================
  699. // Initialize subscriber callback
  700. //===========================================
  701. if(fork())
  702. {
  703. DEBUG_INFO("Initialize subscribe callback function.\n");
  704. result = mosquitto_subscribe_callback(on_message,
  705. NULL,
  706. "client/#",
  707. QOS_ENSURE_BROKER,
  708. BROKER_ADDRESS,
  709. BROKER_PORT,
  710. NULL,
  711. TIMEOUT_KEEPALIVE,
  712. true,
  713. LOGIN_USER,
  714. LOGIN_PASSWD,
  715. NULL,
  716. NULL);
  717. if(result)DEBUG_ERROR("Subscribe initialization error: %s\n", mosquitto_strerror(result));
  718. return -1;
  719. }
  720. //===========================================
  721. // Initialize publisher client instance
  722. //===========================================
  723. DEBUG_INFO("Initialize publisher from host side.\n");
  724. mosq = mosquitto_new(NULL, true, NULL);
  725. if(mosq == NULL)
  726. {
  727. DEBUG_ERROR("Publisher initialization error: Out of memory.\n");
  728. return -1;
  729. }
  730. mosquitto_connect_callback_set(mosq, on_connect);
  731. mosquitto_disconnect_callback_set(mosq, on_disconnect);
  732. mosquitto_publish_callback_set(mosq, on_publish);
  733. //===========================================
  734. // Publisher connect to broker
  735. //===========================================
  736. DEBUG_INFO("Publisher connect to broker.\n");
  737. result = mosquitto_connect(mosq, BROKER_ADDRESS, BROKER_PORT, TIMEOUT_KEEPALIVE);
  738. if(result != MOSQ_ERR_SUCCESS)
  739. {
  740. mosquitto_destroy(mosq);
  741. DEBUG_ERROR("Connect broker error: %s\n", mosquitto_strerror(result));
  742. return -1;
  743. }
  744. //===========================================
  745. // Publisher start loop
  746. //===========================================
  747. DEBUG_INFO("Publisher loop start.\n");
  748. result = mosquitto_loop_start(mosq);
  749. if(result != MOSQ_ERR_SUCCESS)
  750. {
  751. mosquitto_destroy(mosq);
  752. DEBUG_ERROR("MQTT loop start initialization error: %s\n", mosquitto_strerror(result));
  753. return -1;
  754. }
  755. DEBUG_INFO("Module_LcmControl_Wistron initialized.\n");
  756. //===========================================
  757. // Main loop
  758. //===========================================
  759. for(;;)
  760. {
  761. #ifndef TEST_MODE
  762. struct ChargingInfoData *targetChargingInfoData;
  763. uint8_t tempIndex;
  764. static struct PREVIOUS_DATA
  765. {
  766. uint8_t gun_selected;
  767. uint8_t SystemStatus;
  768. }previousData;
  769. // Get selected gun info
  770. switch(gunType[ShmSysConfigAndInfo->SysInfo.CurGunSelected])
  771. {
  772. case GUN_TYPE_CHAdeMO:
  773. if(ShmSysConfigAndInfo->SysConfig.ModelName[8] != '0')
  774. {
  775. tempIndex = ((ShmSysConfigAndInfo->SysInfo.CurGunSelected==2) ? 1: 0);
  776. }
  777. else
  778. {
  779. tempIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected;
  780. }
  781. for (int index = 0; index < CHAdeMO_QUANTITY; index++)
  782. {
  783. if(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == tempIndex)
  784. {
  785. targetChargingInfoData = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  786. }
  787. }
  788. break;
  789. case GUN_TYPE_CCS:
  790. if(ShmSysConfigAndInfo->SysConfig.ModelName[8] != '0')
  791. {
  792. tempIndex = ((ShmSysConfigAndInfo->SysInfo.CurGunSelected==2) ? 1: 0);
  793. }
  794. else
  795. {
  796. tempIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected;
  797. }
  798. for (int index = 0; index < CCS_QUANTITY; index++)
  799. {
  800. if(ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == tempIndex)
  801. {
  802. targetChargingInfoData = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  803. }
  804. }
  805. break;
  806. case GUN_TYPE_GBT:
  807. if(ShmSysConfigAndInfo->SysConfig.ModelName[8] != '0')
  808. {
  809. tempIndex = ((ShmSysConfigAndInfo->SysInfo.CurGunSelected==2) ? 1: 0);
  810. }
  811. else
  812. {
  813. tempIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected;
  814. }
  815. for (int index = 0; index < GB_QUANTITY; index++)
  816. {
  817. if(ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == tempIndex)
  818. {
  819. targetChargingInfoData = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  820. }
  821. }
  822. break;
  823. case GUN_TYPE_DO:
  824. tempIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected;
  825. for (int index = 0; index < GENERAL_GUN_QUANTITY; index++)
  826. {
  827. if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].GeneralChargingData.Index == tempIndex)
  828. {
  829. targetChargingInfoData = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].GeneralChargingData;
  830. }
  831. }
  832. break;
  833. case GUN_TYPE_AC:
  834. if(ShmSysConfigAndInfo->SysConfig.ModelName[8] != '0')
  835. {
  836. tempIndex = 2;
  837. }
  838. else
  839. {
  840. tempIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected;
  841. }
  842. for (int index = 0; index < AC_QUANTITY; index++)
  843. {
  844. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[index].Index == tempIndex)
  845. {
  846. targetChargingInfoData = &ShmSysConfigAndInfo->SysInfo.AcChargingData[index];
  847. }
  848. }
  849. break;
  850. case GUN_TYPE_UNKNOWN:
  851. default:
  852. break;
  853. }
  854. // If selected gun or system status changed, clear page all content
  855. if((previousData.gun_selected != ShmSysConfigAndInfo->SysInfo.CurGunSelected) ||
  856. (previousData.SystemStatus != targetChargingInfoData->SystemStatus))
  857. {
  858. publish_clear_screen(mosq);
  859. previousData.gun_selected = ShmSysConfigAndInfo->SysInfo.CurGunSelected;
  860. previousData.SystemStatus = targetChargingInfoData->SystemStatus;
  861. }
  862. // Display page content depend on selected gun system status
  863. switch(targetChargingInfoData->SystemStatus)
  864. {
  865. case SYS_MODE_BOOTING:
  866. page_Booting(targetChargingInfoData);
  867. break;
  868. case SYS_MODE_IDLE:
  869. page_Idle(targetChargingInfoData);
  870. break;
  871. case SYS_MODE_AUTHORIZING:
  872. page_Authorizing(targetChargingInfoData);
  873. break;
  874. case SYS_MODE_PREPARING:
  875. page_Preparing(targetChargingInfoData);
  876. break;
  877. case SYS_MODE_CHARGING:
  878. page_Charging(targetChargingInfoData);
  879. break;
  880. case SYS_MODE_TERMINATING:
  881. page_Terminating(targetChargingInfoData);
  882. break;
  883. case SYS_MODE_COMPLETE:
  884. page_Complete(targetChargingInfoData);
  885. break;
  886. case SYS_MODE_RESERVATION:
  887. page_Reservation(targetChargingInfoData);
  888. break;
  889. case SYS_MODE_MAINTAIN:
  890. page_Maintain(targetChargingInfoData);
  891. break;
  892. case SYS_MODE_UPDATE:
  893. page_Update(targetChargingInfoData);
  894. break;
  895. case SYS_MODE_ALARM:
  896. case SYS_MODE_FAULT:
  897. page_AlarmFault(targetChargingInfoData);
  898. break;
  899. default:
  900. page_Unknown(targetChargingInfoData);
  901. break;
  902. }
  903. #else
  904. // For test command
  905. char cmd[128];
  906. system("clear");
  907. memset(cmd, 0x00, ARRAY_SIZE(cmd));
  908. printf("\n ===== main menu ==================================");
  909. printf("\n 1: publish_profile_sample.");
  910. printf("\n 2: publish_upgrade_sample.");
  911. printf("\n 3: publish_restart_sample.");
  912. printf("\n 4: publish_power_saving_sample.");
  913. printf("\n 5: publish_back_dimming_sample.");
  914. printf("\n 6: publish_timesync.");
  915. printf("\n 7: publish_textview_add_sample.");
  916. printf("\n 8: publish_imageview_add_sample.");
  917. printf("\n 9: publish_videoview_add_sample.");
  918. printf("\n 10: publish_view_remove_sample.");
  919. printf("\n 11: publish_clear_screen.");
  920. printf("\n 12: publish_audio_volume.");
  921. printf("\n 13: publish_trigger_report_status.");
  922. printf("\n ==================================================");
  923. printf("\n Please input item to test: ");
  924. scanf("%s", &cmd[0]);
  925. switch(atoi(cmd))
  926. {
  927. case 1:
  928. publish_profile_sample(mosq);
  929. break;
  930. case 2:
  931. publish_upgrade_sample(mosq);
  932. break;
  933. case 3:
  934. publish_restart_sample(mosq);
  935. break;
  936. case 4:
  937. publish_power_saving_sample(mosq);
  938. break;
  939. case 5:
  940. publish_back_dimming_sample(mosq);
  941. break;
  942. case 6:
  943. publish_timesync_sample(mosq);
  944. break;
  945. case 7:
  946. publish_textview_add_sample(mosq);
  947. break;
  948. case 8:
  949. publish_imageview_add_sample(mosq);
  950. break;
  951. case 9:
  952. publish_videoview_add_sample(mosq);
  953. break;
  954. case 10:
  955. publish_view_remove_sample(mosq);
  956. break;
  957. case 11:
  958. publish_clear_screen_sample(mosq);
  959. break;
  960. case 12:
  961. publish_audio_volume_sample(mosq);
  962. break;
  963. case 13:
  964. publish_trigger_report_status_sample(mosq);
  965. break;
  966. }
  967. #endif
  968. usleep(500000);
  969. }
  970. mosquitto_lib_cleanup();
  971. return -1;
  972. }