Contiki 3.x
tcp_loader.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 #include "loader/elfloader_compat.h"
39 
40 #include "net/ip/uip.h"
41 
42 #include "dev/xmem.h"
43 
44 #include "codeprop.h"
45 
46 #define PRINTF(x)
47 
48 PROCESS(tcp_loader_process, "TCP loader");
49 
50 static
51 struct codeprop_state {
52  uint16_t addr;
53  uint16_t len;
54  struct pt tcpthread_pt;
55 } s;
56 
57 /*---------------------------------------------------------------------*/
58 static
59 PT_THREAD(recv_tcpthread(struct pt *pt))
60 {
61  PT_BEGIN(pt);
62 
63  /* Read the header. */
64  PT_WAIT_UNTIL(pt, uip_newdata() && uip_datalen() > 0);
65 
66  if(uip_datalen() < sizeof(struct codeprop_tcphdr)) {
67  PRINTF(("codeprop: header not found in first tcp segment\n"));
68  uip_abort();
69  goto thread_done;
70  }
71 
72  s.len = uip_htons(((struct codeprop_tcphdr *)uip_appdata)->len);
73  s.addr = 0;
74  uip_appdata += sizeof(struct codeprop_tcphdr);
75  uip_len -= sizeof(struct codeprop_tcphdr);
76 
77  xmem_erase(XMEM_ERASE_UNIT_SIZE, EEPROMFS_ADDR_CODEPROP);
78 
79  /* Read the rest of the data. */
80  do {
81  if(uip_len > 0) {
82  xmem_pwrite(uip_appdata, uip_len, EEPROMFS_ADDR_CODEPROP + s.addr);
83  s.addr += uip_len;
84  }
85  if(s.addr < s.len) {
87  }
88  } while(s.addr < s.len);
89 
90  /* Kill old program. */
91  elfloader_unload();
92 
93  /* Link, load, and start new program. */
94  int s;
95  static char msg[30 + 10];
96  s = elfloader_load(EEPROMFS_ADDR_CODEPROP);
97  if (s == ELFLOADER_OK)
98  sprintf(msg, "ok\n");
99  else
100  sprintf(msg, "err %d %s\n", s, elfloader_unknown);
101 
102  /* Return "ok" message. */
103  do {
104  s = strlen(msg);
105  uip_send(msg, s);
106  PT_WAIT_UNTIL(pt, uip_acked() || uip_rexmit() || uip_closed());
107  } while(uip_rexmit());
108 
109  /* Close the connection. */
110  uip_close();
111 
112  thread_done:;
113  PT_END(pt);
114 }
115 /*---------------------------------------------------------------------*/
116 PROCESS_THREAD(tcp_loader_process, ev, data)
117 {
118  PROCESS_BEGIN();
119 
120  tcp_listen(UIP_HTONS(CODEPROP_DATA_PORT));
121 
122  while(1) {
123  PROCESS_YIELD();
124  if(ev == tcpip_event && uip_conn->lport == UIP_HTONS(CODEPROP_DATA_PORT)) {
125  if(uip_connected()) { /* Really uip_connecting()!!! */
126  if(data == NULL) {
127  PT_INIT(&s.tcpthread_pt);
128  process_poll(&tcp_loader_process);
129  tcp_markconn(uip_conn, &s);
130  } else {
131  PRINTF(("codeprop: uip_connected() and data != NULL\n"));
132  uip_abort();
133  }
134  }
135  recv_tcpthread(&s.tcpthread_pt); /* Run thread */
136 
137  if(uip_closed() || uip_aborted() || uip_timedout()) {
138  PRINTF(("codeprop: connection down\n"));
139  tcp_markconn(uip_conn, NULL);
140  }
141  }
142  }
143 
144  PROCESS_END();
145 }
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
int elfloader_load(int fd)
Load and relocate an ELF file.
Definition: elfloader.c:339
#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 ELFLOADER_OK
Return value from elfloader_load() indicating that loading worked.
Definition: elfloader.h:83
#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