# bsap — Port 1234 Bristol Babcock BSAP Water & Pipeline Telemetry (`bsap`)

> **FrameAssembly v3.3.0** · Protocol 123 of 81 · Canonical URL: https://frameassembly-lang.dev/docs/protocols/bsap

- **Full Protocol Name:** Emerson / Bristol Babcock Synchronous Asynchronous Protocol (BSAP/IP)
- **Keyword:** `bsap`
- **OSI Layer & Transport:** Layer 7 · UDP 1234 (or TCP 1234 via transport tcp)
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** Emerson OpenBSI / Bristol Babcock ControlWave BSAP IP Specification
- **Wireshark Display Filter:** `udp.port == 1234 || tcp.port == 1234`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> UDP 1234 (or TCP 1234 via transport tcp) (transport tcp|udp) -> Bristol Babcock BSAP`

## Overview

Municipal water distribution and gas SCADA RTUs using ControlWave / Bristol Babcock controllers run BSAP on port `1234` — `bsap` constructs IP RBE headers plus Remote Database `poll`, `read`, and `write` frames.

Bristol Synchronous Asynchronous Protocol over IP (`bsap`) connects **Emerson ControlWave, Bristol Babcock Network 3000 / DPC 3330 RTUs, and OpenBSI SCADA masters** across municipal water/wastewater distribution and natural-gas SCADA networks on **UDP or TCP port `1234`**. BSAP/IP encapsulates Remote Database Access (`RDB` / `NDB`) and poll messages with 16-bit `Destination Address` (`dst`), 16-bit `Source Address` (`src`), 8-bit `Sequence Number` (`seqnum`), Application `Function Code` (`poll`, `read` RDB Read, `write` RDB Write), starting signal/list `addr`, `count`, and 32-bit `value` payloads.

FrameAssembly encodes BSAP/IP headers and RDB payloads cleanly over default UDP `1234` or stateful TCP `1234` (`transport tcp`).

## Minimal Quickstart (`bsap_quickstart.frasm`)

```frameassembly
host openbsi_srv { ip 10.123.1.10 }
host cw_rtu_10   { ip 10.123.4.10 }

compile {
    openbsi_srv -> cw_rtu_10 bsap read dst 10 src 1 seqnum 1 addr 100 count 2 wait 5ms
    openbsi_srv <- cw_rtu_10 bsap read isresponse dst 1 src 10 seqnum 1 addr 100 value 420
}
```

### Automatic Wire Calculations

- Defaults to UDP destination port `1234` (`transport tcp` switches to stateful TCP `1234`) and calculates the BSAP/IP message length header automatically.
- Encodes 16-bit BSAP node addresses (`dst`, `src`), 8-bit `seqnum`, and RDB function codes (`poll`, `read`, `write`, plus response flag bit when `isresponse` is set).
- Packs 16-bit signal `addr`, item `count`, and 32-bit data `value` fields for RDB Read responses and RDB Write commands.

## Practical Lab Example: Water Booster Station ControlWave RTU Cycle: BSAP Poll, Tank Level Read & Pump Setpoint Write (`bsap_controlwave_scada.frasm`)

Polls ControlWave RTU `10` over UDP `1234`, reads reservoir level RDB `addr 200`, and issues an RDB `write` (`addr 205`, `value 65`) with RTU acknowledgment.

```frameassembly
host water_scada { ip 172.24.12.5 }
host booster_rtu { ip 172.24.12.50 }

compile {
    // 1. BSAP Node Status Poll (seqnum 1) -> RTU Response
    water_scada -> booster_rtu bsap poll dst 50 src 1 seqnum 1 wait 4ms
    water_scada <- booster_rtu bsap poll isresponse dst 1 src 50 seqnum 1 wait 10ms
    // 2. Remote Database (RDB) Read of Reservoir Tank Level (addr 200, count 2)
    water_scada -> booster_rtu bsap read dst 50 src 1 seqnum 2 addr 200 count 2 wait 5ms
    water_scada <- booster_rtu bsap read isresponse dst 1 src 50 seqnum 2 addr 200 value 1840 wait 15ms
    // 3. RDB Write of Booster Pump VFD Speed Setpoint (addr 205 = 65%)
    water_scada -> booster_rtu bsap write dst 50 src 1 seqnum 3 addr 205 count 1 value 65 wait 5ms
    water_scada <- booster_rtu bsap write isresponse dst 1 src 50 seqnum 3 addr 205 value 65
}
```

```bash
# 1. Eyeball relative timestamps, ports & wire lengths in your terminal
frameassembly show bsap_controlwave_scada.frasm

