I'm building an application (runs on central device) in which I can scan, connect, pair, & reconnect in LE to a specific target device (peripheral device). My application is able to scan for Bluetooth LE devices and I have set the connection function. When I run my application, it scans until it find the target device and then it starts the connecting process.
On the target side (peripheral), I can see my central device was able to connect to it but the connection is dropped immediately. I think this is because it didn't find any compatible service (I think that just means there was no recognizable GATT attirubute/application/service?). I tried to connect to the same peripheral device through bluetoothctl and the connection drops immediately which makes me think something is missing from my target device (peripheral).
I'm wondering if I need to implement anything else before I try to connect to my peripheral device? Is it disconnecting because it didn't find any recognizable service on my central?
This is what I have from btmon on the peripheral side with another connection which was successful:
Connection request
Read remote supported features (understood remote as target device)
Turn off scan
Read remote extended features
Remote name request
Found this in the BTMON. "Attribute not found" possibly the issue?
> ACL Data RX: Handle 128 flags 0x02 dlen 11 #23 [hci0] 16.564046
ATT: Read By Group Type Request (0x10) len 6
Handle range: 0x0001-0xffff
Attribute group type: Primary Service (0x2800)
< ACL Data TX: Handle 128 flags 0x00 dlen 24 #24 [hci0] 16.564325
ATT: Read By Group Type Response (0x11) len 19
Attribute data length: 6
Attribute group list: 3 entries
Handle range: 0x0001-0x0005
UUID: Generic Access Profile (0x1800)
Handle range: 0x0006-0x000f
UUID: Generic Attribute Profile (0x1801)
Handle range: 0x0010-0x0012
UUID: Device Information (0x180a)
< ACL Data TX: Handle 128 flags 0x00 dlen 11 #25 [hci0] 16.564349
ATT: Read By Type Request (0x08) len 6
Handle range: 0x0001-0xffff
Attribute type: Unknown (0x2b3a)
> HCI Event: Number of Completed Packets (0x13) plen 5 #26 [hci0] 16.580038
Num handles: 1
Handle: 128
Count: 1
> ACL Data RX: Handle 128 flags 0x02 dlen 11 #27 [hci0] 16.596035
ATT: Read By Group Type Request (0x10) len 6
Handle range: 0x0013-0xffff
Attribute group type: Primary Service (0x2800)
> HCI Event: Number of Completed Packets (0x13) plen 5 #28 [hci0] 16.596041
Num handles: 1
Handle: 128
Count: 1
< ACL Data TX: Handle 128 flags 0x00 dlen 9 #29 [hci0] 16.596267
ATT: Error Response (0x01) len 4
Read By Group Type Request (0x10)
Handle: 0x0013
Error: Attribute Not Found (0x0a)
> ACL Data RX: Handle 128 flags 0x02 dlen 9 #30 [hci0] 16.620082
ATT: Error Response (0x01) len 4
Read By Type Request (0x08)
Handle: 0x0001
Error: Attribute Not Found (0x0a)
I'm working on a project using paho-mqtt python library. I have two machines Raspberry pi 3 and a desktop with ubuntu 20.04. The Mosquitto broker installed on the Raspberry pi and my python code running on Ubuntu.
The two machines connected over LAN. when I use Mosquitto client CLI on Ubuntu to publish to the broker (broker running on the Raspberry pi) it is working fine but when I use python code it is working once when I run the code for many time it connects once time only. when I run the python code on the Raspberry pi it working fine without any problem.
I installed Mosquitto broker on Ubuntu also faced the same problem the python code not working properly.
I used python 3.8 and 3.5 interpreters and tried all paho-mqtt library versions but still the same problem. It also doesn't fire exceptions or show logs. it shows logs only when it connects. I think the problem is in Ubuntu 20.04. My python code below:
mqtthandeler.py
import paho.mqtt.client as mqtt
from configmanager import ConfigManager
# Retrieve the commands and configurations from the server via MQTT
class Subscriber:
def __init__(self):
self.cfg = ConfigManager()
self.cfg.get_config()
# Network connection status LEDs indicators
# MQTT connection flag
self.connected = False
def test(self, x, y, z):
print('new command = ' + str(z))
def mqtt_connect(self):
global client
# Creat instance for MQTT client
client = mqtt.Client(client_id=self.cfg.config_data['MQTT']['server id'],
clean_session=False, userdata=None,
protocol=mqtt.MQTTv311, transport='tcp')
# Set the username and password to connect to the MQTT Broker
client.username_pw_set(username=self.cfg.config_data['MQTT']['authentication']['username'],
password=self.cfg.config_data['MQTT']['authentication']['password'])
# callbacks for on connect and disconnect
client.on_connect = self.on_connect
client.on_disconnect = self.on_disconnect
client.on_message = self.on_message
client.on_log = self.on_log
# Connect to the MQTT broker
try:
client.connect_async(host=self.cfg.config_data['MQTT']['hostname'],
port=self.cfg.config_data['MQTT']['port'],
keepalive=60, bind_address="")
client.loop_start()
print('try to connect')
except Exception as e:
# If debug true print error message
print('Unable to connect to MQTT broker \n Error message: ' + e)
if self.cfg.config_data['DEBUG']:
print('Unable to connect to MQTT broker \n Error message: ' + e)
else:
print('Unable to connect to MQTT broker')
def on_log(self, client, userdata, level, buff):
print('log= ' + buff)
# If connected to the MQTT broker print the results of the connection
def on_connect(self, client, userdata, flags, rc):
self.connected = True
# Subscribe to the commands topic to receive commands from the server
client.subscribe(topic=self.cfg.config_data['MQTT']['topics']['commands'],
qos=self.cfg.config_data['MQTT']['qos'])
if self.cfg.config_data['DEBUG']:
print("Connection returned result: " + mqtt.connack_string(rc))
# If disconnected from MQTT broker print the message and try to reconnect again
def on_disconnect(self, _client, userdata, rc):
self.connected = False
if rc != 0 & self.cfg.config_data['DEBUG']:
print("Unexpected disconnection.")
# When Message received from MQTT
def on_message(self, client, userdata, message):
print("Received message '" + str(message.payload) + "' on topic '"
+ message.topic + "' with QoS " + str(message.qos))
'''#================================================================================================================#'''
# Publish data to the MQTT
class Publisher(Subscriber):
def __init__(self):
qos_cfg = ConfigManager()
qos_cfg.get_config()
self._qos = qos_cfg.config_data['MQTT']['qos']
# ================================================================================================================#
def publish(self, _topic, msg):
client.publish(topic=_topic, payload=msg, qos=self._qos)
print('Sent')
main.py
from mqtthandler import Subscriber, Publisher
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print("MDL Server Started")
# Establish connection to the MQTT broker
mqtt = Subscriber()
mqtt.mqtt_connect()
pub = Publisher()
for i in range(10):
pub.publish('prox\\', '2222\\')
Mosquitto -v output (when the code connected)
1612077624: No will message specified.
1612077624: Sending CONNACK to 2222 (1, 0)
1612077624: Received SUBSCRIBE from 2222
1612077624: command\ (QoS 1)
1612077624: 2222 1 command\
1612077624: Sending SUBACK to 2222
1612077624: Received PUBLISH from 2222 (d0, q1, r0, m2, 'prox\', ... (5 bytes))
1612077624: Sending PUBACK to 2222 (m2, rc0)
1612077624: Received PUBLISH from 2222 (d0, q1, r0, m3, 'prox\', ... (5 bytes))
1612077624: Sending PUBACK to 2222 (m3, rc0)
1612077624: Received PUBLISH from 2222 (d0, q1, r0, m4, 'prox\', ... (5 bytes))
1612077624: Sending PUBACK to 2222 (m4, rc0)
1612077624: Received PUBLISH from 2222 (d0, q1, r0, m5, 'prox\', ... (5 bytes))
1612077624: Sending PUBACK to 2222 (m5, rc0)
1612077624: Received PUBLISH from 2222 (d0, q1, r0, m6, 'prox\', ... (5 bytes))
1612077624: Sending PUBACK to 2222 (m6, rc0)
1612077624: Received PUBLISH from 2222 (d0, q1, r0, m7, 'prox\', ... (5 bytes))
1612077624: Sending PUBACK to 2222 (m7, rc0)
1612077624: Received PUBLISH from 2222 (d0, q1, r0, m8, 'prox\', ... (5 bytes))
1612077624: Sending PUBACK to 2222 (m8, rc0)
1612077624: Received PUBLISH from 2222 (d0, q1, r0, m9, 'prox\', ... (5 bytes))
1612077624: Sending PUBACK to 2222 (m9, rc0)
1612077624: Received PUBLISH from 2222 (d0, q1, r0, m10, 'prox\', ... (5 bytes))
1612077624: Sending PUBACK to 2222 (m10, rc0)
1612077624: Received PUBLISH from 2222 (d0, q1, r0, m11, 'prox\', ... (5 bytes))
1612077624: Sending PUBACK to 2222 (m11, rc0)
1612077624: Socket error on client 2222, disconnecting.
and sometimes Mosquitto -v shows this output after many tries
1612078246: Socket error on client <unknown>, disconnecting.
1612079414: New connection from 192.168.10.2 on port 1883.
and sometimes nothing on Mosquitto -v
Also, I ran the code outside the virtualenv (Pycharm virtualenv) but the same behavior
I hope someone can help me. Thank you in advance
I'm just posting the solution that I found in case of someone faces the same problem.
The solution is to add a 0.1-sec delay after calling "mqtt_connect" function in main.py to give the MQTT code enough time to connect to the broker. I think this issue happens because my machine is fast so the program executes and finishes before the MQTT client gets enough time to connect.
final main.py code:
import time
from mqtthandler import Subscriber, Publisher
# Stop
# def exit():
# print('MDL Server stopped')
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print("MDL Server Started")
# Establish connection to the MQTT broker
mqtt = Subscriber()
mqtt.mqtt_connect()
time.sleep(0.1)
pub = Publisher()
for i in range(10):
pub.publish('prox\\', '2222' + '\\')
I am doing some changes in the SO_PRIORITY of the socket that sends UDP packets, using the command setsockopt, is there a way to see that changes with Wireshark or Tcpdump.
I read that can be DSF (Differentiated Services Field), but I am not sure because when I make the changes I see that this field is 00.
I am running a Linux Mint 19.3
It is part of the 802.1Q header. For ex:
> 802.1Q Virtual LAN, PRI: 5, DEI: 0, ID: 4
101. .... .... .... = Priority: Voice, < 10ms latency and jitter (5)
...0 .... .... .... = DEI: Ineligible
.... 0000 0000 0100 = ID: 4
Type: IPv6 (0x86dd)
I have bought two servers both being from the same hosting company and same package,
the first server bought works perfectly, this was just for testing and experimental,
the other server which I have been told by support team that their ssl and ftp versions are the same
does not work and gives me an error in my python script when trying to initiate commands.
'''
from ftplib_custom import FTP_TLS
import ssl
import ftplib_custom
import socket
def launch():
#Wait upon user input
print("Press Enter To Initialise Server Connection: ")
input()
#Info
print("Server Found")
print("Admin Auto Login...")
print("\n")
#Connection Initiation
#Working Test Server
ftp = FTP_TLS('MyHostname', user='Username', passwd='Password')
#Not Working Server
ftp = FTP_TLS('MyHostname', user='Username', passwd='Password')
ftp.ssl_version = ssl.PROTOCOL_TLS
print(ftp.getwelcome())
ftp.set_debuglevel(1)
ftp.set_pasv(True)
ftp.prot_p()
ftp.ccc()
print ("Login Successful")
def listLineCallback(line):
msg = ("** %s*"%line)
print(msg)
#Commands
ftp.pwd()
ftp.cwd("/")
ftp.retrlines('LIST', listLineCallback)
#ftp.dir()
launch()
'''
This is what I get from the working test server...
'''
Press Enter To Initialise Server Connection:
Server Found
Admin Auto Login...
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 1 of 50 allowed.
220-Local time is now 10:53. Server port: 21.
220-This is a private system - No anonymous login
220-IPv6 connections are also welcome on this server.
220 You will be disconnected after 15 minutes of inactivity.
*cmd* 'PBSZ 0'
*resp* '200 PBSZ=0'
*cmd* 'PROT P'
*resp* '200 Data protection level set to "private"'
*cmd* 'CCC'
*resp* '200 Control connection unencrypted'
Login Successful
*cmd* 'PWD'
*resp* '257 "/" is your current location'
*cmd* 'CWD /'
*resp* '250 OK. Current directory is /'
*cmd* 'TYPE A'
*resp* '200 TYPE is now ASCII'
*cmd* 'PASV'
*resp* '227 Entering Passive Mode (91,103,219,222,232,11)'
*cmd* 'LIST'
*resp* '150 Accepted data connection'
** drwxr-xr-x 2 sensitive sensitive 4096 Dec 18 15:53 .*
** drwxr-xr-x 2 sensitive sensitive 4096 Dec 18 15:53 ..*
** -rw------- 1 sensitive sensitive 4 Oct 28 15:59 .ftpquota*
*resp* '226-Options: -a -l \n226 3 matches total'
'''
This is what I get if I try and connect to the main server...
'''
Server Found
Admin Auto Login...
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 1 of 50 allowed.
220-Local time is now 10:57. Server port: 21.
220-This is a private system - No anonymous login
220-IPv6 connections are also welcome on this server.
220 You will be disconnected after 15 minutes of inactivity.
*cmd* 'PBSZ 0'
*resp* '200 PBSZ=0'
*cmd* 'PROT P'
*resp* '200 Data protection level set to "private"'
*cmd* 'CCC'
*resp* '200 Control connection unencrypted'
Login Successful
*cmd* 'PWD'
*resp* '257 "/" is your current location'
*cmd* 'CWD /'
*resp* '250 OK. Current directory is /'
*cmd* 'TYPE A'
*resp* '200 TYPE is now ASCII'
*cmd* 'PASV'
*resp* '227 Entering Passive Mode (91,146,105,202,253,214)'
*cmd* 'LIST'
*resp* '150 Accepted data connection'
Traceback (most recent call last):
File "C:\Users\install\Desktop\WebsiteConnecting\Website_FTP_Testing.py", line 54, in <module>
launch()
File "C:\Users\install\Desktop\WebsiteConnecting\Website_FTP_Testing.py", line 51, in launch
ftp.retrlines('LIST', listLineCallback)
File "C:\Users\install\AppData\Local\Programs\Python\Python37\lib\ftplib_custom.py", line 488, in retrlines
return self.voidresp()
File "C:\Users\install\AppData\Local\Programs\Python\Python37\lib\ftplib_custom.py", line 251, in voidresp
resp = self.getresp()
File "C:\Users\install\AppData\Local\Programs\Python\Python37\lib\ftplib_custom.py", line 236, in getresp
resp = self.getmultiline()
File "C:\Users\install\AppData\Local\Programs\Python\Python37\lib\ftplib_custom.py", line 222, in getmultiline
line = self.getline()
File "C:\Users\install\AppData\Local\Programs\Python\Python37\lib\ftplib_custom.py", line 210, in getline
raise EOFError
EOFError
'''
Any help would be much appreciated and I will edit or add upon request for whats needed.
'''
EDIT:
Status: Resolving address of ########
Status: Connecting to 91.146.105.202:21...
Status: Connection established, initializing TLS...
Error: GnuTLS error -15: An unexpected TLS packet was received.
Status: Connection attempt failed with "ECONNABORTED - Connection aborted".
Error: Could not connect to server
Status: Disconnected from server
Status: Selected port usually in use by a different protocol.
Status: Resolving address of ######
Status: Connecting to 91.146.105.202:21...
Status: Connection established, initializing TLS...
Error: GnuTLS error -15: An unexpected TLS packet was received.
Status: Connection attempt failed with "ECONNABORTED - Connection aborted".
Error: Could not connect to server
Status: Waiting to retry...
Status: Resolving address of #######
Status: Connecting to 91.146.105.202:21...
Status: Connection established, initializing TLS...
Error: GnuTLS error -15: An unexpected TLS packet was received.
Status: Connection attempt failed with "ECONNABORTED - Connection aborted".
Error: Could not connect to server
'''
The situation i have is Linux client is using UDP Socket. Client is sending message and in case no response within 10 seconds , Client will retry again in 10 seconds intervals.
The case is when connection is down , many trials are sent from client at the same time nothing was received on server side . Once the connection is up i found all previous messages are received at the same moment on server which means it was buffered and it causes a lot of problems because of duplicated messages received on the same moment on server side .
TCPDUMP ON client Side:
21:01:14.691903 IP 172.123.13211 > 172.34.13211: length 88 "1st at second 14"
21:01:24.692791 IP 172.123.13211 > 172.34.13211: length 88 "2nd at second 24"
21:01:34.694930 IP 172.123.13211 > 172.34.13211: length 88 "3rd at second 34"
21:01:44.696020 IP 172.123.13211 > 172.34.13211: length 88 "4th ate second 44"
Server TCPDUMP once the connection is up :
12:02:01.509518 IP 172.123.13211 > 13211: length 88 "Received 1st at second 1"
12:02:01.517841 IP 172.123.13211 > 13211: length 88 "Received 2nd at second 1"
12:02:01.543759 IP 172.123.13211 > 13211 length 88 "Received 3rd at second 1"
12:02:01.550741 IP 13211 > 172.123.13211: length 36
12:02:01.567948 IP 172.123.13211 > .13211: length 88
I need to understand in case of UDP socket is used and Connection is down .
how to avoid buffering packets during shortage
Client Code is in C++
Thank you
you might be looking for this :
How to flush Input Buffer of an UDP Socket in C?
Also the language you have used in the question is wrong. Please be more clean and precise and use relevant terminology