summaryrefslogtreecommitdiff
path: root/ADSBDecoderFramework/DataFormat17.swift
blob: e1ea428712fa9368cc9e0d88cd38013c777e5ddb (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
91
92
93
94
95
96
97
98
99
100
//
//  DataFormat17.swift
//  ADSBDecoder
//
//  Created by Jacky Jack on 27/09/2026.
//

import Foundation

public class DataFormat17 {

    let DataFormat=17 //0:5
    var Capability=0  //5:7
    public var AddressAnnounced=0 //8:31
    public var TypeCode=0 //32:36
    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 ParityIntegrity=0//88-111
    
    //all avaliable message types
    public var messageIdentification:ADSBTypeCodeIndentification? = nil
    public var messageAirbornPositon:ADSBTypeCodeAirbonePositon? = nil

    public init(_ adsb_data: String) {
        //print("Dataformat: 17!")
        //print(adsb_data)
        
        var bindata:[UInt8] = []
        
        var startN = adsb_data.startIndex
        var endN = adsb_data.index(startN, offsetBy: 1)
        var count=0//start index value
        while (count<adsb_data.count) {
            let u8 = UInt8(adsb_data[startN...endN], radix: 16)!
            //print(adsb_data[startN...endN])
            bindata.append(u8)
            count += 2
            if (count<adsb_data.count) {
                startN = adsb_data.index(startN, offsetBy: 2)
                endN = adsb_data.index(startN, offsetBy: 1)
            }
        }
        //print(bindata)
        
        //Decode Capability
        let cap = (bindata[0]>>1)&0x7
        //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))
        
        AddressAnnounced = Int(address_ann)
        
        //Decode Type Code
        let tc_byte = bindata[4] >> 3
        //print(String(format: "tc %02d", tc_byte))
        
        TypeCode = Int(tc_byte)
        
        //aircraft indentification and category
        if (tc_byte == 4) {
            let msg = ADSBTypeCodeIndentification(bindata[4...10])
            if decoder_debug_mode {
                print("=====ADSB MESSSGE 04 =======")
                print(msg)
                print("============================")
            }
            
            messageIdentification = msg
        //airborn position
        } else if ((tc_byte >= 8) && (tc_byte <= 18)) {
            let msg = ADSBTypeCodeAirbonePositon(bindata[4...10])
            if decoder_debug_mode {
                print(String(format:"=====ADSB MESSSGE %02d ======= AA:%04d", tc_byte, AddressAnnounced))
                print(msg)
                print("============================")
            }
            
            messageAirbornPositon = msg
        //airborn velocity
        } else if (tc_byte == 19) {
            if decoder_debug_mode {
                print("=====ADSB MESSSGE 19 =======")
                print("=====VELOCITY =======")
            }
        } else {
            if decoder_debug_mode {
                print("=====ADSB MESSSGE UNKNOWN =======")
            }
        }
    }
}