# 2. Compile .frasm -> classic .pcap, nanosecond .pcapng, or pipe via '-'
frameassembly bsap_controlwave_scada.frasm bsap_controlwave_scada.pcap
frameassembly bsap_controlwave_scada.frasm bsap_controlwave_scada.pcapng

# 3. Round-trip .pcap <-> .frasm whenever you want to scrub or tweak an existing capture
frameassembly reverse bsap_controlwave_scada.pcap bsap_controlwave_scada_edited.frasm

# 4. Verify dissected protocol fields in tshark
tshark -r bsap_controlwave_scada.pcap
```

## All `bsap` Keywords & Header Options

Packet line pattern: `src_host -> dst_host bsap [read | write | poll | func "<text_or_num>"] [dst <0..65535>] [src <0..65535>] [seqnum <0..255>] [addr <0..65535>] [count <0..65535>] [value <number>] [isresponse] [transport <udp|tcp>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `read \| write \| poll \| func "<text_or_num>" \| [isresponse]` | BSAP Application Function selector + Response flag | `poll, request` | Selects Poll, RDB Read, RDB Write, or numeric BSAP function byte and toggles response direction. |
| `dst <0..65535> \| src <0..65535>` | 16-bit BSAP Destination & Source RTU/Master Node Addresses | `dst 1, src 0` | Writes the 2-byte Destination and Source BSAP node addresses in the BSAP/IP header. |
| `seqnum <0..255>` | 8-bit BSAP Message Sequence Number (`0..255`) | `1` | Writes the 1-byte transaction Sequence Number echoed by the RTU. |
| `addr <0..65535> \| count <0..65535> \| value <number>` | 16-bit RDB Signal Index/Address, Point Count, and 32-bit Signal Value | `addr 1, count 1, value 0` | Populates RDB Read/Write request lists and data values. |
| `transport <udp\|tcp>` | `udp` or `tcp` | `udp` | Switches Layer 4 between BSAP/UDP (`1234`) and BSAP/TCP (`1234`). |

## Cross-Layer Options Valid with `bsap`

| Option | Range / Format | Default | Effect |
| --- | --- | --- | --- |
| `wait <number><ns\|us\|ms\|s\|m>` | Time gap (ns, us, ms, s, m) | `10ms` | Advances the packet timestamp clock after sending this packet and before the next packet (`wait 0ns` sends back-to-back). |
| `comment "<text>"` | Quoted UTF-8 annotation | `Omitted` | Attaches an Enhanced Packet Block `opt_comment` annotation when compiling to `.pcapng` (Wireshark `pkt_comment` filter). |
| `payload "<text>" \| hexpayload "<hex>"` | Quoted text or hex bytes (choose one) | `Omitted` | Appends or overrides raw payload bytes (`"deadbeef"`, `"de:ad:be:ef"`, or `"\x01\x02"`) and updates packet lengths and checksums. |
| `srcmac <mac> \| dstmac <mac> \| ethertype <0..65535>` | Colon/hyphen/Cisco MAC · EtherType 0..65535 (e.g. 0x0800) | `Host MAC / RFC multicast MAC / auto EtherType` | Overrides the 14-byte Ethernet II source MAC, destination MAC, or standalone Layer 2 16-bit EtherType. |
| `fcs \| badfcs` | Ethernet CRC-32 trailer flags (choose one) | `Omitted` | `fcs` appends a valid IEEE 802.3 4-byte CRC-32 Frame Check Sequence trailer; `badfcs` XOR-corrupts the CRC-32 for NIC/tap negative tests. |
| `vlan <0..4095> [pcp <0..7>] [dei <0\|1>]` | VLAN ID 0..4095, PCP 0..7, DEI 0..1 | `Untagged` | Wraps the packet in an IEEE 802.1Q VLAN tag (`0x8100`) with optional 802.1p QoS priority. |
| `qinq <0..4095> [innervlan <0..4095>]` | Outer S-VLAN 0..4095, Inner C-VLAN 0..4095 | `Untagged (innervlan 1 when qinq is set)` | Double-tags the packet with outer IEEE 802.1ad S-Tag (`0x88a8`) plus inner IEEE 802.1Q C-Tag (`0x8100`). |
| `mpls <0..1048575> [label <0..1048575>] [exp\|tc <0..7>] [bos <0\|1>] [mplsttl <0..255>]` | Repeatable label stack (0..1048575, exp/tc 0..7, bos 0..1, ttl 0..255) | `Unlabeled (auto bos=0 on outer labels, bos=1 on bottom)` | Prepends one or more 32-bit MPLS label shims (`0x8847`, e.g. `mpls 16000 mpls 24010`) ahead of Layer 2 EoMPLS or Layer 3 payloads. |
| `ttl <0..255> [dscp <0..63>] [ecn <0..3>]` | TTL/Hop Limit 0..255, DSCP 0..63, ECN 0..3 | `ttl 64, dscp 0, ecn 0` | Sets IPv4 TTL or IPv6 Hop Limit plus DiffServ (DSCP) and ECN QoS markings. |
| `ipid <0..65535> [df] [mf] [fragoffset <0..8191>] [routeralert] [badipchecksum]` | IPv4 ID, DF/MF flags, offset 0..8191, Router Alert & checksum fault | `ipid 1, flags 0, fragoffset 0, valid RFC 1071 checksum` | Sets IPv4 Identification/fragmentation, injects RFC 2113 Router Alert (`20 04 00 00`), or corrupts the 16-bit IPv4 header checksum (`badipchecksum`). |
| `flowlabel <0..1048575>` | 20-bit IPv6 Flow Label (0..1048575) | `0` | Sets the 20-bit IPv6 Flow Label for ECMP routing tests (valid on IPv6 endpoint pairs only). |
| `srcport <0..65535> \| dstport <0..65535>` | Port number 0..65535 | `srcport 49152 (remembered), UDP 1234 (or TCP 1234 via transport tcp)` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `transport <tcp\|udp\|sctp>` | `tcp`, `udp`, or `sctp` (dual TCP/UDP + universal L7 SCTP) | `transport udp` | Switches Layer 4 transport between stateful TCP, connectionless UDP, or Castagnoli CRC-32c SCTP association. |
| `badchecksum \| nochecksum` | L4 checksum fault or UDP zero-checksum flag | `Valid RFC checksum / CRC-32c` | `badchecksum` corrupts the L4 checksum for negative firewall/NIC offload tests; `nochecksum` writes `0x0000` in UDP headers. |
| `seq <number> \| acknum <number> \| win\|window <0..65535> \| urgent <0..65535> \| syn \| ack \| psh \| fin \| rst \| urg \| ece \| cwr` | TCP sequence/ack numbers, window, flags, SACK, TCP-MD5 & TFO options | `psh ack, win 65535, automatic TCP seq/acknum` | When using TCP (`transport tcp` or default TCP): overrides automatic TCP sequence tracking, flags, or `mss`/`wscale`/`sackperm`/`sackblock`/`tsval`/`tsecr`/`tcpmd5`/`tfo`. |

## Common Mistakes & Quick Fixes

### Swap dst and src BSAP Node IDs on <- RTU Replies

- **Valid v3.3.0 Syntax:** `scada -> rtu bsap read dst 50 src 1 seqnum 2 addr 200
scada <- rtu bsap read isresponse dst 1 src 50 seqnum 2 addr 200 value 100`

Just like DNP3 and ROC Plus, `<-` swaps Ethernet/IP/UDP addresses automatically, while `dst` and `src` explicitly set the application-layer BSAP node fields.

### Keep seqnum Within 8-Bit Bounds (0..255)

- **Avoid:** `scada -> rtu bsap poll seqnum 1000`
- **Valid v3.3.0 Syntax:** `scada -> rtu bsap poll seqnum 1`

BSAP sequence numbers are 1 octet wide (`0..=255`) and wrap modulo 256.
