diff --git a/.gitignore b/.gitignore
index f0f4d72..5234d66 100644
--- a/.gitignore
+++ b/.gitignore
@@ -30,3 +30,37 @@ bin
Debug
Release
.settings
+
+# Clion files #
+######################
+# User-specific stuff
+.idea/**/workspace.xml
+.idea/**/tasks.xml
+.idea/**/usage.statistics.xml
+.idea/**/dictionaries
+.idea/**/shelf
+
+# Generated files
+.idea/**/contentModel.xml
+
+# Sensitive or high-churn files
+.idea/**/dataSources/
+.idea/**/dataSources.ids
+.idea/**/dataSources.local.xml
+.idea/**/sqlDataSources.xml
+.idea/**/dynamic.xml
+.idea/**/uiDesigner.xml
+.idea/**/dbnavigator.xml
+
+# Gradle
+.idea/**/gradle.xml
+.idea/**/libraries
+
+# CMake
+cmake-build-*/
+
+# JIRA plugin
+atlassian-ide-plugin.xml
+
+# File-based project format
+*.iws
diff --git a/DSP_API/.cproject b/DSP_API/.cproject
index 0e2ef20..f2b4bd7 100644
--- a/DSP_API/.cproject
+++ b/DSP_API/.cproject
@@ -1,323 +1,650 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/DSP_API/.idea/.name b/DSP_API/.idea/.name
new file mode 100644
index 0000000..39227f3
--- /dev/null
+++ b/DSP_API/.idea/.name
@@ -0,0 +1 @@
+thumbdv
\ No newline at end of file
diff --git a/DSP_API/.idea/DSP_API.iml b/DSP_API/.idea/DSP_API.iml
new file mode 100644
index 0000000..f08604b
--- /dev/null
+++ b/DSP_API/.idea/DSP_API.iml
@@ -0,0 +1,2 @@
+
+
\ No newline at end of file
diff --git a/DSP_API/.idea/misc.xml b/DSP_API/.idea/misc.xml
new file mode 100644
index 0000000..8822db8
--- /dev/null
+++ b/DSP_API/.idea/misc.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/DSP_API/.idea/modules.xml b/DSP_API/.idea/modules.xml
new file mode 100644
index 0000000..ed3c9ca
--- /dev/null
+++ b/DSP_API/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/DSP_API/.idea/vcs.xml b/DSP_API/.idea/vcs.xml
new file mode 100644
index 0000000..6c0b863
--- /dev/null
+++ b/DSP_API/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/DSP_API/.idea/workspace.xml b/DSP_API/.idea/workspace.xml
new file mode 100644
index 0000000..f745567
--- /dev/null
+++ b/DSP_API/.idea/workspace.xml
@@ -0,0 +1,239 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ stac
+ terminated
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 1562709596724
+
+
+ 1562709596724
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/DSP_API/CMakeLists.txt b/DSP_API/CMakeLists.txt
new file mode 100644
index 0000000..d4bf16a
--- /dev/null
+++ b/DSP_API/CMakeLists.txt
@@ -0,0 +1,133 @@
+cmake_minimum_required(VERSION 3.14)
+project(thumbdv)
+
+set(CMAKE_C_STANDARD 11)
+set(CMAKE_SYSTEM_NAME Linux)
+set(CMAKE_SYSTEM_VERSION gnu)
+set(CMAKE_VERBOSE_MAKEFILE ON)
+
+## Setting up the four types of builds we have
+set(CMAKE_CONFIGURATION_TYPES "ThumbDV_Debug;ThumbDV_Release;Test_Dongle" CACHE STRING "" FORCE)
+
+include_directories(.)
+include_directories(include)
+include_directories(ThumbDV)
+include_directories(SmartSDR_Interface)
+
+link_directories(/src/flex/smoothlake/firmware/src/Firmware/lib
+ /src/flex/smartsdr-dsp/DSP_API/lib)
+
+add_executable(thumbdv
+ SmartSDR_Interface/cmd_basics.c
+ SmartSDR_Interface/cmd_engine.c
+ SmartSDR_Interface/discovery_client.c
+ SmartSDR_Interface/hal_buffer.c
+ SmartSDR_Interface/hal_listener.c
+ SmartSDR_Interface/hal_vita.c
+ SmartSDR_Interface/io_utils.c
+ SmartSDR_Interface/sched_waveform.c
+ SmartSDR_Interface/smartsdr_dsp_api.c
+ SmartSDR_Interface/status_processor.c
+ SmartSDR_Interface/traffic_cop.c
+ SmartSDR_Interface/utils.c
+ SmartSDR_Interface/vita_output.c
+ ThumbDV/bit_pattern_matcher.c
+ ThumbDV/dstar.c
+ ThumbDV/gmsk_modem.c
+ ThumbDV/slow_data.c
+ ThumbDV/thumbDV.c
+ circular_buffer.c
+ resampler.c
+ main.c)
+
+
+if (CMAKE_BUILD_TYPE MATCHES ThumbDV_Release)
+ target_compile_options(thumbdv PRIVATE
+ -U_FORTIFY_SOURCE
+ -O3
+ -mcpu=cortex-a8
+ -mfpu=neon
+ -mfloat-abi=softfp
+ -ftree-vectorize
+ -Wall
+ -c
+ -fmessage-length=0
+ -lpthread
+ -MMD
+ -MP
+ -MF
+ )
+elseif(CMAKE_BUILD_TYPE MATCHES Test_Dongle)
+ target_compile_options(thumbdv PRIVATE
+ -U_FORTIFY_SOURCE
+ -O3
+ #-mcpu=cortex-a8
+ #-mfpu=neon
+ #-mfloat-abi=softfp
+ -ftree-vectorize
+ -Wall
+ -c
+ -fmessage-length=0
+ -lpthread
+ -MMD
+ -MP
+ -MF
+ )
+elseif(CMAKE_BUILD_TYPE MATCHES ThumbDV_Debug)
+ target_compile_options(thumbdv PRIVATE
+ -O0
+ -mcpu=cortex-a8
+ -mfpu=neon
+ -mfloat-abi=softfp
+ -ftree-vectorize
+ -Wall
+ -ggdb
+ -g3
+ -fstack-protector-all
+ -funwind-tables
+ -c
+ -fmessage-length=0
+ -lpthread
+ -MMD
+ -MP
+ -MF
+ )
+ target_compile_definitions(thumbdv PRIVATE
+ DEBUG)
+
+elseif(CMAKE_BUILD_TYPE MATCHES Debug)
+ target_compile_options(thumbdv PRIVATE
+ -O0
+ -ftree-vectorize
+ -g3
+ -ggdb
+ -g3
+ -fstack-protector-all
+ -funwind-tables
+ -Wall
+ -c
+ -fmessage-length=0
+ -lpthread
+ -MMD
+ -MP
+ -MF
+ )
+ target_compile_definitions(thumbdv PRIVATE
+ DEBUG)
+
+endif()
+
+TARGET_LINK_LIBRARIES(thumbdv PRIVATE
+ ftd2xx
+ rt
+ pthread
+ dl
+ m)
+
+
+target_link_options(thumbdv INTERFACE
+ -Map=Firmware.map)
+
+
+add_custom_command(TARGET thumbdv POST_BUILD
+ COMMAND ${CMAKE_COMMAND} -E copy $ /nfsroots/microburst/home/root)
\ No newline at end of file
diff --git a/DSP_API/SmartSDR_Interface/sched_waveform.c b/DSP_API/SmartSDR_Interface/sched_waveform.c
index b211081..7e483aa 100644
--- a/DSP_API/SmartSDR_Interface/sched_waveform.c
+++ b/DSP_API/SmartSDR_Interface/sched_waveform.c
@@ -519,12 +519,20 @@ static void * _sched_waveform_thread( void * param ) {
}
if ( ambe_packet_out == TRUE ) {
+ thumbDV_decode( _dv_serial_handle, ambe_out, DV_PACKET_SAMPLES );
+ }
+
+ if ( thumbDV_getDecodeListBuffering() == FALSE)
+ {
+ // There is something in the decoded list - fetch audio
nout = 0;
- nout = thumbDV_decode( _dv_serial_handle, ambe_out, speech_out, DV_PACKET_SAMPLES );
+ nout = thumbDV_unlinkAudio(speech_out);
uint32 j = 0;
for ( j = 0 ; j < nout ; j++ )
+ {
cbWriteShort( RX3_cb, speech_out[j] );
+ }
}
}
diff --git a/DSP_API/SmartSDR_Interface/utils.c b/DSP_API/SmartSDR_Interface/utils.c
index 2dd9860..f35c62b 100644
--- a/DSP_API/SmartSDR_Interface/utils.c
+++ b/DSP_API/SmartSDR_Interface/utils.c
@@ -86,6 +86,7 @@ float tsfSubtract(struct timespec time1, struct timespec time2)
return result;
}
+
//! get time since a certain time in microseconds
uint32 usSince(struct timespec time)
{
@@ -95,6 +96,14 @@ uint32 usSince(struct timespec time)
return diff_us;
}
+uint32 msSince(struct timespec time)
+{
+ struct timespec delay;
+ clock_gettime(CLOCK_MONOTONIC, &delay);
+ uint32 diff_ms = (uint32)(tsSubtract(delay, time));
+ return diff_ms;
+}
+
uint32 getIP(char* text)
{
uint32 ip;
diff --git a/DSP_API/SmartSDR_Interface/utils.h b/DSP_API/SmartSDR_Interface/utils.h
index 8f6fe52..22d6371 100644
--- a/DSP_API/SmartSDR_Interface/utils.h
+++ b/DSP_API/SmartSDR_Interface/utils.h
@@ -40,6 +40,7 @@ void output(const char *fmt,...);
void tsAdd(struct timespec* time1, struct timespec time2);
float tsfSubtract(struct timespec time1, struct timespec time2);
uint32 usSince(struct timespec time);
+uint32 msSince(struct timespec time);
uint32 getIP(char* text);
void lock_malloc_init(void);
void* safe_malloc(size_t size);
diff --git a/DSP_API/ThumbDV/dstar.c b/DSP_API/ThumbDV/dstar.c
index b2a2ff7..12bef54 100644
--- a/DSP_API/ThumbDV/dstar.c
+++ b/DSP_API/ThumbDV/dstar.c
@@ -951,15 +951,20 @@ BOOL dstar_rxStateMachine( DSTAR_MACHINE machine, BOOL in_bit, unsigned char * a
dstar_updateStatus( machine, machine->slice, STATUS_RX );
+ machine->rx_state = VOICE_FRAME;
+ machine->bit_count = 0;
+ machine->frame_count = 0;
+
} else {
output( ANSI_RED "P_FCS Does Not Match!\n" ANSI_WHITE );
+
+ machine->rx_state = BIT_FRAME_SYNC;
+ machine->bit_count = 0;
}
/* STATE CHANGE */
- machine->rx_state = VOICE_FRAME;
- machine->bit_count = 0;
- machine->frame_count = 0;
+
}
break;
diff --git a/DSP_API/ThumbDV/thumbDV.c b/DSP_API/ThumbDV/thumbDV.c
index 4602544..c8aa797 100644
--- a/DSP_API/ThumbDV/thumbDV.c
+++ b/DSP_API/ThumbDV/thumbDV.c
@@ -73,9 +73,12 @@
#define THUMBDV_MAX_PACKET_LEN 2048U
static pthread_t _read_thread;
+static pthread_t _connect_thread;
BOOL _readThreadAbort = FALSE;
+BOOL _connectThreadAbort = FALSE;
-static uint32 _buffering_target = 1;
+static uint32 _buffering_target = 0;
+static uint32 _encode_buffering_target = 4;
static pthread_rwlock_t _encoded_list_lock;
static BufferDescriptor _encoded_root;
@@ -87,9 +90,13 @@ static BufferDescriptor _decoded_root;
static BOOL _decoded_buffering = TRUE;
static uint32 _decoded_count = 0;
-static void * _thumbDV_readThread( void * param );
+static sem_t _read_sem;
-static BufferDescriptor _thumbDVEncodedList_UnlinkHead( void ) {
+//static void * _thumbDV_readThread( void * param );
+
+BOOL allowedToRead = TRUE;
+
+static BufferDescriptor _thumbDVEncodedList_UnlinkHead(void ) {
BufferDescriptor buf_desc = NULL;
pthread_rwlock_wrlock( &_encoded_list_lock );
@@ -139,7 +146,7 @@ static void _thumbDVEncodedList_LinkTail( BufferDescriptor buf_desc ) {
_encoded_root->prev = buf_desc;
_encoded_count++;
- if ( _encoded_count > _buffering_target ) {
+ if ( _encoded_count > _encode_buffering_target ) {
if ( _encoded_buffering ) output( "Encoded Buffering is now FALSE\n" );
_encoded_buffering = FALSE;
@@ -183,7 +190,7 @@ static BufferDescriptor _thumbDVDecodedList_UnlinkHead( void ) {
}
} else {
if ( !_decoded_buffering )
- output( "DecodedList now Buffering \n" );
+ //output( "DecodedList now Buffering \n" );
_decoded_buffering = TRUE;
}
@@ -202,7 +209,7 @@ static void _thumbDVDecodedList_LinkTail( BufferDescriptor buf_desc ) {
_decoded_count++;
if ( _decoded_count > _buffering_target ) {
- if ( _decoded_buffering ) output( "Decoded Buffering is now FALSE\n" );
+ // if ( _decoded_buffering ) output( "Decoded Buffering is now FALSE\n" );
_decoded_buffering = FALSE;
}
@@ -210,6 +217,11 @@ static void _thumbDVDecodedList_LinkTail( BufferDescriptor buf_desc ) {
pthread_rwlock_unlock( &_decoded_list_lock );
}
+BOOL thumbDV_getDecodeListBuffering(void)
+{
+ return _decoded_buffering;
+}
+
static void delay( unsigned int delay ) {
struct timespec tim, tim2;
tim.tv_sec = 0;
@@ -277,34 +289,37 @@ void thumbDV_dump( char * text, unsigned char * data, unsigned int length ) {
}
}
-static void thumbDV_writeSerial( FT_HANDLE handle , unsigned char * buffer, uint32 bytes )
+static int thumbDV_writeSerial( FT_HANDLE handle , unsigned char * buffer, uint32 bytes )
{
FT_STATUS status = FT_OK;
DWORD written = 0;
if ( handle != NULL )
{
+ //FT_SetRts(handle);
status = FT_Write(handle, buffer, bytes, &written);
if ( status != FT_OK || written != bytes ) {
output( ANSI_RED "Could not write to serial port. status = %d\n", status );
- return;
+ return status;
}
+ //FT_ClrRts(handle);
}
else
{
output( ANSI_RED "Could not write to serial port. Timeout\n" ANSI_WHITE );
}
+ return status;
}
static int _check_serial( FT_HANDLE handle )
{
-
+ int ret = 0;
unsigned char reset[5] = { 0x61, 0x00, 0x01, 0x00, 0x33 };
- thumbDV_writeSerial( handle, reset, 5 );
- int ret = thumbDV_processSerial(handle);
+ thumbDV_writeSerial( handle, reset, 5 );
+ ret = thumbDV_processSerial(handle);
if ( ret != 0 )
{
@@ -314,7 +329,6 @@ static int _check_serial( FT_HANDLE handle )
unsigned char get_prodID[5] = {0x61, 0x00, 0x01, 0x00, 0x30 };
thumbDV_writeSerial( handle, get_prodID, 5 );
-
ret = thumbDV_processSerial(handle);
if ( ret != 0 )
@@ -323,7 +337,6 @@ static int _check_serial( FT_HANDLE handle )
return -1;
}
-
return 0 ;
}
@@ -332,8 +345,9 @@ FT_HANDLE thumbDV_openSerial( FT_DEVICE_LIST_INFO_NODE device )
//struct termios tty;
FT_HANDLE handle = NULL;
FT_STATUS status = FT_OK;
+ UCHAR latency = 5;
- output("Trying to open serial port %s", device.SerialNumber);
+ output("Trying to open serial port %s \n", device.SerialNumber);
status = FT_OpenEx(device.SerialNumber, FT_OPEN_BY_SERIAL_NUMBER, &handle);
@@ -348,13 +362,9 @@ FT_HANDLE thumbDV_openSerial( FT_DEVICE_LIST_INFO_NODE device )
}
FT_SetBaudRate(handle, FT_BAUD_460800);
-
- // Set read and write timeout to 2seconds */
- FT_SetTimeouts(handle, 0, 0);
-
-
FT_SetDataCharacteristics(handle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);
- FT_SetFlowControl(handle, FT_FLOW_NONE, 0, 0);
+ FT_SetTimeouts(handle, 0, 0);
+ FT_SetFlowControl(handle, FT_FLOW_RTS_CTS, 0, 0);
/*
tty.c_cflag = ( tty.c_cflag & ~CSIZE ) | CS8;
@@ -400,13 +410,10 @@ FT_HANDLE thumbDV_openSerial( FT_DEVICE_LIST_INFO_NODE device )
}
FT_SetBaudRate(handle, FT_BAUD_230400 );
-
- FT_SetTimeouts(handle, 0, 0);
-
-
FT_SetDataCharacteristics(handle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);
+ FT_SetTimeouts(handle, 0, 0);
FT_SetFlowControl(handle, FT_FLOW_NONE, 0, 0);
-
+ FT_SetLatencyTimer(handle, latency);
if ( _check_serial( handle ) != 0 ) {
output( "Could not detect THumbDV at 230400 Baud\n" );
@@ -422,7 +429,6 @@ int thumbDV_processSerial( FT_HANDLE handle )
{
unsigned char buffer[BUFFER_LENGTH];
unsigned int respLen;
- uint32 offset = 0;
unsigned char packet_type;
FT_STATUS status = FT_OK;
@@ -444,8 +450,8 @@ int thumbDV_processSerial( FT_HANDLE handle )
if ( us_slept > max_us_sleep )
{
- output("TimeOut\n");
- return 1;
+ output("TimeOut #1\n");
+ return FT_OTHER_ERROR;
}
} while (rx_bytes < AMBE3000_HEADER_LEN && status == FT_OK );
@@ -460,11 +466,9 @@ int thumbDV_processSerial( FT_HANDLE handle )
if ( buffer[0U] != AMBE3000_START_BYTE ) {
output( ANSI_RED "ThumbDV: unknown byte from the DV3000, 0x%02X\n" ANSI_WHITE, buffer[0U] );
- return 1;
+ return FT_OTHER_ERROR;
}
- offset = 0U;
-
respLen = buffer[1U] * 256U + buffer[2U];
us_slept = 0;
@@ -475,14 +479,14 @@ int thumbDV_processSerial( FT_HANDLE handle )
if ( rx_bytes >= respLen )
break;
- usleep(1000);
+ usleep(100);
- us_slept += 1000 ;
+ us_slept += 100 ;
if ( us_slept > max_us_sleep )
{
- output("TimeOut\n");
- return 1;
+ output("TimeOut #2 \n");
+ return FT_OTHER_ERROR;
}
} while (rx_bytes < respLen && status == FT_OK);
@@ -517,37 +521,16 @@ int thumbDV_processSerial( FT_HANDLE handle )
} else {
output( ANSI_RED "Unrecognized packet type 0x%02X ", packet_type );
- return 1;
+ return FT_OTHER_ERROR;
}
- return 0;
+ return FT_OK;
}
-int thumbDV_decode( FT_HANDLE handle, unsigned char * packet_in, short * speech_out, uint8 bytes_in_packet ) {
- uint32 i = 0;
-
- unsigned char full_packet[15] = {0};
-
- if ( packet_in != NULL && handle != NULL ) {
- full_packet[0] = 0x61;
- full_packet[1] = 0x00;
- full_packet[2] = 0x0B;
- full_packet[3] = 0x01;
- full_packet[4] = 0x01;
- full_packet[5] = 0x48;
- uint32 j = 0;
-
- for ( i = 0, j = 8 ; i < 9 ; i++ , j-- ) {
- full_packet[i + 6] = packet_in[i];
- }
-
-// thumbDV_dump("Just AMBE", packet_in, 9);
-// thumbDV_dump("Encoded packet:", full_packet, 15);
- thumbDV_writeSerial( handle, full_packet, 15 );
- }
-
+int thumbDV_unlinkAudio(short * speech_out)
+{
int32 samples_returned = 0;
BufferDescriptor desc = _thumbDVDecodedList_UnlinkHead();
uint32 samples_in_speech_packet = 0;
@@ -583,6 +566,31 @@ int thumbDV_decode( FT_HANDLE handle, unsigned char * packet_in, short * speech_
return samples_returned;
}
+void thumbDV_decode( FT_HANDLE handle, unsigned char * packet_in, uint8 bytes_in_packet ) {
+ uint32 i = 0;
+
+ unsigned char full_packet[15] = {0};
+
+ if ( packet_in != NULL && handle != NULL ) {
+ full_packet[0] = 0x61;
+ full_packet[1] = 0x00;
+ full_packet[2] = 0x0B;
+ full_packet[3] = 0x01;
+ full_packet[4] = 0x01;
+ full_packet[5] = 0x48;
+ uint32 j = 0;
+
+ for ( i = 0, j = 8 ; i < 9 ; i++ , j-- ) {
+ full_packet[i + 6] = packet_in[i];
+ }
+
+// thumbDV_dump("Just AMBE", packet_in, 9);
+// thumbDV_dump("Encoded packet:", full_packet, 15);
+ thumbDV_writeSerial( handle, full_packet, 15 );
+ sem_post(&_read_sem);
+ }
+}
+
int thumbDV_encode( FT_HANDLE handle, short * speech_in, unsigned char * packet_out, uint8 num_of_samples )
{
unsigned char packet[THUMBDV_MAX_PACKET_LEN];
@@ -635,7 +643,10 @@ int thumbDV_encode( FT_HANDLE handle, short * speech_in, unsigned char * packet_
}
if ( handle != NULL )
+ {
thumbDV_writeSerial( handle, packet, length + AMBE3000_HEADER_LEN );
+ sem_post(&_read_sem);
+ }
int32 samples_returned = 0;
BufferDescriptor desc = _thumbDVEncodedList_UnlinkHead();
@@ -668,10 +679,18 @@ static void _connectSerial( FT_HANDLE * ftHandle )
do {
status = FT_CreateDeviceInfoList(&numDevs);
+ if (status != FT_OK)
+ {
+ output("Unable to create Device Info \n");
+ }
devInfo = (FT_DEVICE_LIST_INFO_NODE *) safe_malloc(sizeof(FT_DEVICE_LIST_INFO_NODE) * numDevs);
status = FT_GetDeviceInfoList(devInfo, &numDevs);
+ if (status != FT_OK)
+ {
+ output("Unable to fetch Device Info \n");
+ }
for ( i = 0 ; i < numDevs ; i++ )
{
@@ -692,52 +711,69 @@ static void _connectSerial( FT_HANDLE * ftHandle )
}
} while ( *ftHandle == NULL ) ;
- unsigned char reset[5] = { 0x61, 0x00, 0x01, 0x00, 0x33 };
- thumbDV_writeSerial( *ftHandle, reset, 5 );
- /* Block until we get data from serial port after reset */
- thumbDV_processSerial( *ftHandle );
-//
-// unsigned char reset_softcfg[11] = {0x61, 0x00, 0x07, 0x00, 0x34, 0x05, 0x03, 0xEB, 0xFF, 0xFF, 0xFF};
-// thumbDV_writeSerial(*ftHandle, reset_softcfg, 11);
-// thumbDV_processSerial(*ftHandle);
+ // Reset and Product ID printout are done in thumbDV_openSerial() which calls _check_serial()
unsigned char disable_parity[6] = {0x61, 0x00, 0x02, 0x00, 0x3F, 0x00};
thumbDV_writeSerial( *ftHandle, disable_parity, 6 );
- thumbDV_processSerial( *ftHandle );
+ thumbDV_processSerial(*ftHandle);
- unsigned char get_prodID[5] = {0x61, 0x00, 0x01, 0x00, 0x30 };
unsigned char get_version[5] = {0x61, 0x00, 0x01, 0x00, 0x31};
unsigned char read_cfg[5] = {0x61, 0x00, 0x01, 0x00, 0x37};
unsigned char dstar_mode[17] = {0x61, 0x00, 0x0D, 0x00, 0x0A, 0x01, 0x30, 0x07, 0x63, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48};
- thumbDV_writeSerial( *ftHandle, get_prodID, 5 );
thumbDV_writeSerial( *ftHandle, get_version, 5 );
+ thumbDV_processSerial(*ftHandle);
thumbDV_writeSerial( *ftHandle, read_cfg, 5 );
+ thumbDV_processSerial(*ftHandle);
thumbDV_writeSerial( *ftHandle, dstar_mode, 17 );
+ thumbDV_processSerial(*ftHandle);
//// /* Init */
unsigned char pkt_init[6] = { 0x61, 0x00, 0x02, 0x00, 0x0B, 0x07 };
thumbDV_writeSerial( *ftHandle, pkt_init, 6 );
+ thumbDV_processSerial(*ftHandle);
/* PKT GAIN - set to 0dB */
unsigned char pkt_gain[7] = { 0x61, 0x00, 0x03, 0x00, 0x4B, 0x00, 0x00 };
thumbDV_writeSerial( *ftHandle, pkt_gain, 7 );
+ thumbDV_processSerial(*ftHandle);
/* Companding off so it uses 16bit linear */
unsigned char pkt_compand[6] = { 0x61, 0x00, 0x02, 0x00, 0x32, 0x00 };
thumbDV_writeSerial( *ftHandle, pkt_compand, 6 );
-
- unsigned char test_coded[15] = {0x61, 0x00 , 0x0B , 0x01 , 0x01 , 0x48 , 0x5E , 0x83 , 0x12 , 0x3B , 0x98 , 0x79 , 0xDE , 0x13 , 0x90};
-
- thumbDV_writeSerial( *ftHandle, test_coded, 15 );
+ thumbDV_processSerial(*ftHandle);
unsigned char pkt_fmt[7] = {0x61, 0x00, 0x3, 0x00, 0x15, 0x00, 0x00};
thumbDV_writeSerial( *ftHandle, pkt_fmt, 7 );
+ thumbDV_processSerial(*ftHandle);
}
-
static void * _thumbDV_readThread( void * param )
+{
+ FT_STATUS status = FT_OK;
+ FT_HANDLE handle = *(FT_HANDLE *) param;
+
+ prctl(PR_SET_NAME, "DV-Read");
+
+ while ( !_readThreadAbort )
+ {
+ sem_wait(&_read_sem);
+
+ if (!allowedToRead)
+ {
+ break;
+ }
+ else
+ {
+ thumbDV_processSerial(handle);
+ }
+ }
+ output( ANSI_YELLOW "thumbDV_readThread has exited\n" ANSI_WHITE );
+ return 0;
+}
+
+static void * _thumbDV_connectThread( void * param )
{
int ret;
DWORD rx_bytes;
@@ -745,60 +781,41 @@ static void * _thumbDV_readThread( void * param )
DWORD event_dword;
FT_STATUS status = FT_OK;
- FT_HANDLE handle = *( FT_HANDLE * )param;
- EVENT_HANDLE event_handle;
+ FT_HANDLE handle = *(FT_HANDLE *) param;
- prctl(PR_SET_NAME, "DV-Read");
+ while ( !_connectThreadAbort ) {
+ //TODO Handle reconnection
+ ret = FT_GetStatus(handle, &rx_bytes, &tx_bytes, &event_dword);
- pthread_mutex_init(&event_handle.eMutex, NULL);
- pthread_cond_init(&event_handle.eCondVar, NULL);
+ if (ret != FT_OK) {
- while ( !_readThreadAbort )
- {
- // Setup RX or Status change event notification
- status = FT_SetEventNotification(handle, FT_EVENT_RXCHAR , (PVOID)&event_handle);
+ //clear out read buffer and stop read thread
+ sem_post(&_read_sem);
+ allowedToRead = FALSE;
- struct timespec timeout;
- clock_gettime(CLOCK_REALTIME, &timeout);
+ output("Serial is disconnected\n");
- timeout.tv_sec += 2; // 2 second timeout
-
- // Will block until
- pthread_mutex_lock(&event_handle.eMutex);
- pthread_cond_timedwait(&event_handle.eCondVar, &event_handle.eMutex, &timeout);
- pthread_mutex_unlock(&event_handle.eMutex);
-
- rx_bytes = 0;
- status = FT_GetStatus(handle, &rx_bytes, &tx_bytes, &event_dword);
-
- if ( status != FT_OK )
- {
- fprintf( stderr, "ThumbDV: error from status, status=%d\n", status );
-
- /* Set invalid FD in sched_waveform so we don't call write functions */
+ //Set invalid FD in sched_waveform so we don't call write functions
handle = NULL;
sched_waveform_setHandle(&handle);
- /* This function hangs until a new connection is made */
- _connectSerial( &handle );
- /* Update the sched_waveform to new valid serial */
- sched_waveform_setHandle( &handle );
- }
- else if ( rx_bytes >= AMBE3000_HEADER_LEN )
- {
- ret = thumbDV_processSerial( handle );
- }
-
+ //This function hangs until a new connection is made
+ _connectSerial(&handle);
+ //Update the sched_waveform to new valid serial
+ sched_waveform_setHandle(&handle);
+ //Start read thread again
+ allowedToRead = TRUE;
+ pthread_create( &_read_thread, NULL, &_thumbDV_readThread, &handle );
+ }
}
-
- output( ANSI_YELLOW "thumbDV_readThread has exited\n" ANSI_WHITE );
- return 0;
}
void thumbDV_init( FT_HANDLE * handle ) {
pthread_rwlock_init( &_encoded_list_lock, NULL );
pthread_rwlock_init( &_decoded_list_lock, NULL );
+ sem_init(&_read_sem, 0, 0);
+
pthread_rwlock_wrlock( &_encoded_list_lock );
_encoded_root = ( BufferDescriptor )safe_malloc( sizeof( buffer_descriptor ) );
memset( _encoded_root, 0, sizeof( buffer_descriptor ) );
@@ -813,8 +830,9 @@ void thumbDV_init( FT_HANDLE * handle ) {
_decoded_root->prev = _decoded_root;
pthread_rwlock_unlock( &_decoded_list_lock );
- _connectSerial( handle );
+ _connectSerial(handle);
+ pthread_create( &_connect_thread, NULL, &_thumbDV_connectThread, handle );
pthread_create( &_read_thread, NULL, &_thumbDV_readThread, handle );
struct sched_param fifo_param;
diff --git a/DSP_API/ThumbDV/thumbDV.h b/DSP_API/ThumbDV/thumbDV.h
index 53b68cd..763cfe6 100644
--- a/DSP_API/ThumbDV/thumbDV.h
+++ b/DSP_API/ThumbDV/thumbDV.h
@@ -43,8 +43,11 @@ FT_HANDLE thumbDV_openSerial( FT_DEVICE_LIST_INFO_NODE device );
int thumbDV_processSerial( FT_HANDLE handle );
int thumbDV_encode( FT_HANDLE handle, short * speech_in, unsigned char * packet_out, uint8 num_of_samples );
-int thumbDV_decode( FT_HANDLE handle, unsigned char * packet_in, short * speech_out, uint8 bytes_in_packet );
+void thumbDV_decode( FT_HANDLE handle, unsigned char * packet_in, uint8 bytes_in_packet );
void thumbDV_dump( char * text, unsigned char * data, unsigned int length );
void thumbDV_flushLists(void);
+
+BOOL thumbDV_getDecodeListBuffering(void);
+int thumbDV_unlinkAudio(short * speech_out);
#endif /* THUMBDV_THUMBDV_ */