123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568 |
- /*
- * Module_ConfigTools.c
- *
- * Created on: 2020年6月22日
- * Author: foluswen
- */
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/time.h>
- #include <sys/timeb.h>
- #include <sys/types.h>
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/mman.h>
- #include <linux/wireless.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <dirent.h>
- #include <unistd.h>
- #include <stdarg.h>
- #include <stdio.h> /*標準輸入輸出定義*/
- #include <stdlib.h> /*標準函數庫定義*/
- #include <unistd.h> /*Unix 標準函數定義*/
- #include <fcntl.h> /*檔控制定義*/
- #include <termios.h> /*PPSIX 終端控制定義*/
- #include <errno.h> /*錯誤號定義*/
- #include <errno.h>
- #include <string.h>
- #include <time.h>
- #include <ctype.h>
- #include <ifaddrs.h>
- #include <stdbool.h>
- #include <stddef.h>
- #include <stdint.h>
- #include "define.h"
- //=================================
- // System basic sample constant
- //=================================
- #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
- #define PASS 1
- #define FAIL -1
- #define YES 1
- #define NO 0
- #define ON 1
- #define OFF 0
- #define MtdBlockSize 0x300000
- struct SysConfigAndInfo *ShmSysConfigAndInfo;
- struct StatusCodeData *ShmStatusCodeData;
- 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.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]ConfigToolsLog",
- tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
- buffer,
- tm->tm_year+1900,tm->tm_mon+1);
- #ifdef SystemLogMessage
- system(Buf);
- #endif
- #ifdef ConsloePrintLog
- printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
- #endif
- return rc;
- }
- int runShellCmd(const char*cmd)
- {
- int result = FAIL;
- char buf[256];
- FILE *fp;
- fp = popen(cmd, "r");
- if(fp != NULL)
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- DEBUG_INFO("%s\n", buf);
- }
- result = PASS;
- }
- pclose(fp);
- return result;
- }
- int StoreUsrConfigData(struct SysConfigData *UsrData)
- {
- int result = PASS;
- int fd,wrd;
- unsigned int i,Chk;
- unsigned char *ptr, *BufTmp;
- Chk=0;
- ptr=(unsigned char *)UsrData;
- if((BufTmp=malloc(MtdBlockSize))!=NULL)
- {
- memset(BufTmp,0,MtdBlockSize);
- memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
- for(i=ARRAY_SIZE(UsrData->CsuBootLoadFwRev);i<MtdBlockSize-4;i++)
- Chk+=*(BufTmp+i);
- memcpy(BufTmp+MtdBlockSize-4, &Chk, 4);
- // Output configuration to file.
- fd = open("/mnt/EvseConfig.bin", O_RDWR|O_CREAT);
- if (fd < 0)
- {
- DEBUG_ERROR("open /mnt/EvseConfig.bin NG\n");
- free(BufTmp);
- return 0;
- }
- wrd=write(fd, BufTmp, MtdBlockSize);
- close(fd);
- if(wrd<MtdBlockSize)
- {
- DEBUG_ERROR("write /mnt/EvseConfig.bin NG\n");
- free(BufTmp);
- return 0;
- }
- DEBUG_INFO("EvseConfig write to file in /mnt OK.\n");
- DEBUG_INFO("Erase /dev/mtd10.\n");
- runShellCmd("flash_erase /dev/mtd10 0 12");
- DEBUG_INFO("Write /dev/mtd10.\n");
- runShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseConfig.bin");
- DEBUG_INFO("Erase /dev/mtd11.\n");
- runShellCmd("flash_erase /dev/mtd11 0 12");
- DEBUG_INFO("Write /dev/mtd11.\n");
- runShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseConfig.bin");
- system("rm -f /mnt/EvseConfig.bin");
- DEBUG_INFO("EvseConfig write to flash OK\n");
- }
- else
- {
- DEBUG_ERROR("alloc BlockSize NG\r\n");
- result = FAIL;
- }
- if(BufTmp!=NULL)
- free(BufTmp);
- return result;
- }
- int InitShareMemory()
- {
- int result = PASS;
- int MeterSMId;
- //Initial ShmSysConfigAndInfo
- if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
- #endif
- result = FAIL;
- }
- else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
- #endif
- result = FAIL;
- }
- //Initial ShmStatusCodeData
- if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
- #endif
- result = FAIL;
- }
- else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
- #endif
- result = FAIL;
- }
- return result;
- }
- int main(void)
- {
- char cmd[128];
- if(InitShareMemory() == FAIL)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("InitShareMemory NG\n");
- #endif
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
- }
- sleep(5);
- return 0;
- }
- else
- {
- DEBUG_INFO("InitShareMemory OK.\r\n");
- }
- for(;;)
- {
- system("clear");
- memset(cmd, 0x00, ARRAY_SIZE(cmd));
- printf("\n ===== main menu ===================================");
- printf("\n system: system configuration menu.");
- printf("\n ocpp: ocpp configuration menu.");
- printf("\n network: netwok configuration menu.");
- printf("\n upgrade: trigger firmware upgrade.");
- printf("\n save: Save config.");
- printf("\n exit: Exit config tools.");
- printf("\n =================================================");
- printf("\n Please input item name to config: ");
- scanf("%s", &cmd[0]);
- if(strcmp(cmd, "system") == 0)
- {
- memset(cmd, 0x00, ARRAY_SIZE(cmd));
- printf("\n ***** system configuration menu ******************");
- printf("\n modelname: EVSE model name.");
- printf("\n serialnumber: EVSE serial number.");
- printf("\n *************************************************");
- printf("\n Please input operation item: ");
- scanf("%s", &cmd[0]);
- if(strcmp(cmd, "modelname") == 0)
- {
- memset(cmd, 0x00, ARRAY_SIZE(cmd));
- printf("\n *************************************************");
- printf("\n Current model name: %s", ShmSysConfigAndInfo->SysConfig.ModelName);
- printf("\n 0: Keep current config.");
- printf("\n 1: Input new model name.");
- printf("\n *************************************************");
- printf("\n Please input operation item: ");
- scanf("%s", &cmd[0]);
- if(atoi(cmd) == 1)
- {
- printf("\n Please input model name: ");
- scanf("%s", &cmd[0]);
- memset(&ShmSysConfigAndInfo->SysConfig.ModelName[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
- strcpy((char*)&ShmSysConfigAndInfo->SysConfig.ModelName[0], (char*)&cmd[0]);
- }
- }
- else if(strcmp(cmd, "serialnumber") == 0)
- {
- memset(cmd, 0x00, ARRAY_SIZE(cmd));
- printf("\n *************************************************");
- printf("\n Current serial number: %s", ShmSysConfigAndInfo->SysConfig.SerialNumber);
- printf("\n 0: Keep current config.");
- printf("\n 1: Input new serial number.");
- printf("\n *************************************************");
- printf("\n Please input operation item: ");
- scanf("%s", &cmd[0]);
- if(atoi(cmd) == 1)
- {
- printf("\n Please input serial number: ");
- scanf("%s", &cmd[0]);
- memset(&ShmSysConfigAndInfo->SysConfig.SerialNumber[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
- strcpy((char*)&ShmSysConfigAndInfo->SysConfig.SerialNumber[0], (char*)&cmd[0]);
- }
- }
- }
- else if(strcmp(cmd, "ocpp") == 0)
- {
- memset(cmd, 0x00, ARRAY_SIZE(cmd));
- printf("\n *************************************************");
- printf("\n ocppurl: OCPP backend server url.");
- printf("\n cboxid: Charger box id.");
- printf("\n *************************************************");
- printf("\n Please input operation item: ");
- scanf("%s", &cmd[0]);
- if(strcmp(cmd, "ocppurl") == 0)
- {
- memset(cmd, 0x00, ARRAY_SIZE(cmd));
- printf("\n *************************************************");
- printf("\n Current OCPP url: %s", ShmSysConfigAndInfo->SysConfig.OcppServerURL);
- printf("\n 0: Keep current config.");
- printf("\n 1: Input new ocpp url.");
- printf("\n *************************************************");
- printf("\n Please input operation item: ");
- scanf("%s", &cmd[0]);
- if(atoi(cmd) == 1)
- {
- printf("\n Please input ocpp url: ");
- scanf("%s", &cmd[0]);
- memset(&ShmSysConfigAndInfo->SysConfig.OcppServerURL[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
- strcpy((char*)&ShmSysConfigAndInfo->SysConfig.OcppServerURL[0], (char*)&cmd[0]);
- }
- }
- else if(strcmp(cmd, "cboxid") == 0)
- {
- memset(cmd, 0x00, ARRAY_SIZE(cmd));
- printf("\n *************************************************");
- printf("\n Current OCPP charger box id: %s", ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
- printf("\n 0: Keep current config.");
- printf("\n 1: Input new charger box id.");
- printf("\n *************************************************");
- printf("\n Please input operation item: ");
- scanf("%s", &cmd[0]);
- if(atoi(cmd) == 1)
- {
- printf("\n Please input OCPP charger box id: ");
- scanf("%s", &cmd[0]);
- memset(&ShmSysConfigAndInfo->SysConfig.ChargeBoxId[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
- strcpy((char*)&ShmSysConfigAndInfo->SysConfig.ChargeBoxId[0], (char*)&cmd[0]);
- }
- }
- }
- else if(strcmp(cmd, "network") == 0)
- {
- memset(cmd, 0x00, ARRAY_SIZE(cmd));
- printf("\n *************************************************");
- printf("\n ethdhcp: Ethernet DHCP client.");
- printf("\n wifimode: WiFi mode.");
- printf("\n wifidhcp: WiFi DHCP client.");
- printf("\n wificssid: WiFi client SSID.");
- printf("\n wificpasswd: WiFi client password.");
- printf("\n telemode: Telecomm mode.");
- printf("\n teleapn: Telecomm APN.");
- printf("\n teleid: Telecomm login id.");
- printf("\n telepwd: Telecomm login password.");
- printf("\n *************************************************");
- printf("\n Please input operation item: ");
- scanf("%s", &cmd[0]);
- if(strcmp(cmd, "ethdhcp") == 0)
- {
- memset(cmd, 0x00, ARRAY_SIZE(cmd));
- printf("\n *************************************************");
- printf("\n 0: Disable.");
- printf("\n 1: Enable.");
- printf("\n *************************************************");
- printf("\n Current ethernet dhcp mode: %d", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient^1);
- printf("\n Please input dhcp mode: ");
- scanf("%s", &cmd[0]);
- ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient = ((0<=atoi(cmd))&&(atoi(cmd)<=1)?atoi(cmd)^1:0);
- }
- else if(strcmp(cmd, "wifimode") == 0)
- {
- memset(cmd, 0x00, ARRAY_SIZE(cmd));
- printf("\n *************************************************");
- printf("\n 0: Disable.");
- printf("\n 1: Station.");
- printf("\n 2: Access point.");
- printf("\n *************************************************");
- printf("\n Current WiFi mode: %d", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
- printf("\n Please input WiFi mode: ");
- scanf("%s", &cmd[0]);
- ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode = ((0<=atoi(cmd))&&(atoi(cmd)<=2)?atoi(cmd):0);
- }
- else if(strcmp(cmd, "wifidhcp") == 0)
- {
- memset(cmd, 0x00, ARRAY_SIZE(cmd));
- printf("\n *************************************************");
- printf("\n 0: Disable.");
- printf("\n 1: Enable.");
- printf("\n *************************************************");
- printf("\n Current WiFi dhcp client mode: %d", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient^1);
- printf("\n Please input WiFi mode: ");
- scanf("%s", &cmd[0]);
- ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient = ((0<=atoi(cmd))&&(atoi(cmd)<=1)?atoi(cmd)^1:0);
- }
- else if(strcmp(cmd, "wificssid") == 0)
- {
- memset(cmd, 0x00, ARRAY_SIZE(cmd));
- printf("\n *************************************************");
- printf("\n Current WiFi client SSID: %s", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
- printf("\n 0: Keep current config.");
- printf("\n 1: Input new WiFi client SSID.");
- printf("\n *************************************************");
- printf("\n Please input operation item: ");
- scanf("%s", &cmd[0]);
- if(atoi(cmd) == 1)
- {
- printf("\n Please input WiFi client SSID: ");
- scanf("%s", &cmd[0]);
- memset(&ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid));
- strcpy((char*)&ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid[0], (char*)&cmd[0]);
- }
- }
- else if(strcmp(cmd, "wificpasswd") == 0)
- {
- memset(cmd, 0x00, ARRAY_SIZE(cmd));
- printf("\n *************************************************");
- printf("\n Current WiFi client password: %s", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
- printf("\n 0: Keep current config.");
- printf("\n 1: Input new WiFi client password.");
- printf("\n *************************************************");
- printf("\n Please input operation item: ");
- scanf("%s", &cmd[0]);
- if(atoi(cmd) == 1)
- {
- printf("\n Please input WiFi client password: ");
- scanf("%s", &cmd[0]);
- memset(&ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword));
- strcpy((char*)&ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword[0], (char*)&cmd[0]);
- }
- }
- else if(strcmp(cmd, "telemode") == 0)
- {
- memset(cmd, 0x00, ARRAY_SIZE(cmd));
- printf("\n *************************************************");
- printf("\n 0: Disable.");
- printf("\n 1: Enable.");
- printf("\n *************************************************");
- printf("\n Current telecomm mode: %d", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode^1);
- printf("\n Please input telecomm mode: ");
- scanf("%s", &cmd[0]);
- ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = ((0<=atoi(cmd))&&(atoi(cmd)<=1)?atoi(cmd)^1:0);
- }
- else if(strcmp(cmd, "teleapn") == 0)
- {
- memset(cmd, 0x00, ARRAY_SIZE(cmd));
- printf("\n *************************************************");
- printf("\n Current telecomm APN: %s", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn);
- printf("\n 0: Keep current config.");
- printf("\n 1: Input new telecomm APN.");
- printf("\n *************************************************");
- printf("\n Please input operation item: ");
- scanf("%s", &cmd[0]);
- if(atoi(cmd) == 1)
- {
- printf("\n Please input telecomm APN: ");
- scanf("%s", &cmd[0]);
- memset(&ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn));
- strcpy((char*)&ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn[0], (char*)&cmd[0]);
- }
- }
- else if(strcmp(cmd, "teleid") == 0)
- {
- memset(cmd, 0x00, ARRAY_SIZE(cmd));
- printf("\n *************************************************");
- printf("\n Current telecomm login id: %s", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
- printf("\n 0: Keep current config.");
- printf("\n 1: Input new telecomm login id.");
- printf("\n *************************************************");
- printf("\n Please input operation item: ");
- scanf("%s", &cmd[0]);
- if(atoi(cmd) == 1)
- {
- printf("\n Please input telecomm login id: ");
- scanf("%s", &cmd[0]);
- memset(&ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId));
- strcpy((char*)&ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId[0], (char*)&cmd[0]);
- }
- }
- else if(strcmp(cmd, "telepwd") == 0)
- {
- memset(cmd, 0x00, ARRAY_SIZE(cmd));
- printf("\n *************************************************");
- printf("\n Current telecomm login password: %s", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd);
- printf("\n 0: Keep current config.");
- printf("\n 1: Input new telecomm login password.");
- printf("\n *************************************************");
- printf("\n Please input operation item: ");
- scanf("%s", &cmd[0]);
- if(atoi(cmd) == 1)
- {
- printf("\n Please input telecomm login password: ");
- scanf("%s", &cmd[0]);
- memset(&ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd[0], 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd));
- strcpy((char*)&ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd[0], (char*)&cmd[0]);
- }
- }
- }
- else if(strcmp(cmd, "upgrade") == 0)
- {
- printf("\n Firmware upgrade trigger.");
- ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = ON;
- sleep(2);
- }
- else if(strcmp(cmd, "save") == 0)
- {
- if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
- {
- printf("\n Write configuration fail.\r\n");
- }
- else
- printf("\n Write configuration OK.\r\n");
- sleep(2);
- }
- else if(strcmp(cmd, "exit") == 0)
- {
- printf("\n exit program.\n\n");
- return FAIL;
- }
- }
- return -1;
- }
|