How to notify other application for USB attached to system

Hi All,
My requirement on QNX system is to detect USB device and inform other application for USB device added.
I have code to detect specific usb device attach to system but don’t know how to notify other application, usb device is added on usb port ?? Please have a look on my code

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <sys/usbdi.h>

#define VENDOR 0x0951
#define DEVICE 0x1600

usbd_device_ident_t interest = {
VENDOR,
DEVICE,
USBD_CONNECT_WILDCARD,
USBD_CONNECT_WILDCARD,
USBD_CONNECT_WILDCARD,
};

usbd_funcs_t funcs = {
_USBDI_NFUNCS,
NULL,
NULL,
NULL
};

usbd_connect_parm_t cparms = {
NULL,
USB_VERSION,
USBD_VERSION,
0,
0, // argc
NULL, // *argv
0,
&interest,
&funcs
};

struct usbd_connection *connection;
struct usbd_device *device;
usbd_device_instance_t instance;

usbd_device_descriptor_t *ddesc=NULL;
struct usbd_desc_node *node=NULL;
usbd_string_descriptor_t *sdesc=NULL;

int main (void)
{
int error,status;
int devno,hostid;
char* ID;

error = usbd_connect (&cparms, &connection);
if (error != EOK) 
{ 
	printf ("\n usbd_connect() error number: %d \n",error); 
	return -1; 
} 

instance.config = 1; 
instance.iface = 0; 
instance.alternate = 0; 

for (hostid =0; hostid < 8; ++hostid)
{ 
for (devno = 0; devno < 16; ++devno)
{ 
	//memset(&instance, USBD_CONNECT_WILDCARD,sizeof(usbd_device_instance_t));

	instance.path = hostid, instance.devno = devno; 
	status = usbd_attach(connection, &instance,0,&device); 
	// the device has been found 
	if (status == EOK)
	{ 
		ddesc = usbd_device_descriptor(device,&node ); 
		if (ddesc == NULL )
		{ 
			printf("\n usbd_device_descriptor () error !!\n"); 
			return -1; 
		} 
		// is it a specific device ? 
		if (ddesc->idVendor == VENDOR)
		{ 
			sdesc = usbd_languages_descriptor(device,&node ); 
			ID = usbd_string(device,3,sdesc->bString[0]); 
			printf ("\n I found it -> hostid: %d, devno: %d, seriall number: %s \n",hostid,devno,ID); 
			if (usbd_detach (device) != EOK) 
				printf ("\n usbd_detach (): error ! \n"); 
		} 
		else
		{ 
			if (usbd_detach (device) != EOK) 
			printf ("\n usbd_detach (): error ! \n"); 
		} 
	}  // IF end 
} // 1ST for loop

} // 2ND for loop
if (usbd_disconnect(connection )!= EOK)
printf ("\n usbd_disconnect (): error ! \n");

return 0;
}

You can have to other application use the same code you just posted. Or you can use any of the IPC provided by QNX, signal, messages, pulse/Event, FIFO, sockets,etc. Which method to choose depends on your current design and requirement.

Possibly one of the few examples where a signal may be a reasonable choice (require a notification that something vague happened, no need to convey any information).

Makes for the simplest possible implementation in the client, and is portable to almost any OS (other than Windows).

Still doesn’t justify the existence of signals though :slight_smile:

Hi All,
I have modified my application for detection of USB device and modify the configuration “mass” file at /etc/system/enum/devices/usb path. I have launched my application by calling
driver(application path/App. name)

 I have two doubts with my implementation,
  1. Every time when usb device is connected application is launched as mentioned above. So when one usb device is attached and another usb device is attached another instance of same application will be launched. But i want if one instance is running then second instance shall be not be launched. How to avoid multiple instance of same application in configuration “mass” file ???

    Few lines of mass file is as follows,

device(usb, .class=$(USB_CLASS_MASS_STORAGE))
driver(devb-umass $(DISK_OPTS) $(UMASS_OPTS)",vid=0x$(ven),did=0x$(dev),busno=0x$(busno),devno=0x$(devno),iface=$(ifa
e),ign_remove")
driver(/home/usb_application) // MY APPLICATION LAUNCHED
echo(" USB CARD INSERTED")

  1. I have used pulse to send notification to other application for insertion of usb device. but in the code while(1) is used to keep running application till usb device will be removed.
    Is there any way to avoid while(1) in code also usbRemoval called as soon usb is removed. Currently if i commented out while(1) usbRemoval will not called because launched application already closed. Please find my code,

void usbInsertion(struct usbd_connection *connection,usbd_device_instance_t *instance )
{
int status;

pthread_mutex_lock (&usbMutex); 
printf ("USB device Insertion called \n");

status = usbd_attach(connection,instance,0,&device);
if (status == EOK)
		printf ("USB Device found");

MsgSendPulse(coid, 10, USB_Inserted, 5);

printf ("\n usbInsertion end and status is %d \n",status);
pthread_mutex_unlock (&usbMutex); 

}

void usbRemoval(struct usbd_connection *connection,usbd_device_instance_t *instance )
{
printf (“usbRemoval called \n”);

MsgSendPulse(coid, 10, USB_Removed, 15);

if (usbd_detach (device) != EOK) 
				printf ("\n usbd_detach (): error ! \n");

if (usbd_disconnect(connection )!= EOK) 
		printf ("\n usbd_disconnect (): error ! \n"); 

}

int main (void)
{
int error,status;
int devno,hostid;
char* ID;
char* path;

error = usbd_connect (&cparms, &connection);
if (error != EOK) 
{ 
	printf ("\n usbd_connect() error number: %d \n",error); 
	return -1; 
} 

coid = name_open(MY_SERV, 0);

while(1)
{	
		for (hostid =0; hostid < 8; ++hostid)
		{ 
		for (devno = 0; devno < 16; ++devno)
		{ 
			memset(&instance, USBD_CONNECT_WILDCARD,sizeof(usbd_device_instance_t));
		
			instance.path = hostid, instance.devno = devno; 
			status = usbd_attach(connection, &instance,0,&device); 
			// the device has been found 
			if (status == EOK)
			{ 
				ddesc = usbd_device_descriptor(device,&node ); 
				if (ddesc == NULL )
				{ 
					printf("\n usbd_device_descriptor () error !!\n"); 
					return -1; 
				} 
			}  // IF end 
		} // 1ST for loop
	} // 2ND for loop
}	
printf ("\n USB Application Ended \n");

return 0;
}