Skip to content

Commit 61efbc9

Browse files
refactor: Move Steam parser state from global variables to a per-device instance struct.
fixes #181
1 parent 5840a71 commit 61efbc9

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

src/components/bluepad32/parser/uni_hid_parser_steam.c

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,34 +90,47 @@ static uint8_t cmd_disable_lizard[] = {
9090
};
9191
// clang-format on
9292

93-
static gatt_client_service_t le_steam_service;
94-
static gatt_client_characteristic_t le_steam_characteristic_report;
95-
static hci_con_handle_t connection_handle;
96-
static uni_hid_device_t* device;
97-
static steam_query_state_t query_state;
93+
typedef struct {
94+
gatt_client_service_t service;
95+
gatt_client_characteristic_t characteristic_report;
96+
steam_query_state_t query_state;
97+
} steam_instance_t;
98+
_Static_assert(sizeof(steam_instance_t) < HID_DEVICE_MAX_PARSER_DATA, "Steam instance too big");
9899

99100
static void parse_buttons(struct uni_hid_device_s* d, const uint8_t* data);
100101
static void parse_triggers(struct uni_hid_device_s* d, const uint8_t* data);
101102
static void parse_thumbstick(struct uni_hid_device_s* d, const uint8_t* data);
102103
static void parse_right_pad(struct uni_hid_device_s* d, const uint8_t* data);
103104

105+
static steam_instance_t* get_steam_instance(uni_hid_device_t* d) {
106+
return (steam_instance_t*)&d->parser_data[0];
107+
}
108+
104109
// TODO: Make it easier for "parsers" to write/read/get notified from characteristics
105110
static void uni_steam_handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t* packet, uint16_t size) {
106111
uint8_t att_status;
112+
uni_hid_device_t* device;
113+
steam_instance_t* ins;
107114

108-
ARG_UNUSED(channel);
109115
ARG_UNUSED(size);
110116

111117
if (packet_type != HCI_EVENT_PACKET)
112118
return;
113119

120+
device = uni_hid_device_get_instance_for_connection_handle(channel);
121+
if (!device) {
122+
loge("Steam: Invalid device for connection handle: %d\n", channel);
123+
return;
124+
}
125+
ins = get_steam_instance(device);
126+
114127
uint8_t event = hci_event_packet_get_type(packet);
115-
switch (query_state) {
128+
switch (ins->query_state) {
116129
case STATE_QUERY_SERVICE:
117130
switch (event) {
118131
case GATT_EVENT_SERVICE_QUERY_RESULT:
119132
// store service (we expect only one)
120-
gatt_event_service_query_result_get_service(packet, &le_steam_service);
133+
gatt_event_service_query_result_get_service(packet, &ins->service);
121134
break;
122135
case GATT_EVENT_QUERY_COMPLETE:
123136
att_status = gatt_event_query_complete_get_att_status(packet);
@@ -128,9 +141,9 @@ static void uni_steam_handle_gatt_client_event(uint8_t packet_type, uint16_t cha
128141
break;
129142
}
130143
// service query complete, look for characteristic report
131-
query_state = STATE_QUERY_CHARACTERISTIC_REPORT;
144+
ins->query_state = STATE_QUERY_CHARACTERISTIC_REPORT;
132145
gatt_client_discover_characteristics_for_service_by_uuid128(uni_steam_handle_gatt_client_event,
133-
connection_handle, &le_steam_service,
146+
channel, &ins->service,
134147
le_steam_characteristic_report_uuid);
135148
break;
136149
default:
@@ -147,13 +160,13 @@ static void uni_steam_handle_gatt_client_event(uint8_t packet_type, uint16_t cha
147160
// gap_disconnect(connection_handle);
148161
break;
149162
}
150-
gatt_client_write_value_of_characteristic(uni_steam_handle_gatt_client_event, connection_handle,
151-
le_steam_characteristic_report.value_handle,
163+
gatt_client_write_value_of_characteristic(uni_steam_handle_gatt_client_event, channel,
164+
ins->characteristic_report.value_handle,
152165
sizeof(cmd_clear_mappings), cmd_clear_mappings);
153-
query_state = STATE_QUERY_CLEAR_MAPPINGS;
166+
ins->query_state = STATE_QUERY_CLEAR_MAPPINGS;
154167
break;
155168
case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
156-
gatt_event_characteristic_query_result_get_characteristic(packet, &le_steam_characteristic_report);
169+
gatt_event_characteristic_query_result_get_characteristic(packet, &ins->characteristic_report);
157170
break;
158171
default:
159172
loge("Steam: Unknown event: %#x\n", event);
@@ -169,10 +182,10 @@ static void uni_steam_handle_gatt_client_event(uint8_t packet_type, uint16_t cha
169182
// gap_disconnect(connection_handle);
170183
break;
171184
}
172-
gatt_client_write_value_of_characteristic(uni_steam_handle_gatt_client_event, connection_handle,
173-
le_steam_characteristic_report.value_handle,
185+
gatt_client_write_value_of_characteristic(uni_steam_handle_gatt_client_event, channel,
186+
ins->characteristic_report.value_handle,
174187
sizeof(cmd_disable_lizard), cmd_disable_lizard);
175-
query_state = STATE_QUERY_DISABLE_LIZARD;
188+
ins->query_state = STATE_QUERY_DISABLE_LIZARD;
176189
break;
177190
default:
178191
loge("Steam: Unknown event: %#x\n", event);
@@ -189,7 +202,7 @@ static void uni_steam_handle_gatt_client_event(uint8_t packet_type, uint16_t cha
189202
break;
190203
}
191204
uni_hid_device_set_ready_complete(device);
192-
query_state = STATE_QUERY_END;
205+
ins->query_state = STATE_QUERY_END;
193206
break;
194207
default:
195208
loge("Steam: Unknown event: %#x\n", event);
@@ -198,15 +211,16 @@ static void uni_steam_handle_gatt_client_event(uint8_t packet_type, uint16_t cha
198211
case STATE_QUERY_END:
199212
// pass-through
200213
default:
201-
loge("Steam: Unknown query state: %#x\n", query_state);
214+
loge("Steam: Unknown query state: %#x\n", ins->query_state);
202215
break;
203216
}
204217
}
205218

206219
void uni_hid_parser_steam_setup(struct uni_hid_device_s* d) {
207-
device = d;
208-
connection_handle = d->conn.handle;
209-
query_state = STATE_QUERY_SERVICE;
220+
steam_instance_t* ins = get_steam_instance(d);
221+
memset(ins, 0, sizeof(*ins));
222+
223+
ins->query_state = STATE_QUERY_SERVICE;
210224
gatt_client_discover_primary_services_by_uuid128(uni_steam_handle_gatt_client_event, d->conn.handle,
211225
le_steam_service_uuid);
212226

0 commit comments

Comments
 (0)