#include "cits_parse.h" #include esp_err_t cits_parse_80211(const uint8_t *f, uint16_t len, ieee80211_hdr_t *o) { if (!f || len < 14) { return ESP_ERR_INVALID_SIZE; } memset(o, 0, sizeof(*o)); uint16_t fc = (uint16_t)(f[0] | (f[1] << 8)); o->type = (uint8_t)((fc >> 2) & 0x03); o->subtype = (uint8_t)((fc >> 4) & 0x0F); o->to_ds = (fc >> 8) & 1; o->from_ds = (fc >> 9) & 1; o->more_frag = (fc >> 10) & 1; o->retry = (fc >> 11) & 1; o->protected_ = (fc >> 14) & 1; o->order = (fc >> 15) & 1; /* Address count: data -> 3 (4 if WDS to_ds&&from_ds), mgmt -> 3, ctrl -> 2 */ int naddr; if (o->type == 2) { naddr = (o->to_ds && o->from_ds) ? 4 : 3; } else if (o->type == 0) { naddr = 3; } else { naddr = 2; } int off = 4; if ((uint32_t)off + (uint32_t)naddr * 6 > len) { return ESP_ERR_INVALID_SIZE; } memcpy(o->da, f + off, 6); off += 6; memcpy(o->sa, f + off, 6); off += 6; memcpy(o->bssid, f + off, 6); off += 6; o->addr_count = naddr; if (o->type == 2) { off += 4; /* seq control + frame-control word */ } o->is_data = (o->type == 2); if ((uint32_t)off <= len) { o->payload = f + off; o->payload_len = (uint16_t)(len - off); } else { o->payload = NULL; o->payload_len = 0; } return ESP_OK; } esp_err_t cits_parse_asdu(const uint8_t *p, uint16_t len, cits_msg_t *o) { if (!p || len < 7) { return ESP_ERR_INVALID_SIZE; } memset(o, 0, sizeof(*o)); o->asdu_id = p[0]; o->asdu_version = p[1]; o->asdu_len = (uint16_t)(p[2] | (p[3] << 8)); /* ASdu body length (BE) */ switch (o->asdu_id) { case 0x01: o->type = CITS_SPM; break; case 0x02: o->type = CITS_SPT; break; case 0x03: o->type = CITS_SPS; break; case 0x04: o->type = CITS_SPA; break; case 0x05: o->type = CITS_MAP; break; case 0x06: o->type = CITS_CAM; break; case 0x07: o->type = CITS_DENM; break; default: o->type = CITS_UNKNOWN; break; } /* ASdu body begins after: id(1) ver(1) len(2) protov(1) len2(2) = 7 bytes */ int b = 7; if (o->type == CITS_SPM && (uint16_t)(b + 6) <= len) { o->site_id = (uint32_t)(p[b] << 16) | (p[b + 1] << 8) | p[b + 2]; o->num_phases = p[b + 5]; /* first SignalPhase: phaseId(1) stateId(2) ... -> state is bytes 6..7 */ if (o->num_phases >= 1 && (uint16_t)(b + 8) <= len) { o->phase0_state = p[b + 7]; /* ASN.1 INTEGER state value (0-5) */ } } else if (o->type == CITS_CAM && (uint16_t)(b + 5) <= len) { /* msgIssueID(2) then stationID(21): take leading 3 bytes as a hint */ o->station_id = (uint32_t)(p[b + 2] << 16) | (p[b + 3] << 8) | p[b + 4]; } return ESP_OK; } const char *cits_type_name(cits_msg_type_t t) { switch (t) { case CITS_SPM: return "SpMS"; case CITS_SPT: return "SpTM"; case CITS_SPS: return "SpSM"; case CITS_SPA: return "SpAM"; case CITS_MAP: return "MAPM"; case CITS_CAM: return "CAM"; case CITS_DENM: return "DENM"; default: return "raw"; } }