1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "a2dp_shared_buffer.h"
16 #include "securec.h"
17 #include "log.h"
18 
19 namespace OHOS {
20 namespace bluetooth {
A2dpSharedBuffer()21 A2dpSharedBuffer::A2dpSharedBuffer()
22 {
23     std::lock_guard<std::mutex> lock_(mutex_);
24     cap_ = A2DP_SBC_MAX_PACKET_SIZE * FRAME_THREE;
25     size_ = 0;
26     isValid_ = false;
27 }
28 
~A2dpSharedBuffer()29 A2dpSharedBuffer::~A2dpSharedBuffer()
30 {
31 }
32 
Read(uint8_t * buf,uint32_t len)33 uint32_t A2dpSharedBuffer::Read(uint8_t *buf, uint32_t len)
34 {
35     std::lock_guard<std::mutex> lock_(mutex_);
36     if (!isValid_) {
37         LOG_ERROR("[A2dpSharedBuffer] %{public}s: buffer not ready!", __func__);
38         return 0;
39     }
40     LOG_INFO("[A2dpSharedBuffer] %{public}s: start size_ [%{public}u]  len[%{public}u] cap_ [%{public}u] \n",
41         __func__, size_,  len, cap_);
42     if (size_ < len) {
43         LOG_ERROR("[A2dpSharedBuffer] %{public}s: no sufficient data\n", __func__);
44         return 0;
45     }
46     if (memcpy_s(buf, len, buf_, len) != EOK) {
47         LOG_ERROR("[A2dpSharedBuffer] %{public}s: memcpy_s failed\n", __func__);
48     }
49     size_ -= len;
50     if (memcpy_s(shiftBuf_, cap_, buf_ + len, size_) != EOK) {
51         LOG_ERROR("[A2dpSharedBuffer] %{public}s: memcpy_s failed\n", __func__);
52     }
53     if (memcpy_s(buf_, cap_, shiftBuf_, size_) != EOK) {
54         LOG_ERROR("[A2dpSharedBuffer] %{public}s: memcpy_s failed\n", __func__);
55     }
56     LOG_INFO("[A2dpSharedBuffer] %{public}s: end size_ [%{public}u] len[%{public}u] cap_[%{public}u]\n",
57         __func__, size_, len, cap_);
58     return len;
59 }
60 
Write(const uint8_t * buf,uint32_t len)61 uint32_t A2dpSharedBuffer::Write(const uint8_t *buf, uint32_t len)
62 {
63     std::lock_guard<std::mutex> lock_(mutex_);
64     if (!isValid_) {
65         LOG_ERROR("[A2dpSharedBuffer] %{public}s: buffer not ready!", __func__);
66         return 0;
67     }
68     LOG_INFO("[A2dpSharedBuffer] %{public}s: start size_ [%{public}u] len[%{public}u] cap_[%{public}u]\n",
69         __func__, size_, len, cap_);
70     if (size_ + len > cap_) {
71         LOG_ERROR("[A2dpSharedBuffer] %{public}s: no space size_ [%{public}u] len[%{public}u] cap_[%{public}u]\n",
72             __func__, size_, len, cap_);
73         return 0;
74     }
75     if (memcpy_s(buf_ + size_, cap_ - size_, buf, len) != EOK) {
76         LOG_ERROR("[A2dpSharedBuffer] %{public}s: memcpy_s failed\n", __func__);
77     }
78     size_ += len;
79     LOG_INFO("[A2dpSharedBuffer] %{public}s: end size_ [%{public}u] len[%{public}u] cap_[%{public}u]\n",
80         __func__, size_, len, cap_);
81     return len;
82 }
83 
Reset()84 void A2dpSharedBuffer::Reset()
85 {
86     std::lock_guard<std::mutex> lock_(mutex_);
87     if (memset_s(buf_, cap_, 0, cap_) != EOK) {
88         LOG_ERROR("[A2dpSharedBuffer] %{public}s: memset_s failed\n", __func__);
89     }
90     size_ = 0;
91     isValid_ = false;
92 }
93 
SetValid(bool isValid)94 void A2dpSharedBuffer::SetValid(bool isValid)
95 {
96     std::lock_guard<std::mutex> lock_(mutex_);
97     LOG_ERROR("[A2dpSharedBuffer] %{public}s: isValid %{public}d\n", __func__, isValid);
98     isValid_ = isValid;
99 }
100 }  // namespace bluetooth
101 }  // namespace OHOS