Skip to content

Commit 2189ee9

Browse files
committed
refactor: decompose main.c and payload_mgr.c into focused modules
Split two monolithic files (main.c 1538→233 lines, payload_mgr.c 2355→467 lines) into 8 single-responsibility modules: - http_server.c: MHD request router and all route handlers - repository.c: repo cache, download, install/commit, self-update - sources.c: multi-source repository management - config.c: PldmgrConfig struct, read/write, JSON config handler - log_server.c: ring buffer, SSE streaming, pldmgr_log() - json_helpers.c: JsonListBuilder, JSON parser, file I/O utilities - sha256.c: SHA256 hash implementation - process_mgr.c: process listing and kill Introduces PldmgrConfig struct to replace the 8-out-param read_next_config_values() pattern. Renames extracted functions to match their new modules (e.g. repository_install_download, sources_add, process_kill). No HTTP route changes — frontend is unaffected.
1 parent 631e7de commit 2189ee9

21 files changed

Lines changed: 3824 additions & 3716 deletions

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ INCLUDES := -Iinclude -I$(TARGET)/include
1212
LIBS := -L$(TARGET)/lib -lcurl -lmbedtls -lmbedx509 -lmbedcrypto -lmicrohttpd -lpthread -lSceNetCtl -lSceUserService -lSceSystemService -lSceAppInstUtil -lSceHttp2 -lSceSsl -lSceNet
1313

1414
# Source Files
15-
SRCS := src/main.c src/payload_mgr.c src/ps5_launcher.c src/notification.c src/utils.c src/autoload.c src/app_installer.c
15+
SRCS := src/main.c src/http_server.c src/config.c src/log_server.c \
16+
src/sha256.c src/json_helpers.c src/repository.c src/sources.c \
17+
src/payload_mgr.c src/process_mgr.c \
18+
src/ps5_launcher.c src/notification.c src/utils.c src/autoload.c \
19+
src/app_installer.c
1620
OBJS := $(SRCS:.c=.o)
1721
ELF := pldmgr.elf
1822

include/config.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#ifndef CONFIG_H
2+
#define CONFIG_H
3+
4+
#include <stddef.h>
5+
6+
/* Consolidated configuration state for pldmgr_config.txt */
7+
typedef struct PldmgrConfig {
8+
int autoload_enabled; /* Default: 0 (off) */
9+
long last_repository_update; /* Default: 0 */
10+
int auto_browser_open; /* Default: 1 (on) */
11+
int autoload_delay; /* Default: 5 (seconds) */
12+
int kill_disc_player; /* Default: 1 (on) */
13+
int scan_usb_payloads; /* Default: 0 (off) */
14+
int auto_install_app; /* Default: 1 (on) */
15+
int multi_sources_enabled; /* Default: 0 (off) */
16+
} PldmgrConfig;
17+
18+
/* Read all config values from PLDMGR_CONFIG_PATH.
19+
* Missing file / keys → defaults above. */
20+
void config_read(PldmgrConfig *cfg);
21+
22+
/* Write all config values to PLDMGR_CONFIG_PATH.
23+
* Returns 0 on success, -1 on failure. */
24+
int config_write(const PldmgrConfig *cfg);
25+
26+
/* Read a single boolean (0/1) key from the config file.
27+
* Returns default_val if the key or file is missing. */
28+
int config_read_bool(const char *key, int default_val);
29+
30+
/* Apply a JSON config update sent from the frontend /set_config route.
31+
* Handles AUTOLOAD_ENABLED, AUTO_BROWSER_OPEN, AUTOLOAD_DELAY,
32+
* KILL_DISC_PLAYER_ON_STARTUP, SCAN_USB_PAYLOADS, AUTO_INSTALL_APP,
33+
* MULTI_SOURCES_ENABLED, and AUTOLOAD_LIST.
34+
* Returns 0 on success, -1 on error. */
35+
int config_handle_set_json(const char *json_data);
36+
37+
/* Upsert a single key=value line in the config file.
38+
* Returns 0 on success, -1 on failure. */
39+
int config_upsert_value(const char *key, const char *value);
40+
41+
/* Read the LAST_REPOSITORY_UPDATE timestamp from the config file.
42+
* Always succeeds; sets *out_ts to 0 if not found. */
43+
int config_read_last_update(long *out_ts);
44+
45+
/* Write the LAST_REPOSITORY_UPDATE timestamp to the config file. */
46+
int config_write_last_update(long ts);
47+
48+
#endif

