DroneBridge lib example usage
Examples on how to use DroneBridge library to send & receive custom data

DroneBridge Common library

All DroneBridge modules use the DroneBridge Common library to send & receive data. Below are some examples given on how to use the library. It is available for Python and C/C++. Both implementations are native and not directly linked.
AES encryption 128, 192 or 256-bit is currently only available for the Python implementation. Pycryptodomex is used.

Python

Link to the DroneBridge class. All examples can also be found inside the git repository.

Sending

example_sender.py
1
#
2
# This file is part of DroneBridge: https://github.com/seeul8er/DroneBridge
3
#
4
# Copyright 2019 Wolfgang Christl
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
19
import time
20
21
from DroneBridge import DroneBridge, DBDir, DBMode, DBPort
22
23
list_wifi_interfaces = ['wlx00c0ca973410'] # must be in monitor mode
24
communication_id = 22 # idents a link, multiple systems with same ports on same frequency possible
25
send_recv_port = DBPort.DB_PORT_GENERIC_1 # Virtual port to which the packet is addressed to
26
frame_type = 1 # RTS, 2 DATA
27
compatibility_mode = False # Enable with unpatched Kernels etc. (try without first)
28
send_direction = DBDir.DB_TO_UAV # Direction of the packet. Reverse direction on receiving side needed
29
30
# Set to None for unencrypted link. Must be the same on receiving side.
31
# length of 32, 48 or 64 characters representing 128bit, 192bit and 256bit AES encryption
32
encrypt_key = "3373367639792442264528482B4D6251" # bytes or string representing HEX
33
34
payload_data = b'HelloEveryone.IamPayload!LifeIsEasyWhenYouArePayload'
35
dronebridge = DroneBridge(send_direction, list_wifi_interfaces, DBMode.MONITOR, communication_id, send_recv_port,
36
tag="Test_Sender", db_blocking_socket=True, frame_type=frame_type,
37
compatibility_mode=compatibility_mode, encryption_key=encrypt_key)
38
39
list_sockets_raw = dronebridge.list_lr_sockets # List of raw sockets to be used for manual send/receive operations
40
41
for i in range(10000):
42
time.sleep(0.5)
43
dronebridge.sendto_uav(payload_data, send_recv_port)
44
print("\r" + str(i), end='')
45
46
print("\nDone sending!")
Copied!

Receiving

example_receiver.py
1
#
2
# This file is part of DroneBridge: https://github.com/seeul8er/DroneBridge
3
#
4
# Copyright 2019 Wolfgang Christl
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
19
from DroneBridge import DroneBridge, DBDir, DBMode, DBPort
20
21
list_wifi_interfaces = ['wlx8416f916382c'] # must be in monitor mode
22
communication_id = 22
23
send_recv_port = DBPort.DB_PORT_GENERIC_1
24
frame_type = 1 # RTS, 2 DATA
25
compatibility_mode = False
26
send_direction = DBDir.DB_TO_GND
27
28
# Set to None for unencrypted link. Must be the same on receiving side.
29
# length of 32, 48 or 64 characters representing 128bit, 192bit and 256bit AES encryption
30
encrypt_key = "3373367639792442264528482B4D6251" # bytes or string representing HEX
31
32
dronebridge = DroneBridge(send_direction, list_wifi_interfaces, DBMode.MONITOR, communication_id, send_recv_port,
33
tag="Test_Receiver", db_blocking_socket=True, frame_type=frame_type,
34
compatibility_mode=compatibility_mode, encryption_key=encrypt_key)
35
36
dronebridge.clear_socket_buffers()
37
38
for i in range(100):
39
received_payload = dronebridge.receive_data()
40
if received_payload:
41
print("Received: " + received_payload.decode())
42
Copied!

C/C++

The common library must be linked during compilation. An example CMakeLists.txt is given blow. Place the file inside a folder of the root directory of the DroneBridge git project and it should compile right away.
CMakeLists.txt
1
cmake_minimum_required(VERSION 3.5)
2
project(example)
3
4
set(CMAKE_C_STANDARD 11)
5
6
add_subdirectory(../common db_common)
7
set(SRC_FILES_RECV receive_main.c)
8
set(SRC_FILES_SEND send_main.c)
9
10
add_executable(send ${SRC_FILES_SEND})
11
target_link_libraries(send db_common)
12
13
add_executable(receive ${SRC_FILES_RECV})
14
target_link_libraries(receive db_common)
Copied!

Sending

