diff --git a/libsigrok4DSL/hardware/demo/demo.c b/libsigrok4DSL/hardware/demo/demo.c index 877ae31d..afddb6ab 100644 --- a/libsigrok4DSL/hardware/demo/demo.c +++ b/libsigrok4DSL/hardware/demo/demo.c @@ -21,18 +21,18 @@ */ #include "demo.h" -#include -#include #include +#include +#include +#include #include #include #include #include -#include #include #ifdef _WIN32 -#include #include +#include #define pipe(fds) _pipe(fds, 4096, _O_BINARY) #endif @@ -42,11 +42,11 @@ extern char DS_USR_PATH[500]; static uint64_t samplerates_file[1]; static uint64_t samplecounts_file[1]; -static GTimer *packet_interval = NULL; -static GTimer *run_time = NULL; +static GTimer* packet_interval = NULL; +static GTimer* run_time = NULL; static int max_probe_num = 0; static uint64_t packet_num; -static void *logic_post_buf = NULL; +static void* logic_post_buf = NULL; /* Message logging helpers with subsystem-specific prefix string. */ @@ -55,64 +55,62 @@ static void *logic_post_buf = NULL; /* The size of chunks to send through the session bus. */ /* TODO: Should be configurable. */ -#define BUFSIZE 512*1024 -#define DSO_BUFSIZE 10*1024 +#define BUFSIZE 512 * 1024 +#define DSO_BUFSIZE 10 * 1024 #define PATTERN_COUNT 20 #define RANDOM_NAME "random" -struct demo_mode_pattern -{ - char *patterns[PATTERN_COUNT+1]; - int count; +struct demo_mode_pattern { + char* patterns[PATTERN_COUNT + 1]; + int count; }; static struct demo_mode_pattern demo_pattern_array[3]; static int b_load_directory = 0; -static char* demo_mode_names[3] = {"logic", "dso", "analog"}; +static char* demo_mode_names[3] = { "logic", "dso", "analog" }; static const struct DEMO_channels logic_channel_modes[] = { - {DEMO_LOGIC125x16, LOGIC, SR_CHANNEL_LOGIC, 16, 1, SR_MHZ(1), SR_Mn(1), - SR_KHZ(50), SR_MHZ(125), "Use 16 Channels (Max 125MHz)"}, - {DEMO_LOGIC250x12, LOGIC, SR_CHANNEL_LOGIC, 12, 1, SR_MHZ(1), SR_Mn(1), - SR_KHZ(50), SR_MHZ(250), "Use 12 Channels (Max 250MHz)"}, - {DEMO_LOGIC500x6, LOGIC, SR_CHANNEL_LOGIC, 6, 1, SR_MHZ(1), SR_Mn(1), - SR_KHZ(50), SR_MHZ(500), "Use 6 Channels (Max 500MHz)"}, - {DEMO_LOGIC1000x3, LOGIC, SR_CHANNEL_LOGIC, 3, 1, SR_MHZ(1), SR_Mn(1), - SR_KHZ(50), SR_GHZ(1), "Use 3 Channels (Max 1GHz)"}, + { (enum DEMO_CHANNEL_ID)DEMO_LOGIC125x16, LOGIC, SR_CHANNEL_LOGIC, 16, 1, SR_MHZ(1), SR_Mn(1), + SR_KHZ(50), SR_MHZ(125), "Use 16 Channels (Max 125MHz)" }, + { (enum DEMO_CHANNEL_ID)DEMO_LOGIC250x12, LOGIC, SR_CHANNEL_LOGIC, 12, 1, SR_MHZ(1), SR_Mn(1), + SR_KHZ(50), SR_MHZ(250), "Use 12 Channels (Max 250MHz)" }, + { (enum DEMO_CHANNEL_ID)DEMO_LOGIC500x6, LOGIC, SR_CHANNEL_LOGIC, 6, 1, SR_MHZ(1), SR_Mn(1), + SR_KHZ(50), SR_MHZ(500), "Use 6 Channels (Max 500MHz)" }, + { (enum DEMO_CHANNEL_ID)DEMO_LOGIC1000x3, LOGIC, SR_CHANNEL_LOGIC, 3, 1, SR_MHZ(1), SR_Mn(1), + SR_KHZ(50), SR_GHZ(1), "Use 3 Channels (Max 1GHz)" }, }; -static struct sr_list_item logic_channel_mode_list[ARRAY_SIZE(logic_channel_modes)+1]; +static struct sr_list_item logic_channel_mode_list[ARRAY_SIZE(logic_channel_modes) + 1]; static const struct DEMO_channels channel_modes[] = { // LA Stream - {DEMO_LOGIC100x16, LOGIC, SR_CHANNEL_LOGIC, 16, 1, SR_MHZ(1), SR_Mn(1), - SR_KHZ(50), SR_GHZ(1), "Use 16 Channels (Max 20MHz)"}, + { DEMO_LOGIC100x16, LOGIC, SR_CHANNEL_LOGIC, 16, 1, SR_MHZ(1), SR_Mn(1), + SR_KHZ(50), SR_GHZ(1), "Use 16 Channels (Max 20MHz)" }, // DAQ - {DEMO_ANALOG10x2, ANALOG, SR_CHANNEL_ANALOG, 2, 8, SR_MHZ(1), SR_Mn(1), - SR_HZ(10), SR_MHZ(10), "Use Channels 0~1 (Max 10MHz)"}, + { DEMO_ANALOG10x2, ANALOG, SR_CHANNEL_ANALOG, 2, 8, SR_MHZ(1), SR_Mn(1), + SR_HZ(10), SR_MHZ(10), "Use Channels 0~1 (Max 10MHz)" }, // OSC - {DEMO_DSO200x2, DSO, SR_CHANNEL_DSO, 2, 8, SR_MHZ(100), SR_Kn(10), - SR_HZ(100), SR_MHZ(200), "Use Channels 0~1 (Max 200MHz)"}, + { DEMO_DSO200x2, DSO, SR_CHANNEL_DSO, 2, 8, SR_MHZ(100), SR_Kn(10), + SR_HZ(100), SR_MHZ(200), "Use Channels 0~1 (Max 200MHz)" }, }; - /* Private, per-device-instance driver context. */ /* TODO: struct context as with the other drivers. */ /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */ SR_PRIV struct sr_dev_driver demo_driver_info; -static struct sr_dev_driver *di = &demo_driver_info; +static struct sr_dev_driver* di = &demo_driver_info; -extern struct ds_trigger *trigger; +extern struct ds_trigger* trigger; -static int get_pattern_mode_from_file(const char *sub_dir, struct demo_mode_pattern* info, int max_count); +static int get_pattern_mode_from_file(const char* sub_dir, struct demo_mode_pattern* info, int max_count); static int get_pattern_mode_index_by_string(uint8_t device_mode, const char* pattern); static const char* get_pattern_name(uint8_t device_mode, int index); -static int vdev_init(struct sr_dev_inst *sdi) +static int vdev_init(struct sr_dev_inst* sdi) { struct session_vdev* vdev = sdi->priv; vdev->version = 0; @@ -154,21 +152,21 @@ static int vdev_init(struct sr_dev_inst *sdi) vdev->post_data_len = 0; - memset(vdev->logic_sel_probe_list,0,LOGIC_MAX_PROBE_NUM); + memset(vdev->logic_sel_probe_list, 0, LOGIC_MAX_PROBE_NUM); vdev->logic_sel_probe_num = 0; vdev->logic_ch_mode = DEMO_LOGIC125x16; vdev->logic_ch_mode_index = LOGIC125x16; vdev->is_loop = FALSE; vdev->unit_bits = (sdi->mode == LOGIC) ? 1 : 8; - + return SR_OK; } -static void dso_status_update(struct session_vdev *vdev) +static void dso_status_update(struct session_vdev* vdev) { - struct sr_status *status = (struct sr_status*)&vdev->mstatus; - struct session_packet_buffer *pack_buffer = vdev->packet_buffer; + struct sr_status* status = (struct sr_status*)&vdev->mstatus; + struct session_packet_buffer* pack_buffer = vdev->packet_buffer; uint8_t ch_max = DSO_MID_VAL; uint8_t ch_min = DSO_MID_VAL; @@ -178,13 +176,12 @@ static void dso_status_update(struct session_vdev *vdev) gboolean first_plevel = FALSE; uint32_t temp_llen = 0; - for(int i = 0 ; ipost_buf +i); - if (val > ch_max){ + for (int i = 0; i < DSO_PACKET_LEN; i += 2) { + uint8_t val = *(uint8_t*)(pack_buffer->post_buf + i); + if (val > ch_max) { ch_max = val; } - if (val < ch_min){ + if (val < ch_min) { ch_min = val; } } @@ -194,142 +191,117 @@ static void dso_status_update(struct session_vdev *vdev) status->ch0_cyc_tlen = status->ch1_cyc_tlen = 0; status->ch0_cyc_llen = status->ch1_cyc_llen = 0; status->ch0_cyc_cnt = status->ch1_cyc_cnt = 0; - status->ch0_cyc_rlen = status->ch1_cyc_rlen = 0; - status->ch0_cyc_flen = status->ch1_cyc_flen = 0; - status->ch0_cyc_plen = status->ch1_cyc_plen = 0; + status->ch0_cyc_rlen = status->ch1_cyc_rlen = 0; + status->ch0_cyc_flen = status->ch1_cyc_flen = 0; + status->ch0_cyc_plen = status->ch1_cyc_plen = 0; status->ch0_level_valid = status->ch1_level_valid = 1; - status->ch0_acc_mean = status->ch1_acc_mean = 0; - - for(uint16_t i = 0 ; ipost_len || vdev->instant) - { - uint8_t val = *(uint8_t*)(pack_buffer->post_buf +i); - if (first_plevel) - { - if(val <= DSO_MID_VAL) - { + status->ch0_acc_mean = status->ch1_acc_mean = 0; + + for (uint16_t i = 0; i < DSO_PACKET_LEN; i += 2) { + if (i <= pack_buffer->post_len || vdev->instant) { + uint8_t val = *(uint8_t*)(pack_buffer->post_buf + i); + if (first_plevel) { + if (val <= DSO_MID_VAL) { status->ch0_cyc_plen++; status->ch1_cyc_plen++; } temp_llen++; - if(temp_plevel) - { + if (temp_plevel) { status->ch0_cyc_rlen++; status->ch1_cyc_rlen++; - if(val == ch_max) - { + if (val == ch_max) { temp_plevel = !temp_plevel; - if(status->ch0_plevel == temp_plevel) - { + if (status->ch0_plevel == temp_plevel) { status->ch0_cyc_cnt++; status->ch1_cyc_cnt++; status->ch0_cyc_tlen = status->ch1_cyc_tlen += temp_llen; status->ch0_cyc_llen = status->ch1_cyc_llen = 0; temp_llen = 0; - } - else{ + } else { status->ch0_cyc_llen = status->ch1_cyc_llen = temp_llen; } } - } - else - { + } else { status->ch0_cyc_flen++; status->ch1_cyc_flen++; - if(val == ch_min) - { + if (val == ch_min) { temp_plevel = !temp_plevel; - if(status->ch0_plevel == temp_plevel) - { + if (status->ch0_plevel == temp_plevel) { status->ch0_cyc_cnt++; status->ch1_cyc_cnt++; status->ch0_cyc_tlen = status->ch1_cyc_tlen += temp_llen; status->ch0_cyc_llen = status->ch1_cyc_llen = 0; temp_llen = 0; - } - else{ + } else { status->ch0_cyc_llen = status->ch1_cyc_llen = temp_llen; } } } - } - else - { - if(val == ch_max || val == ch_min) - { - if(val == ch_max){ + } else { + if (val == ch_max || val == ch_min) { + if (val == ch_max) { temp_plevel = status->ch0_plevel = status->ch1_plevel = FALSE; - } - else{ + } else { temp_plevel = status->ch0_plevel = status->ch1_plevel = TRUE; } first_plevel = TRUE; } } total_val += val; - } - else + } else break; } - status->ch0_cyc_rlen = status->ch1_cyc_rlen /=2; - status->ch0_cyc_flen = status->ch1_cyc_flen /=2; + status->ch0_cyc_rlen = status->ch1_cyc_rlen /= 2; + status->ch0_cyc_flen = status->ch1_cyc_flen /= 2; - if(vdev->sample_generator != PATTERN_RANDOM){ - status->ch0_acc_mean = status->ch1_acc_mean = DSO_MID_VAL*pack_buffer->post_len/2; - } - else{ - status->ch0_acc_mean = status->ch1_acc_mean = total_val; + if (vdev->sample_generator != PATTERN_RANDOM) { + status->ch0_acc_mean = status->ch1_acc_mean = DSO_MID_VAL * pack_buffer->post_len / 2; + } else { + status->ch0_acc_mean = status->ch1_acc_mean = total_val; } - status->ch0_acc_square = status->ch1_acc_square = ch_max*pack_buffer->post_len/2*7; + status->ch0_acc_square = status->ch1_acc_square = ch_max * pack_buffer->post_len / 2 * 7; } -static void logic_adjust_samplerate(struct session_vdev * vdev) +static void logic_adjust_samplerate(struct session_vdev* vdev) { vdev->samplerates_max_index = ARRAY_SIZE(samplerates) - 1; - while (samplerates[vdev->samplerates_max_index] > - logic_channel_modes[vdev->logic_ch_mode_index].max_samplerate){ + while (samplerates[vdev->samplerates_max_index] > logic_channel_modes[vdev->logic_ch_mode_index].max_samplerate) { vdev->samplerates_max_index--; } vdev->samplerates_min_index = 0; - while (samplerates[vdev->samplerates_min_index] < - logic_channel_modes[vdev->logic_ch_mode_index].min_samplerate){ + while (samplerates[vdev->samplerates_min_index] < logic_channel_modes[vdev->logic_ch_mode_index].min_samplerate) { vdev->samplerates_min_index++; } assert(vdev->samplerates_max_index >= vdev->samplerates_min_index); - if (vdev->samplerate > samplerates[vdev->samplerates_max_index]){ + if (vdev->samplerate > samplerates[vdev->samplerates_max_index]) { vdev->samplerate = samplerates[vdev->samplerates_max_index]; } - if (vdev->samplerate < samplerates[vdev->samplerates_min_index]){ + if (vdev->samplerate < samplerates[vdev->samplerates_min_index]) { vdev->samplerate = samplerates[vdev->samplerates_min_index]; } } - -static int init_analog_random_data(struct session_vdev * vdev) +static int init_analog_random_data(struct session_vdev* vdev) { safe_free(vdev->data_buf); vdev->data_buf = g_try_malloc0(DSO_BUF_LEN); - if (vdev->data_buf == NULL) - { + if (vdev->data_buf == NULL) { sr_err("%s: vdev->data_buf malloc failed", __func__); return SR_ERR_MALLOC; } - for(uint64_t i = 0;i < DSO_BUF_LEN ;i++) - { - if(i % 2 == 0){ + for (uint64_t i = 0; i < DSO_BUF_LEN; i++) { + if (i % 2 == 0) { *(uint8_t*)(vdev->data_buf + i) = ANALOG_RANDOM_DATA; - } - else{ - *(uint8_t*)(vdev->data_buf + i) = *(uint8_t*)(vdev->data_buf + i -1); + } else { + *(uint8_t*)(vdev->data_buf + i) = *(uint8_t*)(vdev->data_buf + i - 1); } } @@ -337,93 +309,92 @@ static int init_analog_random_data(struct session_vdev * vdev) return SR_OK; } -static int delay_time(struct session_vdev *vdev) +static int delay_time(struct session_vdev* vdev) { gdouble packet_elapsed = g_timer_elapsed(packet_interval, NULL); gdouble waittime = vdev->packet_time - packet_elapsed; - if(waittime > 0){ + if (waittime > 0) { g_usleep(SR_MS(waittime)); } return SR_OK; } -static int logic_delay_time(struct session_vdev *vdev) +static int logic_delay_time(struct session_vdev* vdev) { - gdouble ideal_time = vdev->samplerate/8; - ideal_time = vdev->packet_len/(gdouble)ideal_time; - ideal_time = vdev->logci_cur_packet_num*ideal_time; + gdouble ideal_time = vdev->samplerate / 8; + ideal_time = vdev->packet_len / (gdouble)ideal_time; + ideal_time = vdev->logci_cur_packet_num * ideal_time; gdouble packet_elapsed = g_timer_elapsed(run_time, NULL); gdouble waittime = ideal_time - packet_elapsed; - if(waittime > 0){ + if (waittime > 0) { g_usleep(SR_MS(waittime)); } return SR_OK; } -static int instant_delay_time(struct session_vdev *vdev) +static int instant_delay_time(struct session_vdev* vdev) { - uint16_t cur_packet_num = vdev->post_data_len/vdev->packet_len; + uint16_t cur_packet_num = vdev->post_data_len / vdev->packet_len; gdouble ideal_time = cur_packet_num * vdev->packet_time; gdouble totol_time = g_timer_elapsed(run_time, NULL); gdouble waittime = ideal_time - totol_time; - if(waittime > 0){ + if (waittime > 0) { g_usleep(SR_MS(waittime)); } return SR_OK; } -static void get_last_packet_len(struct sr_datafeed_logic *logic,struct session_vdev * vdev) +static void get_last_packet_len(struct sr_datafeed_logic* logic, struct session_vdev* vdev) { assert(vdev); int last_packet_len = vdev->post_data_len - (logic->length / vdev->enabled_probes); - last_packet_len = (vdev->total_samples/8) - last_packet_len; + last_packet_len = (vdev->total_samples / 8) - last_packet_len; logic->length = last_packet_len * vdev->enabled_probes; - vdev->post_data_len = vdev->total_samples/8; + vdev->post_data_len = vdev->total_samples / 8; } static int get_pattern_mode_index_by_string(uint8_t device_mode, const char* pattern) { int dex = 0; - struct demo_mode_pattern* info = &demo_pattern_array[device_mode]; + struct demo_mode_pattern* info = &demo_pattern_array[device_mode]; - while (dex < info->count) - { - if (strcmp(info->patterns[dex], pattern) == 0){ + while (dex < info->count) { + if (strcmp(info->patterns[dex], pattern) == 0) { return dex; } dex++; } - return -1; + return -1; } static const char* get_pattern_name(uint8_t device_mode, int index) { - struct demo_mode_pattern* info = &demo_pattern_array[device_mode]; - if (index >= 0 && index < info->count){ + struct demo_mode_pattern* info = &demo_pattern_array[device_mode]; + if (index >= 0 && index < info->count) { return info->patterns[index]; - } + } assert(0); return NULL; } -static int get_pattern_mode_from_file(const char *sub_dir, struct demo_mode_pattern* info, int max_count) +static int get_pattern_mode_from_file(const char* sub_dir, struct demo_mode_pattern* info, int max_count) { - const gchar * filename = NULL; + const gchar* filename = NULL; char dir_path_buf[500]; int str_len; - char *dir_path = dir_path_buf; - const char *file_path = NULL; - char short_name[50]; + char* dir_path = dir_path_buf; + const char* file_path = NULL; + char short_name[50]; int i; int num; assert(sub_dir); assert(info); - for (i=0; ipatterns[i] = NULL; } info->patterns[max_count] = NULL; @@ -432,32 +403,28 @@ static int get_pattern_mode_from_file(const char *sub_dir, struct demo_mode_patt num = 1; info->count = num; - strcpy(dir_path, DS_USR_PATH); - strcat(dir_path,"/demo/"); + strcpy(dir_path, DS_USR_PATH); + strcat(dir_path, "/demo/"); strcat(dir_path, sub_dir); - - GDir *dir = NULL; - dir = g_dir_open(dir_path,0,NULL); - if(dir == NULL) - { + + GDir* dir = NULL; + dir = g_dir_open(dir_path, 0, NULL); + if (dir == NULL) { sr_err("Faild to open dir:%s", dir_path); return SR_ERR; } - while ((filename = g_dir_read_name(dir)) != NULL) - { - if (FALSE == g_file_test(filename,G_FILE_TEST_IS_DIR)) - { - file_path = filename; + while ((filename = g_dir_read_name(dir)) != NULL) { + if (FALSE == g_file_test(filename, G_FILE_TEST_IS_DIR)) { + file_path = filename; - if(strstr(file_path,".demo") != NULL) - { + if (strstr(file_path, ".demo") != NULL) { str_len = strlen(file_path) - 5; - strncpy(short_name,file_path, sizeof(short_name)-1); + strncpy(short_name, file_path, sizeof(short_name) - 1); short_name[str_len] = 0; info->patterns[num++] = g_strdup(short_name); - if (num >= max_count){ + if (num >= max_count) { break; } } @@ -470,13 +437,12 @@ static int get_pattern_mode_from_file(const char *sub_dir, struct demo_mode_patt return SR_OK; } -static void scan_dsl_file(struct sr_dev_inst *sdi) +static void scan_dsl_file(struct sr_dev_inst* sdi) { - struct session_vdev * vdev = sdi->priv; + struct session_vdev* vdev = sdi->priv; int dex; - if (b_load_directory == 0) - { + if (b_load_directory == 0) { get_pattern_mode_from_file(demo_mode_names[LOGIC], &demo_pattern_array[LOGIC], PATTERN_COUNT); get_pattern_mode_from_file(demo_mode_names[DSO], &demo_pattern_array[DSO], PATTERN_COUNT); get_pattern_mode_from_file(demo_mode_names[ANALOG], &demo_pattern_array[ANALOG], PATTERN_COUNT); @@ -485,7 +451,7 @@ static void scan_dsl_file(struct sr_dev_inst *sdi) dex = get_pattern_mode_index_by_string(LOGIC, DEFAULT_LOGIC_FILE); - if(dex == -1){ + if (dex == -1) { dex = PATTERN_RANDOM; } @@ -494,134 +460,120 @@ static void scan_dsl_file(struct sr_dev_inst *sdi) reset_dsl_path(sdi, dex); } -static int reset_dsl_path(struct sr_dev_inst *sdi, uint8_t pattern_mode) -{ - struct demo_mode_pattern *info = NULL; +static int reset_dsl_path(struct sr_dev_inst* sdi, uint8_t pattern_mode) +{ + struct demo_mode_pattern* info = NULL; char file_path[500]; safe_free(sdi->path); strcpy(file_path, DS_USR_PATH); - strcat(file_path,"/demo/"); + strcat(file_path, "/demo/"); - if (pattern_mode != PATTERN_RANDOM) - { + if (pattern_mode != PATTERN_RANDOM) { info = &demo_pattern_array[sdi->mode]; assert(pattern_mode < info->count); strcat(file_path, demo_mode_names[sdi->mode]); strcat(file_path, "/"); strcat(file_path, info->patterns[pattern_mode]); - strcat(file_path,".demo"); + strcat(file_path, ".demo"); sdi->path = g_strdup(file_path); - } - else{ + } else { sdi->path = g_strdup(""); } - + return SR_OK; } -static void adjust_samplerate(struct sr_dev_inst *sdi) +static void adjust_samplerate(struct sr_dev_inst* sdi) { - struct session_vdev *vdev = sdi->priv; - if(sdi->mode == LOGIC && vdev->sample_generator > PATTERN_RANDOM){ + struct session_vdev* vdev = sdi->priv; + if (sdi->mode == LOGIC && vdev->sample_generator > PATTERN_RANDOM) { return; } int cur_mode = -1; - if(sdi->mode == LOGIC){ + if (sdi->mode == LOGIC) { cur_mode = 0; - } - else if(sdi->mode == DSO){ + } else if (sdi->mode == DSO) { cur_mode = 2; - } - else if(sdi->mode == ANALOG){ + } else if (sdi->mode == ANALOG) { cur_mode = 1; } - if(cur_mode == -1){ + if (cur_mode == -1) { return; } vdev->samplerates_max_index = ARRAY_SIZE(samplerates) - 1; - while (samplerates[vdev->samplerates_max_index] > - channel_modes[cur_mode].max_samplerate){ + while (samplerates[vdev->samplerates_max_index] > channel_modes[cur_mode].max_samplerate) { vdev->samplerates_max_index--; } vdev->samplerates_min_index = 0; - while (samplerates[vdev->samplerates_min_index] < - channel_modes[cur_mode].min_samplerate){ + while (samplerates[vdev->samplerates_min_index] < channel_modes[cur_mode].min_samplerate) { vdev->samplerates_min_index++; } - assert(vdev->samplerates_max_index >= vdev->samplerates_min_index); - if (vdev->samplerate > samplerates[vdev->samplerates_max_index]){ + if (vdev->samplerate > samplerates[vdev->samplerates_max_index]) { vdev->samplerate = samplerates[vdev->samplerates_max_index]; } - if (vdev->samplerate < samplerates[vdev->samplerates_min_index]){ + if (vdev->samplerate < samplerates[vdev->samplerates_min_index]) { vdev->samplerate = samplerates[vdev->samplerates_min_index]; } } -static int init_random_data(struct session_vdev *vdev) +static int init_random_data(struct session_vdev* vdev) { int cur_probe = 0; - int probe_count[LOGIC_MAX_PROBE_NUM] = {0}; - uint8_t probe_status[LOGIC_MAX_PROBE_NUM] = {LOGIC_HIGH_LEVEL}; + int probe_count[LOGIC_MAX_PROBE_NUM] = { 0 }; + uint8_t probe_status[LOGIC_MAX_PROBE_NUM] = { LOGIC_HIGH_LEVEL }; uint64_t i; static int rand_k = 0; assert(vdev->data_buf); - memset(probe_status,LOGIC_HIGH_LEVEL,16); - memset(vdev->data_buf,0,LOGIC_BUF_LEN); + memset(probe_status, LOGIC_HIGH_LEVEL, 16); + memset(vdev->data_buf, 0, LOGIC_BUF_LEN); srand((unsigned int)(time(NULL) + rand() + (rand_k++))); - for(int i = 0 ;i < vdev->enabled_probes;i++){ - probe_count[i] = rand()%SR_KB(1); + for (int i = 0; i < vdev->enabled_probes; i++) { + probe_count[i] = rand() % SR_KB(1); } - for(i = 0 ; i < vdev->data_buf_len ;i++) - { - if(i % 8 == 0 && i != 0) - { + for (i = 0; i < vdev->data_buf_len; i++) { + if (i % 8 == 0 && i != 0) { cur_probe++; - if(cur_probe >vdev->enabled_probes-1){ + if (cur_probe > vdev->enabled_probes - 1) { cur_probe = 0; } } - if(probe_count[cur_probe]> 0) - { - memset(vdev->data_buf+i,probe_status[cur_probe],1); + if (probe_count[cur_probe] > 0) { + memset(vdev->data_buf + i, probe_status[cur_probe], 1); probe_count[cur_probe] -= 1; - } - else - { - if(probe_status[cur_probe] == LOGIC_HIGH_LEVEL){ + } else { + if (probe_status[cur_probe] == LOGIC_HIGH_LEVEL) { probe_status[cur_probe] = LOGIC_LOW_LEVEL; - } - else{ + } else { probe_status[cur_probe] = LOGIC_HIGH_LEVEL; } - probe_count[cur_probe] = rand()%SR_KB(1); - memset(vdev->data_buf+i,probe_status[cur_probe],1); + probe_count[cur_probe] = rand() % SR_KB(1); + memset(vdev->data_buf + i, probe_status[cur_probe], 1); probe_count[cur_probe] -= 1; } } - return SR_OK; + return SR_OK; } - -static int hw_init(struct sr_context *sr_ctx) +static int hw_init(struct sr_context* sr_ctx) { return std_hw_init(sr_ctx, di, LOG_PREFIX); } @@ -631,29 +583,27 @@ static int hw_clean_up() return SR_OK; } -static GSList *hw_scan(GSList *options) +static GSList* hw_scan(GSList* options) { - struct sr_dev_inst *sdi; - struct session_vdev *vdev; - GSList *devices; + struct sr_dev_inst* sdi; + struct session_vdev* vdev; + GSList* devices; (void)options; devices = NULL; vdev = g_try_malloc0(sizeof(struct session_vdev)); - if (vdev == NULL) - { + if (vdev == NULL) { sr_err("%s: sdi->priv malloc failed", __func__); return devices; } memset(vdev, 0, sizeof(struct session_vdev)); sdi = sr_dev_inst_new(LOGIC, SR_ST_INACTIVE, - supported_Demo[0].vendor, - supported_Demo[0].model, - supported_Demo[0].model_version); - if (!sdi) - { + supported_Demo[0].vendor, + supported_Demo[0].model, + supported_Demo[0].model_version); + if (!sdi) { safe_free(vdev); sr_err("Device instance creation failed."); return NULL; @@ -670,16 +620,15 @@ static GSList *hw_scan(GSList *options) return devices; } -static const GSList *hw_dev_mode_list(const struct sr_dev_inst *sdi) +static const GSList* hw_dev_mode_list(const struct sr_dev_inst* sdi) { (void)sdi; - GSList *l = NULL; + GSList* l = NULL; unsigned int i; - for (i = 0; i < ARRAY_SIZE(sr_mode_list); i++) - { - if (supported_Demo[0].dev_caps.mode_caps & (1 << i)){ + for (i = 0; i < ARRAY_SIZE(sr_mode_list); i++) { + if (supported_Demo[0].dev_caps.mode_caps & (1 << i)) { l = g_slist_append(l, (gpointer)&sr_mode_list[i]); } } @@ -687,29 +636,28 @@ static const GSList *hw_dev_mode_list(const struct sr_dev_inst *sdi) return l; } -static int hw_dev_open(struct sr_dev_inst *sdi) +static int hw_dev_open(struct sr_dev_inst* sdi) { int ret; assert(sdi); assert(sdi->priv); - if (sdi->status == SR_ST_ACTIVE){ + if (sdi->status == SR_ST_ACTIVE) { return SR_OK; } scan_dsl_file(sdi); - + ret = vdev_init(sdi); - if (ret == SR_OK){ + if (ret == SR_OK) { sdi->status = SR_ST_ACTIVE; } - packet_interval = g_timer_new(); - run_time = g_timer_new(); - + packet_interval = g_timer_new(); + run_time = g_timer_new(); + ret = load_virtual_device_session(sdi); - if (ret != SR_OK) - { + if (ret != SR_OK) { sr_err("Error!Load session file failed."); return ret; } @@ -717,30 +665,25 @@ static int hw_dev_open(struct sr_dev_inst *sdi) return SR_OK; } -static int hw_dev_close(struct sr_dev_inst *sdi) +static int hw_dev_close(struct sr_dev_inst* sdi) { - struct session_vdev *vdev; + struct session_vdev* vdev; int i; - struct session_packet_buffer *pack_buf; + struct session_packet_buffer* pack_buf; - if (sdi && sdi->priv) - { + if (sdi && sdi->priv) { vdev = sdi->priv; - if (vdev->packet_buffer != NULL) - { + if (vdev->packet_buffer != NULL) { pack_buf = vdev->packet_buffer; safe_free(pack_buf->post_buf); - for (i = 0; i < SESSION_MAX_CHANNEL_COUNT; i++) - { - if (pack_buf->block_bufs[i] != NULL) - { + for (i = 0; i < SESSION_MAX_CHANNEL_COUNT; i++) { + if (pack_buf->block_bufs[i] != NULL) { safe_free(pack_buf->block_bufs[i]); pack_buf->block_bufs[i] = NULL; - } - else{ + } else { break; } } @@ -760,30 +703,29 @@ static int hw_dev_close(struct sr_dev_inst *sdi) return SR_ERR_CALL_STATUS; } -static int dev_destroy(struct sr_dev_inst *sdi) +static int dev_destroy(struct sr_dev_inst* sdi) { assert(sdi); hw_dev_close(sdi); sr_dev_inst_free(sdi); - + return SR_OK; } -static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi, - const struct sr_channel *ch, - const struct sr_channel_group *cg) +static int config_get(int id, GVariant** data, const struct sr_dev_inst* sdi, + const struct sr_channel* ch, + const struct sr_channel_group* cg) { (void)cg; - const char *patter_name = NULL; + const char* patter_name = NULL; assert(sdi); assert(sdi->priv); - struct session_vdev *vdev = sdi->priv; + struct session_vdev* vdev = sdi->priv; - switch (id) - { + switch (id) { case SR_CONF_SAMPLERATE: *data = g_variant_new_uint64(vdev->samplerate); break; @@ -807,22 +749,22 @@ static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi, *data = g_variant_new_byte(vdev->max_height); break; case SR_CONF_PROBE_OFFSET: - if (ch){ + if (ch) { *data = g_variant_new_uint16(ch->offset); } break; case SR_CONF_PROBE_HW_OFFSET: - if (ch){ + if (ch) { *data = g_variant_new_uint16(ch->hw_offset); } break; case SR_CONF_PROBE_VDIV: - if (ch){ + if (ch) { *data = g_variant_new_uint64(ch->vdiv); } break; case SR_CONF_PROBE_FACTOR: - if (ch){ + if (ch) { *data = g_variant_new_uint64(ch->vfactor); } break; @@ -840,12 +782,12 @@ static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi, *data = g_variant_new_byte(ch->coupling); break; case SR_CONF_TRIGGER_VALUE: - if (ch){ + if (ch) { *data = g_variant_new_byte(ch->trig_value); } break; case SR_CONF_PROBE_EN: - if (ch){ + if (ch) { *data = g_variant_new_boolean(ch->enabled); } break; @@ -856,50 +798,48 @@ static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi, *data = g_variant_new_uint64(SR_Kn(20)); break; case SR_CONF_HW_DEPTH: - switch (sdi->mode) - { + switch (sdi->mode) { case LOGIC: - if(vdev->sample_generator == PATTERN_RANDOM){ + if (vdev->sample_generator == PATTERN_RANDOM) { *data = g_variant_new_uint64(LOGIC_HW_DEPTH); - } - else{ + } else { *data = g_variant_new_uint64(vdev->total_samples); } break; case DSO: - *data = g_variant_new_uint64(vdev->total_samples); + *data = g_variant_new_uint64(vdev->total_samples); break; case ANALOG: - *data = g_variant_new_uint64(ANALOG_HW_DEPTH); + *data = g_variant_new_uint64(ANALOG_HW_DEPTH); break; default: break; } break; case SR_CONF_UNIT_BITS: - *data = g_variant_new_byte(vdev->unit_bits); + *data = g_variant_new_byte(vdev->unit_bits); break; case SR_CONF_PROBE_MAP_DEFAULT: - if (!sdi || !ch){ + if (!sdi || !ch) { return SR_ERR; } *data = g_variant_new_boolean(ch->map_default); break; case SR_CONF_PROBE_MAP_UNIT: - if (!sdi || !ch){ + if (!sdi || !ch) { return SR_ERR; } - + *data = g_variant_new_string(ch->map_unit); break; case SR_CONF_PROBE_MAP_MIN: - if (!sdi || !ch){ + if (!sdi || !ch) { return SR_ERR; } *data = g_variant_new_double(ch->map_min); break; case SR_CONF_PROBE_MAP_MAX: - if (!sdi || !ch){ + if (!sdi || !ch) { return SR_ERR; } *data = g_variant_new_double(ch->map_max); @@ -932,14 +872,14 @@ static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi, return SR_OK; } -static int config_set(int id, GVariant *data, struct sr_dev_inst *sdi, - struct sr_channel *ch, - struct sr_channel_group *cg) +static int config_set(int id, GVariant* data, struct sr_dev_inst* sdi, + struct sr_channel* ch, + struct sr_channel_group* cg) { - (void)cg; + (void)cg; - struct session_vdev *vdev; - const char *stropt; + struct session_vdev* vdev; + const char* stropt; unsigned int i; int nv; uint8_t tmp_sample_generator; @@ -949,8 +889,7 @@ static int config_set(int id, GVariant *data, struct sr_dev_inst *sdi, vdev = sdi->priv; - switch (id) - { + switch (id) { case SR_CONF_SAMPLERATE: vdev->samplerate = g_variant_get_uint64(data); sr_dbg("Setting samplerate to %llu.", (u64_t)vdev->samplerate); @@ -963,43 +902,37 @@ static int config_set(int id, GVariant *data, struct sr_dev_inst *sdi, break; case SR_CONF_DEVICE_MODE: sdi->mode = g_variant_get_int16(data); - nv = get_pattern_mode_index_by_string(sdi->mode, (sdi->mode == LOGIC ? DEFAULT_LOGIC_FILE : - sdi->mode == DSO ? DEFAULT_DSO_FILE : DEFAULT_ANALOG_FILE)); - if (nv != -1){ + nv = get_pattern_mode_index_by_string(sdi->mode, (sdi->mode == LOGIC ? DEFAULT_LOGIC_FILE : sdi->mode == DSO ? DEFAULT_DSO_FILE + : DEFAULT_ANALOG_FILE)); + if (nv != -1) { vdev->sample_generator = nv; - } - else{ + } else { vdev->sample_generator = PATTERN_RANDOM; } - reset_dsl_path(sdi,vdev->sample_generator); + reset_dsl_path(sdi, vdev->sample_generator); load_virtual_device_session(sdi); break; case SR_CONF_PATTERN_MODE: stropt = g_variant_get_string(data, NULL); tmp_sample_generator = vdev->sample_generator; - nv = get_pattern_mode_index_by_string(sdi->mode , stropt); - if(nv == -1){ + nv = get_pattern_mode_index_by_string(sdi->mode, stropt); + if (nv == -1) { vdev->sample_generator = PATTERN_RANDOM; - } - else{ + } else { vdev->sample_generator = nv; } reset_dsl_path(sdi, vdev->sample_generator); - if(sdi->mode == LOGIC) - { - if(vdev->sample_generator == PATTERN_RANDOM) - { - if(!vdev->channel_mode_change && tmp_sample_generator != vdev->sample_generator) - { + if (sdi->mode == LOGIC) { + if (vdev->sample_generator == PATTERN_RANDOM) { + if (!vdev->channel_mode_change && tmp_sample_generator != vdev->sample_generator) { vdev->logic_ch_mode = DEMO_LOGIC125x16; vdev->logic_ch_mode_index = LOGIC125x16; load_virtual_device_session(sdi); } vdev->channel_mode_change = FALSE; - } - else{ + } else { load_virtual_device_session(sdi); - } + } } sr_dbg("%s: setting pattern to %d", @@ -1008,26 +941,22 @@ static int config_set(int id, GVariant *data, struct sr_dev_inst *sdi, case SR_CONF_MAX_HEIGHT: stropt = g_variant_get_string(data, NULL); - for (i = 0; i < ARRAY_SIZE(maxHeights); i++) - { - if (!strcmp(stropt, maxHeights[i])) - { + for (i = 0; i < ARRAY_SIZE(maxHeights); i++) { + if (!strcmp(stropt, maxHeights[i])) { vdev->max_height = i; break; } } sr_dbg("%s: setting Signal Max Height to %d", - __func__, vdev->max_height); + __func__, vdev->max_height); break; case SR_CONF_PROBE_EN: ch->enabled = g_variant_get_boolean(data); break; case SR_CONF_PROBE_VDIV: ch->vdiv = g_variant_get_uint64(data); - if(sdi->mode == DSO) - { - if(vdev->packet_buffer) - { + if (sdi->mode == DSO) { + if (vdev->packet_buffer) { vdev->packet_buffer->post_len = 0; vdev->packet_buffer->block_read_positions[0] = 0; vdev->packet_buffer->block_read_positions[1] = 0; @@ -1041,16 +970,13 @@ static int config_set(int id, GVariant *data, struct sr_dev_inst *sdi, break; case SR_CONF_PROBE_OFFSET: ch->offset = g_variant_get_uint16(data); - if(sdi->mode == DSO && ch->vdiv <= SR_mV(200)) - { + if (sdi->mode == DSO && ch->vdiv <= SR_mV(200)) { ch->hw_offset = ch->offset; vdev->offset_change = TRUE; - } - else{ - if(ch->coupling == 0){ + } else { + if (ch->coupling == 0) { ch->hw_offset = ANALOG_DC_COUL_OFFSET; - } - else{ + } else { ch->hw_offset = ANALOG_AC_COUL_OFFSET; } } @@ -1060,30 +986,24 @@ static int config_set(int id, GVariant *data, struct sr_dev_inst *sdi, break; case SR_CONF_TIMEBASE: vdev->timebase = g_variant_get_uint64(data); - if(sdi->mode == DSO) - { + if (sdi->mode == DSO) { g_timer_start(run_time); vdev->timebase_change = TRUE; - } + } sr_dbg("Setting timebase to %llu.", (u64_t)vdev->timebase); break; case SR_CONF_PROBE_COUPLING: - if(sdi->mode != LOGIC) - { - if(sdi->mode == DSO && ch->vdiv <= SR_mV(200)) - { + if (sdi->mode != LOGIC) { + if (sdi->mode == DSO && ch->vdiv <= SR_mV(200)) { ch->coupling = g_variant_get_byte(data); ch->hw_offset = ch->offset; - } - else - { + } else { ch->coupling = g_variant_get_byte(data); - if(ch->coupling == 0){ + if (ch->coupling == 0) { ch->hw_offset = ANALOG_DC_COUL_OFFSET; - } - else{ + } else { ch->hw_offset = ANALOG_AC_COUL_OFFSET; - } + } } } break; @@ -1096,8 +1016,7 @@ static int config_set(int id, GVariant *data, struct sr_dev_inst *sdi, break; case SR_CONF_PROBE_MAP_DEFAULT: ch->map_default = g_variant_get_boolean(data); - if (ch->map_default) - { + if (ch->map_default) { ch->map_unit = probeMapUnits[0]; ch->map_min = -(ch->vdiv * ch->vfactor * DS_CONF_DSO_VDIVS / 2000.0); ch->map_max = ch->vdiv * ch->vfactor * DS_CONF_DSO_VDIVS / 2000.0; @@ -1128,12 +1047,9 @@ static int config_set(int id, GVariant *data, struct sr_dev_inst *sdi, break; case SR_CONF_CHANNEL_MODE: nv = g_variant_get_int16(data); - if(sdi->mode == LOGIC && vdev->sample_generator == PATTERN_RANDOM) - { - for(i = 0 ; i < ARRAY_SIZE(logic_channel_modes);i++) - { - if(logic_channel_modes[i].id == (enum DEMO_CHANNEL_ID)nv) - { + if (sdi->mode == LOGIC && vdev->sample_generator == PATTERN_RANDOM) { + for (i = 0; i < ARRAY_SIZE(logic_channel_modes); i++) { + if (logic_channel_modes[i].id == (enum DEMO_CHANNEL_ID)nv) { vdev->logic_ch_mode_index = i; vdev->logic_ch_mode = (enum DEMO_CHANNEL_ID)nv; load_virtual_device_session(sdi); @@ -1143,7 +1059,7 @@ static int config_set(int id, GVariant *data, struct sr_dev_inst *sdi, } } break; - case SR_CONF_HORIZ_TRIGGERPOS: + case SR_CONF_HORIZ_TRIGGERPOS: vdev->trigger_hrate = g_variant_get_byte(data); break; default: @@ -1152,41 +1068,38 @@ static int config_set(int id, GVariant *data, struct sr_dev_inst *sdi, } return SR_OK; - } -static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, - const struct sr_channel_group *cg) +static int config_list(int key, GVariant** data, const struct sr_dev_inst* sdi, + const struct sr_channel_group* cg) { (void)cg; - GVariant *gvar; + GVariant* gvar; GVariantBuilder gvb; - struct demo_mode_pattern *info = NULL; + struct demo_mode_pattern* info = NULL; unsigned int i; (void)sdi; - struct session_vdev *vdev = sdi->priv; + struct session_vdev* vdev = sdi->priv; - switch (key) - { + switch (key) { case SR_CONF_DEVICE_OPTIONS: *data = g_variant_new_from_data(G_VARIANT_TYPE("ai"), - hwoptions, ARRAY_SIZE(hwoptions) * sizeof(int32_t), TRUE, NULL, NULL); + hwoptions, ARRAY_SIZE(hwoptions) * sizeof(int32_t), TRUE, NULL, NULL); break; case SR_CONF_DEVICE_SESSIONS: *data = g_variant_new_from_data(G_VARIANT_TYPE("ai"), - sessions, ARRAY_SIZE(sessions) * sizeof(int32_t), TRUE, NULL, NULL); + sessions, ARRAY_SIZE(sessions) * sizeof(int32_t), TRUE, NULL, NULL); break; case SR_CONF_SAMPLERATE: g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); - if(sdi->mode == LOGIC && vdev->sample_generator != PATTERN_RANDOM){ - gvar = g_variant_new_from_data(G_VARIANT_TYPE("at"), - samplerates_file, ARRAY_SIZE(samplerates_file) * sizeof(uint64_t), TRUE, NULL, NULL); - } - else{ + if (sdi->mode == LOGIC && vdev->sample_generator != PATTERN_RANDOM) { gvar = g_variant_new_from_data(G_VARIANT_TYPE("at"), - samplerates + vdev->samplerates_min_index , (vdev->samplerates_max_index - vdev->samplerates_min_index + 1) * sizeof(uint64_t), TRUE, NULL, NULL); + samplerates_file, ARRAY_SIZE(samplerates_file) * sizeof(uint64_t), TRUE, NULL, NULL); + } else { + gvar = g_variant_new_from_data(G_VARIANT_TYPE("at"), + samplerates + vdev->samplerates_min_index, (vdev->samplerates_max_index - vdev->samplerates_min_index + 1) * sizeof(uint64_t), TRUE, NULL, NULL); } g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); *data = g_variant_builder_end(&gvb); @@ -1200,19 +1113,19 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, break; case SR_CONF_PROBE_CONFIGS: *data = g_variant_new_from_data(G_VARIANT_TYPE("ai"), - probeOptions, ARRAY_SIZE(probeOptions) * sizeof(int32_t), TRUE, NULL, NULL); + probeOptions, ARRAY_SIZE(probeOptions) * sizeof(int32_t), TRUE, NULL, NULL); break; case SR_CONF_PROBE_VDIV: g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); gvar = g_variant_new_from_data(G_VARIANT_TYPE("at"), - vdivs10to2000, (ARRAY_SIZE(vdivs10to2000)-1) * sizeof(uint64_t), TRUE, NULL, NULL); + vdivs10to2000, (ARRAY_SIZE(vdivs10to2000) - 1) * sizeof(uint64_t), TRUE, NULL, NULL); g_variant_builder_add(&gvb, "{sv}", "vdivs", gvar); *data = g_variant_builder_end(&gvb); break; case SR_CONF_PROBE_COUPLING: g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); gvar = g_variant_new_from_data(G_VARIANT_TYPE("ay"), - probeCoupling, ARRAY_SIZE(probeCoupling)*sizeof(uint8_t), TRUE, NULL, NULL); + probeCoupling, ARRAY_SIZE(probeCoupling) * sizeof(uint8_t), TRUE, NULL, NULL); g_variant_builder_add(&gvb, "{sv}", "coupling", gvar); *data = g_variant_builder_end(&gvb); break; @@ -1220,18 +1133,15 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, *data = g_variant_new_strv(probeMapUnits, ARRAY_SIZE(probeMapUnits)); break; case SR_CONF_CHANNEL_MODE: - if(sdi->mode == LOGIC&& vdev->sample_generator == PATTERN_RANDOM) - { - for(i = 0; imode == LOGIC && vdev->sample_generator == PATTERN_RANDOM) { + for (i = 0; i < ARRAY_SIZE(logic_channel_modes); i++) { logic_channel_mode_list[i].id = (int)logic_channel_modes[i].id; logic_channel_mode_list[i].name = logic_channel_modes[i].descr; } logic_channel_mode_list[i].id = -1; logic_channel_mode_list[i].name = NULL; *data = g_variant_new_uint64((uint64_t)&logic_channel_mode_list); - } - else{ + } else { return SR_ERR_ARG; } break; @@ -1242,15 +1152,15 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, return SR_OK; } -static int hw_dev_acquisition_start(struct sr_dev_inst *sdi, - void *cb_data) +static int hw_dev_acquisition_start(struct sr_dev_inst* sdi, + void* cb_data) { (void)cb_data; - struct session_vdev *vdev; - GSList *l; - struct sr_channel *probe; + struct session_vdev* vdev; + GSList* l; + struct sr_channel* probe; assert(sdi); assert(sdi->priv); @@ -1263,25 +1173,21 @@ static int hw_dev_acquisition_start(struct sr_dev_inst *sdi, safe_free(vdev->data_buf); - if (sdi->mode == LOGIC) - { - vdev->data_buf = g_try_malloc0(LOGIC_BUF_LEN); + if (sdi->mode == LOGIC) { + vdev->data_buf = g_try_malloc0(LOGIC_BUF_LEN); vdev->data_buf_len = LOGIC_BUF_LEN; - } - else{ + } else { vdev->data_buf = g_try_malloc0(DSO_PACKET_LEN); vdev->data_buf_len = DSO_PACKET_LEN; } - if(vdev->data_buf == NULL) - { + if (vdev->data_buf == NULL) { sr_err("%s: vdev->data_buf malloc failed", __func__); - return SR_ERR_MALLOC; + return SR_ERR_MALLOC; } - if(vdev->sample_generator != PATTERN_RANDOM) - { - if (vdev->archive != NULL){ + if (vdev->sample_generator != PATTERN_RANDOM) { + if (vdev->archive != NULL) { sr_err("history archive is not closed."); } @@ -1289,115 +1195,97 @@ static int hw_dev_acquisition_start(struct sr_dev_inst *sdi, vdev->archive = unzOpen64(sdi->path); - if (NULL == vdev->archive) - { + if (NULL == vdev->archive) { sr_err("Failed to open session file '%s': " - "zip error", + "zip error", sdi->path); return SR_ERR; } } - for (l = sdi->channels; l; l = l->next) - { + for (l = sdi->channels; l; l = l->next) { probe = l->data; if (probe->enabled) vdev->enabled_probes++; } - if(sdi->mode == LOGIC) - { + if (sdi->mode == LOGIC) { vdev->post_data_len = 0; vdev->packet_len = LOGIC_PACKET_LEN(vdev->samplerate); vdev->packet_time = LOGIC_PACKET_TIME(LOGIC_PACKET_NUM_PER_SEC); - if(vdev->packet_len < LOGIC_MIN_PACKET_LEN) - { + if (vdev->packet_len < LOGIC_MIN_PACKET_LEN) { vdev->packet_len = LOGIC_MIN_PACKET_LEN; vdev->packet_time = LOGIC_MIN_PACKET_TIME(vdev->samplerate); } - if(vdev->samplerate >= SR_MHZ(100)) - { + if (vdev->samplerate >= SR_MHZ(100)) { uint64_t ideal_len = 2; - while (1) - { - if(ideal_len>=vdev->packet_len){ + while (1) { + if (ideal_len >= vdev->packet_len) { vdev->packet_len = ideal_len; break; - } - else{ - ideal_len *=2; + } else { + ideal_len *= 2; } } - - vdev->packet_time = vdev->samplerate/8; - packet_num = ceil(vdev->packet_time/(gdouble)vdev->packet_len); - vdev->packet_time = 1/(gdouble)packet_num; - vdev->logic_sel_probe_num = 0; - } - - if(vdev->sample_generator == PATTERN_RANDOM) - { + + vdev->packet_time = vdev->samplerate / 8; + packet_num = ceil(vdev->packet_time / (gdouble)vdev->packet_len); + vdev->packet_time = 1 / (gdouble)packet_num; + vdev->logic_sel_probe_num = 0; + } + + if (vdev->sample_generator == PATTERN_RANDOM) { vdev->logci_cur_packet_num = 1; safe_free(logic_post_buf); logic_post_buf = g_try_malloc0(vdev->enabled_probes * vdev->packet_len); - if(logic_post_buf == NULL) - { + if (logic_post_buf == NULL) { sr_err("%s: logic_post_buf malloc error", __func__); return SR_ERR_MALLOC; } - vdev->logic_mem_limit = (((vdev->total_samples/8)*vdev->enabled_probes) >= LOGIC_MEMORY_LIMIT) ? TRUE : FALSE; + vdev->logic_mem_limit = (((vdev->total_samples / 8) * vdev->enabled_probes) >= LOGIC_MEMORY_LIMIT) ? TRUE : FALSE; assert(run_time); init_random_data(vdev); g_timer_start(run_time); sr_session_source_add(-1, 0, 0, receive_data_logic, sdi); - } - else{ + } else { sr_session_source_add(-1, 0, 0, receive_data_logic_decoder, sdi); } - } - else if(sdi->mode == DSO) - { + } else if (sdi->mode == DSO) { vdev->vdiv_change = TRUE; - if(vdev->instant){ + if (vdev->instant) { vdev->post_data_len = 0; - gdouble total_time = vdev->timebase /(gdouble)SR_SEC(1)*(gdouble)10; - uint64_t post_data_per_sec = DSO_PACKET_LEN/total_time; + gdouble total_time = vdev->timebase / (gdouble)SR_SEC(1) * (gdouble)10; + uint64_t post_data_per_sec = DSO_PACKET_LEN / total_time; vdev->packet_len = 2; - uint64_t packet_num = post_data_per_sec/vdev->packet_len; - vdev->packet_time = SEC/(gdouble)packet_num; - } - else{ + uint64_t packet_num = post_data_per_sec / vdev->packet_len; + vdev->packet_time = SEC / (gdouble)packet_num; + } else { vdev->packet_time = DSO_PACKET_TIME; } - + vdev->load_data = TRUE; g_timer_start(run_time); sr_session_source_add(-1, 0, 0, receive_data_dso, sdi); - } - else if(sdi->mode == ANALOG) - { + } else if (sdi->mode == ANALOG) { vdev->load_data = TRUE; vdev->packet_len = ANALOG_PACKET_LEN(vdev->samplerate); - if(vdev->packet_len < ANALOG_MIN_PACKET_LEN) - { + if (vdev->packet_len < ANALOG_MIN_PACKET_LEN) { vdev->packet_len = ANALOG_MIN_PACKET_LEN; vdev->packet_time = ANALOG_PACKET_TIME(ANALOG_MIN_PACKET_NUM(vdev->samplerate)); - } - else - { - if (vdev->packet_len % ANALOG_PACKET_ALIGN != 0){ + } else { + if (vdev->packet_len % ANALOG_PACKET_ALIGN != 0) { vdev->packet_len += 1; } vdev->packet_time = ANALOG_PACKET_TIME(ANALOG_PACKET_NUM_PER_SEC); } - if(vdev->sample_generator == PATTERN_RANDOM){ + if (vdev->sample_generator == PATTERN_RANDOM) { init_analog_random_data(vdev); } @@ -1410,17 +1298,16 @@ static int hw_dev_acquisition_start(struct sr_dev_inst *sdi, return SR_OK; } -static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi, void *cb_data) +static int hw_dev_acquisition_stop(const struct sr_dev_inst* sdi, void* cb_data) { (void)cb_data; - struct session_vdev *vdev = sdi->priv; + struct session_vdev* vdev = sdi->priv; struct sr_datafeed_packet packet; packet.status = SR_PKT_OK; - if(sdi->mode != LOGIC) - { + if (sdi->mode != LOGIC) { packet.type = SR_DF_END; ds_data_forward(sdi, &packet); close_archive(vdev); @@ -1429,23 +1316,21 @@ static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi, void *cb_data) return SR_OK; } -static int hw_dev_status_get(const struct sr_dev_inst *sdi, struct sr_status *status, gboolean prg) +static int hw_dev_status_get(const struct sr_dev_inst* sdi, struct sr_status* status, gboolean prg) { (void)prg; - struct session_vdev *vdev; - if (sdi) - { + struct session_vdev* vdev; + if (sdi) { vdev = sdi->priv; *status = vdev->mstatus; return SR_OK; - } - else{ + } else { return SR_ERR; } } -static int receive_data_logic(int fd, int revents, const struct sr_dev_inst *sdi) +static int receive_data_logic(int fd, int revents, const struct sr_dev_inst* sdi) { assert(sdi); assert(sdi->priv); @@ -1453,7 +1338,7 @@ static int receive_data_logic(int fd, int revents, const struct sr_dev_inst *sdi (void)fd; (void)revents; - struct session_vdev *vdev = sdi->priv; + struct session_vdev* vdev = sdi->priv; struct sr_datafeed_packet packet; struct sr_datafeed_logic logic; @@ -1466,28 +1351,24 @@ static int receive_data_logic(int fd, int revents, const struct sr_dev_inst *sdi chan_num = vdev->enabled_probes; - if (chan_num < 1) - { + if (chan_num < 1) { sr_err("%s: channel count < 1.", __func__); return SR_ERR_ARG; } - if (chan_num > SESSION_MAX_CHANNEL_COUNT) - { + if (chan_num > SESSION_MAX_CHANNEL_COUNT) { sr_err("%s: channel count is to big.", __func__); return SR_ERR_ARG; } g_timer_start(packet_interval); - if(!vdev->is_loop) - { - if(vdev->post_data_len >= vdev->total_samples/8){ + if (!vdev->is_loop) { + if (vdev->post_data_len >= vdev->total_samples / 8) { bToEnd = 1; } } - if(!bToEnd) - { + if (!bToEnd) { packet.status = SR_PKT_OK; packet.type = SR_DF_LOGIC; packet.payload = &logic; @@ -1496,57 +1377,48 @@ static int receive_data_logic(int fd, int revents, const struct sr_dev_inst *sdi logic.order = 0; logic.length = chan_num * vdev->packet_len; - if(!vdev->is_loop) - { + if (!vdev->is_loop) { vdev->post_data_len += logic.length / vdev->enabled_probes; - if(vdev->post_data_len >= vdev->total_samples/8){ - get_last_packet_len(&logic,vdev); + if (vdev->post_data_len >= vdev->total_samples / 8) { + get_last_packet_len(&logic, vdev); } } - memset(logic_post_buf,LOGIC_LOW_LEVEL,chan_num * vdev->packet_len); - if(vdev->logic_mem_limit) - { - for(int i = 0; i < vdev->logic_sel_probe_num;i++) - { + memset(logic_post_buf, LOGIC_LOW_LEVEL, chan_num * vdev->packet_len); + if (vdev->logic_mem_limit) { + for (int i = 0; i < vdev->logic_sel_probe_num; i++) { uint8_t probe_index = vdev->logic_sel_probe_list[i]; - for(uint16_t j = 0 ; j< vdev->packet_len/8;j++) - { - uint64_t cur_index = (probe_index*8) + (j*vdev->enabled_probes*8); - memset(logic_post_buf+cur_index,LOGIC_HIGH_LEVEL,8); + for (uint16_t j = 0; j < vdev->packet_len / 8; j++) { + uint64_t cur_index = (probe_index * 8) + (j * vdev->enabled_probes * 8); + memset(logic_post_buf + cur_index, LOGIC_HIGH_LEVEL, 8); } } - } - else - { + } else { uint64_t random = vdev->data_buf_len - logic.length; random = rand() % random; int index = vdev->enabled_probes * 8; - random = floor(random/index)*index; - memcpy(logic_post_buf,vdev->data_buf + random,logic.length); + random = floor(random / index) * index; + memcpy(logic_post_buf, vdev->data_buf + random, logic.length); } logic.data = logic_post_buf; logic_delay_time(vdev); ds_data_forward(sdi, &packet); - if(vdev->logic_mem_limit) - { - uint16_t target_packet = (LOGIC_BLOCK_LEN/vdev->packet_len); - if(vdev->logci_cur_packet_num % target_packet == 0) - { - vdev->logic_sel_probe_num = rand()%vdev->enabled_probes+1; - memset(vdev->logic_sel_probe_list,0,vdev->enabled_probes); - for(int i = 0; i< vdev->logic_sel_probe_num;i++){ - vdev->logic_sel_probe_list[i] = rand()%vdev->enabled_probes; + if (vdev->logic_mem_limit) { + uint16_t target_packet = (LOGIC_BLOCK_LEN / vdev->packet_len); + if (vdev->logci_cur_packet_num % target_packet == 0) { + vdev->logic_sel_probe_num = rand() % vdev->enabled_probes + 1; + memset(vdev->logic_sel_probe_list, 0, vdev->enabled_probes); + for (int i = 0; i < vdev->logic_sel_probe_num; i++) { + vdev->logic_sel_probe_list[i] = rand() % vdev->enabled_probes; } } } vdev->logci_cur_packet_num++; } - if (bToEnd || revents == -1) - { + if (bToEnd || revents == -1) { packet.type = SR_DF_END; ds_data_forward(sdi, &packet); sr_session_source_remove(-1); @@ -1555,53 +1427,50 @@ static int receive_data_logic(int fd, int revents, const struct sr_dev_inst *sdi return TRUE; } -static void free_temp_buffer(struct session_vdev *vdev) -{ - struct session_packet_buffer *pack_buf; +static void free_temp_buffer(struct session_vdev* vdev) +{ + struct session_packet_buffer* pack_buf; int i; assert(vdev); pack_buf = vdev->packet_buffer; - if (pack_buf != NULL) - { + if (pack_buf != NULL) { safe_free(pack_buf->post_buf); - for (i = 0; i < SESSION_MAX_CHANNEL_COUNT; i++){ - if (pack_buf->block_bufs[i] != NULL) - { + for (i = 0; i < SESSION_MAX_CHANNEL_COUNT; i++) { + if (pack_buf->block_bufs[i] != NULL) { g_free(pack_buf->block_bufs[i]); pack_buf->block_bufs[i] = NULL; - } - else{ + } else { break; } } - } + } safe_free(vdev->packet_buffer); safe_free(vdev->data_buf); safe_free(vdev->analog_post_buf); } -static int receive_data_logic_decoder(int fd, int revents, const struct sr_dev_inst *sdi) +static int receive_data_logic_decoder(int fd, int revents, const struct sr_dev_inst* sdi) { - struct session_vdev *vdev = NULL; + struct session_vdev* vdev = NULL; struct sr_datafeed_packet packet; struct sr_datafeed_logic logic; int ret; char file_name[32]; int ch_index, malloc_chan_index; - struct session_packet_buffer *pack_buffer; + struct session_packet_buffer* pack_buffer; unz_file_info64 fileInfo; char szFilePath[15]; int bToEnd; int read_chan_index; int chan_num; - uint8_t *p_wr; - uint8_t *p_rd; + uint8_t* p_wr; + uint8_t* p_rd; uint8_t byte_align; assert(sdi); @@ -1622,13 +1491,11 @@ static int receive_data_logic_decoder(int fd, int revents, const struct sr_dev_i chan_num = vdev->enabled_probes; byte_align = sdi->mode == LOGIC ? 8 : 1; - if (chan_num < 1) - { + if (chan_num < 1) { sr_err("%s: channel count < 1.", __func__); return SR_ERR_ARG; } - if (chan_num > SESSION_MAX_CHANNEL_COUNT) - { + if (chan_num > SESSION_MAX_CHANNEL_COUNT) { sr_err("%s: channel count is to big.", __func__); return SR_ERR_ARG; } @@ -1636,20 +1503,17 @@ static int receive_data_logic_decoder(int fd, int revents, const struct sr_dev_i g_timer_start(packet_interval); // Make buffer - if (vdev->packet_buffer == NULL) - { + if (vdev->packet_buffer == NULL) { vdev->cur_block = 0; vdev->packet_buffer = g_try_malloc0(sizeof(struct session_packet_buffer)); - if (vdev->packet_buffer == NULL) - { + if (vdev->packet_buffer == NULL) { sr_err("%s: vdev->packet_buffer malloc failed", __func__); return SR_ERR_MALLOC; } memset(vdev->packet_buffer, 0, sizeof(struct session_packet_buffer)); - for (ch_index = 0; ch_index <= chan_num; ch_index++) - { + for (ch_index = 0; ch_index <= chan_num; ch_index++) { vdev->packet_buffer->block_bufs[ch_index] = NULL; vdev->packet_buffer->block_read_positions[ch_index] = 0; } @@ -1657,8 +1521,7 @@ static int receive_data_logic_decoder(int fd, int revents, const struct sr_dev_i vdev->packet_buffer->post_buf_len = chan_num * vdev->packet_len; vdev->packet_buffer->post_buf = g_try_malloc0(vdev->packet_buffer->post_buf_len + 1); - if (vdev->packet_buffer->post_buf == NULL) - { + if (vdev->packet_buffer->post_buf == NULL) { sr_err("%s: vdev->packet_buffer->post_buf malloc failed", __func__); return SR_ERR_MALLOC; } @@ -1672,11 +1535,9 @@ static int receive_data_logic_decoder(int fd, int revents, const struct sr_dev_i } pack_buffer = vdev->packet_buffer; - if(chan_num != max_probe_num) - { - for(ch_index = 0 ;ch_index < chan_num; ch_index++) - { - if(pack_buffer->block_bufs[ch_index] != NULL){ + if (chan_num != max_probe_num) { + for (ch_index = 0; ch_index < chan_num; ch_index++) { + if (pack_buffer->block_bufs[ch_index] != NULL) { safe_free(pack_buffer->block_bufs[ch_index]); } pack_buffer->block_bufs[ch_index] = NULL; @@ -1688,14 +1549,12 @@ static int receive_data_logic_decoder(int fd, int revents, const struct sr_dev_i max_probe_num = chan_num; } - if(pack_buffer->post_buf_len != chan_num * vdev->packet_len) - { + if (pack_buffer->post_buf_len != chan_num * vdev->packet_len) { pack_buffer->post_buf_len = chan_num * vdev->packet_len; safe_free(pack_buffer->post_buf); pack_buffer->post_buf = g_try_malloc0(pack_buffer->post_buf_len); - if (pack_buffer->post_buf == NULL) - { + if (pack_buffer->post_buf == NULL) { sr_err("%s: pack_buffer->post_buf malloc failed", __func__); return SR_ERR_MALLOC; } @@ -1706,51 +1565,43 @@ static int receive_data_logic_decoder(int fd, int revents, const struct sr_dev_i // Make packet. read_chan_index = 0; - while (pack_buffer->post_len < pack_buffer->post_buf_len) - { - if (pack_buffer->block_chan_read_pos >= pack_buffer->block_data_len) - { - if(vdev->cur_block >= vdev->num_blocks) - { + while (pack_buffer->post_len < pack_buffer->post_buf_len) { + if (pack_buffer->block_chan_read_pos >= pack_buffer->block_data_len) { + if (vdev->cur_block >= vdev->num_blocks) { bToEnd = 1; break; } - for (ch_index = 0; ch_index < chan_num; ch_index++) - { - snprintf(file_name, sizeof(file_name)-1, "L-%d/%d", ch_index, vdev->cur_block); + for (ch_index = 0; ch_index < chan_num; ch_index++) { + snprintf(file_name, sizeof(file_name) - 1, "L-%d/%d", ch_index, vdev->cur_block); - if (unzLocateFile(vdev->archive, file_name, 0) != UNZ_OK) - { + if (unzLocateFile(vdev->archive, file_name, 0) != UNZ_OK) { sr_err("can't locate zip inner file:\"%s\"", file_name); send_error_packet(sdi, vdev, &packet); return FALSE; } if (unzGetCurrentFileInfo64(vdev->archive, &fileInfo, szFilePath, - sizeof(szFilePath), NULL, 0, NULL, 0) != UNZ_OK) - { + sizeof(szFilePath), NULL, 0, NULL, 0) + != UNZ_OK) { sr_err("%s: unzGetCurrentFileInfo64 error.", __func__); send_error_packet(sdi, vdev, &packet); return FALSE; } - if (ch_index == 0){ + if (ch_index == 0) { pack_buffer->block_data_len = fileInfo.uncompressed_size; - if (pack_buffer->block_data_len > pack_buffer->block_buf_len) - { - for (malloc_chan_index = 0; malloc_chan_index < chan_num; malloc_chan_index++){ + if (pack_buffer->block_data_len > pack_buffer->block_buf_len) { + for (malloc_chan_index = 0; malloc_chan_index < chan_num; malloc_chan_index++) { // Release the old buffer. - if (pack_buffer->block_bufs[malloc_chan_index] != NULL) - { + if (pack_buffer->block_bufs[malloc_chan_index] != NULL) { safe_free(pack_buffer->block_bufs[malloc_chan_index]); pack_buffer->block_bufs[malloc_chan_index] = NULL; } pack_buffer->block_bufs[malloc_chan_index] = g_try_malloc0(pack_buffer->block_data_len + 1); - if (pack_buffer->block_bufs[malloc_chan_index] == NULL) - { + if (pack_buffer->block_bufs[malloc_chan_index] == NULL) { sr_err("%s: block buffer malloc failed", __func__); send_error_packet(sdi, vdev, &packet); return FALSE; @@ -1758,11 +1609,8 @@ static int receive_data_logic_decoder(int fd, int revents, const struct sr_dev_i pack_buffer->block_buf_len = pack_buffer->block_data_len; } } - } - else - { - if (pack_buffer->block_data_len != fileInfo.uncompressed_size) - { + } else { + if (pack_buffer->block_data_len != fileInfo.uncompressed_size) { sr_err("The block size is not coincident:%s", file_name); send_error_packet(sdi, vdev, &packet); return FALSE; @@ -1770,16 +1618,14 @@ static int receive_data_logic_decoder(int fd, int revents, const struct sr_dev_i } // Read the data to buffer. - if (unzOpenCurrentFile(vdev->archive) != UNZ_OK) - { + if (unzOpenCurrentFile(vdev->archive) != UNZ_OK) { sr_err("can't open zip inner file:\"%s\"", file_name); send_error_packet(sdi, vdev, &packet); return FALSE; } ret = unzReadCurrentFile(vdev->archive, pack_buffer->block_bufs[ch_index], pack_buffer->block_data_len); - if (-1 == ret) - { + if (-1 == ret) { sr_err("read zip inner file error:\"%s\"", file_name); send_error_packet(sdi, vdev, &packet); return FALSE; @@ -1799,28 +1645,24 @@ static int receive_data_logic_decoder(int fd, int revents, const struct sr_dev_i pack_buffer->block_read_positions[read_chan_index]++; if (pack_buffer->block_read_positions[read_chan_index] % byte_align == 0 - || pack_buffer->block_read_positions[read_chan_index] == pack_buffer->block_data_len) - { + || pack_buffer->block_read_positions[read_chan_index] == pack_buffer->block_data_len) { read_chan_index++; - if (pack_buffer->block_read_positions[read_chan_index] == pack_buffer->block_data_len) - { - if (vdev->cur_block < vdev->num_blocks){ + if (pack_buffer->block_read_positions[read_chan_index] == pack_buffer->block_data_len) { + if (vdev->cur_block < vdev->num_blocks) { break; } } // Each channel's data is ready. - if (read_chan_index == chan_num) - { + if (read_chan_index == chan_num) { read_chan_index = 0; pack_buffer->block_chan_read_pos += byte_align; } } } - if (pack_buffer->post_len >= (uint64_t)(byte_align * chan_num)) - { + if (pack_buffer->post_len >= (uint64_t)(byte_align * chan_num)) { packet.type = SR_DF_LOGIC; packet.payload = &logic; logic.format = LA_CROSS_DATA; @@ -1831,12 +1673,11 @@ static int receive_data_logic_decoder(int fd, int revents, const struct sr_dev_i delay_time(vdev); ds_data_forward(sdi, &packet); - + pack_buffer->post_len = 0; } - if (bToEnd || revents == -1) - { + if (bToEnd || revents == -1) { packet.type = SR_DF_END; ds_data_forward(sdi, &packet); sr_session_source_remove(-1); @@ -1849,38 +1690,35 @@ static int receive_data_logic_decoder(int fd, int revents, const struct sr_dev_i int get_bit(uint64_t timebase) { - if(timebase < SR_MS(1) && timebase >= SR_US(20)){ - return SR_MS(1)/timebase*2; - } - else if(timebase < SR_MS(200) && timebase >= SR_MS(1)){ - return SR_MS(200)/timebase*2; - } - else if(timebase <= SR_SEC(10) && timebase > SR_MS(200)){ - return SR_SEC(40)/timebase*2; - } - else{ + if (timebase < SR_MS(1) && timebase >= SR_US(20)) { + return SR_MS(1) / timebase * 2; + } else if (timebase < SR_MS(200) && timebase >= SR_MS(1)) { + return SR_MS(200) / timebase * 2; + } else if (timebase <= SR_SEC(10) && timebase > SR_MS(200)) { + return SR_SEC(40) / timebase * 2; + } else { return 200; } } -static int receive_data_dso(int fd, int revents, const struct sr_dev_inst *sdi) +static int receive_data_dso(int fd, int revents, const struct sr_dev_inst* sdi) { - struct session_vdev *vdev = NULL; + struct session_vdev* vdev = NULL; struct sr_datafeed_packet packet; struct sr_datafeed_dso dso; - struct sr_channel *probe; + struct sr_channel* probe; int ret; char file_name[32]; int ch_index, malloc_chan_index; - struct session_packet_buffer *pack_buffer; + struct session_packet_buffer* pack_buffer; unz_file_info64 fileInfo; char szFilePath[15]; int bToEnd; int read_chan_index; uint16_t chan_num; - uint8_t *p_wr; - uint8_t *p_rd; + uint8_t* p_wr; + uint8_t* p_rd; uint8_t byte_align; uint16_t tem; @@ -1902,39 +1740,34 @@ static int receive_data_dso(int fd, int revents, const struct sr_dev_inst *sdi) vdev = sdi->priv; assert(vdev->unit_bits > 0); - if(vdev->sample_generator != PATTERN_RANDOM){ + if (vdev->sample_generator != PATTERN_RANDOM) { assert(vdev->archive); } chan_num = vdev->num_probes; byte_align = sdi->mode == LOGIC ? 8 : 1; - if (chan_num < 1) - { + if (chan_num < 1) { sr_err("%s: channel count < 1.", __func__); return SR_ERR_ARG; } - if (chan_num > SESSION_MAX_CHANNEL_COUNT) - { + if (chan_num > SESSION_MAX_CHANNEL_COUNT) { sr_err("%s: channel count is to big.", __func__); return SR_ERR_ARG; } // Make buffer - if (vdev->packet_buffer == NULL) - { + if (vdev->packet_buffer == NULL) { vdev->cur_block = 0; vdev->packet_buffer = g_try_malloc0(sizeof(struct session_packet_buffer)); - if (vdev->packet_buffer == NULL) - { + if (vdev->packet_buffer == NULL) { sr_err("%s: vdev->packet_buffer malloc failed", __func__); return SR_ERR_MALLOC; } memset(vdev->packet_buffer, 0, sizeof(struct session_packet_buffer)); - for (ch_index = 0; ch_index <= chan_num; ch_index++) - { + for (ch_index = 0; ch_index <= chan_num; ch_index++) { vdev->packet_buffer->block_bufs[ch_index] = NULL; vdev->packet_buffer->block_read_positions[ch_index] = 0; } @@ -1942,8 +1775,7 @@ static int receive_data_dso(int fd, int revents, const struct sr_dev_inst *sdi) vdev->packet_buffer->post_buf_len = chan_num * 10000; vdev->packet_buffer->post_buf = g_try_malloc0(vdev->packet_buffer->post_buf_len); - if (vdev->packet_buffer->post_buf == NULL) - { + if (vdev->packet_buffer->post_buf == NULL) { sr_err("%s: vdev->packet_buffer->post_buf malloc failed", __func__); return SR_ERR_MALLOC; } @@ -1956,14 +1788,12 @@ static int receive_data_dso(int fd, int revents, const struct sr_dev_inst *sdi) pack_buffer = vdev->packet_buffer; - if(pack_buffer->post_buf_len != chan_num * 10000) - { + if (pack_buffer->post_buf_len != chan_num * 10000) { vdev->packet_buffer->post_buf_len = chan_num * 10000; safe_free(pack_buffer->post_buf); pack_buffer->post_buf = g_try_malloc0(pack_buffer->post_buf_len); - if (pack_buffer->post_buf == NULL) - { + if (pack_buffer->post_buf == NULL) { sr_err("%s: pack_buffer->post_buf malloc failed", __func__); return SR_ERR_MALLOC; } @@ -1971,11 +1801,9 @@ static int receive_data_dso(int fd, int revents, const struct sr_dev_inst *sdi) pack_buffer->post_len = 0; } - if(chan_num != max_probe_num) - { - for(ch_index = 0 ;ch_index < chan_num; ch_index++) - { - if(pack_buffer->block_bufs[ch_index] != NULL){ + if (chan_num != max_probe_num) { + for (ch_index = 0; ch_index < chan_num; ch_index++) { + if (pack_buffer->block_bufs[ch_index] != NULL) { safe_free(pack_buffer->block_bufs[ch_index]); } pack_buffer->block_bufs[ch_index] = NULL; @@ -1990,69 +1818,55 @@ static int receive_data_dso(int fd, int revents, const struct sr_dev_inst *sdi) // Make packet. read_chan_index = 0; - if(vdev->vdiv_change || vdev->timebase_change ||vdev->offset_change) - { - if(vdev->sample_generator == PATTERN_RANDOM) - { - for(uint64_t i = 0 ; i < pack_buffer->post_buf_len ;i++) - { - if(i % 2 == 0){ + if (vdev->vdiv_change || vdev->timebase_change || vdev->offset_change) { + if (vdev->sample_generator == PATTERN_RANDOM) { + for (uint64_t i = 0; i < pack_buffer->post_buf_len; i++) { + if (i % 2 == 0) { *(uint8_t*)(pack_buffer->post_buf + i) = DSO_RANDOM_DATA; - } - else{ - *(uint8_t*)(pack_buffer->post_buf + i) = *(uint8_t*)(pack_buffer->post_buf + i -1); + } else { + *(uint8_t*)(pack_buffer->post_buf + i) = *(uint8_t*)(pack_buffer->post_buf + i - 1); } } pack_buffer->post_len = pack_buffer->post_buf_len; - } - else - { - if(vdev->load_data) - { + } else { + if (vdev->load_data) { pack_buffer->post_len = 0; - while (pack_buffer->post_len < pack_buffer->post_buf_len) - { - if (pack_buffer->block_chan_read_pos >= pack_buffer->block_data_len) - { - if (vdev->cur_block >= vdev->num_blocks){ + while (pack_buffer->post_len < pack_buffer->post_buf_len) { + if (pack_buffer->block_chan_read_pos >= pack_buffer->block_data_len) { + if (vdev->cur_block >= vdev->num_blocks) { vdev->cur_block = 0; } - for (ch_index = 0; ch_index < chan_num; ch_index++) - { - snprintf(file_name, sizeof(file_name)-1, "O-%d/0", ch_index); + for (ch_index = 0; ch_index < chan_num; ch_index++) { + snprintf(file_name, sizeof(file_name) - 1, "O-%d/0", ch_index); - if (unzLocateFile(vdev->archive, file_name, 0) != UNZ_OK) - { + if (unzLocateFile(vdev->archive, file_name, 0) != UNZ_OK) { sr_err("cant't locate zip inner file:\"%s\"", file_name); send_error_packet(sdi, vdev, &packet); return FALSE; } - + if (unzGetCurrentFileInfo64(vdev->archive, &fileInfo, szFilePath, - sizeof(szFilePath), NULL, 0, NULL, 0) != UNZ_OK) - { + sizeof(szFilePath), NULL, 0, NULL, 0) + != UNZ_OK) { sr_err("%s: unzGetCurrentFileInfo64 error.", __func__); send_error_packet(sdi, vdev, &packet); return FALSE; } - if (ch_index == 0){ + if (ch_index == 0) { pack_buffer->block_data_len = fileInfo.uncompressed_size; - if (pack_buffer->block_data_len > pack_buffer->block_buf_len) - { - for (malloc_chan_index = 0; malloc_chan_index < chan_num; malloc_chan_index++){ + if (pack_buffer->block_data_len > pack_buffer->block_buf_len) { + for (malloc_chan_index = 0; malloc_chan_index < chan_num; malloc_chan_index++) { // Release the old buffer. - if (pack_buffer->block_bufs[malloc_chan_index] != NULL) - { + if (pack_buffer->block_bufs[malloc_chan_index] != NULL) { safe_free(pack_buffer->block_bufs[malloc_chan_index]); pack_buffer->block_bufs[malloc_chan_index] = NULL; } pack_buffer->block_bufs[malloc_chan_index] = g_try_malloc0(pack_buffer->block_data_len + 1); - if (pack_buffer->block_bufs[malloc_chan_index] == NULL) - { + if (pack_buffer->block_bufs[malloc_chan_index] == NULL) { sr_err("%s: block buffer malloc failed", __func__); send_error_packet(sdi, vdev, &packet); return FALSE; @@ -2060,11 +1874,8 @@ static int receive_data_dso(int fd, int revents, const struct sr_dev_inst *sdi) pack_buffer->block_buf_len = pack_buffer->block_data_len; } } - } - else - { - if (pack_buffer->block_data_len != fileInfo.uncompressed_size) - { + } else { + if (pack_buffer->block_data_len != fileInfo.uncompressed_size) { sr_err("The block size is not coincident:%s", file_name); send_error_packet(sdi, vdev, &packet); return FALSE; @@ -2072,16 +1883,14 @@ static int receive_data_dso(int fd, int revents, const struct sr_dev_inst *sdi) } // Read the data to buffer. - if (unzOpenCurrentFile(vdev->archive) != UNZ_OK) - { + if (unzOpenCurrentFile(vdev->archive) != UNZ_OK) { sr_err("cant't open zip inner file:\"%s\"", file_name); send_error_packet(sdi, vdev, &packet); return FALSE; } ret = unzReadCurrentFile(vdev->archive, pack_buffer->block_bufs[ch_index], pack_buffer->block_data_len); - if (-1 == ret) - { + if (-1 == ret) { sr_err("read zip inner file error:\"%s\"", file_name); send_error_packet(sdi, vdev, &packet); return FALSE; @@ -2101,106 +1910,87 @@ static int receive_data_dso(int fd, int revents, const struct sr_dev_inst *sdi) pack_buffer->block_read_positions[read_chan_index]++; if (pack_buffer->block_read_positions[read_chan_index] % byte_align == 0 - || pack_buffer->block_read_positions[read_chan_index] == pack_buffer->block_data_len) - { + || pack_buffer->block_read_positions[read_chan_index] == pack_buffer->block_data_len) { read_chan_index++; - if (pack_buffer->block_read_positions[read_chan_index] == pack_buffer->block_data_len){ - if (vdev->cur_block < vdev->num_blocks){ + if (pack_buffer->block_read_positions[read_chan_index] == pack_buffer->block_data_len) { + if (vdev->cur_block < vdev->num_blocks) { break; } } // Each channel's data is ready. - if (read_chan_index == chan_num) - { + if (read_chan_index == chan_num) { read_chan_index = 0; pack_buffer->block_chan_read_pos += byte_align; } } } - memcpy(vdev->data_buf,pack_buffer->post_buf,DSO_PACKET_LEN); + memcpy(vdev->data_buf, pack_buffer->post_buf, DSO_PACKET_LEN); vdev->load_data = FALSE; - } - else{ - memcpy(pack_buffer->post_buf,vdev->data_buf,DSO_PACKET_LEN); + } else { + memcpy(pack_buffer->post_buf, vdev->data_buf, DSO_PACKET_LEN); pack_buffer->post_buf_len = DSO_PACKET_LEN; } - } - if(vdev->timebase_change || vdev->vdiv_change || vdev->offset_change) - { + if (vdev->timebase_change || vdev->vdiv_change || vdev->offset_change) { dso_wavelength_updata(vdev); - uint16_t high_gate,low_gate; - for(uint64_t i = 0 ; i < pack_buffer->post_buf_len; i++) - { - if(i % 2 == 0){ + uint16_t high_gate, low_gate; + for (uint64_t i = 0; i < pack_buffer->post_buf_len; i++) { + if (i % 2 == 0) { probe = g_slist_nth(sdi->channels, 0)->data; - } - else{ + } else { probe = g_slist_nth(sdi->channels, 1)->data; } - - if(!probe->enabled){ - if(i % 2 == 0){ + + if (!probe->enabled) { + if (i % 2 == 0) { probe = g_slist_nth(sdi->channels, 1)->data; - } - else{ + } else { probe = g_slist_nth(sdi->channels, 0)->data; - } + } } vdiv = probe->vdiv; uint8_t temp_val = *((uint8_t*)pack_buffer->post_buf + i); - if(vdiv > SR_mV(200)) - { - if(temp_val > DSO_MID_VAL) - { + if (vdiv > SR_mV(200)) { + if (temp_val > DSO_MID_VAL) { val = temp_val - DSO_MID_VAL; tem = val * DSO_DEFAULT_VDIV / vdiv; temp_val = DSO_MID_VAL + tem; - } - else if(temp_val < DSO_MID_VAL) - { + } else if (temp_val < DSO_MID_VAL) { val = DSO_MID_VAL - temp_val; - tem = val * DSO_DEFAULT_VDIV / vdiv; + tem = val * DSO_DEFAULT_VDIV / vdiv; temp_val = DSO_MID_VAL - tem; } *((uint8_t*)pack_buffer->post_buf + i) = temp_val; - } - else{ - if(temp_val > DSO_MID_VAL) - { + } else { + if (temp_val > DSO_MID_VAL) { val = temp_val - DSO_MID_VAL; - tem = (uint16_t)val * (uint16_t)DSO_DEFAULT_VDIV/(uint16_t)vdiv; - tem = DSO_EXPAND_MID_VAL(SR_mV(200)/vdiv) + tem; - } - else if(temp_val < DSO_MID_VAL) - { + tem = (uint16_t)val * (uint16_t)DSO_DEFAULT_VDIV / (uint16_t)vdiv; + tem = DSO_EXPAND_MID_VAL(SR_mV(200) / vdiv) + tem; + } else if (temp_val < DSO_MID_VAL) { val = DSO_MID_VAL - temp_val; - tem = (uint16_t)val * (uint16_t)DSO_DEFAULT_VDIV/(uint16_t)vdiv; - tem = DSO_EXPAND_MID_VAL(SR_mV(200)/vdiv) - tem; - } - else{ - tem = DSO_EXPAND_MID_VAL(SR_mV(200)/vdiv); + tem = (uint16_t)val * (uint16_t)DSO_DEFAULT_VDIV / (uint16_t)vdiv; + tem = DSO_EXPAND_MID_VAL(SR_mV(200) / vdiv) - tem; + } else { + tem = DSO_EXPAND_MID_VAL(SR_mV(200) / vdiv); } - high_gate = DSO_EXPAND_MID_VAL(SR_mV(200)/vdiv); + high_gate = DSO_EXPAND_MID_VAL(SR_mV(200) / vdiv); high_gate -= probe->offset; low_gate = high_gate + DSO_LIMIT; - if(tem <=high_gate){ + if (tem <= high_gate) { tem = DSO_MAX_VAL; - } - else if(tem >=low_gate){ + } else if (tem >= low_gate) { tem = DSO_MIN_VAL; + } else { + tem -= high_gate; } - else{ - tem-= high_gate; - } - + *((uint8_t*)pack_buffer->post_buf + i) = (uint8_t)tem; } } @@ -2211,65 +2001,51 @@ static int receive_data_dso(int fd, int revents, const struct sr_dev_inst *sdi) } } - gdouble total_time = vdev->timebase /(gdouble)SR_SEC(1)*(gdouble)10; + gdouble total_time = vdev->timebase / (gdouble)SR_SEC(1) * (gdouble)10; gdouble total_time_elapsed = g_timer_elapsed(run_time, NULL); - if(!vdev->instant) - { - if (total_time_elapsed < total_time) - { + if (!vdev->instant) { + if (total_time_elapsed < total_time) { gdouble percent = total_time_elapsed / total_time; - int buf_len = percent* DSO_PACKET_LEN; - if(buf_len %2 != 0){ - buf_len +=1; + int buf_len = percent * DSO_PACKET_LEN; + if (buf_len % 2 != 0) { + buf_len += 1; } pack_buffer->post_len = buf_len; - } - else - { + } else { uint8_t top0; uint8_t top1; - if(vdev->sample_generator == PATTERN_RANDOM) - { - top0 = *((uint8_t*)pack_buffer->post_buf + pack_buffer->post_buf_len -2); - top1 = *((uint8_t*)pack_buffer->post_buf + pack_buffer->post_buf_len -1); - } - else - { - top0 = *((uint8_t*)pack_buffer->post_buf + get_bit(vdev->timebase) -2); - top1 = *((uint8_t*)pack_buffer->post_buf + get_bit(vdev->timebase) -1); + if (vdev->sample_generator == PATTERN_RANDOM) { + top0 = *((uint8_t*)pack_buffer->post_buf + pack_buffer->post_buf_len - 2); + top1 = *((uint8_t*)pack_buffer->post_buf + pack_buffer->post_buf_len - 1); + } else { + top0 = *((uint8_t*)pack_buffer->post_buf + get_bit(vdev->timebase) - 2); + top1 = *((uint8_t*)pack_buffer->post_buf + get_bit(vdev->timebase) - 1); } - for(int i = pack_buffer->post_len -1; i > 1; i -= 2){ + for (int i = pack_buffer->post_len - 1; i > 1; i -= 2) { *((uint8_t*)pack_buffer->post_buf + i) = *((uint8_t*)pack_buffer->post_buf + i - 2); } - for(int i = pack_buffer->post_len -2; i > 0; i -= 2){ + for (int i = pack_buffer->post_len - 2; i > 0; i -= 2) { *((uint8_t*)pack_buffer->post_buf + i) = *((uint8_t*)pack_buffer->post_buf + i - 2); } *(uint8_t*)pack_buffer->post_buf = top0; - *((uint8_t*)pack_buffer->post_buf + 1)= top1; + *((uint8_t*)pack_buffer->post_buf + 1) = top1; pack_buffer->post_len = DSO_PACKET_LEN; - } - } - else - { - if(DSO_PACKET_LEN >vdev->post_data_len) - { + } + } else { + if (DSO_PACKET_LEN > vdev->post_data_len) { pack_buffer->post_len = vdev->packet_len; vdev->post_data_len += vdev->packet_len; - } - else - { + } else { bToEnd = 1; vdev->instant = FALSE; } } - - if (pack_buffer->post_len >= byte_align * chan_num && !bToEnd) - { + if (pack_buffer->post_len >= byte_align * chan_num && !bToEnd) { packet.type = SR_DF_DSO; packet.payload = &dso; dso.probes = sdi->channels; @@ -2277,28 +2053,23 @@ static int receive_data_dso(int fd, int revents, const struct sr_dev_inst *sdi) dso.unit = SR_UNIT_VOLT; dso.mqflags = SR_MQFLAG_AC; dso.num_samples = pack_buffer->post_len / chan_num; - if (vdev->instant){ - dso.data = pack_buffer->post_buf+vdev->post_data_len; - } - else{ + if (vdev->instant) { + dso.data = pack_buffer->post_buf + vdev->post_data_len; + } else { dso.data = pack_buffer->post_buf; } ds_data_forward(sdi, &packet); - if (vdev->instant){ + if (vdev->instant) { instant_delay_time(vdev); - } - else - { + } else { delay_time(vdev); g_timer_start(packet_interval); } - dso_status_update(vdev); } - if (bToEnd || revents == -1) - { + if (bToEnd || revents == -1) { packet.type = SR_DF_END; ds_data_forward(sdi, &packet); sr_session_source_remove(-1); @@ -2307,13 +2078,12 @@ static int receive_data_dso(int fd, int revents, const struct sr_dev_inst *sdi) return TRUE; } - -static int receive_data_analog(int fd, int revents, const struct sr_dev_inst *sdi) +static int receive_data_analog(int fd, int revents, const struct sr_dev_inst* sdi) { - struct session_vdev *vdev = sdi->priv; + struct session_vdev* vdev = sdi->priv; struct sr_datafeed_packet packet; struct sr_datafeed_analog analog; - struct sr_channel *probe = NULL; + struct sr_channel* probe = NULL; char file_name[32]; int ret; @@ -2332,53 +2102,45 @@ static int receive_data_analog(int fd, int revents, const struct sr_dev_inst *sd g_timer_start(packet_interval); - if(vdev->load_data) - { - if(vdev->sample_generator != PATTERN_RANDOM) - { + if (vdev->load_data) { + if (vdev->sample_generator != PATTERN_RANDOM) { vdev->data_buf_len = 0; void* analog_data = g_try_malloc0(ANALOG_DATA_LEN_PER_CYCLE); - if(analog_data == NULL) - { - sr_err("%s:analog_data malloc failed",__func__); + if (analog_data == NULL) { + sr_err("%s:analog_data malloc failed", __func__); return SR_ERR_MALLOC; } - snprintf(file_name, sizeof(file_name)-1, "%s-%d/%d", "A", - 0, 0); + snprintf(file_name, sizeof(file_name) - 1, "%s-%d/%d", "A", + 0, 0); - if (unzLocateFile(vdev->archive, file_name, 0) != UNZ_OK) - { + if (unzLocateFile(vdev->archive, file_name, 0) != UNZ_OK) { sr_err("can't locate zip inner file:\"%s\"", file_name); send_error_packet(sdi, vdev, &packet); return FALSE; } - if (unzOpenCurrentFile(vdev->archive) != UNZ_OK) - { + if (unzOpenCurrentFile(vdev->archive) != UNZ_OK) { sr_err("can't open zip inner file:\"%s\"", file_name); send_error_packet(sdi, vdev, &packet); return FALSE; } ret = unzReadCurrentFile(vdev->archive, analog_data, ANALOG_DATA_LEN_PER_CYCLE); - if (-1 == ret) - { + if (-1 == ret) { sr_err("read zip inner file error:\"%s\"", file_name); send_error_packet(sdi, vdev, &packet); return FALSE; } uint64_t total_buf_len = ANALOG_CYCLE_RATIO * vdev->total_samples * ANALOG_PROBE_NUM; - if(total_buf_len % ANALOG_DATA_LEN_PER_CYCLE != 0){ + if (total_buf_len % ANALOG_DATA_LEN_PER_CYCLE != 0) { total_buf_len = total_buf_len / ANALOG_DATA_LEN_PER_CYCLE * ANALOG_DATA_LEN_PER_CYCLE; } - safe_free(vdev->data_buf); - vdev->data_buf = g_try_malloc0(total_buf_len); - if (vdev->data_buf == NULL) - { + vdev->data_buf = g_try_malloc0(total_buf_len); + if (vdev->data_buf == NULL) { sr_err("%s: vdev->data_buf malloc failed", __func__); return SR_ERR_MALLOC; } @@ -2395,50 +2157,42 @@ static int receive_data_analog(int fd, int revents, const struct sr_dev_inst *sd uint16_t tem; uint64_t cur_l = 0; - for(int i = 0 ; i < ANALOG_DATA_LEN_PER_CYCLE;i++) - { - if(i % 2 == 0){ + for (int i = 0; i < ANALOG_DATA_LEN_PER_CYCLE; i++) { + if (i % 2 == 0) { vdiv = p0_vdiv; - } - else{ + } else { vdiv = p1_vdiv; } tem = 0; uint8_t temp_value = *((uint8_t*)analog_data + i); - if(temp_value > ANALOG_MID_VAL){ + if (temp_value > ANALOG_MID_VAL) { val = temp_value - ANALOG_MID_VAL; tem = val * ANALOG_DEFAULT_VDIV / vdiv; - if(tem >= ANALOG_MID_VAL){ + if (tem >= ANALOG_MID_VAL) { temp_value = ANALOG_MIN_VAL; - } - else{ + } else { temp_value = ANALOG_MID_VAL + tem; } - } - else if(temp_value < ANALOG_MID_VAL) - { + } else if (temp_value < ANALOG_MID_VAL) { val = ANALOG_MID_VAL - temp_value; - tem = val * ANALOG_DEFAULT_VDIV / vdiv; + tem = val * ANALOG_DEFAULT_VDIV / vdiv; - if(tem >= ANALOG_MID_VAL){ + if (tem >= ANALOG_MID_VAL) { temp_value = ANALOG_MAX_VAL; - } - else{ + } else { temp_value = ANALOG_MID_VAL - tem; } } - for(uint64_t j = 0 ; j data_buf + cur_l,temp_value,1); + memset(vdev->data_buf + cur_l, temp_value, 1); } } safe_free(analog_data); @@ -2446,29 +2200,24 @@ static int receive_data_analog(int fd, int revents, const struct sr_dev_inst *sd vdev->load_data = FALSE; } - if(vdev->analog_post_buf_len != vdev->packet_len) - { + if (vdev->analog_post_buf_len != vdev->packet_len) { safe_free(vdev->analog_post_buf); vdev->analog_post_buf = g_try_malloc0(vdev->packet_len); - if(vdev->analog_post_buf == NULL) - { + if (vdev->analog_post_buf == NULL) { sr_err("%s: buf malloc failed", __func__); return SR_ERR_MALLOC; } vdev->analog_post_buf_len = vdev->packet_len; } - if(vdev->analog_read_pos + vdev->packet_len >= vdev->data_buf_len - 1 ) - { + if (vdev->analog_read_pos + vdev->packet_len >= vdev->data_buf_len - 1) { uint64_t back_len = vdev->data_buf_len - vdev->analog_read_pos; uint64_t front_len = vdev->packet_len - back_len; - memcpy(vdev->analog_post_buf , vdev->data_buf + vdev->analog_read_pos , back_len); - memcpy(vdev->analog_post_buf+ back_len , vdev->data_buf, front_len); + memcpy(vdev->analog_post_buf, vdev->data_buf + vdev->analog_read_pos, back_len); + memcpy(vdev->analog_post_buf + back_len, vdev->data_buf, front_len); vdev->analog_read_pos = front_len; - } - else - { - memcpy(vdev->analog_post_buf,vdev->data_buf + vdev->analog_read_pos,vdev->packet_len); + } else { + memcpy(vdev->analog_post_buf, vdev->data_buf + vdev->analog_read_pos, vdev->packet_len); vdev->analog_read_pos += vdev->packet_len; } @@ -2484,12 +2233,11 @@ static int receive_data_analog(int fd, int revents, const struct sr_dev_inst *sd delay_time(vdev); ds_data_forward(sdi, &packet); - return TRUE; } -static void send_error_packet(const struct sr_dev_inst *cb_sdi, struct session_vdev *vdev, struct sr_datafeed_packet *packet) +static void send_error_packet(const struct sr_dev_inst* cb_sdi, struct session_vdev* vdev, struct sr_datafeed_packet* packet) { packet->type = SR_DF_END; packet->status = SR_PKT_SOURCE_ERROR; @@ -2498,20 +2246,18 @@ static void send_error_packet(const struct sr_dev_inst *cb_sdi, struct session_v close_archive(vdev); } -static int close_archive(struct session_vdev *vdev) +static int close_archive(struct session_vdev* vdev) { - if(vdev->sample_generator != PATTERN_RANDOM) - { + if (vdev->sample_generator != PATTERN_RANDOM) { assert(vdev->archive); - if (vdev->capfile) - { + if (vdev->capfile) { unzCloseCurrentFile(vdev->archive); vdev->capfile = 0; } int ret = unzClose(vdev->archive); - if (ret != UNZ_OK){ + if (ret != UNZ_OK) { sr_err("close zip archive error!"); } @@ -2521,58 +2267,52 @@ static int close_archive(struct session_vdev *vdev) return SR_OK; } -static int load_virtual_device_session(struct sr_dev_inst *sdi) +static int load_virtual_device_session(struct sr_dev_inst* sdi) { - GKeyFile *kf; + GKeyFile* kf; unzFile archive = NULL; char szFilePath[15]; unz_file_info64 fileInfo; - struct sr_channel *probe; + struct sr_channel* probe; int i, j; uint64_t tmp_u64; char **sections, **keys, *metafile, *val; int channel_type = SR_CHANNEL_LOGIC; - struct session_vdev * vdev = sdi->priv; + struct session_vdev* vdev = sdi->priv; assert(sdi); - if (vdev->sample_generator != PATTERN_RANDOM){ + if (vdev->sample_generator != PATTERN_RANDOM) { assert(sdi->path); } - - switch (sdi->mode) - { + + switch (sdi->mode) { case LOGIC: - if(vdev->sample_generator != PATTERN_RANDOM) - { + if (vdev->sample_generator != PATTERN_RANDOM) { archive = unzOpen64(sdi->path); - if (NULL == archive) - { + if (NULL == archive) { sr_err("%s: Load file error:\"%s\"", __func__, sdi->path); return SR_ERR; } - if (unzLocateFile(archive, "header", 0) != UNZ_OK) - { + if (unzLocateFile(archive, "header", 0) != UNZ_OK) { unzClose(archive); sr_err("%s: unzLocateFile error.", __func__); return SR_ERR; } if (unzGetCurrentFileInfo64(archive, &fileInfo, szFilePath, - sizeof(szFilePath), NULL, 0, NULL, 0) != UNZ_OK) - { + sizeof(szFilePath), NULL, 0, NULL, 0) + != UNZ_OK) { unzClose(archive); sr_err("%s: unzGetCurrentFileInfo64 error.", __func__); return SR_ERR; } - if (unzOpenCurrentFile(archive) != UNZ_OK) - { + if (unzOpenCurrentFile(archive) != UNZ_OK) { sr_err("%s: Cant't open zip inner file.", __func__); unzClose(archive); return SR_ERR; } - if (!(metafile = g_try_malloc0(fileInfo.uncompressed_size))) - { + if (!(metafile = g_try_malloc0(fileInfo.uncompressed_size))) { sr_err("%s: metafile malloc failed", __func__); return SR_ERR_MALLOC; } @@ -2580,61 +2320,46 @@ static int load_virtual_device_session(struct sr_dev_inst *sdi) unzReadCurrentFile(archive, metafile, fileInfo.uncompressed_size); unzCloseCurrentFile(archive); - if (unzClose(archive) != UNZ_OK) - { + if (unzClose(archive) != UNZ_OK) { sr_err("%s: Close zip archive error.", __func__); return SR_ERR; } archive = NULL; kf = g_key_file_new(); - if (!g_key_file_load_from_data(kf, metafile, fileInfo.uncompressed_size, 0, NULL)) - { + if (!g_key_file_load_from_data(kf, metafile, fileInfo.uncompressed_size, 0, NULL)) { sr_err("Failed to parse metadata."); return SR_ERR; } sections = g_key_file_get_groups(kf, NULL); - for (i = 0; sections[i]; i++) - { - if (!strncmp(sections[i], "header", 6)) - { + for (i = 0; sections[i]; i++) { + if (!strncmp(sections[i], "header", 6)) { keys = g_key_file_get_keys(kf, sections[i], NULL, NULL); - for (j = 0; keys[j]; j++) - { + for (j = 0; keys[j]; j++) { val = g_key_file_get_string(kf, sections[i], keys[j], NULL); - if (!strcmp(keys[j], "samplerate")) - { + if (!strcmp(keys[j], "samplerate")) { sr_parse_sizestring(val, &tmp_u64); vdev->samplerate = tmp_u64; samplerates_file[0] = vdev->samplerate; - } - else if (!strcmp(keys[j], "total samples")) - { + } else if (!strcmp(keys[j], "total samples")) { tmp_u64 = strtoull(val, NULL, 10); vdev->total_samples = tmp_u64; samplecounts_file[0] = vdev->total_samples; - } - else if (!strcmp(keys[j], "total blocks")) - { + } else if (!strcmp(keys[j], "total blocks")) { tmp_u64 = strtoull(val, NULL, 10); vdev->num_blocks = tmp_u64; - } - else if (!strcmp(keys[j], "total probes")) - { + } else if (!strcmp(keys[j], "total probes")) { sr_dev_probes_free(sdi); tmp_u64 = strtoull(val, NULL, 10); vdev->num_probes = tmp_u64; - } - else if (!strncmp(keys[j], "probe", 5)) - { + } else if (!strncmp(keys[j], "probe", 5)) { tmp_u64 = strtoul(keys[j] + 5, NULL, 10); channel_type = SR_CHANNEL_LOGIC; - if (!(probe = sr_channel_new(tmp_u64, channel_type, TRUE, val))) - { + if (!(probe = sr_channel_new(tmp_u64, channel_type, TRUE, val))) { sr_err("%s: create channel failed", __func__); sr_dev_inst_free(sdi); return SR_ERR; @@ -2650,18 +2375,14 @@ static int load_virtual_device_session(struct sr_dev_inst *sdi) g_strfreev(sections); g_key_file_free(kf); safe_free(metafile); - } - else - { + } else { vdev->samplerate = LOGIC_DEFAULT_SAMPLERATE; vdev->total_samples = LOGIC_DEFAULT_TOTAL_SAMPLES; vdev->num_probes = logic_channel_modes[vdev->logic_ch_mode_index].num; sr_dev_probes_free(sdi); - for (int i = 0; i < vdev->num_probes; i++) - { - if (!(probe = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE, probe_names[i]))) - { + for (int i = 0; i < vdev->num_probes; i++) { + if (!(probe = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE, probe_names[i]))) { sr_err("%s: create channel failed", __func__); sr_dev_inst_free(sdi); return SR_ERR; @@ -2678,10 +2399,8 @@ static int load_virtual_device_session(struct sr_dev_inst *sdi) vdev->num_blocks = DSO_DEFAULT_NUM_BLOCK; sr_dev_probes_free(sdi); - for (int i = 0; i < DSO_DEFAULT_NUM_PROBE; i++) - { - if (!(probe = sr_channel_new(i, SR_CHANNEL_DSO, TRUE, probe_names[i]))) - { + for (int i = 0; i < DSO_DEFAULT_NUM_PROBE; i++) { + if (!(probe = sr_channel_new(i, SR_CHANNEL_DSO, TRUE, probe_names[i]))) { sr_err("%s: create channel failed", __func__); sr_dev_inst_free(sdi); return SR_ERR; @@ -2703,11 +2422,9 @@ static int load_virtual_device_session(struct sr_dev_inst *sdi) vdev->num_probes = ANALOG_DEFAULT_NUM_PROBE; vdev->num_blocks = ANALOG_DEFAULT_NUM_BLOCK; sr_dev_probes_free(sdi); - - for (int i = 0; i < ANALOG_DEFAULT_NUM_PROBE; i++) - { - if (!(probe = sr_channel_new(i, SR_CHANNEL_ANALOG, TRUE, probe_names[i]))) - { + + for (int i = 0; i < ANALOG_DEFAULT_NUM_PROBE; i++) { + if (!(probe = sr_channel_new(i, SR_CHANNEL_ANALOG, TRUE, probe_names[i]))) { sr_err("%s: create channel failed", __func__); sr_dev_inst_free(sdi); return SR_ERR; @@ -2722,7 +2439,7 @@ static int load_virtual_device_session(struct sr_dev_inst *sdi) probe->trig_value = ANALOG_DEFAULT_TRIG_VAL; probe->map_default = ANALOG_DEFAULT_MAP_DEFAULT; probe->map_unit = ANALOG_DEFAULT_MAP_UNIT; - probe->map_min = ANALOG_DEFAULT_MAP_MIN; + probe->map_min = ANALOG_DEFAULT_MAP_MIN; probe->map_max = ANALOG_DEFAULT_MAP_MAX; sdi->channels = g_slist_append(sdi->channels, probe); @@ -2735,72 +2452,58 @@ static int load_virtual_device_session(struct sr_dev_inst *sdi) return SR_OK; } -int dso_wavelength_updata(struct session_vdev *vdev) +int dso_wavelength_updata(struct session_vdev* vdev) { int index; - int bit = get_bit(vdev->timebase); - struct session_packet_buffer *pack_buffer= vdev->packet_buffer; + int bit = get_bit(vdev->timebase); + struct session_packet_buffer* pack_buffer = vdev->packet_buffer; uint8_t tmp_val; int l = 0; char* pattern_mode = demo_pattern_array[DSO].patterns[vdev->sample_generator]; const uint8_t wave_max_val = 78; - if(vdev->sample_generator!= PATTERN_RANDOM) - { - if(strcmp(pattern_mode,"square")) - { - for(int i = 0 ; i < DSO_PACKET_LEN/2 ; i += 2) - { + if (vdev->sample_generator != PATTERN_RANDOM) { + if (strcmp(pattern_mode, "square")) { + for (int i = 0; i < DSO_PACKET_LEN / 2; i += 2) { tmp_val = *((uint8_t*)pack_buffer->post_buf + i); - if(tmp_val == wave_max_val) - { + if (tmp_val == wave_max_val) { l = i; break; } } } - void* tmp_buf = g_try_malloc0(bit); - if(tmp_buf == NULL) - { + if (tmp_buf == NULL) { sr_err("%s: tmp_buf malloc failed", __func__); return SR_ERR_MALLOC; } - for(int i = 0 ; i < bit ; i++) - { - if(i%2 == 0) - { - if(!strcmp(pattern_mode,"sawtooth") && i == bit - 2){ - index = DSO_WAVE_PERIOD_LEN-2; - } - else if(strcmp(pattern_mode,"sawtooth") && bit == 10){ + for (int i = 0; i < bit; i++) { + if (i % 2 == 0) { + if (!strcmp(pattern_mode, "sawtooth") && i == bit - 2) { + index = DSO_WAVE_PERIOD_LEN - 2; + } else if (strcmp(pattern_mode, "sawtooth") && bit == 10) { index = i * 16; - } - else{ + } else { index = i * DSO_WAVE_PERIOD_LEN_PER_PROBE / (bit / 2); } - } - else - { - if(!strcmp(pattern_mode,"sawtooth") && i == bit - 1){ - index = DSO_WAVE_PERIOD_LEN-1; - } - else if(strcmp(pattern_mode,"sawtooth") && bit == 10){ - index = (i-1) * 16 + 1; - } - else{ - index = (i-1) * DSO_WAVE_PERIOD_LEN_PER_PROBE / (bit / 2) + 1; + } else { + if (!strcmp(pattern_mode, "sawtooth") && i == bit - 1) { + index = DSO_WAVE_PERIOD_LEN - 1; + } else if (strcmp(pattern_mode, "sawtooth") && bit == 10) { + index = (i - 1) * 16 + 1; + } else { + index = (i - 1) * DSO_WAVE_PERIOD_LEN_PER_PROBE / (bit / 2) + 1; } } - - *((uint8_t*)tmp_buf+ i) = *((uint8_t*)pack_buffer->post_buf + index + l); + + *((uint8_t*)tmp_buf + i) = *((uint8_t*)pack_buffer->post_buf + index + l); } - for(int i = 0 ; i < DSO_PACKET_LEN/bit ; i++){ - memcpy(pack_buffer->post_buf+i*bit,tmp_buf,bit); + for (int i = 0; i < DSO_PACKET_LEN / bit; i++) { + memcpy(pack_buffer->post_buf + i * bit, tmp_buf, bit); } safe_free(tmp_buf); diff --git a/libsigrok4DSL/output/csv.c b/libsigrok4DSL/output/csv.c index 0bff430b..993c0fc5 100644 --- a/libsigrok4DSL/output/csv.c +++ b/libsigrok4DSL/output/csv.c @@ -18,31 +18,30 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ - -#include "../libsigrok-internal.h" -#include -#include -#include #include "../config.h" /* Needed for PACKAGE_STRING and others. */ +#include "../libsigrok-internal.h" #include "../log.h" -#include #include +#include +#include +#include +#include #undef LOG_PREFIX #define LOG_PREFIX "csv: " - + struct context { - unsigned int num_enabled_channels; - uint64_t samplerate; + unsigned int num_enabled_channels; + uint64_t samplerate; uint64_t limit_samples; - char separator; - gboolean header_done; - int *channel_index; - int *channel_unit; - float *channel_scale; - uint16_t *channel_offset; - double *channel_mmin; - double *channel_mmax; + char separator; + gboolean header_done; + int* channel_index; + int* channel_unit; + float* channel_scale; + uint16_t* channel_offset; + double* channel_mmin; + double* channel_mmax; uint32_t ref_min; uint32_t ref_max; uint64_t mask; @@ -63,115 +62,114 @@ struct context { * - Trigger support. */ -static int init(struct sr_output *o, GHashTable *options) +static int init(struct sr_output* o, GHashTable* options) { - struct context *ctx = NULL; - struct sr_channel *ch; - GSList *l; - int i; + struct context* ctx = NULL; + struct sr_channel* ch; + GSList* l; + int i; float range; int ch_num; - if (!o || !o->sdi) - return SR_ERR_ARG; + if (!o || !o->sdi) + return SR_ERR_ARG; - ctx = g_try_malloc0(sizeof(struct context)); - if (ctx == NULL){ + ctx = g_try_malloc0(sizeof(struct context)); + if (ctx == NULL) { sr_err("%s,ERROR:failed to alloc memory.", __func__); return SR_ERR; } memset(ctx, 0, sizeof(struct context)); - o->priv = ctx; - ctx->separator = ','; + o->priv = ctx; + ctx->separator = ','; ctx->mask = 0; ctx->index = 0; ctx->type = g_variant_get_int16(g_hash_table_lookup(options, "type")); ch_num = 0; - if (o->start_sample_index > 0){ + if (o->start_sample_index > 0) { ctx->index = o->start_sample_index; } - /* Get the number of channels, and the unitsize. */ - for (l = o->sdi->channels; l; l = l->next) { - ch = l->data; + /* Get the number of channels, and the unitsize. */ + for (l = o->sdi->channels; l; l = l->next) { + ch = l->data; if (ch->type != ctx->type) - continue; - if (!ch->enabled) - continue; - ctx->num_enabled_channels++; - } + continue; + if (!ch->enabled) + continue; + ctx->num_enabled_channels++; + } - ctx->channel_index = g_try_malloc0(sizeof(int) * ctx->num_enabled_channels); + ctx->channel_index = g_try_malloc0(sizeof(int) * ctx->num_enabled_channels); ctx->channel_unit = g_try_malloc0(sizeof(int) * ctx->num_enabled_channels); ctx->channel_scale = g_try_malloc0(sizeof(float) * ctx->num_enabled_channels); ctx->channel_offset = g_try_malloc0(sizeof(uint16_t) * ctx->num_enabled_channels); ctx->channel_mmax = g_try_malloc0(sizeof(double) * ctx->num_enabled_channels); ctx->channel_mmin = g_try_malloc0(sizeof(double) * ctx->num_enabled_channels); - if (ctx->channel_index == NULL || ctx->channel_mmin == NULL){ + if (ctx->channel_index == NULL || ctx->channel_mmin == NULL) { sr_err("%s,ERROR:failed to alloc memory.", __func__); return SR_ERR; } - /* Once more to map the enabled channels. */ + /* Once more to map the enabled channels. */ i = 0; - for (l = o->sdi->channels; l; l = l->next) { - ch = l->data; + for (l = o->sdi->channels; l; l = l->next) { + ch = l->data; if (ch->type != ctx->type) - continue; - if (!ch->enabled) - continue; + continue; + if (!ch->enabled) + continue; ctx->channel_index[i] = ch->index; - //ctx->mask |= (1 << ch->index); + // ctx->mask |= (1 << ch->index); ctx->mask |= (1 << i); range = ch->vdiv * ch->vfactor * DS_CONF_DSO_VDIVS; - ctx->channel_unit[i] = (range >= 5000000) ? 1000000 : - (range >= 5000) ? 1000 : 1; - // sr_info("unit %d:%d", i, ctx->channel_unit[i]); + ctx->channel_unit[i] = (range >= 5000000) ? 1000000 : (range >= 5000) ? 1000 + : 1; + // sr_info("unit %d:%d", i, ctx->channel_unit[i]); ctx->channel_scale[i] = range / ctx->channel_unit[i]; ctx->channel_offset[i] = ch->hw_offset; ctx->channel_mmax[i] = ch->map_max; ctx->channel_mmin[i] = ch->map_min; i++; - } + } - return SR_OK; + return SR_OK; } -static GString *gen_header(const struct sr_output *o) +static GString* gen_header(const struct sr_output* o) { - struct context *ctx; - struct sr_channel *ch; - GString *header; - GSList *l; - time_t t; - int num_channels, i; - - ctx = o->priv; - header = g_string_sized_new(512); - - /* Some metadata */ - t = time(NULL); - g_string_append_printf(header, "; CSV, generated by DSView, sampled on %s\n", - o->time_string); //ctime(&t) - - /* Columns / channels */ + struct context* ctx; + struct sr_channel* ch; + GString* header; + GSList* l; + time_t t; + int num_channels, i; + + ctx = o->priv; + header = g_string_sized_new(512); + + /* Some metadata */ + t = time(NULL); + g_string_append_printf(header, "; CSV, generated by DSView, sampled on %s\n", + o->time_string); // ctime(&t) + + /* Columns / channels */ if (ctx->type == SR_CHANNEL_LOGIC) num_channels = g_slist_length(o->sdi->channels); else num_channels = ctx->num_enabled_channels; g_string_append_printf(header, "; Channels (%d/%d)\n", - ctx->num_enabled_channels, num_channels); + ctx->num_enabled_channels, num_channels); - - char *samplerate_s = sr_samplerate_string(ctx->samplerate); + char* samplerate_s = sr_samplerate_string(ctx->samplerate); g_string_append_printf(header, "; Sample rate: %s\n", samplerate_s); g_free(samplerate_s); - char *depth_s = sr_samplecount_string(ctx->limit_samples); + char* depth_s = sr_samplecount_string(ctx->limit_samples); g_string_append_printf(header, "; Sample count: %s\n", depth_s); g_free(depth_s); @@ -179,7 +177,7 @@ static GString *gen_header(const struct sr_output *o) g_string_append_printf(header, "Time(s),"); i = 0; - + for (l = o->sdi->channels; l; l = l->next) { ch = l->data; @@ -189,15 +187,13 @@ static GString *gen_header(const struct sr_output *o) continue; if (ctx->type == SR_CHANNEL_DSO) { - char *unit_s = ctx->channel_unit[i] >= 1000000 ? "kV" : - ctx->channel_unit[i] >= 1000 ? "V" : "mV"; - //sr_info("head %d:%s", i, unit_s); + char* unit_s = ctx->channel_unit[i] >= 1000000 ? "kV" : ctx->channel_unit[i] >= 1000 ? "V" + : "mV"; + // sr_info("head %d:%s", i, unit_s); g_string_append_printf(header, " %s (Unit: %s),", ch->name, unit_s); - } - else if (ctx->type == SR_CHANNEL_ANALOG) { + } else if (ctx->type == SR_CHANNEL_ANALOG) { g_string_append_printf(header, " %s (Unit: %s),", ch->name, ch->map_unit); - } - else { + } else { g_string_append_printf(header, " %s,", ch->name); } @@ -209,51 +205,50 @@ static GString *gen_header(const struct sr_output *o) g_string_truncate(header, header->len - 1); g_string_append_printf(header, "\n"); - return header; + return header; } -static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet, - GString **out) +static int receive(const struct sr_output* o, const struct sr_datafeed_packet* packet, + GString** out) { - const struct sr_datafeed_meta *meta; - const struct sr_datafeed_logic *logic; - const struct sr_datafeed_dso *dso; - const struct sr_datafeed_analog *analog; - const struct sr_config *src; - GSList *l; - struct context *ctx; - int idx; - uint64_t i, j; + const struct sr_datafeed_meta* meta; + const struct sr_datafeed_logic* logic; + const struct sr_datafeed_dso* dso; + const struct sr_datafeed_analog* analog; + const struct sr_config* src; + GSList* l; + struct context* ctx; + int idx; + uint64_t i, j; unsigned char *p, c; double tmpv; - struct sr_channel *ch; + struct sr_channel* ch; int bDotLetter = 0; char tmp_buffer[50]; - char *pFlagCheck = tmp_buffer; + char* pFlagCheck = tmp_buffer; sprintf(tmp_buffer, "%0.15g", DBL_MAX); - - while (*pFlagCheck) - { - if (*pFlagCheck == '.'){ + + while (*pFlagCheck) { + if (*pFlagCheck == '.') { bDotLetter = 1; break; } pFlagCheck++; } - *out = NULL; - if (!o || !o->sdi) - return SR_ERR_ARG; - if (!(ctx = o->priv)) - return SR_ERR_ARG; - - switch (packet->type) { - case SR_DF_META: - meta = packet->payload; - for (l = meta->config; l; l = l->next) { - src = l->data; + *out = NULL; + if (!o || !o->sdi) + return SR_ERR_ARG; + if (!(ctx = o->priv)) + return SR_ERR_ARG; + + switch (packet->type) { + case SR_DF_META: + meta = packet->payload; + for (l = meta->config; l; l = l->next) { + src = l->data; if (src->key == SR_CONF_SAMPLERATE) ctx->samplerate = g_variant_get_uint64(src->data); else if (src->key == SR_CONF_LIMIT_SAMPLES) @@ -263,56 +258,55 @@ static int receive(const struct sr_output *o, const struct sr_datafeed_packet *p else if (src->key == SR_CONF_REF_MAX) ctx->ref_max = g_variant_get_uint32(src->data); } - break; - case SR_DF_LOGIC: - logic = packet->payload; - if (!ctx->header_done) { - *out = gen_header(o); - ctx->header_done = TRUE; - } else { - *out = g_string_sized_new(512); - } - - for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { - ctx->index++; + break; + case SR_DF_LOGIC: + logic = packet->payload; + if (!ctx->header_done) { + *out = gen_header(o); + ctx->header_done = TRUE; + } else { + *out = g_string_sized_new(512); + } - if (packet->bExportOriginalData == 0){ - if (ctx->index > 1 && (*(uint64_t *)(logic->data + i) & ctx->mask) == ctx->pre_data) - continue; - } - - tmpv = (double)(ctx->index-1) / (double)ctx->samplerate; + for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { + ctx->index++; - if (bDotLetter){ - g_string_append_printf(*out, "%0.15g", tmpv); + if (packet->bExportOriginalData == 0) { + if (ctx->index > 1 && (*(uint64_t*)(logic->data + i) & ctx->mask) == ctx->pre_data) + continue; } - else{ + + tmpv = (double)(ctx->index - 1) / (double)ctx->samplerate; + + if (bDotLetter) { + g_string_append_printf(*out, "%0.15g", tmpv); + } else { sprintf(tmp_buffer, "%0.15g", tmpv); pFlagCheck = tmp_buffer; - while (*pFlagCheck){ - if (*pFlagCheck == ','){ + while (*pFlagCheck) { + if (*pFlagCheck == ',') { *pFlagCheck = '.'; break; } pFlagCheck++; } - g_string_append_printf(*out, tmp_buffer); + g_string_append(*out, tmp_buffer); } for (j = 0; j < ctx->num_enabled_channels; j++) { idx = j; - p = logic->data + i + idx / 8; - c = *p & (1 << (idx % 8)); + p = logic->data + i + idx / 8; + c = *p & (1 << (idx % 8)); g_string_append_c(*out, ctx->separator); g_string_append_c(*out, c ? '1' : '0'); - } - g_string_append_printf(*out, "\n"); - ctx->pre_data = (*(uint64_t *)(logic->data + i) & ctx->mask); - } - break; - case SR_DF_DSO: + } + g_string_append_printf(*out, "\n"); + ctx->pre_data = (*(uint64_t*)(logic->data + i) & ctx->mask); + } + break; + case SR_DF_DSO: dso = packet->payload; if (!ctx->header_done) { *out = gen_header(o); @@ -325,9 +319,7 @@ static int receive(const struct sr_output *o, const struct sr_datafeed_packet *p for (j = 0; j < ctx->num_enabled_channels; j++) { idx = ctx->channel_index[j]; p = dso->data + i * ctx->num_enabled_channels + idx * ((ctx->num_enabled_channels > 1) ? 1 : 0); - g_string_append_printf(*out, "%0.5f", (ctx->channel_offset[j] - *p) * - ctx->channel_scale[j] / - (ctx->ref_max - ctx->ref_min)); + g_string_append_printf(*out, "%0.5f", (ctx->channel_offset[j] - *p) * ctx->channel_scale[j] / (ctx->ref_max - ctx->ref_min)); g_string_append_c(*out, ctx->separator); } @@ -337,89 +329,88 @@ static int receive(const struct sr_output *o, const struct sr_datafeed_packet *p } break; case SR_DF_ANALOG: - analog = packet->payload; - if (!ctx->header_done) { - *out = gen_header(o); - ctx->header_done = TRUE; - } else { - *out = g_string_sized_new(512); - } - - int enalbe_channel_flags[8]; - int ch_num = 0; - - for (l = o->sdi->channels; l; l = l->next){ + analog = packet->payload; + if (!ctx->header_done) { + *out = gen_header(o); + ctx->header_done = TRUE; + } else { + *out = g_string_sized_new(512); + } + + int enalbe_channel_flags[8]; + int ch_num = 0; + + for (l = o->sdi->channels; l; l = l->next) { ch = l->data; enalbe_channel_flags[ch_num++] = ch->enabled; - } - - double max_min_ref = (double)(ctx->ref_max - ctx->ref_min); - double vf = 0; - int ch_cfg_dex = 0; - double hw_offset = 0; - double mapRange = 0; - double mmax = 0; - double mmin = 0; - void *ptr; - - for (i = 0; i < (uint64_t)analog->num_samples; i++) { + } + + double max_min_ref = (double)(ctx->ref_max - ctx->ref_min); + double vf = 0; + int ch_cfg_dex = 0; + double hw_offset = 0; + double mapRange = 0; + double mmax = 0; + double mmin = 0; + void* ptr; + + for (i = 0; i < (uint64_t)analog->num_samples; i++) { ch_cfg_dex = 0; - for (j = 0; j < ch_num; j++) { + for (j = 0; j < (uint64_t)ch_num; j++) { - if (enalbe_channel_flags[j] == 0){ + if (enalbe_channel_flags[j] == 0) { continue; - } - - ptr = (unsigned char*)analog->data + i * ch_num + j; - p = (unsigned char*)ptr; - hw_offset = (double)ctx->channel_offset[ch_cfg_dex]; - mmax = ctx->channel_mmax[ch_cfg_dex]; - mmin = ctx->channel_mmin[ch_cfg_dex]; - mapRange = (mmax - mmin); - vf = (hw_offset - (double)(*p)) * mapRange / max_min_ref; - - g_string_append_printf(*out, "%0.5f", vf); - g_string_append_c(*out, ctx->separator); - - ch_cfg_dex++; - } - - /* Drop last separator. */ - g_string_truncate(*out, (*out)->len - 1); - g_string_append_printf(*out, "\n"); - } - break; - } - - return SR_OK; + } + + ptr = (unsigned char*)analog->data + i * ch_num + j; + p = (unsigned char*)ptr; + hw_offset = (double)ctx->channel_offset[ch_cfg_dex]; + mmax = ctx->channel_mmax[ch_cfg_dex]; + mmin = ctx->channel_mmin[ch_cfg_dex]; + mapRange = (mmax - mmin); + vf = (hw_offset - (double)(*p)) * mapRange / max_min_ref; + + g_string_append_printf(*out, "%0.5f", vf); + g_string_append_c(*out, ctx->separator); + + ch_cfg_dex++; + } + + /* Drop last separator. */ + g_string_truncate(*out, (*out)->len - 1); + g_string_append_printf(*out, "\n"); + } + break; + } + + return SR_OK; } -static int cleanup(struct sr_output *o) +static int cleanup(struct sr_output* o) { - struct context *ctx; + struct context* ctx; - if (!o || !o->sdi) - return SR_ERR_ARG; + if (!o || !o->sdi) + return SR_ERR_ARG; - if (o->priv) { - ctx = o->priv; - g_free(ctx->channel_index); - g_free(o->priv); - o->priv = NULL; - } + if (o->priv) { + ctx = o->priv; + g_free(ctx->channel_index); + g_free(o->priv); + o->priv = NULL; + } - return SR_OK; + return SR_OK; } SR_PRIV struct sr_output_module output_csv = { - .id = "csv", - .name = "CSV", - .desc = "Comma-separated values", - .exts = (const char*[]){"csv", NULL}, - .options = NULL, - .init = init, - .receive = receive, - .cleanup = cleanup, + .id = "csv", + .name = "CSV", + .desc = "Comma-separated values", + .exts = (const char*[]) { "csv", NULL }, + .options = NULL, + .init = init, + .receive = receive, + .cleanup = cleanup, }; -