Monado OpenXR Runtime
Loading...
Searching...
No Matches
vp2_hid.c File Reference

Implementation of the Vive Pro 2 HID interface. More...

#include "xrt/xrt_byte_order.h"
#include "xrt/xrt_compiler.h"
#include "os/os_hid.h"
#include "os/os_time.h"
#include "os/os_threading.h"
#include "util/u_logging.h"
#include "util/u_debug.h"
#include "util/u_misc.h"
#include "util/u_file.h"
#include "math/m_api.h"
#include <inttypes.h>
#include "vp2_hid.h"
#include "vp2_config.h"
#include "vp2_protocol.h"
Include dependency graph for vp2_hid.c:

Data Structures

struct  vp2_hid
 

Macros

#define VP2_TRACE(vp2, ...)   U_LOG_IFL_T(vp2->log_level, __VA_ARGS__)
 
#define VP2_DEBUG(vp2, ...)   U_LOG_IFL_D(vp2->log_level, __VA_ARGS__)
 
#define VP2_INFO(vp2, ...)   U_LOG_IFL_I(vp2->log_level, __VA_ARGS__)
 
#define VP2_WARN(vp2, ...)   U_LOG_IFL_W(vp2->log_level, __VA_ARGS__)
 
#define VP2_ERROR(vp2, ...)   U_LOG_IFL_E(vp2->log_level, __VA_ARGS__)
 
#define VP2_CONFIG_FOLDER   "vive_pro_2"
 
#define VP2_WRITE_DELAY_NS   (U_TIME_1MS_IN_NS * 10)
 Delay after every write so the MCU can handle it before the next one, the HTC driver waits this long between display requests.
 
#define VP2_BUTTON_DEBOUNCE_REPORTS   2
 The buttons glitch for single status reports, for example mute flashing on while pressing the volume buttons, so a new button state is only accepted once it has been seen in this many reports in a row.
 

Functions

static int vp2_write (struct vp2_hid *vp2, uint8_t id, const uint8_t *data, size_t size)
 
static int vp2_write_feature (struct vp2_hid *vp2, uint8_t id, uint16_t sub_id, const uint8_t *data, size_t size)
 
static int vp2_set_resolution (struct vp2_hid *vp2, enum vp2_resolution resolution)
 
static int vp2_read (struct vp2_hid *vp2, uint8_t id, const uint8_t *prefix, size_t prefix_size, uint8_t *out_data, size_t out_size)
 
static void vp2_drain_reads (struct vp2_hid *vp2)
 Reads and drops any reports that are already queued, for example status reports from an earlier session.
 
static int vp2_get_config_size (struct vp2_hid *vp2, size_t *out_size)
 
static int vp2_read_config (struct vp2_hid *vp2, char **out_config, size_t *out_size)
 
static int vp2_string_command (struct vp2_hid *vp2, const char *command, char *out, size_t out_size)
 Sends an ASCII command on VP2_REPORT_ID_STRING and reads back its reply as a null terminated string.
 
static int vp2_read_serial (struct vp2_hid *vp2, char *out, size_t out_size)
 
static int vp2_load_serial (struct vp2_hid *vp2)
 
static int vp2_load_config (struct vp2_hid *vp2)
 
static int vp2_send_noise_cancelling_commands (struct vp2_hid *vp2, const char **commands, size_t command_count)
 
static int vp2_enable_noise_cancelling (struct vp2_hid *vp2)
 
static int vp2_disable_noise_cancelling (struct vp2_hid *vp2)
 
static int vp2_write_feature_event (struct vp2_hid *vp2, const struct vp2_feature_event *event)
 
static int vp2_set_status_polling (struct vp2_hid *vp2, bool enable)
 
static int vp2_set_power_state (struct vp2_hid *vp2, enum vp2_power_state state)
 
static int vp2_get_feature_state (struct vp2_hid *vp2, struct vp2_feature_state *out_state)
 Any GET_FEATURE of VP2_REPORT_ID_FEATURE returns the state block, no request is needed.
 
static int vp2_display_query (struct vp2_hid *vp2, const char *request, char *out, size_t out_size)
 Sends a display request getter and reads back its reply.
 
static const char * vp2_power_state_to_string (uint8_t state)
 
static void vp2_flatten_reply (char *reply)
 Some replies are several lines, put them on one.
 
static void vp2_log_feature_state (struct vp2_hid *vp2, const struct vp2_feature_state *state)
 
static void vp2_log_status (struct vp2_hid *vp2)
 Logs everything the headset reports about itself.
 
static uint16_t get_raw_buttons (const struct vp2_status_report *report)
 Button bytes from a status report, system in the high byte and the audio button in the low byte.
 
static void handle_status_packet (struct vp2_hid *vp2, const struct vp2_status_report *report, uint16_t buttons)
 
static void * status_thread_runner (void *ptr)
 
int vp2_hid_open (struct os_hid_device *hid_dev, struct vp2_hid **out_hid)
 
enum vp2_resolution vp2_get_resolution (struct vp2_hid *vp2)
 
struct vp2_config * vp2_get_config (struct vp2_hid *vpd)
 
const char * vp2_get_serial (struct vp2_hid *vp2)
 
int vp2_set_noise_cancelling (struct vp2_hid *vp2, bool enable)
 
float vp2_get_brightness (struct vp2_hid *vp2)
 
int vp2_set_brightness (struct vp2_hid *vp2, float brightness)
 
int vp2_prepare_shutdown (struct vp2_hid *vp2)
 Prepares the display panels to have video terminated.
 
bool vp2_get_status (struct vp2_hid *vp2, bool *out_proximity, float *out_lens_separation_meters, bool *out_system, bool *out_volume_up, bool *out_volume_down, bool *out_mute_mic)
 
void vp2_hid_destroy (struct vp2_hid *vp2)
 

Detailed Description

Implementation of the Vive Pro 2 HID interface.

Author
Beyley Cardellio ep1cm.nosp@m.1n10.nosp@m.n123@.nosp@m.gmai.nosp@m.l.com

Macro Definition Documentation

◆ VP2_BUTTON_DEBOUNCE_REPORTS

#define VP2_BUTTON_DEBOUNCE_REPORTS   2

The buttons glitch for single status reports, for example mute flashing on while pressing the volume buttons, so a new button state is only accepted once it has been seen in this many reports in a row.

A status report comes in roughly every 52ms.

◆ VP2_WRITE_DELAY_NS

#define VP2_WRITE_DELAY_NS   (U_TIME_1MS_IN_NS * 10)

Delay after every write so the MCU can handle it before the next one, the HTC driver waits this long between display requests.

Function Documentation

◆ get_raw_buttons()

static uint16_t get_raw_buttons ( const struct vp2_status_report *  report)
static

Button bytes from a status report, system in the high byte and the audio button in the low byte.

References vp2_status_report::audio_button, and vp2_status_report::system_button.

Referenced by status_thread_runner().

◆ status_thread_runner()

◆ vp2_display_query()

static int vp2_display_query ( struct vp2_hid *  vp2,
const char *  request,
char *  out,
size_t  out_size 
)
static

Sends a display request getter and reads back its reply.

References VP2_COMMAND_DISPLAY_REQUEST, VP2_REPORT_ID_FEATURE, and vp2_string_command().

Referenced by vp2_log_status().

◆ vp2_drain_reads()

static void vp2_drain_reads ( struct vp2_hid *  vp2)
static

Reads and drops any reports that are already queued, for example status reports from an earlier session.

References VP2_DATA_SIZE.

◆ vp2_flatten_reply()

static void vp2_flatten_reply ( char *  reply)
static

Some replies are several lines, put them on one.

Referenced by vp2_log_status().

◆ vp2_get_feature_state()

static int vp2_get_feature_state ( struct vp2_hid *  vp2,
struct vp2_feature_state *  out_state 
)
static

Any GET_FEATURE of VP2_REPORT_ID_FEATURE returns the state block, no request is needed.

References vp2_feature_state::id, vp2_feature_state::sub_id, VP2_COMMAND_FEATURE_STATE, and VP2_REPORT_ID_FEATURE.

Referenced by vp2_log_status().

◆ vp2_log_status()

static void vp2_log_status ( struct vp2_hid *  vp2)
static

Logs everything the headset reports about itself.

This reads replies, so it has to run while the status thread isn't running and the status reports are off. Only read-only commands are used.

References ARRAY_SIZE, U_LOG_IFL_T_HEX, VP2_DATA_SIZE, vp2_display_query(), vp2_flatten_reply(), vp2_get_feature_state(), and vp2_string_command().

◆ vp2_prepare_shutdown()

int vp2_prepare_shutdown ( struct vp2_hid *  vp2)

Prepares the display panels to have video terminated.

References os_nanosleep(), U_TIME_1MS_IN_NS, and VP2_POWER_STATE_IDLE.

◆ vp2_string_command()

static int vp2_string_command ( struct vp2_hid *  vp2,
const char *  command,
char *  out,
size_t  out_size 
)
static

Sends an ASCII command on VP2_REPORT_ID_STRING and reads back its reply as a null terminated string.

References VP2_REPORT_ID_STRING.

Referenced by vp2_display_query(), and vp2_log_status().