1 /* Copyright (c) 2015, The Linux Foundation. All rights reserved. 2 * 3 * Redistribution and use in source and binary forms, with or without 4 * modification, are permitted provided that the following conditions are 5 * met: 6 * * Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * * Redistributions in binary form must reproduce the above 9 * copyright notice, this list of conditions and the following 10 * disclaimer in the documentation and/or other materials provided 11 * with the distribution. 12 * * Neither the name of The Linux Foundation nor the names of its 13 * contributors may be used to endorse or promote products derived 14 * from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <stdint.h> 30 #include <stdlib.h> 31 32 #include "sync.h" 33 #include "wifi_hal.h" 34 #include "common.h" 35 36 #include "ring_buffer.h" 37 #include "rb_wrapper.h" 38 39 #define LOG_TAG "WifiHAL" 40 41 wifi_error rb_init(hal_info *info, struct rb_info *rb_info, int id, 42 size_t size_of_buf, int num_bufs, char *name) 43 { 44 rb_info->rb_ctx = ring_buffer_init(size_of_buf, num_bufs); 45 if (rb_info->rb_ctx == NULL) { 46 ALOGE("Failed to init ring buffer"); 47 return WIFI_ERROR_OUT_OF_MEMORY; 48 } 49 strlcpy(rb_info->name, name, MAX_RB_NAME_SIZE); 50 rb_info->ctx = info; 51 rb_info->id = id; 52 53 /* Initialize last_push_time */ 54 gettimeofday(&rb_info->last_push_time, NULL); 55 return WIFI_SUCCESS; 56 } 57 58 void rb_deinit(struct rb_info *rb_info) 59 { 60 if (rb_info->rb_ctx) { 61 ring_buffer_deinit(rb_info->rb_ctx); 62 rb_info->rb_ctx = NULL; 63 } 64 rb_info->name[0] = '\0'; 65 } 66 67 void get_rb_status(struct rb_info *rb_info, wifi_ring_buffer_status *rbs) 68 { 69 struct rb_stats rb_stats; 70 71 strlcpy((char *)rbs->name, rb_info->name, MAX_RB_NAME_SIZE); 72 rbs->flags = rb_info->flags; 73 rbs->ring_id = rb_info->id; 74 rbs->verbose_level = rb_info->verbose_level; 75 rb_get_stats(rb_info->rb_ctx, &rb_stats); 76 rbs->ring_buffer_byte_size = rb_stats.max_num_bufs * 77 rb_stats.each_buf_size; 78 rbs->written_bytes = rb_stats.total_bytes_written; 79 rbs->read_bytes = rb_stats.total_bytes_read; 80 rbs->written_records = rb_info->written_records; 81 } 82 83 int is_rb_name_match(struct rb_info *rb_info, char *name) 84 { 85 return (strncmp(rb_info->name, name, MAX_RB_NAME_SIZE) == 0); 86 } 87 88 wifi_error ring_buffer_write(struct rb_info *rb_info, u8 *buf, size_t length, 89 int no_of_records) 90 { 91 if (rb_write(rb_info->rb_ctx, buf, length, 0) != RB_SUCCESS) { 92 ALOGE("Failed to write into rb, RB migght be full"); 93 /* TODO Probably the data can be read from here the RB and then 94 * rb_write can be tried */ 95 return WIFI_ERROR_OUT_OF_MEMORY; 96 } 97 98 rb_info->written_records += no_of_records; 99 return WIFI_SUCCESS; 100 } 101 102 void push_out_rb_data(void *cb_ctx) 103 { 104 struct rb_info *rb_info = (struct rb_info *)cb_ctx; 105 hal_info *info = (hal_info *)rb_info->ctx; 106 wifi_ring_buffer_status rbs; 107 108 if (info->on_ring_buffer_data == NULL) { 109 ALOGE("on_ring_buffer_data handle is not set yet"); 110 return; 111 } 112 113 while (1) { 114 size_t length = 0; 115 u8 *buf; 116 117 buf = rb_get_read_buf(rb_info->rb_ctx, &length); 118 if (buf == NULL) { 119 break; 120 } 121 get_rb_status(rb_info, &rbs); 122 info->on_ring_buffer_data(rb_info->name, (char *)buf, length, &rbs); 123 free(buf); 124 }; 125 gettimeofday(&rb_info->last_push_time, NULL); 126 } 127 128 wifi_error rb_start_logging(struct rb_info *rb_info, u32 verbose_level, 129 u32 flags, u32 max_interval_sec, u32 min_data_size) 130 { 131 rb_info->verbose_level = verbose_level; 132 rb_info->flags = flags; 133 rb_info->max_interval_sec = max_interval_sec; 134 135 rb_config_threshold(rb_info->rb_ctx, min_data_size, push_out_rb_data, rb_info); 136 return WIFI_SUCCESS; 137 } 138 139 void rb_check_for_timeout(struct rb_info *rb_info, struct timeval *now) 140 { 141 if (rb_info->max_interval_sec == 0) { 142 return; 143 } 144 if (now->tv_sec >= 145 (rb_info->last_push_time.tv_sec + 146 (__kernel_time_t)rb_info->max_interval_sec)) { 147 push_out_rb_data(rb_info); 148 } 149 } 150