1060ff233Sopenharmony_ci/* 2060ff233Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 3060ff233Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4060ff233Sopenharmony_ci * you may not use this file except in compliance with the License. 5060ff233Sopenharmony_ci * You may obtain a copy of the License at 6060ff233Sopenharmony_ci * 7060ff233Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8060ff233Sopenharmony_ci * 9060ff233Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10060ff233Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11060ff233Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12060ff233Sopenharmony_ci * See the License for the specific language governing permissions and 13060ff233Sopenharmony_ci * limitations under the License. 14060ff233Sopenharmony_ci */ 15060ff233Sopenharmony_ci#include "softbus_adapter_hisysevent.h" 16060ff233Sopenharmony_ci 17060ff233Sopenharmony_ci#include <securec.h> 18060ff233Sopenharmony_ci#include <sstream> 19060ff233Sopenharmony_ci 20060ff233Sopenharmony_ci#include "comm_log.h" 21060ff233Sopenharmony_ci#include "hisysevent_c.h" 22060ff233Sopenharmony_ci#include "softbus_adapter_mem.h" 23060ff233Sopenharmony_ci#include "softbus_adapter_thread.h" 24060ff233Sopenharmony_ci#include "softbus_error_code.h" 25060ff233Sopenharmony_ci 26060ff233Sopenharmony_cistatic const char *g_domain = "DSOFTBUS"; 27060ff233Sopenharmony_cistatic bool g_init_lock = false; 28060ff233Sopenharmony_cistatic SoftBusMutex g_dfx_lock; 29060ff233Sopenharmony_cistatic HiSysEventParam g_dstParam[SOFTBUS_EVT_PARAM_BUTT]; 30060ff233Sopenharmony_ci 31060ff233Sopenharmony_cistatic int32_t ConvertToHisEventString(SoftBusEvtParam *srcParam, HiSysEventParam *dstParam) 32060ff233Sopenharmony_ci{ 33060ff233Sopenharmony_ci dstParam->t = HISYSEVENT_STRING; 34060ff233Sopenharmony_ci dstParam->v.s = reinterpret_cast<char *>(SoftBusCalloc(sizeof(char) * SOFTBUS_HISYSEVT_PARAM_LEN)); 35060ff233Sopenharmony_ci if (dstParam->v.s == nullptr) { 36060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "ConvertEventParam: SoftBusMalloc fail"); 37060ff233Sopenharmony_ci return SOFTBUS_MALLOC_ERR; 38060ff233Sopenharmony_ci } 39060ff233Sopenharmony_ci if (strcpy_s(dstParam->v.s, SOFTBUS_HISYSEVT_PARAM_LEN, srcParam->paramValue.str) != EOK) { 40060ff233Sopenharmony_ci SoftBusFree(dstParam->v.s); 41060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "ConvertEventParam:copy string var fail"); 42060ff233Sopenharmony_ci return SOFTBUS_STRCPY_ERR; 43060ff233Sopenharmony_ci } 44060ff233Sopenharmony_ci return SOFTBUS_OK; 45060ff233Sopenharmony_ci} 46060ff233Sopenharmony_ci 47060ff233Sopenharmony_cistatic int32_t ConvertToHisEventUint32Array(SoftBusEvtParam *srcParam, HiSysEventParam *dstParam, uint32_t arraySize) 48060ff233Sopenharmony_ci{ 49060ff233Sopenharmony_ci dstParam->t = HISYSEVENT_UINT32_ARRAY; 50060ff233Sopenharmony_ci dstParam->v.array = reinterpret_cast<uint32_t *>(SoftBusCalloc(arraySize)); 51060ff233Sopenharmony_ci if (dstParam->v.array == nullptr) { 52060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "ConvertEventParam: SoftBusMalloc fail"); 53060ff233Sopenharmony_ci return SOFTBUS_MALLOC_ERR; 54060ff233Sopenharmony_ci } 55060ff233Sopenharmony_ci if (memcpy_s(dstParam->v.array, arraySize, srcParam->paramValue.u32a, arraySize) != EOK) { 56060ff233Sopenharmony_ci SoftBusFree(dstParam->v.array); 57060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "ConvertEventParam:copy uint32 array var fail"); 58060ff233Sopenharmony_ci return SOFTBUS_MEM_ERR; 59060ff233Sopenharmony_ci } 60060ff233Sopenharmony_ci return SOFTBUS_OK; 61060ff233Sopenharmony_ci} 62060ff233Sopenharmony_ci 63060ff233Sopenharmony_cistatic int32_t ConvertEventParam(SoftBusEvtParam *srcParam, HiSysEventParam *dstParam) 64060ff233Sopenharmony_ci{ 65060ff233Sopenharmony_ci uint32_t arraySize = sizeof(uint32_t) * SOFTBUS_HISYSEVT_PARAM_UINT32_ARRAY_SIZE; 66060ff233Sopenharmony_ci switch (srcParam->paramType) { 67060ff233Sopenharmony_ci case SOFTBUS_EVT_PARAMTYPE_BOOL: 68060ff233Sopenharmony_ci dstParam->t = HISYSEVENT_BOOL; 69060ff233Sopenharmony_ci dstParam->v.b = srcParam->paramValue.b; 70060ff233Sopenharmony_ci break; 71060ff233Sopenharmony_ci case SOFTBUS_EVT_PARAMTYPE_UINT8: 72060ff233Sopenharmony_ci dstParam->t = HISYSEVENT_UINT8; 73060ff233Sopenharmony_ci dstParam->v.ui8 = srcParam->paramValue.u8v; 74060ff233Sopenharmony_ci break; 75060ff233Sopenharmony_ci case SOFTBUS_EVT_PARAMTYPE_UINT16: 76060ff233Sopenharmony_ci dstParam->t = HISYSEVENT_UINT16; 77060ff233Sopenharmony_ci dstParam->v.ui16 = srcParam->paramValue.u16v; 78060ff233Sopenharmony_ci break; 79060ff233Sopenharmony_ci case SOFTBUS_EVT_PARAMTYPE_INT32: 80060ff233Sopenharmony_ci dstParam->t = HISYSEVENT_INT32; 81060ff233Sopenharmony_ci dstParam->v.i32 = srcParam->paramValue.i32v; 82060ff233Sopenharmony_ci break; 83060ff233Sopenharmony_ci case SOFTBUS_EVT_PARAMTYPE_UINT32: 84060ff233Sopenharmony_ci dstParam->t = HISYSEVENT_UINT32; 85060ff233Sopenharmony_ci dstParam->v.ui32 = srcParam->paramValue.u32v; 86060ff233Sopenharmony_ci break; 87060ff233Sopenharmony_ci case SOFTBUS_EVT_PARAMTYPE_INT64: 88060ff233Sopenharmony_ci dstParam->t = HISYSEVENT_INT64; 89060ff233Sopenharmony_ci dstParam->v.i64 = srcParam->paramValue.i64v; 90060ff233Sopenharmony_ci break; 91060ff233Sopenharmony_ci case SOFTBUS_EVT_PARAMTYPE_UINT64: 92060ff233Sopenharmony_ci dstParam->t = HISYSEVENT_UINT64; 93060ff233Sopenharmony_ci dstParam->v.ui64 = srcParam->paramValue.u64v; 94060ff233Sopenharmony_ci break; 95060ff233Sopenharmony_ci case SOFTBUS_EVT_PARAMTYPE_FLOAT: 96060ff233Sopenharmony_ci dstParam->t = HISYSEVENT_FLOAT; 97060ff233Sopenharmony_ci dstParam->v.f = srcParam->paramValue.f; 98060ff233Sopenharmony_ci break; 99060ff233Sopenharmony_ci case SOFTBUS_EVT_PARAMTYPE_DOUBLE: 100060ff233Sopenharmony_ci dstParam->t = HISYSEVENT_DOUBLE; 101060ff233Sopenharmony_ci dstParam->v.d = srcParam->paramValue.d; 102060ff233Sopenharmony_ci break; 103060ff233Sopenharmony_ci case SOFTBUS_EVT_PARAMTYPE_STRING: 104060ff233Sopenharmony_ci return ConvertToHisEventString(srcParam, dstParam); 105060ff233Sopenharmony_ci case SOFTBUS_EVT_PARAMTYPE_UINT32_ARRAY: 106060ff233Sopenharmony_ci return ConvertToHisEventUint32Array(srcParam, dstParam, arraySize); 107060ff233Sopenharmony_ci default: 108060ff233Sopenharmony_ci break; 109060ff233Sopenharmony_ci } 110060ff233Sopenharmony_ci return SOFTBUS_OK; 111060ff233Sopenharmony_ci} 112060ff233Sopenharmony_ci 113060ff233Sopenharmony_cistatic int32_t ConvertMsgToHiSysEvent(SoftBusEvtReportMsg *msg) 114060ff233Sopenharmony_ci{ 115060ff233Sopenharmony_ci if (memset_s(g_dstParam, sizeof(HiSysEventParam) * SOFTBUS_EVT_PARAM_BUTT, 0, 116060ff233Sopenharmony_ci sizeof(HiSysEventParam) * SOFTBUS_EVT_PARAM_BUTT) != EOK) { 117060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "init g_dstParam fail"); 118060ff233Sopenharmony_ci return SOFTBUS_ERR; 119060ff233Sopenharmony_ci } 120060ff233Sopenharmony_ci for (uint32_t i = 0; i < msg->paramNum; i++) { 121060ff233Sopenharmony_ci if (strcpy_s(g_dstParam[i].name, MAX_LENGTH_OF_PARAM_NAME, msg->paramArray[i].paramName) != EOK) { 122060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "copy param fail"); 123060ff233Sopenharmony_ci return SOFTBUS_ERR; 124060ff233Sopenharmony_ci } 125060ff233Sopenharmony_ci if (ConvertEventParam(&msg->paramArray[i], &g_dstParam[i]) != SOFTBUS_OK) { 126060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "ConvertMsgToHiSysEvent:convert param fail"); 127060ff233Sopenharmony_ci return SOFTBUS_ERR; 128060ff233Sopenharmony_ci } 129060ff233Sopenharmony_ci } 130060ff233Sopenharmony_ci return SOFTBUS_OK; 131060ff233Sopenharmony_ci} 132060ff233Sopenharmony_ci 133060ff233Sopenharmony_cistatic void HiSysEventParamDeInit(uint32_t size) 134060ff233Sopenharmony_ci{ 135060ff233Sopenharmony_ci for (uint32_t i = 0; i < size; i++) { 136060ff233Sopenharmony_ci if (g_dstParam[i].t == HISYSEVENT_STRING && g_dstParam[i].v.s != nullptr) { 137060ff233Sopenharmony_ci SoftBusFree(g_dstParam[i].v.s); 138060ff233Sopenharmony_ci g_dstParam[i].v.s = nullptr; 139060ff233Sopenharmony_ci } 140060ff233Sopenharmony_ci } 141060ff233Sopenharmony_ci} 142060ff233Sopenharmony_ci 143060ff233Sopenharmony_cistatic HiSysEventEventType ConvertMsgType(SoftBusEvtType type) 144060ff233Sopenharmony_ci{ 145060ff233Sopenharmony_ci HiSysEventEventType hiSysEvtType; 146060ff233Sopenharmony_ci switch (type) { 147060ff233Sopenharmony_ci case SOFTBUS_EVT_TYPE_FAULT: 148060ff233Sopenharmony_ci hiSysEvtType = HISYSEVENT_FAULT; 149060ff233Sopenharmony_ci break; 150060ff233Sopenharmony_ci case SOFTBUS_EVT_TYPE_STATISTIC: 151060ff233Sopenharmony_ci hiSysEvtType = HISYSEVENT_STATISTIC; 152060ff233Sopenharmony_ci break; 153060ff233Sopenharmony_ci case SOFTBUS_EVT_TYPE_SECURITY: 154060ff233Sopenharmony_ci hiSysEvtType = HISYSEVENT_SECURITY; 155060ff233Sopenharmony_ci break; 156060ff233Sopenharmony_ci case SOFTBUS_EVT_TYPE_BEHAVIOR: 157060ff233Sopenharmony_ci hiSysEvtType = HISYSEVENT_BEHAVIOR; 158060ff233Sopenharmony_ci break; 159060ff233Sopenharmony_ci default: 160060ff233Sopenharmony_ci hiSysEvtType = HISYSEVENT_STATISTIC; 161060ff233Sopenharmony_ci break; 162060ff233Sopenharmony_ci } 163060ff233Sopenharmony_ci return hiSysEvtType; 164060ff233Sopenharmony_ci} 165060ff233Sopenharmony_ci 166060ff233Sopenharmony_cistatic void InitHisEvtMutexLock() 167060ff233Sopenharmony_ci{ 168060ff233Sopenharmony_ci if (SoftBusMutexInit(&g_dfx_lock, nullptr) != SOFTBUS_OK) { 169060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "init HisEvtMutexLock fail"); 170060ff233Sopenharmony_ci return; 171060ff233Sopenharmony_ci } 172060ff233Sopenharmony_ci} 173060ff233Sopenharmony_ci 174060ff233Sopenharmony_ci#ifdef __cplusplus 175060ff233Sopenharmony_ci#if __cplusplus 176060ff233Sopenharmony_ciextern "C" { 177060ff233Sopenharmony_ci#endif 178060ff233Sopenharmony_ci#endif 179060ff233Sopenharmony_ci 180060ff233Sopenharmony_ciint32_t SoftbusWriteHisEvt(SoftBusEvtReportMsg *reportMsg) 181060ff233Sopenharmony_ci{ 182060ff233Sopenharmony_ci if (reportMsg == nullptr) { 183060ff233Sopenharmony_ci return SOFTBUS_ERR; 184060ff233Sopenharmony_ci } 185060ff233Sopenharmony_ci if (!g_init_lock) { 186060ff233Sopenharmony_ci InitHisEvtMutexLock(); 187060ff233Sopenharmony_ci g_init_lock = true; 188060ff233Sopenharmony_ci } 189060ff233Sopenharmony_ci if (SoftBusMutexLock(&g_dfx_lock) != 0) { 190060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "lock failed"); 191060ff233Sopenharmony_ci return SOFTBUS_LOCK_ERR; 192060ff233Sopenharmony_ci } 193060ff233Sopenharmony_ci ConvertMsgToHiSysEvent(reportMsg); 194060ff233Sopenharmony_ci OH_HiSysEvent_Write( 195060ff233Sopenharmony_ci g_domain, reportMsg->evtName, ConvertMsgType(reportMsg->evtType), g_dstParam, reportMsg->paramNum); 196060ff233Sopenharmony_ci HiSysEventParamDeInit(reportMsg->paramNum); 197060ff233Sopenharmony_ci (void)SoftBusMutexUnlock(&g_dfx_lock); 198060ff233Sopenharmony_ci return SOFTBUS_OK; 199060ff233Sopenharmony_ci} 200060ff233Sopenharmony_ci 201060ff233Sopenharmony_civoid SoftbusFreeEvtReportMsg(SoftBusEvtReportMsg *msg) 202060ff233Sopenharmony_ci{ 203060ff233Sopenharmony_ci if (msg == nullptr) { 204060ff233Sopenharmony_ci return; 205060ff233Sopenharmony_ci } 206060ff233Sopenharmony_ci if (msg->paramArray != nullptr) { 207060ff233Sopenharmony_ci SoftBusFree(msg->paramArray); 208060ff233Sopenharmony_ci msg->paramArray = nullptr; 209060ff233Sopenharmony_ci } 210060ff233Sopenharmony_ci SoftBusFree(msg); 211060ff233Sopenharmony_ci} 212060ff233Sopenharmony_ci 213060ff233Sopenharmony_ciSoftBusEvtReportMsg *SoftbusCreateEvtReportMsg(int32_t paramNum) 214060ff233Sopenharmony_ci{ 215060ff233Sopenharmony_ci if (paramNum <= SOFTBUS_EVT_PARAM_ZERO || paramNum >= SOFTBUS_EVT_PARAM_BUTT) { 216060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "param is invalid"); 217060ff233Sopenharmony_ci return nullptr; 218060ff233Sopenharmony_ci } 219060ff233Sopenharmony_ci SoftBusEvtReportMsg *msg = reinterpret_cast<SoftBusEvtReportMsg *>(SoftBusCalloc(sizeof(SoftBusEvtReportMsg))); 220060ff233Sopenharmony_ci if (msg == nullptr) { 221060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "report msg is null"); 222060ff233Sopenharmony_ci return nullptr; 223060ff233Sopenharmony_ci } 224060ff233Sopenharmony_ci msg->paramArray = reinterpret_cast<SoftBusEvtParam *>(SoftBusCalloc(sizeof(SoftBusEvtParam) * paramNum)); 225060ff233Sopenharmony_ci if (msg->paramArray == nullptr) { 226060ff233Sopenharmony_ci SoftbusFreeEvtReportMsg(msg); 227060ff233Sopenharmony_ci return nullptr; 228060ff233Sopenharmony_ci } 229060ff233Sopenharmony_ci return msg; 230060ff233Sopenharmony_ci} 231060ff233Sopenharmony_ci 232060ff233Sopenharmony_ci#ifdef __cplusplus 233060ff233Sopenharmony_ci#if __cplusplus 234060ff233Sopenharmony_ci} 235060ff233Sopenharmony_ci#endif /* __cplusplus */ 236060ff233Sopenharmony_ci#endif /* __cplusplus */