-
Notifications
You must be signed in to change notification settings - Fork 10
/
socket.cpp
132 lines (107 loc) · 3.15 KB
/
socket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
socket.cpp - network sockett class
Copyright (C) 2011 DIYSandbox LLC
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "socket.h"
#include <Wirefree.h>
static uint16_t local_port;
/**
* @brief This Socket function initialize the channel in perticular mode, and set the port and wait for W5100 done it.
* @return 1 for success else 0.
*/
uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag)
{
//uint8_t ret;
//if ((protocol == IPPROTO::TCP))
//{
close(s);
if (port != 0) {
GS.configSocket(s, protocol, port);
}
else {
local_port++; // if don't set the source port, set local_port number.
GS.configSocket(s, protocol, local_port);
}
//W5100.execCmdSn(s, Sock_OPEN);
return 1;
//}
//return 0;
}
/**
* @brief This function close the socket and parameter is "s" which represent the socket number
*/
void close(SOCKET s)
{
GS.execSocketCmd(s, CMD_CLOSE_CONN);
}
/**
* @brief This function established the connection for the channel in passive (server) mode. This function waits for the request from the peer.
* @return 1 for success else 0.
*/
uint8_t listen(SOCKET s)
{
if (GS.readSocketStatus(s) != SOCK_STATUS::INIT)
return 0;
if (GS.getSocketProtocol(s) == IPPROTO::TCP) {
GS.execSocketCmd(s, CMD_TCP_LISTEN);
} else if (GS.getSocketProtocol(s) == IPPROTO::UDP) {
GS.execSocketCmd(s, CMD_UDP_LISTEN);
}
return 1;
}
uint16_t recv(SOCKET s, uint8_t *buf, uint16_t len)
{
uint16_t ret=0;
if ( (len > 0) && GS.isDataOnSock(s))
{
ret = GS.readData(s, buf, len);
}
return ret;
}
void disconnect(SOCKET s)
{
GS.execSocketCmd(s, CMD_CLOSE_CONN);
}
uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len)
{
uint8_t status=0;
uint16_t ret=0;
uint16_t freesize=0;
if (len > GS.SSIZE)
ret = GS.SSIZE; // check size not to exceed MAX size.
else
ret = len;
ret = GS.writeData(s, buf, len);
return ret;
}
/**
* @brief This function established the connection for the channel in Active (client) mode.
* This function waits for the untill the connection is established.
*
* @return 1 for success else 0.
*/
uint8_t connect(SOCKET s, String addr, String port)
{
String ip;
ip = addr; // FIXME : why local variable?
if
(
((addr[0] == 0xFF) && (addr[1] == 0xFF) && (addr[2] == 0xFF) && (addr[3] == 0xFF)) ||
((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) //||
//(port == 0x00)
)
return 0;
// set destination IP
return GS.connectSocket(s, ip, port);
}