onenet.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * File : onenet.h
  3. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Change Logs:
  20. * Date Author Notes
  21. * 2018-04-24 chenyong first version
  22. */
  23. #ifndef _ONENET_H_
  24. #define _ONENET_H_
  25. #include <rtthread.h>
  26. #include <cJSON.h>
  27. #define ONENET_DEBUG 1
  28. #define ONENET_SW_VERSION "1.0.0"
  29. #ifndef ONENET_MALLOC
  30. #define ONENET_MALLOC rt_malloc
  31. #endif
  32. #ifndef ONENET_CALLOC
  33. #define ONENET_CALLOC rt_calloc
  34. #endif
  35. #ifndef ONENET_FREE
  36. #define ONENET_FREE rt_free
  37. #endif
  38. #ifndef ONENET_MQTT_SUBTOPIC
  39. #define ONENET_MQTT_SUBTOPIC "$sys/" ONENET_INFO_PROID "/" ONENET_INFO_DEVID "/thing/property/set"
  40. #endif
  41. #define ONENET_SERVER_URL "tcp://183.230.40.96:1883"
  42. #define ONENET_INFO_DEVID_LEN 16
  43. #define ONENET_INFO_APIKEY_LEN 32
  44. #define ONENET_INFO_PROID_LEN 16
  45. #define ONENET_INFO_AUTH_LEN 300
  46. #define ONENET_INFO_NAME_LEN 64
  47. #define ONENET_INFO_URL_LEN 32
  48. #define ONENET_DATASTREAM_NAME_MAX 32
  49. struct rt_onenet_info
  50. {
  51. char device_id[ONENET_INFO_DEVID_LEN];
  52. char api_key[ONENET_INFO_APIKEY_LEN];
  53. char pro_id[ONENET_INFO_PROID_LEN];
  54. char auth_info[ONENET_INFO_AUTH_LEN];
  55. char server_uri[ONENET_INFO_URL_LEN];
  56. };
  57. typedef struct rt_onenet_info *rt_onenet_info_t;
  58. /* onenet datastream info */
  59. struct rt_onenet_ds_info
  60. {
  61. char id[ONENET_DATASTREAM_NAME_MAX];
  62. char tags[ONENET_DATASTREAM_NAME_MAX];
  63. char update_time[ONENET_DATASTREAM_NAME_MAX];
  64. char create_time[ONENET_DATASTREAM_NAME_MAX];
  65. char unit[ONENET_DATASTREAM_NAME_MAX];
  66. char unit_symbol[ONENET_DATASTREAM_NAME_MAX];
  67. char current_value[ONENET_DATASTREAM_NAME_MAX];
  68. };
  69. typedef struct rt_onenet_ds_info *rt_onenet_ds_info_t;
  70. /* OneNET MQTT initialize. */
  71. int onenet_mqtt_init(void);
  72. /* Publish MQTT data to subscribe topic. */
  73. rt_err_t onenet_mqtt_publish(const char *topic, const uint8_t *msg, size_t len);
  74. #ifdef RT_USING_DFS
  75. /* Publish MQTT binary data to onenet by path. */
  76. rt_err_t onenet_mqtt_upload_bin_by_path(const char *ds_name, const char *bin_path);
  77. #endif
  78. /* Publish MQTT binary data to onenet. */
  79. rt_err_t onenet_mqtt_upload_bin(const char *ds_name, uint8_t *bin, size_t len);
  80. /* Publish MQTT string data to onenet. */
  81. rt_err_t onenet_mqtt_upload_string(const char *ds_name, const char *str);
  82. /* Publish MQTT digit data to onenet. */
  83. rt_err_t onenet_mqtt_upload_digit(const char *ds_name, const double digit);
  84. /* Device send data to OneNET cloud. */
  85. rt_err_t onenet_http_upload_digit(const char *ds_name, const double digit);
  86. rt_err_t onenet_http_upload_string(const char *ds_name, const char *str);
  87. #ifdef ONENET_USING_AUTO_REGISTER
  88. /* Register a device to OneNET cloud. */
  89. rt_err_t onenet_http_register_device(const char *dev_name, const char *auth_info);
  90. #endif
  91. /* get a datastream from OneNET cloud. */
  92. rt_err_t onenet_http_get_datastream(const char *ds_name, struct rt_onenet_ds_info *datastream);
  93. /* get datapoints from OneNET cloud. Returned cJSON need to be free when user finished using the data. */
  94. cJSON *onenet_get_dp_by_limit(char *ds_name, size_t limit);
  95. cJSON *onenet_get_dp_by_start_end(char *ds_name, uint32_t start, uint32_t end, size_t limit);
  96. cJSON *onenet_get_dp_by_start_duration(char *ds_name, uint32_t start, size_t duration, size_t limit);
  97. /* Set the command response callback function. User needs to malloc memory for response data. */
  98. void onenet_set_cmd_rsp_cb(void(*cmd_rsp_cb)(uint8_t *recv_data, size_t recv_size, uint8_t **resp_data, size_t *resp_size));
  99. /* ========================== User port function ============================ */
  100. #ifdef ONENET_USING_AUTO_REGISTER
  101. /* Save device info. */
  102. rt_err_t onenet_port_save_device_info(char *dev_id, char *api_key);
  103. /* Get device name and auth info for register. */
  104. rt_err_t onenet_port_get_register_info(char *dev_name, char *auth_info);
  105. /* Get device info. */
  106. rt_err_t onenet_port_get_device_info(char *dev_id, char *api_key, char *auth_info);
  107. /* Check the device has been registered or not. */
  108. rt_bool_t onenet_port_is_registed(void);
  109. #endif
  110. #endif /* _ONENET_H_ */