summaryrefslogtreecommitdiff
path: root/fw/fe310/phone/ecp.c
blob: 32c15ead619994626f8b6715c2cd07f46d1a127b (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <eos.h>
#include <soc/i2s.h>
#include <dev/net.h>

#include <eve/eve.h>
#include <eve/eve_kbd.h>
#include <eve/eve_font.h>

#include <eve/screen/window.h>
#include <eve/screen/page.h>
#include <eve/screen/form.h>

#include "app/app.h"
#include "app/status.h"

#include "sms.h"

#include <ecp/core.h>
#include <ecp/tr.h>

#include "ecp.h"

#define CTYPE_PHONE     0
#define MTYPE_VOIP      0
#define MTYPE_TEXT      1

static ECPContext ctx;
static ECPSocket sock;
static ECPConnHandler handler;

int arc4random_alloc(void **rsp, size_t rsp_size, void **rsxp, size_t rsxp_size) {
    *rsp = malloc(rsp_size);
    *rsxp = malloc(rsxp_size);

    if ((*rsp == NULL) || (*rsxp == NULL)) {
        if (*rsp) free(*rsp);
        if (*rsp) free(*rsxp);
        return -1;
    }
    return 0;
}

static void handle_err(ECPConnection *conn, unsigned char mtype, int err) {
    printf("ERR: CTYPE:0x%x MTYPE:0x%x ERR:%d\n", conn->type, mtype, err);
}

static int handle_open(ECPConnection *conn, ECP2Buffer *b) {
    APP_LOG(APP_LOG_DEBUG, "CONN OPEN\n");

    return ECP_OK;
}

static ssize_t handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) {
    switch (mtype) {
        case MTYPE_VOIP: {
            eos_i2s_spk_write(msg, msg_size, EOS_I2S_TRANS_MONO2D);
            break;
        }

        case MTYPE_TEXT: {
            APP_LOG(APP_LOG_DEBUG, "ECP TEXT:%s\n", msg);
            break;
        }

    }

    return msg_size;
}

static ECPConnection *conn_new(ECPSocket *sock, unsigned char type) {
    ECPConnection *conn;

    conn = malloc(sizeof(ECPConnection));
    if (conn) ecp_conn_init(conn, sock, type);
    return conn;
}

static void conn_free(ECPConnection *conn) {
    if (ecp_conn_is_gc(conn)) free(conn);
}

void ecp_init(void) {
    ECPDHKey key_perma;
    int rv;

    rv = ecp_ctx_init(&ctx, handle_err, conn_new, conn_free);
    if (rv) {
        APP_LOG(APP_LOG_ERR, "CTX INIT ERR:%d\n", rv);
        return;
    }

    ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL);
    ecp_ctx_set_handler(&ctx, CTYPE_PHONE, &handler);

    rv = ecp_dhkey_gen(&key_perma);
    if (rv) {
        APP_LOG(APP_LOG_ERR, "DHKEY GEN ERR:%d\n", rv);
        return;
    }

    rv = ecp_sock_create(&sock, &ctx, &key_perma);
    if (rv) {
        APP_LOG(APP_LOG_ERR, "SOCK CREATE ERR:%d\n", rv);
        return;
    }

    rv = ecp_sock_open(&sock, NULL);
    if (rv) {
        APP_LOG(APP_LOG_ERR, "SOCK OPEN ERR:%d\n", rv);
        return;
    }

}

static void connect(void) {
    ECPNode node = {
        .key_perma.public = {
            0xf0, 0x30, 0xde, 0xb2, 0x82, 0xe8, 0x7c, 0x87, 0x7d, 0x62, 0x15, 0xc3, 0xd9, 0xfd, 0xe2, 0x25,
            0x6c, 0x02, 0xb0, 0x74, 0x35, 0xb3, 0xd6, 0xc2, 0xed, 0xa9, 0x6b, 0xf9, 0x74, 0x53, 0x36, 0x1c,
        },
        .key_perma.valid = 1,
        .addr = {
            .host = {192,168,1,8},
            .port = 3001,
        },
    };
    ECPConnection *conn;
    int rv;

    conn = malloc(sizeof(ECPConnection));
    if (conn == NULL) {
        APP_LOG(APP_LOG_ERR, "OUT OF MEMORY\n");
        return;
    }

    ecp_conn_init(conn, &sock, CTYPE_PHONE);
    rv = ecp_conn_open(conn, &node);
    if (rv) {
        APP_LOG(APP_LOG_ERR, "CONN OPEN ERR:%d\n", rv);
        return;
    }
}

int ecp_app(EVEWindow *window, EVEViewStack *stack) {
    EVEFormSpec spec[] = {
        APP_SPACERW(APP_SCREEN_W,1),
    };
    EVEPage *page = eve_form_create(window, stack, spec, APP_SPEC_SIZE(spec), ecp_uievt, ecp_close);
    if (page == NULL) {
        APP_LOG(APP_LOG_ERR, "OUT OF MEMORY\n");
        return EVE_ERR_NOMEM;
    }

    return EVE_OK;
}

int ecp_uievt(EVEPage *page, uint16_t evt, void *param) {
    int ret = 0;

    switch (evt) {
        case EVE_UIEVT_GEST_TOUCH: {
            connect();
            break;
        }

        default: {
            ret = eve_form_uievt(page, evt, param);
            break;
        }
    }

    return ret;
}

void ecp_close(EVEPage *page) {
    eve_form_destroy(page);
}