2 * Copyright (c) 2018 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * IPMonitorAWDReport.m
26 * - C shim layer to interact with AWD to generate and submit a metric
30 #import <WirelessDiagnostics/WirelessDiagnostics.h>
31 #import "AWDMetricIds_IPMonitor.h"
32 #import "AWDIPMonitorInterfaceAdvisoryReport.h"
33 #import "IPMonitorAWDReport.h"
34 #import <SystemConfiguration/SCPrivate.h>
36 #include "symbol_scope.h"
38 #if defined(TEST_IPMONITOR_AWD_REPORT) || defined(TEST_IPMONITOR_CONTROL)
40 #define my_log(__level, __format, ...) SCPrint(TRUE, stdout, CFSTR(__format "\n"), ## __VA_ARGS__)
42 #else /* TEST_IPMONITOR_AWD_REPORT || TEST_IPMONITOR_CONTROL */
44 #define my_log(__level, __format, ...) SC_log(__level, __format, ## __VA_ARGS__)
46 #endif /* TEST_IPMONITOR_AWD_REPORT || TEST_IPMONITOR_CONTROL */
49 STATIC AWDServerConnection *
50 IPMonitorAWDServerConnection(void)
52 AWDServerConnection * server;
54 if ([AWDServerConnection class] == nil) {
57 server = [[AWDServerConnection alloc]
58 initWithComponentId:AWDComponentId_IPMonitor];
60 my_log(LOG_NOTICE, "Failed to create AWD server connection");
65 STATIC InterfaceAdvisoryReportRef
66 _InterfaceAdvisoryReportCreate(AWDIPMonitorInterfaceType type)
68 AWDIPMonitorInterfaceAdvisoryReport * metric;
70 if ([AWDServerConnection class] == nil) {
73 metric = [[AWDIPMonitorInterfaceAdvisoryReport alloc] init];
74 metric.interfaceType = type;
76 /* default to zero values */
78 metric.advisoryCount = 0;
80 return ((InterfaceAdvisoryReportRef)metric);
83 PRIVATE_EXTERN InterfaceAdvisoryReportRef
84 InterfaceAdvisoryReportCreate(AWDIPMonitorInterfaceType type)
86 InterfaceAdvisoryReportRef report;
89 report = _InterfaceAdvisoryReportCreate(type);
95 _InterfaceAdvisoryReportSubmit(InterfaceAdvisoryReportRef report)
97 AWDMetricContainer * container;
98 AWDServerConnection * server;
100 server = IPMonitorAWDServerConnection();
101 if (server == NULL) {
104 container = [server newMetricContainerWithIdentifier:
105 AWDMetricId_IPMonitor_InterfaceAdvisoryReport];
106 [container setMetric:(AWDIPMonitorInterfaceAdvisoryReport *)report];
107 [server submitMetric:container];
114 InterfaceAdvisoryReportSubmit(InterfaceAdvisoryReportRef report)
117 _InterfaceAdvisoryReportSubmit(report);
121 #define INTERFACE_ADVISORY_REPORT_SET_PROP(report, name, value) \
123 AWDIPMonitorInterfaceAdvisoryReport * metric; \
125 metric = (AWDIPMonitorInterfaceAdvisoryReport *)report; \
126 metric.name = value; \
130 InterfaceAdvisoryReportSetFlags(InterfaceAdvisoryReportRef report,
131 AWDIPMonitorInterfaceAdvisoryReport_Flags flags)
133 INTERFACE_ADVISORY_REPORT_SET_PROP(report, flags, flags);
137 InterfaceAdvisoryReportSetAdvisoryCount(InterfaceAdvisoryReportRef report,
140 INTERFACE_ADVISORY_REPORT_SET_PROP(report, advisoryCount, count);
143 #ifdef TEST_IPMONITOR_AWD_REPORT
146 main(int argc, char * argv[])
148 InterfaceAdvisoryReportRef report;
149 AWDIPMonitorInterfaceType type;
151 type = AWDIPMonitorInterfaceType_IPMONITOR_INTERFACE_TYPE_WIFI;
152 report = InterfaceAdvisoryReportCreate(type);
153 if (report == NULL) {
154 fprintf(stderr, "WirelessDiagnostics framework not available\n");
157 printf("Before setting values:\n");
163 InterfaceAdvisoryReportSetFlags(report,
164 AWDIPMonitorInterfaceAdvisoryReport_Flags_LINK_LAYER_ISSUE
165 | AWDIPMonitorInterfaceAdvisoryReport_Flags_UPLINK_ISSUE);
166 InterfaceAdvisoryReportSetAdvisoryCount(report, 2);
168 printf("After setting values:\n");
172 InterfaceAdvisoryReportSubmit(report);
175 fprintf(stderr, "pid is %d\n", getpid());
180 #endif /* TEST_IPMONITOR_AWD_REPORT */