/* * Module_LcmControl_Wistron.c * * Created on: 2021/11/6 * Author: folus */ #include "Module_LcmControl_Wistron.h" #include "define.h" struct SysConfigAndInfo *ShmSysConfigAndInfo; struct StatusCodeData *ShmStatusCodeData; struct OCPP16Data *ShmOCPP16Data; struct OCPP20Data *ShmOCPP20Data; ParsingRatedCur modelnameInfo = {0}; uint8_t gunType[4] = {0}; struct timespec timer[TMR_IDX_CNT]; /** * * @param fmt * @return */ int StoreLogMsg(const char *fmt, ...) { char Buf[4096+256]; char buffer[4096]; time_t CurrentTime; struct tm *tm; struct timeval tv; va_list args; va_start(args, fmt); int rc = vsnprintf(buffer, sizeof(buffer), fmt, args); va_end(args); memset(Buf,0,sizeof(Buf)); CurrentTime = time(NULL); tm=localtime(&CurrentTime); gettimeofday(&tv, NULL); // get microseconds, 10^-6 sprintf(Buf,"echo -n \'[%04d.%02d.%02d %02d:%02d:%02d.%03ld]%s\' >> /Storage/SystemLog/[%04d.%02d]Module_LcmControl_Wistron_Log", 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, tm->tm_year+1900,tm->tm_mon+1); #ifdef SystemLogMessage system((const char*)Buf); #endif #ifdef ConsloePrintLog 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); #endif return rc; } /** * * @param data * @param length * @return */ uint32_t crc32(uint8_t *data, uint32_t length) { uint32_t crc=0xFFFFFFFF; for(size_t i=0;i>=1; if(b) crc=crc^0xEDB88320; ch>>=1; } } return ~crc; } /** * * @param filename * @return */ uint32_t getFileCrc32(char *filename) { uint32_t result = 0; int fd = open(filename, O_RDONLY); if(fd < 0) { DEBUG_ERROR("Can not open file %s\n", filename); } else { struct stat st; stat(filename, &st); uint8_t *data; data = malloc(st.st_size); if(read(fd,data,st.st_size) == st.st_size) { result = crc32(data, st.st_size); close(fd); } free(data); } return result; } /** * * @param timer */ void refreshStartTimer(struct timespec *timer) { clock_gettime(CLOCK_MONOTONIC, timer); } /** * * @param timer * @return */ int getDiffSecNow(struct timespec timer) { struct timespec timerNow; clock_gettime(CLOCK_MONOTONIC, &timerNow); return (int)((((unsigned long)(timerNow.tv_sec - timer.tv_sec) * 1000) + ((unsigned long)((timerNow.tv_nsec / 1000000) - (timer.tv_nsec / 1000000))))/1000); } /** * * @return */ int InitShareMemory() { int result = PASS; int MeterSMId; //Initial ShmSysConfigAndInfo if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0) { DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n"); result = FAIL; } else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1) { DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n"); result = FAIL; } else {} // Initial ShmStatusCodeData if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0) { DEBUG_ERROR("shmget ShmStatusCodeData NG\n"); result = FAIL; } else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1) { DEBUG_ERROR("shmat ShmStatusCodeData NG\n"); result = FAIL; } else {} // Initial ShmOCPP16Data if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0) { DEBUG_ERROR("shmget ShmOCPP16Data NG\n"); result = FAIL; } else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1) { DEBUG_ERROR("shmat ShmOCPP16Data NG\n"); result = FAIL; } else {} // Initial ShmOCPP20Data if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), 0777)) < 0) { DEBUG_ERROR("shmget ShmOCPP20Data NG\n"); result = FAIL; } else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1) { DEBUG_ERROR("shmat ShmOCPP20Data NG\n"); result = FAIL; } else {} // Parsing model name to get related info about charger if(RatedCurrentParsing((char*)ShmSysConfigAndInfo->SysConfig.ModelName, &modelnameInfo) != -1) { DEBUG_INFO("Model name rated power: %d\n", modelnameInfo.ratedPower); if((ShmSysConfigAndInfo->SysConfig.ModelName[0]=='D') && ((ShmSysConfigAndInfo->SysConfig.ModelName[1]=='B') || (ShmSysConfigAndInfo->SysConfig.ModelName[1]=='K') || (ShmSysConfigAndInfo->SysConfig.ModelName[1]=='O')) ) // 'D' means DC { // DO series for(int gun_index=0; gun_indexpayload); if(!is_error(payload)) { /* * TODO: * 1. Process message depend on receive topic * 2. Message maybe could define as JSON format */ if(strstr(msg->topic, "client/connect")) { sprintf((char*)clientInfo.verHW, "%s", json_object_get_string(json_object_object_get(payload, "HW_version"))); sprintf((char*)clientInfo.verFW_OS, "%s", json_object_get_string(json_object_object_get(payload, "FW_version_OS"))); sprintf((char*)clientInfo.verFW_APK, "%s", json_object_get_string(json_object_object_get(payload, "FW_version_APK"))); sprintf((char*)clientInfo.verFW_UI, "%s", json_object_get_string(json_object_object_get(payload, "FW_version_UI"))); sprintf((char*)clientInfo.macAddr, "%s", json_object_get_string(json_object_object_get(payload, "MAC_addr"))); sprintf((char*)clientInfo.orientation, "%s", json_object_get_string(json_object_object_get(payload, "Orientation"))); DEBUG_INFO("HW_version: %s\n", clientInfo.verHW); DEBUG_INFO("FW_version_OS: %s\n", clientInfo.verFW_OS); DEBUG_INFO("FW_version_APK: %s\n", clientInfo.verFW_APK); DEBUG_INFO("FW_version_UI: %s\n", clientInfo.verFW_UI); DEBUG_INFO("MAC_addr: %s\n", clientInfo.macAddr); DEBUG_INFO("Orientation: %s\n", clientInfo.orientation); } else if(strstr(msg->topic, "client/touch")) { if(json_object_object_get(payload, "touchlist") != NULL) { for(int idx=0;idx x: %s, y: %s\n", idx, json_object_get_string(json_object_object_get(json_object_array_get_idx(json_object_object_get(payload, "touchlist"), idx), "obj_type")), json_object_get_string(json_object_object_get(json_object_array_get_idx(json_object_object_get(payload, "touchlist"), idx), "obj_onClick")), json_object_get_string(json_object_object_get(json_object_array_get_idx(json_object_object_get(payload, "touchlist"), idx), "touch_point_x")), json_object_get_string(json_object_object_get(json_object_array_get_idx(json_object_object_get(payload, "touchlist"), idx), "touch_point_y"))); } } } else if(strstr(msg->topic, "client/info")) { if(json_object_object_get(payload, "status") != NULL) { switch(atoi(json_object_get_string(json_object_object_get(payload, "status")))) { case 1: DEBUG_INFO("Status normal.\n"); break; case 2: DEBUG_INFO("Status downloading.\n"); break; case 3: DEBUG_INFO("Status uploading.\n"); break; case 4: DEBUG_INFO("Status reboot.\n"); break; case 5: DEBUG_INFO("Status reset.\n"); break; case 6: DEBUG_INFO("Status sleep.\n"); break; case 255: DEBUG_INFO("Status fault.\n"); break; default: DEBUG_INFO("Unknown status.\n"); break; } } if(json_object_object_get(payload, "temperature") != NULL) { DEBUG_INFO("Temperature: %s.\n", json_object_get_string(json_object_object_get(payload, "temperature"))); } if(json_object_object_get(payload, "cpu_temperatue") != NULL) { DEBUG_INFO("CPU temperature: %s.\n", json_object_get_string(json_object_object_get(payload, "cpu_temperatue"))); } } else if(strstr(msg->topic, "client/profile") || strstr(msg->topic, "client/command") || strstr(msg->topic, "client/timesync") || strstr(msg->topic, "client/layout/textview") || strstr(msg->topic, "client/layout/imageview") || strstr(msg->topic, "client/layout/videoview") || strstr(msg->topic, "client/layout/remove")) { // Skip topic publish from CSU } else { DEBUG_INFO("Unknown topic: %s\n", msg->topic); DEBUG_INFO("Unknown payload: %s\n", msg->payload); } } else { DEBUG_WARN("Payload is not JSON format.\n"); } json_object_put(payload); return 0; } /** * * @param mosq * @param obj * @param reason_code */ void on_connect(struct mosquitto *mosq, void *obj, int reason_code) { DEBUG_INFO("on_connect: %s\n", mosquitto_connack_string(reason_code)); if(reason_code != 0)mosquitto_disconnect(mosq); } /** * * @param mosq * @param obj * @param reason_code */ void on_disconnect(struct mosquitto *mosq, void *obj, int reason_code) { DEBUG_INFO("on_disconnect: %s\n", mosquitto_connack_string(reason_code)); if(reason_code != 0)mosquitto_disconnect(mosq); } /** * * @param mosq * @param obj * @param mid */ void on_publish(struct mosquitto *mosq, void *obj, int mid) {} //========================================================================== // Page routine //========================================================================== /** * * @param targetChargingInfoData */ void page_Booting(struct ChargingInfoData *targetChargingInfoData) { /* * TODO: * 1. Page content */ } /** * * @param targetChargingInfoData */ void page_Idle(struct ChargingInfoData *targetChargingInfoData) { /* * TODO: * 1. Page content */ } /** * * @param targetChargingInfoData */ void page_Authorizing(struct ChargingInfoData *targetChargingInfoData) { /* * TODO: * 1. Page content */ } /** * * @param targetChargingInfoData */ void page_Preparing(struct ChargingInfoData *targetChargingInfoData) { /* * TODO: * 1. Page content */ } /** * * @param targetChargingInfoData */ void page_Charging(struct ChargingInfoData *targetChargingInfoData) { /* * TODO: * 1. Page content */ } /** * * @param targetChargingInfoData */ void page_Terminating(struct ChargingInfoData *targetChargingInfoData) { /* * TODO: * 1. Page content */ } /** * * @param targetChargingInfoData */ void page_Complete(struct ChargingInfoData *targetChargingInfoData) { /* * TODO: * 1. Page content */ } /** * * @param targetChargingInfoData */ void page_Reservation(struct ChargingInfoData *targetChargingInfoData) { /* * TODO: * 1. Page content */ } /** * * @param targetChargingInfoData */ void page_Maintain(struct ChargingInfoData *targetChargingInfoData) { /* * TODO: * 1. Page content */ } /** * * @param targetChargingInfoData */ void page_Update(struct ChargingInfoData *targetChargingInfoData) { /* * TODO: * 1. Page content */ } /** * * @param targetChargingInfoData */ void page_AlarmFault(struct ChargingInfoData *targetChargingInfoData) { /* * TODO: * 1. Page content */ } /** * * @param targetChargingInfoData */ void page_Unknown(struct ChargingInfoData *targetChargingInfoData) { /* * TODO: * 1. Page content */ } //========================================================================== // Command publish sample routine //========================================================================== /** * * @param mosq */ int publish_profile_sample(struct mosquitto *mosq) { return publish_profile(mosq, PROFILE_CONNECT_TIMEOUT, "10000"); } /** * * @param mosq */ int publish_upgrade_sample(struct mosquitto *mosq) { char url[128]; char chkCrc32[16]; sprintf(url, "ftp://ftpuser:ftppasswd@%s/mnt/dummy_Image.zip", BROKER_ADDRESS); sprintf(chkCrc32, "0x%08X", getFileCrc32("/mnt/dummy_Image.zip")); DEBUG_INFO("image file crc32: %s\n", chkCrc32); return publish_upgrade(mosq, OTA_TYPE_SDCARD, url, chkCrc32, "V0.01"); } /** * * @param mosq */ int publish_restart_sample(struct mosquitto *mosq) { return publish_restart(mosq, FALSE); } /** * * @param mosq */ int publish_power_saving_sample(struct mosquitto *mosq) { return publish_power_saving(mosq, TRUE); } /** * * @param mosq */ int publish_back_dimming_sample(struct mosquitto *mosq) { return publish_back_dimming(mosq, BRIGHTNESS_LEVEL_AUTO); } /** * * @param mosq */ int publish_timesync_sample(struct mosquitto *mosq) { return publish_timesync(mosq); } /** * * @param mosq */ int publish_textview_add_sample(struct mosquitto *mosq) { Text_List textList[2] = {0}; for(int idx=0;idxSysInfo.CurGunSelected]) { case GUN_TYPE_CHAdeMO: if(ShmSysConfigAndInfo->SysConfig.ModelName[8] != '0') { tempIndex = ((ShmSysConfigAndInfo->SysInfo.CurGunSelected==2) ? 1: 0); } else { tempIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected; } for (int index = 0; index < CHAdeMO_QUANTITY; index++) { if(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == tempIndex) { targetChargingInfoData = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index]; } } break; case GUN_TYPE_CCS: if(ShmSysConfigAndInfo->SysConfig.ModelName[8] != '0') { tempIndex = ((ShmSysConfigAndInfo->SysInfo.CurGunSelected==2) ? 1: 0); } else { tempIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected; } for (int index = 0; index < CCS_QUANTITY; index++) { if(ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == tempIndex) { targetChargingInfoData = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index]; } } break; case GUN_TYPE_GBT: if(ShmSysConfigAndInfo->SysConfig.ModelName[8] != '0') { tempIndex = ((ShmSysConfigAndInfo->SysInfo.CurGunSelected==2) ? 1: 0); } else { tempIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected; } for (int index = 0; index < GB_QUANTITY; index++) { if(ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == tempIndex) { targetChargingInfoData = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index]; } } break; case GUN_TYPE_DO: tempIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected; for (int index = 0; index < GENERAL_GUN_QUANTITY; index++) { if(ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].GeneralChargingData.Index == tempIndex) { targetChargingInfoData = &ShmSysConfigAndInfo->SysInfo.ConnectorInfo[index].GeneralChargingData; } } break; case GUN_TYPE_AC: if(ShmSysConfigAndInfo->SysConfig.ModelName[8] != '0') { tempIndex = 2; } else { tempIndex = ShmSysConfigAndInfo->SysInfo.CurGunSelected; } for (int index = 0; index < AC_QUANTITY; index++) { if(ShmSysConfigAndInfo->SysInfo.AcChargingData[index].Index == tempIndex) { targetChargingInfoData = &ShmSysConfigAndInfo->SysInfo.AcChargingData[index]; } } break; case GUN_TYPE_UNKNOWN: default: break; } // If selected gun or system status changed, clear page all content if((previousData.gun_selected != ShmSysConfigAndInfo->SysInfo.CurGunSelected) || (previousData.SystemStatus != targetChargingInfoData->SystemStatus)) { publish_clear_screen(mosq); previousData.gun_selected = ShmSysConfigAndInfo->SysInfo.CurGunSelected; previousData.SystemStatus = targetChargingInfoData->SystemStatus; } // Display page content depend on selected gun system status switch(targetChargingInfoData->SystemStatus) { case SYS_MODE_BOOTING: page_Booting(targetChargingInfoData); break; case SYS_MODE_IDLE: page_Idle(targetChargingInfoData); break; case SYS_MODE_AUTHORIZING: page_Authorizing(targetChargingInfoData); break; case SYS_MODE_PREPARING: page_Preparing(targetChargingInfoData); break; case SYS_MODE_CHARGING: page_Charging(targetChargingInfoData); break; case SYS_MODE_TERMINATING: page_Terminating(targetChargingInfoData); break; case SYS_MODE_COMPLETE: page_Complete(targetChargingInfoData); break; case SYS_MODE_RESERVATION: page_Reservation(targetChargingInfoData); break; case SYS_MODE_MAINTAIN: page_Maintain(targetChargingInfoData); break; case SYS_MODE_UPDATE: page_Update(targetChargingInfoData); break; case SYS_MODE_ALARM: case SYS_MODE_FAULT: page_AlarmFault(targetChargingInfoData); break; default: page_Unknown(targetChargingInfoData); break; } #else // For test command char cmd[128]; system("clear"); memset(cmd, 0x00, ARRAY_SIZE(cmd)); printf("\n ===== main menu =================================="); printf("\n 1: publish_profile_sample."); printf("\n 2: publish_upgrade_sample."); printf("\n 3: publish_restart_sample."); printf("\n 4: publish_power_saving_sample."); printf("\n 5: publish_back_dimming_sample."); printf("\n 6: publish_timesync."); printf("\n 7: publish_textview_add_sample."); printf("\n 8: publish_imageview_add_sample."); printf("\n 9: publish_videoview_add_sample."); printf("\n 10: publish_view_remove_sample."); printf("\n 11: publish_clear_screen."); printf("\n 12: publish_audio_volume."); printf("\n 13: publish_trigger_report_status."); printf("\n 14: publish_qr_code_image."); printf("\n =================================================="); printf("\n Please input item to test: "); scanf("%s", &cmd[0]); switch(atoi(cmd)) { case 1: publish_profile_sample(mosq); break; case 2: publish_upgrade_sample(mosq); break; case 3: publish_restart_sample(mosq); break; case 4: publish_power_saving_sample(mosq); break; case 5: publish_back_dimming_sample(mosq); break; case 6: publish_timesync_sample(mosq); break; case 7: publish_textview_add_sample(mosq); break; case 8: publish_imageview_add_sample(mosq); break; case 9: publish_videoview_add_sample(mosq); break; case 10: publish_view_remove_sample(mosq); break; case 11: publish_clear_screen_sample(mosq); break; case 12: publish_audio_volume_sample(mosq); break; case 13: publish_trigger_report_status_sample(mosq); case 14: publish_qr_code_image(mosq); break; } #endif usleep(500000); } mosquitto_lib_cleanup(); return -1; }