DeviceCallbacks.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. *
  3. * Copyright (c) 2020 Project CHIP Authors
  4. * All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /**
  19. * @file DeviceCallbacks.cpp
  20. *
  21. * Implements all the callbacks to the application from the CHIP Stack
  22. *
  23. **/
  24. #include "DeviceCallbacks.h"
  25. #include "AppConfig.h"
  26. #include "BoltLockManager.h"
  27. #include "esp_heap_caps.h"
  28. #include "esp_log.h"
  29. #include <app/common/gen/attribute-id.h>
  30. #include <app/common/gen/cluster-id.h>
  31. #include <support/CodeUtils.h>
  32. static const char * TAG = "lock-devicecallbacks";
  33. using namespace ::chip;
  34. using namespace ::chip::Inet;
  35. using namespace ::chip::System;
  36. using namespace ::chip::DeviceLayer;
  37. void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_t arg)
  38. {
  39. switch (event->Type)
  40. {
  41. case DeviceEventType::kInternetConnectivityChange:
  42. OnInternetConnectivityChange(event);
  43. break;
  44. case DeviceEventType::kSessionEstablished:
  45. OnSessionEstablished(event);
  46. break;
  47. }
  48. ESP_LOGI(TAG, "Current free heap: %d\n", heap_caps_get_free_size(MALLOC_CAP_8BIT));
  49. }
  50. void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, ClusterId clusterId, AttributeId attributeId, uint8_t mask,
  51. uint16_t manufacturerCode, uint8_t type, uint16_t size, uint8_t * value)
  52. {
  53. ESP_LOGI(TAG, "PostAttributeChangeCallback - Cluster ID: '0x%04x', EndPoint ID: '0x%02x', Attribute ID: '0x%04x'", clusterId,
  54. endpointId, attributeId);
  55. switch (clusterId)
  56. {
  57. case ZCL_ON_OFF_CLUSTER_ID:
  58. OnOnOffPostAttributeChangeCallback(endpointId, attributeId, value);
  59. break;
  60. default:
  61. ESP_LOGI(TAG, "Unhandled cluster ID: %d", clusterId);
  62. break;
  63. }
  64. ESP_LOGI(TAG, "Current free heap: %d\n", heap_caps_get_free_size(MALLOC_CAP_8BIT));
  65. }
  66. void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event)
  67. {
  68. if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established)
  69. {
  70. ESP_LOGI(TAG, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT);
  71. }
  72. else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost)
  73. {
  74. ESP_LOGE(TAG, "Lost IPv4 connectivity...");
  75. }
  76. if (event->InternetConnectivityChange.IPv6 == kConnectivity_Established)
  77. {
  78. ESP_LOGI(TAG, "IPv6 Server ready...");
  79. }
  80. else if (event->InternetConnectivityChange.IPv6 == kConnectivity_Lost)
  81. {
  82. ESP_LOGE(TAG, "Lost IPv6 connectivity...");
  83. }
  84. }
  85. void DeviceCallbacks::OnSessionEstablished(const ChipDeviceEvent * event)
  86. {
  87. if (event->SessionEstablished.IsCommissioner)
  88. {
  89. ESP_LOGI(TAG, "Commissioner detected!");
  90. }
  91. }
  92. void DeviceCallbacks::OnOnOffPostAttributeChangeCallback(EndpointId endpointId, AttributeId attributeId, uint8_t * value)
  93. {
  94. VerifyOrExit(attributeId == ZCL_ON_OFF_ATTRIBUTE_ID, ESP_LOGI(TAG, "Unhandled Attribute ID: '0x%04x", attributeId));
  95. VerifyOrExit(endpointId == 1 || endpointId == 2, ESP_LOGE(TAG, "Unexpected EndPoint ID: `0x%02x'", endpointId));
  96. if (*value)
  97. {
  98. BoltLockMgr().InitiateAction(AppEvent::kEventType_Lock, BoltLockManager::LOCK_ACTION);
  99. }
  100. else
  101. {
  102. BoltLockMgr().InitiateAction(AppEvent::kEventType_Lock, BoltLockManager::UNLOCK_ACTION);
  103. }
  104. exit:
  105. return;
  106. }