【iOS13】why call method`retrieveConnectedPeripheralsWithServices:`does not return any peripheral - core-bluetooth

I make sure that there has peripheral in system-bluetooth and match services when I call the method retrieveConnectedPeripheralsWithServices,but does not return any peripheral object

in iOS 13 you should wait until the CBCentralManager state is in on state before retrieving peripherals.

Related

Synchronization primitives in DriverKit

In a DriverKit extension, I would like to block a call from a user client until a specific hardware interrupt fires. Since there are no semaphores available (Does the DriverKit SDK support semaphores?), I've reached for a very basic spinlock using an _Atomic(bool) member and busy waiting:
struct IVars
{
volatile _Atomic(bool) InterruptOccurred = false;
}
// In the user client method handler
{
// Clear the flag
atomic_store(&ivars->InterruptOccurred, false);
// Set up the interrupt on the device
...
// Wait for the interrupt
while (!atomic_load(&ivars->InterruptOccurred))
{
IOSleep(10);
}
}
// In the interrupt handler
{
bool expected = false;
if (atomic_compare_exchange_strong(&ivars->InterruptOccurred, &expected, true))
{
return;
}
// Proceed with normal handling if the user client method is not waiting
}
The user client method is called infrequently and the interrupt is guaranteed to fire within 100ms, so in principle busy waiting should be acceptable, but I am not very happy with the solution. I haven't worked with spinlocks before and they make me feel rather uneasy.
I would like to avoid taking an IOLock in the interrupt handler. Is there any other synchronization primitive in DriverKit I could reach for? I guess a cleaner way to handle this would be for the user client method to accept a callback that fires on the interrupt, but that would still require synchronization with the interrupt handler and would complicate the client application code.
Preliminaries
I would like to avoid taking an IOLock in the interrupt handler.
I assume you're aware that, this being DriverKit, this isn't running in the context of a primary interrupt controller, but you're already behind a layer of Mach messaging, kernel/user context switch, and IODispatchQueue serialisation?
Possible solutions:
Since there are no semaphores available[…]
OSAction
The OSAction class contains a set of methods for sleeping in a thread until the action is invoked. (WillWait/Wait/EndWait) This might be a feasible way of implementing what you're trying to do. As usual, the documentation is in the header/iig file but hasn't made it into the web-based API docs.
IODispatchQueue
As of DriverKit 21 (macOS 12), you also get Apple's simpler Sleep/Wakeup event system baked into IODispatchQueue, which you might be familiar with from the kernel. (It is also similar to pthreads condition variables.) Note you need to create the queue with the kIODispatchQueueReentrant option in this case.
From DriverKit 22 (macOS 13/iPadOS) on, there's also a version with a deadline for the sleep SleepWithDeadline.
Async callbacks
I guess a cleaner way to handle this would be for the user client method to accept a callback that fires on the interrupt, but that would still require synchronization with the interrupt handler and would complicate the client application code.
If you're happy calling the async callback in the app on every interrupt, there's not really any synchronisation needed, you can just invoke the same OSAction repeatedly. Even if you want to only invoke the async call on the "next" interrupt, atomic compare-and-swap should be sufficient for the interrupt handler to claim the OSAction* pointer.
Important note:
With all of these potential solutions except IODispatchQueue::Sleep and the async callback: bear in mind that sleeping in the context of a user client external method will block the dispatch queue and thus any other calls to external methods in that user client will fail to make progress. (As well as any other methods scheduled to that queue.)

Basic question about UART RXCn flag (AVR)

I got stuck in the interrupt part while learning AVR.
Datasheet says about RXCn flag:
"This flag bit is set when there are unread data in the receive buffer and cleared when the receive buffer is empty
(i.e., does not contain any unread data)."
and there is an example about getting a characters with uart
while ( !(UCSRnA & (1<<RXCn)) );
/* Get and return received data from buffer */
return UDRn;
Will it wait here forever until the data comes from the Uart? And will mcu not be able to do any other work because of "while(1);"?
I know this method is polling and I also know that there is an interrupt method but will the mcu be locked because of this?
As #AterLux already said the program will halt until data is recived there are some other possibilities to catch the data nonblocking e.g.:
char uart_get(char *data)
{
if (UCSRnA & (1<<RXCn) );
{
*data = UDRn;
return 1;
}
return 0;
}
If no data has been received you will get 0 and can continue with the program. If you should use interrupt handling or polling depends on your problem. With interrupt handling you can use for example a circular buffer to save received data and use it if you need it. if you are still waiting for one value polling is also an oppertunity.
Yes. It will wait forever while the condition (!(UCSRnA & (1<<RXCn))) is fulfiled. I.e. it will wait until UCSRnA has the bit RXCn set.
If the Global Interrupt Flag (I flag in SREG register) is not cleared (by calling cli(), or entering an interrupt handler) then interrupts still able to run, all the peripherals (counters, SPI, TWI, etc) continue to work, while in this cycle. Of course the program beneath the cycle will not execute.

why kernel invokes other handler when kernel receive interrupt?

