-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDP_API.h
267 lines (172 loc) · 5.95 KB
/
UDP_API.h
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
2020 © Copyright (c) BiDaE Technology Inc.
Provided under BiDaE SHAREWARE LICENSE-1.0 in the LICENSE.
Project Name:
BeDIS
File Name:
UDP_API.h
Version:
2.0, 20190608
File Description:
This file contains the header of function declarations and variable used
in UDP_API.c
Abstract:
BeDIS uses LBeacons to deliver 3D coordinates and textual descriptions of
their locations to users' devices. Basically, a LBeacon is an inexpensive,
Bluetooth Smart Ready device. The 3D coordinates and location description
of every LBeacon are retrieved from BeDIS (Building/environment Data and
Information System) and stored locally during deployment and maintenance
times. Once initialized, each LBeacon broadcasts its coordinates and
location description to Bluetooth enabled user devices within its coverage
area.
Authors:
Gary Xiao , [email protected]
*/
#ifndef UDP_API_H
#define UDP_API_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <pthread.h>
#include <errno.h>
#ifdef _MSC_VER
#include <winsock2.h>
#include <windows.h>
#pragma comment(lib,"WS2_32.lib")
#include <WS2tcpip.h>
#else
#include <arpa/inet.h>
#include <sys/socket.h>
#endif
#include "Common.h"
#include "pkt_Queue.h"
/* The time interval in seconds for Select() break the block */
#define UDP_SELECT_TIMEOUT 60
/* The time in milliseconds for the send thread to sleep when it is idle */
#define SEND_THREAD_IDLE_SLEEP_TIME 50
/* The time in milliseconds for the receive thread to sleep when it is idle */
#define RECEIVE_THREAD_IDLE_SLEEP_TIME 50
#define DELIMITER_SEMICOLON ";"
#define LENGTH_OF_SHA256 512
#define LENGTH_OF_ENCODED_WIFI_MESSAGE (WIFI_MESSAGE_LENGTH + LENGTH_OF_SHA256)
/* When debugging is needed */
//#define debugging
typedef struct {
#ifdef _WIN32
WSADATA wsaData;
WORD sockVersion;
#endif
struct sockaddr_in si_server;
int optval;
int send_socket, recv_socket;
int recv_port;
pthread_t udp_send_thread, udp_receive_thread;
/* The flag set to true whwn the process need to stop */
bool shutdown;
spkt_ptr pkt_Queue, Received_Queue;
} sudp_config;
typedef sudp_config *pudp_config;
enum{
socket_error = -1,
send_socket_error = -2,
recv_socket_error = -3,
set_socketopt_error = -4,
recv_socket_bind_error = -5,
addpkt_msg_oversize = -6
};
/*
udp_initial
For initialize UDP Socket including the send queue and the receive queue.
Parameter:
udp_config: The pointer points to the structure contains all variables for
the UDP connection.
Return Value:
int : If return 0, everything work successfully.
If not 0 , somthing wrong.
*/
int udp_initial(pudp_config udp_config, int recv_port);
/*
udp_addpkt_without_encoding
This function is used to add the packet to the assigned pkt queue without AES encoding and sh256 hash.
Parameter:
udp_config : The pointer points to the structure contains all variables
for the UDP connection.
port : The port number to be sent to.
address : The pointer points to the destnation address of the packet.
content : The pointer points to the content we decided to send.
size : The size of the content.
Return Value:
int : If return 0, everything work successfully.
If not 0 , something wrong.
*/
int udp_addpkt_without_encoding(pudp_config udp_config, char *address, unsigned int port,
char *content, int size);
/*
udp_getrecv_without_encoding
This function is used for get received packet from the received queue without AES encoding and sha256 hash.
Parameter:
udp_config : The pointer points to the structure contains all variables
for the UDP connection.
Return Value:
sPkt : return the first pkt content in the received queue.
*/
sPkt udp_getrecv_without_encoding(pudp_config udp_config);
/*
udp_addpkt
This function is used to add the packet to the assigned pkt queue.
Parameter:
udp_config : The pointer points to the structure contains all variables
for the UDP connection.
port : The port number to be sent to.
address : The pointer points to the destnation address of the packet.
content : The pointer points to the content we decided to send.
size : The size of the content.
Return Value:
int : If return 0, everything work successfully.
If not 0 , something wrong.
*/
int udp_addpkt(pudp_config udp_config, char *address, unsigned int port,
char *content, int size);
/*
udp_getrecv
This function is used for get received packet from the received queue.
Parameter:
udp_config : The pointer points to the structure contains all variables
for the UDP connection.
Return Value:
sPkt : return the first pkt content in the received queue.
*/
sPkt udp_getrecv(pudp_config udp_config);
/*
udp_send_pkt_routine
The thread for sending packets to the destination address.
Parameter:
udpconfig: The pointer points to the structure contains all variables for
the UDP connection.
Return Value:
None
*/
void *udp_send_pkt_routine(void *udpconfig);
/*
udp_recv_pkt_routine
The thread for receiving packets.
Parameter:
udpconfig: The pointer points to the structure contains all variables for
the UDP connection.
Return Value:
None
*/
void *udp_recv_pkt_routine(void *udpconfig);
/*
udp_release
Release All pkts and mutexes.
Parameter:
udp_config: The pointer points to the structure contains all variables for
the UDP connection.
Return Value:
int : If return 0, everything work successfully.
If not 0 , something wrong.
*/
int udp_release(pudp_config udp_config);
#endif