• 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 "sms_broadcast_subscriber.h"
17 
18 #include "common_event_support.h"
19 #include "short_message.h"
20 #include "sms_cb_data.h"
21 #include "string_utils.h"
22 
23 namespace OHOS {
24 namespace Telephony {
25 using namespace OHOS::EventFwk;
SmsBroadcastSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo & subscriberInfo)26 SmsBroadcastSubscriber::SmsBroadcastSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo)
27     : CommonEventSubscriber(subscriberInfo)
28 {}
29 
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & data)30 void SmsBroadcastSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data)
31 {
32     std::cout << "CommonEventPublishOrderedEventTest::Subscriber OnReceiveEvent" << std::endl;
33     OHOS::EventFwk::Want want = data.GetWant();
34     std::string action = data.GetWant().GetAction();
35     if (action == CommonEventSupport::COMMON_EVENT_SMS_RECEIVE_COMPLETED) {
36         int msgcode = GetCode();
37         std::string msgdata = GetData();
38         bool isCdma = want.GetBoolParam("isCdma", false);
39         std::cout << "Sms Receive::OnReceiveEvent msgcode" << msgcode << std::endl;
40         std::cout << "Sms Receive::OnReceiveEvent data = " << msgdata.data() << std::endl;
41         std::cout << "Sms Receive::OnReceiveEvent format Type = " << (isCdma ? "Cdma" : "Gsm") << std::endl;
42         const std::vector<std::string> pdus = want.GetStringArrayParam("pdus");
43         for (unsigned int index = 0; index < pdus.size(); ++index) {
44             std::vector<unsigned char> pdu = StringUtils::HexToByteVector(pdus[index]);
45             auto message = new ShortMessage();
46             std::u16string netType = isCdma ? u"3gpp2" : u"3gpp";
47             ShortMessage::CreateMessage(pdu, netType, *message);
48             if (message != nullptr) {
49                 std::string messageBody = StringUtils::ToUtf8(message->GetVisibleMessageBody());
50                 std::cout << "receive new sms = " << messageBody.c_str() << std::endl;
51                 delete message;
52                 message = nullptr;
53             } else {
54                 std::cout << "Sms Receive::OnReceiveEvent pdus = " << pdus[index] << std::endl;
55             }
56         }
57     } else if (action == CommonEventSupport::COMMON_EVENT_SMS_EMERGENCY_CB_RECEIVE_COMPLETED ||
58         action == CommonEventSupport::COMMON_EVENT_SMS_CB_RECEIVE_COMPLETED) {
59         CbMessageTest(want);
60     } else if (action == CommonEventSupport::COMMON_EVENT_SMS_WAPPUSH_RECEIVE_COMPLETED) {
61         WapPushMessageTest(want);
62     } else {
63         std::cout << "CommonEventPublishOrderedEventTest::Subscriber OnReceiveEvent do nothing" << std::endl;
64     }
65 }
66 
CbMessageTest(const OHOS::EventFwk::Want & want) const67 void SmsBroadcastSubscriber::CbMessageTest(const OHOS::EventFwk::Want &want) const
68 {
69     std::cout << SmsCbData::SLOT_ID << ":" << want.GetIntParam(SmsCbData::SLOT_ID, DEFAULT_VALUE) << std::endl;
70     std::cout << SmsCbData::CB_MSG_TYPE << ":"
71               << std::to_string(static_cast<uint8_t>(want.GetByteParam(SmsCbData::CB_MSG_TYPE, DEFAULT_VALUE)))
72               << std::endl;
73     std::cout << SmsCbData::LANG_TYPE << ":"
74               << std::to_string(static_cast<uint8_t>(want.GetByteParam(SmsCbData::LANG_TYPE, DEFAULT_VALUE)))
75               << std::endl;
76     std::cout << SmsCbData::DCS << ":"
77               << std::to_string(static_cast<uint8_t>(want.GetByteParam(SmsCbData::DCS, DEFAULT_VALUE)))
78               << std::endl;
79     std::cout << SmsCbData::PRIORITY << ":"
80               << std::to_string(want.GetByteParam(SmsCbData::PRIORITY, DEFAULT_VALUE)) << std::endl;
81     std::cout << SmsCbData::CMAS_CLASS << ":"
82               << std::to_string(want.GetByteParam(SmsCbData::CMAS_CLASS, DEFAULT_VALUE)) << std::endl;
83     std::cout << SmsCbData::CMAS_CATEGORY << ":"
84               << std::to_string(want.GetByteParam(SmsCbData::CMAS_CATEGORY, DEFAULT_VALUE)) << std::endl;
85     std::cout << SmsCbData::CMAS_RESPONSE << ":"
86               << std::to_string(want.GetByteParam(SmsCbData::CMAS_RESPONSE, DEFAULT_VALUE)) << std::endl;
87     std::cout << SmsCbData::SEVERITY << ":"
88               << std::to_string(want.GetByteParam(SmsCbData::SEVERITY, DEFAULT_VALUE)) << std::endl;
89     std::cout << SmsCbData::URGENCY << ":" << std::to_string(want.GetByteParam(SmsCbData::URGENCY, DEFAULT_VALUE))
90               << std::endl;
91     std::cout << SmsCbData::CERTAINTY << ":"
92               << std::to_string(want.GetByteParam(SmsCbData::CERTAINTY, DEFAULT_VALUE)) << std::endl;
93     std::cout << SmsCbData::MSG_BODY << ":" << want.GetStringParam(SmsCbData::MSG_BODY) << std::endl;
94     std::cout << SmsCbData::FORMAT << ":" << std::to_string(want.GetByteParam(SmsCbData::FORMAT, DEFAULT_VALUE))
95               << std::endl;
96     std::cout << SmsCbData::SERIAL_NUM << ":"
97               << want.GetIntParam(SmsCbData::SERIAL_NUM, DEFAULT_VALUE) << std::endl;
98     std::cout << SmsCbData::RECV_TIME << ":" << want.GetStringParam(SmsCbData::RECV_TIME) << std::endl;
99     std::cout << SmsCbData::MSG_ID << ":"
100               << want.GetIntParam(SmsCbData::MSG_ID, DEFAULT_VALUE) << std::endl;
101     std::cout << SmsCbData::SERVICE_CATEGORY << ":"
102               << want.GetIntParam(SmsCbData::SERVICE_CATEGORY, DEFAULT_VALUE) << std::endl;
103     std::cout << SmsCbData::IS_ETWS_PRIMARY << ":" << want.GetBoolParam(SmsCbData::IS_ETWS_PRIMARY, false)
104               << std::endl;
105     std::cout << SmsCbData::IS_CMAS_MESSAGE << ":" << want.GetBoolParam(SmsCbData::IS_CMAS_MESSAGE, false)
106               << std::endl;
107     std::cout << SmsCbData::IS_ETWS_MESSAGE << ":" << want.GetBoolParam(SmsCbData::IS_ETWS_MESSAGE, false)
108               << std::endl;
109     std::cout << SmsCbData::PLMN << ":" << want.GetStringParam(SmsCbData::PLMN) << std::endl;
110     std::cout << SmsCbData::LAC << ":" << want.GetIntParam(SmsCbData::LAC, DEFAULT_VALUE) << std::endl;
111     std::cout << SmsCbData::CID << ":" << want.GetIntParam(SmsCbData::CID, DEFAULT_VALUE) << std::endl;
112     std::cout << SmsCbData::WARNING_TYPE << ":"
113               << want.GetIntParam(SmsCbData::WARNING_TYPE, DEFAULT_VALUE) << std::endl;
114     std::cout << SmsCbData::GEO_SCOPE << ":"
115               << std::to_string(want.GetByteParam(SmsCbData::GEO_SCOPE, DEFAULT_VALUE)) << std::endl;
116 }
117 
WapPushMessageTest(const OHOS::EventFwk::Want & want) const118 void SmsBroadcastSubscriber::WapPushMessageTest(const OHOS::EventFwk::Want &want) const
119 {
120     std::cout << "wap push slotId:" << want.GetIntParam("slotId", 0) << std::endl;
121     std::cout << "wap push pushType:" << want.GetIntParam("pushType", 0) << std::endl;
122     std::cout << "wap push transactionId:" << want.GetIntParam("transactionId", 0) << std::endl;
123     std::cout << "wap push strAppId:" << want.GetStringParam("strAppId") << std::endl;
124     std::cout << "wap push contentType:" << want.GetStringParam("contentType") << std::endl;
125     std::cout << "wap push header:" << want.GetStringParam("headerData") << std::endl;
126     std::cout << "wap push data:" << want.GetStringParam("rawData") << std::endl;
127 }
128 } // namespace Telephony
129 } // namespace OHOS
130