send_main.c
1
/*
2
* This file is part of DroneBridge: https://github.com/seeul8er/DroneBridge
3
*
4
* Copyright 2019 Wolfgang Christl
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
*/
19
20
#include <signal.h>
21
#include <stdint.h>
22
#include <string.h>
23
#include <stdio.h>
24
#include <zconf.h>
25
#include "../common/db_raw_send_receive.h"
26
27
// DroneBridge options
28
#define INTERFACE "wlx8416f916382c"
29
#define FRAME_TYPE 2 // 1=RTS; 2=DATA
30
#define DATARATE 11 // Mbit
31
#define COMMID 16
32
#define DST_PORT 10 // destination port
33
#define RECV_PORT DST_PORT
34
35
#define PAYLOAD_BUFF_SIZ 1024
36
37
int keep_running = 1;
38
39
void int_handler(int dummy) {
40
keep_running = 0;
41
}
42
43
int main(int argc, char *argv[]) {
44
signal(SIGINT, int_handler);
45
signal(SIGTERM, int_handler);
46
char interface[IFNAMSIZ];
47
strcpy(interface, INTERFACE);
48
49
db_socket_t raw_socket = open_db_socket(interface, COMMID, 'm', DATARATE, DB_DIREC_GROUND,
50
RECV_PORT, (uint8_t) FRAME_TYPE);
51
52
uint8_t seq_num = 0;
53
uint8_t payload[PAYLOAD_BUFF_SIZ];
54
memset(payload, 1, PAYLOAD_BUFF_SIZ);
55
56
while(keep_running) {
57
db_send_div(&raw_socket, payload, DST_PORT, PAYLOAD_BUFF_SIZ, update_seq_num(&seq_num), 0);
58
printf(".");
59
fflush(stdout);
60
usleep((unsigned int) 1e6);
61
}
62
close(raw_socket.db_socket);
63
printf("Terminated");
64
}
Copied!

Receiving

receive_main.c
1
/*
2
* This file is part of DroneBridge: https://github.com/seeul8er/DroneBridge
3
*
4
* Copyright 2019 Wolfgang Christl
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
*/
19
20
/**
21
* Example on how to use DroneBridge common library to receive data.
22
* This example receives on the port of the status module.
23
* Set port & adapter name to match your configuration
24
*/
25
26
#include <string.h>
27
#include <stdio.h>
28
#include <signal.h>
29
#include <zconf.h>
30
#include "../common/db_raw_send_receive.h"
31
#include "../common/db_raw_receive.h"
32
33
#define BUFFER_SIZE 2048
34
35
#define ADAPTER_NAME "wlx8416f916382c" // Name of WiFi adapter. Must be in monitor mode
36
#define DB_RECEIVING_PORT DB_PORT_STATUS // Receive data on DB raw port for status module
37
#define DB_SEND_DIRECTION DB_DIREC_DRONE // Send data in direction of UAV
38
39
int keep_going = 1;
40
41
void sig_handler(int sig) {
42
printf("DroneBridge example receiver: Terminating...\n");
43
keep_going = 0;
44
}
45
46
int main(int argc, char *argv[]) {
47
uint8_t buffer[BUFFER_SIZE];
48
uint8_t payload_buff[DATA_UNI_LENGTH];
49
uint8_t seq_num = 0;
50
memset(buffer, 0, BUFFER_SIZE);
51
52
uint8_t comm_id = 200;
53
char db_mode = 'm';
54
char adapters[DB_MAX_ADAPTERS][IFNAMSIZ];
55
strncpy(adapters[0], ADAPTER_NAME, IFNAMSIZ);
56
57
// init DroneBridge sockets for raw protocol
58
db_socket_t raw_interfaces[DB_MAX_ADAPTERS]; // array of DroneBridge sockets
59
memset(raw_interfaces, 0, DB_MAX_ADAPTERS);
60
raw_interfaces[0] = open_db_socket(adapters[0], comm_id, db_mode, 6, DB_SEND_DIRECTION,
61
DB_RECEIVING_PORT, DB_FRAMETYPE_DEFAULT);
62
// Some stuff for proper termination
63
struct sigaction action;
64
memset(&action, 0, sizeof(struct sigaction));
65
action.sa_handler = sig_handler;
66
sigaction(SIGTERM, &action, NULL);
67
sigaction(SIGINT, &action, NULL);
68
printf("DroneBridge example receiver: Waiting for data\n");
69
while (keep_going) {
70
uint16_t radiotap_length = 0;
71
ssize_t received_bytes = recv(raw_interfaces[0].db_socket, buffer, BUFFER_SIZE, 0);
72
uint16_t payload_length = get_db_payload(buffer, received_bytes, payload_buff, &seq_num, &radiotap_length);
73
printf("Received raw frame with %zi bytes & %i bytes of payload\n", received_bytes, payload_length);
74
}
75
for (int i = 0; i < DB_MAX_ADAPTERS; i++)
76
close(raw_interfaces[i].db_socket);
77
return 0;
78
}
79
Copied!
Last modified 2yr ago