1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18 #include <ctype.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "payload.h"
26
payload_init(struct Payload * p,uint32_t tx_len,uint32_t exp_len)27 bool payload_init(struct Payload *p, uint32_t tx_len, uint32_t exp_len) {
28 if (!p)
29 return false;
30 if (!buffer_init(&p->tx, tx_len))
31 return false;
32 if (buffer_init(&p->expected, exp_len))
33 return true;
34 buffer_free(&p->tx);
35 return false;
36 }
37
payload_free(struct Payload * p)38 void payload_free(struct Payload *p) {
39 if (p) {
40 buffer_free(&p->tx);
41 buffer_free(&p->expected);
42 }
43 }
44
payload_read(struct Payload * p,FILE * fp)45 bool payload_read(struct Payload *p, FILE *fp) {
46 if (!p)
47 return false;
48 p->tx.len = 0;
49 p->expected.len = 0;
50 while (buffer_read_hex(&p->tx, fp, true) && p->tx.len == 0)
51 ;
52 if (p->tx.len == 0)
53 return false;
54 if (!buffer_read_hex(&p->expected, fp, false)) {
55 p->expected.buffer[0] = 0x90;
56 p->expected.buffer[1] = 0x00;
57 p->expected.len = 2;
58 }
59 return true;
60 }
61
payload_dump(const struct Payload * payload,FILE * fp)62 void payload_dump(const struct Payload *payload, FILE *fp) {
63 fprintf(fp, "Payload {\n");
64 buffer_dump(&payload->tx, " ", "Transmit", 240, fp);
65 buffer_dump(&payload->expected, " ", "Expected", 240, fp);
66 fprintf(fp, "}\n");
67 }
68