gagent_def.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #ifndef __GAGENT_DEF_H__
  25. #define __GAGENT_DEF_H__
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #include <rtthread.h>
  30. #include <string.h>
  31. #include <lwip/sockets.h>
  32. #include "webclient.h"
  33. #include "paho_mqtt.h"
  34. #ifdef PKG_GAGENT_CLOUD_DEBUG
  35. #define gagent_dbg rt_kprintf("[gagent dbg:%d] %s ", __LINE__, __FUNCTION__);rt_kprintf
  36. #define gagent_err rt_kprintf("[gagent err:%d] %s ", __LINE__, __FUNCTION__);rt_kprintf
  37. #else
  38. #define gagent_dbg(...)
  39. #define gagent_err(...)
  40. #endif
  41. #define MAX_CLIENT 8
  42. #define BUF_LEN 2048
  43. #define HEAD_LEN 5
  44. #define UDP_RECV_ERROR 199
  45. #define TCP_RECV_ERROR_BASE 100
  46. #define LOG_IP_BUF_LENGTH 16
  47. #define HARD_VERSION "01RTT001"
  48. #define SOFT_VERSION "04020020"
  49. #define G_SERVICE_DOMAIN "api.gizwits.com"
  50. #define G_SERVICE_PORT "80"
  51. #define G_M2M_DOMAIN "sandbox.gizwits.com"
  52. #define G_M2M_PORT "1883"
  53. #define G_M2M_SSL_PORT "8883"
  54. #define DID_LENGTH 22
  55. enum GAGENT_HARD_TYPE
  56. {
  57. GAGENT_HARD_SOC = 1,
  58. GAGENT_HARD_MCU = 2,
  59. };
  60. typedef struct gagent_config con_st;
  61. struct _cloud_st
  62. {
  63. con_st *con;
  64. //
  65. char mqtt_server[128];
  66. int mqtt_port;
  67. char sub_topic[2][128];
  68. //
  69. char recv_buf[BUF_LEN];
  70. int recv_len;
  71. char send_buf[BUF_LEN];
  72. int send_len;
  73. //
  74. char ota_info[128];
  75. int sn;
  76. };
  77. typedef struct _cloud_st cloud_st;
  78. typedef struct _lan_st lan_st;
  79. struct _lan_st
  80. {
  81. con_st *con;
  82. //
  83. int client_fd[MAX_CLIENT];
  84. int tcp_server;
  85. int udp_server;
  86. struct sockaddr_in tcp_server_addr;
  87. struct sockaddr_in udp_socket_addr;
  88. struct sockaddr_in broadcast_to;
  89. //local
  90. int local_sock;
  91. int local_port;
  92. //
  93. char recv_buf[BUF_LEN];
  94. int recv_len;
  95. char send_buf[BUF_LEN];
  96. int send_len;
  97. //
  98. int sn;
  99. };
  100. enum CMD_FROM_TYPE
  101. {
  102. CMD_FROM_LAN = 0,
  103. CMD_FROM_MQTT,
  104. };
  105. enum UDP_SEND_TYPE
  106. {
  107. UDP_SEND_TYPE_DISCOVER = 0,
  108. UDP_SEND_TYPE_BOARDCAST,
  109. };
  110. int gagent_add_pkcs(char *src, int len);
  111. uint16_t gagent_parse_rem_len(const uint8_t* buf);
  112. uint8_t gagent_num_rem_len_bytes(const uint8_t* buf);
  113. int gagent_get_one_packet(char *packet, int *data_len, rt_uint8_t *len_num, int remain_len);
  114. int gagent_set_one_packet(char *packet, uint8_t action, uint8_t *buf, uint32_t buf_len);
  115. uint8_t gagent_get_rem_len(int length, char *buf);
  116. int gagent_strtohex(char *pbDest, char *pbSrc, int nLen);
  117. int gagent_lan_send_packet(lan_st *lan, rt_uint8_t action, rt_uint8_t *buf, rt_uint16_t buf_len);
  118. int gagent_lan_init(lan_st *lan);
  119. int gagent_cloud_register(cloud_st *cloud);
  120. int gagent_cloud_provision(cloud_st *cloud);
  121. int gagent_cloud_check_ota(cloud_st *cloud);
  122. int gagent_mqtt_send_packet(cloud_st *cloud, rt_uint8_t action, rt_uint8_t *kv, rt_uint16_t kv_len);
  123. int gagent_mqtt_init(cloud_st *cloud);
  124. #ifdef __cplusplus
  125. }
  126. #endif
  127. #endif