include/http_server.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef HTTP_SERVER_H
2+
#define HTTP_SERVER_H
3+
4+
#include <microhttpd.h>
5+
6+
/* MHD request handler callback — dispatches all routes. */
7+
enum MHD_Result http_on_request(void *cls, struct MHD_Connection *conn,
8+
const char *url, const char *method,
9+
const char *version, const char *upload_data,
10+
size_t *upload_data_size, void **con_cls);
11+
12+
#endif

include/json_helpers.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef JSON_HELPERS_H
2+
#define JSON_HELPERS_H
3+
4+
#include <stddef.h>
5+
6+
/* Incremental JSON array/object builder */
7+
typedef struct JsonListBuilder {
8+
char *buf;
9+
size_t size;
10+
size_t pos;
11+
int first;
12+
} JsonListBuilder;
13+
14+
/* Append formatted text to the builder. Returns 0 on success, -1 on overflow. */
15+
int json_append(JsonListBuilder *jb, const char *fmt, ...);
16+
17+
/* Extract a quoted string value for a given key from a JSON object bounded by
18+
* [obj_start, obj_end). Returns 0 on success, -1 if key not found. */
19+
int json_extract_string(const char *obj_start, const char *obj_end,
20+
const char *key, char *out, size_t out_size);
21+
22+
/* Read an entire file into a malloc'd buffer. Caller must free *out_buf.
23+
* Returns 0 on success, -1 on failure. */
24+
int read_file_text(const char *path, char **out_buf, size_t *out_size);
25+
26+
/* Write data to a file atomically. Returns 0 on success, -1 on failure. */
27+
int write_file_text(const char *path, const char *data, size_t size);
28+
29+
/* Create a directory if it doesn't exist. Returns 0 on success. */
30+
int mkdir_if_missing(const char *path);
31+
32+
/* Recursively create all directories in path. Returns 0 on success. */
33+
int ensure_dir_recursive(const char *path);
34+
35+
#endif

include/log_server.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef LOG_SERVER_H
2+
#define LOG_SERVER_H
3+
4+
#include <stddef.h>
5+
#include <stdint.h>
6+
#include <microhttpd.h>
7+
8+
#define MAX_LOG_LINES 100
9+
#define MAX_LOG_LINE_LEN 256
10+
11+
/* SSE log stream connection state */
12+
struct LogSSEConnection {
13+
int last_log_version;
14+
int sent_initial;
15+
};
16+
17+
/* MHD content reader callback for SSE log streaming */
18+
ssize_t log_stream_callback(void *cls, uint64_t pos, char *buf, size_t max);
19+
20+
/* MHD free callback for SSE connections */
21+
void log_stream_cleanup(void *cls);
22+
23+
/* Build the /log JSON response into buf. Returns bytes written. */
24+
size_t log_build_json(char *buf, size_t buf_size);
25+
26+
/* Track whether any browser client has connected */
27+
void log_server_set_active(void);
28+
/* pldmgr_server_is_active() and pldmgr_log() are declared in pldmgr.h */
29+
30+
#endif

include/payload_mgr.h

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,27 @@
33

44
#include <stddef.h>
55

