Developer Area

root/apps/iphone/superbook/trunk/DotTel_SDK/Classes/RecordLoc.m @ 497

Revision 363, 4.3 kB (checked in by henri, 5 years ago)

Updated copyright dates

Line 
1//
2//  RecordLoc.m
3//  DotTel SDK
4//
5//  Created by Henri Asseily on 9/13/08.
6/*
7 Copyright (c) 2008-2009, Telnic Ltd. All rights reserved.
8 
9 Redistribution and use in source and binary forms, with or without modification,
10 are permitted provided that the following conditions are met:
11 
12 Redistributions of source code must retain the above copyright notice, this list of conditions
13 and the following disclaimer. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the documentation and/or other
15 materials provided with the distribution.
16 Neither the name of the Telnic Ltd. nor the names of its contributors may be used to endorse or
17 promote products derived from this software without specific prior written permission.
18 THIS DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
19 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27//
28
29#import "RecordLoc.h"
30
31
32@implementation RecordLoc
33
34@synthesize isValid;
35@synthesize expiryDate;
36
37+ (id)recordWithRr:(ldns_rr *)rr {
38        NSDate *lookupDate = [[NSDate date] retain];
39        self = [RecordLoc recordWithRr:rr date:lookupDate];
40        [lookupDate release];
41        return self;
42}
43
44+ (id)recordWithRr:(ldns_rr *)rr date:(NSDate *)lookupDate {
45        self = [[[RecordLoc alloc] initWithRr:rr date:lookupDate] autorelease];
46        return self;
47}
48
49- (id)init {
50        // Never initialize it empty
51        self = [super init];
52        isValid = NO;
53        return self;
54}
55
56- (id)initWithRr:(ldns_rr *)rr date:(NSDate *)lookupDate {
57        self = [super init];
58        isValid = NO;
59       
60        if (ldns_rr_get_type(rr) != LDNS_RR_TYPE_LOC)
61                return self;
62       
63        NSTimeInterval ttl = (NSTimeInterval)ldns_rr_ttl(rr);
64        expiryDate = [lookupDate addTimeInterval:ttl];
65       
66        // get the first RDF of the resource record, nothing else matters
67        ldns_rdf *rdf;
68        rdf = ldns_rr_rdf(rr, 0);
69       
70        // Parse the LOC record and convert it into a CLLocation instance
71        // Sample parsing code at http://tools.ietf.org/html/rfc1876
72       
73        uint8_t version = ldns_rdf_data(rdf)[0];
74       
75        if (version != 0) {
76                //NSLog(@"LOC Record version is not 0. Can't understand it");
77                return self;
78        }
79       
80        // uint8_t size;
81        uint32_t horizontal_precision;
82        uint32_t vertical_precision;
83        uint32_t longitude;
84        uint32_t latitude;
85        CLLocationDistance altitude;
86        CLLocationCoordinate2D coords2D;
87        CLLocationAccuracy h_accuracy;
88        CLLocationAccuracy v_accuracy;
89       
90        uint32_t equator = (uint32_t) ldns_power(2, 31);
91       
92        // size = ldns_rdf_data(rdf)[1]; // CLLocation doesn't use it
93        horizontal_precision = ldns_rdf_data(rdf)[2];
94        vertical_precision = ldns_rdf_data(rdf)[3];
95       
96        latitude = ldns_read_uint32(&ldns_rdf_data(rdf)[4]);
97        longitude = ldns_read_uint32(&ldns_rdf_data(rdf)[8]);
98        altitude = ldns_read_uint32(&ldns_rdf_data(rdf)[12]);
99       
100        if (latitude > equator) { // North (positive)
101                latitude = latitude - equator;
102                coords2D.latitude = 1.0 * (long)latitude / (long)(1000 * 60 * 60);
103        } else { // South (negative)
104                latitude = equator - latitude;
105                coords2D.latitude = -1.0 * (long)latitude / (long)(1000 * 60 * 60);
106        }
107       
108        if (longitude > equator) { // East (positive)
109                longitude = longitude - equator;
110                coords2D.longitude = 1.0 * (long)longitude / (long)(1000 * 60 * 60);
111        } else { // West (negative)
112                longitude = equator - longitude;
113                coords2D.longitude = -1.0 * (long)longitude / (long)(1000 * 60 * 60);
114        }
115       
116        // Fix the other numbers
117        altitude = altitude/100 - 100000;
118        h_accuracy = ((horizontal_precision & 0xf0) >> 4) * pow(10,(horizontal_precision & 0x0f)-2);
119        v_accuracy = ((vertical_precision & 0xf0) >> 4) * pow(10,(vertical_precision & 0x0f)-2);
120       
121        [self initWithCoordinate:coords2D
122                                        altitude:altitude
123                  horizontalAccuracy:h_accuracy
124                        verticalAccuracy:v_accuracy
125                                   timestamp:[NSDate date]];
126               
127        isValid = YES;
128        return self;
129}
130
131@end
Note: See TracBrowser for help on using the browser.
Telnic
Search This Site
Partners
Neustar
ICANN
Main site | WHOIS | Sell .tel | FAQ | Archived Site | About Telnic | Contact Us