Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

device_ledger: factor the prologue code #4032

Merged
merged 1 commit into from
Jul 19, 2018

Conversation

moneromooo-monero
Copy link
Collaborator

No description provided.

@@ -317,6 +317,30 @@ namespace hw {
memset(this->buffer_recv, 0, BUFFER_RECV_SIZE);
}

int device_ledger::prologue(BYTE type, BYTE extra) {
reset_buffer();
int offset = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused?

return 5;
}

int device_ledger::prologue_noopt(BYTE type, BYTE extra) {
Copy link
Contributor

@stoffu stoffu Jun 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it necessary to have prologue and prologue_noopt separately? Can't we have just one prologue as:

int device_ledger::prologue(BYTE type, BYTE extra) {
  reset_buffer();
  this->buffer_send[0] = 0x00;
  this->buffer_send[1] = type;
  this->buffer_send[2] = extra;
  this->buffer_send[3] = 0x00;
  this->buffer_send[4] = 0x00;
  int offset = 5;
  //options
  this->buffer_send[offset++] = 0;
  return offset;
}

void device_ledger::send_simple(BYTE type, BYTE extra) {
  int offset = prologue(type, extra);
  this->buffer_send[4] = offset - 5;
  this->length_send = offset;
  this->exchange();
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to allow some more factoring while avoiding a function with (what I think would be) too many parameters.

Copy link
Contributor

@stoffu stoffu Jun 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this way of split seems weird to me, because AIUI this line

this->buffer_send[4] = offset - 5;

is supposed to go together with these lines

this->length_send = offset;
this->exchange();

I.e. the content of buffer_send[4] indicates the size of optional argument data starting from buffer_send[5]. In the current patch, the call to prologue_noopt() puts 1 to buffer_send[4], which will then be overwritten by the subsequent line this->buffer_send[4] = offset-5;. The end result is the same, but this patch adds an unnecessary step of buffer_send[4] = 1 which seems weird to me. I also don't really understand what you said above (to allow more factoring and to avoid a function with too many parameters). Can you elaborate?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT, buffer_send[4] is a message length, so it looks right. This step is only unnecessary when the prologue is followed by other data, increasing the message length.
What I meant by a function with more parameters is that there could be one single function which you configure with command byte, extra byte, optional byte, and possibly more. I simply decided not to do it that way because I judged it less clear/too annoying for the caller.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks for the clarification.

Copy link
Contributor

@stoffu stoffu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, though approval from @cslashm would be very desirable

@moneromooo-monero
Copy link
Collaborator Author

one more call using send_simple

@moneromooo-monero
Copy link
Collaborator Author

Expand the prologue functions to also factor a couple more variants, per request.

Copy link
Contributor

@cslashm cslashm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except the naming, looks fine for me .

offset = 5;
//options
this->buffer_send[offset] = 0x00;
int device_ledger::prologue(BYTE type, BYTE extra, BYTE extraer) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to respect the ISO7816 protocol naming (which is used by PCSC and the NanosApp) I 'd prefere this naming:

int device_ledger::set_command_header(BYTE ins , BYTE p1, BYTE p2)

this->buffer_send[4] = offset-5;
this->length_send = offset;
this->exchange();
int device_ledger::prologue_noopt(BYTE type, BYTE extra, BYTE extraer) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to respect the ISO7816 protocol naming (which is used by PCSC and the NanosApp) I 'd prefere this naming:

int device_ledger::set_command_header_noopt(BYTE ins , BYTE p1, BYTE p2)

@luigi1111 luigi1111 merged commit 87e158b into monero-project:master Jul 19, 2018
luigi1111 added a commit that referenced this pull request Jul 19, 2018
87e158b device_ledger: factor the prologue code (moneromooo-monero)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants