diff --git a/lib/pktgen/pktgen.c b/lib/pktgen/pktgen.c index 5639525b2..20ac63688 100644 --- a/lib/pktgen/pktgen.c +++ b/lib/pktgen/pktgen.c @@ -97,7 +97,7 @@ int onewifi_pktgen_is_running(void) { FILE *fp; char str[BUF_LEN_MAX]; - char run_if[32]; + char run_if[BUF_LEN_MAX] = ""; int retval = 0; fp = fopen(PKTGEN_THREAD_FILE_0, "r"); diff --git a/source/sampleapps/webconfig_consumer_apis.c b/source/sampleapps/webconfig_consumer_apis.c index 67e0cf417..276c663e4 100644 --- a/source/sampleapps/webconfig_consumer_apis.c +++ b/source/sampleapps/webconfig_consumer_apis.c @@ -2448,7 +2448,10 @@ void webconfig_consumer_sta_conn_status(rbusHandle_t handle, rbusEvent_t const* printf("%s:%d Rbus event name=%s\n",__FUNCTION__, __LINE__, event->name); - sscanf(event->name, "Device.WiFi.STA.%d.Connection.Status", &index); + if (sscanf(event->name, "Device.WiFi.STA.%u.Connection.Status", &index) != 1) { + printf("%s:%d sscanf failed to parse index from event name\n", __FUNCTION__, __LINE__); + return; + } temp_buff = rbusValue_GetBytes(value, &len); if (temp_buff == NULL) { printf("%s:%d Rbus get string failure len=%d\n", __FUNCTION__, __LINE__, len); @@ -2457,17 +2460,19 @@ void webconfig_consumer_sta_conn_status(rbusHandle_t handle, rbusEvent_t const* memcpy(&sta_conn_info, temp_buff, len); conn_status = (sta_conn_info.connect_status == wifi_connection_status_connected) ? true:false; - if (conn_status == true) { - printf("%s:%d: Station successfully connected with external AP radio:%d\r\n", + if ((conn_status == true) && (index > 0)) { + printf("%s:%d: Station successfully connected with external AP radio:%u\r\n", __func__, __LINE__, index - 1); if (index == 1) { get_rbus_sta_interface_name(WIFI_STA_2G_INTERFACE_NAME); } else if (index == 2) { get_rbus_sta_interface_name(WIFI_STA_5G_INTERFACE_NAME); } + } else if ((conn_status == false) && (index > 0)) { + printf("%s:%d: Station disconnected from external AP radio:%u\r\n", + __func__, __LINE__, index - 1); } else { - printf("%s:%d: Station disconnected with external AP:%d radio:%d\r\n", - __func__, __LINE__, conn_status, index - 1); + printf("%s:%d: Unknown radio index\r\n", __func__, __LINE__); } printf("%s:%d: MAC address info:%s\r\n", __func__, __LINE__, to_mac_str(sta_conn_info.bssid, mac_str)); diff --git a/source/sampleapps/wifievents_consumer_sample.c b/source/sampleapps/wifievents_consumer_sample.c index 103b269b6..6d31fba7b 100644 --- a/source/sampleapps/wifievents_consumer_sample.c +++ b/source/sampleapps/wifievents_consumer_sample.c @@ -538,62 +538,90 @@ void rotate_and_write_CSIData(mac_address_t sta_mac, wifi_csi_data_t *csi) { #define MB(x) ((long int)(x) << 20) #define CSI_FILE "/tmp/CSI.bin" -#define CSI_TMP_FILE "/tmp/CSI_tmp.bin" - WIFI_EVENT_CONSUMER_DGB("Enter %s: %d\n", __FUNCTION__, __LINE__); - char filename[] = CSI_FILE; - char filename_tmp[] = CSI_TMP_FILE; - FILE *csifptr; - FILE *csifptr_tmp; +#define CSI_TMP_TEMPLATE "/tmp/CSI_tmp.XXXXXX" + + WIFI_EVENT_CONSUMER_DGB("Enter %s\n", __FUNCTION__); + FILE *csifptr = NULL; + FILE *csifptr_tmp = NULL; + int tmp_fd = -1; + char tmp_filename[] = CSI_TMP_TEMPLATE; + struct stat st; mac_address_t tmp_mac; - wifi_csi_matrix_t tmp_csi_matrix; wifi_frame_info_t tmp_frame_info; + wifi_csi_matrix_t tmp_csi_matrix; - if (csi == NULL) - return; - csifptr = fopen(filename, "r"); - csifptr_tmp = fopen(filename_tmp, "w"); - if (csifptr != NULL) { - // get the size of the file - stat(filename, &st); - if (st.st_size > MB(1)) // if file size is greate than 1 mb - { - mac_address_t tmp_mac; - wifi_frame_info_t tmp_frame_info; - wifi_csi_matrix_t tmp_csi_matrix; - - fread(&tmp_mac, sizeof(mac_address_t), 1, csifptr); - fread(&tmp_frame_info, sizeof(wifi_frame_info_t), 1, csifptr); - fread(&tmp_csi_matrix, sizeof(wifi_csi_matrix_t), 1, csifptr); - } - // copy rest of the content in to the temp file - while (csifptr != NULL && fread(&tmp_mac, sizeof(mac_address_t), 1, csifptr)) { - fread(&tmp_frame_info, sizeof(wifi_frame_info_t), 1, csifptr); - fread(&tmp_csi_matrix, sizeof(wifi_csi_matrix_t), 1, csifptr); - fwrite(&tmp_mac, sizeof(mac_address_t), 1, csifptr_tmp); - fwrite(&tmp_frame_info, sizeof(wifi_frame_info_t), 1, csifptr_tmp); - fwrite(&tmp_csi_matrix, sizeof(wifi_csi_matrix_t), 1, csifptr_tmp); + /* Open existing CSI file (optional) */ + csifptr = fopen(CSI_FILE, "r"); + + /* Secure temp file */ + mode_t old_umask = umask(0066); + tmp_fd = mkstemp(tmp_filename); + umask(old_umask); + if (tmp_fd < 0) { + WIFI_EVENT_CONSUMER_DGB("mkstemp failed: %s\n", strerror(errno)); + goto cleanup; + } + + csifptr_tmp = fdopen(tmp_fd, "w"); + if (!csifptr_tmp) { + WIFI_EVENT_CONSUMER_DGB("fdopen failed: %s\n", strerror(errno)); + goto cleanup; + } + + /* Rotate old data if needed */ + if (csifptr && fstat(fileno(csifptr), &st) == 0 && st.st_size > MB(1)) { + /* Drop the oldest record. On partial read the file position is + * mid-record, which would misalign the copy loop below. Rewind + * to the start so we copy everything rather than corrupt data. */ + if (fread(&tmp_mac, sizeof(tmp_mac), 1, csifptr) != 1 || + fread(&tmp_frame_info, sizeof(tmp_frame_info), 1, csifptr) != 1 || + fread(&tmp_csi_matrix, sizeof(tmp_csi_matrix), 1, csifptr) != 1) { + WIFI_EVENT_CONSUMER_DGB("Failed to read oldest CSI record\n"); + rewind(csifptr); } } - if (csifptr_tmp != NULL) { - fwrite(sta_mac, sizeof(mac_address_t), 1, csifptr_tmp); - fwrite(&(csi->frame_info), sizeof(wifi_frame_info_t), 1, csifptr_tmp); - fwrite(&(csi->csi_matrix), sizeof(wifi_csi_matrix_t), 1, csifptr_tmp); + /* Copy remaining records */ + if (csifptr) { + while (fread(&tmp_mac, sizeof(tmp_mac), 1, csifptr) == 1) { + if (fread(&tmp_frame_info, sizeof(tmp_frame_info), 1, csifptr) != 1 || + fread(&tmp_csi_matrix, sizeof(tmp_csi_matrix), 1, csifptr) != 1) { + WIFI_EVENT_CONSUMER_DGB("Failed to read CSI record during copy\n"); + break; + } + + fwrite(&tmp_mac, sizeof(tmp_mac), 1, csifptr_tmp); + fwrite(&tmp_frame_info, sizeof(tmp_frame_info), 1, csifptr_tmp); + fwrite(&tmp_csi_matrix, sizeof(tmp_csi_matrix), 1, csifptr_tmp); + } } - if (csifptr != NULL) { + /* Append new CSI record */ + fwrite(sta_mac, sizeof(mac_address_t), 1, csifptr_tmp); + fwrite(&csi->frame_info, sizeof(wifi_frame_info_t), 1, csifptr_tmp); + fwrite(&csi->csi_matrix, sizeof(wifi_csi_matrix_t), 1, csifptr_tmp); + +cleanup: + if (csifptr) { fclose(csifptr); - unlink(filename); } - if (csifptr_tmp != NULL) { + + if (csifptr_tmp) { fclose(csifptr_tmp); - rename(filename_tmp, filename); + if (rename(tmp_filename, CSI_FILE) < 0) { + WIFI_EVENT_CONSUMER_DGB("rename %s -> %s failed: %s\n", + tmp_filename, CSI_FILE, strerror(errno)); + unlink(tmp_filename); + } + } else if (tmp_fd >= 0) { + close(tmp_fd); + unlink(tmp_filename); } csi_data_in_json_format(sta_mac, csi); - WIFI_EVENT_CONSUMER_DGB("Exit %s: %d\n", __FUNCTION__, __LINE__); + WIFI_EVENT_CONSUMER_DGB("Exit %s\n", __FUNCTION__); } static void print_csi_data(char *buffer)