this repo has no description
1/*
2 * Copyright (c) 2018 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
49STATIC AWDServerConnection *
50IPMonitorAWDServerConnection(void)
51{
52 AWDServerConnection * server;
53
54 if ([AWDServerConnection class] == nil) {
55 return (nil);
56 }
57 server = [[AWDServerConnection alloc]
58 initWithComponentId:AWDComponentId_IPMonitor];
59 if (server == NULL) {
60 my_log(LOG_NOTICE, "Failed to create AWD server connection");
61 }
62 return (server);
63}
64
65STATIC InterfaceAdvisoryReportRef
66_InterfaceAdvisoryReportCreate(AWDIPMonitorInterfaceType type)
67{
68 AWDIPMonitorInterfaceAdvisoryReport * metric;
69
70 if ([AWDServerConnection class] == nil) {
71 return (NULL);
72 }
73 metric = [[AWDIPMonitorInterfaceAdvisoryReport alloc] init];
74 metric.interfaceType = type;
75
76 /* default to zero values */
77 metric.flags = 0;
78 metric.advisoryCount = 0;
79
80 return ((InterfaceAdvisoryReportRef)metric);
81}
82
83PRIVATE_EXTERN InterfaceAdvisoryReportRef
84InterfaceAdvisoryReportCreate(AWDIPMonitorInterfaceType type)
85{
86 InterfaceAdvisoryReportRef report;
87
88 @autoreleasepool {
89 report = _InterfaceAdvisoryReportCreate(type);
90 }
91 return (report);
92}
93
94STATIC void
95_InterfaceAdvisoryReportSubmit(InterfaceAdvisoryReportRef report)
96{
97 AWDMetricContainer * container;
98 AWDServerConnection * server;
99
100 server = IPMonitorAWDServerConnection();
101 if (server == NULL) {
102 return;
103 }
104 container = [server newMetricContainerWithIdentifier:
105 AWDMetricId_IPMonitor_InterfaceAdvisoryReport];
106 [container setMetric:(AWDIPMonitorInterfaceAdvisoryReport *)report];
107 [server submitMetric:container];
108 [server release];
109 [container release];
110 return;
111}
112
113PRIVATE_EXTERN void
114InterfaceAdvisoryReportSubmit(InterfaceAdvisoryReportRef report)
115{
116 @autoreleasepool {
117 _InterfaceAdvisoryReportSubmit(report);
118 }
119}
120
121#define INTERFACE_ADVISORY_REPORT_SET_PROP(report, name, value) \
122 @autoreleasepool { \
123 AWDIPMonitorInterfaceAdvisoryReport * metric; \
124 \
125 metric = (AWDIPMonitorInterfaceAdvisoryReport *)report; \
126 metric.name = value; \
127 }
128
129void
130InterfaceAdvisoryReportSetFlags(InterfaceAdvisoryReportRef report,
131 AWDIPMonitorInterfaceAdvisoryReport_Flags flags)
132{
133 INTERFACE_ADVISORY_REPORT_SET_PROP(report, flags, flags);
134}
135
136void
137InterfaceAdvisoryReportSetAdvisoryCount(InterfaceAdvisoryReportRef report,
138 uint32_t count)
139{
140 INTERFACE_ADVISORY_REPORT_SET_PROP(report, advisoryCount, count);
141}
142
143#ifdef TEST_IPMONITOR_AWD_REPORT
144
145int
146main(int argc, char * argv[])
147{
148 InterfaceAdvisoryReportRef report;
149 AWDIPMonitorInterfaceType type;
150
151 type = AWDIPMonitorInterfaceType_IPMONITOR_INTERFACE_TYPE_WIFI;
152 report = InterfaceAdvisoryReportCreate(type);
153 if (report == NULL) {
154 fprintf(stderr, "WirelessDiagnostics framework not available\n");
155 exit(1);
156 }
157 printf("Before setting values:\n");
158 CFShow(report);
159 fflush(stdout);
160
161
162 /* set values */
163 InterfaceAdvisoryReportSetFlags(report,
164 AWDIPMonitorInterfaceAdvisoryReport_Flags_LINK_LAYER_ISSUE
165 | AWDIPMonitorInterfaceAdvisoryReport_Flags_UPLINK_ISSUE);
166 InterfaceAdvisoryReportSetAdvisoryCount(report, 2);
167
168 printf("After setting values:\n");
169 CFShow(report);
170 fflush(stdout);
171
172 InterfaceAdvisoryReportSubmit(report);
173 CFRelease(report);
174 if (argc > 1) {
175 fprintf(stderr, "pid is %d\n", getpid());
176 sleep(120);
177 }
178}
179
180#endif /* TEST_IPMONITOR_AWD_REPORT */