Merge changes I6e276f31,I63859b5b

* changes:
  wifi_files: don't touch update.new too often.
  wifi_files: escape SSID for JSON.
diff --git a/cmds/wifi_files.c b/cmds/wifi_files.c
index 31f9d4a..49e77c6 100644
--- a/cmds/wifi_files.c
+++ b/cmds/wifi_files.c
@@ -777,26 +777,6 @@
 }
 
 
-static void TouchUpdateFile()
-{
-  char filename[PATH_MAX];
-  int fd;
-
-  snprintf(filename, sizeof(filename), "%s/updated.new", STATIONS_DIR);
-  if ((fd = open(filename, O_CREAT | O_WRONLY, 0666)) < 0) {
-    perror("TouchUpdatedFile open");
-    exit(1);
-  }
-
-  if (write(fd, "updated", 7) < 7) {
-    perror("TouchUpdatedFile write");
-    exit(1);
-  }
-
-  close(fd);
-} /* TouchUpdateFile */
-
-
 static void ClientStateToLog(gpointer key, gpointer value, gpointer user_data)
 {
   const client_state_t *state = (const client_state_t *)value;
@@ -850,7 +830,6 @@
 void UpdateAssociatedDevices()
 {
   g_hash_table_foreach(clients, ClientStateToJson, NULL);
-  TouchUpdateFile();
 }
 
 
@@ -887,12 +866,21 @@
   int i;
 
   for (i = 0; i < len; i++) {
-    if (isprint(data[i]) && data[i] != ' ' && data[i] != '\\')
-      fprintf(f, "%c", data[i]);
-    else if (data[i] == ' ' && (i != 0 && i != len -1))
-      fprintf(f, " ");
-    else
-      fprintf(f, "\\x%.2x", data[i]);
+    switch(data[i]) {
+      case '\\': fprintf(f, "\\\\"); break;
+      case '"': fprintf(f, "\\\""); break;
+      case '\b': fprintf(f, "\\b"); break;
+      case '\f': fprintf(f, "\\f"); break;
+      case '\n': fprintf(f, "\\n"); break;
+      case '\r': fprintf(f, "\\r"); break;
+      case '\t': fprintf(f, "\\t"); break;
+      default:
+        if ((data[i] <= 0x1f) || !isprint(data[i])) {
+          fprintf(f, "\\u00%02x", data[i]);
+        } else {
+          fprintf(f, "%c", data[i]); break;
+        }
+    }
   }
 }
 
@@ -1027,6 +1015,26 @@
 }
 
 #ifndef UNIT_TESTS
+static void TouchUpdateFile()
+{
+  char filename[PATH_MAX];
+  int fd;
+
+  snprintf(filename, sizeof(filename), "%s/updated.new", STATIONS_DIR);
+  if ((fd = open(filename, O_CREAT | O_WRONLY, 0666)) < 0) {
+    perror("TouchUpdatedFile open");
+    exit(1);
+  }
+
+  if (write(fd, "updated", 7) < 7) {
+    perror("TouchUpdatedFile write");
+    exit(1);
+  }
+
+  close(fd);
+} /* TouchUpdateFile */
+
+
 int main(int argc, char **argv)
 {
   int done = 0;
diff --git a/cmds/wifi_files_test.c b/cmds/wifi_files_test.c
index bb0043c..9d48dd1 100644
--- a/cmds/wifi_files_test.c
+++ b/cmds/wifi_files_test.c
@@ -34,8 +34,8 @@
 {
   FILE *f = tmpfile();
   char buf[32];
-  const uint8_t ssid[] = {'a', 'b', 0x86, ' ', 'c'};  /* not NUL terminated. */
-  const uint8_t expected[] = {'a', 'b', '\\', 'x', '8', '6', ' ', 'c'};
+  const uint8_t ssid[] = {'b', 0x86, ' ', 'c'};  /* not NUL terminated. */
+  const uint8_t expected[] = {'b', '\\', 'u', '0', '0', '8', '6', ' ', 'c'};
 
   printf("Testing \"%s\" in %s:\n", __FUNCTION__, __FILE__);
   memset(buf, 0, sizeof(buf));