Home | History | Annotate | Download | only in wifi_hal
      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, size_t record_length)
     90 {
     91     enum rb_status status;
     92 
     93     status = rb_write(rb_info->rb_ctx, buf, length, 0, record_length);
     94     if ((status == RB_FULL) || (status == RB_RETRY)) {
     95          push_out_rb_data(rb_info);
     96          /* Try writing the data after reading it out */
     97         status = rb_write(rb_info->rb_ctx, buf, length, 0, record_length);
     98         if (status != RB_SUCCESS) {
     99             ALOGE("Failed to rewrite %zu bytes to rb %s with error %d", length,
    100                   rb_info->name, status);
    101             return WIFI_ERROR_UNKNOWN;
    102         }
    103     } else if (status == RB_FAILURE) {
    104         ALOGE("Failed to write %zu bytes to rb %s with error %d", length,
    105               rb_info->name, status);
    106         return WIFI_ERROR_UNKNOWN;
    107     }
    108 
    109     rb_info->written_records += no_of_records;
    110     return WIFI_SUCCESS;
    111 }
    112 
    113 void push_out_rb_data(void *cb_ctx)
    114 {
    115     struct rb_info *rb_info = (struct rb_info *)cb_ctx;
    116     hal_info *info = (hal_info *)rb_info->ctx;
    117     wifi_ring_buffer_status rbs;
    118     wifi_ring_buffer_data_handler handler;
    119 
    120     while (1) {
    121         size_t length = 0;
    122         u8 *buf;
    123 
    124         buf = rb_get_read_buf(rb_info->rb_ctx, &length);
    125         if (buf == NULL) {
    126             break;
    127         }
    128         get_rb_status(rb_info, &rbs);
    129         pthread_mutex_lock(&info->lh_lock);
    130         handler.on_ring_buffer_data = info->on_ring_buffer_data;
    131         pthread_mutex_unlock(&info->lh_lock);
    132         if (handler.on_ring_buffer_data) {
    133             handler.on_ring_buffer_data(rb_info->name, (char *)buf,
    134                                         length, &rbs);
    135         }
    136         free(buf);
    137     };
    138     gettimeofday(&rb_info->last_push_time, NULL);
    139 }
    140 
    141 wifi_error rb_start_logging(struct rb_info *rb_info, u32 verbose_level,
    142                             u32 flags, u32 max_interval_sec, u32 min_data_size)
    143 {
    144     rb_info->verbose_level = verbose_level;
    145     rb_info->flags = flags;
    146     rb_info->max_interval_sec = max_interval_sec;
    147 
    148     rb_config_threshold(rb_info->rb_ctx, min_data_size, push_out_rb_data, rb_info);
    149     return WIFI_SUCCESS;
    150 }
    151 
    152 void rb_check_for_timeout(struct rb_info *rb_info, struct timeval *now)
    153 {
    154     if (rb_info->max_interval_sec == 0) {
    155         return;
    156     }
    157     if (now->tv_sec >=
    158         (rb_info->last_push_time.tv_sec +
    159          (__kernel_time_t)rb_info->max_interval_sec)) {
    160         push_out_rb_data(rb_info);
    161     }
    162 }
    163