6-
/*
7-
* Scans directories and populates json_buf with a JSON array of payloads.
8-
* Returns the length of the string written.
6+
/*
7+
* Scans directories and populates json_buf with a JSON array of payloads.
8+
* Returns the length of the string written.
99
*/
1010
size_t payload_mgr_list_json(char *json_buf, size_t buf_size);
1111
int payload_mgr_resolve_path(const char *filename, char *out_path, size_t out_size);
1212
int payload_mgr_delete_payload_file(const char *filename);
1313

14-
int payload_mgr_repository_ensure_fresh(int force_refresh);
15-
size_t payload_mgr_repository_list_json(char *json_buf, size_t buf_size, int force_refresh);
16-
int payload_mgr_repository_install_download(const char *filename, const char *install_source_detail, char *msg_buf, size_t msg_buf_size);
17-
int payload_mgr_repository_install_commit(const char *filename, const char *temp_path, const char *install_source, const char *install_source_detail, char *msg_buf, size_t msg_buf_size);
18-
int payload_mgr_import_to_storage(const char *filename, const char *temp_path, const char *install_source, const char *install_source_detail, char *msg_buf, size_t msg_buf_size);
14+
int payload_mgr_import_to_storage(const char *filename, const char *temp_path,
15+
const char *install_source, const char *install_source_detail,
16+
char *msg_buf, size_t msg_buf_size);
1917
int payload_mgr_check_existing(const char *filename, char *out_json, size_t out_size);
20-
int payload_mgr_write_metadata(const char *payload_path, const char *install_source, const char *install_source_detail);
21-
int payload_mgr_repository_push_json(const char *json, size_t len);
18+
int payload_mgr_write_metadata(const char *payload_path, const char *install_source,
19+
const char *install_source_detail);
2220

2321
/* USB Import */
2422
int payload_mgr_usb_check(const char *usb_path, char *out_json, size_t out_size);
25-
int payload_mgr_usb_move(const char *usb_path, int overwrite, char *out_json, size_t out_size);
23+
int payload_mgr_usb_move(const char *usb_path, int overwrite, char *out_json,
24+
size_t out_size);
2625

27-
int payload_mgr_check_self_update(char *out_path, size_t out_size);
28-
29-
/* Sources Management */
30-
int payload_mgr_sources_list_json(char *buf, size_t size);
31-
int payload_mgr_sources_save(const char *json, size_t len);
32-
int payload_mgr_sources_add(const char *url, char *msg_buf, size_t msg_size);
33-
int payload_mgr_sources_remove(int index, char *msg_buf, size_t msg_size);
34-
35-
/* Multi-Source Repository */
36-
size_t payload_mgr_multi_repository_list_json(char *buf, size_t size, int force_refresh);
37-
int payload_mgr_multi_repository_install(const char *filename, const char *source_id, const char *repo_url, char *msg, size_t msg_size);
38-
39-
/* Processes Management */
40-
size_t payload_mgr_process_list_json(char *buf, size_t max_size);
41-
int payload_mgr_process_kill(int pid);
26+
/* Internal utility — exposed for use by repository.c and sources.c */
27+
void payload_mgr_remove_old_files(const char *dir_path, const char *new_filename);
4228

4329
#endif

include/pldmgr.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@
4646
"https://itsplk.github.io/ps5-payloads-mirror/payloads.json"
4747
#define REPOSITORY_REFRESH_INTERVAL_SEC 86400
4848

49-
/* Logging */
49+
/* Logging (implementation in log_server.c) */
5050
void pldmgr_log(const char *fmt, ...);
5151
int pldmgr_server_is_active();
52-
int pldmgr_read_config_bool(const char *key, int default_val);
5352

5453
#include "autoload.h"
5554
#include "notification.h"

include/process_mgr.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef PROCESS_MGR_H
2+
#define PROCESS_MGR_H
3+
4+
#include <stddef.h>
5+
6+
/* Build a JSON list of all running processes.
7+
* Returns bytes written. */
8+
size_t process_list_json(char *buf, size_t max_size);
9+
10+
/* Kill a process by PID. Returns 0 on success, -1 on failure. */
11+
int process_kill(int pid);
12+
13+
#endif

