-
Notifications
You must be signed in to change notification settings - Fork 1
/
AnnouncementUnit.cpp
157 lines (124 loc) · 3.38 KB
/
AnnouncementUnit.cpp
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright (C) 2014 by Jonathan Naylor G4KLX
* Copyright (c) Thomas A. Early N7TAE
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <sys/stat.h>
#include <cassert>
#include "DStarDefines.h"
#include "HeaderData.h"
#include "AnnouncementUnit.h"
#include "Utils.h"
CAnnouncementUnit::CAnnouncementUnit(IRepeaterCallback* handler, const std::string& callsign, const std::string& fileName, const std::string& name) :
m_handler(handler),
m_callsign(callsign),
m_fileName(fileName),
m_name(name),
m_reader(NULL),
m_status(NS_IDLE),
m_timer(1000U, REPLY_TIME),
m_out(0U),
m_id(0U)
{
assert(handler != NULL);
m_name.resize(SHORT_CALLSIGN_LENGTH, ' ');
printf("Connected '%s' to %s using file - %s\n", name.c_str(), callsign.c_str(), fileName.c_str());
}
CAnnouncementUnit::~CAnnouncementUnit()
{
}
void CAnnouncementUnit::sendAnnouncement()
{
if (m_status != NS_IDLE)
return;
struct stat sbuf;
if (stat(m_fileName.c_str(), &sbuf))
return;
m_reader = new CDVTOOLFileReader;
bool ret = m_reader->open(m_fileName);
if (!ret) {
delete m_reader;
m_reader = NULL;
return;
}
m_status = NS_WAIT;
m_timer.start();
}
void CAnnouncementUnit::clock(unsigned int ms)
{
m_timer.clock(ms);
if (m_status == NS_WAIT && m_timer.hasExpired()) {
m_reader->read(); // Pop the first record off the file
m_id = CHeaderData::createId();
CHeaderData header;
header.setMyCall1(m_callsign);
header.setMyCall2(m_name);
header.setYourCall("CQCQCQ ");
header.setId(m_id);
m_handler->process(header, DIR_INCOMING, AS_INFO);
m_timer.stop();
m_out = 0U;
m_status = NS_TRANSMIT;
// m_time.Start();
m_start = std::chrono::high_resolution_clock::now();
return;
}
if (m_status == NS_TRANSMIT) {
unsigned int needed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - m_start).count();
needed /= DSTAR_FRAME_TIME_MS;
// unsigned int needed = m_time.Time() / DSTAR_FRAME_TIME_MS;
while (m_out < needed) {
DVTFR_TYPE type = m_reader->read();
if (type != DVTFR_DATA) {
m_out = 0U;
m_status = NS_IDLE;
m_timer.stop();
m_reader->close();
delete m_reader;
m_reader = NULL;
return;
}
CAMBEData* data = m_reader->readData();
data->setId(m_id);
m_out++;
m_handler->process(*data, DIR_INCOMING, AS_INFO);
bool end = data->isEnd();
delete data;
if (end) {
m_out = 0U;
m_status = NS_IDLE;
m_reader->close();
delete m_reader;
m_reader = NULL;
m_timer.stop();
return;
}
}
return;
}
}
void CAnnouncementUnit::cancel()
{
m_status = NS_IDLE;
m_out = 0U;
m_id = 0U;
if (m_reader != NULL) {
m_reader->close();
delete m_reader;
m_reader = NULL;
}
m_timer.stop();
}