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 #include "dslm_rpc_process.h"
16
17 #include <stdbool.h>
18 #include <stddef.h>
19 #include <unistd.h>
20
21 #include "utils_log.h"
22
23 #include "device_security_defines.h"
24 #include "dslm_core_process.h"
25 #include "dslm_hievent.h"
26 #include "dslm_hitrace.h"
27 #include "dslm_messenger_wrapper.h"
28 #include "dslm_msg_serialize.h"
29
30 #ifdef L0_MINI
31 #include "dslm_ohos_credential.h"
32 #endif
33
34 #define SLEEP_TIME (1000 * 500)
35 #define TRY_TIMES 20
36
OnPeerMsgReceived(const DeviceIdentify * devId,const uint8_t * msg,uint32_t len)37 int32_t OnPeerMsgReceived(const DeviceIdentify *devId, const uint8_t *msg, uint32_t len)
38 {
39 if (devId == NULL || msg == NULL || len == 0) {
40 SECURITY_LOG_ERROR("invalid params, len = %{public}u", len);
41 return ERR_INVALID_PARA;
42 }
43
44 const MessageBuff buff = {.buff = (uint8_t *)msg, .length = len};
45 int32_t ret = SUCCESS;
46 MessagePacket *packet = ParseMessage(&buff);
47 if (packet == NULL) {
48 SECURITY_LOG_ERROR("packet is null");
49 return ERR_INVALID_PARA;
50 }
51 if (packet->payload == NULL) {
52 FreeMessagePacket(packet);
53 SECURITY_LOG_ERROR("packet->payload is null");
54 return ERR_INVALID_PARA;
55 }
56
57 switch (packet->type) {
58 case MSG_TYPE_DSLM_CRED_REQUEST:
59 ret = OnPeerMsgRequestInfoReceived(devId, packet->payload, packet->length);
60 break;
61 case MSG_TYPE_DSLM_CRED_RESPONSE:
62 ret = OnPeerMsgResponseInfoReceived(devId, packet->payload, packet->length);
63 break;
64 default:
65 ret = ERR_INVALID_PARA;
66 break;
67 }
68 if (ret != SUCCESS) {
69 SECURITY_LOG_ERROR("ret = %{public}d, packet->type = %{public}u", ret, packet->type);
70 }
71 FreeMessagePacket(packet);
72 return ret;
73 }
74
OnSendResultNotifier(const DeviceIdentify * devId,uint64_t transNo,uint32_t result)75 int32_t OnSendResultNotifier(const DeviceIdentify *devId, uint64_t transNo, uint32_t result)
76 {
77 return OnMsgSendResultNotifier(devId, transNo, result);
78 }
79
InitService(void)80 uint32_t InitService(void)
81 {
82 uint32_t times = 0;
83 SECURITY_LOG_INFO("InitService");
84 #ifdef L0_MINI
85 DslmStartProcessTrace("InitService");
86 DslmCredFunctionsConstructor();
87 InitDeviceManager();
88 #endif
89 uint32_t ret = InitMessenger(OnPeerMsgReceived, OnPeerStatusReceiver, OnSendResultNotifier);
90 if (ret != SUCCESS) {
91 DslmFinishProcessTrace();
92 ReportHiEventServiceStartFailed(ret);
93 SECURITY_LOG_ERROR("InitMessenger ret = %{public}u", ret);
94 return ret;
95 }
96
97 SECURITY_LOG_INFO("InitService InitMessenger success");
98
99 while (true) {
100 DslmCountTrace("InitDslmProcess", times);
101 if (InitDslmProcess()) {
102 break;
103 }
104 usleep(SLEEP_TIME);
105 if (times > TRY_TIMES) {
106 SECURITY_LOG_ERROR("wait SoftBus timeout");
107 break;
108 }
109 times++;
110 }
111 DslmFinishProcessTrace();
112
113 return SUCCESS;
114 }
115
UnInitService(void)116 void UnInitService(void)
117 {
118 DeinitDslmProcess();
119 DeinitMessenger();
120 }
121