|
1 | 1 | package protocol |
2 | 2 |
|
3 | | -import "strconv" |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | +) |
| 6 | + |
| 7 | +type HamlibError string |
| 8 | + |
| 9 | +func (e HamlibError) Error() string { |
| 10 | + message, ok := HamlibErrorMessages[string(e)] |
| 11 | + if ok { |
| 12 | + return message |
| 13 | + } |
| 14 | + return "unknown hamlib error: " + string(e) |
| 15 | +} |
| 16 | + |
| 17 | +const ( |
| 18 | + InvalidParameter HamlibError = "-1" |
| 19 | + InvalidConfiguration HamlibError = "-2" |
| 20 | + MemoryShortage HamlibError = "-3" |
| 21 | + FeatureNotImplemented HamlibError = "-4" |
| 22 | + CommunicationTimedOut HamlibError = "-5" |
| 23 | + IOError HamlibError = "-6" |
| 24 | + InternalHamlibError HamlibError = "-7" |
| 25 | + ProtocolError HamlibError = "-8" |
| 26 | + CommandRejectedByTheRig HamlibError = "-9" |
| 27 | + CommandPerformedButTruncated HamlibError = "-10" |
| 28 | + FeatureNotAvailable HamlibError = "-11" |
| 29 | + TargetVFOUnaccessible HamlibError = "-12" |
| 30 | + CommunicationBusError HamlibError = "-13" |
| 31 | + CommunicationBusCollision HamlibError = "-14" |
| 32 | + NullRigHandle HamlibError = "-15" |
| 33 | + InvalidVFO HamlibError = "-16" |
| 34 | + ArgumentOutOfDomain HamlibError = "-17" |
| 35 | + FunctionDeprecated HamlibError = "-18" |
| 36 | + SecurityError HamlibError = "-19" |
| 37 | + RigNotPoweredOn HamlibError = "-20" |
| 38 | +) |
| 39 | + |
| 40 | +var HamlibErrorMessages = map[string]string{ |
| 41 | + "0": "Command completed successfully", |
| 42 | + "-1": "Invalid parameter", |
| 43 | + "-2": "Invalid configuration", |
| 44 | + "-3": "Memory shortage", |
| 45 | + "-4": "Feature not implemented", |
| 46 | + "-5": "Communication timed out", |
| 47 | + "-6": "IO error", |
| 48 | + "-7": "Internal Hamlib error", |
| 49 | + "-8": "Protocol error", |
| 50 | + "-9": "Command rejected by the rig", |
| 51 | + "-10": "Command performed, but arg truncated, result not guaranteed", |
| 52 | + "-11": "Feature not available", |
| 53 | + "-12": "Target VFO unaccessible", |
| 54 | + "-13": "Communication bus error", |
| 55 | + "-14": "Communication bus collision", |
| 56 | + "-15": "NULL RIG handle or invalid pointer parameter", |
| 57 | + "-16": "Invalid VFO", |
| 58 | + "-17": "Argument out of domain of func", |
| 59 | + "-18": "Function deprecated", |
| 60 | + "-19": "Security error password not provided or crypto failure", |
| 61 | + "-20": "Rig is not powered on", |
| 62 | +} |
4 | 63 |
|
5 | 64 | func OKResponse(cmd CommandKey) Response { |
6 | 65 | return Response{Command: cmd, Result: "0"} |
7 | 66 | } |
8 | 67 |
|
| 68 | +func ErrorResponse(cmd CommandKey, err HamlibError) Response { |
| 69 | + return Response{Command: cmd, Result: string(err)} |
| 70 | +} |
| 71 | + |
9 | 72 | func GetFreqResponse(frequency int) Response { |
10 | 73 | return Response{ |
11 | 74 | Command: "get_freq", |
@@ -77,6 +140,28 @@ func GetPTTResponse(enabled bool) Response { |
77 | 140 | } |
78 | 141 | } |
79 | 142 |
|
| 143 | +func GetLevelKeyspdResponse(wpm int) Response { |
| 144 | + return Response{ |
| 145 | + Command: "get_level_keyspd", |
| 146 | + Data: []string{strconv.Itoa(wpm)}, |
| 147 | + Keys: []string{"KEYSPD"}, |
| 148 | + Result: "0", |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func GetLockModeResponse(enabled bool) Response { |
| 153 | + lockModeEnabled := "0" |
| 154 | + if enabled { |
| 155 | + lockModeEnabled = "1" |
| 156 | + } |
| 157 | + return Response{ |
| 158 | + Command: "get_lock_mode", |
| 159 | + Data: []string{lockModeEnabled}, |
| 160 | + Keys: []string{"Locked"}, |
| 161 | + Result: "0", |
| 162 | + } |
| 163 | +} |
| 164 | + |
80 | 165 | var NoResponse = Response{} |
81 | 166 |
|
82 | 167 | var ChkVFOResponse = Response{ |
|
0 commit comments