cfg.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package main
  2. import "errors"
  3. var ErrCfgVerifyFailed = errors.New("config verify failed")
  4. type ConfigDefClass struct {
  5. Serial struct {
  6. ComPort string `toml:"port"`
  7. Baudrate int `toml:"baud"`
  8. DataBits int `toml:"data_bits"`
  9. StopBits float32 `toml:"stop_bits"`
  10. Parity string `toml:"parity"`
  11. } `toml:"serial"`
  12. Performance struct {
  13. TxBufferSize int `toml:"tx_buf_size"`
  14. RxBufferSize int `toml:"rx_buf_size"`
  15. } `toml:"perf"`
  16. Server struct {
  17. Mode string `toml:"mode"`
  18. TCPConfig struct {
  19. Bind string `toml:"bind"`
  20. } `toml:"tcp"`
  21. } `toml:"server"`
  22. }
  23. func ConfigVerifyAndFillDefault() error {
  24. // TODO: process default config
  25. if Config.Serial.ComPort == "" {
  26. App.Error("no serial port specified.")
  27. return ErrCfgVerifyFailed
  28. }
  29. if Config.Serial.Baudrate <= 0 {
  30. App.Error("baudrate should more than 0.")
  31. return ErrCfgVerifyFailed
  32. }
  33. if Config.Serial.DataBits == 0 {
  34. Config.Serial.DataBits = 8
  35. }
  36. if Config.Serial.StopBits == 0 {
  37. Config.Serial.StopBits = 1
  38. }
  39. if Config.Serial.Parity == "" {
  40. Config.Serial.Parity = "N"
  41. }
  42. if Config.Performance.TxBufferSize <= 0 {
  43. Config.Performance.TxBufferSize = 2048
  44. }
  45. if Config.Performance.RxBufferSize <= 0 {
  46. Config.Performance.RxBufferSize = 2048
  47. }
  48. return nil
  49. }