1 /*
2  * Copyright (C) 2022 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 "cellular_data_hisysevent.h"
17 
18 #include "chrono"
19 #include "string"
20 #include "type_traits"
21 #include "apn_manager.h"
22 #include "cellular_data_net_agent.h"
23 
24 namespace OHOS {
25 namespace Telephony {
26 // EVENT
27 static constexpr const char *DATA_CONNECTION_STATE_EVENT = "DATA_CONNECTION_STATE";
28 static constexpr const char *ROAMING_DATA_CONNECTION_STATE_EVENT = "ROAMING_DATA_CONNECTION_STATE";
29 static constexpr const char *DATA_ACTIVATE_FAILED_EVENT = "DATA_ACTIVATE_FAILED";
30 static constexpr const char *DATA_DEACTIVED_EVENT = "DATA_DEACTIVED";
31 static constexpr const char *CELLULAR_REQUEST_EVENT = "CELLULAR_REQUEST";
32 
33 // KEY
34 static constexpr const char *MODULE_NAME_KEY = "MODULE";
35 static constexpr const char *SLOT_ID_KEY = "SLOT_ID";
36 static constexpr const char *SUPPLIER_ID_KEY = "SUPPLIER_ID";
37 static constexpr const char *STATE_KEY = "STATE";
38 static constexpr const char *DATA_SWITCH_KEY = "DATA_SWITCH";
39 static constexpr const char *UPLINK_DATA_KEY = "UPLINK_DATA";
40 static constexpr const char *DOWNLINK_DATA_KEY = "DOWNLINK_DATA";
41 static constexpr const char *DATASTATE_KEY = "DATASTATE";
42 static constexpr const char *ERROR_TYPE_KEY = "ERROR_TYPE";
43 static constexpr const char *ERROR_MSG_KEY = "ERROR_MSG";
44 static constexpr const char *TYPE_KEY = "TYPE";
45 static constexpr const char *APN_TYPE_KEY = "APN_TYPE";
46 static constexpr const char *CALL_UID_KEY = "CALL_UID";
47 static constexpr const char *CALL_PID_KEY = "CALL_PID";
48 static constexpr const char *NAME_KEY = "NAME";
49 static constexpr const char *REQUEST_ID_KEY = "REQUEST_ID";
50 
51 // VALUE
52 static constexpr const char *CELLULAR_DATA_MODULE = "CELLULAR_DATA";
53 static constexpr int32_t NUMBER_MINUS_ONE = -1;
54 
WriteDataDeactiveBehaviorEvent(const int32_t slotId, const DataDisconnectCause type, const std::string &apnType)55 void CellularDataHiSysEvent::WriteDataDeactiveBehaviorEvent(const int32_t slotId, const DataDisconnectCause type,
56     const std::string &apnType)
57 {
58     int32_t bitMap = ApnManager::FindApnTypeByApnName(apnType);
59     HiWriteBehaviorEvent(DATA_DEACTIVED_EVENT, SLOT_ID_KEY, slotId, APN_TYPE_KEY, bitMap,
60         TYPE_KEY, static_cast<int32_t>(type));
61 }
62 
WriteDataConnectStateBehaviorEvent(const int32_t slotId, const std::string &apnType, const uint64_t capability, const int32_t state)63 void CellularDataHiSysEvent::WriteDataConnectStateBehaviorEvent(const int32_t slotId, const std::string &apnType,
64     const uint64_t capability, const int32_t state)
65 {
66     int32_t bitMap = ApnManager::FindApnTypeByApnName(apnType);
67     CellularDataNetAgent &netAgent = CellularDataNetAgent::GetInstance();
68     int32_t supplierId = netAgent.GetSupplierId(slotId, capability);
69     HiWriteBehaviorEvent(DATA_CONNECTION_STATE_EVENT, SLOT_ID_KEY, slotId, APN_TYPE_KEY, bitMap,
70         SUPPLIER_ID_KEY, supplierId, STATE_KEY, state);
71 }
72 
WriteRoamingConnectStateBehaviorEvent(const int32_t state)73 void CellularDataHiSysEvent::WriteRoamingConnectStateBehaviorEvent(const int32_t state)
74 {
75     HiWriteBehaviorEvent(ROAMING_DATA_CONNECTION_STATE_EVENT, STATE_KEY, state);
76 }
77 
WriteCellularRequestBehaviorEvent( const uint32_t uid, const std::string name, const uint64_t type, const int32_t state)78 void CellularDataHiSysEvent::WriteCellularRequestBehaviorEvent(
79     const uint32_t uid, const std::string name, const uint64_t type, const int32_t state)
80 {
81     HiWriteBehaviorEvent(CELLULAR_REQUEST_EVENT, CALL_UID_KEY, static_cast<int32_t>(uid),
82         CALL_PID_KEY, NUMBER_MINUS_ONE, NAME_KEY, name, REQUEST_ID_KEY, NUMBER_MINUS_ONE,
83         TYPE_KEY, static_cast<int32_t>(type), STATE_KEY, state);
84 }
85 
WriteDataActivateFaultEvent( const int32_t slotId, const int32_t switchState, const CellularDataErrorCode errorType, const std::string &errorMsg)86 void CellularDataHiSysEvent::WriteDataActivateFaultEvent(
87     const int32_t slotId, const int32_t switchState, const CellularDataErrorCode errorType, const std::string &errorMsg)
88 {
89     HiWriteFaultEvent(DATA_ACTIVATE_FAILED_EVENT, MODULE_NAME_KEY, CELLULAR_DATA_MODULE, SLOT_ID_KEY, slotId,
90         DATA_SWITCH_KEY, switchState, UPLINK_DATA_KEY, INVALID_PARAMETER, DOWNLINK_DATA_KEY, INVALID_PARAMETER,
91         DATASTATE_KEY, INVALID_PARAMETER, ERROR_TYPE_KEY, static_cast<int32_t>(errorType), ERROR_MSG_KEY, errorMsg);
92 }
93 
SetCellularDataActivateStartTime()94 void CellularDataHiSysEvent::SetCellularDataActivateStartTime()
95 {
96     dataActivateStartTime_ =
97         std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
98             .count();
99 }
100 
JudgingDataActivateTimeOut(const int32_t slotId, const int32_t switchState)101 void CellularDataHiSysEvent::JudgingDataActivateTimeOut(const int32_t slotId, const int32_t switchState)
102 {
103     int64_t dataActivateEndTime =
104         std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
105             .count();
106     if (dataActivateEndTime - dataActivateStartTime_ > DATA_ACTIVATE_TIME) {
107         WriteDataActivateFaultEvent(slotId, switchState, CellularDataErrorCode::DATA_ERROR_DATA_ACTIVATE_TIME_OUT,
108             "data activate time out " + std::to_string(dataActivateEndTime - dataActivateStartTime_));
109     }
110 }
111 } // namespace Telephony
112 } // namespace OHOS
113