1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //! NP Rust C FFI functionality common to V0 ser/deser flows.
16
17 use crate::{panic, panic_if_invalid, unwrap, PanicReason};
18 use np_ffi_core::serialize::AdvertisementBuilderKind;
19 use np_ffi_core::utils::FfiEnum;
20 use np_ffi_core::v0::*;
21
22 /// Gets the tag of a `V0DataElement` tagged-union.
23 #[no_mangle]
np_ffi_V0DataElement_kind(de: V0DataElement) -> V0DataElementKind24 pub extern "C" fn np_ffi_V0DataElement_kind(de: V0DataElement) -> V0DataElementKind {
25 de.kind()
26 }
27
28 /// Casts a `V0DataElement` to the `TxPower` variant, panicking in the
29 /// case where the passed value is of a different enum variant.
30 #[no_mangle]
np_ffi_V0DataElement_into_TX_POWER(de: V0DataElement) -> TxPower31 pub extern "C" fn np_ffi_V0DataElement_into_TX_POWER(de: V0DataElement) -> TxPower {
32 unwrap(de.into_tx_power(), PanicReason::EnumCastFailed)
33 }
34
35 /// Upcasts a Tx power DE to a generic V0 data-element.
36 #[no_mangle]
np_ffi_TxPower_into_V0DataElement(tx_power: TxPower) -> V0DataElement37 pub extern "C" fn np_ffi_TxPower_into_V0DataElement(tx_power: TxPower) -> V0DataElement {
38 V0DataElement::TxPower(tx_power)
39 }
40
41 /// Casts a `V0DataElement` to the `Actions` variant, panicking in the
42 /// case where the passed value is of a different enum variant.
43 #[no_mangle]
np_ffi_V0DataElement_into_ACTIONS(de: V0DataElement) -> V0Actions44 pub extern "C" fn np_ffi_V0DataElement_into_ACTIONS(de: V0DataElement) -> V0Actions {
45 unwrap(de.into_actions(), PanicReason::EnumCastFailed)
46 }
47
48 /// Upcasts a V0 actions DE to a generic V0 data-element.
49 #[no_mangle]
np_ffi_V0Actions_into_V0DataElement(actions: V0Actions) -> V0DataElement50 pub extern "C" fn np_ffi_V0Actions_into_V0DataElement(actions: V0Actions) -> V0DataElement {
51 V0DataElement::Actions(actions)
52 }
53
54 /// Gets the tag of a `BuildTxPowerResult` tagged-union.
55 #[no_mangle]
np_ffi_BuildTxPowerResult_kind( result: BuildTxPowerResult, ) -> BuildTxPowerResultKind56 pub extern "C" fn np_ffi_BuildTxPowerResult_kind(
57 result: BuildTxPowerResult,
58 ) -> BuildTxPowerResultKind {
59 result.kind()
60 }
61
62 /// Casts a `BuildTxPowerResult` to the `Success` variant, panicking in the
63 /// case where the passed value is of a different enum variant.
64 #[no_mangle]
np_ffi_BuildTxPowerResult_into_SUCCESS(result: BuildTxPowerResult) -> TxPower65 pub extern "C" fn np_ffi_BuildTxPowerResult_into_SUCCESS(result: BuildTxPowerResult) -> TxPower {
66 unwrap(result.into_success(), PanicReason::EnumCastFailed)
67 }
68
69 /// Attempts to construct a new TxPower from
70 /// the given signed-byte value.
71 #[no_mangle]
np_ffi_TxPower_build_from_signed_byte(tx_power: i8) -> BuildTxPowerResult72 pub extern "C" fn np_ffi_TxPower_build_from_signed_byte(tx_power: i8) -> BuildTxPowerResult {
73 TxPower::build_from_signed_byte(tx_power)
74 }
75
76 /// Gets the value of the given TxPower as a signed byte.
77 #[no_mangle]
np_ffi_TxPower_as_signed_byte(tx_power: TxPower) -> i878 pub extern "C" fn np_ffi_TxPower_as_signed_byte(tx_power: TxPower) -> i8 {
79 tx_power.as_i8()
80 }
81
82 /// Gets the discriminant of the `SetV0ActionResult` tagged-union.
83 #[no_mangle]
np_ffi_SetV0ActionResult_kind( result: SetV0ActionResult, ) -> SetV0ActionResultKind84 pub extern "C" fn np_ffi_SetV0ActionResult_kind(
85 result: SetV0ActionResult,
86 ) -> SetV0ActionResultKind {
87 result.kind()
88 }
89
90 /// Attempts to cast a `SetV0ActionResult` tagged-union into the `Success` variant.
91 #[no_mangle]
np_ffi_SetV0ActionResult_into_SUCCESS(result: SetV0ActionResult) -> V0Actions92 pub extern "C" fn np_ffi_SetV0ActionResult_into_SUCCESS(result: SetV0ActionResult) -> V0Actions {
93 unwrap(result.into_success(), PanicReason::EnumCastFailed)
94 }
95
96 /// Attempts to cast a `SetV0ActionResult` tagged-union into the `Error` variant.
97 #[no_mangle]
np_ffi_SetV0ActionResult_into_ERROR(result: SetV0ActionResult) -> V0Actions98 pub extern "C" fn np_ffi_SetV0ActionResult_into_ERROR(result: SetV0ActionResult) -> V0Actions {
99 unwrap(result.into_error(), PanicReason::EnumCastFailed)
100 }
101
102 /// Constructs a new V0 actions DE with no declared boolean
103 /// actions and a zeroed context sync sequence number,
104 /// where the DE is intended for the given advertisement
105 /// kind (plaintext/encrypted).
106 #[no_mangle]
np_ffi_build_new_zeroed_V0Actions(kind: AdvertisementBuilderKind) -> V0Actions107 pub extern "C" fn np_ffi_build_new_zeroed_V0Actions(kind: AdvertisementBuilderKind) -> V0Actions {
108 V0Actions::new_zeroed(kind)
109 }
110
111 /// Return whether a boolean action type is set in this data element
112 #[no_mangle]
np_ffi_V0Actions_has_action(actions: V0Actions, action_type: ActionType) -> bool113 pub extern "C" fn np_ffi_V0Actions_has_action(actions: V0Actions, action_type: ActionType) -> bool {
114 match actions.has_action(action_type) {
115 Ok(b) => b,
116 Err(_) => panic(PanicReason::InvalidStackDataStructure),
117 }
118 }
119
120 /// Attempts to set the given action bit to the given boolean value.
121 /// This operation may fail if the requested action bit may not be
122 /// set for the kind of containing advertisement (public/encrypted)
123 /// that this action DE is intended to belong to. In this case,
124 /// the original action bits will be yielded back to the caller,
125 /// unaltered.
126 #[no_mangle]
np_ffi_V0Actions_set_action( actions: V0Actions, action_type: ActionType, value: bool, ) -> SetV0ActionResult127 pub extern "C" fn np_ffi_V0Actions_set_action(
128 actions: V0Actions,
129 action_type: ActionType,
130 value: bool,
131 ) -> SetV0ActionResult {
132 panic_if_invalid(actions.set_action(action_type, value))
133 }
134
135 /// Returns the representation of the passed `V0Actions` as an unsigned
136 /// integer, where the bit-positions correspond to individual actions.
137 #[no_mangle]
np_ffi_V0Actions_as_u32(actions: V0Actions) -> u32138 pub extern "C" fn np_ffi_V0Actions_as_u32(actions: V0Actions) -> u32 {
139 actions.as_u32()
140 }
141