1 /*
2 * os_trans.c
3 *
4 * Copyright 2001-2009 Texas Instruments, Inc. - http://www.ti.com/
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 #include <netinet/in.h>
19 #include <sys/socket.h>
20 #include <sys/types.h>
21 #include <arpa/inet.h>
22 #include <unistd.h>
23
24
25 #include "cu_osapi.h"
26 #include "os_trans.h"
27
os_trans_create()28 TI_BOOL os_trans_create()
29 {
30 return TRUE;
31 }
32
33
os_socket(THandle * pSock)34 TI_BOOL os_socket (THandle* pSock)
35 {
36 SOCKET socket_id;
37 TI_BOOL optval = TRUE;
38
39 socket_id = socket(PF_INET, SOCK_STREAM, 0);
40
41 if (socket_id==SOCKET_ERROR)
42 {
43 /* Error opening socket */
44 os_error_printf(CU_MSG_ERROR, "%s: error opening socket.\n", __FUNCTION__);
45 return (FALSE);
46 }
47
48 /*************************/
49 /* Configure the socket */
50 /***********************/
51
52 if (setsockopt(socket_id, SOL_SOCKET, SO_REUSEADDR, (const THandle)&optval, sizeof(optval)) == OSAL_ERROR) {
53 /* Error setting socket option */
54 os_error_printf(CU_MSG_ERROR, "%s: error setting socket option. Error %d\n", __FUNCTION__, os_get_last_error());
55 close(socket_id); //close socket
56 return FALSE;
57 }
58
59 *pSock = (THandle) socket_id;
60
61 return TRUE;
62 }
63
64
os_bind(THandle sock,U16 port)65 TI_BOOL os_bind (THandle sock, U16 port)
66 {
67 struct sockaddr_in server_addr;
68 TI_SIZE_T result;
69
70 server_addr.sin_family = AF_INET;
71 server_addr.sin_addr.s_addr = htonl (INADDR_ANY);
72 server_addr.sin_port = htons(port);
73
74 result = bind((SOCKET)sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
75
76 if (result != OK) {
77 /* Error binding socket */
78 os_error_printf(CU_MSG_ERROR, "%s: error binding socket. Error %d\n", __FUNCTION__, os_get_last_error());
79 close((SOCKET)sock);
80 return(FALSE);
81 }
82
83 return TRUE;
84 }
85
86
os_sockWaitForConnection(THandle socket_id,THandle * pConnSock)87 TI_BOOL os_sockWaitForConnection (THandle socket_id, THandle* pConnSock)
88 {
89 struct sockaddr_in client_addr;
90 socklen_t client_addr_len;
91 TI_SIZE_T result;
92
93 result = listen((SOCKET)socket_id, MAX_QUEUE_LENGTH);
94
95 if (result == OSAL_ERROR) {
96 /* Error listening to socket */
97 /* os_error_printf(CU_MSG_ERROR, "%s: error listening to socket. Error %d\n", __FUNCTION__, errno);
98 *pConnSock = NULL;
99 closesocket((SOCKET)socket_id);
100 return(FALSE);*/
101 }
102
103 /**********************/
104 /* Accept connection */
105 /********************/
106 client_addr_len = sizeof(client_addr);
107
108 /* We suppose to get new socket id after accept (blocking action) */
109 result = accept((SOCKET)socket_id, (struct sockaddr *)&client_addr, &client_addr_len);
110
111 if (result == OSAL_ERROR) {
112 *pConnSock = NULL;
113 /* Error accepting connection */
114 os_error_printf(CU_MSG_ERROR, "%s: error accepting connection. Error %d\n", __FUNCTION__, os_get_last_error());
115 close((SOCKET) socket_id);
116 return(FALSE);
117 }
118
119 *pConnSock = (THandle) result;
120
121 return TRUE;
122 }
123
124
os_sockSend(THandle socket_id,PS8 buffer,U32 bufferSize)125 TI_BOOL os_sockSend (THandle socket_id, PS8 buffer, U32 bufferSize)
126 {
127 TI_SIZE_T result;
128
129 /* Write to the socket */
130 result = send( (SOCKET) socket_id, buffer, (U32)bufferSize, 0);
131 if (result == SOCKET_ERROR) {
132
133 /**************************/
134 /* Error writing to port */
135 /************************/
136 os_error_printf(CU_MSG_ERROR, "%s: Error writing to socket (result = %d), error %d\n",
137 __FUNCTION__, result, os_get_last_error());
138
139 return FALSE;
140 }
141
142 return TRUE;
143 }
144
os_trans_destroy()145 VOID os_trans_destroy()
146 {
147
148 }
149
os_sockRecv(THandle socket_id,PU8 pBuffer,U32 bufferSize,TI_SIZE_T flags)150 S32 os_sockRecv (THandle socket_id, PU8 pBuffer, U32 bufferSize, TI_SIZE_T flags)
151
152 {
153 TI_SIZE_T result;
154
155 /* Read from the socket */
156 result = recv((SOCKET)socket_id, pBuffer, bufferSize, flags);
157 if (result == SOCKET_ERROR) {
158
159 /***************************/
160 /* Error reading from port */
161 /***************************/
162
163 os_error_printf(CU_MSG_ERROR, "%s: Error reading from socket (result = %d), error %d\n", __FUNCTION__, result, os_get_last_error());
164 }
165
166 return (S32)result;
167 }
168
169