# c1222 — Port 1153 ANSI C12.22 / IEEE 1703 Smart Grid AMI Tables (`c1222`)

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

- **Full Protocol Name:** ANSI C12.22 / IEEE 1703 Smart-Meter Utility AMI Application Protocol
- **Keyword:** `c1222`
- **OSI Layer & Transport:** Layer 7 · UDP 1153 (or TCP 1153 via transport tcp)
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** ANSI C12.22-2012, IEEE 1703, ANSI C12.19 (Tables)
- **Wireshark Display Filter:** `c1222`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> UDP 1153 (or TCP 1153 via transport tcp) (transport tcp|udp) -> ANSI C12.22 (AMI Smart Grid)`

## Overview

Utility smart-meter AMI headends communicate over ANSI C12.22 on port `1153`: `c1222` encodes ASN.1 BER ACSE AP-titles (`callingap`, `calledap`) and EPSEM C12.19 table reads/writes (`tableid`).

ANSI C12.22 (`c1222`, IEEE 1703) transports **ANSI C12.19 smart electric/gas/water meter tables** across utility **Advanced Metering Infrastructure (AMI)** head-end servers, field-area mesh collectors, and IPv4/IPv6 smart meters on **UDP or TCP port `1153`**. Every C12.22 frame wraps an **ISO 8650 ASN.1 BER ACSE header** (`Calling-AP-title` OID, `Called-AP-title` OID, `Calling/Called AP-invocation-id`) around an **Extended Protocol Specification for Electric Metering (`EPSEM`)** payload (`EPSEM Control` byte, optional `ED-Class`, and PSEM service codes: `0x20` Identify, `0x50` Logon, `0x30` Full Table Read, `0x40` Full Table Write, `0x52` Logoff, `0x21` Terminate).

FrameAssembly encodes valid ASN.1 BER ACSE OIDs, BER length octets, and ANSI C12.19 `tableid` read/write requests and `<OK>` (`0x00`) responses.

## Minimal Quickstart (`c1222_quickstart.frasm`)

```frameassembly
host ami_headend { ip 10.115.1.10 }
host smart_meter { ip 10.115.20.45 }

compile {
    ami_headend -> smart_meter c1222 read callingap 1.3.6.1.4.1.9999.1 calledap 1.3.6.1.4.1.9999.45 callingid 1 tableid 1 wait 5ms
    ami_headend <- smart_meter c1222 response callingap 1.3.6.1.4.1.9999.45 calledap 1.3.6.1.4.1.9999.1 callingid 1 value 4820
}
```

### Automatic Wire Calculations

- Defaults to UDP destination port `1153` (`transport tcp` switches to stateful TCP `1153`) and formats the outer ASN.1 BER `0x60` (`AARE`/`AARQ` C12.22 ACSE PDU) envelope.
- Encodes `callingap` and `calledap` dotted OIDs in ASN.1 BER Object Identifier format alongside `callingid` / `calledid` invocation integers.
- Builds the inner `user-information` (`0xbe`) **EPSEM** frame with `epsemctrl` (`0x80` Cleartext), big-endian 16-bit C12.19 `tableid`, and 1-byte checksum.

## Practical Lab Example: IPv6 Wi-SUN Smart Meter Billing Collection: Identify, Logon, C12.19 Table 23 Read & Logoff (`c1222_ipv6_smart_meter_ami.frasm`)

Simulates an IPv6 utility AMI collector identifying a residential smart meter, logging on, reading ANSI C12.19 Standard Table `23` (Current Register Data), and terminating the session.

```frameassembly
host collector_v6 { ip 2001:db8:1153::1 }
host meter_0042   { ip 2001:db8:1153::42 }

