]> git.saurik.com Git - apple/configd.git/blob - IPMonitorControl/IPMonitorAWDReport.m
configd-1109.101.1.tar.gz
[apple/configd.git] / IPMonitorControl / IPMonitorAWDReport.m
1 /*
2 * Copyright (c) 2018, 2020 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * IPMonitorAWDReport.m
26 * - C shim layer to interact with AWD to generate and submit a metric
27 */
28
29
30 #import <WirelessDiagnostics/WirelessDiagnostics.h>
31 #import "AWDMetricIds_IPMonitor.h"
32 #import "AWDIPMonitorInterfaceAdvisoryReport.h"
33 #import "IPMonitorAWDReport.h"
34 #import <SystemConfiguration/SCPrivate.h>
35
36 #include "symbol_scope.h"
37
38 #if defined(TEST_IPMONITOR_AWD_REPORT) || defined(TEST_IPMONITOR_CONTROL)
39
40 #define my_log(__level, __format, ...) SCPrint(TRUE, stdout, CFSTR(__format "\n"), ## __VA_ARGS__)
41
42 #else /* TEST_IPMONITOR_AWD_REPORT || TEST_IPMONITOR_CONTROL */
43
44 #define my_log(__level, __format, ...) SC_log(__level, __format, ## __VA_ARGS__)
45
46 #endif /* TEST_IPMONITOR_AWD_REPORT || TEST_IPMONITOR_CONTROL */
47
48 STATIC AWDServerConnection *
49 IPMonitorAWDServerConnection(void)
50 {
51 AWDServerConnection * server;
52
53 if ([AWDServerConnection class] == nil) {
54 return (nil);
55 }
56 server = [[AWDServerConnection alloc]
57 initWithComponentId:AWDComponentId_IPMonitor];
58 if (server == NULL) {
59 my_log(LOG_NOTICE, "Failed to create AWD server connection");
60 }
61 return (server);
62 }
63
64 STATIC InterfaceAdvisoryReportRef
65 _InterfaceAdvisoryReportCreate(AWDIPMonitorInterfaceType type)
66 {
67 AWDIPMonitorInterfaceAdvisoryReport * metric;
68
69 if ([AWDServerConnection class] == nil) {
70 return (NULL);
71 }
72 metric = [[AWDIPMonitorInterfaceAdvisoryReport alloc] init];
73 metric.interfaceType = type;
74
75 /* default to zero values */
76 metric.flags = 0;
77 metric.advisoryCount = 0;
78
79 return ((InterfaceAdvisoryReportRef)metric);
80 }
81
82 PRIVATE_EXTERN InterfaceAdvisoryReportRef
83 InterfaceAdvisoryReportCreate(AWDIPMonitorInterfaceType type)
84 {
85 InterfaceAdvisoryReportRef report;
86
87 @autoreleasepool {
88 report = _InterfaceAdvisoryReportCreate(type);
89 }
90 return (report);
91 }
92
93 STATIC void
94 _InterfaceAdvisoryReportSubmit(InterfaceAdvisoryReportRef report)
95 {
96 AWDMetricContainer * container;
97 AWDServerConnection * server;
98
99 server = IPMonitorAWDServerConnection();
100 if (server == NULL) {
101 return;
102 }
103 container = [server newMetricContainerWithIdentifier:
104 AWDMetricId_IPMonitor_InterfaceAdvisoryReport];
105 [container setMetric:(AWDIPMonitorInterfaceAdvisoryReport *)report];
106 [server submitMetric:container];
107 [server release];
108 [container release];
109 return;
110 }
111
112 PRIVATE_EXTERN void
113 InterfaceAdvisoryReportSubmit(InterfaceAdvisoryReportRef report)
114 {
115 @autoreleasepool {
116 _InterfaceAdvisoryReportSubmit(report);
117 }
118 }
119
120 #define INTERFACE_ADVISORY_REPORT_SET_PROP(report, name, value) \
121 @autoreleasepool { \
122 AWDIPMonitorInterfaceAdvisoryReport * metric; \
123 \
124 metric = (AWDIPMonitorInterfaceAdvisoryReport *)report; \
125 metric.name = value; \
126 }
127
128 PRIVATE_EXTERN void
129 InterfaceAdvisoryReportSetFlags(InterfaceAdvisoryReportRef report,
130 AWDIPMonitorInterfaceAdvisoryReport_Flags flags)
131 {
132 INTERFACE_ADVISORY_REPORT_SET_PROP(report, flags, flags);
133 }
134
135 PRIVATE_EXTERN void
136 InterfaceAdvisoryReportSetAdvisoryCount(InterfaceAdvisoryReportRef report,
137 uint32_t count)
138 {
139 INTERFACE_ADVISORY_REPORT_SET_PROP(report, advisoryCount, count);
140 }
141
142 #ifdef TEST_IPMONITOR_AWD_REPORT
143
144 int
145 main(int argc, char * argv[])
146 {
147 InterfaceAdvisoryReportRef report;
148 AWDIPMonitorInterfaceType type;
149
150 type = AWDIPMonitorInterfaceType_IPMONITOR_INTERFACE_TYPE_WIFI;
151 report = InterfaceAdvisoryReportCreate(type);
152 if (report == NULL) {
153 fprintf(stderr, "WirelessDiagnostics framework not available\n");
154 exit(1);
155 }
156 printf("Before setting values:\n");
157 CFShow(report);
158 fflush(stdout);
159
160
161 /* set values */
162 InterfaceAdvisoryReportSetFlags(report,
163 AWDIPMonitorInterfaceAdvisoryReport_Flags_LINK_LAYER_ISSUE
164 | AWDIPMonitorInterfaceAdvisoryReport_Flags_UPLINK_ISSUE);
165 InterfaceAdvisoryReportSetAdvisoryCount(report, 2);
166
167 printf("After setting values:\n");
168 CFShow(report);
169 fflush(stdout);
170
171 InterfaceAdvisoryReportSubmit(report);
172 CFRelease(report);
173 if (argc > 1) {
174 fprintf(stderr, "pid is %d\n", getpid());
175 sleep(120);
176 }
177 }
178
179 #endif /* TEST_IPMONITOR_AWD_REPORT */