include/repository.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#ifndef REPOSITORY_H
2+
#define REPOSITORY_H
3+
4+
#include <stddef.h>
5+
6+
/* Payload entry from a repository JSON */
7+
typedef struct RepoPayload {
8+
char name[128];
9+
char filename[256];
10+
char url[1024];
11+
char source[1024];
12+
char source_direct[1024];
13+
char description[1024];
14+
char last_update[64];
15+
char version[64];
16+
char checksum[65];
17+
char source_name[256];
18+
} RepoPayload;
19+
20+
/* Download a URL to a local file. Returns 0 on success, -1 on failure. */
21+
int download_to_file(const char *url, const char *out_path);
22+
23+
/* Parse a JSON string containing payload objects into a RepoPayload array.
24+
* Caller must free *out_items. Returns 0 on success, -1 on failure. */
25+
int parse_repository_payloads(const char *json, RepoPayload **out_items, size_t *out_count);
26+
27+
/* Write a .json sidecar file with payload metadata.
28+
* Returns 0 on success. */
29+
int write_payload_details_json(const RepoPayload *item, const char *details_path,
30+
const char *install_source, const char *install_source_detail);
31+
32+
/* Write a minimal sidecar file when full RepoPayload info isn't available. */
33+
int write_simple_payload_details_json(const char *filename, const char *details_path,
34+
const char *install_source, const char *install_source_detail);
35+
36+
/* ── Repository cache management ────────────────────────── */
37+
38+
/* Accept a full JSON blob pushed by the browser and replace the cache. */
39+
int repository_push_json(const char *json, size_t len);
40+
41+
/* Ensure the repository cache is up-to-date. If force_refresh, always re-download. */
42+
int repository_ensure_fresh(int force_refresh);
43+
44+
/* Build the /repository_payloads JSON response.
45+
* Returns bytes written. */
46+
size_t repository_list_json(char *json_buf, size_t buf_size, int force_refresh);
47+
48+
/* Download and install a payload from the default repository.
49+
* msg_buf receives a human-readable status message. */
50+
int repository_install_download(const char *filename, const char *install_source_detail,
51+
char *msg_buf, size_t msg_buf_size);
52+
53+
/* Verify a previously-uploaded file against the repository and commit it.
54+
* msg_buf receives a human-readable status message. */
55+
int repository_install_commit(const char *filename, const char *uploaded_temp_path,
56+
const char *install_source, const char *install_source_detail,
57+
char *msg_buf, size_t msg_buf_size);
58+
59+
/* ── Self-update ────────────────────────────────────────── */
60+
61+
/* Read the PLDMGR_VER: signature from an ELF binary.
62+
* Returns 0 on success. */
63+
int get_elf_pldmgr_version(const char *path, char *out_version, size_t out_size);
64+
65+
/* Scan the payload storage for a newer pldmgr.elf.
66+
* If found, writes the path to out_path and returns 0. */
67+
int repository_check_self_update(char *out_path, size_t out_size);
68+
69+
#endif

include/sha256.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef SHA256_H
2+
#define SHA256_H
3+
4+
#include <stddef.h>
5+
6+
typedef struct SHA256_CTX {
7+
unsigned char data[64];
8+
unsigned int datalen;
9+
unsigned long long bitlen;
10+
unsigned int state[8];
11+
} SHA256_CTX;
12+
13+
void sha256_init(SHA256_CTX *ctx);
14+
void sha256_update(SHA256_CTX *ctx, const unsigned char data[], size_t len);
15+
void sha256_final(SHA256_CTX *ctx, unsigned char hash[]);
16+
17+
/* Compute SHA256 of a file, writing the 64-char hex string to out_hex.
18+
* Returns 0 on success, -1 on failure. */
19+
int compute_sha256_file(const char *path, char out_hex[65]);
20+
21+
#endif

0 commit comments

Comments
 (0)