Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / temperature-measurement-app / esp32 / main / CHIPDeviceManager.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *    Unless required by applicable law or agreed to in writing, software
12  *    distributed under the License is distributed on an "AS IS" BASIS,
13  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *    See the License for the specific language governing permissions and
15  *    limitations under the License.
16  */
17
18 /**
19  *    @file
20  *      This file implements the CHIP Device Interface that is used by
21  *      applications to interact with the CHIP stack
22  *
23  */
24
25 #include <stdlib.h>
26
27 #include "CHIPDeviceManager.h"
28 #include <app/util/basic-types.h>
29 #include <setup_payload/SetupPayload.h>
30 #include <support/CHIPMem.h>
31 #include <support/CodeUtils.h>
32 #include <support/ErrorStr.h>
33
34 using namespace ::chip;
35
36 namespace chip {
37
38 namespace DeviceManager {
39
40 using namespace ::chip::DeviceLayer;
41
42 void CHIPDeviceManager::CommonDeviceEventHandler(const ChipDeviceEvent * event, intptr_t arg)
43 {
44     CHIPDeviceManagerCallbacks * cb = reinterpret_cast<CHIPDeviceManagerCallbacks *>(arg);
45     if (cb != nullptr)
46     {
47         cb->DeviceEventCallback(event, reinterpret_cast<intptr_t>(cb));
48     }
49 }
50
51 /**
52  *
53  */
54 CHIP_ERROR CHIPDeviceManager::Init(CHIPDeviceManagerCallbacks * cb)
55 {
56     CHIP_ERROR err;
57     mCB = cb;
58
59     // Initialize the CHIP stack.
60     err = PlatformMgr().InitChipStack();
61     SuccessOrExit(err);
62
63     switch (static_cast<RendezvousInformationFlags>(CONFIG_RENDEZVOUS_MODE))
64     {
65     case RendezvousInformationFlags::kBLE:
66         ConnectivityMgr().SetBLEAdvertisingEnabled(true);
67         break;
68
69     case RendezvousInformationFlags::kWiFi:
70         ConnectivityMgr().SetBLEAdvertisingEnabled(false);
71         ConnectivityMgr().SetWiFiAPMode(ConnectivityManager::kWiFiAPMode_Enabled);
72         break;
73
74     case RendezvousInformationFlags::kNone:
75         // If rendezvous is bypassed, enable SoftAP so that the device can still
76         // be communicated with via its SoftAP as needed.
77         ConnectivityMgr().SetWiFiAPMode(ConnectivityManager::kWiFiAPMode_Enabled);
78         break;
79
80     default:
81         break;
82     }
83
84     err = Platform::MemoryInit();
85     SuccessOrExit(err);
86
87     // Register a function to receive events from the CHIP device layer.  Note that calls to
88     // this function will happen on the CHIP event loop thread, not the app_main thread.
89     PlatformMgr().AddEventHandler(CHIPDeviceManager::CommonDeviceEventHandler, reinterpret_cast<intptr_t>(cb));
90
91     // Start a task to run the CHIP Device event loop.
92     err = PlatformMgr().StartEventLoopTask();
93     SuccessOrExit(err);
94
95 exit:
96     return err;
97 }
98 } // namespace DeviceManager
99 } // namespace chip
100
101 void emberAfPostAttributeChangeCallback(EndpointId endpointId, ClusterId clusterId, AttributeId attributeId, uint8_t mask,
102                                         uint16_t manufacturerCode, uint8_t type, uint8_t size, uint8_t * value)
103 {
104     chip::DeviceManager::CHIPDeviceManagerCallbacks * cb =
105         chip::DeviceManager::CHIPDeviceManager::GetInstance().GetCHIPDeviceManagerCallbacks();
106     if (cb != nullptr)
107     {
108         cb->PostAttributeChangeCallback(endpointId, clusterId, attributeId, mask, manufacturerCode, type, size, value);
109     }
110 }