I am having trouble on a function that prints plain text via serial port. All the other similar functions are working fine but this one crashes the entire system when called. The function is named plainOutput and is inside the log.c. It prints until the first or second character and crashes everything. I have tried a lot of things but I cannot see what is wrong. The function printHelp in serial_forwarding.c calls the function plainOutput in log.c to send the strings. serial_forwarding.c is a colection of routines to forward messages coming from other serial ports and redirect to the log serial port. log.c has log routines and the corresponding thead.
log.c
Code: Select all
#include "log.h"
#include <string.h>
#include "strutils.h"
static output_queue_t *out = NULL;
static semaphore_t sem;
static logHook hook = NULL;
void logInit(output_queue_t *q)
{
chSysLock();
if (out)
{
chSysUnlock();
return;
}
out = q;
chSemObjectInit(&sem, 1);
chSysUnlock();
}
void plainOutput(const char *msg){
char buffer[LOG_MAX_MSGLEN+1];
/*Check if logging system has been initialized*/
if (!out)
return;
buffer[0] = '\0';
strncat(buffer, msg, LOG_MAX_MSGLEN - strlen(buffer) - 2);
chSemWait(&sem);
chOQWriteTimeout(out, (uint8_t *)buffer, strlen(buffer), TIME_INFINITE);
if (hook) hook(buffer);
chSemSignal(&sem);
}
void plainNumOutput(const char *msg, uint8_t length){
char buffer[LOG_MAX_MSGLEN+1];
/*Check if logging system has been initialized*/
if (!out)
return;
buffer[0] = '\0';
strncat(buffer, msg, LOG_MAX_MSGLEN - length - 2);
chSemWait(&sem);
chOQWriteTimeout(out, (uint8_t *)buffer, length, TIME_INFINITE);
if (hook) hook(buffer);
chSemSignal(&sem);
}
void logOutput(LogLevel lvl, const char *msg)
{
char buffer[LOG_MAX_MSGLEN+1];
char tmp[3]; /* Capable to hold a two digit number and a trailing zero*/
systime_t ts = chVTGetSystemTime();
/*Check if logging system has been initialized*/
if (!out)
return;
/*Prepare buffer*/
buffer[0] = '\0';
strcat(buffer, "<S>");
strcat(buffer, "[");
itoa(tmp, (ts / CH_CFG_ST_FREQUENCY / 3600) % 100);
strcat(buffer, tmp);
strcat(buffer, "-");
itoa(tmp, (ts / CH_CFG_ST_FREQUENCY / 60) % 60);
strcat(buffer, tmp);
strcat(buffer, ":");
itoa(tmp, (ts / CH_CFG_ST_FREQUENCY) % 60);
strcat(buffer, tmp);
strcat(buffer, "]");
switch (lvl)
{
case LOG_ERROR:
strcat(buffer, "[ERROR] ");
break;
case LOG_CRITICAL:
strcat(buffer, "[CRITICAL] ");
break;
case LOG_WARNING:
strcat(buffer, "[WARNING] ");
break;
default:
strcat(buffer, "[INFO] ");
break;
}
strncat(buffer, msg, LOG_MAX_MSGLEN - strlen(buffer) - 2); /*Note: we need space for \n\0*/
strcat(buffer, "\n");
/*Print buffer to output but protect it from inter-thread interference*/
chSemWait(&sem);
chOQWriteTimeout(out, (uint8_t *)buffer, strlen(buffer), TIME_INFINITE);
if (hook) hook(buffer);
chSemSignal(&sem);
}
void logSetHook(logHook h)
{
chSysLock();
hook = h;
chSysUnlock();
}
serial_forwarding.c
Code: Select all
#include "log.h"
#include <string.h>
#include "strutils.h"
static output_queue_t *out = NULL;
static semaphore_t sem;
static logHook hook = NULL;
void logInit(output_queue_t *q)
{
chSysLock();
if (out)
{
chSysUnlock();
return;
}
out = q;
chSemObjectInit(&sem, 1);
chSysUnlock();
}
void plainOutput(const char *msg){
char buffer[LOG_MAX_MSGLEN+1];
/*Check if logging system has been initialized*/
if (!out)
return;
buffer[0] = '\0';
strncat(buffer, msg, LOG_MAX_MSGLEN - strlen(buffer) - 2);
strcat(buffer, "\n"); // Teste BL
chSemWait(&sem);
chOQWriteTimeout(out, (uint8_t *)buffer, strlen(buffer), TIME_INFINITE);
if (hook) hook(buffer);
chSemSignal(&sem);
}
void plainNumOutput(const char *msg, uint8_t length){
char buffer[LOG_MAX_MSGLEN+1];
/*Check if logging system has been initialized*/
if (!out)
return;
buffer[0] = '\0';
strncat(buffer, msg, LOG_MAX_MSGLEN - length - 2);
strcat(buffer, "\n"); // Teste BL
chSemWait(&sem);
chOQWriteTimeout(out, (uint8_t *)buffer, length, TIME_INFINITE);
if (hook) hook(buffer);
chSemSignal(&sem);
}
void logOutput(LogLevel lvl, const char *msg)
{
char buffer[LOG_MAX_MSGLEN+1];
char tmp[3]; /* Capable to hold a two digit number and a trailing zero*/
systime_t ts = chVTGetSystemTime();
/*Check if logging system has been initialized*/
if (!out)
return;
/*Prepare buffer*/
buffer[0] = '\0';
strcat(buffer, "<S>");
strcat(buffer, "[");
itoa(tmp, (ts / CH_CFG_ST_FREQUENCY / 3600) % 100);
strcat(buffer, tmp);
strcat(buffer, "-");
itoa(tmp, (ts / CH_CFG_ST_FREQUENCY / 60) % 60);
strcat(buffer, tmp);
strcat(buffer, ":");
itoa(tmp, (ts / CH_CFG_ST_FREQUENCY) % 60);
strcat(buffer, tmp);
strcat(buffer, "]");
switch (lvl)
{
case LOG_ERROR:
strcat(buffer, "[ERROR] ");
break;
case LOG_CRITICAL:
strcat(buffer, "[CRITICAL] ");
break;
case LOG_WARNING:
strcat(buffer, "[WARNING] ");
break;
default:
strcat(buffer, "[INFO] ");
break;
}
strncat(buffer, msg, LOG_MAX_MSGLEN - strlen(buffer) - 2); /*Note: we need space for \n\0*/
strcat(buffer, "\n");
/*Print buffer to output but protect it from inter-thread interference*/
chSemWait(&sem);
chOQWriteTimeout(out, (uint8_t *)buffer, strlen(buffer), TIME_INFINITE);
if (hook) hook(buffer);
chSemSignal(&sem);
}
void logSetHook(logHook h)
{
chSysLock();
hook = h;
chSysUnlock();
}
The image below shows the cutecom terminal when the system crashes. Notice that before I send the ? (that uses plainOutput function) I sent the string <S>=lsr,A1 that activates a laser and prints the message "<S>[0-1:24] [INFO] Laser A on", using the function logOutput without problems. But when I sent ? character, that calls the function plainOutput (the problematic one) the system crashed at the second character printed. I have a 1Hz LED blinking routine in the main thread and it stops as well. Notice that I am using a semaphore before send via serial in both functions, but the crash problem only happens on the plainOutput function.
Please help me if you see something I am doing wrong. Thank you.
Terminal crash image example
[img]
https://drive.google.com/open?id=0B_ZfW ... ndRRjhaVEk
[/img]