3 * Copyright (c) 2021 Project CHIP Authors
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
28 pthread_t sPlatformMainThread;
30 void * PlatformMainLoop(void *)
32 ChipLogProgress(DeviceLayer, "Platform main loop started.");
33 chip::DeviceLayer::PlatformMgr().RunEventLoop();
34 ChipLogProgress(DeviceLayer, "Platform main loop completed.");
40 extern "C" void pychip_native_init()
42 CHIP_ERROR err = CHIP_NO_ERROR;
44 err = chip::Platform::MemoryInit();
45 if (err != CHIP_NO_ERROR)
47 ChipLogError(DeviceLayer, "Failed to initialize CHIP stack: memory init failed: %s", chip::ErrorStr(err));
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)
55 ChipLogError(DeviceLayer, "Failed to configure BLE central: %s", chip::ErrorStr(err));
57 #endif // CHIP_DEVICE_LAYER_TARGET_LINUX && CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
59 err = chip::DeviceLayer::PlatformMgr().InitChipStack();
60 if (err != CHIP_NO_ERROR)
62 ChipLogError(DeviceLayer, "Failed to initialize CHIP stack: platform init failed: %s", chip::ErrorStr(err));
64 int result = pthread_create(&sPlatformMainThread, nullptr, PlatformMainLoop, nullptr);
69 ChipLogError(DeviceLayer, "Failed to initialize CHIP stack: pthread_create failed: %s", strerror(tmpErrno));