UNPKG

4.51 kBPlain TextView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#import "RNCConnectionState.h"
9#if !TARGET_OS_TV
10#import <CoreTelephony/CTTelephonyNetworkInfo.h>
11#endif
12
13#if TARGET_OS_TV || TARGET_OS_OSX
14#include <ifaddrs.h>
15#endif
16
17@implementation RNCConnectionState
18
19// Creates a new "blank" state
20- (instancetype)init
21{
22 self = [super init];
23 if (self) {
24 _type = RNCConnectionTypeUnknown;
25 _cellularGeneration = nil;
26 _expensive = false;
27 }
28 return self;
29}
30
31// Creates the state from the given reachability references
32- (instancetype)initWithReachabilityFlags:(SCNetworkReachabilityFlags)flags
33{
34 self = [self init];
35 if (self) {
36 _type = RNCConnectionTypeUnknown;
37 _cellularGeneration = nil;
38 _expensive = false;
39
40 if ((flags & kSCNetworkReachabilityFlagsReachable) == 0 ||
41 (flags & kSCNetworkReachabilityFlagsConnectionRequired) != 0) {
42 _type = RNCConnectionTypeNone;
43 }
44#if !TARGET_OS_TV && !TARGET_OS_OSX
45 else if ((flags & kSCNetworkReachabilityFlagsIsWWAN) != 0) {
46 _type = RNCConnectionTypeCellular;
47 _expensive = true;
48
49 CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init];
50 if (netinfo) {
51 if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS] ||
52 [netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge] ||
53 [netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
54 _cellularGeneration = RNCCellularGeneration2g;
55 } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA] ||
56 [netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA] ||
57 [netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA] ||
58 [netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0] ||
59 [netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA] ||
60 [netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB] ||
61 [netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) {
62 _cellularGeneration = RNCCellularGeneration3g;
63 } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
64 _cellularGeneration = RNCCellularGeneration4g;
65 }
66 }
67 }
68#endif
69 else {
70 _type = RNCConnectionTypeWifi;
71#if TARGET_OS_TV || TARGET_OS_OSX
72 struct ifaddrs *interfaces = NULL;
73 struct ifaddrs *temp_addr = NULL;
74 int success = 0;
75 // retrieve the current interfaces - returns 0 on success
76 success = getifaddrs(&interfaces);
77 if (success == 0) {
78 // Loop through linked list of interfaces
79 temp_addr = interfaces;
80 while (temp_addr != NULL) {
81 if (temp_addr->ifa_addr->sa_family == AF_INET) {
82 // Check if interface is en0 which is the ethernet connection on the Apple TV
83 NSString* ifname = [NSString stringWithUTF8String:temp_addr->ifa_name];
84 if ([ifname isEqualToString:@"en0"]) {
85 _type = RNCConnectionTypeEthernet;
86 }
87 }
88 temp_addr = temp_addr->ifa_next;
89 }
90 }
91 // Free memory
92 freeifaddrs(interfaces);
93#endif
94 }
95 }
96 return self;
97}
98
99// Checks if two states are equal
100- (BOOL)isEqualToConnectionState:(RNCConnectionState *)otherState
101{
102 return [self.type isEqualToString:otherState.type]
103 && [self.cellularGeneration isEqualToString:otherState.cellularGeneration]
104 && self.expensive == otherState.expensive;
105}
106
107- (BOOL)connected
108{
109 return ![self.type isEqualToString:RNCConnectionTypeNone] && ![self.type isEqualToString:RNCConnectionTypeUnknown];
110}
111
112@end