forked from intrepidcs/libneoradio2
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdevice.cpp
More file actions
141 lines (122 loc) · 2.54 KB
/
Copy pathdevice.cpp
File metadata and controls
141 lines (122 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "device.h"
#include <string>
#include <sstream>
#include <cstring> // memcpy
Device::Device()
{
mState = DeviceStateIdle;
mQuit = false;
mThread = nullptr;
memset(&mDevInfo.di, 0, sizeof(mDevInfo.di));
mDevInfo.is_blocking = 0;
mDevInfo.is_open = 0;
}
Device::~Device()
{
close();
quit(true);
}
bool Device::open()
{
if (!isOpen())
{
changeState(DeviceStateConnecting);
mThread = new std::thread(&Device::start, this);
}
return true;
}
bool Device::close()
{
if (isOpen())
{
changeState(DeviceStateDisconnecting);
}
return true;
}
void Device::start()
{
#ifdef ENABLE_DEBUG_PRINT
auto id = std::this_thread::get_id();
std::stringstream temp;
temp << "THREAD ID: " << id;
DEBUG_PRINT("Started... %s", temp.str().c_str());
#endif // ENABLE_DEBUG_PRINT
using namespace std::chrono;
mMutex.lock();
mIsRunning = true;
mMutex.unlock();
while (!mQuit)
{
auto start_time = std::chrono::high_resolution_clock::now();
bool success = false;
switch (mState)
{
case DeviceStateIdle:
success = runIdle();
break;
case DeviceStateConnecting:
success = runConnecting();
break;
case DeviceStateConnected:
success = runConnected();
break;
case DeviceStateDisconnecting:
success = runDisconnecting();
break;
};
// Make sure we don't hog the CPU
auto elapsed_time = std::chrono::high_resolution_clock::now() - start_time;
if (elapsed_time < 3ms)
std::this_thread::sleep_for(3ms - elapsed_time);
}
mMutex.lock();
mIsRunning = false;
mMutex.unlock();
#ifdef ENABLE_DEBUG_PRINT
auto id2 = std::this_thread::get_id();
std::stringstream temp2;
temp2 << "THREAD ID: " << id2;
DEBUG_PRINT("Stopping... %s", temp2.str().c_str());
#endif // ENABLE_DEBUG_PRINT
}
bool Device::isOpen()
{
mDevInfo.is_open = state() == DeviceStateConnected;
return mDevInfo.is_open;
}
bool Device::quit(bool wait_for_quit)
{
mMutex.lock();
mQuit = true;
mMutex.unlock();
if (mThread)
{
if (wait_for_quit)
{
try
{
mThread->join();
}
catch(const std::exception& e)
{
DEBUG_PRINT("%s\n", e.what());
}
}
delete mThread;
mThread = nullptr;
}
return true;
}
bool Device::addPath(DeviceChannel channel, std::string path)
{
if (mDevInfo.channel_paths.find(channel) != mDevInfo.channel_paths.end())
return false;
mDevInfo.channel_paths.insert(std::make_pair(channel, path));
return true;
}
std::string Device::path(DeviceChannel channel)
{
if (mDevInfo.channel_paths.find(channel) == mDevInfo.channel_paths.end())
return std::string();
return mDevInfo.channel_paths[channel];
}