|
22 | 22 | public class Freedv implements Protocol {
|
23 | 23 | private static final String TAG = Freedv.class.getSimpleName();
|
24 | 24 |
|
| 25 | + private static final int CRC_LENGTH = 2; |
| 26 | + private static final int PKT_SIZE_LENGTH = 2; |
| 27 | + |
25 | 28 | private ProtocolCallback _parentProtocolCallback;
|
26 | 29 | private Transport _transport;
|
27 | 30 |
|
@@ -89,15 +92,18 @@ public void sendTextMessage(TextMessage textMessage) throws IOException {
|
89 | 92 |
|
90 | 93 | @Override
|
91 | 94 | public void sendData(String src, String dst, String path, byte[] dataPacket) throws IOException {
|
92 |
| - if (dataPacket.length > _dataBuffer.length - 2) { |
| 95 | + if (dataPacket.length > _dataBuffer.length - CRC_LENGTH - PKT_SIZE_LENGTH) { |
93 | 96 | Log.e(TAG, "Too large packet " + dataPacket.length + " > " + _dataBuffer.length);
|
94 | 97 | return;
|
95 | 98 | }
|
96 | 99 | long cnt = Codec2.freedvRawDataPreambleTx(_freedvData, _dataSamplesBuffer);
|
97 | 100 | _transport.write(Arrays.copyOf(_dataSamplesBuffer, (int) cnt));
|
98 | 101 |
|
99 | 102 | Arrays.fill(_dataBuffer, (byte) 0);
|
100 |
| - System.arraycopy(dataPacket, 0, _dataBuffer, 0, dataPacket.length); |
| 103 | + // transmit packet size first |
| 104 | + _dataBuffer[0] = (byte)((dataPacket.length >> 8) & 0xff); |
| 105 | + _dataBuffer[1] = (byte)(dataPacket.length & 0xff); |
| 106 | + System.arraycopy(dataPacket, 0, _dataBuffer, 2, dataPacket.length); |
101 | 107 | Codec2.freedvRawDataTx(_freedvData, _dataSamplesBuffer, _dataBuffer);
|
102 | 108 | _transport.write(_dataSamplesBuffer);
|
103 | 109 |
|
@@ -151,8 +157,12 @@ public boolean receive() throws IOException {
|
151 | 157 | long cntRead = Codec2.freedvRawDataRx(_freedvData, _dataBuffer, samplesData);
|
152 | 158 | if (cntRead > 0) {
|
153 | 159 | Log.i(TAG, "receive " + cntRead);
|
| 160 | + // extract packet length |
| 161 | + int pktLen = (((int)_dataBuffer[0] & 0xff) << 8) | ((int)_dataBuffer[1] & 0xff); |
| 162 | + byte [] pkt = new byte[pktLen]; |
| 163 | + System.arraycopy(_dataBuffer, 2, pkt, 0, pktLen); |
154 | 164 | // TODO, refactor, use onReceiveData
|
155 |
| - _parentProtocolCallback.onReceiveCompressedAudio(null, null, -1, _dataBuffer); |
| 165 | + _parentProtocolCallback.onReceiveCompressedAudio(null, null, -1, pkt); |
156 | 166 | float snr = Codec2.freedvGetModemStat(_freedvData);
|
157 | 167 | _parentProtocolCallback.onReceiveSignalLevel((short) 0, (short)(100 * snr));
|
158 | 168 | isRead = true;
|
|
0 commit comments