• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "stream_ffi.h"
17 
18 using namespace OHOS::FFI;
19 using namespace OHOS::CJSystemapi;
20 using namespace OHOS::CJSystemapi::FileFs;
21 
22 extern "C" {
FfiOHOSStreamClose(int64_t id)23 RetCode FfiOHOSStreamClose(int64_t id)
24 {
25     LOGI("OHOS::CJSystemapi FfiOHOSStreamClose");
26     auto instance = FFIData::GetData<StreamImpl>(id);
27     if (!instance) {
28         LOGE("Stream instance not exist %{public}" PRId64, id);
29         return ERR_INVALID_INSTANCE_CODE;
30     }
31     return instance->Close();
32 }
33 
FfiOHOSStreamFlush(int64_t id)34 RetCode FfiOHOSStreamFlush(int64_t id)
35 {
36     LOGI("OHOS::CJSystemapi FfiOHOSStreamFlush");
37     auto instance = FFIData::GetData<StreamImpl>(id);
38     if (!instance) {
39         LOGE("Stream instance not exist %{public}" PRId64, id);
40         return ERR_INVALID_INSTANCE_CODE;
41     }
42     return instance->Flush();
43 }
44 
FfiOHOSStreamWriteCur(int64_t id,const char * buffer,int64_t length,const char * encode)45 RetDataI64 FfiOHOSStreamWriteCur(int64_t id, const char* buffer, int64_t length, const char* encode)
46 {
47     LOGI("OHOS::CJSystemapi FfiOHOSStreamWriteCur");
48     RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
49     auto instance = FFIData::GetData<StreamImpl>(id);
50     if (!instance) {
51         LOGE("Stream instance not exist %{public}" PRId64, id);
52         return ret;
53     }
54     auto [state, writeLen] =
55         instance->WriteCur(reinterpret_cast<void *>(const_cast<char*>(buffer)), length, encode);
56     ret.code = state;
57     if (state != SUCCESS_CODE) {
58         ret.data = 0;
59         return ret;
60     }
61     ret.data = writeLen;
62     return ret;
63 }
64 
FfiOHOSStreamWrite(int64_t id,const char * buffer,int64_t length,int64_t offset,const char * encode)65 RetDataI64 FfiOHOSStreamWrite(int64_t id, const char* buffer, int64_t length, int64_t offset, const char* encode)
66 {
67     LOGI("OHOS::CJSystemapi FfiOHOSStreamWriteByString");
68     RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
69     auto instance = FFIData::GetData<StreamImpl>(id);
70     if (!instance) {
71         LOGE("Stream instance not exist %{public}" PRId64, id);
72         return ret;
73     }
74     auto [state, writeLen] =
75         instance->Write(reinterpret_cast<void *>(const_cast<char*>(buffer)), length, offset, encode);
76     ret.code = state;
77     if (state != SUCCESS_CODE) {
78         ret.data = 0;
79         return ret;
80     }
81     ret.data = writeLen;
82     return ret;
83 }
84 
FfiOHOSStreamReadCur(int64_t id,uint8_t * buffer,int64_t bufLen,int64_t length)85 RetDataI64 FfiOHOSStreamReadCur(int64_t id, uint8_t* buffer, int64_t bufLen, int64_t length)
86 {
87     LOGI("OHOS::CJSystemapi FfiOHOSStreamReadCur");
88     RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
89     auto instance = FFIData::GetData<StreamImpl>(id);
90     if (!instance) {
91         LOGE("Stream instance not exist %{public}" PRId64, id);
92         return ret;
93     }
94     auto [state, readLen] = instance->ReadCur(buffer, bufLen, length);
95     ret.code = state;
96     if (state != SUCCESS_CODE) {
97         ret.data = 0;
98         return ret;
99     }
100     ret.data = readLen;
101     LOGI("OHOS::CJSystemapi FfiOHOSStreamReadCur success");
102     return ret;
103 }
104 
FfiOHOSStreamRead(int64_t id,uint8_t * buffer,int64_t bufLen,int64_t length,int64_t offset)105 RetDataI64 FfiOHOSStreamRead(int64_t id, uint8_t* buffer, int64_t bufLen, int64_t length, int64_t offset)
106 {
107     LOGI("OHOS::CJSystemapi FfiOHOSStreamRead");
108     RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
109     auto instance = FFIData::GetData<StreamImpl>(id);
110     if (!instance) {
111         LOGE("Stream instance not exist %{public}" PRId64, id);
112         return ret;
113     }
114     auto [state, readLen] = instance->Read(buffer, bufLen, length, offset);
115     ret.code = state;
116     if (state != SUCCESS_CODE) {
117         ret.data = 0;
118         return ret;
119     }
120     ret.data = readLen;
121     LOGI("OHOS::CJSystemapi FfiOHOSStreamRead success");
122     return ret;
123 }
124 }