1 /*
2 * Copyright (c) 2021-2023 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 "data_buffer.h"
17 #include "distributed_camera_errno.h"
18 #include "distributed_camera_constants.h"
19
20 namespace OHOS {
21 namespace DistributedHardware {
DataBuffer(size_t capacity)22 DataBuffer::DataBuffer(size_t capacity)
23 {
24 if (capacity != 0 && capacity <= DCAMERA_MAX_RECV_DATA_LEN) {
25 data_ = new uint8_t[capacity] {0};
26 if (data_ != nullptr) {
27 capacity_ = capacity;
28 rangeLength_ = capacity;
29 }
30 }
31 }
32
Capacity() const33 size_t DataBuffer::Capacity() const
34 {
35 return capacity_;
36 }
37
Size() const38 size_t DataBuffer::Size() const
39 {
40 return rangeLength_;
41 }
42
Offset() const43 size_t DataBuffer::Offset() const
44 {
45 return rangeOffset_;
46 }
47
Data() const48 uint8_t *DataBuffer::Data() const
49 {
50 return data_ + rangeOffset_;
51 }
52
SetRange(size_t offset,size_t size)53 int32_t DataBuffer::SetRange(size_t offset, size_t size)
54 {
55 if (!(offset <= capacity_) || !(offset + size <= capacity_)) {
56 return DCAMERA_BAD_VALUE;
57 }
58
59 rangeOffset_ = offset;
60 rangeLength_ = size;
61 return DCAMERA_OK;
62 }
63
SetInt32(const std::string name,int32_t value)64 void DataBuffer::SetInt32(const std::string name, int32_t value)
65 {
66 int32Map_[name] = value;
67 }
68
SetInt64(const std::string name,int64_t value)69 void DataBuffer::SetInt64(const std::string name, int64_t value)
70 {
71 int64Map_[name] = value;
72 }
73
SetString(const std::string name,std::string value)74 void DataBuffer::SetString(const std::string name, std::string value)
75 {
76 stringMap_[name] = value;
77 }
78
FindInt32(const std::string & name,int32_t & value)79 bool DataBuffer::FindInt32(const std::string& name, int32_t& value)
80 {
81 if (int32Map_.count(name) != 0) {
82 value = int32Map_[name];
83 return true;
84 } else {
85 value = 0;
86 return false;
87 }
88 }
89
FindInt64(const std::string & name,int64_t & value)90 bool DataBuffer::FindInt64(const std::string& name, int64_t& value)
91 {
92 if (int64Map_.count(name) != 0) {
93 value = int64Map_[name];
94 return true;
95 } else {
96 value = 0;
97 return false;
98 }
99 }
100
FindString(const std::string & name,std::string & value)101 bool DataBuffer::FindString(const std::string& name, std::string& value)
102 {
103 if (stringMap_.count(name) != 0) {
104 value = stringMap_[name];
105 return true;
106 } else {
107 value = "";
108 return false;
109 }
110 }
111
GetTimeStamp()112 int64_t DataBuffer::GetTimeStamp()
113 {
114 return frameInfo_.pts;
115 }
116
~DataBuffer()117 DataBuffer::~DataBuffer()
118 {
119 if (data_ != nullptr) {
120 delete[] data_;
121 data_ = nullptr;
122 }
123 }
124 } // namespace DistributedHardware
125 } // namespace OHOS
126