From b408a24b9aa3dfae6da239b40c8239c62e7969e0 Mon Sep 17 00:00:00 2001 From: sh123 Date: Tue, 2 Aug 2022 13:48:57 +0300 Subject: [PATCH] Bug fixing --- .../com/radio/codec2talkie/protocol/Hdlc.java | 16 ++++++++++++++++ .../com/radio/codec2talkie/tools/BitTools.java | 10 ++++++---- .../radio/codec2talkie/transport/SoundModem.java | 13 +++++++++++-- libcodec2-android/src/main/cpp/Codec2JNI.cpp | 2 +- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/codec2talkie/src/main/java/com/radio/codec2talkie/protocol/Hdlc.java b/codec2talkie/src/main/java/com/radio/codec2talkie/protocol/Hdlc.java index af09747..593790b 100644 --- a/codec2talkie/src/main/java/com/radio/codec2talkie/protocol/Hdlc.java +++ b/codec2talkie/src/main/java/com/radio/codec2talkie/protocol/Hdlc.java @@ -88,18 +88,32 @@ public class Hdlc implements Protocol { if (_readByte == 0x7e) { Log.i(TAG, "HDLC " + _prevHdlc/8); int pos = _currentFrameBuffer.position(); + + // shift/flush back previous 8 - 1 bits if (pos >= 7) { _currentFrameBuffer.position(_currentFrameBuffer.position() - 7); } else { _currentFrameBuffer.position(0); } + // get packet bits between 0x7e _currentFrameBuffer.flip(); byte[] packetBits = new byte[_currentFrameBuffer.remaining()]; _currentFrameBuffer.get(packetBits); + + // get bytes from bits byte[] packetBytes = BitTools.convertFromHDLCBitArray(packetBits); if (packetBytes != null) { Log.i(TAG, DebugTools.byteBitsToString(packetBits)); Log.i(TAG, DebugTools.bytesToHex(packetBytes)); + if (packetBytes.length > 2) { + byte[] contentBytes = Arrays.copyOf(packetBytes, packetBytes.length - 2); + int calculatedCrc = ChecksumTools.calculateFcs(contentBytes); + int packetCrc = ((int)packetBytes[packetBytes.length - 2] & 0xff) | (((int)packetBytes[packetBytes.length - 1] & 0xff) << 8); + Log.i(TAG, "checksum: " + calculatedCrc + " " + packetCrc); + if (calculatedCrc == packetCrc) { + Log.i(TAG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); + } + } } _currentFrameBuffer.clear(); _readByte = 0; @@ -147,6 +161,8 @@ public class Hdlc implements Protocol { buffer.flip(); byte[] data = new byte[buffer.remaining()]; buffer.get(data); + Log.i(TAG, DebugTools.bytesToHex(data)); + byte[] dataBytesAsBits = BitTools.convertToHDLCBitArray(data, true); // add preamble diff --git a/codec2talkie/src/main/java/com/radio/codec2talkie/tools/BitTools.java b/codec2talkie/src/main/java/com/radio/codec2talkie/tools/BitTools.java index 89f0a53..b41cfd1 100644 --- a/codec2talkie/src/main/java/com/radio/codec2talkie/tools/BitTools.java +++ b/codec2talkie/src/main/java/com/radio/codec2talkie/tools/BitTools.java @@ -77,8 +77,7 @@ public class BitTools { boolean skipNext = false; StringBuffer s = new StringBuffer(); int cntBits = 0; - for (int i = 0; i < dataBitsAsBytes.length; i++) { - byte currentBit = dataBitsAsBytes[i]; + for (byte currentBit : dataBitsAsBytes) { if (skipNext) { // cannot have 6 consecutive 1, non-HDLC data if (currentBit == 1) return null; @@ -94,9 +93,12 @@ public class BitTools { } else { cntOnes = 0; } - if (i % 8 == 7) { + if (cntBits % 8 == 3) { + s.append(':'); + } + if (cntBits % 8 == 7) { s.append(' '); - byteBuffer.put((byte)(currentByte & 0xff)); + byteBuffer.put((byte) (currentByte & 0xff)); currentByte = 0; } // 5 ones, skip next diff --git a/codec2talkie/src/main/java/com/radio/codec2talkie/transport/SoundModem.java b/codec2talkie/src/main/java/com/radio/codec2talkie/transport/SoundModem.java index 5fbab58..8a49dc0 100644 --- a/codec2talkie/src/main/java/com/radio/codec2talkie/transport/SoundModem.java +++ b/codec2talkie/src/main/java/com/radio/codec2talkie/transport/SoundModem.java @@ -13,6 +13,7 @@ import android.util.Log; import androidx.preference.PreferenceManager; import com.radio.codec2talkie.settings.PreferenceKeys; +import com.radio.codec2talkie.tools.AudioTools; import com.radio.codec2talkie.tools.BitTools; import com.radio.codec2talkie.tools.DebugTools; import com.ustadmobile.codec2.Codec2; @@ -45,6 +46,7 @@ public class SoundModem implements Transport, Runnable { private final SharedPreferences _sharedPreferences; private boolean _isRunning = true; + private final boolean _loopbackTest = false; private final long _fskModem; @@ -69,7 +71,8 @@ public class SoundModem implements Transport, Runnable { constructSystemAudioDevices(); - new Thread(this).start(); + if (!_loopbackTest) + new Thread(this).start(); } private void constructSystemAudioDevices() { @@ -122,7 +125,7 @@ public class SoundModem implements Transport, Runnable { _bitBuffer.flip(); int len = _bitBuffer.remaining(); _bitBuffer.get(data, 0, len); - //Log.i(TAG, "-- " + DebugTools.byteBitsToString(data)); + //Log.i(TAG, "read " + DebugTools.byteBitsToString(data)); _bitBuffer.compact(); return len; } @@ -134,6 +137,11 @@ public class SoundModem implements Transport, Runnable { public int write(byte[] srcDataBytesAsBits) throws IOException { byte[] dataBytesAsBits = BitTools.convertToNRZI(srcDataBytesAsBits); + if (_loopbackTest) { + Log.i(TAG, "write " + DebugTools.byteBitsToString(srcDataBytesAsBits)); + _bitBuffer.put(BitTools.convertFromNRZI(dataBytesAsBits, (byte) 0)); + } + int j = 0; for (int i = 0; i < dataBytesAsBits.length; i++, j++) { if (j >= _playbackBitBuffer.length) { @@ -168,6 +176,7 @@ public class SoundModem implements Transport, Runnable { while (_isRunning) { // TODO, take readCnt into account, do not read if playback is active int readCnt = _systemAudioRecorder.read(_recordAudioBuffer, 0, Codec2.fskNin(_fskModem)); + //Log.i(TAG, "! " + AudioTools.getSampleLevelDb(Arrays.copyOf(_recordAudioBuffer, Codec2.fskNin(_fskModem)))); //Log.i(TAG, DebugTools.shortsToHex(_recordAudioBuffer)); //Log.i(TAG, readCnt + " " + _recordAudioBuffer.length + " " + Codec2.fskNin(_fskModem)); Codec2.fskDemodulate(_fskModem, _recordAudioBuffer, _recordBitBuffer); diff --git a/libcodec2-android/src/main/cpp/Codec2JNI.cpp b/libcodec2-android/src/main/cpp/Codec2JNI.cpp index d1d2b60..720cc4c 100644 --- a/libcodec2-android/src/main/cpp/Codec2JNI.cpp +++ b/libcodec2-android/src/main/cpp/Codec2JNI.cpp @@ -74,7 +74,7 @@ namespace Java_com_ustadmobile_codec2_Codec2 { conFsk->demodBits = (uint8_t*)malloc(sizeof(uint8_t) * fsk->Nbits); conFsk->demodBuf = (int16_t*)malloc(sizeof(short) * (fsk->N + 2 * fsk->Ts)); - fsk_set_freq_est_limits(fsk, -sampleFrequency / 2, sampleFrequency / 2); + fsk_set_freq_est_limits(fsk, 500, 2700); fsk_set_freq_est_alg(fsk, 1); auto pv = (unsigned long) conFsk;