• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "uds_client.h"
17 
18 #undef MMI_LOG_TAG
19 #define MMI_LOG_TAG "UDSClient"
20 
21 namespace OHOS {
22 namespace MMI {
UDSClient()23 UDSClient::UDSClient()
24 {
25     CALL_DEBUG_ENTER;
26 }
27 
~UDSClient()28 UDSClient::~UDSClient()
29 {
30     CALL_DEBUG_ENTER;
31 }
32 
ConnectTo()33 int32_t UDSClient::ConnectTo()
34 {
35     CALL_DEBUG_ENTER;
36     if (Socket() < 0) {
37         MMI_HILOGE("Socket failed");
38         return RET_ERR;
39     }
40     OnConnected();
41     return RET_OK;
42 }
43 
SendMsg(const char * buf,size_t size) const44 bool UDSClient::SendMsg(const char *buf, size_t size) const
45 {
46     CHKPF(buf);
47     if ((size == 0) || (size > MAX_PACKET_BUF_SIZE)) {
48         MMI_HILOGE("Stream buffer size out of range");
49         return false;
50     }
51     if (fd_ < 0) {
52         MMI_HILOGE("The fd_ is less than 0");
53         return false;
54     }
55 
56     int32_t idx = 0;
57     int32_t retryCount = 0;
58     const int32_t bufSize = static_cast<int32_t>(size);
59     int32_t remSize = bufSize;
60     while (remSize > 0 && retryCount < SEND_RETRY_LIMIT) {
61         retryCount += 1;
62         auto count = send(fd_, &buf[idx], remSize, MSG_DONTWAIT | MSG_NOSIGNAL);
63         if (count < 0) {
64             if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK) {
65                 MMI_HILOGW("Continue for errno EAGAIN|EINTR|EWOULDBLOCK, errno:%{public}d", errno);
66                 continue;
67             }
68             MMI_HILOGE("Send return failed,error:%{public}d fd:%{public}d", errno, fd_);
69             return false;
70         }
71         idx += count;
72         remSize -= count;
73         if (remSize > 0) {
74             MMI_HILOGW("Remsize:%{public}d", remSize);
75             usleep(SEND_RETRY_SLEEP_TIME);
76         }
77     }
78     if (retryCount >= SEND_RETRY_LIMIT || remSize != 0) {
79         MMI_HILOGE("Send too many times:%{public}d/%{public}d,size:%{public}d/%{public}d fd:%{public}d",
80             retryCount, SEND_RETRY_LIMIT, idx, bufSize, fd_);
81         return false;
82     }
83     return true;
84 }
85 
SendMsg(const NetPacket & pkt) const86 bool UDSClient::SendMsg(const NetPacket &pkt) const
87 {
88     if (pkt.ChkRWError()) {
89         MMI_HILOGE("Read and write status is error");
90         return false;
91     }
92     StreamBuffer buf;
93     pkt.MakeData(buf);
94     return SendMsg(buf.Data(), buf.Size());
95 }
96 
StartClient(MsgClientFunCallback fun)97 bool UDSClient::StartClient(MsgClientFunCallback fun)
98 {
99     CALL_DEBUG_ENTER;
100     if (isRunning_ || isConnected_) {
101         MMI_HILOGE("Client is connected or started");
102         return false;
103     }
104     isExit = false;
105     recvFun_ = fun;
106     if (ConnectTo() < 0) {
107         MMI_HILOGW("Client connection failed, Try again later");
108         return false;
109     }
110     return true;
111 }
112 
Stop()113 void UDSClient::Stop()
114 {
115     CALL_DEBUG_ENTER;
116     isExit = true;
117     isRunning_ = false;
118     Close();
119 }
120 } // namespace MMI
121 } // namespace OHOS