2021-08-06 11:20 AM
Hi everyone,
I want to send messages with L2CAP protocol between two Bluetooth devices. This is my client code:
void process_bluetooth () {
int bl_buff_it = 0; // global buffer iterator
// Code of: https://people.csail.mit.edu/albert/bluez-intro/x559.html
struct sockaddr_l2 addr = { 0 };
int s, status;
char dest[18] = "24:0A:64:28:71:E6";
// Socket creation
s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
// Connection parameters
addr.l2_family = AF_BLUETOOTH;
addr.l2_psm = htobs(0x1001);
str2ba( dest, &addr.l2_bdaddr );
// Establishing connection
status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
if (status < 0) exit(0);
// Message buffer
int tam_bluetooth_buff = DATA_BLUETOOTH_BUFFER * (SIZE_DATA + 1); // DATA_BLUETOOTH_BUFFER = 128, SIZE_DATA = 3
char bluetooth_buff[tam_bluetooth_buff];
strcpy(bluetooth_buff, "");
int msg_to_send = 15, msg_num = 0;
char value_string[SIZE_DATA];
while (msg_num < msg_to_send) {
// Checking if data available in global data buffer
if (bl_check_buff[bl_buff_it] == '1') {
bl_check_buff[bl_buff_it] = '0';
// Add data to buffer
sprintf(value_string, "%u%u%u ", bl_data_buff[bl_buff_it].bytes[0], bl_data_buff[bl_buff_it].bytes[1], bl_data_buff[bl_buff_it].bytes[2]); // uint8_t bytes[3]
strcat(bluetooth_buff, value_string);
bl_buff_it = (bl_buff_it + 1) % NUM_ELEMENTS; // NUM_ELEMENTS = huge number
if (bl_buff_it % DATA_BLUETOOTH_BUFFER == 0 && bl_buff_it != 0) {
// Message sending
if(write(s, bluetooth_buff, tam_bluetooth_buff) < 0) {
int errnum = errno;
fprintf(stderr, "Value of errno: %d\n", errno);
perror("Error printed by perror");
fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
exit(0);
}
msg_num++;
strcpy(bluetooth_buff, "");
}
}
}
close(s);
}
My server code is exactly the same as appears in https://people.csail.mit.edu/albert/bluez-intro/x559.html , but buffer size is 65535.
When I try to increase bluetooth_buff 's length - tam_bluetooth_buff variable - I get this error:
Value of errno: 90
Error printed by perror: Message too long
Error opening file: Message too long
Note that everything is working fine if DATA_BLUETOOTH_BUFFER is 128 and not if is 256, for example.
Please help!!
Regards