• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "obex_body.h"
17 #include <cstring>
18 #include <memory>
19 #include "log.h"
20 #include "obex_types.h"
21 
22 namespace bluetooth {
ObexArrayBodyObject(const uint8_t * buf,size_t bufLen)23 ObexArrayBodyObject::ObexArrayBodyObject(const uint8_t *buf, size_t bufLen)
24 {
25     auto ret = PrivateWrite(buf, bufLen);
26     if (ret == 0) {
27         OBEX_LOG_ERROR("%{public}s, ret=%zu", __PRETTY_FUNCTION__, ret);
28     }
29 }
30 
Read(uint8_t * buf,size_t bufLen)31 size_t ObexArrayBodyObject::Read(uint8_t *buf, size_t bufLen)
32 {
33     std::lock_guard<std::mutex> lock(mutex_);
34     size_t readSize = bufLen;
35     size_t remainSize = body_.size() - index_;
36     if (remainSize < readSize) {
37         readSize = remainSize;
38     }
39     for (size_t i = 0; i < readSize; i++) {
40         buf[i] = body_[index_++];
41     }
42     OBEX_LOG_DEBUG("ObexArrayBodyObject::Read: %zu / %zu", index_, body_.size());
43     return readSize;
44 }
45 
Write(const uint8_t * buf,size_t bufLen)46 size_t ObexArrayBodyObject::Write(const uint8_t *buf, size_t bufLen)
47 {
48     return PrivateWrite(buf, bufLen);
49 }
50 
PrivateWrite(const uint8_t * buf,size_t bufLen)51 size_t ObexArrayBodyObject::PrivateWrite(const uint8_t *buf, size_t bufLen)
52 {
53     std::lock_guard<std::mutex> lock(mutex_);
54     body_.insert(body_.end(), buf, buf + bufLen);
55     return bufLen;
56 }
57 
Close()58 int ObexArrayBodyObject::Close()
59 {
60     std::lock_guard<std::mutex> lock(mutex_);
61     return 0;
62 }
63 }  // namespace bluetooth