blob: 5ea6be1c2b5562d34e54913e584ac04b076611dd [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/* Copyright Statement:
2 *
3 * This software/firmware and related documentation ("MediaTek Software") are
4 * protected under relevant copyright laws. The information contained herein is
5 * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
6 * the prior written permission of MediaTek inc. and/or its licensors, any
7 * reproduction, modification, use or disclosure of MediaTek Software, and
8 * information contained herein, in whole or in part, shall be strictly
9 * prohibited.
10 *
11 * MediaTek Inc. (C) 2016~2017. All rights reserved.
12 *
13 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
14 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
15 * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
16 * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
17 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19 * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
20 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
21 * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
22 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
23 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
24 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
25 * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
26 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
27 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
28 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
29 * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
30 * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
31 * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
32 *
33 * The following software/firmware and/or related documentation ("MediaTek
34 * Software") have been modified by MediaTek Inc. All revisions are subject to
35 * any receiver's applicable license agreements with MediaTek Inc.
36 */
37
38//- vim: set ts=4 sts=4 sw=4 et: --------------------------------------------
39#include <errno.h>
40#include <string.h>
41#include <stddef.h>
42#include <unistd.h>
43
44#include "boots.h"
45#include "boots_skt.h"
46
47//---------------------------------------------------------------------------
48#define LOG_TAG "boots_skt"
49
50//---------------------------------------------------------------------------
51int boots_sk_create(int * sk, struct sockaddr_un * sockaddr, socklen_t * socklen,
52 char * name, int client)
53{
54 UNUSED(boots_btif);
55 int retry_counter = 10;
56
57 // Create server socket
58 *sk = socket(client ? PF_LOCAL : AF_UNIX, SOCK_DGRAM, 0);
59 if (*sk < 0) {
60 BPRINT_E("Create socket failed: %s %s(%d)", name, strerror(errno), errno);
61 return errno;
62 }
63
64 // Init server socket
65 sockaddr->sun_family = AF_UNIX;
66 memset(sockaddr->sun_path, 0, sizeof(sockaddr->sun_path));
67 strncpy(sockaddr->sun_path, name, strlen(name));
68 *socklen = (offsetof(struct sockaddr_un, sun_path) + strlen(sockaddr->sun_path) + 1);
69
70 if (!client) {
71 // Remove the socket first
72 unlink(sockaddr->sun_path);
73 if (bind(*sk, (struct sockaddr *)sockaddr, *socklen) < 0) {
74 BPRINT_E("Bind socket failed: %s(%d)", strerror(errno), errno);
75 return errno;
76 }
77
78 } else {
79 int memsize = HCI_BUF_SIZE;
80 if (setsockopt(*sk, SOL_SOCKET, SO_RCVBUF, &memsize, sizeof(memsize)) < 0)
81 BPRINT_E("Set socket buffer size failed: %s(%d)", strerror(errno), errno);
82
83 while (retry_counter > 0) {
84 retry_counter--;
85 if (connect(*sk, (struct sockaddr *)sockaddr, *socklen) < 0) {
86 if (retry_counter > 0) {
87 BPRINT_W("Failed to connect to socket: %s(%d). Retry...", strerror(errno), errno);
88 sleep(1);
89 continue;
90 } else {
91 BPRINT_E("Failed to connect to socket: %s(%d). Stop Retry!", strerror(errno), errno);
92 return errno;
93 }
94 } else {
95 break;
96 }
97 }
98 }
99
100 BPRINT_D("Socket %s create success", name);
101 return 0;
102}
103
104//---------------------------------------------------------------------------
105int boots_sk_close(int * sk)
106{
107 if (sk == NULL) {
108 BPRINT_E("%s: Incorrect socket", __func__);
109 return -1;
110 }
111 close(*sk);
112 *sk = -1;
113 return 0;
114}
115
116//---------------------------------------------------------------------------
117ssize_t boots_sk_send(int s, const void * buf, size_t len, struct sockaddr_un * to,
118 char * name)
119{
120 ssize_t ret = 0;
121
122 if (!buf || !len) {
123 BPRINT_E("%s: Incorrect argument", __func__);
124 return -1;
125 }
126 if (s < 0) {
127 BPRINT_E("%s: Incorrect FD(%d)", __func__, s);
128 return -1;
129 }
130#if 0
131 ret = sendto(s, buf, len, 0, (const struct sockaddr *)to,
132 sizeof(struct sockaddr_un));
133#else
134 UNUSED(to);
135 UNUSED(name);
136 ret = send(s, buf, len, 0);
137#endif
138 if (ret < 0)
139 BPRINT_E("%s: Send message failed: %s(%d)", __func__, strerror(errno), errno);
140
141 return ret;
142}
143
144//---------------------------------------------------------------------------
145ssize_t boots_sk_recv(int s, const void * buf, size_t len, int flags,
146 struct sockaddr * src_addr, socklen_t * addrlen)
147{
148 ssize_t ret = 0;
149
150 if (!buf || !len) {
151 BPRINT_E("%s: Incorrect argument", __func__);
152 return -1;
153 }
154 if (s < 0) {
155 BPRINT_E("%s: Incorrect FD(%d)", __func__, s);
156 return -1;
157 }
158
159 ret = recvfrom(s, (void *)buf, len, flags, src_addr, addrlen);
160 if (ret < 0)
161 BPRINT_E("%s: Receive message failed: %s(%d)", __func__, strerror(errno), errno);
162
163 return ret;
164}
165
166//---------------------------------------------------------------------------