My Project 1
Yet another Wii Balance Board Driver
|
Core functions necessary for basic interaction with the Wii Balance Board. More...
Functions | |
void | print_info (const LogLevel *is_debug_level, const char *message, const unsigned char *buffer, int length, const WiiBalanceBoard *board) |
Logs messages with different verbosity levels, providing diagnostics and raw data output. More... | |
int | find_wii_balance_board (WiiBalanceBoard *board) |
Finds the Wii Balance Board by scanning nearby Bluetooth devices. More... | |
void | send_command (int sock, const unsigned char *command, int length) |
Sends a command to the Wii Balance Board over the control socket. More... | |
int | connect_l2cap (const char *bdaddr_str, uint16_t psm) |
Establishes a L2CAP connection with the Wii Balance Board. More... | |
void | process_received_data (int bytes_read, unsigned char *buffer, WiiBalanceBoard *board) |
Processes received data from the Wii Balance Board. More... | |
void * | threadFunction (void *arg) |
Thread function for monitoring user input to control the Wii Balance Board. More... | |
void | createThread (WiiBalanceBoard *board, pthread_t *threadId) |
Creates a new thread and starts the threadFunction to monitor user control. More... | |
int | is_valid_mac (int argc, char *argv[]) |
Validates a given MAC address for format and content. More... | |
Core functions necessary for basic interaction with the Wii Balance Board.
This group includes the primary functions needed to find, connect, and send commands to the Wii Balance Board. These functions form the foundation of the YAWiiBBD project and enable essential Bluetooth communication and control.
int connect_l2cap | ( | const char * | bdaddr_str, |
uint16_t | psm | ||
) |
Establishes a L2CAP connection with the Wii Balance Board.
This function creates an L2CAP Bluetooth socket and connects to the specified service (PSM) on the Wii Balance Board using its MAC address. It returns the socket descriptor or exits on failure.
bdaddr_str | Constant character string representing the Bluetooth MAC address. |
psm | Integer specifying the Protocol/Service Multiplexer (PSM) channel. |
void createThread | ( | WiiBalanceBoard * | board, |
pthread_t * | threadId | ||
) |
Creates a new thread and starts the threadFunction
to monitor user control.
This function creates a new thread and instructs it to execute the threadFunction
. In case of errors, an error message is displayed, the is_running
flag of the Wii Balance Board is set to false
, and the program exits with an error code.
board | Pointer to the WiiBalanceBoard object monitored by the threadFunction. |
threadId | Pointer to the pthread_t variable where the ID of the new thread will be stored. |
int find_wii_balance_board | ( | WiiBalanceBoard * | board | ) |
Finds the Wii Balance Board by scanning nearby Bluetooth devices.
Requires the device to be in pairing mode and not jet paired
This function scans for Bluetooth devices in the area and identifies the Wii Balance Board based on its specific name. If found, the board’s MAC address is stored directly in the board
structure.
board | Pointer to WiiBalanceBoard structure where the MAC address is stored. |
int is_valid_mac | ( | int | argc, |
char * | argv[] | ||
) |
Validates a given MAC address for format and content.
Checks whether a valid MAC address has been passed as a single argument. The address must be exactly 17 characters long and conform to the format XX:XX:XX:XX:XX:XX
, where X
is a hexadecimal character (0-9
, A-F
, a-f
). The function returns 1
if the input meets the requirements; otherwise, it returns 0
.
In case of an invalid format or erroneous inputs, specific error messages are generated to indicate possible causes.
argc | Number of arguments passed to the program. |
argv | Array of arguments, where the second (argv[1] ) should be the MAC address. |
1
if the MAC address is valid; otherwise, 0
. void print_info | ( | const LogLevel * | is_debug_level, |
const char * | message, | ||
const unsigned char * | buffer, | ||
int | length, | ||
const WiiBalanceBoard * | board | ||
) |
Logs messages with different verbosity levels, providing diagnostics and raw data output.
This function outputs messages to the console with optional raw data from the Wii Balance Board. It supports multiple verbosity levels (RAW, DEBUG, and VERBOSE), controlled by the LogLevel
enum. The function is essential for debugging and tracking the board's status and output during development.
This function's implementation interacts closely with the Wiimote protocol, pushing the limits of my current understanding of the protocol. Despite attempts to isolate functionality in essentials
, it remains a complex function with room for refinement.
Potential areas for improvement include:
level | Pointer to a LogLevel enum indicating the verbosity level (RAW, DEBUG, VERBOSE). |
message | Constant character string representing the message to be logged. |
buffer | Optional byte array containing raw data to display if needed. This parameter is processed only when provided (not NULL). |
length | Length of the byte array (ignored if buffer is NULL). |
board | Pointer to the WiiBalanceBoard structure for accessing board-specific data. |
void process_received_data | ( | int | bytes_read, |
unsigned char * | buffer, | ||
WiiBalanceBoard * | board | ||
) |
Processes received data from the Wii Balance Board.
This function checks the received data and evaluates it. If a certain condition is met (e.g., the receive code indicates an error), the is_running
status is set to false
.
bytes_read | Number of bytes read in the buffer . |
buffer | Buffer containing the received data. |
board | Pointer to the WiiBalanceBoard structure that holds the current status. |
void send_command | ( | int | sock, |
const unsigned char * | command, | ||
int | length | ||
) |
Sends a command to the Wii Balance Board over the control socket.
This function transmits a command to the board using the specified socket. If an error occurs, the program will exit with a failure status.
sock | Integer control socket descriptor. |
command | Byte array of command data to be sent. |
length | Integer representing the length of the command array. |
void * threadFunction | ( | void * | arg | ) |
Thread function for monitoring user input to control the Wii Balance Board.
Waits for user input via the console or the power button of the board. If the user presses the button or the enter key without additional characters, the function sets the is_running
flag of the WiiBalanceBoard
object to false
and exits the thread.
arg | A void pointer to the WiiBalanceBoard object passed to the function and called at runtime to change the status. |
NULL
– indicates that the thread has terminated.