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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
//
// This file is part of Dire Wolf, an amateur radio packet TNC.
//
// Copyright (C) 2011,2012,2013 John Langner, WB2OSZ
//
// 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, see <http://www.gnu.org/licenses/>.
//
/*------------------------------------------------------------------
*
* Module: redecode.c
*
* Purpose: Retry decoding frames that have a bad FCS.
*
* Description:
*
*
* Usage: (1) The main application calls redecode_init.
*
* This will initialize the retry decoding queue
* and create a thread to work on contents of the queue.
*
* (2) The application queues up frames by calling rdq_append.
*
*
* (3) redecode_thread removes raw frames from the queue and
* tries to recover from errors.
*
*---------------------------------------------------------------*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
//#include <sys/time.h>
#include <time.h>
#if __WIN32__
#include <windows.h>
#endif
#include "direwolf.h"
#include "ax25_pad.h"
#include "textcolor.h"
#include "audio.h"
#include "rdq.h"
#include "redecode.h"
#include "hdlc_send.h"
#include "hdlc_rec2.h"
#include "ptt.h"
#if __WIN32__
static unsigned redecode_thread (void *arg);
#else
static void * redecode_thread (void *arg);
#endif
/*-------------------------------------------------------------------
*
* Name: redecode_init
*
* Purpose: Initialize the process to try fixing bits in frames with bad FCS.
*
* Inputs: none.
*
* Outputs: none.
*
* Description: Initialize the queue to be empty and set up other
* mechanisms for sharing it between different threads.
*
* Start up redecode_thread to actually process the
* raw frames from the queue.
*
*--------------------------------------------------------------------*/
void redecode_init (void)
{
//int j;
#if __WIN32__
HANDLE redecode_th;
#else
pthread_t redecode_tid;
int e;
#endif
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("redecode_init ( ... )\n");
#endif
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("redecode_init: about to call rdq_init \n");
#endif
rdq_init ();
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("redecode_init: about to create thread \n");
#endif
#if __WIN32__
redecode_th = _beginthreadex (NULL, 0, redecode_thread, NULL, 0, NULL);
if (redecode_th == NULL) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("Could not create redecode thread\n");
return;
}
#else
//TODO: Give thread lower priority.
e = pthread_create (&redecode_tid, NULL, redecode_thread, (void *)0);
if (e != 0) {
text_color_set(DW_COLOR_ERROR);
perror("Could not create redecode thread");
return;
}
#endif
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("redecode_init: finished \n");
#endif
} /* end redecode_init */
/*-------------------------------------------------------------------
*
* Name: redecode_thread
*
* Purpose: Try to decode frames with a bad FCS.
*
* Inputs: None.
*
* Outputs:
*
* Description: Initialize the queue to be empty and set up other
* mechanisms for sharing it between different threads.
*
*
*--------------------------------------------------------------------*/
#if __WIN32__
static unsigned redecode_thread (void *arg)
#else
static void * redecode_thread (void *arg)
#endif
{
rrbb_t block;
int blen;
int chan, subchan;
int alevel;
#if __WIN32__
HANDLE tid = GetCurrentThread();
//int tp;
//tp = GetThreadPriority (tid);
//text_color_set(DW_COLOR_DEBUG);
//dw_printf ("Starting redecode thread priority=%d\n", tp);
SetThreadPriority (tid, THREAD_PRIORITY_LOWEST);
//tp = GetThreadPriority (tid);
//dw_printf ("New redecode thread priority=%d\n", tp);
#endif
while (1) {
rdq_wait_while_empty ();
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("redecode_thread: woke up\n");
#endif
block = rdq_remove ();
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("redecode_thread: rdq_remove() returned %p\n", block);
#endif
/* Don't expect null ever but be safe. */
if (block != NULL) {
chan = rrbb_get_chan(block);
subchan = rrbb_get_subchan(block);
alevel = rrbb_get_audio_level(block);
blen = rrbb_get_len(block);
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("redecode_thread: begin processing %p, from channel %d, blen=%d\n", block, chan, blen);
#endif
hdlc_rec2_try_to_fix_later (block, chan, subchan, alevel);
#if DEBUG
text_color_set(DW_COLOR_DEBUG);
dw_printf ("redecode_thread: finished processing %p\n", block);
#endif
rrbb_delete (block);
}
}
return 0;
} /* end redecode_thread */
/* end redecode.c */
|