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