Contiki 2.5
checkpoint-arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Checkpoint library implementation for the Tmote Sky platform.
36  *
37  * \author
38  * Fredrik Osterlind <fros@sics.se>
39  */
40 
41 #include "contiki.h"
42 
43 #include "lib/crc16.h"
44 #include "lib/checkpoint.h"
45 
46 #include "sys/rtimer.h"
47 #include "sys/mt.h"
48 #include "sys/energest.h"
49 #include "sys/compower.h"
50 #include "dev/leds.h"
51 #include "dev/watchdog.h"
52 #include "dev/serial-line.h"
53 #include "dev/uart1.h"
54 #include "dev/cc2420.h"
55 #include "dev/button-sensor.h"
56 #include "cfs/cfs.h"
57 #include "cfs/cfs-coffee.h"
58 #include <stdio.h>
59 
60 #define DEBUG 0
61 #if DEBUG
62 #define PRINTF(...) printf(__VA_ARGS__)
63 #else
64 #define PRINTF(...)
65 #endif
66 
67 #ifndef CHECKPOINT_ROLLBACK_BUTTON
68 #define CHECKPOINT_ROLLBACK_BUTTON 1 /* rollback "cp_wdt" on button click */
69 #endif /* CHECKPOINT_ROLLBACK_BUTTON */
70 
71 #if CHECKPOINT_ROLLBACK_BUTTON
72 PROCESS(checkpoint_button_process, "Rollback on button");
73 #endif /* CHECKPOINT_ROLLBACK_BUTTON */
74 
75 #define WITH_SERIAL_COMMANDS 0 /* checkpoint via serial port */
76 #if WITH_SERIAL_COMMANDS
77 #if UART1_CONF_TX_WITH_INTERRUPT
78 #error TX_WITH_INTERRUPTS must be 0
79 #endif /* UART1_CONF_TX_WITH_INTERRUPT */
80 #define PRINTF_COMMAND(...) printf(__VA_ARGS__)
81 #else /* WITH_SERIAL_COMMANDS */
82 #define PRINTF_COMMAND(...)
83 #endif /* WITH_SERIAL_COMMANDS */
84 
85 #define COMMAND_ROLLBACK 1
86 #define COMMAND_CHECKPOINT 2
87 #define COMMAND_METRICS 3
88 
89 #define INCLUDE_RAM 1 /* Less then 10240 bytes */
90 #define INCLUDE_TIMERS 1 /* 16 bytes */
91 #define INCLUDE_LEDS 1 /* 1 bytes */
92 /* ... */
93 
94 /* 10kb memory */
95 #define RAM_START 0x1100
96 #define RAM_END 0x3900
97 
98 #define PAUSE_TIME() \
99  TACTL &= ~(MC1); \
100  TBCTL &= ~(MC1); \
101  watchdog_stop();
102 #define RESUME_TIME() \
103  TACTL |= MC1; \
104  TBCTL |= MC1; \
105  TACCR1 = clock_fine_max(); \
106  watchdog_start();
107 #define PAUSE_TIME_INT() \
108  dint(); \
109  PAUSE_TIME();
110 #define RESUME_TIME_INT() \
111  RESUME_TIME(); \
112  eint();
113 
114 static struct mt_thread checkpoint_thread;
115 static uint8_t preset_cmd;
116 static int preset_fd;
117 
118 /* bookkeeping */
119 #if WITH_SERIAL_COMMANDS
120 static int nr_pongs=0;
121 #endif /* WITH_SERIAL_COMMANDS */
122 static int nr_checkpoints=0, nr_rollbacks=0, nr_metrics=0;
123 
124 /*---------------------------------------------------------------------------*/
125 typedef union {
126  unsigned char u8[2];
127  unsigned short u16;
128 } word_union_t;
129 /*---------------------------------------------------------------------------*/
130 static int
131 write_byte(int fd, uint8_t c)
132 {
133  return cfs_write(fd, &c, 1);
134 }
135 /*---------------------------------------------------------------------------*/
136 static void
137 write_word(int fd, uint16_t w)
138 {
139  word_union_t tmp;
140  tmp.u16 = w;
141  write_byte(fd, tmp.u8[0]);
142  write_byte(fd, tmp.u8[1]);
143 }
144 /*---------------------------------------------------------------------------*/
145 static uint8_t
146 read_byte(int fd)
147 {
148  uint8_t c;
149  cfs_read(fd, &c, 1);
150  return c;
151 }
152 /*---------------------------------------------------------------------------*/
153 static uint16_t
154 read_word(int fd)
155 {
156  word_union_t tmp;
157  tmp.u8[0] = read_byte(fd);
158  tmp.u8[1] = read_byte(fd);
159  return tmp.u16;
160 }
161 /*---------------------------------------------------------------------------*/
162 static void
163 thread_checkpoint(int fd)
164 {
165 #if INCLUDE_RAM
166  unsigned char *addr;
167  uint16_t size = 0;
168  unsigned char *thread_mem_start = (unsigned char *)&checkpoint_thread.thread.stack;
169  unsigned char *thread_mem_end = thread_mem_start + sizeof(checkpoint_thread.thread.stack) - 1;
170  unsigned char *coffee_mem_start = cfs_coffee_get_protected_mem(&size);
171  unsigned char *coffee_mem_end = coffee_mem_start + size - 1;
172 #endif /* INCLUDE_RAM */
173 
174  /*PRINTF("protected thread memory: %u, size=%u\n", (uint16_t) thread_mem_start, sizeof(checkpoint_thread.thread.stack));*/
175  /*PRINTF("protected coffee memory: %u, size=%u\n", (uint16_t) coffee_mem_start, size);*/
176 
177  /* RAM */
178 #if INCLUDE_RAM
179  for(addr = (unsigned char *)RAM_START;
180  addr < (unsigned char *)RAM_END;
181  addr++) {
182 
183  if((addr >= thread_mem_start && addr <= thread_mem_end)) {
184  /* Skip */
185  continue;
186  }
187 
188  if((addr >= coffee_mem_start && addr <= coffee_mem_end)) {
189  /* Skip */
190  continue;
191  }
192 
193  /* TODO Use write_array() */
194  write_byte(fd, *addr);
195 
196  /*if(((int)addr % 512) == 0) {
197  PRINTF(".");
198  }*/
199  }
200 
201 #endif /* INCLUDE_RAM */
202 
203  /* Timers */
204 #if INCLUDE_TIMERS
205  write_word(fd, TACTL);
206  write_word(fd, TACCTL1);
207  write_word(fd, TACCR1);
208  write_word(fd, TAR);
209 
210  write_word(fd, TBCTL);
211  write_word(fd, TBCCTL1);
212  write_word(fd, TBCCR1);
213  write_word(fd, TBR);
214 #endif /* INCLUDE_TIMERS */
215 
216  /* LEDs */
217 #if INCLUDE_LEDS
218  write_byte(fd, leds_arch_get());
219 #endif /* INCLUDE_LEDS */
220 
221  /* Radio */
222  /* ADC */
223  /* ... */
224 
225  write_byte(fd, -1); /* Coffee padding byte */
226 }
227 /*---------------------------------------------------------------------------*/
228 static void
229 thread_rollback(int fd)
230 {
231 #if INCLUDE_RAM
232  unsigned char *addr;
233  uint16_t size = 0;
234  unsigned char *thread_mem_start = (unsigned char *)&checkpoint_thread.thread.stack;
235  unsigned char *thread_mem_end = thread_mem_start + sizeof(checkpoint_thread.thread.stack) - 1;
236  unsigned char *coffee_mem_start = cfs_coffee_get_protected_mem(&size);
237  unsigned char *coffee_mem_end = coffee_mem_start + size - 1;
238 #endif /* INCLUDE_RAM */
239 
240  /*PRINTF("protected thread memory: %u, size=%u\n", (uint16_t) thread_mem_start, sizeof(checkpoint_thread.thread.stack));*/
241  /*PRINTF("protected coffee memory: %u, size=%u\n", (uint16_t) coffee_mem_start, size);*/
242 
243  /* RAM */
244 #if INCLUDE_RAM
245  for(addr = (unsigned char *)RAM_START;
246  addr < (unsigned char *)RAM_END;
247  addr++) {
248  if((addr >= thread_mem_start && addr <= thread_mem_end)) {
249  /* Skip */
250  continue;
251  }
252 
253  if((addr >= coffee_mem_start && addr <= coffee_mem_end)) {
254  /* Skip */
255  continue;
256  }
257 
258  *addr = read_byte(fd);
259  }
260 #endif /* INCLUDE_RAM */
261 
262  /* Timers */
263 #if INCLUDE_TIMERS
264  TACTL = read_word(fd);
265  TACCTL1 = read_word(fd);
266  TACCR1 = read_word(fd);
267  TAR = read_word(fd);
268 
269  TBCTL = read_word(fd);
270  TBCCTL1 = read_word(fd);
271  TBCCR1 = read_word(fd);
272  TBR = read_word(fd);
273 #endif /* INCLUDE_TIMERS */
274 
275  /* LEDs */
276 #if INCLUDE_LEDS
277  leds_arch_set(read_byte(fd));
278 #endif /* INCLUDE_LEDS */
279 
280  /* Radio */
281  /* ADC */
282  /* ... */
283 
284  read_byte(fd); /* Coffee padding byte */
285 }
286 /*---------------------------------------------------------------------------*/
287 #if WITH_SERIAL_COMMANDS
288 static uint32_t
289 thread_metric_tx(void)
290 {
291  energest_flush();
292  return energest_type_time(ENERGEST_TYPE_TRANSMIT);
293 }
294 /*---------------------------------------------------------------------------*/
295 static uint32_t
296 thread_metric_rx(void)
297 {
298  energest_flush();
299  return energest_type_time(ENERGEST_TYPE_LISTEN);
300 }
301 #endif /* WITH_SERIAL_COMMANDS */
302 /*---------------------------------------------------------------------------*/
303 static void
304 thread_metrics(void)
305 {
306  PRINTF_COMMAND("METRICS:START\n");
307  PRINTF_COMMAND("M:RTIMER_NOW:%u\n", RTIMER_NOW()); /* TODO extract */
308  PRINTF_COMMAND("M:ENERGY_TX:%lu\n", thread_metric_tx());
309  PRINTF_COMMAND("M:ENERGY_RX:%lu\n", thread_metric_rx());
310  PRINTF_COMMAND("M:RTIMER_NOW2:%u\n", RTIMER_NOW());
311  nr_metrics++;
312  PRINTF_COMMAND("METRICS:DONE %u\n", nr_metrics);
313 }
314 /*---------------------------------------------------------------------------*/
315 static void
316 checkpoint_thread_loop(void *data)
317 {
318  uint8_t cmd;
319  int fd;
320 
321  while(1) {
322  /* Store command and file descriptor on stack */
323  cmd = preset_cmd;
324  fd = preset_fd;
325 
326  /* Handle command */
327  if(cmd == COMMAND_ROLLBACK) {
328  PRINTF_COMMAND("RB:START\n");
329  thread_rollback(fd);
330  nr_rollbacks++;
331  PRINTF_COMMAND("RB:DONE %u\n", nr_rollbacks);
332  /* TODO Synch before leaving this thread. */
333  } else if(cmd == COMMAND_CHECKPOINT) {
334  PRINTF_COMMAND("CP:START\n");
335  thread_checkpoint(fd);
336  thread_metrics();
337  nr_checkpoints++;
338  PRINTF_COMMAND("CP:DONE %u\n", nr_checkpoints);
339  } else if(cmd == COMMAND_METRICS) {
340  thread_metrics();
341  } else {
342  printf("ERROR: Unknown thread command: %u\n", cmd);
343  }
344 
345  /* Return to Contiki */
346  mt_yield();
347  }
348 }
349 /*---------------------------------------------------------------------------*/
350 int
351 checkpoint_arch_size()
352 {
353  return 10258;
354 }
355 /*---------------------------------------------------------------------------*/
356 void
357 checkpoint_arch_checkpoint(int fd)
358 {
359  PAUSE_TIME_INT();
360 
361  preset_cmd = COMMAND_CHECKPOINT;
362  preset_fd = fd;
363  mt_exec(&checkpoint_thread);
364 
365  RESUME_TIME_INT();
366 }
367 /*---------------------------------------------------------------------------*/
368 void
369 checkpoint_arch_rollback(int fd)
370 {
371  PAUSE_TIME_INT();
372 
373  preset_cmd = COMMAND_ROLLBACK;
374  preset_fd = fd;
375  mt_exec(&checkpoint_thread);
376 
377  RESUME_TIME_INT();
378 }
379 /*---------------------------------------------------------------------------*/
380 static uint8_t inited = 0;
381 void
382 checkpoint_arch_init(void)
383 {
384  if(inited) {
385  return;
386  }
387 
388  mt_init();
389  mt_start(&checkpoint_thread, checkpoint_thread_loop, NULL);
390  inited = 1;
391 
392 #if CHECKPOINT_ROLLBACK_BUTTON
393  process_start(&checkpoint_button_process, NULL);
394 #endif /* CHECKPOINT_ROLLBACK_BUTTON */
395 
396  /*mt_stop(&checkpoint_thread);*/
397  /*mt_remove();*/
398 }
399 /*---------------------------------------------------------------------------*/
400 struct power_log {
401  uint32_t transmit;
402  uint32_t listen;
403 };
404 /*---------------------------------------------------------------------------*/
405 #if WITH_SERIAL_COMMANDS
406 static void
407 serial_interrupt_checkpoint()
408 {
409  int fd = 0;
410  PAUSE_TIME();
411 
412  if(SPI_IS_ENABLED()) {
413  /* SPI is busy, abort */
414  PRINTF_COMMAND("CP:SPIBUSY\n");
415  RESUME_TIME();
416  return;
417  }
418 
419  /* Open file */
420  cfs_remove("cp");
421  cfs_coffee_reserve("cp", checkpoint_arch_size());
422  fd = cfs_open("cp", CFS_WRITE);
423 
424  if(fd < 0) {
425  printf("ERROR: No file access (cp)\n");
426  RESUME_TIME();
427  return;
428  }
429 
430  /* Checkpoint */
431  preset_cmd = COMMAND_CHECKPOINT;
432  preset_fd = fd;
433  mt_exec(&checkpoint_thread);
434 
435  /* Close file */
436  cfs_close(fd);
437 
438  RESUME_TIME();
439 }
440 /*---------------------------------------------------------------------------*/
441 static void
442 serial_interrupt_rollback()
443 {
444  int fd = 0;
445  PAUSE_TIME();
446 
447  if(SPI_IS_ENABLED()) {
448  /* SPI is busy, abort */
449  PRINTF_COMMAND("RB:SPIBUSY\n");
450  RESUME_TIME();
451  return;
452  }
453 
454  /* Open file */
455  fd = cfs_open("cp", CFS_READ);
456 
457  if(fd < 0) {
458  printf("ERROR: No file access (rb)\n");
459  RESUME_TIME();
460  return;
461  }
462 
463  /* Rollback */
464  preset_cmd = COMMAND_ROLLBACK;
465  preset_fd = fd;
466  mt_exec(&checkpoint_thread);
467 
468  /* Close file */
469  cfs_close(fd);
470 
471  RESUME_TIME();
472 }
473 /*---------------------------------------------------------------------------*/
474 static void
475 serial_interrupt_metrics()
476 {
477  PAUSE_TIME();
478 
479  preset_cmd = COMMAND_METRICS;
480  preset_fd = -1;
481  mt_exec(&checkpoint_thread);
482 
483  RESUME_TIME();
484 }
485 /*---------------------------------------------------------------------------*/
486 static const unsigned char command_checkpoint[] = { 'c', 'p', '\n' };
487 static const unsigned char command_rollback[] = { 'r', 'b', '\n' };
488 static const unsigned char command_metrics[] = { 'm', 't', '\n' };
489 static volatile int command_checkpoint_state = 0;
490 static volatile int command_rollback_state = 0;
491 static volatile int command_metrics_state = 0;
492 /*---------------------------------------------------------------------------*/
493 static int
494 serial_input_byte_intercept(unsigned char c)
495 {
496  /* Detect checkpoint request */
497  if(command_checkpoint[command_checkpoint_state] == c) {
498  command_checkpoint_state++;
499 
500  if(command_checkpoint_state == sizeof(command_checkpoint)) {
501  serial_interrupt_checkpoint();
502  command_checkpoint_state = 0;
503  }
504  } else {
505  command_checkpoint_state = 0;
506  }
507 
508  /* Detect rollback request */
509  if(command_rollback[command_rollback_state] == c) {
510  command_rollback_state++;
511 
512  if(command_rollback_state == sizeof(command_rollback)) {
513  serial_interrupt_rollback();
514  command_rollback_state = 0;
515  }
516  } else {
517  command_rollback_state = 0;
518  }
519 
520  /* Detect metrics request */
521  if(command_metrics[command_metrics_state] == c) {
522  command_metrics_state++;
523 
524  if(command_metrics_state == sizeof(command_metrics)) {
525  serial_interrupt_metrics();
526  command_metrics_state = 0;
527  }
528  } else {
529  command_metrics_state = 0;
530  }
531 
532  /* Forward to serial line input byte */
533  return serial_line_input_byte(c);
534 }
535 /*---------------------------------------------------------------------------*/
536 static void
537 handle_get_command(void)
538 {
539  int fd = 0;
540  fd = cfs_open("cp", CFS_READ);
541  if(fd < 0) {
542  printf("ERROR: No file access (get)\n");
543  } else {
544  PRINTF_COMMAND("GET:START\n");
545  char data[8];
546  int offset=0, size=0, read=8;
547  unsigned short crc = 0;
548  cfs_seek(fd, offset, CFS_SEEK_SET);
549 
550  while (read == 8) {
551  int i;
552 
553  /*if(offset != cfs_seek(fd, offset, CFS_SEEK_SET)) {
554  printf("bad seek, breaking\n");
555  break;
556  }*/
557  read = cfs_read(fd, data, 8);
558  size += read;
559 
560  printf("%04i: ", offset); /*REMOVE*/
561  for (i=0; i < read; i++) {
562  crc = crc16_add((uint8_t) data[i], crc);
563  printf("%02x", (uint8_t) (0xff&data[i]));
564  }
565  printf("\n");
566 
567  offset += 8;
568  }
569 
570  PRINTF_COMMAND("GET:DONE CRC=%u\n", crc);
571  cfs_close(fd);
572  }
573 }
574 /*---------------------------------------------------------------------------*/
575 static int
576 hex_decode_char(char c)
577 {
578  if(c >= 'A' && c <= 'F') {
579  return c - 'A' + 10;
580  } else if(c >= 'a' && c <= 'f') {
581  return c - 'a' + 10;
582  } else if(c >= '0' && c <= '9') {
583  return c - '0';
584  } else {
585  printf("WARN: bad hex: %c\n", c);
586  return 0;
587  }
588 }
589 /*---------------------------------------------------------------------------*/
590 PROCESS(checkpoint_serial_process, "Checkpoint via serial commands");
591 PROCESS_THREAD(checkpoint_serial_process, ev, data)
592 {
593  static int set_fd = -1;
594  static int set_count = -1;
595 
596  PROCESS_BEGIN();
597 
598  /* Note: 'cp', 'rb', and 'mt' commands are intercepted */
599  PROCESS_PAUSE();
600  uart1_set_input(serial_input_byte_intercept);
601 
602  /* Format Coffee? */
603  PRINTF("Formatting Coffee\n");
605  PRINTF("Formatting Coffee... done!\n");
606 
607  while(1) {
608  PROCESS_WAIT_EVENT_UNTIL(ev == serial_line_event_message && data != NULL);
609 
610  if(strcmp("set", data) == 0) {
611  /* TODO Handle set command */
612  /* Open file */
613  cfs_remove("cp");
614  cfs_coffee_reserve("cp", checkpoint_arch_size());
615  set_fd = cfs_open("cp", CFS_WRITE);
616  set_count = 0;
617  if(set_fd < 0) {
618  printf("SET:FSBUSY\n");
619  } else {
620  printf("SET:LINE\n");
621  }
622  } else if(set_fd >= 0 && strcmp("set:done", data) == 0) {
623  cfs_close(set_fd);
624  set_fd = -1;
625  if(set_count == 9862) {
626  printf("SET:DONE\n");
627  } else {
628  printf("SET:WRONGSIZE\n");
629  }
630  } else if(set_fd >= 0) {
631  /* We are ready for another line */
632  printf("SET:LINE\n");
633  /* Set command: parse hex data */
634  int len = strlen((char*)data);
635  if(len > 16 || (len%2)!=0) {
636  printf("WARN: bad set data: %s\n", (char*)data);
637  } else {
638  int i;
639  for (i=0; i < len; i+=2) {
640  uint8_t b =
641  (hex_decode_char(((char*)data)[i]) << 4) +
642  (hex_decode_char(((char*)data)[i+1]));
643 
644  PRINTF("Parsing set command: writing to CFS: %02x\n", b);
645  write_byte(set_fd, b); /* TODO Check return value */
646  set_count++;
647  }
648  }
649  } else if(strcmp("", data) == 0 ||
650  strcmp("cp", data) == 0 ||
651  strcmp("rb", data) == 0 ||
652  strcmp("mt", data) == 0) {
653  /* ignore commands: handled by interrupt */
654  } else if(strcmp("ping", data) == 0) {
655  nr_pongs++;
656  printf("pong %u\n", nr_pongs);
657  } else if(strcmp("get", data) == 0) {
658  handle_get_command();
659  } else {
660  printf("WARN: Unknown command: '%s'\n", (char*)data);
661  }
662  }
663 
664  PROCESS_END();
665 }
666 #endif /* WITH_SERIAL_COMMANDS */
667 /*---------------------------------------------------------------------------*/
668 #if CHECKPOINT_ROLLBACK_BUTTON
669 PROCESS_THREAD(checkpoint_button_process, ev, data)
670 {
671  PROCESS_BEGIN();
672 
673  button_sensor.configure(SENSORS_ACTIVE, 1);
674 
675  while(1) {
677 
678  if(ev == sensors_event && data == &button_sensor) {
679  int fd = 0;
680 
681  /* Rollback from Coffee file "cp_wdt" */
682  fd = cfs_open("cp_wdt", CFS_READ);
683  if(fd >= 0) {
684  checkpoint_rollback(fd);
685  cfs_close(fd);
686  }
687  }
688  }
689 
690  PROCESS_END();
691 }
692 #endif /* CHECKPOINT_ROLLBACK_BUTTON */