Skip to content

Commit

Permalink
Update toit and minor fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch committed May 17, 2024
1 parent c898583 commit 7b6a712
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
17 changes: 8 additions & 9 deletions README-external-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ Example:
``` c
/// @brief Register an external service.
static void __attribute__((constructor)) init() {
toit_msg_cbs_t cbs = {
.on_created = on_created,
.on_message = on_message,
.on_rpc_request = on_rpc_request,
.on_removed = on_removed,
};
toit_msg_cbs_t cbs = TOIT_MSG_EMPTY_CBS();
cbs.on_created = on_created;
cbs.on_message = on_message;
cbs.on_rpc_request = on_rpc_request;
cbs.on_removed = on_removed;
toit_msg_add_handler("toitlang.org/demo", NULL, cbs);
}
```
Expand All @@ -36,7 +35,7 @@ The second argument to `toit_msg_add_handler` is user-data that is passed to the
callbacks when they are invoked. In our case we just used `NULL`, but often one
provides an object that is used to store state that is needed by the callbacks.
Callbacks that are not used can be set to `NULL`.
Callbacks that are not used can be left unset.
### ESP-IDF cmake
Expand Down Expand Up @@ -90,10 +89,10 @@ Example:
``` c
/// A function that echoes back the message it got.
static toit_err_t on_rpc_request(void* user_data, int sender, int function, toit_msg_request_handle_t handle, uint8_t* data, int length) {
if (toit_msg_request_reply(handle, data, length, true) != TOIT_ERR_SUCCESS) {
if (toit_msg_request_reply(handle, data, length, true) != TOIT_OK) {
printf("unable to reply\n");
}
return TOIT_ERR_SUCCESS;
return TOIT_OK;
}
```
Expand Down
20 changes: 10 additions & 10 deletions components/echo/echo.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ typedef struct {
/// @param user_data The user data passed to `toit_msg_add_handler`.
/// @param context The context of the external message handler. Calls to
/// `toit_msg_notify` and `toit_msg_remove_handler` need this context.
/// @return TOIT_ERR_SUCCESS. The only allowed return value.
/// @return TOIT_OK. The only allowed return value.
static toit_err_t on_created(void* user_data, toit_msg_context_t* context) {
echo_service_t* echo_service = (echo_service_t*)user_data;
echo_service->msg_context = context;
return TOIT_ERR_SUCCESS;
return TOIT_OK;
}

/// @brief Called when a notification message is received.
Expand All @@ -35,14 +35,14 @@ static toit_err_t on_created(void* user_data, toit_msg_context_t* context) {
/// @param sender The id of the sender.
/// @param data The data of the message.
/// @param length The length of the message.
/// @return TOIT_ERR_SUCCESS. The only allowed return value.
/// @return TOIT_OK. The only allowed return value.
static toit_err_t on_message(void* user_data, int sender, uint8_t* data, int length) {
echo_service_t* echo_service = (echo_service_t*)user_data;
toit_msg_context_t* context = echo_service->msg_context;
if (toit_msg_notify(context, sender, data, length, true) != TOIT_ERR_SUCCESS) {
if (toit_msg_notify(context, sender, data, length, true) != TOIT_OK) {
printf("unable to send\n");
}
return TOIT_ERR_SUCCESS;
return TOIT_OK;
}

/// @brief Called when an RPC request is received.
Expand All @@ -57,23 +57,23 @@ static toit_err_t on_message(void* user_data, int sender, uint8_t* data, int len
/// `toit_msg_request_reply` or `toit_msg_request_fail`.
/// @param data The data of the request.
/// @param length The length of the request.
/// @return TOIT_ERR_SUCCESS. The only allowed return value.
/// @return TOIT_OK. The only allowed return value.
static toit_err_t on_rpc_request(void* user_data, int sender, int function, toit_msg_request_handle_t handle, uint8_t* data, int length) {
if (toit_msg_request_reply(handle, data, length, true) != TOIT_ERR_SUCCESS) {
if (toit_msg_request_reply(handle, data, length, true) != TOIT_OK) {
printf("unable to reply\n");
}
return TOIT_ERR_SUCCESS;
return TOIT_OK;
}

/// @brief Called when the external message handler is removed.
///
/// When this function is called, it is safe to free the user data.
///
/// @param user_data The user data passed to `toit_msg_add_handler`.
/// @return TOIT_ERR_SUCCESS. The only allowed return value.
/// @return TOIT_OK. The only allowed return value.
static toit_err_t on_removed(void* user_data) {
free(user_data);
return TOIT_ERR_SUCCESS;
return TOIT_OK;
}

/// @brief Register the external service.
Expand Down

0 comments on commit 7b6a712

Please sign in to comment.