From e712bd86ba60751ca95277be2782f759214be51a Mon Sep 17 00:00:00 2001 From: Robin Mallinson Date: Wed, 21 Nov 2012 03:15:12 +0000 Subject: [PATCH] All works as intended now. Commands can be written partially, and a single write to the device file may contain multiple commands. To achieve both of these the driver now only parses the commands on dev_close(). --- ServoBlaster/servoblaster.c | 64 ++++++++++++++--------------------- ServoBlaster/servoblaster.ko | Bin 10751 -> 10451 bytes 2 files changed, 25 insertions(+), 39 deletions(-) diff --git a/ServoBlaster/servoblaster.c b/ServoBlaster/servoblaster.c index 30dbb1c..6838839 100644 --- a/ServoBlaster/servoblaster.c +++ b/ServoBlaster/servoblaster.c @@ -51,7 +51,6 @@ #include #include #include -//#include "servoblaster.h" #define GPIO_LEN 0xb4 #define DMA_LEN 0x24 @@ -410,7 +409,6 @@ ssize_t process_command_string(const char str[]) int servo; int cnt; int n; - printk(KERN_WARNING "ServoBlasterc: str(%s)\n", str); n = sscanf(str, "%d=%d", &servo, &cnt); if (n != 2) { printk(KERN_WARNING "ServoBlaster: Failed to parse command (n=%d)\n", n); @@ -442,63 +440,51 @@ ssize_t process_command_string(const char str[]) return 0; } -// dev_write separates the user input (delimited by \n) into strings for passing -// to process_command_string() +// Read user data into buffer until full. static ssize_t dev_write(struct file *filp,const char *buf,size_t count,loff_t *f_pos) { - char* str = 0; struct process_data* const pdata = filp->private_data; if (0 == pdata) return 0; - // Read user data into str { - char* end_of_line=0; static const int max_idx = sizeof(pdata->cmd_str) - 1; int* const idx = &(pdata->cmd_idx); - str = pdata->cmd_str; - - printk(KERN_WARNING "ServoBlasterA: idx(%i),count(%u),max_idx (%i)\n", - *idx,count, max_idx - ); if ((*idx+count) > max_idx) count = max_idx-*idx; - if (copy_from_user(str+*idx, buf, count)) { + if (copy_from_user(pdata->cmd_str + *idx, buf, count)) { return -EFAULT; } *idx+=count; - str[*idx] = '\0'; - printk(KERN_WARNING "ServoBlasterB: idx(%i),count(%u),max_idx (%i) str(%s)EOS\n", - *idx, count, max_idx, str - ); - - end_of_line = strchr(str, '\n'); - if (NULL == end_of_line) - { - if (max_idx == *idx) { - // Full buf without '\n' - printk(KERN_WARNING "ServoBlaster: Failed to parse command (%s)\n", str); - return -EINVAL; - } - return count; // Incomplete line. - } - - // End of command, so terminate string at '\n' and reset idx. - *end_of_line = '\0'; - *idx=0; - } - - // Process the complete user string. - { - ssize_t error = process_command_string(str); - if (error < 0) count=error; } return count; } +// dev_close separates the user input (delimited by \n) into strings for passing +// to process_command_string() then frees up all process data static int dev_close(struct inode *inod,struct file *fil) { - if (0 != fil->private_data) kfree(fil->private_data); + struct process_data* const pdata = fil->private_data; + if (0 != pdata) + { + static const int max_idx = sizeof(pdata->cmd_str) - 1; + char* cmd_str = pdata->cmd_str; + char* command; + cmd_str[max_idx] = 0; // Ensure command string is null terminated. + + // Execute all commands. + command = strsep(&cmd_str, "\n"); + while( NULL != command ) { + if (*command != 0) { + printk(KERN_DEBUG "ServoBlaster: Command %s\n", command); + (void)process_command_string(command); + } + command = strsep(&cmd_str, "\n"); + } + + // Free process data. + kfree(pdata); + } return 0; } diff --git a/ServoBlaster/servoblaster.ko b/ServoBlaster/servoblaster.ko index 273e94b4634eef33c3d7d27c578e80b1950ede23..4569ae75dfc40ec2e9148206700bcf02d6e56477 100644 GIT binary patch delta 1937 zcmZXUe@s(X6vxkdwB#cTT@x1hQt4#n*8!V-^RKc!ywK;ENCCW&)I(UZ1+OYj%O|Nq2rtN5 zDBb~`$-uT)a=Ugt*+81E)wi@Z*_!HG8|!w|X)Q0eyi#Y2wia|rI9nK! z^6^@sv2e%m;9&2N$5QR16UD@evluek^9Mft<*3g;>hq3Z@9M^=t|*v;H;Q8zDz;;2 zr9FDXctSQl+;@ETyKS32cy&>}(iUF2zWbvuqps4ElDwFQznd!c{xtw?xY}H$S1EV_ zZZlU&wb*N}(a)|0aN#)V5u7pCNE7&zxyDdi01%VVP*y1~7UG67tE`seZi2D`-zS_f z;#op_Iet(0+lYS>j+A5B8mnwC!4`t21P^8KF=4s{zs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