libjoybus
Joybus implementation for 32-bit MCUs
Loading...
Searching...
No Matches
target.h
1
47
48#pragma once
49
50#include <stdbool.h>
51#include <stdint.h>
52
53#include <joybus/attributes.h>
54#include <joybus/errors.h>
55
56struct joybus_target;
57
59#define JOYBUS_TARGET(target) ((struct joybus_target *)(target))
60
68typedef void (*joybus_target_response_cb)(const uint8_t *response, uint8_t len, void *user_data);
69
85 int (*byte_received)(struct joybus_target *target, const uint8_t *command, uint8_t byte_idx,
86 joybus_target_response_cb send_response, void *user_data);
87};
88
94 const struct joybus_target_api *api;
95
98
101};
102
116static inline int joybus_target_byte_received(struct joybus_target *target, const uint8_t *command, uint8_t byte_idx,
117 joybus_target_response_cb send_response, void *user_data)
118{
119 // No target attached to handle the command
120 if (!target)
122
123 return target->api->byte_received(target, command, byte_idx, send_response, user_data);
124}
125
132static inline bool joybus_target_is_attached(struct joybus_target *target)
133{
134 return target->attached;
135}
136
static int joybus_target_byte_received(struct joybus_target *target, const uint8_t *command, uint8_t byte_idx, joybus_target_response_cb send_response, void *user_data)
Handle a received command byte for a single Joybus target.
Definition target.h:116
void(* joybus_target_response_cb)(const uint8_t *response, uint8_t len, void *user_data)
Callback type for sending responses from target command handlers.
Definition target.h:68
static bool joybus_target_is_attached(struct joybus_target *target)
Check if a target is currently attached to a bus.
Definition target.h:132
@ JOYBUS_ERR_NOT_SUPPORTED
Command not supported by Joybus target.
Definition errors.h:27
API for implementing a Joybus target.
Definition target.h:73
int(* byte_received)(struct joybus_target *target, const uint8_t *command, uint8_t byte_idx, joybus_target_response_cb send_response, void *user_data)
Handle a received command byte.
Definition target.h:85
Interface for a Joybus target, a device on the Joybus which responds to commands from a host.
Definition target.h:92
bool attached
Whether the target is currently attached to a bus.
Definition target.h:97
const struct joybus_target_api * api
API for handling received commands.
Definition target.h:94
struct joybus_target * next
The next target attached to the same bus, in attachment order.
Definition target.h:100