network.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2019-12-09 21:31:02
  5. * @LastEditTime: 2020-10-17 14:14:41
  6. * @Description: the code belongs to jiejie, please keep the author
  7. * information and source code according to the license.
  8. */
  9. #ifndef _NETWORK_H_
  10. #define _NETWORK_H_
  11. #include "mqtt_defconfig.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #define NETWORK_CHANNEL_TCP 0
  16. #define NETWORK_CHANNEL_TLS 1
  17. typedef struct network {
  18. const char* host;
  19. const char* port;
  20. int socket;
  21. #ifndef MQTT_NETWORK_TYPE_NO_TLS
  22. int channel; /* tcp or tls */
  23. const char* ca_crt;
  24. unsigned int ca_crt_len;
  25. unsigned int timeout_ms; // SSL handshake timeout in millisecond
  26. void* nettype_tls_params;
  27. #endif
  28. } network_t;
  29. int network_init(network_t* n,
  30. const char* host,
  31. const char* port,
  32. const char* ca);
  33. int network_set_ca(network_t* n, const char* ca);
  34. void network_set_channel(network_t* n, int channel);
  35. int network_set_host_port(network_t* n, char* host, char* port);
  36. int network_read(network_t* n, unsigned char* buf, int len, int timeout);
  37. int network_write(network_t* n, unsigned char* buf, int len, int timeout);
  38. int network_connect(network_t* n);
  39. void network_disconnect(network_t* n);
  40. void network_release(network_t* n);
  41. #ifdef __cplusplus
  42. }
  43. #endif
  44. #endif