/
socket.cpp
148 lines (120 loc) · 3.87 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
Copyright (c) 2015 MidAir Technology, LLC. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, either
version 3 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "socket.h"
#include "data_management_layer.h"
#include "debug.h"
#include "registered_data_services.h"
#include <cstring>
#include <stdio.h>
Socket::Socket() { id=-1;inUse=false; bufferLength=0;bufferStart=0; }
int32_t Socket::init(uint8_t family, uint8_t type, uint8_t protocol, uint16_t port, uint32_t nif)
{
bufferLength=0;
bufferStart=0;
inUse = true;
this->family = family;
this->type = type;
this->protocol = protocol;
this->port = port;
this->nif = nif;
return 0;
}
int32_t Socket::connect(uint32_t sockid, const sockaddr_b *addr, long addrlen)
{
int offset = 0;
#if PLATFORM_ID==269
offset = 3;
#endif
id = sockid;
//format to tell the gateway to open a socket is: <sockid><family><type><protocol><port><address>
uint8_t data[9+addrlen+offset];
//first, the service ID
data[0+offset] = SOCKET_DATA_SERVICE & 0xFF;
//next, the socket ID
data[1+offset] = ((SOCKET_CONNECT << 4) & 0xF0) | (id & 0x0F);
//then the connection code
data[2+offset] = (SOCKET_CONNECT & 0xFF00) >> 8;
data[3+offset] = SOCKET_CONNECT & 0xFF;
data[4+offset] = family;
data[5+offset] = type;
data[6+offset] = protocol;
data[7+offset] = (port & 0xFF00) >> 8;
data[8+offset] = port & 0xFF;
memcpy(data+9+offset, addr, addrlen);
DataManagementLayer::sendData(9+addrlen+offset, data);
return 0;
}
int32_t Socket::send(const void* data, uint32_t len)
{
int offset = 0;
#if PLATFORM_ID==269
offset = 3;
#endif
DEBUG("Sending on socket %d data of size %d!", id, len);
uint8_t dataPlusID[len+2+offset];
dataPlusID[0+offset] = SOCKET_DATA_SERVICE & 0xFF;
dataPlusID[1+offset] = ((SOCKET_DATA << 4) & 0xF0) | (id & 0x0F);
memcpy(dataPlusID+2+offset, data, len);
// uint8_t dataPlusID[len];
// memcpy(dataPlusID, data, len);
DataManagementLayer::sendData(len+2+offset, dataPlusID);
return len;
}
int32_t Socket::receive(void* data, uint32_t len, unsigned long _timeout)
{
if (bufferLength > 0) {
DEBUG("RX ASK: %d AVAIL: %d", len, bufferLength);
} else {
return 0;
}
int bytesToCopy = len;
if (len > bufferLength) {
//they are asking for too much data
bytesToCopy = bufferLength;
}
memcpy(data, buffer+bufferStart, bytesToCopy);
bufferStart += bytesToCopy;
bufferLength -= bytesToCopy;
if (bufferLength == 0) {
bufferStart = 0;
}
return bytesToCopy;
}
int32_t Socket::close()
{
bufferLength=0;
bufferStart=0;
inUse = false;
// uint8_t data[2];
// data[0] = SOCKET_DATA_SERVICE & 0xFF;
// data[1] = (SOCKET_DISCONNECT & 0xF0) | (id & 0x0F);
//
// DataManagementLayer::sendData(2, data);
return 0;
}
int32_t Socket::bytes_available()
{
return bufferLength;
}
int32_t Socket::feed(uint8_t* data, uint32_t len)
{
if (bufferLength+len > SOCKET_BUFFER_SIZE) {
//can't put all this data in.
return -1;
}
memcpy(buffer+bufferStart+bufferLength, data, len);
bufferLength+=len;
// DEBUG("Fed %d bytes to socket id %d, leaving it with %d bytes", len, id, bufferLength);
return 0;
}