Skip to content
Draft
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: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ set(sources
mobile_data.h
relay.c
relay.h
reon.c
reon.h
serial.c
serial.h
util.c
Expand Down
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ libmobile_la_SOURCES = \
mobile_data.h \
relay.c \
relay.h \
reon.c \
reon.h \
serial.c \
serial.h \
util.c \
Expand Down
99 changes: 99 additions & 0 deletions callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "mobile_data.h"
#include "compat.h"
#include "reon.h"

// Optional implementations of the callback functions.
// Library internals will always call `mobile_impl_` functions, which will be
Expand Down Expand Up @@ -100,6 +101,78 @@ IMPL void mobile_impl_update_number(A_UNUSED void *user, A_UNUSED enum mobile_nu
{
return;
}

IMPL const char *mobile_impl_reon_impl_name(A_UNUSED void *user)
{
return NULL;
}

IMPL const char *mobile_impl_reon_get_number(A_UNUSED void *user)
{
return NULL;
}

IMPL bool mobile_impl_reon_get_current_ip(A_UNUSED void *user, A_UNUSED struct mobile_addr *addr)
{
return false;
}

IMPL bool mobile_impl_reon_get_baud_rate(A_UNUSED void *user, A_UNUSED unsigned *baud_rate, A_UNUSED bool *writable)
{
return false;
}

IMPL bool mobile_impl_reon_set_baud_rate(A_UNUSED void *user, A_UNUSED unsigned baud_rate)
{
return false;
}

IMPL bool mobile_impl_reon_wifi_ap_count(A_UNUSED void *user, A_UNUSED unsigned *count)
{
return false;
}

IMPL bool mobile_impl_reon_wifi_ap_get(A_UNUSED void *user, A_UNUSED unsigned index,
A_UNUSED char *ssid, A_UNUSED signed char *rssi, A_UNUSED unsigned char *security)
{
return false;
}

IMPL bool mobile_impl_reon_bt_device_count(A_UNUSED void *user, A_UNUSED unsigned *count)
{
return false;
}

IMPL bool mobile_impl_reon_bt_device_get(A_UNUSED void *user, A_UNUSED unsigned index,
A_UNUSED unsigned char *mac, A_UNUSED char *name)
{
return false;
}

IMPL bool mobile_impl_reon_custom_count(A_UNUSED void *user, A_UNUSED unsigned *count)
{
return false;
}

IMPL bool mobile_impl_reon_custom_get_desc(A_UNUSED void *user, A_UNUSED unsigned index,
A_UNUSED unsigned char *id, A_UNUSED unsigned char *type,
A_UNUSED unsigned char *flags, A_UNUSED char *name)
{
return false;
}

IMPL int mobile_impl_reon_custom_get_value(A_UNUSED void *user, A_UNUSED unsigned char id,
A_UNUSED unsigned char *buffer, A_UNUSED size_t buffer_size)
{
return 0;
}

IMPL int mobile_impl_reon_custom_set_value(A_UNUSED void *user, A_UNUSED unsigned char id,
A_UNUSED unsigned char type, A_UNUSED const unsigned char *value,
A_UNUSED unsigned char value_len)
{
return MOBILE_REON_ERROR_INVALID_PARAM;
}
#endif

void mobile_callback_init(struct mobile_adapter *adapter)
Expand All @@ -121,6 +194,19 @@ void mobile_callback_init(struct mobile_adapter *adapter)
adapter->callback.sock_send = mobile_impl_sock_send;
adapter->callback.sock_recv = mobile_impl_sock_recv;
adapter->callback.update_number = mobile_impl_update_number;
adapter->callback.reon_impl_name = mobile_impl_reon_impl_name;
adapter->callback.reon_get_number = mobile_impl_reon_get_number;
adapter->callback.reon_get_current_ip = mobile_impl_reon_get_current_ip;
adapter->callback.reon_get_baud_rate = mobile_impl_reon_get_baud_rate;
adapter->callback.reon_set_baud_rate = mobile_impl_reon_set_baud_rate;
adapter->callback.reon_wifi_ap_count = mobile_impl_reon_wifi_ap_count;
adapter->callback.reon_wifi_ap_get = mobile_impl_reon_wifi_ap_get;
adapter->callback.reon_bt_device_count = mobile_impl_reon_bt_device_count;
adapter->callback.reon_bt_device_get = mobile_impl_reon_bt_device_get;
adapter->callback.reon_custom_count = mobile_impl_reon_custom_count;
adapter->callback.reon_custom_get_desc = mobile_impl_reon_custom_get_desc;
adapter->callback.reon_custom_get_value = mobile_impl_reon_custom_get_value;
adapter->callback.reon_custom_set_value = mobile_impl_reon_custom_set_value;
#endif
}

Expand All @@ -146,4 +232,17 @@ def(sock_accept)
def(sock_send)
def(sock_recv)
def(update_number)
def(reon_impl_name)
def(reon_get_number)
def(reon_get_current_ip)
def(reon_get_baud_rate)
def(reon_set_baud_rate)
def(reon_wifi_ap_count)
def(reon_wifi_ap_get)
def(reon_bt_device_count)
def(reon_bt_device_get)
def(reon_custom_count)
def(reon_custom_get_desc)
def(reon_custom_get_value)
def(reon_custom_set_value)
#endif
26 changes: 26 additions & 0 deletions callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ struct mobile_adapter_callback {
mobile_func_sock_send sock_send;
mobile_func_sock_recv sock_recv;
mobile_func_update_number update_number;
mobile_func_reon_impl_name reon_impl_name;
mobile_func_reon_get_number reon_get_number;
mobile_func_reon_get_current_ip reon_get_current_ip;
mobile_func_reon_get_baud_rate reon_get_baud_rate;
mobile_func_reon_set_baud_rate reon_set_baud_rate;
mobile_func_reon_wifi_ap_count reon_wifi_ap_count;
mobile_func_reon_wifi_ap_get reon_wifi_ap_get;
mobile_func_reon_bt_device_count reon_bt_device_count;
mobile_func_reon_bt_device_get reon_bt_device_get;
mobile_func_reon_custom_count reon_custom_count;
mobile_func_reon_custom_get_desc reon_custom_get_desc;
mobile_func_reon_custom_get_value reon_custom_get_value;
mobile_func_reon_custom_set_value reon_custom_set_value;
#endif
};
void mobile_callback_init(struct mobile_adapter *adapter);
Expand Down Expand Up @@ -63,3 +76,16 @@ void mobile_callback_init(struct mobile_adapter *adapter);
#define mobile_cb_sock_send(...) _mobile_cb(sock_send, __VA_ARGS__)
#define mobile_cb_sock_recv(...) _mobile_cb(sock_recv, __VA_ARGS__)
#define mobile_cb_update_number(...) _mobile_cb(update_number, __VA_ARGS__)
#define mobile_cb_reon_impl_name(...) _mobile_cb(reon_impl_name, __VA_ARGS__)
#define mobile_cb_reon_get_number(...) _mobile_cb(reon_get_number, __VA_ARGS__)
#define mobile_cb_reon_get_current_ip(...) _mobile_cb(reon_get_current_ip, __VA_ARGS__)
#define mobile_cb_reon_get_baud_rate(...) _mobile_cb(reon_get_baud_rate, __VA_ARGS__)
#define mobile_cb_reon_set_baud_rate(...) _mobile_cb(reon_set_baud_rate, __VA_ARGS__)
#define mobile_cb_reon_wifi_ap_count(...) _mobile_cb(reon_wifi_ap_count, __VA_ARGS__)
#define mobile_cb_reon_wifi_ap_get(...) _mobile_cb(reon_wifi_ap_get, __VA_ARGS__)
#define mobile_cb_reon_bt_device_count(...) _mobile_cb(reon_bt_device_count, __VA_ARGS__)
#define mobile_cb_reon_bt_device_get(...) _mobile_cb(reon_bt_device_get, __VA_ARGS__)
#define mobile_cb_reon_custom_count(...) _mobile_cb(reon_custom_count, __VA_ARGS__)
#define mobile_cb_reon_custom_get_desc(...) _mobile_cb(reon_custom_get_desc, __VA_ARGS__)
#define mobile_cb_reon_custom_get_value(...) _mobile_cb(reon_custom_get_value, __VA_ARGS__)
#define mobile_cb_reon_custom_set_value(...) _mobile_cb(reon_custom_set_value, __VA_ARGS__)
28 changes: 23 additions & 5 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "mobile_inet.h"
#include "util.h"
#include "compat.h"
#include "reon.h"

#ifdef MOBILE_LIBCONF_USE
#include <mobile_config.h>
Expand Down Expand Up @@ -134,11 +135,12 @@ static void do_end_session(struct mobile_adapter *adapter)
s->mode_32bit = false;
}

