File: main.c

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (136 lines) | stat: -rw-r--r-- 3,812 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "spiproxyd"

#include <assert.h>
#include <fcntl.h>
#include <getopt.h>
#include <log/log.h>
#include <stdlib.h>
#include <string.h>
#include <trusty/tipc.h>
#include <unistd.h>

int handle_msg(int trusty_dev_fd, int spi_dev_fd) {
    int rc;
    uint8_t msg_buf[4096];
    size_t msg_len;

    /* read request from SPI Trusty app */
    rc = read(trusty_dev_fd, &msg_buf, sizeof(msg_buf));
    if (rc < 0) {
        ALOGE("failed (%d) to read request from TA\n", rc);
        return rc;
    }
    msg_len = rc;

    /* forward request to SPI host device */
    rc = write(spi_dev_fd, &msg_buf, msg_len);
    if (rc < 0 || (size_t)rc != msg_len) {
        ALOGE("failed (%d) to forward request to host\n", rc);
        return rc < 0 ? rc : -1;
    }

    /* read response from SPI host device */
    rc = read(spi_dev_fd, &msg_buf, sizeof(msg_buf));
    if (rc < 0) {
        ALOGE("failed (%d) to read response from host\n", rc);
        return rc;
    }
    msg_len = rc;

    /* forward response to SPI Trusty app */
    rc = write(trusty_dev_fd, &msg_buf, msg_len);
    if (rc < 0 || (size_t)rc != msg_len) {
        ALOGE("failed (%d) to forward response to TA\n", rc);
        return rc < 0 ? rc : -1;
    }

    return 0;
}

int event_loop(int trusty_dev_fd, int spi_dev_fd) {
    while (true) {
        int rc = handle_msg(trusty_dev_fd, spi_dev_fd);
        if (rc < 0) {
            ALOGE("exiting event loop\n");
            return EXIT_FAILURE;
        }
    }
}

static void show_usage() {
    ALOGE("usage: spiproxyd -t TRUSTY_DEVICE -s SPI_DEVICE -p SPI_PROXY_PORT\n");
}

static void parse_args(int argc, char* argv[], const char** trusty_dev_name,
                       const char** spi_dev_name, const char** spi_proxy_port) {
    int opt;
    while ((opt = getopt(argc, argv, "ht:s:p:")) != -1) {
        switch (opt) {
            case 'h':
                show_usage();
                exit(EXIT_SUCCESS);
                break;
            case 't':
                *trusty_dev_name = strdup(optarg);
                break;
            case 's':
                *spi_dev_name = strdup(optarg);
                break;
            case 'p':
                *spi_proxy_port = strdup(optarg);
                break;
            default:
                show_usage();
                exit(EXIT_FAILURE);
                break;
        }
    }

    if (!*trusty_dev_name || !*spi_dev_name || !*spi_proxy_port) {
        show_usage();
        exit(EXIT_FAILURE);
    }
}

int main(int argc, char* argv[]) {
    int rc;
    const char* trusty_dev_name = NULL;
    const char* spi_dev_name = NULL;
    const char* spi_proxy_port = NULL;
    int trusty_dev_fd;
    int spi_dev_fd;

    parse_args(argc, argv, &trusty_dev_name, &spi_dev_name, &spi_proxy_port);

    rc = tipc_connect(trusty_dev_name, spi_proxy_port);
    if (rc < 0) {
        ALOGE("failed (%d) to connect to SPI proxy port\n", rc);
        return rc;
    }
    trusty_dev_fd = rc;

    rc = open(spi_dev_name, O_RDWR, 0);
    if (rc < 0) {
        ALOGE("failed (%d) to open SPI device\n", rc);
        return rc;
    }
    spi_dev_fd = rc;

    return event_loop(trusty_dev_fd, spi_dev_fd);
}