-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Copyright (C) 2024 Toitware ApS. | ||
# Use of this source code is governed by a Zero-Clause BSD license that can | ||
# be found in the LICENSE file. | ||
|
||
include("${CMAKE_CURRENT_LIST_DIR}/toit.cmake") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright (C) 2024 Toitware ApS. | ||
// Use of this source code is governed by a Zero-Clause BSD license that can | ||
// be found in the LICENSE file. | ||
|
||
#include <toit/toit.h> | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdint.h> | ||
|
||
/// A struct to hold the context of the external service. | ||
/// Typically, this would hold more data, but for this example, we only need | ||
/// the message context. | ||
typedef struct { | ||
toit_msg_context_t* msg_context; | ||
} echo_service_t; | ||
|
||
/// @brief Called when the external message handler is created. | ||
/// @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. | ||
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; | ||
} | ||
|
||
/// @brief Called when a notification message is received. | ||
/// | ||
/// Echoes the message back to the sender. | ||
/// Note that notifications are not required to be replied to. | ||
/// | ||
/// @param user_data The user data passed to `toit_msg_add_handler`. | ||
/// @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. | ||
static toit_err_t on_message(void* user_data, int sender, void* 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) { | ||
printf("unable to send\n"); | ||
} | ||
return TOIT_ERR_SUCCESS; | ||
} | ||
|
||
/// @brief Called when an RPC request is received. | ||
/// | ||
/// For every RPC request the service must reply through either `toit_msg_request_reply` or | ||
/// `toit_msg_request_fail`. | ||
/// | ||
/// @param user_data The user data passed to `toit_msg_add_handler`. | ||
/// @param sender The id of the sender. | ||
/// @param function The function id of the request. For the echo service this is ignored. | ||
/// @param handle The handle to reply to the request. This handle must be provided to either | ||
/// `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. | ||
static toit_err_t on_rpc_request(void* user_data, int sender, int function, toit_msg_request_handle_t handle, void* data, int length) { | ||
if (toit_msg_request_reply(handle, data, length, true) != TOIT_ERR_SUCCESS) { | ||
printf("unable to reply\n"); | ||
} | ||
return TOIT_ERR_SUCCESS; | ||
} | ||
|
||
/// @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. | ||
static toit_err_t on_removed(void* user_data) { | ||
free(user_data); | ||
return TOIT_ERR_SUCCESS; | ||
} | ||
|
||
/// @brief Register the external service. | ||
static void __attribute__((constructor)) init() { | ||
echo_service_t* echo_service = (echo_service_t*)malloc(sizeof(echo_service_t)); | ||
echo_service->msg_context = NULL; | ||
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_add_handler("toitlang.org/demo-echo", echo_service, cbs); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (C) 2024 Toitware ApS. | ||
// Use of this source code is governed by a Zero-Clause BSD license that can | ||
// be found in the LICENSE file. | ||
/** | ||
Example code for communicating with the external echo service. | ||
*/ | ||
|
||
import system.external | ||
|
||
FUNCTION-ID ::= 0 | ||
|
||
main: | ||
echo := external.Client.open "toitlang.org/demo-echo" | ||
echo.set-on-notify:: print "Got message: $it.to-string" | ||
echo.notify "Hello, world!" | ||
|
||
response := echo.request FUNCTION-ID #[1, 2, 3] | ||
print "Got response: $response" | ||
|
||
echo.close |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright (C) 2024 Toitware ApS. | ||
# Use of this source code is governed by a Zero-Clause BSD license that can | ||
# be found in the LICENSE file. | ||
|
||
# Due to this function call being in a `toit.cmake` file, the library is also | ||
# compiled into the host executable. If this is not desired, then rename this | ||
# file or copy the content into the `CMakeLists.txt` file. | ||
|
||
idf_component_register( | ||
REQUIRES toit | ||
SRCS echo.c | ||
WHOLE_ARCHIVE | ||
) |
Submodule toit
updated
25 files
+13 −178 | .github/workflows/ci.yml | |
+5 −1 | CMakeLists.txt | |
+2 −2 | Makefile | |
+156 −0 | actions/setup-build/action.yml | |
+84 −0 | include/toit/toit.h | |
+19 −0 | lib/core/message_.toit | |
+83 −0 | lib/system/external.toit | |
+3 −1 | src/CMakeLists.txt | |
+2 −0 | src/compiler/propagation/type_primitive_core.cc | |
+332 −16 | src/messaging.cc | |
+71 −2 | src/messaging.h | |
+1 −0 | src/primitive.h | |
+6 −1 | src/primitive_core.cc | |
+2 −1 | src/run.cc | |
+3 −3 | src/scheduler.cc | |
+3 −9 | src/scheduler.h | |
+2 −0 | src/toit_esp32.cc | |
+2 −0 | src/toit_run_image.cc | |
+29 −7 | tests/ctest/CMakeLists.txt | |
+73 −0 | tests/ctest/external-messaging2-toit-run-input.toit | |
+91 −0 | tests/ctest/external-messaging2-toit-run-test.c | |
+1 −1 | tests/negative/gold/process-send-serialization-test.gold | |
+1 −1 | toolchains/host.cmake | |
+1 −1 | toolchains/idf/components/toit/CMakeLists.txt | |
+73 −0 | tools/external.cmake |