Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 11, 2025

The WebSocket server on port 8080 was previously unidirectional (logging only). This change enables full bidirectional communication for custom characteristic commands using the same protocol as BLE, with responses sent back through the WebSocket connection.

Changes

  • WebSocketAppender: Added bidirectional message handling

    • Poll connected clients for incoming messages
    • Register onMessage callback on client connection
    • Route received messages to BLE_ss2kCustomCharacteristic::process()
    • Track which clients have sent commands
    • Send responses back through WebSocket to command-sending clients only
  • BLE_Custom_Characteristic: Send responses via WebSocket

    • Call webSocketAppender.SendResponse() after setting characteristic values
    • Handle both standard and string response formats
  • Main.h: Added extern declaration for webSocketAppender to enable cross-module access

  • Documentation: Updated CustomCharacteristic.md to document WebSocket as a fully bidirectional access method alongside BLE and DirCon

Usage

const ws = new WebSocket('ws://192.168.1.100:8080');
ws.binaryType = 'arraybuffer';

ws.onopen = () => {
  // Write 263 to simulatedSpeed (0x06) using standard custom characteristic format
  const command = new Uint8Array([0x02, 0x06, 0x07, 0x01]);
  ws.send(command.buffer);
};

ws.onmessage = (event) => {
  if (event.data instanceof ArrayBuffer) {
    // Binary response from custom characteristic command
    const response = new Uint8Array(event.data);
    console.log('Response:', Array.from(response).map(b => '0x' + b.toString(16).padStart(2, '0')).join(', '));
  } else {
    // Text message (logging output)
    console.log('Log:', event.data);
  }
};

Custom characteristic commands now have three access paths with full bidirectional communication: BLE (direct), DirCon (TCP/8081), and WebSocket (8080). All three use the same processing logic via BLE_ss2kCustomCharacteristic::process() and send responses back to clients.

Note: Only WebSocket clients that have sent at least one command will receive command responses. Clients that only listen for logs will continue to receive text log messages but not binary command responses.

Original prompt

We currently have a websocket server for logging. I'd like to expand that same websocket server so that it can recieve custom characteristic commands. The custom characteristic commands should be processed by the existing BLE_CustomCharacteristic.cpp, much like we do for Dircon with the other BLE characteristics.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Expand websocket server to receive custom characteristic commands Extend WebSocket server to receive custom characteristic commands Dec 11, 2025
Copilot AI requested a review from doudar December 11, 2025 22:46
@doudar
Copy link
Owner

doudar commented Dec 11, 2025

@copilot we should implement sending the response over the web socket as well. This could be simply adding a line for it near pCharacteristic->indicate(); in BLE_Custom_Characteristic.cpp . We should only send these messages websocket clients that have previously sent a command.

Copy link
Contributor Author

Copilot AI commented Dec 11, 2025

@copilot we should implement sending the response over the web socket as well. This could be simply adding a line for it near pCharacteristic->indicate(); in BLE_Custom_Characteristic.cpp . We should ...

Implemented in commit d8c48bb.

The WebSocket server now sends responses back to clients that have sent commands:

  • Added tracking via _clientSentCommand[] array
  • Only clients that send at least one command receive responses
  • Responses are sent as binary messages via SendResponse() method
  • Called after pCharacteristic->setValue() in both response paths (standard and string returns)

The documentation has been updated with an example showing how to receive responses via ws.onmessage.

Copilot AI changed the title Extend WebSocket server to receive custom characteristic commands Extend WebSocket server with bidirectional custom characteristic commands Dec 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants