blob: 4ec5776791bae2ee6e053eb30983e354a910d60a [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#include <errno.h>
39#include <string.h>
40#include <stddef.h>
41#include <unistd.h>
42
43#include "boots.h"
44#include "boots_eth.h"
45
46//---------------------------------------------------------------------------
47#define LOG_TAG "boots_eth"
48
49//---------------------------------------------------------------------------
50int boots_eth_create(int * sk, struct sockaddr_in * sockaddr, int client)
51{
52 UNUSED(boots_btif);
53
54 // Create server socket
55 *sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
56 if (*sk < 0) {
57 BPRINT_E("Create socket failed: %s(%d)", strerror(errno), errno);
58 return errno;
59 }
60
61 // Init server socket
62 if (!client) {
63 memset(sockaddr, 0, sizeof(struct sockaddr_in));
64 sockaddr->sin_family = AF_INET;
65 sockaddr->sin_port = ETH_UDP_SRV_PORT;
66 sockaddr->sin_addr.s_addr = INADDR_ANY;
67 if (bind(*sk, (struct sockaddr *)sockaddr, sizeof(struct sockaddr)) < 0) {
68 BPRINT_E("Bind socket failed: %s(%d)", strerror(errno), errno);
69 return errno;
70 }
71 }
72
73 BPRINT_D("Socket %s create success", client ? "Client" : "Server");
74 return 0;
75}
76
77//---------------------------------------------------------------------------
78int boots_eth_close(int * sk)
79{
80 if (sk == NULL) {
81 BPRINT_E("%s: Incorrect socket", __func__);
82 return -1;
83 }
84 close(*sk);
85 *sk = -1;
86 return 0;
87}
88
89//---------------------------------------------------------------------------
90ssize_t boots_eth_send(int s, const void * buf, size_t len, struct sockaddr_in * to)
91{
92 ssize_t ret = 0;
93
94 if (!buf || !len) {
95 BPRINT_E("%s: Incorrect parameters", __func__);
96 return -1;
97 }
98#if 1
99 ret = sendto(s, buf, len, 0, (const struct sockaddr *)to,
100 sizeof(struct sockaddr));
101#else
102 ret = send(s, buf, len, 0);
103#endif
104 if (ret < 0)
105 BPRINT_E("%s: Send message failed: %s(%d)", __func__, strerror(errno), errno);
106
107 return ret;
108}
109
110//---------------------------------------------------------------------------
111ssize_t boots_eth_recv(int s, const void * buf, size_t len, int flags)
112{
113 ssize_t ret = 0;
114 struct sockaddr srcaddr;
115 socklen_t srcaddr_len = sizeof(struct sockaddr);
116
117 if (!buf || !len) {
118 BPRINT_E("%s: Incorrect parameters", __func__);
119 return -1;
120 }
121
122 ret = recvfrom(s, (void *)buf, len, flags, &srcaddr, &srcaddr_len);
123 if (ret < 0)
124 BPRINT_E("%s: Receive message failed: %s(%d)", __func__, strerror(errno), errno);
125
126 return ret;
127}
128