If you can't read please download the document
Upload
dingxin-xu
View
4.163
Download
6
Tags:
Embed Size (px)
DESCRIPTION
An introduction to Contiki, which is a famous open source operating system for WSN.
Citation preview
2. Outline
3. Hello world /* Declare the process */ PROCESS(hello_world_process, Hello world); /* Make the process start when the module is loaded */ AUTOSTART_PROCESSES(&hello_world_process); /* Define the process code */ PROCESS_THREAD(hello_world_process, ev, data) { PROCESS_BEGIN();/* Must always come first */ printf(Hello, world!n);/* Initialization code goes here */ while(1) {/* Loop for ever */ PROCESS_WAIT_EVENT();/* Wait for something to happen */ } PROCESS_END();/* Must always come last */ } 4. Contiki process and protothread
5. Contiki Process 6. Contiki Process 7. Process Implementation: PT_THREAD
#define PROCESS_THREAD(name, ev, data)static PT_THREAD(process_thread_##name(struct pt *process_pt,process_event_t ev,process_data_t data)) #define PT_THREAD(name_args)char name_args static char process_thread_##name(.) 8. Inside a Process: protothread 9. Protothread implementation: C-switchint a_protothread(struct pt *pt) { PT_BEGIN(pt); PT_WAIT_UNTIL(pt, condition1); if(something) { PT_WAIT_UNTIL(pt, condition2); }PT_END(pt); } int a_protothread(struct pt *pt) { switch(pt->lc) { case 0: pt->lc = 5; case 5: if(!condition1) return 0; if(something) { pt->lc = 10; case 10: if(!condition2) return 0; }} return 1; } Line numbers 10. Why protothread? 11. Event-driven state machine implementation: messy enum {ON, WAITING, OFF} state; void eventhandler() { if(state == ON) { if(expired(timer)) { timer = t_sleep; if(!comm_complete()) { state = WAITING; wait_timer = t_wait_max; } else { radio_off(); state = OFF; } } } else if(state == WAITING) { if(comm_complete() || expired(wait_timer)) { state = OFF; radio_off(); } } else if(state == OFF) { if(expired(timer)) { radio_on(); state = ON; timer = t_awake; } } }Radio on t 0 t awake t wait_max t sleep Radio off Communicationleft ON OFF WAITING Timer expires Timer expires Timer expires Communication completes, timer expires 12. Protothreads-based implementation is shorter int protothread(struct pt *pt) { PT_BEGIN(pt); while(1) { radio_on(); timer = t_awake; PT_WAIT_UNTIL(pt, expired(timer)); timer = t_sleep; if(!comm_complete()) { wait_timer = t_wait_max; PT_WAIT_UNTIL(pt, comm_complete()|| expired(wait_timer)); } radio off(); PT_WAIT_UNTIL(pt, expired(timer)); } PT_END(pt); }
Radio on t 0 t awake t wait_max t sleep Radio off Communicationleft 13. Outline
14. Four types of Timers
15. Outline
16. Communication programming: before Rime 17. Rime: Sockets for WSN 18. Chameleon: Adapting to underlying layers(link, MAC) 19. Rime/Chameleon:the whole picture 20. How to communicate with Rime 21. Underlying Rime 22. Programming with Rime: example 23. Thats All