compile {
    // 1. EPSEM Identify (0x20) -> <OK> (0x00) Response
    collector_v6 -> meter_0042 c1222 identify callingap 1.3.6.1.4.1.33500.1 calledap 1.3.6.1.4.1.33500.42 callingid 10 wait 4ms
    collector_v6 <- meter_0042 c1222 response callingap 1.3.6.1.4.1.33500.42 calledap 1.3.6.1.4.1.33500.1 callingid 10 errcode 0 wait 6ms
    // 2. EPSEM Logon (0x50) -> <OK>
    collector_v6 -> meter_0042 c1222 logon callingap 1.3.6.1.4.1.33500.1 calledap 1.3.6.1.4.1.33500.42 callingid 11 wait 4ms
    collector_v6 <- meter_0042 c1222 ok callingap 1.3.6.1.4.1.33500.42 calledap 1.3.6.1.4.1.33500.1 callingid 11 wait 6ms
    // 3. Full Read (0x30) of ANSI C12.19 Table 23 (Current Register Data kWh)
    collector_v6 -> meter_0042 c1222 read callingap 1.3.6.1.4.1.33500.1 calledap 1.3.6.1.4.1.33500.42 callingid 12 tableid 23 wait 5ms
    collector_v6 <- meter_0042 c1222 response callingap 1.3.6.1.4.1.33500.42 calledap 1.3.6.1.4.1.33500.1 callingid 12 value 12845 wait 6ms
    // 4. Graceful Terminate (0x21)
    collector_v6 -> meter_0042 c1222 terminate callingap 1.3.6.1.4.1.33500.1 calledap 1.3.6.1.4.1.33500.42 callingid 13
}
```

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

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

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

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

## All `c1222` Keywords & Header Options

Packet line pattern: `src_host -> dst_host c1222 [read | write | identify | logon | logoff | terminate | response | ok | service "<text_or_num>"] [callingap <oid>] [calledap <oid>] [callingid <number>] [calledid <number>] [epsemctrl <0..255>] [edclass <number>] [tableid <0..65535>] [value <0..65535>] [errcode <0..255>] [transport <udp|tcp>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `read \| write \| identify \| logon \| logoff \| terminate \| response \| ok \| service "<text_or_num>"` | EPSEM PSEM Service Code (`0x20` Identify, `0x21` Terminate, `0x30` Read, `0x40` Write, `0x50` Logon, `0x52` Logoff, `0x00` OK) | `read (0x30)` | Selects the EPSEM Request or Response service byte inside the ACSE `user-information` block. |
| `callingap <oid> \| calledap <oid>` | Dotted ASN.1 OID (unquoted `1.3.6.1...` or quoted `"1.3.6.1..."`) | `1.3.6.1.4.1.9999.1 / 1.3.6.1.4.1.9999.2` | Encodes `Calling-AP-title` (`0xa6`) and `Called-AP-title` (`0xa2`) universal ApTitles. |
| `callingid <number> \| calledid <number>` | 32-bit `AP-invocation-id` integers | `callingid 1` | Encodes `Calling-AP-invocation-id` (`0xa8`) and optional `Called-AP-invocation-id` (`0xa4`). |
| `epsemctrl <0..255> \| edclass <number>` | 8-bit EPSEM Control byte & optional 32-bit End-Device Class | `epsemctrl 0x80 (Reserved bit, cleartext)` | Writes the EPSEM Control header byte and optional 4-byte `ED-Class` when bit `0x10` is set. |
| `tableid <0..65535> \| value <0..65535> \| errcode <0..255>` | 16-bit ANSI C12.19 Table ID, 16-bit Table Data Word, and 8-bit PSEM Response Code (`0x00` OK) | `tableid 1, value 0, errcode 0` | Populates C12.19 Table Read/Write payloads and `<OK>`/`<ERR>` response codes. |
| `transport <udp\|tcp>` | `udp` or `tcp` | `udp` | Switches Layer 4 between connectionless C12.22/UDP (`1153`) and stateful TCP (`1153`). |

## Cross-Layer Options Valid with `c1222`

| 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 1153 (or TCP 1153 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

### Pass Valid Dotted Numeric OIDs (At Least 2 Arcs) to callingap / calledap

- **Avoid:** `headend -> meter c1222 read callingap "meter01"`
- **Valid v3.3.0 Syntax:** `headend -> meter c1222 read callingap 1.3.6.1.4.1.9999.1 calledap 1.3.6.1.4.1.9999.2`

ANSI C12.22 `ApTitle` fields are ASN.1 BER `OBJECT IDENTIFIER` values (`1.3.6.1...` or `2.16.124...`), not free-form hostnames.

### Swap callingap and calledap on Meter <- Responses

- **Valid v3.3.0 Syntax:** `h -> m c1222 identify callingap 1.3.6.1.4.1.99.1 calledap 1.3.6.1.4.1.99.2
h <- m c1222 ok callingap 1.3.6.1.4.1.99.2 calledap 1.3.6.1.4.1.99.1`

When the smart meter responds, its own `ApTitle` becomes `Calling-AP-title` and the head-end’s OID becomes `Called-AP-title`.
