network.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2019-12-09 21:30:54
  5. * @LastEditTime: 2020-06-05 17:17:48
  6. * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
  7. */
  8. #include "platform_timer.h"
  9. #include "platform_memory.h"
  10. #include "nettype_tcp.h"
  11. #ifndef MQTT_NETWORK_TYPE_NO_TLS
  12. #include "nettype_tls.h"
  13. #endif
  14. int network_read(network_t *n, unsigned char *buf, int len, int timeout)
  15. {
  16. #ifndef MQTT_NETWORK_TYPE_NO_TLS
  17. if (n->channel)
  18. return nettype_tls_read(n, buf, len, timeout);
  19. #endif
  20. return nettype_tcp_read(n, buf, len, timeout);
  21. }
  22. int network_write(network_t *n, unsigned char *buf, int len, int timeout)
  23. {
  24. #ifndef MQTT_NETWORK_TYPE_NO_TLS
  25. if (n->channel)
  26. return nettype_tls_write(n, buf, len, timeout);
  27. #endif
  28. return nettype_tcp_write(n, buf, len, timeout);
  29. }
  30. int network_connect(network_t *n)
  31. {
  32. #ifndef MQTT_NETWORK_TYPE_NO_TLS
  33. if (n->channel)
  34. return nettype_tls_connect(n);
  35. #endif
  36. return nettype_tcp_connect(n);
  37. }
  38. void network_disconnect(network_t *n)
  39. {
  40. #ifndef MQTT_NETWORK_TYPE_NO_TLS
  41. if (n->channel)
  42. nettype_tls_disconnect(n);
  43. else
  44. #endif
  45. nettype_tcp_disconnect(n);
  46. }
  47. int network_init(network_t *n, const char *host, const char *port, const char *ca)
  48. {
  49. if (NULL == n)
  50. RETURN_ERROR(MQTT_NULL_VALUE_ERROR);
  51. n->socket = -1;
  52. n->host = host;
  53. n->port = port;
  54. #ifndef MQTT_NETWORK_TYPE_NO_TLS
  55. n->channel = 0;
  56. if (NULL != ca) {
  57. network_set_ca(n, ca);
  58. }
  59. #endif
  60. RETURN_ERROR(MQTT_SUCCESS_ERROR);
  61. }
  62. void network_release(network_t* n)
  63. {
  64. if (n->socket >= 0)
  65. {
  66. network_disconnect(n);
  67. memset(n, 0, sizeof(network_t));
  68. }
  69. }
  70. void network_set_channel(network_t *n, int channel)
  71. {
  72. #ifndef MQTT_NETWORK_TYPE_NO_TLS
  73. n->channel = channel;
  74. #endif
  75. }
  76. int network_set_ca(network_t *n, const char *ca)
  77. {
  78. #ifndef MQTT_NETWORK_TYPE_NO_TLS
  79. if ((NULL == n) || (NULL == ca))
  80. RETURN_ERROR(MQTT_NULL_VALUE_ERROR);
  81. n->ca_crt = ca;
  82. n->ca_crt_len = strlen(ca);
  83. n->channel = NETWORK_CHANNEL_TLS;
  84. n->timeout_ms = MQTT_TLS_HANDSHAKE_TIMEOUT;
  85. #endif
  86. RETURN_ERROR(MQTT_SUCCESS_ERROR);
  87. }
  88. int network_set_host_port(network_t* n, char *host, char *port)
  89. {
  90. if (!(n && host && port))
  91. RETURN_ERROR(MQTT_NULL_VALUE_ERROR);
  92. n->host = host;
  93. n->port = port;
  94. RETURN_ERROR(MQTT_SUCCESS_ERROR);
  95. }