static void do_start_session(struct mobile_adapter *adapter)
static void do_start_session(struct mobile_adapter *adapter, bool reon_mode)
{
struct mobile_adapter_commands *s = &adapter->commands;

s->session_started = true;
s->reon_mode = reon_mode;
s->state = MOBILE_CONNECTION_DISCONNECTED;
memset(s->connections, false, sizeof(s->connections));

Expand All @@ -162,7 +164,7 @@ static struct mobile_packet *command_start(struct mobile_adapter *adapter, struc
if (s->session_started) return error_packet(packet, 1);
if (packet->length == sizeof(happy) &&
memcmp_P(packet->data, happy, sizeof(happy)) == 0) {
do_start_session(adapter);
do_start_session(adapter, true); // REON mode enabled
return packet;
}
if (adapter->serial.device != MOBILE_ADAPTER_RED) {
Expand All @@ -175,7 +177,7 @@ static struct mobile_packet *command_start(struct mobile_adapter *adapter, struc
return error_packet(packet, 2);
}

do_start_session(adapter);
do_start_session(adapter, false); // Standard mode
return packet;
}

Expand Down Expand Up @@ -659,9 +661,14 @@ static struct mobile_packet *command_data(struct mobile_adapter *adapter, struct
// 2 - Still connected/failed to disconnect(?)
static struct mobile_packet *command_reinit(struct mobile_adapter *adapter, struct mobile_packet *packet)
{
struct mobile_adapter_commands *s = &adapter->commands;

// Preserve REON mode across reinit
bool reon_mode = s->reon_mode;

// Reset everything without ending the session
do_end_session(adapter);
do_start_session(adapter);
do_start_session(adapter, reon_mode);

packet->length = 0;
return packet;
Expand Down Expand Up @@ -1205,6 +1212,12 @@ struct mobile_packet *mobile_commands_process(struct mobile_adapter *adapter, st
return command_udp_disconnect(adapter, packet);
case MOBILE_COMMAND_DNS_REQUEST:
return command_dns_request(adapter, packet);
case MOBILE_COMMAND_REON_GET_OPTIONS:
return command_reon_get_options(adapter, packet);
case MOBILE_COMMAND_REON_GET_VALUE:
return command_reon_get_value(adapter, packet);
case MOBILE_COMMAND_REON_SET_VALUE:
return command_reon_set_value(adapter, packet);
case MOBILE_COMMAND_TEST_MODE:
return command_test_mode(adapter, packet);
default:
Expand All @@ -1213,7 +1226,7 @@ struct mobile_packet *mobile_commands_process(struct mobile_adapter *adapter, st
}
}

bool mobile_commands_exists(enum mobile_command command)
bool mobile_commands_exists(struct mobile_adapter *adapter, enum mobile_command command)
{
// Used by serial.c:mobile_serial_transfer() to check if a command may be used

Expand All @@ -1239,6 +1252,11 @@ bool mobile_commands_exists(enum mobile_command command)
case MOBILE_COMMAND_DNS_REQUEST:
case MOBILE_COMMAND_TEST_MODE:
return true;
// REON commands only exist when in REON mode
case MOBILE_COMMAND_REON_GET_OPTIONS:
case MOBILE_COMMAND_REON_GET_VALUE:
case MOBILE_COMMAND_REON_SET_VALUE:
return adapter->commands.reon_mode;
default:
return false;
}
Expand Down
6 changes: 5 additions & 1 deletion commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ enum mobile_command {
MOBILE_COMMAND_UDP_CONNECT,
MOBILE_COMMAND_UDP_DISCONNECT,
MOBILE_COMMAND_DNS_REQUEST = 0x28,
MOBILE_COMMAND_REON_GET_OPTIONS = 0x30,
MOBILE_COMMAND_REON_GET_VALUE,
MOBILE_COMMAND_REON_SET_VALUE,
MOBILE_COMMAND_TEST_MODE = 0x3F,
MOBILE_COMMAND_ERROR = 0x6E
};
Expand Down Expand Up @@ -65,13 +68,14 @@ struct mobile_adapter_commands {
enum mobile_connection_state state;
bool connections[MOBILE_MAX_CONNECTIONS];
bool dns2_use;
bool reon_mode;
struct mobile_addr4 dns1;
struct mobile_addr4 dns2;
};

void mobile_commands_init(struct mobile_adapter *adapter);
void mobile_commands_reset(struct mobile_adapter *adapter);
struct mobile_packet *mobile_commands_process(struct mobile_adapter *adapter, struct mobile_packet *packet);
bool mobile_commands_exists(enum mobile_command command);
bool mobile_commands_exists(struct mobile_adapter *adapter, enum mobile_command command);

#undef _Atomic // "atomic.h"
70 changes: 70 additions & 0 deletions debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string.h>

#include "mobile_data.h"
#include "commands.h"
#include "compat.h"

void mobile_debug_init(struct mobile_adapter *adapter)
Expand Down Expand Up @@ -376,6 +377,75 @@ void mobile_debug_command(struct mobile_adapter *adapter, const struct mobile_pa
packet_end(adapter, packet, 2);
break;

case MOBILE_COMMAND_REON_GET_OPTIONS:
debug_print("REON Get Options");
if (!send) {
if (packet->length < 2) {
packet_end(adapter, packet, 0);
break;
}
debug_print(" (offset: %u, count: %u)",
packet->data[0], packet->data[1]);
packet_end(adapter, packet, 2);
} else {
if (packet->length < 2) {
packet_end(adapter, packet, 0);
break;
}
debug_print(" (total: %u, returned: %u)",
packet->data[0], packet->data[1]);
packet_end(adapter, packet, packet->length);
}
break;

case MOBILE_COMMAND_REON_GET_VALUE:
debug_print("REON Get Value");
if (!send) {
if (packet->length < 1) {
packet_end(adapter, packet, 0);
break;
}
debug_print(" (option: 0x%02X)", packet->data[0]);
packet_end(adapter, packet, packet->length);
} else {
if (packet->length < 3) {
packet_end(adapter, packet, 0);
break;
}
debug_print(" (option: 0x%02X, type: 0x%02X, len: %u)",
packet->data[0], packet->data[1], packet->data[2]);
if (packet->length > 3) {
dump_hex(adapter, packet->data + 3, packet->length - 3);
} else {
debug_endl();
}
}
break;

case MOBILE_COMMAND_REON_SET_VALUE:
debug_print("REON Set Value");
if (!send) {
if (packet->length < 3) {
packet_end(adapter, packet, 0);
break;
}
debug_print(" (option: 0x%02X, type: 0x%02X, len: %u)",
packet->data[0], packet->data[1], packet->data[2]);
if (packet->length > 3) {
dump_hex(adapter, packet->data + 3, packet->length - 3);
} else {
debug_endl();
}
} else {
if (packet->length < 1) {
packet_end(adapter, packet, 0);
break;
}
debug_print(" (option: 0x%02X) OK", packet->data[0]);
packet_end(adapter, packet, 1);
}
break;

default:
debug_print("Unknown");
dump_hex(adapter, packet->data, packet->length);
Expand Down
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ sources = [
'mobile_data.h',
'relay.c',
'relay.h',
'reon.c',
'reon.h',
'serial.c',
'serial.h',
'util.c',
Expand Down
Loading