From 58d9d561df35f88884b2959d2cf322f1ee69e3cd Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Wed, 26 Jun 2024 08:29:38 +0100 Subject: Decoding lat/lon seems to work --- ADSBDecoder/AirplaneTracker.swift | 110 +++++++++++++++++++ ADSBDecoder/Decoder.swift | 91 ++++++++-------- ADSBDecoder/PositionDecoder.swift | 221 ++++++++++++++++++++++++++++++++++++++ ADSBDecoder/Query.swift | 65 ++++++++++- ADSBDecoder/main.swift | 42 +++++++- 5 files changed, 478 insertions(+), 51 deletions(-) create mode 100644 ADSBDecoder/AirplaneTracker.swift create mode 100644 ADSBDecoder/PositionDecoder.swift diff --git a/ADSBDecoder/AirplaneTracker.swift b/ADSBDecoder/AirplaneTracker.swift new file mode 100644 index 0000000..0ac8bfc --- /dev/null +++ b/ADSBDecoder/AirplaneTracker.swift @@ -0,0 +1,110 @@ +// +// AirplaneTracker.swift +// ADSBDecoder +// +// Created by Jacky Jack on 20/06/2024. +// + +import Foundation + +struct Airplane { + var addressReady:Bool = false + var Address:Int + var ICAOready:Bool = false + var ICAOname:String = "" + var locationReady:Bool = false + var lat:Double = 0.0 + var long:Double = 0.0 + var altitudeReady:Bool = false + var altitude:Int = 0 + var altitudeCount:Int = 0 + var positionDecoder:PositionDecoder = PositionDecoder() +} + +class AirPlaneTracker { + + var airplanes:[Int:Airplane] = [:] + + init () { + + } + + func addDF17Indentification(_ address: Int, _ ICAOname: String) { + if (airplanes[address] == nil) { + airplanes[address] = Airplane(addressReady: true, Address: address,ICAOready: true, ICAOname: ICAOname) + + } else { + if (airplanes[address]?.ICAOname == "") { + airplanes[address]?.ICAOname = ICAOname + airplanes[address]?.ICAOready = true + } + } + } + + func addDF17AirBornPosition(_ address: Int, _ cpr_lat: Int, _ cpr_long: Int, _ alt: Int, _ even: Bool) { + + if airplanes[address] == nil { + return + } + + //deal with altitude + //if airplanes[address] != nil { + if (airplanes[address]?.altitudeReady != true) { + airplanes[address]?.altitudeReady = true + airplanes[address]?.altitude = alt + airplanes[address]?.altitudeCount += 1 + } else { + airplanes[address]?.altitude = alt + airplanes[address]?.altitudeCount += 1 + } + //} + //do the airborn position + if (even) { + airplanes[address]?.positionDecoder.addEvenPosition(UInt32(cpr_lat), UInt32(cpr_long), mstime()) + } else { + + airplanes[address]?.positionDecoder.addOddPosition(UInt32(cpr_lat), UInt32(cpr_long), mstime()) + } + + } + + + func getPosition(_ address: Int) -> (Double,Double)? { + if (airplanes[address] == nil) { + return nil + } + + if let airplane = airplanes[address] { + if (airplane.positionDecoder.calcPosition()) { + return airplane.positionDecoder.getPosition() + } + } + + return nil + } + + func getAltitude() { + + print("not implemented") + } + + func printAllICAOnames() { + let extra = true + var once=false + for (address,plane) in airplanes { + if plane.ICAOready { + print(String("\(plane.ICAOname) "), terminator: "") + } + if (extra) { + print("Alitude \(plane.altitudeCount) Positions \(plane.positionDecoder.queue.count)") + } + //if (!once) { + // for i in plane.positionDecoder.queue { + // print("\(i.cpr_lat) \(i.cpr_long) \(i.even)") + // } + // once = true + //} + } + } + +} diff --git a/ADSBDecoder/Decoder.swift b/ADSBDecoder/Decoder.swift index 95f798b..65a9ddd 100644 --- a/ADSBDecoder/Decoder.swift +++ b/ADSBDecoder/Decoder.swift @@ -14,7 +14,7 @@ func BarometricAltitudeFeat(_ altitude: UInt16) -> Int { let part1:UInt16 = altitude&0xf let part2:UInt16 = (altitude>>1)&0x7ff0 let altitude25 = part1 + part2 - print("altitude2 \(altitude25) ") + //print("altitude2 \(altitude25) ") return Int(altitude25)*25-1000 } return Int(altitude)*100-1000 @@ -86,11 +86,15 @@ class DataFormat17 { var MovementField=0//37:43 var HeadingBit=0//44 var HeadingField=0//45:51 - var CPROddEven=0//53 - var CPRlat=0//54:70 - var CPRlon=0//71:87 + //var CPROddEven=0//53 + //var CPRlat=0//54:70 + //var CPRlon=0//71:87 var ParityIntegrity=0//88-111 + //all avaliable message types + var messageIdentification:ADSBTypeCodeIndentification? = nil + var messageAirbornPositon:ADSBTypeCodeAirbonePositon? = nil + init(_ adsb_data: String) { //print("Dataformat: 17!") //print(adsb_data) @@ -114,65 +118,48 @@ class DataFormat17 { //Decode Capability let cap = (bindata[0]>>1)&0x7 - print(String(format: "cap %02x", cap)) + //print(String(format: "cap %02x", cap)) Capability = Int(cap) //Decode Address Announcement let address_ann = UInt32(bindata[1])<<16 + UInt32(bindata[2])<<8 + UInt32(bindata[3]) - print(String(format: "address %06x", address_ann)) + //print(String(format: "address %06x", address_ann)) AddressAnnounced = Int(address_ann) //Decode Type Code let tc_byte = bindata[4] >> 3 - print(String(format: "tc %02d", tc_byte)) + //print(String(format: "tc %02d", tc_byte)) TypeCode = Int(tc_byte) - + + //aircraft indentification and category if (tc_byte == 4) { - let msg = ADSBTypeCode4(bindata[4...10]) + let msg = ADSBTypeCodeIndentification(bindata[4...10]) print("=====ADSB MESSSGE 04 =======") print(msg) print("============================") - } else if (tc_byte == 11) { - let msg = ADSBTypeCode11(bindata[4...10]) - print("=====ADSB MESSSGE 11 =======") + + messageIdentification = msg + //airborn position + } else if ((tc_byte >= 8) && (tc_byte <= 18)) { + let msg = ADSBTypeCodeAirbonePositon(bindata[4...10]) + print(String(format:"=====ADSB MESSSGE %02d ======= AA:%04d", tc_byte, AddressAnnounced)) print(msg) print("============================") + + messageAirbornPositon = msg + //airborn velocity } else if (tc_byte == 19) { - print("Byte 19") + print("=====ADSB MESSSGE 19 =======") + print("=====VELOCITY =======") } else { - print("Unknow TC byte") - /* - //Decode Movement Field - let mov_byte = (bindata[4]&0x7) + (bindata[5]>>5) - print(String(format: "mov %02x", mov_byte)) - - //Heading Bit - let heading_bit = (bindata[5]>>3)&0x1 - print(String(format: "heading %02x", heading_bit)) - - //Heading direction - let heading_direction = (bindata[5]&0xF)<<3+(bindata[6]>>4)&0xF - print(String(format: "hdir %02x", heading_direction)) - - //CPR Odd/Even - let cpr_odd_even = (bindata[6]>>2)&0x1; - print(String(format: "cpr %02x", cpr_odd_even)) - - //CPR lat - let cpr_lat = (bindata[6]&0x7)<<14 + (bindata[7]<<7) + (bindata[8]>>1) - print(String(format: "cpr lat %06x", cpr_lat)) - - //CPR lon - let cpr_lon = ((bindata[8]&0x1)<<16) + (bindata[9]<<8) + (bindata[10]) - print(String(format: "cpr lon %06x", cpr_lon)) - */ + print("=====ADSB MESSSGE UNKNOWN =======") } } } -class ADSBTypeCode4: CustomStringConvertible { +class ADSBTypeCodeIndentification: CustomStringConvertible { var TypeCode:Int = 4 var Category:Int = 0 var ICAOName:String @@ -200,7 +187,8 @@ class ADSBTypeCode4: CustomStringConvertible { } } -class ADSBTypeCode11:CustomStringConvertible { +//DF17 message types codes 8 to 18 +class ADSBTypeCodeAirbonePositon:CustomStringConvertible { var TypeCode:Int = 11 var SurveillanceStatus: Int = 0 var SingleAntennaFlag: Int = 0 @@ -216,7 +204,7 @@ class ADSBTypeCode11:CustomStringConvertible { let saf = bindata[5]&0x1 SingleAntennaFlag = Int(saf) let altitude = UInt16(bindata[5])<<4 + (UInt16(bindata[6])>>4)&0xf - print(altitude) + //print(altitude) Altitude = BarometricAltitudeFeat(altitude) let time = (bindata[6]>>3)&0x1 Time = Int(time) @@ -230,15 +218,24 @@ class ADSBTypeCode11:CustomStringConvertible { var description: String { var description = "SS \(SurveillanceStatus) SAF \(SingleAntennaFlag) Altitude \(Altitude)ft \n" - description += "Time \(Time) CPR \(CPRFormat) \n" + description += "Time \(Time) CPR \(CPRFormat)" + if (CPRFormat == 0) { + description += "even" + } else { + description += "odd" + } + description += "\n" description += "Lat \(Latitude) Long \(Longitude)" return description } } -class DataFormat18 { - init(_ adsb_data: String) { - print("Dataformat: 18") - print(adsb_data) +class DataFormat19:CustomStringConvertible { + init(_ bindata:ArraySlice) { + print("Dataformat: 19") + } + + var description: String { + return "" } } diff --git a/ADSBDecoder/PositionDecoder.swift b/ADSBDecoder/PositionDecoder.swift new file mode 100644 index 0000000..7d77221 --- /dev/null +++ b/ADSBDecoder/PositionDecoder.swift @@ -0,0 +1,221 @@ +// +// PositionDecoder.swift +// ADSBDecoder +// +// Created by Jacky Jack on 14/06/2024. +// + +import Foundation +import Collections + +struct PositionData { + var cpr_lat: UInt32 + var cpr_long: UInt32 + var even: Bool + var cprtime: Int64 +} + +func cpr_mod(_ a: Int, _ b: Int) -> Int { + var res:Int = a % b; + if (res < 0) { + res += b + } + return res; +} + +/* The NL function uses the precomputed table from 1090-WP-9-14 */ +func cpr_NLFunction(_ _lat: Double) -> Int { + var lat:Double = _lat + if (lat < 0) { + lat = -lat + } /* Table is simmetric about the equator. */ + if (lat < 10.47047130) {return 59} + if (lat < 14.82817437) {return 58} + if (lat < 18.18626357) {return 57} + if (lat < 21.02939493) {return 56} + if (lat < 23.54504487) {return 55} + if (lat < 25.82924707) {return 54} + if (lat < 27.93898710) {return 53} + if (lat < 29.91135686) {return 52} + if (lat < 31.77209708) {return 51} + if (lat < 33.53993436) {return 50} + if (lat < 35.22899598) {return 49} + if (lat < 36.85025108) {return 48} + if (lat < 38.41241892) {return 47} + if (lat < 39.92256684) {return 46} + if (lat < 41.38651832) {return 45} + if (lat < 42.80914012) {return 44} + if (lat < 44.19454951) {return 43} + if (lat < 45.54626723) {return 42} + if (lat < 46.86733252) {return 41} + if (lat < 48.16039128) {return 40} + if (lat < 49.42776439) {return 39} + if (lat < 50.67150166) {return 38} + if (lat < 51.89342469) {return 37} + if (lat < 53.09516153) {return 36} + if (lat < 54.27817472) {return 35} + if (lat < 55.44378444) {return 34} + if (lat < 56.59318756) {return 33} + if (lat < 57.72747354) {return 32} + if (lat < 58.84763776) {return 31} + if (lat < 59.95459277) {return 30} + if (lat < 61.04917774) {return 29} + if (lat < 62.13216659) {return 28} + if (lat < 63.20427479) {return 27} + if (lat < 64.26616523) {return 26} + if (lat < 65.31845310) {return 25} + if (lat < 66.36171008) {return 24} + if (lat < 67.39646774) {return 23} + if (lat < 68.42322022) {return 22} + if (lat < 69.44242631) {return 21} + if (lat < 70.45451075) {return 20} + if (lat < 71.45986473) {return 19} + if (lat < 72.45884545) {return 18} + if (lat < 73.45177442) {return 17} + if (lat < 74.43893416) {return 16} + if (lat < 75.42056257) {return 15} + if (lat < 76.39684391) {return 14} + if (lat < 77.36789461) {return 13} + if (lat < 78.33374083) {return 12} + if (lat < 79.29428225) {return 11} + if (lat < 80.24923213) {return 10} + if (lat < 81.19801349) {return 9} + if (lat < 82.13956981) {return 8} + if (lat < 83.07199445) {return 7} + if (lat < 83.99173563) {return 6} + if (lat < 84.89166191) {return 5} + if (lat < 85.75541621) {return 4} + if (lat < 86.53536998) {return 3} + if (lat < 87.00000000) {return 2} + else {return 1} +} + +func cpr_NFunction(_ lat: Double, _ isodd: Int) -> Int { + var nl:Int = cpr_NLFunction(lat) - isodd; + if (nl < 1) { + nl = 1 + } + return nl; +} + +func cpr_DlonFunction(_ lat: Double, _ isodd: Int) -> Double { + return 360.0 / Double(cpr_NFunction(lat, isodd)); +} + +func mstime() -> Int64 { + var tv:timeval = timeval(tv_sec: 0, tv_usec: 0) + var mst:Int64=0; + + gettimeofday(&tv, nil); + mst = Int64(tv.tv_sec)*1000; + mst += Int64(tv.tv_usec)/1000; + return mst; +} + +class PositionDecoder { + + var long: Double = 0.0 + var lat: Double = 0.0 + var ready: Bool = false + var newposition: Bool = false + + //initialise first values so this values is nonsence + var queue: Deque = [] + + init() { + + } + + func addEvenPosition(_ cpr_lat: UInt32,_ cpr_long: UInt32, _ cprtime: Int64) { + queue.append(PositionData(cpr_lat:cpr_lat, cpr_long:cpr_long, even:true, cprtime: cprtime)) + } + + func addOddPosition(_ cpr_lat: UInt32,_ cpr_long: UInt32, _ cprtime: Int64) { + queue.append(PositionData(cpr_lat:cpr_lat,cpr_long:cpr_long,even:false, cprtime: cprtime)) + } + + func getPosition() -> (Double, Double)? { + if (ready) { + return (lat, long) + } + return nil + } + + //TODO: allways adds to queue newer frees the queue + func calcPosition() -> Bool { + if (queue.count > 2) { + let el1 = queue[queue.count-1] + let el2 = queue[queue.count-2] + if (el1.even == el2.even) { + //ready = false + return false + } + } else { + ready = false + print("Position queue to short to calculate values") + return false + } + + //last to elements are evan and odd + let el1 = queue[queue.count-1] + let el2 = queue[queue.count-2] + let cpr_even = el1.even ? el1 : el2 + let cpr_odd = (!el1.even) ? el1 : el2 + + print("Position queue is ready to calculate location \(cpr_even) \(cpr_odd)") + // from here https://github.com/antirez/dump1090/blob/master/dump1090.c#L1718 + let AirDlat0:Double = 360.0/60.0 + let AirDlat1:Double = 360.0/59.0 + + let lat0 = Int(cpr_even.cpr_lat) + let lat1 = Int(cpr_odd.cpr_lat) + let lon0 = Int(cpr_even.cpr_long) + let lon1 = Int(cpr_odd.cpr_long) + + //latitude index + let j:Int = Int(floor(((59.0*Double(lat0) - 60.0*Double(lat1)) / 131072.0) + 0.5)) + + var rlat0:Double = AirDlat0 * (Double(cpr_mod(j,60)) + Double(lat0) / 131072.0) + var rlat1:Double = AirDlat1 * (Double(cpr_mod(j,59)) + Double(lat1) / 131072.0) + + if (rlat0 >= 270.0) { + rlat0 -= 360.0 + } + if (rlat1 >= 270.0) { + rlat1 -= 360.0 + } + + /* Check that both are in the same latitude zone, or abort. */ + if (cpr_NLFunction(rlat0) != cpr_NLFunction(rlat1)) { + print("Not same lat?!") + return false + } + + /* Compute ni and the longitude index m */ + if (cpr_even.cprtime > cpr_odd.cprtime) { + /* Use even packet. */ + let ni:Int = cpr_NFunction(rlat0,0); + let m:Int = Int( + floor((((Double(lon0) * (Double(cpr_NLFunction(rlat0))-1.0)) - + (Double(lon1) * Double(cpr_NLFunction(rlat0)))) / 131072.0) + 0.5) + ); + self.long = Double(cpr_DlonFunction(rlat0,0)) * (Double(cpr_mod(m,ni))+Double(lon0)/131072.0); + self.lat = rlat0; + } else { + /* Use odd packet. */ + let ni:Int = cpr_NFunction(rlat1,1); + let m:Int = Int(floor((((Double(lon0) * (Double(cpr_NLFunction(rlat1))-1.0)) - + (Double(lon1) * Double(cpr_NLFunction(rlat1)))) / 131072.0) + 0.5)); + self.long = cpr_DlonFunction(rlat1,1) * (Double(cpr_mod(m,ni))+Double(lon1)/131072.0); + self.lat = rlat1; + } + if (self.long > 180) { + self.long -= 360 + } + + ready = true + return true + } + + +} diff --git a/ADSBDecoder/Query.swift b/ADSBDecoder/Query.swift index 99d9075..a92d919 100644 --- a/ADSBDecoder/Query.swift +++ b/ADSBDecoder/Query.swift @@ -52,7 +52,7 @@ class QueryDF17TC: CustomStringConvertible { } var description: String { - var description = "TypeCode Count\n" + var description = "DF17 TypeCode Count\n" for i in 0...(TClist.count-1) { if TClist[i] != 0 { description += String(format: " %02d: %04d\n", i, TClist[i]) @@ -61,3 +61,66 @@ class QueryDF17TC: CustomStringConvertible { return description } } + +class QueryDecodedMessages: CustomStringConvertible { + + var decoded:Int = 0 + var total:Int = 0 + + init() { + + } + + func addDecoded() { + decoded += 1 + total += 1 + } + + func addUndecoded() { + total = total + 1 + } + + var description: String { + var description = "Total messages:\n" + description += "Decoded:\(decoded)\n" + description += "Total :\(total) \(String(format:"%02.01f",Float(Float(decoded)/Float(total)*100.0)))%\n" + return description + } + +} + + +class QueryDF17TC_decoded: CustomStringConvertible { + var TClist_decoded:[Int] = Array(repeating: 0, count: 32) + var TClist_total:[Int] = Array(repeating: 0, count: 32) + + init() { + + } + + func addDecoded(_ typecode: Int) { + TClist_decoded[typecode] += 1 + TClist_total[typecode] += 1 + } + + func addUndecode(_ typecode: Int) { + TClist_total[typecode] += 1 + } + + var description: String { + var description = "" + var decoded:Int = 0 + var total:Int = 0 + for i in 0...(TClist_decoded.count-1) { + total += TClist_total[i] + decoded += TClist_decoded[i] + if TClist_total[i] != 0 { + description += String(format: " %02d: %04d\n", i, TClist_decoded[i]) + } + } + description += String(format:"Total %d/%d %.1f",decoded,total,Double(Double(decoded)/Double(total))*100) + description += "%\n" + return description + } +} + diff --git a/ADSBDecoder/main.swift b/ADSBDecoder/main.swift index a324a0f..a346fbb 100644 --- a/ADSBDecoder/main.swift +++ b/ADSBDecoder/main.swift @@ -8,6 +8,7 @@ import Foundation import ArgumentParser import RegexBuilder +import SQLite3 //return true if file excists func checkIfFileExists(_ fname: String) -> Bool { @@ -82,13 +83,18 @@ let matchADSBShort = Regex { //gather stats var q_df = QueryDF() var q_dftc = QueryDF17TC() +var q_decoded = QueryDecodedMessages() +var q_df17_decoded = QueryDF17TC_decoded() + +//track all airplanes +var tracker = AirPlaneTracker() //parse line by line -var cnt=0 +var count_messages=0 for line in adsb_source.components(separatedBy: .newlines) { var found=false //print("\(cnt) \(line)") - //cnt += 1 + count_messages += 1 if let tokenMatch = try matchADSBLong.prefixMatch(in: line) { //print("\(String(tokenMatch.output))") found = true @@ -100,9 +106,32 @@ for line in adsb_source.components(separatedBy: .newlines) { if let d17 = decoder.getDataFormat17() { //print(d17) q_dftc.addTC(d17.TypeCode) + q_decoded.addDecoded() + if (d17.TypeCode == 4) { + if let indentification = d17.messageIdentification { + tracker.addDF17Indentification(d17.AddressAnnounced, indentification.ICAOName) + } + q_df17_decoded.addDecoded(4) + } else if (d17.TypeCode >= 9 && d17.TypeCode <= 18) { + if let airbornposition = d17.messageAirbornPositon { + tracker.addDF17AirBornPosition( + d17.AddressAnnounced, + airbornposition.Latitude, + airbornposition.Longitude, + airbornposition.Altitude, + airbornposition.CPRFormat == 0 + ) + if let position = tracker.getPosition(d17.AddressAnnounced) { + print("position: \(position)") + } + } + q_df17_decoded.addDecoded(d17.TypeCode) + } else { + q_df17_decoded.addUndecode(d17.TypeCode) + } } } else { - + q_decoded.addUndecoded() } q_df.addDF(decoder.DataFormat) }; @@ -110,6 +139,8 @@ for line in adsb_source.components(separatedBy: .newlines) { if let tokenMatch = try matchADSBShort.prefixMatch(in: line) { print("\(tokenMatch.output)") found = true + + q_decoded.addUndecoded() }; if (found == false) { @@ -125,4 +156,9 @@ if args.show_stats { //q_df.showStat() print(q_df) print(q_dftc) + print(q_decoded) + print(q_df17_decoded) + tracker.printAllICAOnames() + + print("Total message:\(count_messages)") } -- cgit v1.2.3