Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void setup() {

void loop() {
float res;
if (!Bridge.call("multiply", res, 1.0, 2.0)) {
if (!Bridge.call("multiply", 1.0, 2.0).result(res)) {
Serial.println("Error calling method: multiply");
Serial.println(Bridge.get_error_code());
Serial.println(Bridge.get_error_message());
Expand Down
14 changes: 13 additions & 1 deletion examples/simple_bridge/simple_bridge.ino
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,23 @@ void setup() {

void loop() {
float res;
if (!Bridge.call("multiply", res, 1.0, 2.0)) {
if (!Bridge.call("multiply", 1.0, 2.0).result(res)) {
Serial.println("Error calling method: multiply");
Serial.println(Bridge.get_error_code());
Serial.println(Bridge.get_error_message());
};

// Call with deferred response check
RpcResult outcome = Bridge.call("multiply", 5.0, 7.0);
Serial.println("RPC called");
delay(10);
if (outcome.result(res)) {
Serial.print("Result of the operation is: ");
Serial.println(res);
} else {
Serial.println(Bridge.get_error_code());
Serial.println(Bridge.get_error_message());
}

Bridge.notify("signal", 200);
}
84 changes: 61 additions & 23 deletions src/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#define RESET_METHOD "$/reset"
#define BIND_METHOD "$/register"
//#define BRIDGE_ERROR "$/bridgeLog"

#define UPDATE_THREAD_STACK_SIZE 500
#define UPDATE_THREAD_PRIORITY 5
Expand All @@ -28,6 +29,58 @@

void updateEntryPoint(void *, void *, void *);

class RpcResult {

public:
RpcError error;

RpcResult(uint32_t id, RPCClient* c, struct k_mutex* rm, struct k_mutex* wm) : msg_id_wait(id), client(c), read_mutex(rm), write_mutex(wm) {}

template<typename RType> bool result(RType& result) {
if (_executed) return error.code == NO_ERR;

while(true) {
if (k_mutex_lock(read_mutex, K_MSEC(10)) == 0 ) {
if (client->get_response(msg_id_wait, result, error)) {
k_mutex_unlock(read_mutex);
// if (error.code == PARSING_ERR) {
// k_mutex_lock(write_mutex, K_FOREVER);
// client->notify(BRIDGE_ERROR, error.traceback);
// k_mutex_unlock(write_mutex);
// }
break;
}
k_mutex_unlock(read_mutex);
k_msleep(1);
} else {
k_yield();
}
}
_executed = true;
return error.code == NO_ERR;
}

bool result() {
MsgPack::object::nil_t nil;
return result(nil);
}

~RpcResult(){
result();
}

operator bool() {
return result();
}

private:
uint32_t msg_id_wait;
RPCClient* client;
bool _executed = false;
struct k_mutex* read_mutex;
struct k_mutex* write_mutex;
};

class BridgeClass {

RPCClient* client = nullptr;
Expand Down Expand Up @@ -73,7 +126,7 @@ class BridgeClass {
UPDATE_THREAD_PRIORITY, 0, K_NO_WAIT);

bool res;
call(RESET_METHOD, res);
call(RESET_METHOD).result(res);
if (res) {
started = true;
}
Expand All @@ -83,7 +136,7 @@ class BridgeClass {
template<typename F>
bool provide(const MsgPack::str_t& name, F&& func) {
bool res;
if (!call(BIND_METHOD, res, name)) {
if (!call(BIND_METHOD, name).result(res)) {
return false;
}
return server->bind(name, func);
Expand All @@ -92,7 +145,7 @@ class BridgeClass {
template<typename F>
bool provide_safe(const MsgPack::str_t& name, F&& func) {
bool res;
if (!call(BIND_METHOD, res, name)) {
if (!call(BIND_METHOD, name).result(res)) {
return false;
}

Expand Down Expand Up @@ -131,8 +184,8 @@ class BridgeClass {

}

template<typename RType, typename... Args>
bool call(const MsgPack::str_t& method, RType& result, Args&&... args) {
template<typename... Args>
RpcResult call(const MsgPack::str_t& method, Args&&... args) {

uint32_t msg_id_wait;

Expand All @@ -145,26 +198,11 @@ class BridgeClass {
} else {
k_yield();
}
}

// Lock read mutex
while(true) {
if (k_mutex_lock(&read_mutex, K_MSEC(10)) == 0 ) {
if (client->get_response(msg_id_wait, result)) {
k_mutex_unlock(&read_mutex);
break;
}
k_mutex_unlock(&read_mutex);
k_msleep(1);
} else {
k_yield();
}

}
}
return RpcResult{msg_id_wait, client, &read_mutex, &write_mutex};
}

return (client->lastError.code == NO_ERR);

}

template<typename... Args>
void notify(const MsgPack::str_t method, Args&&... args) {
Expand Down
15 changes: 6 additions & 9 deletions src/monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class BridgeMonitor: public Stream {
if (!bridge_started) {
bridge_started = bridge->begin();
}
return bridge_started && bridge->call(MON_CONNECTED_METHOD, is_connected);
return bridge_started && bridge->call(MON_CONNECTED_METHOD).result(is_connected);
}

explicit operator bool() const {
Expand Down Expand Up @@ -99,7 +99,7 @@ class BridgeMonitor: public Stream {
}

size_t written;
const bool ret = bridge->call(MON_WRITE_METHOD, written, send_buffer);
const bool ret = bridge->call(MON_WRITE_METHOD, send_buffer).result(written);
if (ret) {
return written;
}
Expand All @@ -109,7 +109,7 @@ class BridgeMonitor: public Stream {

bool reset() {
bool res;
bool ok = bridge->call(MON_RESET_METHOD, res);
bool ok = bridge->call(MON_RESET_METHOD).result(res);
if (ok && res) {
is_connected = false;
}
Expand All @@ -121,25 +121,22 @@ class BridgeMonitor: public Stream {

if (size == 0) return;

k_mutex_lock(&monitor_mutex, K_FOREVER);

MsgPack::arr_t<uint8_t> message;
bool ret = bridge->call(MON_READ_METHOD, message, size);
bool ret = bridge->call(MON_READ_METHOD, size).result(message);

if (ret) {
k_mutex_lock(&monitor_mutex, K_FOREVER);
for (size_t i = 0; i < message.size(); ++i) {
temp_buffer.store_char(static_cast<char>(message[i]));
}
k_mutex_unlock(&monitor_mutex);
}

// if (bridge.lastError.code > NO_ERR) {
// is_connected = false;
// }

k_mutex_unlock(&monitor_mutex);
}


};

extern BridgeClass Bridge;
Expand Down
10 changes: 5 additions & 5 deletions src/tcp_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class BridgeTCPClient : public Client {

k_mutex_lock(&client_mutex, K_FOREVER);

const bool resp = bridge->call(TCP_CONNECT_METHOD, connection_id, hostname, port);
const bool resp = bridge->call(TCP_CONNECT_METHOD, hostname, port).result(connection_id);

if (!resp) {
_connected = false;
Expand All @@ -84,7 +84,7 @@ class BridgeTCPClient : public Client {

k_mutex_lock(&client_mutex, K_FOREVER);

const bool resp = bridge->call(TCP_CONNECT_SSL_METHOD, connection_id, hostname, port, ca_cert_str);
const bool resp = bridge->call(TCP_CONNECT_SSL_METHOD, hostname, port, ca_cert_str).result(connection_id);

if (!resp) {
_connected = false;
Expand Down Expand Up @@ -116,7 +116,7 @@ class BridgeTCPClient : public Client {
}

size_t written;
const bool ret = bridge->call(TCP_WRITE_METHOD, written, connection_id, payload);
const bool ret = bridge->call(TCP_WRITE_METHOD, connection_id, payload).result(written);
if (ret) {
return written;
}
Expand Down Expand Up @@ -170,7 +170,7 @@ class BridgeTCPClient : public Client {
void stop() override {
k_mutex_lock(&client_mutex, K_FOREVER);
String msg;
const bool resp = bridge->call(TCP_CLOSE_METHOD, msg, connection_id);
const bool resp = bridge->call(TCP_CLOSE_METHOD, connection_id).result(msg);
if (resp) {
_connected = false;
}
Expand Down Expand Up @@ -198,7 +198,7 @@ class BridgeTCPClient : public Client {
k_mutex_lock(&client_mutex, K_FOREVER);

MsgPack::arr_t<uint8_t> message;
const bool ret = bridge->call(TCP_READ_METHOD, message, connection_id, size);
const bool ret = bridge->call(TCP_READ_METHOD, connection_id, size).result(message);

if (ret) {
for (size_t i = 0; i < message.size(); ++i) {
Expand Down
6 changes: 3 additions & 3 deletions src/tcp_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class BridgeTCPServer final: public Server {
k_mutex_lock(&server_mutex, K_FOREVER);

String hostname = _addr.toString();
_listening = bridge->call(TCP_LISTEN_METHOD, listener_id, hostname, _port);
_listening = bridge->call(TCP_LISTEN_METHOD, hostname, _port).result(listener_id);

k_mutex_unlock(&server_mutex);

Expand All @@ -70,7 +70,7 @@ class BridgeTCPServer final: public Server {

k_mutex_lock(&server_mutex, K_FOREVER);

const bool ret = bridge->call(TCP_ACCEPT_METHOD, connection_id, listener_id);
const bool ret = bridge->call(TCP_ACCEPT_METHOD, listener_id).result(connection_id);

k_mutex_unlock(&server_mutex);

Expand Down Expand Up @@ -104,7 +104,7 @@ class BridgeTCPServer final: public Server {
k_mutex_lock(&server_mutex, K_FOREVER);

String msg;
const bool ret = bridge->call(TCP_CLOSE_LISTENER_METHOD, msg, listener_id);
const bool ret = bridge->call(TCP_CLOSE_LISTENER_METHOD, listener_id).result(msg);

if (ret) {
_listening = false;
Expand Down