diff --git a/features/cellular/framework/AT/ATHandler.cpp b/features/cellular/framework/AT/ATHandler.cpp index 9b35a37b8f..a9591ed866 100644 --- a/features/cellular/framework/AT/ATHandler.cpp +++ b/features/cellular/framework/AT/ATHandler.cpp @@ -1597,3 +1597,18 @@ void ATHandler::set_send_delay(uint16_t send_delay) { _at_send_delay = send_delay; } + +void ATHandler::write_hex_string(char *str, size_t size) +{ + // do common checks before sending subparameter + if (check_cmd_send() == false) { + return; + } + + char hexbuf[2]; + for (int i = 0; i < size; i++) { + hexbuf[0] = hex_values[((str[i]) >> 4) & 0x0F]; + hexbuf[1] = hex_values[(str[i]) & 0x0F]; + write(hexbuf, 2); + } +} diff --git a/features/cellular/framework/AT/ATHandler.h b/features/cellular/framework/AT/ATHandler.h index 5a1352ca24..21be72bbe6 100644 --- a/features/cellular/framework/AT/ATHandler.h +++ b/features/cellular/framework/AT/ATHandler.h @@ -419,6 +419,14 @@ public: */ ssize_t read_hex_string(char *str, size_t size); + /** Converts contained chars to their hex ascii value and writes the resulting string to the file handle + * For example: "AV" to "4156". + * + * @param str input buffer to be converted to hex ascii + * @param size of the input param str + */ + void write_hex_string(char *str, size_t size); + /** Reads as string and converts result to integer. Supports only non-negative integers. * * @return the non-negative integer or -1 in case of error. diff --git a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularStack.cpp b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularStack.cpp index 03c39342b6..848b5e0ee3 100644 --- a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularStack.cpp +++ b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularStack.cpp @@ -185,11 +185,6 @@ nsapi_size_or_error_t QUECTEL_BC95_CellularStack::socket_sendto_impl(CellularSoc return NSAPI_ERROR_PARAMETER; } - char *hexstr = new char[size * 2 + 1]; - int hexlen = char_str_to_hex_str((const char *)data, size, hexstr); - // NULL terminated for write_string - hexstr[hexlen] = 0; - if (socket->proto == NSAPI_UDP) { _at.cmd_start("AT+NSOST="); _at.write_int(socket->id); @@ -201,11 +196,10 @@ nsapi_size_or_error_t QUECTEL_BC95_CellularStack::socket_sendto_impl(CellularSoc _at.write_int(socket->id); _at.write_int(size); } else { - delete [] hexstr; return NSAPI_ERROR_PARAMETER; } - _at.write_string(hexstr, false); + _at.write_hex_string((char *)data, size); _at.cmd_stop(); _at.resp_start(); // skip socket id @@ -213,8 +207,6 @@ nsapi_size_or_error_t QUECTEL_BC95_CellularStack::socket_sendto_impl(CellularSoc sent_len = _at.read_int(); _at.resp_stop(); - delete [] hexstr; - if (_at.get_last_error() == NSAPI_ERROR_OK) { return sent_len; }