94 lines
2.6 KiB
C
94 lines
2.6 KiB
C
#include <string.h>
|
|
#include "pico/platform/panic.h"
|
|
|
|
#define NB_MAX_CDC_CONNEXION 10
|
|
#define INVALID_ID ((char)-1)
|
|
|
|
struct log_buffer_t{
|
|
char idx;
|
|
char copy_active;
|
|
char tampon[1020];
|
|
char log_dispo[1020];
|
|
unsigned int index_tampon;
|
|
}log_buffer[10];
|
|
|
|
static struct log_buffer_t * get_buffer(const char idx);
|
|
|
|
static struct log_buffer_t * get_buffer(const char idx){
|
|
for (int i=0; i<NB_MAX_CDC_CONNEXION; i++){
|
|
if(log_buffer[i].idx == idx){
|
|
return &(log_buffer[i]);
|
|
}
|
|
}
|
|
/// TODO: panic ?
|
|
panic("Buffer IDX not found: %d\n", idx);
|
|
return NULL;
|
|
}
|
|
|
|
/// @brief Initialisation des structure de reception des données USB-CDC
|
|
void log_init(){
|
|
for (int i=0; i<NB_MAX_CDC_CONNEXION; i++){
|
|
log_buffer[i].log_dispo[0] = '\0';
|
|
log_buffer[i].index_tampon = 0;
|
|
log_buffer[i].copy_active = 0;
|
|
log_buffer[i].idx = INVALID_ID;
|
|
}
|
|
}
|
|
|
|
/// @brief Affectation des tampons de reception à une liaison USB-CDC
|
|
/// @param idx :
|
|
void log_create(const char idx){
|
|
for (int i=0; i<NB_MAX_CDC_CONNEXION; i++){
|
|
if(log_buffer[i].idx == INVALID_ID){
|
|
log_buffer[i].idx = idx;
|
|
return;
|
|
}
|
|
}
|
|
panic("Creation impossible: idx: %d\n", idx);
|
|
}
|
|
|
|
void log_destroy(const char idx){
|
|
struct log_buffer_t * p_log_bufffer;
|
|
p_log_bufffer = get_buffer(idx);
|
|
p_log_bufffer->idx = INVALID_ID;
|
|
}
|
|
|
|
void log_analyse_input_string(const char idx, const char * input_data, unsigned int str_len){
|
|
// On charge les données dans le tampon
|
|
// Si on a un message complet, on charge dans log dispo (s'il y a la place)
|
|
struct log_buffer_t * p_log_bufffer;
|
|
p_log_bufffer = get_buffer(idx);
|
|
for(int i=0; i< str_len; i++){
|
|
if(input_data[i] == '>'){
|
|
p_log_bufffer->copy_active = 1;
|
|
}
|
|
if(p_log_bufffer->copy_active == 1){
|
|
p_log_bufffer->tampon[p_log_bufffer->index_tampon] = input_data[i];
|
|
p_log_bufffer->index_tampon++;
|
|
|
|
if(input_data[i] == '\n'){
|
|
p_log_bufffer->copy_active = 0;
|
|
p_log_bufffer->tampon[p_log_bufffer->index_tampon] = '\0';
|
|
strcat(p_log_bufffer->log_dispo, p_log_bufffer->tampon);
|
|
p_log_bufffer->index_tampon=0;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
// On renvoi la chaine et on remet log_dispo à 0;
|
|
void log_get(const char idx, char * chaine){
|
|
struct log_buffer_t * p_log_bufffer;
|
|
p_log_bufffer = get_buffer(idx);
|
|
if(p_log_bufffer == NULL){
|
|
chaine[0] = '\0';
|
|
return;
|
|
}
|
|
|
|
strcpy(chaine, p_log_bufffer->log_dispo);
|
|
p_log_bufffer->log_dispo[0] = '\0';
|
|
}
|
|
|
|
|