-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBluetoothHelperProtocol.cs
More file actions
180 lines (158 loc) · 6.94 KB
/
Copy pathBluetoothHelperProtocol.cs
File metadata and controls
180 lines (158 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System.Text.Json;
using SwitchifyPc.Protocol;
namespace SwitchifyPc.Core.Bluetooth;
public abstract record BluetoothHelperEvent(string Type);
public sealed record BluetoothReadyEvent() : BluetoothHelperEvent("ready");
public sealed record BluetoothUnavailableEvent(string Reason) : BluetoothHelperEvent("unavailable");
public sealed record BluetoothConnectedEvent(string ConnectionId, string Label) : BluetoothHelperEvent("connected");
public sealed record BluetoothMessageEvent(string ConnectionId, BluetoothFrame Frame) : BluetoothHelperEvent("message");
public sealed record BluetoothDisconnectedEvent(string ConnectionId, string Reason) : BluetoothHelperEvent("disconnected");
public sealed record BluetoothDiagnosticEvent(string Event) : BluetoothHelperEvent("diagnostic");
public sealed record BluetoothSystemStatusEvent(
bool AdapterPresent,
string RadioState,
bool? IsLowEnergySupported,
bool? IsPeripheralRoleSupported) : BluetoothHelperEvent("systemStatus");
public sealed record BluetoothErrorEvent(string Reason) : BluetoothHelperEvent("error");
public static class BluetoothHelperProtocol
{
public static readonly Guid ServiceUuid = Guid.Parse("7a78f7e8-1d6d-4d92-9ef0-1f89d3db21f4");
public static readonly Guid RxCharacteristicUuid = Guid.Parse("7a78f7e9-1d6d-4d92-9ef0-1f89d3db21f4");
public static readonly Guid TxCharacteristicUuid = Guid.Parse("7a78f7ea-1d6d-4d92-9ef0-1f89d3db21f4");
public static readonly Guid StatusCharacteristicUuid = Guid.Parse("7a78f7eb-1d6d-4d92-9ef0-1f89d3db21f4");
public static bool TryParseEvent(string json, out BluetoothHelperEvent? helperEvent)
{
helperEvent = null;
try
{
using JsonDocument document = JsonDocument.Parse(json);
JsonElement root = document.RootElement;
if (root.ValueKind != JsonValueKind.Object || !TryGetString(root, "type", out string type))
{
return false;
}
helperEvent = type switch
{
"ready" => new BluetoothReadyEvent(),
"unavailable" => ParseUnavailable(root),
"connected" => ParseConnected(root),
"message" => ParseMessage(root),
"disconnected" => ParseDisconnected(root),
"diagnostic" => ParseDiagnostic(root),
"systemStatus" => ParseSystemStatus(root),
"error" => ParseError(root),
_ => null
};
return helperEvent is not null;
}
catch (JsonException)
{
return false;
}
}
private static BluetoothHelperEvent? ParseUnavailable(JsonElement root)
{
return TryGetString(root, "reason", out string reason) && BluetoothStatusModel.UnavailableReasons.Contains(reason)
? new BluetoothUnavailableEvent(reason)
: null;
}
private static BluetoothHelperEvent? ParseConnected(JsonElement root)
{
return TryGetString(root, "connectionId", out string connectionId) &&
TryGetString(root, "label", out string label)
? new BluetoothConnectedEvent(connectionId, label)
: null;
}
private static BluetoothHelperEvent? ParseMessage(JsonElement root)
{
if (!TryGetString(root, "connectionId", out string connectionId) ||
!root.TryGetProperty("frame", out JsonElement frameElement) ||
frameElement.ValueKind != JsonValueKind.Object)
{
return null;
}
try
{
BluetoothFrame? frame = JsonSerializer.Deserialize<BluetoothFrame>(frameElement.GetRawText(), FrameJsonOptions);
return frame is not null && BluetoothFrameCodec.Validate(frame).Reason == "incomplete"
? new BluetoothMessageEvent(connectionId, frame)
: null;
}
catch (JsonException)
{
return null;
}
}
private static BluetoothHelperEvent? ParseDisconnected(JsonElement root)
{
return TryGetString(root, "connectionId", out string connectionId) &&
TryGetString(root, "reason", out string reason) &&
BluetoothStatusModel.DisconnectReasons.Contains(reason)
? new BluetoothDisconnectedEvent(connectionId, reason)
: null;
}
private static BluetoothHelperEvent? ParseDiagnostic(JsonElement root)
{
return TryGetString(root, "event", out string diagnosticEvent) && BluetoothStatusModel.DiagnosticEvents.Contains(diagnosticEvent)
? new BluetoothDiagnosticEvent(diagnosticEvent)
: null;
}
private static BluetoothHelperEvent? ParseSystemStatus(JsonElement root)
{
return TryGetBoolean(root, "adapterPresent", out bool adapterPresent) &&
TryGetString(root, "radioState", out string radioState) &&
BluetoothStatusModel.SystemRadioStates.Contains(radioState) &&
TryGetOptionalBoolean(root, "isLowEnergySupported", out bool? isLowEnergySupported) &&
TryGetOptionalBoolean(root, "isPeripheralRoleSupported", out bool? isPeripheralRoleSupported)
? new BluetoothSystemStatusEvent(adapterPresent, radioState, isLowEnergySupported, isPeripheralRoleSupported)
: null;
}
private static BluetoothHelperEvent? ParseError(JsonElement root)
{
return TryGetString(root, "reason", out string reason) ? new BluetoothErrorEvent(reason) : null;
}
private static bool TryGetString(JsonElement value, string propertyName, out string result)
{
result = "";
return value.TryGetProperty(propertyName, out JsonElement property) &&
property.ValueKind == JsonValueKind.String &&
!string.IsNullOrEmpty(result = property.GetString() ?? "");
}
private static bool TryGetBoolean(JsonElement value, string propertyName, out bool result)
{
result = false;
if (!value.TryGetProperty(propertyName, out JsonElement property)) return false;
if (property.ValueKind == JsonValueKind.True)
{
result = true;
return true;
}
if (property.ValueKind == JsonValueKind.False)
{
result = false;
return true;
}
return false;
}
private static bool TryGetOptionalBoolean(JsonElement value, string propertyName, out bool? result)
{
result = null;
if (!value.TryGetProperty(propertyName, out JsonElement property)) return false;
if (property.ValueKind == JsonValueKind.Null) return true;
if (property.ValueKind == JsonValueKind.True)
{
result = true;
return true;
}
if (property.ValueKind == JsonValueKind.False)
{
result = false;
return true;
}
return false;
}
private static readonly JsonSerializerOptions FrameJsonOptions = new()
{
PropertyNameCaseInsensitive = true
};
}