-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathNullController.cpp
185 lines (137 loc) · 3.68 KB
/
NullController.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (C) 2021 by Jonathan Naylor G4KLX
*
* 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 "NullController.h"
#include <cstdio>
#include <cstdint>
#include <cassert>
const unsigned char MMDVM_FRAME_START = 0xE0U;
const unsigned char MMDVM_GET_VERSION = 0x00U;
const unsigned char MMDVM_GET_STATUS = 0x01U;
const unsigned char MMDVM_SET_CONFIG = 0x02U;
const unsigned char MMDVM_SET_MODE = 0x03U;
const unsigned char MMDVM_SET_FREQ = 0x04U;
const unsigned char MMDVM_FM_PARAMS1 = 0x60U;
const unsigned char MMDVM_FM_PARAMS2 = 0x61U;
const unsigned char MMDVM_FM_PARAMS3 = 0x62U;
const unsigned char MMDVM_FM_PARAMS4 = 0x63U;
const unsigned char MMDVM_ACK = 0x70U;
const unsigned char MMDVM_NAK = 0x7FU;
const unsigned char PROTOCOL_VERSION = 2U;
const char* HARDWARE = "Null Modem Controller";
CNullController::CNullController() :
m_buffer(200U, "Null Controller Buffer")
{
}
CNullController::~CNullController()
{
}
bool CNullController::open()
{
return true;
}
int CNullController::read(unsigned char* buffer, unsigned int length)
{
unsigned int dataSize = m_buffer.dataSize();
if (dataSize == 0U)
return 0;
if (length > dataSize)
length = dataSize;
m_buffer.getData(buffer, length);
return int(length);
}
int CNullController::write(const unsigned char* buffer, unsigned int length)
{
switch (buffer[2U]) {
case MMDVM_GET_VERSION:
writeVersion();
break;
case MMDVM_GET_STATUS:
writeStatus();
break;
case MMDVM_SET_CONFIG:
case MMDVM_SET_FREQ:
case MMDVM_SET_MODE:
case MMDVM_FM_PARAMS1:
case MMDVM_FM_PARAMS2:
case MMDVM_FM_PARAMS3:
case MMDVM_FM_PARAMS4:
writeAck(buffer[2U]);
break;
default:
break;
}
return int(length);
}
void CNullController::close()
{
}
void CNullController::writeVersion()
{
unsigned char reply[200U];
reply[0U] = MMDVM_FRAME_START;
reply[1U] = 0U;
reply[2U] = MMDVM_GET_VERSION;
reply[3U] = PROTOCOL_VERSION;
// Return two bytes of mode capabilities
reply[4U] = 0xFFU;
reply[5U] = 0xFFU;
// CPU type/manufacturer. 0=Atmel ARM, 1=NXP ARM, 2=St-Micro ARM
reply[6U] = 2U;
// Reserve 16 bytes for the UDID
::memset(reply + 7U, 0x00U, 16U);
uint8_t count = 23U;
for (uint8_t i = 0U; HARDWARE[i] != 0x00U; i++, count++)
reply[count] = HARDWARE[i];
reply[1U] = count;
m_buffer.addData(reply, count);
}
void CNullController::writeStatus()
{
unsigned char reply[30U];
// Send all sorts of interesting internal values
reply[0U] = MMDVM_FRAME_START;
reply[1U] = 20U;
reply[2U] = MMDVM_GET_STATUS;
reply[3U] = 0U;
reply[4U] = 0x00U;
reply[5U] = 0x00U;
reply[6U] = 20U;
reply[7U] = 20U;
reply[8U] = 20U;
reply[9U] = 20U;
reply[10U] = 20U;
reply[11U] = 20U;
reply[12U] = 20U;
reply[13U] = 20U;
reply[14U] = 0x00U;
reply[15U] = 0x00U;
reply[16U] = 20U;
reply[17U] = 20U;
reply[18U] = 0x00U;
reply[19U] = 0x00U;
m_buffer.addData(reply, 20U);
}
void CNullController::writeAck(unsigned char type)
{
unsigned char reply[4U];
reply[0U] = MMDVM_FRAME_START;
reply[1U] = 4U;
reply[2U] = MMDVM_ACK;
reply[3U] = type;
m_buffer.addData(reply, 4U);
}