My Project 1
Yet another Wii Balance Board Driver
Functions
Essential Functions

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...
 

Detailed Description

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.

Function Documentation

◆ connect_l2cap()

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.

Parameters
bdaddr_strConstant character string representing the Bluetooth MAC address.
psmInteger specifying the Protocol/Service Multiplexer (PSM) channel.
Returns
Socket descriptor on success, exits program on failure.

◆ createThread()

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.

Parameters
boardPointer to the WiiBalanceBoard object monitored by the threadFunction.
threadIdPointer to the pthread_t variable where the ID of the new thread will be stored.

◆ find_wii_balance_board()

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.

Parameters
boardPointer to WiiBalanceBoard structure where the MAC address is stored.
Returns
0 on success (board found), -1 if not found.

◆ is_valid_mac()

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.

Parameters
argcNumber of arguments passed to the program.
argvArray of arguments, where the second (argv[1]) should be the MAC address.
Returns
1 if the MAC address is valid; otherwise, 0.

◆ print_info()

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:

  • Consistent use of pointers: Aiming to rely on pointers wherever possible to reduce stack usage and optimize memory management.
  • Modularizing verbosity level handling: Simplifying logic for handling different verbosity levels, potentially through helper functions to make the code more manageable.
Parameters
levelPointer to a LogLevel enum indicating the verbosity level (RAW, DEBUG, VERBOSE).
messageConstant character string representing the message to be logged.
bufferOptional byte array containing raw data to display if needed. This parameter is processed only when provided (not NULL).
lengthLength of the byte array (ignored if buffer is NULL).
boardPointer to the WiiBalanceBoard structure for accessing board-specific data.

◆ process_received_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.

Parameters
bytes_readNumber of bytes read in the buffer.
bufferBuffer containing the received data.
boardPointer to the WiiBalanceBoard structure that holds the current status.

◆ send_command()

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.

Parameters
sockInteger control socket descriptor.
commandByte array of command data to be sent.
lengthInteger representing the length of the command array.

◆ threadFunction()

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.

Parameters
argA void pointer to the WiiBalanceBoard object passed to the function and called at runtime to change the status.
Returns
Always NULL – indicates that the thread has terminated.