15bebb993Sopenharmony_ci/*
25bebb993Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
35bebb993Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45bebb993Sopenharmony_ci * you may not use this file except in compliance with the License.
55bebb993Sopenharmony_ci * You may obtain a copy of the License at
65bebb993Sopenharmony_ci *
75bebb993Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
85bebb993Sopenharmony_ci *
95bebb993Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105bebb993Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115bebb993Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125bebb993Sopenharmony_ci * See the License for the specific language governing permissions and
135bebb993Sopenharmony_ci * limitations under the License.
145bebb993Sopenharmony_ci */
155bebb993Sopenharmony_ci
165bebb993Sopenharmony_ci#include "stream_ffi.h"
175bebb993Sopenharmony_ci
185bebb993Sopenharmony_ciusing namespace OHOS::FFI;
195bebb993Sopenharmony_ciusing namespace OHOS::CJSystemapi;
205bebb993Sopenharmony_ciusing namespace OHOS::CJSystemapi::FileFs;
215bebb993Sopenharmony_ci
225bebb993Sopenharmony_ciextern "C" {
235bebb993Sopenharmony_ciRetCode FfiOHOSStreamClose(int64_t id)
245bebb993Sopenharmony_ci{
255bebb993Sopenharmony_ci    LOGI("OHOS::CJSystemapi FfiOHOSStreamClose");
265bebb993Sopenharmony_ci    auto instance = FFIData::GetData<StreamImpl>(id);
275bebb993Sopenharmony_ci    if (!instance) {
285bebb993Sopenharmony_ci        LOGE("Stream instance not exist %{public}" PRId64, id);
295bebb993Sopenharmony_ci        return ERR_INVALID_INSTANCE_CODE;
305bebb993Sopenharmony_ci    }
315bebb993Sopenharmony_ci    return instance->Close();
325bebb993Sopenharmony_ci}
335bebb993Sopenharmony_ci
345bebb993Sopenharmony_ciRetCode FfiOHOSStreamFlush(int64_t id)
355bebb993Sopenharmony_ci{
365bebb993Sopenharmony_ci    LOGI("OHOS::CJSystemapi FfiOHOSStreamFlush");
375bebb993Sopenharmony_ci    auto instance = FFIData::GetData<StreamImpl>(id);
385bebb993Sopenharmony_ci    if (!instance) {
395bebb993Sopenharmony_ci        LOGE("Stream instance not exist %{public}" PRId64, id);
405bebb993Sopenharmony_ci        return ERR_INVALID_INSTANCE_CODE;
415bebb993Sopenharmony_ci    }
425bebb993Sopenharmony_ci    return instance->Flush();
435bebb993Sopenharmony_ci}
445bebb993Sopenharmony_ci
455bebb993Sopenharmony_ciRetDataI64 FfiOHOSStreamWriteCur(int64_t id, const char* buffer, int64_t length, const char* encode)
465bebb993Sopenharmony_ci{
475bebb993Sopenharmony_ci    LOGI("OHOS::CJSystemapi FfiOHOSStreamWriteCur");
485bebb993Sopenharmony_ci    RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
495bebb993Sopenharmony_ci    auto instance = FFIData::GetData<StreamImpl>(id);
505bebb993Sopenharmony_ci    if (!instance) {
515bebb993Sopenharmony_ci        LOGE("Stream instance not exist %{public}" PRId64, id);
525bebb993Sopenharmony_ci        return ret;
535bebb993Sopenharmony_ci    }
545bebb993Sopenharmony_ci    auto [state, writeLen] = instance->WriteCur(buffer, length, encode);
555bebb993Sopenharmony_ci    ret.code = state;
565bebb993Sopenharmony_ci    if (state != SUCCESS_CODE) {
575bebb993Sopenharmony_ci        ret.data = 0;
585bebb993Sopenharmony_ci        return ret;
595bebb993Sopenharmony_ci    }
605bebb993Sopenharmony_ci    ret.data = writeLen;
615bebb993Sopenharmony_ci    return ret;
625bebb993Sopenharmony_ci}
635bebb993Sopenharmony_ci
645bebb993Sopenharmony_ciRetDataI64 FfiOHOSStreamWrite(int64_t id, const char* buffer, int64_t length, int64_t offset, const char* encode)
655bebb993Sopenharmony_ci{
665bebb993Sopenharmony_ci    LOGI("OHOS::CJSystemapi FfiOHOSStreamWriteByString");
675bebb993Sopenharmony_ci    RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
685bebb993Sopenharmony_ci    auto instance = FFIData::GetData<StreamImpl>(id);
695bebb993Sopenharmony_ci    if (!instance) {
705bebb993Sopenharmony_ci        LOGE("Stream instance not exist %{public}" PRId64, id);
715bebb993Sopenharmony_ci        return ret;
725bebb993Sopenharmony_ci    }
735bebb993Sopenharmony_ci    auto [state, writeLen] = instance->Write(buffer, length, offset, encode);
745bebb993Sopenharmony_ci    ret.code = state;
755bebb993Sopenharmony_ci    if (state != SUCCESS_CODE) {
765bebb993Sopenharmony_ci        ret.data = 0;
775bebb993Sopenharmony_ci        return ret;
785bebb993Sopenharmony_ci    }
795bebb993Sopenharmony_ci    ret.data = writeLen;
805bebb993Sopenharmony_ci    return ret;
815bebb993Sopenharmony_ci}
825bebb993Sopenharmony_ci
835bebb993Sopenharmony_ciRetDataI64 FfiOHOSStreamReadCur(int64_t id, uint8_t* buffer, int64_t bufLen, int64_t length)
845bebb993Sopenharmony_ci{
855bebb993Sopenharmony_ci    LOGI("OHOS::CJSystemapi FfiOHOSStreamReadCur");
865bebb993Sopenharmony_ci    RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
875bebb993Sopenharmony_ci    auto instance = FFIData::GetData<StreamImpl>(id);
885bebb993Sopenharmony_ci    if (!instance) {
895bebb993Sopenharmony_ci        LOGE("Stream instance not exist %{public}" PRId64, id);
905bebb993Sopenharmony_ci        return ret;
915bebb993Sopenharmony_ci    }
925bebb993Sopenharmony_ci    auto [state, readLen] = instance->ReadCur(buffer, bufLen, length);
935bebb993Sopenharmony_ci    ret.code = state;
945bebb993Sopenharmony_ci    if (state != SUCCESS_CODE) {
955bebb993Sopenharmony_ci        ret.data = 0;
965bebb993Sopenharmony_ci        return ret;
975bebb993Sopenharmony_ci    }
985bebb993Sopenharmony_ci    ret.data = readLen;
995bebb993Sopenharmony_ci    LOGI("OHOS::CJSystemapi FfiOHOSStreamReadCur success");
1005bebb993Sopenharmony_ci    return ret;
1015bebb993Sopenharmony_ci}
1025bebb993Sopenharmony_ci
1035bebb993Sopenharmony_ciRetDataI64 FfiOHOSStreamRead(int64_t id, uint8_t* buffer, int64_t bufLen, int64_t length, int64_t offset)
1045bebb993Sopenharmony_ci{
1055bebb993Sopenharmony_ci    LOGI("OHOS::CJSystemapi FfiOHOSStreamRead");
1065bebb993Sopenharmony_ci    RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
1075bebb993Sopenharmony_ci    auto instance = FFIData::GetData<StreamImpl>(id);
1085bebb993Sopenharmony_ci    if (!instance) {
1095bebb993Sopenharmony_ci        LOGE("Stream instance not exist %{public}" PRId64, id);
1105bebb993Sopenharmony_ci        return ret;
1115bebb993Sopenharmony_ci    }
1125bebb993Sopenharmony_ci    auto [state, readLen] = instance->Read(buffer, bufLen, length, offset);
1135bebb993Sopenharmony_ci    ret.code = state;
1145bebb993Sopenharmony_ci    if (state != SUCCESS_CODE) {
1155bebb993Sopenharmony_ci        ret.data = 0;
1165bebb993Sopenharmony_ci        return ret;
1175bebb993Sopenharmony_ci    }
1185bebb993Sopenharmony_ci    ret.data = readLen;
1195bebb993Sopenharmony_ci    LOGI("OHOS::CJSystemapi FfiOHOSStreamRead success");
1205bebb993Sopenharmony_ci    return ret;
1215bebb993Sopenharmony_ci}
1225bebb993Sopenharmony_ci}