Commit 7f68e8d6 authored by Arnaud Blanchard's avatar Arnaud Blanchard

Improvements for blc_channels on network

parent 88a1bbfb
...@@ -59,81 +59,85 @@ blc_channel::blc_channel(char const *new_name, int mode, uint32_t type, uint32_t ...@@ -59,81 +59,85 @@ blc_channel::blc_channel(char const *new_name, int mode, uint32_t type, uint32_t
va_end(arguments); va_end(arguments);
} }
//Should use code in common with fprint_info void blc_channel::check_name_type_format_parameter(){
int blc_channel::sprint_info(char *buffer, size_t buffer_size){ if (strlen(name)==0) EXIT_ON_ERROR("name is empty");
int width;
uint32_t str_type;
uint32_t str_format;
if (type==0) EXIT_ON_ERROR("The type should not be null for '%s'. Use 'NDEF' by default.", name); if (type==0) EXIT_ON_ERROR("The type should not be null for '%s'. Use 'NDEF' by default.", name);
if (format==0) EXIT_ON_ERROR("The format should not be null for '%s'. Use 'NDEF' by default.", name); if (format==0) EXIT_ON_ERROR("The format should not be null for '%s'. Use 'NDEF' by default.", name);
if (parameter[0]==0) EXIT_ON_ERROR("The parameter should not be null for '%s'. Use \"NDEF\" by default.", name); if (parameter[0]==0) EXIT_ON_ERROR("The parameter should not be null for '%s'. Use 'NDEF' by default.", name);
}
//Should use code in common with fprint_info
int blc_channel::sprint_info(char *buffer, size_t buffer_size, bool print_id){
int width=0;
uint32_t str_type, str_format;
SYSTEM_ERROR_CHECK(width=snprintf(buffer, buffer_size, "%.4s %.4s %-31s ", UINT32_TO_STRING(str_type, type), UINT32_TO_STRING(str_format, format), parameter), -1, NULL); check_name_type_format_parameter();
SYSTEM_ERROR_CHECK(width+=sprint_dims(buffer+width, buffer_size-width), -1, NULL);
if (strlen(name)==0) EXIT_ON_ERROR("name is empty"); if (print_id) SYSTEM_ERROR_CHECK(width=snprintf(buffer, buffer_size, "%6d ", id), -1, nullptr);
SYSTEM_ERROR_CHECK(width+=snprintf(buffer+width, buffer_size-width," %32s\n", name), -1, NULL); SYSTEM_ERROR_CHECK(width+=snprintf(buffer+width, buffer_size, "%.4s %.4s %-31s ", UINT32_TO_STRING(str_type, type), UINT32_TO_STRING(str_format, format), parameter), -1, NULL);
width+=sprint_dims(buffer+width, buffer_size-width);
SYSTEM_ERROR_CHECK(width+=snprintf(buffer+width, buffer_size-width," %s\n", name), -1, NULL);
if (width >=buffer_size) EXIT_ON_ERROR("The buffer '%d' is too small for writting '%d' chars: '%s'", buffer_size, width, buffer); if (width >=buffer_size) EXIT_ON_ERROR("The buffer '%d' is too small for writting '%d' chars: '%s'", buffer_size, width, buffer);
return width; return width;
} }
int blc_channel::fprint_info(FILE *file, int print_id){ int blc_channel::fprint_info(FILE *file, bool print_id){
int width=0; int width=0;
uint32_t net_type = htonl(type); uint32_t str_type, str_format;
uint32_t net_format = htonl(format);
if (type==0) EXIT_ON_ERROR("The type should not be null for '%s'. Use NDEF by default.", name);
if (format==0) EXIT_ON_ERROR("The format should not be null for '%s'. Use NDEF by default.", name);
if (parameter[0]==0) EXIT_ON_ERROR("The parameter should not be null for '%s'. Use NDEF by default.", name);
if (print_id) {
SYSTEM_ERROR_CHECK(width+=fprintf(file, "%.6d ", id),-1,"print id");
}
SYSTEM_ERROR_CHECK(width+=fprintf(file, "%.4s %.4s %-31s ", (char*)&net_type, (char*)&net_format, parameter), 1, NULL); check_name_type_format_parameter();
if (print_id) SYSTEM_ERROR_CHECK(width+=fprintf(file, "%6d ", id),-1,"print id");
SYSTEM_ERROR_CHECK(width+=fprintf(file, "%.4s %.4s %-31s ", UINT32_TO_STRING(str_type, type), UINT32_TO_STRING(str_format, format), parameter), 1, NULL);
width+=fprint_dims(file); width+=fprint_dims(file);
if (strlen(name)==0) EXIT_ON_ERROR("name is empty"); SYSTEM_ERROR_CHECK(width+=fprintf(file, " %s\n", name), -1, NULL);
SYSTEM_ERROR_CHECK(width+=fprintf(file, " %32s\n", name), -1, NULL);
return width; return width;
} }
//Should use code in common with fscan_info //Should use code in common with fscan_info
int blc_channel::sscan_info(char const *string){ int blc_channel::sscan_info(char const *string, bool scan_id){
int ret, pos; int ret, pos=0, width=0;
dims_nb=0; dims_nb=0;
data=NULL; data=NULL;
size=0; size=0;
ret = sscanf(string, "%4c %4c %31s%n", (char*)&type, (char*)&format, parameter, &pos); if (scan_id) {
ret = sscanf(string, "%d %n", &id, &pos);
if (ret == EOF) return -1;
}
width+=pos;
ret = sscanf(string+width, "%4c %4c %31c %n", (char*)&type, (char*)&format, parameter, &pos);
parameter[BLC_PARAMETER_MAX]=0;//Last null char
if (ret == EOF) return -1; if (ret == EOF) return -1;
else if (ret!=3) EXIT_ON_ERROR("Only %d parameters have been read instead of 3 in '%s'.", ret, string); else if (ret!=3) EXIT_ON_ERROR("Only %d parameters have been read instead of 3 in '%s'.", ret, string);
string+=pos;
NTOHL(type); NTOHL(type);
NTOHL(format); NTOHL(format);
string+=sscan_dims(string); width+=pos;
width+=sscan_dims(string+width);
if (sscanf(string, " %"STRINGIFY_CONTENT(NAME_MAX)"[^\n]\n", name) != 1) EXIT_ON_ERROR("Impossible to read channel's name of channel id '%d'.", id); if (sscanf(string+width, " %" STRINGIFY_CONTENT(NAME_MAX)"[^\n]\n%n", name, &pos) != 1) EXIT_ON_ERROR("Impossible to read channel's name of channel id '%d'.", id);
width+=pos;
if (id > blc_channel_id_max) blc_channel_id_max=id; if (id > blc_channel_id_max) blc_channel_id_max=id;
return id; return width;
} }
//Be this remove replace '.' by '/' in the begining of the name //Be this remove replace '.' by '/' in the begining of the name
int blc_channel::fscan_info(FILE *file, int scan_id){ int blc_channel::fscan_info(FILE *file, int scan_id){
int ret; int ret, width=0, pos=0;
dims_nb=0; dims_nb=0;
data=NULL; data=NULL;
size=0; size=0;
if (scan_id){ if (scan_id){
SYSTEM_ERROR_CHECK(ret = fscanf(file, "%d ", &id),-1, "Reading channel id"); SYSTEM_ERROR_CHECK(ret = fscanf(file, "%d %n", &id, &pos), -1, "Reading channel id");
if (ret!=1) EXIT_ON_ERROR("impossible to read id"); if (ret!=1) EXIT_ON_ERROR("impossible to read id");
} }
ret = fscanf(file, "%4c %4c %31s", (char*)&type, (char*)&format, parameter); ret = fscanf(file, "%4c %4c %31c ", (char*)&type, (char*)&format, parameter);
parameter[BLC_PARAMETER_MAX]=0;//Last null char
if (ret == EOF) return -1; if (ret == EOF) return -1;
else if (ret!=3) EXIT_ON_ERROR("%d parameters have been read instead of 3 in " BLC_CHANNELS_LIST_PATH" id '%d'.", ret, id); else if (ret!=3) EXIT_ON_ERROR("%d parameters have been read instead of 3 in " BLC_CHANNELS_LIST_PATH" id '%d'.", ret, id);
...@@ -142,10 +146,10 @@ int blc_channel::fscan_info(FILE *file, int scan_id){ ...@@ -142,10 +146,10 @@ int blc_channel::fscan_info(FILE *file, int scan_id){
fscan_dims(file); fscan_dims(file);
if (fscanf(file, " %"STRINGIFY_CONTENT(NAME_MAX)"[^\n]\n", name) != 1) EXIT_ON_ERROR("Impossible to read channel's name of channel id '%d'.", id); if (fscanf(file, " %" STRINGIFY_CONTENT(NAME_MAX)"[^\n]\n", name) != 1) EXIT_ON_ERROR("Impossible to read channel's name of channel id '%d'.", id);
if (id > blc_channel_id_max) blc_channel_id_max=id; if (id > blc_channel_id_max) blc_channel_id_max=id;
return id; return width;
} }
void blc_channel::map_memory(int access_mode){ void blc_channel::map_memory(int access_mode){
...@@ -177,15 +181,13 @@ void blc_channel::open_semaphores(int create){ ...@@ -177,15 +181,13 @@ void blc_channel::open_semaphores(int create){
else SYSTEM_ERROR_CHECK(sem_ack_data = sem_open(tmp_name, NO_FLAG), SEM_FAILED, "Opening named semaphore '%s' for blc_channel '%s'", tmp_name, name); else SYSTEM_ERROR_CHECK(sem_ack_data = sem_open(tmp_name, NO_FLAG), SEM_FAILED, "Opening named semaphore '%s' for blc_channel '%s'", tmp_name, name);
} }
///Create a channel once the blc_array is already defined
void blc_channel::create(char const *new_name, int access_mode){ void blc_channel::create(int access_mode){
blc_channel info; blc_channel info;
FILE *file; FILE *file;
int sync_new_data=0, sync_ack_data=0; int sync_new_data=0, sync_ack_data=0;
strcpy(parameter, "NDEF"); strcpy(parameter, "NDEF");
STRCPY(name, new_name);
switch (name[0]){ switch (name[0]){
case '/': case '/':
break; break;
...@@ -260,6 +262,12 @@ void blc_channel::create(char const *new_name, int access_mode){ ...@@ -260,6 +262,12 @@ void blc_channel::create(char const *new_name, int access_mode){
} }
} }
///Create a channel once the blc_array is already defined
void blc_channel::create(char const *new_name, int access_mode){
STRCPY(name, new_name);
create(access_mode);
}
///Create a blc channel ///Create a blc channel
void blc_channel::create(char const *new_name, int access_mode, uint32_t type, uint32_t format, int dims_nb, blc_dim *dims){ void blc_channel::create(char const *new_name, int access_mode, uint32_t type, uint32_t format, int dims_nb, blc_dim *dims){
def_array(type, format, dims_nb, dims); def_array(type, format, dims_nb, dims);
...@@ -291,7 +299,8 @@ int blc_channel::conflict(const char *new_name){ ...@@ -291,7 +299,8 @@ int blc_channel::conflict(const char *new_name){
blc_channel info; blc_channel info;
id = blc_channel_get_info_with_name(&info, new_name); id = blc_channel_get_info_with_name(&info, new_name);
if (id!=-1) { if (id == -1) conflict_id=-1;
else {
if (info.dims_nb!=dims_nb) conflict_id=1; if (info.dims_nb!=dims_nb) conflict_id=1;
else if (info.type!=type) conflict_id=2; else if (info.type!=type) conflict_id=2;
else if (info.format!=format) conflict_id=3; else if (info.format!=format) conflict_id=3;
...@@ -441,13 +450,15 @@ int blc_channel::create_or_open(char const *new_name, int mode, uint32_t type, u ...@@ -441,13 +450,15 @@ int blc_channel::create_or_open(char const *new_name, int mode, uint32_t type, u
return created; return created;
} }
int blc_channel::create_or_open(char const *name, int mode){ int blc_channel::create_or_open(char const *name, int mode){
return create_or_open(name, mode, type, format, dims_nb, dims); return create_or_open(name, mode, type, format, dims_nb, dims);
} }
blc_channel::~blc_channel(){ blc_channel::~blc_channel(){
if (data){ if (data){
SYSTEM_SUCCESS_CHECK(munmap(data, size), 0, "Closing blc_channel '%s'", name); SYSTEM_SUCCESS_CHECK(munmap(data, size), 0, "Closing blc_channel '%s'. Size '%lu'", name, size);
data=NULL; data=NULL;
} }
...@@ -501,7 +512,6 @@ int blc_channel::post_new_data(){ ...@@ -501,7 +512,6 @@ int blc_channel::post_new_data(){
if (sem_trywait(this->sem_new_data)==0) listenner=0; if (sem_trywait(this->sem_new_data)==0) listenner=0;
else if (errno==EAGAIN) listenner=1; else if (errno==EAGAIN) listenner=1;
else EXIT_ON_SYSTEM_ERROR("blc_channel '%s'", this->name); // EAGAIN just means semaphore is busy else EXIT_ON_SYSTEM_ERROR("blc_channel '%s'", this->name); // EAGAIN just means semaphore is busy
SYSTEM_ERROR_CHECK(sem_post(this->sem_new_data), -1, "Sem new_data of blc_channel '%s'", this->name); //We signal new_data SYSTEM_ERROR_CHECK(sem_post(this->sem_new_data), -1, "Sem new_data of blc_channel '%s'", this->name); //We signal new_data
} }
return listenner; return listenner;
......
...@@ -172,7 +172,7 @@ int blc_channel_create_or_open(blc_channel *channel, const char *name, int mode, ...@@ -172,7 +172,7 @@ int blc_channel_create_or_open(blc_channel *channel, const char *name, int mode,
return created; return created;
} }
//Search for channel name whatever is start by / or . //Search for channel name whatever is start by /,: ,^ or .
int blc_channel_get_info_with_name(blc_channel *info, char const *name) int blc_channel_get_info_with_name(blc_channel *info, char const *name)
{ {
FILE *file; FILE *file;
......
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