summaryrefslogtreecommitdiff
path: root/ADSBDecoderFramework/Decoder.swift
blob: 1296b40cfd54f8d80efe5e65a0a893576adbfc18 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
//  Decoder.swift
//  ADSBDecoder
//
//  Created by Jacky Jack on 28/05/2024.
//

import Foundation

func BarometricAltitudeFeat(_ altitude: UInt16) -> Int {
    let QBit = (altitude>>4)&0x1
    if QBit == 1 {
        //remove qbit
        let part1:UInt16 = altitude&0xf
        let part2:UInt16 = (altitude>>1)&0x7ff0
        let altitude25 = part1 + part2
        //print("altitude2 \(altitude25) ")
        return Int(altitude25)*25-1000
    }
    return Int(altitude)*100-1000
}

public class Decoder {
    var adsb_data: String = ""
    public var DataFormat: UInt32 = 0;

    public init (_ adsb_data: String) {
        //print(adsb_data)
        self.adsb_data = adsb_data
        //get the first 8 bits as integer
        let startI =  adsb_data.startIndex
        let endI = adsb_data.index(startI, offsetBy: 1)
        let firstByteString = adsb_data[startI...endI]
        //print("\(firstByteString)")
        if let ControlMsg = UInt32(firstByteString, radix: 16) {
            //print("ControlMsg = \(ControlMsg)")
            let CM_DataFormat = (ControlMsg&0xF8)>>3
            DataFormat = CM_DataFormat
            //let CM_TranspoderCapability = ControlMsg&(0x7)
        }
        //print("Data Format \(DataFormat)")
    }
    
    public func getDataFormat17() -> DataFormat17? {
        
        if (DataFormat != 17) {
            return nil
        }
        //let startI =  adsb_data.index(adsb_data.startIndex, offsetBy: 1)
        //let endI = adsb_data.index(adsb_data.endIndex, offsetBy: -1)
        //let adsbData = adsb_data[startI...endI]
        let ret:DataFormat17 = DataFormat17(adsb_data)
        return ret;
    }
    
    func getDataFormat18() {
        print("Not implemented")
    }
    
}

func ICAOAlphabet(_ code: UInt8) -> String {
    if ((code>=1) && (code<=26)) {
        return String(UnicodeScalar(code+64))
    } else if ((code >= 48)&&(code<=57)) {
        return String(UnicodeScalar(code))
    } else if (code==32) {
        return " "
    }
    return "#"
}

func ICAO2String(_ b1: UInt8, _ b2: UInt8, _ b3: UInt8, _ b4: UInt8, _ b5: UInt8, _ b6: UInt8, _ b7: UInt8, _ b8: UInt8) -> String {
    return ICAOAlphabet(b1)+ICAOAlphabet(b2)+ICAOAlphabet(b3)+ICAOAlphabet(b4)+ICAOAlphabet(b5)+ICAOAlphabet(b6)+ICAOAlphabet(b7)+ICAOAlphabet(b8)
}






class DataFormat19:CustomStringConvertible  {
    init(_ bindata:ArraySlice<UInt8>) {
        print("Dataformat: 19")
    }
    
    var description: String {
        return ""
    }
}