417 lines
12 KiB
C++
417 lines
12 KiB
C++
#include "protocol/protocol_message_builder.h"
|
|
|
|
#include "protocol/utf8_text.h"
|
|
|
|
#include <cstring>
|
|
|
|
#include <cJSON.h>
|
|
|
|
namespace borla {
|
|
|
|
bool ProtocolMessageBuilder::BuildHello(
|
|
uint8_t protocol_version,
|
|
char* output,
|
|
size_t capacity,
|
|
size_t* output_size) const
|
|
{
|
|
return BuildHello(
|
|
protocol_version,
|
|
nullptr,
|
|
output,
|
|
capacity,
|
|
output_size);
|
|
}
|
|
|
|
bool ProtocolMessageBuilder::BuildHello(
|
|
uint8_t protocol_version,
|
|
const MemorySnapshot* memory,
|
|
char* output,
|
|
size_t capacity,
|
|
size_t* output_size) const
|
|
{
|
|
cJSON* root = cJSON_CreateObject();
|
|
cJSON* features = cJSON_CreateObject();
|
|
cJSON* audio = cJSON_CreateObject();
|
|
if (root == nullptr || features == nullptr || audio == nullptr) {
|
|
cJSON_Delete(root);
|
|
cJSON_Delete(features);
|
|
cJSON_Delete(audio);
|
|
return false;
|
|
}
|
|
|
|
cJSON_AddStringToObject(root, "type", "hello");
|
|
cJSON_AddNumberToObject(root, "version", protocol_version);
|
|
cJSON_AddStringToObject(root, "transport", "websocket");
|
|
cJSON_AddBoolToObject(features, "mcp", true);
|
|
cJSON_AddBoolToObject(features, "local_audio", true);
|
|
cJSON_AddBoolToObject(
|
|
features, "play_local_sequence", true);
|
|
cJSON_AddBoolToObject(
|
|
features, "barge_in_two_phase", true);
|
|
cJSON_AddItemToObject(root, "features", features);
|
|
cJSON_AddStringToObject(audio, "format", "opus");
|
|
cJSON_AddNumberToObject(audio, "sample_rate", 16000);
|
|
cJSON_AddNumberToObject(audio, "channels", 1);
|
|
cJSON_AddNumberToObject(audio, "frame_duration", 60);
|
|
cJSON_AddItemToObject(root, "audio_params", audio);
|
|
if (memory != nullptr && memory->HasData() &&
|
|
IsValidMemorySnapshot(*memory)) {
|
|
cJSON* cache = cJSON_CreateObject();
|
|
if (cache == nullptr) {
|
|
cJSON_Delete(root);
|
|
return false;
|
|
}
|
|
if (memory->child_name[0] != '\0') {
|
|
cJSON_AddStringToObject(
|
|
cache,
|
|
"child_name",
|
|
memory->child_name.data());
|
|
}
|
|
if (memory->brain_text[0] != '\0') {
|
|
cJSON_AddStringToObject(
|
|
cache,
|
|
"brain_text",
|
|
memory->brain_text.data());
|
|
}
|
|
cJSON_AddNumberToObject(
|
|
cache,
|
|
"interaction_count",
|
|
memory->interaction_count);
|
|
cJSON_AddItemToObject(root, "memory_cache", cache);
|
|
}
|
|
return Print(root, output, capacity, output_size);
|
|
}
|
|
|
|
bool ProtocolMessageBuilder::BuildStartListening(
|
|
const char* session_id,
|
|
char* output,
|
|
size_t capacity,
|
|
size_t* output_size) const
|
|
{
|
|
cJSON* root = cJSON_CreateObject();
|
|
if (root == nullptr || session_id == nullptr) {
|
|
cJSON_Delete(root);
|
|
return false;
|
|
}
|
|
cJSON_AddStringToObject(root, "session_id", session_id);
|
|
cJSON_AddStringToObject(root, "type", "listen");
|
|
cJSON_AddStringToObject(root, "state", "start");
|
|
cJSON_AddStringToObject(root, "mode", "auto");
|
|
return Print(root, output, capacity, output_size);
|
|
}
|
|
|
|
bool ProtocolMessageBuilder::BuildStopListening(
|
|
const char* session_id,
|
|
char* output,
|
|
size_t capacity,
|
|
size_t* output_size) const
|
|
{
|
|
cJSON* root = cJSON_CreateObject();
|
|
if (root == nullptr || session_id == nullptr) {
|
|
cJSON_Delete(root);
|
|
return false;
|
|
}
|
|
cJSON_AddStringToObject(root, "session_id", session_id);
|
|
cJSON_AddStringToObject(root, "type", "listen");
|
|
cJSON_AddStringToObject(root, "state", "stop");
|
|
return Print(root, output, capacity, output_size);
|
|
}
|
|
|
|
bool ProtocolMessageBuilder::BuildAbort(
|
|
const char* session_id,
|
|
ConversationInterruptSource source,
|
|
const char* playback_text,
|
|
char* output,
|
|
size_t capacity,
|
|
size_t* output_size) const
|
|
{
|
|
cJSON* root = cJSON_CreateObject();
|
|
if (root == nullptr || session_id == nullptr) {
|
|
cJSON_Delete(root);
|
|
return false;
|
|
}
|
|
cJSON_AddStringToObject(root, "session_id", session_id);
|
|
cJSON_AddStringToObject(root, "type", "abort");
|
|
cJSON_AddStringToObject(
|
|
root,
|
|
"reason",
|
|
source == ConversationInterruptSource::kWakeWordDetected
|
|
? "wake_word_detected"
|
|
: "user_initiated");
|
|
if (playback_text != nullptr && playback_text[0] != '\0') {
|
|
const size_t kept = utf8_text::SafeTruncatedLength(
|
|
playback_text,
|
|
std::strlen(playback_text),
|
|
kPlaybackTextBytes);
|
|
if (kept != 0) {
|
|
// cJSON escapes quotes and control characters on its own, so the
|
|
// only way this field can corrupt the frame is an encoding split —
|
|
// which is exactly what SafeTruncatedLength rules out.
|
|
char truncated[kPlaybackTextBytes + 1] = {};
|
|
std::memcpy(truncated, playback_text, kept);
|
|
cJSON_AddStringToObject(
|
|
root, "playback_text", truncated);
|
|
}
|
|
}
|
|
return Print(root, output, capacity, output_size);
|
|
}
|
|
|
|
bool ProtocolMessageBuilder::BuildPing(
|
|
char* output,
|
|
size_t capacity,
|
|
size_t* output_size) const
|
|
{
|
|
cJSON* root = cJSON_CreateObject();
|
|
if (root == nullptr) {
|
|
return false;
|
|
}
|
|
cJSON_AddStringToObject(root, "type", "ping");
|
|
return Print(root, output, capacity, output_size);
|
|
}
|
|
|
|
bool ProtocolMessageBuilder::BuildTraceMarker(
|
|
const char* trace_id,
|
|
const char* stage,
|
|
uint64_t sequence,
|
|
size_t frames,
|
|
size_t bytes,
|
|
uint64_t client_send_ms,
|
|
int64_t client_epoch_ms,
|
|
char* output,
|
|
size_t capacity,
|
|
size_t* output_size) const
|
|
{
|
|
cJSON* root = cJSON_CreateObject();
|
|
if (root == nullptr || stage == nullptr) {
|
|
cJSON_Delete(root);
|
|
return false;
|
|
}
|
|
cJSON_AddStringToObject(root, "type", "trace");
|
|
if (trace_id != nullptr && trace_id[0] != '\0') {
|
|
cJSON_AddStringToObject(root, "trace_id", trace_id);
|
|
}
|
|
cJSON_AddStringToObject(root, "stage", stage);
|
|
cJSON_AddNumberToObject(
|
|
root, "seq", static_cast<double>(sequence));
|
|
cJSON_AddNumberToObject(
|
|
root, "frames", static_cast<double>(frames));
|
|
cJSON_AddNumberToObject(
|
|
root, "bytes", static_cast<double>(bytes));
|
|
cJSON_AddNumberToObject(
|
|
root,
|
|
"client_send_ms",
|
|
static_cast<double>(client_send_ms));
|
|
cJSON_AddNumberToObject(
|
|
root,
|
|
"client_epoch_ms",
|
|
static_cast<double>(client_epoch_ms));
|
|
return Print(root, output, capacity, output_size);
|
|
}
|
|
|
|
bool ProtocolMessageBuilder::BuildGoodbye(
|
|
char* output,
|
|
size_t capacity,
|
|
size_t* output_size) const
|
|
{
|
|
cJSON* root = cJSON_CreateObject();
|
|
if (root == nullptr) {
|
|
return false;
|
|
}
|
|
cJSON_AddStringToObject(root, "type", "goodbye");
|
|
return Print(root, output, capacity, output_size);
|
|
}
|
|
|
|
bool ProtocolMessageBuilder::BuildPong(
|
|
char* output,
|
|
size_t capacity,
|
|
size_t* output_size) const
|
|
{
|
|
cJSON* root = cJSON_CreateObject();
|
|
if (root == nullptr) {
|
|
return false;
|
|
}
|
|
cJSON_AddStringToObject(root, "type", "pong");
|
|
return Print(root, output, capacity, output_size);
|
|
}
|
|
|
|
bool ProtocolMessageBuilder::BuildButton(
|
|
uint8_t click_count,
|
|
char* output,
|
|
size_t capacity,
|
|
size_t* output_size) const
|
|
{
|
|
if (click_count != 3 && click_count != 4) {
|
|
return false;
|
|
}
|
|
cJSON* root = cJSON_CreateObject();
|
|
if (root == nullptr) {
|
|
return false;
|
|
}
|
|
cJSON_AddStringToObject(root, "type", "button");
|
|
cJSON_AddNumberToObject(root, "count", click_count);
|
|
return Print(root, output, capacity, output_size);
|
|
}
|
|
|
|
bool ProtocolMessageBuilder::BuildLocalPlaybackState(
|
|
const char* group,
|
|
bool started,
|
|
char* output,
|
|
size_t capacity,
|
|
size_t* output_size) const
|
|
{
|
|
if (group == nullptr || group[0] == '\0') {
|
|
return false;
|
|
}
|
|
cJSON* root = cJSON_CreateObject();
|
|
if (root == nullptr) {
|
|
return false;
|
|
}
|
|
cJSON_AddStringToObject(root, "type", "play_local");
|
|
cJSON_AddStringToObject(
|
|
root, "state", started ? "start" : "done");
|
|
cJSON_AddStringToObject(root, "group", group);
|
|
return Print(root, output, capacity, output_size);
|
|
}
|
|
|
|
bool ProtocolMessageBuilder::BuildStatus(
|
|
uint8_t battery_percent,
|
|
uint16_t battery_voltage_mv,
|
|
bool charging,
|
|
const char* version,
|
|
int16_t volume_percent,
|
|
char* output,
|
|
size_t capacity,
|
|
size_t* output_size) const
|
|
{
|
|
cJSON* root = cJSON_CreateObject();
|
|
if (root == nullptr || version == nullptr) {
|
|
cJSON_Delete(root);
|
|
return false;
|
|
}
|
|
cJSON_AddStringToObject(root, "type", "status");
|
|
cJSON_AddNumberToObject(root, "battery", battery_percent);
|
|
cJSON_AddNumberToObject(
|
|
root, "battery_voltage_mv", battery_voltage_mv);
|
|
cJSON_AddBoolToObject(root, "charging", charging);
|
|
cJSON_AddStringToObject(root, "version", version);
|
|
if (volume_percent >= 0) {
|
|
cJSON_AddNumberToObject(root, "volume", volume_percent);
|
|
}
|
|
return Print(root, output, capacity, output_size);
|
|
}
|
|
|
|
bool ProtocolMessageBuilder::BuildAlarmFired(
|
|
int32_t alarm_id,
|
|
int64_t fired_at_epoch,
|
|
char* output,
|
|
size_t capacity,
|
|
size_t* output_size) const
|
|
{
|
|
cJSON* root = cJSON_CreateObject();
|
|
if (root == nullptr) {
|
|
return false;
|
|
}
|
|
cJSON_AddStringToObject(root, "type", "alarm_fired");
|
|
cJSON_AddNumberToObject(root, "id", alarm_id);
|
|
cJSON_AddNumberToObject(
|
|
root, "fired_at", static_cast<double>(fired_at_epoch));
|
|
return Print(root, output, capacity, output_size);
|
|
}
|
|
|
|
bool ProtocolMessageBuilder::BuildCallAction(
|
|
const char* state,
|
|
const char* call_id,
|
|
char* output,
|
|
size_t capacity,
|
|
size_t* output_size) const
|
|
{
|
|
cJSON* root = cJSON_CreateObject();
|
|
if (root == nullptr || state == nullptr || call_id == nullptr ||
|
|
call_id[0] == '\0') {
|
|
cJSON_Delete(root);
|
|
return false;
|
|
}
|
|
cJSON_AddStringToObject(root, "type", "call");
|
|
cJSON_AddStringToObject(root, "state", state);
|
|
cJSON_AddStringToObject(root, "call_id", call_id);
|
|
return Print(root, output, capacity, output_size);
|
|
}
|
|
|
|
bool ProtocolMessageBuilder::BuildOtaNotify(
|
|
const char* text,
|
|
bool optional,
|
|
char* output,
|
|
size_t capacity,
|
|
size_t* output_size) const
|
|
{
|
|
cJSON* root = cJSON_CreateObject();
|
|
if (root == nullptr || text == nullptr || text[0] == '\0') {
|
|
cJSON_Delete(root);
|
|
return false;
|
|
}
|
|
cJSON_AddStringToObject(root, "type", "ota_notify");
|
|
cJSON_AddStringToObject(root, "text", text);
|
|
if (optional) {
|
|
// Only emitted for advisory notices so the existing server contract
|
|
// (absent field == mandatory upgrade) is preserved byte for byte.
|
|
cJSON_AddBoolToObject(root, "optional", true);
|
|
}
|
|
return Print(root, output, capacity, output_size);
|
|
}
|
|
|
|
bool ProtocolMessageBuilder::BuildFriendAdd(
|
|
const char* local_mac,
|
|
const char* local_name,
|
|
const char* remote_mac,
|
|
const char* remote_name,
|
|
char* output,
|
|
size_t capacity,
|
|
size_t* output_size) const
|
|
{
|
|
cJSON* root = cJSON_CreateObject();
|
|
if (root == nullptr || local_mac == nullptr ||
|
|
local_name == nullptr || remote_mac == nullptr ||
|
|
remote_name == nullptr || local_mac[0] == '\0' ||
|
|
remote_mac[0] == '\0') {
|
|
cJSON_Delete(root);
|
|
return false;
|
|
}
|
|
cJSON_AddStringToObject(root, "type", "friend_add");
|
|
cJSON_AddStringToObject(root, "local_mac", local_mac);
|
|
cJSON_AddStringToObject(root, "local_name", local_name);
|
|
cJSON_AddStringToObject(root, "remote_mac", remote_mac);
|
|
cJSON_AddStringToObject(root, "remote_name", remote_name);
|
|
return Print(root, output, capacity, output_size);
|
|
}
|
|
|
|
bool ProtocolMessageBuilder::Print(
|
|
void* opaque_root,
|
|
char* output,
|
|
size_t capacity,
|
|
size_t* output_size) const
|
|
{
|
|
auto* root = static_cast<cJSON*>(opaque_root);
|
|
if (root == nullptr || output == nullptr ||
|
|
capacity == 0 || output_size == nullptr) {
|
|
cJSON_Delete(root);
|
|
return false;
|
|
}
|
|
|
|
char* rendered = cJSON_PrintUnformatted(root);
|
|
cJSON_Delete(root);
|
|
if (rendered == nullptr) {
|
|
return false;
|
|
}
|
|
const size_t size = std::strlen(rendered);
|
|
if (size >= capacity) {
|
|
cJSON_free(rendered);
|
|
return false;
|
|
}
|
|
std::memcpy(output, rendered, size + 1);
|
|
*output_size = size;
|
|
cJSON_free(rendered);
|
|
return true;
|
|
}
|
|
|
|
} // namespace borla
|