• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "telephony_observer.h"
17 
18 #include "telephony_errors.h"
19 #include "telephony_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace Telephony {
OnCallStateUpdated(int32_t slotId,int32_t callState,const std::u16string & phoneNumber)23 void TelephonyObserver::OnCallStateUpdated(
24     int32_t slotId, int32_t callState, const std::u16string &phoneNumber) {}
25 
OnSignalInfoUpdated(int32_t slotId,const std::vector<sptr<SignalInformation>> & vec)26 void TelephonyObserver::OnSignalInfoUpdated(
27     int32_t slotId, const std::vector<sptr<SignalInformation>> &vec) {}
28 
OnNetworkStateUpdated(int32_t slotId,const sptr<NetworkState> & networkState)29 void TelephonyObserver::OnNetworkStateUpdated(
30     int32_t slotId, const sptr<NetworkState> &networkState) {}
31 
OnCellInfoUpdated(int32_t slotId,const std::vector<sptr<CellInformation>> & vec)32 void TelephonyObserver::OnCellInfoUpdated(
33     int32_t slotId, const std::vector<sptr<CellInformation>> &vec) {}
34 
OnSimStateUpdated(int32_t slotId,CardType type,SimState state,LockReason reason)35 void TelephonyObserver::OnSimStateUpdated(
36     int32_t slotId, CardType type, SimState state, LockReason reason) {}
37 
OnCellularDataConnectStateUpdated(int32_t slotId,int32_t dataState,int32_t networkType)38 void TelephonyObserver::OnCellularDataConnectStateUpdated(
39     int32_t slotId, int32_t dataState, int32_t networkType) {}
40 
OnCellularDataFlowUpdated(int32_t slotId,int32_t dataFlowType)41 void TelephonyObserver::OnCellularDataFlowUpdated(
42     int32_t slotId, int32_t dataFlowType) {}
43 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)44 int32_t TelephonyObserver::OnRemoteRequest(
45     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
46 {
47     TELEPHONY_LOGI("TelephonyObserver::OnRemoteRequest code = %{public}u......\n", code);
48     if (data.ReadInterfaceToken() != GetDescriptor()) {
49         TELEPHONY_LOGE("TelephonyObserver::OnRemoteRequest verify token failed!");
50         return TELEPHONY_ERR_DESCRIPTOR_MISMATCH;
51     }
52     switch (static_cast<ObserverBrokerCode>(code)) {
53         case ObserverBrokerCode::ON_CALL_STATE_UPDATED: {
54             OnCallStateUpdatedInner(data, reply);
55             break;
56         }
57         case ObserverBrokerCode::ON_SIGNAL_INFO_UPDATED: {
58             OnSignalInfoUpdatedInner(data, reply);
59             break;
60         }
61         case ObserverBrokerCode::ON_CELL_INFO_UPDATED: {
62             OnCellInfoUpdatedInner(data, reply);
63             break;
64         }
65         case ObserverBrokerCode::ON_NETWORK_STATE_UPDATED: {
66             OnNetworkStateUpdatedInner(data, reply);
67             break;
68         }
69         case ObserverBrokerCode::ON_SIM_STATE_UPDATED: {
70             OnSimStateUpdatedInner(data, reply);
71             break;
72         }
73         case ObserverBrokerCode::ON_CELLULAR_DATA_CONNECT_STATE_UPDATED: {
74             OnCellularDataConnectStateUpdatedInner(data, reply);
75             break;
76         }
77         case ObserverBrokerCode::ON_CELLULAR_DATA_FLOW_UPDATED: {
78             OnCellularDataFlowUpdatedInner(data, reply);
79             break;
80         }
81         default: {
82             return OHOS::UNKNOWN_TRANSACTION;
83         }
84     }
85     return OHOS::NO_ERROR;
86 }
87 
OnCallStateUpdatedInner(MessageParcel & data,MessageParcel & reply)88 void TelephonyObserver::OnCallStateUpdatedInner(
89     MessageParcel &data, MessageParcel &reply)
90 {
91     int32_t slotId = data.ReadInt32();
92     int32_t callState = data.ReadInt32();
93     std::u16string phoneNumber = data.ReadString16();
94     OnCallStateUpdated(slotId, callState, phoneNumber);
95 }
96 
OnSignalInfoUpdatedInner(MessageParcel & data,MessageParcel & reply)97 void TelephonyObserver::OnSignalInfoUpdatedInner(
98     MessageParcel &data, MessageParcel &reply)
99 {
100     int32_t slotId = data.ReadInt32();
101     std::vector<sptr<SignalInformation>> signalInfos;
102     ConvertSignalInfoList(data, signalInfos);
103     OnSignalInfoUpdated(slotId, signalInfos);
104 }
105 
OnNetworkStateUpdatedInner(MessageParcel & data,MessageParcel & reply)106 void TelephonyObserver::OnNetworkStateUpdatedInner(
107     MessageParcel &data, MessageParcel &reply)
108 {
109     int32_t slotId = data.ReadInt32();
110     sptr<NetworkState> networkState = NetworkState::Unmarshalling(data);
111     OnNetworkStateUpdated(slotId, networkState);
112 }
113 
OnCellInfoUpdatedInner(MessageParcel & data,MessageParcel & reply)114 void TelephonyObserver::OnCellInfoUpdatedInner(
115     MessageParcel &data, MessageParcel &reply)
116 {
117     int32_t slotId = data.ReadInt32();
118     std::vector<sptr<CellInformation>> cells;
119     ConvertCellInfoList(data, cells);
120     OnCellInfoUpdated(slotId, cells);
121 }
122 
OnSimStateUpdatedInner(MessageParcel & data,MessageParcel & reply)123 void TelephonyObserver::OnSimStateUpdatedInner(
124     MessageParcel &data, MessageParcel &reply)
125 {
126     int32_t slotId = data.ReadInt32();
127     CardType type = static_cast<CardType>(data.ReadInt32());
128     SimState simState = static_cast<SimState>(data.ReadInt32());
129     LockReason reson = static_cast<LockReason>(data.ReadInt32());
130     OnSimStateUpdated(slotId, type, simState, reson);
131 }
132 
OnCellularDataConnectStateUpdatedInner(MessageParcel & data,MessageParcel & reply)133 void TelephonyObserver::OnCellularDataConnectStateUpdatedInner(
134     MessageParcel &data, MessageParcel &reply)
135 {
136     int32_t slotId = data.ReadInt32();
137     int32_t dataState = data.ReadInt32();
138     int32_t networkType = data.ReadInt32();
139     OnCellularDataConnectStateUpdated(slotId, dataState, networkType);
140 }
141 
OnCellularDataFlowUpdatedInner(MessageParcel & data,MessageParcel & reply)142 void TelephonyObserver::OnCellularDataFlowUpdatedInner(
143     MessageParcel &data, MessageParcel &reply)
144 {
145     int32_t slotId = data.ReadInt32();
146     int32_t flowType = data.ReadInt32();
147     OnCellularDataFlowUpdated(slotId, flowType);
148 }
149 
ConvertSignalInfoList(MessageParcel & data,std::vector<sptr<SignalInformation>> & result)150 void TelephonyObserver::ConvertSignalInfoList(
151     MessageParcel &data, std::vector<sptr<SignalInformation>> &result)
152 {
153     int32_t size = data.ReadInt32();
154     size = (size > SIGNAL_NUM_MAX) ? SIGNAL_NUM_MAX : size;
155     SignalInformation::NetworkType type;
156     for (int i = 0; i < size; ++i) {
157         type = static_cast<SignalInformation::NetworkType>(data.ReadInt32());
158         switch (type) {
159             case SignalInformation::NetworkType::GSM: {
160                 std::unique_ptr<GsmSignalInformation> signal = std::make_unique<GsmSignalInformation>();
161                 if (signal != nullptr) {
162                     signal->ReadFromParcel(data);
163                     result.emplace_back(signal.release());
164                 }
165                 break;
166             }
167             case SignalInformation::NetworkType::CDMA: {
168                 std::unique_ptr<CdmaSignalInformation> signal = std::make_unique<CdmaSignalInformation>();
169                 if (signal != nullptr) {
170                     signal->ReadFromParcel(data);
171                     result.emplace_back(signal.release());
172                 }
173                 break;
174             }
175             case SignalInformation::NetworkType::LTE: {
176                 std::unique_ptr<LteSignalInformation> signal = std::make_unique<LteSignalInformation>();
177                 if (signal != nullptr) {
178                     signal->ReadFromParcel(data);
179                     result.emplace_back(signal.release());
180                 }
181                 break;
182             }
183             case SignalInformation::NetworkType::WCDMA: {
184                 std::unique_ptr<WcdmaSignalInformation> signal = std::make_unique<WcdmaSignalInformation>();
185                 if (signal != nullptr) {
186                     signal->ReadFromParcel(data);
187                     result.emplace_back(signal.release());
188                 }
189                 break;
190             }
191             default:
192                 break;
193         }
194     }
195 }
196 
ConvertCellInfoList(MessageParcel & data,std::vector<sptr<CellInformation>> & cells)197 void TelephonyObserver::ConvertCellInfoList(
198     MessageParcel &data, std::vector<sptr<CellInformation>> &cells)
199 {
200     int32_t size = data.ReadInt32();
201     size = (size > CELL_NUM_MAX) ? CELL_NUM_MAX : size;
202     CellInformation::CellType type;
203     for (int i = 0; i < size; ++i) {
204         type = static_cast<CellInformation::CellType>(data.ReadInt32());
205         switch (type) {
206             case CellInformation::CellType::CELL_TYPE_GSM: {
207                 std::unique_ptr<GsmCellInformation> cell = std::make_unique<GsmCellInformation>();
208                 if (cell != nullptr) {
209                     cell->ReadFromParcel(data);
210                     cells.emplace_back(cell.release());
211                 }
212                 break;
213             }
214             case CellInformation::CellType::CELL_TYPE_LTE: {
215                 std::unique_ptr<LteCellInformation> cell = std::make_unique<LteCellInformation>();
216                 if (cell != nullptr) {
217                     cell->ReadFromParcel(data);
218                     cells.emplace_back(cell.release());
219                 }
220                 break;
221             }
222             default:
223                 break;
224         }
225     }
226 }
227 } // namespace Telephony
228 } // namespace OHOS
229