Pcd driver with multiple devices lseek implementation
Now, let’s test our lseek method. For that, just type man lseek( as shown in Figure 1) to understand how to use lseek.
So, lseek takes three arguments you already know this. The first one is the fd (that is the file descriptor), and offset, and whence information as shown in Figure 2.
The whence information could be anything. SEEK_SET, SEEK_CUR, or SEEK_END. Suppose, if it is SEEK_END, let’s say. The file offset is set to the size of the file plus offset bytes. Here, you have to mention how many bytes you want to seek with reference to this whence information.
Let’s try this. Let’s go to our application and activate this code block( shown in Figure 3).
Activate this for lseek testing. So, let’s try to seek for 10 position, let’s say, from the end. And from the end, the file position tries to go to 10 position further. That is not allowed for our device. Because our devices has some limitation. It has got fixed of memory.
So, once you reach the end of the memory, you cannot seek further. That’s the end. That’s why I think I would expect our driver to return the error code here.
If this fails, then there is a problem. So, if return is less than 0, lseek out. Here, out you can do close_fd here. close_fd target you can give. I would give close_fd here.
You have to close_fd first, then return.
Before testing, remember that, so we have to initialize our drivers lseek method in the file operation structure of our driver.
That’s why open your driver source code, go to the file operation structure, and you have to initialize the function pointer for the lseek member element of this structure. .llseek = pcd_lseek. This you have to include.
And after that, unload the driver. Build the project once again and then load back. And now, let’s compile our application, and let’s execute.
Then load back as shown in Figure 9.
Here you can see that the open was successful, but the lseek failed. That’s a invalid argument. Because you are trying to seek 10 positions further from the device end, and that’s not allowed for a Driver. I mean, this is entirely driver-dependent.
It depends on how you code your driver. So, now let’s change our application. And let’s do something else here, SEEK_SET. So, you can do SEEK_SET.
Let’s see what happens.
There is no data in the device actually.
Let’s put some data. And now, let’s try to do this. So, you can see that the position was first move to 10 bytes away from the beginning. You can see that the output is not from the beginning of this line.
Output is actually at the offset of 10. That’s why, first, the file position is adjusted to the offset, which we mentioned that is 10 offsets. And from there, it printed the data.
What if I give -10 here? (Shown in Figure 14).
Let’s see whether it takes that condition or not. That’s a invalid argument, as shown in Figure 15.
I think our driver checks that. Try with different combinations of user-level system calls, and with that note, I would like to end this article here and let me know if you have any questions. I will see you in the following article.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1