001/*
002 * Copyright (C) Cross The Road Electronics.  All rights reserved.
003 * License information can be found in CTRE_LICENSE.txt
004 * For support and suggestions contact support@ctr-electronics.com or file
005 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
006 */
007package com.ctre.phoenix6.configs;
008
009import java.lang.foreign.Arena;
010import java.lang.foreign.ValueLayout;
011
012import static com.ctre.phoenix6.configs.ffi.ConfigNative.*;
013
014import com.ctre.phoenix6.StatusCode;
015import com.ctre.phoenix6.ffi.CtreNativeWrapper;
016import com.ctre.phoenix6.hardware.DeviceIdentifier;
017import com.ctre.phoenix6.spns.*;
018
019/**
020 * Handles applying and refreshing configurations for the
021 * {@link com.ctre.phoenix6.hardware.CANrange}.
022 */
023public class CANrangeConfigurator extends ParentConfigurator {
024    /**
025     * Constructs a configurator for the CANrange.
026     * Users should call {@code getConfigurator()} on the device instead.
027     *
028     * @param id Device identifier of the CANrange
029     */
030    public CANrangeConfigurator(DeviceIdentifier id) {
031        super(id);
032    }
033
034    /**
035     * Applies the contents of the specified config to the device.
036     * <p>
037     * This will wait up to {@link #DefaultTimeoutSeconds}.
038     * <p>
039     * Call to apply the selected configs.
040     *
041     * @param configs Configs to apply against.
042     * @return Status code of applying the configs
043     */
044    public final StatusCode apply(CANrangeConfiguration configs) {
045        return apply(configs, DefaultTimeoutSeconds);
046    }
047
048    /**
049     * Applies the contents of the specified config to the device.
050     * <p>
051     * Call to apply the selected configs.
052     *
053     * @param configs Configs to apply against.
054     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
055     * @return Status code of applying the configs
056     */
057    public final StatusCode apply(CANrangeConfiguration configs, double timeoutSeconds) {
058        return setConfigsPrivate(configs.serialize(), timeoutSeconds, configs.FutureProofConfigs, false);
059    }
060
061    /**
062     * Applies the contents of the specified config to the device.
063     * <p>
064     * This will wait up to {@link #DefaultTimeoutSeconds}.
065     * <p>
066     * Call to apply the selected configs.
067     *
068     * @param configs Configs to apply against.
069     * @return Status code of applying the configs
070     */
071    public final StatusCode apply(CustomParamsConfigs configs) {
072        return apply(configs, DefaultTimeoutSeconds);
073    }
074
075    /**
076     * Applies the contents of the specified config to the device.
077     * <p>
078     * Call to apply the selected configs.
079     *
080     * @param configs Configs to apply against.
081     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
082     * @return Status code of applying the configs
083     */
084    public final StatusCode apply(CustomParamsConfigs configs, double timeoutSeconds) {
085        return setConfigsPrivate(configs.serialize(), timeoutSeconds, false, false);
086    }
087
088    /**
089     * Applies the contents of the specified config to the device.
090     * <p>
091     * This will wait up to {@link #DefaultTimeoutSeconds}.
092     * <p>
093     * Call to apply the selected configs.
094     *
095     * @param configs Configs to apply against.
096     * @return Status code of applying the configs
097     */
098    public final StatusCode apply(ToFParamsConfigs configs) {
099        return apply(configs, DefaultTimeoutSeconds);
100    }
101
102    /**
103     * Applies the contents of the specified config to the device.
104     * <p>
105     * Call to apply the selected configs.
106     *
107     * @param configs Configs to apply against.
108     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
109     * @return Status code of applying the configs
110     */
111    public final StatusCode apply(ToFParamsConfigs configs, double timeoutSeconds) {
112        return setConfigsPrivate(configs.serialize(), timeoutSeconds, false, false);
113    }
114
115    /**
116     * Applies the contents of the specified config to the device.
117     * <p>
118     * This will wait up to {@link #DefaultTimeoutSeconds}.
119     * <p>
120     * Call to apply the selected configs.
121     *
122     * @param configs Configs to apply against.
123     * @return Status code of applying the configs
124     */
125    public final StatusCode apply(ProximityParamsConfigs configs) {
126        return apply(configs, DefaultTimeoutSeconds);
127    }
128
129    /**
130     * Applies the contents of the specified config to the device.
131     * <p>
132     * Call to apply the selected configs.
133     *
134     * @param configs Configs to apply against.
135     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
136     * @return Status code of applying the configs
137     */
138    public final StatusCode apply(ProximityParamsConfigs configs, double timeoutSeconds) {
139        return setConfigsPrivate(configs.serialize(), timeoutSeconds, false, false);
140    }
141
142    /**
143     * Applies the contents of the specified config to the device.
144     * <p>
145     * This will wait up to {@link #DefaultTimeoutSeconds}.
146     * <p>
147     * Call to apply the selected configs.
148     *
149     * @param configs Configs to apply against.
150     * @return Status code of applying the configs
151     */
152    public final StatusCode apply(FovParamsConfigs configs) {
153        return apply(configs, DefaultTimeoutSeconds);
154    }
155
156    /**
157     * Applies the contents of the specified config to the device.
158     * <p>
159     * Call to apply the selected configs.
160     *
161     * @param configs Configs to apply against.
162     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
163     * @return Status code of applying the configs
164     */
165    public final StatusCode apply(FovParamsConfigs configs, double timeoutSeconds) {
166        return setConfigsPrivate(configs.serialize(), timeoutSeconds, false, false);
167    }
168
169    /**
170     * Refreshes the values of the specified config group.
171     * <p>
172     * This will wait up to {@link #DefaultTimeoutSeconds}.
173     * <p>
174     * Call to refresh the selected configs from the device.
175     *
176     * @param configs The configs to refresh
177     * @return Status code of refreshing the configs
178     */
179    public final StatusCode refresh(CANrangeConfiguration configs) {
180        return refresh(configs, DefaultTimeoutSeconds);
181    }
182
183    /**
184     * Refreshes the values of the specified config group.
185     * <p>
186     * Call to refresh the selected configs from the device.
187     *
188     * @param configs The configs to refresh
189     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
190     * @return Status code of refreshing the configs
191     */
192    public final StatusCode refresh(CANrangeConfiguration configs, double timeoutSeconds) {
193        StringBuilder serializedString = new StringBuilder();
194        StatusCode err = getConfigsPrivate(serializedString, timeoutSeconds);
195        if (err == StatusCode.OK) {
196            /* Only deserialize if we successfully got configs */
197            configs.deserialize(serializedString.toString());
198        }
199        return err;
200    }
201
202    /**
203     * Refreshes the values of the specified config group.
204     * <p>
205     * This will wait up to {@link #DefaultTimeoutSeconds}.
206     * <p>
207     * Call to refresh the selected configs from the device.
208     *
209     * @param configs The configs to refresh
210     * @return Status code of refreshing the configs
211     */
212    public final StatusCode refresh(CustomParamsConfigs configs) {
213        return refresh(configs, DefaultTimeoutSeconds);
214    }
215
216    /**
217     * Refreshes the values of the specified config group.
218     * <p>
219     * Call to refresh the selected configs from the device.
220     *
221     * @param configs The configs to refresh
222     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
223     * @return Status code of refreshing the configs
224     */
225    public final StatusCode refresh(CustomParamsConfigs configs, double timeoutSeconds) {
226        StringBuilder serializedString = new StringBuilder();
227        StatusCode err = getConfigsPrivate(serializedString, timeoutSeconds);
228        if (err == StatusCode.OK) {
229            /* Only deserialize if we successfully got configs */
230            configs.deserialize(serializedString.toString());
231        }
232        return err;
233    }
234
235    /**
236     * Refreshes the values of the specified config group.
237     * <p>
238     * This will wait up to {@link #DefaultTimeoutSeconds}.
239     * <p>
240     * Call to refresh the selected configs from the device.
241     *
242     * @param configs The configs to refresh
243     * @return Status code of refreshing the configs
244     */
245    public final StatusCode refresh(ToFParamsConfigs configs) {
246        return refresh(configs, DefaultTimeoutSeconds);
247    }
248
249    /**
250     * Refreshes the values of the specified config group.
251     * <p>
252     * Call to refresh the selected configs from the device.
253     *
254     * @param configs The configs to refresh
255     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
256     * @return Status code of refreshing the configs
257     */
258    public final StatusCode refresh(ToFParamsConfigs configs, double timeoutSeconds) {
259        StringBuilder serializedString = new StringBuilder();
260        StatusCode err = getConfigsPrivate(serializedString, timeoutSeconds);
261        if (err == StatusCode.OK) {
262            /* Only deserialize if we successfully got configs */
263            configs.deserialize(serializedString.toString());
264        }
265        return err;
266    }
267
268    /**
269     * Refreshes the values of the specified config group.
270     * <p>
271     * This will wait up to {@link #DefaultTimeoutSeconds}.
272     * <p>
273     * Call to refresh the selected configs from the device.
274     *
275     * @param configs The configs to refresh
276     * @return Status code of refreshing the configs
277     */
278    public final StatusCode refresh(ProximityParamsConfigs configs) {
279        return refresh(configs, DefaultTimeoutSeconds);
280    }
281
282    /**
283     * Refreshes the values of the specified config group.
284     * <p>
285     * Call to refresh the selected configs from the device.
286     *
287     * @param configs The configs to refresh
288     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
289     * @return Status code of refreshing the configs
290     */
291    public final StatusCode refresh(ProximityParamsConfigs configs, double timeoutSeconds) {
292        StringBuilder serializedString = new StringBuilder();
293        StatusCode err = getConfigsPrivate(serializedString, timeoutSeconds);
294        if (err == StatusCode.OK) {
295            /* Only deserialize if we successfully got configs */
296            configs.deserialize(serializedString.toString());
297        }
298        return err;
299    }
300
301    /**
302     * Refreshes the values of the specified config group.
303     * <p>
304     * This will wait up to {@link #DefaultTimeoutSeconds}.
305     * <p>
306     * Call to refresh the selected configs from the device.
307     *
308     * @param configs The configs to refresh
309     * @return Status code of refreshing the configs
310     */
311    public final StatusCode refresh(FovParamsConfigs configs) {
312        return refresh(configs, DefaultTimeoutSeconds);
313    }
314
315    /**
316     * Refreshes the values of the specified config group.
317     * <p>
318     * Call to refresh the selected configs from the device.
319     *
320     * @param configs The configs to refresh
321     * @param timeoutSeconds Maximum amount of time to wait when performing configuration
322     * @return Status code of refreshing the configs
323     */
324    public final StatusCode refresh(FovParamsConfigs configs, double timeoutSeconds) {
325        StringBuilder serializedString = new StringBuilder();
326        StatusCode err = getConfigsPrivate(serializedString, timeoutSeconds);
327        if (err == StatusCode.OK) {
328            /* Only deserialize if we successfully got configs */
329            configs.deserialize(serializedString.toString());
330        }
331        return err;
332    }
333
334
335    /**
336     * Clear the sticky faults in the device.
337     * <p>
338     * This typically has no impact on the device functionality.  Instead,
339     * it just clears telemetry faults that are accessible via API and
340     * Tuner Self-Test.
341     * <p>
342     * This will wait up to {@link #DefaultTimeoutSeconds}.
343     * <p>
344     * This is available in the configurator in case the user wants
345     * to initialize their device entirely without passing a device
346     * reference down to the code that performs the initialization.
347     * In this case, the user passes down the configurator object
348     * and performs all the initialization code on the object.
349     * 
350     * @return StatusCode of the set command
351     */
352    public final StatusCode clearStickyFaults() {
353        return clearStickyFaults(DefaultTimeoutSeconds);
354    }
355    /**
356     * Clear the sticky faults in the device.
357     * <p>
358     * This typically has no impact on the device functionality.  Instead,
359     * it just clears telemetry faults that are accessible via API and
360     * Tuner Self-Test.
361     * <p>
362     * This is available in the configurator in case the user wants
363     * to initialize their device entirely without passing a device
364     * reference down to the code that performs the initialization.
365     * In this case, the user passes down the configurator object
366     * and performs all the initialization code on the object.
367     * 
368     * @param timeoutSeconds Maximum time to wait up to in seconds.
369     * @return StatusCode of the set command
370     */
371    public final StatusCode clearStickyFaults(double timeoutSeconds) {
372        String serialized;
373        try (final var arena = Arena.ofConfined()) {
374            final var valuePtr = arena.allocate(ValueLayout.ADDRESS);
375
376            c_ctre_phoenix6_serialize_double(SpnValue.SPN_ClearStickyFaults.value, 0.0, valuePtr);
377            final var value = valuePtr.get(CtreNativeWrapper.CHAR_POINTER, 0L);
378            if (value.address() != 0L) {
379                serialized = value.getString(0L);
380                c_ctre_phoenix6_free_memory(value);
381            } else {
382                serialized = "";
383            }
384        }
385        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
386    }
387
388    /**
389     * Clear sticky fault: Hardware fault occurred
390     * <p>
391     * This will wait up to {@link #DefaultTimeoutSeconds}.
392     * <p>
393     * This is available in the configurator in case the user wants
394     * to initialize their device entirely without passing a device
395     * reference down to the code that performs the initialization.
396     * In this case, the user passes down the configurator object
397     * and performs all the initialization code on the object.
398     * 
399     * @return StatusCode of the set command
400     */
401    public final StatusCode clearStickyFault_Hardware() {
402        return clearStickyFault_Hardware(DefaultTimeoutSeconds);
403    }
404    /**
405     * Clear sticky fault: Hardware fault occurred
406     * <p>
407     * This is available in the configurator in case the user wants
408     * to initialize their device entirely without passing a device
409     * reference down to the code that performs the initialization.
410     * In this case, the user passes down the configurator object
411     * and performs all the initialization code on the object.
412     * 
413     * @param timeoutSeconds Maximum time to wait up to in seconds.
414     * @return StatusCode of the set command
415     */
416    public final StatusCode clearStickyFault_Hardware(double timeoutSeconds) {
417        String serialized;
418        try (final var arena = Arena.ofConfined()) {
419            final var valuePtr = arena.allocate(ValueLayout.ADDRESS);
420
421            c_ctre_phoenix6_serialize_double(SpnValue.ClearStickyFault_Hardware.value, 0.0, valuePtr);
422            final var value = valuePtr.get(CtreNativeWrapper.CHAR_POINTER, 0L);
423            if (value.address() != 0L) {
424                serialized = value.getString(0L);
425                c_ctre_phoenix6_free_memory(value);
426            } else {
427                serialized = "";
428            }
429        }
430        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
431    }
432
433    /**
434     * Clear sticky fault: Device supply voltage dropped to near brownout
435     * levels
436     * <p>
437     * This will wait up to {@link #DefaultTimeoutSeconds}.
438     * <p>
439     * This is available in the configurator in case the user wants
440     * to initialize their device entirely without passing a device
441     * reference down to the code that performs the initialization.
442     * In this case, the user passes down the configurator object
443     * and performs all the initialization code on the object.
444     * 
445     * @return StatusCode of the set command
446     */
447    public final StatusCode clearStickyFault_Undervoltage() {
448        return clearStickyFault_Undervoltage(DefaultTimeoutSeconds);
449    }
450    /**
451     * Clear sticky fault: Device supply voltage dropped to near brownout
452     * levels
453     * <p>
454     * This is available in the configurator in case the user wants
455     * to initialize their device entirely without passing a device
456     * reference down to the code that performs the initialization.
457     * In this case, the user passes down the configurator object
458     * and performs all the initialization code on the object.
459     * 
460     * @param timeoutSeconds Maximum time to wait up to in seconds.
461     * @return StatusCode of the set command
462     */
463    public final StatusCode clearStickyFault_Undervoltage(double timeoutSeconds) {
464        String serialized;
465        try (final var arena = Arena.ofConfined()) {
466            final var valuePtr = arena.allocate(ValueLayout.ADDRESS);
467
468            c_ctre_phoenix6_serialize_double(SpnValue.ClearStickyFault_Undervoltage.value, 0.0, valuePtr);
469            final var value = valuePtr.get(CtreNativeWrapper.CHAR_POINTER, 0L);
470            if (value.address() != 0L) {
471                serialized = value.getString(0L);
472                c_ctre_phoenix6_free_memory(value);
473            } else {
474                serialized = "";
475            }
476        }
477        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
478    }
479
480    /**
481     * Clear sticky fault: Device boot while detecting the enable signal
482     * <p>
483     * This will wait up to {@link #DefaultTimeoutSeconds}.
484     * <p>
485     * This is available in the configurator in case the user wants
486     * to initialize their device entirely without passing a device
487     * reference down to the code that performs the initialization.
488     * In this case, the user passes down the configurator object
489     * and performs all the initialization code on the object.
490     * 
491     * @return StatusCode of the set command
492     */
493    public final StatusCode clearStickyFault_BootDuringEnable() {
494        return clearStickyFault_BootDuringEnable(DefaultTimeoutSeconds);
495    }
496    /**
497     * Clear sticky fault: Device boot while detecting the enable signal
498     * <p>
499     * This is available in the configurator in case the user wants
500     * to initialize their device entirely without passing a device
501     * reference down to the code that performs the initialization.
502     * In this case, the user passes down the configurator object
503     * and performs all the initialization code on the object.
504     * 
505     * @param timeoutSeconds Maximum time to wait up to in seconds.
506     * @return StatusCode of the set command
507     */
508    public final StatusCode clearStickyFault_BootDuringEnable(double timeoutSeconds) {
509        String serialized;
510        try (final var arena = Arena.ofConfined()) {
511            final var valuePtr = arena.allocate(ValueLayout.ADDRESS);
512
513            c_ctre_phoenix6_serialize_double(SpnValue.ClearStickyFault_BootDuringEnable.value, 0.0, valuePtr);
514            final var value = valuePtr.get(CtreNativeWrapper.CHAR_POINTER, 0L);
515            if (value.address() != 0L) {
516                serialized = value.getString(0L);
517                c_ctre_phoenix6_free_memory(value);
518            } else {
519                serialized = "";
520            }
521        }
522        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
523    }
524
525    /**
526     * Clear sticky fault: An unlicensed feature is in use, device may not
527     * behave as expected.
528     * <p>
529     * This will wait up to {@link #DefaultTimeoutSeconds}.
530     * <p>
531     * This is available in the configurator in case the user wants
532     * to initialize their device entirely without passing a device
533     * reference down to the code that performs the initialization.
534     * In this case, the user passes down the configurator object
535     * and performs all the initialization code on the object.
536     * 
537     * @return StatusCode of the set command
538     */
539    public final StatusCode clearStickyFault_UnlicensedFeatureInUse() {
540        return clearStickyFault_UnlicensedFeatureInUse(DefaultTimeoutSeconds);
541    }
542    /**
543     * Clear sticky fault: An unlicensed feature is in use, device may not
544     * behave as expected.
545     * <p>
546     * This is available in the configurator in case the user wants
547     * to initialize their device entirely without passing a device
548     * reference down to the code that performs the initialization.
549     * In this case, the user passes down the configurator object
550     * and performs all the initialization code on the object.
551     * 
552     * @param timeoutSeconds Maximum time to wait up to in seconds.
553     * @return StatusCode of the set command
554     */
555    public final StatusCode clearStickyFault_UnlicensedFeatureInUse(double timeoutSeconds) {
556        String serialized;
557        try (final var arena = Arena.ofConfined()) {
558            final var valuePtr = arena.allocate(ValueLayout.ADDRESS);
559
560            c_ctre_phoenix6_serialize_double(SpnValue.ClearStickyFault_UnlicensedFeatureInUse.value, 0.0, valuePtr);
561            final var value = valuePtr.get(CtreNativeWrapper.CHAR_POINTER, 0L);
562            if (value.address() != 0L) {
563                serialized = value.getString(0L);
564                c_ctre_phoenix6_free_memory(value);
565            } else {
566                serialized = "";
567            }
568        }
569        return setConfigsPrivate(serialized, timeoutSeconds, false, true);
570    }
571}