1 /*
2  * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3  */
4 
5 #include "ulog_ring_fifo.h"
6 #include <string.h>
7 #include <stdio.h>
8 #include "aos/kernel.h"
9 #include "ulog_config.h"
10 
11 static aos_queue_t ulog_queue;
12 static uint8_t*    ulog_buffer;
13 
14 
15 /**
16 * This function will create a ring fifo for ulog.
17 *
18 *
19 * @return  0: success.
20 */
uring_fifo_init()21 int uring_fifo_init()
22 {
23     int rc = -1;
24     if (ulog_buffer == NULL) {
25         ulog_buffer = aos_malloc(DEFAULT_ASYNC_BUF_SIZE);
26         if (ulog_buffer != NULL) {
27             rc = aos_queue_new(&ulog_queue, ulog_buffer, DEFAULT_ASYNC_BUF_SIZE, ULOG_SIZE);
28             if (0 != rc) {
29                 aos_free(ulog_buffer);
30                 ulog_buffer = NULL;
31             }
32         }
33     }
34 
35     return rc;
36 }
37 
38 /**
39 * Thread Safe to put the msg into ring - fifo.
40 *
41 * @param[in]  queue  pointer to the queue.
42 * @param[in]  msg    msg to send.
43 * @param[in]  size   size of the msg.
44 *
45 * @return  0: success.
46 */
uring_fifo_push_s(const void * buf,const uint16_t len)47 int uring_fifo_push_s(const void* buf, const uint16_t len)
48 {
49     return aos_queue_send(&ulog_queue, (void*)buf, len);
50 }
51 
uring_fifo_pop_cb(pop_callback cb,void * cb_arg)52 int uring_fifo_pop_cb(pop_callback cb, void* cb_arg)
53 {
54     char tmp_buf[ULOG_SIZE];
55     unsigned int rcv_size = 0;
56     int rc = aos_queue_recv(&ulog_queue, AOS_WAIT_FOREVER, tmp_buf, &rcv_size);
57     if ((0 == rc) && (cb != NULL)) {
58         cb(cb_arg, tmp_buf, (uint16_t)rcv_size);
59     }
60     return rc;
61 }
62