2020 *
2121 *=============================================================================
2222 *
23- * OTA Upgrade mechanism implemented using DTLS 1.2
23+ * OTA Upgrade mechanism implemented using UART
2424 *
2525 */
2626
4242#include <errno.h>
4343
4444#define MSGLEN (4 + 4 + 8)
45- #define PORT "/dev/ttyACM0"
46-
45+ #ifndef UART_DEV
46+ #ifdef _MACH_
47+ #define UART_DEV "/dev/usbmodem144241"
48+ #else
49+ #define UART_DEV "/dev/ttyACM0"
50+ #endif
51+ #endif
52+ #ifndef B115200
53+ #define B115200 115200
54+ #endif
4755
4856static volatile int cleanup ; /* To handle shutdown */
4957union usb_ack {
@@ -52,8 +60,8 @@ union usb_ack {
5260};
5361
5462
55- static uint8_t pktbuf [MSGLEN ];
56- static unsigned int pktbuf_size = 0 ;
63+ static uint8_t pktbuf [MSGLEN ];
64+ static unsigned int pktbuf_size = 0 ;
5765static int serialfd = -1 ;
5866static uint32_t high_ack ;
5967
@@ -79,7 +87,7 @@ static int recv_ack(union usb_ack *ack)
7987 if (c == '#' ) {
8088 int i = 0 ;
8189 err = 0 ;
82- ack -> offset = 0 ;
90+ ack -> offset = 0 ;
8391 while (i < 4 ) {
8492 res = read (serialfd , & c , 1 );
8593 if (res < 1 ) {
@@ -121,27 +129,27 @@ int main(int argc, char** argv)
121129 union usb_ack ack ;
122130 struct termios tty ;
123131 sigset (SIGALRM , alarm_handler );
124-
125-
126132
127133 if (argc != 2 ) {
128134 printf ("Usage: %s firmware_filename\n" , argv [0 ]);
129135 exit (1 );
130136 }
131137
138+ /* open file and get size */
132139 ffd = open (argv [1 ], O_RDONLY );
133140 if (ffd < 0 ) {
134141 perror ("opening file" );
135142 exit (2 );
136143 }
137-
138144 res = fstat (ffd , & st );
139145 if (res != 0 ) {
140146 perror ("fstat file" );
141147 exit (2 );
142148 }
143149 tot_len = st .st_size ;
144- serialfd = open (PORT , O_RDWR | O_NOCTTY );
150+
151+ /* open UART */
152+ serialfd = open (UART_DEV , O_RDWR | O_NOCTTY );
145153 tcgetattr (serialfd , & tty );
146154 cfsetospeed (& tty , B115200 );
147155 cfsetispeed (& tty , B115200 );
@@ -155,8 +163,8 @@ int main(int argc, char** argv)
155163 tty .c_cc [VMIN ] = 0 ;
156164 tty .c_cc [VTIME ] = 5 ;
157165 tcsetattr (serialfd , TCSANOW , & tty );
158- /* Await Start hash */
159166
167+ /* Wait for start hash (asterisk) */
160168 while (1 ) {
161169 char c ;
162170
@@ -172,18 +180,17 @@ int main(int argc, char** argv)
172180 printf ("%c" ,c );
173181 fflush (stdout );
174182 }
175-
183+
176184 }
177185 printf ("Target connected.\n" );
178186 usleep (500000 );
179187 printf ("Starting update.\n" );
180188
181-
182189 do {
183190 uint8_t hdr [2 ] = { 0xA5 , 0x5A };
184191 len = 0 ;
185- lseek (ffd , 0 , SEEK_SET );
186- write (serialfd , & hdr , 2 );
192+ lseek (ffd , 0 , SEEK_SET );
193+ write (serialfd , & hdr , 2 );
187194 write (serialfd , & tot_len , sizeof (uint32_t ));
188195 printf ("Sent image file size (%d)\n" , tot_len );
189196 while (len < tot_len ) {
@@ -201,7 +208,7 @@ int main(int argc, char** argv)
201208 pktbuf_size = 0 ;
202209 if (ack .offset != len ) {
203210 printf ("buf rewind %u\n" , ack .offset );
204- lseek (ffd , ack .offset , SEEK_SET );
211+ lseek (ffd , ack .offset , SEEK_SET );
205212 len = ack .offset ;
206213 }
207214 memcpy (pktbuf + 4 , & len , sizeof (len ));
@@ -215,25 +222,27 @@ int main(int argc, char** argv)
215222 check (pktbuf );
216223 write (serialfd , pktbuf , pktbuf_size );
217224 len += res ;
218- printf ("Sent bytes: %d/%d %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x \r" , len , tot_len , pktbuf [0 ], pktbuf [1 ], pktbuf [2 ], pktbuf [3 ], pktbuf [4 ], pktbuf [5 ], pktbuf [6 ], pktbuf [7 ]);
225+
226+ printf ("Sent bytes: %d/%d %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x \r" ,
227+ len , tot_len , pktbuf [0 ], pktbuf [1 ], pktbuf [2 ], pktbuf [3 ], pktbuf [4 ], pktbuf [5 ], pktbuf [6 ], pktbuf [7 ]);
219228
220229 fflush (stdout );
221230 alarm (2 );
222231 }
223232 }
224233 printf ("\n\n" );
225234 } while (0 );
235+
226236 printf ("waiting for last ack...\n" );
227237 while (!cleanup ) {
228238 res = recv_ack (& ack );
229239 if ((res == 0 ) && (ack .offset == tot_len )) {
230240 printf ("Transfer complete.\n" );
231241 break ;
232- }
242+ }
233243 }
234244 printf ("All done.\n" );
235245 close (serialfd );
236246
237247 return 0 ;
238248}
239-
0 commit comments