Skip to content

Commit

Permalink
coap_ws.c: Combine header and data in output from coap_ws_write()
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdeep1 committed Oct 27, 2023
1 parent 17e44c7 commit 50b5f29
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions src/coap_ws.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ coap_ws_log_header(const coap_session_t *session, const uint8_t *header) {
for (i = 0; i < extra_hdr_len; i++) {
snprintf(&buf[i*3], 4, " %02x", header[i]);
}
coap_log_debug("* %s: ws: h recv %4d bytes\n",
coap_session_str(session), extra_hdr_len);
coap_log_debug("* %s: WS header:%s\n", coap_session_str(session), buf);
#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG */
}
Expand Down Expand Up @@ -206,6 +208,7 @@ coap_ws_write(coap_session_t *session, const uint8_t *data, size_t datalen) {
uint8_t ws_header[COAP_MAX_FS];
ssize_t hdr_len = 2;
ssize_t ret;
uint8_t *wdata;

/* If lower layer not yet up, return error */
if (!session->ws) {
Expand Down Expand Up @@ -253,35 +256,30 @@ coap_ws_write(coap_session_t *session, const uint8_t *data, size_t datalen) {
hdr_len += 4;
}
coap_ws_log_header(session, ws_header);
ret = session->sock.lfunc[COAP_LAYER_WS].l_write(session, ws_header, hdr_len);
if (ret != hdr_len) {
wdata = coap_malloc_type(COAP_STRING, datalen + hdr_len);
if (!wdata) {
errno = ENOMEM;
return -1;
}
memcpy(wdata, ws_header, hdr_len);
memcpy(&wdata[hdr_len], data, datalen);
if (session->ws->state == COAP_SESSION_TYPE_CLIENT) {
/* Need to mask the data */
uint8_t *wdata = coap_malloc_type(COAP_STRING, datalen);

if (!wdata) {
errno = ENOMEM;
return -1;
}
session->ws->data_size = datalen;
memcpy(wdata, data, datalen);
coap_ws_mask_data(session, wdata, datalen);
ret = session->sock.lfunc[COAP_LAYER_WS].l_write(session, wdata, datalen);
coap_free_type(COAP_STRING, wdata);
} else {
ret = session->sock.lfunc[COAP_LAYER_WS].l_write(session, data, datalen);
coap_ws_mask_data(session, &wdata[hdr_len], datalen);
}
if (ret <= 0) {
ret = session->sock.lfunc[COAP_LAYER_WS].l_write(session, wdata, datalen + hdr_len);
coap_free_type(COAP_STRING, wdata);
if (ret < hdr_len) {
return ret;
}
if (ret == (ssize_t)datalen)
coap_log_debug("* %s: ws h: sent %4zd bytes\n",
coap_session_str(session), hdr_len);
if (ret == (ssize_t)(datalen + hdr_len))
coap_log_debug("* %s: ws: sent %4zd bytes\n",
coap_session_str(session), ret);
coap_session_str(session), ret - hdr_len);
else
coap_log_debug("* %s: ws: sent %4zd of %4zd bytes\n",
coap_session_str(session), ret, datalen);
coap_session_str(session), ret, datalen - hdr_len);
return datalen;
}

Expand Down

0 comments on commit 50b5f29

Please sign in to comment.