Root/
Source at commit 87cc67d5f7bdc1e5c67de41fb05a9efc643734aa created 11 years 9 months ago. By Werner Almesberger, fw/: add image data upload (completely untested) | |
---|---|
1 | /* |
2 | * fw/image.c - Image data (upload and conversion) |
3 | * |
4 | * Written 2012 by Werner Almesberger |
5 | * Copyright 2012 Werner Almesberger |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | */ |
12 | |
13 | |
14 | #include <stdbool.h> |
15 | #include <stdint.h> |
16 | #include <string.h> |
17 | |
18 | #include "io.h" |
19 | #include "hash.h" |
20 | #include "proto.h" |
21 | #include "dispatch.h" |
22 | #include "image.h" |
23 | |
24 | |
25 | static struct line images[2][MAX_LINES]; |
26 | static const struct line *next_image; |
27 | static struct line *p, *end; |
28 | static bool failed; |
29 | |
30 | const struct line *image = images[0]; |
31 | |
32 | |
33 | static const uint8_t image_secret[2*PAYLOAD] = { |
34 | #include "image-secret.inc" |
35 | }; |
36 | |
37 | |
38 | #define MAP(port, value, group) ( \ |
39 | ((value) & 1 ? MASK(port, LED_##group##1) : 0) | \ |
40 | ((value) & 2 ? MASK(port, LED_##group##2) : 0) | \ |
41 | ((value) & 4 ? MASK(port, LED_##group##3) : 0) | \ |
42 | ((value) & 8 ? MASK(port, LED_##group##4) : 0) | \ |
43 | ((value) & 16 ? MASK(port, LED_##group##5) : 0) | \ |
44 | ((value) & 32 ? MASK(port, LED_##group##6) : 0) | \ |
45 | ((value) & 64 ? MASK(port, LED_##group##7) : 0) | \ |
46 | ((value) & 128 ? MASK(port, LED_##group##8) : 0)) |
47 | |
48 | |
49 | static void add_payload(const uint8_t *payload) |
50 | { |
51 | uint8_t i, b, c, d; |
52 | |
53 | for (i = 0; i != PAYLOAD && p != end; i += 2) { |
54 | b = MAP(B, payload[i], A) | MAP(B, payload[i+1], B); |
55 | c = MAP(C, payload[i], A) | MAP(C, payload[i+1], B); |
56 | d = MAP(D, payload[i], A) | MAP(D, payload[i+1], B); |
57 | p->d = d; |
58 | p->cb = c | b; |
59 | p++; |
60 | } |
61 | } |
62 | |
63 | |
64 | static bool image_first(const uint8_t *payload) |
65 | { |
66 | hash_init(); |
67 | next_image = p = image == images[0] ? images[1] : images[0]; |
68 | end = p+MAX_LINES; |
69 | memset(p, 0, (char *) end-(char *) p); |
70 | add_payload(payload); |
71 | hash_merge(payload, PAYLOAD); |
72 | failed = 0; |
73 | return 1; |
74 | } |
75 | |
76 | |
77 | static bool image_more(uint8_t seq, uint8_t limit, const uint8_t *payload) |
78 | { |
79 | switch (limit-seq) { |
80 | default: |
81 | add_payload(payload); |
82 | /* fall through */ |
83 | case 3: |
84 | case 2: |
85 | hash_merge(payload, PAYLOAD); |
86 | break; |
87 | case 1: |
88 | failed = !hash_eq(payload, PAYLOAD, 0); |
89 | break; |
90 | case 0: |
91 | if (!hash_eq(payload, PAYLOAD, PAYLOAD)) |
92 | failed = 1; |
93 | if (failed) |
94 | return 0; |
95 | image = next_image; |
96 | break; |
97 | } |
98 | return 1; |
99 | } |
100 | |
101 | |
102 | struct handler image_handler = { |
103 | .type = IMAGE, |
104 | .first = image_first, |
105 | .more = image_more, |
106 | }; |
107 |
Branches:
master
tornado-v1