2 * Copyright (c) 2018, 2020 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 */
48 STATIC AWDServerConnection *
49 IPMonitorAWDServerConnection(void)
51 AWDServerConnection * server;
53 if ([AWDServerConnection class] == nil) {
56 server = [[AWDServerConnection alloc]
57 initWithComponentId:AWDComponentId_IPMonitor];
59 my_log(LOG_NOTICE, "Failed to create AWD server connection");
64 STATIC InterfaceAdvisoryReportRef
65 _InterfaceAdvisoryReportCreate(AWDIPMonitorInterfaceType type)
67 AWDIPMonitorInterfaceAdvisoryReport * metric;
69 if ([AWDServerConnection class] == nil) {
72 metric = [[AWDIPMonitorInterfaceAdvisoryReport alloc] init];
73 metric.interfaceType = type;
75 /* default to zero values */
77 metric.advisoryCount = 0;
79 return ((InterfaceAdvisoryReportRef)metric);
82 PRIVATE_EXTERN InterfaceAdvisoryReportRef
83 InterfaceAdvisoryReportCreate(AWDIPMonitorInterfaceType type)
85 InterfaceAdvisoryReportRef report;
88 report = _InterfaceAdvisoryReportCreate(type);
94 _InterfaceAdvisoryReportSubmit(InterfaceAdvisoryReportRef report)
96 AWDMetricContainer * container;
97 AWDServerConnection * server;
99 server = IPMonitorAWDServerConnection();
100 if (server == NULL) {
103 container = [server newMetricContainerWithIdentifier:
104 AWDMetricId_IPMonitor_InterfaceAdvisoryReport];
105 [container setMetric:(AWDIPMonitorInterfaceAdvisoryReport *)report];
106 [server submitMetric:container];
113 InterfaceAdvisoryReportSubmit(InterfaceAdvisoryReportRef report)
116 _InterfaceAdvisoryReportSubmit(report);
120 #define INTERFACE_ADVISORY_REPORT_SET_PROP(report, name, value) \
122 AWDIPMonitorInterfaceAdvisoryReport * metric; \
124 metric = (AWDIPMonitorInterfaceAdvisoryReport *)report; \
125 metric.name = value; \
129 InterfaceAdvisoryReportSetFlags(InterfaceAdvisoryReportRef report,
130 AWDIPMonitorInterfaceAdvisoryReport_Flags flags)
132 INTERFACE_ADVISORY_REPORT_SET_PROP(report, flags, flags);
136 InterfaceAdvisoryReportSetAdvisoryCount(InterfaceAdvisoryReportRef report,
139 INTERFACE_ADVISORY_REPORT_SET_PROP(report, advisoryCount, count);
142 #ifdef TEST_IPMONITOR_AWD_REPORT
145 main(int argc, char * argv[])
147 InterfaceAdvisoryReportRef report;
148 AWDIPMonitorInterfaceType type;
150 type = AWDIPMonitorInterfaceType_IPMONITOR_INTERFACE_TYPE_WIFI;
151 report = InterfaceAdvisoryReportCreate(type);
152 if (report == NULL) {
153 fprintf(stderr, "WirelessDiagnostics framework not available\n");
156 printf("Before setting values:\n");
162 InterfaceAdvisoryReportSetFlags(report,
163 AWDIPMonitorInterfaceAdvisoryReport_Flags_LINK_LAYER_ISSUE
164 | AWDIPMonitorInterfaceAdvisoryReport_Flags_UPLINK_ISSUE);
165 InterfaceAdvisoryReportSetAdvisoryCount(report, 2);
167 printf("After setting values:\n");
171 InterfaceAdvisoryReportSubmit(report);
174 fprintf(stderr, "pid is %d\n", getpid());
179 #endif /* TEST_IPMONITOR_AWD_REPORT */