Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
500Foods authored Jul 19, 2024
1 parent 2710d33 commit 60db7f8
Show file tree
Hide file tree
Showing 10 changed files with 1,416 additions and 105 deletions.
169 changes: 101 additions & 68 deletions elements/001-hydrogen/hydrogen/src/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,51 +68,67 @@ char* get_file_modification_time(const char* filename) {

void create_default_config(const char* config_path) {
json_t* root = json_object();
json_object_set_new(root, "ServerName", json_string(DEFAULT_SERVER_NAME));
json_object_set_new(root, "WebPort", json_integer(DEFAULT_WEB_PORT));
json_object_set_new(root, "WebsocketPort", json_integer(DEFAULT_WEBSOCKET_PORT));
json_object_set_new(root, "UploadPath", json_string(DEFAULT_UPLOAD_PATH));
json_object_set_new(root, "UploadDir", json_string(DEFAULT_UPLOAD_DIR));
json_object_set_new(root, "MaxUploadSize", json_integer(DEFAULT_MAX_UPLOAD_SIZE));
json_object_set_new(root, "LogFile", json_string(DEFAULT_LOG_FILE));

// Server Name
json_object_set_new(root, "ServerName", json_string("Philement/hydrogen"));

// Log File
json_object_set_new(root, "LogFile", json_string("/var/log/hydrogen.log"));

// Web Configuration
json_t* web = json_object();
json_object_set_new(web, "Port", json_integer(5000));
json_object_set_new(web, "WebRoot", json_string("/home/asimard/lithium"));
json_object_set_new(web, "UploadPath", json_string("/api/upload"));
json_object_set_new(web, "UploadDir", json_string("/tmp/hydrogen_uploads"));
json_object_set_new(web, "MaxUploadSize", json_integer(2147483648));
json_object_set_new(root, "Web", web);

// WebSocket Configuration
json_t* websocket = json_object();
json_object_set_new(websocket, "Port", json_integer(5001));
json_object_set_new(websocket, "Key", json_string("default_key_change_me"));
json_object_set_new(websocket, "Protocol", json_string("hydrogen-protocol"));
json_object_set_new(root, "WebSocket", websocket);

// mDNS Configuration
json_t* mdns = json_object();
json_object_set_new(mdns, "DeviceId", json_string("hydrogen-printer"));
json_object_set_new(mdns, "FriendlyName", json_string("Hydrogen 3D Printer"));
json_object_set_new(mdns, "Model", json_string("Hydrogen"));
json_object_set_new(mdns, "Manufacturer", json_string("Philement"));
json_object_set_new(mdns, "Version", json_string(VERSION));
json_object_set_new(mdns, "Version", json_string("0.1.0"));

json_t* services = json_array();

json_t* web_service = json_object();
json_object_set_new(web_service, "Name", json_string("Hydrogen Web Interface"));
json_object_set_new(web_service, "Type", json_string("_http._tcp"));
json_object_set_new(web_service, "Port", json_integer(DEFAULT_WEB_PORT));
json_object_set_new(web_service, "TxtRecords", json_string("path=/api/upload"));
json_array_append_new(services, web_service);
json_t* http_service = json_object();
json_object_set_new(http_service, "Name", json_string("hydrogen"));
json_object_set_new(http_service, "Type", json_string("_http._tcp.local"));
json_object_set_new(http_service, "Port", json_integer(5000));
json_object_set_new(http_service, "TxtRecords", json_string("path=/api/upload"));
json_array_append_new(services, http_service);

json_t* octoprint_service = json_object();
json_object_set_new(octoprint_service, "Name", json_string("Hydrogen OctoPrint Emulation"));
json_object_set_new(octoprint_service, "Type", json_string("_octoprint._tcp"));
json_object_set_new(octoprint_service, "Port", json_integer(DEFAULT_WEB_PORT));
json_object_set_new(octoprint_service, "Name", json_string("hydrogen"));
json_object_set_new(octoprint_service, "Type", json_string("_octoprint._tcp.local"));
json_object_set_new(octoprint_service, "Port", json_integer(5000));
json_object_set_new(octoprint_service, "TxtRecords", json_string("path=/api,version=1.1.0"));
json_array_append_new(services, octoprint_service);

json_t* websocket_service = json_object();
json_object_set_new(websocket_service, "Name", json_string("Hydrogen WebSocket"));
json_object_set_new(websocket_service, "Type", json_string("_websocket._tcp"));
json_object_set_new(websocket_service, "Port", json_integer(DEFAULT_WEBSOCKET_PORT));
json_object_set_new(websocket_service, "Name", json_string("Hydrogen"));
json_object_set_new(websocket_service, "Type", json_string("_websocket._tcp.local"));
json_object_set_new(websocket_service, "Port", json_integer(5001));
json_object_set_new(websocket_service, "TxtRecords", json_string("path=/websocket"));
json_array_append_new(services, websocket_service);

json_object_set_new(mdns, "Services", services);
json_object_set_new(root, "mDNS", mdns);

if (json_dump_file(root, config_path, JSON_INDENT(4)) != 0) {
fprintf(stderr, "Error: Unable to create default config at %s\n", config_path);
log_this("Configuration", "Error: Unable to create default config at %s", 3, true, true, true, config_path);
} else {
printf("Created default config at %s\n", config_path);
log_this("Configuration", "Created default config at %s", 0, true, true, true, config_path);
}

json_decref(root);
Expand All @@ -123,86 +139,103 @@ AppConfig* load_config(const char* config_path) {
json_t* root = json_load_file(config_path, 0, &error);

if (!root) {
fprintf(stderr, "Error: Failed to load config file: %s\n", error.text);
create_default_config(config_path);
root = json_load_file(config_path, 0, &error);
if (!root) {
fprintf(stderr, "Error: Failed to load default config file: %s\n", error.text);
return NULL;
}
log_this("Configuration", "Failed to load config file: %s", 3, true, true, true, error.text);
return NULL;
}

AppConfig* config = calloc(1, sizeof(AppConfig));
if (!config) {
fprintf(stderr, "Error: Failed to allocate memory for config\n");
log_this("Configuration", "Failed to allocate memory for config", 3, true, true, true);
json_decref(root);
return NULL;
}

config->server_name = strdup(json_string_value(json_object_get(root, "ServerName")));
config->web_port = json_integer_value(json_object_get(root, "WebPort"));
config->websocket_port = json_integer_value(json_object_get(root, "WebsocketPort"));
config->upload_path = strdup(json_string_value(json_object_get(root, "UploadPath")));
config->upload_dir = strdup(json_string_value(json_object_get(root, "UploadDir")));
config->max_upload_size = json_integer_value(json_object_get(root, "MaxUploadSize"));
config->log_file_path = strdup(json_string_value(json_object_get(root, "LogFile")));
config->web_root = strdup(json_string_value(json_object_get(root, "WebRoot")));
// Server Name
json_t* server_name = json_object_get(root, "ServerName");
if (json_is_string(server_name)) {
config->server_name = strdup(json_string_value(server_name));
}

// Log File
json_t* log_file = json_object_get(root, "LogFile");
if (json_is_string(log_file)) {
config->log_file_path = strdup(json_string_value(log_file));
}

// Web Configuration
json_t* web = json_object_get(root, "Web");
if (json_is_object(web)) {
config->web.port = json_integer_value(json_object_get(web, "Port"));
config->web.web_root = strdup(json_string_value(json_object_get(web, "WebRoot")));
config->web.upload_path = strdup(json_string_value(json_object_get(web, "UploadPath")));
config->web.upload_dir = strdup(json_string_value(json_object_get(web, "UploadDir")));
config->web.max_upload_size = json_integer_value(json_object_get(web, "MaxUploadSize"));
}

config->executable_path = get_executable_path();
// WebSocket Configuration
json_t* websocket = json_object_get(root, "WebSocket");
if (json_is_object(websocket)) {
config->websocket.port = json_integer_value(json_object_get(websocket, "Port"));
config->websocket.key = strdup(json_string_value(json_object_get(websocket, "Key")));
config->websocket.protocol = strdup(json_string_value(json_object_get(websocket, "Protocol")));

json_t* max_message_mb = json_object_get(websocket, "MaxMessageMB");
if (json_is_integer(max_message_mb)) {
config->websocket.max_message_size = json_integer_value(max_message_mb) * 1024 * 1024;
} else {
config->websocket.max_message_size = 10 * 1024 * 1024; // Default to 10 MB if not specified
}

}

// mDNS Configuration
json_t* mdns = json_object_get(root, "mDNS");
if (mdns) {
if (json_is_object(mdns)) {
config->mdns.device_id = strdup(json_string_value(json_object_get(mdns, "DeviceId")));
config->mdns.friendly_name = strdup(json_string_value(json_object_get(mdns, "FriendlyName")));
config->mdns.model = strdup(json_string_value(json_object_get(mdns, "Model")));
config->mdns.manufacturer = strdup(json_string_value(json_object_get(mdns, "Manufacturer")));
config->mdns.version = strdup(json_string_value(json_object_get(mdns, "Version")));

json_t* services = json_object_get(mdns, "Services");
if (services && json_is_array(services)) {
if (json_is_array(services)) {
config->mdns.num_services = json_array_size(services);
config->mdns.services = calloc(config->mdns.num_services, sizeof(mdns_service_t));

for (int i = 0; i < config->mdns.num_services; i++) {
for (size_t i = 0; i < config->mdns.num_services; i++) {
json_t* service = json_array_get(services, i);
config->mdns.services[i].name = strdup(json_string_value(json_object_get(service, "Name")));
config->mdns.services[i].type = strdup(json_string_value(json_object_get(service, "Type")));
config->mdns.services[i].port = json_integer_value(json_object_get(service, "Port"));

const char* txt_records_str = json_string_value(json_object_get(service, "TxtRecords"));
char* txt_records_copy = strdup(txt_records_str);
char* saveptr;
char* token = strtok_r(txt_records_copy, ",", &saveptr);
int num_txt_records = 0;
char** txt_records = NULL;

while (token != NULL) {
while (*token == ' ') token++;
char* end = token + strlen(token) - 1;
while (end > token && *end == ' ') end--;
*(end + 1) = '\0';

txt_records = realloc(txt_records, (num_txt_records + 1) * sizeof(char*));
txt_records[num_txt_records] = strdup(token);
num_txt_records++;
token = strtok_r(NULL, ",", &saveptr);

// Handle TXT records
json_t* txt_records = json_object_get(service, "TxtRecords");
if (json_is_string(txt_records)) {
// If TxtRecords is a single string, treat it as one record
config->mdns.services[i].num_txt_records = 1;
config->mdns.services[i].txt_records = malloc(sizeof(char*));
config->mdns.services[i].txt_records[0] = strdup(json_string_value(txt_records));
} else if (json_is_array(txt_records)) {
// If TxtRecords is an array, handle multiple records
config->mdns.services[i].num_txt_records = json_array_size(txt_records);
config->mdns.services[i].txt_records = malloc(config->mdns.services[i].num_txt_records * sizeof(char*));
for (size_t j = 0; j < config->mdns.services[i].num_txt_records; j++) {
config->mdns.services[i].txt_records[j] = strdup(json_string_value(json_array_get(txt_records, j)));
}
} else {
// If TxtRecords is not present or invalid, set to NULL
config->mdns.services[i].num_txt_records = 0;
config->mdns.services[i].txt_records = NULL;
}

config->mdns.services[i].txt_records = txt_records;
config->mdns.services[i].num_txt_records = num_txt_records;
free(txt_records_copy);
}
}
}

json_decref(root);

// Calculate max priority label width
calculate_max_priority_label_width();

return config;
}


const char* get_priority_label(int priority) {
for (int i = 0; i < NUM_PRIORITY_LEVELS; i++) {
if (DEFAULT_PRIORITY_LEVELS[i].value == priority) {
Expand Down
30 changes: 22 additions & 8 deletions elements/001-hydrogen/hydrogen/src/configuration.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#ifndef CONFIGURATION_H
#define CONFIGURATION_H

// System Libraries
#include <stddef.h>

// Third-Party Libraries
#include <jansson.h>

// Project Libraries
#include "mdns.h"
#include "mdns_server.h"

#define VERSION "0.1.0"
#define DEFAULT_SERVER_NAME "Philement/hydrogen"
Expand All @@ -22,26 +25,37 @@ typedef struct {
const char* label;
} PriorityLevel;

typedef struct {
int port;
char *web_root;
char *upload_path;
char *upload_dir;
size_t max_upload_size;
} WebConfig;

typedef struct {
int port;
char *key;
char *protocol;
size_t max_message_size; // bytes
} WebSocketConfig;

typedef struct {
char *device_id;
char *friendly_name;
char *model;
char *manufacturer;
char *version;
mdns_service_t *services;
int num_services;
size_t num_services;
} mDNSConfig;

typedef struct {
char *server_name;
char *executable_path;
char *log_file_path;
int web_port;
int websocket_port;
char *upload_path;
char *upload_dir;
size_t max_upload_size;
char *web_root;
WebConfig web;
WebSocketConfig websocket;
mDNSConfig mdns;
} AppConfig;

Expand Down
Loading

0 comments on commit 60db7f8

Please sign in to comment.