main.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * Config.h
  3. *
  4. * Created on: 2020年01月15日
  5. * Author: Eason Yang
  6. */
  7. #ifndef CONFIG_MAIN_H_
  8. #define CONFIG_MAIN_H_
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/time.h>
  12. #include <sys/timeb.h>
  13. #include <sys/types.h>
  14. #include <sys/ioctl.h>
  15. #include <sys/socket.h>
  16. #include <sys/ipc.h>
  17. #include <sys/shm.h>
  18. #include <sys/mman.h>
  19. #include <linux/wireless.h>
  20. #include <arpa/inet.h>
  21. #include <netinet/in.h>
  22. #include <dirent.h>
  23. #include <unistd.h>
  24. #include <stdarg.h>
  25. #include <stdio.h> /*標準輸入輸出定義*/
  26. #include <stdlib.h> /*標準函數庫定義*/
  27. #include <unistd.h> /*Unix 標準函數定義*/
  28. #include <fcntl.h> /*檔控制定義*/
  29. #include <termios.h> /*PPSIX 終端控制定義*/
  30. #include <errno.h> /*錯誤號定義*/
  31. #include <errno.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include <ctype.h>
  35. #include <ifaddrs.h>
  36. #include <stdbool.h>
  37. #include <stddef.h>
  38. #include <stdint.h>
  39. #include <sqlite3.h>
  40. //===================================
  41. // Define CP State constant
  42. //===================================
  43. #define CP_STATE_UNKNOWN 0
  44. #define CP_STATE_A 1
  45. #define CP_STATE_B 2
  46. #define CP_STATE_C 3
  47. #define CP_STATE_D 4
  48. #define CP_STATE_E 5
  49. #define CP_STATE_F 6
  50. //===================================
  51. // Define start mode constant
  52. //===================================
  53. #define START_METHOD_FREE 0
  54. #define START_METHOD_RFID 1
  55. #define START_METHOD_BACKEND 2
  56. #define START_METHOD_BLE 3
  57. //===================================
  58. // Define Speaker type constant
  59. //===================================
  60. #define SPEAKER_STOP 0
  61. #define SPEAKER_ALWAYS_ON 1
  62. #define SPEAKER_SHORT 2
  63. #define SPEAKER_LONG 3
  64. #define SPEAKER_INTERVAL_SHORT 4
  65. #define SPEAKER_INTERVAL_LONG 5
  66. #define SPEAKER_INTERVAL_3COUNT 6
  67. //===================================
  68. // Define Alarm code constant
  69. //===================================
  70. #define ALARM_OVER_VOLTAGE 0x000001
  71. #define ALARM_UNDER_VOLTAGE 0x000002
  72. #define ALARM_OVER_CURRENT 0x000004
  73. #define ALARM_OVER_TEMPERATURE 0x000008
  74. #define ALARM_GROUND_FAIL 0x000010
  75. #define ALARM_CP_ERROR 0x000020
  76. #define ALARM_CURRENT_LEAK_AC 0x000040
  77. #define ALARM_CURRENT_LEAK_DC 0x000080
  78. #define ALARM_MCU_TESTFAIL 0x000100
  79. #define ALARM_HANDSHAKE_TIMEOUT 0x000200
  80. #define ALARM_EMERGENCY_STOP 0x000400
  81. #define ALARM_RELAY_WELDING 0x000800
  82. #define ALARM_LEAK_MODULE_FAIL 0x001000
  83. #define ALARM_SHUTTER_FAULT 0x002000
  84. #define ALARM_LOCKER_FAULT 0x004000
  85. #define ALARM_POWER_DROP 0x008000
  86. #define ALARM_CURRENT_SHORT 0x010000
  87. #define ALARM_ROTATORY_SWITCH_FAULT 0x020000
  88. #define ALARM_RELAY_DRIVE_FAULT 0x040000
  89. //===================================
  90. // Define Led constant
  91. //===================================
  92. #define LED_ACTION_INIT 0
  93. #define LED_ACTION_IDLE 1
  94. #define LED_ACTION_AUTHED 2
  95. #define LED_ACTION_CONNECTED 3
  96. #define LED_ACTION_CHARGING 4
  97. #define LED_ACTION_STOP 5
  98. #define LED_ACTION_ALARM 6
  99. #define LED_ACTION_MAINTAIN 7
  100. #define LED_ACTION_RFID_PASS 8
  101. #define LED_ACTION_RFID_FAIL 9
  102. #define LED_ACTION_BLE_CONNECT 10
  103. #define LED_ACTION_BLE_DISABLE 11
  104. #define LED_ACTION_DEBUG 12
  105. #define LED_ACTION_ALL_OFF 13
  106. #define LED_RELAY_ON 14
  107. #define LED_RELAY_OFF 15
  108. #define LED_ACTION_HANDSHAKE_FAIL 16
  109. #define LED_ACTION_INTERNET_DISCONNECT 17
  110. //=================================
  111. //CCS related define
  112. //=================================
  113. #define CCS_PWM_DUTY_CP_STAT_E 0
  114. #define CCS_PWM_DUTY_5 5
  115. #define CCS_PWM_DUTY_100 100
  116. #define HANDSHAKE_DUTY_5 1
  117. #define HANDSHAKE_CCS 2
  118. #define HANDSHAKE_CP_STATE_E 3
  119. #define HANDSHAKE_SET_MAX_CURRENT 4
  120. #define HANDSHAKE_NORMAL_CP 5
  121. #define HANDSHAKE_NORMAL_CCS 6
  122. #define CHARGING_MODE_BC 0
  123. #define CHARGING_MODE_HL 1
  124. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  125. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  126. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  127. extern int StoreLogMsg(const char *fmt, ...);
  128. //============================================================
  129. // Private shared memory key define
  130. //============================================================
  131. #define ShmChargerKey 2001
  132. typedef struct Verion
  133. {
  134. char Version_FW[32];
  135. char Version_HW[32];
  136. }Ver;
  137. typedef struct PRESENTINPUTVOLTAGE
  138. {
  139. unsigned char inputType; // 0x00: Line to Line 0x01: Line to Neutral
  140. double L1N_L12;
  141. double L2N_L23;
  142. double L3N_L31;
  143. }PresentInputVoltage;
  144. typedef struct PRESENTOUTPUTVOLTAGE
  145. {
  146. double behindFuse_Voltage_C1;
  147. double behindRelay_Voltage_C1;
  148. double behindFuse_Voltage_C2;
  149. double behindRelay_Voltage_C2;
  150. }PresentOutputVoltage;
  151. typedef struct FANSPEED
  152. {
  153. unsigned short int speed[4];
  154. }FanSpeed;
  155. typedef struct TEMPERATURE
  156. {
  157. unsigned char point[8];
  158. }Temperature;
  159. typedef struct AUXPOWER
  160. {
  161. unsigned char voltage[8];
  162. }AuxPower;
  163. typedef struct RELAY
  164. {
  165. unsigned char relay_status[2][8];
  166. }Relay;
  167. typedef struct GFD
  168. {
  169. unsigned short int adc_value_positive[2];
  170. unsigned short int adc_value_negative[2];
  171. }Gfd;
  172. typedef struct GPIO_IN
  173. {
  174. unsigned char AC_Connector;
  175. unsigned char AC_MainBreaker;
  176. unsigned char SPD;
  177. unsigned char Door_Open;
  178. unsigned char GFD[2];
  179. unsigned char Button[2];
  180. unsigned char Button_Emergency;
  181. }Gpio_in;
  182. typedef struct GPIO_OUT
  183. {
  184. unsigned char AC_Connector;
  185. unsigned char Button_LED[2];
  186. unsigned char System_LED[4];
  187. }Gpio_out;
  188. typedef struct ALARM_LOG
  189. {
  190. unsigned char logArea;
  191. unsigned int alarmIndex;
  192. unsigned char log[8];
  193. }Alarm_Log;
  194. typedef struct BLE_CONFIG_DATA
  195. {
  196. unsigned char isLogin:1;
  197. unsigned char isRequestStart:1;
  198. unsigned char isRequestStop:1;
  199. }Ble_Config_Data;
  200. typedef struct BLE_LONGIN_CENTRAL_ID
  201. {
  202. unsigned char id[32];
  203. }Ble_Login_Central_Id;
  204. typedef struct RTC
  205. {
  206. unsigned short int year;
  207. unsigned char month;
  208. unsigned char day;
  209. unsigned char hour;
  210. unsigned char min;
  211. unsigned char sec;
  212. }Rtc;
  213. typedef struct PRESENTOUTPUTCURRENT
  214. {
  215. double L1N_L12[2];
  216. double L2N_L23[2];
  217. double L3N_L31[2];
  218. }Presentoutputcurrent;
  219. typedef struct AC_PRIMARY_MCU
  220. {
  221. unsigned char cp_state;
  222. unsigned int current_limit;
  223. float cp_voltage_positive;
  224. float cp_voltage_negtive;
  225. unsigned char locker_state;
  226. unsigned char relay_state;
  227. unsigned char shutter_state;
  228. unsigned char meter_state;
  229. unsigned char pp_state;
  230. unsigned char rating_current;
  231. unsigned char rotatory_switch;
  232. Relay relayState;
  233. }Ac_Primary_Mcu;
  234. typedef struct AC_PRIMARY_MCU_ALARM
  235. {
  236. union
  237. {
  238. unsigned long InputAlarmCode;
  239. struct
  240. {
  241. unsigned long OVP:1;
  242. unsigned long UVP:1;
  243. unsigned long OCP:1;
  244. unsigned long OTP:1;
  245. unsigned long gmi_fault:1;
  246. unsigned long cp_fault:1;
  247. unsigned long ac_leak:1;
  248. unsigned long dc_leak:1;
  249. unsigned long mcu_selftest_fail:1;
  250. unsigned long handshaking_timeout:1;
  251. unsigned long emergency_stop:1;
  252. unsigned long relay_welding:1;
  253. unsigned long leak_module_fail:1;
  254. unsigned long shutter_fault:1;
  255. unsigned long locker_fault:1;
  256. unsigned long power_drop:1;
  257. unsigned long circuit_short:1;
  258. unsigned long set_circuit:1;
  259. unsigned long relay_drive_fault:1;
  260. unsigned long comm_timeout:1;
  261. }bits;
  262. };
  263. }Ac_Primary_Mcu_Alarm;
  264. typedef struct AC_PRIMARY_MCU_LED
  265. {
  266. unsigned char mode;
  267. unsigned long alarm_code;
  268. }Ac_Primary_Mcu_Led;
  269. typedef struct EVSE_ID
  270. {
  271. unsigned char model_name[14];
  272. unsigned char serial_number[12];
  273. }Evse_Id;
  274. typedef struct AC_PRIMARY_MCU_CP_PWM_DUTY
  275. {
  276. unsigned int max_current;
  277. }Ac_Primary_Mcu_Cp_Pwm_Duty;
  278. typedef struct LEGACY_REQUEST
  279. {
  280. unsigned char isLegacyRequest:1;
  281. uint8_t isRelayOn:1;
  282. }Legacy_Request;
  283. typedef struct POWER_CONSUMPTION
  284. {
  285. uint32_t power_consumption;
  286. uint32_t power_consumption_at_start;
  287. }Power_Consumption;
  288. typedef struct MCU_OP_FLAG
  289. {
  290. unsigned char isSetModePass:1;
  291. unsigned char isSetSerialNumberPass:1;
  292. unsigned char isSetModelNamePass:1;
  293. unsigned char isReadFwVerPass:1;
  294. unsigned char isMcuUpgradeReq:1;
  295. unsigned char isSetCpPwmDuty:1;
  296. }Mcu_Op_Flag;
  297. typedef struct SYSTEM_ALARM_CODE
  298. {
  299. unsigned long SystemAlarmCode;
  300. }System_Alarm_Code;
  301. typedef struct OTHER_ALARM_CODE
  302. {
  303. unsigned long isHandshakingTimeOut:1;
  304. unsigned long isDcLeakage:1;
  305. unsigned long isACLeakage:1;
  306. }Other_Alarm_Code;
  307. typedef struct PILOT_VOLTAGE
  308. {
  309. float PilotVoltagePositive;
  310. float PilotVoltageNegative;
  311. }Pilot_Voltage;
  312. typedef struct FW_UPGRADE_INFO
  313. {
  314. int fwType;
  315. char modelName[17];
  316. char location[384];
  317. }Fw_Upgrade_Info;
  318. typedef struct GUN_PLUGIN_TIMES
  319. {
  320. uint32_t GunPluginTimes;
  321. }Gun_Plugin_Times;
  322. typedef struct MCU_RESET_REQUEST
  323. {
  324. unsigned char isMcuResetRequest:1;
  325. }Mcu_Reset_Request;
  326. typedef struct TIMEOUT_SPEC
  327. {
  328. int Setting_Timeout_Spec;
  329. int Present_Timeout_Spec;
  330. }Timeout_Spec;
  331. typedef struct SET_BREATHE_LED_TIMING
  332. {
  333. uint16_t set_Led_Action_Connected_Fade_In;
  334. uint16_t set_Led_Action_Connected_Fade_Out;
  335. uint16_t set_Led_Action_Authed_Fade_In;
  336. uint16_t set_Led_Action_Authed_Fade_Out;
  337. uint16_t Set_Led_Action_Chaging_Fade_In;
  338. uint16_t set_Led_Action_Chaging_Fade_Out;
  339. }Set_Breathe_Led_Timing;
  340. typedef struct SET_LED_BRIGHTNESS
  341. {
  342. uint8_t sector_1; // 0~1 AM and 1~2 AM
  343. uint8_t sector_2; // 2~3 AM and 3~4 AM
  344. uint8_t sector_3; // 4~5 AM and 5~6 AM
  345. uint8_t sector_4; // 6~7 AM and 7~8 AM
  346. uint8_t sector_5; // 8~9 AM and 9~10 AM
  347. uint8_t sector_6; // 10~11 AM and 11~12 AM
  348. uint8_t sector_7; // 12~13 PM and 13~14 PM
  349. uint8_t sector_8; // 14~15 PM and 15~16 PM
  350. uint8_t sector_9; // 16~17 PM and 17~18 PM
  351. uint8_t sector_10; // 18~19 PM and 19~20 PM
  352. uint8_t sector_11; // 20~21 PM and 21~22 PM
  353. uint8_t sector_12; // 22~23 PM and 23~24 PM
  354. }Set_Led_Brightness;
  355. typedef struct CCS_INFO
  356. {
  357. uint8_t PresentMsgFlowStatus;
  358. /* ISO15118_2018
  359. 0: Idle(wait B2 state),
  360. 1: CM_SLAC_PARM.REQ,
  361. 2: CM_SLAC_PARM.CNF,
  362. 3: CM_START_ATTEN_CHAR.IND
  363. 4: CM_MNBC_SOUND.IND,
  364. 5: CM_ATTEN_CHAR.IND,
  365. 6: CM_ATTEN_CHAR.RSP,
  366. 7: CM_VALIDATE.REQ
  367. 8: CM_VALIDATE.CNF,
  368. 9: CM_SLAC_MATCH.REQ,
  369. 10: CM_SLAC_MATCH.CNF,
  370. 11: CM_AMP_MAP.REQ
  371. 12: CM_AMP_MAP.CNF,
  372. 13: SLACC/SDP/TCP connection,
  373. 16: SupportedAppProtocolRequest,
  374. 17: SupportedAppProtocolResponse,
  375. 18: SessionSetupRequest
  376. 19: SessionSetupResponse,
  377. 20: ServiceDiscoveryRequest,
  378. 21: ServiceDiscoveryResponse
  379. 22: ServiceDetailRequest,
  380. 23: ServiceDetailResponse
  381. 24:ServiceAndPaymentSelectionRequest/ServiceSelectionRequest,
  382. 25: ServiceAndPaymentSelectionResponse/ServiceSelectionResponse
  383. 26: PaymentDetailsRequest/IdentificationDetailsRequest;,
  384. 27: PaymentDetailsResponse/IdentificationDetailsResponse,
  385. 28: AuthorizationRequest,
  386. 29: AuthorizationResponse,
  387. 30: CertificateUpdateRequest,
  388. 31: CertificateUpdateResponse,
  389. 32:CertificateInstallationRequest,
  390. 33: CertificateInstallationResponse
  391. 34: ChargeParameterDiscoveryRequest,
  392. 35: ChargeParameterDiscoveryResponse
  393. 36: CableCheckRequest,
  394. 37: CableCheckResponse,
  395. 38: PreChargeRequest,
  396. 39: PreChargeResponse
  397. 40: PowerDeliveryRequest start,
  398. 41: PowerDeliveryResponse start,
  399. 42: ChargingStatusRequest,
  400. 43: ChargingStatusResponse
  401. 44:CurrentDemandRequest,
  402. 45:CurrentDemandResponse,
  403. 46:MeteringReceiptRequest,
  404. 47: MeteringReceiptResponse
  405. 48: PowerDeliveryRequest end,
  406. 49: PowerDeliveryRequest end,
  407. 50: WeldingDetectionRequest,
  408. 51: WeldingDetectionResponse,
  409. 52: SessionStopRequest,
  410. 53: SessionStopResponse
  411. 253: Performance Timeout,
  412. 254: Sequence Timeout,
  413. 255: Fault
  414. */
  415. uint8_t ChargingPermission; /*0x00: Not ready yet, stay in idle mode or go into terminating process.
  416. 0x01: start charging process*/
  417. uint8_t ConnectorLockerStatus; /*0x00: released
  418. 0x01: locked*/
  419. uint8_t ConnectorTemperature1; /*0x00: -60°C ~ 0xFE: 194°C, resolution: 1°C, 0xFF: invalid*/
  420. uint8_t ConnectorTemperature2; /*0x00: -60°C ~ 0xFE: 194°C, resolution: 1°C, 0xFF: invalid*/
  421. uint8_t PresetBatterySoc; /*0 ~ 100*/
  422. uint16_t TargetVoltage; /*0x0000: 0 volt ~ 0xFFFF: 6553.5 volt, resolution: 0.1 volt*/
  423. uint16_t TargetCurrent; /*0x0000: 0 amp ~ 0xFFFF: 6553.5 amp, resolution: 0.1 amp*/
  424. uint16_t ChargingRemainTime; /*0x0000: 0 sec ~ 0xFFFF: 65535 sec, resolution: 1 second*/
  425. uint8_t BatteryChargeType; /*0x00: AC charging, 0x01: DC charging*/
  426. uint16_t TotalBatteryCapacity; /*0x0000: 0 kWh ~ 0xFFFF: 6553.5 kWh, resolution: 0.1 kWh*/
  427. uint16_t BatteryMaximumVoltage; /*0x0000: 0 volt ~ 0xFFFF: 6553.5 volt, resolution: 0.1 volt*/
  428. uint16_t BatteryMaximumCurrent; /*0x0000: 0 amp ~ 0xFFFF: 6553.5 amp, resolution: 0.1 amp*/
  429. uint8_t EVCCID[12];
  430. uint8_t CcsLibRev[32];
  431. uint16_t StopChargeEvent1:1;
  432. uint16_t StopChargeEvent2:1;
  433. uint16_t StopChargeEvent3:1;
  434. uint16_t StopChargeEvent4:1;
  435. uint16_t StopChargeEvent5:1;
  436. uint16_t StopChargeEvent6:1;
  437. uint16_t StopChargeEvent7:1;
  438. uint16_t StopChargeEvent8:1;
  439. }Ccs_Info;
  440. typedef struct GUN_INFO
  441. {
  442. Ver ver;
  443. PresentInputVoltage inputVoltage;
  444. Presentoutputcurrent outputCurrent;
  445. Temperature temperature;
  446. Ble_Config_Data bleConfigData;
  447. Ble_Login_Central_Id bleLoginCentralId;
  448. Rtc rtc;
  449. Ac_Primary_Mcu primaryMcuState;
  450. Ac_Primary_Mcu_Alarm primaryMcuAlarm;
  451. Ac_Primary_Mcu_Led primaryMcuLed;
  452. Mcu_Op_Flag mcuFlag;
  453. Power_Consumption powerConsumption;
  454. Legacy_Request legacyRequest;
  455. System_Alarm_Code systemAlarmCode;
  456. Ac_Primary_Mcu_Cp_Pwm_Duty primaryMcuCp_Pwn_Duty;
  457. Other_Alarm_Code otherAlarmCode;
  458. Pilot_Voltage PilotVoltage;
  459. Gun_Plugin_Times gunPluginTimes;
  460. Mcu_Reset_Request mcuResetRequest;
  461. Set_Breathe_Led_Timing setBreatheLedTiming;
  462. Set_Led_Brightness setLedBrightness;
  463. Ccs_Info ccsInfo;
  464. uint8_t ccsHandshakeState;
  465. uint8_t chargingMode;
  466. uint16_t targetCurrent;
  467. uint16_t isAuthPassEnd:1;
  468. uint16_t rfidReq:1;
  469. uint16_t isGunPlugged:1;
  470. uint16_t isInitialPass:1;
  471. uint16_t isSetBreatheLedTiming:1;
  472. uint16_t isSetLedBrightness:1;
  473. uint16_t isUnlockerConnetor:1;
  474. uint16_t isOperactive:1;
  475. }Gun_Info;
  476. struct Charger
  477. {
  478. Ver ver;
  479. Evse_Id evseId;
  480. Gun_Info gun_info[2];
  481. Fw_Upgrade_Info fwUpgradeInfo;
  482. Timeout_Spec timeoutSpec;
  483. uint8_t gun_selectd;
  484. uint8_t speaker_type;
  485. uint8_t isSpeakerOn:1;
  486. uint8_t isUpdateSuccess:1;
  487. };
  488. #endif /* CONFIG_MAIN_H_ */