Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / controller / python / chip / native / StackInit.cpp
1 /*
2  *
3  *    Copyright (c) 2021 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 #include <errno.h>
18 #include <pthread.h>
19
20 #include <platform/CHIPDeviceLayer.h>
21 #include <platform/PlatformManager.h>
22 #include <support/CHIPMem.h>
23 #include <support/ErrorStr.h>
24 #include <support/logging/CHIPLogging.h>
25
26 namespace {
27
28 pthread_t sPlatformMainThread;
29
30 void * PlatformMainLoop(void *)
31 {
32     ChipLogProgress(DeviceLayer, "Platform main loop started.");
33     chip::DeviceLayer::PlatformMgr().RunEventLoop();
34     ChipLogProgress(DeviceLayer, "Platform main loop completed.");
35     return nullptr;
36 }
37
38 } // namespace
39
40 extern "C" void pychip_native_init()
41 {
42     CHIP_ERROR err = CHIP_NO_ERROR;
43
44     err = chip::Platform::MemoryInit();
45     if (err != CHIP_NO_ERROR)
46     {
47         ChipLogError(DeviceLayer, "Failed to initialize CHIP stack: memory init failed: %s", chip::ErrorStr(err));
48     }
49
50 #if CHIP_DEVICE_LAYER_TARGET_LINUX && CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
51     // By default, Linux device is configured as a BLE peripheral while the controller needs a BLE central.
52     err = chip::DeviceLayer::Internal::BLEMgrImpl().ConfigureBle(/* BLE adapter ID */ 0, /* BLE central */ true);
53     if (err != CHIP_NO_ERROR)
54     {
55         ChipLogError(DeviceLayer, "Failed to configure BLE central:  %s", chip::ErrorStr(err));
56     }
57 #endif // CHIP_DEVICE_LAYER_TARGET_LINUX && CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
58
59     err = chip::DeviceLayer::PlatformMgr().InitChipStack();
60     if (err != CHIP_NO_ERROR)
61     {
62         ChipLogError(DeviceLayer, "Failed to initialize CHIP stack: platform init failed: %s", chip::ErrorStr(err));
63     }
64     int result   = pthread_create(&sPlatformMainThread, nullptr, PlatformMainLoop, nullptr);
65     int tmpErrno = errno;
66
67     if (result != 0)
68     {
69         ChipLogError(DeviceLayer, "Failed to initialize CHIP stack: pthread_create failed: %s", strerror(tmpErrno));
70     }
71 }