You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.8 KiB
81 lines
2.8 KiB
/* SPDX-License-Identifier: MIT |
|
* Copyright (c) 2026 Dan Chokola, KD2HSG */ |
|
/* Build one frame and print every intermediate, for comparison with the |
|
* format-v1 test vectors (tools/check_c.py drives it). |
|
* |
|
* cm_txvec SF BW COHERENT PROFILE ENCRYPTED SRC DST BODYHEX OUT.cf32 |
|
* (SRC/DST "-" = none) |
|
*/ |
|
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
|
#include "cm.h" |
|
|
|
static int hexval(char c) |
|
{ |
|
return c >= '0' && c <= '9' ? c - '0' : (c | 32) - 'a' + 10; |
|
} |
|
|
|
int main(int argc, char **argv) |
|
{ |
|
if (argc != 10) { |
|
fprintf(stderr, "usage: %s SF BW COHERENT PROFILE ENCRYPTED SRC DST BODYHEX OUT.cf32\n", |
|
argv[0]); |
|
return 2; |
|
} |
|
cm_phy p; |
|
cm_phy_init(&p, atoi(argv[1]), atof(argv[2]), 223.58e6); |
|
int coherent = atoi(argv[3]), profile = atoi(argv[4]), enc = atoi(argv[5]); |
|
const char *src = strcmp(argv[6], "-") ? argv[6] : NULL; |
|
const char *dst = strcmp(argv[7], "-") ? argv[7] : NULL; |
|
size_t nh = strlen(argv[8]) / 2; |
|
uint8_t *body = malloc(nh + 1), mac[CM_MAX_FRAME]; |
|
for (size_t i = 0; i < nh; i++) |
|
body[i] = (uint8_t)(hexval(argv[8][2 * i]) * 16 + hexval(argv[8][2 * i + 1])); |
|
int ml = cm_mac_pack(src, dst, body, (int)nh, mac); |
|
if (ml < 0) { |
|
fprintf(stderr, "bad MAC frame\n"); |
|
return 1; |
|
} |
|
printf("mac_frame_hex "); |
|
for (int i = 0; i < ml; i++) |
|
printf("%02x", mac[i]); |
|
cm_header h = {0, profile, coherent ? 0 : 1, 0, ml, enc}; |
|
uint8_t hb[4]; |
|
cm_header_pack(&h, hb); |
|
printf("\nheader_hex %02x%02x%02x%02x\n", hb[0], hb[1], hb[2], hb[3]); |
|
|
|
cm_codec hc; |
|
cm_segcodec pc; |
|
int bps = coherent ? p.sf + 2 : p.sf; |
|
cm_header_codec(&hc, &p); |
|
cm_segcodec_init(&pc, ml, bps); |
|
uint8_t *bits = malloc((size_t)pc.n_sym * bps + (size_t)hc.n_sym * p.sf); |
|
cm_codec_encode(&hc, hb, bits); |
|
printf("n_hdr_sym %d\nn_pay_sym %d\nsegments", hc.n_sym, pc.n_sym); |
|
for (int i = 0; i < pc.n_seg; i++) |
|
printf(" %d", pc.seg[i].n_bytes); |
|
printf("\nheader_bits "); |
|
for (int i = 0; i < hc.n_sym * p.sf; i++) |
|
putchar('0' + bits[i]); |
|
cm_segcodec_encode(&pc, mac, bits); |
|
printf("\npayload_bits "); |
|
for (int i = 0; i < pc.n_sym * bps; i++) |
|
putchar('0' + bits[i]); |
|
|
|
int n = cm_frame_n_data(&p, ml, coherent, NULL, NULL); |
|
int *idx = malloc(sizeof(int) * (size_t)n); |
|
uint8_t *q = malloc((size_t)n); |
|
cm_frame_symbols(&p, mac, ml, profile, enc, coherent, idx, q); |
|
printf("\nsymbols"); |
|
for (int i = 0; i < n; i++) |
|
printf(" %d:%d", idx[i], q[i]); |
|
size_t ns = cm_frame_samples(&p, n); |
|
cf32 *wav = malloc(sizeof(cf32) * ns); |
|
cm_modulate(&p, idx, q, n, wav); |
|
FILE *f = fopen(argv[9], "wb"); |
|
fwrite(wav, sizeof(cf32), ns, f); |
|
fclose(f); |
|
printf("\nn_samples %zu\n", ns); |
|
return 0; |
|
}
|
|
|