gagent_cloud_demo.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * File : gagent_tool.c
  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 "gagent_cloud.h"
  26. #ifdef RT_USING_FINSH
  27. #include <finsh.h>
  28. #endif
  29. #ifdef RT_USING_DFS
  30. #include <dfs_posix.h>
  31. #endif
  32. #define DEMO_PRODUCT_KEY "1371df627fa64e849f32fe17ebd5fd38"
  33. #define DEMO_PRODUCT_KEY_SECRET "067a83b650544d979bb3d4d147f32034"
  34. #define DEMO_MAC "\xBC\x12\x34\x56\x78\x23"
  35. static gagent_cloud_param gagent_param;
  36. int gagent_read_param(struct gagent_config *param, rt_uint32_t len)
  37. {
  38. /* read param */
  39. #ifdef RT_USING_DFS
  40. int fd;
  41. fd = open("/sdcard/demo", O_RDONLY, 0);
  42. if(fd >= 0)
  43. {
  44. read(fd, param, len);
  45. close(fd);
  46. }
  47. else
  48. return -RT_EOK;
  49. #endif
  50. return RT_EOK;
  51. }
  52. int gagent_write_param(struct gagent_config *param, rt_uint32_t len)
  53. {
  54. /* write param */
  55. rt_kprintf("mac:%s", param->mac);
  56. rt_kprintf("did:%s", param->did);
  57. rt_kprintf("passcode:%s", param->passcode);
  58. rt_kprintf("pk:%s", param->pk);
  59. rt_kprintf("pk_secret:%s", param->pk_secret);
  60. rt_kprintf("hard_version:%s", param->hard_version);
  61. rt_kprintf("soft_version:%s", param->soft_version);
  62. #ifdef RT_USING_DFS
  63. int fd;
  64. fd = open("/sdcard/demo", O_WRONLY | O_CREAT |O_TRUNC | O_BINARY, 0);
  65. if(fd >= 0)
  66. {
  67. write(fd, param, len);
  68. close(fd);
  69. }
  70. #endif
  71. return RT_EOK;
  72. }
  73. int gagent_recv_packet(rt_uint8_t from, rt_uint8_t action, rt_uint8_t *kv, rt_uint16_t kv_len)
  74. {
  75. /* please read product protocol */
  76. static uint8_t power;
  77. switch(action)
  78. {
  79. case ACTION_CONTROL:
  80. rt_kprintf("ACTION_CONTROL\r\n");
  81. power = *(kv + 1);
  82. rt_kprintf("power:%d\n", power);
  83. gagent_cloud_send_packet(ACTION_REPORT_STATUS, &power, 1);
  84. break;
  85. case ACTION_READ_STATUS:
  86. rt_kprintf("ACTION_READ_STATUS\r\n");
  87. gagent_cloud_send_packet(ACTION_READ_STATUS_ACK, &power, 1);
  88. break;
  89. case ACTION_TRANS_RECV:
  90. rt_kprintf("this is your raw data from app\r\n");
  91. break;
  92. case ACTION_PUSH_OTA:
  93. rt_kprintf("ACTION_PUSH_OTA\r\n");
  94. break;
  95. }
  96. return RT_EOK;
  97. }
  98. int gagent_cloud(void)
  99. {
  100. int rc = RT_EOK;
  101. rt_memset(&gagent_param, 0, sizeof(gagent_param));
  102. //
  103. strcpy(gagent_param.product_key, DEMO_PRODUCT_KEY);
  104. strcpy(gagent_param.product_secret, DEMO_PRODUCT_KEY_SECRET);
  105. strcpy(gagent_param.mac, DEMO_MAC);
  106. gagent_param.read_param_callback = gagent_read_param;
  107. gagent_param.write_param_callback = gagent_write_param;
  108. gagent_param.recv_packet_callback = gagent_recv_packet;
  109. //
  110. gagent_cloud_start(&gagent_param);
  111. return rc;
  112. }
  113. #ifdef RT_USING_FINSH
  114. MSH_CMD_EXPORT(gagent_cloud, gagent cloud demo);
  115. FINSH_FUNCTION_EXPORT(gagent_cloud, "gagent cloud test");
  116. #endif