List character device programmatically - c++

In my code NVRAM is implemented as character device and I want to give a condition to check whether it is character device or not. How to implement it programmatically? How can I do it through c++ code.
In terminal I gave command cat /proc/devices and it lists:
Character devices:
1 mem
4 /dev/vc/0
4 tty
4 ttyS
5 /dev/tty
5 /dev/console
5 /dev/nvram
What does this number 5 in /dev/nvram denote?

The number denotes the device's major number, and the Character devices: heading in that listing tells you it's a character device.
If your character device is linked into the filesystem somewhere, like /dev/mydevice, you can also get information about it via the stat system call. The st_mode field of the struct stat structure can be tested with the S_ISCHR macro.

Related

Get inode for lookup in /proc/net/udp

I am trying to programmatically track the amount of data in my receive buffer. I am receiving UDP data. After doing some research it seems that the only way to do this in Linux is to look at /proc/net/udp. This seems like a good solution until I realized that two applications could be listening to the same multicast group and I need to tell them apart. It seems that I am supposed to do this by determining what my inode is.
I spend some time looking into this and there are suggestions that sockfd_lookup or sock_from_file is the way to go but on my CentOS Linux machine, these functions do not seem to be available.
Can someone please help me to figure out which line in /proc/net/udp belongs to my application?
I started using the ioctl (handle, FIONREAD, &bytesInBuffer) call only to discover that in Linux this only returns the size of the first datagram packet in the buffer.
Google seems to suggest that the sockfd_lookup call can be used to get the inode but a grep in my /usr/local/include/ does not return these functions.
My linux/net.h seems pretty bare-bone compared to some I can find on google which includes structs like "socket" which has the sock member which I believe has the inode information. My linux/net.h on CentOs only is 58 lines long and has only a few #defines and an enum.
after a bit of fiddling I noticed that readlink("/proc/self/fd/$fd") (under Linux 5.3) gives me something like:
socket:[3753088]
back. I can parse this and use the resulting digits to look up the relevant line in /proc/net/udp:
sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode ref pointer drops
2867: 00000000:0BB8 00000000:0000 07 00000000:00000000 00:00000000 00000000 1000 0 3753088 2 000000003ae8e911 0
that said, I don't understand why you'd want to do this, but never mind!
I'm also not sure why you don't just look up by sock&peer name, which might be easier

usb_string from user space in Linux

I have a USB Device with the following string present in the string descriptor table:
Index 0: Standard Language 0x0409 (English)
Index 1: Standard Manufacturer "xxx Corporation"
Index 2: Standard Product "xxx Board HID Device"
Index 3: Custom Sector "Bootloader" or "Application" (depending on current mode of operation).
I want to read the string at Index 3. For Windows, we have an API
HidD_GetIndexedString() which allows to get the string at any Index. What is the corresponding API in Linux? I see the driver is using usb_string to get the string in drivers but do we have any similar userspace API for usb in Linux?

Python reading a constant serial byte length from device

