Rewrite all units and other logic to better use return values and added TRY() helper
This commit is contained in:
@@ -28,9 +28,9 @@ static TF_Result bulkwrite_lst(TinyFrame *tf, TF_Msg *msg)
|
||||
goto close;
|
||||
}
|
||||
else if (msg->type == MSG_BULK_DATA || msg->type == MSG_BULK_END) {
|
||||
// if past len, say we're done and close
|
||||
// if past len, speak up
|
||||
if (bulk->offset >= bulk->len) {
|
||||
com_respond_err(bulk->frame_id, "WRITE OVERRUN");
|
||||
com_respond_error(bulk->frame_id, E_OVERRUN);
|
||||
goto close;
|
||||
}
|
||||
|
||||
|
||||
@@ -60,7 +60,7 @@ void com_respond_str(TF_TYPE type, TF_ID frame_id, const char *str)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
void com_respond_err(TF_ID frame_id, const char *message)
|
||||
static void respond_err(TF_ID frame_id, const char *message)
|
||||
{
|
||||
com_respond_str(MSG_ERROR, frame_id, message);
|
||||
}
|
||||
@@ -68,13 +68,15 @@ void com_respond_err(TF_ID frame_id, const char *message)
|
||||
|
||||
void com_respond_bad_cmd(TF_ID frame_id)
|
||||
{
|
||||
com_respond_err(frame_id, "BAD COMMAND");
|
||||
respond_err(frame_id, "BAD COMMAND");
|
||||
}
|
||||
|
||||
|
||||
void com_respond_malformed_cmd(TF_ID frame_id)
|
||||
void com_respond_error(TF_ID frame_id, error_t error)
|
||||
{
|
||||
com_respond_err(frame_id, "MALFORMED PAYLOAD");
|
||||
if (error == E_SUCCESS)
|
||||
com_respond_ok(frame_id);
|
||||
else
|
||||
respond_err(frame_id, error_get_message(error));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
+8
-31
@@ -11,7 +11,6 @@
|
||||
|
||||
/**
|
||||
* Respond to a TF message using printf-like formatting.
|
||||
* Works synchronously, must be called on a job queue.
|
||||
*
|
||||
* @param type - response type byte
|
||||
* @param frame_id - ID of the original msg
|
||||
@@ -23,7 +22,6 @@ com_respond_snprintf(TF_ID frame_id, TF_TYPE type, const char *format, ...);
|
||||
|
||||
/**
|
||||
* Respond to a TF message with a buffer of fixed length and custom type.
|
||||
* Works synchronously, must be called on a job queue.
|
||||
*
|
||||
* @param type - response type byte
|
||||
* @param frame_id - ID of the original msg
|
||||
@@ -34,15 +32,21 @@ void com_respond_buf(TF_ID frame_id, TF_TYPE type, const uint8_t *buf, uint32_t
|
||||
|
||||
/**
|
||||
* Respond to a TF message with empty body and MSG_SUCCESS type.
|
||||
* Works synchronously, must be called on a job queue.
|
||||
*
|
||||
* @param frame_id - ID of the original msg
|
||||
*/
|
||||
void com_respond_ok(TF_ID frame_id);
|
||||
|
||||
/**
|
||||
* Respond with a error constant (converted to string)
|
||||
*
|
||||
* @param frame_id - ID of the original msg
|
||||
* @param error - error to report
|
||||
*/
|
||||
void com_respond_error(TF_ID frame_id, error_t error);
|
||||
|
||||
/**
|
||||
* Same like tf_respond_buf(), but used for sending spontaneous reports.
|
||||
* Works synchronously, must be called on a job queue / timer task etc.
|
||||
*
|
||||
* @param type - response type byte
|
||||
* @param buf - byte buffer
|
||||
@@ -53,7 +57,6 @@ void com_send_buf(TF_TYPE type, const uint8_t *buf, uint32_t len);
|
||||
/**
|
||||
* Same like tf_respond_buf(), but the buffer length is measured with strlen.
|
||||
* Used to sending ASCII string responses.
|
||||
* Works synchronously, must be called on a job queue.
|
||||
*
|
||||
* @param type - response type byte
|
||||
* @param frame_id - ID of the original msg
|
||||
@@ -61,32 +64,8 @@ void com_send_buf(TF_TYPE type, const uint8_t *buf, uint32_t len);
|
||||
*/
|
||||
void com_respond_str(TF_TYPE type, TF_ID frame_id, const char *str);
|
||||
|
||||
/**
|
||||
* Schedule sending an ASCII string error response.
|
||||
* Schedules a low priority job.
|
||||
*
|
||||
* @param frame_id - ID of the original msg
|
||||
* @param str - character buffer, zero terminated
|
||||
*/
|
||||
void com_respond_err(TF_ID frame_id, const char *str);
|
||||
|
||||
/**
|
||||
* Variant of sched_respond_err() for reporting bad received command code
|
||||
*
|
||||
* @param msg_id - ID of the original msg
|
||||
*/
|
||||
void com_respond_bad_cmd(TF_ID frame_id);
|
||||
|
||||
/**
|
||||
* Variant of sched_respond_err() for reporting malformed commands (e.g. too short payload)
|
||||
*
|
||||
* @param msg_id - ID of the original msg
|
||||
*/
|
||||
void com_respond_malformed_cmd(TF_ID frame_id);
|
||||
|
||||
/**
|
||||
* Schedule sending a one-byte response with MSG_SUCCESS type.
|
||||
* Schedules a high priority job.
|
||||
*
|
||||
* @param frame_id - ID of the original msg
|
||||
* @param d - data
|
||||
@@ -95,7 +74,6 @@ void com_respond_u8(TF_ID frame_id, uint8_t d);
|
||||
|
||||
/**
|
||||
* Schedule sending a two-byte response with MSG_SUCCESS type.
|
||||
* Schedules a high priority job.
|
||||
*
|
||||
* @param frame_id - ID of the original msg
|
||||
* @param d - data
|
||||
@@ -104,7 +82,6 @@ void com_respond_u16(TF_ID frame_id, uint16_t d);
|
||||
|
||||
/**
|
||||
* Schedule sending a 4-byte response with MSG_SUCCESS type.
|
||||
* Schedules a high priority job.
|
||||
*
|
||||
* @param frame_id - ID of the original msg
|
||||
* @param d - data
|
||||
|
||||
Reference in New Issue
Block a user