changeset 190:62ecc0aa081f

sip-manual-out: add state machine for capturing full IS messages
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 17 Mar 2023 10:56:43 -0800
parents 1266e024de6c
children 6ac96217c442
files sip-manual-out/rtp.c
diffstat 1 files changed, 106 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/sip-manual-out/rtp.c	Fri Mar 17 09:54:22 2023 -0800
+++ b/sip-manual-out/rtp.c	Fri Mar 17 10:56:43 2023 -0800
@@ -31,22 +31,23 @@
 					 0, 1, 1, 0, 1, 0, 1, 0, 0, 1};
 
 static uint8_t is_hunt_buf[320];
+static int is_state;
+static unsigned is_offset, is_bit_count;
+static uint32_t is_rx_word;
 
 static void
 reset_is_hunt()
 {
 	memset(is_hunt_buf, 0xFF, 320);
+	is_state = 0;
 }
 
 static void
-is_hunt_proc(input, input_pos)
-	uint8_t *input;
+is_rx_hunt(input_pos)
 	unsigned input_pos;
 {
 	unsigned offset, n;
 
-	memmove(is_hunt_buf, is_hunt_buf + 16, 304);
-	memcpy(is_hunt_buf + 304, input, 16);
 	for (offset = 0; offset < 16; offset++) {
 		for (n = 0; n < 20; n++)
 			if ((is_hunt_buf[offset + n*16] & 1) != hdr_pattern[n])
@@ -54,9 +55,106 @@
 		if (n == 20)
 			break;
 	}
-	if (n == 20)
-		printf("Found IS_Header, last bit offset %u\n",
-			input_pos * 16 + offset);
+	if (n != 20)
+		return;
+	printf("Found IS_Header, last bit offset %u\n",
+		input_pos * 16 + offset);
+	is_offset = offset;
+	is_state = 1;
+	is_bit_count = 0;
+	is_rx_word = 0;
+}
+
+static void
+is_process_cmd()
+{
+	int cont;
+
+	printf("IS_Command: 0x%03X", is_rx_word);
+	switch (is_rx_word) {
+	case 0x05D:
+		printf(" (REQ)\n");
+		cont = 1;
+		break;
+	case 0x0BA:
+		printf(" (ACK)\n");
+		cont = 1;
+		break;
+	case 0x0E7:
+		printf(" (IPE)\n");
+		cont = 1;
+		break;
+	case 0x129:
+		printf(" (FILL)\n");
+		cont = 0;
+		break;
+	case 0x174:
+		printf(" (DUP)\n");
+		cont = 0;
+		break;
+	case 0x193:
+		printf(" (SYL)\n");
+		cont = 0;
+		break;
+	default:
+		printf(" (bad)\n");
+		cont = 0;
+	}
+	if (cont) {
+		is_state = 2;
+		is_bit_count = 0;
+		is_rx_word = 0;
+	} else
+		is_state = 0;
+}
+
+static void
+is_process_ext()
+{
+	printf("IS_Extension: 0x%05X", is_rx_word);
+	if (is_rx_word & 0x80200) {
+		printf(" (bad sync)\n");
+		is_state = 0;
+		return;
+	}
+	switch (is_rx_word & 3) {
+	case 0:
+		printf(" (final)\n");
+		is_state = 0;
+		return;
+	case 3:
+		printf(" (continue)\n");
+		is_state = 2;
+		is_bit_count = 0;
+		is_rx_word = 0;
+		return;
+	default:
+		printf(" (bad EX)\n");
+		is_state = 0;
+	}
+}
+
+static void
+is_rx_process(input, input_pos)
+	uint8_t *input;
+	unsigned input_pos;
+{
+	unsigned new_bit;
+
+	memmove(is_hunt_buf, is_hunt_buf + 16, 304);
+	memcpy(is_hunt_buf + 304, input, 16);
+	if (!is_state) {
+		is_rx_hunt(input_pos);
+		return;
+	}
+	new_bit = input[is_offset] & 1;
+	is_rx_word <<= 1;
+	is_rx_word |= new_bit;
+	is_bit_count++;
+	if (is_state == 1 && is_bit_count == 10)
+		is_process_cmd();
+	else if (is_state == 2 && is_bit_count == 20)
+		is_process_ext();
 }
 
 void
@@ -152,7 +250,7 @@
 		rtp_start_flag = 1;
 	}
 	for (is_chunk = 0; is_chunk < 10; is_chunk++)
-		is_hunt_proc(pkt.payload + is_chunk * 16, is_chunk);
+		is_rx_process(pkt.payload + is_chunk * 16, is_chunk);
 }
 
 void