-
Notifications
You must be signed in to change notification settings - Fork 75
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
22 changed files
with
815 additions
and
45 deletions.
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
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
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,86 @@ | ||
// Copyright (C) 2024 Toitware ApS. | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; version | ||
// 2.1 only. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// The license can be found in the file `LICENSE` in the top level | ||
// directory of this repository. | ||
|
||
#pragma once | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#else | ||
#include <stdbool.h> | ||
#endif | ||
|
||
/* | ||
* C interface for Toit's external API. | ||
*/ | ||
|
||
typedef enum { | ||
// The operation succeeded. | ||
TOIT_ERR_SUCCESS = 0, | ||
// The operation encountered an out-of-memory error. | ||
TOIT_ERR_OOM, | ||
// An error, for when the receiver of a system message didn't exist. | ||
TOIT_ERR_NO_SUCH_RECEIVER, | ||
// The corresponding resource was not found. | ||
TOIT_ERR_NOT_FOUND, | ||
// An unknown error. | ||
TOIT_ERR_ERROR, | ||
} toit_err_t; | ||
|
||
const int TOIT_MSG_RESERVED_TYPES = 64; | ||
|
||
struct toit_msg_context_t; | ||
typedef struct toit_msg_context_t toit_msg_context_t; | ||
|
||
typedef struct { | ||
int sender; | ||
int request_handle; | ||
toit_msg_context_t* context; | ||
} toit_msg_request_handle_t; | ||
|
||
typedef toit_err_t (*toit_msg_on_created_cb_t)(void* user_data, toit_msg_context_t* context); | ||
typedef toit_err_t (*toit_msg_on_message_cb_t)(void* user_data, int sender, void* data, int length); | ||
typedef toit_err_t (*toit_msg_on_request_cb_t)(void* user_data, | ||
int sender, | ||
int function, | ||
toit_msg_request_handle_t rpc_handle, | ||
void* data, int length); | ||
typedef toit_err_t (*toit_msg_on_removed_cb_t)(void* user_data); | ||
|
||
typedef struct toit_msg_cbs_t { | ||
toit_msg_on_created_cb_t on_created; | ||
toit_msg_on_message_cb_t on_message; | ||
toit_msg_on_request_cb_t on_rpc_request; | ||
toit_msg_on_removed_cb_t on_removed; | ||
} toit_msg_cbs_t; | ||
|
||
toit_err_t toit_msg_add_handler(const char* id, | ||
void* user_data, | ||
toit_msg_cbs_t cbs); | ||
|
||
toit_err_t toit_msg_remove_handler(toit_msg_context_t* context); | ||
|
||
toit_err_t toit_msg_notify(toit_msg_context_t* context, | ||
int target_pid, | ||
void* data, int length, | ||
bool free_on_failure); | ||
|
||
toit_err_t toit_msg_request_reply(toit_msg_request_handle_t handle, void* data, int length, bool free_on_failure); | ||
toit_err_t toit_msg_request_fail(toit_msg_request_handle_t handle, const char* error); | ||
|
||
toit_err_t toit_gc(); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
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
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,83 @@ | ||
// Copyright (C) 2024 Toitware ApS. All rights reserved. | ||
// Use of this source code is governed by an MIT-style license that can be | ||
// found in the lib/LICENSE file. | ||
import io | ||
import rpc | ||
|
||
clients_ ::= {:} | ||
notification-listeners_ := 0 | ||
notification-handler_/ExternalMessageHandler_? := null | ||
|
||
class Client: | ||
pid/int | ||
id/string | ||
on-notify_/Lambda? := null | ||
is-closed_/bool := false | ||
|
||
constructor.private_ .pid .id .on-notify_: | ||
|
||
static open id/string --on-notify/Lambda?=null -> Client?: | ||
pid := pid-for-external-id_ id | ||
if pid == -1: throw "NOT_FOUND" | ||
if clients_.contains pid: throw "ALREADY_IN_USE" | ||
result := Client.private_ pid id on-notify | ||
clients_[pid] = result | ||
return result | ||
|
||
close -> none: | ||
if is-closed_: return | ||
// Go through the function so that the ref-counting is correct. | ||
set-on-notify null | ||
is-closed_ = true | ||
|
||
is-closed -> bool: | ||
return is-closed_ | ||
|
||
/** Helper to convert the $message to a ByteArray. */ | ||
encode-message_ message/io.Data --copy/bool -> ByteArray: | ||
bytes/ByteArray := ? | ||
if copy or message is not ByteArray: | ||
bytes = ByteArray message.byte-size | ||
message.write-to-byte-array bytes --at=0 0 message.byte-size | ||
else: | ||
bytes = message as ByteArray | ||
return bytes | ||
|
||
notify message/io.Data --copy/bool=true: | ||
if is-closed_: throw "ALREADY_CLOSED" | ||
bytes := encode-message_ message --copy=copy | ||
process-send_ pid SYSTEM-EXTERNAL-NOTIFICATION_ bytes | ||
|
||
request function/int message/ByteArray --copy/bool=true -> any: | ||
if is-closed_: throw "ALREADY_CLOSED" | ||
bytes := encode-message_ message --copy=copy | ||
return rpc.invoke pid function bytes | ||
|
||
set-on-notify callback/Lambda? -> none: | ||
if is-closed_: throw "ALREADY_CLOSED" | ||
if on-notify_ != null: notification-listeners_-- | ||
on-notify_ = callback | ||
if callback: | ||
notification-listeners_++ | ||
ExternalMessageHandler_.handle-listener-change | ||
|
||
class ExternalMessageHandler_ implements SystemMessageHandler_: | ||
static TYPE ::= SYSTEM-EXTERNAL-NOTIFICATION_ | ||
|
||
close -> none: | ||
clear-system-message-handler_ TYPE | ||
|
||
on-message type/int gid/int pid/int argument -> none: | ||
client/Client? := clients_.get pid | ||
if not client: return | ||
if client.on-notify_: | ||
client.on-notify_.call argument | ||
|
||
static handle-listener-change -> none: | ||
if notification-listeners_ > 0 and not notification-handler_: | ||
notification-handler_ = ExternalMessageHandler_ | ||
set-system-message-handler_ TYPE notification-handler_ | ||
else if notification-listeners_ <= 0 and notification-handler_: | ||
notification-handler_.close | ||
notification-handler_ = null |
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
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
Oops, something went wrong.