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

chore: eliminate [in/out] parameters #94

Merged
merged 24 commits into from
Sep 9, 2023
Merged

chore: eliminate [in/out] parameters #94

merged 24 commits into from
Sep 9, 2023

Conversation

zfields
Copy link
Collaborator

@zfields zfields commented Sep 8, 2023

No description provided.

n_helpers.c Outdated
NOTE_C_LOG_ERROR("NULL parameter");
return ERRSTR("NULL parameter", c_err);
}

if (*outLen < cobsGuaranteedFit(inLen)) {
if (outLen < cobsGuaranteedFit(inLen)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

cobsGuaranteedFit adds the +1 for the transport framing. I think we can make things more consistent, by making a clear separation between the pure COBS encoding requirements and the transport framing requirements (the +1.). Adding #define COBS_TRANSPORT_OVERHEAD=1 will make this easier to spot in the code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I like this idea. To follow the common naming convention, I will use the symbol COBS_EOP_OVERHEAD.

From Wikipedia:

   [OHB]                                : Overhead byte (Start of frame)
     3+ -------------->|                : Points to relative location of first zero symbol
                       2+-------->|     : Is a zero data byte, pointing to next zero symbol
                                  [EOP] : Location of end-of-packet zero symbol.
     0     1     2     3     4    5     : Byte Position
     03    11    22    02    33   00    : COBS Data Frame
           11    22    00    33         : Extracted Data
     
OHB = Overhead Byte (Points to next zero symbol)
EOP = End Of Packet

@@ -331,19 +340,63 @@ uint32_t NoteBinaryMaxEncodedLength(uint32_t unencodedLength)
return cobsEncodedMaxLength(unencodedLength);
Copy link
Contributor

Choose a reason for hiding this comment

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

Needs a +1 for the framing overhead.

We can avoid littering the code with +1 everywhere, with

cobsEncodedToBufferLength(uint32_t encodedLength) {
   return encodedLength+COBS_TRANSPORT_OVERHEAD;
}

That DRYs it up and means just a single change should the transport framing change, although here the DRY and clear semantics is more pertinent atm.

The code would use this whenever it needs to convert from the cobs encoding data length (such as the cobs value from the Notecard, or the max encoded length) to the actual buffer size needed to receive it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

now promoted up through cobsEncodedMaxLength()

n_helpers.c Outdated
if (!decodedLen) {
NOTE_C_LOG_ERROR("decodedLen cannot be NULL");
return ERRSTR("decodedLen cannot be NULL", c_err);
if (bufLen < (cobsEncodedMaxLength(decodedLen) + 1)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that when we want to know about the required size of the buffer we should use NoteBinaryMaxEncodedLength() since that's what we expect the callers to use.

Currently NoteBinaryMaxEncodedLength doesn't do the +1 so this call fails at present.

I thought we'd agreed on this last week that it's ok to call NoteBinaryMaxEncodedLength rather than replicating the code, especially when it's about validating buffer sizes.

(This is the 3rd time this has bit us btw.)

The cobsEncodedMaxLength() etc functions are suitable when talking about purely cobs conversion, and shouldn't make adjustments for the framing overhead.

Copy link
Collaborator Author

@zfields zfields Sep 8, 2023

Choose a reason for hiding this comment

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

I don't understand how the code you're referencing (line 419) failed; it incorporates the +1.

Assuming what is currently sitting on line 419 works, the ugly syntax will be resolved by the underlying change to cobsEncodedMaxLength().

Copy link
Contributor

Choose a reason for hiding this comment

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

NoteBinaryMaxEncodedLength didn't add the +1, so it was returning 1 byte less. I feel adding +1 to cobsXXX functions is mixing concerns.

Copy link
Collaborator Author

@zfields zfields Sep 8, 2023

Choose a reason for hiding this comment

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

I understand your concern and I don't disagree, but I don't have the freedom to modify cobsGuaranteedFit() (as it is derived from the Notecard firmware).

cobsEncodedMaxLength() was designed to be an analog to cobsGuaranteedFit(), and I believe it needs to follow precedent.

n_helpers.c Outdated
@param len [out] the length of the decoded contents of the Notecard's binary
data store.
@param encData The encoded binary data to decode.
@param encLen The length of the encoded binary data.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we change encLen to encDataLen, to match how we use decData and decDataLen for NoteBinaryCodecEncode?

n_helpers.c Outdated
length of the decoded data.
@param decData The decoded binary data to encode.
@param decDataLen The length of the decoded binary data.
@param encBuf The target buffer for the encoded data. This can be in the same
Copy link
Collaborator

Choose a reason for hiding this comment

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

This can be in the same - > This can be the same

Copy link
Collaborator Author

@zfields zfields Sep 8, 2023

Choose a reason for hiding this comment

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

I deliberately chose to add "in". The addresses MUST be different, but they can both be part of (or "in") the same buffer. I'm open to suggestions for rewording, but that point must be expressed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I reworded it to be more clear and explicit regarding the concern expressed above.

n_helpers.c Outdated
@param decDataLen The length of the decoded binary data.
@param encBuf The target buffer for the encoded data. This can be in the same
buffer as `decData`, allowing for in-place encoding (see note).
@param encBufSize The size of `encBuf`.

@returns NULL on success, else an error string pointer.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is outdated.

n_helpers.c Outdated
stored on the Notecard.

@param len [out] The length required to hold the entire contents of the
Notecard's binary data store. If there's no data stored on the
Copy link
Collaborator

Choose a reason for hiding this comment

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

270-271 are indented by 1 additional space. This is really unacceptable, @zfields.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

😬

n_helpers.c Outdated
//**************************************************************************/
/*!
@brief Receive a large binary object from the Notecard's binary buffer

@param buffer A buffer to hold the binary object
@param bufLen The total length of the provided buffer
@param buffer A buffer to hold the binary range
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm good with either convention, but just noting that we aren't aligning the param descriptions for some of the other APIs in this PR. Also, prefer consistency with ending descriptions with a period.

n_helpers.c Show resolved Hide resolved
n_helpers.c Outdated
}

// Return `NULL` if success, else error string pointer
return NULL;
}

//**************************************************************************/
/*!
@brief Reset the Notecard's binary buffer.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we call it the binary data store / binary store instead of binary buffer? Same question applies elsewhere where we use the term binary buffer.

return err;
}

return NULL;
Copy link
Collaborator

@haydenroche5 haydenroche5 Sep 8, 2023

Choose a reason for hiding this comment

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

Need to free rsp. Should be caught by the unit test with valgrind; not sure why it wasn't on the CI run.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good catch!

n_helpers.c Outdated
@@ -564,19 +596,21 @@ const char * NoteBinaryTransmit(uint8_t *unencodedData, uint32_t unencodedLen,

// Ensure the transaction doesn't return an error.
if (!NoteRequest(req)) {
NOTE_C_LOG_ERROR("failed to initialize binary transaction");
const char *err = ERRSTR("failed to initialize binary transaction", c_err);
NOTE_C_LOG_ERROR(err);
_UnlockNote();
// On errors, we restore the caller's input buffer by COBS
Copy link
Collaborator

Choose a reason for hiding this comment

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

Might make sense to remove "COBS" here, while you're making changes.

@zfields zfields merged commit ff18fe8 into master Sep 9, 2023
4 checks passed
@zfields zfields deleted the zak-in-out branch September 9, 2023 01:01
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.

3 participants