Skip to content

Commit

Permalink
Fix some missing repr(C) annotations, add some missing fields, fix an…
Browse files Browse the repository at this point in the history
… incorrect data type
  • Loading branch information
Neil McAlister committed Aug 16, 2019
1 parent c0e6677 commit 989cd28
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion scannit-core-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn main() {

let mut response_buffer = [0; MAX_BUFFER_SIZE];
let stored_value =
transcieve(&card, Command::ReadPeriodPass.into(), &mut response_buffer).unwrap();
transcieve(&card, Command::ReadStoredValue.into(), &mut response_buffer).unwrap();

let mut response_buffer = [0; MAX_BUFFER_SIZE];
let e_ticket = transcieve(&card, Command::ReadETicket.into(), &mut response_buffer).unwrap();
Expand Down
1 change: 1 addition & 0 deletions scannit-core-ffi/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl From<&mut Vec<u8>> for FFIByteBuffer {
}
}

#[repr(C)]
pub struct FFIHistoryBuffer {
data: *mut FFIHistory,
len: usize,
Expand Down
3 changes: 1 addition & 2 deletions scannit-core-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ pub unsafe extern "C" fn free_travel_card(travel_card_ptr: *mut FFITravelCard) {
ffi::free_history_buffer(travel_card.history);

ffi::free_byte_buffer(travel_card.period_pass.validity_area_1_value);
ffi::free_byte_buffer(travel_card.period_pass.validity_area_2_value);
ffi::free_byte_buffer(travel_card.period_pass.last_board_area_value);
ffi::free_byte_buffer(travel_card.period_pass.validity_area_2_value);

ffi::free_byte_buffer(travel_card.e_ticket.validity_area_value);
ffi::free_byte_buffer(travel_card.e_ticket.period_pass_validity_area_value);
Expand Down
27 changes: 19 additions & 8 deletions scannit-core-ffi/src/models.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ffi::{FFIHistoryBuffer, FFIByteBuffer};
use crate::ffi::{FFIByteBuffer, FFIHistoryBuffer};
use libc::c_char;
use scannit_core::eticket::ETicket;
use scannit_core::history::{History, TransactionType};
Expand Down Expand Up @@ -82,6 +82,7 @@ pub struct FFIPeriodPass {
/// We'll represent it as something that's always an array.
pub validity_area_1_value: FFIByteBuffer,
pub period_start_date_1: UnixTimestamp,
pub period_end_date_1: UnixTimestamp,

pub product_code_2_kind: ProductCodeKind,
pub product_code_2_value: u16,
Expand All @@ -90,6 +91,7 @@ pub struct FFIPeriodPass {
/// We'll represent it as something that's always an array.
pub validity_area_2_value: FFIByteBuffer,
pub period_start_date_2: UnixTimestamp,
pub period_end_date_2: UnixTimestamp,

// Most recent card load:
pub loaded_period_product_kind: ProductCodeKind,
Expand All @@ -107,8 +109,8 @@ pub struct FFIPeriodPass {
pub last_board_location_kind: BoardingLocationKind,
pub last_board_location_value: u16,
pub last_board_direction: BoardingDirection,
pub last_board_area_kind: ValidityAreaKind,
pub last_board_area_value: FFIByteBuffer,
pub last_board_area_kind: BoardingAreaKind,
pub last_board_area_value: u8,
}

impl FFIPeriodPass {
Expand All @@ -119,12 +121,14 @@ impl FFIPeriodPass {
validity_area_1_kind: ValidityAreaKind::from(&period_pass.validity_area_1),
validity_area_1_value: FFIByteBuffer::from(period_pass.validity_area_1),
period_start_date_1: period_pass.period_start_date_1.and_hms(0, 0, 0).timestamp(),
period_end_date_1: period_pass.period_end_date_1.and_hms(0, 0, 0).timestamp(),

product_code_2_kind: ProductCodeKind::from(&period_pass.product_code_2),
product_code_2_value: u16::from(&period_pass.product_code_2),
validity_area_2_kind: ValidityAreaKind::from(&period_pass.validity_area_2),
validity_area_2_value: FFIByteBuffer::from(period_pass.validity_area_2),
period_start_date_2: period_pass.period_start_date_2.and_hms(0, 0, 0).timestamp(),
period_end_date_2: period_pass.period_end_date_2.and_hms(0, 0, 0).timestamp(),

loaded_period_product_kind: ProductCodeKind::from(&period_pass.loaded_period_product),
loaded_period_product_value: u16::from(&period_pass.loaded_period_product),
Expand All @@ -139,8 +143,8 @@ impl FFIPeriodPass {
last_board_location_kind: BoardingLocationKind::from(&period_pass.last_board_location),
last_board_location_value: u16::from(&period_pass.last_board_location),
last_board_direction: period_pass.last_board_direction,
last_board_area_kind: ValidityAreaKind::from(&period_pass.last_board_area),
last_board_area_value: FFIByteBuffer::from(period_pass.last_board_area),
last_board_area_kind: BoardingAreaKind::from(&period_pass.last_board_area),
last_board_area_value: u8::from(&period_pass.last_board_area),
}
}
}
Expand Down Expand Up @@ -220,18 +224,24 @@ impl FFIETicket {
period_pass_validity_area_kind: ValidityAreaKind::from(
&e_ticket.period_pass_validity_area,
),
period_pass_validity_area_value: FFIByteBuffer::from(e_ticket.period_pass_validity_area),
period_pass_validity_area_value: FFIByteBuffer::from(
e_ticket.period_pass_validity_area,
),
extension_product_code_kind: ProductCodeKind::from(&e_ticket.extension_product_code),
extension_product_code_value: u16::from(&e_ticket.extension_product_code),
extension_1_validity_area_kind: ValidityAreaKind::from(
&e_ticket.extension_1_validity_area,
),
extension_1_validity_area_value: FFIByteBuffer::from(e_ticket.extension_1_validity_area),
extension_1_validity_area_value: FFIByteBuffer::from(
e_ticket.extension_1_validity_area,
),
extension_1_fare_cents: e_ticket.extension_1_fare_cents,
extension_2_validity_area_kind: ValidityAreaKind::from(
&e_ticket.extension_2_validity_area,
),
extension_2_validity_area_value: FFIByteBuffer::from(e_ticket.extension_2_validity_area),
extension_2_validity_area_value: FFIByteBuffer::from(
e_ticket.extension_2_validity_area,
),
extension_2_fare_cents: e_ticket.extension_2_fare_cents,
sale_status: e_ticket.sale_status,
validity_start_datetime: e_ticket.validity_start_datetime.timestamp(),
Expand All @@ -249,6 +259,7 @@ impl FFIETicket {
}
}

#[repr(C)]
pub struct FFIHistory {
pub transaction_type: TransactionType,
pub boarding_datetime: UnixTimestamp,
Expand Down
4 changes: 2 additions & 2 deletions src/travelcard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub struct PeriodPass {
pub last_board_vehicle_number: u16,
pub last_board_location: BoardingLocation,
pub last_board_direction: BoardingDirection,
pub last_board_area: ValidityArea,
pub last_board_area: BoardingArea,
}

pub fn create_travel_card(
Expand Down Expand Up @@ -199,7 +199,7 @@ fn read_period_pass(period_pass: &[u8]) -> PeriodPass {
last_board_location_num,
),
last_board_direction: BoardingDirection::from(last_board_direction),
last_board_area: ValidityArea::new(last_board_area_type, last_board_area),
last_board_area: BoardingArea::new(last_board_area_type, last_board_area),
}
}

Expand Down

0 comments on commit 989cd28

Please sign in to comment.