pull/1/head
Andreas Eversberg 1 year ago
parent 869625cfcd
commit 7fa5f85731
  1. 1
      .gitignore
  2. 1
      README
  3. 1
      configure.ac
  4. 1
      docs/index.html
  5. 1
      src/Makefile.am
  6. 1
      src/eurosignal/eurosignal.c
  7. 1
      src/libdebug/debug.c
  8. 65
      src/libdebug/debug.h
  9. 51
      src/pocsag/Makefile.am
  10. 288
      src/pocsag/dsp.c
  11. 4
      src/pocsag/dsp.h
  12. 494
      src/pocsag/frame.c
  13. 4
      src/pocsag/frame.h
  14. 6
      src/pocsag/image.c
  15. 355
      src/pocsag/main.c
  16. 537
      src/pocsag/pocsag.c
  17. 111
      src/pocsag/pocsag.h

1
.gitignore vendored

@ -70,6 +70,7 @@ src/imts/imts-dialer
src/mpt1327/mpt1327
src/jolly/jollycom
src/eurosignal/eurosignal
src/pocsag/pocsag
src/tv/osmotv
src/radio/osmoradio
src/datenklo/datenklo

@ -25,6 +25,7 @@ Additionally the following communication services are implemented:
* Radio transmitter / receiver
* Analog Modem Emulation (AM7911)
* German classic 'Zeitansage' (talking clock)
* POCSAG transmitter / receiver
USE AT YOUR OWN RISK!

@ -105,6 +105,7 @@ AC_OUTPUT(
src/mpt1327/Makefile
src/jolly/Makefile
src/eurosignal/Makefile
src/pocsag/Makefile
src/tv/Makefile
src/radio/Makefile
src/datenklo/Makefile

@ -125,6 +125,7 @@ Additional features:
<li><a href="magnetic.html">C-Netz Magnetic Card</a></li>
<li>Zeitansage (German talking clock)</li>
<li>C-Netz FuVSt (MSC to control a real base station)</li>
<li>POCSAG</li>
</ul>
</td></tr></table></center>

@ -55,6 +55,7 @@ SUBDIRS += \
mpt1327 \
jolly \
eurosignal \
pocsag \
tv \
radio \
zeitansage \

@ -656,7 +656,6 @@ int call_down_setup(int callref, const char __attribute__((unused)) *caller_id,
euro_t *euro;
euro_call_t *call;
/* find transmitter */
for (sender = sender_head; sender; sender = sender->next) {
/* skip channels that are different than requested */

@ -56,6 +56,7 @@ struct debug_cat {
{ "mpt1327", "\033[1;34m" },
{ "jollycom", "\033[1;34m" },
{ "eurosignal", "\033[1;34m" },
{ "pocsag", "\033[1;34m" },
{ "frame", "\033[0;36m" },
{ "call", "\033[0;37m" },
{ "cc", "\033[1;32m" },

@ -18,38 +18,39 @@
#define DMPT1327 11
#define DJOLLY 12
#define DEURO 13
#define DFRAME 14
#define DCALL 15
#define DCC 16
#define DDB 17
#define DTRANS 18
#define DDMS 19
#define DSMS 20
#define DSDR 21
#define DUHD 22
#define DSOAPY 23
#define DWAVE 24
#define DRADIO 25
#define DAM791X 26
#define DUART 27
#define DDEVICE 28
#define DDATENKLO 29
#define DZEIT 30
#define DSIM1 31
#define DSIM2 32
#define DSIMI 33
#define DSIM7 34
#define DMTP2 35
#define DMTP3 36
#define DMUP 37
#define DROUTER 38
#define DSTDERR 39
#define DSS5 40
#define DISDN 41
#define DMISDN 42
#define DDSS1 43
#define DSIP 44
#define DTEL 45
#define DPOCSAG 14
#define DFRAME 15
#define DCALL 16
#define DCC 17
#define DDB 18
#define DTRANS 19
#define DDMS 20
#define DSMS 21
#define DSDR 22
#define DUHD 23
#define DSOAPY 24
#define DWAVE 25
#define DRADIO 26
#define DAM791X 27
#define DUART 28
#define DDEVICE 29
#define DDATENKLO 30
#define DZEIT 31
#define DSIM1 32
#define DSIM2 33
#define DSIMI 34
#define DSIM7 35
#define DMTP2 36
#define DMTP3 37
#define DMUP 38
#define DROUTER 39
#define DSTDERR 40
#define DSS5 41
#define DISDN 42
#define DMISDN 43
#define DDSS1 44
#define DSIP 45
#define DTEL 46
void get_win_size(int *w, int *h);

@ -0,0 +1,51 @@
AM_CPPFLAGS = -Wall -Wextra -g $(all_includes)
bin_PROGRAMS = \
pocsag
pocsag_SOURCES = \
pocsag.c \
frame.c \
dsp.c \
image.c \
main.c
pocsag_LDADD = \
$(COMMON_LA) \
../anetz/libgermanton.a \
$(top_builddir)/src/liboptions/liboptions.a \
$(top_builddir)/src/libdebug/libdebug.a \
$(top_builddir)/src/libmobile/libmobile.a \
$(top_builddir)/src/libosmocc/libosmocc.a \
$(top_builddir)/src/libdisplay/libdisplay.a \
$(top_builddir)/src/libjitter/libjitter.a \
$(top_builddir)/src/libtimer/libtimer.a \
$(top_builddir)/src/libsamplerate/libsamplerate.a \
$(top_builddir)/src/libemphasis/libemphasis.a \
$(top_builddir)/src/libfm/libfm.a \
$(top_builddir)/src/libfilter/libfilter.a \
$(top_builddir)/src/libwave/libwave.a \
$(top_builddir)/src/libsample/libsample.a \
$(top_builddir)/src/libg711/libg711.a \
$(top_builddir)/src/libaaimage/libaaimage.a \
-lm
if HAVE_ALSA
pocsag_LDADD += \
$(top_builddir)/src/libsound/libsound.a \
$(ALSA_LIBS)
endif
if HAVE_SDR
pocsag_LDADD += \
$(top_builddir)/src/libsdr/libsdr.a \
$(top_builddir)/src/libam/libam.a \
$(top_builddir)/src/libfft/libfft.a \
$(UHD_LIBS) \
$(SOAPY_LIBS)
endif
if HAVE_ALSA
AM_CPPFLAGS += -DHAVE_ALSA
endif

@ -0,0 +1,288 @@
/* POCSAG signal processing
*
* (C) 2019 by Andreas Eversberg <jolly@eversberg.eu>
* All Rights Reserved
*
* 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 3 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/>.
*/
#define CHAN pocsag->sender.kanal
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include "../libsample/sample.h"
#include "../libdebug/debug.h"
#include "pocsag.h"
#include "frame.h"
#include "dsp.h"
#define CODEWORD_SYNC 0x7cd215d8
#define MAX_DISPLAY 1.4 /* something above speech level, no emphasis */
static void dsp_init_ramp(pocsag_t *pocsag)
{
double c;
int i;
PDEBUG_CHAN(DDSP, DEBUG_DEBUG, "Generating cosine shaped ramp table.\n");
for (i = 0; i < 256; i++) {
/* This is mathematically incorrect... */
if (i < 64)
c = 1.0;
else if (i >= 192)
c = -1.0;
else
c = cos((double)(i - 64) / 128.0 * M_PI);
pocsag->fsk_ramp_down[i] = c * pocsag->fsk_deviation * pocsag->fsk_polarity;
pocsag->fsk_ramp_up[i] = -pocsag->fsk_ramp_down[i];
}
}
/* Init transceiver instance. */
int dsp_init_sender(pocsag_t *pocsag, int samplerate, int baudrate, double deviation, double polarity)
{
int rc;
PDEBUG_CHAN(DDSP, DEBUG_DEBUG, "Init DSP for transceiver.\n");
/* set modulation parameters */
// NOTE: baudrate equals modulation, because we have a raised cosine ramp of beta = 0.5
sender_set_fm(&pocsag->sender, deviation, baudrate, deviation, MAX_DISPLAY);
pocsag->fsk_bitduration = (double)samplerate / (double)baudrate;
pocsag->fsk_bitstep = 1.0 / pocsag->fsk_bitduration;
PDEBUG_CHAN(DDSP, DEBUG_DEBUG, "Use %.4f samples for one bit duration @ %d.\n", pocsag->fsk_bitduration, pocsag->sender.samplerate);
pocsag->fsk_tx_buffer_size = pocsag->fsk_bitduration * 32.0 + 10; /* 32 bit, add some extra to prevent short buffer due to rounding */
pocsag->fsk_tx_buffer = calloc(sizeof(sample_t), pocsag->fsk_tx_buffer_size);
if (!pocsag->fsk_tx_buffer) {
PDEBUG_CHAN(DDSP, DEBUG_ERROR, "No memory!\n");
rc = -ENOMEM;
goto error;
}
/* create deviation and ramp */
pocsag->fsk_deviation = 1.0; // equals what we st at sender_set_fm()
pocsag->fsk_polarity = polarity;
dsp_init_ramp(pocsag);
return 0;
error:
dsp_cleanup_sender(pocsag);
return -rc;
}
/* Cleanup transceiver instance. */
void dsp_cleanup_sender(pocsag_t *pocsag)
{
PDEBUG_CHAN(DDSP, DEBUG_DEBUG, "Cleanup DSP for transceiver.\n");
if (pocsag->fsk_tx_buffer) {
free(pocsag->fsk_tx_buffer);
pocsag->fsk_tx_buffer = NULL;
}
}
/* encode one codeward into samples
* input: 32 data bits
* output: samples
* return number of samples */
static int fsk_block_encode(pocsag_t *pocsag, uint32_t word)
{
/* alloc samples, add 1 in case there is a rest */
sample_t *spl;
double phase, bitstep, devpol;
int i, count;
uint8_t lastbit;
devpol = pocsag->fsk_deviation * pocsag->fsk_polarity;
spl = pocsag->fsk_tx_buffer;
phase = pocsag->fsk_tx_phase;
lastbit = pocsag->fsk_tx_lastbit;
bitstep = pocsag->fsk_bitstep * 256.0;
/* add 32 bits */
for (i = 0; i < 32; i++) {
if (lastbit) {
if ((word & 0x80000000)) {
/* stay up */
do {
*spl++ = devpol;
phase += bitstep;
} while (phase < 256.0);
phase -= 256.0;
} else {
/* ramp down */
do {
*spl++ = pocsag->fsk_ramp_down[(uint8_t)phase];
phase += bitstep;
} while (phase < 256.0);
phase -= 256.0;
lastbit = 0;
}
} else {
if ((word & 0x80000000)) {
/* ramp up */
do {
*spl++ = pocsag->fsk_ramp_up[(uint8_t)phase];
phase += bitstep;
} while (phase < 256.0);
phase -= 256.0;
lastbit = 1;
} else {
/* stay down */
do {
*spl++ = -devpol;
phase += bitstep;
} while (phase < 256.0);
phase -= 256.0;
}
}
word <<= 1;
}
/* depending on the number of samples, return the number */
count = ((uintptr_t)spl - (uintptr_t)pocsag->fsk_tx_buffer) / sizeof(*spl);
pocsag->fsk_tx_phase = phase;
pocsag->fsk_tx_lastbit = lastbit;
return count;
}
static void fsk_block_decode(pocsag_t *pocsag, uint8_t bit)
{
if (!pocsag->fsk_rx_sync) {
pocsag->fsk_rx_word = (pocsag->fsk_rx_word << 1) | bit;
if (pocsag->fsk_rx_word == CODEWORD_SYNC) {
put_codeword(pocsag, pocsag->fsk_rx_word, -1, -1);
pocsag->fsk_rx_sync = 16;
pocsag->fsk_rx_index = 0;
} else
if (pocsag->fsk_rx_word == (uint32_t)(~CODEWORD_SYNC))
PDEBUG_CHAN(DDSP, DEBUG_NOTICE, "Received inverted sync, caused by wrong polarity or by radio noise. Verify correct polarity!\n");
} else {
pocsag->fsk_rx_word = (pocsag->fsk_rx_word << 1) | bit;
if (++pocsag->fsk_rx_index == 32) {
pocsag->fsk_rx_index = 0;
put_codeword(pocsag, pocsag->fsk_rx_word, (16 - pocsag->fsk_rx_sync) >> 1, pocsag->fsk_rx_sync & 1);
--pocsag->fsk_rx_sync;
}
}
}
static void fsk_decode(pocsag_t *pocsag, sample_t *spl, int length)
{
double phase, bitstep, polarity;
int i;
uint8_t lastbit;
polarity = pocsag->fsk_polarity;
phase = pocsag->fsk_rx_phase;
lastbit = pocsag->fsk_rx_lastbit;
bitstep = pocsag->fsk_bitstep;
for (i = 0; i < length; i++) {
if (*spl++ * polarity > 0.0) {
if (lastbit) {
/* stay up */
phase += bitstep;
if (phase >= 1.0) {
phase -= 1.0;
fsk_block_decode(pocsag, 1);
}
} else {
/* ramp up */
phase = -0.5;
fsk_block_decode(pocsag, 1);
lastbit = 1;
}
} else {
if (lastbit) {
/* ramp down */
phase = -0.5;
fsk_block_decode(pocsag, 0);
lastbit = 0;
} else {
/* stay down */
phase += bitstep;
if (phase >= 1.0) {
phase -= 1.0;
fsk_block_decode(pocsag, 0);
}
}
}
}
pocsag->fsk_rx_phase = phase;
pocsag->fsk_rx_lastbit = lastbit;
}
/* Process received audio stream from radio unit. */
void sender_receive(sender_t *sender, sample_t *samples, int length, double __attribute__((unused)) rf_level_db)
{
pocsag_t *pocsag = (pocsag_t *) sender;
if (pocsag->rx)
fsk_decode(pocsag, samples, length);
}
/* Provide stream of audio toward radio unit */
void sender_send(sender_t *sender, sample_t *samples, uint8_t *power, int length)
{
pocsag_t *pocsag = (pocsag_t *) sender;
again:
/* get word */
if (!pocsag->fsk_tx_buffer_length) {
int64_t word = get_codeword(pocsag);
/* no message, power is off */
if (word < 0) {
memset(samples, 0, sizeof(samples) * length);
memset(power, 0, length);
return;
}
/* encode */
pocsag->fsk_tx_buffer_length = fsk_block_encode(pocsag, word);
pocsag->fsk_tx_buffer_pos = 0;
}
/* send encoded word until end of source or destination buffer is reaced */
while (length) {
*power++ = 1;
*samples++ = pocsag->fsk_tx_buffer[pocsag->fsk_tx_buffer_pos++];
length--;
if (pocsag->fsk_tx_buffer_pos == pocsag->fsk_tx_buffer_length) {
pocsag->fsk_tx_buffer_length = 0;
break;
}
}
/* do again, if destination buffer is not yet full */
if (length)
goto again;
}

@ -0,0 +1,4 @@
int dsp_init_sender(pocsag_t *pocsag, int samplerate, int baudrate, double deviation, double polarity);
void dsp_cleanup_sender(pocsag_t *pocsag);

@ -0,0 +1,494 @@
/* POCSAG framing
*
* (C) 2021 by Andreas Eversberg <jolly@eversberg.eu>
* All Rights Reserved
*
* 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 3 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/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "../libsample/sample.h"
#include "../libdebug/debug.h"
#include "pocsag.h"
#include "frame.h"
#define CHAN pocsag->sender.kanal
#define PREAMBLE_COUNT 18
#define CODEWORD_PREAMBLE 0xaaaaaaaa
#define CODEWORD_SYNC 0x7cd215d8
#define CODEWORD_IDLE 0x7a89c197
#define IDLE_BATCHES 2
static const char numeric[16] = "0123456789RU -][";
static const char hex[16] = "0123456789abcdef";
static const char *ctrlchar[32] = {
"<NUL>",
"<SOH>",
"<STX>",
"<ETX>",
"<EOT>",
"<ENQ>",
"<ACK>",
"<BEL>",
"<BS>",
"<HT>",
"<LF>",
"<VT>",
"<FF>",
"<CR>",
"<SO>",
"<SI>",
"<DLE>",
"<DC1>",
"<DC2",
"<DC3>",
"<DC4>",
"<NAK>",
"<SYN>",
"<ETB>",
"<CAN>",
"<EM>",
"<SUB>",
"<ESC>",
"<FS>",
"<GS>",
"<RS>",
"<US>",
};
static uint32_t pocsag_crc(uint32_t word)
{
uint32_t denominator = 0x76900000;
int i;
word <<= 10;
for (i = 0; i < 21; i++) {
if ((word >> (30 - i)) & 1)
word ^= denominator;
denominator >>= 1;
}
return word & 0x3ff;
}
static uint32_t pocsag_parity(uint32_t word)
{
word ^= word >> 16;
word ^= word >> 8;
word ^= word >> 4;
word ^= word >> 2;
word ^= word >> 1;
return word & 1;
}
static int debug_word(uint32_t word, int slot)
{
if (pocsag_crc(word >> 11) != ((word >> 1) & 0x3ff)) {
PDEBUG(DPOCSAG, DEBUG_NOTICE, "CRC error in codeword 0x%08x.\n", word);
return -EINVAL;
}
if (pocsag_parity(word)) {
PDEBUG(DPOCSAG, DEBUG_NOTICE, "Parity error in codeword 0x%08x.\n", word);
return -EINVAL;
}
if (word == CODEWORD_SYNC) {
PDEBUG(DPOCSAG, DEBUG_DEBUG, "-> valid sync word\n");
return 0;
}
if (word == CODEWORD_IDLE) {
PDEBUG(DPOCSAG, DEBUG_DEBUG, "-> valid idle word\n");
return 0;
}
if (!(word & 0x80000000)) {
PDEBUG(DPOCSAG, DEBUG_DEBUG, "-> valid address word: RIC = '%d', function = '%d' (%s)\n", ((word >> 10) & 0x1ffff8) + slot, (word >> 11) & 0x3, pocsag_function_name[(word >> 11) & 0x3]);
} else {
PDEBUG(DPOCSAG, DEBUG_DEBUG, "-> valid message word: message = '0x%05x'\n", (word >> 11) & 0xfffff);
}
return 0;
}
static uint32_t encode_address(pocsag_msg_t *msg)
{
uint32_t word;
/* compose message */
word = 0x0;
/* RIC */
word = (word << 18) | (msg->ric >> 3);
word = (word << 2) | msg->function;
word = (word << 10) | pocsag_crc(word);
word = (word << 1) | pocsag_parity(word);
return word;
}
static void decode_address(uint32_t word, uint8_t slot, uint32_t *ric, enum pocsag_function *function)
{
*ric = ((word >> 10) & 0x1ffff8) + slot;
*function = (word >> 11) & 0x3;
}
static uint32_t encode_numeric(pocsag_msg_t *msg)
{
uint8_t digit[5] = { 0xc, 0xc, 0xc, 0xc, 0xc };
int index, i;
uint32_t word;
/* get characters from string */
index = 0;
while (msg->data_index < msg->data_length) {
for (i = 0; i < 16; i++) {
if (numeric[i] == msg->data[msg->data_index])
break;
}
msg->data_index++;
if (i < 16)
digit[index++] = i;
if (index == 5)
break;
}
/* compose message */
word = 0x1;
for (i = 0; i < 5; i++) {
word = (word << 1) | (digit[i] & 0x1);
word = (word << 1) | ((digit[i] >> 1) & 0x1);
word = (word << 1) | ((digit[i] >> 2) & 0x1);
word = (word << 1) | ((digit[i] >> 3) & 0x1);
}
word = (word << 10) | pocsag_crc(word);
word = (word << 1) | pocsag_parity(word);
return word;
}
static void decode_numeric(pocsag_t *pocsag, uint32_t word)
{
uint8_t digit;
int i;
for (i = 0; i < 5; i++) {
if (pocsag->rx_msg_data_length == sizeof(pocsag->rx_msg_data))
return;
digit = (word >> (27 - i * 4)) & 0x1;
digit = (digit << 1) | ((word >> (28 - i * 4)) & 0x1);
digit = (digit << 1) | ((word >> (29 - i * 4)) & 0x1);
digit = (digit << 1) | ((word >> (30 - i * 4)) & 0x1);
pocsag->rx_msg_data[pocsag->rx_msg_data_length++] = numeric[digit];
}
}
static uint32_t encode_alpha(pocsag_msg_t *msg)
{
int bits;
uint32_t word;
/* compose message */
word = 0x1;
bits = 0;
/* get character from string */
while (msg->data_index < msg->data_length) {
if ((msg->data[msg->data_index] & 0x80)) {
msg->data_index++;
continue;
}
while (42) {
word = (word << 1) | ((msg->data[msg->data_index] >> msg->bit_index) & 1);
bits++;
if (++msg->bit_index == 7) {
msg->bit_index = 0;
msg->data_index++;
break;
}
if (bits == 20)
break;
}
if (bits == 20)
break;
}
/* fill remaining digit space with 0x04 (EOT) */
while (bits <= 13) {
word = (word << 7) | 0x10;
bits += 7;
}
/* fill remaining bits with '0's */
if (bits < 20)
word <<= 20 - bits;
word = (word << 10) | pocsag_crc(word);
word = (word << 1) | pocsag_parity(word);
return word;
}
static void decode_alpha(pocsag_t *pocsag, uint32_t word)
{
int i;
for (i = 0; i < 20; i++) {
if (pocsag->rx_msg_data_length == sizeof(pocsag->rx_msg_data))
return;
if (!pocsag->rx_msg_bit_index)
pocsag->rx_msg_data[pocsag->rx_msg_data_length] = 0x00;
pocsag->rx_msg_data[pocsag->rx_msg_data_length] >>= 1;
pocsag->rx_msg_data[pocsag->rx_msg_data_length] |= ((word >> (30 - i)) & 0x1) << 6;
if (++pocsag->rx_msg_bit_index == 7) {
pocsag->rx_msg_bit_index = 0;
pocsag->rx_msg_data_length++;
}
}
}
static void decode_hex(pocsag_t *pocsag, uint32_t word)
{
uint8_t digit;
int i;
for (i = 0; i < 5; i++) {
if (pocsag->rx_msg_data_length == sizeof(pocsag->rx_msg_data))
return;
digit = (word >> (27 - i * 4)) & 0x1;
digit = (digit << 1) | ((word >> (28 - i * 4)) & 0x1);
digit = (digit << 1) | ((word >> (29 - i * 4)) & 0x1);
digit = (digit << 1) | ((word >> (30 - i * 4)) & 0x1);
pocsag->rx_msg_data[pocsag->rx_msg_data_length++] = hex[digit];
}
}
/* get codeword from scheduler */
int64_t get_codeword(pocsag_t *pocsag)
{
pocsag_msg_t *msg;
uint32_t word = 0; // make GCC happy
uint8_t slot = (pocsag->word_count - 1) >> 1;
uint8_t subslot = (pocsag->word_count - 1) & 1;
/* no codeword, if not transmitting */
if (!pocsag->tx)
return -1;
/* transmitter state */
switch (pocsag->state) {
case POCSAG_IDLE:
return -1;
case POCSAG_PREAMBLE:
if (!pocsag->word_count)
PDEBUG_CHAN(DPOCSAG, DEBUG_INFO, "Sending preamble.\n");
/* transmit preamble */
PDEBUG_CHAN(DPOCSAG, DEBUG_DEBUG, "Sending 32 bits of preamble pattern 0x%08x.\n", CODEWORD_PREAMBLE);
if (++pocsag->word_count == PREAMBLE_COUNT) {
pocsag_new_state(pocsag, POCSAG_MESSAGE);
pocsag->word_count = 0;
pocsag->idle_count = 0;
}
word = CODEWORD_PREAMBLE;
break;
case POCSAG_MESSAGE:
if (!pocsag->word_count)
PDEBUG_CHAN(DPOCSAG, DEBUG_INFO, "Sending batch.\n");
/* send sync */
if (pocsag->word_count == 0) {
PDEBUG_CHAN(DPOCSAG, DEBUG_DEBUG, "Sending 32 bits of sync pattern 0x%08x.\n", CODEWORD_SYNC);
/* count codewords */
++pocsag->word_count;
word = CODEWORD_SYNC;
break;
}
/* send message data, if there is an ongoing message */
if ((msg = pocsag->current_msg)) {
/* reset idle counter */
pocsag->idle_count = 0;
/* encode data */
switch (msg->function) {
case POCSAG_FUNCTION_NUMERIC:
word = encode_numeric(msg);
break;
case POCSAG_FUNCTION_ALPHA:
word = encode_alpha(msg);
break;
default:
word = CODEWORD_IDLE; /* should never happen */
}
/* if message is complete, reset index. if message is not to be repeated, remove message */
if (msg->data_index == msg->data_length) {
pocsag->current_msg = NULL;
msg->data_index = 0;
if (msg->repeat || pocsag->sender.loopback)
msg->repeat--;
else
pocsag_msg_destroy(msg);
}
/* prevent 'use-after-free' from this point on */
msg = NULL;
PDEBUG_CHAN(DPOCSAG, DEBUG_DEBUG, "Sending 32 bits of message codeword 0x%08x (frame %d.%d).\n", word, slot, subslot);
/* count codewords */
if (++pocsag->word_count == 17)
pocsag->word_count = 0;
break;
}
/* if we are about to send an address codeword, we search for a pending message */
for (msg = pocsag->msg_list; msg; msg = msg->next) {
/* if a message matches the right time slot */
if ((msg->ric & 7) == slot)
break;
}
if (msg) {
PDEBUG_CHAN(DPOCSAG, DEBUG_INFO, "Sending message to RIC '%d' / function '%d' (%s)\n", msg->ric, msg->function, pocsag_function_name[msg->function]);
/* reset idle counter */
pocsag->idle_count = 0;
/* encode address */
word = encode_address(msg);
/* link message, if there is data to be sent */
if ((msg->function == POCSAG_FUNCTION_NUMERIC || msg->function == POCSAG_FUNCTION_ALPHA) && msg->data_length) {
char text[msg->data_length + 1];
memcpy(text, msg->data, msg->data_length);
text[msg->data_length] = '\0';
PDEBUG_CHAN(DPOCSAG, DEBUG_INFO, " -> Message text is \"%s\".\n", text);
pocsag->current_msg = msg;
msg->data_index = 0;
msg->bit_index = 0;
} else {
/* if message is not to be repeated, remove message */
if (msg->repeat || pocsag->sender.loopback)
msg->repeat--;
else
pocsag_msg_destroy(msg);
/* prevent 'use-after-free' from this point on */
msg = NULL;
}
PDEBUG_CHAN(DPOCSAG, DEBUG_DEBUG, "Sending 32 bits of address codeword 0x%08x (frame %d.%d).\n", word, slot, subslot);
/* count codewords */
if (++pocsag->word_count == 17)
pocsag->word_count = 0;
break;
}
/* no message, so we send idle pattern */
PDEBUG_CHAN(DPOCSAG, DEBUG_DEBUG, "Sending 32 bits of idle pattern 0x%08x (frame %d.%d).\n", CODEWORD_IDLE, slot, subslot);
/* count codewords */
if (++pocsag->word_count == 17) {
pocsag->word_count = 0;
/* if no message has been scheduled during transmission and idle counter is reached, stop transmitter */
if (!pocsag->msg_list && pocsag->idle_count++ == IDLE_BATCHES) {
PDEBUG_CHAN(DPOCSAG, DEBUG_INFO, "Transmission done.\n");
PDEBUG_CHAN(DPOCSAG, DEBUG_DEBUG, "Reached %d of idle batches, turning transmitter off.\n", IDLE_BATCHES);
pocsag_new_state(pocsag, POCSAG_IDLE);
}
}
word = CODEWORD_IDLE;
break;
}
if (word != CODEWORD_PREAMBLE)
debug_word(word, slot);
return word;
}
static void done_rx_msg(pocsag_t *pocsag)
{
if (!pocsag->rx_msg_valid)
return;
pocsag->rx_msg_valid = 0;
PDEBUG_CHAN(DPOCSAG, DEBUG_INFO, "Received message from RIC '%d' / function '%d' (%s)\n", pocsag->rx_msg_ric, pocsag->rx_msg_function, pocsag_function_name[pocsag->rx_msg_function]);
{
char text[pocsag->rx_msg_data_length * 5 + 1];
int i, j;
for (i = 0, j = 0; i < pocsag->rx_msg_data_length; i++) {
if (pocsag->rx_msg_data[i] == 127) {
strcpy(text + j, "<DEL>");
j += strlen(text + j);
} else
if (pocsag->rx_msg_data[i] < 32) {
strcpy(text + j, ctrlchar[(int)pocsag->rx_msg_data[i]]);
j += strlen(text + j);
} else
text[j++] = pocsag->rx_msg_data[i];
}
text[j] = '\0';
PDEBUG_CHAN(DPOCSAG, DEBUG_INFO, " -> Message text is \"%s\".\n", text);
pocsag_msg_receive(pocsag->language, pocsag->sender.kanal, pocsag->rx_msg_ric, pocsag->rx_msg_function, text);
}
}
void put_codeword(pocsag_t *pocsag, uint32_t word, int8_t slot, int8_t subslot)
{
int rc;
if (slot < 0 && word == CODEWORD_SYNC) {
PDEBUG_CHAN(DPOCSAG, DEBUG_DEBUG, "Received 32 bits of sync pattern 0x%08x.\n", CODEWORD_SYNC);
return;
}
if (word == CODEWORD_IDLE) {
PDEBUG_CHAN(DPOCSAG, DEBUG_DEBUG, "Received 32 bits of idle pattern 0x%08x.\n", CODEWORD_IDLE);
} else
if (!(word & 0x80000000))
PDEBUG_CHAN(DPOCSAG, DEBUG_DEBUG, "Received 32 bits of address codeword 0x%08x (frame %d.%d).\n", word, slot, subslot);
else
PDEBUG_CHAN(DPOCSAG, DEBUG_DEBUG, "Received 32 bits of message codeword 0x%08x (frame %d.%d).\n", word, slot, subslot);
rc = debug_word(word, slot);
if (rc < 0) {
done_rx_msg(pocsag);
return;
}
if (word == CODEWORD_IDLE) {
done_rx_msg(pocsag);
return;
}
if (!(word & 0x80000000)) {
done_rx_msg(pocsag);
pocsag->rx_msg_valid = 1;
decode_address(word, slot, &pocsag->rx_msg_ric, &pocsag->rx_msg_function);
pocsag->rx_msg_data_length = 0;
pocsag->rx_msg_bit_index = 0;
} else {
if (!pocsag->rx_msg_valid)
return;
switch (pocsag->rx_msg_function) {
case POCSAG_FUNCTION_NUMERIC:
decode_numeric(pocsag, word);
break;
case POCSAG_FUNCTION_ALPHA:
decode_alpha(pocsag, word);
break;
default:
decode_hex(pocsag, word);
;
}
}
}

@ -0,0 +1,4 @@
int64_t get_codeword(pocsag_t *pocsag);
void put_codeword(pocsag_t *pocsag, uint32_t word, int8_t slot, int8_t subslot);

@ -0,0 +1,6 @@
#include <stdio.h>
const char *aaimage[] = {
NULL
};

@ -0,0 +1,355 @@
/* POCSAG (Radio-Paging Code #1) main
*
* (C) 2021 by Andreas Eversberg <jolly@eversberg.eu>
* All Rights Reserved
*
* 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 3 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/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "../libsample/sample.h"
#include "../libdebug/debug.h"
#include "../libmobile/call.h"
#include "../libmobile/main_mobile.h"
#include "../liboptions/options.h"
#include "../libfm/fm.h"
#include "../anetz/besetztton.h"
#include "pocsag.h"
#include "dsp.h"
#define MSG_SEND "/tmp/pocsag_msg_send"
#define MSG_RECEIVED "/tmp/pocsag_msg_received"
static int msg_send_fd = -1;
static int tx = 0; /* we transmit */
static int rx = 0; /* we receive */
static int baudrate = 1200;
static int baudrate_given = 0;
static double deviation = 4500;
static int deviation_given = 0;
static double polarity = -1;
static int polarity_given = 0;
static enum pocsag_function function = POCSAG_FUNCTION_NUMERIC;
static const char *message = "1234";
static enum pocsag_language language = LANGUAGE_DEFAULT;
static uint32_t scan_from = 0;
static uint32_t scan_to = 0;
void print_help(const char *arg0)
{
main_mobile_print_help(arg0, "-k 466.230 | -k list");
/* - - */
printf(" -T --tx\n");
printf(" Transmit Eurosignal on given channel, to page a receiver. (default)\n");
printf(" -R --rx\n");
printf(" Receive Eurosignal on given channel, so we are the receiver.\n");
printf(" If none of the options -T nor -R is given, only transmitter is enabled.\n");
printf(" -B --baud-rate 512 | 1200 | 2400\n");
printf(" Choose baud rate of transmitter.\n");
printf(" -D --deviation wide | 4.5 | narrow | 2.5 | <other KHz>\n");
printf(" Choose deviation of FFSK signal (default %.0f KHz).\n", deviation / 1000.0);
printf(" -P --polarity -1 | nagative | 1 | positive\n");
printf(" Choose polarity of FFSK signal. 'negative' means that a binary 0 uses\n");
printf(" positive and a binary 1 negative deviation. (default %s KHz).\n", (polarity < 0) ? "negative" : "positive");
printf(" -F --function 0..3 | A..D | numeric | beep1 | beep2 | alphanumeric\n");
printf(" Choose default function when 7 digit only number is dialed.\n");
printf(" (default %d = %s)\n", function, pocsag_function_name[function]);
printf(" -M --message \"...\"\n");
printf(" Send this message, if no caller ID was given or of built-in console\n");
printf(" is used. (default \"%s\").\n", message);
printf(" -L --language\n");
printf(" Translate German spcial characters from/to UTF-8.\n");
printf(" -S --scan <from> <to>\n");
printf(" Scan through given IDs once (no repetition). This can be useful to find\n");
printf(" the RIC of a vintage receiver. Note that scanning all RICs from 0\n");
printf(" through 2097151 would take about 16.5 Hours at 1200 Baud and known sub\n");
printf(" RIC.\n");
printf("\n");
printf("File: %s\n", MSG_SEND);
printf(" Write \"<ric>,0,message\" to it to send a numerical message.\n");
printf(" Write \"<ric>,3,message\" to it to send an alphanumerical message.\n");
printf("File: %s\n", MSG_RECEIVED);
printf(" Read from it to see received messages.\n");
main_mobile_print_station_id();
main_mobile_print_hotkeys();
}
static void add_options(void)
{
main_mobile_add_options();
option_add('T', "tx", 0);
option_add('R', "rx", 0);
option_add('B', "baud-rate", 1);
option_add('D', "deviation", 1);
option_add('F', "function", 1);
option_add('P', "polarity", 1);
option_add('M', "message", 1);
option_add('L', "language", 0);
option_add('S', "scan", 2);
}
static int handle_options(int short_option, int argi, char **argv)
{
int rc;
switch (short_option) {
case 'T':
tx = 1;
break;
case 'R':
rx = 1;
break;
case 'B':
baudrate = atoi(argv[argi]);
if (baudrate != 512 && baudrate != 1200 && baudrate != 2400) {
fprintf(stderr, "Given baud-rate is not 512, 1200 nor 2400, use '-h' for help.\n");
return -EINVAL;
}
baudrate_given = 1;
break;
case 'D':
if (argv[argi][0] == 'n' || argv[argi][0] == 'N')
deviation = 2500.0;
else if (argv[argi][0] == 'w' || argv[argi][0] == 'W')
deviation = 4500.0;
else
deviation = atof(argv[argi]) * 1000.0;
if (deviation < 1000.0) {
fprintf(stderr, "Given deviation is too low, use higher deviation.\n");
return -EINVAL;
}
if (deviation > 10000.0) {
fprintf(stderr, "Given deviation is too high, use lower deviation.\n");
return -EINVAL;
}
deviation_given = 1;
break;
case 'P':
if (argv[argi][0] == 'n' || argv[argi][0] == 'N')
polarity = -1.0;
else if (argv[argi][0] == 'p' || argv[argi][0] == 'P')
polarity = 1.0;
else if (atoi(argv[argi]) == -1)
polarity = -1.0;
else if (atoi(argv[argi]) == 1)
polarity = 1.0;
else {
fprintf(stderr, "Given polarity is not positive nor negative, use '-h' for help.\n");
return -EINVAL;
}
polarity_given = 1;
break;
case 'F':
rc = pocsag_function_name2value(argv[argi]);
if (rc < 0) {
fprintf(stderr, "Given function is invalid, use '-h' for help.\n");
return rc;
}
function = rc;
break;
case 'M':
message = options_strdup(argv[argi++]);
break;
case 'L':
language = LANGUAGE_GERMAN;
break;
case 'S':
scan_from = atoi(argv[argi++]);
if (scan_from > 2097151) {
fprintf(stderr, "Given RIC to scan from is out of range!\n");
return -EINVAL;
}
scan_to = atoi(argv[argi++]) + 1;
if (scan_to > 2097151) {
fprintf(stderr, "Given RIC to scan to is out of range!\n");
return -EINVAL;
}
break;
default:
return main_mobile_handle_options(short_option, argi, argv);
}
return 1;
}
static void myhandler(void)
{
static char buffer[256];
static int pos = 0, rc, i;
int space = sizeof(buffer) - pos;
rc = read(msg_send_fd, buffer + pos, space);
if (rc > 0) {
pos += rc;
if (pos == space) {
fprintf(stderr, "Message buffer overflow!\n");
pos = 0;
}
/* check for end of line */
for (i = 0; i < pos; i++) {
if (buffer[i] == '\r' || buffer[i] == '\n')
break;
}
/* send msg */
if (i < pos) {
buffer[i] = '\0';
pos = 0;
if (tx)
pocsag_msg_send(language, buffer);
else
PDEBUG(DPOCSAG, DEBUG_ERROR, "Failed to send message, transmitter is not enabled!\n");
}
}
}
int msg_receive(const char *text)
{
FILE *fp;
fp = fopen(MSG_RECEIVED, "a");
if (!fp) {
fprintf(stderr, "Failed to open MSG receive file '%s'!\n", MSG_RECEIVED);
return -1;
}
fprintf(fp, "%s\n", text);
fclose(fp);
return 0;
}
static const struct number_lengths number_lengths[] = {
{ 7, "RIC with default function" },
{ 8, "RIC with function (append 0..3)" },
{ 0, NULL }
};
int main(int argc, char *argv[])
{
int rc, argi;
const char *station_id = "";
int i;
double frequency;
/* init common tones */
init_besetzton();
/* init mobile interface */
main_mobile_init("0123456789", number_lengths, NULL, pocsag_number_valid);
/* handle options / config file */
add_options();
rc = options_config_file(argc, argv, "~/.osmocom/analog/pocsag.conf", handle_options);
if (rc < 0)
return 0;
argi = options_command_line(argc, argv, handle_options);
if (argi <= 0)
return argi;
if (argi < argc) {
station_id = argv[argi];
rc = main_mobile_number_ask(station_id, "station ID (RIC)");
if (rc)
return rc;
}
if (!num_kanal) {
printf("No channel is specified, Use '-k list' to get a list of all channels.\n\n");
print_help(argv[0]);
return 0;
}
if (!strcasecmp(kanal[0], "list")) {
pocsag_list_channels();
goto fail;
}
if (use_sdr) {
/* set device */
for (i = 0; i < num_kanal; i++)
dsp_device[i] = "sdr";
num_device = num_kanal;
}
if (num_kanal == 1 && num_device == 0)
num_device = 1; /* use default */
if (num_kanal != num_device) {
fprintf(stderr, "You need to specify as many sound devices as you have channels.\n");
exit(0);
}
/* TX is default */
if (!tx && !rx)
tx = 1;
/* create pipe for message sendy */
unlink(MSG_SEND);
rc = mkfifo(MSG_SEND, 0666);
if (rc < 0) {
fprintf(stderr, "Failed to create mwaaage send FIFO '%s'!\n", MSG_SEND);
goto fail;
} else {
msg_send_fd = open(MSG_SEND, O_RDONLY | O_NONBLOCK);
if (msg_send_fd < 0) {
fprintf(stderr, "Failed to open mwaaage send FIFO! '%s'\n", MSG_SEND);
goto fail;
}
}
/* inits */
fm_init(fast_math);
pocsag_init();
/* create transceiver instance */
for (i = 0; i < num_kanal; i++) {
frequency = pocsag_channel2freq(kanal[i], (deviation_given) ? NULL : &deviation, (polarity_given) ? NULL : &polarity, (baudrate_given) ? NULL : &baudrate);
if (frequency == 0.0) {
printf("Invalid channel '%s', Use '-k list' to get a list of all channels.\n\n", kanal[i]);
goto fail;
}
rc = pocsag_create(kanal[i], frequency, dsp_device[i], use_sdr, dsp_samplerate, rx_gain, tx_gain, tx, rx, language, baudrate, deviation, polarity, function, message, scan_from, scan_to, write_rx_wave, write_tx_wave, read_rx_wave, read_tx_wave, loopback);
if (rc < 0) {
fprintf(stderr, "Failed to create \"Sender\" instance. Quitting!\n");
goto fail;
}
printf("Base station ready, please tune transmitter (or receiver) to %.4f MHz\n", frequency / 1e6);
}
main_mobile_loop("pocsag", &quit, myhandler, station_id);
fail:
/* pipe */
if (msg_send_fd > 0)
close(msg_send_fd);
unlink(MSG_SEND);
/* destroy transceiver instance */
while(sender_head)
pocsag_destroy(sender_head);
/* exits */
fm_exit();
pocsag_exit();
options_free();
return 0;
}

<
@ -0,0 +1,537 @@
/* POCSAG (Radio-Paging Code #1) processing
*
* (C) 2021 by Andreas Eversberg <jolly@eversberg.eu>
* All Rights Reserved