Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #ifndef devwrite__c
  2. #define devwrite__c
  3.  
  4. #include <linux/init.h> // Macros used to mark up functions e.g. __init __exit
  5. #include <linux/module.h> // Core header for loading LKMs into the kernel
  6. #include <linux/device.h> // Header to support the kernel Driver Model
  7. #include <linux/kernel.h> // Contains types, macros, functions for the kernel
  8. #include <linux/fs.h> // Header for the Linux file system support
  9. #include <asm/uaccess.h> // Required for the copy to user function
  10. #include <linux/slab.h>
  11.  
  12. #include "outpmodes.c"
  13.  
  14. /** @brief This function is called whenever the device is being written to from user space i.e.
  15. * data is sent to the device from the user. The data is copied to the message[] array in this
  16. * LKM using the sprintf() function along with the length of the string.
  17. * @param filep A pointer to a file object
  18. * @param buffer The buffer to that contains the string to write to the device
  19. * @param len The length of the array of data that is being passed in the const char buffer
  20. * @param offset The offset if required
  21. */
  22. static ssize_t dev_write(struct file *filep, const char *buffer, size_t len, loff_t *offset){
  23. //sprintf(message, "%s(%zu letters)", buffer, len); // appending received string with its length
  24. char tmpMsg[256] = {0};
  25. copy_from_user(tmpMsg,buffer, len);
  26. if(strncmp(tmpMsg, forward, strlen(forward)) == 0){
  27. direction = 1;
  28. printk(KERN_INFO "Forward direction");
  29. return strlen(tmpMsg);
  30. } else if(strncmp(tmpMsg, back, strlen(back)) == 0){
  31. direction = -1;
  32. printk(KERN_INFO "Back direction");
  33. return strlen(tmpMsg);
  34. } else if(strncmp(tmpMsg, delete, strlen(delete)) == 0){
  35. printk(KERN_INFO "Delete");
  36. message[0] = 0;
  37. size_of_message = 0;
  38. return strlen(tmpMsg);
  39. }
  40. strcpy(message, tmpMsg);
  41. size_of_message = len; // store the length of the stored message
  42. *offset += len;
  43. printk(KERN_INFO "EBBChar: Received %zu characters from the user\n", len);
  44. return len;
  45. }
  46. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement