ua_mqtt_pal.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright (c) 2018 Fraunhofer IOSB (Author: Lukas Meling)
  6. * Copyright (c) 2019 Kalycito Infotech Private Limited
  7. */
  8. #include "../../deps/mqtt-c/mqtt.h"
  9. #include <open62541/network_tcp.h>
  10. ssize_t
  11. mqtt_pal_sendall(mqtt_pal_socket_handle fd, const void* buf, size_t len, int flags) {
  12. UA_Connection *connection = (UA_Connection*) fd->connection;
  13. UA_ByteString sendBuffer;
  14. sendBuffer.data = (UA_Byte*)UA_malloc(len);
  15. sendBuffer.length = len;
  16. memcpy(sendBuffer.data, buf, len);
  17. UA_StatusCode ret = connection->send(connection, &sendBuffer);
  18. if(ret != UA_STATUSCODE_GOOD)
  19. return -1;
  20. return (ssize_t)len;
  21. }
  22. ssize_t
  23. mqtt_pal_recvall(mqtt_pal_socket_handle fd, void* buf, size_t bufsz, int flags) {
  24. UA_Connection *connection = (UA_Connection*) fd->connection;
  25. connection->config.recvBufferSize = (UA_UInt32) bufsz;
  26. UA_ByteString inBuffer;
  27. UA_StatusCode ret = connection->recv(connection, &inBuffer, fd->timeout);
  28. if(ret == UA_STATUSCODE_GOOD ){
  29. // Buffer received, copy to recv buffer
  30. memcpy(buf, inBuffer.data, inBuffer.length);
  31. ssize_t bytesReceived = (ssize_t)inBuffer.length;
  32. /* free recv buffer */
  33. connection->releaseRecvBuffer(connection, &inBuffer);
  34. return bytesReceived;
  35. }else if(ret == UA_STATUSCODE_GOODNONCRITICALTIMEOUT){
  36. return 0;
  37. }else{
  38. return -1; //error case, no free necessary
  39. }
  40. }