Commit 415bd433 authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾

Do not read quickjs logs

`setLog` must be used by the program using the library to define which logs
should be published.
parent 37a13ee9
......@@ -15,7 +15,7 @@
#define DATA_SET_WRITER_ID 1
#define WRITER_GROUP_ID 1
#define MAX_MESSAGE_SIZE 1024
#define MAX_STR_SIZE 1024
typedef struct {
UA_UInt16 id;
......@@ -29,8 +29,10 @@ typedef struct {
UA_Float yaw;
UA_Float speed;
UA_Float climbRate;
char message[MAX_MESSAGE_SIZE];
char message[MAX_STR_SIZE];
UA_UInt32 messageId;
char log[MAX_STR_SIZE];
UA_UInt32 logId;
} JSDroneData;
typedef struct {
......@@ -79,6 +81,8 @@ int runPubsub(const UA_Logger *logger, UA_String *transportProfile,
UA_String get_message(void);
UA_String get_log(void);
UA_UInt16 get_drone_id(UA_UInt32 nb);
void init_drone_node_id(UA_UInt32 id, UA_UInt32 nb, UA_UInt32 magic);
......@@ -89,7 +93,4 @@ VariableData pubsub_get_value(UA_String identifier);
DLL_PUBLIC JSModuleDef *js_init_module(JSContext *ctx, const char *module_name);
void Subscriber_log(void *context, UA_LogLevel level, UA_LogCategory category,
const char *msg, va_list args);
#endif /* __PUBSUB_H__ */
......@@ -7,7 +7,7 @@
#include "autopilot_API.h"
#endif
#define DRONE_VARIABLE_NB 3
#define DRONE_VARIABLE_NB 4
#define SUBSCRIBER_VARIABLE_NB 1
struct strNode {
char *str;
......@@ -24,7 +24,7 @@ UA_UInt32 positionArrayDims[] = {POSITION_ARRAY_SIZE};
UA_Double speedArray[SPEED_ARRAY_SIZE] = { 0 };
UA_UInt32 speedArrayDims[] = {SPEED_ARRAY_SIZE};
UA_String message = {
UA_String initial_ua_string = {
.length = 0,
.data = NULL,
};
......@@ -61,7 +61,7 @@ VariableData droneVariableArray[] = {
{
.name = "message",
.description = "Message to send to the other drones",
.value = &message,
.value = &initial_ua_string,
.type = UA_TYPES_STRING,
.builtInType = UA_NS0ID_STRING,
.valueRank = UA_VALUERANK_SCALAR,
......@@ -69,6 +69,17 @@ VariableData droneVariableArray[] = {
.arrayDimensions = NULL,
.getter.getString = get_message,
},
{
.name = "log",
.description = "Message to send to the other drones",
.value = &initial_ua_string,
.type = UA_TYPES_STRING,
.builtInType = UA_NS0ID_STRING,
.valueRank = UA_VALUERANK_SCALAR,
.arrayDimensionsSize = 0,
.arrayDimensions = NULL,
.getter.getString = get_log,
},
};
VariableStruct droneVariables = {
......@@ -80,7 +91,7 @@ VariableData subscriberVariableArray[] = {
{
.name = "message",
.description = "Message to send to the other drones",
.value = &message,
.value = &initial_ua_string,
.type = UA_TYPES_STRING,
.builtInType = UA_NS0ID_STRING,
.valueRank = UA_VALUERANK_SCALAR,
......@@ -104,19 +115,21 @@ static UA_Boolean pubsubExited = true;
static UA_UInt32 nbDrone;
static UA_UInt32 nbSubscriber;
static JSValueConst *droneObjectIdList;
static MessageQueue messageQueue = {
static StrQueue messageQueue = {
.head = NULL,
.tail = NULL,
};
UA_String currentMessage;
static StrQueue logQueue = {
.head = NULL,
.tail = NULL,
};
UA_String currentLog;
pthread_mutex_t mutex;
pthread_cond_t threadCond;
bool isADrone;
bool debug;
FILE *quickjsLogFptr;
// Position class functions
......@@ -279,11 +292,11 @@ static JSValue js_drone_get(JSContext *ctx, JSValueConst thisVal, int magic)
}
}
static void addMessageToQueue(const char* msg, MessageQueue* pQueue)
static void addStrToQueue(const char* str, StrQueue* pQueue)
{
struct messageNode *newNode;
newNode = (struct messageNode*)malloc(sizeof(struct messageNode));
newNode->message = strdup(msg);
struct strNode *newNode;
newNode = (struct strNode*)malloc(sizeof(struct strNode));
newNode->str = strdup(str);
newNode->next = NULL;
if (pQueue->tail == NULL) {
......@@ -294,23 +307,34 @@ static void addMessageToQueue(const char* msg, MessageQueue* pQueue)
}
}
static JSValue js_drone_set_message(JSContext *ctx, JSValueConst thisVal,
int argc, JSValueConst *argv)
static JSValue add_jsstr_to_queue(JSContext *ctx, JSValueConst jsStr, StrQueue *queue)
{
const char *message;
message = JS_ToCString(ctx, argv[0]);
if (strlen(message) > MAX_MESSAGE_SIZE) {
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Message too long");
const char *str;
str = JS_ToCString(ctx, jsStr);
if (strlen(str) > MAX_STR_SIZE) {
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "String too long");
return JS_EXCEPTION;
}
addMessageToQueue(message, &messageQueue);
JS_FreeCString(ctx, message);
addStrToQueue(str, queue);
JS_FreeCString(ctx, str);
return JS_UNDEFINED;
}
static void delete_message_node(struct messageNode *node) {
free(node->message);
static JSValue js_drone_set_message(JSContext *ctx, JSValueConst thisVal,
int argc, JSValueConst *argv)
{
return add_jsstr_to_queue(ctx, argv[0], &messageQueue);
}
static JSValue js_drone_set_log(JSContext *ctx, JSValueConst thisVal,
int argc, JSValueConst *argv)
{
return add_jsstr_to_queue(ctx, argv[0], &logQueue);
}
static void delete_str_node(struct strNode *node) {
free(node->str);
free(node);
}
......@@ -318,33 +342,33 @@ static UA_Boolean UA_String_isEmpty(const UA_String *s) {
return (s->length == 0 || s->data == NULL);
}
static void clear_message(UA_String message) {
if (!UA_String_isEmpty(&message))
UA_String_clear(&message);
static void clear_str(UA_String *pstr) {
if (!UA_String_isEmpty(pstr))
UA_String_clear(pstr);
}
UA_String get_message(void)
static UA_String get_StrQueue_content(StrQueue *queue, UA_String currentStr)
{
struct messageNode *current;
struct strNode *current;
current = messageQueue.head;
current = queue->head;
if (current != NULL) {
clear_message(currentMessage);
currentMessage = UA_STRING_ALLOC(current->message);
messageQueue.head = current->next == NULL ? (messageQueue.tail = NULL) : current->next;
delete_message_node(current);
clear_str(&currentStr);
currentStr = UA_STRING_ALLOC(current->str);
queue->head = current->next == NULL ? (queue->tail = NULL) : current->next;
delete_str_node(current);
}
return currentMessage;
return currentStr;
}
UA_String get_message(void)
{
return get_StrQueue_content(&messageQueue, currentMessage);
}
UA_String get_log(void)
{
char log_str[MAX_MESSAGE_SIZE];
if (debug && isADrone && fread(log_str, sizeof(char), MAX_MESSAGE_SIZE, quickjsLogFptr) > 0) {
clear_message(currentLog);
currentLog = UA_STRING_ALLOC(log_str);
}
return currentLog;
return get_StrQueue_content(&logQueue, currentLog);
}
static JSClassDef jsDroneClass = {
......@@ -606,27 +630,28 @@ static JSValue js_init_pubsub(JSContext *ctx, JSValueConst thisVal,
return JS_NewInt32(ctx, 0);
}
static void cleanQueue(StrQueue queue, UA_String current_str)
{
struct strNode *current;
while (queue.head != NULL) {
current = queue.head;
queue.head = current->next;
delete_str_node(current);
}
clear_str(&current_str);
}
static JSValue js_stop_pubsub(JSContext *ctx, JSValueConst thisVal,
int argc, JSValueConst *argv)
{
struct messageNode *current;
pubsubShouldRun = false;
while(!pubsubExited)
sleep(1);
free(droneObjectIdList);
while (messageQueue.head != NULL) {
current = messageQueue.head;
messageQueue.head = current->next;
delete_message_node(current);
}
clear_message(currentMessage);
clear_message(currentLog);
if (quickjsLogFptr != NULL) {
fclose(quickjsLogFptr);
}
cleanQueue(messageQueue, currentMessage);
cleanQueue(logQueue, currentLog);
return JS_NewInt32(ctx, 0);
}
......@@ -638,7 +663,6 @@ static JSValue js_start(JSContext *ctx, JSValueConst thisVal,
{
const char *ip;
const char *mavsdk_log_file;
const char *quickjs_log_file;
int port;
int timeout;
int res;
......@@ -647,18 +671,12 @@ static JSValue js_start(JSContext *ctx, JSValueConst thisVal,
if (JS_ToInt32(ctx, &port, argv[1]))
return JS_EXCEPTION;
mavsdk_log_file = JS_ToCString(ctx, argv[2]);
quickjs_log_file = JS_ToCString(ctx, argv[3]);
if (JS_ToInt32(ctx, &timeout, argv[4]))
if (JS_ToInt32(ctx, &timeout, argv[3]))
return JS_EXCEPTION;
debug = JS_ToBool(ctx, argv[5]);
if (debug) {
quickjsLogFptr = fopen(quickjs_log_file, "r");
}
res = start(ip, port, mavsdk_log_file, timeout);
JS_FreeCString(ctx, ip);
JS_FreeCString(ctx, mavsdk_log_file);
JS_FreeCString(ctx, quickjs_log_file);
return JS_NewInt32(ctx, res);
}
......@@ -837,11 +855,12 @@ static JSValue js_updateLogAndProjection(JSContext *ctx, JSValueConst this_val,
static const JSCFunctionListEntry js_funcs[] = {
JS_CFUNC_DEF("setMessage", 1, js_drone_set_message ),
JS_CFUNC_DEF("setLog", 1, js_drone_set_log ),
JS_CFUNC_DEF("runPubsub", 6, js_run_pubsub ),
JS_CFUNC_DEF("stopPubsub", 0, js_stop_pubsub ),
JS_CFUNC_DEF("initPubsub", 2, js_init_pubsub ),
#ifdef WITH_AUTOPILOT
JS_CFUNC_DEF("start", 6, js_start ),
JS_CFUNC_DEF("start", 4, js_start ),
JS_CFUNC_DEF("stop", 0, js_stop ),
JS_CFUNC_DEF("reboot", 0, js_reboot ),
JS_CFUNC_DEF("arm", 0, js_arm ),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment