-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathSKNetworkConnection.m
More file actions
198 lines (158 loc) · 6.36 KB
/
Copy pathSKNetworkConnection.m
File metadata and controls
198 lines (158 loc) · 6.36 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// SKNetworkConnection.m
// SensingKit
//
// Copyright (c) 2014. Kleomenis Katevas
// Kleomenis Katevas, minos.kat@gmail.com
//
// This file is part of SensingKit-iOS library.
// For more information, please visit https://www.sensingkit.org
//
// SensingKit-iOS is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SensingKit-iOS is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with SensingKit-iOS. If not, see <http://www.gnu.org/licenses/>.
//
#import "SKNetworkConnection.h"
#import "SKNetworkConnectionData.h"
#include <net/if.h>
#include <ifaddrs.h>
@interface SKNetworkConnection ()
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic) SKNetworkDataActivity totalNetworkDataActivity;
@end
@implementation SKNetworkConnection
- (instancetype)initWithConfiguration:(SKNetworkConnectionConfiguration *)configuration
{
if (self = [super init])
{
self.configuration = configuration;
self.totalNetworkDataActivity = (SKNetworkDataActivity){0, 0, 0, 0};
}
return self;
}
#pragma mark Configuration
- (void)setConfiguration:(SKConfiguration *)configuration
{
super.configuration = configuration;
// Cast the configuration instance
// SKNetworkConnectionConfiguration *networkConnectionConfiguration = (SKNetworkConnectionConfiguration *)configuration;
// Make the required updates on the sensor
//
}
#pragma mark Sensing
+ (BOOL)isSensorAvailable
{
// Always available
return YES;
}
- (BOOL)startSensing:(NSError **)error
{
if (![super startSensing:error]) {
return NO;
}
if (![SKNetworkConnection isSensorAvailable])
{
if (error) {
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: NSLocalizedString(@"Network Connection sensor is not available.", nil),
};
*error = [NSError errorWithDomain:SKErrorDomain
code:SKSensorNotAvailableError
userInfo:userInfo];
}
return NO;
}
// Save offset
SKNetworkDataActivity startSensingOffset = [self getNetworkDataSinceDeviceBoot];
// Start sensor
SKNetworkConnectionConfiguration *networkConnectionConfiguration = (SKNetworkConnectionConfiguration *)self.configuration;
NSTimeInterval interval = 1 / networkConnectionConfiguration.sampleRate;
__weak typeof(self) weakSelf = self;
self.timer = [NSTimer scheduledTimerWithTimeInterval:interval repeats:YES block:^(NSTimer * _Nonnull timer) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf) { return; }
// get actual data consumed (since start sensing)
SKNetworkDataActivity currentDataActivity = [strongSelf getNetworkDataWithOffset:startSensingOffset];
// accumulate data
SKNetworkDataActivity totalNetworkDataActivity = strongSelf.totalNetworkDataActivity;
totalNetworkDataActivity.wifiSent += currentDataActivity.wifiSent;
totalNetworkDataActivity.wifiReceived += currentDataActivity.wifiReceived;
totalNetworkDataActivity.cellularSent += currentDataActivity.cellularSent;
totalNetworkDataActivity.cellularReceived += currentDataActivity.cellularReceived;
strongSelf.totalNetworkDataActivity = totalNetworkDataActivity;
SKNetworkConnectionData *data = [[SKNetworkConnectionData alloc] initWithNetworkDataActivity:totalNetworkDataActivity];
[strongSelf submitSensorData:data error:NULL];
}];
return YES;
}
- (BOOL)stopSensing:(NSError **)error
{
[self.timer invalidate];
self.timer = nil;
return [super stopSensing:error];
}
- (SKNetworkDataActivity)getNetworkDataWithOffset:(SKNetworkDataActivity)offset
{
SKNetworkDataActivity networkDataActivity = [self getNetworkDataSinceDeviceBoot];
// apply offset
networkDataActivity.wifiSent -= offset.wifiSent;
networkDataActivity.wifiReceived -= offset.wifiReceived;
networkDataActivity.cellularSent -= offset.cellularSent;
networkDataActivity.cellularReceived -= offset.cellularReceived;
return networkDataActivity;
}
// thanks to:
// https://stackoverflow.com/questions/7946699/iphone-data-usage-tracking-monitoring
- (SKNetworkDataActivity)getNetworkDataSinceDeviceBoot
{
SKNetworkDataActivity networkDataActivity = (SKNetworkDataActivity){0, 0, 0, 0};
struct ifaddrs *addrs;
if (getifaddrs(&addrs) == 0)
{
const struct ifaddrs *cursor = addrs;
while (cursor != NULL)
{
if (cursor->ifa_addr->sa_family == AF_LINK)
{
NSString *name = @(cursor->ifa_name);
if ([name hasPrefix:@"en"]) // WiFi
{
[self accumulateBytesForIfAddress:cursor
intoSent:&networkDataActivity.wifiSent
andReceived:&networkDataActivity.wifiReceived];
}
else if ([name hasPrefix:@"pdp_ip"]) // Cellular
{
[self accumulateBytesForIfAddress:cursor
intoSent:&networkDataActivity.cellularSent
andReceived:&networkDataActivity.cellularReceived];
}
// else, not interested. Ignore.
}
cursor = cursor->ifa_next;
}
freeifaddrs(addrs);
}
return networkDataActivity;
}
- (void)accumulateBytesForIfAddress:(const struct ifaddrs *)ifaddrs
intoSent:(uint64_t *)sent
andReceived:(uint64_t *)received
{
const struct if_data *ifa_data = (struct if_data *)ifaddrs->ifa_data;
if (ifa_data != NULL)
{
*sent += ifa_data->ifi_obytes;
*received += ifa_data->ifi_ibytes;
}
}
@end