• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define SLEEP_TIME (1000 * 500)
31 #define TRY_TIMES 20
32 
OnPeerMsgReceived(const DeviceIdentify * devId,const uint8_t * msg,uint32_t len)33 int32_t OnPeerMsgReceived(const DeviceIdentify *devId, const uint8_t *msg, uint32_t len)
34 {
35     if (devId == NULL || msg == NULL || len == 0) {
36         SECURITY_LOG_ERROR("invalid params, len = %{public}u", len);
37         return ERR_INVALID_PARA;
38     }
39 
40     const MessageBuff buff = {.buff = (uint8_t *)msg, .length = len};
41     int32_t ret = SUCCESS;
42     MessagePacket *packet = ParseMessage(&buff);
43     if (packet == NULL) {
44         SECURITY_LOG_ERROR("packet is null");
45         return ERR_INVALID_PARA;
46     }
47     if (packet->payload == NULL) {
48         FreeMessagePacket(packet);
49         SECURITY_LOG_ERROR("packet->payload is null");
50         return ERR_INVALID_PARA;
51     }
52 
53     switch (packet->type) {
54         case MSG_TYPE_DSLM_CRED_REQUEST:
55             ret = OnPeerMsgRequestInfoReceived(devId, packet->payload, packet->length);
56             break;
57         case MSG_TYPE_DSLM_CRED_RESPONSE:
58             ret = OnPeerMsgResponseInfoReceived(devId, packet->payload, packet->length);
59             break;
60         default:
61             ret = ERR_INVALID_PARA;
62             break;
63     }
64     if (ret != SUCCESS) {
65         SECURITY_LOG_ERROR("ret = %{public}d, packet->type = %{public}u", ret, packet->type);
66     }
67     FreeMessagePacket(packet);
68     return ret;
69 }
70 
OnSendResultNotifier(const DeviceIdentify * devId,uint64_t transNo,uint32_t result)71 int32_t OnSendResultNotifier(const DeviceIdentify *devId, uint64_t transNo, uint32_t result)
72 {
73     return OnMsgSendResultNotifier(devId, transNo, result);
74 }
75 
InitService(void)76 uint32_t InitService(void)
77 {
78     uint32_t times = 0;
79     DslmStartProcessTrace("InitService");
80     uint32_t ret = InitMessenger(OnPeerMsgReceived, OnPeerStatusReceiver, OnSendResultNotifier);
81     if (ret != SUCCESS) {
82         DslmFinishProcessTrace();
83         ReportHiEventServiceStartFailed(ret);
84         SECURITY_LOG_ERROR("InitMessenger ret = %{public}u", ret);
85         return ret;
86     }
87 
88     SECURITY_LOG_INFO("InitService InitMessenger success");
89 
90     while (true) {
91         DslmCountTrace("InitDslmProcess", times);
92         if (InitDslmProcess()) {
93             break;
94         }
95         usleep(SLEEP_TIME);
96         if (times > TRY_TIMES) {
97             SECURITY_LOG_ERROR("wait SoftBus timeout");
98             break;
99         }
100         times++;
101     }
102     DslmFinishProcessTrace();
103 
104     return SUCCESS;
105 }
106 
UnInitService(void)107 void UnInitService(void)
108 {
109     DeinitDslmProcess();
110     DeinitMessenger();
111 }
112