77 lines
1.5 KiB
C
77 lines
1.5 KiB
C
//
|
|
// Created by rick on 28-01-21.
|
|
//
|
|
|
|
#include <util/stream.h>
|
|
#include <libc/string.h>
|
|
#include <stdbool.h>
|
|
#include <attributes.h>
|
|
#include <tasks/task.h>
|
|
#include <libc/libc.h>
|
|
#include "kprint.h"
|
|
|
|
#define MAX_HANDLERS 8
|
|
#define STREAM_SIZE 512
|
|
#define PRINT_BUFFER_SIZE 64
|
|
|
|
stream_t *kprint_stream;
|
|
|
|
kprint_handler handlers[MAX_HANDLERS] = {
|
|
NULL,
|
|
NULL,
|
|
NULL,
|
|
NULL,
|
|
NULL,
|
|
NULL,
|
|
NULL,
|
|
NULL,
|
|
};
|
|
|
|
void kprint_register(kprint_handler handler) {
|
|
for (int i = 0; i < MAX_HANDLERS; ++i) {
|
|
if (handlers[i] == NULL) {
|
|
handlers[i] = handler;
|
|
break;
|
|
}
|
|
}
|
|
// todo handle
|
|
}
|
|
|
|
void kprint(const char *msg) {
|
|
stream_write(kprint_stream, (const uint8_t *) msg, strlen(msg));
|
|
}
|
|
|
|
void kprint_internal(const char *msg) {
|
|
for (int i = 0; i < MAX_HANDLERS; ++i) {
|
|
if (handlers[i] == NULL) {
|
|
continue;
|
|
}
|
|
handlers[i](msg);
|
|
}
|
|
}
|
|
|
|
void kprint_sync(const char* msg) {
|
|
kprint_internal(msg);
|
|
}
|
|
|
|
void kprint_init() {
|
|
kprint_stream = stream_create(STREAM_SIZE);
|
|
}
|
|
|
|
void noreturn kprint_task(void *_) {
|
|
uint32_t last_read = 0;
|
|
uint8_t data[PRINT_BUFFER_SIZE + 1] = {0};
|
|
while (true) {
|
|
stream_wait(kprint_stream);
|
|
while (true) {
|
|
last_read = stream_read(kprint_stream, data, PRINT_BUFFER_SIZE);
|
|
if (last_read == 0) break;
|
|
data[last_read] = 0;
|
|
kprint_internal((const char *) data);
|
|
};
|
|
}
|
|
}
|
|
|
|
void kprint_start_task() {
|
|
task_spawn(kprint_task, NULL);
|
|
} |