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 18using namespace OHOS::FFI; 19using namespace OHOS::CJSystemapi; 20using namespace OHOS::CJSystemapi::FileFs; 21 22extern "C" { 23RetCode 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 34RetCode 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 45RetDataI64 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] = instance->WriteCur(buffer, length, encode); 55 ret.code = state; 56 if (state != SUCCESS_CODE) { 57 ret.data = 0; 58 return ret; 59 } 60 ret.data = writeLen; 61 return ret; 62} 63 64RetDataI64 FfiOHOSStreamWrite(int64_t id, const char* buffer, int64_t length, int64_t offset, const char* encode) 65{ 66 LOGI("OHOS::CJSystemapi FfiOHOSStreamWriteByString"); 67 RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 }; 68 auto instance = FFIData::GetData<StreamImpl>(id); 69 if (!instance) { 70 LOGE("Stream instance not exist %{public}" PRId64, id); 71 return ret; 72 } 73 auto [state, writeLen] = instance->Write(buffer, length, offset, encode); 74 ret.code = state; 75 if (state != SUCCESS_CODE) { 76 ret.data = 0; 77 return ret; 78 } 79 ret.data = writeLen; 80 return ret; 81} 82 83RetDataI64 FfiOHOSStreamReadCur(int64_t id, uint8_t* buffer, int64_t bufLen, int64_t length) 84{ 85 LOGI("OHOS::CJSystemapi FfiOHOSStreamReadCur"); 86 RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 }; 87 auto instance = FFIData::GetData<StreamImpl>(id); 88 if (!instance) { 89 LOGE("Stream instance not exist %{public}" PRId64, id); 90 return ret; 91 } 92 auto [state, readLen] = instance->ReadCur(buffer, bufLen, length); 93 ret.code = state; 94 if (state != SUCCESS_CODE) { 95 ret.data = 0; 96 return ret; 97 } 98 ret.data = readLen; 99 LOGI("OHOS::CJSystemapi FfiOHOSStreamReadCur success"); 100 return ret; 101} 102 103RetDataI64 FfiOHOSStreamRead(int64_t id, uint8_t* buffer, int64_t bufLen, int64_t length, int64_t offset) 104{ 105 LOGI("OHOS::CJSystemapi FfiOHOSStreamRead"); 106 RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 }; 107 auto instance = FFIData::GetData<StreamImpl>(id); 108 if (!instance) { 109 LOGE("Stream instance not exist %{public}" PRId64, id); 110 return ret; 111 } 112 auto [state, readLen] = instance->Read(buffer, bufLen, length, offset); 113 ret.code = state; 114 if (state != SUCCESS_CODE) { 115 ret.data = 0; 116 return ret; 117 } 118 ret.data = readLen; 119 LOGI("OHOS::CJSystemapi FfiOHOSStreamRead success"); 120 return ret; 121} 122}