in linux kernel development,i read about interrupt that when the kernel receives interrupt,it invokes sequentially each registered handler on the line.
My question is why kernel invokes other handler?
This is because several devices are sharing the same interrupt line. The only way the kernel can detect which handler to invoke is by passing the dev_id of the device to all the handlers which requires invoking the handler. The handler that was registered with the passed dev_id gets a match and it continues to run.
Remember, the handler was registered as:
static irqreturn_t intr_handler(int irq, void *dev_id, struct pt_regs *regs)
the handler was registered by passing the dev_id. So, that's the only thing that distinguishes devices on the same IRQ line.
In a well define interrupt handler, specifically share the irq line, which will check whether the interrupt is raised by the specific device by reading some registers, and if it is, then handles the interrupt and return IRQ_HANDLED, or return IRQ_NONE to indicate that is not the device the handler servicing.
So, it invokes sequentially each registered handler on the line until that the handler return IRQ_HANDLED meaning processing properly

NULL pointer dereference in swiotlb_unmap_sg_attrs() on disk IO

I'm getting an error I really don't understand when reading or writing files using a PCIe block device driver. I seem to be hitting an issue in swiotlb_unmap_sg_attrs(), which appears to be doing a NULL dereference of the sg pointer, but I don't know where this is coming from, as the only scatterlist I use myself is allocated as part of the device info structure and persists as long as the driver does.
There is a stacktrace to go with the problem. It tends to vary a bit in exact details, but it always crashes in swiotlb_unmap_sq_attrs().
I think it's likely I have a locking issue, as I am not sure how to handle the locks around the IO functions. The lock is already held when the request function is called, I release it before the IO functions themselves are called, as they need an (MSI) IRQ to complete. The IRQ handler updates a "status" value, which the IO function is waiting for. When the IO function returns, I then take the lock back up and return to request queue handling.
The crash happens in blk_fetch_request() during the following:
if (!__blk_end_request(req, res, bytes)){
printk(KERN_ERR "%s next request\n", DRIVER_NAME);
req = blk_fetch_request(q);
} else {
printk(KERN_ERR "%s same request\n", DRIVER_NAME);
}
where bytes is updated by the request handler to be the total length of IO (summed length of each scatter-gather segment).
It turned out this was due to re-entrancy of the request function. Because I was unlocking in the middle to allow IRQs to come in, the request function could be called again, would take the lock (while the original request handler was waiting on IO) and then the wrong handler would get the IRQ and everything went south with stacks of failed IO.
The way I solved this was to set a "busy" flag at the start of the request function, clear it at the end and return immediately at the start of the function if this is set:
static void mydev_submit_req(struct request_queue *q){
struct mydevice *dev = q->queuedata;
// We are already processing a request
// so reentrant calls can take a hike
// They'll be back
if (dev->has_request)
return;
// We own the IO now, new requests need to wait
// Queue lock is held when this function is called
// so no need for an atomic set
dev->has_request = 1;
// Access request queue here, while queue lock is held
spin_unlock_irq(q->queue_lock);
// Perform IO here, with IRQs enabled
// You can't access the queue or request here, make sure
// you got the info you need out before you release the lock
spin_lock_irq(q->queue_lock);
// you can end the requests as needed here, with the lock held
// allow new requests to be processed after we return
dev->has_request = 0;
// lock is held when the function returns
}
I am still not sure why I consistently got the stacktrace from swiotlb_unmap_sq_attrs(), however.

Detecting if existing serial connection is lost due to device unplugged on Windows

I'm calling Win32 ReadFile to read from a Windows serial port. If my device is unplugged, ReadFile still returns success. How can I check if the connection to the device has been lost? I could do this by sending a message and not getting a response or listening for device events but is there any more straightforward way?
Serial ports are dumb in that there is no device independent way of knowing when something is or is/not plugged into the serial port. Because of this, most devices that use serial ports support some kind of communication protocol which will allow you to determine if something is connected or not.
Depends on the language, I am not certain if serial devices cause a device change event, and I do not have one at the moment to test.
However if PNP will pick it up I would try
From the WmiCode creator
Imports System
Imports System.Management
Imports System.Windows.Forms
Namespace WMISample
Public Class WMIReceiveEvent
Public Overloads Shared Function Main() As Integer
Try
Dim query As New WqlEventQuery( _
"SELECT * FROM Win32_DeviceChangeEvent")
Dim watcher As New ManagementEventWatcher(query)
Console.WriteLine("Waiting for an event...")
Dim eventObj As ManagementBaseObject = watcher.WaitForNextEvent()
Console.WriteLine("{0} event occurred.", eventObj("__CLASS"))
' Cancel the event subscription
watcher.Stop()
Return 0
Catch err As ManagementException
MessageBox.Show("An error occurred while trying to receive an event: " & err.Message)
End Try
End Function
End Class
End Namespace
From there you van further refine what was added or removed from the system.
It does little however for the fact the machine may have been booted without the device attached.
Either way I suggest in all communication protocols that proper communication error handing is essential. It is similar to the fact that you cannot guarantee that a socket has become disconnected between checking to see if connected and writing.

Resources