Skip to content

Commit 5ec28e2

Browse files
authored
[fabric-bridge] Replace pthread with std::thread (#33552)
1 parent fb8d9e5 commit 5ec28e2

File tree

1 file changed

+35
-49
lines changed
  • examples/fabric-bridge-app/linux

1 file changed

+35
-49
lines changed

examples/fabric-bridge-app/linux/main.cpp

+35-49
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <app/ConcreteAttributePath.h>
2727
#include <app/EventLogging.h>
2828
#include <app/reporting/reporting.h>
29+
#include <app/server/Server.h>
2930
#include <app/util/af-types.h>
3031
#include <app/util/attribute-storage.h>
3132
#include <app/util/endpoint-config-api.h>
@@ -39,16 +40,14 @@
3940
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
4041
#include <setup_payload/SetupPayload.h>
4142

42-
#include <pthread.h>
43-
#include <sys/ioctl.h>
44-
4543
#include "CommissionableInit.h"
4644
#include "Device.h"
47-
#include <app/server/Server.h>
4845

4946
#include <cassert>
5047
#include <iostream>
5148
#include <string>
49+
#include <sys/ioctl.h>
50+
#include <thread>
5251

5352
using namespace chip;
5453
using namespace chip::app;
@@ -58,12 +57,41 @@ using namespace chip::Transport;
5857
using namespace chip::DeviceLayer;
5958
using namespace chip::app::Clusters;
6059

60+
#define POLL_INTERVAL_MS (100)
61+
6162
namespace {
6263

6364
EndpointId gCurrentEndpointId;
6465
EndpointId gFirstDynamicEndpointId;
6566
Device * gDevices[CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT + 1];
6667

68+
bool KeyboardHit()
69+
{
70+
int bytesWaiting;
71+
ioctl(0, FIONREAD, &bytesWaiting);
72+
return bytesWaiting > 0;
73+
}
74+
75+
void BridgePollingThread()
76+
{
77+
while (true)
78+
{
79+
if (KeyboardHit())
80+
{
81+
int ch = getchar();
82+
if (ch == 'e')
83+
{
84+
ChipLogProgress(DeviceLayer, "Exiting.....");
85+
exit(0);
86+
}
87+
continue;
88+
}
89+
90+
// Sleep to avoid tight loop reading commands
91+
usleep(POLL_INTERVAL_MS * 1000);
92+
}
93+
}
94+
6795
} // namespace
6896

6997
// REVISION DEFINITIONS:
@@ -245,42 +273,6 @@ Protocols::InteractionModel::Status emberAfExternalAttributeWriteCallback(Endpoi
245273
return ret;
246274
}
247275

248-
#define POLL_INTERVAL_MS (100)
249-
uint8_t poll_prescale = 0;
250-
251-
bool kbhit()
252-
{
253-
int byteswaiting;
254-
ioctl(0, FIONREAD, &byteswaiting);
255-
return byteswaiting > 0;
256-
}
257-
258-
const int16_t oneDegree = 100;
259-
260-
void * bridge_polling_thread(void * context)
261-
{
262-
while (true)
263-
{
264-
if (kbhit())
265-
{
266-
int ch = getchar();
267-
268-
// Commands used for the actions bridge test plan.
269-
if (ch == 'e')
270-
{
271-
ChipLogProgress(DeviceLayer, "Exiting.....");
272-
exit(0);
273-
}
274-
continue;
275-
}
276-
277-
// Sleep to avoid tight loop reading commands
278-
usleep(POLL_INTERVAL_MS * 1000);
279-
}
280-
281-
return nullptr;
282-
}
283-
284276
void ApplicationInit()
285277
{
286278
// Clear out the device database
@@ -292,15 +284,9 @@ void ApplicationInit()
292284
static_cast<int>(emberAfEndpointFromIndex(static_cast<uint16_t>(emberAfFixedEndpointCount() - 1))) + 1);
293285
gCurrentEndpointId = gFirstDynamicEndpointId;
294286

295-
{
296-
pthread_t poll_thread;
297-
int res = pthread_create(&poll_thread, nullptr, bridge_polling_thread, nullptr);
298-
if (res)
299-
{
300-
printf("Error creating polling thread: %d\n", res);
301-
exit(1);
302-
}
303-
}
287+
// Start a thread for bridge polling
288+
std::thread pollingThread(BridgePollingThread);
289+
pollingThread.detach();
304290
}
305291

306292
void ApplicationShutdown() {}

0 commit comments

Comments
 (0)