gagent_def.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * File : gagent_def.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-01-03 flyingcys first version
  23. */
  24. #include <rtthread.h>
  25. #include <string.h>
  26. #include <lwip/sockets.h>
  27. #include "webclient.h"
  28. #include "paho_mqtt.h"
  29. #ifndef PKG_GAGENT_CLOUD_DEBUG
  30. #define PKG_GAGENT_CLOUD_DEBUG 1
  31. #endif
  32. #if (PKG_GAGENT_CLOUD_DEBUG == 1)
  33. #define gagent_dbg rt_kprintf("[gagent dbg:%d] %s ", __LINE__, __FUNCTION__);rt_kprintf
  34. #define gagent_err rt_kprintf("[gagent err:%d] %s ", __LINE__, __FUNCTION__);rt_kprintf
  35. #else
  36. #define gagent_dbg(...)
  37. #define gagent_err(...)
  38. #endif
  39. #define MAX_CLIENT 8
  40. #define BUF_LEN 2048
  41. #define HEAD_LEN 5
  42. #define UDP_RECV_ERROR 199
  43. #define TCP_RECV_ERROR_BASE 100
  44. #define LOG_IP_BUF_LENGTH 16
  45. #define HARD_VERSION "01RTT001"
  46. #define SOFT_VERSION "04020020"
  47. #define G_SERVICE_DOMAIN "api.gizwits.com"
  48. #define G_SERVICE_PORT "80"
  49. #define G_M2M_DOMAIN "sandbox.gizwits.com"
  50. #define G_M2M_PORT "1883"
  51. #define G_M2M_SSL_PORT "8883"
  52. #define DID_LENGTH 22
  53. enum GAGENT_HARD_TYPE
  54. {
  55. GAGENT_HARD_SOC = 1,
  56. GAGENT_HARD_MCU = 2,
  57. };
  58. typedef struct _con_st con_st;
  59. struct _con_st
  60. {
  61. char mac[32 + 1];
  62. char did[32 + 1];
  63. char passcode[16 + 1];
  64. char pk[48 + 1];
  65. char pk_secret[48 + 1];
  66. char hard_version[16 + 1];
  67. char soft_version[16 + 1];
  68. };
  69. typedef struct _cloud_st cloud_st;
  70. struct _cloud_st
  71. {
  72. con_st *con;
  73. //
  74. char mqtt_server[128];
  75. int mqtt_port;
  76. char sub_topic[2][128];
  77. //
  78. char recv_buf[BUF_LEN];
  79. int recv_len;
  80. char send_buf[BUF_LEN];
  81. int send_len;
  82. //
  83. char ota_info[128];
  84. int sn;
  85. };
  86. typedef struct _lan_st lan_st;
  87. struct _lan_st
  88. {
  89. con_st *con;
  90. //
  91. int client_fd[MAX_CLIENT];
  92. int tcp_server;
  93. int udp_server;
  94. struct sockaddr_in tcp_server_addr;
  95. struct sockaddr_in udp_socket_addr;
  96. struct sockaddr_in broadcast_to;
  97. //local
  98. int local_sock;
  99. int local_port;
  100. //
  101. char recv_buf[BUF_LEN];
  102. int recv_len;
  103. char send_buf[BUF_LEN];
  104. int send_len;
  105. //
  106. int sn;
  107. };
  108. enum CMD_FROM_TYPE
  109. {
  110. CMD_FROM_LAN = 0,
  111. CMD_FROM_MQTT,
  112. };
  113. enum UDP_SEND_TYPE
  114. {
  115. UDP_SEND_TYPE_DISCOVER = 0,
  116. UDP_SEND_TYPE_BOARDCAST,
  117. };
  118. int gagent_add_pkcs(char *src, int len);
  119. uint16_t gagent_parse_rem_len(const uint8_t* buf);
  120. uint8_t gagent_num_rem_len_bytes(const uint8_t* buf);
  121. int gagent_get_one_packet(char *packet, int *data_len, rt_uint8_t *len_num, int remain_len);
  122. int gagent_set_one_packet(char *packet, uint8_t action, uint8_t *buf, uint32_t buf_len);
  123. uint8_t gagent_get_rem_len(int length, char *buf);
  124. int gagent_strtohex(char *pbDest, char *pbSrc, int nLen);
  125. int gagent_lan_send_packet(lan_st *lan, rt_uint8_t action, rt_uint8_t *buf, rt_uint16_t buf_len);
  126. int gagent_lan_init(lan_st *lan);
  127. int gagent_cloud_register(cloud_st *cloud);
  128. int gagent_cloud_provision(cloud_st *cloud);
  129. int gagent_cloud_check_ota(cloud_st *cloud);
  130. int gagent_mqtt_send_packet(cloud_st *cloud, rt_uint8_t action, rt_uint8_t *kv, rt_uint16_t kv_len);
  131. int gagent_mqtt_init(cloud_st *cloud);
  132. //#endif