Merge Network code with webserver etc
[clinton/Smoothieware.git] / src / libs / utils.cpp
index b2e26b4..72a862e 100644 (file)
@@ -13,34 +13,74 @@ using namespace std;
 using std::string;
 #include <cstring>
 
+volatile bool _isr_context = false;
 
-uint16_t get_checksum(string to_check){
+uint16_t get_checksum(const string& to_check){
+    return get_checksum(to_check.c_str());
+}
+
+uint16_t get_checksum(const char* to_check){
    // From: http://en.wikipedia.org/wiki/Fletcher%27s_checksum
    uint16_t sum1 = 0;
    uint16_t sum2 = 0;
-   for( unsigned int index = 0; index < to_check.length(); ++index ){
-      sum1 = (sum1 + to_check[index]) % 255;
+   const char* p= to_check;
+   char c;
+   while((c= *p++) != 0) {
+      sum1 = (sum1 + c) % 255;
       sum2 = (sum2 + sum1) % 255;
    }
    return (sum2 << 8) | sum1;
 }
 
-void get_checksums(uint16_t check_sums[],string key){
-    key = key.append(" ");
+void get_checksums(uint16_t check_sums[], const string key){
+    const string k = key+" ";
     check_sums[0] = 0x0000;
     check_sums[1] = 0x0000;
     check_sums[2] = 0x0000;
     size_t begin_key = 0;
     unsigned int counter = 0;
     while( begin_key < key.size()-1 ){
-        size_t end_key =  key.find_first_of(" .", begin_key);
-        string key_node = key.substr(begin_key, end_key - begin_key);
+        const size_t end_key =  k.find_first_of(" .", begin_key);
+        const string key_node = k.substr(begin_key, end_key - begin_key);
         check_sums[counter] = get_checksum(key_node);
         begin_key = end_key + 1;
         counter++;
     }
 }
 
+bool is_alpha(int c)
+{
+    if ((c >= 'a') && (c <= 'z')) return true;
+    if ((c >= 'A') && (c <= 'Z')) return true;
+    if ((c == '_')) return true;
+    return false;
+}
+
+bool is_digit(int c)
+{
+    if ((c >= '0') && (c <= '9')) return true;
+    return false;
+}
+
+bool is_numeric(int c)
+{
+    if (is_digit(c)) return true;
+    if ((c == '.') || (c == '-')) return true;
+    if ((c == 'e')) return true;
+    return false;
+}
+
+bool is_alphanum(int c)
+{
+    return is_alpha(c) || is_numeric(c);
+}
+
+bool is_whitespace(int c)
+{
+    if ((c == ' ') || (c == '\t')) return true;
+    return false;
+}
+
 // Convert to lowercase
 string lc(string str){
     for (unsigned int i=0; i<strlen(str.c_str()); i++)
@@ -74,11 +114,11 @@ string shift_parameter( string &parameters ){
 string get_arguments( string possible_command ){
     size_t beginning = possible_command.find_first_of(" ");
     if( beginning == string::npos ){ return ""; }
-    return possible_command.substr( beginning+1, possible_command.size() - beginning);
+    return possible_command.substr( beginning + 1, possible_command.size() - beginning + 1);
 }
 
 // Returns true if the file exists
-bool file_exists( string file_name ){
+bool file_exists( const string file_name ){
     bool exists = false;
     FILE *lp = fopen(file_name.c_str(), "r");
     if(lp){ exists = true; }
@@ -86,13 +126,17 @@ bool file_exists( string file_name ){
     return exists;
 }
 
-// Prepares and executes a watchdog reset
-void system_reset( void ){
-    LPC_WDT->WDCLKSEL = 0x1;                // Set CLK src to PCLK
-    uint32_t clk = SystemCoreClock / 16;    // WD has a fixed /4 prescaler, PCLK default is /4
-    LPC_WDT->WDTC = 1 * (float)clk;         // Reset in 1 second
-    LPC_WDT->WDMOD = 0x3;                   // Enabled and Reset
-    LPC_WDT->WDFEED = 0xAA;                 // Kick the dog!
-    LPC_WDT->WDFEED = 0x55;
+// Prepares and executes a watchdog reset for dfu or reboot
+void system_reset( bool dfu ){
+    if(dfu) {
+        LPC_WDT->WDCLKSEL = 0x1;                // Set CLK src to PCLK
+        uint32_t clk = SystemCoreClock / 16;    // WD has a fixed /4 prescaler, PCLK default is /4
+        LPC_WDT->WDTC = 1 * (float)clk;         // Reset in 1 second
+        LPC_WDT->WDMOD = 0x3;                   // Enabled and Reset
+        LPC_WDT->WDFEED = 0xAA;                 // Kick the dog!
+        LPC_WDT->WDFEED = 0x55;
+    }else{
+        NVIC_SystemReset();
+    }
 }