Module_LcmControl_Wistron.c 27 KB

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