I have a device that sends 23 characters (numbers and alpha's) via RS232 serial in the following format:
$02 T AAAAA Q CCC PP ZZZ S RR I I NFF $0D
(the spaces in the above string are for readability only)
In this 23 character string the:
$02 represents the start of text 2 hex ( I am not sure what hex this is?)
$0D represents a Carriage Return 13 decimal.
I am currently reading this information in via Python mostly successfully but I still feel I am not doing it properly. I rarely program in Python but I have to use a raspberry PI so decided to go with python for the coding.
I setup my RPI serial port with the following function:
def setupSerialPort():
ser = serial.Serial(
port='/dev/ttyAMA0',
baudrate = 9600,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1,
xonxoff=0,
rtscts=0
)
return ser
From a while loop I read the port as follow:
# setup serial port
cSerial = setupSerialPort()
while 1:
inbuff = cSerial.inWaiting()
if inbuff > 0:
msgCOM = cSerial.read(inbuff)
#vMsgCOM = re.sub('[^A-Za-z0-9]+', '', msgCOM)
//insert value into database
sleep(1)
at which point I insert the value "vMsgCOM" or "msgCOM" string into a mysql database as I read/receive the data. At first I thought that this works pretty well but after a week of data it became clear that I sometimes only capture partial data which splits over two database rows as mentioned previously. I'll give an example:
A correct 23 char string will look like this: K00000E1120002000063B00.
Now sometimes the string is split into two rows like
(1) K00000E11200020
(2) 00063B00
Another variation of the above is the multiple 23 chunks returned as:
K00000E1120002000063B00K00000E1120002000063B00K00000E1120002000063B00
This happens roughly 15 times out of 400 reads for the above.
Can anyone help me in terms of coding to somehow ensure that I always read the buffer correctly when the 23 string arrives. I know timing can be an issue hence the timeout=1 but somehow I read to quickly (or to long) when the read is not complete.
I had a look at this code (haven't tried it yet): pySerial inWaiting returns incorrect number of bytes (the def read_all(port, chunk_size=200) function part)
but thought it best to rather ask advice from those in the know.
I have in my code a bit of corrective code to concat the two rows and split the multiple chunk event should these instance(s) happen but I still think it is not the best way of doing things.
If anyone can help me with some example code I will really appreciate it.

How to send ATR to card

I have some card, wanted to get ATR from it (using method from some SDK).
Implementation looks like this:
unsigned char ATR[128]={0};
int len=33;
int maxlen=33;
ret = sd7816_ATR(0,1,ATR,len,maxlen,1);
first, second and last parameters to sd7816_ATR function should be like that.
The length fields I tried changing to different values, including 0 but no help.
My concern is the ATR string I am sending is empty in the beginning, and I am expecting
something to get written in it after the call finishes (which actually returns success).
But after call ATR is still empty .. What can be going wrong here?
(I want to find out if card is of ISO/IEC 14443 or ISO/IEC 7816 type).
You are trying to receive an ATR for a command specific for a ISO/IEC 7816-3 contact card. In this particular case, that's requested from a (SIM form) SAM card reader. However, you are trying to read out the contactless based reader.
Now contactless cards do not have an ATR. Some cards do have an ATS (i.e. ISO/IEC 14443 Type A cards), but that should be requested by a similar 14443 SELECT command. Some cards, particularly Type B cards, contain an EF.ATR to make up for the lack of (space within the) ATR. Still, an ATR/ATS has only limited functionality for identifying cards.
ISO/IEC 7816 is comprised of several parts: parts 1 to 3 describe contact cards and 4 and higher describe the Application level APDU commands and file structure of processor cards. If your contactless card implements ISO/IEC 7816-4 then you can - in general - also directly use the PCSC interface to send and receive APDU's to/from the card.
In general readers are for contact or contactless only. If you have a reader which contains both contact and contactless operation then in general they will show up as two different readers in the operating system. So in general, if you know the reader, you know if the card is a contact card or contactless card.
SAM slots may not be identified as readers by the operating system - you may only be able to access them using a low level interface. They are mainly used as a secure storage of keys from the terminal/inspection system/interface device or whatever the name is of the system that reads out the card.

know a usb device's endpoint

Is there a bash command, a program or a libusb function (although I did not find one) which indicates me what are the OUT or IN endpoints of a usb device ?
For example, bNumEndpoints of libusb_interface_descriptor (from libusb1.0 library) shows me my usb drive has 3 endpoints, but how can I know what is their idnumber ?
After you have claimed the device, run this (where $ represents the terminal entry point):
$ sudo lsusb -v -d 16c0:05df
Where 16c0:05df are your vendor and product ids separated by a colon. (If you don't know these, type lsusb and figure out which device is yours by unplugging and re-running lsusb)
If you get confused use the lsusb man page:
http://linux.die.net/man/8/lsusb
Then once your description comes up, find the line labeled bEndpointAddress and the hex code following will be the endpoint for that specific Report.
I finally found the answer in lubusb-1.0. In was actually not a function, but a struct field :
uint8_t libusb_endpoint_descriptor::bEndpointAddress
The address of the endpoint described by this descriptor.
Bits 0:3 are the endpoint number. Bits 4:6 are reserved. Bit 7
indicates direction, see libusb_endpoint_direction.
For each interface for the usb drive, I just had to write these lines to display the available endpoints :
cout<<"Number of endpoints: "<<(int)interdesc->bNumEndpoints<<endl;
for(int k=0; k<(int)interdesc->bNumEndpoints; k++) {
epdesc = &interdesc->endpoint[k];
cout<<"Descriptor Type: "<<(int)epdesc->bDescriptorType<<endl;
cout<<"EP Address: "<<(int)epdesc->bEndpointAddress<<endl;
}
Where epdesc is the libusb_endpoint_descriptor and interdesc is the libusb_interface_descriptor.