Contiki 3.x
tcp_loader2.c
1 /*
2  * Copyright (c) 2005, 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 #include <stdio.h>
34 #include <string.h>
35 
36 #include "contiki.h"
37 #include "sys/etimer.h"
38 
39 #include "loader/cle.h"
40 #include "loader/cmod.h"
41 
42 #include "net/ip/uip.h"
43 
44 #include "dev/xmem.h"
45 
46 #define NDEBUG
47 #include "lib/assert.h"
48 
49 #include "codeprop.h"
50 
51 #define PRINTF(x)
52 
53 PROCESS(tcp_loader_process, "TCP loader");
54 
55 static
56 struct codeprop_state {
57  uint16_t addr;
58  uint16_t len;
59  struct pt tcpthread_pt;
60 } s;
61 
62 /*---------------------------------------------------------------------*/
63 static
64 PT_THREAD(recv_tcpthread(struct pt *pt))
65 {
66  register int ret;
67 
68  PT_BEGIN(pt);
69 
70  /* Read the header. */
71  PT_WAIT_UNTIL(pt, uip_newdata() && uip_datalen() > 0);
72 
73  if(uip_datalen() < sizeof(struct codeprop_tcphdr)) {
74  PRINTF(("codeprop: header not found in first tcp segment\n"));
75  uip_abort();
76  goto thread_done;
77  }
78 
79  s.len = uip_htons(((struct codeprop_tcphdr *)uip_appdata)->len);
80  s.addr = 0;
81  uip_appdata += sizeof(struct codeprop_tcphdr);
82  uip_len -= sizeof(struct codeprop_tcphdr);
83 
84  ret = xmem_erase(XMEM_ERASE_UNIT_SIZE, EEPROMFS_ADDR_CODEPROP);
85  assert(ret == 0);
86 
87  /* Read the rest of the data. */
88  do {
89  if(uip_len > 0) {
90  ret = xmem_pwrite(uip_appdata, uip_len, EEPROMFS_ADDR_CODEPROP + s.addr);
91  assert(ret = uip_len);
92  s.addr += uip_len;
93  }
94  if(s.addr < s.len) {
96  }
97  } while(s.addr < s.len);
98 
99  /* Kill old program. */
100  cmod_unload(0);
101 
102  /* Link, load, and start new program. */
103  int s;
104  static char msg[30 + 10];
105  cle_scratch elfloader_unknown;
106  s = cmod_load(0, elfloader_unknown, xmem_pread, EEPROMFS_ADDR_CODEPROP);
107  if (s == CMOD_OK)
108  sprintf(msg, "ok\n");
109  else
110  sprintf(msg, "err %d %s\n", s, elfloader_unknown);
111 
112  /* Return "ok" message. */
113  do {
114  s = strlen(msg);
115  uip_send(msg, s);
116  PT_WAIT_UNTIL(pt, uip_acked() || uip_rexmit() || uip_closed());
117  } while(uip_rexmit());
118 
119  /* Close the connection. */
120  uip_close();
121 
122  thread_done:;
123  PT_END(pt);
124 }
125 /*---------------------------------------------------------------------*/
126 PROCESS_THREAD(tcp_loader_process, ev, data)
127 {
128  PROCESS_BEGIN();
129 
130  tcp_listen(UIP_HTONS(CODEPROP_DATA_PORT));
131 
132  while(1) {
133  PROCESS_YIELD();
134  if(ev == tcpip_event && uip_conn->lport == UIP_HTONS(CODEPROP_DATA_PORT)) {
135  if(uip_connected()) { /* Really uip_connecting()!!! */
136  if(data == NULL) {
137  PT_INIT(&s.tcpthread_pt);
138  process_poll(&tcp_loader_process);
139  tcp_markconn(uip_conn, &s);
140  } else {
141  PRINTF(("codeprop: uip_connected() and data != NULL\n"));
142  uip_abort();
143  }
144  }
145  recv_tcpthread(&s.tcpthread_pt); /* Run thread */
146 
147  if(uip_closed() || uip_aborted() || uip_timedout()) {
148  PRINTF(("codeprop: connection down\n"));
149  tcp_markconn(uip_conn, NULL);
150  }
151  }
152  }
153 
154  PROCESS_END();
155 }
uip_len
The length of the packet in the uip_buf buffer.
Definition: tcp_loader.c:75
void process_poll(struct process *p)
Request a process to be polled.
Definition: process.c:371
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
Representation of a uIP TCP connection.
Definition: uip.h:1336
Header file for the uIP TCP/IP stack.
#define uip_aborted()
Has the connection been aborted by the other end?
Definition: uip.h:781
CCIF void uip_send(const void *data, int len)
Send data on the current connection.
Definition: uip6.c:2310
#define uip_newdata()
Is new incoming data available?
Definition: uip.h:738
uint16_t lport
The local TCP port, in network byte order.
Definition: uip.h:1339
#define NULL
The null pointer.
#define PT_INIT(pt)
Initialize a protothread.
Definition: pt.h:79
#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_acked()
Has previously sent data been acknowledged?
Definition: uip.h:749
#define uip_connected()
Has the connection just been connected?
Definition: uip.h:761
#define PT_WAIT_UNTIL(pt, condition)
Block and wait until condition is true.
Definition: pt.h:147
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
cle_scratch elfloader_unknown
If elfloader_load() could not find a specific symbol, it is copied into this array.
Definition: tcp_loader2.c:105
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define uip_abort()
Abort the current connection.
Definition: uip.h:682
CCIF uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
Definition: uip6.c:2298
#define PT_BEGIN(pt)
Declare the start of a protothread inside the C function implementing the protothread.
Definition: pt.h:114
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define uip_close()
Close the current connection.
Definition: uip.h:671
#define uip_datalen()
The length of any incoming data that is currently available (if available) in the uip_appdata buffer...
Definition: uip.h:651
#define PT_YIELD_UNTIL(pt, cond)
Yield from the protothread until a condition occurs.
Definition: pt.h:309
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 PT_END(pt)
Declare the end of a protothread.
Definition: pt.h:126
#define PROCESS_YIELD()
Yield the currently running process.
Definition: process.h:164
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
Event timer header file.
uip_appdata
Pointer to the application data in the packet buffer.
Definition: tcp_loader.c:74
#define uip_rexmit()
Do we need to retransmit previously data?
Definition: uip.h:803