@tx5dr/contracts 1.7.7 → 1.7.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -100,6 +100,136 @@ export const PermissionGrantSchema = z.object({
100
100
 
101
101
  export type PermissionGrant = z.infer<typeof PermissionGrantSchema>;
102
102
 
103
+ // ===== Frequency permission helpers =====
104
+
105
+ export interface FrequencyPermissionRange {
106
+ band?: string;
107
+ minFrequency: number;
108
+ maxFrequency: number;
109
+ }
110
+
111
+ export const FREQUENCY_PERMISSION_BAND_RANGES: Record<string, FrequencyPermissionRange> = {
112
+ '160m': { band: '160m', minFrequency: 1_800_000, maxFrequency: 2_000_000 },
113
+ '80m': { band: '80m', minFrequency: 3_500_000, maxFrequency: 4_000_000 },
114
+ '60m': { band: '60m', minFrequency: 5_000_000, maxFrequency: 5_500_000 },
115
+ '40m': { band: '40m', minFrequency: 7_000_000, maxFrequency: 7_300_000 },
116
+ '30m': { band: '30m', minFrequency: 10_100_000, maxFrequency: 10_150_000 },
117
+ '20m': { band: '20m', minFrequency: 14_000_000, maxFrequency: 14_350_000 },
118
+ '17m': { band: '17m', minFrequency: 18_068_000, maxFrequency: 18_168_000 },
119
+ '15m': { band: '15m', minFrequency: 21_000_000, maxFrequency: 21_450_000 },
120
+ '12m': { band: '12m', minFrequency: 24_890_000, maxFrequency: 24_990_000 },
121
+ '10m': { band: '10m', minFrequency: 28_000_000, maxFrequency: 29_700_000 },
122
+ '6m': { band: '6m', minFrequency: 50_000_000, maxFrequency: 54_000_000 },
123
+ '2m': { band: '2m', minFrequency: 144_000_000, maxFrequency: 148_000_000 },
124
+ '70cm': { band: '70cm', minFrequency: 420_000_000, maxFrequency: 450_000_000 },
125
+ };
126
+
127
+ function getFrequencyCondition(grant: PermissionGrant): Record<string, unknown> | null {
128
+ if (grant.permission !== Permission.RADIO_SET_FREQUENCY) return null;
129
+ const frequency = grant.conditions?.frequency;
130
+ if (!frequency || typeof frequency !== 'object' || Array.isArray(frequency)) return null;
131
+ return frequency as Record<string, unknown>;
132
+ }
133
+
134
+ function normalizeFrequency(value: unknown): number | null {
135
+ return typeof value === 'number' && Number.isFinite(value) && value > 0
136
+ ? Math.round(value)
137
+ : null;
138
+ }
139
+
140
+ function requireFrequency(value: unknown, label: string): number {
141
+ const frequency = normalizeFrequency(value);
142
+ if (frequency === null) {
143
+ throw new Error(`${label} must be a finite positive frequency in Hz`);
144
+ }
145
+ return frequency;
146
+ }
147
+
148
+ export function getPresetFrequenciesFromFrequencyGrants(grants: PermissionGrant[] | undefined): number[] {
149
+ const result: number[] = [];
150
+ const seen = new Set<number>();
151
+
152
+ for (const grant of grants ?? []) {
153
+ const condition = getFrequencyCondition(grant);
154
+ const values = condition?.$in;
155
+ if (!Array.isArray(values)) continue;
156
+
157
+ for (const value of values) {
158
+ const frequency = normalizeFrequency(value);
159
+ if (frequency !== null && !seen.has(frequency)) {
160
+ seen.add(frequency);
161
+ result.push(frequency);
162
+ }
163
+ }
164
+ }
165
+
166
+ return result;
167
+ }
168
+
169
+ export function getRangesFromFrequencyGrants(grants: PermissionGrant[] | undefined): FrequencyPermissionRange[] {
170
+ const result: FrequencyPermissionRange[] = [];
171
+
172
+ for (const grant of grants ?? []) {
173
+ const condition = getFrequencyCondition(grant);
174
+ const minFrequency = normalizeFrequency(condition?.$gte);
175
+ const maxFrequency = normalizeFrequency(condition?.$lte);
176
+ if (minFrequency === null || maxFrequency === null || minFrequency > maxFrequency) continue;
177
+
178
+ result.push({
179
+ minFrequency,
180
+ maxFrequency,
181
+ band: inferFrequencyPermissionBand(minFrequency, maxFrequency),
182
+ });
183
+ }
184
+
185
+ return result;
186
+ }
187
+
188
+ export function inferFrequencyPermissionBand(minFrequency: number, maxFrequency: number): string | undefined {
189
+ const entry = Object.entries(FREQUENCY_PERMISSION_BAND_RANGES)
190
+ .find(([, range]) => range.minFrequency === minFrequency && range.maxFrequency === maxFrequency);
191
+ return entry?.[0];
192
+ }
193
+
194
+ export function buildRadioFrequencyPermissionGrants(
195
+ presetFrequencies: number[],
196
+ ranges: FrequencyPermissionRange[],
197
+ ): PermissionGrant[] {
198
+ const grants: PermissionGrant[] = [];
199
+ const normalizedPresetFrequencies = [...new Set(presetFrequencies.map((value, index) => requireFrequency(value, `presetFrequencies[${index}]`)))];
200
+
201
+ if (normalizedPresetFrequencies.length > 0) {
202
+ grants.push({
203
+ permission: Permission.RADIO_SET_FREQUENCY,
204
+ conditions: {
205
+ frequency: { $in: normalizedPresetFrequencies },
206
+ },
207
+ });
208
+ }
209
+
210
+ for (const [index, range] of ranges.entries()) {
211
+ const minFrequency = requireFrequency(range.minFrequency, `ranges[${index}].minFrequency`);
212
+ const maxFrequency = requireFrequency(range.maxFrequency, `ranges[${index}].maxFrequency`);
213
+ if (minFrequency > maxFrequency) {
214
+ throw new Error(`ranges[${index}] minFrequency must be less than or equal to maxFrequency`);
215
+ }
216
+
217
+ grants.push({
218
+ permission: Permission.RADIO_SET_FREQUENCY,
219
+ conditions: {
220
+ frequency: {
221
+ $gte: minFrequency,
222
+ $lte: maxFrequency,
223
+ },
224
+ },
225
+ });
226
+ }
227
+
228
+ return grants.length > 0
229
+ ? grants
230
+ : [{ permission: Permission.RADIO_SET_FREQUENCY }];
231
+ }
232
+
103
233
  // ===== Raw rule type (pure data, no CASL dependency) =====
104
234
 
105
235
  export interface RawRule {
@@ -38,6 +38,7 @@ export type PluginInstanceScope = z.infer<typeof PluginInstanceScopeSchema>;
38
38
  */
39
39
  export const PluginPermissionSchema = z.enum([
40
40
  'network',
41
+ 'host:hamlib',
41
42
  'radio:read',
42
43
  'radio:control',
43
44
  'radio:power',