Skip to content

Commit

Permalink
working RX and TX
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-rabault committed Feb 13, 2024
1 parent d9ae911 commit 87ccf71
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
41 changes: 20 additions & 21 deletions network/ws_network/HAL/BROWSER/ws_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,48 +30,53 @@ using namespace client;
******************************************************************************/

static const char *s_url = WS_NETWORK_BROKER_ADDR;
[[cheerp::genericjs]] WebSocket *clientWebsocket;

/*******************************************************************************
* Function
******************************************************************************/

[[cheerp::genericjs]] void openEventCallback()
{
console.log("PROGRAM, openEventCallback");
console.log("Websocket opened!");
}

// [[cheerp::genericjs]] void messageEventCallback()
// {
// printf("MESSAGE !");
// std::vector<uint8_t> wasmData{1, 2, 3, 4};
// Ws_Reception(wasmData.data(), sizeof(wasmData));
// }
[[cheerp::genericjs]] void closeEventCallback()
{
console.log("Websocket closed!");
}

[[cheerp::genericjs]] void errorEventCallback()
{
console.log("Websocket error!");
}

void WsHAL_ReceptionWeb(const std::vector<uint8_t> &data)
{
printf("HAL_Ws_Reception");
Ws_Reception((uint8_t *)data.data(), data.size());
}

[[cheerp::genericjs]] void WsHAL_SendWeb(const std::vector<uint8_t> &data)
{
printf("WsHAL_SendWeb");
// Convert the data to ArrayBufferView*
ArrayBufferView *arrayBufferView = cheerp::MakeTypedArray(data.data(), data.size());
// clientWebsocket->send(data);
clientWebsocket->send(arrayBufferView);
}

[[cheerp::genericjs]] void WsHAL_InitWeb()
{
console.log("TEST!", s_url);
__asm__("const WebSocket = require('ws')");
WebSocket *clientWebsocket = new WebSocket(s_url);
clientWebsocket = new WebSocket(s_url);
clientWebsocket->addEventListener(
"open",
cheerp::Callback(openEventCallback));
// clientWebsocket->addEventListener(
// "message",
// cheerp::Callback(messageEventCallback));
clientWebsocket->addEventListener(
"close",
cheerp::Callback(closeEventCallback));
clientWebsocket->addEventListener(
"error",
cheerp::Callback(errorEventCallback));

clientWebsocket->addEventListener(
"message",
Expand All @@ -84,8 +89,6 @@ void WsHAL_ReceptionWeb(const std::vector<uint8_t> &data)
Uint8Array *wasmDataArray = cheerp::MakeTypedArray(wasmData.data(), wasmData.size()); // create a Uint8Array wrapper ove the linear memory held by the vector
wasmDataArray->set(dataArray); // copy the data with the builtin set() function. A for() loop with element-wise copy would do but this is faster
WsHAL_ReceptionWeb(wasmData); // NOTE: this now takes a const std::vector<uint8_t>& (or a std::vector<uint8_t> if you want to pass ownership)

// Ws_Reception(convertedData, sizeof convertedData);
}));
}

Expand All @@ -110,9 +113,7 @@ void WsHAL_Init(void)
PUBLIC
void WsHAL_Loop(void)
{
// Get the messages and put them on the reception function
// Ws_Reception(uint8_t *msg, (uint32_t)messageSize);
// If you have a callback you can create a callback instead of this function and call it here
// Nothing to do here
}

/******************************************************************************
Expand All @@ -123,8 +124,6 @@ void WsHAL_Loop(void)
******************************************************************************/
void WsHAL_Send(const uint8_t *data, uint16_t size)
{
// Send data to the websocket in binary mode
printf("WsHAL_Send size :%d data: %s\n", size, data);
// Convert the data and size to a std::vector
std::vector<uint8_t> wasmData(data, data + size);
WsHAL_SendWeb(wasmData);
Expand Down
6 changes: 0 additions & 6 deletions network/ws_network/src/ws_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,6 @@ void Ws_JobHandler(luos_phy_t *phy_ptr, phy_job_t *job)
_CRITICAL void Ws_Reception(uint8_t *data, uint32_t size)
{
LUOS_ASSERT((size <= sizeof(msg_t)) && (data != NULL));
printf("WS reception : ");
for (int i = 0; i < size; i++)
{
printf("%x ", data[i]);
}
printf("\n");
if (size >= sizeof(header_t))
{
// Reception is finished, we can give it to luos_engine
Expand Down

0 comments on commit 87ccf71

Please sign in to comment.