Contiki 3.x
cmdd.c
1 #include "contiki.h"
2 #include "contiki-lib.h"
3 #include "contiki-net.h"
4 
5 #include <string.h>
6 
7 struct cmdd_state {
8  struct psock s;
9  char inputbuf[8];
10  struct timer timer;
11  int i;
12  char command;
13 };
14 
15 #define COMMAND_NONE 0
16 #define COMMAND_PS 1
17 
18 MEMB(conns, struct cmdd_state, 1);
19 
20 PROCESS(cmdd_process, "Command server");
21 
22 static struct uip_udp_conn *udpconn;
23 static char send_udp = 0;
24 static const char *prompt = "contiki> ";
25 
26 /*---------------------------------------------------------------------------*/
27 static char * CC_FASTCALL
28 n(uint16_t num, char *ptr)
29 {
30  uint16_t d;
31  uint8_t a, f;
32 
33  if(num == 0) {
34  *ptr = '0';
35  return ptr + 1;
36  } else {
37  f = 0;
38  for(d = 10000; d >= 1; d /= 10) {
39  a = (num / d) % 10;
40  if(f == 1 || a > 0) {
41  *ptr = a + '0';
42  ++ptr;
43  f = 1;
44  }
45  }
46  }
47  return ptr;
48 }
49 /*---------------------------------------------------------------------------*/
50 /*static unsigned short
51 ps_generate(void *state)
52 {
53  struct ek_proc *p = (struct ek_proc *)state;
54  char *ptr = (char *)uip_appdata;
55 
56  ptr = n(EK_PROC_ID(p), ptr);
57 
58  *ptr++ = ' ';
59  strncpy(ptr, p->name, 40);
60  ptr += strlen(p->name);
61  *ptr++ = '\n';
62 
63  return ptr - (char *)uip_appdata;
64 }*/
65 /*---------------------------------------------------------------------------*/
66 static
67 PT_THREAD(handle_connection(struct cmdd_state *s))
68 {
69  PSOCK_BEGIN(&s->s);
70 
71  while(1) {
72  PSOCK_SEND(&s->s, prompt, strlen(prompt));
73  PSOCK_WAIT_UNTIL(&s->s, PSOCK_NEWDATA(&s->s) ||
74  s->command != COMMAND_NONE);
75 
76  if(PSOCK_NEWDATA(&s->s)) {
77  PSOCK_READTO(&s->s, '\n');
78  if(strncmp(s->inputbuf, "quit", 4) == 0) {
79  PSOCK_CLOSE_EXIT(&s->s);
80  memb_free(&conns, s);
81  tcp_markconn(uip_conn, NULL);
82  /* } else if(strncmp(s->inputbuf, "ps", 2) == 0) {
83  PSOCK_GENERATOR_SEND(&s->s, ps_generate, ek_procs);
84 
85  for(s->i = 0; s->i < 40; s->i++) {
86  if(ek_process(s->i) != NULL) {
87  PSOCK_GENERATOR_SEND(&s->s, ps_generate, ek_process(s->i));
88  }
89  }*/
90  } else if(strncmp(s->inputbuf, "send", 4) == 0) {
91  send_udp = 1;
92  PSOCK_WAIT_UNTIL(&s->s, send_udp == 0);
93  } else {
94  PSOCK_SEND(&s->s, "?\n", 2);
95  }
96  } else {
97  switch(s->command) {
98  }
99  }
100 
101  }
102  PSOCK_END(&s->s);
103 }
104 /*---------------------------------------------------------------------------*/
105 static void
106 appcall(void *state)
107 {
108  struct cmdd_state *s = (struct cmdd_state *)state;
109 
110  if(uip_udpconnection()) {
111  if(send_udp) {
112  uip_udp_send(8);
113  send_udp = 0;
114  }
115  } else {
116  if(uip_closed() || uip_timedout() || uip_aborted()) {
117  if(state != NULL) {
118  memb_free(&conns, state);
119  }
120  } else if(uip_connected()) {
121  s = (struct cmdd_state *)memb_alloc(&conns);
122  if(s == NULL) {
123  uip_abort();
124  } else {
125  tcp_markconn(uip_conn, s);
126  PSOCK_INIT(&s->s, s->inputbuf, sizeof(s->inputbuf) - 1);
127  timer_set(&s->timer, CLOCK_SECOND * 60);
128  handle_connection(s);
129  }
130  } else if(s != NULL) {
131  if(uip_poll()) {
132  if(timer_expired(&s->timer)) {
133  memb_free(&conns, state);
134  uip_abort();
135  return;
136  }
137  }
138  timer_reset(&s->timer);
139  handle_connection(s);
140  } else {
141  uip_abort();
142  }
143  }
144 }
145 /*---------------------------------------------------------------------------*/
146 PROCESS_THREAD(cmdd_process, ev, data)
147 {
148  uip_ipaddr_t ipaddr;
149 
150  PROCESS_BEGIN();
151 
152  tcp_listen(UIP_HTONS(6581));
153  memb_init(&conns);
154  uip_ipaddr(&ipaddr, 255,255,255,255);
155  udpconn = udp_new(&ipaddr, UIP_HTONS(6712), NULL);
156 
157  while(1) {
159  if(ev == tcpip_event) {
160  appcall(data);
161  } else if(ev == PROCESS_EVENT_EXIT) {
162  process_exit(&cmdd_process);
163  LOADER_UNLOAD();
164  }
165  }
166  PROCESS_END();
167 }
168 /*---------------------------------------------------------------------------*/
A timer.
Definition: timer.h:86
#define PSOCK_READTO(psock, c)
Read data up to a specified character.
Definition: psock.h:291
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
Definition: memb.c:52
#define PSOCK_CLOSE_EXIT(psock)
Close a protosocket and exit the protosocket&#39;s protothread.
Definition: psock.h:331
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define PSOCK_BEGIN(psock)
Start the protosocket protothread in a function.
Definition: psock.h:164
Representation of a uIP TCP connection.
Definition: uip.h:1336
#define uip_aborted()
Has the connection been aborted by the other end?
Definition: uip.h:781
CCIF struct uip_udp_conn * udp_new(const uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
Create a new UDP connection.
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition: timer.c:64
char memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Definition: memb.c:79
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
Definition: memb.c:59
#define NULL
The null pointer.
#define uip_poll()
Is the connection being polled by uIP?
Definition: uip.h:817
#define uip_udp_send(len)
Send a UDP datagram of length len on the current connection.
Definition: uip.h:901
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
Definition: uip.h:1238
#define PT_THREAD(name_args)
Declaration of a protothread.
Definition: pt.h:99
#define uip_connected()
Has the connection just been connected?
Definition: uip.h:761
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PSOCK_WAIT_UNTIL(psock, condition)
Wait until a condition is true.
Definition: psock.h:395
#define uip_abort()
Abort the current connection.
Definition: uip.h:682
#define PSOCK_INIT(psock, buffer, buffersize)
Initialize a protosocket.
Definition: psock.h:150
#define LOADER_UNLOAD()
Unload a program from memory.
Definition: loader.h:104
#define MEMB(name, structure, num)
Declare a memory block.
Definition: memb.h:89
#define PSOCK_SEND(psock, data, datalen)
Send data.
Definition: psock.h:184
The representation of a protosocket.
Definition: psock.h:112
#define PSOCK_END(psock)
Declare the end of a protosocket&#39;s protothread.
Definition: psock.h:348
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
Definition: process.h:141
#define uip_udpconnection()
Is the current connection a UDP connection?
Definition: uip.h:727
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
process_event_t tcpip_event
The uIP event.
Definition: tcpip.c:75
#define uip_timedout()
Has the connection timed out?
Definition: uip.h:791
#define PSOCK_NEWDATA(psock)
Check if new data has arrived on a protosocket.
Definition: psock.h:362
CCIF void tcp_listen(uint16_t port)
Open a TCP port.
#define uip_closed()
Has the connection been closed by the other end?
Definition: uip.h:771
Representation of a uIP UDP connection.
Definition: uip.h:1394
#define CC_FASTCALL
Configure if the C compiler supports fastcall function declarations.
Definition: cc.h:77
#define uip_ipaddr(addr, addr0, addr1, addr2, addr3)
Construct an IP address from four bytes.
Definition: uip.h:955
int timer_expired(struct timer *t)
Check if a timer has expired.
Definition: timer.c:121
void process_exit(struct process *p)
Cause a process to exit.
Definition: process.c:202
void timer_reset(struct timer *t)
Reset the timer with the same interval.
Definition: timer.c:84
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82