at_sample_mg21.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * File : at_sample_mg21.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 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. * 2021-08-03 yangtao first version
  23. */
  24. #include <rtthread.h>
  25. #include <finsh.h>
  26. #include <at_device_mg21.h>
  27. #define LOG_TAG "at.sample.mg"
  28. #include <at_log.h>
  29. #define AT_MG21_SAMPLE_DEIVCE_NAME "mg0"
  30. static struct at_device_mg21 mg0 =
  31. {
  32. AT_MG21_SAMPLE_DEIVCE_NAME,
  33. AT_MG21_SAMPLE_CLIENT_NAME,
  34. AT_MG21_SAMPLE_RECV_BUFF_LEN,
  35. };
  36. static int mg21_device_register(void)
  37. {
  38. struct at_device_mg21 *mg21 = &mg0;
  39. LOG_I("mg21 register");
  40. return at_device_register(&(mg21->device),
  41. mg21->device_name,
  42. mg21->client_name,
  43. AT_DEVICE_CLASS_MG21,
  44. (void *) mg21);
  45. }
  46. INIT_APP_EXPORT(mg21_device_register);
  47. int zb_pj(int argc, char**argv)
  48. {
  49. pj_req_t *pjCmd = rt_malloc(sizeof(pj_req_t));
  50. pjCmd -> sec = 0x3C;
  51. pjCmd -> nodeId = 0xFFFF;
  52. struct at_device *device = RT_NULL;
  53. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_DEVICE, "mg0");
  54. if(device == RT_NULL)
  55. {
  56. rt_kprintf("can't find device\r\n");
  57. return -RT_ERROR;
  58. }
  59. at_device_control(device, AT_DEVICE_CTRL_PJ, (void *)pjCmd);
  60. rt_free(pjCmd);
  61. return RT_EOK;
  62. }
  63. /* Export open network function to msh */
  64. MSH_CMD_EXPORT(zb_pj, Coordinator Open Network);
  65. int zb_on(int argc, char**argv)
  66. {
  67. zcl_onoff_cmd_t *onCmd = rt_malloc(sizeof(zcl_onoff_cmd_t));
  68. //Suppose there is a switch device with network short address 0xABCD and port number 0x0A connected to the coordinator
  69. onCmd -> nwkAddr = 0xABCD;
  70. onCmd -> endpoint = 0x0A;
  71. onCmd -> mode = 1;
  72. struct at_device *device = RT_NULL;
  73. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_DEVICE, "mg0");
  74. if(device == RT_NULL)
  75. {
  76. rt_kprintf("can't find device\r\n");
  77. return -RT_ERROR;
  78. }
  79. at_device_control(device, AT_DEVICE_CTRL_ONOFF, (void *)onCmd);
  80. rt_free(onCmd);
  81. return RT_EOK;
  82. }
  83. /* Export onoff function to msh */
  84. MSH_CMD_EXPORT(zb_on, Control devices with onoff attribute);
  85. int zb_off(int argc, char**argv)
  86. {
  87. zcl_onoff_cmd_t *offCmd = rt_malloc(sizeof(zcl_onoff_cmd_t));
  88. //Suppose there is a switch device with network short address 0xABCD and port number 0x0A connected to the coordinator
  89. offCmd -> nwkAddr = 0xABCD;
  90. offCmd -> endpoint = 0x0A;
  91. offCmd -> mode = 1;
  92. struct at_device *device = RT_NULL;
  93. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_DEVICE, "mg0");
  94. if(device == RT_NULL)
  95. {
  96. rt_kprintf("can't find device\r\n");
  97. return -RT_ERROR;
  98. }
  99. at_device_control(device, AT_DEVICE_CTRL_ONOFF, (void *)offCmd);
  100. rt_free(offCmd);
  101. return RT_EOK;
  102. }
  103. /* Export onoff function to msh */
  104. MSH_CMD_EXPORT(zb_off, Control devices with onoff attribute);
  105. int zb_readAttr(int argc, char**argv)
  106. {
  107. read_attrs_req_t *readAttrCmd = rt_malloc(sizeof(read_attrs_req_t));
  108. //Suppose there is a switch device with network short address 0xABCD and port number 0x0A connected to the coordinator
  109. readAttrCmd -> nwkAddr = 0xABCD;
  110. readAttrCmd -> endpoint = 0x0A;
  111. readAttrCmd -> clusterID = 0x0006;
  112. readAttrCmd -> attrID = 0x0000;
  113. struct at_device *device = RT_NULL;
  114. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_DEVICE, "mg0");
  115. if(device == RT_NULL)
  116. {
  117. rt_kprintf("can't find device\r\n");
  118. return -RT_ERROR;
  119. }
  120. at_device_control(device, AT_DEVICE_CTRL_READATTR, (void *)readAttrCmd);
  121. rt_free(readAttrCmd);
  122. return RT_EOK;
  123. }
  124. /* Export read device attribute function to msh */
  125. MSH_CMD_EXPORT(zb_readAttr, Read attribute from the device);