gateway_common.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Tencent is pleased to support the open source community by making IoT Hub
  3. available.
  4. * Copyright (C) 2018-2020 THL A29 Limited, a Tencent company. All rights
  5. reserved.
  6. * Licensed under the MIT License (the "License"); you may not use this file
  7. except in
  8. * compliance with the License. You may obtain a copy of the License at
  9. * http://opensource.org/licenses/MIT
  10. * Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is
  12. * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND,
  14. * either express or implied. See the License for the specific language
  15. governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. #ifndef IOT_GATEWAY_COMMON_H_
  20. #define IOT_GATEWAY_COMMON_H_
  21. #include "qcloud_iot_export.h"
  22. #define GATEWAY_PAYLOAD_BUFFER_LEN 1024
  23. #define GATEWAY_RECEIVE_BUFFER_LEN 1024
  24. #define GATEWAY_LOOP_MAX_COUNT 100
  25. #define SUBDEV_BIND_SIGN_LEN 64
  26. #define BIND_SIGN_KEY_SIZE MAX_SIZE_OF_DEVICE_SECRET
  27. #define GATEWAY_ONLINE_OP_STR "online"
  28. #define GATEWAY_OFFLIN_OP_STR "offline"
  29. #define GATEWAY_BIND_OP_STR "bind"
  30. #define GATEWAY_UNBIND_OP_STR "unbind"
  31. /* The format of operation of gateway topic */
  32. #define GATEWAY_TOPIC_OPERATION_FMT "$gateway/operation/%s/%s"
  33. /* The format of operation result of gateway topic */
  34. #define GATEWAY_TOPIC_OPERATION_RESULT_FMT "$gateway/operation/result/%s/%s"
  35. /* The format of gateway client id */
  36. #define GATEWAY_CLIENT_ID_FMT "%s/%s"
  37. /* The format of status cmd payload */
  38. #define GATEWAY_PAYLOAD_STATUS_FMT \
  39. "{\"type\":\"%s\",\"payload\":{\"devices\":[{\"product_id\":\"%s\"," \
  40. "\"device_name\":\"%s\"}]}}"
  41. /* The format of bind cmd payload */
  42. #define GATEWAY_PAYLOAD_OP_FMT \
  43. "{\"type\":\"%s\",\"payload\":{\"devices\":[{\"product_id\":\"%s\"," \
  44. "\"device_name\":\"%s\",\"signature\":\"%s\",\"random\":%d,\"timestamp\":%d," \
  45. "\"signmethod\":\"%s\",\"authtype\":\"%s\"}]}}"
  46. /* Subdevice seesion status */
  47. typedef enum _SubdevSessionStatus {
  48. /* Initial */
  49. SUBDEV_SEESION_STATUS_INIT,
  50. /* Online */
  51. SUBDEV_SEESION_STATUS_ONLINE,
  52. /* Offline */
  53. SUBDEV_SEESION_STATUS_OFFLINE,
  54. /* Maximum number of seesion status type */
  55. SUBDEV_SEESION_STATUS_MAX
  56. } SubdevSessionStatus;
  57. /* The structure of subdevice session */
  58. typedef struct _SubdevSession {
  59. char product_id[MAX_SIZE_OF_PRODUCT_ID + 1];
  60. char device_name[MAX_SIZE_OF_DEVICE_NAME + 1];
  61. SubdevSessionStatus session_status;
  62. struct _SubdevSession *next;
  63. } SubdevSession;
  64. /* The structure of common reply data */
  65. typedef struct _ReplyData {
  66. int32_t result;
  67. char client_id[MAX_SIZE_OF_CLIENT_ID + 1];
  68. } ReplyData;
  69. /* The structure of gateway data */
  70. typedef struct _GatewayData {
  71. int32_t sync_status;
  72. ReplyData online;
  73. ReplyData offline;
  74. ReplyData bind;
  75. ReplyData unbind;
  76. } GatewayData;
  77. /* The structure of gateway context */
  78. typedef struct _Gateway {
  79. void * mqtt;
  80. SubdevSession * session_list;
  81. GatewayData gateway_data;
  82. MQTTEventHandler event_handle;
  83. int is_construct;
  84. #ifdef MULTITHREAD_ENABLED
  85. bool yield_thread_running;
  86. int yield_thread_exit_code;
  87. #endif
  88. } Gateway;
  89. SubdevSession *subdev_add_session(Gateway *gateway, char *product_id, char *device_name);
  90. SubdevSession *subdev_find_session(Gateway *gateway, char *product_id, char *device_name);
  91. int subdev_remove_session(Gateway *gateway, char *product_id, char *device_name);
  92. int gateway_subscribe_unsubscribe_topic(Gateway *gateway, char *topic_filter, SubscribeParams *params,
  93. int is_subscribe);
  94. int gateway_subscribe_unsubscribe_default(Gateway *gateway, GatewayParam *param);
  95. int gateway_publish_sync(Gateway *gateway, char *topic, PublishParams *params, int32_t *result);
  96. int subdev_bind_hmac_sha1_cal(DeviceInfo *pDevInfo, char *signout, int max_signlen, int nonce, long timestamp);
  97. #endif /* IOT_GATEWAY_COMMON_H_ */