diff options
190 files changed, 5196 insertions, 4161 deletions
diff --git a/ecp/server/Makefile b/ecp/server/Makefile new file mode 100644 index 0000000..2105e9f --- /dev/null +++ b/ecp/server/Makefile @@ -0,0 +1,16 @@ +src_dir = ../src +include $(src_dir)/ecp/common.mk +CFLAGS += -Wno-int-to-void-pointer-cast + +obj = server.o dir.o vlink.o ht.o +dep = ../build-posix/*.a + +%.o: %.c + $(CC) $(CFLAGS) -c $< + +ecp_server: $(obj) + $(CC) -o $@ $(obj) $(dep) $(LDFLAGS) + +clean: + rm -f *.o + rm -f ecp_server diff --git a/ecp/server/dir.c b/ecp/server/dir.c new file mode 100644 index 0000000..0066bac --- /dev/null +++ b/ecp/server/dir.c @@ -0,0 +1,551 @@ +#include <stdlib.h> +#include <unistd.h> +#include <string.h> + +#include <ecp/core.h> +#include <ecp/dir/dir.h> +#include <ecp/ht.h> +#include <ecp/tm.h> + +#include "server.h" +#include "vlink.h" +#include "dir.h" +#include "ht.h" + +static ecp_ht_table_t *dir_shadow = NULL; +static pthread_rwlock_t dir_shadow_rwlock; +static pthread_rwlock_t dir_online_rwlock; +static pthread_t dir_announce_thd; + +static DirList dir_online; + +static SRVConfig *srv_config; + +ssize_t dir_send_ann(ECPConnection *conn) { + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_SIZE_PKT_BUF(sizeof(uint16_t), ECP_MTYPE_DIR_ANN, conn)]; + unsigned char pld_buf[ECP_SIZE_PLD_BUF(sizeof(uint16_t), ECP_MTYPE_DIR_ANN, conn)]; + unsigned char *msg; + + packet.buffer = pkt_buf; + packet.size = sizeof(pkt_buf); + payload.buffer = pld_buf; + payload.size = sizeof(pld_buf); + + ecp_pld_set_type(payload.buffer, payload.size, ECP_MTYPE_DIR_ANN); + msg = ecp_pld_get_msg(payload.buffer, payload.size); + msg[0] = srv_config->capabilities >> 8; + msg[1] = srv_config->capabilities; + + return ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(sizeof(uint16_t), ECP_MTYPE_DIR_ANN), 0); +} + +ssize_t dir_send_shadow(ECPConnection *conn) { + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_MAX_PKT]; + unsigned char pld_buf[ECP_MAX_PLD]; + unsigned char *msg; + + struct hashtable_itr itr; + DirNode *node; + uint16_t count; + ecp_sts_t access_ts; + size_t msg_size; + + packet.buffer = pkt_buf; + packet.size = ECP_MAX_PKT; + payload.buffer = pld_buf; + payload.size = ECP_MAX_PLD; + + ecp_pld_set_type(payload.buffer, payload.size, ECP_MTYPE_DIR_SHADOW); + msg = ecp_pld_get_msg(payload.buffer, payload.size); + msg_size = 0; + + pthread_rwlock_rdlock(&dir_shadow_rwlock); + + count = ecp_ht_count(dir_shadow); + msg[0] = count >> 8; + msg[1] = count; + msg += sizeof(uint16_t); + msg_size += sizeof(uint16_t); + + if (count > 0) { + size_t rv; + int _rv; + + ecp_ht_itr_create(&itr, dir_shadow); + do { + node = ecp_ht_itr_value(&itr); + + pthread_mutex_lock(&node->mutex); + access_ts = node->access_ts; + pthread_mutex_unlock(&node->mutex); + + rv = ecp_dir_item_serialize(&node->dir_item, msg); + msg += rv; + msg_size += rv; + + msg[0] = access_ts >> 24; + msg[1] = access_ts >> 16; + msg[2] = access_ts >> 8; + msg[3] = access_ts; + msg += sizeof(uint32_t); + msg_size += sizeof(uint32_t); + + _rv = ecp_ht_itr_advance(&itr); + } while (_rv == ECP_OK); + } + + pthread_rwlock_unlock(&dir_shadow_rwlock); + + return ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(msg_size, ECP_MTYPE_DIR_SHADOW), 0); +} + +ssize_t dir_send_online(ECPConnection *conn) { + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_MAX_PKT]; + unsigned char pld_buf[ECP_MAX_PLD]; + unsigned char *msg; + size_t msg_size; + + packet.buffer = pkt_buf; + packet.size = ECP_MAX_PKT; + payload.buffer = pld_buf; + payload.size = ECP_MAX_PLD; + + ecp_pld_set_type(payload.buffer, payload.size, ECP_MTYPE_DIR_REP); + msg = ecp_pld_get_msg(payload.buffer, payload.size); + + pthread_rwlock_rdlock(&dir_online_rwlock); + + msg[0] = dir_online.count >> 8; + msg[1] = dir_online.count; + msg += sizeof(uint16_t); + msg_size = sizeof(uint16_t) + dir_online.count * ECP_SIZE_DIR_ITEM; + memcpy(msg, dir_online.msg, dir_online.count * ECP_SIZE_DIR_ITEM); + + pthread_rwlock_unlock(&dir_online_rwlock); + + return ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(msg_size, ECP_MTYPE_DIR_REP), 0); +} + +ssize_t dir_handle_ann(ECPConnection *conn, unsigned char *msg, size_t msg_size) { + ECPDirItem dir_item; + int is_new; + uint16_t capabilities; + ecp_sts_t now; + ssize_t rsize; + ssize_t rv; + + rsize = sizeof(uint16_t); + if (msg_size < rsize) return ECP_ERR_SIZE; + + capabilities = \ + ((uint16_t)msg[0] << 8) | \ + ((uint16_t)msg[1]); + + memset(&dir_item, 0, sizeof(ECPDirItem)); + dir_item.node = conn->remote; + dir_item.capabilities = capabilities; + + now = ecp_tm_get_s(); + is_new = dir_process_item(&dir_item, now); + if (is_new) vlink_new_node(conn->sock, &dir_item); + + rv = dir_send_shadow(conn); + if (rv < 0) return rv; + + return rsize; +} + +ssize_t dir_handle_req(ECPConnection *conn, unsigned char *msg, size_t msg_size) { + ssize_t rv; + + rv = dir_send_online(conn); + if (rv < 0) return rv; + + return 0; +} + +ssize_t dir_handle_shadow(ECPConnection *conn, unsigned char *msg, size_t msg_size) { + ecp_sts_t now; + uint16_t count; + size_t rsize; + int i; + + if (msg_size < sizeof(uint16_t)) return ECP_ERR_SIZE; + + count = \ + ((uint16_t)msg[0] << 8) | \ + ((uint16_t)msg[1]); + + rsize = sizeof(uint16_t) + count * (ECP_SIZE_DIR_ITEM + sizeof(uint32_t)); + if (msg_size < rsize) return ECP_ERR_SIZE; + + msg += sizeof(uint16_t); + + now = ecp_tm_get_s(); + for (i=0; i<count; i++) { + ECPDirItem dir_item; + ecp_sts_t access_ts; + size_t rv; + + rv = ecp_dir_item_parse(&dir_item, msg); + msg += rv; + + access_ts = \ + ((uint32_t)msg[0] << 24) | \ + ((uint32_t)msg[1] << 16) | \ + ((uint32_t)msg[2] << 8) | \ + ((uint32_t)msg[3]); + msg += sizeof(uint32_t); + + if (ECP_STS_LT(now, access_ts) || (now - access_ts < (NODE_EXP_TIME / 2))) { + int is_new; + + is_new = dir_process_item(&dir_item, access_ts); + if (is_new) vlink_new_node(conn->sock, &dir_item); + } + } + + return rsize; +} + +ssize_t dir_handle_msg(struct ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, struct ECP2Buffer *b) { + switch (mtype) { + case ECP_MTYPE_DIR_REQ: { + if (ecp_conn_is_outb(conn)) return ECP_ERR; + return dir_handle_req(conn, msg, msg_size); + } + + case ECP_MTYPE_DIR_ANN: { + if (ecp_conn_is_outb(conn)) return ECP_ERR; + if (!conn->remote.key_perma.valid) return ECP_ERR_VBOX; + return dir_handle_ann(conn, msg, msg_size); + } + + case ECP_MTYPE_DIR_SHADOW: { + if (ecp_conn_is_inb(conn)) return ECP_ERR; + return dir_handle_shadow(conn, msg, msg_size); + } + + default: + return ECP_ERR_MTYPE; + } +} + +int dir_handle_open(ECPConnection *conn, ECP2Buffer *bufs) { + ssize_t rv; + + if (ecp_conn_is_inb(conn)) return ECP_OK; + + rv = dir_send_ann(conn); + if (rv < 0) return rv; + + return ECP_OK; +} + +void dir_online_switch(void) { + struct hashtable_itr itr; + DirNode *node; + unsigned char *msg; + + pthread_rwlock_rdlock(&dir_shadow_rwlock); + pthread_rwlock_wrlock(&dir_online_rwlock); + + msg = dir_online.msg; + dir_online.count = ecp_ht_count(dir_shadow); + + if (dir_online.count > 0) { + size_t _rv; + int rv; + + ecp_ht_itr_create(&itr, dir_shadow); + do { + node = ecp_ht_itr_value(&itr); + + _rv = ecp_dir_item_serialize(&node->dir_item, msg); + msg += _rv; + + rv = ecp_ht_itr_advance(&itr); + } while (rv == ECP_OK); + } + + pthread_rwlock_unlock(&dir_online_rwlock); + pthread_rwlock_unlock(&dir_shadow_rwlock); +} + +int dir_process_item(ECPDirItem *dir_item, ecp_sts_t access_ts) { + DirNode *node; + int is_new = 0; + int rv = ECP_OK; + + pthread_rwlock_rdlock(&dir_shadow_rwlock); + + node = ht_search_item(dir_shadow, dir_item); + if (node) { + pthread_mutex_lock(&node->mutex); + if (!node->zombie) node->access_ts = access_ts; + pthread_mutex_unlock(&node->mutex); + + pthread_rwlock_unlock(&dir_shadow_rwlock); + } else { + pthread_rwlock_unlock(&dir_shadow_rwlock); + + LOG(LOG_DEBUG, "dir new node\n"); + + node = dir_create_node(dir_item, access_ts); + if (node == NULL) rv = ECP_ERR_ALLOC; + + if (!rv) { + pthread_rwlock_wrlock(&dir_shadow_rwlock); + if (ecp_ht_count(dir_shadow) > MAX_DIR_ITEM) rv = ECP_ERR_FULL; + if (!rv) { + if (ht_search_node(dir_shadow, node) == NULL) { + rv = ht_insert_node(dir_shadow, node); + } else { + rv = ECP_ERR_DUP; + } + } + pthread_rwlock_unlock(&dir_shadow_rwlock); + } + + if (!rv) { + is_new = 1; + /* always switch */ + dir_online_switch(); + } else { + if (node) dir_destroy_node(node); + LOG(LOG_ERR, "dir create node err:%d\n", rv); + } + } + + return is_new; +} + +DirNode *dir_create_node(ECPDirItem *dir_item, ecp_sts_t access_ts) { + DirNode *node; + int rv; + + node = malloc(sizeof(DirNode)); + if (node == NULL) return NULL; + + memset(node, 0, sizeof(DirNode)); + + rv = pthread_mutex_init(&node->mutex, NULL); + if (rv) { + free(node); + return NULL; + } + + node->dir_item = *dir_item; + node->access_ts = access_ts; + + return node; +} + +void dir_destroy_node(DirNode *node) { + pthread_mutex_destroy(&node->mutex); + free(node); +} + +int dir_open_conn(ECPSocket *sock, ECPNode *node) { + ECPConnection *conn; + int rv; + + conn = malloc(sizeof(ECPConnection)); + if (conn == NULL) return ECP_ERR_ALLOC; + + ecp_dir_conn_init(conn, sock); + ecp_conn_set_flags(conn, ECP_CONN_FLAG_GC | ECP_CONN_FLAG_VBOX); + rv = ecp_conn_try_open(conn, node); + return rv; +} + +DirNode *dir_search_conn(ECPConnection *conn) { + DirNode *node; + DirNode *ret = NULL; + + pthread_rwlock_rdlock(&dir_shadow_rwlock); + + node = ht_search_conn(dir_shadow, conn); + if (node) { + pthread_mutex_lock(&node->mutex); + if (!node->zombie) { + node->refcount++; + ret = node; + } else { + ret = NULL; + } + pthread_mutex_unlock(&node->mutex); + } + + pthread_rwlock_unlock(&dir_shadow_rwlock); + + return ret; +} + +void dir_announce(ECPSocket *sock) { + static DirNode *expire_node[MAX_EXPIRE_CNT]; + static int expire_node_z[MAX_EXPIRE_CNT]; + static int expire_cnt; + + struct hashtable_itr itr; + ecp_sts_t now; + DirNode *node; + DirNode *node_next; + DirNode *announce_node[MAX_ANNOUNCE_CNT]; + int announce_cnt; + int refcount; + int i; + int rv; + + now = ecp_tm_get_s(); + node_next = NULL; + do { + announce_cnt = 0; + pthread_rwlock_rdlock(&dir_shadow_rwlock); + + if (ecp_ht_count(dir_shadow) > 0) { + ecp_ht_itr_create(&itr, dir_shadow); + if (node_next) { + rv = ecp_ht_itr_search(&itr, node_next); + if (rv) { + LOG(LOG_ERR, "dir ann itr search err:%d\n", rv); + break; + } + node_next = NULL; + } + do { + node = ecp_ht_itr_value(&itr); + rv = ecp_ht_itr_advance(&itr); + + pthread_mutex_lock(&node->mutex); + if (ECP_STS_LT(node->access_ts, now) && (now - node->access_ts > NODE_EXP_TIME)) { + node->zombie = 1; + pthread_mutex_unlock(&node->mutex); + + if (expire_cnt < MAX_EXPIRE_CNT) { + expire_node[expire_cnt] = node; + expire_node_z[expire_cnt] = 1; + expire_cnt++; + } + } else { + pthread_mutex_unlock(&node->mutex); + + if ((node->dir_item.capabilities & ECP_DIR_CAP_DIR) && (memcmp(node->dir_item.node.key_perma.public, &srv_config->key_perma.public, sizeof(srv_config->key_perma.public)) != 0)) { + announce_node[announce_cnt] = node; + announce_cnt++; + + if (announce_cnt == MAX_ANNOUNCE_CNT) { + if (!rv) { + node_next = ecp_ht_itr_key(&itr); + } else { + node_next = NULL; + } + break; + } + } + } + } while (rv == ECP_OK); + } + + pthread_rwlock_unlock(&dir_shadow_rwlock); + + if (expire_cnt) { + pthread_rwlock_wrlock(&dir_shadow_rwlock); + for (i=0; i<expire_cnt; i++) { + if (expire_node_z[i]) { + expire_node_z[i] = 0; + ht_remove_node(dir_shadow, expire_node[i]); + LOG(LOG_DEBUG, "dir remove node\n"); + } + } + pthread_rwlock_unlock(&dir_shadow_rwlock); + } + + for (i=0; i<announce_cnt; i++) { + rv = dir_open_conn(sock, &announce_node[i]->dir_item.node); + if (rv) LOG(LOG_ERR, "dir open connection err:%d\n", rv); + } + + i = expire_cnt - 1; + while (i >= 0) { + node = expire_node[i]; + + pthread_mutex_lock(&node->mutex); + refcount = node->refcount; + pthread_mutex_unlock(&node->mutex); + + if (refcount == 0) { + int j = i; + + expire_cnt--; + while (j < expire_cnt) { + expire_node[j] = expire_node[j+1]; + j++; + } + expire_node[j] = NULL; + dir_destroy_node(node); + } + i--; + } + } while (node_next); +} + +static void *_dir_announce(void *arg) { + ECPSocket *sock = arg; + + while (1) { + LOG(LOG_DEBUG, "dir announce...\n"); + dir_announce(sock); + sleep(10); + } + + return NULL; +} + +int dir_start_announce(ECPSocket *sock) { + int rv; + + rv = pthread_create(&dir_announce_thd, NULL, _dir_announce, sock); + if (rv) return ECP_ERR; + return ECP_OK; +} + +int dir_init(ECPContext *ctx) { + ECPConnHandler *handler; + int rv; + + srv_config = srv_get_config(); + + handler = malloc(sizeof(ECPConnHandler)); + if (handler == NULL) return ECP_ERR_ALLOC; + + ecp_conn_handler_init(handler, dir_handle_msg, dir_handle_open, NULL, NULL); + rv = ecp_ctx_set_handler(ctx, ECP_CTYPE_DIR, handler); + if (rv) return rv; + + rv = pthread_rwlock_init(&dir_shadow_rwlock, NULL); + if (rv) { + return ECP_ERR; + } + + rv = pthread_rwlock_init(&dir_online_rwlock, NULL); + if (rv) { + pthread_rwlock_destroy(&dir_shadow_rwlock); + return ECP_ERR; + } + + dir_shadow = ecp_ht_create_keys(); + if (dir_shadow == NULL) { + pthread_rwlock_destroy(&dir_shadow_rwlock); + pthread_rwlock_destroy(&dir_online_rwlock); + return ECP_ERR_ALLOC; + } + + return ECP_OK; +} diff --git a/ecp/server/dir.h b/ecp/server/dir.h new file mode 100644 index 0000000..cbc0c87 --- /dev/null +++ b/ecp/server/dir.h @@ -0,0 +1,35 @@ +#define MAX_DIR_ITEM 30 +#define MAX_EXPIRE_CNT 100 +#define MAX_ANNOUNCE_CNT 100 + +#define NODE_EXP_TIME 86400 + +typedef struct DirNode { + ECPDirItem dir_item; + int refcount; + int zombie; + ecp_sts_t access_ts; + pthread_mutex_t mutex; +} DirNode; + +typedef struct DirList { + unsigned char msg[ECP_MAX_PLD]; + uint16_t count; +} DirList; + +ssize_t dir_send_ann(ECPConnection *conn); +ssize_t dir_send_shadow(ECPConnection *conn); +ssize_t dir_send_online(ECPConnection *conn); +ssize_t dir_handle_ann(ECPConnection *conn, unsigned char *msg, size_t msg_size); +ssize_t dir_handle_req(ECPConnection *conn, unsigned char *msg, size_t msg_size); +ssize_t dir_handle_shadow(ECPConnection *conn, unsigned char *msg, size_t msg_size); +ssize_t dir_handle_msg(struct ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, struct ECP2Buffer *b); +int dir_handle_open(ECPConnection *conn, ECP2Buffer *bufs); +int dir_process_item(ECPDirItem *dir_item, ecp_sts_t access_ts); +DirNode *dir_create_node(ECPDirItem *dir_item, ecp_sts_t access_ts); +void dir_destroy_node(DirNode *node); +int dir_open_conn(ECPSocket *sock, ECPNode *node); +DirNode *dir_search_conn(ECPConnection *conn); +void dir_announce(ECPSocket *sock); +int dir_start_announce(ECPSocket *sock); +int dir_init(ECPContext *ctx); diff --git a/ecp/server/ht.c b/ecp/server/ht.c new file mode 100644 index 0000000..0daf8d5 --- /dev/null +++ b/ecp/server/ht.c @@ -0,0 +1,34 @@ +#include <ecp/core.h> +#include <ecp/dir/dir.h> +#include <ecp/ht.h> + +#include "dir.h" +#include "ht.h" + +int ht_insert_node(ecp_ht_table_t *table, DirNode *node) { + return ecp_ht_insert(table, &node->dir_item.node.key_perma.public, node); +} + +void ht_remove_node(ecp_ht_table_t *table, DirNode *node) { + ecp_ht_remove(table, &node->dir_item.node.key_perma.public); +} + +void *ht_search_node(ecp_ht_table_t *table, DirNode *node) { + return ecp_ht_search(table, &node->dir_item.node.key_perma.public); +} + +int ht_insert_conn(ecp_ht_table_t *table, ECPConnection *conn) { + return ecp_ht_insert(table, &conn->remote.key_perma.public, conn); +} + +void ht_remove_conn(ecp_ht_table_t *table, ECPConnection *conn) { + ecp_ht_remove(table, &conn->remote.key_perma.public); +} + +void *ht_search_conn(ecp_ht_table_t *table, ECPConnection *conn) { + return ecp_ht_search(table, &conn->remote.key_perma.public); +} + +void *ht_search_item(ecp_ht_table_t *table, ECPDirItem *dir_item) { + return ecp_ht_search(table, &dir_item->node.key_perma.public); +} diff --git a/ecp/server/ht.h b/ecp/server/ht.h new file mode 100644 index 0000000..cba4691 --- /dev/null +++ b/ecp/server/ht.h @@ -0,0 +1,7 @@ +int ht_insert_node(ecp_ht_table_t *table, DirNode *node); +void ht_remove_node(ecp_ht_table_t *table, DirNode *node); +void *ht_search_node(ecp_ht_table_t *table, DirNode *node); +int ht_insert_conn(ecp_ht_table_t *table, ECPConnection *conn); +void ht_remove_conn(ecp_ht_table_t *table, ECPConnection *conn); +void *ht_search_conn(ecp_ht_table_t *table, ECPConnection *conn); +void *ht_search_item(ecp_ht_table_t *table, ECPDirItem *dir_item);
\ No newline at end of file diff --git a/ecp/server/server.c b/ecp/server/server.c new file mode 100644 index 0000000..bbe7c4e --- /dev/null +++ b/ecp/server/server.c @@ -0,0 +1,204 @@ +#include <stdlib.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/stat.h> + +#include <ecp/core.h> +#include <ecp/dir/dir.h> +#include <ecp/vconn/vconn.h> + +#include "dir.h" +#include "vlink.h" +#include "ht.h" + +#include "server.h" + +static SRVConfig srv_config; + +SRVConfig *srv_get_config(void) { + return &srv_config; +} + +static void usage(char *arg) { + fprintf(stderr, "Usage: %s <capabilities> <priv> <addr> [ <pub> <addr> ]\n", arg); + exit(1); +} + +static void handle_err(ECPConnection *conn, unsigned char mtype, int err) { + if (conn->type == ECP_CTYPE_VLINK) vlink_handle_err(conn, mtype, err); + LOG(LOG_ERR, "handle error ctype:%d mtype:%d err:%d\n", conn->type, mtype, err); +} + +static ECPConnection *conn_new(ECPSocket *sock, unsigned char type) { + ECPConnection *conn = NULL; + + switch (type) { + case ECP_CTYPE_VCONN: { + ECPVConnInb *_conn; + + _conn = malloc(sizeof(ECPVConnInb)); + if (_conn) { + ecp_vconn_init_inb(_conn, sock); + conn = &_conn->b; + } + break; + } + + case ECP_CTYPE_VLINK: { + conn = malloc(sizeof(ECPConnection)); + if (conn) ecp_vlink_init(conn, sock); + break; + } + + default: { + conn = malloc(sizeof(ECPConnection)); + if (conn) ecp_conn_init(conn, sock, type); + break; + } + } + + return conn; +} + +static void conn_free(ECPConnection *conn) { + free(conn); +} + +int ecp_init(ECPContext *ctx, ECPConnHandler *vconn_handler, ECPConnHandler *vlink_handler) { + int rv; + + rv = ecp_ctx_init(ctx, handle_err, conn_new, conn_free); + if (rv) return rv; + + rv = ecp_vconn_handler_init(ctx, vconn_handler); + if (rv) return rv; + + rv = ecp_vlink_handler_init(ctx, vlink_handler); + if (rv) return rv; + + return ECP_OK; +} + +int main(int argc, char *argv[]) { + ecp_tr_addr_t addr; + ECPContext ctx; + ECPSocket sock; + ECPConnHandler vconn_handler; + ECPConnHandler vlink_handler; + char *endptr; + int fd; + int rv; + + if ((argc < 4) || (argc > 6)) usage(argv[0]); + + srv_config.capabilities = (uint16_t)strtol(argv[1], &endptr, 16); + if (endptr[0] != '\0') { + fprintf(stderr, "Bad capabilities\n"); + exit(1); + } + + if ((fd = open(argv[2], O_RDONLY)) < 0) { + fprintf(stderr, "Unable to open %s\n", argv[2]); + exit(1); + } + if (read(fd, &srv_config.key_perma.public, sizeof(ecp_ecdh_public_t)) != sizeof(ecp_ecdh_public_t)) { + close(fd); + fprintf(stderr, "Unable to read public key from %s\n", argv[2]); + exit(1); + } + if (read(fd, &srv_config.key_perma.private, sizeof(ecp_ecdh_private_t)) != sizeof(ecp_ecdh_private_t)) { + close(fd); + fprintf(stderr, "Unable to read private key from %s\n", argv[2]); + exit(1); + } + close(fd); + srv_config.key_perma.valid = 1; + + rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); + if (rv) { + fprintf(stderr, "ecp_init RV:%d\n", rv); + exit(1); + } + rv = ecp_sock_create(&sock, &ctx, &srv_config.key_perma); + if (rv) { + fprintf(stderr, "ecp_sock_create RV:%d\n", rv); + exit(1); + } + + + rv = ecp_addr_init(&addr, argv[3]); + if (rv) { + fprintf(stderr, "ecp_addr_init RV:%d\n", rv); + exit(1); + } + + rv = ecp_sock_open(&sock, &addr); + if (rv) { + fprintf(stderr, "ecp_sock_open RV:%d\n", rv); + exit(1); + } + + rv = dir_init(&ctx); + if (rv) { + fprintf(stderr, "dir_init RV:%d\n", rv); + exit(1); + } + + rv = vlink_init(&ctx); + if (rv) { + fprintf(stderr, "vlink_init RV:%d\n", rv); + exit(1); + } + + rv = dir_start_announce(&sock); + if (rv) { + fprintf(stderr, "dir_start_announce RV:%d\n", rv); + exit(1); + } + + rv = vlink_start_open(&sock); + if (rv) { + fprintf(stderr, "vlink_start_open RV:%d\n", rv); + exit(1); + } + + rv = vlink_start_keyx(); + if (rv) { + fprintf(stderr, "vlink_start_keyx RV:%d\n", rv); + exit(1); + } + + if (argc == 6) { + ECPNode node; + ecp_ecdh_public_t node_pub; + ecp_tr_addr_t node_addr; + + if ((fd = open(argv[4], O_RDONLY)) < 0) { + fprintf(stderr, "Unable to open %s\n", argv[4]); + exit(1); + } + if (read(fd, &node_pub, sizeof(ecp_ecdh_public_t)) != sizeof(ecp_ecdh_public_t)) { + close(fd); + fprintf(stderr, "Unable to read public key from %s\n", argv[4]); + exit(1); + } + close(fd); + + int rv; + + ecp_node_init(&node, &node_pub, NULL); + rv = ecp_node_set_addr(&node, argv[5]); + if (rv) { + fprintf(stderr, "ecp_node_set_addr RV:%d\n", rv); + exit(1); + } + + rv = dir_open_conn(&sock, &node); + if (rv) { + fprintf(stderr, "dir_open_conn RV:%d\n", rv); + exit(1); + } + } + + ecp_receiver(&sock); +}
\ No newline at end of file diff --git a/ecp/server/server.h b/ecp/server/server.h new file mode 100644 index 0000000..ff8e444 --- /dev/null +++ b/ecp/server/server.h @@ -0,0 +1,15 @@ +#include <stdio.h> + +#define LOG_DEBUG 1 +#define LOG_INFO 2 +#define LOG_ERR 3 + +#define LOG_LEVEL LOG_DEBUG +#define LOG(l, ...) (l >= LOG_LEVEL ? fprintf(stderr, __VA_ARGS__) : 0 ) + +typedef struct SRVConfig { + ECPDHKey key_perma; + uint16_t capabilities; +} SRVConfig; + +SRVConfig *srv_get_config(void); diff --git a/ecp/server/vlink.c b/ecp/server/vlink.c new file mode 100644 index 0000000..e98dacc --- /dev/null +++ b/ecp/server/vlink.c @@ -0,0 +1,321 @@ +#include <stdlib.h> +#include <unistd.h> +#include <string.h> + +#include <ecp/core.h> +#include <ecp/dir/dir.h> +#include <ecp/vconn/vconn.h> +#include <ecp/ht.h> +#include <ecp/tm.h> + +#include "server.h" +#include "dir.h" +#include "ht.h" + +#include "vlink.h" + +static ecp_ht_table_t *vlink_conn = NULL; +static ecp_ht_table_t *vlink_node = NULL; +static pthread_mutex_t vlink_conn_mutex; +static pthread_mutex_t vlink_node_mutex; +static pthread_t vlink_open_thd; +static pthread_t vlink_keyx_thd; + +static SRVConfig *srv_config; + +void vlink_handle_err(ECPConnection *conn, unsigned char mtype, int err) { + switch (mtype) { + case ECP_MTYPE_OPEN_REP: { + if (err == ECP_ERR_TIMEOUT) { + int rv; + + rv = vlink_insert_node(conn); + if (rv) LOG(LOG_ERR, "vlink insert node err:%d\n", rv); + } + ecp_conn_close(conn); + break; + } + } +} + +int vlink_handle_open(ECPConnection *conn, ECP2Buffer *bufs) { + int rv; + + rv = ecp_vlink_handle_open(conn, bufs); + if (rv) return rv; + + if (ecp_conn_is_inb(conn)) return ECP_OK; + + pthread_mutex_lock(&vlink_conn_mutex); + rv = ht_insert_conn(vlink_conn, conn); + pthread_mutex_unlock(&vlink_conn_mutex); + + if (rv) { + ecp_vlink_handle_close(conn); + return rv; + } + + return ECP_OK; +} + +void vlink_handle_close(ECPConnection *conn) { + int rv; + + ecp_vlink_handle_close(conn); + + if (ecp_conn_is_inb(conn)) return; + + rv = vlink_insert_node(conn); + if (rv) LOG(LOG_ERR, "vlink insert node err:%d\n", rv); +} + +int vlink_open_conn(ECPSocket *sock, ECPNode *node) { + ECPConnection *conn; + int rv; + + conn = malloc(sizeof(ECPConnection)); + if (conn == NULL) return ECP_ERR_ALLOC; + + ecp_vlink_init(conn, sock); + rv = ecp_conn_open(conn, node); + return rv; +} + +void vlink_new_node(ECPSocket *sock, ECPDirItem *dir_item) { + if ((dir_item->capabilities & ECP_DIR_CAP_VCONN) && (memcmp(&dir_item->node.key_perma.public, &srv_config->key_perma.public, sizeof(srv_config->key_perma.public)) < 0)) { + int rv; + + LOG(LOG_DEBUG, "vlink open connection\n"); + rv = vlink_open_conn(sock, &dir_item->node); + if (rv) LOG(LOG_ERR, "vlink open connection err:%d\n", rv); + } +} + +int vlink_insert_node(ECPConnection *conn) { + DirNode *node; + int rv = ECP_OK; + + node = dir_search_conn(conn); + if (node) { + pthread_mutex_lock(&vlink_node_mutex); + rv = ht_insert_node(vlink_node, node); + pthread_mutex_unlock(&vlink_node_mutex); + + if (rv) { + pthread_mutex_lock(&node->mutex); + node->refcount--; + pthread_mutex_unlock(&node->mutex); + } + } + + return rv; +} + +void vlink_open(ECPSocket *sock) { + struct hashtable_itr itr; + DirNode *node; + DirNode *node_next; + DirNode *open_node[MAX_OPEN_CNT]; + int open_cnt; + int i; + int rv; + + node_next = NULL; + do { + open_cnt = 0; + pthread_mutex_lock(&vlink_node_mutex); + + if (ecp_ht_count(vlink_node) > 0) { + ecp_ht_itr_create(&itr, vlink_node); + if (node_next) { + rv = ecp_ht_itr_search(&itr, node_next); + if (rv) { + LOG(LOG_ERR, "vlink open itr search err:%d\n", rv); + break; + } + node_next = NULL; + } + do { + node = ecp_ht_itr_value(&itr); + rv = ecp_ht_itr_remove(&itr); + + open_node[open_cnt] = node; + open_cnt++; + if (open_cnt == MAX_OPEN_CNT) { + if (!rv) { + node_next = ecp_ht_itr_key(&itr); + } else { + node_next = NULL; + } + } + } while (rv == ECP_OK); + } + + pthread_mutex_unlock(&vlink_node_mutex); + + for (i=0; i<open_cnt; i++) { + pthread_mutex_lock(&open_node[i]->mutex); + + if (open_node[i]->zombie) { + open_node[i]->refcount--; + + pthread_mutex_unlock(&open_node[i]->mutex); + } else { + pthread_mutex_unlock(&open_node[i]->mutex); + + LOG(LOG_DEBUG, "vlink open connection\n"); + rv = vlink_open_conn(sock, &open_node[i]->dir_item.node); + if (rv) LOG(LOG_ERR, "vlink open connection err:%d\n", rv); + } + } + } while (node_next); +} + +void vlink_keyx(void) { + struct hashtable_itr itr; + ecp_sts_t now; + ECPConnection *conn; + ECPConnection *keyx_next; + ECPConnection *keyx_conn[MAX_KEYX_CNT]; + int keyx_conn_z[MAX_KEYX_CNT]; + int keyx_cnt; + int is_zombie; + int i; + int rv; + + now = ecp_tm_get_s(); + keyx_next = NULL; + do { + keyx_cnt = 0; + pthread_mutex_lock(&vlink_conn_mutex); + + if (ecp_ht_count(vlink_conn) > 0) { + ecp_ht_itr_create(&itr, vlink_conn); + if (keyx_next) { + rv = ecp_ht_itr_search(&itr, keyx_next); + if (rv) { + LOG(LOG_ERR, "vlink keyx itr search err:%d\n", rv); + break; + } + keyx_next = NULL; + } + do { + conn = ecp_ht_itr_value(&itr); + + is_zombie = ecp_conn_is_zombie(conn, now, CONN_EXP_TIME); + if (is_zombie) { + rv = ecp_ht_itr_remove(&itr); + } else { + rv = ecp_ht_itr_advance(&itr); + } + + keyx_conn[keyx_cnt] = conn; + keyx_conn_z[keyx_cnt] = is_zombie; + keyx_cnt++; + + if (keyx_cnt == MAX_KEYX_CNT) { + if (!rv) { + keyx_next = ecp_ht_itr_key(&itr); + } else { + keyx_next = NULL; + } + } + } while (rv == ECP_OK); + } + + pthread_mutex_unlock(&vlink_conn_mutex); + + for (i=0; i<keyx_cnt; i++) { + if (keyx_conn_z[i]) { + LOG(LOG_DEBUG, "vlink close connection\n"); + ecp_conn_close(keyx_conn[i]); + } else { + ssize_t _rv; + + LOG(LOG_DEBUG, "vlink send keyx\n"); + _rv = ecp_send_keyx_req(keyx_conn[i], 1); + if (_rv < 0) LOG(LOG_ERR, "vlink send keyx err:%ld\n", _rv); + } + } + } while (keyx_next); +} + +static void *_vlink_open(void *arg) { + ECPSocket *sock = arg; + + while (1) { + LOG(LOG_DEBUG, "vlink open...\n"); + vlink_open(sock); + sleep(10); + } + + return NULL; +} + +static void *_vlink_keyx(void *arg) { + while (1) { + LOG(LOG_DEBUG, "vlink keyx...\n"); + vlink_keyx(); + sleep(10); + } + + return NULL; +} + +int vlink_start_open(ECPSocket *sock) { + int rv; + + rv = pthread_create(&vlink_open_thd, NULL, _vlink_open, sock); + if (rv) return ECP_ERR; + return ECP_OK; +} + +int vlink_start_keyx(void) { + int rv; + + rv = pthread_create(&vlink_keyx_thd, NULL, _vlink_keyx, NULL); + if (rv) return ECP_ERR; + return ECP_OK; +} + +int vlink_init(ECPContext *ctx) { + ECPConnHandler *handler; + int rv; + + srv_config = srv_get_config(); + + handler = ecp_ctx_get_handler(ctx, ECP_CTYPE_VLINK); + if (handler == NULL) return ECP_ERR; + + handler->handle_open = vlink_handle_open; + handler->handle_close = vlink_handle_close; + + rv = pthread_mutex_init(&vlink_conn_mutex, NULL); + if (rv) { + return ECP_ERR; + } + + rv = pthread_mutex_init(&vlink_node_mutex, NULL); + if (rv) { + pthread_mutex_destroy(&vlink_conn_mutex); + return ECP_ERR; + } + + rv = ECP_OK; + vlink_conn = ecp_ht_create_keys(); + if (vlink_conn == NULL) rv = ECP_ERR_ALLOC; + if (!rv) { + vlink_node = ecp_ht_create_keys(); + if (vlink_node == NULL) rv = ECP_ERR_ALLOC; + } + + if (rv) { + pthread_mutex_destroy(&vlink_node_mutex); + pthread_mutex_destroy(&vlink_conn_mutex); + if (vlink_node) ecp_ht_destroy(vlink_node); + if (vlink_conn) ecp_ht_destroy(vlink_conn); + return rv; + } + + return ECP_OK; +} diff --git a/ecp/server/vlink.h b/ecp/server/vlink.h new file mode 100644 index 0000000..78af6b1 --- /dev/null +++ b/ecp/server/vlink.h @@ -0,0 +1,17 @@ +#define MAX_KEYX_CNT 100 +#define MAX_OPEN_CNT 100 + +#define CONN_EXP_TIME 22 + +void vlink_handle_err(ECPConnection *conn, unsigned char mtype, int err); +int vlink_handle_open(ECPConnection *conn, ECP2Buffer *bufs); +void vlink_handle_close(ECPConnection *conn); +int vlink_open_conn(ECPSocket *sock, ECPNode *node); +void vlink_new_node(ECPSocket *sock, ECPDirItem *item); +int vlink_insert_node(ECPConnection *conn); + +void vlink_keyx(void); +void vlink_open(ECPSocket *sock); +int vlink_start_open(ECPSocket *sock); +int vlink_start_keyx(void); +int vlink_init(ECPContext *ctx); diff --git a/ecp/src/ecp/common.mk b/ecp/src/ecp/common.mk index a07b5c5..9380130 100644 --- a/ecp/src/ecp/common.mk +++ b/ecp/src/ecp/common.mk @@ -7,11 +7,7 @@ platform_dir = $(abspath $(src_dir)/platform/$(platform)) include $(platform_dir)/platform.mk include $(platform_dir)/features.mk -CFLAGS += -I$(src_dir)/ecp -I$(ssl_dir)/include -I$(platform_dir) - -ifeq ($(with_dirsrv),yes) -with_dir = yes -endif +CFLAGS += -I$(src_dir) -I$(platform_dir) -I$(ssl_dir)/include ifeq ($(with_pthread),yes) CFLAGS += -DECP_WITH_PTHREAD=1 @@ -27,24 +23,29 @@ CFLAGS += -DECP_WITH_VCONN=1 subdirs += vconn endif +ifeq ($(with_frag),yes) +CFLAGS += -DECP_WITH_FRAG=1 +ext_subdir = yes +endif + ifeq ($(with_rbuf),yes) CFLAGS += -DECP_WITH_RBUF=1 -subdirs += ext +ext_subdir = yes endif ifeq ($(with_msgq),yes) CFLAGS += -DECP_WITH_MSGQ=1 endif +ifdef ext_subdir +subdirs += ext +endif + ifeq ($(with_dir),yes) CFLAGS += -DECP_WITH_DIR=1 subdirs += dir endif -ifeq ($(with_dirsrv),yes) -CFLAGS += -DECP_WITH_DIRSRV=1 -endif - ifeq ($(with_debug),yes) CFLAGS += -DECP_DEBUG=1 endif diff --git a/ecp/src/ecp/core.c b/ecp/src/ecp/core.c index 1ba042d..9585388 100644 --- a/ecp/src/ecp/core.c +++ b/ecp/src/ecp/core.c @@ -12,10 +12,6 @@ #include "vconn/vconn.h" #endif -#ifdef ECP_WITH_DIR -#include "dir/dir.h" -#endif - #include "cr.h" #include "tr.h" #include "tm.h" @@ -34,13 +30,12 @@ int ecp_dhkey_gen(ECPDHKey *key) { return ECP_OK; } -int ecp_ctx_init(ECPContext *ctx, ecp_err_handler_t handle_err, ecp_dir_handler_t handle_dir, ecp_conn_alloc_t conn_alloc, ecp_conn_free_t conn_free) { +int ecp_ctx_init(ECPContext *ctx, ecp_err_handler_t handle_err, ecp_conn_new_t conn_new, ecp_conn_free_t conn_free) { int rv; memset(ctx, 0, sizeof(ECPContext)); ctx->handle_err = handle_err; - ctx->handle_dir = handle_dir; - ctx->conn_alloc = conn_alloc; + ctx->conn_new = conn_new; ctx->conn_free = conn_free; rv = ecp_tr_init(ctx); @@ -52,14 +47,44 @@ int ecp_ctx_init(ECPContext *ctx, ecp_err_handler_t handle_err, ecp_dir_handler_ return ECP_OK; } -int ecp_ctx_set_handler(ECPContext *ctx, ECPConnHandler *handler, unsigned char ctype) { - if (ctype >= ECP_MAX_CTYPE) return ECP_ERR_CTYPE; +int ecp_ctx_set_handler(ECPContext *ctx, unsigned char ctype, ECPConnHandler *handler) { + unsigned char _ctype = ctype & ECP_CTYPE_MASK; - ctx->handler[ctype] = handler; + if (ctype & ECP_CTYPE_FLAG_SYS) { + if (_ctype >= ECP_MAX_CTYPE_SYS) return ECP_ERR_CTYPE; + ctx->handler_sys[_ctype] = handler; + } else { + if (_ctype >= ECP_MAX_CTYPE) return ECP_ERR_CTYPE; + ctx->handler[_ctype] = handler; + } return ECP_OK; } -int ecp_node_init(ECPNode *node, ecp_ecdh_public_t *public, void *addr) { +ECPConnHandler *ecp_ctx_get_handler(ECPContext *ctx, unsigned char ctype) { + unsigned char _ctype = ctype & ECP_CTYPE_MASK; + + if (ctype & ECP_CTYPE_FLAG_SYS) { + if (_ctype >= ECP_MAX_CTYPE_SYS) return NULL; + return ctx->handler_sys[_ctype]; + } else { + if (_ctype >= ECP_MAX_CTYPE) return NULL; + return ctx->handler[_ctype]; + } + return NULL; +} + +int ecp_addr_init(ecp_tr_addr_t *addr, void *addr_s) { + int rv = ECP_OK; + + memset(addr, 0, sizeof(ecp_tr_addr_t)); + if (addr_s) { + rv = ecp_tr_addr_set(addr, addr_s); + } + + return rv; +} + +void ecp_node_init(ECPNode *node, ecp_ecdh_public_t *public, ecp_tr_addr_t *addr) { memset(node, 0, sizeof(ECPNode)); if (public) { @@ -70,13 +95,8 @@ int ecp_node_init(ECPNode *node, ecp_ecdh_public_t *public, void *addr) { } if (addr) { - int rv; - - rv = ecp_tr_addr_set(&node->addr, addr); - if (rv) return ECP_ERR_NET_ADDR; + node->addr = *addr; } - - return ECP_OK; } void ecp_node_set_pub(ECPNode *node, ecp_ecdh_public_t *public) { @@ -106,7 +126,7 @@ static int conn_table_create(ECPConnTable *conn_table) { return ECP_ERR; } - rv = pthread_mutex_init(&conn_table->mutex_inb, NULL); + rv = pthread_mutex_init(&conn_table->mutex_gc, NULL); if (rv) { pthread_mutex_destroy(&conn_table->mutex); return ECP_ERR; @@ -120,8 +140,8 @@ static int conn_table_create(ECPConnTable *conn_table) { if (conn_table->keys == NULL) rv = ECP_ERR_ALLOC; if (!rv) { - conn_table->keys_inb = ecp_ht_create_keys(); - if (conn_table->keys_inb == NULL) rv = ECP_ERR_ALLOC; + conn_table->keys_gc = ecp_ht_create_keys(); + if (conn_table->keys_gc == NULL) rv = ECP_ERR_ALLOC; } if (!rv) { @@ -131,11 +151,11 @@ static int conn_table_create(ECPConnTable *conn_table) { if (rv) { #ifdef ECP_WITH_PTHREAD - pthread_mutex_destroy(&conn_table->mutex_inb); + pthread_mutex_destroy(&conn_table->mutex_gc); pthread_mutex_destroy(&conn_table->mutex); #endif if (conn_table->addrs) ecp_ht_destroy(conn_table->addrs); - if (conn_table->keys_inb) ecp_ht_destroy(conn_table->keys_inb); + if (conn_table->keys_gc) ecp_ht_destroy(conn_table->keys_gc); if (conn_table->keys) ecp_ht_destroy(conn_table->keys); } #endif @@ -145,12 +165,12 @@ static int conn_table_create(ECPConnTable *conn_table) { static void conn_table_destroy(ECPConnTable *conn_table) { #ifdef ECP_WITH_PTHREAD - pthread_mutex_destroy(&conn_table->mutex_inb); + pthread_mutex_destroy(&conn_table->mutex_gc); pthread_mutex_destroy(&conn_table->mutex); #endif #ifdef ECP_WITH_HTABLE ecp_ht_destroy(conn_table->addrs); - ecp_ht_destroy(conn_table->keys_inb); + ecp_ht_destroy(conn_table->keys_gc); ecp_ht_destroy(conn_table->keys); #endif } @@ -162,7 +182,7 @@ static int conn_table_insert(ECPConnection *conn) { int i, rv = ECP_OK; if (ecp_conn_is_outb(conn)) { - if (ecp_conn_is_root(conn) && !ecp_conn_is_open(conn)) { + if (ecp_conn_is_root(conn) && !_ecp_conn_is_open(conn)) { rv = ecp_ht_insert(sock->conn_table.addrs, &conn->remote.addr, conn); if (rv) return rv; } @@ -178,8 +198,8 @@ static int conn_table_insert(ECPConnection *conn) { ecp_ht_remove(sock->conn_table.keys, &conn->key[j].public); } } - if (ecp_conn_is_root(conn) && !ecp_conn_is_open(conn)) { - ecp_ht_remove(sock->conn_table.addrs, &conn->remote.addr); + if (ecp_conn_is_root(conn) && !_ecp_conn_is_open(conn)) { + ecp_ht_remove_kv(sock->conn_table.addrs, &conn->remote.addr, conn); } return rv; } @@ -202,25 +222,24 @@ static int conn_table_insert(ECPConnection *conn) { } } } -#else + +#else /* ECP_WITH_HTABLE */ + if (sock->conn_table.size == ECP_MAX_SOCK_CONN) return ECP_ERR_FULL; sock->conn_table.arr[sock->conn_table.size] = conn; sock->conn_table.size++; -#endif + +#endif /* ECP_WITH_HTABLE */ return ECP_OK; } -static int conn_table_insert_inb(ECPConnection *conn) { +static int conn_table_insert_gc(ECPConnection *conn) { ECPSocket *sock = conn->sock; int rv = ECP_OK; #ifdef ECP_WITH_HTABLE - unsigned char idx; - - idx = conn->rkey_curr % ECP_MAX_NODE_KEY; - - rv = ecp_ht_insert(sock->conn_table.keys_inb, &conn->rkey[idx].public, conn); + rv = ecp_ht_insert(sock->conn_table.keys_gc, &conn->remote.key_perma.public, conn); #endif return rv; @@ -231,14 +250,15 @@ static void conn_table_remove(ECPConnection *conn) { int i; #ifdef ECP_WITH_HTABLE + if (ecp_conn_is_outb(conn)) { for (i=0; i<ECP_MAX_CONN_KEY; i++) { if (conn->key[i].valid) { ecp_ht_remove(sock->conn_table.keys, &conn->key[i].public); } } - if (ecp_conn_is_root(conn) && !ecp_conn_is_open(conn)) { - ecp_ht_remove(sock->conn_table.addrs, &conn->remote.addr); + if (ecp_conn_is_root(conn) && !_ecp_conn_is_open(conn)) { + ecp_ht_remove_kv(sock->conn_table.addrs, &conn->remote.addr, conn); } } else { for (i=0; i<ECP_MAX_NODE_KEY; i++) { @@ -247,7 +267,9 @@ static void conn_table_remove(ECPConnection *conn) { } } } -#else + +#else /* ECP_WITH_HTABLE */ + for (i=0; i<sock->conn_table.size; i++) { if (conn == sock->conn_table.arr[i]) { while (i < (sock->conn_table.size-1)) { @@ -259,130 +281,176 @@ static void conn_table_remove(ECPConnection *conn) { return; } } -#endif + +#endif /* ECP_WITH_HTABLE */ } static void conn_table_remove_addr(ECPConnection *conn) { ECPSocket *sock = conn->sock; #ifdef ECP_WITH_HTABLE - ecp_ht_remove(sock->conn_table.addrs, &conn->remote.addr); + ecp_ht_remove_kv(sock->conn_table.addrs, &conn->remote.addr, conn); #endif } -static ECPConnection *conn_table_search(ECPSocket *sock, unsigned char c_idx, ecp_ecdh_public_t *c_public, ecp_tr_addr_t *addr, ECPConnection *parent) { -#ifdef ECP_WITH_VCONN - if ((c_public == NULL) && parent) { - return parent->next; - } -#endif - +static ECPConnection *conn_table_search_pub(ECPSocket *sock, unsigned char c_idx, ecp_ecdh_public_t *c_public) { #ifdef ECP_WITH_HTABLE - if (c_public) { - return ecp_ht_search(sock->conn_table.keys, c_public); - } else if (addr) { - return ecp_ht_search(sock->conn_table.addrs, addr); - } else { - return NULL; - } -#else + return ecp_ht_search(sock->conn_table.keys, c_public); +#else /* ECP_WITH_HTABLE */ ECPConnection *conn = NULL; int i; - if (c_public) { - for (i=0; i<sock->conn_table.size; i++) { - conn = sock->conn_table.arr[i]; - if (ecp_conn_is_outb(conn)) { - if (c_idx >= ECP_MAX_CONN_KEY) continue; + for (i=0; i<sock->conn_table.size; i++) { + conn = sock->conn_table.arr[i]; + if (ecp_conn_is_outb(conn)) { + if (c_idx >= ECP_MAX_CONN_KEY) continue; - if (conn->key[c_idx].valid && ecp_ecdh_pub_eq(c_public, &conn->key[c_idx].public)) { - return conn; - } - } else { - unsigned char _c_idx; + if (conn->key[c_idx].valid && ecp_ecdh_pub_eq(c_public, &conn->key[c_idx].public)) { + return conn; + } + } else { + unsigned char _c_idx; - if (c_idx & ~ECP_ECDH_IDX_MASK) continue; + if (c_idx & ~ECP_ECDH_IDX_MASK) continue; - _c_idx = c_idx % ECP_MAX_NODE_KEY; - if (conn->rkey[_c_idx].valid && ecp_ecdh_pub_eq(c_public, &conn->rkey[_c_idx].public)) { - return conn; - } + _c_idx = c_idx % ECP_MAX_NODE_KEY; + if (conn->rkey[_c_idx].valid && ecp_ecdh_pub_eq(c_public, &conn->rkey[_c_idx].public)) { + return conn; } } - } else if (addr) { - for (i=0; i<sock->conn_table.size; i++) { - conn = sock->conn_table.arr[i]; + } + + return NULL; +#endif /* ECP_WITH_HTABLE */ +} +static ECPConnection *conn_table_search_addr(ECPSocket *sock, ecp_tr_addr_t *addr) { +#ifdef ECP_WITH_HTABLE + return ecp_ht_search(sock->conn_table.addrs, addr); +#else /* ECP_WITH_HTABLE */ + ECPConnection *conn = NULL; + int i; + + for (i=0; i<sock->conn_table.size; i++) { + conn = sock->conn_table.arr[i]; + if (ecp_conn_is_root(conn) && ecp_conn_is_outb(conn) && ecp_tr_addr_eq(&conn->remote.addr, addr)) { + return conn; + } + } + + return NULL; +#endif /* ECP_WITH_HTABLE */ +} + +static ECPConnection *conn_table_search_addr_next(ECPSocket *sock, ecp_tr_addr_t *addr, ECPConnection *_conn) { +#ifdef ECP_WITH_HTABLE + return ecp_ht_search_next(sock->conn_table.addrs, addr, _conn); +#else /* ECP_WITH_HTABLE */ + ECPConnection *conn = NULL; + int i, f; + + f = 0; + for (i=0; i<sock->conn_table.size; i++) { + conn = sock->conn_table.arr[i]; + if (f) if (ecp_conn_is_root(conn) && ecp_conn_is_outb(conn) && ecp_tr_addr_eq(&conn->remote.addr, addr)) { return conn; } + } else if (conn == _conn) { + f = 1; } } return NULL; +#endif /* ECP_WITH_HTABLE */ +} + +static ECPConnection *conn_table_search(ECPSocket *sock, unsigned char c_idx, ecp_ecdh_public_t *c_public, ecp_tr_addr_t *addr, ECPConnection *parent) { +#ifdef ECP_WITH_VCONN + if ((c_public == NULL) && parent) { + if ((parent->type == ECP_CTYPE_VCONN) && ecp_conn_is_outb(parent)) { + ECPVConnOutb *_parent = (ECPVConnOutb *)parent; + + return _parent->next; + } + return NULL; + } #endif + + if (c_public) { + return conn_table_search_pub(sock, c_idx, c_public); + } else if (addr) { + return conn_table_search_addr(sock, addr); + } + + return NULL; } -static void conn_table_expire_inb(ECPSocket *sock, ecp_sts_t to) { +static void conn_table_expire(ECPSocket *sock, ecp_sts_t to, ecp_conn_expired_t conn_expired) { ECPConnection *conn; ECPConnection *to_remove[ECP_MAX_EXP]; - int i, remove_cnt; - ecp_sts_t access_ts, now = ecp_tm_abstime_ms(0); + int i, remove_cnt, expired; + ecp_sts_t now = ecp_tm_get_s(); #ifdef ECP_WITH_HTABLE struct hashtable_itr itr; void *remove_next; int rv = ECP_OK; + remove_next = NULL; do { + ecp_ht_table_t *keys_gc = sock->conn_table.keys_gc; remove_cnt = 0; #ifdef ECP_WITH_PTHREAD - pthread_mutex_lock(&sock->conn_table.mutex_inb); + pthread_mutex_lock(&sock->conn_table.mutex_gc); #endif - ecp_ht_itr_create(&itr, sock->conn_table.keys_inb); - if (remove_next) { - ecp_ht_itr_search(&itr, remove_next); - remove_next = NULL; - } - do { - conn = ecp_ht_itr_value(&itr); - if (conn) { + if (ecp_ht_count(keys_gc) > 0) { + ecp_ht_itr_create(&itr, keys_gc); + if (remove_next) { + ecp_ht_itr_search(&itr, remove_next); + remove_next = NULL; + } + do { + conn = ecp_ht_itr_value(&itr); + #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn->mutex); #endif - access_ts = conn->access_ts; + expired = conn_expired(conn, now, to); #ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&conn->mutex); #endif - if (now - access_ts > to) { + if (expired) { + rv = ecp_ht_itr_remove(&itr); + to_remove[remove_cnt] = conn; remove_cnt++; - rv = ecp_ht_itr_remove(&itr); if (remove_cnt == ECP_MAX_EXP) { - if (!rv) remove_next = ecp_ht_itr_key(&itr); + if (!rv) { + remove_next = ecp_ht_itr_key(&itr); + } else { + remove_next = NULL; + } break; } } else { rv = ecp_ht_itr_advance(&itr); } - } else { - rv = ECP_ITR_END; - } - } while (rv == ECP_OK); + } while (rv == ECP_OK); + } #ifdef ECP_WITH_PTHREAD - pthread_mutex_unlock(&sock->conn_table.mutex_inb); + pthread_mutex_unlock(&sock->conn_table.mutex_gc); #endif for (i=0; i<remove_cnt; i++) { _ecp_conn_close(to_remove[i]); } - } while (remove_next); #else /* ECP_WITH_HTABLE */ @@ -396,18 +464,18 @@ static void conn_table_expire_inb(ECPSocket *sock, ecp_sts_t to) { for (i=0; i<sock->conn_table.size; i++) { conn = sock->conn_table.arr[i]; - if (ecp_conn_is_inb(conn)) { + if (ecp_conn_is_gc(conn)) { #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn->mutex); #endif - access_ts = conn->access_ts; + expired = conn_expired(conn, now, to); #ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&conn->mutex); #endif - if (now - access_ts > to)) { + if (expired) { to_remove[remove_cnt] = conn; remove_cnt++; if (remove_cnt == ECP_MAX_EXP) break; @@ -482,7 +550,7 @@ void ecp_sock_destroy(ECPSocket *sock) { #endif } -int ecp_sock_open(ECPSocket *sock, void *myaddr) { +int ecp_sock_open(ECPSocket *sock, ecp_tr_addr_t *myaddr) { return ecp_tr_open(sock, myaddr); } @@ -596,11 +664,20 @@ void ecp_sock_get_nonce(ECPSocket *sock, ecp_nonce_t *nonce) { #endif } +static int _conn_expired_inb(ECPConnection *conn, ecp_sts_t now, ecp_sts_t to) { + if (ecp_conn_is_inb(conn) && _ecp_conn_expired(conn, now, to)) return 1; + return 0; +} + +void ecp_sock_expire(ECPSocket *sock, ecp_sts_t to, ecp_conn_expired_t conn_expired) { + conn_table_expire(sock, to, conn_expired); +} + int ecp_sock_expire_inb(ECPSocket *sock, ecp_sts_t to) { int rv; rv = ecp_sock_minkey_new(sock); - if (!rv) conn_table_expire_inb(sock, to); + if (!rv) conn_table_expire(sock, to, _conn_expired_inb); return rv; } @@ -654,7 +731,7 @@ static int conn_dhkey_new(ECPConnection *conn, unsigned char idx, ECPDHKey *key) if (idx >= ECP_MAX_CONN_KEY) return ECP_ERR_ECDH_IDX; #ifdef ECP_WITH_HTABLE - if (ecp_conn_is_outb(conn) && ecp_conn_is_reg(conn) && conn->key[idx].valid) { + if (ecp_conn_is_outb(conn) && _ecp_conn_is_reg(conn) && conn->key[idx].valid) { ecp_ht_remove(sock->conn_table.keys, &conn->key[idx].public); } #endif @@ -662,7 +739,7 @@ static int conn_dhkey_new(ECPConnection *conn, unsigned char idx, ECPDHKey *key) conn->key[idx] = *key; #ifdef ECP_WITH_HTABLE - if (ecp_conn_is_outb(conn) && ecp_conn_is_reg(conn)) { + if (ecp_conn_is_outb(conn) && _ecp_conn_is_reg(conn)) { int rv; rv = ecp_ht_insert(sock->conn_table.keys, &conn->key[idx].public, conn); @@ -679,7 +756,7 @@ static void conn_dhkey_del(ECPConnection *conn, unsigned char idx) { if (idx >= ECP_MAX_CONN_KEY) return; #ifdef ECP_WITH_HTABLE - if (ecp_conn_is_outb(conn) && ecp_conn_is_reg(conn) && conn->key[idx].valid) { + if (ecp_conn_is_outb(conn) && _ecp_conn_is_reg(conn) && conn->key[idx].valid) { ecp_ht_remove(sock->conn_table.keys, &conn->key[idx].public); } #endif @@ -713,7 +790,7 @@ static ECPDHPub *conn_dhkey_get_remote(ECPConnection *conn, unsigned char idx) { } /* node will send public key */ -static void conn_dhkey_get_pub(ECPConnection *conn, unsigned char idx) { +static void conn_dhkey_send_pub(ECPConnection *conn, unsigned char idx) { unsigned char _idx; int i; @@ -744,10 +821,10 @@ static int conn_dhkey_set_pub(ECPConnection *conn, unsigned char idx, ecp_ecdh_p _idx = idx % ECP_MAX_NODE_KEY; key = &conn->rkey[_idx]; - if (key->valid && (memcmp(public, &key->public, sizeof(key->public)) == 0)) return ECP_ERR_ECDH_KEY_DUP; + if (key->valid && (memcmp(public, &key->public, sizeof(key->public)) == 0)) return ECP_ERR_DUP; #ifdef ECP_WITH_HTABLE - if (ecp_conn_is_inb(conn) && ecp_conn_is_reg(conn) && (key->valid)) { + if (ecp_conn_is_inb(conn) && _ecp_conn_is_reg(conn) && (key->valid)) { ecp_ht_remove(sock->conn_table.keys, &key->public); } #endif @@ -756,7 +833,7 @@ static int conn_dhkey_set_pub(ECPConnection *conn, unsigned char idx, ecp_ecdh_p key->valid = 1; #ifdef ECP_WITH_HTABLE - if (ecp_conn_is_inb(conn) && ecp_conn_is_reg(conn)) { + if (ecp_conn_is_inb(conn) && _ecp_conn_is_reg(conn)) { int rv; rv = ecp_ht_insert(sock->conn_table.keys, &key->public, conn); @@ -837,24 +914,11 @@ static int conn_shkey_set(ECPConnection *conn, unsigned char s_idx, unsigned cha return ECP_OK; } -int ecp_conn_alloc(ECPSocket *sock, unsigned char ctype, ECPConnection **_conn) { +ECPConnection *ecp_conn_new_inb(ECPSocket *sock, unsigned char ctype) { ECPContext *ctx = sock->ctx; - ECPConnection *conn; - int rv; - - if (ctx->conn_alloc == NULL) return ECP_ERR_ALLOC; - conn = ctx->conn_alloc(sock, ctype); - if (conn == NULL) return ECP_ERR_ALLOC; - - *_conn = conn; - return ECP_OK; -} - -void ecp_conn_free(ECPConnection *conn) { - ECPContext *ctx = conn->sock->ctx; - - if (ctx->conn_free) ctx->conn_free(conn); + if (ctx->conn_new) return ctx->conn_new(sock, ctype); + return NULL; } void ecp_conn_init(ECPConnection *conn, ECPSocket *sock, unsigned char ctype) { @@ -868,138 +932,146 @@ void ecp_conn_init(ECPConnection *conn, ECPSocket *sock, unsigned char ctype) { conn->key_next = ECP_ECDH_IDX_INV; conn->rkey_curr = ECP_ECDH_IDX_INV; arc4random_buf(&conn->nonce_out, sizeof(conn->nonce_out)); + conn->access_ts = ecp_tm_get_s(); } -void ecp_conn_reinit(ECPConnection *conn) { - conn->flags = 0; +int ecp_conn_reset(ECPConnection *conn) { + if (conn->flags) return ECP_ERR; + + conn->key_curr = ECP_ECDH_IDX_INV; conn->key_next = ECP_ECDH_IDX_INV; conn->rkey_curr = ECP_ECDH_IDX_INV; memset(&conn->key, 0, sizeof(conn->key)); memset(&conn->rkey, 0, sizeof(conn->rkey)); memset(&conn->shkey, 0, sizeof(conn->shkey)); arc4random_buf(&conn->nonce_out, sizeof(conn->nonce_out)); + conn->nonce_in = 0; + conn->nonce_map = 0; + + return ECP_OK; } -int ecp_conn_create(ECPConnection *conn, ECPSocket *sock, unsigned char ctype) { +int ecp_conn_create(ECPConnection *conn, ECPConnection *parent) { int rv; +#ifdef ECP_WITH_VCONN + unsigned short pcount; - ecp_conn_init(conn, sock, ctype); + pcount = parent ? parent->pcount + 1 : 0; + if (pcount > ECP_MAX_PARENT) return ECP_ERR_MAX_PARENT; +#endif #ifdef ECP_WITH_PTHREAD rv = pthread_mutex_init(&conn->mutex, NULL); if (rv) return ECP_ERR; #endif - return ECP_OK; -} - -int ecp_conn_create_inb(ECPConnection *conn, ECPSocket *sock, unsigned char ctype) { - int rv; - - rv = ecp_conn_create(conn, sock, ctype); - if (rv) return rv; - - ecp_conn_set_inb(conn); - -#ifdef ECP_WITH_VCONN - if (conn->parent) { - ecp_conn_refcount_inc(conn->parent); - } + rv = ecp_ext_conn_create(conn); + if (rv) { +#ifdef ECP_WITH_PTHREAD + pthread_mutex_destroy(&conn->mutex); #endif - return ECP_OK; -} - -void ecp_conn_destroy(ECPConnection *conn) { -#ifdef ECP_WITH_VCONN - if (ecp_conn_is_inb(conn) && conn->parent) { - ecp_conn_refcount_dec(conn->parent); + return ECP_ERR; } -#endif - ecp_ext_conn_destroy(conn); +#ifdef ECP_WITH_VCONN + if (parent) { + conn->parent = parent; + conn->pcount = pcount; -#ifdef ECP_WITH_PTHREAD - pthread_mutex_destroy(&conn->mutex); + ecp_conn_refcount_inc(parent); + } #endif -} - -void ecp_conn_set_flags(ECPConnection *conn, unsigned char flags) { - flags &= flags & ECP_CONN_FLAG_MASK; - conn->flags_im |= flags; -} -void ecp_conn_clr_flags(ECPConnection *conn, unsigned char flags) { - flags &= flags & ECP_CONN_FLAG_MASK; - conn->flags_im &= ~flags; -} - -void ecp_conn_set_remote_key(ECPConnection *conn, ECPDHPub *key) { - conn->remote.key_perma = *key; -} - -void ecp_conn_set_remote_addr(ECPConnection *conn, ecp_tr_addr_t *addr) { - conn->remote.addr = *addr; + return ECP_OK; } -int ecp_conn_init_inb(ECPConnection *conn, ECPConnection *parent, unsigned char s_idx, unsigned char c_idx, ecp_ecdh_public_t *public, ECPDHPub *remote_key, ecp_aead_key_t *shkey) { +int ecp_conn_create_inb(ECPConnection *conn, ECPConnection *parent, unsigned char s_idx, unsigned char c_idx, ecp_ecdh_public_t *public, ECPDHPub *remote_key, ecp_aead_key_t *shkey) { ECPSocket *sock = conn->sock; - unsigned short pcount; int rv; -#ifdef ECP_WITH_VCONN - pcount = (parent ? parent->pcount + 1 : 0); - if (pcount > ECP_MAX_PARENT) return ECP_ERR_MAX_PARENT; -#endif - if (ecp_conn_has_vbox(conn) && ((remote_key == NULL) || !remote_key->valid)) return ECP_ERR_VBOX; + ecp_conn_set_inb(conn); + rv = conn_dhkey_set_pub(conn, c_idx, public); if (rv) return rv; rv = conn_shkey_set(conn, s_idx, c_idx, shkey); if (rv) return rv; -#ifdef ECP_WITH_VCONN - conn->parent = parent; - conn->pcount = pcount; -#endif - + ecp_conn_set_flags(conn, ECP_CONN_FLAG_GC); conn->refcount = 1; conn->key_curr = s_idx; conn->rkey_curr = c_idx; - if (remote_key && remote_key->valid) conn->remote.key_perma = *remote_key; + if (remote_key && remote_key->valid) { + conn->remote.key_perma = *remote_key; + } else { + ECPDHPub *key_perma = &conn->remote.key_perma; + memcpy(&key_perma->public, public, sizeof(key_perma->public)); + } - return ECP_OK; + rv = ecp_conn_create(conn, parent); + return rv; } -int ecp_conn_init_outb(ECPConnection *conn, ECPNode *node) { +int ecp_conn_create_outb(ECPConnection *conn, ECPConnection *parent, ECPNode *node) { + int rv; + + ecp_conn_set_outb(conn); + + conn->refcount = 1; + if (node) conn->remote = *node; + + rv = ecp_conn_reset_outb(conn); + if (rv) return rv; + + rv = ecp_conn_create(conn, parent); + return rv; +} + +int ecp_conn_reset_outb(ECPConnection *conn) { ECPDHKey key; - unsigned char key_curr; int rv; - if (conn->key_curr == ECP_ECDH_IDX_INV) { - key_curr = 0; - } else { - key_curr = (conn->key_curr + 1) % ECP_MAX_CONN_KEY; - } + conn->key_curr = 0; + rv = ecp_dhkey_gen(&key); if (rv) return rv; - rv = conn_dhkey_new(conn, key_curr, &key); + rv = conn_dhkey_new(conn, conn->key_curr, &key); if (rv) return rv; - if (node) conn->remote = *node; - conn->key_curr = key_curr; - return ECP_OK; } +void ecp_conn_destroy(ECPConnection *conn) { +#ifdef ECP_WITH_VCONN + if (conn->parent) { + ecp_conn_refcount_dec(conn->parent); + } +#endif + + ecp_ext_conn_destroy(conn); + +#ifdef ECP_WITH_PTHREAD + pthread_mutex_destroy(&conn->mutex); +#endif + + ecp_conn_free(conn); +} + +void ecp_conn_free(ECPConnection *conn) { + ECPContext *ctx = conn->sock->ctx; + + if (ctx->conn_free) ctx->conn_free(conn); +} + int ecp_conn_insert(ECPConnection *conn) { ECPSocket *sock = conn->sock; int rv; - ecp_conn_set_reg(conn); + _ecp_conn_set_reg(conn); #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&sock->conn_table.mutex); @@ -1011,25 +1083,23 @@ int ecp_conn_insert(ECPConnection *conn) { pthread_mutex_unlock(&sock->conn_table.mutex); #endif - if (rv) ecp_conn_clr_reg(conn); + if (rv) _ecp_conn_clr_reg(conn); return rv; } -int ecp_conn_insert_inb(ECPConnection *conn) { +int ecp_conn_insert_gc(ECPConnection *conn) { ECPSocket *sock = conn->sock; int rv; #ifdef ECP_WITH_PTHREAD - pthread_mutex_lock(&sock->conn_table.mutex_inb); - pthread_mutex_lock(&conn->mutex); + pthread_mutex_lock(&sock->conn_table.mutex_gc); #endif - rv = conn_table_insert_inb(conn); + rv = conn_table_insert_gc(conn); #ifdef ECP_WITH_PTHREAD - pthread_mutex_unlock(&conn->mutex); - pthread_mutex_unlock(&sock->conn_table.mutex_inb); + pthread_mutex_unlock(&sock->conn_table.mutex_gc); #endif return rv; @@ -1043,9 +1113,9 @@ void ecp_conn_remove(ECPConnection *conn, unsigned short *refcount) { pthread_mutex_lock(&conn->mutex); #endif - if (ecp_conn_is_reg(conn)) { + if (_ecp_conn_is_reg(conn)) { conn_table_remove(conn); - ecp_conn_clr_reg(conn); + _ecp_conn_clr_reg(conn); } if (refcount) *refcount = conn->refcount; @@ -1072,57 +1142,64 @@ void ecp_conn_remove_addr(ECPConnection *conn) { } -int ecp_conn_open(ECPConnection *conn, ECPNode *node) { +int _ecp_conn_open(ECPConnection *conn, ECPConnection *parent, ECPNode *node, int retry) { int rv; ssize_t _rv; - rv = ecp_conn_init_outb(conn, node); + rv = ecp_conn_create_outb(conn, parent, node); if (rv) return rv; rv = ecp_conn_insert(conn); - if (rv) return rv; + if (rv) { + ecp_conn_destroy(conn); + return rv; + } + + if (ecp_conn_is_gc(conn)) { + rv = ecp_conn_insert_gc(conn); + if (rv) { + ecp_conn_refcount_dec(conn); + _ecp_conn_close(conn); + return rv; + } + } - _rv = ecp_send_init_req(conn); + _rv = ecp_send_init_req(conn, retry); if (_rv < 0) { - ecp_timer_remove(conn); - ecp_conn_remove(conn, NULL); + ecp_conn_refcount_dec(conn); + ecp_conn_close(conn); return _rv; } + ecp_conn_refcount_dec(conn); + return ECP_OK; } -int ecp_conn_reset(ECPConnection *conn) { - unsigned short refcount = 0; - int i; +int ecp_conn_open(ECPConnection *conn, ECPNode *node) { int rv; - /* timer holds one reference to this connection */ - ecp_conn_remove(conn, &refcount); - if (refcount > 1) return ECP_ERR_BUSY; - - ecp_conn_reinit(conn); - if (rv) return rv; - - rv = ecp_conn_init_outb(conn, NULL); - if (rv) return rv; + rv = _ecp_conn_open(conn, NULL, node, 1); + return rv; +} - rv = ecp_conn_insert(conn); - if (rv) return rv; +int ecp_conn_try_open(ECPConnection *conn, ECPNode *node) { + int rv; - return ECP_OK; + rv = _ecp_conn_open(conn, NULL, node, 0); + return rv; } static void conn_close(ECPConnection *conn) { - if (ecp_conn_is_open(conn)) { + if (_ecp_conn_is_open(conn)) { ecp_close_handler_t handler; - ecp_conn_clr_open(conn); + _ecp_conn_clr_open(conn); handler = ecp_get_close_handler(conn); if (handler) handler(conn); + ecp_ext_conn_close(conn); } ecp_conn_destroy(conn); - if (ecp_conn_is_inb(conn)) ecp_conn_free(conn); } int _ecp_conn_close(ECPConnection *conn) { @@ -1137,8 +1214,49 @@ int _ecp_conn_close(ECPConnection *conn) { } int ecp_conn_close(ECPConnection *conn) { - if (ecp_conn_is_inb(conn)) return ECP_ERR; - return _ecp_conn_close(conn); + int rv; + + if (ecp_conn_is_gc(conn)) { + ecp_conn_mark_closed(conn); + rv = ECP_OK; + } else { + rv = _ecp_conn_close(conn); + } + + return rv; +} + +void ecp_conn_mark_closed(ECPConnection *conn) { +#ifdef ECP_WITH_PTHREAD + pthread_mutex_lock(&conn->mutex); +#endif + + _ecp_conn_set_closed(conn); + +#ifdef ECP_WITH_PTHREAD + pthread_mutex_unlock(&conn->mutex); +#endif +} + +int _ecp_conn_expired(ECPConnection *conn, ecp_sts_t now, ecp_sts_t to) { + if (_ecp_conn_is_closed(conn) || (ECP_STS_LT(conn->access_ts, now) && (now - conn->access_ts > to))) return 1; + return 0; +} + +int ecp_conn_is_zombie(ECPConnection *conn, ecp_sts_t now, ecp_sts_t to) { + int z = 0; + +#ifdef ECP_WITH_PTHREAD + pthread_mutex_lock(&conn->mutex); +#endif + + z = _ecp_conn_expired(conn, now, to); + +#ifdef ECP_WITH_PTHREAD + pthread_mutex_unlock(&conn->mutex); +#endif + + return z; } void ecp_conn_refcount_inc(ECPConnection *conn) { @@ -1163,7 +1281,7 @@ void ecp_conn_refcount_dec(ECPConnection *conn) { conn->refcount--; refcount = conn->refcount; - is_reg = ecp_conn_is_reg(conn); + is_reg = _ecp_conn_is_reg(conn); #ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&conn->mutex); @@ -1172,6 +1290,24 @@ void ecp_conn_refcount_dec(ECPConnection *conn) { if (!is_reg && (refcount == 0)) conn_close(conn); } +void ecp_conn_set_flags(ECPConnection *conn, unsigned char flags) { + flags &= ECP_CONN_FLAG_MASK; + conn->flags_im |= flags; +} + +void ecp_conn_clr_flags(ECPConnection *conn, unsigned char flags) { + flags &= ECP_CONN_FLAG_MASK; + conn->flags_im &= ~flags; +} + +void ecp_conn_set_remote_key(ECPConnection *conn, ECPDHPub *key) { + conn->remote.key_perma = *key; +} + +void ecp_conn_set_remote_addr(ECPConnection *conn, ecp_tr_addr_t *addr) { + conn->remote.addr = *addr; +} + int ecp_conn_dhkey_new(ECPConnection *conn) { ECPSocket *sock = conn->sock; ECPDHKey new_key; @@ -1198,13 +1334,9 @@ int ecp_conn_dhkey_new(ECPConnection *conn) { pthread_mutex_unlock(&conn->mutex); if (ecp_conn_is_outb(conn)) pthread_mutex_unlock(&sock->conn_table.mutex); #endif - if (idx == ECP_ECDH_IDX_INV) return ECP_ERR_ECDH_IDX; - if (rv) return rv; - - _rv = ecp_send_keyx_req(conn); - if (_rv < 0) return _rv; - return ECP_OK; + if (idx == ECP_ECDH_IDX_INV) return ECP_ERR_ECDH_IDX; + return rv; } int ecp_conn_dhkey_get(ECPConnection *conn, unsigned char idx, ECPDHKey *key) { @@ -1264,7 +1396,6 @@ int ecp_conn_dhkey_get_pub(ECPConnection *conn, unsigned char *idx, ecp_ecdh_pub #ifdef ECP_WITH_PTHREAD if (will_send) pthread_mutex_lock(&conn->mutex); #endif - } else { ECPDHKey *key; @@ -1272,7 +1403,7 @@ int ecp_conn_dhkey_get_pub(ECPConnection *conn, unsigned char *idx, ecp_ecdh_pub pthread_mutex_lock(&conn->mutex); #endif - if (will_send) { + if (will_send && (conn->key_next != ECP_ECDH_IDX_INV)) { _idx = conn->key_next; } else { _idx = conn->key_curr; @@ -1286,7 +1417,7 @@ int ecp_conn_dhkey_get_pub(ECPConnection *conn, unsigned char *idx, ecp_ecdh_pub if (!rv) memcpy(public, &key->public, sizeof(key->public)); } - if (!rv && will_send) conn_dhkey_get_pub(conn, _idx); + if (!rv && will_send) conn_dhkey_send_pub(conn, _idx); #ifdef ECP_WITH_PTHREAD if (will_send || ecp_conn_is_outb(conn)) pthread_mutex_unlock(&conn->mutex); @@ -1314,7 +1445,7 @@ int ecp_conn_dhkey_set_pub(ECPConnection *conn, unsigned char idx, ecp_ecdh_publ if (ecp_conn_is_inb(conn)) pthread_mutex_unlock(&sock->conn_table.mutex); #endif - if (rv == ECP_ERR_ECDH_KEY_DUP) rv = ECP_OK; + if (rv == ECP_ERR_DUP) rv = ECP_OK; return rv; } @@ -1334,7 +1465,7 @@ void ecp_conn_dhkey_set_curr(ECPConnection *conn) { #endif } -void ecp_conn_handler_init(ECPConnHandler *handler, ecp_msg_handler_t handle_msg, ecp_open_handler_t handle_open, ecp_close_handler_t handle_close, ecp_open_send_t send_open) { +void ecp_conn_handler_init(ECPConnHandler *handler, ecp_msg_handler_t handle_msg, ecp_open_handler_t handle_open, ecp_close_handler_t handle_close, ecp_send_open_t send_open) { memset(handler, 0, sizeof(ECPConnHandler)); handler->handle_msg = handle_msg; handler->handle_open = handle_open; @@ -1346,87 +1477,56 @@ ecp_msg_handler_t ecp_get_msg_handler(ECPConnection *conn) { ECPContext *ctx = conn->sock->ctx; unsigned char ctype; - ctype = conn->type; + ctype = conn->type & ECP_CTYPE_MASK; if (ecp_conn_is_sys(conn)) { - switch (ctype) { -#ifdef ECP_WITH_DIR - case ECP_CTYPE_DIR: - return ecp_dir_handle_msg; -#endif - -#ifdef ECP_WITH_VCONN - case ECP_CTYPE_VCONN: - case ECP_CTYPE_VLINK: - return ecp_vconn_handle_msg; -#endif - - default: - return NULL; - } + if (ctype >= ECP_MAX_CTYPE_SYS) return NULL; + return ctx->handler_sys[ctype] ? ctx->handler_sys[ctype]->handle_msg : NULL; + } else { + if (ctype >= ECP_MAX_CTYPE) return NULL; + return ctx->handler[ctype] ? ctx->handler[ctype]->handle_msg : NULL; } - - if (ctype >= ECP_MAX_CTYPE) return NULL; - return ctx->handler[ctype] ? ctx->handler[ctype]->handle_msg : NULL; } ecp_open_handler_t ecp_get_open_handler(ECPConnection *conn) { ECPContext *ctx = conn->sock->ctx; unsigned char ctype; - ctype = conn->type; + ctype = conn->type & ECP_CTYPE_MASK; if (ecp_conn_is_sys(conn)) { - switch (ctype) { -#ifdef ECP_WITH_DIR - case ECP_CTYPE_DIR: - return ecp_dir_handle_open; -#endif - -#ifdef ECP_WITH_VCONN - case ECP_CTYPE_VCONN: - case ECP_CTYPE_VLINK: - return ecp_vconn_handle_open; -#endif - - default: - return NULL; - } + if (ctype >= ECP_MAX_CTYPE_SYS) return NULL; + return ctx->handler_sys[ctype] ? ctx->handler_sys[ctype]->handle_open : NULL; + } else { + if (ctype >= ECP_MAX_CTYPE) return NULL; + return ctx->handler[ctype] ? ctx->handler[ctype]->handle_open : NULL; } - - if (ctype >= ECP_MAX_CTYPE) return NULL; - return ctx->handler[ctype] ? ctx->handler[ctype]->handle_open : NULL; } ecp_close_handler_t ecp_get_close_handler(ECPConnection *conn) { ECPContext *ctx = conn->sock->ctx; unsigned char ctype; - ctype = conn->type; + ctype = conn->type & ECP_CTYPE_MASK; if (ecp_conn_is_sys(conn)) { - switch (ctype) { -#ifdef ECP_WITH_DIR - case ECP_CTYPE_DIR: - return NULL; -#endif - -#ifdef ECP_WITH_VCONN - case ECP_CTYPE_VCONN: - case ECP_CTYPE_VLINK: - return ecp_vconn_handle_close; -#endif - - default: - return NULL; - } + if (ctype >= ECP_MAX_CTYPE_SYS) return NULL; + return ctx->handler_sys[ctype] ? ctx->handler_sys[ctype]->handle_close : NULL; + } else { + if (ctype >= ECP_MAX_CTYPE) return NULL; + return ctx->handler[ctype] ? ctx->handler[ctype]->handle_close : NULL; } - - if (ctype >= ECP_MAX_CTYPE) return NULL; - return ctx->handler[ctype] ? ctx->handler[ctype]->handle_close : NULL; } -ecp_dir_handler_t ecp_get_dir_handler(ECPConnection *conn) { +ecp_send_open_t ecp_get_send_open_f(ECPConnection *conn) { ECPContext *ctx = conn->sock->ctx; + unsigned char ctype; - return ctx->handle_dir; + ctype = conn->type & ECP_CTYPE_MASK; + if (ecp_conn_is_sys(conn)) { + if (ctype >= ECP_MAX_CTYPE_SYS) return NULL; + return ctx->handler_sys[ctype] ? ctx->handler_sys[ctype]->send_open : NULL; + } else { + if (ctype >= ECP_MAX_CTYPE) return NULL; + return ctx->handler[ctype] ? ctx->handler[ctype]->send_open : NULL; + } } void ecp_err_handle(ECPConnection *conn, unsigned char mtype, int err) { @@ -1444,6 +1544,7 @@ static ssize_t _send_ireq(ECPConnection *conn, ECPTimerItem *ti) { ECPBuffer payload; unsigned char pkt_buf[ECP_SIZE_PKT_BUF(0, ECP_MTYPE_INIT_REQ, conn)]; unsigned char pld_buf[ECP_SIZE_PLD_BUF(0, ECP_MTYPE_INIT_REQ, conn)]; + ssize_t rv; packet.buffer = pkt_buf; packet.size = sizeof(pkt_buf); @@ -1452,29 +1553,50 @@ static ssize_t _send_ireq(ECPConnection *conn, ECPTimerItem *ti) { ecp_pld_set_type(payload.buffer, payload.size, ECP_MTYPE_INIT_REQ); - return _ecp_pld_send(conn, &packet, ECP_ECDH_IDX_PERMA, ECP_ECDH_IDX_INV, NULL, NULL, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_INIT_REQ), 0, ti); + rv = _ecp_pld_send(conn, &packet, ECP_ECDH_IDX_PERMA, ECP_ECDH_IDX_INV, NULL, NULL, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_INIT_REQ), 0, ti); + return rv; } static ssize_t _retry_ireq(ECPConnection *conn, ECPTimerItem *ti) { + unsigned short refcount = 0; int rv; + /* timer holds one reference to this connection */ + ecp_conn_remove(conn, &refcount); + if (refcount > 1) return ECP_ERR_BUSY; + rv = ecp_conn_reset(conn); if (rv) return rv; + rv = ecp_conn_reset_outb(conn); + if (rv) return rv; + + rv = ecp_conn_insert(conn); + if (rv) return rv; + return _send_ireq(conn, ti); } -ssize_t ecp_send_init_req(ECPConnection *conn) { - ECPTimerItem ti; +ssize_t ecp_send_init_req(ECPConnection *conn, int retry) { + ssize_t rv; + + if (retry) { + ECPTimerItem ti; + + ecp_timer_item_init(&ti, conn, ECP_MTYPE_OPEN_REP, _retry_ireq, ECP_SEND_TRIES-1, ECP_SEND_TIMEOUT); - ecp_timer_item_init(&ti, conn, ECP_MTYPE_OPEN_REP, _retry_ireq, ECP_SEND_TRIES-1, ECP_SEND_TIMEOUT); + rv = _send_ireq(conn, &ti); + } else { + rv = _send_ireq(conn, NULL); + } - return _send_ireq(conn, &ti); + return rv; } -ssize_t ecp_handle_init_req(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, unsigned char c_idx, unsigned char *public_buf, ecp_aead_key_t *shkey) { +ssize_t ecp_handle_init_req(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, unsigned char c_idx, unsigned char *public_buf, ecp_aead_key_t *shkey, ECP2Buffer *bufs) { ssize_t rv; + ecp_tr_release(bufs->packet, 1); rv = ecp_send_init_rep(sock, parent, addr, c_idx, public_buf, shkey); if (rv < 0) return rv; @@ -1521,9 +1643,8 @@ ssize_t ecp_send_init_rep(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t return rv; } -ssize_t ecp_handle_init_rep(ECPConnection *conn, unsigned char *msg, size_t msg_size) { - ecp_open_send_t send_open_f; - unsigned char ctype; +ssize_t ecp_handle_init_rep(ECPConnection *conn, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) { + ecp_send_open_t send_open_f; unsigned char *cookie; ssize_t rv; int _rv; @@ -1533,29 +1654,11 @@ ssize_t ecp_handle_init_rep(ECPConnection *conn, unsigned char *msg, size_t msg_ _rv = ecp_conn_dhkey_set_pub(conn, msg[0], (ecp_ecdh_public_t *)(msg+1)); if (_rv) return _rv; - ctype = conn->type; - send_open_f = NULL; cookie = msg+1+ECP_SIZE_ECDH_PUB; - if (ecp_conn_is_sys(conn)) { - switch (ctype) { -#ifdef ECP_WITH_VCONN - case ECP_CTYPE_VCONN: - case ECP_CTYPE_VLINK: - send_open_f = ecp_vconn_send_open_req; - break; -#endif - - default: - break; - } - } else { - ECPContext *ctx = conn->sock->ctx; - - if (ctype >= ECP_MAX_CTYPE) return ECP_ERR_CTYPE; - send_open_f = ctx->handler[ctype] ? ctx->handler[ctype]->send_open : NULL; - } + send_open_f = ecp_get_send_open_f(conn); if (send_open_f == NULL) send_open_f = ecp_send_open_req; + ecp_tr_release(bufs->packet, 1); rv = send_open_f(conn, cookie); if (rv < 0) return rv; @@ -1674,17 +1777,27 @@ ssize_t ecp_handle_open_req(ECPSocket *sock, ECPConnection *parent, unsigned cha remote_key.valid = 1; } - _rv = ecp_conn_alloc(sock, ctype, &conn); + conn = ecp_conn_new_inb(sock, ctype); + if (conn == NULL) return ECP_ERR_ALLOC; + + _rv = ecp_conn_create_inb(conn, parent, s_idx, c_idx, (ecp_ecdh_public_t *)public_buf, remote_key.valid ? &remote_key : NULL, shkey); if (_rv) return _rv; - _rv = ecp_conn_init_inb(conn, parent, s_idx, c_idx, (ecp_ecdh_public_t *)public_buf, remote_key.valid ? &remote_key : NULL, shkey); - if (!_rv) _rv = ecp_conn_insert(conn); + _rv = ecp_conn_insert(conn); if (_rv) { ecp_conn_destroy(conn); - ecp_conn_free(conn); return _rv; } + if (ecp_conn_is_gc(conn)) { + _rv = ecp_conn_insert_gc(conn); + if (_rv) { + ecp_conn_refcount_dec(conn); + _ecp_conn_close(conn); + return _rv; + } + } + *_conn = conn; /* handle_open will be called later from msg handler */ @@ -1708,77 +1821,75 @@ ssize_t ecp_send_open_rep(ECPConnection *conn) { return rv; } -ssize_t ecp_handle_open(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) { - size_t rsize; - ecp_open_handler_t handler; - int rv = ECP_OK; +ssize_t ecp_check_open(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size) { + int is_open; + ssize_t rv; + +#ifdef ECP_WITH_PTHREAD + pthread_mutex_lock(&conn->mutex); +#endif + + is_open = _ecp_conn_is_open(conn); + +#ifdef ECP_WITH_PTHREAD + pthread_mutex_unlock(&conn->mutex); +#endif + + if (is_open) return ECP_ERR; if (mtype == ECP_MTYPE_OPEN_REQ) { if (ecp_conn_is_outb(conn)) return ECP_ERR; - rsize = 2; - if (msg_size < rsize) return ECP_ERR_SIZE; + rv = 2; + if (msg_size < rv) return ECP_ERR_SIZE; - if (msg[1]) rsize += ECP_SIZE_VBOX; - if (msg_size < rsize) return ECP_ERR_SIZE; + if (msg[1]) rv += ECP_SIZE_VBOX; + if (msg_size < rv) return ECP_ERR_SIZE; } else { if (ecp_conn_is_inb(conn)) return ECP_ERR; - rsize = 0; + rv = 0; } -#ifdef ECP_WITH_PTHREAD - pthread_mutex_lock(&conn->mutex); -#endif - - if (ecp_conn_is_open(conn)) rv = ECP_ERR; - if (!rv) rv = ecp_ext_conn_open(conn); - if (!rv) ecp_conn_set_open(conn); + return rv; +} -#ifdef ECP_WITH_PTHREAD - pthread_mutex_unlock(&conn->mutex); -#endif +int ecp_handle_open(ECPConnection *conn, ECP2Buffer *bufs) { + ecp_open_handler_t handler; + int rv = ECP_OK; + rv = ecp_ext_conn_open(conn); if (rv) return rv; handler = ecp_get_open_handler(conn); - if (handler) rv = handler(conn, bufs); - if (rv) { + if (handler) { + rv = handler(conn, bufs); + if (rv) { + ecp_ext_conn_close(conn); + return rv; + } + } + #ifdef ECP_WITH_PTHREAD - pthread_mutex_lock(&conn->mutex); + pthread_mutex_lock(&conn->mutex); #endif - ecp_conn_clr_open(conn); + _ecp_conn_set_open(conn); #ifdef ECP_WITH_PTHREAD - pthread_mutex_unlock(&conn->mutex); + pthread_mutex_unlock(&conn->mutex); #endif - if (ecp_conn_is_inb(conn)) _ecp_conn_close(conn); - return rv; - } - if (ecp_conn_is_inb(conn)) { - ssize_t _rv; - ecp_tr_release(bufs->packet, 1); - - _rv = ecp_send_open_rep(conn); - if (_rv < 0) { - _ecp_conn_close(conn); - return _rv; - } - rv = ecp_conn_insert_inb(conn); - if (rv) { - _ecp_conn_close(conn); - return rv; - } + ecp_send_open_rep(conn); } else if (ecp_conn_is_root(conn)) { ecp_conn_remove_addr(conn); } - return rsize; + + return ECP_OK; } -static ssize_t _send_keyx_req(ECPConnection *conn, ECPTimerItem *ti) { +static ssize_t _send_kxreq(ECPConnection *conn, ECPTimerItem *ti) { ECPBuffer packet; ECPBuffer payload; unsigned char pkt_buf[ECP_SIZE_PKT_BUF(1+ECP_SIZE_ECDH_PUB, ECP_MTYPE_KEYX_REQ, conn)]; @@ -1802,8 +1913,16 @@ static ssize_t _send_keyx_req(ECPConnection *conn, ECPTimerItem *ti) { return rv; } -ssize_t ecp_send_keyx_req(ECPConnection *conn) { - return ecp_timer_send(conn, _send_keyx_req, ECP_MTYPE_KEYX_REP, ECP_SEND_TRIES, ECP_SEND_TIMEOUT); +ssize_t ecp_send_keyx_req(ECPConnection *conn, int retry) { + ssize_t rv; + + if (retry) { + rv = ecp_timer_send(conn, _send_kxreq, ECP_MTYPE_KEYX_REP, ECP_SEND_TRIES, ECP_SEND_TIMEOUT); + } else { + rv = _send_kxreq(conn, NULL); + } + + return rv; } ssize_t ecp_send_keyx_rep(ECPConnection *conn) { @@ -1847,7 +1966,6 @@ ssize_t ecp_handle_keyx(ECPConnection *conn, unsigned char mtype, unsigned char if (ecp_conn_is_outb(conn)) return ECP_ERR; ecp_tr_release(bufs->packet, 1); - rv = ecp_send_keyx_rep(conn); if (rv < 0) return rv; } @@ -1860,7 +1978,7 @@ ssize_t ecp_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, switch (mtype) { case ECP_MTYPE_OPEN_REQ: case ECP_MTYPE_OPEN_REP: - return ecp_handle_open(conn, mtype, msg, msg_size, bufs); + return ecp_check_open(conn, mtype, msg, msg_size); case ECP_MTYPE_KEYX_REQ: case ECP_MTYPE_KEYX_REP: @@ -1891,17 +2009,20 @@ ssize_t ecp_pld_handle_one(ECPConnection *conn, ecp_seq_t seq, unsigned char *pa _rv = ecp_pld_get_type(payload, pld_size, &mtype); if (_rv) return _rv; - ecp_timer_pop(conn, mtype); - msg = ecp_pld_get_msg(payload, pld_size); if (msg == NULL) return ECP_ERR; hdr_size = msg - payload; msg_size = pld_size - hdr_size; rv = ecp_msg_handle(conn, seq, mtype, msg, msg_size, bufs); - if (rv < 0) return rv; + ecp_timer_pop(conn, mtype); + if (rv < 0) { + ecp_err_handle(conn, mtype, rv); + return rv; + } rv += hdr_size; + if (rv > pld_size) return ECP_ERR_SIZE; return rv; } @@ -1910,7 +2031,7 @@ ssize_t ecp_pld_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *payloa size_t pld_size = _pld_size; ssize_t rv; - rv = ecp_ext_pld_handle(conn, seq, payload, pld_size, bufs); + rv = ecp_ext_pld_handle_one(conn, seq, payload, pld_size, bufs); if (rv < 0) return rv; payload += rv; @@ -1928,7 +2049,7 @@ ssize_t ecp_pld_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *payloa return _pld_size; } -ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ECP2Buffer *bufs, size_t _pkt_size, ECPConnection **_conn, unsigned char **_payload, ecp_seq_t *_seq) { +ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ECP2Buffer *bufs, size_t _pkt_size, ECPConnection **_conn, unsigned char **_payload, ecp_seq_t *_seq, int *is_open_msg) { ECPConnection *conn = NULL; unsigned char idx; unsigned char s_idx; @@ -1952,6 +2073,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, *_conn = NULL; *_payload = NULL; *_seq = 0; + *is_open_msg = 0; packet = bufs->packet->buffer; idx = packet[ECP_SIZE_PROTO]; @@ -1974,7 +2096,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, if (conn) { is_inb = ecp_conn_is_inb(conn); - is_open = ecp_conn_is_open(conn); + is_open = _ecp_conn_is_open(conn); if (!is_open && !is_inb && (idx == ECP_ECDH_IDX_INV)) { nonce_buf = packet+ECP_SIZE_PROTO+1; @@ -2073,6 +2195,44 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, payload = bufs->payload->buffer; rv = ecp_aead_dec(payload, bufs->payload->size, packet, pkt_size, &shkey, &nonce_pkt, is_inb ? ECP_NTYPE_INB : ECP_NTYPE_OUTB); + + /* init reply collision */ + if ((rv == ECP_ERR_DECRYPT) && conn && !is_open && !is_inb && (idx == ECP_ECDH_IDX_INV)) { + ECPConnection *_conn = conn; + +#ifdef ECP_WITH_PTHREAD + pthread_mutex_lock(&sock->conn_table.mutex); +#endif + + conn = conn_table_search_addr(sock, addr); + + while (conn && (rv == ECP_ERR_DECRYPT)) { + if (conn != _conn) { +#ifdef ECP_WITH_PTHREAD + pthread_mutex_lock(&conn->mutex); +#endif + + _rv = conn_shkey_get(conn, s_idx, c_idx, &shkey); + +#ifdef ECP_WITH_PTHREAD + pthread_mutex_unlock(&conn->mutex); +#endif + + if (_rv) rv = _rv; + if (rv == ECP_ERR_DECRYPT) rv = ecp_aead_dec(payload, bufs->payload->size, packet, pkt_size, &shkey, &nonce_pkt, ECP_NTYPE_OUTB); + } + if (rv == ECP_ERR_DECRYPT) conn = conn_table_search_addr_next(sock, addr, conn); + } + + if (rv < 0) conn = NULL; + if (conn) ecp_conn_refcount_inc(conn); + +#ifdef ECP_WITH_PTHREAD + pthread_mutex_unlock(&sock->conn_table.mutex); +#endif + + ecp_conn_refcount_dec(_conn); + } if (rv < 0) goto unpack_err; pld_size = rv; @@ -2098,11 +2258,8 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, case ECP_MTYPE_INIT_REQ: { unsigned char _public_buf[ECP_SIZE_ECDH_PUB]; - /* we should release incoming packet before sending reply packet */ memcpy(_public_buf, public_buf, sizeof(_public_buf)); - ecp_tr_release(bufs->packet, 1); - - rv = ecp_handle_init_req(sock, parent, addr, c_idx, _public_buf, &shkey); + rv = ecp_handle_init_req(sock, parent, addr, c_idx, _public_buf, &shkey, bufs); if (rv < 0) return rv; rv += hdr_size; @@ -2116,7 +2273,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, /* pass to payload handler */ nonce_in = nonce_pkt; nonce_map = ECP_ACK_FULL; - is_open = 1; + *is_open_msg = 1; rv = 0; break; } @@ -2148,10 +2305,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, switch (mtype) { case ECP_MTYPE_INIT_REP: { - /* we should release incoming packet before sending reply packet */ - ecp_tr_release(bufs->packet, 1); - - rv = ecp_handle_init_rep(conn, msg, msg_size); + rv = ecp_handle_init_rep(conn, msg, msg_size, bufs); if (rv < 0) goto unpack_err; rv += hdr_size; @@ -2162,7 +2316,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, /* pass to payload handler */ nonce_in = nonce_pkt; nonce_map = ECP_ACK_FULL; - is_open = 1; + *is_open_msg = 1; rv = 0; break; } @@ -2177,16 +2331,16 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, } if (conn) { - if (is_open) { + if (is_open || *is_open_msg) { #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn->mutex); #endif conn->nonce_in = nonce_in; conn->nonce_map = nonce_map; - if (is_inb) { - conn->access_ts = ecp_tm_abstime_ms(0); - if (addr) conn->remote.addr = *addr; + conn->access_ts = ecp_tm_get_s(); + if (is_inb && addr) { + conn->remote.addr = *addr; } #ifdef ECP_WITH_PTHREAD @@ -2209,13 +2363,14 @@ unpack_err: } ssize_t ecp_pkt_handle(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ECP2Buffer *bufs, size_t pkt_size) { - ECPConnection *conn = NULL; + ECPConnection *conn; unsigned char *payload; + int is_open_msg; ecp_seq_t seq; size_t pld_size; ssize_t rv; - rv = ecp_unpack(sock, parent, addr, bufs, pkt_size, &conn, &payload, &seq); + rv = ecp_unpack(sock, parent, addr, bufs, pkt_size, &conn, &payload, &seq, &is_open_msg); if (rv < 0) return rv; pld_size = pkt_size - rv; @@ -2225,7 +2380,7 @@ ssize_t ecp_pkt_handle(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *ad } if (pld_size) { - rv = ecp_ext_pld_store(conn, seq, payload, pld_size, bufs); + rv = ecp_ext_pld_handle(conn, seq, payload, pld_size, bufs); if (rv < 0) goto pkt_handle_fin; payload += rv; @@ -2240,6 +2395,16 @@ ssize_t ecp_pkt_handle(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *ad pld_size -= rv; } + if (is_open_msg) { + int _rv; + + _rv = ecp_handle_open(conn, bufs); + if (_rv) { + rv = _rv; + goto pkt_handle_fin; + } + } + rv = pkt_size - pld_size; pkt_handle_fin: @@ -2473,8 +2638,8 @@ int ecp_pld_get_frag(unsigned char *pld, size_t pld_size, unsigned char *frag_cn *frag_cnt = pld[1]; *frag_tot = pld[2]; *frag_size = \ - (pld[3] << 8) | \ - (pld[4]); + ((uint16_t)pld[3] << 8) | \ + ((uint16_t)pld[4]); return ECP_OK; } diff --git a/ecp/src/ecp/core.h b/ecp/src/ecp/core.h index 4b47bcb..9be0dc0 100644 --- a/ecp/src/ecp/core.h +++ b/ecp/src/ecp/core.h @@ -15,18 +15,18 @@ #define ECP_ERR_ALLOC -3 #define ECP_ERR_SIZE -4 #define ECP_ERR_BUSY -5 -#define ECP_ERR_EMPTY -6 -#define ECP_ERR_FULL -7 -#define ECP_ERR_MTYPE -8 -#define ECP_ERR_CTYPE -9 -#define ECP_ERR_HANDLER -10 -#define ECP_ERR_COOKIE -11 - -#define ECP_ERR_NET_ADDR -12 -#define ECP_ERR_MAX_PARENT -13 -#define ECP_ERR_NEXT -14 - -#define ECP_ERR_ECDH_KEY_DUP -21 +#define ECP_ERR_DUP -6 +#define ECP_ERR_EMPTY -7 +#define ECP_ERR_FULL -8 +#define ECP_ERR_MTYPE -9 +#define ECP_ERR_CTYPE -10 +#define ECP_ERR_HANDLER -11 +#define ECP_ERR_COOKIE -12 + +#define ECP_ERR_NET_ADDR -13 +#define ECP_ERR_MAX_PARENT -14 +#define ECP_ERR_NEXT -15 + #define ECP_ERR_ECDH_IDX -22 #define ECP_ERR_ENCRYPT -26 #define ECP_ERR_DECRYPT -27 @@ -43,6 +43,7 @@ #define ECP_MAX_CONN_KEY 2 #define ECP_MAX_NODE_KEY 2 #define ECP_MAX_CTYPE 8 +#define ECP_MAX_CTYPE_SYS 3 #define ECP_MAX_MTYPE 16 #define ECP_MAX_PARENT 3 #define ECP_MAX_SEQ_FWD 1024 @@ -121,31 +122,37 @@ #define ECP_CONN_FLAG_INB 0x80 #define ECP_CONN_FLAG_VBOX 0x01 #define ECP_CONN_FLAG_RBUF 0x02 +#define ECP_CONN_FLAG_GC 0x04 #define ECP_CONN_FLAG_MASK 0x7F /* mutable flags */ -#define ECP_CONN_FLAG_REG 0x04 -#define ECP_CONN_FLAG_OPEN 0x08 +#define ECP_CONN_FLAG_REG 0x01 +#define ECP_CONN_FLAG_OPEN 0x02 +#define ECP_CONN_FLAG_CLOSED 0x04 #define ECP_SEND_FLAG_REPLY 0x01 #define ECP_SEND_FLAG_MORE 0x02 #define ecp_conn_has_vbox(conn) ((conn)->flags_im & ECP_CONN_FLAG_VBOX) #define ecp_conn_has_rbuf(conn) ((conn)->flags_im & ECP_CONN_FLAG_RBUF) +#define ecp_conn_is_gc(conn) ((conn)->flags_im & ECP_CONN_FLAG_GC) #define ecp_conn_is_inb(conn) ((conn)->flags_im & ECP_CONN_FLAG_INB) #define ecp_conn_is_outb(conn) (!((conn)->flags_im & ECP_CONN_FLAG_INB)) -#define ecp_conn_is_reg(conn) ((conn)->flags & ECP_CONN_FLAG_REG) -#define ecp_conn_is_open(conn) ((conn)->flags & ECP_CONN_FLAG_OPEN) #define ecp_conn_is_sys(conn) ((conn)->type & ECP_CTYPE_FLAG_SYS) +#define _ecp_conn_is_reg(conn) ((conn)->flags & ECP_CONN_FLAG_REG) +#define _ecp_conn_is_open(conn) ((conn)->flags & ECP_CONN_FLAG_OPEN) +#define _ecp_conn_is_closed(conn) ((conn)->flags & ECP_CONN_FLAG_CLOSED) #define ecp_conn_set_inb(conn) ((conn)->flags_im |= ECP_CONN_FLAG_INB) #define ecp_conn_set_outb(conn) ((conn)->flags_im &= ~ECP_CONN_FLAG_INB) -#define ecp_conn_set_reg(conn) ((conn)->flags |= ECP_CONN_FLAG_REG) -#define ecp_conn_set_open(conn) ((conn)->flags |= ECP_CONN_FLAG_OPEN) +#define _ecp_conn_set_reg(conn) ((conn)->flags |= ECP_CONN_FLAG_REG) +#define _ecp_conn_set_open(conn) ((conn)->flags |= ECP_CONN_FLAG_OPEN) +#define _ecp_conn_set_closed(conn) ((conn)->flags |= ECP_CONN_FLAG_CLOSED) -#define ecp_conn_clr_reg(conn) ((conn)->flags &= ~ECP_CONN_FLAG_REG) -#define ecp_conn_clr_open(conn) ((conn)->flags &= ~ECP_CONN_FLAG_OPEN) +#define _ecp_conn_clr_reg(conn) ((conn)->flags &= ~ECP_CONN_FLAG_REG) +#define _ecp_conn_clr_open(conn) ((conn)->flags &= ~ECP_CONN_FLAG_OPEN) +#define _ecp_conn_clr_closed(conn) ((conn)->flags &= ~ECP_CONN_FLAG_CLOSED) typedef uint32_t ecp_ack_t; #define ECP_SIZE_ACKB (sizeof(ecp_ack_t)*8) @@ -174,9 +181,8 @@ typedef uint64_t ecp_nonce_t; struct ECP2Buffer; struct ECPSocket; struct ECPConnection; - -#ifdef ECP_WITH_DIRSRV -struct ECPDirList; +#ifdef ECP_WITH_FRAG +struct ECPFragIter; #endif #ifdef ECP_WITH_HTABLE @@ -186,15 +192,16 @@ struct ECPDirList; #include "transport.h" #include "timer.h" +typedef int (*ecp_conn_expired_t) (struct ECPConnection *conn, ecp_sts_t now, ecp_sts_t to); + typedef void (*ecp_err_handler_t) (struct ECPConnection *conn, unsigned char mtype, int err); -typedef ssize_t (*ecp_dir_handler_t) (struct ECPConnection *conn, unsigned char *msg, size_t msg_size, struct ECP2Buffer *b); -typedef struct ECPConnection * (*ecp_conn_alloc_t) (struct ECPSocket *sock, unsigned char type); +typedef struct ECPConnection * (*ecp_conn_new_t) (struct ECPSocket *sock, unsigned char type); typedef void (*ecp_conn_free_t) (struct ECPConnection *conn); typedef ssize_t (*ecp_msg_handler_t) (struct ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, struct ECP2Buffer *b); typedef int (*ecp_open_handler_t) (struct ECPConnection *conn, struct ECP2Buffer *b); typedef void (*ecp_close_handler_t) (struct ECPConnection *conn); -typedef ssize_t (*ecp_open_send_t) (struct ECPConnection *conn, unsigned char *cookie); +typedef ssize_t (*ecp_send_open_t) (struct ECPConnection *conn, unsigned char *cookie); typedef struct ECPBuffer { unsigned char *buffer; @@ -241,24 +248,21 @@ typedef struct ECPConnHandler { ecp_msg_handler_t handle_msg; ecp_open_handler_t handle_open; ecp_close_handler_t handle_close; - ecp_open_send_t send_open; + ecp_send_open_t send_open; } ECPConnHandler; typedef struct ECPContext { ecp_err_handler_t handle_err; - ecp_dir_handler_t handle_dir; - ecp_conn_alloc_t conn_alloc; + ecp_conn_new_t conn_new; /* inbound connections only */ ecp_conn_free_t conn_free; ECPConnHandler *handler[ECP_MAX_CTYPE]; -#ifdef ECP_WITH_DIRSRV - struct ECPDirSrv *dir_srv; -#endif + ECPConnHandler *handler_sys[ECP_MAX_CTYPE_SYS]; } ECPContext; typedef struct ECPConnTable { #ifdef ECP_WITH_HTABLE ecp_ht_table_t *keys; - ecp_ht_table_t *keys_inb; + ecp_ht_table_t *keys_gc; ecp_ht_table_t *addrs; #else struct ECPConnection *arr[ECP_MAX_SOCK_CONN]; @@ -266,7 +270,7 @@ typedef struct ECPConnTable { #endif #ifdef ECP_WITH_PTHREAD pthread_mutex_t mutex; - pthread_mutex_t mutex_inb; + pthread_mutex_t mutex_gc; #endif } ECPConnTable; @@ -304,70 +308,79 @@ typedef struct ECPConnection { unsigned char rkey_curr; ECPDHShkey shkey[ECP_MAX_NODE_KEY][ECP_MAX_NODE_KEY]; ecp_sts_t access_ts; -#ifdef ECP_WITH_PTHREAD - pthread_mutex_t mutex; -#endif + void *param; #ifdef ECP_WITH_VCONN struct ECPConnection *parent; - struct ECPConnection *next; unsigned short pcount; #endif +#ifdef ECP_WITH_FRAG + struct ECPFragIter *iter; +#endif +#ifdef ECP_WITH_PTHREAD + pthread_mutex_t mutex; +#endif } ECPConnection; int ecp_dhkey_gen(ECPDHKey *key); -int ecp_init(ECPContext *ctx); -int ecp_ctx_init(ECPContext *ctx, ecp_err_handler_t handle_err, ecp_dir_handler_t handle_dir, ecp_conn_alloc_t conn_alloc, ecp_conn_free_t conn_free); -int ecp_ctx_set_handler(ECPContext *ctx, ECPConnHandler *handler, unsigned char ctype); +int ecp_ctx_init(ECPContext *ctx, ecp_err_handler_t handle_err, ecp_conn_new_t conn_new, ecp_conn_free_t conn_free); +int ecp_ctx_set_handler(ECPContext *ctx, unsigned char ctype, ECPConnHandler *handler); +ECPConnHandler *ecp_ctx_get_handler(ECPContext *ctx, unsigned char ctype); + +int ecp_addr_init(ecp_tr_addr_t *addr, void *addr_s); -int ecp_node_init(ECPNode *node, ecp_ecdh_public_t *public, void *addr); +void ecp_node_init(ECPNode *node, ecp_ecdh_public_t *public, ecp_tr_addr_t *addr); void ecp_node_set_pub(ECPNode *node, ecp_ecdh_public_t *public); int ecp_node_set_addr(ECPNode *node, void *addr); int ecp_sock_init(ECPSocket *sock, ECPContext *ctx, ECPDHKey *key); int ecp_sock_create(ECPSocket *sock, ECPContext *ctx, ECPDHKey *key); void ecp_sock_destroy(ECPSocket *sock); -int ecp_sock_open(ECPSocket *sock, void *myaddr); +int ecp_sock_open(ECPSocket *sock, ecp_tr_addr_t *myaddr); void ecp_sock_close(ECPSocket *sock); int ecp_sock_minkey_new(ECPSocket *sock); int ecp_sock_dhkey_new(ECPSocket *sock); int ecp_sock_dhkey_get(ECPSocket *sock, unsigned char idx, ECPDHKey *key); int ecp_sock_dhkey_get_pub(ECPSocket *sock, unsigned char *idx, ecp_ecdh_public_t *public); void ecp_sock_get_nonce(ECPSocket *sock, ecp_nonce_t *nonce); + +void ecp_sock_expire(ECPSocket *sock, ecp_sts_t to, ecp_conn_expired_t conn_expired); int ecp_sock_expire_inb(ECPSocket *sock, ecp_sts_t to); int ecp_cookie_gen(ECPSocket *sock, unsigned char *cookie, unsigned char *public_buf); int ecp_cookie_verify(ECPSocket *sock, unsigned char *cookie, unsigned char *public_buf); -int ecp_conn_alloc(ECPSocket *sock, unsigned char ctype, ECPConnection **_conn); -void ecp_conn_free(ECPConnection *conn); - +ECPConnection *ecp_conn_new_inb(ECPSocket *sock, unsigned char ctype); void ecp_conn_init(ECPConnection *conn, ECPSocket *sock, unsigned char ctype); -void ecp_conn_reinit(ECPConnection *conn); -int ecp_conn_create(ECPConnection *conn, ECPSocket *sock, unsigned char ctype); -int ecp_conn_create_inb(ECPConnection *conn, ECPSocket *sock, unsigned char ctype); +int ecp_conn_reset(ECPConnection *conn); +int ecp_conn_create(ECPConnection *conn, ECPConnection *parent); +int ecp_conn_create_inb(ECPConnection *conn, ECPConnection *parent, unsigned char s_idx, unsigned char c_idx, ecp_ecdh_public_t *public, ECPDHPub *remote_key, ecp_aead_key_t *shkey); +int ecp_conn_create_outb(ECPConnection *conn, ECPConnection *parent, ECPNode *node); +int ecp_conn_reset_outb(ECPConnection *conn); void ecp_conn_destroy(ECPConnection *conn); - -void ecp_conn_set_flags(ECPConnection *conn, unsigned char flags); -void ecp_conn_clr_flags(ECPConnection *conn, unsigned char flags); -void ecp_conn_set_remote_key(ECPConnection *conn, ECPDHPub *key); -void ecp_conn_set_remote_addr(ECPConnection *conn, ecp_tr_addr_t *addr); - -int ecp_conn_init_inb(ECPConnection *conn, ECPConnection *parent, unsigned char s_idx, unsigned char c_idx, ecp_ecdh_public_t *public, ECPDHPub *remote_key, ecp_aead_key_t *shkey); -int ecp_conn_init_outb(ECPConnection *conn, ECPNode *node); +void ecp_conn_free(ECPConnection *conn); int ecp_conn_insert(ECPConnection *conn); -int ecp_conn_insert_inb(ECPConnection *conn); +int ecp_conn_insert_gc(ECPConnection *conn); void ecp_conn_remove(ECPConnection *conn, unsigned short *refcount); void ecp_conn_remove_addr(ECPConnection *conn); +int _ecp_conn_open(ECPConnection *conn, ECPConnection *parent, ECPNode *node, int retry); int ecp_conn_open(ECPConnection *conn, ECPNode *node); -int ecp_conn_reset(ECPConnection *conn); +int ecp_conn_try_open(ECPConnection *conn, ECPNode *node); int _ecp_conn_close(ECPConnection *conn); int ecp_conn_close(ECPConnection *conn); +void ecp_conn_mark_closed(ECPConnection *conn); +int _ecp_conn_expired(ECPConnection *conn, ecp_sts_t now, ecp_sts_t to); +int ecp_conn_is_zombie(ECPConnection *conn, ecp_sts_t now, ecp_sts_t to); void ecp_conn_refcount_inc(ECPConnection *conn); void ecp_conn_refcount_dec(ECPConnection *conn); +void ecp_conn_set_flags(ECPConnection *conn, unsigned char flags); +void ecp_conn_clr_flags(ECPConnection *conn, unsigned char flags); +void ecp_conn_set_remote_key(ECPConnection *conn, ECPDHPub *key); +void ecp_conn_set_remote_addr(ECPConnection *conn, ecp_tr_addr_t *addr); + int ecp_conn_dhkey_new(ECPConnection *conn); int ecp_conn_dhkey_get(ECPConnection *conn, unsigned char idx, ECPDHKey *key); int ecp_conn_dhkey_get_remote(ECPConnection *conn, unsigned char idx, ECPDHPub *key); @@ -375,24 +388,26 @@ int ecp_conn_dhkey_get_pub(ECPConnection *conn, unsigned char *idx, ecp_ecdh_pub int ecp_conn_dhkey_set_pub(ECPConnection *conn, unsigned char idx, ecp_ecdh_public_t *public); void ecp_conn_dhkey_set_curr(ECPConnection *conn); -void ecp_conn_handler_init(ECPConnHandler *handler, ecp_msg_handler_t handle_msg, ecp_open_handler_t handle_open, ecp_close_handler_t handle_close, ecp_open_send_t send_open); +void ecp_conn_handler_init(ECPConnHandler *handler, ecp_msg_handler_t handle_msg, ecp_open_handler_t handle_open, ecp_close_handler_t handle_close, ecp_send_open_t send_open); ecp_msg_handler_t ecp_get_msg_handler(ECPConnection *conn); ecp_open_handler_t ecp_get_open_handler(ECPConnection *conn); ecp_close_handler_t ecp_get_close_handler(ECPConnection *conn); -ecp_dir_handler_t ecp_get_dir_handler(ECPConnection *conn); +ecp_send_open_t ecp_get_send_open_f(ECPConnection *conn); void ecp_err_handle(ECPConnection *conn, unsigned char mtype, int err); -ssize_t ecp_send_init_req(ECPConnection *conn); -ssize_t ecp_handle_init_req(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, unsigned char c_idx, unsigned char *public_buf, ecp_aead_key_t *shkey); +ssize_t ecp_send_init_req(ECPConnection *conn, int retry); +ssize_t ecp_handle_init_req(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, unsigned char c_idx, unsigned char *public_buf, ecp_aead_key_t *shkey, ECP2Buffer *bufs); ssize_t ecp_send_init_rep(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, unsigned char c_idx, unsigned char *public_buf, ecp_aead_key_t *shkey); -ssize_t ecp_handle_init_rep(ECPConnection *conn, unsigned char *msg, size_t msg_size); +ssize_t ecp_handle_init_rep(ECPConnection *conn, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs); + ssize_t ecp_write_open_req(ECPConnection *conn, ECPBuffer *payload); ssize_t ecp_send_open_req(ECPConnection *conn, unsigned char *cookie); ssize_t ecp_handle_open_req(ECPSocket *sock, ECPConnection *parent, unsigned char s_idx, unsigned char c_idx, unsigned char *public_buf, unsigned char *msg, size_t msg_size, ecp_aead_key_t *shkey, ECPConnection **_conn); ssize_t ecp_send_open_rep(ECPConnection *conn); -ssize_t ecp_handle_open(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs); +ssize_t ecp_check_open(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size); +int ecp_handle_open(ECPConnection *conn, ECP2Buffer *bufs); -ssize_t ecp_send_keyx_req(ECPConnection *conn); +ssize_t ecp_send_keyx_req(ECPConnection *conn, int retry); ssize_t ecp_send_keyx_rep(ECPConnection *conn); ssize_t ecp_handle_keyx(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs); @@ -400,7 +415,7 @@ ssize_t ecp_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, ssize_t ecp_pld_handle_one(ECPConnection *conn, ecp_seq_t seq, unsigned char *payload, size_t pld_size, ECP2Buffer *bufs); ssize_t ecp_pld_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *payload, size_t _pld_size, ECP2Buffer *bufs); -ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ECP2Buffer *bufs, size_t _pkt_size, ECPConnection **_conn, unsigned char **_payload, ecp_seq_t *_seq); +ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ECP2Buffer *bufs, size_t _pkt_size, ECPConnection **_conn, unsigned char **_payload, ecp_seq_t *_seq, int *is_open_msg); ssize_t ecp_pkt_handle(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ECP2Buffer *bufs, size_t pkt_size); ssize_t ecp_pkt_send(ECPSocket *sock, ECPBuffer *packet, size_t pkt_size, unsigned char flags, ECPTimerItem *ti, ecp_tr_addr_t *addr); diff --git a/ecp/src/ecp/crypto/crypto.c b/ecp/src/ecp/crypto/crypto.c index 6841e9d..ba013fa 100644 --- a/ecp/src/ecp/crypto/crypto.c +++ b/ecp/src/ecp/crypto/crypto.c @@ -1,8 +1,8 @@ #include <stdlib.h> #include <string.h> -#include <core.h> -#include <cr.h> +#include <ecp/core.h> +#include <ecp/cr.h> #include <openssl/curve25519.h> diff --git a/ecp/src/ecp/dir/Makefile b/ecp/src/ecp/dir/Makefile index 9283144..a002b65 100644 --- a/ecp/src/ecp/dir/Makefile +++ b/ecp/src/ecp/dir/Makefile @@ -2,10 +2,6 @@ include ../common.mk obj = dir.o -ifeq ($(with_dirsrv),yes) -obj += dir_srv.o -endif - %.o: %.c $(CC) $(CFLAGS) -c $< diff --git a/ecp/src/ecp/dir/dir.c b/ecp/src/ecp/dir/dir.c index 72097c7..d826e24 100644 --- a/ecp/src/ecp/dir/dir.c +++ b/ecp/src/ecp/dir/dir.c @@ -1,131 +1,72 @@ #include <stdlib.h> #include <string.h> -#include <core.h> -#include <cr.h> +#include <ecp/core.h> +#include <ecp/cr.h> #include "dir.h" -#include "dir_srv.h" -static int dir_update(ECPDirList *list, ECPDirItem *item) { - int i; - - for (i=0; i<list->count; i++) { - if (memcmp(&list->item[i].node.key_perma.public, &item->node.key_perma.public, sizeof(item->node.key_perma.public)) == 0) { - return ECP_OK; - } - } - - if (list->count == ECP_MAX_DIR_ITEM) return ECP_ERR_SIZE; - - list->item[list->count] = *item; - list->count++; - - return ECP_OK; -} - -ssize_t ecp_dir_parse(ECPDirList *list, unsigned char *buf, size_t buf_size) { - ECPDirItem item; - size_t rsize; - uint16_t count; - int i; - int rv; - - if (buf_size < sizeof(uint16_t)) return ECP_ERR_SIZE; - - count = \ - (buf[0] << 8) | \ - (buf[1]); - - rsize = sizeof(uint16_t) + count * ECP_SIZE_DIR_ITEM; - if (buf_size < rsize) return ECP_ERR_SIZE; - - buf += sizeof(uint16_t); - for (i=0; i<count; i++) { - ecp_dir_item_parse(&item, buf); - - rv = dir_update(list, &item); - if (rv) return rv; - - buf += ECP_SIZE_DIR_ITEM; - } - - return rsize; -} - -ssize_t ecp_dir_serialize(ECPDirList *list, unsigned char *buf, size_t buf_size) { - size_t rsize; - int i; - - rsize = sizeof(uint16_t) + list->count * ECP_SIZE_DIR_ITEM; - if (buf_size < rsize) return ECP_ERR_SIZE; - - buf[0] = (list->count & 0xFF00) >> 8; - buf[1] = (list->count & 0x00FF); - buf += sizeof(uint16_t); - for (i=0; i<list->count; i++) { - ecp_dir_item_serialize(&list->item[i], buf); - buf += ECP_SIZE_DIR_ITEM; - } - - return rsize; -} - -void ecp_dir_item_parse(ECPDirItem *item, unsigned char *buf) { +size_t ecp_dir_item_parse(ECPDirItem *item, unsigned char *buf) { ECPDHPub *key; ecp_tr_addr_t *addr; + size_t rsize = 0; + + memset(item, 0, sizeof(ECPDirItem)); key = &item->node.key_perma; addr = &item->node.addr; + key->valid = 1; memcpy(&key->public, buf, sizeof(key->public)); buf += sizeof(key->public); + rsize += sizeof(key->public); memcpy(&addr->host, buf, sizeof(addr->host)); buf += sizeof(addr->host); + rsize += sizeof(addr->host); addr->port = \ - (buf[0] << 8) | \ - (buf[1]); + ((uint16_t)buf[0] << 8) | \ + ((uint16_t)buf[1]); buf += sizeof(uint16_t); + rsize += sizeof(uint16_t); item->capabilities = \ - (buf[0] << 8) | \ - (buf[1]); + ((uint16_t)buf[0] << 8) | \ + ((uint16_t)buf[1]); buf += sizeof(uint16_t); + rsize += sizeof(uint16_t); + + return rsize; } -void ecp_dir_item_serialize(ECPDirItem *item, unsigned char *buf) { +size_t ecp_dir_item_serialize(ECPDirItem *item, unsigned char *buf) { ECPDHPub *key; ecp_tr_addr_t *addr; + size_t rsize = 0; key = &item->node.key_perma; addr = &item->node.addr; memcpy(buf, &key->public, sizeof(key->public)); buf += sizeof(key->public); + rsize += sizeof(key->public); memcpy(buf, &addr->host, sizeof(addr->host)); buf += sizeof(addr->host); + rsize += sizeof(addr->host); - buf[0] = (addr->port & 0xFF00) >> 8; - buf[1] = (addr->port & 0x00FF); + buf[0] = addr->port >> 8; + buf[1] = addr->port; buf += sizeof(uint16_t); + rsize += sizeof(uint16_t); - buf[0] = (item->capabilities & 0xFF00) >> 8; - buf[1] = (item->capabilities & 0x00FF); + buf[0] = item->capabilities >> 8; + buf[1] = item->capabilities; buf += sizeof(uint16_t); -} - -ssize_t ecp_dir_handle(ECPConnection *conn, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { - ecp_dir_handler_t handler; + rsize += sizeof(uint16_t); - handler = ecp_get_dir_handler(conn); - if (handler) { - return handler(conn, msg, msg_size, b); - } else { - return ECP_ERR_HANDLER; - } + return rsize; } int ecp_dir_handle_open(ECPConnection *conn, ECP2Buffer *b) { @@ -133,41 +74,12 @@ int ecp_dir_handle_open(ECPConnection *conn, ECP2Buffer *b) { if (ecp_conn_is_inb(conn)) return ECP_OK; -#ifdef ECP_WITH_DIRSRV - rv = ecp_dir_send_upd(conn); -#else rv = ecp_dir_send_req(conn); -#endif if (rv < 0) return rv; return ECP_OK; } -ssize_t ecp_dir_handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { - switch (mtype) { -#ifdef ECP_WITH_DIRSRV - case ECP_MTYPE_DIR_UPD: { - return ecp_dir_handle_upd(conn, msg, msg_size); - } - - case ECP_MTYPE_DIR_REQ: { - return ecp_dir_handle_req(conn, msg, msg_size); - } -#endif - - case ECP_MTYPE_DIR_REP: { -#ifdef ECP_WITH_DIRSRV - return ecp_dir_handle_rep(conn, msg, msg_size); -#else - return ecp_dir_handle(conn, msg, msg_size, b); -#endif - } - - default: - return ECP_ERR_MTYPE; - } -} - static ssize_t _dir_send_req(ECPConnection *conn, ECPTimerItem *ti) { ECPBuffer packet; ECPBuffer payload; @@ -187,15 +99,6 @@ ssize_t ecp_dir_send_req(ECPConnection *conn) { return ecp_timer_send(conn, _dir_send_req, ECP_MTYPE_DIR_REP, ECP_SEND_TRIES, ECP_SEND_TIMEOUT); } -int ecp_dir_get(ECPConnection *conn, ECPSocket *sock, ECPNode *node) { - int rv; - ssize_t _rv; - - rv = ecp_conn_create(conn, sock, ECP_CTYPE_DIR); - if (rv) return rv; - - rv = ecp_conn_open(conn, node); - if (rv) return rv; - - return ECP_OK; +void ecp_dir_conn_init(ECPConnection *conn, ECPSocket *sock) { + ecp_conn_init(conn, sock, ECP_CTYPE_DIR); } diff --git a/ecp/src/ecp/dir/dir.h b/ecp/src/ecp/dir/dir.h index e9f8e1e..b28f801 100644 --- a/ecp/src/ecp/dir/dir.h +++ b/ecp/src/ecp/dir/dir.h @@ -1,32 +1,24 @@ -#define ECP_MAX_DIR_ITEM 30 #define ECP_SIZE_DIR_ITEM 40 #define ECP_MTYPE_DIR_UPD 0x00 #define ECP_MTYPE_DIR_REQ 0x01 -#define ECP_MTYPE_DIR_REP 0x02 +#define ECP_MTYPE_DIR_ANN 0x02 +#define ECP_MTYPE_DIR_REP 0x03 +#define ECP_MTYPE_DIR_SHADOW 0x04 #define ECP_CTYPE_DIR (0x00 | ECP_CTYPE_FLAG_SYS) +#define ECP_DIR_CAP_DIR 0x01 +#define ECP_DIR_CAP_VCONN 0x02 + typedef struct ECPDirItem { ECPNode node; uint16_t capabilities; } ECPDirItem; -typedef struct ECPDirList { - ECPDirItem item[ECP_MAX_DIR_ITEM]; - uint16_t count; -} ECPDirList; - -ssize_t ecp_dir_parse(ECPDirList *list, unsigned char *buf, size_t buf_size); -ssize_t ecp_dir_serialize(ECPDirList *list, unsigned char *buf, size_t buf_size); - -void ecp_dir_item_parse(ECPDirItem *item, unsigned char *buf); -void ecp_dir_item_serialize(ECPDirItem *item, unsigned char *buf); - -ssize_t ecp_dir_handle(ECPConnection *conn, unsigned char *msg, size_t msg_size, ECP2Buffer *b); +size_t ecp_dir_item_parse(ECPDirItem *item, unsigned char *buf); +size_t ecp_dir_item_serialize(ECPDirItem *item, unsigned char *buf); int ecp_dir_handle_open(ECPConnection *conn, ECP2Buffer *b); -ssize_t ecp_dir_handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b); - ssize_t ecp_dir_send_req(ECPConnection *conn); -int ecp_dir_get(ECPConnection *conn, ECPSocket *sock, ECPNode *node);
\ No newline at end of file +void ecp_dir_conn_init(ECPConnection *conn, ECPSocket *sock);
\ No newline at end of file diff --git a/ecp/src/ecp/dir/dir_srv.c b/ecp/src/ecp/dir/dir_srv.c deleted file mode 100644 index cbaafc5..0000000 --- a/ecp/src/ecp/dir/dir_srv.c +++ /dev/null @@ -1,143 +0,0 @@ -#include <stdlib.h> - -#include <core.h> -#include <cr.h> - -#include "dir.h" -#include "dir_srv.h" - -int ecp_dir_create(ECPContext *ctx, ECPDirSrv *dir_srv, ECPDirList *dir_online, ECPDirList *dir_shadow) { - int rv; - -#ifdef ECP_WITH_PTHREAD - rv = pthread_mutex_init(&dir_srv->online.mutex, NULL); - if (rv) return ECP_ERR; - - rv = pthread_mutex_init(&dir_srv->shadow.mutex, NULL); - if (rv) { - pthread_mutex_destroy(&dir_srv->online.mutex); - return ECP_ERR; - } -#endif - - dir_srv->online.list = dir_online; - dir_srv->shadow.list = dir_shadow; - ctx->dir_srv = dir_srv; - - return ECP_OK; -} - -void ecp_dir_destroy(ECPContext *ctx) { - ECPDirSrv *dir_srv = ctx->dir_srv; - -#ifdef ECP_WITH_PTHREAD - pthread_mutex_destroy(&dir_srv->shadow.mutex); - pthread_mutex_destroy(&dir_srv->online.mutex); -#endif - -} - -ssize_t ecp_dir_send_list(ECPConnection *conn, unsigned char mtype, ECPDirTable *dir_table) { - ECPBuffer packet; - ECPBuffer payload; - unsigned char pkt_buf[ECP_MAX_PKT]; - unsigned char pld_buf[ECP_MAX_PLD]; - unsigned char *msg; - size_t hdr_size; - size_t msg_size; - ssize_t rv; - - packet.buffer = pkt_buf; - packet.size = ECP_MAX_PKT; - payload.buffer = pld_buf; - payload.size = ECP_MAX_PLD; - - ecp_pld_set_type(payload.buffer, payload.size, mtype); - msg = ecp_pld_get_msg(payload.buffer, payload.size); - hdr_size = msg - payload.buffer; - msg_size = payload.size - hdr_size; - -#ifdef ECP_WITH_PTHREAD - pthread_mutex_lock(&dir_table->mutex); -#endif - - rv = ecp_dir_serialize(dir_table->list, msg, msg_size); - -#ifdef ECP_WITH_PTHREAD - pthread_mutex_unlock(&dir_table->mutex); -#endif - - if (rv < 0) return rv; - - rv = ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(rv, mtype), 0); - return rv; -} - -ssize_t ecp_dir_send_upd(ECPConnection *conn) { - ECPDirSrv *dir_srv = conn->sock->ctx->dir_srv; - ECPDirTable *dir_table = &dir_srv->shadow; - ssize_t rv; - - rv = ecp_dir_send_list(conn, ECP_MTYPE_DIR_UPD, dir_table); - return rv; -} - -ssize_t ecp_dir_handle_upd(ECPConnection *conn, unsigned char *msg, size_t msg_size) { - ECPDirSrv *dir_srv = conn->sock->ctx->dir_srv; - ECPDirTable *dir_table = &dir_srv->shadow; - ssize_t rsize; - ssize_t rv; - -#ifdef ECP_WITH_PTHREAD - pthread_mutex_lock(&dir_table->mutex); -#endif - - rsize = ecp_dir_parse(dir_table->list, msg, msg_size); - -#ifdef ECP_WITH_PTHREAD - pthread_mutex_unlock(&dir_table->mutex); -#endif - - if (rsize < 0) return rsize; - - rv = ecp_dir_send_list(conn, ECP_MTYPE_DIR_REP, dir_table); - if (rv < 0) return rv; - - return rsize; -} - -ssize_t ecp_dir_handle_req(ECPConnection *conn, unsigned char *msg, size_t msg_size) { - ssize_t rv; - - rv = ecp_dir_send_rep(conn); - if (rv < 0) return rv; - - return 0; -} - -ssize_t ecp_dir_send_rep(ECPConnection *conn) { - ECPDirSrv *dir_srv = conn->sock->ctx->dir_srv; - ECPDirTable *dir_table = &dir_srv->online; - ssize_t rv; - - rv = ecp_dir_send_list(conn, ECP_MTYPE_DIR_REP, dir_table); - return rv; -} - -ssize_t ecp_dir_handle_rep(ECPConnection *conn, unsigned char *msg, size_t msg_size) { - ECPDirSrv *dir_srv = conn->sock->ctx->dir_srv; - ECPDirTable *dir_table = &dir_srv->shadow; - ssize_t rv; - -#ifdef ECP_WITH_PTHREAD - pthread_mutex_lock(&dir_table->mutex); -#endif - - rv = ecp_dir_parse(dir_table->list, msg, msg_size); - -#ifdef ECP_WITH_PTHREAD - pthread_mutex_unlock(&dir_table->mutex); -#endif - - return rv; -} diff --git a/ecp/src/ecp/dir/dir_srv.h b/ecp/src/ecp/dir/dir_srv.h deleted file mode 100644 index 5e107df..0000000 --- a/ecp/src/ecp/dir/dir_srv.h +++ /dev/null @@ -1,23 +0,0 @@ -typedef struct ECPDirTable { - ECPDirList *list; -#ifdef ECP_WITH_PTHREAD - pthread_mutex_t mutex; -#endif -} ECPDirTable; - -typedef struct ECPDirSrv { - ECPDirTable online; - ECPDirTable shadow; -} ECPDirSrv; - -int ecp_dir_create(ECPContext *ctx, ECPDirSrv *dir_srv, ECPDirList *dir_online, ECPDirList *dir_shadow); -void ecp_dir_destroy(ECPContext *ctx); -ssize_t ecp_dir_send_list(ECPConnection *conn, unsigned char mtype, ECPDirTable *list); - -ssize_t ecp_dir_send_upd(ECPConnection *conn); -ssize_t ecp_dir_handle_upd(ECPConnection *conn, unsigned char *msg, size_t msg_size); - -ssize_t ecp_dir_handle_req(ECPConnection *conn, unsigned char *msg, size_t msg_size); - -ssize_t ecp_dir_send_rep(ECPConnection *conn); -ssize_t ecp_dir_handle_rep(ECPConnection *conn, unsigned char *msg, size_t msg_size); diff --git a/ecp/src/ecp/ext.h b/ecp/src/ecp/ext.h index 4c91000..d661329 100644 --- a/ecp/src/ecp/ext.h +++ b/ecp/src/ecp/ext.h @@ -1,23 +1,36 @@ +#ifdef ECP_WITH_FRAG + +ssize_t ecp_ext_pld_handle_one(ECPConnection *conn, ecp_seq_t seq, unsigned char *payload, size_t pld_size, ECP2Buffer *bufs); +ssize_t ecp_ext_msg_send(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECPBuffer *packet, ECPBuffer *payload); + +#else + +#define ecp_ext_pld_handle_one(c,s,p,sz,b) (0) +#define ecp_ext_msg_send(c,t,m,sz,p1,p2) (0) + +#endif + #ifdef ECP_WITH_RBUF int ecp_ext_err_handle(ECPConnection *conn, unsigned char mtype, int err); -int ecp_ext_conn_open(ECPConnection *conn); +int ecp_ext_conn_create(ECPConnection *conn); void ecp_ext_conn_destroy(ECPConnection *conn); +int ecp_ext_conn_open(ECPConnection *conn); +void ecp_ext_conn_close(ECPConnection *conn); + ssize_t ecp_ext_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs); -ssize_t ecp_ext_pld_store(ECPConnection *conn, ecp_seq_t seq, unsigned char *payload, size_t pld_size, ECP2Buffer *bufs); ssize_t ecp_ext_pld_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *payload, size_t pld_size, ECP2Buffer *bufs); ssize_t ecp_ext_pld_send(ECPConnection *conn, ECPBuffer *payload, size_t pld_size, ECPBuffer *packet, size_t pkt_size, unsigned char flags, ECPTimerItem *ti, ecp_tr_addr_t *addr); -ssize_t ecp_ext_msg_send(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECPBuffer *packet, ECPBuffer *payload); #else #define ecp_ext_err_handle(c,t,e) (ECP_PASS) -#define ecp_ext_conn_open(c) (ECP_OK) +#define ecp_ext_conn_create(c) (0) #define ecp_ext_conn_destroy(c) ; +#define ecp_ext_conn_open(c) (ECP_OK) +#define ecp_ext_conn_close(c) ; #define ecp_ext_msg_handle(c,s,t,m,sz,b) (0) -#define ecp_ext_pld_store(c,s,p,sz,b) (0) #define ecp_ext_pld_handle(c,s,p,sz,b) (0) #define ecp_ext_pld_send(c,p1,sz1,p2,sz2,f,t,a) (0) -#define ecp_ext_msg_send(c,t,m,sz,p1,p2) (0) #endif diff --git a/ecp/src/ecp/ext/Makefile b/ecp/src/ecp/ext/Makefile index 0e61e83..b20258f 100644 --- a/ecp/src/ecp/ext/Makefile +++ b/ecp/src/ecp/ext/Makefile @@ -1,6 +1,18 @@ include ../common.mk -obj = ext.o frag.o rbuf.o rbuf_send.o rbuf_recv.o msgq.o +obj = + +ifeq ($(with_frag),yes) +obj += frag.o +endif + +ifeq ($(with_rbuf),yes) +obj += rbuf.o rbuf_send.o rbuf_recv.o rbuf_ext.o +endif + +ifeq ($(with_msgq),yes) +obj += msgq.o +endif %.o: %.c diff --git a/ecp/src/ecp/ext/frag.c b/ecp/src/ecp/ext/frag.c index 58941ba..8795470 100644 --- a/ecp/src/ecp/ext/frag.c +++ b/ecp/src/ecp/ext/frag.c @@ -1,12 +1,11 @@ #include <stdlib.h> #include <string.h> -#include <core.h> +#include <ecp/core.h> -#include "rbuf.h" #include "frag.h" -int ecp_frag_iter_init(ECPRBConn *conn, ECPFragIter *iter, unsigned char *buffer, size_t buf_size) { +int ecp_frag_iter_init(ECPConnection *conn, ECPFragIter *iter, unsigned char *buffer, size_t buf_size) { memset(iter, 0, sizeof(ECPFragIter)); iter->buffer = buffer; iter->buf_size = buf_size; @@ -15,17 +14,64 @@ int ecp_frag_iter_init(ECPRBConn *conn, ECPFragIter *iter, unsigned char *buffer return ECP_OK; } -ECPFragIter *ecp_frag_iter_get(ECPRBConn *conn) { - return conn->iter; -} - void ecp_frag_iter_reset(ECPFragIter *iter) { iter->seq = 0; iter->frag_cnt = 0; iter->pld_size = 0; } -ssize_t ecp_msg_frag(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECPBuffer *packet, ECPBuffer *payload) { +ssize_t ecp_pld_defrag(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *payload, size_t pld_size) { + ECPFragIter *iter = conn->iter; + unsigned char *msg; + unsigned char frag_cnt, frag_tot; + uint16_t frag_size; + size_t hdr_size, msg_size; + size_t buf_offset; + int rv; + + rv = ecp_pld_get_frag(payload, pld_size, &frag_cnt, &frag_tot, &frag_size); + if (rv) return ECP_ERR; + + msg = ecp_pld_get_msg(payload, pld_size); + if (msg == NULL) return ECP_ERR; + hdr_size = msg - payload; + + msg_size = pld_size - hdr_size; + if (msg_size == 0) return ECP_ERR; + + if (iter->pld_size && (iter->seq + frag_cnt != seq)) ecp_frag_iter_reset(iter); + + if (iter->pld_size == 0) { + iter->seq = seq - frag_cnt; + iter->frag_cnt = 0; + } + + mtype &= ~ECP_MTYPE_FLAG_FRAG; + buf_offset = ECP_SIZE_MTYPE + ECP_SIZE_MT_FLAG(mtype) + frag_size * frag_cnt; + if (buf_offset + msg_size > iter->buf_size) return ECP_ERR_SIZE; + memcpy(iter->buffer + buf_offset, msg, msg_size); + + if (frag_cnt == 0) { + if (ECP_SIZE_MTYPE + ECP_SIZE_MT_FLAG(mtype) > iter->buf_size) return ECP_ERR_SIZE; + + iter->buffer[0] = mtype; + if (ECP_SIZE_MT_FLAG(mtype)) { + memcpy(iter->buffer + ECP_SIZE_MTYPE, payload + ECP_SIZE_MTYPE, ECP_SIZE_MT_FLAG(mtype)); + } + msg_size += ECP_SIZE_MTYPE + ECP_SIZE_MT_FLAG(mtype); + } + + iter->frag_cnt++; + iter->pld_size += msg_size; + if (iter->frag_cnt == frag_tot) { + ecp_pld_handle_one(conn, iter->seq, iter->buffer, iter->pld_size, NULL); + ecp_frag_iter_reset(iter); + } + + return pld_size; +} + +ssize_t ecp_msg_send_wfrag(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECPBuffer *packet, ECPBuffer *payload) { unsigned char *msg_buf; unsigned char *pld_buf; size_t pld_size; @@ -72,58 +118,26 @@ ssize_t ecp_msg_frag(ECPConnection *conn, unsigned char mtype, unsigned char *ms return msg_size; } -ssize_t ecp_pld_defrag(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *payload, size_t pld_size) { - ECPRBConn *_conn = NULL; - ECPFragIter *iter = NULL; - unsigned char *msg; - unsigned char frag_cnt, frag_tot; - uint16_t frag_size; - size_t hdr_size, msg_size; - size_t buf_offset; - int rv; - - _conn = ecp_rbuf_get_rbconn(conn); - if (_conn) iter = ecp_frag_iter_get(_conn); - if (iter == NULL) ECP_ERR; - - rv = ecp_pld_get_frag(payload, pld_size, &frag_cnt, &frag_tot, &frag_size); - if (rv) return ECP_ERR; - - msg = ecp_pld_get_msg(payload, pld_size); - if (msg == NULL) return ECP_ERR; - hdr_size = msg - payload; - - msg_size = pld_size - hdr_size; - if (msg_size == 0) return ECP_ERR; - - if (iter->pld_size && (iter->seq + frag_cnt != seq)) ecp_frag_iter_reset(iter); - - if (iter->pld_size == 0) { - iter->seq = seq - frag_cnt; - iter->frag_cnt = 0; - } - - mtype &= ~ECP_MTYPE_FLAG_FRAG; - buf_offset = ECP_SIZE_MTYPE + ECP_SIZE_MT_FLAG(mtype) + frag_size * frag_cnt; - if (buf_offset + msg_size > iter->buf_size) return ECP_ERR_SIZE; - memcpy(iter->buffer + buf_offset, msg, msg_size); +ssize_t ecp_ext_pld_handle_one(ECPConnection *conn, ecp_seq_t seq, unsigned char *payload, size_t pld_size, ECP2Buffer *bufs) { + if (conn->iter) { + unsigned char mtype; + int rv; - if (frag_cnt == 0) { - if (ECP_SIZE_MTYPE + ECP_SIZE_MT_FLAG(mtype) > iter->buf_size) return ECP_ERR_SIZE; + rv = ecp_pld_get_type(payload, pld_size, &mtype); + if (rv) return rv; - iter->buffer[0] = mtype; - if (ECP_SIZE_MT_FLAG(mtype)) { - memcpy(iter->buffer + ECP_SIZE_MTYPE, payload + ECP_SIZE_MTYPE, ECP_SIZE_MT_FLAG(mtype)); + if (mtype & ECP_MTYPE_FLAG_FRAG) { + return ecp_pld_defrag(conn, seq, mtype, payload, pld_size); } - msg_size += ECP_SIZE_MTYPE + ECP_SIZE_MT_FLAG(mtype); } - iter->frag_cnt++; - iter->pld_size += msg_size; - if (iter->frag_cnt == frag_tot) { - ecp_pld_handle_one(conn, iter->seq, iter->buffer, iter->pld_size, NULL); - ecp_frag_iter_reset(iter); - } + return 0; +} - return pld_size; +ssize_t ecp_ext_msg_send(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECPBuffer *packet, ECPBuffer *payload) { + if (ECP_SIZE_PKT_BUF(msg_size, mtype, conn) > ECP_MAX_PKT) { + return ecp_msg_send_wfrag(conn, mtype, msg, msg_size, packet, payload); + } else { + return 0; + } } diff --git a/ecp/src/ecp/ext/frag.h b/ecp/src/ecp/ext/frag.h index 1400c1d..2bf35d1 100644 --- a/ecp/src/ecp/ext/frag.h +++ b/ecp/src/ecp/ext/frag.h @@ -6,8 +6,7 @@ typedef struct ECPFragIter { size_t pld_size; } ECPFragIter; -int ecp_frag_iter_init(ECPRBConn *conn, ECPFragIter *iter, unsigned char *buffer, size_t buf_size); -ECPFragIter *ecp_frag_iter_get(ECPRBConn *conn); +int ecp_frag_iter_init(ECPConnection *conn, ECPFragIter *iter, unsigned char *buffer, size_t buf_size); void ecp_frag_iter_reset(ECPFragIter *iter); -ssize_t ecp_msg_frag(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECPBuffer *packet, ECPBuffer *payload); -ssize_t ecp_pld_defrag(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *payload, size_t pld_size);
\ No newline at end of file +ssize_t ecp_pld_defrag(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *payload, size_t pld_size); +ssize_t ecp_msg_send_wfrag(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECPBuffer *packet, ECPBuffer *payload); diff --git a/ecp/src/ecp/ext/msgq.c b/ecp/src/ecp/ext/msgq.c index e383b03..7172e51 100644 --- a/ecp/src/ecp/ext/msgq.c +++ b/ecp/src/ecp/ext/msgq.c @@ -2,7 +2,7 @@ #include <stdlib.h> #include <string.h> -#include <core.h> +#include <ecp/core.h> #include "rbuf.h" #include "msgq.h" @@ -21,13 +21,15 @@ static struct timespec *abstime_ts(struct timespec *ts, ecp_sts_t msec) { return ts; } -int ecp_msgq_create(ECPRBConn *conn, ECPMsgQ *msgq) { - int i; - int rv; +void ecp_msgq_init(ECPRBConn *conn, ECPMsgQ *msgq) { + memset(msgq, 0, sizeof(ECPMsgQ)); - if (conn->recv == NULL) return ECP_ERR; + conn->recv->msgq = msgq; +} - memset(msgq, 0, sizeof(ECPMsgQ)); +int ecp_msgq_create(ECPRBConn *conn) { + ECPMsgQ *msgq = conn->recv->msgq; + int i, rv; for (i=0; i<ECP_MSGQ_MAX_MTYPE; i++) { rv = pthread_cond_init(&msgq->cond[i], NULL); @@ -40,7 +42,6 @@ int ecp_msgq_create(ECPRBConn *conn, ECPMsgQ *msgq) { return ECP_ERR; } } - conn->recv->msgq = msgq; return ECP_OK; } @@ -52,8 +53,6 @@ void ecp_msgq_destroy(ECPRBConn *conn) { for (i=0; i<ECP_MSGQ_MAX_MTYPE; i++) { pthread_cond_destroy(&msgq->cond[i]); } - - conn->recv->msgq = NULL; } void ecp_msgq_start(ECPRBConn *conn, ecp_seq_t seq) { @@ -117,6 +116,8 @@ ssize_t ecp_msgq_pop(ECPRBConn *conn, unsigned char mtype, unsigned char *msg, s msg_size = pld_size - hdr_size; rbuf->arr.pld[idx].flags &= ~ECP_RBUF_FLAG_IN_MSGQ; + // if (rbuf->arr.pld[idx].flags == 0); + msgq->idx_r[mtype]++; if (msgq->seq_start == seq) { int i; diff --git a/ecp/src/ecp/ext/msgq.h b/ecp/src/ecp/ext/msgq.h index dddb6e7..669a5af 100644 --- a/ecp/src/ecp/ext/msgq.h +++ b/ecp/src/ecp/ext/msgq.h @@ -11,7 +11,8 @@ typedef struct ECPMsgQ { pthread_cond_t cond[ECP_MSGQ_MAX_MTYPE]; } ECPMsgQ; -int ecp_msgq_create(ECPRBConn *conn, ECPMsgQ *msgq); +void ecp_msgq_init(ECPRBConn *conn, ECPMsgQ *msgq); +int ecp_msgq_create(ECPRBConn *conn); void ecp_msgq_destroy(ECPRBConn *conn); void ecp_msgq_start(ECPRBConn *conn, ecp_seq_t seq); int ecp_msgq_push(ECPRBConn *conn, ecp_seq_t seq, unsigned char mtype); diff --git a/ecp/src/ecp/ext/rbuf.c b/ecp/src/ecp/ext/rbuf.c index 70ee0d2..e1fab7b 100644 --- a/ecp/src/ecp/ext/rbuf.c +++ b/ecp/src/ecp/ext/rbuf.c @@ -1,6 +1,6 @@ #include <stdlib.h> -#include <core.h> +#include <ecp/core.h> #include "rbuf.h" @@ -28,41 +28,35 @@ int _ecp_rbuf_msg_idx(ECPRBuffer *rbuf, ecp_seq_t seq, unsigned short *idx) { return ECP_OK; } -void ecp_rbuf_conn_init(ECPRBConn *conn) { +void ecp_rbuf_init(ECPRBConn *conn) { ECPConnection *_conn = ecp_rbuf_get_conn(conn); ecp_conn_set_flags(_conn, ECP_CONN_FLAG_RBUF); conn->send = NULL; conn->recv = NULL; - conn->iter = NULL; } -int ecp_rbuf_conn_create(ECPRBConn *conn, ECPSocket *sock, unsigned char type) { - ECPConnection *_conn = ecp_rbuf_get_conn(conn); +int ecp_rbuf_create(ECPRBConn *conn) { int rv; - rv = ecp_conn_create(_conn, sock, type); - if (rv) return rv; - - ecp_rbuf_conn_init(conn); - return ECP_OK; -} - -int ecp_rbuf_conn_create_inb(ECPRBConn *conn, ECPSocket *sock, unsigned char type) { - ECPConnection *_conn = ecp_rbuf_get_conn(conn); - int rv; - - rv = ecp_conn_create_inb(_conn, sock, type); - if (rv) return rv; + if (conn->send) { + rv = ecp_rbsend_create(conn); + if (rv) return rv; + } + if (conn->recv) { + rv = ecp_rbrecv_create(conn); + if (rv) { + if (conn->send) ecp_rbsend_destroy(conn); + return rv; + } + } - ecp_rbuf_conn_init(conn); return ECP_OK; } void ecp_rbuf_destroy(ECPRBConn *conn) { if (conn->send) ecp_rbsend_destroy(conn); if (conn->recv) ecp_rbrecv_destroy(conn); - conn->iter = NULL; } void ecp_rbuf_start(ECPRBConn *conn) { @@ -71,14 +65,32 @@ void ecp_rbuf_start(ECPRBConn *conn) { if (conn->send) { ecp_seq_t seq_out; +#ifdef ECP_WITH_PTHREAD + pthread_mutex_lock(&_conn->mutex); +#endif + seq_out = (ecp_seq_t)(_conn->nonce_out); + +#ifdef ECP_WITH_PTHREAD + pthread_mutex_unlock(&_conn->mutex); +#endif + ecp_rbsend_start(conn, seq_out); } if (conn->recv) { ecp_seq_t seq_in; +#ifdef ECP_WITH_PTHREAD + pthread_mutex_lock(&_conn->mutex); +#endif + seq_in = (ecp_seq_t)(_conn->nonce_in); + +#ifdef ECP_WITH_PTHREAD + pthread_mutex_unlock(&_conn->mutex); +#endif + ecp_rbrecv_start(conn, seq_in); } } diff --git a/ecp/src/ecp/ext/rbuf.h b/ecp/src/ecp/ext/rbuf.h index 36ff963..bf6e6df 100644 --- a/ecp/src/ecp/ext/rbuf.h +++ b/ecp/src/ecp/ext/rbuf.h @@ -87,16 +87,14 @@ typedef struct ECPRBConn { ECPConnection b; ECPRBRecv *recv; ECPRBSend *send; - struct ECPFragIter *iter; } ECPRBConn; ECPRBConn *ecp_rbuf_get_rbconn(ECPConnection *conn); ECPConnection *ecp_rbuf_get_conn(ECPRBConn *conn); void _ecp_rbuf_start(ECPRBuffer *rbuf, ecp_seq_t seq); int _ecp_rbuf_msg_idx(ECPRBuffer *rbuf, ecp_seq_t seq, unsigned short *idx); -void ecp_rbuf_conn_init(ECPRBConn *conn); -int ecp_rbuf_conn_create(ECPRBConn *conn, ECPSocket *sock, unsigned char type); -int ecp_rbuf_conn_create_inb(ECPRBConn *conn, ECPSocket *sock, unsigned char type); +void ecp_rbuf_init(ECPRBConn *conn); +int ecp_rbuf_create(ECPRBConn *conn); void ecp_rbuf_destroy(ECPRBConn *conn); void ecp_rbuf_start(ECPRBConn *conn); ssize_t ecp_rbuf_msg_handle(ECPRBConn *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs); @@ -105,19 +103,23 @@ int ecp_rbuf_err_handle(ECPRBConn *conn, unsigned char mtype, int err); /* send */ ssize_t ecp_rbuf_send_flush(ECPRBConn *conn); ssize_t ecp_rbuf_handle_ack(ECPRBConn *conn, unsigned char *msg, size_t msg_size); -int ecp_rbsend_create(ECPRBConn *conn, ECPRBSend *buf, ECPRBPacket *pkt, unsigned short pkt_size); +void ecp_rbsend_init(ECPRBConn *conn, ECPRBSend *buf, ECPRBPacket *pkt, unsigned short pkt_size); +int ecp_rbsend_create(ECPRBConn *conn); void ecp_rbsend_destroy(ECPRBConn *conn); void ecp_rbsend_start(ECPRBConn *conn, ecp_seq_t seq); int ecp_rbuf_set_wsize(ECPRBConn *conn, ecp_win_t size); int ecp_rbuf_flush(ECPRBConn *conn); ssize_t ecp_rbuf_pld_send(ECPRBConn *conn, ECPBuffer *payload, size_t pld_size, ECPBuffer *packet, size_t pkt_size, ECPTimerItem *ti); +/* recv */ ssize_t ecp_rbuf_handle_nop(ECPRBConn *conn, unsigned char *msg, size_t msg_size); ssize_t ecp_rbuf_handle_flush(ECPRBConn *conn); -void ecp_rbuf_handle_timer(ECPRBConn *conn) ; -int ecp_rbrecv_create(ECPRBConn *conn, ECPRBRecv *buf, ECPRBPayload *pld, unsigned short pld_size); +void ecp_rbuf_handle_timer(ECPRBConn *conn); +void ecp_rbrecv_init(ECPRBConn *conn, ECPRBRecv *buf, ECPRBPayload *pld, unsigned short pld_size); +int ecp_rbrecv_create(ECPRBConn *conn); void ecp_rbrecv_destroy(ECPRBConn *conn); void ecp_rbrecv_start(ECPRBConn *conn, ecp_seq_t seq); + int ecp_rbuf_set_hole(ECPRBConn *conn, unsigned short hole_max); int ecp_rbuf_set_delay(ECPRBConn *conn, ecp_sts_t delay); ssize_t ecp_rbuf_store(ECPRBConn *conn, ecp_seq_t seq, unsigned char *pld, size_t pld_size); diff --git a/ecp/src/ecp/ext/ext.c b/ecp/src/ecp/ext/rbuf_ext.c index d407e80..67b6a8d 100644 --- a/ecp/src/ecp/ext/ext.c +++ b/ecp/src/ecp/ext/rbuf_ext.c @@ -1,10 +1,9 @@ #include <stdlib.h> -#include <core.h> -#include <ext.h> +#include <ecp/core.h> +#include <ecp/ext.h> #include "rbuf.h" -#include "frag.h" int ecp_ext_err_handle(ECPConnection *conn, unsigned char mtype, int err) { ECPRBConn *_conn = ecp_rbuf_get_rbconn(conn); @@ -13,44 +12,41 @@ int ecp_ext_err_handle(ECPConnection *conn, unsigned char mtype, int err) { return ECP_PASS; } -int ecp_ext_conn_open(ECPConnection *conn) { +int ecp_ext_conn_create(ECPConnection *conn) { ECPRBConn *_conn = ecp_rbuf_get_rbconn(conn); + int rv = ECP_OK; - if (_conn) ecp_rbuf_start(_conn); - return ECP_OK; + if (_conn) rv = ecp_rbuf_create(_conn); + return rv; } void ecp_ext_conn_destroy(ECPConnection *conn) { ECPRBConn *_conn = ecp_rbuf_get_rbconn(conn); + if (_conn) ecp_rbuf_destroy(_conn); } -ssize_t ecp_ext_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) { +int ecp_ext_conn_open(ECPConnection *conn) { ECPRBConn *_conn = ecp_rbuf_get_rbconn(conn); - if (_conn) return ecp_rbuf_msg_handle(_conn, seq, mtype, msg, msg_size, bufs); - return 0; + if (_conn) ecp_rbuf_start(_conn); + return ECP_OK; } -ssize_t ecp_ext_pld_store(ECPConnection *conn, ecp_seq_t seq, unsigned char *payload, size_t pld_size, ECP2Buffer *bufs) { +void ecp_ext_conn_close(ECPConnection *conn) {} + +ssize_t ecp_ext_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) { ECPRBConn *_conn = ecp_rbuf_get_rbconn(conn); - if (_conn && _conn->recv) return ecp_rbuf_store(_conn, seq, payload, pld_size); + if (_conn) return ecp_rbuf_msg_handle(_conn, seq, mtype, msg, msg_size, bufs); return 0; } ssize_t ecp_ext_pld_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *payload, size_t pld_size, ECP2Buffer *bufs) { - unsigned char mtype; - int rv; - - rv = ecp_pld_get_type(payload, pld_size, &mtype); - if (rv) return rv; + ECPRBConn *_conn = ecp_rbuf_get_rbconn(conn); - if (mtype & ECP_MTYPE_FLAG_FRAG) { - return ecp_pld_defrag(conn, seq, mtype, payload, pld_size); - } else { - return 0; - } + if (_conn && _conn->recv) return ecp_rbuf_store(_conn, seq, payload, pld_size); + return 0; } ssize_t ecp_ext_pld_send(ECPConnection *conn, ECPBuffer *payload, size_t pld_size, ECPBuffer *packet, size_t pkt_size, unsigned char flags, ECPTimerItem *ti, ecp_tr_addr_t *addr) { @@ -59,11 +55,3 @@ ssize_t ecp_ext_pld_send(ECPConnection *conn, ECPBuffer *payload, size_t pld_siz if (_conn && _conn->send) return ecp_rbuf_pld_send(_conn, payload, pld_size, packet, pkt_size, ti); return 0; } - -ssize_t ecp_ext_msg_send(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECPBuffer *packet, ECPBuffer *payload) { - if (ECP_SIZE_PKT_BUF(msg_size, mtype, conn) > ECP_MAX_PKT) { - return ecp_msg_frag(conn, mtype, msg, msg_size, packet, payload); - } else { - return 0; - } -} diff --git a/ecp/src/ecp/ext/rbuf_recv.c b/ecp/src/ecp/ext/rbuf_recv.c index 81327d5..b981e3f 100644 --- a/ecp/src/ecp/ext/rbuf_recv.c +++ b/ecp/src/ecp/ext/rbuf_recv.c @@ -1,8 +1,8 @@ #include <stdlib.h> #include <string.h> -#include <core.h> -#include <tm.h> +#include <ecp/core.h> +#include <ecp/tm.h> #include "rbuf.h" @@ -89,7 +89,7 @@ static void msg_flush(ECPRBConn *conn) { rv = ecp_pld_get_pts(rbuf->arr.pld[idx].buf, rbuf->arr.pld[idx].size, &msg_pts); if (!rv && buf->deliver_delay) { - ecp_sts_t now = ecp_tm_abstime_ms(0); + ecp_sts_t now = ecp_tm_get_ms(); msg_pts += buf->deliver_delay; if (ECP_PTS_LT(now, msg_pts)) { @@ -122,6 +122,7 @@ static void msg_flush(ECPRBConn *conn) { rbuf->arr.pld[idx].flags &= ~ECP_RBUF_FLAG_SKIP; } rbuf->arr.pld[idx].flags &= ~ECP_RBUF_FLAG_IN_RBUF; + // if (rbuf->arr.pld[idx].flags == 0); } else { if (buf->flags & ECP_RBUF_FLAG_RELIABLE) break; if (!ECP_SEQ_LT(seq, rbuf->seq_max - buf->hole_max)) break; @@ -274,9 +275,8 @@ void ecp_rbuf_handle_timer(ECPRBConn *conn) { #endif } -int ecp_rbrecv_create(ECPRBConn *conn, ECPRBRecv *buf, ECPRBPayload *pld, unsigned short pld_size) { +void ecp_rbrecv_init(ECPRBConn *conn, ECPRBRecv *buf, ECPRBPayload *pld, unsigned short pld_size) { ECPRBuffer *rbuf = &buf->rbuf; - int rv; memset(buf, 0, sizeof(ECPRBRecv)); memset(pld, 0, sizeof(ECPRBPayload) * pld_size); @@ -286,12 +286,31 @@ int ecp_rbrecv_create(ECPRBConn *conn, ECPRBRecv *buf, ECPRBPayload *pld, unsign rbuf->arr.pld = pld; rbuf->arr_size = pld_size; + conn->recv = buf; +} + +int ecp_rbrecv_create(ECPRBConn *conn) { + ECPRBRecv *buf = conn->recv; + int rv; + #ifdef ECP_WITH_PTHREAD rv = pthread_mutex_init(&buf->mutex, NULL); if (rv) return ECP_ERR; #endif - conn->recv = buf; +#ifdef ECP_WITH_MSGQ + if (buf->msgq) { + rv = ecp_msgq_create(conn); + if (rv) { +#ifdef ECP_WITH_PTHREAD + pthread_mutex_destroy(&buf->mutex); +#endif + + return ECP_ERR; + } + } +#endif + return ECP_OK; } @@ -305,8 +324,6 @@ void ecp_rbrecv_destroy(ECPRBConn *conn) { #ifdef ECP_WITH_MSGQ if (buf->msgq) ecp_msgq_destroy(conn); #endif - - conn->recv = NULL; } void ecp_rbrecv_start(ECPRBConn *conn, ecp_seq_t seq) { diff --git a/ecp/src/ecp/ext/rbuf_send.c b/ecp/src/ecp/ext/rbuf_send.c index d3d2f04..40d526c 100644 --- a/ecp/src/ecp/ext/rbuf_send.c +++ b/ecp/src/ecp/ext/rbuf_send.c @@ -1,7 +1,7 @@ #include <stdlib.h> #include <string.h> -#include <core.h> +#include <ecp/core.h> #include "rbuf.h" @@ -43,6 +43,7 @@ static void cc_flush(ECPRBConn *conn, unsigned char flags) { } if (!(buf->flags & ECP_RBUF_FLAG_RELIABLE)) { rbuf->arr.pkt[idx].flags = 0; + // if (rbuf->arr.pkt[idx].flags == 0); } buf->seq_cc++; idx = ECP_RBUF_IDX_MASK(idx + 1, rbuf->arr_size); @@ -209,6 +210,7 @@ ssize_t ecp_rbuf_handle_ack(ECPRBConn *conn, unsigned char *msg, size_t msg_size idx = rbuf->idx_start; for (i=0; i<msg_cnt; i++) { rbuf->arr.pkt[idx].flags = 0; + // if (rbuf->arr.pkt[idx].flags == 0); idx = ECP_RBUF_IDX_MASK(idx + 1, rbuf->arr_size); } rbuf->seq_start = seq_start; @@ -237,9 +239,8 @@ handle_ack_fin: return rsize; } -int ecp_rbsend_create(ECPRBConn *conn, ECPRBSend *buf, ECPRBPacket *pkt, unsigned short pkt_size) { +void ecp_rbsend_init(ECPRBConn *conn, ECPRBSend *buf, ECPRBPacket *pkt, unsigned short pkt_size) { ECPRBuffer *rbuf = &buf->rbuf; - int rv; memset(buf, 0, sizeof(ECPRBRecv)); memset(pkt, 0, sizeof(ECPRBPacket) * pkt_size); @@ -247,12 +248,18 @@ int ecp_rbsend_create(ECPRBConn *conn, ECPRBSend *buf, ECPRBPacket *pkt, unsigne rbuf->arr.pkt = pkt; rbuf->arr_size = pkt_size; + conn->send = buf; +} + +int ecp_rbsend_create(ECPRBConn *conn) { + ECPRBSend *buf = conn->send; + int rv; + #ifdef ECP_WITH_PTHREAD rv = pthread_mutex_init(&buf->mutex, NULL); if (rv) return ECP_ERR; #endif - conn->send = buf; return ECP_OK; } @@ -262,8 +269,6 @@ void ecp_rbsend_destroy(ECPRBConn *conn) { #ifdef ECP_WITH_PTHREAD pthread_mutex_destroy(&buf->mutex); #endif - - conn->send = NULL; } void ecp_rbsend_start(ECPRBConn *conn, ecp_seq_t seq) { diff --git a/ecp/src/ecp/ht.h b/ecp/src/ecp/ht.h index 31ca698..805fbda 100644 --- a/ecp/src/ecp/ht.h +++ b/ecp/src/ecp/ht.h @@ -2,13 +2,16 @@ ecp_ht_table_t *ecp_ht_create_keys(void); ecp_ht_table_t *ecp_ht_create_addrs(void); void ecp_ht_destroy(ecp_ht_table_t *h); -int ecp_ht_insert(ecp_ht_table_t *h, void *k, ECPConnection *v); -ECPConnection *ecp_ht_remove(ecp_ht_table_t *h, void *k); -ECPConnection *ecp_ht_search(ecp_ht_table_t *h, void *k); +int ecp_ht_insert(ecp_ht_table_t *h, void *k, void *v); +void *ecp_ht_remove(ecp_ht_table_t *h, void *k); +void *ecp_ht_remove_kv(ecp_ht_table_t *h, void *k, void *v); +void *ecp_ht_search(ecp_ht_table_t *h, void *k); +void *ecp_ht_search_next(ecp_ht_table_t *h, void *k, void *v); +unsigned int ecp_ht_count(ecp_ht_table_t *h); void ecp_ht_itr_create(ecp_ht_itr_t *i, ecp_ht_table_t *h); int ecp_ht_itr_advance(ecp_ht_itr_t *i); int ecp_ht_itr_remove(ecp_ht_itr_t *i); int ecp_ht_itr_search(ecp_ht_itr_t *i, void *k); void *ecp_ht_itr_key(ecp_ht_itr_t *i); -ECPConnection *ecp_ht_itr_value(ecp_ht_itr_t *i); +void *ecp_ht_itr_value(ecp_ht_itr_t *i); diff --git a/ecp/src/ecp/htable/hashtable.c b/ecp/src/ecp/htable/hashtable.c index 4a48edb..13ed68c 100755 --- a/ecp/src/ecp/htable/hashtable.c +++ b/ecp/src/ecp/htable/hashtable.c @@ -212,6 +212,32 @@ hashtable_search(struct hashtable *h, void *k) } /*****************************************************************************/ +void * /* returns value associated with key k after value v */ +hashtable_search_next(struct hashtable *h, void *k, void *v) +{ + struct entry *e; + unsigned int hashvalue, index, f; + hashvalue = hashtable_hash(h,k); + index = indexFor(h->tablelength,hashvalue); + f = 0; + e = h->table[index]; + while (NULL != e) + { + if (f) + { + /* Check hash value to short circuit heavier comparison */ + if ((hashvalue == e->h) && (h->eqfn(k, e->k))) return e->v; + } + else if (v == e->v) + { + f = 1; + } + e = e->next; + } + return NULL; +} + +/*****************************************************************************/ void * /* returns value associated with key */ hashtable_remove(struct hashtable *h, void *k) { @@ -228,6 +254,17 @@ hashtable_remove(struct hashtable *h, void *k) return v; } +void * /* returns value associated with key */ +hashtable_remove_kv(struct hashtable *h, void *k, void *v) +{ + struct entry *e; + e = hashtable_remove_kv_static(h,k,v); + if (NULL == e) return NULL; + freekey(e->k); + free(e); + return v; +} + struct entry * /* returns hash table entry associated with key */ hashtable_remove_static(struct hashtable *h, void *k) { @@ -253,6 +290,30 @@ hashtable_remove_static(struct hashtable *h, void *k) return NULL; } +struct entry * /* returns hash table entry associated with key k and value v */ +hashtable_remove_kv_static(struct hashtable *h, void *k, void *v) +{ + struct entry *e; + struct entry **pE; + unsigned int hashvalue, index; + hashvalue = hashtable_hash(h,k); + index = indexFor(h->tablelength,hashvalue); + pE = &(h->table[index]); + e = *pE; + while (NULL != e) + { + if (v == e->v) + { + *pE = e->next; + h->entrycount--; + return e; + } + pE = &(e->next); + e = e->next; + } + return NULL; +} + /*****************************************************************************/ /* destroy */ void diff --git a/ecp/src/ecp/htable/hashtable.h b/ecp/src/ecp/htable/hashtable.h index 8faeafc..a5b3ab8 100755 --- a/ecp/src/ecp/htable/hashtable.h +++ b/ecp/src/ecp/htable/hashtable.h @@ -150,6 +150,9 @@ int fnname (struct hashtable *h, keytype *k, valuetype *v) \ void * hashtable_search(struct hashtable *h, void *k); +void * +hashtable_search_next(struct hashtable *h, void *k, void *v); + #define DEFINE_HASHTABLE_SEARCH(fnname, keytype, valuetype) \ valuetype * fnname (struct hashtable *h, keytype *k) \ { \ @@ -168,9 +171,15 @@ valuetype * fnname (struct hashtable *h, keytype *k) \ void * /* returns value */ hashtable_remove(struct hashtable *h, void *k); +void * /* returns value v */ +hashtable_remove_kv(struct hashtable *h, void *k, void *v); + struct entry * /* returns hash table entry */ hashtable_remove_static(struct hashtable *h, void *k); +struct entry * /* returns hash table entry */ +hashtable_remove_kv_static(struct hashtable *h, void *k, void *v); + #define DEFINE_HASHTABLE_REMOVE(fnname, keytype, valuetype) \ valuetype * fnname (struct hashtable *h, keytype *k) \ { \ diff --git a/ecp/src/ecp/htable/htable.c b/ecp/src/ecp/htable/htable.c index c0efebb..c22d2ab 100644 --- a/ecp/src/ecp/htable/htable.c +++ b/ecp/src/ecp/htable/htable.c @@ -1,9 +1,9 @@ #include <stdlib.h> -#include <core.h> -#include <cr.h> -#include <tr.h> -#include <ht.h> +#include <ecp/core.h> +#include <ecp/cr.h> +#include <ecp/tr.h> +#include <ecp/ht.h> ecp_ht_table_t *ecp_ht_create_keys(void) { return hashtable_create(1000, (unsigned int (*)(void *))ecp_ecdh_pub_hash, (int (*)(void *, void *))ecp_ecdh_pub_eq); @@ -17,7 +17,7 @@ void ecp_ht_destroy(ecp_ht_table_t *h) { hashtable_destroy(h, 0); } -int ecp_ht_insert(ecp_ht_table_t *h, void *k, ECPConnection *v) { +int ecp_ht_insert(ecp_ht_table_t *h, void *k, void *v) { int rv; rv = hashtable_insert(h, k, v); @@ -25,14 +25,26 @@ int ecp_ht_insert(ecp_ht_table_t *h, void *k, ECPConnection *v) { return ECP_OK; } -ECPConnection *ecp_ht_remove(ecp_ht_table_t *h, void *k) { +void *ecp_ht_remove(ecp_ht_table_t *h, void *k) { return hashtable_remove(h, k); } -ECPConnection *ecp_ht_search(ecp_ht_table_t *h, void *k) { +void *ecp_ht_remove_kv(ecp_ht_table_t *h, void *k, void *v) { + return hashtable_remove_kv(h, k, v); +} + +void *ecp_ht_search(ecp_ht_table_t *h, void *k) { return hashtable_search(h, k); } +void *ecp_ht_search_next(ecp_ht_table_t *h, void *k, void *v) { + return hashtable_search_next(h, k, v); +} + +unsigned int ecp_ht_count(ecp_ht_table_t *h) { + return hashtable_count(h); +} + void ecp_ht_itr_create(ecp_ht_itr_t *i, ecp_ht_table_t *h) { hashtable_iterator(i, h); } @@ -42,7 +54,7 @@ int ecp_ht_itr_advance(ecp_ht_itr_t *i) { rv = hashtable_iterator_advance(i); if (rv == 0) return ECP_ITR_END; - return rv; + return ECP_OK; } int ecp_ht_itr_remove(ecp_ht_itr_t *i) { @@ -65,6 +77,6 @@ void *ecp_ht_itr_key(ecp_ht_itr_t *i) { return hashtable_iterator_key(i); } -ECPConnection *ecp_ht_itr_value(ecp_ht_itr_t *i) { +void *ecp_ht_itr_value(ecp_ht_itr_t *i) { return hashtable_iterator_value(i); } diff --git a/ecp/src/ecp/timer.c b/ecp/src/ecp/timer.c index 445c343..38df398 100644 --- a/ecp/src/ecp/timer.c +++ b/ecp/src/ecp/timer.c @@ -37,13 +37,13 @@ int ecp_timer_push(ECPTimerItem *ti) { ECPConnection *conn = ti->conn; ECPTimer *timer = &conn->sock->timer; - ti->abstime = ecp_tm_abstime_ms(ti->timeout); + ti->abstime = ecp_tm_get_ms() + ti->timeout; #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&timer->mutex); pthread_mutex_lock(&conn->mutex); #endif - is_reg = ecp_conn_is_reg(conn); + is_reg = _ecp_conn_is_reg(conn); if (is_reg) conn->refcount++; #ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&conn->mutex); @@ -137,7 +137,7 @@ ecp_sts_t ecp_timer_exe(ECPSocket *sock) { ECPTimer *timer = &sock->timer; ECPTimerItem to_exec[ECP_MAX_TIMER]; int to_exec_size = 0; - ecp_sts_t now = ecp_tm_abstime_ms(0); + ecp_sts_t now = ecp_tm_get_ms(); #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&timer->mutex); diff --git a/ecp/src/ecp/tm.h b/ecp/src/ecp/tm.h index af84b66..45e5784 100644 --- a/ecp/src/ecp/tm.h +++ b/ecp/src/ecp/tm.h @@ -1,4 +1,5 @@ int ecp_tm_init(ECPContext *ctx); -ecp_sts_t ecp_tm_abstime_ms(ecp_sts_t msec); -void ecp_tm_sleep_ms(ecp_sts_t msec); +ecp_sts_t ecp_tm_get_s(void); +ecp_sts_t ecp_tm_get_ms(void); +void ecp_tm_sleep(ecp_sts_t msec); void ecp_tm_timer_set(ecp_sts_t next); diff --git a/ecp/src/ecp/tr.h b/ecp/src/ecp/tr.h index 263577c..6c99f15 100644 --- a/ecp/src/ecp/tr.h +++ b/ecp/src/ecp/tr.h @@ -2,7 +2,7 @@ int ecp_tr_init(ECPContext *ctx); unsigned int ecp_tr_addr_hash(ecp_tr_addr_t *addr); int ecp_tr_addr_eq(ecp_tr_addr_t *addr1, ecp_tr_addr_t *addr2); int ecp_tr_addr_set(ecp_tr_addr_t *addr, void *addr_s); -int ecp_tr_open(ECPSocket *sock, void *addr_s); +int ecp_tr_open(ECPSocket *sock, ecp_tr_addr_t *addr); void ecp_tr_close(ECPSocket *sock); ssize_t ecp_tr_send(ECPSocket *sock, ECPBuffer *packet, size_t pkt_size, ecp_tr_addr_t *addr, unsigned char flags); ssize_t ecp_tr_recv(ECPSocket *sock, ECPBuffer *packet, ecp_tr_addr_t *addr, int timeout); diff --git a/ecp/src/ecp/vconn/vconn.c b/ecp/src/ecp/vconn/vconn.c index 3d4dd29..f52b2f5 100644 --- a/ecp/src/ecp/vconn/vconn.c +++ b/ecp/src/ecp/vconn/vconn.c @@ -1,11 +1,11 @@ #include <stdlib.h> #include <string.h> -#include <core.h> -#include <tr.h> +#include <ecp/core.h> +#include <ecp/tr.h> #ifdef ECP_WITH_HTABLE -#include <ht.h> +#include <ecp/ht.h> #endif #include "vconn.h" @@ -13,9 +13,9 @@ #ifdef ECP_WITH_HTABLE -static int insert_key_next(ECPConnection *conn, unsigned char c_idx, ecp_ecdh_public_t *public) { +static int insert_key_next(ECPVConnInb *vconn, unsigned char c_idx, ecp_ecdh_public_t *public) { + ECPConnection *conn = &vconn->b; ECPSocket *sock = conn->sock; - ECPVConn *_conn = (ECPVConn *)conn; ECPDHPub *key = NULL; int rv = ECP_OK; @@ -25,11 +25,11 @@ static int insert_key_next(ECPConnection *conn, unsigned char c_idx, ecp_ecdh_pu pthread_mutex_lock(&conn->mutex); #endif - if (c_idx != _conn->key_next_curr) { + if (c_idx != vconn->key_next_curr) { unsigned char _c_idx; _c_idx = c_idx % ECP_MAX_NODE_KEY; - key = &_conn->key_next[_c_idx]; + key = &vconn->key_next[_c_idx]; if (key->valid && (memcmp(public, &key->public, sizeof(key->public)) == 0)) { key = NULL; @@ -54,7 +54,7 @@ static int insert_key_next(ECPConnection *conn, unsigned char c_idx, ecp_ecdh_pu rv = ecp_ht_insert(sock->conn_table.keys, &key->public, conn); if (!rv) { key->valid = 1; - _conn->key_next_curr = c_idx; + vconn->key_next_curr = c_idx; } #ifdef ECP_WITH_PTHREAD @@ -67,23 +67,37 @@ static int insert_key_next(ECPConnection *conn, unsigned char c_idx, ecp_ecdh_pu } static ssize_t handle_next(ECPConnection *conn, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) { + ECPVConnInb *vconn = (ECPVConnInb *)conn; ECPSocket *sock = conn->sock; + int is_open; if (msg_size < ECP_SIZE_ECDH_PUB) return ECP_ERR_SIZE; if (ecp_conn_is_outb(conn)) return ECP_ERR; #ifdef ECP_WITH_PTHREAD + pthread_mutex_lock(&conn->mutex); +#endif + + is_open = _ecp_conn_is_open(conn); + +#ifdef ECP_WITH_PTHREAD + pthread_mutex_unlock(&conn->mutex); +#endif + + if (is_open) return ECP_ERR_NEXT; + +#ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&sock->conn_table.mutex); #endif - conn->next = ecp_ht_search(sock->conn_table.keys, (ecp_ecdh_public_t *)msg); - if (conn->next) ecp_conn_refcount_inc(conn->next); + vconn->next = ecp_ht_search(sock->conn_table.keys, (ecp_ecdh_public_t *)msg); + if (vconn->next) ecp_conn_refcount_inc(vconn->next); #ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&sock->conn_table.mutex); #endif - if (conn->next == NULL) return ECP_ERR_NEXT; + if (vconn->next == NULL) return ECP_ERR_NEXT; return ECP_SIZE_ECDH_PUB; } @@ -105,13 +119,15 @@ static ssize_t handle_relay(ECPConnection *conn, unsigned char *msg, size_t msg_ switch (conn->type) { /* forward message */ case ECP_CTYPE_VCONN: { + ECPVConnInb *vconn = (ECPVConnInb *)conn; + if (ecp_conn_is_outb(conn)) return ECP_ERR; - conn_next = conn->next; + conn_next = vconn->next; if (conn_next) { _idx = (idx & 0x0F); - _rv = insert_key_next(conn, _idx, (ecp_ecdh_public_t *)(msg+ECP_SIZE_PROTO+1)); + _rv = insert_key_next(vconn, _idx, (ecp_ecdh_public_t *)(msg+ECP_SIZE_PROTO+1)); if (_rv) return _rv; } @@ -167,6 +183,7 @@ static ssize_t handle_relay(ECPConnection *conn, unsigned char *msg, size_t msg_ static ssize_t handle_exec(ECPConnection *conn, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) { ECPBuffer *packet; + ssize_t rv; if (bufs == NULL) return ECP_ERR; @@ -174,7 +191,8 @@ static ssize_t handle_exec(ECPConnection *conn, unsigned char *msg, size_t msg_s if (packet->size < msg_size) return ECP_ERR_SIZE; memcpy(packet->buffer, msg, msg_size); - return ecp_pkt_handle(conn->sock, conn, NULL, bufs, msg_size); + rv = ecp_pkt_handle(conn->sock, conn, NULL, bufs, msg_size); + return rv; } ssize_t ecp_vconn_pack_parent(ECPConnection *conn, ECPBuffer *payload, ECPBuffer *packet, size_t pkt_size, ecp_tr_addr_t *addr) { @@ -197,70 +215,77 @@ ssize_t ecp_vconn_pack_parent(ECPConnection *conn, ECPBuffer *payload, ECPBuffer return rv; } -static int vconn_create(ECPConnection vconn[], ecp_ecdh_public_t keys[], size_t vconn_size, ECPSocket *sock) { +void ecp_vconn_init(ECPVConnOutb vconn[], ecp_ecdh_public_t keys[], size_t vconn_size, ECPSocket *sock) { ECPDHPub key; - int i, j; - int rv; + int i; key.valid = 1; for (i=0; i<vconn_size; i++) { - rv = ecp_conn_create(&vconn[i], sock, ECP_CTYPE_VCONN); - if (rv) { - for (j=0; j<i; j++) { - ecp_conn_destroy(&vconn[j]); - } - return rv; - } memcpy(&key.public, &keys[i], sizeof(keys[i])); - ecp_conn_set_remote_key(&vconn[i], &key); + ecp_conn_init(&vconn[i].b, sock, ECP_CTYPE_VCONN); + ecp_conn_set_remote_key(&vconn[i].b, &key); + if (i < vconn_size - 1) { + vconn[i].next = &vconn[i + 1].b; + } else { + vconn[i].next = NULL; + } } - - return ECP_OK; } -int ecp_vconn_create(ECPConnection vconn[], ecp_ecdh_public_t keys[], size_t vconn_size, ECPConnection *conn) { - ECPConnection *_conn; - unsigned short pcount; - int rv; +#ifdef ECP_WITH_HTABLE - _conn = conn; - pcount = vconn_size; +void ecp_vconn_init_inb(ECPVConnInb *vconn, ECPSocket *sock) { + ECPConnection *conn = &vconn->b; - if (pcount > ECP_MAX_PARENT) return ECP_ERR_MAX_PARENT; + ecp_conn_init(conn, sock, ECP_CTYPE_VCONN); + vconn->next = NULL; + memset(vconn->key_next, 0, sizeof(vconn->key_next)); + vconn->key_next_curr = ECP_ECDH_IDX_INV; +} - rv = vconn_create(vconn, keys, vconn_size, conn->sock); - if (rv) return rv; +#endif /* ECP_WITH_HTABLE */ - while (pcount) { - _conn->parent = &vconn[pcount-1]; - _conn->parent->next = _conn; - _conn->pcount = pcount; +int ecp_vconn_open(ECPVConnOutb *vconn, ECPConnection *conn, ECPNode *node) { + ECPVConnOutb *_vconn; + int rv; - _conn = _conn->parent; - pcount--; + _vconn = vconn; + while (_vconn->next) { + _vconn = (ECPVConnOutb *)_vconn->next; } - return ECP_OK; + _vconn->next = conn; + if (node) { + ecp_conn_set_remote_key(conn, &node->key_perma); + ecp_conn_set_remote_addr(&vconn->b, &node->addr); + } + + rv = _ecp_conn_open(&vconn->b, NULL, NULL, 1); + return rv; } -#ifdef ECP_WITH_HTABLE +int ecp_vconn_handle_open(ECPConnection *conn, ECP2Buffer *bufs) { + int rv = ECP_OK; -int ecp_vconn_create_inb(ECPVConn *conn, ECPSocket *sock) { - ECPConnection *_conn = &conn->b; - int rv; + if (ecp_conn_is_outb(conn)) { + ECPVConnOutb *vconn = (ECPVConnOutb *)conn; - rv = ecp_conn_create_inb(_conn, sock, ECP_CTYPE_VCONN); - if (rv) return rv; + if (vconn->next == NULL) return ECP_ERR; - memset(conn->key_next, 0, sizeof(conn->key_next)); - conn->key_next_curr = ECP_ECDH_IDX_INV; + /* we should release incoming packet before sending next open packet */ + ecp_tr_release(bufs->packet, 1); + rv = _ecp_conn_open(vconn->next, conn, NULL, 1); + if (rv) rv = ECP_ERR_NEXT; + } - return ECP_OK; + return rv; } -void ecp_vconn_destroy(ECPConnection *conn) { +#ifdef ECP_WITH_HTABLE + +void ecp_vconn_handle_close(ECPConnection *conn) { + ECPVConnInb *vconn = (ECPVConnInb *)conn; ECPSocket *sock = conn->sock; - ECPVConn *_conn = (ECPVConn *)conn; int i; if (ecp_conn_is_outb(conn)) return; @@ -272,7 +297,7 @@ void ecp_vconn_destroy(ECPConnection *conn) { for (i=0; i<ECP_MAX_NODE_KEY; i++) { ECPDHPub *key; - key = &_conn->key_next[i]; + key = &vconn->key_next[i]; if (key->valid) { ecp_ht_remove(sock->conn_table.keys, &key->public); } @@ -282,77 +307,82 @@ void ecp_vconn_destroy(ECPConnection *conn) { pthread_mutex_unlock(&sock->conn_table.mutex); #endif - if (conn->next) ecp_conn_refcount_dec(conn->next); + if (vconn->next) ecp_conn_refcount_dec(vconn->next); } #endif /* ECP_WITH_HTABLE */ -int ecp_vconn_open(ECPConnection *conn, ECPNode *node) { - ECPConnection *vconn0; - int rv; +ssize_t ecp_vconn_handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) { + ssize_t rv; - vconn0 = conn; - while (vconn0->parent) { - vconn0 = vconn0->parent; - } + switch (mtype) { +#ifdef ECP_WITH_HTABLE + case ECP_MTYPE_NEXT: + rv = handle_next(conn, msg, msg_size, bufs); + break; - if (node) { - ecp_conn_set_remote_key(conn, &node->key_perma); - ecp_conn_set_remote_addr(vconn0, &node->addr); + case ECP_MTYPE_RELAY: + rv = handle_relay(conn, msg, msg_size, bufs); + break; +#endif + + case ECP_MTYPE_EXEC: + rv = handle_exec(conn, msg, msg_size, bufs); + break; + + default: + rv = ECP_ERR_MTYPE; + break; } - rv = ecp_conn_open(vconn0, NULL); return rv; } -static int vconn_handle_open(ECPConnection *conn, ECP2Buffer *bufs) { - int rv = ECP_OK; +ssize_t ecp_vconn_send_open_req(ECPConnection *conn, unsigned char *cookie) { + ECPVConnOutb *vconn = (ECPVConnOutb *)conn; + ECPDHPub key_next; + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_SIZE_PKT_BUF(2+ECP_SIZE_VBOX, ECP_MTYPE_OPEN_REQ, conn)+ECP_SIZE_COOKIE+ECP_SIZE_PLD(ECP_SIZE_ECDH_PUB, ECP_MTYPE_NEXT)]; + unsigned char pld_buf[ECP_SIZE_PLD_BUF(2+ECP_SIZE_VBOX, ECP_MTYPE_OPEN_REQ, conn)+ECP_SIZE_COOKIE+ECP_SIZE_PLD(ECP_SIZE_ECDH_PUB, ECP_MTYPE_NEXT)]; + unsigned char *msg; + unsigned char *_pld_buf; + size_t _pld_size; + ssize_t rv; + int _rv; - if (ecp_conn_is_outb(conn) && conn->next) { - ecp_tr_release(bufs->packet, 1); - rv = ecp_conn_open(conn->next, NULL); - if (rv) ecp_err_handle(conn->next, ECP_MTYPE_INIT_REQ, rv); - } + if (vconn->next == NULL) return ECP_ERR_NEXT; - return rv;; -} + _rv = ecp_conn_dhkey_get_remote(vconn->next, ECP_ECDH_IDX_PERMA, &key_next); + if (_rv) return _rv; -int ecp_vlink_create(ECPConnection *conn, ECPSocket *sock) { - int rv; + packet.buffer = pkt_buf; + packet.size = sizeof(pkt_buf); + payload.buffer = pld_buf; + payload.size = sizeof(pld_buf); - rv = ecp_conn_create(conn, sock, ECP_CTYPE_VLINK); - if (!rv) ecp_conn_set_flags(conn, ECP_CONN_FLAG_VBOX); - return rv; -} + rv = ecp_write_open_req(conn, &payload); + if (rv < 0) return rv; -#ifdef ECP_WITH_HTABLE + _pld_buf = payload.buffer + rv; + _pld_size = payload.size - rv; -int ecp_vlink_create_inb(ECPConnection *conn, ECPSocket *sock) { - int rv; + ecp_pld_set_type(_pld_buf, _pld_size, ECP_MTYPE_NEXT); + msg = ecp_pld_get_msg(_pld_buf, _pld_size); + memcpy(msg, &key_next.public, ECP_SIZE_ECDH_PUB); - rv = ecp_conn_create_inb(conn, sock, ECP_CTYPE_VLINK); - if (!rv) ecp_conn_set_flags(conn, ECP_CONN_FLAG_VBOX); + rv = ecp_pld_send_wcookie(conn, &packet, &payload, rv+ECP_SIZE_PLD(ECP_SIZE_ECDH_PUB, ECP_MTYPE_NEXT), 0, cookie); return rv; } -void ecp_vlink_destroy(ECPConnection *conn) { - ECPSocket *sock = conn->sock; - ECPDHPub *key = &conn->remote.key_perma; - - if (key->valid) { -#ifdef ECP_WITH_PTHREAD - pthread_mutex_lock(&sock->conn_table.mutex); -#endif - - ecp_ht_remove(sock->conn_table.keys, &key->public); - -#ifdef ECP_WITH_PTHREAD - pthread_mutex_unlock(&sock->conn_table.mutex); -#endif - } +void ecp_vlink_init(ECPConnection *conn, ECPSocket *sock) { + ecp_conn_init(conn, sock, ECP_CTYPE_VLINK); + ecp_conn_set_flags(conn, ECP_CONN_FLAG_VBOX); } -static int vlink_handle_open(ECPConnection *conn) { +#ifdef ECP_WITH_HTABLE + +int ecp_vlink_handle_open(ECPConnection *conn, ECP2Buffer *bufs) { ECPSocket *sock = conn->sock; ECPDHPub *key; int rv = ECP_OK; @@ -393,55 +423,30 @@ static int vlink_handle_open(ECPConnection *conn) { return rv; } -#endif /* ECP_WITH_HTABLE */ - -int ecp_vconn_handle_open(ECPConnection *conn, ECP2Buffer *bufs) { - int rv; - - switch (conn->type) { - case ECP_CTYPE_VCONN: - rv = vconn_handle_open(conn, bufs); - break; +void ecp_vlink_handle_close(ECPConnection *conn) { + ECPSocket *sock = conn->sock; + ECPDHPub *key = &conn->remote.key_perma; - case ECP_CTYPE_VLINK: -#ifdef ECP_WITH_HTABLE - rv = vlink_handle_open(conn); -#else - rv = ECP_OK; + if (key->valid) { +#ifdef ECP_WITH_PTHREAD + pthread_mutex_lock(&sock->conn_table.mutex); #endif - break; - - default: - rv = ECP_ERR_CTYPE; - break; - } - - return rv; -} -void ecp_vconn_handle_close(ECPConnection *conn) { - switch (conn->type) { -#ifdef ECP_WITH_HTABLE - case ECP_CTYPE_VCONN: - ecp_vconn_destroy(conn); - break; + ecp_ht_remove(sock->conn_table.keys, &key->public); - case ECP_CTYPE_VLINK: - ecp_vlink_destroy(conn); - break; +#ifdef ECP_WITH_PTHREAD + pthread_mutex_unlock(&sock->conn_table.mutex); #endif } } -ssize_t ecp_vconn_handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) { +#endif /* ECP_WITH_HTABLE */ + +ssize_t ecp_vlink_handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) { ssize_t rv; switch (mtype) { #ifdef ECP_WITH_HTABLE - case ECP_MTYPE_NEXT: - rv = handle_next(conn, msg, msg_size, bufs); - break; - case ECP_MTYPE_RELAY: rv = handle_relay(conn, msg, msg_size, bufs); break; @@ -459,42 +464,18 @@ ssize_t ecp_vconn_handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char m return rv; } -ssize_t ecp_vconn_send_open_req(ECPConnection *conn, unsigned char *cookie) { - if (conn->type == ECP_CTYPE_VCONN) { - ECPDHPub key_next; - ECPBuffer packet; - ECPBuffer payload; - unsigned char pkt_buf[ECP_SIZE_PKT_BUF(2+ECP_SIZE_VBOX, ECP_MTYPE_OPEN_REQ, conn)+ECP_SIZE_COOKIE+ECP_SIZE_PLD(ECP_SIZE_ECDH_PUB, ECP_MTYPE_NEXT)]; - unsigned char pld_buf[ECP_SIZE_PLD_BUF(2+ECP_SIZE_VBOX, ECP_MTYPE_OPEN_REQ, conn)+ECP_SIZE_COOKIE+ECP_SIZE_PLD(ECP_SIZE_ECDH_PUB, ECP_MTYPE_NEXT)]; - unsigned char *msg; - unsigned char *_pld_buf; - size_t _pld_size; - ssize_t rv; - int _rv; - - if (conn->next == NULL) return ECP_ERR_NEXT; - - _rv = ecp_conn_dhkey_get_remote(conn->next, ECP_ECDH_IDX_PERMA, &key_next); - if (_rv) return _rv; - - packet.buffer = pkt_buf; - packet.size = sizeof(pkt_buf); - payload.buffer = pld_buf; - payload.size = sizeof(pld_buf); - - rv = ecp_write_open_req(conn, &payload); - if (rv < 0) return rv; - - _pld_buf = payload.buffer + rv; - _pld_size = payload.size - rv; - - ecp_pld_set_type(_pld_buf, _pld_size, ECP_MTYPE_NEXT); - msg = ecp_pld_get_msg(_pld_buf, _pld_size); - memcpy(msg, &key_next.public, ECP_SIZE_ECDH_PUB); - - rv = ecp_pld_send_wcookie(conn, &packet, &payload, rv+ECP_SIZE_PLD(ECP_SIZE_ECDH_PUB, ECP_MTYPE_NEXT), 0, cookie); - return rv; - } else { - return ecp_send_open_req(conn, cookie); - } +int ecp_vconn_handler_init(ECPContext *ctx, ECPConnHandler *vconn_handler) { + int rv; + + ecp_conn_handler_init(vconn_handler, ecp_vconn_handle_msg, ecp_vconn_handle_open, ecp_vconn_handle_close, ecp_vconn_send_open_req); + rv = ecp_ctx_set_handler(ctx, ECP_CTYPE_VCONN, vconn_handler); + return rv; +} + +int ecp_vlink_handler_init(ECPContext *ctx, ECPConnHandler *vlink_handler) { + int rv; + + ecp_conn_handler_init(vlink_handler, ecp_vlink_handle_msg, ecp_vlink_handle_open, ecp_vlink_handle_close, NULL); + rv = ecp_ctx_set_handler(ctx, ECP_CTYPE_VLINK, vlink_handler); + return rv; } diff --git a/ecp/src/ecp/vconn/vconn.h b/ecp/src/ecp/vconn/vconn.h index 8871ded..62275bf 100644 --- a/ecp/src/ecp/vconn/vconn.h +++ b/ecp/src/ecp/vconn/vconn.h @@ -5,29 +5,39 @@ #define ECP_MTYPE_EXEC 0x02 #define ECP_MTYPE_RELAY 0x01 -/* inbound only */ -typedef struct ECPVConn { +typedef struct ECPVConnInb { ECPConnection b; + ECPConnection *next; ECPDHPub key_next[ECP_MAX_NODE_KEY]; unsigned char key_next_curr; -} ECPVConn; +} ECPVConnInb; + +typedef struct ECPVConnOutb { + ECPConnection b; + ECPConnection *next; +} ECPVConnOutb; ssize_t ecp_vconn_pack_parent(ECPConnection *conn, ECPBuffer *payload, ECPBuffer *packet, size_t pkt_size, ecp_tr_addr_t *addr); -int ecp_vconn_create(ECPConnection vconn[], ecp_ecdh_public_t keys[], size_t vconn_size, ECPConnection *conn); +void ecp_vconn_init(ECPVConnOutb vconn[], ecp_ecdh_public_t keys[], size_t vconn_size, ECPSocket *sock); #ifdef ECP_WITH_HTABLE -int ecp_vconn_create_inb(ECPVConn *conn, ECPSocket *sock); -void ecp_vconn_destroy(ECPConnection *conn); +void ecp_vconn_init_inb(ECPVConnInb *vconn, ECPSocket *sock); #endif -int ecp_vconn_open(ECPConnection *conn, ECPNode *node); - -int ecp_vlink_create(ECPConnection *conn, ECPSocket *sock); +int ecp_vconn_open(ECPVConnOutb *vconn, ECPConnection *conn, ECPNode *node); #ifdef ECP_WITH_HTABLE -int ecp_vlink_create_inb(ECPConnection *conn, ECPSocket *sock); -void ecp_vlink_destroy(ECPConnection *conn); -#endif - int ecp_vconn_handle_open(ECPConnection *conn, ECP2Buffer *bufs); void ecp_vconn_handle_close(ECPConnection *conn); +#endif ssize_t ecp_vconn_handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs); ssize_t ecp_vconn_send_open_req(ECPConnection *conn, unsigned char *cookie); + + +void ecp_vlink_init(ECPConnection *conn, ECPSocket *sock); +#ifdef ECP_WITH_HTABLE +int ecp_vlink_handle_open(ECPConnection *conn, ECP2Buffer *bufs); +void ecp_vlink_handle_close(ECPConnection *conn); +#endif +ssize_t ecp_vlink_handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs); + +int ecp_vconn_handler_init(ECPContext *ctx, ECPConnHandler *vconn_handler); +int ecp_vlink_handler_init(ECPContext *ctx, ECPConnHandler *vlink_handler); diff --git a/ecp/src/platform/fe310/time.c b/ecp/src/platform/fe310/time.c index 1e5eb17..4bf530c 100644 --- a/ecp/src/platform/fe310/time.c +++ b/ecp/src/platform/fe310/time.c @@ -1,12 +1,12 @@ #include <stdlib.h> -#include <core.h> -#include <tr.h> +#include <ecp/core.h> +#include <ecp/tm.h> #include <eos/eos.h> -#include <eos/net.h> #include <eos/event.h> -#include <eos/timer.h> +#include <eos/soc/timer.h> +#include <eos/dev/net.h> extern ECPSocket *_ecp_tr_sock; @@ -23,11 +23,15 @@ int ecp_tm_init(ECPContext *ctx) { return ECP_OK; } -ecp_sts_t ecp_tm_abstime_ms(ecp_sts_t msec) { - return eos_time_get_tick() * 1000 / EOS_TIMER_RTC_FREQ + msec; +ecp_sts_t ecp_tm_get_s(void) { + return eos_time_get_tick() / EOS_TIMER_RTC_FREQ; } -void ecp_tm_sleep_ms(ecp_sts_t msec) { +ecp_sts_t ecp_tm_get_ms(void) { + return eos_time_get_tick() * 1000 / EOS_TIMER_RTC_FREQ; +} + +void ecp_tm_sleep(ecp_sts_t msec) { eos_time_sleep(msec); } diff --git a/ecp/src/platform/fe310/transport.c b/ecp/src/platform/fe310/transport.c index 2640ecf..bc758c9 100644 --- a/ecp/src/platform/fe310/transport.c +++ b/ecp/src/platform/fe310/transport.c @@ -2,11 +2,11 @@ #include <string.h> #include <stdio.h> -#include <core.h> -#include <tr.h> +#include <ecp/core.h> +#include <ecp/tr.h> #include <eos/eos.h> -#include <eos/net.h> +#include <eos/dev/net.h> ECPSocket *_ecp_tr_sock = NULL; unsigned char pld_buf[ECP_MAX_PLD]; @@ -65,7 +65,7 @@ int ecp_tr_addr_set(ecp_tr_addr_t *addr, void *addr_s) { return ECP_ERR; } -int ecp_tr_open(ECPSocket *sock, void *addr_s) { +int ecp_tr_open(ECPSocket *sock, ecp_tr_addr_t *addr) { sock->sock = eos_sock_open_udp(packet_handler, NULL); if (sock->sock < 0) { sock->sock = 0; diff --git a/ecp/src/platform/fe310/transport.h b/ecp/src/platform/fe310/transport.h index 1e0c526..52ece15 100644 --- a/ecp/src/platform/fe310/transport.h +++ b/ecp/src/platform/fe310/transport.h @@ -1,4 +1,4 @@ -#include <eos/sock.h> +#include <eos/net/sock.h> typedef struct EOSNetAddr ecp_tr_addr_t; typedef int ecp_tr_sock_t; diff --git a/ecp/src/platform/posix/features.mk b/ecp/src/platform/posix/features.mk index 2ce8220..ea5e44c 100644 --- a/ecp/src/platform/posix/features.mk +++ b/ecp/src/platform/posix/features.mk @@ -1,7 +1,8 @@ with_pthread = yes with_htable = yes with_vconn = yes -with_dirsrv = yes +with_frag = yes with_rbuf = yes with_msgq = yes +with_dir = yes with_debug = yes diff --git a/ecp/src/platform/posix/time.c b/ecp/src/platform/posix/time.c index efee4f8..49a85e7 100644 --- a/ecp/src/platform/posix/time.c +++ b/ecp/src/platform/posix/time.c @@ -1,25 +1,30 @@ #include <stdlib.h> #include <unistd.h> +#include <time.h> #include <sys/time.h> -#include <core.h> -#include <tm.h> +#include <ecp/core.h> +#include <ecp/tm.h> int ecp_tm_init(ECPContext *ctx) { return ECP_OK; } -ecp_sts_t ecp_tm_abstime_ms(ecp_sts_t msec) { +ecp_sts_t ecp_tm_get_s(void) { + return time(NULL); +} + +ecp_sts_t ecp_tm_get_ms(void) { struct timeval tv; ecp_sts_t ms_now; gettimeofday(&tv, NULL); ms_now = tv.tv_sec * 1000 + tv.tv_usec / 1000; - return ms_now + msec; + return ms_now; } -void ecp_tm_sleep_ms(ecp_sts_t msec) { - usleep(msec*1000); +void ecp_tm_sleep(ecp_sts_t msec) { + usleep(msec * 1000); } void ecp_tm_timer_set(ecp_sts_t next) {} diff --git a/ecp/src/platform/posix/transport.c b/ecp/src/platform/posix/transport.c index ebfbc7b..51f311a 100644 --- a/ecp/src/platform/posix/transport.c +++ b/ecp/src/platform/posix/transport.c @@ -6,8 +6,8 @@ #include <arpa/inet.h> #include <poll.h> -#include <core.h> -#include <tr.h> +#include <ecp/core.h> +#include <ecp/tr.h> #define MAX_ADDR_STR 32 @@ -49,20 +49,15 @@ int ecp_tr_addr_set(ecp_tr_addr_t *addr, void *addr_s) { return 0; } -int ecp_tr_open(ECPSocket *sock, void *addr_s) { +int ecp_tr_open(ECPSocket *sock, ecp_tr_addr_t *addr) { struct sockaddr_in _myaddr; int rv; memset((char *)&_myaddr, 0, sizeof(_myaddr)); _myaddr.sin_family = AF_INET; - if (addr_s) { - ecp_tr_addr_t addr; - - rv = ecp_tr_addr_set(&addr, addr_s); - if (rv) return rv; - - memcpy((void *)&_myaddr.sin_addr, addr.host, sizeof(addr.host)); - _myaddr.sin_port = addr.port; + if (addr) { + memcpy((void *)&_myaddr.sin_addr, addr->host, sizeof(addr->host)); + _myaddr.sin_port = addr->port; } else { _myaddr.sin_addr.s_addr = htonl(INADDR_ANY); _myaddr.sin_port = htons(0); diff --git a/ecp/test/Makefile b/ecp/test/Makefile index 50a4202..b2fd6f8 100644 --- a/ecp/test/Makefile +++ b/ecp/test/Makefile @@ -1,13 +1,15 @@ src_dir = ../src include $(src_dir)/ecp/common.mk CFLAGS += -I../util -Wno-int-to-void-pointer-cast +dep = ../build-posix/*.a ../util/libecputil.a + +target = basic client server vc_inb vc_outb vcs vc_server vc_client -dep=../build-posix/*.a ../util/libecputil.a %.o: %.c $(CC) $(CFLAGS) -c $< -all: basic client server vcs vc_server vc_client +all: $(target) basic: basic.o init.o $(dep) $(CC) -o $@ $< init.o $(dep) $(LDFLAGS) @@ -18,6 +20,12 @@ client: client.o init.o $(dep) server: server.o init.o $(dep) $(CC) -o $@ $< init.o $(dep) $(LDFLAGS) +vc_inb: vc_inb.o vc_common.o init_vconn.o $(dep) + $(CC) -o $@ $< vc_common.o init_vconn.o $(dep) $(LDFLAGS) + +vc_outb: vc_outb.o vc_common.o init_vconn.o $(dep) + $(CC) -o $@ $< vc_common.o init_vconn.o $(dep) $(LDFLAGS) + vcs: vcs.o init_vconn.o $(dep) $(CC) -o $@ $< init_vconn.o $(dep) $(LDFLAGS) @@ -29,4 +37,4 @@ vc_client: vc_client.o init_vconn.o $(dep) clean: rm -f *.o - rm -f basic dir client server echo frag stress vcs vc_server vc_client vc_client_t voip + rm -f $(target) diff --git a/ecp/test/basic.c b/ecp/test/basic.c index b266a07..637a829 100644 --- a/ecp/test/basic.c +++ b/ecp/test/basic.c @@ -2,7 +2,9 @@ #include <unistd.h> #include <stdio.h> -#include <core.h> +#include <ecp/core.h> + +#include "init.h" ECPContext ctx_s; ECPSocket sock_s; @@ -43,6 +45,7 @@ static ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t seq, unsigned char mt } int main(int argc, char *argv[]) { + ecp_tr_addr_t addr; ECPDHKey key_perma_c; ECPDHKey key_perma_s; ECPNode node; @@ -53,7 +56,7 @@ int main(int argc, char *argv[]) { printf("ecp_init RV:%d\n", rv); ecp_conn_handler_init(&handler_s, handle_msg_s, NULL, NULL, NULL); - ecp_ctx_set_handler(&ctx_s, &handler_s, CTYPE_TEST); + ecp_ctx_set_handler(&ctx_s, CTYPE_TEST, &handler_s); rv = ecp_dhkey_gen(&key_perma_s); printf("ecp_dhkey_gen RV:%d\n", rv); @@ -61,7 +64,10 @@ int main(int argc, char *argv[]) { rv = ecp_sock_create(&sock_s, &ctx_s, &key_perma_s); printf("ecp_sock_create RV:%d\n", rv); - rv = ecp_sock_open(&sock_s, "0.0.0.0:3000"); + rv = ecp_addr_init(&addr, "0.0.0.0:3000"); + printf("ecp_addr_init RV:%d\n", rv); + + rv = ecp_sock_open(&sock_s, &addr); printf("ecp_sock_open RV:%d\n", rv); rv = ecp_start_receiver(&sock_s); @@ -72,7 +78,7 @@ int main(int argc, char *argv[]) { printf("ecp_init RV:%d\n", rv); ecp_conn_handler_init(&handler_c, handle_msg_c, handle_open_c, NULL, NULL); - ecp_ctx_set_handler(&ctx_c, &handler_c, CTYPE_TEST); + ecp_ctx_set_handler(&ctx_c, CTYPE_TEST, &handler_c); rv = ecp_dhkey_gen(&key_perma_c); printf("ecp_dhkey_gen RV:%d\n", rv); @@ -86,12 +92,11 @@ int main(int argc, char *argv[]) { rv = ecp_start_receiver(&sock_c); printf("ecp_start_receiver RV:%d\n", rv); - rv = ecp_node_init(&node, &key_perma_s.public, "127.0.0.1:3000"); - printf("ecp_node_init RV:%d\n", rv); - - rv = ecp_conn_create(&conn, &sock_c, CTYPE_TEST); - printf("ecp_conn_create RV:%d\n", rv); + ecp_node_init(&node, &key_perma_s.public, NULL); + rv = ecp_node_set_addr(&node, "127.0.0.1:3000"); + printf("ecp_node_set_addr RV:%d\n", rv); + ecp_conn_init(&conn, &sock_c, CTYPE_TEST); rv = ecp_conn_open(&conn, &node); printf("ecp_conn_open RV:%d\n", rv); diff --git a/ecp/test/client.c b/ecp/test/client.c index d526f76..52a8587 100644 --- a/ecp/test/client.c +++ b/ecp/test/client.c @@ -3,10 +3,12 @@ #include <unistd.h> #include <stdio.h> -#include <core.h> +#include <ecp/core.h> #include <util.h> +#include "init.h" + ECPContext ctx; ECPSocket sock; ECPConnHandler handler; @@ -48,7 +50,7 @@ int main(int argc, char *argv[]) { printf("ecp_init RV:%d\n", rv); ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); - ecp_ctx_set_handler(&ctx, &handler, CTYPE_TEST); + ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); rv = ecp_dhkey_gen(&key_perma); printf("ecp_dhkey_gen RV:%d\n", rv); @@ -62,15 +64,14 @@ int main(int argc, char *argv[]) { rv = ecp_start_receiver(&sock); printf("ecp_start_receiver RV:%d\n", rv); - rv = ecp_conn_create(&conn, &sock, CTYPE_TEST); - printf("ecp_conn_create RV:%d\n", rv); - rv = ecp_util_load_pub(&node_pub, argv[2]); printf("ecp_util_load_pub RV:%d\n", rv); - rv = ecp_node_init(&node, &node_pub, argv[1]); - printf("ecp_node_init RV:%d\n", rv); + ecp_node_init(&node, &node_pub, NULL); + rv = ecp_node_set_addr(&node, argv[1]); + printf("ecp_node_set_addr RV:%d\n", rv); + ecp_conn_init(&conn, &sock, CTYPE_TEST); rv = ecp_conn_open(&conn, &node); printf("ecp_conn_open RV:%d\n", rv); diff --git a/ecp/test/init.c b/ecp/test/init.c index f4b033a..b75b2ee 100644 --- a/ecp/test/init.c +++ b/ecp/test/init.c @@ -1,34 +1,27 @@ #include <stdlib.h> #include <stdio.h> -#include <core.h> +#include <ecp/core.h> -void handle_err(ECPConnection *conn, unsigned char mtype, int err) { +static void handle_err(ECPConnection *conn, unsigned char mtype, int err) { printf("ERR: CTYPE:%d MTYPE:%x ERR:%d\n", conn->type, mtype, err); } -static ECPConnection *conn_alloc(ECPSocket *sock, unsigned char type) { +static ECPConnection *conn_new(ECPSocket *sock, unsigned char type) { ECPConnection *conn; - int rv; conn = malloc(sizeof(ECPConnection)); - if (conn == NULL) return NULL; - - rv = ecp_conn_create_inb(conn, sock, type); - if (rv) { - free(conn); - return NULL; - } + if (conn) ecp_conn_init(conn, sock, type); return conn; } static void conn_free(ECPConnection *conn) { - free(conn); + if (ecp_conn_is_gc(conn)) free(conn); } int ecp_init(ECPContext *ctx) { int rv; - rv = ecp_ctx_init(ctx, handle_err, NULL, conn_alloc, conn_free); + rv = ecp_ctx_init(ctx, handle_err, conn_new, conn_free); return rv; } diff --git a/ecp/test/init.h b/ecp/test/init.h new file mode 100644 index 0000000..a1a21d1 --- /dev/null +++ b/ecp/test/init.h @@ -0,0 +1 @@ +int ecp_init(ECPContext *ctx);
\ No newline at end of file diff --git a/ecp/test/init_vconn.c b/ecp/test/init_vconn.c index 88b6fa0..b690bec 100644 --- a/ecp/test/init_vconn.c +++ b/ecp/test/init_vconn.c @@ -2,50 +2,59 @@ #include <unistd.h> #include <stdio.h> -#include <core.h> -#include <vconn/vconn.h> +#include <ecp/core.h> +#include <ecp/vconn/vconn.h> -void handle_err(ECPConnection *conn, unsigned char mtype, int err) { +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 ECPConnection *conn_alloc(ECPSocket *sock, unsigned char type) { - ECPConnection *conn; - int rv; +static ECPConnection *conn_new(ECPSocket *sock, unsigned char type) { + ECPConnection *conn = NULL; switch (type) { - case ECP_CTYPE_VCONN: - conn = malloc(sizeof(ECPVConn)); - if (conn) rv = ecp_vconn_create_inb((ECPVConn *)conn, sock); + case ECP_CTYPE_VCONN: { + ECPVConnInb *_conn; + + _conn = malloc(sizeof(ECPVConnInb)); + if (_conn) { + ecp_vconn_init_inb(_conn, sock); + conn = &_conn->b; + } break; + } - case ECP_CTYPE_VLINK: + case ECP_CTYPE_VLINK: { conn = malloc(sizeof(ECPConnection)); - if (conn) rv = ecp_vlink_create_inb(conn, sock); + if (conn) ecp_vlink_init(conn, sock); break; + } - default: + default: { conn = malloc(sizeof(ECPConnection)); - if (conn) rv = ecp_conn_create_inb(conn, sock, type); + if (conn) ecp_conn_init(conn, sock, type); break; - } - - if (conn == NULL) return NULL; - if (rv) { - free(conn); - return NULL; + } } return conn; } static void conn_free(ECPConnection *conn) { - free(conn); + if (ecp_conn_is_gc(conn)) free(conn); } -int ecp_init(ECPContext *ctx) { +int ecp_init(ECPContext *ctx, ECPConnHandler *vconn_handler, ECPConnHandler *vlink_handler) { int rv; - rv = ecp_ctx_init(ctx, handle_err, NULL, conn_alloc, conn_free); - return rv; + rv = ecp_ctx_init(ctx, handle_err, conn_new, conn_free); + if (rv) return rv; + + rv = ecp_vconn_handler_init(ctx, vconn_handler); + if (rv) return rv; + + rv = ecp_vlink_handler_init(ctx, vlink_handler); + if (rv) return rv; + + return ECP_OK; }
\ No newline at end of file diff --git a/ecp/test/init_vconn.h b/ecp/test/init_vconn.h new file mode 100644 index 0000000..64e3269 --- /dev/null +++ b/ecp/test/init_vconn.h @@ -0,0 +1 @@ +int ecp_init(ECPContext *ctx, ECPConnHandler *vconn_handler, ECPConnHandler *vlink_handler);
\ No newline at end of file diff --git a/ecp/test/server.c b/ecp/test/server.c index 228dbea..19b2f4f 100644 --- a/ecp/test/server.c +++ b/ecp/test/server.c @@ -3,10 +3,12 @@ #include <unistd.h> #include <stdio.h> -#include <core.h> +#include <ecp/core.h> #include <util.h> +#include "init.h" + ECPContext ctx; ECPSocket sock; ECPDHKey key_perma; @@ -31,6 +33,7 @@ static void usage(char *arg) { } int main(int argc, char *argv[]) { + ecp_tr_addr_t addr; ECPDHKey key_perma; int rv; @@ -41,7 +44,7 @@ int main(int argc, char *argv[]) { printf("ecp_init RV:%d\n", rv); ecp_conn_handler_init(&handler, handle_msg, NULL, NULL, NULL); - ecp_ctx_set_handler(&ctx, &handler, CTYPE_TEST); + ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); rv = ecp_util_load_key(&key_perma.public, &key_perma.private, argv[2]); printf("ecp_util_load_key RV:%d\n", rv); @@ -50,7 +53,10 @@ int main(int argc, char *argv[]) { rv = ecp_sock_create(&sock, &ctx, &key_perma); printf("ecp_sock_create RV:%d\n", rv); - rv = ecp_sock_open(&sock, argv[1]); + rv = ecp_addr_init(&addr, argv[1]); + printf("ecp_addr_init RV:%d\n", rv); + + rv = ecp_sock_open(&sock, &addr); printf("ecp_sock_open RV:%d\n", rv); rv = ecp_start_receiver(&sock); diff --git a/ecp/test/stress.c b/ecp/test/stress.c deleted file mode 100644 index 6eed851..0000000 --- a/ecp/test/stress.c +++ /dev/null @@ -1,256 +0,0 @@ -#include <stdlib.h> -#include <string.h> -#include <stdio.h> -#include <unistd.h> -#include <signal.h> -#include <sys/time.h> -#include <pthread.h> - -#include "core.h" - -#ifdef __linux__ -#define SIGINFO SIGTSTP -#endif - -#define NUM_S 32 -#define NUM_C 256 -#define MSG_RATE 65 - -#define CTYPE_TEST 0 -#define MTYPE_MSG 8 - -ECPContext ctx_s; -ECPDHKey key_perma_s; -ECPConnHandler handler_s; -ECPSocket *sock_s; - -ECPConnHandler handler_c; -ECPContext *ctx_c; -ECPSocket *sock_c; -ECPDHKey *key_perma_c; - -ECPNode *node; -ECPConnection *conn; - -pthread_t *s_thd; -pthread_t *r_thd; -pthread_mutex_t *t_mtx; -int *t_sent, *t_rcvd; - -uint64_t t_start = 0, t_end = 0; -int c_start = 0; - -int num_s = NUM_S, num_c = NUM_C; -int msg_rate = MSG_RATE; - -static void display(void) { - int i, s = 0, r = 0; - - for (i=0; i<num_c; i++) { - pthread_mutex_lock(&t_mtx[i]); - s += t_sent[i]; - r += t_rcvd[i]; - pthread_mutex_unlock(&t_mtx[i]); - } - - uint64_t t_time = t_end - t_start; - - printf("TOTAL SENT:%d RCVD:%d\n", s, r); - printf("L:%f%%\n", (s-r)/(float)s*100); - printf("T/S S:%f R:%f\n", s/((float)t_time/1000000), r/((float)t_time/1000000)); -} - -static void catchINFO(int sig) { - struct timeval tv; - - if (!c_start) { - gettimeofday(&tv, NULL); - t_start = tv.tv_sec*(uint64_t)1000000+tv.tv_usec; - c_start = 1; - printf("COUNTER START\n"); - } else { - gettimeofday(&tv, NULL); - t_end = tv.tv_sec*(uint64_t)1000000+tv.tv_usec; - display(); - } -} - -void *sender(ECPConnection *c) { - int idx = (int)(c->conn_data); - ECPBuffer packet; - ECPBuffer payload; - unsigned char pkt_buf[ECP_MAX_PKT]; - unsigned char pld_buf[ECP_MAX_PLD]; - - packet.buffer = pkt_buf; - packet.size = ECP_MAX_PKT; - payload.buffer = pld_buf; - payload.size = ECP_MAX_PLD; - - printf("OPEN:%d\n", idx); - while(1) { - uint32_t rnd; - c->sock->ctx->rng(&rnd, sizeof(uint32_t)); - usleep(rnd % (2000000/msg_rate)); - - ecp_pld_set_type(payload.buffer, payload.size, MTYPE_MSG); - ssize_t _rv = 0; - // XXX refactor - // _rv = ecp_pld_send(c, &packet, &payload, ECP_SIZE_PLD(1000, 0)); - if (c_start && (_rv > 0)) { - pthread_mutex_lock(&t_mtx[idx]); - t_sent[idx]++; - pthread_mutex_unlock(&t_mtx[idx]); - } - } -} - -ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { - int idx = (int)(conn->conn_data); - int rv = 0; - - ecp_conn_handle_open(conn, sq, t, p, s, b); - rv = pthread_create(&s_thd[idx], NULL, (void *(*)(void *))sender, (void *)conn); - if (rv) { - char msg[256]; - sprintf(msg, "THD %d CREATE\n", idx); - perror(msg); - exit(1); - } - return s; -} - -ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { - int idx = (int)(conn->conn_data); - ECPBuffer packet; - ECPBuffer payload; - unsigned char pkt_buf[ECP_MAX_PKT]; - unsigned char pld_buf[ECP_MAX_PLD]; - - packet.buffer = pkt_buf; - packet.size = ECP_MAX_PKT; - payload.buffer = pld_buf; - payload.size = ECP_MAX_PLD; - - if (c_start) { - pthread_mutex_lock(&t_mtx[idx]); - t_rcvd[idx]++; - pthread_mutex_unlock(&t_mtx[idx]); - } - - // ecp_pld_set_type(payload.buffer, payload.size, MTYPE_MSG); - // ssize_t _rv = ecp_pld_send(c, &packet, &payload, ECP_SIZE_PLD(1000, 0)); - return s; -} - -ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { - ECPBuffer packet; - ECPBuffer payload; - unsigned char pkt_buf[ECP_MAX_PKT]; - unsigned char pld_buf[ECP_MAX_PLD]; - - packet.buffer = pkt_buf; - packet.size = ECP_MAX_PKT; - payload.buffer = pld_buf; - payload.size = ECP_MAX_PLD; - - ecp_pld_set_type(payload.buffer, payload.size, MTYPE_MSG); - // XXX refactor - // ssize_t _rv = ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(1000, 0)); - return s; -} - -int main(int argc, char *argv[]) { - char addr[256]; - int rv; - int i; - - ECPConnHandler handler_c; - - ECPContext *ctx_c; - ECPSocket *sock_c; - ECPDHKey *key_perma_c; - - ECPNode *node; - ECPConnection *conn; - - sock_s = malloc(num_s * sizeof(ECPSocket)); - ctx_c = malloc(num_c * sizeof(ECPContext)); - sock_c = malloc(num_c * sizeof(ECPSocket)); - key_perma_c = malloc(num_c * sizeof(ECPDHKey)); - node = malloc(num_c * sizeof(ECPNode)); - conn = malloc(num_c * sizeof(ECPConnection)); - - s_thd = malloc(num_c * sizeof(pthread_t)); - r_thd = malloc(num_c * sizeof(pthread_t)); - t_mtx = malloc(num_c * sizeof(pthread_mutex_t)); - t_sent = malloc(num_c * sizeof(int)); - t_rcvd = malloc(num_c * sizeof(int)); - memset(t_rcvd, 0, num_c * sizeof(int)); - memset(t_sent, 0, num_c * sizeof(int)); - - struct sigaction actINFO; - memset(&actINFO, 0, sizeof(actINFO)); - actINFO.sa_handler = &catchINFO; - sigaction(SIGINFO, &actINFO, NULL); - - rv = ecp_init(&ctx_s); - if (!rv) rv = ecp_conn_handler_init(&handler_s); - handler_s.msg[MTYPE_MSG] = handle_msg_s; - ctx_s.handler[CTYPE_TEST] = &handler_s; - - if (!rv) rv = ecp_dhkey_gen(&ctx_s, &key_perma_s); - - for (i=0; i<num_s; i++) { - if (!rv) rv = ecp_sock_init(&sock_s[i], &ctx_s, &key_perma_s); - - strcpy(addr, "0.0.0.0:"); - sprintf(addr+strlen(addr), "%d", 3000+i); - if (!rv) rv = ecp_sock_open(&sock_s[i], addr); - - if (!rv) rv = pthread_create(&r_thd[i], NULL, (void *(*)(void *))ecp_receiver, (void *)&sock_s[i]); - - if (rv) { - char msg[256]; - sprintf(msg, "SERVER %d CREATE:%d\n", i, rv); - perror(msg); - exit(1); - } - } - - rv = ecp_conn_handler_init(&handler_c); - - handler_c.msg[ECP_MTYPE_OPEN] = handle_open_c; - handler_c.msg[MTYPE_MSG] = handle_msg_c; - - for (i=0; i<num_c; i++) { - pthread_mutex_init(&t_mtx[i], NULL); - - if (!rv) rv = ecp_init(&ctx_c[i]); - ctx_c[i].handler[CTYPE_TEST] = &handler_c; - - if (!rv) rv = ecp_dhkey_gen(&ctx_c[i], &key_perma_c[i]); - if (!rv) rv = ecp_sock_init(&sock_c[i], &ctx_c[i], &key_perma_c[i]); - if (!rv) rv = ecp_sock_open(&sock_c[i], NULL); - - if (!rv) rv = pthread_create(&r_thd[i], NULL, (void *(*)(void *))ecp_receiver, (void *)&sock_c[i]); - - strcpy(addr, "127.0.0.1:"); - sprintf(addr+strlen(addr), "%d", 3000 + (i % num_s)); - if (!rv) rv = ecp_node_init(&node[i], &key_perma_s.public, addr); - - if (!rv) rv = ecp_conn_init(&conn[i], &sock_c[i], CTYPE_TEST); - conn[i].conn_data = (void *)i; - - if (!rv) rv = ecp_conn_open(&conn[i], &node[i]); - - if (rv) { - char msg[256]; - sprintf(msg, "CLIENT %d CREATE:%d\n", i, rv); - perror(msg); - exit(1); - } - - } - while (1) sleep(1); -}
\ No newline at end of file diff --git a/ecp/test/vc_client.c b/ecp/test/vc_client.c index 89602ca..41c364d 100644 --- a/ecp/test/vc_client.c +++ b/ecp/test/vc_client.c @@ -3,16 +3,20 @@ #include <unistd.h> #include <stdio.h> -#include <core.h> -#include <vconn/vconn.h> +#include <ecp/core.h> +#include <ecp/vconn/vconn.h> #include <util.h> +#include "init_vconn.h" + ECPContext ctx; ECPSocket sock; ECPConnHandler handler; +ECPConnHandler vconn_handler; +ECPConnHandler vlink_handler; ECPConnection conn; -ECPConnection vconn[3]; +ECPVConnOutb vconn[3]; #define CTYPE_TEST 0 #define MTYPE_MSG 0 @@ -47,11 +51,11 @@ int main(int argc, char *argv[]) { if ((argc < 4) || (argc > 6)) usage(argv[0]); - rv = ecp_init(&ctx); + rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); printf("ecp_init RV:%d\n", rv); ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); - ecp_ctx_set_handler(&ctx, &handler, CTYPE_TEST); + ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); rv = ecp_dhkey_gen(&key_perma); printf("ecp_dhkey_gen RV:%d\n", rv); @@ -65,24 +69,21 @@ int main(int argc, char *argv[]) { rv = ecp_start_receiver(&sock); printf("ecp_start_receiver RV:%d\n", rv); + rv = ecp_util_load_pub(&node_pub, argv[1]); + printf("ecp_util_load_pub RV:%d\n", rv); + + ecp_node_init(&node, &node_pub, NULL); + rv = ecp_node_set_addr(&node, argv[2]); + printf("ecp_node_set_addr RV:%d\n", rv); + for (i=3; i<argc; i++) { rv = ecp_util_load_pub(&vconn_pub[i-3], argv[i]); printf("ecp_util_load_pub RV:%d\n", rv); } - rv = ecp_conn_create(&conn, &sock, CTYPE_TEST); - printf("ecp_conn_create RV:%d\n", rv); - - rv = ecp_vconn_create(vconn, vconn_pub, argc-3, &conn); - printf("ecp_vconn_create RV:%d\n", rv); - - rv = ecp_util_load_pub(&node_pub, argv[1]); - printf("ecp_util_load_pub RV:%d\n", rv); - - rv = ecp_node_init(&node, &node_pub, argv[2]); - printf("ecp_node_init RV:%d\n", rv); - - rv = ecp_vconn_open(&conn, &node); + ecp_conn_init(&conn, &sock, CTYPE_TEST); + ecp_vconn_init(vconn, vconn_pub, argc-3, &sock); + rv = ecp_vconn_open(vconn, &conn, &node); printf("ecp_vconn_open RV:%d\n", rv); while (1) sleep(1); diff --git a/ecp/test/vc_client_t.c b/ecp/test/vc_client_t.c deleted file mode 100644 index 968d643..0000000 --- a/ecp/test/vc_client_t.c +++ /dev/null @@ -1,118 +0,0 @@ -#include <stdio.h> -#include <string.h> -#include <unistd.h> -#include <stdlib.h> -#include <sys/time.h> - -#include "core.h" -#include "vconn/vconn.h" -#include "util.h" - -ECPContext ctx; -ECPSocket sock; -ECPConnHandler handler; - -ECPNode node; -ECPConnection conn; - -ECPVConnOut vconn[20]; -ECPNode vconn_node[20]; - -#define CTYPE_TEST 0 -#define MTYPE_MSG 8 - - -int counter = 0; -uint64_t t_start = 0; -uint64_t t_end = 0; -ssize_t handle_open(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { - ssize_t rv = ecp_conn_handle_open(conn, sq, t, p, s, b); - if (rv < 0) { - printf("OPEN ERR:%ld\n", s); - return rv; - } - - printf("OPEN\n"); - char *msg = "PERA JE CAR!"; - unsigned char buf[1000]; - - strcpy((char *)buf, msg); - // ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000); - - struct timeval tv; - gettimeofday(&tv, NULL); - t_start = tv.tv_sec*(uint64_t)1000000+tv.tv_usec; - - return rv; -} - -ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { - ecp_send(conn, t, p, s); - write(2, p+1, s-1); - fflush(stderr); - // printf("MSG C:%s size:%ld\n", p, s); - return s; - - counter++; - char *msg = "PERA JE CAR!"; - unsigned char buf[1000]; - - strcpy((char *)buf, msg); - ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000); - - if (counter % 100 == 0) { - struct timeval tv; - uint64_t t_time; - - gettimeofday(&tv, NULL); - t_end = tv.tv_sec*(uint64_t)1000000+tv.tv_usec; - t_time = t_end - t_start; - printf("T:%f\n", (float)t_time/1000000); - t_start = t_end; - } - return s; -} - -static void usage(char *arg) { - fprintf(stderr, "Usage: %s <server.pub> <vcs1.pub> ... <vcsn.pub>\n", arg); - exit(1); -} - -int main(int argc, char *argv[]) { - int rv, i; - - if ((argc < 3) || (argc > 22)) usage(argv[0]); - - rv = ecp_init(&ctx); - printf("ecp_init RV:%d\n", rv); - - rv = ecp_conn_handler_init(&handler); - handler.msg[ECP_MTYPE_OPEN] = handle_open; - handler.msg[MTYPE_MSG] = handle_msg; - ctx.handler[CTYPE_TEST] = &handler; - - rv = ecp_sock_init(&sock, &ctx, NULL); - printf("ecp_sock_init RV:%d\n", rv); - - rv = ecp_sock_open(&sock, NULL); - printf("ecp_sock_open RV:%d\n", rv); - - rv = ecp_start_receiver(&sock); - printf("ecp_start_receiver RV:%d\n", rv); - - rv = ecp_util_node_load(&ctx, &node, argv[1]); - printf("ecp_util_node_load RV:%d\n", rv); - - for (i=0; i<argc-2; i++) { - rv = ecp_util_node_load(&ctx, &vconn_node[i], argv[i+2]); - printf("ecp_util_node_load RV:%d\n", rv); - } - - rv = ecp_conn_init(&conn, &sock, CTYPE_TEST); - printf("ecp_conn_init RV:%d\n", rv); - - rv = ecp_vconn_open(&conn, &node, vconn, vconn_node, argc-2); - printf("ecp_vconn_open RV:%d\n", rv); - - while (1) sleep(1); -} diff --git a/ecp/test/vc_common.c b/ecp/test/vc_common.c new file mode 100644 index 0000000..0508ac1 --- /dev/null +++ b/ecp/test/vc_common.c @@ -0,0 +1,127 @@ +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <stdio.h> + +#include <ecp/core.h> +#include <ecp/dir/dir.h> +#include <ecp/vconn/vconn.h> + +#include <openssl/crypto.h> +#include <openssl/sha.h> + +#include "vc_common.h" + +#define VCONN_NODES 3 + +#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) +#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y)) + +static void msg_remove_item(unsigned char *msg, uint16_t i, uint16_t c) { + if (c - i - 1) memmove(msg + i * ECP_SIZE_DIR_ITEM, msg + (i + 1) * ECP_SIZE_DIR_ITEM, (c - i - 1) * ECP_SIZE_DIR_ITEM); +} + +static void hrw_select(unsigned char *msg, uint16_t count, ecp_ecdh_public_t *pub, ecp_ecdh_public_t *hrw_pub, ecp_ecdh_public_t vconn_pub[], size_t *_vconn_size, ecp_tr_addr_t *addr) { + unsigned char *_msg = msg; + unsigned char hash[SHA_DIGEST_LENGTH]; + unsigned char hrw_hash[SHA_DIGEST_LENGTH]; + ecp_ecdh_public_t hash_pub[2]; + size_t vconn_size = *_vconn_size; + ECPDirItem dir_item; + SHA_CTX ctx; + int i, hrw_i; + + if (count == 0) return; + + memset(hrw_hash, 0, sizeof(hrw_hash)); + memcpy(&hash_pub[0], pub, sizeof(ecp_ecdh_public_t)); + + for (i=0; i<count; i++) { + unsigned char *host; + uint16_t port; + size_t rv; + + rv = ecp_dir_item_parse(&dir_item, _msg); + _msg += rv; + + host = dir_item.node.addr.host; + port = dir_item.node.addr.port; + printf("ITEM: %d.%d.%d.%d:%d\n", host[0], host[1], host[2], host[3], ntohs(port)); + + memcpy(&hash_pub[1], &dir_item.node.key_perma.public, sizeof(ecp_ecdh_public_t)); + SHA1_Init(&ctx); + SHA1_Update(&ctx, hash_pub, sizeof(hash_pub)); + SHA1_Final(hash, &ctx); + if (memcmp(hrw_hash, hash, sizeof(hash)) < 0) { + hrw_i = i; + memcpy(hrw_hash, hash, sizeof(hash)); + memcpy(hrw_pub, &dir_item.node.key_perma, sizeof(ecp_ecdh_public_t)); + } + } + + printf("SELECTED: %d\n", hrw_i); + msg_remove_item(msg, hrw_i, count); + count--; + + vconn_size = MIN(count, vconn_size); + for (i=0; i<vconn_size; i++) { + uint16_t s; + + s = arc4random() % count; + _msg = msg + s * ECP_SIZE_DIR_ITEM; + ecp_dir_item_parse(&dir_item, _msg); + memcpy(&vconn_pub[i], &dir_item.node.key_perma, sizeof(ecp_ecdh_public_t)); + if (i==0) *addr = dir_item.node.addr; + msg_remove_item(msg, s, count); + count--; + } + *_vconn_size = vconn_size; +} + +int vc_open_inb(ECPConnection *vlink, unsigned char *msg, uint16_t count, ecp_ecdh_public_t *local_pub) { + ecp_ecdh_public_t vconn_pub[VCONN_NODES - 1]; + ecp_ecdh_public_t hrw_pub; + ecp_tr_addr_t addr; + ECPNode node; + ECPVConnOutb *vconn; + size_t vconn_size; + int rv; + + vconn_size = VCONN_NODES - 1; + hrw_select(msg, count, local_pub, &hrw_pub, vconn_pub, &vconn_size, &addr); + + vconn = malloc(sizeof(ECPVConnOutb) * vconn_size); + if (vconn == NULL) return ECP_ERR_ALLOC; + + ecp_vconn_init(vconn, vconn_pub, vconn_size, vlink->sock); + ecp_node_init(&node, &hrw_pub, &addr); + rv = ecp_vconn_open(vconn, vlink, &node); + printf("ecp_vconn_open RV:%d\n", rv); + + return ECP_OK; +} + +int vc_open_outb(ECPConnection *conn, unsigned char *msg, uint16_t count, ecp_ecdh_public_t *remote_pub) { + ecp_ecdh_public_t vconn_pub[VCONN_NODES]; + ecp_ecdh_public_t hrw_pub; + ecp_tr_addr_t addr; + ECPNode node; + ECPVConnOutb *vconn; + size_t vconn_size; + int rv; + + vconn_size = VCONN_NODES - 1; + hrw_select(msg, count, remote_pub, &hrw_pub, vconn_pub, &vconn_size, &addr); + memcpy(&vconn_pub[vconn_size], &hrw_pub, sizeof(ecp_ecdh_public_t)); + vconn_size++; + + vconn = malloc(sizeof(ECPVConnOutb) * vconn_size); + if (vconn == NULL) return ECP_ERR_ALLOC; + + ecp_vconn_init(vconn, vconn_pub, vconn_size, conn->sock); + ecp_node_init(&node, remote_pub, &addr); + rv = ecp_vconn_open(vconn, conn, &node); + printf("ecp_vconn_open RV:%d\n", rv); + + return ECP_OK; +} diff --git a/ecp/test/vc_common.h b/ecp/test/vc_common.h new file mode 100644 index 0000000..a684566 --- /dev/null +++ b/ecp/test/vc_common.h @@ -0,0 +1,2 @@ +int vc_open_inb(ECPConnection *vlink, unsigned char *msg, uint16_t count, ecp_ecdh_public_t *local_pub); +int vc_open_outb(ECPConnection *conn, unsigned char *msg, uint16_t count, ecp_ecdh_public_t *remote_pub); diff --git a/ecp/test/vc_inb.c b/ecp/test/vc_inb.c new file mode 100644 index 0000000..13b493c --- /dev/null +++ b/ecp/test/vc_inb.c @@ -0,0 +1,124 @@ +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <stdio.h> + +#include <ecp/core.h> +#include <ecp/dir/dir.h> +#include <ecp/vconn/vconn.h> + +#include <util.h> + +#include "init_vconn.h" +#include "vc_common.h" + +ECPContext ctx; +ECPSocket sock; +ECPConnHandler handler; +ECPConnHandler dir_handler; +ECPConnHandler vconn_handler; +ECPConnHandler vlink_handler; + +#define CTYPE_TEST 0 +#define MTYPE_MSG 0 + +static int handle_open(ECPConnection *conn, ECP2Buffer *b) { + printf("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) { + char *_msg = "VAISTINU JE CAR!"; + ssize_t rv; + + printf("MSG:%s size:%ld\n", msg, msg_size); + rv = ecp_msg_send(conn, MTYPE_MSG, (unsigned char *)_msg, strlen(_msg)+1); + + return msg_size; +} + +static ssize_t handle_dir_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { + size_t rsize; + uint16_t count; + ECPConnection *vlink; + int rv; + + if (msg_size < sizeof(uint16_t)) return ECP_ERR_SIZE; + + count = \ + ((uint16_t)msg[0] << 8) | \ + ((uint16_t)msg[1]); + + printf("DIR MSG:%d\n", count); + + rsize = sizeof(uint16_t) + count * ECP_SIZE_DIR_ITEM; + if (msg_size < rsize) return ECP_ERR_SIZE; + + msg += sizeof(uint16_t); + + vlink = malloc(sizeof(ECPConnection)); + if (vlink == NULL) return ECP_ERR_ALLOC; + + ecp_vlink_init(vlink, conn->sock); + rv = vc_open_inb(vlink, msg, count, &conn->sock->key_perma.public); + if (rv) { + free(vlink); + return rv; + } + + return rsize; +} + +static void usage(char *arg) { + fprintf(stderr, "Usage: %s <my.priv> <dir pub> <dir addr>\n", arg); + exit(1); +} + +int main(int argc, char *argv[]) { + ECPConnection *conn_dir; + ECPNode node_dir; + ecp_ecdh_public_t dir_pub; + ECPDHKey key_perma; + int rv; + + if (argc != 4) usage(argv[0]); + + rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); + printf("ecp_init RV:%d\n", rv); + + ecp_conn_handler_init(&dir_handler, handle_dir_msg, ecp_dir_handle_open, NULL, NULL); + ecp_ctx_set_handler(&ctx, ECP_CTYPE_DIR, &dir_handler); + + ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); + ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); + + rv = ecp_util_load_key(&key_perma.public, &key_perma.private, argv[1]); + printf("ecp_util_load_key RV:%d\n", rv); + key_perma.valid = 1; + + rv = ecp_sock_create(&sock, &ctx, &key_perma); + printf("ecp_sock_create RV:%d\n", rv); + + rv = ecp_sock_open(&sock, NULL); + printf("ecp_sock_open RV:%d\n", rv); + + rv = ecp_start_receiver(&sock); + printf("ecp_start_receiver RV:%d\n", rv); + + rv = ecp_util_load_pub(&dir_pub, argv[2]); + printf("ecp_util_load_pub RV:%d\n", rv); + + ecp_node_init(&node_dir, &dir_pub, NULL); + rv = ecp_node_set_addr(&node_dir, argv[3]); + printf("ecp_node_set_addr RV:%d\n", rv); + + conn_dir = malloc(sizeof(ECPConnection)); + if (conn_dir == NULL) printf("out of memory\n"); + + ecp_dir_conn_init(conn_dir, &sock); + rv = ecp_conn_open(conn_dir, &node_dir); + printf("ecp_conn_open RV:%d\n", rv); + + while (1) sleep(1); +} diff --git a/ecp/test/vc_outb.c b/ecp/test/vc_outb.c new file mode 100644 index 0000000..6018602 --- /dev/null +++ b/ecp/test/vc_outb.c @@ -0,0 +1,128 @@ +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <stdio.h> + +#include <ecp/core.h> +#include <ecp/dir/dir.h> +#include <ecp/vconn/vconn.h> + +#include <util.h> + +#include "init_vconn.h" +#include "vc_common.h" + +ECPContext ctx; +ECPSocket sock; +ECPConnHandler handler; +ECPConnHandler dir_handler; +ECPConnHandler vconn_handler; +ECPConnHandler vlink_handler; +ecp_ecdh_public_t remote_pub; + +#define CTYPE_TEST 0 +#define MTYPE_MSG 0 + +static int handle_open(ECPConnection *conn, ECP2Buffer *b) { + char *_msg = "PERA JE CAR!"; + ssize_t rv; + + printf("OPEN\n"); + rv = ecp_msg_send(conn, MTYPE_MSG, (unsigned char *)_msg, strlen(_msg)+1); + + 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) { + printf("MSG:%s size:%ld\n", msg, msg_size); + + return msg_size; +} + +static ssize_t handle_dir_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { + size_t rsize; + uint16_t count; + ECPConnection *_conn; + int rv; + + if (msg_size < sizeof(uint16_t)) return ECP_ERR_SIZE; + + count = \ + ((uint16_t)msg[0] << 8) | \ + ((uint16_t)msg[1]); + + printf("DIR MSG:%d\n", count); + + rsize = sizeof(uint16_t) + count * ECP_SIZE_DIR_ITEM; + if (msg_size < rsize) return ECP_ERR_SIZE; + + msg += sizeof(uint16_t); + + _conn = malloc(sizeof(ECPConnection)); + if (_conn == NULL) return ECP_ERR_ALLOC; + + ecp_conn_init(_conn, conn->sock, CTYPE_TEST); + rv = vc_open_outb(_conn, msg, count, &remote_pub); + if (rv) { + printf("vc_open_outb RV:%d\n", rv); + free(_conn); + return rv; + } + + return rsize; +} + +static void usage(char *arg) { + fprintf(stderr, "Usage: %s <remote.pub> <dir pub> <dir addr>\n", arg); + exit(1); +} + +int main(int argc, char *argv[]) { + ECPConnection *conn_dir; + ECPNode node_dir; + ecp_ecdh_public_t dir_pub; + ECPDHKey key_perma; + int rv; + + if (argc != 4) usage(argv[0]); + + rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); + printf("ecp_init RV:%d\n", rv); + + ecp_conn_handler_init(&dir_handler, handle_dir_msg, ecp_dir_handle_open, NULL, NULL); + ecp_ctx_set_handler(&ctx, ECP_CTYPE_DIR, &dir_handler); + + ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); + ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); + + rv = ecp_dhkey_gen(&key_perma); + printf("ecp_dhkey_gen RV:%d\n", rv); + + rv = ecp_sock_create(&sock, &ctx, &key_perma); + printf("ecp_sock_create RV:%d\n", rv); + + rv = ecp_sock_open(&sock, NULL); + printf("ecp_sock_open RV:%d\n", rv); + + rv = ecp_start_receiver(&sock); + printf("ecp_start_receiver RV:%d\n", rv); + + rv = ecp_util_load_pub(&remote_pub, argv[1]); + printf("ecp_util_load_pub RV:%d\n", rv); + + rv = ecp_util_load_pub(&dir_pub, argv[2]); + printf("ecp_util_load_pub RV:%d\n", rv); + + ecp_node_init(&node_dir, &dir_pub, NULL); + rv = ecp_node_set_addr(&node_dir, argv[3]); + printf("ecp_node_set_addr RV:%d\n", rv); + + conn_dir = malloc(sizeof(ECPConnection)); + if (conn_dir == NULL) printf("out of memory\n"); + + ecp_dir_conn_init(conn_dir, &sock); + rv = ecp_conn_open(conn_dir, &node_dir); + printf("ecp_conn_open RV:%d\n", rv); + + while (1) sleep(1); +} diff --git a/ecp/test/vc_server.c b/ecp/test/vc_server.c index de7f275..548b52d 100644 --- a/ecp/test/vc_server.c +++ b/ecp/test/vc_server.c @@ -3,16 +3,20 @@ #include <unistd.h> #include <stdio.h> -#include <core.h> -#include <vconn/vconn.h> +#include <ecp/core.h> +#include <ecp/vconn/vconn.h> #include <util.h> +#include "init_vconn.h" + ECPContext ctx; ECPSocket sock; ECPConnHandler handler; +ECPConnHandler vconn_handler; +ECPConnHandler vlink_handler; ECPConnection conn; -ECPConnection vconn[3]; +ECPVConnOutb vconn[3]; #define CTYPE_TEST 0 #define MTYPE_MSG 0 @@ -46,11 +50,11 @@ int main(int argc, char *argv[]) { if ((argc < 4) || (argc > 7)) usage(argv[0]); - rv = ecp_init(&ctx); + rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); printf("ecp_init RV:%d\n", rv); ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); - ecp_ctx_set_handler(&ctx, &handler, CTYPE_TEST); + ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); rv = ecp_util_load_key(&key_perma.public, &key_perma.private, argv[1]); printf("ecp_util_load_key RV:%d\n", rv); @@ -65,29 +69,29 @@ int main(int argc, char *argv[]) { rv = ecp_start_receiver(&sock); printf("ecp_start_receiver RV:%d\n", rv); - rv = ecp_vlink_create(&conn, &sock); - printf("ecp_vlink_create RV:%d\n", rv); + rv = ecp_util_load_pub(&node_pub, argv[argc-1]); + printf("ecp_util_load_pub RV:%d\n", rv); + ecp_node_init(&node, &node_pub, NULL); + rv = ecp_node_set_addr(&node, argv[2]); + printf("ecp_node_set_addr RV:%d\n", rv); + + ecp_vlink_init(&conn, &sock); if (argc > 4) { - ecp_ecdh_public_t vconn_key[3]; + ecp_ecdh_public_t vconn_pub[3]; for (i=3; i<argc-1; i++) { - rv = ecp_util_load_pub(&vconn_key[i-3], argv[i]); + rv = ecp_util_load_pub(&vconn_pub[i-3], argv[i]); printf("ecp_util_load_pub RV:%d\n", rv); } + ecp_vconn_init(vconn, vconn_pub, argc-4, &sock); - rv = ecp_vconn_create(vconn, vconn_key, argc-4, &conn); - printf("ecp_vconn_create RV:%d\n", rv); + rv = ecp_vconn_open(vconn, &conn, &node); + printf("ecp_vconn_open RV:%d\n", rv); + } else { + rv = ecp_conn_open(&conn, &node); + printf("ecp_conn_open RV:%d\n", rv); } - rv = ecp_util_load_pub(&node_pub, argv[argc-1]); - printf("ecp_util_load_pub RV:%d\n", rv); - - rv = ecp_node_init(&node, &node_pub, argv[2]); - printf("ecp_node_init RV:%d\n", rv); - - rv = ecp_vconn_open(&conn, &node); - printf("ecp_vconn_open RV:%d\n", rv); - while (1) sleep(1); } diff --git a/ecp/test/vcs.c b/ecp/test/vcs.c index c4beed7..92d1eb2 100644 --- a/ecp/test/vcs.c +++ b/ecp/test/vcs.c @@ -3,14 +3,18 @@ #include <unistd.h> #include <stdio.h> -#include <core.h> -#include <vconn/vconn.h> +#include <ecp/core.h> +#include <ecp/vconn/vconn.h> #include <util.h> +#include "init_vconn.h" + ECPContext ctx; ECPSocket sock; ECPConnection conn; +ECPConnHandler vconn_handler; +ECPConnHandler vlink_handler; static void usage(char *arg) { fprintf(stderr, "Usage: %s <my addr> <my.priv> [ <node addr> <node.pub> ]\n", arg); @@ -18,12 +22,13 @@ static void usage(char *arg) { } int main(int argc, char *argv[]) { + ecp_tr_addr_t addr; ECPDHKey key_perma; int rv; if ((argc < 3) || (argc > 5)) usage(argv[0]); - rv = ecp_init(&ctx); + rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); printf("ecp_init RV:%d\n", rv); rv = ecp_util_load_key(&key_perma.public, &key_perma.private, argv[2]); @@ -33,7 +38,10 @@ int main(int argc, char *argv[]) { rv = ecp_sock_create(&sock, &ctx, &key_perma); printf("ecp_sock_create RV:%d\n", rv); - rv = ecp_sock_open(&sock, argv[1]); + rv = ecp_addr_init(&addr, argv[1]); + printf("ecp_addr_init RV:%d\n", rv); + + rv = ecp_sock_open(&sock, &addr); printf("ecp_sock_open RV:%d\n", rv); rv = ecp_start_receiver(&sock); @@ -43,15 +51,14 @@ int main(int argc, char *argv[]) { ECPNode node; ecp_ecdh_public_t node_pub; - rv = ecp_vlink_create(&conn, &sock); - printf("ecp_vlink_create RV:%d\n", rv); - rv = ecp_util_load_pub(&node_pub, argv[4]); printf("ecp_util_load_pub RV:%d\n", rv); - rv = ecp_node_init(&node, &node_pub, argv[3]); - printf("ecp_node_init RV:%d\n", rv); + ecp_node_init(&node, &node_pub, NULL); + rv = ecp_node_set_addr(&node, argv[3]); + printf("ecp_node_set_addr RV:%d\n", rv); + ecp_vlink_init(&conn, &sock); rv = ecp_conn_open(&conn, &node); printf("ecp_conn_open RV:%d\n", rv); } diff --git a/ecp/util/mknode.c b/ecp/util/mknode.c index 764ac18..bc66da0 100644 --- a/ecp/util/mknode.c +++ b/ecp/util/mknode.c @@ -4,7 +4,7 @@ #include <stdio.h> #include <string.h> -#include <core.h> +#include <ecp/core.h> #include "util.h" @@ -38,8 +38,11 @@ int main(int argc, char *argv[]) { rv = ecp_dhkey_gen(&key); if (rv) goto err; - rv = ecp_node_init(&node, &key.public, addr); - if (rv) goto err; + ecp_node_init(&node, &key.public, NULL); + if (addr) { + rv = ecp_node_set_addr(&node, addr); + if (rv) goto err; + } rv = ecp_util_save_key(&key.public, &key.private, fn_key); if (rv) goto err; diff --git a/ecp/util/util.c b/ecp/util/util.c index 2b67127..efcf152 100644 --- a/ecp/util/util.c +++ b/ecp/util/util.c @@ -3,7 +3,7 @@ #include <unistd.h> #include <sys/stat.h> -#include <core.h> +#include <ecp/core.h> #include "util.h" diff --git a/ext/fsfat/diskio.c b/ext/fsfat/diskio.c index 026ffd2..90a12e6 100644 --- a/ext/fsfat/diskio.c +++ b/ext/fsfat/diskio.c @@ -10,7 +10,7 @@ #include "ff.h" /* Obtains integer types */
#include "diskio.h" /* Declarations of disk functions */
-#include <eos/sdcard.h>
+#include <eos/dev/sdcard.h>
#define TIMEOUT 1000
#define TIMEOUT_TRIM 60000
diff --git a/ext/libressl/ssl_obj.mk b/ext/libressl/ssl_obj.mk index d0504ee..411d2ea 100644 --- a/ext/libressl/ssl_obj.mk +++ b/ext/libressl/ssl_obj.mk @@ -1,6 +1,6 @@ obj_dep_ = curve25519/curve25519.o curve25519/curve25519-generic.o \ chacha/chacha.o poly1305/poly1305.o aead/e_chacha20poly1305.o \ - sha/sha256.o sha/sha512.o \ + sha/sha1dgst.o sha/sha256.o sha/sha512.o \ aes/aes_core.o aes/aes_ecb.o aes/aes_cbc.o aes/aes_cfb.o aes/aes_ctr.o aes/aes_ige.o aes/aes_ofb.o \ compat/arc4random.o compat/explicit_bzero.o compat/timingsafe_memcmp.o compat/timingsafe_bcmp.o obj_dep = $(addprefix crypto/,$(obj_dep_)) diff --git a/fw/fe310/Makefile b/fw/fe310/Makefile index f9da883..70bacff 100644 --- a/fw/fe310/Makefile +++ b/fw/fe310/Makefile @@ -5,25 +5,24 @@ MAKEFLAGS += -I$(fe310_dir) include $(crypto_dir)/crypto_obj.mk obj_crypto = $(addprefix $(crypto_dir)/,$(obj_dep)) -subdirs = bsp/drivers bsp/gloss bsp/metal eos eos/i2c eos/eve eos/eve/screen eos/eve/widget $(ext_dir)/fsfat +subdirs_bsp = bsp/drivers bsp/gloss bsp/metal +subdirs_ext = $(crypto_dir) $(ext_dir)/fsfat +subdirs = eos eos/soc eos/dev eos/net eos/eve $(subdirs_bsp) $(subdirs_ext) -all: libeos.a %.o: %.c $(CC) $(CFLAGS) -c $< -libeos.a: - for i in $(subdirs) $(crypto_dir); do \ - (cd $$i && $(MAKE)) || exit; \ - done - rm -f $@ +all: + rm -f *.a for i in $(subdirs); do \ - $(AR) rcs $@ $$i/*.o; \ + (cd $$i && $(MAKE)) || exit; \ done - $(AR) rcs $@ $(obj_crypto) + $(AR) rcs libeos-bsp.a $(addsuffix /*.o,$(subdirs_bsp)) + $(AR) rcs libeos-ext.a $(obj_crypto) $(ext_dir)/fsfat/*.o clean: - for i in $(subdirs) $(crypto_dir); do \ + for i in $(subdirs); do \ (cd $$i && $(MAKE) clean) || exit; \ done rm -f *.o *.a diff --git a/fw/fe310/eos/Makefile b/fw/fe310/eos/Makefile index a52b5db..e9c6421 100644 --- a/fw/fe310/eos/Makefile +++ b/fw/fe310/eos/Makefile @@ -1,7 +1,8 @@ include ../common.mk -CFLAGS += -I. -I$(bsp_dir)/include -I$(bsp_dir)/drivers -I$(ext_dir)/crypto +CFLAGS += -I$(bsp_dir)/include -obj = trap_entry.o eos.o msgq.o event.o interrupt.o timer.o pwr.o i2s.o i2c.o uart.o spi.o spi_dev.o lcd.o sdcard.o sdc_crypto.o cam.o net.o rng.o wifi.o cell.o sock.o unicode.o +obj = eos.o msgq.o event.o unicode.o +lib = ../libeos.a %.o: %.c %.h @@ -10,7 +11,10 @@ obj = trap_entry.o eos.o msgq.o event.o interrupt.o timer.o pwr.o i2s.o i2c.o ua %.o: %.S $(CC) $(CFLAGS) -c $< -all: $(obj) +all: $(lib) + +$(lib): $(obj) + $(AR) rcs $@ $(obj) clean: - rm -f *.o + rm -f *.o $(lib) diff --git a/fw/fe310/eos/board.h b/fw/fe310/eos/board.h index 99832a7..3891c61 100644 --- a/fw/fe310/eos/board.h +++ b/fw/fe310/eos/board.h @@ -5,30 +5,33 @@ #define SPI_CSID_NET 0 #define SPI_CSID_EVE 3 -#define SPI_CSID_SDC SPI_CSID_NONE +#define SPI_CSID_SDC -1 #define SPI_CSID_CAM 2 -#define SPI_IOF_MASK ((1 << IOF_SPI1_SCK) | (1 << IOF_SPI1_MOSI) | (1 << IOF_SPI1_MISO) | (1 << IOF_SPI1_SS0) | (1 << IOF_SPI1_SS2) | (1 << IOF_SPI1_SS3)) - -#define SPI_CSPIN_NET 2 -#define SPI_CSPIN_EVE 10 +#define SPI_CSPIN_NET -1 +#define SPI_CSPIN_EVE -1 #define SPI_CSPIN_SDC 0 -#define SPI_CSPIN_CAM 9 +#define SPI_CSPIN_CAM -1 + +#define SPI_CSPIN_LCD 11 + +#define SPI_IOF_MASK ((1 << IOF_SPI1_SCK) | (1 << IOF_SPI1_MOSI) | (1 << IOF_SPI1_MISO) | (1 << IOF_SPI1_SS0) | (1 << IOF_SPI1_SS2) | (1 << IOF_SPI1_SS3)) #define NET_PIN_RTS 20 #define NET_PIN_CTS 22 -#define LCD_PIN_CS 11 - -#define EVE_PIN_INTR 23 +#define EVE_PIN_INTR 1 +//#define EVE_PIN_INTR 23 #define I2S_PIN_CK 1 /* PWM 0.1 */ #define I2S_PIN_CK_SW 21 /* PWM 1.2 */ #define I2S_PIN_CK_SR 18 #define I2S_PIN_WS_MIC 19 /* PWM 1.1 */ #define I2S_PIN_WS_SPK 11 /* PWM 2.1 */ -#define I2S_PIN_SD_IN 13 -#define I2S_PIN_SD_OUT 12 +#define I2S_PIN_SD_IN 17 +#define I2S_PIN_SD_OUT 16 +//#define I2S_PIN_SD_IN 13 +//#define I2S_PIN_SD_OUT 12 #define I2S_IRQ_WS_ID (INT_PWM2_BASE + 0) #define I2S_IRQ_SD_ID (INT_PWM2_BASE + 3) @@ -39,7 +42,8 @@ #define I2S_IDLE_CYCLES 1 -#define CTP_PIN_INT 1 +//#define CTP_PIN_INT 1 +#define CTP_PIN_INT 23 #define CTP_PIN_RST 19 #define EVE_GPIO_DIR 0xf diff --git a/fw/fe310/eos/dev/Makefile b/fw/fe310/eos/dev/Makefile new file mode 100644 index 0000000..7407212 --- /dev/null +++ b/fw/fe310/eos/dev/Makefile @@ -0,0 +1,20 @@ +include ../../common.mk +CFLAGS += -I$(bsp_dir)/include -I$(ext_dir)/crypto + +obj = spi.o net.o bq25895.o sdcard.o sdc_crypto.o eve.o ov2640.o cam.o +lib = ../../libeos-dev.a + + +%.o: %.c %.h + $(CC) $(CFLAGS) -c $< + +%.o: %.S + $(CC) $(CFLAGS) -c $< + +all: $(lib) + +$(lib): $(obj) + $(AR) rcs $@ $(obj) + +clean: + rm -f *.o $(lib) diff --git a/fw/fe310/eos/i2c/bq25895.c b/fw/fe310/eos/dev/bq25895.c index 570e9a0..11323c7 100644 --- a/fw/fe310/eos/i2c/bq25895.c +++ b/fw/fe310/eos/dev/bq25895.c @@ -3,9 +3,10 @@ #include <stdio.h> #include "eos.h" -#include "pwr.h" -#include "i2c.h" -#include "i2c/bq25895.h" +#include "soc/pwr.h" +#include "soc/i2c.h" + +#include "bq25895.h" static int reg_read(uint8_t reg, uint8_t *data) { return eos_i2c_read8(BQ25895_ADDR, reg, data, 1); diff --git a/fw/fe310/eos/i2c/bq25895.h b/fw/fe310/eos/dev/bq25895.h index cbef36e..cbef36e 100644 --- a/fw/fe310/eos/i2c/bq25895.h +++ b/fw/fe310/eos/dev/bq25895.h diff --git a/fw/fe310/eos/cam.c b/fw/fe310/eos/dev/cam.c index 43293af..50a0bdd 100644 --- a/fw/fe310/eos/cam.c +++ b/fw/fe310/eos/dev/cam.c @@ -2,7 +2,9 @@ #include <stdint.h> #include "eos.h" -#include "spi.h" + +#include "soc/spi.h" + #include "cam.h" const int _eos_cam_resolution[][2] = { @@ -105,7 +107,7 @@ int eos_cam_capture_done(void) { void eos_cam_capture_wait(void) { int done = 0; - + while (!done) { done = eos_cam_capture_done(); } @@ -113,7 +115,7 @@ void eos_cam_capture_wait(void) { uint32_t eos_cam_fbuf_size(void) { uint32_t ret; - + ret = reg_read(CAM_REG_FIFO_SIZE1); ret |= reg_read(CAM_REG_FIFO_SIZE2) << 8; ret |= (reg_read(CAM_REG_FIFO_SIZE3) & 0x7f) << 16; @@ -122,7 +124,7 @@ uint32_t eos_cam_fbuf_size(void) { void eos_cam_fbuf_read(uint8_t *buffer, uint16_t sz, int first) { int i; - + eos_spi_cs_set(); eos_spi_xchg8(CAM_REG_READ_BURST, 0); if (first) eos_spi_xchg8(0, 0); diff --git a/fw/fe310/eos/cam.h b/fw/fe310/eos/dev/cam.h index f61757b..f61757b 100644 --- a/fw/fe310/eos/cam.h +++ b/fw/fe310/eos/dev/cam.h diff --git a/fw/fe310/eos/i2c/drv2605.h b/fw/fe310/eos/dev/drv2605.h index fe90a9b..fe90a9b 100644 --- a/fw/fe310/eos/i2c/drv2605.h +++ b/fw/fe310/eos/dev/drv2605.h diff --git a/fw/fe310/eos/dev/eve.c b/fw/fe310/eos/dev/eve.c new file mode 100644 index 0000000..0f98fed --- /dev/null +++ b/fw/fe310/eos/dev/eve.c @@ -0,0 +1,152 @@ +#include <stdlib.h> +#include <stdio.h> + +#include "platform.h" + +#include "eos.h" +#include "event.h" + +#include "board.h" + +#include "soc/interrupt.h" +#include "soc/pwr.h" +#include "soc/i2s.h" + +#include "eve/eve.h" +#include "eve/eve_touch_engine.h" + +#include "eve.h" + +static void handle_time(unsigned char type) { + if (!eos_eve_running()) return; + + eve_spi_start(); + eve_handle_time(); + eve_spi_stop(); +} + +static void handle_evt(unsigned char type, unsigned char *buffer, uint16_t len) { + if (!eos_eve_running()) return; + + eve_spi_start(); + eve_handle_intr(); + eve_spi_stop(); + + GPIO_REG(GPIO_LOW_IE) |= (1 << EVE_PIN_INTR); +} + +static void handle_intr(void) { + GPIO_REG(GPIO_LOW_IE) &= ~(1 << EVE_PIN_INTR); + GPIO_REG(GPIO_LOW_IP) = (1 << EVE_PIN_INTR); + eos_evtq_push_isr(EOS_EVT_EVE | EVE_ETYPE_INTR, NULL, 0); +} + +static void _start(void) { + eve_touch_start(); + eve_start(); + + GPIO_REG(GPIO_INPUT_EN) |= (1 << EVE_PIN_INTR); + GPIO_REG(GPIO_LOW_IE) |= (1 << EVE_PIN_INTR); + + eos_intr_enable(INT_GPIO_BASE + EVE_PIN_INTR); +} + +static void _stop(void) { + eos_intr_disable(INT_GPIO_BASE + EVE_PIN_INTR); + + GPIO_REG(GPIO_LOW_IE) &= ~(1 << EVE_PIN_INTR); + GPIO_REG(GPIO_INPUT_EN) &= ~(1 << EVE_PIN_INTR); + + eve_touch_stop(); + eve_stop(); +} + +int eos_eve_init(uint8_t wakeup_cause) { + int rst = (wakeup_cause == EOS_PWR_WAKE_RST); + int rv = EVE_OK; + + eve_spi_start(); + if (rst) { + rv = eve_init(); + if (!rv) { + eve_gpio_set_dir(EVE_GPIO_DIR); + eve_touch_init_engine(); + } + } else { + eve_activate(); + } + eve_spi_stop(); + + if (rv) return EOS_ERR; + + eve_touch_init(); + + eos_evtq_set_handler(EOS_EVT_EVE, handle_evt); + eos_timer_set_handler(EOS_TIMER_ETYPE_UI, handle_time); + eos_intr_set_handler(INT_GPIO_BASE + EVE_PIN_INTR, handle_intr); + eos_intr_set_priority(INT_GPIO_BASE + EVE_PIN_INTR, IRQ_PRIORITY_EVE); + + return EOS_OK; +} + +void eos_eve_calibrate(void) { + uint32_t matrix[6]; + int r; + + eve_spi_start(); + + eve_brightness(0x40); + eve_touch_set_extended(0); + + eve_cmd(CMD_TEXT, "hhhhs", EVE_HSIZE/2, EVE_VSIZE/2, 27, EVE_OPT_CENTER, "Please tap on the dot."); + eve_cmd(CMD_CALIBRATE, "w", 0); + eve_cmd_exec(0); + + do { + r = eve_cmd_done(); + if (r < 0) break; + eve_spi_stop(); + eos_evtq_exec(); + eve_spi_start(); + } while (!r); + + eve_touch_set_extended(1); + eve_brightness(0); + + eve_touch_get_matrix(matrix); + eve_spi_stop(); + + printf("TOUCH MATRIX:\n"); + printf("uint32_t touch_matrix[6] = {0x%x,0x%x,0x%x,0x%x,0x%x,0x%x}\n", matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]); +} + +void eos_eve_set_touch_matrix(const uint32_t *matrix) { + eve_spi_start(); + eve_touch_set_matrix(matrix); + eve_spi_stop(); +} + +int eos_eve_run(uint8_t wakeup_cause) { + eve_spi_start(); + _start(); + eve_start_clk(); + eve_spi_stop(); + + return EOS_OK; +} + +void eos_eve_start(void) { + eve_spi_start(); + _start(); + eve_spi_stop(); +} + +void eos_eve_stop(void) { + eve_spi_start(); + _stop(); + eve_spi_stop(); +} + +int eos_eve_running(void) { + return !!(GPIO_REG(GPIO_INPUT_EN) & (1 << EVE_PIN_INTR)); +} diff --git a/fw/fe310/eos/dev/eve.h b/fw/fe310/eos/dev/eve.h new file mode 100644 index 0000000..8eb982f --- /dev/null +++ b/fw/fe310/eos/dev/eve.h @@ -0,0 +1,9 @@ +#include <stdint.h> + +int eos_eve_init(uint8_t wakeup_cause); +void eos_eve_calibrate(void); +void eos_eve_set_touch_matrix(const uint32_t *matrix); +int eos_eve_run(uint8_t wakeup_cause); +void eos_eve_start(void); +void eos_eve_stop(void); +int eos_eve_running(void); diff --git a/fw/fe310/eos/i2c/lsm9ds1.h b/fw/fe310/eos/dev/lsm9ds1.h index 92220e7..92220e7 100644 --- a/fw/fe310/eos/i2c/lsm9ds1.h +++ b/fw/fe310/eos/dev/lsm9ds1.h diff --git a/fw/fe310/eos/net.c b/fw/fe310/eos/dev/net.c index e10414c..d287e5a 100644 --- a/fw/fe310/eos/net.c +++ b/fw/fe310/eos/dev/net.c @@ -6,16 +6,17 @@ #include "eos.h" #include "msgq.h" -#include "interrupt.h" #include "event.h" -#include "timer.h" -#include "pwr.h" #include "board.h" +#include "soc/interrupt.h" +#include "soc/timer.h" +#include "soc/pwr.h" +#include "soc/spi.h" +#include "soc/spi_priv.h" + #include "spi.h" -#include "spi_priv.h" -#include "spi_dev.h" #include "net.h" @@ -213,6 +214,7 @@ static void net_handle_cts(void) { static void net_handle_rts(void) { uint32_t rts_offset = (1 << NET_PIN_RTS); + if (GPIO_REG(GPIO_RISE_IP) & rts_offset) { GPIO_REG(GPIO_RISE_IP) = rts_offset; net_state_flags |= NET_STATE_FLAG_RTS; @@ -302,9 +304,8 @@ static void net_resume(void) { } static void net_start(void) { + eos_spi_dev_configure(EOS_SPI_DEV_NET); eos_intr_set_handler(INT_SPI1_BASE, net_handle_xchg); - SPI1_REG(SPI_REG_SCKDIV) = eos_spi_div(EOS_SPI_DEV_NET); - SPI1_REG(SPI_REG_CSID) = eos_spi_csid(EOS_SPI_DEV_NET); } static void net_stop(void) { @@ -327,14 +328,10 @@ int eos_net_init(uint8_t wakeup_cause) { eos_evtq_set_handler(EOS_EVT_NET, net_handle_evt); GPIO_REG(GPIO_INPUT_EN) |= (1 << NET_PIN_CTS); - GPIO_REG(GPIO_OUTPUT_EN) &= ~(1 << NET_PIN_CTS); - GPIO_REG(GPIO_RISE_IE) |= (1 << NET_PIN_CTS); eos_intr_set(INT_GPIO_BASE + NET_PIN_CTS, IRQ_PRIORITY_NET_CTS, net_handle_cts); GPIO_REG(GPIO_INPUT_EN) |= (1 << NET_PIN_RTS); - GPIO_REG(GPIO_OUTPUT_EN) &= ~(1 << NET_PIN_RTS); - GPIO_REG(GPIO_RISE_IE) |= (1 << NET_PIN_RTS); GPIO_REG(GPIO_FALL_IE) |= (1 << NET_PIN_RTS); eos_intr_set(INT_GPIO_BASE + NET_PIN_RTS, IRQ_PRIORITY_NET_RTS, net_handle_rts); diff --git a/fw/fe310/eos/net.h b/fw/fe310/eos/dev/net.h index 79caf4b..2482a32 100644 --- a/fw/fe310/eos/net.h +++ b/fw/fe310/eos/dev/net.h @@ -1,5 +1,5 @@ #include <stdint.h> -#include "event.h" +#include "../event.h" /* common */ #define EOS_NET_MTU 1500 diff --git a/fw/fe310/eos/i2c/ov2640.c b/fw/fe310/eos/dev/ov2640.c index 6e54f10..e84a59b 100644 --- a/fw/fe310/eos/i2c/ov2640.c +++ b/fw/fe310/eos/dev/ov2640.c @@ -3,10 +3,10 @@ #include <math.h> #include "eos.h" -#include "timer.h" -#include "cam.h" +#include "soc/timer.h" +#include "soc/i2c.h" -#include "i2c.h" +#include "cam.h" #include "ov2640_regs.h" #include "ov2640.h" diff --git a/fw/fe310/eos/i2c/ov2640.h b/fw/fe310/eos/dev/ov2640.h index 3d08c2a..3d08c2a 100644 --- a/fw/fe310/eos/i2c/ov2640.h +++ b/fw/fe310/eos/dev/ov2640.h diff --git a/fw/fe310/eos/i2c/ov2640_regs.h b/fw/fe310/eos/dev/ov2640_regs.h index deb7521..deb7521 100644 --- a/fw/fe310/eos/i2c/ov2640_regs.h +++ b/fw/fe310/eos/dev/ov2640_regs.h diff --git a/fw/fe310/eos/sdc_crypto.c b/fw/fe310/eos/dev/sdc_crypto.c index c094468..f0e935d 100644 --- a/fw/fe310/eos/sdc_crypto.c +++ b/fw/fe310/eos/dev/sdc_crypto.c @@ -48,4 +48,4 @@ void eos_sdcc_decrypt(uint32_t sect, uint8_t *buffer) { memcpy(iv, §, sizeof(sect)); sdc_crypto->enc_essiv(sdc_crypto->ctx_essiv, iv); sdc_crypto->dec(sdc_crypto->ctx, iv, buffer, 512); -}
\ No newline at end of file +} diff --git a/fw/fe310/eos/sdc_crypto.h b/fw/fe310/eos/dev/sdc_crypto.h index 015bf8a..6442547 100644 --- a/fw/fe310/eos/sdc_crypto.h +++ b/fw/fe310/eos/dev/sdc_crypto.h @@ -15,4 +15,4 @@ typedef struct EOSSDCCrypto { void eos_sdcc_init(EOSSDCCrypto *crypto, uint8_t *key, void *ctx, eve_sdcc_init_t init, eve_sdcc_crypt_t enc, eve_sdcc_crypt_t dec, void *ctx_essiv, eve_sdcc_init_t init_essiv, eve_sdcc_essiv_t enc_essiv); void eos_sdcc_encrypt(uint32_t sect, uint8_t *buffer); -void eos_sdcc_decrypt(uint32_t sect, uint8_t *buffer);
\ No newline at end of file +void eos_sdcc_decrypt(uint32_t sect, uint8_t *buffer); diff --git a/fw/fe310/eos/sdcard.c b/fw/fe310/eos/dev/sdcard.c index 6f5488d..fa316c7 100644 --- a/fw/fe310/eos/sdcard.c +++ b/fw/fe310/eos/dev/sdcard.c @@ -3,10 +3,10 @@ #include <stdio.h> #include "eos.h" -#include "timer.h" +#include "soc/timer.h" +#include "soc/spi.h" #include "spi.h" -#include "spi_dev.h" #include "sdc_crypto.h" #include "sdcard.h" @@ -94,8 +94,8 @@ static uint16_t sdc_crc16(uint16_t crc, uint8_t b) { return crc; } -static uint32_t sdc_nto(uint64_t start, uint32_t timeout) { - uint32_t d = eos_time_since(start); +static uint32_t sdc_nto(uint32_t start, uint32_t timeout) { + uint32_t d = eos_time_delta_ms(start); return (d > timeout) ? 0 : timeout - d; } @@ -167,12 +167,12 @@ static int sdc_xchg_cmd(uint8_t cmd, uint32_t arg, uint8_t flags) { static int sdc_ready(uint32_t timeout) { uint8_t d = 0; - uint64_t start; + uint32_t start; if (timeout == 0) return EOS_ERR_BUSY; start = eos_time_get_tick(); do { - if (eos_time_since(start) > timeout) break; + if (eos_time_delta_ms(start) > timeout) break; d = sdc_xchg8(0xff); } while (d != 0xff); if (d != 0xff) return EOS_ERR_BUSY; @@ -182,12 +182,12 @@ static int sdc_ready(uint32_t timeout) { static int sdc_block_read(uint8_t *buffer, uint16_t len, uint32_t timeout) { uint8_t token = 0xff; - uint64_t start; + uint32_t start; if (timeout == 0) return EOS_ERR_BUSY; start = eos_time_get_tick(); do { - if (eos_time_since(start) > timeout) break; + if (eos_time_delta_ms(start) > timeout) break; token = sdc_xchg8(0xff); } while (token == 0xff); if (token == 0xff) return EOS_ERR_BUSY; @@ -236,7 +236,7 @@ static int sdc_cmd(uint8_t cmd, uint32_t arg, uint8_t flags, uint32_t timeout) { static int sdc_acmd(uint8_t cmd, uint32_t arg, uint8_t flags, uint32_t timeout) { int rv; - uint64_t start; + uint32_t start; start = eos_time_get_tick(); rv = sdc_cmd(APP_CMD, 0, flags, timeout); @@ -253,7 +253,7 @@ static int sdc_init(uint32_t timeout) { int rv, i; uint8_t _type; uint8_t ocr[4]; - uint64_t start; + uint32_t start; start = eos_time_get_tick(); eos_time_sleep(100); @@ -378,7 +378,7 @@ uint8_t eos_sdc_cap(void) { int eos_sdc_get_sect_count(uint32_t timeout, uint32_t *sectors) { int rv; uint8_t csd[16]; - uint64_t start = eos_time_get_tick(); + uint32_t start = eos_time_get_tick(); sdc_select(); rv = sdc_cmd(SEND_CSD, 0, SDC_CMD_FLAG_NOCS, timeout); @@ -404,7 +404,7 @@ int eos_sdc_get_sect_count(uint32_t timeout, uint32_t *sectors) { int eos_sdc_get_blk_size(uint32_t timeout, uint32_t *size) { int rv; uint8_t rbl[64]; /* SD Status or CSD register */ - uint64_t start = eos_time_get_tick(); + uint32_t start = eos_time_get_tick(); sdc_select(); if (sdc_type & EOS_SDC_TYPE_SDC2) { @@ -444,7 +444,7 @@ int eos_sdc_sync(uint32_t timeout) { int eos_sdc_erase(uint32_t blk_start, uint32_t blk_end, uint32_t timeout) { int rv; - uint64_t start; + uint32_t start; if (!(sdc_type & EOS_SDC_TYPE_SDC)) return EOS_ERR; if (!(sdc_type & EOS_SDC_CAP_ERASE_EN)) return EOS_ERR; @@ -468,11 +468,9 @@ int eos_sdc_erase(uint32_t blk_start, uint32_t blk_end, uint32_t timeout) { int eos_sdc_sect_read(uint32_t sect, unsigned int count, uint8_t *buffer) { int rv; - uint64_t start; uint8_t cmd = ((count == 1) ? READ_SINGLE_BLOCK : READ_MULTIPLE_BLOCK); if (!(sdc_type & EOS_SDC_CAP_BLK)) sect *= 512; - start = eos_time_get_tick(); sdc_select(); rv = sdc_cmd(cmd, sect, SDC_CMD_FLAG_NOCS, SDC_TIMEOUT_CMD); @@ -498,10 +496,8 @@ int eos_sdc_sect_read(uint32_t sect, unsigned int count, uint8_t *buffer) { int eos_sdc_sect_write(uint32_t sect, unsigned int count, uint8_t *buffer) { int rv; - uint64_t start; if (!(sdc_type & EOS_SDC_CAP_BLK)) sect *= 512; - start = eos_time_get_tick(); if (count == 1) { sdc_select(); diff --git a/fw/fe310/eos/sdcard.h b/fw/fe310/eos/dev/sdcard.h index 910a6e0..910a6e0 100644 --- a/fw/fe310/eos/sdcard.h +++ b/fw/fe310/eos/dev/sdcard.h diff --git a/fw/fe310/eos/spi_dev.c b/fw/fe310/eos/dev/spi.c index c0c21b0..12549fc 100644 --- a/fw/fe310/eos/spi_dev.c +++ b/fw/fe310/eos/dev/spi.c @@ -6,16 +6,17 @@ #include "eos.h" #include "msgq.h" -#include "interrupt.h" #include "event.h" #include "board.h" +#include "soc/interrupt.h" +#include "soc/spi.h" + #include "net.h" + #include "spi.h" -#include "spi_priv.h" #include "spi_cfg.h" -#include "spi_dev.h" static uint8_t spi_dev; static uint8_t spi_lock; @@ -26,10 +27,8 @@ int eos_spi_dev_init(uint8_t wakeup_cause) { for (i=0; i<EOS_SPI_MAX_DEV; i++) { spi_div[i] = spi_cfg[i].div; - if (spi_cfg[i].cspin != SPI_CSPIN_NONE) { + if (!(spi_cfg[i].flags & SPI_DEV_FLAG_CSFLOAT) && (spi_cfg[i].cspin != -1)) { GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << spi_cfg[i].cspin); - - GPIO_REG(GPIO_INPUT_EN) &= ~(1 << spi_cfg[i].cspin); GPIO_REG(GPIO_OUTPUT_EN) |= (1 << spi_cfg[i].cspin); } } @@ -40,17 +39,26 @@ int eos_spi_dev_init(uint8_t wakeup_cause) { int eos_spi_select(unsigned char dev) { if (spi_lock) return EOS_ERR_BUSY; - if (spi_dev == EOS_SPI_DEV_NET) { - eos_net_stop(); + if (spi_cfg[spi_dev].flags & SPI_DEV_FLAG_9BIT) { + eos_spi_enable(); } else { - eos_spi_stop(); + if (spi_dev == EOS_SPI_DEV_NET) { + eos_net_stop(); + } else { + eos_spi_stop(); + } } spi_dev = dev; - if (dev == EOS_SPI_DEV_NET) { - eos_net_start(); + if (spi_cfg[dev].flags & SPI_DEV_FLAG_9BIT) { + eos_spi_configure(spi_div[dev], spi_cfg[dev].csid, spi_cfg[dev].cspin, spi_cfg[dev].evt); + eos_spi_disable(); } else { - eos_spi_start(spi_div[dev], spi_cfg[dev].csid, spi_cfg[dev].cspin, spi_cfg[dev].evt); + if (dev == EOS_SPI_DEV_NET) { + eos_net_start(); + } else { + eos_spi_start(spi_div[dev], spi_cfg[dev].csid, spi_cfg[dev].cspin, spi_cfg[dev].evt); + } } return EOS_OK; @@ -60,7 +68,11 @@ int eos_spi_deselect(void) { if (spi_lock) return EOS_ERR_BUSY; if (spi_dev == EOS_SPI_DEV_NET) return EOS_ERR; - eos_spi_stop(); + if (spi_cfg[spi_dev].flags & SPI_DEV_FLAG_9BIT) { + eos_spi_enable(); + } else { + eos_spi_stop(); + } spi_dev = EOS_SPI_DEV_NET; eos_net_start(); @@ -68,6 +80,10 @@ int eos_spi_deselect(void) { return EOS_OK; } +void eos_spi_dev_configure(unsigned char dev) { + eos_spi_configure(spi_div[dev], spi_cfg[dev].csid, spi_cfg[dev].cspin, spi_cfg[dev].evt); +} + uint8_t eos_spi_dev(void) { return spi_dev; } diff --git a/fw/fe310/eos/spi_dev.h b/fw/fe310/eos/dev/spi.h index e801f7e..c899e83 100644 --- a/fw/fe310/eos/spi_dev.h +++ b/fw/fe310/eos/dev/spi.h @@ -4,10 +4,12 @@ #define EOS_SPI_DEV_EVE 1 #define EOS_SPI_DEV_SDC 2 #define EOS_SPI_DEV_CAM 3 +#define EOS_SPI_DEV_LCD 4 int eos_spi_dev_init(uint8_t wakeup_cause); int eos_spi_select(unsigned char dev); int eos_spi_deselect(void); +void eos_spi_dev_configure(unsigned char dev); uint8_t eos_spi_dev(void); uint16_t eos_spi_div(unsigned char dev); diff --git a/fw/fe310/eos/spi_cfg.h b/fw/fe310/eos/dev/spi_cfg.h index 84ab8bb..6a5d7b4 100644 --- a/fw/fe310/eos/spi_cfg.h +++ b/fw/fe310/eos/dev/spi_cfg.h @@ -1,37 +1,52 @@ #include <stdint.h> -#define EOS_SPI_MAX_DEV 4 +#define EOS_SPI_MAX_DEV 5 typedef struct { uint16_t div; - uint8_t csid; - uint8_t cspin; + int8_t csid; + int8_t cspin; + uint8_t flags; unsigned char evt; } SPIConfig; +#define SPI_DEV_FLAG_9BIT 0x1 +#define SPI_DEV_FLAG_CSFLOAT 0x2 + static const SPIConfig spi_cfg[EOS_SPI_MAX_DEV] = { { // DEV_NET .div = SPI_DIV_NET, .csid = SPI_CSID_NET, .cspin = SPI_CSPIN_NET, + .flags = 0, .evt = 0, // Not SPI event }, { // DEV_EVE .div = SPI_DIV_EVE, .csid = SPI_CSID_EVE, .cspin = SPI_CSPIN_EVE, + .flags = 0, .evt = 0, }, { // DEV_SDC .div = SPI_DIV_SDC, .csid = SPI_CSID_SDC, .cspin = SPI_CSPIN_SDC, + .flags = 0, .evt = EOS_SPI_EVT_SDC, }, { // DEV_CAM .div = SPI_DIV_CAM, .csid = SPI_CSID_CAM, .cspin = SPI_CSPIN_CAM, + .flags = 0, .evt = EOS_SPI_EVT_CAM, }, + { // DEV_LCD 9bit spi + .div = 0, + .csid = -1, + .cspin = SPI_CSPIN_LCD, + .flags = SPI_DEV_FLAG_9BIT | SPI_DEV_FLAG_CSFLOAT, + .evt = 0, + }, }; diff --git a/fw/fe310/eos/eos.c b/fw/fe310/eos/eos.c index b52ed93..03609ae 100644 --- a/fw/fe310/eos/eos.c +++ b/fw/fe310/eos/eos.c @@ -1,31 +1,29 @@ #include <stdio.h> #include "event.h" -#include "interrupt.h" -#include "timer.h" -#include "pwr.h" -#include "i2s.h" -#include "i2c.h" -#include "uart.h" -#include "spi.h" -#include "spi_dev.h" -#include "lcd.h" -#include "sdcard.h" -#include "net.h" -#include "wifi.h" -#include "cell.h" -#include "sock.h" -#include "i2c/bq25895.h" -#include "eve/eve_eos.h" - -#include "board.h" +#include "soc/interrupt.h" +#include "soc/timer.h" +#include "soc/pwr.h" +#include "soc/i2s.h" +#include "soc/i2c.h" +#include "soc/uart.h" +#include "soc/spi.h" + +#include "dev/spi.h" +#include "dev/net.h" +#include "dev/sdcard.h" +#include "dev/bq25895.h" +#include "dev/eve.h" + +#include "net/pwr.h" +#include "net/wifi.h" +#include "net/sock.h" +#include "net/cell.h" #include "eos.h" void eos_init(void) { uint8_t wakeup_cause = eos_pwr_wakeup_cause(); - uint32_t touch_matrix[6] = {0xfa46,0xfffffcf6,0x422fe,0xffffff38,0x10002,0xf3cb0}; - int touch_calibrate = 0; int rv; eos_evtq_init(wakeup_cause); @@ -45,26 +43,19 @@ void eos_init(void) { if (rv) printf("SPI INIT ERR:%d\n", rv); rv = eos_spi_dev_init(wakeup_cause); if (rv) printf("SPI DEV INIT ERR:%d\n", rv); - rv = eos_sdc_init(wakeup_cause); - if (rv) printf("SDC INIT ERR:%d\n", rv); - rv = eos_net_init(wakeup_cause); - if (rv) printf("NET INIT ERR:%d\n", rv); - eos_i2c_start(); rv = eos_bq25895_init(wakeup_cause); - eos_i2c_stop(); if (rv) printf("BQ25895 INIT ERR:%d\n", rv); - rv = eos_eve_init(wakeup_cause, EVE_GPIO_DIR, touch_calibrate, touch_matrix); - if (rv) printf("EVE INIT ERR:%d\n", rv); + rv = eos_net_init(wakeup_cause); + if (rv) printf("NET INIT ERR:%d\n", rv); - rv = eos_lcd_init(wakeup_cause); - if (rv) printf("LCD INIT ERR:%d\n", rv); + rv = eos_sdc_init(wakeup_cause); + if (rv) printf("SDC INIT ERR:%d\n", rv); + + rv = eos_eve_init(wakeup_cause); + if (rv) printf("EVE INIT ERR:%d\n", rv); - if (touch_calibrate) { - printf("TOUCH MATRIX:\n"); - printf("uint32_t touch_matrix[6] = {0x%x,0x%x,0x%x,0x%x,0x%x,0x%x}\n", touch_matrix[0], touch_matrix[1], touch_matrix[2], touch_matrix[3], touch_matrix[4], touch_matrix[5]); - } eos_run(wakeup_cause); } @@ -74,10 +65,14 @@ void eos_run(uint8_t wakeup_cause) { rv = eos_eve_run(wakeup_cause); if (rv) printf("EVE RUN ERR:%d\n", rv); - eos_pwr_netinit(); - eos_wifi_netinit(); - eos_cell_netinit(); - eos_sock_netinit(); + eos_pwr_net_init(); + eos_wifi_init(); + eos_sock_init(); + eos_cell_init(); rv = eos_net_run(wakeup_cause); if (rv) printf("NET RUN ERR:%d\n", rv); } + +void eos_run_once(void) { + eos_eve_calibrate(); +} diff --git a/fw/fe310/eos/eos.h b/fw/fe310/eos/eos.h index 87a7e27..f3caf6b 100644 --- a/fw/fe310/eos/eos.h +++ b/fw/fe310/eos/eos.h @@ -9,8 +9,10 @@ #define EOS_ERR_FULL -11 #define EOS_ERR_EMPTY -12 #define EOS_ERR_NOTFOUND -13 +#define EOS_ERR_ABSENT -14 #define EOS_ERR_NET -20 void eos_init(void); -void eos_run(uint8_t wakeup_cause);
\ No newline at end of file +void eos_run(uint8_t wakeup_cause); +void eos_run_once(void);
\ No newline at end of file diff --git a/fw/fe310/eos/eve/Makefile b/fw/fe310/eos/eve/Makefile index 323a132..650bff8 100644 --- a/fw/fe310/eos/eve/Makefile +++ b/fw/fe310/eos/eve/Makefile @@ -1,7 +1,8 @@ include ../../common.mk -CFLAGS += -I$(bsp_dir)/include -obj = eve.o eve_eos.o eve_platform.o eve_touch.o eve_phy.o eve_vtrack.o eve_font.o eve_kbd.o eve_text.o clipb.o +obj = eve.o eve_platform.o eve_touch.o eve_touch_engine.o eve_phy.o eve_vtrack.o eve_font.o eve_kbd.o eve_text.o clipb.o +subdirs = screen widget +lib = ../../libeve.a %.o: %.c %.h @@ -10,7 +11,16 @@ obj = eve.o eve_eos.o eve_platform.o eve_touch.o eve_phy.o eve_vtrack.o eve_font %.o: %.S $(CC) $(CFLAGS) -c $< -all: $(obj) +all: $(lib) + +$(lib): $(obj) + for i in $(subdirs); do \ + (cd $$i && $(MAKE)) || exit; \ + done + $(AR) rcs $@ $(obj) screen/*.o widget/*.o clean: - rm -f *.o + for i in $(subdirs); do \ + (cd $$i && $(MAKE) clean) || exit; \ + done + rm -f *.o $(lib) diff --git a/fw/fe310/eos/eve/eve.c b/fw/fe310/eos/eve/eve.c index e1191fc..5103e5f 100644 --- a/fw/fe310/eos/eve/eve.c +++ b/fw/fe310/eos/eve/eve.c @@ -13,6 +13,7 @@ static char dl_burst; static uint32_t dl_addr; static uint8_t power_state; +static int lcd_absent = 0; void eve_command(uint8_t command, uint8_t parameter) { eve_spi_cs_set(); @@ -289,6 +290,7 @@ int eve_cmd_exec(int w) { void eve_cmd_burst_start(void) { uint32_t addr = EVE_RAM_CMD + cmd_offset; + eve_spi_lock(); eve_spi_cs_set(); eve_spi_xchg24(addr | EVE_MEM_WRITE, EVE_SPI_FLAG_TX); @@ -306,10 +308,10 @@ void eve_handle_intr(void) { uint16_t intr_flags; intr_flags = eve_read16(REG_INT_FLAGS); - eve_handle_touch(intr_flags); + if (intr_flags & (EVE_INT_CONVCOMPLETE | EVE_INT_TAG)) eve_handle_touch(intr_flags); } -int eve_init(uint8_t gpio_dir, int touch_calibrate, uint32_t *touch_matrix) { +int eve_init(void) { uint8_t chipid = 0; uint8_t reset = 0x07; uint16_t timeout; @@ -319,11 +321,11 @@ int eve_init(uint8_t gpio_dir, int touch_calibrate, uint32_t *touch_matrix) { eve_command(EVE_CLKEXT, 0); eve_command(EVE_CLKSEL, 0x46); /* set clock to 72 MHz */ eve_command(EVE_ACTIVE, 0); /* start EVE */ - eve_time_sleep(4); + eve_sleep(4); timeout = 0; while (chipid != 0x7c) { /* if chipid is not 0x7c, continue to read it until it is, EVE needs a moment for it's power on self-test and configuration */ - eve_time_sleep(1); + eve_sleep(1); chipid = eve_read8(REG_ID); timeout++; if (timeout > 400) return EVE_ERR; @@ -331,7 +333,7 @@ int eve_init(uint8_t gpio_dir, int touch_calibrate, uint32_t *touch_matrix) { timeout = 0; while (reset != 0x00) { /* check if EVE is in working status */ - eve_time_sleep(1); + eve_sleep(1); reset = eve_read8(REG_CPURESET) & 0x07; timeout++; if(timeout > 50) return EVE_ERR; @@ -341,7 +343,7 @@ int eve_init(uint8_t gpio_dir, int touch_calibrate, uint32_t *touch_matrix) { eve_write8(REG_PWM_DUTY, 0x00); eve_write16(REG_GPIOX, 0x0000); - eve_write16(REG_GPIOX_DIR, 0x8000 | (gpio_dir & 0x0f)); + eve_write16(REG_GPIOX_DIR, 0x8000); /* initialize display */ eve_write16(REG_HCYCLE, EVE_HCYCLE); /* total number of clocks per line, incl front/back porch */ @@ -361,7 +363,8 @@ int eve_init(uint8_t gpio_dir, int touch_calibrate, uint32_t *touch_matrix) { /* do not set PCLK yet - wait for just after the first display list */ /* disable audio */ - eve_write16(REG_SOUND, 0x0000); /* set synthesizer to silence */ + eve_write16(REG_SOUND, 0x0060); /* set synthesizer to mute */ + eve_write8(REG_PLAY, 0x01); eve_write8(REG_VOL_SOUND, 0x00); /* turn synthesizer volume off */ eve_write8(REG_VOL_PB, 0x00); /* turn recorded audio volume off */ @@ -381,21 +384,16 @@ int eve_init(uint8_t gpio_dir, int touch_calibrate, uint32_t *touch_matrix) { #endif /* nothing is being displayed yet... the pixel clock is still 0x00 */ - eve_touch_init(touch_calibrate, touch_matrix); return EVE_OK; } void eve_start(void) { - eve_touch_start(); - /* enable interrupts */ eve_write8(REG_INT_EN, 0x01); while(eve_read8(REG_INT_FLAGS)); } void eve_stop(void) { - eve_touch_stop(); - /* disable interrupts */ eve_write8(REG_INT_EN, 0x00); while(eve_read8(REG_INT_FLAGS)); @@ -417,12 +415,12 @@ void eve_stop_clk(void) { eve_write8(REG_PCLK, 0); } -void eve_active(void) { +void eve_activate(void) { eve_command(EVE_ACTIVE, 0); - eve_time_sleep(40); + eve_sleep(40); } -void eve_standby(void) { +void eve_pwr_standby(void) { if (power_state != EVE_PSTATE_ACTIVE) return; eve_command(EVE_STANDBY, 0); @@ -430,7 +428,7 @@ void eve_standby(void) { power_state = EVE_PSTATE_STANDBY; } -void eve_sleep(void) { +void eve_pwr_sleep(void) { if (power_state != EVE_PSTATE_ACTIVE) return; eve_stop_clk(); @@ -441,8 +439,8 @@ void eve_sleep(void) { power_state = EVE_PSTATE_SLEEP; } -void eve_wake(void) { - eve_active(); +void eve_pwr_wake(void) { + eve_activate(); if (power_state == EVE_PSTATE_SLEEP) { eve_start(); @@ -452,10 +450,6 @@ void eve_wake(void) { power_state = EVE_PSTATE_ACTIVE; } -void eve_brightness(uint8_t b) { - eve_write8(REG_PWM_DUTY, b); -} - int eve_gpio_get(int gpio) { uint16_t reg = eve_read16(REG_GPIOX); @@ -483,3 +477,12 @@ void eve_gpio_set_dir(uint8_t dir) { reg |= dir & 0x0f; eve_write16(REG_GPIOX_DIR, reg); } + +void eve_brightness(uint8_t b) { + if (lcd_absent) b = 0; + eve_write8(REG_PWM_DUTY, b); +} + +void eve_lcd_absent(void) { + lcd_absent = 1; +} diff --git a/fw/fe310/eos/eve/eve.h b/fw/fe310/eos/eve/eve.h index 02fa697..c1b91e4 100644 --- a/fw/fe310/eos/eve/eve.h +++ b/fw/fe310/eos/eve/eve.h @@ -58,19 +58,21 @@ void eve_cmd_burst_end(void); void eve_handle_intr(void); -int eve_init(uint8_t gpio_dir, int touch_calibrate, uint32_t *touch_matrix); +int eve_init(void); void eve_start(void); void eve_stop(void); void eve_start_clk(void); void eve_stop_clk(void); -void eve_active(void); -void eve_standby(void); -void eve_sleep(void); -void eve_wake(void); +void eve_activate(void); +void eve_pwr_standby(void); +void eve_pwr_sleep(void); +void eve_pwr_wake(void); int eve_gpio_get(int gpio); void eve_gpio_set(int gpio, int val); uint8_t eve_gpio_get_dir(void); void eve_gpio_set_dir(uint8_t dir); + void eve_brightness(uint8_t b); +void eve_lcd_absent(void);
\ No newline at end of file diff --git a/fw/fe310/eos/eve/eve_def.h b/fw/fe310/eos/eve/eve_def.h index 2f7e0b7..b7110b9 100755 --- a/fw/fe310/eos/eve/eve_def.h +++ b/fw/fe310/eos/eve/eve_def.h @@ -697,6 +697,10 @@ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR TH #define REG_PLAY_CONTROL 0x30914eUL
#define REG_COPRO_PATCH_PTR 0x309162UL
+#define REG_EHOST_TOUCH_X 0x30210cUL
+#define REG_EHOST_TOUCH_Y 0x302118UL
+#define REG_EHOST_TOUCH_ID 0x302114UL
+#define REG_EHOST_TOUCH_ACK 0x302170UL
/* BT81x graphics engine specific macros */
#define BITMAP_EXT_FORMAT(format) ((46UL<<24)|(((format)&65535UL)<<0))
diff --git a/fw/fe310/eos/eve/eve_eos.c b/fw/fe310/eos/eve/eve_eos.c deleted file mode 100644 index 2e13cd2..0000000 --- a/fw/fe310/eos/eve/eve_eos.c +++ /dev/null @@ -1,104 +0,0 @@ -#include <stdlib.h> - -#include "platform.h" - -#include "eos.h" -#include "interrupt.h" -#include "event.h" -#include "pwr.h" - -#include "board.h" - -#include "eve.h" -#include "eve_eos.h" - -static int _run; - -static void handle_time(unsigned char type) { - if (_run) { - eve_spi_start(); - eve_handle_time(); - eve_spi_stop(); - } -} - -static void handle_evt(unsigned char type, unsigned char *buffer, uint16_t len) { - if (_run) { - eve_spi_start(); - eve_handle_intr(); - eve_spi_stop(); - - GPIO_REG(GPIO_LOW_IE) |= (1 << EVE_PIN_INTR); - } -} - -static void handle_intr(void) { - GPIO_REG(GPIO_LOW_IE) &= ~(1 << EVE_PIN_INTR); - GPIO_REG(GPIO_LOW_IP) = (1 << EVE_PIN_INTR); - eos_evtq_push_isr(EOS_EVT_UI | EVE_ETYPE_INTR, NULL, 0); -} - - -static void _start(void) { - eve_start(); - - GPIO_REG(GPIO_INPUT_EN) |= (1 << EVE_PIN_INTR); - GPIO_REG(GPIO_OUTPUT_EN) &= ~(1 << EVE_PIN_INTR); - - GPIO_REG(GPIO_LOW_IE) |= (1 << EVE_PIN_INTR); - - eos_intr_enable(INT_GPIO_BASE + EVE_PIN_INTR); - _run = 1; -} - -static void _stop(void) { - _run = 0; - eos_intr_disable(INT_GPIO_BASE + EVE_PIN_INTR); - - GPIO_REG(GPIO_LOW_IE) &= ~(1 << EVE_PIN_INTR); - - eve_stop(); -} - -int eos_eve_init(uint8_t wakeup_cause, uint8_t gpio_dir, int touch_calibrate, uint32_t *touch_matrix) { - int rst = (wakeup_cause == EOS_PWR_WAKE_RST); - int rv = EVE_OK; - - eve_spi_start(); - if (rst) { - rv = eve_init(gpio_dir, touch_calibrate, touch_matrix); - } else { - eve_active(); - } - eve_spi_stop(); - - if (rv) return EOS_ERR; - - eos_evtq_set_handler(EOS_EVT_UI, handle_evt); - eos_timer_set_handler(EOS_TIMER_ETYPE_UI, handle_time); - eos_intr_set(INT_GPIO_BASE + EVE_PIN_INTR, IRQ_PRIORITY_UI, handle_intr); - eos_intr_disable(INT_GPIO_BASE + EVE_PIN_INTR); - - return EOS_OK; -} - -int eos_eve_run(uint8_t wakeup_cause) { - eve_spi_start(); - _start(); - eve_start_clk(); - eve_spi_stop(); - - return EOS_OK; -} - -void eos_eve_start(void) { - eve_spi_start(); - _start(); - eve_spi_stop(); -} - -void eos_eve_stop(void) { - eve_spi_start(); - _stop(); - eve_spi_stop(); -} diff --git a/fw/fe310/eos/eve/eve_eos.h b/fw/fe310/eos/eve/eve_eos.h deleted file mode 100644 index 80eea86..0000000 --- a/fw/fe310/eos/eve/eve_eos.h +++ /dev/null @@ -1,6 +0,0 @@ -#include <stdint.h> - -int eos_eve_init(uint8_t wakeup_cause, uint8_t gpio_dir, int touch_calibrate, uint32_t *touch_matrix); -int eos_eve_run(uint8_t wakeup_cause); -void eos_eve_start(void); -void eos_eve_stop(void); diff --git a/fw/fe310/eos/eve/eve_phy.c b/fw/fe310/eos/eve/eve_phy.c index 1e255fe..cfddc80 100644 --- a/fw/fe310/eos/eve/eve_phy.c +++ b/fw/fe310/eos/eve/eve_phy.c @@ -9,30 +9,44 @@ void eve_phy_acc_init(EVEPhyAcc *param, int a) { param->a = a; } -void eve_phy_acc_start(EVEPhyAcc *param, int x0, int y0, int v0x, int v0y) { - double v0 = sqrt(v0x * v0x + v0y * v0y); +int eve_phy_acc_start(EVEPhyAcc *param, int x0, int y0, uint32_t t0, int v0x, int v0y) { + double v0; + uint32_t dt = eve_get_tick() - t0; + v0 = sqrt(v0x * v0x + v0y * v0y); + param->k = 2 * v0 / param->a * EVE_RTC_FREQ; + if ((param->k < 0) && (dt >= -param->k / 2)) { + return 0; + } param->x0 = x0; param->y0 = y0; param->v0x = v0x; param->v0y = v0y; - param->k = 2 * v0 / param->a * EVE_RTC_FREQ; + + return 1; } -int eve_phy_acc_tick(EVEPhyAcc *param, int dt, int *x, int *y) { +int eve_phy_acc_tick(EVEPhyAcc *param, uint32_t dt, int *x, int *y) { int k = param->k; int x0 = param->x0; int y0 = param->y0; int v0x = param->v0x; int v0y = param->v0y; + int _dt = dt; int more = 1; + if (k == 0) { + if (x) *x = x0; + if (y) *y = y0; + return 0; + } + if ((k < 0) && (dt >= -k / 2)) { dt = -k / 2; more = 0; } - if (x) *x = x0 + (v0x * dt + v0x * dt / k * dt) / (int)(EVE_RTC_FREQ); - if (y) *y = y0 + (v0y * dt + v0y * dt / k * dt) / (int)(EVE_RTC_FREQ); + if (x) *x = x0 + (v0x * _dt + v0x * _dt / k * _dt) / EVE_RTC_FREQ; + if (y) *y = y0 + (v0y * _dt + v0y * _dt / k * _dt) / EVE_RTC_FREQ; return more; } @@ -53,9 +67,11 @@ void eve_phy_lho_init(EVEPhyLHO *param, int x, int y, uint32_t T, double d, uint int eve_phy_lho_start(EVEPhyLHO *param, int x0, int y0) { param->x0 = x0; param->y0 = y0; + + return 1; } -int eve_phy_lho_tick(EVEPhyLHO *param, int dt, int *x, int *y) { +int eve_phy_lho_tick(EVEPhyLHO *param, uint32_t dt, int *x, int *y) { int ax = param->x0 - param->x; int ay = param->y0 - param->y; int more = 1; diff --git a/fw/fe310/eos/eve/eve_phy.h b/fw/fe310/eos/eve/eve_phy.h index 1be5fd0..e747b44 100644 --- a/fw/fe310/eos/eve/eve_phy.h +++ b/fw/fe310/eos/eve/eve_phy.h @@ -10,8 +10,8 @@ typedef struct EVEPhyAcc { } EVEPhyAcc; void eve_phy_acc_init(EVEPhyAcc *param, int a); -void eve_phy_acc_start(EVEPhyAcc *param, int x0, int y0, int v0x, int v0y); -int eve_phy_acc_tick(EVEPhyAcc *param, int dt, int *x, int *y); +int eve_phy_acc_start(EVEPhyAcc *param, int x0, int y0, uint32_t t0, int v0x, int v0y); +int eve_phy_acc_tick(EVEPhyAcc *param, uint32_t dt, int *x, int *y); typedef struct EVEPhyLHO { int x; @@ -25,4 +25,4 @@ typedef struct EVEPhyLHO { void eve_phy_lho_init(EVEPhyLHO *param, int x, int y, uint32_t T, double d, uint32_t t_max); int eve_phy_lho_start(EVEPhyLHO *param, int x0, int y0); -int eve_phy_lho_tick(EVEPhyLHO *param, int dt, int *x, int *y);
\ No newline at end of file +int eve_phy_lho_tick(EVEPhyLHO *param, uint32_t dt, int *x, int *y);
\ No newline at end of file diff --git a/fw/fe310/eos/eve/eve_platform.c b/fw/fe310/eos/eve/eve_platform.c index 4450412..f0fc399 100644 --- a/fw/fe310/eos/eve/eve_platform.c +++ b/fw/fe310/eos/eve/eve_platform.c @@ -17,19 +17,19 @@ void eve_free(void *p) { free(p); } -void eve_timer_set(uint32_t ms) { - eos_timer_set(ms, EOS_TIMER_ETYPE_UI); +void eve_sleep(uint32_t ms) { + eos_time_sleep(ms); } -void eve_time_sleep(uint32_t ms) { - eos_time_sleep(ms); +uint32_t eve_get_tick(void) { + return (uint32_t)eos_time_get_tick(); } -uint64_t eve_time_get_tick(void) { - return eos_time_get_tick(); +void eve_sys_timer_set(uint32_t ms) { + eos_timer_set(EOS_TIMER_ETYPE_UI, ms); } -void eve_timer_clear(void) { +void eve_sys_timer_clear(void) { eos_timer_clear(EOS_TIMER_ETYPE_UI); } diff --git a/fw/fe310/eos/eve/eve_platform.h b/fw/fe310/eos/eve/eve_platform.h index 77afffc..29240d5 100644 --- a/fw/fe310/eos/eve/eve_platform.h +++ b/fw/fe310/eos/eve/eve_platform.h @@ -1,9 +1,9 @@ #include <stdint.h> #include <stdlib.h> -#include "timer.h" -#include "spi.h" -#include "spi_dev.h" +#include "../soc/timer.h" +#include "../soc/spi.h" +#include "../dev/spi.h" #define EVE_ETYPE_INTR 1 @@ -31,7 +31,7 @@ void eve_spi_stop(void); #define eve_spi_lock eos_spi_lock #define eve_spi_unlock eos_spi_unlock -void eve_time_sleep(uint32_t ms); -uint64_t eve_time_get_tick(void); -void eve_timer_set(uint32_t ms); -void eve_timer_clear(void); +void eve_sleep(uint32_t ms); +uint32_t eve_get_tick(void); +void eve_sys_timer_set(uint32_t ms); +void eve_sys_timer_clear(void); diff --git a/fw/fe310/eos/eve/eve_touch.c b/fw/fe310/eos/eve/eve_touch.c index 7c06f81..d17a509 100644 --- a/fw/fe310/eos/eve/eve_touch.c +++ b/fw/fe310/eos/eve/eve_touch.c @@ -1,10 +1,9 @@ #include <stdlib.h> #include <string.h> -#include <math.h> #include "eve.h" +#include "eve_touch_engine.h" -static int touch_multi; static uint8_t touch_tag0; static EVETouch touch_obj[EVE_MAX_TOUCH]; @@ -14,235 +13,222 @@ static eve_touch_handler_t touch_handler; static void *touch_handler_param; static uint8_t touch_tag_opt[256]; -static const uint32_t _reg_touch[] = { - REG_CTOUCH_TOUCH0_XY, - REG_CTOUCH_TOUCH1_XY, - REG_CTOUCH_TOUCH2_XY, - REG_CTOUCH_TOUCH3_XY -}; - -static const uint32_t _reg_tag[] = { - REG_TOUCH_TAG, - REG_TOUCH_TAG1, - REG_TOUCH_TAG2, - REG_TOUCH_TAG3, - REG_TOUCH_TAG4 -}; - -static const uint32_t _reg_track[] = { - REG_TRACKER, - REG_TRACKER_1, - REG_TRACKER_2, - REG_TRACKER_3, - REG_TRACKER_4 -}; +void eve_touch_init(void) { + int i; + + touch_tag0 = 0; + memset(&touch_timer, 0, sizeof(touch_timer)); + for (i=0; i<EVE_MAX_TOUCH; i++) { + EVETouch *touch = &touch_obj[i]; + + memset(&touch_obj[i], 0, sizeof(EVETouch)); + touch->eevt |= EVE_TOUCH_EETYPE_NOTOUCH; + } + eve_vtrack_init(); +} void eve_handle_touch(uint16_t intr_flags) { int i; - char touch_ex = 0; - char int_ccomplete = 0; - uint16_t intr_mask; - - intr_mask = eve_read16(REG_INT_MASK); - if (!touch_multi && (intr_flags & EVE_INT_TOUCH)) touch_multi = 1; for (i=0; i<EVE_MAX_TOUCH; i++) { - uint8_t touch_tag; - uint32_t touch_xy; - uint64_t now = 0; + uint32_t now = 0; uint16_t touch_evt = 0; EVETouch *touch = &touch_obj[i]; - touch_xy = i < 4 ? eve_read32(_reg_touch[i]) : (((uint32_t)eve_read16(REG_CTOUCH_TOUCH4_X) << 16) | eve_read16(REG_CTOUCH_TOUCH4_Y)); - - if (touch_xy != EVE_NOTOUCH) { - int16_t touch_x = touch_xy >> 16; - int16_t touch_y = touch_xy & 0xffff; - now = eve_time_get_tick(); - if (touch->eevt & EVE_TOUCH_EETYPE_NOTOUCH) { - uint16_t _evt = 0; - uint16_t _eevt = 0; - uint16_t _ttevt = eve_touch_timer_get_evt(touch); - - if (_ttevt) { - touch->eevt &= ~EVE_TOUCH_EETYPE_NOTOUCH; + now = eve_get_tick(); + + if (intr_flags & EVE_INT_CONVCOMPLETE) { + uint32_t touch_xy = eve_touch_reg_xy(i); + uint16_t timer_evt; + + if (touch_xy != EVE_NOTOUCH) { + int16_t touch_x = touch_xy >> 16; + int16_t touch_y = touch_xy & 0xffff; + int check_track, check_timer; + + if (touch->eevt & EVE_TOUCH_EETYPE_NOTOUCH) { + uint16_t _evt = 0; + uint16_t _eevt = 0; + + timer_evt = eve_timer_get_evt(touch); + if (timer_evt) { + uint16_t _touch_evt = 0; + + touch->eevt &= ~EVE_TOUCH_EETYPE_NOTOUCH; + if (timer_evt & EVE_TOUCH_ETYPE_TAP2) { + int dx = touch_x - touch->x0; + int dy = touch_y - touch->y0; + + dx = dx < 0 ? -dx : dx; + dy = dy < 0 ? -dy : dy; + if ((dx > EVE_TOUCH_THRESHOLD_X) || (dy > EVE_TOUCH_THRESHOLD_Y)) { + _touch_evt |= EVE_TOUCH_ETYPE_TAP1; + } else { + _evt |= EVE_TOUCH_ETYPE_TAP2; + _eevt |= EVE_TOUCH_EETYPE_TAP2; + } + } + if (timer_evt & EVE_TOUCH_ETYPE_TRACK) { + EVEVTrack *vtrack = eve_vtrack_get(); - if (_ttevt & EVE_TOUCH_ETYPE_TAP2) { - int dx = touch_x - touch->x0; - int dy = touch_y - touch->y0; + _eevt |= EVE_TOUCH_EETYPE_TRACK_ABORT; + _touch_evt |= (EVE_TOUCH_ETYPE_TRACK_STOP | EVE_TOUCH_ETYPE_TRACK_ABORT); + if (vtrack->stop) vtrack->stop(touch, vtrack->param); + } + if (timer_evt & EVE_TOUCH_ETYPE_TIMER) { + _eevt |= EVE_TOUCH_EETYPE_TIMER_ABORT; + _touch_evt |= EVE_TOUCH_ETYPE_TIMER_ABORT; + } - dx = dx < 0 ? -dx : dx; - dy = dy < 0 ? -dy : dy; - if ((dx > EVE_TOUCH_THRESHOLD_X) || (dy > EVE_TOUCH_THRESHOLD_Y)) { - touch_evt |= EVE_TOUCH_ETYPE_TAP1; - } else { - _evt |= EVE_TOUCH_ETYPE_TAP2; - _eevt |= EVE_TOUCH_EETYPE_TAP2; + eve_timer_clear(touch); + if (touch_handler && _touch_evt) { + touch_handler(touch_timer.touch, _touch_evt, touch_timer.tag0, touch_handler_param); } } - if (_ttevt & EVE_TOUCH_ETYPE_TRACK) { - EVEVTrack *vtrack = eve_vtrack_get(); - - _eevt |= EVE_TOUCH_EETYPE_TRACK_ABORT; - touch_evt |= (EVE_TOUCH_ETYPE_TRACK_STOP | EVE_TOUCH_ETYPE_TRACK_ABORT); - if (vtrack->stop) vtrack->stop(touch, vtrack->param); + touch_evt |= EVE_TOUCH_ETYPE_POINT | _evt; + touch->eevt = _eevt; + touch->tag0 = 0; + touch->tag = 0; + touch->tag_up = 0; + touch->tracker.tag = 0; + touch->tracker.track = 0; + touch->tracker.val = 0; + touch->t = 0; + touch->vx = 0; + touch->vy = 0; + touch->x0 = touch_x; + touch->y0 = touch_y; + } else if (touch->tracker.track) { + uint32_t dt = now - touch->t; + int vx = ((int)touch_x - touch->x) * EVE_RTC_FREQ / (int)dt; + int vy = ((int)touch_y - touch->y) * EVE_RTC_FREQ / (int)dt; + + touch->vx = vx; + touch->vy = vy; + touch->t = now; + } + touch->x = touch_x; + touch->y = touch_y; + + timer_evt = eve_timer_get_evt(touch); + check_track = touch->tracker.tag && !touch->tracker.track; + check_timer = timer_evt & (EVE_TOUCH_ETYPE_LPRESS | EVE_TOUCH_ETYPE_TAP2); + + if (check_track || check_timer) { + int dx = touch->x - touch->x0; + int dy = touch->y - touch->y0; + + dx = dx < 0 ? -dx : dx; + dy = dy < 0 ? -dy : dy; + if (check_track) { + if ((dx > EVE_TOUCH_THRESHOLD_X) && !(touch_tag_opt[touch->tracker.tag] & EVE_TOUCH_OPT_TRACK_X)) { + touch->tracker.tag = 0; + } + if ((dy > EVE_TOUCH_THRESHOLD_Y) && !(touch_tag_opt[touch->tracker.tag] & EVE_TOUCH_OPT_TRACK_Y)) { + touch->tracker.tag = 0; + } + if (touch->tracker.tag && ((dx > EVE_TOUCH_THRESHOLD_X) || (dy > EVE_TOUCH_THRESHOLD_Y))) { + int track_x = 0; + + if ((dx > EVE_TOUCH_THRESHOLD_X) && (dx > EVE_TOUCH_THRESHOLD_Y) && ((touch_tag_opt[touch->tracker.tag] & EVE_TOUCH_OPT_TRACK_XY) == EVE_TOUCH_OPT_TRACK_XY)) { + if (dx > dy) { + track_x = 1; + } + } else if (dx > EVE_TOUCH_THRESHOLD_X) { + track_x = 1; + } + if (track_x) { + touch->eevt |= touch->x > touch->x0 ? EVE_TOUCH_EETYPE_TRACK_RIGHT : EVE_TOUCH_EETYPE_TRACK_LEFT; + } else { + touch->eevt |= touch->y > touch->y0 ? EVE_TOUCH_EETYPE_TRACK_DOWN : EVE_TOUCH_EETYPE_TRACK_UP; + } + touch_evt |= EVE_TOUCH_ETYPE_TRACK_START; + touch->tracker.track = 1; + touch->t = now; + } } - if (_ttevt & EVE_TOUCH_ETYPE_TIMER) { - _eevt |= EVE_TOUCH_EETYPE_TIMER_ABORT; - touch_evt |= EVE_TOUCH_ETYPE_TIMER_ABORT; + if (check_timer && ((dx > EVE_TOUCH_THRESHOLD_X) || (dy > EVE_TOUCH_THRESHOLD_Y))) { + eve_timer_set_evt(touch, timer_evt & ~(EVE_TOUCH_ETYPE_LPRESS | EVE_TOUCH_ETYPE_TAP2)); } - - eve_touch_timer_clear(touch); - if (touch_handler && touch_evt) { - touch_handler(touch_timer.touch, touch_evt, touch_timer.tag0, touch_handler_param); - } - } - touch_evt = EVE_TOUCH_ETYPE_POINT | _evt; - touch->eevt = _eevt; - touch->tag0 = 0; - touch->tag = 0; - touch->tag_up = 0; - touch->tracker.tag = 0; - touch->tracker.track = 0; - touch->tracker.val = 0; - touch->t = 0; - touch->vx = 0; - touch->vy = 0; - touch->x0 = touch_x; - touch->y0 = touch_y; - } else if (touch->t) { - int dt = now - touch->t; - int vx = ((int)touch_x - touch->x) * (int)(EVE_RTC_FREQ) / dt; - int vy = ((int)touch_y - touch->y) * (int)(EVE_RTC_FREQ) / dt; - touch->vx = touch->vx ? (vx + touch->vx * EVE_TOUCH_TRAVG) / (EVE_TOUCH_TRAVG + 1) : vx; - touch->vy = touch->vy ? (vy + touch->vy * EVE_TOUCH_TRAVG) / (EVE_TOUCH_TRAVG + 1) : vy; - touch->t = now; - } - touch->x = touch_x; - touch->y = touch_y; - if (touch_multi || (intr_flags & EVE_INT_TAG)) { - touch_tag = eve_read8(_reg_tag[i]); - } else { - touch_tag = touch->tag; - } - touch_ex = 1; - } else { - touch_tag = 0; - if (!(touch->eevt & EVE_TOUCH_EETYPE_NOTOUCH)) { - uint16_t _ttevt = eve_touch_timer_get_evt(touch); - - touch_evt = EVE_TOUCH_ETYPE_POINT_UP; - touch->eevt |= EVE_TOUCH_EETYPE_NOTOUCH; - if (_ttevt & EVE_TOUCH_ETYPE_LPRESS) { - eve_touch_timer_set_evt(touch, _ttevt & ~EVE_TOUCH_ETYPE_LPRESS); } if (touch->tracker.tag && touch->tracker.track) { - uint8_t opt = touch_tag_opt[touch->tracker.tag]; - char track_ext = ((opt & EVE_TOUCH_OPT_TRACK_EXT_X) && (touch->eevt & EVE_TOUCH_EETYPE_TRACK_X)) || - ((opt & EVE_TOUCH_OPT_TRACK_EXT_Y) && (touch->eevt & EVE_TOUCH_EETYPE_TRACK_Y)); - if (!eve_touch_timer_get_evt(NULL) && track_ext) { - EVEVTrack *vtrack = eve_vtrack_get(); - - eve_touch_timer_set(touch, EVE_TOUCH_ETYPE_TRACK, touch_tag0, EVE_TOUCH_TIMEOUT_TRACK); - if (vtrack->start) vtrack->start(touch, vtrack->param); + if (touch_tag_opt[touch->tracker.tag] & EVE_TOUCH_OPT_TRACK) touch_evt |= EVE_TOUCH_ETYPE_TRACK; + if (touch_tag_opt[touch->tracker.tag] & EVE_TOUCH_OPT_TRACK_REG) touch_evt |= EVE_TOUCH_ETYPE_TRACK_REG; + } + if (touch_evt & EVE_TOUCH_ETYPE_TRACK_REG) { + uint32_t touch_track = eve_touch_reg_track(i); + + if (touch->tracker.tag == (touch_track & 0xff)) { + touch->tracker.val = touch_track >> 16; } else { - touch_evt |= EVE_TOUCH_ETYPE_TRACK_STOP; + touch_evt &= ~EVE_TOUCH_ETYPE_TRACK_REG; } } - } - } - if (touch_tag != touch->tag) { - if (touch_tag) { - if (!touch_tag0) touch_tag0 = touch_tag; - if (!touch->tag0) { - touch->tag0 = touch_tag; - if (touch_tag_opt[touch_tag] & (EVE_TOUCH_OPT_TRACK | EVE_TOUCH_OPT_TRACK_REG)) { - touch->tracker.tag = touch_tag; - } - if (touch->tracker.tag && !(touch_tag_opt[touch->tracker.tag] & EVE_TOUCH_OPT_TRACK_XY)) { - touch_evt |= EVE_TOUCH_ETYPE_TRACK_START; - touch->tracker.track = 1; - touch->t = now; + } else { + if (!(touch->eevt & EVE_TOUCH_EETYPE_NOTOUCH)) { + touch_evt |= EVE_TOUCH_ETYPE_POINT_UP; + touch->eevt |= EVE_TOUCH_EETYPE_NOTOUCH; + + timer_evt = eve_timer_get_evt(touch); + if (timer_evt & EVE_TOUCH_ETYPE_LPRESS) { + eve_timer_set_evt(touch, timer_evt & ~EVE_TOUCH_ETYPE_LPRESS); } - if (!eve_touch_timer_get_evt(NULL) && (touch_tag_opt[touch_tag] & (EVE_TOUCH_OPT_LPRESS | EVE_TOUCH_OPT_TAP2))) { - uint16_t _evt = 0; + if (touch->tracker.tag && touch->tracker.track) { + int start = 0; + uint8_t opt = touch_tag_opt[touch->tracker.tag]; + uint8_t track_ext = ((opt & EVE_TOUCH_OPT_TRACK_EXT_X) && (touch->eevt & EVE_TOUCH_EETYPE_TRACK_X)) || + ((opt & EVE_TOUCH_OPT_TRACK_EXT_Y) && (touch->eevt & EVE_TOUCH_EETYPE_TRACK_Y)); + + if (!eve_timer_get_evt(NULL) && track_ext) { + EVEVTrack *vtrack = eve_vtrack_get(); - if (touch_tag_opt[touch_tag] & EVE_TOUCH_OPT_LPRESS) _evt |= EVE_TOUCH_ETYPE_LPRESS; - if (touch_tag_opt[touch_tag] & EVE_TOUCH_OPT_TAP2) _evt |= EVE_TOUCH_ETYPE_TAP2; - eve_touch_timer_set(touch, _evt, touch_tag0, EVE_TOUCH_TIMEOUT_TAP); + if (vtrack->start) start = vtrack->start(touch, vtrack->param); + } + if (start) { + eve_timer_set(touch, EVE_TOUCH_ETYPE_TRACK, touch_tag0, EVE_TOUCH_TIMEOUT_TRACK); + } else { + touch_evt |= EVE_TOUCH_ETYPE_TRACK_STOP; + } } } } - touch->tag_up = touch->tag; - if (touch->tag_up) touch_evt |= EVE_TOUCH_ETYPE_TAG_UP; - touch->tag = touch_tag; - if (touch->tag) touch_evt |= EVE_TOUCH_ETYPE_TAG; } - if (touch_xy != EVE_NOTOUCH) { - uint16_t _ttevt = eve_touch_timer_get_evt(touch); - int _track = touch->tracker.tag && !touch->tracker.track; - int _timer = _ttevt & (EVE_TOUCH_ETYPE_LPRESS | EVE_TOUCH_ETYPE_TAP2); - if (_track || _timer) { - int dx = touch->x - touch->x0; - int dy = touch->y - touch->y0; - dx = dx < 0 ? -dx : dx; - dy = dy < 0 ? -dy : dy; - if (_track) { - if ((dx > EVE_TOUCH_THRESHOLD_X) && !(touch_tag_opt[touch->tracker.tag] & EVE_TOUCH_OPT_TRACK_X)) { - touch->tracker.tag = 0; - } - if ((dy > EVE_TOUCH_THRESHOLD_Y) && !(touch_tag_opt[touch->tracker.tag] & EVE_TOUCH_OPT_TRACK_Y)) { - touch->tracker.tag = 0; - } - if (touch->tracker.tag && ((dx > EVE_TOUCH_THRESHOLD_X) || (dy > EVE_TOUCH_THRESHOLD_Y))) { - if (dx > EVE_TOUCH_THRESHOLD_X) { - touch->eevt |= touch->x > touch->x0 ? EVE_TOUCH_EETYPE_TRACK_RIGHT : EVE_TOUCH_EETYPE_TRACK_LEFT; + + if (intr_flags & EVE_INT_TAG) { + uint8_t touch_tag = eve_touch_reg_tag(i); + + if (touch_tag != touch->tag) { + if (touch_tag) { + if (!touch_tag0) touch_tag0 = touch_tag; + if (!touch->tag0) { + touch->tag0 = touch_tag; + if (touch_tag_opt[touch_tag] & (EVE_TOUCH_OPT_TRACK | EVE_TOUCH_OPT_TRACK_REG)) { + touch->tracker.tag = touch_tag; } - if (dy > EVE_TOUCH_THRESHOLD_Y) { - touch->eevt |= touch->y > touch->y0 ? EVE_TOUCH_EETYPE_TRACK_DOWN : EVE_TOUCH_EETYPE_TRACK_UP; + if (touch->tracker.tag && !(touch_tag_opt[touch->tracker.tag] & EVE_TOUCH_OPT_TRACK_XY)) { + touch_evt |= EVE_TOUCH_ETYPE_TRACK_START; + touch->tracker.track = 1; + touch->t = now; + } + if (!eve_timer_get_evt(NULL) && (touch_tag_opt[touch_tag] & (EVE_TOUCH_OPT_LPRESS | EVE_TOUCH_OPT_TAP2))) { + uint16_t _evt = 0; + + if (touch_tag_opt[touch_tag] & EVE_TOUCH_OPT_LPRESS) _evt |= EVE_TOUCH_ETYPE_LPRESS; + if (touch_tag_opt[touch_tag] & EVE_TOUCH_OPT_TAP2) _evt |= EVE_TOUCH_ETYPE_TAP2; + eve_timer_set(touch, _evt, touch_tag0, EVE_TOUCH_TIMEOUT_TAP); } - touch_evt |= EVE_TOUCH_ETYPE_TRACK_START; - touch->tracker.track = 1; - touch->t = now; } } - if (_timer && ((dx > EVE_TOUCH_THRESHOLD_X) || (dy > EVE_TOUCH_THRESHOLD_Y))) { - eve_touch_timer_set_evt(touch, _ttevt & ~(EVE_TOUCH_ETYPE_LPRESS | EVE_TOUCH_ETYPE_TAP2)); - _timer = 0; - } + touch->tag_up = touch->tag; + if (touch->tag_up) touch_evt |= EVE_TOUCH_ETYPE_TAG_UP; + touch->tag = touch_tag; + if (touch->tag) touch_evt |= EVE_TOUCH_ETYPE_TAG; } - if (touch->tracker.tag && touch->tracker.track) { - if (touch_tag_opt[touch->tracker.tag] & EVE_TOUCH_OPT_TRACK) touch_evt |= EVE_TOUCH_ETYPE_TRACK; - if (touch_tag_opt[touch->tracker.tag] & EVE_TOUCH_OPT_TRACK_REG) touch_evt |= EVE_TOUCH_ETYPE_TRACK_REG; - } - if (touch_evt & EVE_TOUCH_ETYPE_TRACK_REG) { - uint32_t touch_track = eve_read32(_reg_track[i]); - if (touch->tracker.tag == (touch_track & 0xff)) { - touch->tracker.val = touch_track >> 16; - } else { - touch_evt &= ~EVE_TOUCH_ETYPE_TRACK_REG; - } - } - if (touch->tracker.tag || _timer) int_ccomplete = 1; } + if (touch_handler && touch_evt) { touch_handler(touch, touch_evt, touch_tag0, touch_handler_param); } - if (!touch_multi) break; - } - - if (!touch_ex) { - touch_tag0 = 0; - touch_multi = 0; - } - - if (touch_multi) int_ccomplete = 1; - - if (int_ccomplete && !(intr_mask & EVE_INT_CONVCOMPLETE)) { - eve_write16(REG_INT_MASK, intr_mask | EVE_INT_CONVCOMPLETE); - } - if (!int_ccomplete && (intr_mask & EVE_INT_CONVCOMPLETE)) { - eve_write16(REG_INT_MASK, intr_mask & ~EVE_INT_CONVCOMPLETE); } } @@ -278,7 +264,7 @@ void eve_handle_time(void) { } if (more) { - eve_timer_set(touch_timer.to); + eve_sys_timer_set(touch_timer.to); } else { touch_timer.evt = 0; } @@ -289,74 +275,12 @@ void eve_handle_time(void) { } } -void eve_touch_init(int touch_calibrate, uint32_t *touch_matrix) { - /* configure touch */ - eve_write8(REG_CPURESET, 2); /* touch engine reset */ - eve_write16(REG_TOUCH_CONFIG, 0x4000); /* host mode multi touch */ - eve_write8(REG_CPURESET, 0); /* clear reset */ - - if (touch_calibrate) { - eve_write8(REG_PWM_DUTY, 0x40); - eve_cmd(CMD_TEXT, "hhhhs", EVE_HSIZE/2, EVE_VSIZE/2, 27, EVE_OPT_CENTER, "Please tap on the dot."); - eve_cmd(CMD_CALIBRATE, "w", 0); - eve_cmd_exec(1); - eve_write8(REG_PWM_DUTY, 0); - - touch_matrix[0] = eve_read32(REG_TOUCH_TRANSFORM_A); - touch_matrix[1] = eve_read32(REG_TOUCH_TRANSFORM_B); - touch_matrix[2] = eve_read32(REG_TOUCH_TRANSFORM_C); - touch_matrix[3] = eve_read32(REG_TOUCH_TRANSFORM_D); - touch_matrix[4] = eve_read32(REG_TOUCH_TRANSFORM_E); - touch_matrix[5] = eve_read32(REG_TOUCH_TRANSFORM_F); - } else { - eve_write32(REG_TOUCH_TRANSFORM_A, touch_matrix[0]); - eve_write32(REG_TOUCH_TRANSFORM_B, touch_matrix[1]); - eve_write32(REG_TOUCH_TRANSFORM_C, touch_matrix[2]); - eve_write32(REG_TOUCH_TRANSFORM_D, touch_matrix[3]); - eve_write32(REG_TOUCH_TRANSFORM_E, touch_matrix[4]); - eve_write32(REG_TOUCH_TRANSFORM_F, touch_matrix[5]); - } - - eve_write8(REG_CTOUCH_EXTENDED, 0x00); /* set extended mode */ -} - -void eve_touch_start(void) { - uint16_t intr_mask; - int i; - - touch_multi = 0; - touch_tag0 = 0; - memset(&touch_timer, 0, sizeof(touch_timer)); - for (i=0; i<EVE_MAX_TOUCH; i++) { - EVETouch *touch = &touch_obj[i]; - - memset(&touch_obj[i], 0, sizeof(EVETouch)); - touch->eevt |= EVE_TOUCH_EETYPE_NOTOUCH; - } - eve_vtrack_init(); - - intr_mask = eve_read16(REG_INT_MASK); - eve_write16(REG_INT_MASK, intr_mask | EVE_INT_TAG | EVE_INT_TOUCH); - - eve_write8(REG_TOUCH_MODE, EVE_TMODE_CONTINUOUS); -} - -void eve_touch_stop(void) { - uint16_t intr_mask; - - intr_mask = eve_read16(REG_INT_MASK); - eve_write16(REG_INT_MASK, intr_mask & ~(EVE_INT_TAG | EVE_INT_TOUCH | EVE_INT_CONVCOMPLETE)); - eve_touch_timer_clear(touch_timer.touch); - - eve_write8(REG_TOUCH_MODE, EVE_TMODE_OFF); -} - void eve_touch_set_handler(eve_touch_handler_t handler, void *param) { touch_handler = handler; touch_handler_param = param; } -EVETouch *eve_touch_get(int i) { +EVETouch *eve_touch_get_obj(int i) { return &touch_obj[i]; } @@ -411,46 +335,59 @@ void eve_touch_clear_opt(void) { memset(touch_tag_opt, 0, sizeof(touch_tag_opt)); } -void eve_touch_timer_set(EVETouch *touch, uint16_t evt, uint8_t tag0, uint32_t to) { +void eve_timer_set(EVETouch *touch, uint16_t evt, uint8_t tag0, uint32_t to) { touch_timer.touch = touch; touch_timer.evt = evt; touch_timer.tag0 = tag0; touch_timer.to = to; - eve_timer_set(to); + eve_sys_timer_set(to); +} + +void eve_timer_clear(EVETouch *touch) { + eve_timer_set_evt(touch, 0); } -void eve_touch_timer_clear(EVETouch *touch) { - eve_touch_timer_set_evt(touch, 0); +void eve_timer_set_evt(EVETouch *touch, uint16_t evt) { + if ((touch == NULL) || (touch == touch_timer.touch)) { + touch_timer.evt = evt; + } else if (touch_timer.touch == NULL) { + touch_timer.evt = evt; + } + if (!touch_timer.evt) { + eve_sys_timer_clear(); + touch_timer.touch = NULL; + touch_timer.tag0 = 0; + touch_timer.to = 0; + } } -uint16_t eve_touch_timer_get_evt(EVETouch *touch) { +uint16_t eve_timer_get_evt(EVETouch *touch) { uint16_t ret = 0; if ((touch == NULL) || (touch_timer.touch == touch)) { ret = touch_timer.evt; } else if (touch_timer.touch == NULL) { - ret = touch_timer.evt & EVE_TOUCH_ETYPE_TIMER; + ret = touch_timer.evt; } return ret; } -void eve_touch_timer_set_evt(EVETouch *touch, uint16_t evt) { - if (touch == touch_timer.touch) { - touch_timer.evt = evt; - } else if (touch_timer.touch == NULL) { - touch_timer.evt = evt & EVE_TOUCH_ETYPE_TIMER; - } - if (!touch_timer.evt) eve_timer_clear(); +EVETouchTimer *eve_timer_get_obj(void) { + return &touch_timer; } -void eve_touch_timer_start(uint8_t tag0, uint32_t to) { - eve_touch_timer_set(NULL, EVE_TOUCH_ETYPE_TIMER, tag0, to); +void eve_timer_start(uint8_t tag0, uint32_t to) { + if (!touch_timer.evt) eve_timer_set(NULL, EVE_TOUCH_ETYPE_TIMER, tag0, to); } -void eve_touch_timer_stop(void) { - eve_touch_timer_clear(NULL); +void eve_timer_stop(void) { + if (touch_timer.touch == NULL) eve_timer_clear(NULL); } -EVETouchTimer *eve_touch_timer_get(void) { - return &touch_timer; +int eve_timer_running(void) { + return (touch_timer.evt == EVE_TOUCH_ETYPE_TIMER); +} + +void eve_touch_clear_tag0(void) { + touch_tag0 = 0; } diff --git a/fw/fe310/eos/eve/eve_touch.h b/fw/fe310/eos/eve/eve_touch.h index b10fde2..8ec6e0e 100644 --- a/fw/fe310/eos/eve/eve_touch.h +++ b/fw/fe310/eos/eve/eve_touch.h @@ -5,7 +5,6 @@ #define EVE_TOUCH_THRESHOLD_X 5 #define EVE_TOUCH_THRESHOLD_Y 5 -#define EVE_TOUCH_TRAVG 3 #define EVE_NOTAG 0 #define EVE_NOTOUCH 0x80008000 @@ -77,7 +76,7 @@ typedef struct EVETouch { int vy; int x0; int y0; - uint64_t t; + uint32_t t; uint16_t eevt; uint8_t tag0; uint8_t tag; @@ -98,15 +97,12 @@ typedef struct EVETouchTimer { typedef void (*eve_touch_handler_t) (EVETouch *, uint16_t, uint8_t, void *); +void eve_touch_init(void); void eve_handle_touch(uint16_t intr_flags); void eve_handle_time(void); -void eve_touch_init(int touch_calibrate, uint32_t *touch_matrix); -void eve_touch_start(void); -void eve_touch_stop(void); - void eve_touch_set_handler(eve_touch_handler_t handler, void *handler_param); -EVETouch *eve_touch_get(int i); +EVETouch *eve_touch_get_obj(int i); int8_t eve_touch_get_idx(EVETouch *touch); uint16_t eve_touch_evt(EVETouch *touch, uint16_t evt, uint8_t tag0, uint8_t tag_min, uint8_t tag_n); @@ -114,11 +110,13 @@ void eve_touch_set_opt(uint8_t tag, uint8_t opt); uint8_t eve_touch_get_opt(uint8_t tag); void eve_touch_clear_opt(void); -void eve_touch_timer_set(EVETouch *touch, uint16_t evt, uint8_t tag0, uint32_t to); -void eve_touch_timer_clear(EVETouch *touch); -uint16_t eve_touch_timer_get_evt(EVETouch *touch); -void eve_touch_timer_set_evt(EVETouch *touch, uint16_t evt); -void eve_touch_timer_start(uint8_t tag0, uint32_t to); -void eve_touch_timer_stop(void); +void eve_timer_set(EVETouch *touch, uint16_t evt, uint8_t tag0, uint32_t to); +void eve_timer_clear(EVETouch *touch); +void eve_timer_set_evt(EVETouch *touch, uint16_t evt); +uint16_t eve_timer_get_evt(EVETouch *touch); +EVETouchTimer *eve_timer_get_obj(void); -EVETouchTimer *eve_touch_timer_get(void); +void eve_timer_start(uint8_t tag0, uint32_t to); +void eve_timer_stop(void); +int eve_timer_running(void); +void eve_touch_clear_tag0(void); diff --git a/fw/fe310/eos/eve/eve_touch_engine.c b/fw/fe310/eos/eve/eve_touch_engine.c new file mode 100644 index 0000000..2c75ca0 --- /dev/null +++ b/fw/fe310/eos/eve/eve_touch_engine.c @@ -0,0 +1,119 @@ +#include <stdlib.h> + +#include "eve.h" +#include "eve_touch_engine.h" + +static const uint32_t _reg_touch[] = { + REG_CTOUCH_TOUCH0_XY, + REG_CTOUCH_TOUCH1_XY, + REG_CTOUCH_TOUCH2_XY, + REG_CTOUCH_TOUCH3_XY +}; + +static const uint32_t _reg_tag[] = { + REG_TOUCH_TAG, + REG_TOUCH_TAG1, + REG_TOUCH_TAG2, + REG_TOUCH_TAG3, + REG_TOUCH_TAG4 +}; + +static const uint32_t _reg_track[] = { + REG_TRACKER, + REG_TRACKER_1, + REG_TRACKER_2, + REG_TRACKER_3, + REG_TRACKER_4 +}; + +void eve_touch_init_engine(void) { + /* configure touch */ + eve_write8(REG_CPURESET, 2); /* touch engine reset */ + eve_write16(REG_TOUCH_CONFIG, 0x4000); /* host mode multi touch */ + eve_write8(REG_CTOUCH_EXTENDED, 0x00); /* set extended mode */ + eve_write8(REG_CPURESET, 0); /* clear reset */ + + eve_touch_ehost_enter(0, 0x8000, 0x8000); + eve_touch_ehost_end(); +} + +void eve_touch_start(void) { + uint16_t intr_mask; + + intr_mask = eve_read16(REG_INT_MASK); + eve_write16(REG_INT_MASK, intr_mask | (EVE_INT_CONVCOMPLETE | EVE_INT_TAG)); + + eve_write8(REG_TOUCH_MODE, EVE_TMODE_CONTINUOUS); +} + +void eve_touch_stop(void) { + uint16_t intr_mask; + + intr_mask = eve_read16(REG_INT_MASK); + eve_write16(REG_INT_MASK, intr_mask & ~(EVE_INT_CONVCOMPLETE | EVE_INT_TAG)); + eve_timer_clear(NULL); + + eve_write8(REG_TOUCH_MODE, EVE_TMODE_OFF); +} + +void eve_touch_set_extended(int extended) { + eve_write8(REG_CPURESET, 2); /* touch engine reset */ + eve_write8(REG_CTOUCH_EXTENDED, !extended); /* set / clear extended mode */ + eve_write8(REG_CPURESET, 0); /* clear reset */ +} + +void eve_touch_get_matrix(uint32_t *touch_matrix) { + touch_matrix[0] = eve_read32(REG_TOUCH_TRANSFORM_A); + touch_matrix[1] = eve_read32(REG_TOUCH_TRANSFORM_B); + touch_matrix[2] = eve_read32(REG_TOUCH_TRANSFORM_C); + touch_matrix[3] = eve_read32(REG_TOUCH_TRANSFORM_D); + touch_matrix[4] = eve_read32(REG_TOUCH_TRANSFORM_E); + touch_matrix[5] = eve_read32(REG_TOUCH_TRANSFORM_F); +} + +void eve_touch_set_matrix(const uint32_t *touch_matrix) { + eve_write32(REG_TOUCH_TRANSFORM_A, touch_matrix[0]); + eve_write32(REG_TOUCH_TRANSFORM_B, touch_matrix[1]); + eve_write32(REG_TOUCH_TRANSFORM_C, touch_matrix[2]); + eve_write32(REG_TOUCH_TRANSFORM_D, touch_matrix[3]); + eve_write32(REG_TOUCH_TRANSFORM_E, touch_matrix[4]); + eve_write32(REG_TOUCH_TRANSFORM_F, touch_matrix[5]); +} + +uint32_t eve_touch_reg_xy(int i) { + uint32_t touch_xy; + + touch_xy = i < 4 ? eve_read32(_reg_touch[i]) : (((uint32_t)eve_read16(REG_CTOUCH_TOUCH4_X) << 16) | eve_read16(REG_CTOUCH_TOUCH4_Y)); + return touch_xy; +} + +uint8_t eve_touch_reg_tag(int i) { + uint8_t touch_tag; + + touch_tag = eve_read8(_reg_tag[i]); + return touch_tag; +} + +uint32_t eve_touch_reg_track(int i) { + uint32_t touch_track; + + touch_track = eve_read32(_reg_track[i]); + return touch_track; +} + +int eve_touch_ehost_ready(void) { + uint8_t ack; + + ack = eve_read8(REG_EHOST_TOUCH_ACK); + return (ack == 1); +} + +void eve_touch_ehost_enter(uint8_t id, uint16_t x, uint16_t y) { + eve_write16(REG_EHOST_TOUCH_X, x); + eve_write16(REG_EHOST_TOUCH_Y, y); + eve_write8(REG_EHOST_TOUCH_ID, id); +} + +void eve_touch_ehost_end(void) { + eve_write8(REG_EHOST_TOUCH_ID, 0xf); +} diff --git a/fw/fe310/eos/eve/eve_touch_engine.h b/fw/fe310/eos/eve/eve_touch_engine.h new file mode 100644 index 0000000..3a7508f --- /dev/null +++ b/fw/fe310/eos/eve/eve_touch_engine.h @@ -0,0 +1,17 @@ +#include <stdint.h> + +void eve_touch_init_engine(void); +void eve_touch_start(void); +void eve_touch_stop(void); + +void eve_touch_set_extended(int extended); +void eve_touch_get_matrix(uint32_t *touch_matrix); +void eve_touch_set_matrix(const uint32_t *touch_matrix); + +int eve_touch_ehost_ready(void); +void eve_touch_ehost_enter(uint8_t id, uint16_t x, uint16_t y); +void eve_touch_ehost_end(void); + +uint32_t eve_touch_reg_xy(int i); +uint8_t eve_touch_reg_tag(int i); +uint32_t eve_touch_reg_track(int i); diff --git a/fw/fe310/eos/eve/eve_vtrack.c b/fw/fe310/eos/eve/eve_vtrack.c index b9f28af..a7619fb 100644 --- a/fw/fe310/eos/eve/eve_vtrack.c +++ b/fw/fe310/eos/eve/eve_vtrack.c @@ -26,40 +26,31 @@ void eve_vtrack_reset(void) { eve_vtrack_set(eve_vtrack_acc_start, eve_vtrack_acc_tick, NULL, &vtrack_acc); } -void eve_vtrack_start(EVETouch *touch, uint8_t tag0, uint32_t to) { - eve_touch_timer_set(touch, EVE_TOUCH_ETYPE_TRACK, tag0, to); - if (vtrack.start) vtrack.start(touch, vtrack.param); +int eve_vtrack_start(EVETouch *touch, uint8_t tag0, uint32_t to) { + if (vtrack.start) { + int start; + + start = vtrack.start(touch, vtrack.param); + if (start) eve_timer_set(touch, EVE_TOUCH_ETYPE_TRACK, tag0, to); + + return start; + } + return 0; } void eve_vtrack_stop(EVETouch *touch) { - eve_touch_timer_clear(touch); + eve_timer_clear(touch); eve_vtrack_reset(); } -void eve_vtrack_acc_start(EVETouch *touch, void *p) { +int eve_vtrack_acc_start(EVETouch *touch, void *p) { EVEPhyAcc *param = (EVEPhyAcc *)p; - eve_phy_acc_start(param, touch->x, touch->y, touch->vx, touch->vy); + + return eve_phy_acc_start(param, touch->x, touch->y, touch->t, touch->vx, touch->vy); } int eve_vtrack_acc_tick(EVETouch *touch, void *p) { EVEPhyAcc *param = (EVEPhyAcc *)p; - return eve_phy_acc_tick(param, eve_time_get_tick() - touch->t, &touch->x, &touch->y); + return eve_phy_acc_tick(param, eve_get_tick() - touch->t, &touch->x, &touch->y); } - -void eve_vtrack_lho_start(EVETouch *touch, void *p) { - EVEPhyLHO *param = (EVEPhyLHO *)p; - - eve_phy_lho_start(param, touch->x, touch->y); -} - -int eve_vtrack_lho_tick(EVETouch *touch, void *p) { - EVEPhyLHO *param = (EVEPhyLHO *)p; - - return eve_phy_lho_tick(param, eve_time_get_tick() - touch->t, &touch->x, &touch->y); -} - -void eve_vtrack_lho_stop(EVETouch *touch, void *p) { - eve_vtrack_reset(); -} - diff --git a/fw/fe310/eos/eve/eve_vtrack.h b/fw/fe310/eos/eve/eve_vtrack.h index b75f673..8455502 100644 --- a/fw/fe310/eos/eve/eve_vtrack.h +++ b/fw/fe310/eos/eve/eve_vtrack.h @@ -1,8 +1,8 @@ #include <stdint.h> -#define EVE_VTRACK_ACC_A 1000 +#define EVE_VTRACK_ACC_A 10000 -typedef void (*eve_vtrack_start_t) (EVETouch *, void *); +typedef int (*eve_vtrack_start_t) (EVETouch *, void *); typedef int (*eve_vtrack_tick_t) (EVETouch *, void *); typedef void (*eve_vtrack_stop_t) (EVETouch *, void *); @@ -17,12 +17,8 @@ void eve_vtrack_init(void); EVEVTrack *eve_vtrack_get(void); void eve_vtrack_set(eve_vtrack_start_t start, eve_vtrack_tick_t tick, eve_vtrack_stop_t stop, void *param); void eve_vtrack_reset(void); -void eve_vtrack_start(EVETouch *touch, uint8_t tag0, uint32_t to); +int eve_vtrack_start(EVETouch *touch, uint8_t tag0, uint32_t to); void eve_vtrack_stop(EVETouch *touch); -void eve_vtrack_acc_start(EVETouch *touch, void *p); -int eve_vtrack_acc_tick(EVETouch *touch, void *p); - -void eve_vtrack_lho_start(EVETouch *touch, void *p); -int eve_vtrack_lho_tick(EVETouch *touch, void *p); -void eve_vtrack_lho_stop(EVETouch *touch, void *p); +int eve_vtrack_acc_start(EVETouch *touch, void *p); +int eve_vtrack_acc_tick(EVETouch *touch, void *p);
\ No newline at end of file diff --git a/fw/fe310/eos/eve/screen/form.c b/fw/fe310/eos/eve/screen/form.c index 8555158..13021a0 100644 --- a/fw/fe310/eos/eve/screen/form.c +++ b/fw/fe310/eos/eve/screen/form.c @@ -9,8 +9,6 @@ #include "page.h" #include "form.h" -#include "widget/widgets.h" - #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) #define MAX(X, Y) (((X) > (Y)) ? (X) : (Y)) @@ -57,7 +55,7 @@ static void widgets_destroy(EVEWidget *widget, uint16_t widget_size) { } } -EVEForm *eve_form_create(EVEWindow *window, EVEViewStack *stack, EVEWidgetSpec spec[], uint16_t spec_size, eve_form_uievt_t uievt, eve_form_action_t action, eve_form_destructor_t destructor) { +EVEForm *eve_form_create(EVEWindow *window, EVEViewStack *stack, EVEFormSpec spec[], uint16_t spec_size, eve_form_uievt_t uievt, eve_form_action_t action, eve_form_destructor_t destructor) { EVEWidget *widgets; EVEWidget *widget; EVELabel *label; @@ -65,17 +63,17 @@ EVEForm *eve_form_create(EVEWindow *window, EVEViewStack *stack, EVEWidgetSpec s int w_size = 0; int i, r; - for (i=0; i<spec_size; i++) { - w_size += eve_widget_size(spec[i].widget.type); - } + if (uievt == NULL) uievt = eve_form_uievt; + if (destructor == NULL) destructor = eve_form_destroy; form = eve_malloc(sizeof(EVEForm)); if (form == NULL) { return NULL; } - if (uievt == NULL) uievt = eve_form_uievt; - if (destructor == NULL) destructor = eve_form_destroy; eve_form_init(form, window, stack, NULL, 0, uievt, action, destructor); + for (i=0; i<spec_size; i++) { + w_size += eve_widget_size(spec[i].widget.type); + } widgets = eve_malloc(w_size); if (widgets == NULL) { eve_free(form); @@ -84,7 +82,7 @@ EVEForm *eve_form_create(EVEWindow *window, EVEViewStack *stack, EVEWidgetSpec s widget = widgets; for (i=0; i<spec_size; i++) { - r = eve_widget_create(widget, spec[i].widget.type, &spec[i].widget.g, (EVEPage *)form, &spec[i].widget.spec); + r = eve_widget_create(widget, &spec[i].widget, (EVEPage *)form); if (r) { widgets_destroy(widgets, i); eve_free(widgets); @@ -92,7 +90,6 @@ EVEForm *eve_form_create(EVEWindow *window, EVEViewStack *stack, EVEWidgetSpec s return NULL; } if (spec[i].label.title) { - EVEFont *font = spec[i].label.font ? spec[i].label.font : eve_window_font(window); label = eve_malloc(sizeof(EVELabel)); if (label == NULL) { eve_widget_destroy(widget); @@ -101,15 +98,13 @@ EVEForm *eve_form_create(EVEWindow *window, EVEViewStack *stack, EVEWidgetSpec s eve_free(form); return NULL; } - eve_label_init(label, &spec[i].label.g, font, spec[i].label.title); + eve_label_create(label, &spec[i].label, (EVEPage *)form); eve_widget_set_label(widget, label); - if (label->g.w == 0) label->g.w = eve_font_str_w(font, label->title); } - if (widget->label && (widget->label->g.w == 0)) eve_font_str_w(label->font, label->title) + EVE_FORM_LABEL_MARGIN; if (widget->g.w == 0) widget->g.w = window->g.w - (widget->label ? widget->label->g.w : 0); widget = eve_widget_next(widget); } - eve_form_update(form, widgets, spec_size); + eve_form_set_widget(form, widgets, spec_size); return form; } @@ -118,11 +113,11 @@ void eve_form_init(EVEForm *form, EVEWindow *window, EVEViewStack *stack, EVEWid memset(form, 0, sizeof(EVEForm)); eve_page_init(&form->p, window, stack, NULL, 0, EVE_PAGE_OPT_SCROLL_Y | EVE_PAGE_OPT_SCROLL_BACK | EVE_PAGE_OPT_TRACK_EXT_Y, eve_page_draw, eve_page_touch, (eve_view_uievt_t)uievt, (eve_page_destructor_t)destructor); form->action = action; - eve_form_update(form, widget, widget_size); + eve_form_set_widget(form, widget, widget_size); } -void eve_form_update(EVEForm *form, EVEWidget *widget, uint16_t widget_size) { - eve_page_update((EVEPage *)form, widget, widget_size); +void eve_form_set_widget(EVEForm *form, EVEWidget *widget, uint16_t widget_size) { + eve_page_set_widget((EVEPage *)form, widget, widget_size); form_update_g(form, NULL); } @@ -138,25 +133,24 @@ int eve_form_uievt(EVEForm *form, uint16_t evt, void *param) { form_update_g(form, (EVEWidget *)param); break; - case EVE_UIEVT_PAGE_SCROLL_START: + case EVE_UIEVT_GEST_SCROLL_START: break; - case EVE_UIEVT_PAGE_SCROLL_STOP: + case EVE_UIEVT_GEST_SCROLL_STOP: break; - case EVE_UIEVT_PAGE_TRACK_START: + case EVE_UIEVT_GEST_TRACK_START: break; - case EVE_UIEVT_PAGE_TRACK_STOP: { + case EVE_UIEVT_GEST_TRACK_STOP: { EVEUIEvtTouch *touch_p = (EVEUIEvtTouch *)param; - if (touch_p->evt & EVE_TOUCH_ETYPE_TRACK_STOP) { - if (touch_p->touch->eevt & EVE_TOUCH_EETYPE_TRACK_RIGHT) { - eve_page_close((EVEPage *)form); - return 1; - } - if (touch_p->touch->eevt & EVE_TOUCH_EETYPE_TRACK_LEFT) { - if (form->action) form->action(form); - } + + if (touch_p->touch->eevt & EVE_TOUCH_EETYPE_TRACK_RIGHT) { + eve_page_close((EVEPage *)form); + return 1; + } + if (touch_p->touch->eevt & EVE_TOUCH_EETYPE_TRACK_LEFT) { + if (form->action) form->action(form); } break; } diff --git a/fw/fe310/eos/eve/screen/form.h b/fw/fe310/eos/eve/screen/form.h index 272b6ed..62e10f7 100644 --- a/fw/fe310/eos/eve/screen/form.h +++ b/fw/fe310/eos/eve/screen/form.h @@ -1,10 +1,10 @@ #include <stdint.h> -#define EVE_FORM_LABEL_MARGIN 10 +#include "../widget/widget.h" struct EVEWidget; -struct EVEWidgetSpec; struct EVEForm; +struct EVEFormSpec; typedef int (*eve_form_uievt_t) (struct EVEForm *, uint16_t, void *); typedef void (*eve_form_action_t) (struct EVEForm *); @@ -15,9 +15,15 @@ typedef struct EVEForm { eve_form_action_t action; } EVEForm; -EVEForm *eve_form_create(EVEWindow *window, EVEViewStack *stack, struct EVEWidgetSpec *spec, uint16_t spec_size, eve_form_uievt_t uievt, eve_form_action_t action, eve_form_destructor_t destructor); -void eve_form_init(EVEForm *form, EVEWindow *window, EVEViewStack *stack, struct EVEWidget *widget, uint16_t widget_size, eve_form_uievt_t uievt, eve_form_action_t action, eve_form_destructor_t destructor); -void eve_form_update(EVEForm *form, struct EVEWidget *widget, uint16_t widget_size); +typedef struct EVEFormSpec { + EVELabelSpec label; + EVEWidgetSpec widget; +} EVEFormSpec; + +EVEForm *eve_form_create(EVEWindow *window, EVEViewStack *stack, EVEFormSpec *spec, uint16_t spec_size, eve_form_uievt_t uievt, eve_form_action_t action, eve_form_destructor_t destructor); +void eve_form_init(EVEForm *form, EVEWindow *window, EVEViewStack *stack, EVEWidget *widget, uint16_t widget_size, eve_form_uievt_t uievt, eve_form_action_t action, eve_form_destructor_t destructor); +void eve_form_set_widget(EVEForm *form, EVEWidget *widget, uint16_t widget_size); void eve_form_destroy(EVEForm *form); int eve_form_uievt(EVEForm *form, uint16_t evt, void *param); + diff --git a/fw/fe310/eos/eve/screen/page.c b/fw/fe310/eos/eve/screen/page.c index 65dd534..64fc473 100644 --- a/fw/fe310/eos/eve/screen/page.c +++ b/fw/fe310/eos/eve/screen/page.c @@ -8,7 +8,6 @@ #include "window.h" #include "page.h" -#include "widget/label.h" #include "widget/widget.h" #define PAGE_TMODE_NONE 0 @@ -21,10 +20,22 @@ void eve_page_init(EVEPage *page, EVEWindow *window, EVEViewStack *stack, EVEWid page->stack = stack; page->opt = opt; page->destructor = destructor; - eve_page_update(page, widget, widget_size); + eve_page_set_widget(page, widget, widget_size); } -void eve_page_update(EVEPage *page, EVEWidget *widget, uint16_t widget_size) { +EVEWidget *eve_page_widget(EVEPage *page, uint16_t idx) { + EVEWidget *w = page->widget; + int i; + + if (idx >= page->widget_size) return NULL; + + for (i=0; i<idx; i++) { + w = eve_widget_next(w); + } + return w; +} + +void eve_page_set_widget(EVEPage *page, EVEWidget *widget, uint16_t widget_size) { page->widget = widget; page->widget_size = widget_size; } @@ -45,16 +56,16 @@ void eve_page_close(EVEPage *page) { if (stack->level <= 1) return; - if (page->lho_t0) { - page->lho_t0 = 0; - eve_touch_timer_stop(); + if (eve_timer_running()) { + eve_timer_stop(); } if (eve_window_scroll(window->root, NULL) == window) { eve_window_scroll_stop(window); } - if (destructor) destructor(page); eve_window_kbd_detach(window); + + if (destructor) destructor(page); eve_view_destroy(window, stack); } @@ -93,30 +104,32 @@ int eve_page_rect_visible(EVEPage *page, EVERect *g) { return 0; } -void eve_page_focus(EVEPage *page, EVERect *rect) { - if (rect) { - EVERect g; +void eve_page_rect_set_visible(EVEPage *page, EVERect *rect) { + EVERect g; - eve_window_visible_g(page->v.window, &g); - g.x -= page->v.window->g.x; - g.y -= page->v.window->g.y; + eve_window_visible_g(page->v.window, &g); + g.x -= page->v.window->g.x; + g.y -= page->v.window->g.y; - if (rect->x < page->g.x + g.x) { - page->g.x = rect->x - g.x; - } - if (rect->y < page->g.y + g.y) { - page->g.y = rect->y - g.y; - } - if ((rect->x + rect->w) > (page->g.x + g.x + g.w)) { - page->g.x = (rect->x + rect->w) - (g.x + g.w); - } - if ((rect->y + rect->h) > (page->g.y + g.y + g.h)) { - page->g.y = (rect->y + rect->h) - (g.y + g.h); - } + if (rect->x < page->g.x + g.x) { + page->g.x = rect->x - g.x; } + if (rect->y < page->g.y + g.y) { + page->g.y = rect->y - g.y; + } + if ((rect->x + rect->w) > (page->g.x + g.x + g.w)) { + page->g.x = (rect->x + rect->w) - (g.x + g.w); + } + if ((rect->y + rect->h) > (page->g.y + g.y + g.h)) { + page->g.y = (rect->y + rect->h) - (g.y + g.h); + } +} + +EVEWidget *eve_page_focus(EVEPage *page) { + return page->widget_f; } -void eve_page_focus_widget(EVEPage *page, EVEWidget *widget, EVERect *rect) { +void eve_page_set_focus(EVEPage *page, EVEWidget *widget, EVERect *rect) { if (page->widget_f != widget) { EVEWindow *window = page->v.window; EVEWidget *widget_f = page->widget_f; @@ -131,67 +144,59 @@ void eve_page_focus_widget(EVEPage *page, EVEWidget *widget, EVERect *rect) { if (kbd) eve_kbd_set_handler(kbd, widget->putc, widget); if (!(widget_f && widget_f->putc)) eve_window_kbd_attach(window); } + if (page->widget_f) eve_view_uievt_push(&page->v, EVE_UIEVT_WIDGET_FOCUS_OUT, page->widget_f); page->widget_f = widget; + if (page->widget_f) eve_view_uievt_push(&page->v, EVE_UIEVT_WIDGET_FOCUS_IN, page->widget_f); } - if (rect) eve_page_focus(page, rect); + if (rect) eve_page_rect_set_visible(page, rect); } -EVEWidget *eve_page_focus_widget_get(EVEPage *page) { - return page->widget_f; -} - -EVEWidget *eve_page_widget(EVEPage *page, uint16_t idx) { - EVEWidget *w = page->widget; - int i; - - if (idx >= page->widget_size) return NULL; - - for (i=0; i<idx; i++) { - w = eve_widget_next(w); - } - return w; -} +#include <stdio.h> static int page_touch(EVEPage *page, EVETouch *touch, uint16_t evt, uint8_t tag0) { EVEView *view = &page->v; EVEWindow *window = view->window; - int scroll, scroll_x, scroll_y; + int scroll_x = 0, scroll_y = 0, scroll; int ret = 0; - scroll_x = page->opt & (EVE_PAGE_OPT_SCROLL_X | EVE_PAGE_OPT_SCROLL_XY); - if (scroll_x && touch) scroll_x = touch->eevt & ((page->opt & EVE_PAGE_OPT_SCROLL_XY) ? EVE_TOUCH_EETYPE_TRACK_XY : EVE_TOUCH_EETYPE_TRACK_X); - - scroll_y = page->opt & (EVE_PAGE_OPT_SCROLL_Y | EVE_PAGE_OPT_SCROLL_XY); - if (scroll_y && touch) scroll_y = touch->eevt & ((page->opt & EVE_PAGE_OPT_SCROLL_XY) ? EVE_TOUCH_EETYPE_TRACK_XY : EVE_TOUCH_EETYPE_TRACK_Y); + if (touch) { + if ((page->opt & EVE_PAGE_OPT_SCROLL_XY) == EVE_PAGE_OPT_SCROLL_XY) { + scroll_x = scroll_y = (touch->eevt & EVE_TOUCH_EETYPE_TRACK_XY); + } else if (page->opt & EVE_PAGE_OPT_SCROLL_X) { + scroll_x = (touch->eevt & EVE_TOUCH_EETYPE_TRACK_X); + } else if (page->opt & EVE_PAGE_OPT_SCROLL_Y) { + scroll_y = (touch->eevt & EVE_TOUCH_EETYPE_TRACK_Y); + } + } scroll = scroll_x || scroll_y; if ((evt & EVE_TOUCH_ETYPE_POINT_UP) && !(touch->eevt & (EVE_TOUCH_EETYPE_TRACK_XY | EVE_TOUCH_EETYPE_ABORT))) { int _ret = 0; - if (page->widget_f) eve_page_focus_widget(page, NULL, NULL); - _ret = eve_view_uievt_pusht(view, EVE_UIEVT_PAGE_TOUCH, touch, evt, tag0); + if (page->widget_f) eve_page_set_focus(page, NULL, NULL); + _ret = eve_view_uievt_push_gest(view, EVE_UIEVT_GEST_TOUCH, touch, evt, tag0); if (_ret) return _ret; ret = 1; } - /* Scroll start */ + /* Scroll / track start */ if ((evt & EVE_TOUCH_ETYPE_TRACK_START) && !(touch->eevt & EVE_TOUCH_EETYPE_ABORT)) { int _ret = 0; if (scroll) { page->track_mode = PAGE_TMODE_SCROLL; eve_window_scroll_start(window, touch->tracker.tag); - _ret = eve_view_uievt_pusht(view, EVE_UIEVT_PAGE_SCROLL_START, touch, evt, tag0); + _ret = eve_view_uievt_push_gest(view, EVE_UIEVT_GEST_SCROLL_START, touch, evt, tag0); } else { page->track_mode = PAGE_TMODE_TRACK; - _ret = eve_view_uievt_pusht(view, EVE_UIEVT_PAGE_TRACK_START, touch, evt, tag0); + _ret = eve_view_uievt_push_gest(view, EVE_UIEVT_GEST_TRACK_START, touch, evt, tag0); } if (_ret) return _ret; ret = 1; } - /* Scroll stop */ + /* Scroll / track stop */ if (((evt & EVE_TOUCH_ETYPE_TRACK_STOP) && !(evt & EVE_TOUCH_ETYPE_TRACK_ABORT)) || ((evt & EVE_TOUCH_ETYPE_POINT_UP) && (touch->eevt & EVE_TOUCH_EETYPE_ABORT) && !(touch->eevt & EVE_TOUCH_EETYPE_TRACK_XY))) { if ((page->track_mode == PAGE_TMODE_SCROLL) && (page->opt & EVE_PAGE_OPT_SCROLL_BACK)) { @@ -211,23 +216,29 @@ static int page_touch(EVEPage *page, EVETouch *touch, uint16_t evt, uint8_t tag0 eve_window_scroll(window->root, &_tag); - eve_phy_lho_init(lho, lho_x, lho_y, 1000, 0.5, 0); + page->lho_t0 = eve_get_tick(); + eve_phy_lho_init(lho, lho_x, lho_y, 100, 0.5, 0); eve_phy_lho_start(lho, page->g.x, page->g.y); - page->lho_t0 = eve_time_get_tick(); - eve_touch_timer_start(_tag, 20); + eve_timer_start(_tag, 20); } } - if (!page->lho_t0) { + if (!eve_timer_running()) { int _ret = 0; - if (page->track_mode == PAGE_TMODE_SCROLL) { - page->track_mode = PAGE_TMODE_NONE; - eve_window_scroll_stop(window); - _ret = eve_view_uievt_pusht(view, EVE_UIEVT_PAGE_SCROLL_STOP, touch, evt, tag0); - } else if (!(touch->eevt & EVE_TOUCH_EETYPE_ABORT)) { - page->track_mode = PAGE_TMODE_NONE; - _ret = eve_view_uievt_pusht(view, EVE_UIEVT_PAGE_TRACK_STOP, touch, evt, tag0); + switch (page->track_mode) { + case PAGE_TMODE_SCROLL: { + page->track_mode = PAGE_TMODE_NONE; + eve_window_scroll_stop(window); + _ret = eve_view_uievt_push_gest(view, EVE_UIEVT_GEST_SCROLL_STOP, touch, evt, tag0); + break; + } + + case PAGE_TMODE_TRACK: { + page->track_mode = PAGE_TMODE_NONE; + _ret = eve_view_uievt_push_gest(view, EVE_UIEVT_GEST_TRACK_STOP, touch, evt, tag0); + break; + } } if (_ret) return _ret; ret = 1; @@ -254,26 +265,26 @@ static int page_touch(EVEPage *page, EVETouch *touch, uint16_t evt, uint8_t tag0 } if ((evt & EVE_TOUCH_ETYPE_TIMER) && (page->opt & EVE_PAGE_OPT_SCROLL_BACK)) { EVEPhyLHO *lho = &page->lho; + int scroll_x = page->opt & EVE_PAGE_OPT_SCROLL_X; + int scroll_y = page->opt & EVE_PAGE_OPT_SCROLL_Y; int x, y, more; - more = eve_phy_lho_tick(lho, eve_time_get_tick() - page->lho_t0, scroll_x ? &x : NULL, scroll_y ? &y : NULL); + more = eve_phy_lho_tick(lho, eve_get_tick() - page->lho_t0, scroll_x ? &x : NULL, scroll_y ? &y : NULL); if (scroll_x) page->g.x = x; if (scroll_y) page->g.y = y; + printf("TIMER: %d\n", y); if (!more) { int _ret = 0; - page->lho_t0 = 0; - eve_touch_timer_stop(); + printf("TIMER STOP!\n"); page->track_mode = PAGE_TMODE_NONE; + eve_timer_stop(); eve_window_scroll_stop(window); - _ret = eve_view_uievt_pusht(view, EVE_UIEVT_PAGE_SCROLL_STOP, touch, evt, tag0); + _ret = eve_view_uievt_push_gest(view, EVE_UIEVT_GEST_SCROLL_STOP, touch, evt, tag0); if (_ret) return _ret; } ret = 1; } - if (evt & EVE_TOUCH_EETYPE_TIMER_ABORT) { - page->lho_t0 = 0; - } } return ret; diff --git a/fw/fe310/eos/eve/screen/page.h b/fw/fe310/eos/eve/screen/page.h index 26c33c5..7aa8c1d 100644 --- a/fw/fe310/eos/eve/screen/page.h +++ b/fw/fe310/eos/eve/screen/page.h @@ -5,10 +5,11 @@ #define EVE_PAGE_OPT_SCROLL_X 0x01 #define EVE_PAGE_OPT_SCROLL_Y 0x02 #define EVE_PAGE_OPT_SCROLL_BACK 0x04 -#define EVE_PAGE_OPT_SCROLL_XY 0x08 #define EVE_PAGE_OPT_TRACK_EXT_X 0x10 #define EVE_PAGE_OPT_TRACK_EXT_Y 0x20 -#define EVE_PAGE_OPT_TRACK_EXT_XY (EVE_PAGE_OPT_TRACK_EXT_X | EVE_PAGE_OPT_TRACK_EXT_Y) + +#define EVE_PAGE_OPT_SCROLL_XY (EVE_PAGE_OPT_SCROLL_X | EVE_PAGE_OPT_SCROLL_Y) +#define EVE_PAGE_OPT_TRACK_EXT_XY (EVE_PAGE_OPT_TRACK_EXT_X | EVE_PAGE_OPT_TRACK_EXT_Y) struct EVEWidget; struct EVEPage; @@ -26,13 +27,16 @@ typedef struct EVEPage { uint16_t widget_size; struct EVEWidget *widget_f; EVEPhyLHO lho; - uint64_t lho_t0; + uint32_t lho_t0; uint8_t track_mode; uint8_t opt; } EVEPage; void eve_page_init(EVEPage *page, EVEWindow *window, EVEViewStack *stack, struct EVEWidget *widget, uint16_t widget_size, uint8_t opt,eve_view_draw_t draw, eve_view_touch_t touch, eve_view_uievt_t uievt, eve_page_destructor_t destructor); -void eve_page_update(EVEPage *page, struct EVEWidget *widget, uint16_t widget_size); + +struct EVEWidget *eve_page_widget(EVEPage *page, uint16_t idx); +void eve_page_set_widget(EVEPage *page, struct EVEWidget *widget, uint16_t widget_size); + void eve_page_open(EVEPage *parent, eve_view_constructor_t constructor); void eve_page_close(EVEPage *page); @@ -46,11 +50,10 @@ int16_t eve_page_win_y(EVEPage *page, int16_t y); int16_t eve_page_scr_x(EVEPage *page, int16_t x); int16_t eve_page_scr_y(EVEPage *page, int16_t y); int eve_page_rect_visible(EVEPage *page, EVERect *g); +void eve_page_rect_set_visible(EVEPage *page, EVERect *rect); -void eve_page_focus(EVEPage *page, EVERect *rect); -void eve_page_focus_widget(EVEPage *page, struct EVEWidget *widget, EVERect *rect); -struct EVEWidget *eve_page_focus_widget_get(EVEPage *page); -struct EVEWidget *eve_page_widget(EVEPage *page, uint16_t idx); +struct EVEWidget *eve_page_focus(EVEPage *page); +void eve_page_set_focus(EVEPage *page, struct EVEWidget *widget, EVERect *rect); uint8_t eve_page_draw(EVEView *view, uint8_t tag0); int eve_page_touch(EVEView *view, EVETouch *touch, uint16_t evt, uint8_t tag0); diff --git a/fw/fe310/eos/eve/screen/uievt.h b/fw/fe310/eos/eve/screen/uievt.h index 98f0d95..8e1e682 100644 --- a/fw/fe310/eos/eve/screen/uievt.h +++ b/fw/fe310/eos/eve/screen/uievt.h @@ -1,11 +1,13 @@ -#define EVE_UIEVT_WIN_UPDATE_G 1 -#define EVE_UIEVT_PAGE_UPDATE_G 2 -#define EVE_UIEVT_PAGE_TOUCH 3 -#define EVE_UIEVT_PAGE_SCROLL_START 4 -#define EVE_UIEVT_PAGE_SCROLL_STOP 5 -#define EVE_UIEVT_PAGE_TRACK_START 6 -#define EVE_UIEVT_PAGE_TRACK_STOP 7 +#define EVE_UIEVT_GEST_TOUCH 1 +#define EVE_UIEVT_GEST_SCROLL_START 2 +#define EVE_UIEVT_GEST_SCROLL_STOP 3 +#define EVE_UIEVT_GEST_TRACK_START 4 +#define EVE_UIEVT_GEST_TRACK_STOP 5 +#define EVE_UIEVT_WIN_UPDATE_G 6 +#define EVE_UIEVT_PAGE_UPDATE_G 7 #define EVE_UIEVT_WIDGET_UPDATE_G 8 +#define EVE_UIEVT_WIDGET_FOCUS_IN 9 +#define EVE_UIEVT_WIDGET_FOCUS_OUT 10 typedef struct EVEUIEvtTouch { EVETouch *touch; diff --git a/fw/fe310/eos/eve/screen/view.c b/fw/fe310/eos/eve/screen/view.c index ac9aef4..466644d 100644 --- a/fw/fe310/eos/eve/screen/view.c +++ b/fw/fe310/eos/eve/screen/view.c @@ -77,18 +77,19 @@ void eve_view_destroy(EVEWindow *window, EVEViewStack *stack) { } } -void eve_view_uievt_push(EVEView *view, uint16_t evt, void *param) { - if (view->uievt) view->uievt(view, evt, param); +int eve_view_uievt_push(EVEView *view, uint16_t evt, void *param) { + if (view->uievt) return view->uievt(view, evt, param); + return 0; } -int eve_view_uievt_pusht(EVEView *view, uint16_t evt, EVETouch *touch, uint16_t t_evt, uint8_t tag0) { +int eve_view_uievt_push_gest(EVEView *view, uint16_t evt, EVETouch *touch, uint16_t t_evt, uint8_t tag0) { if (view->uievt) { EVEUIEvtTouch param; param.touch = touch; param.evt = t_evt; param.tag0 = tag0; - view->uievt(view, evt, ¶m); + return view->uievt(view, evt, ¶m); } return 0; } diff --git a/fw/fe310/eos/eve/screen/view.h b/fw/fe310/eos/eve/screen/view.h index 65999d7..c8be33a 100644 --- a/fw/fe310/eos/eve/screen/view.h +++ b/fw/fe310/eos/eve/screen/view.h @@ -38,5 +38,5 @@ void eve_view_stack_init(EVEViewStack *stack); void eve_view_create(struct EVEWindow *window, EVEViewStack *stack, eve_view_constructor_t constructor); void eve_view_destroy(struct EVEWindow *window, EVEViewStack *stack); -void eve_view_uievt_push(EVEView *view, uint16_t evt, void *param); -int eve_view_uievt_pusht(EVEView *view, uint16_t evt, EVETouch *touch, uint16_t t_evt, uint8_t tag0); +int eve_view_uievt_push(EVEView *view, uint16_t evt, void *param); +int eve_view_uievt_push_gest(EVEView *view, uint16_t evt, EVETouch *touch, uint16_t t_evt, uint8_t tag0); diff --git a/fw/fe310/eos/eve/widget/Makefile b/fw/fe310/eos/eve/widget/Makefile index 31349a8..0bb1533 100644 --- a/fw/fe310/eos/eve/widget/Makefile +++ b/fw/fe310/eos/eve/widget/Makefile @@ -1,7 +1,7 @@ include ../../../common.mk CFLAGS += -I.. -obj = label.o widget.o freew.o spacerw.o pagew.o strw.o textw.o selectw.o +obj = label.o widget.o widgets.o freew.o spacerw.o pagew.o strw.o textw.o selectw.o togglew.o %.o: %.c %.h diff --git a/fw/fe310/eos/eve/widget/freew.c b/fw/fe310/eos/eve/widget/freew.c index 86c2686..e7591da 100644 --- a/fw/fe310/eos/eve/widget/freew.c +++ b/fw/fe310/eos/eve/widget/freew.c @@ -8,15 +8,7 @@ #include "screen/window.h" #include "screen/page.h" -#include "label.h" #include "widget.h" -#include "freew.h" - -int eve_freew_create(EVEFreeWidget *widget, EVERect *g, EVEPage *page, EVEFreeSpec *spec) { - eve_freew_init(widget, g, page, spec->draw, spec->touch, spec->putc); - - return EVE_OK; -} void eve_freew_init(EVEFreeWidget *widget, EVERect *g, EVEPage *page, eve_freew_draw_t draw, eve_freew_touch_t touch, eve_kbd_input_handler_t putc) { EVEWidget *_widget = &widget->w; @@ -25,16 +17,15 @@ void eve_freew_init(EVEFreeWidget *widget, EVERect *g, EVEPage *page, eve_freew_ eve_widget_init(_widget, EVE_WIDGET_TYPE_FREE, g, page, eve_freew_draw, eve_freew_touch, putc); widget->_draw = draw; widget->_touch = touch; - widget->w.putc = putc; } -void eve_freew_tag(EVEFreeWidget *widget) { - EVEWidget *_widget = &widget->w; +int eve_freew_create(EVEWidget *_widget, EVEWidgetSpec *spec, EVEPage *page) { + EVEFreeWidget *widget = (EVEFreeWidget *)_widget; + EVEFreeSpec *tspec = &spec->tspec.free; - if (_widget->tagN != EVE_NOTAG) { - eve_cmd_dl(TAG(_widget->tagN)); - _widget->tagN++; - } + eve_freew_init(widget, &spec->g, page, tspec->draw, tspec->touch, tspec->putc); + + return EVE_OK; } uint8_t eve_freew_draw(EVEWidget *_widget, uint8_t tag0) { @@ -49,6 +40,18 @@ uint8_t eve_freew_draw(EVEWidget *_widget, uint8_t tag0) { int eve_freew_touch(EVEWidget *_widget, EVETouch *touch, uint16_t evt) { EVEFreeWidget *widget = (EVEFreeWidget *)_widget; + int ret; + + ret = widget->_touch(widget, touch, evt); + if (ret) eve_widget_set_focus(_widget, NULL); + return ret; +} - return widget->_touch(widget, touch, evt); +void eve_freew_tag(EVEFreeWidget *widget) { + EVEWidget *_widget = &widget->w; + + if (_widget->tagN != EVE_NOTAG) { + eve_cmd_dl(TAG(_widget->tagN)); + _widget->tagN++; + } } diff --git a/fw/fe310/eos/eve/widget/freew.h b/fw/fe310/eos/eve/widget/freew.h index 7eda8c1..324e98d 100644 --- a/fw/fe310/eos/eve/widget/freew.h +++ b/fw/fe310/eos/eve/widget/freew.h @@ -1,6 +1,7 @@ #include <stdint.h> struct EVEFreeWidget; +struct EVEWidgetSpec; typedef void (*eve_freew_draw_t) (struct EVEFreeWidget *); typedef int (*eve_freew_touch_t) (struct EVEFreeWidget *, EVETouch *, uint16_t); @@ -17,9 +18,10 @@ typedef struct EVEFreeSpec { eve_kbd_input_handler_t putc; } EVEFreeSpec; -int eve_freew_create(EVEFreeWidget *widget, EVERect *g, EVEPage *page, EVEFreeSpec *spec); void eve_freew_init(EVEFreeWidget *widget, EVERect *g, EVEPage *page, eve_freew_draw_t draw, eve_freew_touch_t touch, eve_kbd_input_handler_t putc); -void eve_freew_tag(EVEFreeWidget *widget); +int eve_freew_create(EVEWidget *_widget, struct EVEWidgetSpec *spec, EVEPage *page); uint8_t eve_freew_draw(EVEWidget *_widget, uint8_t tag0); int eve_freew_touch(EVEWidget *_widget, EVETouch *touch, uint16_t evt); + +void eve_freew_tag(EVEFreeWidget *widget); diff --git a/fw/fe310/eos/eve/widget/label.c b/fw/fe310/eos/eve/widget/label.c index f2d2e8e..43d555d 100644 --- a/fw/fe310/eos/eve/widget/label.c +++ b/fw/fe310/eos/eve/widget/label.c @@ -16,6 +16,13 @@ void eve_label_init(EVELabel *label, EVERect *g, EVEFont *font, char *title) { label->font = font; label->title = title; if (label->g.h == 0) label->g.h = eve_font_h(font); + if (label->g.w == 0) label->g.w = eve_font_str_w(font, label->title) + EVE_LABEL_MARGIN; +} + +void eve_label_create(EVELabel *label, EVELabelSpec *spec, EVEPage *page) { + EVEFont *font = spec->font ? spec->font : eve_window_font(page->v.window); + + eve_label_init(label, &spec->g, font, spec->title); } void eve_label_draw(EVELabel *label) { diff --git a/fw/fe310/eos/eve/widget/label.h b/fw/fe310/eos/eve/widget/label.h index 659c057..9992f8c 100644 --- a/fw/fe310/eos/eve/widget/label.h +++ b/fw/fe310/eos/eve/widget/label.h @@ -1,10 +1,21 @@ #include <stdint.h> +#define EVE_LABEL_MARGIN 10 + typedef struct EVELabel { EVERect g; EVEFont *font; char *title; } EVELabel; +typedef struct EVELabelSpec { + EVERect g; + EVEFont *font; + char *title; + int16_t margin; +} EVELabelSpec; + void eve_label_init(EVELabel *label, EVERect *g, EVEFont *font, char *title); + +void eve_label_create(EVELabel *label, EVELabelSpec *spec, EVEPage *page); void eve_label_draw(EVELabel *label);
\ No newline at end of file diff --git a/fw/fe310/eos/eve/widget/pagew.c b/fw/fe310/eos/eve/widget/pagew.c index bd7819f..c926892 100644 --- a/fw/fe310/eos/eve/widget/pagew.c +++ b/fw/fe310/eos/eve/widget/pagew.c @@ -8,17 +8,7 @@ #include "screen/window.h" #include "screen/page.h" -#include "label.h" #include "widget.h" -#include "pagew.h" - -int eve_pagew_create(EVEPageWidget *widget, EVERect *g, EVEPage *page, EVEPageSpec *spec) { - EVEFont *font = spec->font ? spec->font : eve_window_font(page->v.window); - - eve_pagew_init(widget, g, page, font, spec->title, spec->constructor); - - return EVE_OK; -} void eve_pagew_init(EVEPageWidget *widget, EVERect *g, EVEPage *page, EVEFont *font, char *title, eve_view_constructor_t constructor) { EVEWidget *_widget = &widget->w; @@ -31,6 +21,16 @@ void eve_pagew_init(EVEPageWidget *widget, EVERect *g, EVEPage *page, EVEFont *f if (_widget->g.h == 0) _widget->g.h = eve_font_h(widget->font); } +int eve_pagew_create(EVEWidget *_widget, EVEWidgetSpec *spec, EVEPage *page) { + EVEPageWidget *widget = (EVEPageWidget *)_widget; + EVEPageSpec *tspec = &spec->tspec.page; + EVEFont *font = tspec->font ? tspec->font : eve_window_font(page->v.window); + + eve_pagew_init(widget, &spec->g, page, font, tspec->title, tspec->constructor); + + return EVE_OK; +} + uint8_t eve_pagew_draw(EVEWidget *_widget, uint8_t tag0) { EVEPageWidget *widget = (EVEPageWidget *)_widget; @@ -51,6 +51,7 @@ int eve_pagew_touch(EVEWidget *_widget, EVETouch *touch, uint16_t evt) { EVEPageWidget *widget = (EVEPageWidget *)_widget; if (evt & EVE_TOUCH_ETYPE_TAG_UP) { + eve_widget_set_focus(_widget, NULL); eve_page_open(parent, widget->constructor); return 1; } diff --git a/fw/fe310/eos/eve/widget/pagew.h b/fw/fe310/eos/eve/widget/pagew.h index 76ac0d2..b1a91b2 100644 --- a/fw/fe310/eos/eve/widget/pagew.h +++ b/fw/fe310/eos/eve/widget/pagew.h @@ -1,5 +1,7 @@ #include <stdint.h> +struct EVEWidgetSpec; + typedef struct EVEPageWidget { EVEWidget w; EVEFont *font; @@ -13,8 +15,8 @@ typedef struct EVEPageSpec { eve_view_constructor_t constructor; } EVEPageSpec; -int eve_pagew_create(EVEPageWidget *widget, EVERect *g, EVEPage *page, EVEPageSpec *spec); void eve_pagew_init(EVEPageWidget *widget, EVERect *g, EVEPage *page, EVEFont *font, char *title, eve_view_constructor_t constructor); +int eve_pagew_create(EVEWidget *_widget, struct EVEWidgetSpec *spec, EVEPage *page); uint8_t eve_pagew_draw(EVEWidget *_widget, uint8_t tag0); int eve_pagew_touch(EVEWidget *_widget, EVETouch *touch, uint16_t evt); diff --git a/fw/fe310/eos/eve/widget/selectw.c b/fw/fe310/eos/eve/widget/selectw.c index 46ed3d1..5acf998 100644 --- a/fw/fe310/eos/eve/widget/selectw.c +++ b/fw/fe310/eos/eve/widget/selectw.c @@ -8,9 +8,7 @@ #include "screen/window.h" #include "screen/page.h" -#include "label.h" #include "widget.h" -#include "selectw.h" #define SELECTW_NOSELECT 0xffffffff @@ -59,21 +57,6 @@ static void selectw_update_sz(EVESelectWidget *widget, int uievt) { if (uievt) eve_view_uievt_push(&page->v, EVE_UIEVT_WIDGET_UPDATE_G, _widget); } -int eve_selectw_create(EVESelectWidget *widget, EVERect *g, EVEPage *page, EVESelectSpec *spec) { - EVEFont *font = spec->font ? spec->font : eve_window_font(page->v.window); - utf8_t *option; - - option = eve_malloc(spec->option_size); - if (option == NULL) { - return EVE_ERR_NOMEM; - } - memset(option, 0, spec->option_size); - - eve_selectw_init(widget, g, page, font, option, spec->option_size, spec->multi); - - return EVE_OK; -} - void eve_selectw_init(EVESelectWidget *widget, EVERect *g, EVEPage *page, EVEFont *font, utf8_t *option, uint16_t option_size, uint8_t multi) { EVEWidget *_widget = &widget->w; int rv; @@ -92,13 +75,32 @@ void eve_selectw_init(EVESelectWidget *widget, EVERect *g, EVEPage *page, EVEFon widget->select = widget->multi ? 0 : SELECTW_NOSELECT; } -void eve_selectw_destroy(EVESelectWidget *widget) { +int eve_selectw_create(EVEWidget *_widget, EVEWidgetSpec *spec, EVEPage *page) { + EVESelectWidget *widget = (EVESelectWidget *)_widget; + EVESelectSpec *tspec = &spec->tspec.select; + EVEFont *font = tspec->font ? tspec->font : eve_window_font(page->v.window); + utf8_t *option; + + option = eve_malloc(tspec->option_size); + if (option == NULL) { + return EVE_ERR_NOMEM; + } + memset(option, 0, tspec->option_size); + + eve_selectw_init(widget, &spec->g, page, font, option, tspec->option_size, tspec->multi); + + return EVE_OK; +} + +void eve_selectw_destroy(EVEWidget *_widget) { + EVESelectWidget *widget = (EVESelectWidget *)_widget; + eve_free(widget->option); } uint8_t eve_selectw_draw(EVEWidget *_widget, uint8_t tag0) { - EVEPage *page = _widget->page; EVESelectWidget *widget = (EVESelectWidget *)_widget; + EVEPage *page = _widget->page; int o_len; int o_curr; int i, s; @@ -161,6 +163,7 @@ int eve_selectw_touch(EVEWidget *_widget, EVETouch *touch, uint16_t evt) { if (evt & EVE_TOUCH_ETYPE_TAG_UP) { int i = touch->tag0 - _widget->tag0 + widget->line0; + if (widget->multi) { uint32_t f = (0x1 << i); @@ -176,12 +179,15 @@ int eve_selectw_touch(EVEWidget *_widget, EVETouch *touch, uint16_t evt) { widget->select = i; } } + + eve_widget_set_focus(_widget, NULL); return 1; } + return 0; } -utf8_t *eve_selectw_option_get(EVESelectWidget *widget, int idx) { +utf8_t *eve_selectw_option(EVESelectWidget *widget, int idx) { int o_len; int o_curr; int i; @@ -199,11 +205,11 @@ utf8_t *eve_selectw_option_get(EVESelectWidget *widget, int idx) { return NULL; } -utf8_t *eve_selectw_option_get_select(EVESelectWidget *widget) { - return eve_selectw_option_get(widget, widget->select); +utf8_t *eve_selectw_option_selected(EVESelectWidget *widget) { + return eve_selectw_option(widget, widget->select); } -int eve_selectw_option_add(EVESelectWidget *widget, utf8_t *option) { +int eve_selectw_add_option(EVESelectWidget *widget, utf8_t *option) { int o_len; int o_curr; int rv, i; @@ -231,7 +237,7 @@ int eve_selectw_option_add(EVESelectWidget *widget, utf8_t *option) { return EVE_OK; } -int eve_selectw_option_set(EVESelectWidget *widget, utf8_t *option, uint16_t option_size) { +int eve_selectw_set_option(EVESelectWidget *widget, utf8_t *option, uint16_t option_size) { int rv, i; rv = selectw_verify(option, option_size); diff --git a/fw/fe310/eos/eve/widget/selectw.h b/fw/fe310/eos/eve/widget/selectw.h index 939e362..f1b7071 100644 --- a/fw/fe310/eos/eve/widget/selectw.h +++ b/fw/fe310/eos/eve/widget/selectw.h @@ -1,5 +1,7 @@ #include <stdint.h> +struct EVEWidgetSpec; + typedef struct EVESelectWidget { EVEWidget w; EVEFont *font; @@ -17,14 +19,14 @@ typedef struct EVESelectSpec { uint8_t multi; } EVESelectSpec; -int eve_selectw_create(EVESelectWidget *widget, EVERect *g, EVEPage *page, EVESelectSpec *spec); void eve_selectw_init(EVESelectWidget *widget, EVERect *g, EVEPage *page, EVEFont *font, utf8_t *option, uint16_t option_size, uint8_t multi); -void eve_selectw_destroy(EVESelectWidget *widget); +int eve_selectw_create(EVEWidget *_widget, struct EVEWidgetSpec *spec, EVEPage *page); +void eve_selectw_destroy(EVEWidget *_widget); uint8_t eve_selectw_draw(EVEWidget *_widget, uint8_t tag0); int eve_selectw_touch(EVEWidget *_widget, EVETouch *touch, uint16_t evt); -utf8_t *eve_selectw_option_get(EVESelectWidget *widget, int idx); -utf8_t *eve_selectw_option_get_select(EVESelectWidget *widget); -int eve_selectw_option_add(EVESelectWidget *widget, utf8_t *option); -int eve_selectw_option_set(EVESelectWidget *widget, utf8_t *option, uint16_t option_size); +utf8_t *eve_selectw_option(EVESelectWidget *widget, int idx); +utf8_t *eve_selectw_option_selected(EVESelectWidget *widget); +int eve_selectw_add_option(EVESelectWidget *widget, utf8_t *option); +int eve_selectw_set_option(EVESelectWidget *widget, utf8_t *option, uint16_t option_size); diff --git a/fw/fe310/eos/eve/widget/spacerw.c b/fw/fe310/eos/eve/widget/spacerw.c index d9f9104..aa5bec1 100644 --- a/fw/fe310/eos/eve/widget/spacerw.c +++ b/fw/fe310/eos/eve/widget/spacerw.c @@ -8,21 +8,17 @@ #include "screen/window.h" #include "screen/page.h" -#include "label.h" #include "widget.h" -#include "spacerw.h" -int eve_spacerw_create(EVESpacerWidget *widget, EVERect *g, EVEPage *page, EVESpacerSpec *spec) { - eve_spacerw_init(widget, g, page); - - return EVE_OK; +void eve_spacerw_init(EVEWidget *widget, EVERect *g, EVEPage *page) { + memset(widget, 0, sizeof(EVEWidget)); + eve_widget_init(widget, EVE_WIDGET_TYPE_SPACER, g, page, eve_spacerw_draw, eve_spacerw_touch, NULL); } -void eve_spacerw_init(EVESpacerWidget *widget, EVERect *g, EVEPage *page) { - EVEWidget *_widget = &widget->w; +int eve_spacerw_create(EVEWidget *_widget, EVEWidgetSpec *spec, EVEPage *page) { + eve_spacerw_init(_widget, &spec->g, page); - memset(widget, 0, sizeof(EVESpacerWidget)); - eve_widget_init(_widget, EVE_WIDGET_TYPE_SPACER, g, page, eve_spacerw_draw, eve_spacerw_touch, NULL); + return EVE_OK; } uint8_t eve_spacerw_draw(EVEWidget *_widget, uint8_t tag0) { diff --git a/fw/fe310/eos/eve/widget/spacerw.h b/fw/fe310/eos/eve/widget/spacerw.h index 9dafa8b..621b45b 100644 --- a/fw/fe310/eos/eve/widget/spacerw.h +++ b/fw/fe310/eos/eve/widget/spacerw.h @@ -1,14 +1,9 @@ #include <stdint.h> -typedef struct EVESpacerWidget { - EVEWidget w; -} EVESpacerWidget; +struct EVEWidgetSpec; -typedef struct EVESpacerSpec { -} EVESpacerSpec; - -int eve_spacerw_create(EVESpacerWidget *widget, EVERect *g, EVEPage *page, EVESpacerSpec *spec); -void eve_spacerw_init(EVESpacerWidget *widget, EVERect *g, EVEPage *page); +void eve_spacerw_init(EVEWidget *widget, EVERect *g, EVEPage *page); +int eve_spacerw_create(EVEWidget *_widget, struct EVEWidgetSpec *spec, EVEPage *page); uint8_t eve_spacerw_draw(EVEWidget *_widget, uint8_t tag0); int eve_spacerw_touch(EVEWidget *_widget, EVETouch *touch, uint16_t evt); diff --git a/fw/fe310/eos/eve/widget/strw.c b/fw/fe310/eos/eve/widget/strw.c index e78cf46..ae9ccab 100644 --- a/fw/fe310/eos/eve/widget/strw.c +++ b/fw/fe310/eos/eve/widget/strw.c @@ -10,9 +10,7 @@ #include "screen/window.h" #include "screen/page.h" -#include "label.h" #include "widget.h" -#include "strw.h" #define STRW_TOUCH_OPT EVE_TOUCH_OPT_TRACK | EVE_TOUCH_OPT_TRACK_X | EVE_TOUCH_OPT_TRACK_EXT_X | EVE_TOUCH_OPT_LPRESS @@ -29,20 +27,6 @@ #define CHAR_VALID_INPUT(c) ((c >= 0x20) && (c < 0x7f)) -int eve_strw_create(EVEStrWidget *widget, EVERect *g, EVEPage *page, EVEStrSpec *spec) { - EVEFont *font = spec->font ? spec->font : eve_window_font(page->v.window); - utf8_t *str; - uint16_t *line; - - str = eve_malloc(spec->str_size); - if (str == NULL) return EVE_ERR_NOMEM; - str[0] = '\0'; - - eve_strw_init(widget, g, page, font, str, spec->str_size); - - return EVE_OK; -} - void eve_strw_init(EVEStrWidget *widget, EVERect *g, EVEPage *page, EVEFont *font, utf8_t *str, uint16_t str_size) { EVEWidget *_widget = &widget->w; int rv, str_len; @@ -71,10 +55,29 @@ int eve_strw_update(EVEStrWidget *widget) { widget->str[str_len] = '\0'; } widget->str_len = str_len; + widget->str_g.w = eve_font_str_w(widget->font, widget->str); return (rv == UTF_OK) ? EVE_OK : EVE_ERR; } -void eve_strw_destroy(EVEStrWidget *widget) { +int eve_strw_create(EVEWidget *_widget, EVEWidgetSpec *spec, EVEPage *page) { + EVEStrWidget *widget = (EVEStrWidget *)_widget; + EVEStrSpec *tspec = &spec->tspec.str; + EVEFont *font = tspec->font ? tspec->font : eve_window_font(page->v.window); + utf8_t *str; + uint16_t *line; + + str = eve_malloc(tspec->str_size); + if (str == NULL) return EVE_ERR_NOMEM; + str[0] = '\0'; + + eve_strw_init(widget, &spec->g, page, font, str, tspec->str_size); + + return EVE_OK; +} + +void eve_strw_destroy(EVEWidget *_widget) { + EVEStrWidget *widget = (EVEStrWidget *)_widget; + eve_free(widget->str); } @@ -86,7 +89,7 @@ static void set_focus(EVEStrWidget *widget) { focus.y = _widget->g.y; focus.w = _widget->g.w; focus.h = 2 * widget->font->h; - eve_widget_focus(_widget, &focus); + eve_widget_set_focus(_widget, &focus); } static EVEStrCursor *cursor_prox(EVEStrWidget *widget, EVEStrCursor *cursor, EVETouch *touch, short *dx) { @@ -215,8 +218,8 @@ uint8_t eve_strw_draw(EVEWidget *_widget, uint8_t tag0) { } int eve_strw_touch(EVEWidget *_widget, EVETouch *touch, uint16_t evt) { - EVEPage *page = _widget->page; EVEStrWidget *widget = (EVEStrWidget *)_widget; + EVEPage *page = _widget->page; EVEStrCursor *t_cursor = NULL; short dx; int ret = 0; @@ -290,6 +293,7 @@ int eve_strw_touch(EVEWidget *_widget, EVETouch *touch, uint16_t evt) { widget->track.dx = 0; } + if (ret) eve_widget_set_focus(_widget, NULL); return ret; } @@ -305,7 +309,6 @@ void eve_strw_putc(void *w, int c) { int ins_c = 0, del_c = 0; int ins_w = 0, del_w = 0; - if (c == EVE_PAGE_KBDCH_CLOSE) { if (cursor1->on) eve_strw_cursor_clear(widget, cursor1); if (cursor2->on) eve_strw_cursor_clear(widget, cursor2); diff --git a/fw/fe310/eos/eve/widget/strw.h b/fw/fe310/eos/eve/widget/strw.h index 44e5e2d..acfd74b 100644 --- a/fw/fe310/eos/eve/widget/strw.h +++ b/fw/fe310/eos/eve/widget/strw.h @@ -1,5 +1,7 @@ #include <stdint.h> +struct EVEWidgetSpec; + typedef struct EVEStrCursor { char on; uint16_t x; @@ -31,13 +33,14 @@ typedef struct EVEStrSpec { uint16_t str_size; } EVEStrSpec; -int eve_strw_create(EVEStrWidget *widget, EVERect *g, EVEPage *page, EVEStrSpec *spec); void eve_strw_init(EVEStrWidget *widget, EVERect *g, EVEPage *page, EVEFont *font, utf8_t *str, uint16_t str_size); -void eve_strw_destroy(EVEStrWidget *widget); int eve_strw_update(EVEStrWidget *widget); +int eve_strw_create(EVEWidget *_widget, struct EVEWidgetSpec *spec, EVEPage *page); +void eve_strw_destroy(EVEWidget *_widget); uint8_t eve_strw_draw(EVEWidget *_widget, uint8_t tag0); int eve_strw_touch(EVEWidget *_widget, EVETouch *touch, uint16_t evt); + void eve_strw_putc(void *_page, int c); void eve_strw_cursor_set(EVEStrWidget *widget, EVEStrCursor *cursor, int16_t x); void eve_strw_cursor_clear(EVEStrWidget *widget, EVEStrCursor *cursor);
\ No newline at end of file diff --git a/fw/fe310/eos/eve/widget/textw.c b/fw/fe310/eos/eve/widget/textw.c index e994c0e..70a1e06 100644 --- a/fw/fe310/eos/eve/widget/textw.c +++ b/fw/fe310/eos/eve/widget/textw.c @@ -10,9 +10,7 @@ #include "screen/window.h" #include "screen/page.h" -#include "label.h" #include "widget.h" -#include "textw.h" #define TEXTW_TOUCH_OPT EVE_TOUCH_OPT_TRACK | EVE_TOUCH_OPT_TRACK_XY | EVE_TOUCH_OPT_TRACK_EXT_XY | EVE_TOUCH_OPT_LPRESS @@ -32,27 +30,6 @@ #define DIVC(x,y) ((x) / (y) + ((x) % (y) != 0)) -int eve_textw_create(EVETextWidget *widget, EVERect *g, EVEPage *page, EVETextSpec *spec) { - EVEFont *font = spec->font ? spec->font : eve_window_font(page->v.window); - utf8_t *text; - uint16_t *line; - - text = eve_malloc(spec->text_size); - if (text == NULL) { - return EVE_ERR_NOMEM; - } - text[0] = '\0'; - line = eve_malloc(sizeof(uint16_t) * spec->line_size); - if (line == NULL) { - free(text); - return EVE_ERR_NOMEM; - } - - eve_textw_init(widget, g, page, font, text, spec->text_size, line, spec->line_size); - - return EVE_OK; -} - void eve_textw_init(EVETextWidget *widget, EVERect *g, EVEPage *page, EVEFont *font, utf8_t *text, uint16_t text_size, uint16_t *line, uint16_t line_size) { EVEWidget *_widget = &widget->w; int rv, text_len; @@ -74,7 +51,32 @@ void eve_textw_init(EVETextWidget *widget, EVERect *g, EVEPage *page, EVEFont *f eve_textw_text_update(widget, 0, 0); } -void eve_textw_destroy(EVETextWidget *widget) { +int eve_textw_create(EVEWidget *_widget, EVEWidgetSpec *spec, EVEPage *page) { + EVETextWidget *widget = (EVETextWidget *)_widget; + EVETextSpec *tspec = &spec->tspec.text; + EVEFont *font = tspec->font ? tspec->font : eve_window_font(page->v.window); + utf8_t *text; + uint16_t *line; + + text = eve_malloc(tspec->text_size); + if (text == NULL) { + return EVE_ERR_NOMEM; + } + text[0] = '\0'; + line = eve_malloc(sizeof(uint16_t) * tspec->line_size); + if (line == NULL) { + free(text); + return EVE_ERR_NOMEM; + } + + eve_textw_init(widget, &spec->g, page, font, text, tspec->text_size, line, tspec->line_size); + + return EVE_OK; +} + +void eve_textw_destroy(EVEWidget *_widget) { + EVETextWidget *widget = (EVETextWidget *)_widget; + eve_free(widget->line); eve_free(widget->text); } @@ -87,7 +89,7 @@ static void set_focus(EVETextWidget *widget, EVETextCursor *cursor) { focus.y = _widget->g.y + cursor->line * widget->font->h; focus.w = _widget->g.w; focus.h = 2 * widget->font->h; - eve_widget_focus(_widget, &focus); + eve_widget_set_focus(_widget, &focus); } static EVETextCursor *cursor_prox(EVETextWidget *widget, EVETextCursor *cursor, EVETouch *touch, short *dx, short *dl) { @@ -144,8 +146,8 @@ static void draw_cursor(EVETextWidget *widget, EVETextCursor *cursor) { } uint8_t eve_textw_draw(EVEWidget *_widget, uint8_t tag0) { - EVEPage *page = _widget->page; EVETextWidget *widget = (EVETextWidget *)_widget; + EVEPage *page = _widget->page; int line0, lineN; int _line0, _lineN; char lineNvisible; @@ -237,8 +239,8 @@ uint8_t eve_textw_draw(EVEWidget *_widget, uint8_t tag0) { } int eve_textw_touch(EVEWidget *_widget, EVETouch *touch, uint16_t evt) { - EVEPage *page = _widget->page; EVETextWidget *widget = (EVETextWidget *)_widget; + EVEPage *page = _widget->page; EVETextCursor *t_cursor = NULL; short dx, dl; int ret = 0; @@ -289,6 +291,7 @@ int eve_textw_touch(EVEWidget *_widget, EVETouch *touch, uint16_t evt) { widget->track.dl = 0; } + if (ret) eve_widget_set_focus(_widget, NULL); return ret; } diff --git a/fw/fe310/eos/eve/widget/textw.h b/fw/fe310/eos/eve/widget/textw.h index 570bba8..1c33733 100644 --- a/fw/fe310/eos/eve/widget/textw.h +++ b/fw/fe310/eos/eve/widget/textw.h @@ -1,5 +1,7 @@ #include <stdint.h> +struct EVEWidgetSpec; + typedef struct EVETextCursor { char on; uint16_t x; @@ -32,10 +34,10 @@ typedef struct EVETextSpec { uint16_t line_size; } EVETextSpec; -int eve_textw_create(EVETextWidget *widget, EVERect *g, EVEPage *page, EVETextSpec *spec); void eve_textw_init(EVETextWidget *widget, EVERect *g, EVEPage *page, EVEFont *font, utf8_t *text, uint16_t text_size, uint16_t *line, uint16_t line_size); -void eve_textw_destroy(EVETextWidget *widget); +int eve_textw_create(EVEWidget *_widget, struct EVEWidgetSpec *spec, EVEPage *page); +void eve_textw_destroy(EVEWidget *_widget); uint8_t eve_textw_draw(EVEWidget *_widget, uint8_t tag0); int eve_textw_touch(EVEWidget *_widget, EVETouch *touch, uint16_t evt); void eve_textw_putc(void *_w, int c); diff --git a/fw/fe310/eos/eve/widget/togglew.c b/fw/fe310/eos/eve/widget/togglew.c new file mode 100644 index 0000000..fbebacb --- /dev/null +++ b/fw/fe310/eos/eve/widget/togglew.c @@ -0,0 +1,58 @@ +#include <stdlib.h> +#include <string.h> + +#include "eve.h" +#include "eve_kbd.h" +#include "eve_font.h" + +#include "screen/window.h" +#include "screen/page.h" + +#include "widget.h" + +void eve_togglew_init(EVEToggleWidget *widget, EVERect *g, EVEPage *page, EVEFont *font, char *labels) { + EVEWidget *_widget = &widget->w; + + memset(widget, 0, sizeof(EVEPageWidget)); + eve_widget_init(_widget, EVE_WIDGET_TYPE_TOGGLE, g, page, eve_togglew_draw, eve_togglew_touch, NULL); + widget->font = font; + widget->labels = labels; + if (_widget->g.w == 0) _widget->g.w = 3 * eve_font_h(widget->font); + if (_widget->g.h == 0) _widget->g.h = eve_font_h(widget->font); +} + +int eve_togglew_create(EVEWidget *_widget, EVEWidgetSpec *spec, EVEPage *page) { + EVEToggleWidget *widget = (EVEToggleWidget *)_widget; + EVEToggleSpec *tspec = &spec->tspec.toggle; + EVEFont *font = tspec->font ? tspec->font : eve_window_font(page->v.window); + + eve_togglew_init(widget, &spec->g, page, font, tspec->labels); + + return EVE_OK; +} + +uint8_t eve_togglew_draw(EVEWidget *_widget, uint8_t tag0) { + EVEToggleWidget *widget = (EVEToggleWidget *)_widget; + + _widget->tag0 = tag0; + if (tag0 != EVE_NOTAG) { + eve_cmd_dl(TAG(tag0)); + tag0++; + } + _widget->tagN = tag0; + + eve_cmd(CMD_TOGGLE, "hhhhhhs", _widget->g.x, _widget->g.y, _widget->g.w, widget->font->id, 0, widget->state ? 65535 : 0, widget->labels); + + return _widget->tagN; +} + +int eve_togglew_touch(EVEWidget *_widget, EVETouch *touch, uint16_t evt) { + EVEToggleWidget *widget = (EVEToggleWidget *)_widget; + + if (evt & EVE_TOUCH_ETYPE_TAG_UP) { + widget->state = !widget->state; + return 1; + } + + return 0; +} diff --git a/fw/fe310/eos/eve/widget/togglew.h b/fw/fe310/eos/eve/widget/togglew.h new file mode 100644 index 0000000..26ff77c --- /dev/null +++ b/fw/fe310/eos/eve/widget/togglew.h @@ -0,0 +1,21 @@ +#include <stdint.h> + +struct EVEWidgetSpec; + +typedef struct EVEToggleWidget { + EVEWidget w; + EVEFont *font; + char *labels; + uint8_t state; +} EVEToggleWidget; + +typedef struct EVEToggleSpec { + EVEFont *font; + char *labels; +} EVEToggleSpec; + +void eve_togglew_init(EVEToggleWidget *widget, EVERect *g, EVEPage *page, EVEFont *font, char *labels); + +int eve_togglew_create(EVEWidget *_widget, struct EVEWidgetSpec *spec, EVEPage *page); +uint8_t eve_togglew_draw(EVEWidget *_widget, uint8_t tag0); +int eve_togglew_touch(EVEWidget *_widget, EVETouch *touch, uint16_t evt); diff --git a/fw/fe310/eos/eve/widget/widget.c b/fw/fe310/eos/eve/widget/widget.c index 96d2ddf..a157301 100644 --- a/fw/fe310/eos/eve/widget/widget.c +++ b/fw/fe310/eos/eve/widget/widget.c @@ -8,37 +8,11 @@ #include "screen/window.h" #include "screen/page.h" -#include "widgets.h" - -static const size_t _widget_size[] = { - 0, - sizeof(EVEFreeWidget), - sizeof(EVESpacerWidget), - sizeof(EVEPageWidget), - sizeof(EVEStrWidget), - sizeof(EVETextWidget), - sizeof(EVESelectWidget), -}; - -static const eve_widget_create_t _widget_create[] = { - NULL, - (eve_widget_create_t)eve_freew_create, - (eve_widget_create_t)eve_spacerw_create, - (eve_widget_create_t)eve_pagew_create, - (eve_widget_create_t)eve_strw_create, - (eve_widget_create_t)eve_textw_create, - (eve_widget_create_t)eve_selectw_create, -}; - -static const eve_widget_destroy_t _widget_destroy[] = { - NULL, - NULL, - NULL, - NULL, - (eve_widget_destroy_t)eve_strw_destroy, - (eve_widget_destroy_t)eve_textw_destroy, - (eve_widget_destroy_t)eve_selectw_destroy, -}; +#include "widget.h" + +extern const size_t _eve_widget_size[]; +extern const eve_widget_create_t _eve_widget_create[]; +extern const eve_widget_destroy_t _eve_widget_destroy[]; void eve_widget_init(EVEWidget *widget, uint8_t type, EVERect *g, EVEPage *page, eve_widget_draw_t draw, eve_widget_touch_t touch, eve_kbd_input_handler_t putc) { if (g) widget->g = *g; @@ -50,7 +24,7 @@ void eve_widget_init(EVEWidget *widget, uint8_t type, EVERect *g, EVEPage *page, } size_t eve_widget_size(uint8_t type) { - return _widget_size[type]; + return _eve_widget_size[type]; } void eve_widget_set_label(EVEWidget *widget, EVELabel *label) { @@ -59,19 +33,32 @@ void eve_widget_set_label(EVEWidget *widget, EVELabel *label) { EVEWidget *eve_widget_next(EVEWidget *widget) { char *_w = (char *)widget; - return (EVEWidget *)(_w + _widget_size[widget->type]); + return (EVEWidget *)(_w + _eve_widget_size[widget->type]); } -int eve_widget_create(EVEWidget *widget, uint8_t type, EVERect *g, EVEPage *page, EVEWidgetSpecT *spec) { - return _widget_create[type](widget, g, page, spec); +int eve_widget_create(EVEWidget *widget, EVEWidgetSpec *spec, EVEPage *page) { + int rv; + + rv = _eve_widget_create[spec->type](widget, spec, page); + if (!rv) eve_widget_set_flags(widget, spec->flags); + + return rv; } void eve_widget_destroy(EVEWidget *widget) { - if (_widget_destroy[widget->type]) _widget_destroy[widget->type](widget); + if (_eve_widget_destroy[widget->type]) _eve_widget_destroy[widget->type](widget); } -void eve_widget_focus(EVEWidget *widget, EVERect *rect) { +void eve_widget_set_focus(EVEWidget *widget, EVERect *rect) { EVEPage *page = widget->page; - eve_page_focus_widget(page, widget, rect); + eve_page_set_focus(page, widget, rect); +} + +void eve_widget_set_flags(EVEWidget *widget, uint8_t flags) { + widget->flags |= flags; +} + +void eve_widget_clr_flags(EVEWidget *widget, uint8_t flags) { + widget->flags &= ~flags; } diff --git a/fw/fe310/eos/eve/widget/widget.h b/fw/fe310/eos/eve/widget/widget.h index f081b4c..0bef06e 100644 --- a/fw/fe310/eos/eve/widget/widget.h +++ b/fw/fe310/eos/eve/widget/widget.h @@ -1,11 +1,10 @@ #include <stdint.h> -#define EVE_WIDGET_TYPE_FREE 1 -#define EVE_WIDGET_TYPE_SPACER 2 -#define EVE_WIDGET_TYPE_PAGE 3 -#define EVE_WIDGET_TYPE_STR 4 -#define EVE_WIDGET_TYPE_TEXT 5 -#define EVE_WIDGET_TYPE_SELECT 6 +#include "label.h" + +#define EVEP_WIDGET_FLAG_RO 0x01 +#define EVEP_WIDGET_FLAG_HIDDEN 0x02 +#define EVEP_WIDGET_FLAG_BUSY 0x04 struct EVEWidget; @@ -20,12 +19,31 @@ typedef struct EVEWidget { eve_kbd_input_handler_t putc; EVELabel *label; uint8_t type; + uint8_t flags; uint8_t tag0; uint8_t tagN; } EVEWidget; +#include "widgets.h" + +typedef struct EVEWidgetSpec { + EVERect g; + EVEWidgetSpecT tspec; + uint8_t type; + uint8_t flags; +} EVEWidgetSpec; + +typedef int (*eve_widget_create_t) (EVEWidget *, EVEWidgetSpec *, EVEPage *); +typedef void (*eve_widget_destroy_t) (EVEWidget *); + +int eve_widget_create(EVEWidget *widget, EVEWidgetSpec *spec, EVEPage *page); +void eve_widget_destroy(EVEWidget *widget); + void eve_widget_init(EVEWidget *widget, uint8_t type, EVERect *g, EVEPage *page, eve_widget_draw_t draw, eve_widget_touch_t touch, eve_kbd_input_handler_t putc); size_t eve_widget_size(uint8_t type); void eve_widget_set_label(EVEWidget *widget, EVELabel *label); EVEWidget *eve_widget_next(EVEWidget *widget); -void eve_widget_focus(EVEWidget *widget, EVERect *rect);
\ No newline at end of file + +void eve_widget_set_focus(EVEWidget *widget, EVERect *rect); +void eve_widget_set_flags(EVEWidget *widget, uint8_t flags); +void eve_widget_clr_flags(EVEWidget *widget, uint8_t flags);
\ No newline at end of file diff --git a/fw/fe310/eos/eve/widget/widgets.c b/fw/fe310/eos/eve/widget/widgets.c new file mode 100644 index 0000000..ec422c1 --- /dev/null +++ b/fw/fe310/eos/eve/widget/widgets.c @@ -0,0 +1,43 @@ +#include <stdlib.h> + +#include "eve.h" +#include "eve_kbd.h" +#include "eve_font.h" + +#include "screen/window.h" +#include "screen/page.h" + +#include "widget.h" + +const size_t _eve_widget_size[] = { + 0, + sizeof(EVEFreeWidget), + sizeof(EVEWidget), + sizeof(EVEPageWidget), + sizeof(EVEToggleWidget), + sizeof(EVEStrWidget), + sizeof(EVETextWidget), + sizeof(EVESelectWidget), +}; + +const eve_widget_create_t _eve_widget_create[] = { + NULL, + eve_freew_create, + eve_spacerw_create, + eve_pagew_create, + eve_togglew_create, + eve_strw_create, + eve_textw_create, + eve_selectw_create, +}; + +const eve_widget_destroy_t _eve_widget_destroy[] = { + NULL, + NULL, + NULL, + NULL, + NULL, + eve_strw_destroy, + eve_textw_destroy, + eve_selectw_destroy, +}; diff --git a/fw/fe310/eos/eve/widget/widgets.h b/fw/fe310/eos/eve/widget/widgets.h index 2f8d3ca..9c96d8c 100644 --- a/fw/fe310/eos/eve/widget/widgets.h +++ b/fw/fe310/eos/eve/widget/widgets.h @@ -1,39 +1,24 @@ -#include "label.h" -#include "widget.h" - #include "freew.h" #include "spacerw.h" #include "pagew.h" #include "strw.h" #include "textw.h" #include "selectw.h" +#include "togglew.h" + +#define EVE_WIDGET_TYPE_FREE 1 +#define EVE_WIDGET_TYPE_SPACER 2 +#define EVE_WIDGET_TYPE_PAGE 3 +#define EVE_WIDGET_TYPE_TOGGLE 4 +#define EVE_WIDGET_TYPE_STR 5 +#define EVE_WIDGET_TYPE_TEXT 6 +#define EVE_WIDGET_TYPE_SELECT 7 typedef union EVEWidgetSpecT { EVEFreeSpec free; - EVESpacerSpec spacer; EVEPageSpec page; + EVEToggleSpec toggle; EVEStrSpec str; EVETextSpec text; EVESelectSpec select; } EVEWidgetSpecT; - -typedef struct EVELabelSpec { - EVERect g; - EVEFont *font; - char *title; -} APPLabelSpec; - -typedef struct EVEWidgetSpec { - APPLabelSpec label; - struct { - EVERect g; - EVEWidgetSpecT spec; - uint8_t type; - } widget; -} EVEWidgetSpec; - -typedef int (*eve_widget_create_t) (EVEWidget *, EVERect *, EVEPage *, EVEWidgetSpecT *); -typedef void (*eve_widget_destroy_t) (EVEWidget *); - -int eve_widget_create(EVEWidget *widget, uint8_t type, EVERect *g, EVEPage *page, EVEWidgetSpecT *spec); -void eve_widget_destroy(EVEWidget *widget); diff --git a/fw/fe310/eos/event.c b/fw/fe310/eos/event.c index 753cdb4..f76384a 100644 --- a/fw/fe310/eos/event.c +++ b/fw/fe310/eos/event.c @@ -121,22 +121,28 @@ void eos_evtq_flush_isr(void) { } void eos_evtq_loop(void) { + int foo = 1; + + while(foo) { + eos_evtq_exec(); + } +} + +void eos_evtq_exec(void) { unsigned char type; unsigned char *buffer; uint16_t len; - int foo = 1; - while(foo) { - clear_csr(mstatus, MSTATUS_MIE); - eos_msgq_pop(&_eos_event_q, &type, &buffer, &len); - if (type) { - set_csr(mstatus, MSTATUS_MIE); - evt_handler[0](type, buffer, len); - } else { - asm volatile ("wfi"); - set_csr(mstatus, MSTATUS_MIE); - } + clear_csr(mstatus, MSTATUS_MIE); + eos_msgq_pop(&_eos_event_q, &type, &buffer, &len); + if (type) { + set_csr(mstatus, MSTATUS_MIE); + evt_handler[0](type, buffer, len); + } else { + asm volatile ("wfi"); + set_csr(mstatus, MSTATUS_MIE); } + } void eos_evtq_bad_handler(unsigned char type, unsigned char *buffer, uint16_t len) { diff --git a/fw/fe310/eos/event.h b/fw/fe310/eos/event.h index 82043f4..9501a15 100644 --- a/fw/fe310/eos/event.h +++ b/fw/fe310/eos/event.h @@ -15,6 +15,7 @@ void eos_evtq_wait(unsigned char type, unsigned char *selector, uint16_t sel_len void eos_evtq_flush(void); void eos_evtq_flush_isr(void); void eos_evtq_loop(void); +void eos_evtq_exec(void); void eos_evtq_bad_handler(unsigned char type, unsigned char *buffer, uint16_t len); void eos_evtq_set_handler(unsigned char type, eos_evt_handler_t handler); diff --git a/fw/fe310/eos/evt_def.h b/fw/fe310/eos/evt_def.h index 16c4255..e34b2d2 100644 --- a/fw/fe310/eos/evt_def.h +++ b/fw/fe310/eos/evt_def.h @@ -3,7 +3,8 @@ #define EOS_EVT_NET 0x30 #define EOS_EVT_SPI 0x40 #define EOS_EVT_UART 0x50 -#define EOS_EVT_UI 0x60 +#define EOS_EVT_EVE 0x60 +#define EOS_EVT_CTP 0x70 #define EOS_EVT_USER 0x80 #define EOS_EVT_MAX_EVT 8 diff --git a/fw/fe310/eos/i2c/Makefile b/fw/fe310/eos/i2c/Makefile deleted file mode 100644 index 97e4f12..0000000 --- a/fw/fe310/eos/i2c/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -include ../../common.mk - -obj = bq25895.o ov2640.o - - -%.o: %.c %.h - $(CC) $(CFLAGS) -c $< - -%.o: %.S - $(CC) $(CFLAGS) -c $< - -all: $(obj) - -clean: - rm -f *.o diff --git a/fw/fe310/eos/irq_def.h b/fw/fe310/eos/irq_def.h index 5d9fb1e..ea3e7a8 100644 --- a/fw/fe310/eos/irq_def.h +++ b/fw/fe310/eos/irq_def.h @@ -8,4 +8,5 @@ #define IRQ_PRIORITY_UART 1 -#define IRQ_PRIORITY_UI 5 +#define IRQ_PRIORITY_EVE 5 +#define IRQ_PRIORITY_CTP 5 diff --git a/fw/fe310/eos/lcd.c b/fw/fe310/eos/lcd.c deleted file mode 100644 index cf09018..0000000 --- a/fw/fe310/eos/lcd.c +++ /dev/null @@ -1,522 +0,0 @@ -#include <stdlib.h> -#include <stdint.h> - -#include "encoding.h" -#include "platform.h" - -#include "eos.h" -#include "timer.h" -#include "i2s.h" -#include "net.h" -#include "spi_dev.h" -#include "eve/eve.h" - -#include "board.h" - -#include "lcd.h" - -#define BIT_PUT(b, pin) if (b) GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << (pin)); else GPIO_REG(GPIO_OUTPUT_VAL) &= ~(1 << (pin)); -#define BIT_GET(pin) ((GPIO_REG(GPIO_INPUT_VAL) & (1 << (pin))) >> (pin)) - -#define SCK_UP { GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << IOF_SPI1_SCK); } -#define SCK_DN { GPIO_REG(GPIO_OUTPUT_VAL) &= ~(1 << IOF_SPI1_SCK); } - -static inline void _sleep(int n) { - volatile int x = n; - - while(x) x--; -} - -int eos_lcd_select(void) { - if (eos_i2s_running()) return EOS_ERR_BUSY; - if (eos_spi_dev() != EOS_SPI_DEV_NET) return EOS_ERR_BUSY; - - eos_net_stop(); - - GPIO_REG(GPIO_OUTPUT_VAL) &= ~(1 << LCD_PIN_CS); - - GPIO_REG(GPIO_INPUT_EN) &= ~(1 << LCD_PIN_CS); - GPIO_REG(GPIO_OUTPUT_EN) |= (1 << LCD_PIN_CS); - - GPIO_REG(GPIO_IOF_EN) &= ~SPI_IOF_MASK; - - return EOS_OK; -} - -void eos_lcd_deselect(void) { - GPIO_REG(GPIO_IOF_EN) |= SPI_IOF_MASK; - eos_net_start(); -} - -void eos_lcd_cs_set(void) { - GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << LCD_PIN_CS); -} - -void eos_lcd_cs_clear(void) { - GPIO_REG(GPIO_OUTPUT_VAL) &= ~(1 << LCD_PIN_CS); -} - -/* sck frequency for r/w operations is 0.8Mhz */ -void eos_lcd_write(uint8_t dc, uint8_t data) { - int i; - - BIT_PUT(dc, IOF_SPI1_MOSI); - _sleep(10); - SCK_UP; - for (i=0; i<8; i++) { - _sleep(10); - SCK_DN; - BIT_PUT(data & 0x80, IOF_SPI1_MOSI); - _sleep(10); - SCK_UP; - data = data << 1; - } - _sleep(10); - SCK_DN; -} - -void eos_lcd_read(uint8_t *data) { - int i; - - *data = 0; - for (i=0; i<8; i++) { - _sleep(10); - *data = *data << 1; - *data |= BIT_GET(IOF_SPI1_MISO); - SCK_UP; - _sleep(10); - SCK_DN; - } -} - -static int _init(void) { - int rv; - - rv = eos_lcd_select(); - if (rv) return rv; - eos_lcd_cs_set(); - - /* LCD Setting */ - eos_lcd_write(0, 0xFF); // change to Page 1 CMD - eos_lcd_write(1, 0xFF); - eos_lcd_write(1, 0x98); - eos_lcd_write(1, 0x06); - eos_lcd_write(1, 0x04); - eos_lcd_write(1, 0x01); - - // eos_lcd_write(0, 0x08); // Output SDA - // eos_lcd_write(1, 0x10); - - eos_lcd_write(0, 0x20); // set DE/VSYNC mode - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x21); // DE = 1 Active - eos_lcd_write(1, 0x01); - - eos_lcd_write(0, 0x30); // resolution setting 480 X 854 - eos_lcd_write(1, 0x01); - - eos_lcd_write(0, 0x31); // inversion setting 2-dot - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x40); // BT AVDD,AVDD - eos_lcd_write(1, 0x16); - - eos_lcd_write(0, 0x41); - eos_lcd_write(1, 0x33); // 22 - - eos_lcd_write(0, 0x42); - eos_lcd_write(1, 0x03); // VGL=DDVDH+VCIP-DDVDL, VGH=2DDVDL-VCIP - - eos_lcd_write(0, 0x43); - eos_lcd_write(1, 0x09); // set VGH clamp level - - eos_lcd_write(0, 0x44); - eos_lcd_write(1, 0x06); // set VGL clamp level - - eos_lcd_write(0, 0x50); // VREG1 - eos_lcd_write(1, 0x88); - - eos_lcd_write(0, 0x51); // VREG2 - eos_lcd_write(1, 0x88); - - eos_lcd_write(0, 0x52); // flicker MSB - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x53); // flicker LSB - eos_lcd_write(1, 0x49); // VCOM - - eos_lcd_write(0, 0x55); // flicker - eos_lcd_write(1, 0x49); - - eos_lcd_write(0, 0x60); - eos_lcd_write(1, 0x07); - - eos_lcd_write(0, 0x61); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x62); - eos_lcd_write(1, 0x07); - - eos_lcd_write(0, 0x63); - eos_lcd_write(1, 0x00); - - /* Gamma Setting */ - eos_lcd_write(0, 0xA0); // positive Gamma - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0xA1); - eos_lcd_write(1, 0x09); - - eos_lcd_write(0, 0xA2); - eos_lcd_write(1, 0x11); - - eos_lcd_write(0, 0xA3); - eos_lcd_write(1, 0x0B); - - eos_lcd_write(0, 0xA4); - eos_lcd_write(1, 0x05); - - eos_lcd_write(0, 0xA5); - eos_lcd_write(1, 0x08); - - eos_lcd_write(0, 0xA6); - eos_lcd_write(1, 0x06); - - eos_lcd_write(0, 0xA7); - eos_lcd_write(1, 0x04); - - eos_lcd_write(0, 0xA8); - eos_lcd_write(1, 0x09); - - eos_lcd_write(0, 0xA9); - eos_lcd_write(1, 0x0C); - - eos_lcd_write(0, 0xAA); - eos_lcd_write(1, 0x15); - - eos_lcd_write(0, 0xAB); - eos_lcd_write(1, 0x08); - - eos_lcd_write(0, 0xAC); - eos_lcd_write(1, 0x0F); - - eos_lcd_write(0, 0xAD); - eos_lcd_write(1, 0x12); - - eos_lcd_write(0, 0xAE); - eos_lcd_write(1, 0x09); - - eos_lcd_write(0, 0xAF); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0xC0); // negative Gamma - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0xC1); - eos_lcd_write(1, 0x09); - - eos_lcd_write(0, 0xC2); - eos_lcd_write(1, 0x10); - - eos_lcd_write(0, 0xC3); - eos_lcd_write(1, 0x0C); - - eos_lcd_write(0, 0xC4); - eos_lcd_write(1, 0x05); - - eos_lcd_write(0, 0xC5); - eos_lcd_write(1, 0x08); - - eos_lcd_write(0, 0xC6); - eos_lcd_write(1, 0x06); - - eos_lcd_write(0, 0xC7); - eos_lcd_write(1, 0x04); - - eos_lcd_write(0, 0xC8); - eos_lcd_write(1, 0x08); - - eos_lcd_write(0, 0xC9); - eos_lcd_write(1, 0x0C); - - eos_lcd_write(0, 0xCA); - eos_lcd_write(1, 0x14); - - eos_lcd_write(0, 0xCB); - eos_lcd_write(1, 0x08); - - eos_lcd_write(0, 0xCC); - eos_lcd_write(1, 0x0F); - - eos_lcd_write(0, 0xCD); - eos_lcd_write(1, 0x11); - - eos_lcd_write(0, 0xCE); - eos_lcd_write(1, 0x09); - - eos_lcd_write(0, 0xCF); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0xFF); // change to Page 6 CMD for GIP timing - eos_lcd_write(1, 0xFF); - eos_lcd_write(1, 0x98); - eos_lcd_write(1, 0x06); - eos_lcd_write(1, 0x04); - eos_lcd_write(1, 0x06); - - eos_lcd_write(0, 0x00); - eos_lcd_write(1, 0x20); - - eos_lcd_write(0, 0x01); - eos_lcd_write(1, 0x0A); - - eos_lcd_write(0, 0x02); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x03); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x04); - eos_lcd_write(1, 0x01); - - eos_lcd_write(0, 0x05); - eos_lcd_write(1, 0x01); - - eos_lcd_write(0, 0x06); - eos_lcd_write(1, 0x98); - - eos_lcd_write(0, 0x07); - eos_lcd_write(1, 0x06); - - eos_lcd_write(0, 0x08); - eos_lcd_write(1, 0x01); - - eos_lcd_write(0, 0x09); - eos_lcd_write(1, 0x80); - - eos_lcd_write(0, 0x0A); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x0B); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x0C); - eos_lcd_write(1, 0x01); - - eos_lcd_write(0, 0x0D); - eos_lcd_write(1, 0x01); - - eos_lcd_write(0, 0x0E); - eos_lcd_write(1, 0x05); - - eos_lcd_write(0, 0x0F); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x10); - eos_lcd_write(1, 0xF0); - - eos_lcd_write(0, 0x11); - eos_lcd_write(1, 0xF4); - - eos_lcd_write(0, 0x12); - eos_lcd_write(1, 0x01); - - eos_lcd_write(0, 0x13); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x14); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x15); - eos_lcd_write(1, 0xC0); - - eos_lcd_write(0, 0x16); - eos_lcd_write(1, 0x08); - - eos_lcd_write(0, 0x17); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x18); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x19); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x1A); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x1B); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x1C); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x1D); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x20); - eos_lcd_write(1, 0x01); - - eos_lcd_write(0, 0x21); - eos_lcd_write(1, 0x23); - - eos_lcd_write(0, 0x22); - eos_lcd_write(1, 0x45); - - eos_lcd_write(0, 0x23); - eos_lcd_write(1, 0x67); - - eos_lcd_write(0, 0x24); - eos_lcd_write(1, 0x01); - - eos_lcd_write(0, 0x25); - eos_lcd_write(1, 0x23); - - eos_lcd_write(0, 0x26); - eos_lcd_write(1, 0x45); - - eos_lcd_write(0, 0x27); - eos_lcd_write(1, 0x67); - - eos_lcd_write(0, 0x30); - eos_lcd_write(1, 0x11); - - eos_lcd_write(0, 0x31); - eos_lcd_write(1, 0x11); - - eos_lcd_write(0, 0x32); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x33); - eos_lcd_write(1, 0xEE); - - eos_lcd_write(0, 0x34); - eos_lcd_write(1, 0xFF); - - eos_lcd_write(0, 0x35); - eos_lcd_write(1, 0xBB); - - eos_lcd_write(0, 0x36); - eos_lcd_write(1, 0xAA); - - eos_lcd_write(0, 0x37); - eos_lcd_write(1, 0xDD); - - eos_lcd_write(0, 0x38); - eos_lcd_write(1, 0xCC); - - eos_lcd_write(0, 0x39); - eos_lcd_write(1, 0x66); - - eos_lcd_write(0, 0x3A); - eos_lcd_write(1, 0x77); - - eos_lcd_write(0, 0x3B); - eos_lcd_write(1, 0x22); - - eos_lcd_write(0, 0x3C); - eos_lcd_write(1, 0x22); - - eos_lcd_write(0, 0x3D); - eos_lcd_write(1, 0x22); - - eos_lcd_write(0, 0x3E); - eos_lcd_write(1, 0x22); - - eos_lcd_write(0, 0x3F); - eos_lcd_write(1, 0x22); - - eos_lcd_write(0, 0x40); - eos_lcd_write(1, 0x22); - - eos_lcd_write(0, 0xFF); // change to Page 7 CMD for GIP timing - eos_lcd_write(1, 0xFF); - eos_lcd_write(1, 0x98); - eos_lcd_write(1, 0x06); - eos_lcd_write(1, 0x04); - eos_lcd_write(1, 0x07); - - eos_lcd_write(0, 0x17); - eos_lcd_write(1, 0x22); - - eos_lcd_write(0, 0x02); - eos_lcd_write(1, 0x77); - - eos_lcd_write(0, 0x26); - eos_lcd_write(1, 0xB2); - - eos_lcd_write(0, 0xFF); // change to Page 0 CMD for normal command - eos_lcd_write(1, 0xFF); - eos_lcd_write(1, 0x98); - eos_lcd_write(1, 0x06); - eos_lcd_write(1, 0x04); - eos_lcd_write(1, 0x00); - - eos_lcd_write(0, 0x3A); - eos_lcd_write(1, 0x70); // 24BIT - - eos_lcd_write(0, 0x11); - eos_time_sleep(120); - eos_lcd_write(0, 0x29); - eos_time_sleep(25); - - eos_lcd_cs_clear(); - eos_lcd_deselect(); - - return EOS_OK; -} - -int eos_lcd_init(uint8_t wakeup_cause) { - eos_spi_select(EOS_SPI_DEV_EVE); - eve_gpio_set(EVE_GPIO_LCD_EN, 1); - eos_spi_deselect(); - eos_time_sleep(200); - - return _init(); -} - -int eos_lcd_sleep(void) { - int rv; - - rv = eos_lcd_select(); - if (rv) return rv; - - eos_lcd_cs_set(); - - eos_lcd_write(0, 0x28); - eos_time_sleep(10); - eos_lcd_write(0, 0x10); - - eos_lcd_cs_clear(); - eos_lcd_deselect(); - - eos_spi_select(EOS_SPI_DEV_EVE); - eve_gpio_set(EVE_GPIO_LCD_EN, 0); - eos_spi_deselect(); - - return EOS_OK; -} - -int eos_lcd_wake(void) { - int rv; - - eos_spi_select(EOS_SPI_DEV_EVE); - eve_gpio_set(EVE_GPIO_LCD_EN, 1); - eos_spi_deselect(); - eos_time_sleep(200); - - rv = eos_lcd_select(); - if (rv) return rv; - - eos_lcd_cs_set(); - - eos_lcd_write(0, 0x11); - eos_time_sleep(120); - eos_lcd_write(0, 0x29); - - eos_lcd_cs_clear(); - eos_lcd_deselect(); - - return EOS_OK; -} diff --git a/fw/fe310/eos/lcd.h b/fw/fe310/eos/lcd.h deleted file mode 100644 index 1bcb2cd..0000000 --- a/fw/fe310/eos/lcd.h +++ /dev/null @@ -1,12 +0,0 @@ -#include <stdint.h> - -int eos_lcd_init(uint8_t wakeup_cause); -int eos_lcd_select(void); -void eos_lcd_deselect(void); -void eos_lcd_cs_set(void); -void eos_lcd_cs_clear(void); -void eos_lcd_write(uint8_t dc, uint8_t data); -void eos_lcd_read(uint8_t *data); - -int eos_lcd_sleep(void); -int eos_lcd_wake(void);
\ No newline at end of file diff --git a/fw/fe310/eos/net/Makefile b/fw/fe310/eos/net/Makefile new file mode 100644 index 0000000..fc65454 --- /dev/null +++ b/fw/fe310/eos/net/Makefile @@ -0,0 +1,19 @@ +include ../../common.mk + +obj = rng.o pwr.o wifi.o sock.o cell.o +lib = ../../libeos-net.a + + +%.o: %.c %.h + $(CC) $(CFLAGS) -c $< + +%.o: %.S + $(CC) $(CFLAGS) -c $< + +all: $(lib) + +$(lib): $(obj) + $(AR) rcs $@ $(obj) + +clean: + rm -f *.o $(lib) diff --git a/fw/fe310/eos/cell.c b/fw/fe310/eos/net/cell.c index 20a9f42..4bfbb35 100644 --- a/fw/fe310/eos/cell.c +++ b/fw/fe310/eos/net/cell.c @@ -4,7 +4,7 @@ #include "eos.h" #include "event.h" -#include "net.h" +#include "dev/net.h" #include "cell.h" @@ -28,7 +28,7 @@ static void cell_handle_msg(unsigned char type, unsigned char *buffer, uint16_t } } -void eos_cell_netinit(void) { +void eos_cell_init(void) { int i; for (i=0; i<EOS_CELL_MAX_MTYPE; i++) { diff --git a/fw/fe310/eos/cell.h b/fw/fe310/eos/net/cell.h index b01d4cf..f04eef2 100644 --- a/fw/fe310/eos/cell.h +++ b/fw/fe310/eos/net/cell.h @@ -1,5 +1,5 @@ #include <stdint.h> -#include "event.h" +#include "../event.h" #define EOS_CELL_MTYPE_DEV 0x10 #define EOS_CELL_MTYPE_VOICE 0x20 @@ -38,14 +38,24 @@ #define EOS_CELL_MTYPE_USSD_REPLY 2 #define EOS_CELL_MTYPE_USSD_CANCEL 3 -#define EOS_CELL_MTYPE_PDP_CONFIG 1 -#define EOS_CELL_MTYPE_PDP_CONNECT 2 -#define EOS_CELL_MTYPE_PDP_DISCONNECT 3 +#define EOS_CELL_MTYPE_PDP_GET_APN 1 +#define EOS_CELL_MTYPE_PDP_GET_USR 2 +#define EOS_CELL_MTYPE_PDP_GET_PWD 3 +#define EOS_CELL_MTYPE_PDP_SET_APN 4 +#define EOS_CELL_MTYPE_PDP_SET_USR 5 +#define EOS_CELL_MTYPE_PDP_SET_PWD 6 +#define EOS_CELL_MTYPE_PDP_CONNECT 7 +#define EOS_CELL_MTYPE_PDP_DISCONNECT 8 #define EOS_CELL_SMS_ADDRTYPE_INTL 1 #define EOS_CELL_SMS_ADDRTYPE_ALPHA 2 #define EOS_CELL_SMS_ADDRTYPE_OTHER 3 -void eos_cell_netinit(void); +#define EOS_CELL_PDP_SIZE_APN 64 +#define EOS_CELL_PDP_SIZE_USR 64 +#define EOS_CELL_PDP_SIZE_PWD 64 +#define EOS_CELL_PDP_SIZE_ARG 64 + +void eos_cell_init(void); void eos_cell_set_handler(unsigned char mtype, eos_evt_handler_t handler); eos_evt_handler_t eos_cell_get_handler(unsigned char mtype);
\ No newline at end of file diff --git a/fw/fe310/eos/net/pwr.c b/fw/fe310/eos/net/pwr.c new file mode 100644 index 0000000..734e3cd --- /dev/null +++ b/fw/fe310/eos/net/pwr.c @@ -0,0 +1,69 @@ +#include <stdlib.h> +#include <stdint.h> + +#include "eos.h" +#include "event.h" +#include "dev/net.h" + +#include "soc/pwr.h" +#include "soc/spi.h" +#include "dev/spi.h" +#include "eve/eve.h" + +#include "pwr.h" + +static eos_evt_handler_t evt_handler[EOS_PWR_MAX_MTYPE]; +static unsigned char power_btn_down; + +static void pwr_handle_msg(unsigned char type, unsigned char *buffer, uint16_t len) { + unsigned char mtype; + + if ((buffer == NULL) || (len < 1)) { + eos_net_bad_handler(type, buffer, len); + return; + } + + mtype = buffer[0]; + if ((mtype < EOS_PWR_MAX_MTYPE) && evt_handler[mtype]) { + evt_handler[mtype](mtype, buffer, len); + } else { + eos_net_bad_handler(type, buffer, len); + } +} + +static void pwr_handle_btn(unsigned char type, unsigned char *buffer, uint16_t len) { + int rv; + unsigned char level = buffer[1]; + + eos_net_free(buffer, 0); + if (!level) { + power_btn_down = 1; + return; + } + if (!power_btn_down) return; + + eos_spi_select(EOS_SPI_DEV_EVE); + eve_pwr_sleep(); + eos_spi_deselect(); + + rv = eos_pwr_sleep(); +} + +void eos_pwr_net_init(void) { + int i; + + for (i=0; i<EOS_PWR_MAX_MTYPE; i++) { + evt_handler[i] = NULL; + } + eos_net_set_handler(EOS_NET_MTYPE_POWER, pwr_handle_msg); + eos_pwr_set_handler(EOS_PWR_MTYPE_BUTTON, pwr_handle_btn); +} + +void eos_pwr_set_handler(unsigned char mtype, eos_evt_handler_t handler) { + if (mtype < EOS_PWR_MAX_MTYPE) evt_handler[mtype] = handler; +} + +eos_evt_handler_t eos_pwr_get_handler(unsigned char mtype) { + if (mtype < EOS_PWR_MAX_MTYPE) return evt_handler[mtype]; + return NULL; +} diff --git a/fw/fe310/eos/net/pwr.h b/fw/fe310/eos/net/pwr.h new file mode 100644 index 0000000..b82a96b --- /dev/null +++ b/fw/fe310/eos/net/pwr.h @@ -0,0 +1,10 @@ +#include <stdint.h> +#include "../event.h" + +#define EOS_PWR_MTYPE_BUTTON 1 + +#define EOS_PWR_MAX_MTYPE 2 + +void eos_pwr_net_init(void); +void eos_pwr_set_handler(unsigned char mtype, eos_evt_handler_t handler); +eos_evt_handler_t eos_pwr_get_handler(unsigned char mtype); diff --git a/fw/fe310/eos/rng.c b/fw/fe310/eos/net/rng.c index 317941d..7d05a81 100644 --- a/fw/fe310/eos/rng.c +++ b/fw/fe310/eos/net/rng.c @@ -2,7 +2,7 @@ #include <stdint.h> #include <string.h> -#include "net.h" +#include "dev/net.h" int getentropy(unsigned char *b, size_t sz) { unsigned char type; diff --git a/fw/fe310/eos/rng.h b/fw/fe310/eos/net/rng.h index 27bbf74..27bbf74 100644 --- a/fw/fe310/eos/rng.h +++ b/fw/fe310/eos/net/rng.h diff --git a/fw/fe310/eos/sock.c b/fw/fe310/eos/net/sock.c index 7365c97..1db0cd9 100644 --- a/fw/fe310/eos/sock.c +++ b/fw/fe310/eos/net/sock.c @@ -4,7 +4,7 @@ #include "eos.h" #include "event.h" -#include "net.h" +#include "dev/net.h" #include "sock.h" @@ -34,7 +34,7 @@ static void sock_handle_msg(unsigned char type, unsigned char *buffer, uint16_t } } -void eos_sock_netinit(void) { +void eos_sock_init(void) { int i; for (i=0; i<EOS_SOCK_MAX_SOCK; i++) { @@ -69,20 +69,29 @@ int eos_sock_open_udp(eos_evt_handler_t handler, unsigned char *buffer) { buffer[0] = EOS_SOCK_MTYPE_OPEN_DGRAM; rv = eos_net_xchg(&type, buffer, &len); - if (rv) return rv; + if (rv) goto sock_open_udp_fin; - if (type != EOS_NET_MTYPE_SOCK) return EOS_ERR_NET; - if (len < 2) return EOS_ERR_SIZE; + if (type != EOS_NET_MTYPE_SOCK) { + rv = EOS_ERR_NET; + goto sock_open_udp_fin; + } + if (len < 2) { + rv = EOS_ERR_SIZE; + goto sock_open_udp_fin; + } sock = buffer[1]; - if (sock == 0) return EOS_ERR_NET; - - if (do_release) { - eos_net_free(buffer, 1); + if (sock == 0) { + rv = EOS_ERR_NET; + goto sock_open_udp_fin; } + + rv = sock; eos_sock_set_handler(sock, handler); - return sock; +sock_open_udp_fin: + if (do_release) eos_net_free(buffer, 1); + return rv; } void eos_sock_close(unsigned char sock, unsigned char *buffer) { diff --git a/fw/fe310/eos/sock.h b/fw/fe310/eos/net/sock.h index 5ef42e3..e2f8637 100644 --- a/fw/fe310/eos/sock.h +++ b/fw/fe310/eos/net/sock.h @@ -1,5 +1,5 @@ #include <stdint.h> -#include "event.h" +#include "../event.h" #define EOS_SOCK_MTYPE_PKT 0 #define EOS_SOCK_MTYPE_OPEN_DGRAM 1 @@ -16,7 +16,7 @@ typedef struct EOSNetAddr { uint16_t port; } EOSNetAddr; -void eos_sock_netinit(void); +void eos_sock_init(void); void eos_sock_set_handler(unsigned char sock, eos_evt_handler_t handler); eos_evt_handler_t eos_sock_get_handler(unsigned char sock); diff --git a/fw/fe310/eos/net/wifi.c b/fw/fe310/eos/net/wifi.c new file mode 100644 index 0000000..4db49f8 --- /dev/null +++ b/fw/fe310/eos/net/wifi.c @@ -0,0 +1,179 @@ +#include <stdlib.h> +#include <stdint.h> +#include <string.h> + +#include "eos.h" +#include "event.h" +#include "dev/net.h" + +#include "wifi.h" + +#define WIFI_SIZE_SSID 33 +#define WIFI_SIZE_PWD 64 + +static eos_evt_handler_t evt_handler[EOS_WIFI_MAX_MTYPE]; + +static void wifi_handle_msg(unsigned char type, unsigned char *buffer, uint16_t len) { + unsigned char mtype; + + if ((buffer == NULL) || (len < 1)) { + eos_net_bad_handler(type, buffer, len); + return; + } + + mtype = buffer[0]; + if ((mtype < EOS_WIFI_MAX_MTYPE) && evt_handler[mtype]) { + evt_handler[mtype](mtype, buffer, len); + } else { + eos_net_bad_handler(type, buffer, len); + } +} + +void eos_wifi_init(void) { + int i; + + for (i=0; i<EOS_WIFI_MAX_MTYPE; i++) { + evt_handler[i] = NULL; + } + eos_net_set_handler(EOS_NET_MTYPE_WIFI, wifi_handle_msg); +} + +void eos_wifi_set_handler(unsigned char mtype, eos_evt_handler_t handler) { + if (mtype < EOS_WIFI_MAX_MTYPE) evt_handler[mtype] = handler; +} + +eos_evt_handler_t eos_wifi_get_handler(unsigned char mtype) { + if (mtype < EOS_WIFI_MAX_MTYPE) return evt_handler[mtype]; + return NULL; +} + +int eos_wifi_status(unsigned char *buffer, uint8_t *status, uint8_t ip_addr[], char *ssid) { + unsigned char *buf; + unsigned char type; + uint16_t len; + int do_release; + int rv; + + do_release = 0; + if (buffer == NULL) { + buffer = eos_net_alloc(); + do_release = 1; + } + + type = EOS_NET_MTYPE_WIFI; + len = 1; + buffer[0] = EOS_WIFI_MTYPE_STATUS; + + rv = eos_net_xchg(&type, buffer, &len); + if (rv) goto wifi_status_fin; + + if (type != EOS_NET_MTYPE_WIFI) { + rv = EOS_ERR_NET; + goto wifi_status_fin; + } + if (len < 2) { + rv = EOS_ERR_SIZE; + goto wifi_status_fin; + } + + *status = buffer[1]; + buf = buffer + 2; + len -= 2; + + switch (*status) { + case EOS_WIFI_STATUS_GOT_IP: + if (len < sizeof(uint32_t)) { + rv = EOS_ERR_SIZE; + goto wifi_status_fin; + } + if (ip_addr) memcpy(ip_addr, buf, sizeof(uint32_t)); + buf += sizeof(uint32_t); + len -= sizeof(uint32_t); + case EOS_WIFI_STATUS_CONNECTED: + if ((len == 0) || (len > WIFI_SIZE_SSID)) { + rv = EOS_ERR_SIZE; + goto wifi_status_fin; + } + buf[len - 1] = '\0'; + if (ssid) strcpy(ssid, buf); + break; + } + +wifi_status_fin: + if (do_release) eos_net_free(buffer, 1); + return rv; +} + +int eos_wifi_start(unsigned char *buffer) { + int async; + + async = 0; + if (buffer == NULL) { + buffer = eos_net_alloc(); + async = 1; + } + buffer[0] = EOS_WIFI_MTYPE_START; + return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1); +} + +int eos_wifi_stop(unsigned char *buffer) { + int async; + + async = 0; + if (buffer == NULL) { + buffer = eos_net_alloc(); + async = 1; + } + buffer[0] = EOS_WIFI_MTYPE_STOP; + return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1); +} + +int eos_wifi_scan(unsigned char *buffer) { + int async; + + async = 0; + if (buffer == NULL) { + buffer = eos_net_alloc(); + async = 1; + } + buffer[0] = EOS_WIFI_MTYPE_SCAN; + return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1); +} + +int eos_wifi_connect(const char *ssid, const char *pwd, unsigned char *buffer) { + unsigned char *buf; + size_t ssid_len, pwd_len; + int async; + + ssid_len = strlen(ssid); + pwd_len = strlen(pwd); + if (ssid_len > WIFI_SIZE_SSID - 1) return EOS_ERR_SIZE; + if (pwd_len > WIFI_SIZE_PWD - 1) return EOS_ERR_SIZE; + + async = 0; + if (buffer == NULL) { + buffer = eos_net_alloc(); + async = 1; + } + buf = buffer; + buf[0] = EOS_WIFI_MTYPE_CONNECT; + buf++; + strcpy(buf, ssid); + buf += ssid_len + 1; + strcpy(buf, pwd); + buf += pwd_len + 1; + + return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1); +} + +int eos_wifi_disconnect(unsigned char *buffer) { + int async; + + async = 0; + if (buffer == NULL) { + buffer = eos_net_alloc(); + async = 1; + } + buffer[0] = EOS_WIFI_MTYPE_DISCONNECT; + return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1); +} diff --git a/fw/fe310/eos/net/wifi.h b/fw/fe310/eos/net/wifi.h new file mode 100644 index 0000000..2100144 --- /dev/null +++ b/fw/fe310/eos/net/wifi.h @@ -0,0 +1,27 @@ +#include <stdint.h> +#include "../event.h" + +#define EOS_WIFI_MTYPE_STATUS 0 +#define EOS_WIFI_MTYPE_SCAN 1 +#define EOS_WIFI_MTYPE_START 2 +#define EOS_WIFI_MTYPE_STOP 3 +#define EOS_WIFI_MTYPE_CONNECT 4 +#define EOS_WIFI_MTYPE_DISCONNECT 5 + +#define EOS_WIFI_MAX_MTYPE 2 + +#define EOS_WIFI_STATUS_OFF 0 +#define EOS_WIFI_STATUS_DISCONNECTED 1 +#define EOS_WIFI_STATUS_CONNECTED 2 +#define EOS_WIFI_STATUS_GOT_IP 3 + +void eos_wifi_init(void); +void eos_wifi_set_handler(unsigned char mtype, eos_evt_handler_t handler); +eos_evt_handler_t eos_wifi_get_handler(unsigned char mtype); + +int eos_wifi_status(unsigned char *buffer, uint8_t *status, uint8_t ip_addr[], char *ssid); +int eos_wifi_start(unsigned char *buffer); +int eos_wifi_stop(unsigned char *buffer); +int eos_wifi_scan(unsigned char *buffer); +int eos_wifi_connect(const char *ssid, const char *pwd, unsigned char *buffer); +int eos_wifi_disconnect(unsigned char *buffer);
\ No newline at end of file diff --git a/fw/fe310/eos/pwr.c b/fw/fe310/eos/pwr.c deleted file mode 100644 index f42249d..0000000 --- a/fw/fe310/eos/pwr.c +++ /dev/null @@ -1,133 +0,0 @@ -#include <stdlib.h> -#include <stdint.h> - -#include "encoding.h" -#include "platform.h" - -#include "eos.h" -#include "event.h" -#include "timer.h" -#include "spi.h" -#include "spi_dev.h" -#include "net.h" -#include "lcd.h" -#include "eve/eve.h" - -#include "pwr.h" - -#define PWR_RTC_SCALE 15 -#define PWR_RTC_SFREQ (EOS_TIMER_RTC_FREQ >> PWR_RTC_SCALE) - -static eos_evt_handler_t evt_handler[EOS_PWR_MAX_MTYPE]; -static unsigned char power_btn_down; - -int eos_pwr_init(uint8_t wakeup_cause) { - AON_REG(AON_PMUKEY) = 0x51F15E; - AON_REG(AON_PMUIE) = 0x5; - - AON_REG(AON_RTCCMP) = 0xFFFFFFFF; - AON_REG(AON_RTCCFG) = PWR_RTC_SCALE; - AON_REG(AON_RTCHI) = 0; - AON_REG(AON_RTCLO) = 0; - - return EOS_OK; -} - -uint8_t eos_pwr_wakeup_cause(void) { - return AON_REG(AON_PMUCAUSE) & 0xff; -} - -uint8_t eos_pwr_reset_cause(void) { - return (AON_REG(AON_PMUCAUSE) >> 8) & 0xff; -} - -int eos_pwr_sleep(void) { - int rv; - - rv = eos_lcd_sleep(); - if (rv) return rv; - - eos_spi_select(EOS_SPI_DEV_EVE); - eve_sleep(); - eos_spi_deselect(); - - rv = eos_net_sleep(1000); - if (rv) return rv; - - AON_REG(AON_PMUKEY) = 0x51F15E; - AON_REG(AON_PMUSLEEP) = 1; - - return EOS_OK; -} - -void eos_pwr_wake_at(uint32_t msec) { - uint32_t pmuie; - - AON_REG(AON_RTCCFG) |= AON_RTCCFG_ENALWAYS; - AON_REG(AON_RTCCMP) = msec * PWR_RTC_SFREQ / 1000; - - pmuie = AON_REG(AON_PMUIE) | 0x2; - AON_REG(AON_PMUKEY) = 0x51F15E; - AON_REG(AON_PMUIE) = pmuie; -} - -void eos_pwr_wake_disable(void) { - uint32_t pmuie; - - AON_REG(AON_RTCCMP) = 0xFFFFFFFF; - AON_REG(AON_RTCCFG) &= ~AON_RTCCFG_ENALWAYS; - AON_REG(AON_RTCHI) = 0; - AON_REG(AON_RTCLO) = 0; - - pmuie = AON_REG(AON_PMUIE) & ~0x2; - AON_REG(AON_PMUKEY) = 0x51F15E; - AON_REG(AON_PMUIE) = pmuie; -} - -static void pwr_handle_msg(unsigned char type, unsigned char *buffer, uint16_t len) { - unsigned char mtype; - - if ((buffer == NULL) || (len < 1)) { - eos_net_bad_handler(type, buffer, len); - return; - } - - mtype = buffer[0]; - if ((mtype < EOS_PWR_MAX_MTYPE) && evt_handler[mtype]) { - evt_handler[mtype](mtype, buffer, len); - } else { - eos_net_bad_handler(type, buffer, len); - } -} - -static void pwr_handle_btn(unsigned char type, unsigned char *buffer, uint16_t len) { - unsigned char level = buffer[1]; - - eos_net_free(buffer, 0); - if (!level) { - power_btn_down = 1; - return; - } - if (!power_btn_down) return; - - eos_pwr_sleep(); -} - -void eos_pwr_netinit(void) { - int i; - - for (i=0; i<EOS_PWR_MAX_MTYPE; i++) { - evt_handler[i] = NULL; - } - eos_net_set_handler(EOS_NET_MTYPE_POWER, pwr_handle_msg); - eos_pwr_set_handler(EOS_PWR_MTYPE_BUTTON, pwr_handle_btn); -} - -void eos_pwr_set_handler(unsigned char mtype, eos_evt_handler_t handler) { - if (mtype < EOS_PWR_MAX_MTYPE) evt_handler[mtype] = handler; -} - -eos_evt_handler_t eos_pwr_get_handler(unsigned char mtype) { - if (mtype < EOS_PWR_MAX_MTYPE) return evt_handler[mtype]; - return NULL; -} diff --git a/fw/fe310/eos/soc/Makefile b/fw/fe310/eos/soc/Makefile new file mode 100644 index 0000000..1404c81 --- /dev/null +++ b/fw/fe310/eos/soc/Makefile @@ -0,0 +1,20 @@ +include ../../common.mk +CFLAGS += -I$(bsp_dir)/include -I$(bsp_dir)/drivers + +obj = trap_entry.o interrupt.o timer.o pwr.o i2s.o i2c.o uart.o spi.o +lib = ../../libeos-soc.a + + +%.o: %.c %.h + $(CC) $(CFLAGS) -c $< + +%.o: %.S + $(CC) $(CFLAGS) -c $< + +all: $(lib) + +$(lib): $(obj) + $(AR) rcs $@ $(obj) + +clean: + rm -f *.o $(lib) diff --git a/fw/fe310/eos/i2c.c b/fw/fe310/eos/soc/i2c.c index a507af1..553a9bf 100644 --- a/fw/fe310/eos/i2c.c +++ b/fw/fe310/eos/soc/i2c.c @@ -11,22 +11,28 @@ int eos_i2c_init(uint8_t wakeup_cause) { eos_i2c_speed(EOS_I2C_SPEED); - // eos_i2c_start(); + eos_i2c_enable(); return EOS_OK; } -void eos_i2c_start(void) { +void eos_i2c_enable(void) { I2C0_REGB(I2C_CONTROL) |= I2C_CONTROL_EN; + GPIO_REG(GPIO_IOF_SEL) &= ~IOF0_I2C0_MASK; GPIO_REG(GPIO_IOF_EN) |= IOF0_I2C0_MASK; } -void eos_i2c_stop(void) { +void eos_i2c_disable(void) { GPIO_REG(GPIO_IOF_EN) &= ~IOF0_I2C0_MASK; + I2C0_REGB(I2C_CONTROL) &= ~I2C_CONTROL_EN; } +int eos_i2c_enabled(void) { + return !!(GPIO_REG(GPIO_IOF_EN) & IOF0_I2C0_MASK); +} + void eos_i2c_speed(uint32_t baud_rate) { unsigned long clock_rate = PRCI_get_cpu_freq(); uint16_t prescaler = (clock_rate / (baud_rate * 5)) - 1; diff --git a/fw/fe310/eos/i2c.h b/fw/fe310/eos/soc/i2c.h index 20d3dc7..5032988 100644 --- a/fw/fe310/eos/i2c.h +++ b/fw/fe310/eos/soc/i2c.h @@ -3,9 +3,9 @@ #define EOS_I2C_SPEED 100000 int eos_i2c_init(uint8_t wakeup_cause); -int eos_i2c_run(uint8_t wakeup_cause); -void eos_i2c_start(void); -void eos_i2c_stop(void); +void eos_i2c_enable(void); +void eos_i2c_disable(void); +int eos_i2c_enabled(void); void eos_i2c_speed(uint32_t baud_rate); int eos_i2c_read8(uint8_t addr, uint8_t reg, uint8_t *buffer, uint16_t len); int eos_i2c_read16(uint8_t addr, uint16_t reg, uint8_t *buffer, uint16_t len); diff --git a/fw/fe310/eos/i2s.c b/fw/fe310/eos/soc/i2s.c index 9cc9d9c..5e5eaa7 100644 --- a/fw/fe310/eos/i2s.c +++ b/fw/fe310/eos/soc/i2s.c @@ -25,41 +25,20 @@ #define EOS_ABUF_IDX_MASK(IDX, SIZE) ((IDX) & ((SIZE) - 1)) -EOSABuf i2s_mic_buf; -EOSABuf i2s_spk_buf; +EOSABuf _eos_i2s_mic_buf; +EOSABuf _eos_i2s_spk_buf; +uint32_t _eos_i2s_fmt = 0; +uint32_t _eos_i2s_mic_wm = 0; +uint32_t _eos_i2s_spk_wm = 0; +uint32_t _eos_i2s_mic_evt_enable = 0; +uint32_t _eos_i2s_spk_evt_enable = 0; -static eos_i2s_handler_t i2s_mic_handler = NULL; static eos_i2s_handler_t i2s_spk_handler = NULL; +static eos_i2s_handler_t i2s_mic_handler = NULL; static uint32_t i2s_clk_period; static uint8_t i2s_mic_volume = 0; /* 0 - 8 */ static uint8_t i2s_spk_volume = 16; /* 0 - 16 */ -uint32_t _eos_i2s_drvr[] = { - 0, /* I2S_MIC_BUF */ - 0, /* I2S_SPK_BUF */ - EOS_I2S_FMT_PCM16, /* I2S_FMT */ - EOS_I2S_MODE_STEREO, /* I2S_MODE */ - 0, /* I2S_MIC_WM */ - 0, /* I2S_SPK_WM */ - 0, /* I2S_MIC_EVT */ - 0, /* I2S_SPK_EVT */ - 0, /* I2S_MIC_CMP2 */ - 0, /* I2S_MIC_CMP3 */ - 0, /* I2S_SAMPLE */ -}; - -#define I2S_MIC_BUF 0 -#define I2S_SPK_BUF 1 -#define I2S_FMT 2 -#define I2S_MODE 3 -#define I2S_MIC_WM 4 -#define I2S_SPK_WM 5 -#define I2S_MIC_EVT 6 -#define I2S_SPK_EVT 7 -#define I2S_MIC_CMP2 8 -#define I2S_MIC_CMP3 9 -#define I2S_SAMPLE 10 - static void _abuf_init(EOSABuf *buf, uint8_t *array, uint16_t size) { buf->idx_r = 0; buf->idx_w = 0; @@ -105,6 +84,7 @@ static int _abuf_pop16(EOSABuf *buf, uint16_t *sample) { } } + static void _abuf_flush(EOSABuf *buf) { buf->idx_r = 0; buf->idx_w = 0; @@ -117,63 +97,42 @@ static uint16_t _abuf_len(EOSABuf *buf) { static void i2s_handle_evt(unsigned char type, unsigned char *buffer, uint16_t len) { switch(type & ~EOS_EVT_MASK) { case EOS_I2S_ETYPE_MIC: - if (i2s_mic_handler) { - i2s_mic_handler(type); - clear_csr(mstatus, MSTATUS_MIE); - _eos_i2s_drvr[I2S_MIC_EVT] = 1; - set_csr(mstatus, MSTATUS_MIE); - } + if (i2s_mic_handler) i2s_mic_handler(type); + clear_csr(mstatus, MSTATUS_MIE); + _eos_i2s_mic_evt_enable = 1; + set_csr(mstatus, MSTATUS_MIE); break; - case EOS_I2S_ETYPE_SPK: - if (i2s_spk_handler) { - i2s_spk_handler(type); - clear_csr(mstatus, MSTATUS_MIE); - _eos_i2s_drvr[I2S_SPK_EVT] = 1; - set_csr(mstatus, MSTATUS_MIE); - } + if (i2s_spk_handler) i2s_spk_handler(type); + clear_csr(mstatus, MSTATUS_MIE); + _eos_i2s_spk_evt_enable = 1; + set_csr(mstatus, MSTATUS_MIE); break; - default: eos_evtq_bad_handler(type, buffer, len); break; } } -#define PLIC_PRIORITY 0x0C000000 - -static void i2s_cmp_set(void) { - int c = 7; /* interrupt will trigger c i2s clocks before spk ws */ - int spk_ws_offset = i2s_spk_volume - 16 + i2s_mic_volume; - volatile uint32_t *p = (uint32_t *)PLIC_PRIORITY+I2S_IRQ_SD_ID; - - /* interrupt trigger - will start with left channel */ - if (spk_ws_offset - c < 0) { - I2S_REG_WS_SPK(PWM_CMP3) = i2s_clk_period * (64 + spk_ws_offset - c); - } else { - I2S_REG_WS_SPK(PWM_CMP3) = i2s_clk_period * (spk_ws_offset - c); - } - - /* disable interrupt for this cycle */ - *p = 0; +static void _mic_vol_set(uint8_t vol) { + I2S_REG_WS_MIC(PWM_CMP2) = i2s_clk_period * (vol + 1); + I2S_REG_WS_MIC(PWM_CMP3) = I2S_REG_WS_MIC(PWM_CMP2) + i2s_clk_period * 16; +} - /* empty buffers */ - // i2s_mic_buf.idx_r = i2s_mic_buf.idx_w; - // i2s_spk_buf.idx_w = i2s_spk_buf.idx_r; +static void _spk_vol_set(uint8_t vol) { + int spk_cmp = vol + i2s_mic_volume - 16; - /* adjust spk ws relative to mic ws */ - if (spk_ws_offset <= 0) { - spk_ws_offset += 32; + if (spk_cmp <= 0) { + I2S_REG_WS_SPK(PWM_CMP1) = i2s_clk_period * (32 + spk_cmp); + I2S_REG_WS_SPK(PWM_CMP2) = i2s_clk_period * (64 + spk_cmp); + I2S_REG_WS_SPK(PWM_CMP3) = i2s_clk_period * 33; GPIO_REG(GPIO_OUTPUT_XOR) &= ~(1 << I2S_PIN_WS_SPK); } else { - GPIO_REG(GPIO_OUTPUT_XOR) |= (1 << I2S_PIN_WS_SPK); + I2S_REG_WS_SPK(PWM_CMP1) = i2s_clk_period * spk_cmp; + I2S_REG_WS_SPK(PWM_CMP2) = i2s_clk_period * (32 + spk_cmp); + I2S_REG_WS_SPK(PWM_CMP3) = i2s_clk_period * (33 + spk_cmp); + GPIO_REG(GPIO_OUTPUT_XOR) |= (1 << I2S_PIN_WS_SPK); } - I2S_REG_WS_SPK(PWM_CMP1) = i2s_clk_period * spk_ws_offset; - I2S_REG_WS_SPK(PWM_CMP2) = i2s_clk_period * (32 + spk_ws_offset); - - /* mic cmp2 relative to interrupt trigger */ - _eos_i2s_drvr[I2S_MIC_CMP2] = (17 + c - i2s_spk_volume) * i2s_clk_period; /* (17 + c - i2s_spk_volume) == (1 + i2s_mic_volume) - (spk_ws_offset - c) */ - _eos_i2s_drvr[I2S_MIC_CMP3] = 16 * i2s_clk_period; } extern void _eos_i2s_start_pwm(void); @@ -181,10 +140,6 @@ extern void _eos_i2s_start_pwm(void); int eos_i2s_init(uint8_t wakeup_cause) { eos_evtq_set_handler(EOS_EVT_I2S, i2s_handle_evt); - I2S_REG_CK(PWM_CFG) = 0; - I2S_REG_WS_MIC(PWM_CFG) = 0; - I2S_REG_WS_SPK(PWM_CFG) = 0; - eos_i2s_init_mux(); GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << I2S_PIN_CK_SW); @@ -218,7 +173,7 @@ void eos_i2s_init_mux(void) { GPIO_REG(GPIO_OUTPUT_EN) &= ~(1 << I2S_PIN_SD_OUT); } -void eos_i2s_start(uint32_t sample_rate) { +void eos_i2s_start(uint32_t sample_rate, unsigned char fmt) { i2s_clk_period = ((PRCI_get_cpu_freq() / (sample_rate * 64)) & ~I2S_PWM_SCALE_CK_MASK) + 1; GPIO_REG(GPIO_INPUT_EN) |= (1 << I2S_PIN_SD_IN); @@ -234,33 +189,23 @@ void eos_i2s_start(uint32_t sample_rate) { I2S_REG_WS_MIC(PWM_CMP0) = i2s_clk_period * 64 - 1; I2S_REG_WS_MIC(PWM_CMP1) = i2s_clk_period * 32; + _mic_vol_set(i2s_mic_volume); I2S_REG_WS_SPK(PWM_CMP0) = i2s_clk_period * 64 - 1; - i2s_cmp_set(); + _spk_vol_set(i2s_spk_volume); I2S_REG_CK(PWM_COUNT) = 0; I2S_REG_WS_MIC(PWM_COUNT) = 0; I2S_REG_WS_SPK(PWM_COUNT) = i2s_clk_period / 2; - if (i2s_mic_buf.array && i2s_mic_buf.size) { - _eos_i2s_drvr[I2S_MIC_BUF] = (uint32_t)&i2s_mic_buf; - if (_eos_i2s_drvr[I2S_MIC_WM] == 0) { - _eos_i2s_drvr[I2S_MIC_WM] = i2s_mic_buf.size / 2; - } - } - if (i2s_spk_buf.array && i2s_spk_buf.size) { - _eos_i2s_drvr[I2S_SPK_BUF] = (uint32_t)&i2s_spk_buf; - if (_eos_i2s_drvr[I2S_SPK_WM] == 0) { - _eos_i2s_drvr[I2S_SPK_WM] = i2s_spk_buf.size / 2; - } - } - if (i2s_mic_handler) _eos_i2s_drvr[I2S_MIC_EVT] = 1; - if (i2s_spk_handler) _eos_i2s_drvr[I2S_SPK_EVT] = 1; + _eos_i2s_fmt = fmt; + _eos_i2s_mic_evt_enable = 1; + _eos_i2s_spk_evt_enable = 1; - eos_intr_set_priority(I2S_IRQ_SD_ID, IRQ_PRIORITY_I2S_SD); eos_intr_set_priority(I2S_IRQ_WS_ID, IRQ_PRIORITY_I2S_WS); - eos_intr_enable(I2S_IRQ_SD_ID); + eos_intr_set_priority(I2S_IRQ_SD_ID, 0); eos_intr_enable(I2S_IRQ_WS_ID); + eos_intr_enable(I2S_IRQ_SD_ID); _eos_i2s_start_pwm(); /* @@ -269,13 +214,9 @@ void eos_i2s_start(uint32_t sample_rate) { I2S_REG_WS_SPK(PWM_CFG) = PWM_CFG_ENALWAYS | PWM_CFG_ZEROCMP | PWM_CFG_CMP1GANG; */ - GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << I2S_PIN_WS_MIC); - GPIO_REG(GPIO_INPUT_EN) &= ~(1 << I2S_PIN_WS_MIC); - GPIO_REG(GPIO_OUTPUT_EN) |= (1 << I2S_PIN_WS_MIC); - - GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << I2S_PIN_CK_SR); - GPIO_REG(GPIO_IOF_SEL) |= I2S_PIN_PWM; - GPIO_REG(GPIO_IOF_EN) |= I2S_PIN_PWM; + GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << I2S_PIN_CK_SR); + GPIO_REG(GPIO_IOF_SEL) |= I2S_PIN_PWM; + GPIO_REG(GPIO_IOF_EN) |= I2S_PIN_PWM; } void eos_i2s_stop(void) { @@ -286,14 +227,10 @@ void eos_i2s_stop(void) { I2S_REG_WS_MIC(PWM_COUNT) = 0; I2S_REG_WS_SPK(PWM_COUNT) = 0; - _eos_i2s_drvr[I2S_MIC_BUF] = 0; - _eos_i2s_drvr[I2S_MIC_EVT] = 0; - _eos_i2s_drvr[I2S_MIC_WM] = 0; - - _eos_i2s_drvr[I2S_SPK_BUF] = 0; - _eos_i2s_drvr[I2S_SPK_EVT] = 0; - _eos_i2s_drvr[I2S_SPK_WM] = 0; - + _eos_i2s_mic_evt_enable = 0; + _eos_i2s_spk_evt_enable = 0; + eos_intr_set_priority(I2S_IRQ_WS_ID, 0); + eos_intr_set_priority(I2S_IRQ_SD_ID, 0); eos_intr_disable(I2S_IRQ_WS_ID); eos_intr_disable(I2S_IRQ_SD_ID); @@ -302,26 +239,15 @@ void eos_i2s_stop(void) { GPIO_REG(GPIO_OUTPUT_XOR) &= ~(1 << I2S_PIN_WS_SPK); GPIO_REG(GPIO_IOF_EN) &= ~I2S_PIN_PWM; - - eos_i2s_mic_set_wm(0); - eos_i2s_spk_set_wm(0); } int eos_i2s_running(void) { - return !!(GPIO_REG(GPIO_IOF_EN) & (1 << I2S_PIN_CK)); -} - -void eos_i2s_set_fmt(unsigned char fmt) { - _eos_i2s_drvr[I2S_FMT] = fmt; -} - -void eos_i2s_set_mode(unsigned char mode) { - _eos_i2s_drvr[I2S_MODE] = mode; + return !!(GPIO_REG(GPIO_IOF_EN) & I2S_PIN_PWM); } void eos_i2s_mic_init(uint8_t *mic_arr, uint16_t mic_arr_size) { clear_csr(mstatus, MSTATUS_MIE); - _abuf_init(&i2s_mic_buf, mic_arr, mic_arr_size); + _abuf_init(&_eos_i2s_mic_buf, mic_arr, mic_arr_size); set_csr(mstatus, MSTATUS_MIE); } @@ -333,14 +259,14 @@ void eos_i2s_mic_set_handler(eos_i2s_handler_t wm_handler) { void eos_i2s_mic_set_wm(uint16_t wm) { clear_csr(mstatus, MSTATUS_MIE); - _eos_i2s_drvr[I2S_MIC_WM] = wm; + _eos_i2s_mic_wm = wm; set_csr(mstatus, MSTATUS_MIE); } uint16_t eos_i2s_mic_len(void) { clear_csr(mstatus, MSTATUS_MIE); - uint16_t ret = _abuf_len(&i2s_mic_buf); + uint16_t ret = _abuf_len(&_eos_i2s_mic_buf); set_csr(mstatus, MSTATUS_MIE); return ret; } @@ -350,37 +276,31 @@ uint16_t eos_i2s_mic_read(uint8_t *sample, uint16_t ssize) { uint16_t _ssize = 0; clear_csr(mstatus, MSTATUS_MIE); - _ssize = MIN(ssize, _abuf_len(&i2s_mic_buf)); + _ssize = MIN(ssize, _abuf_len(&_eos_i2s_mic_buf)); set_csr(mstatus, MSTATUS_MIE); for (i=0; i<_ssize; i++) { - sample[i] = i2s_mic_buf.array[EOS_ABUF_IDX_MASK(i2s_mic_buf.idx_r + i, i2s_mic_buf.size)]; + sample[i] = _eos_i2s_mic_buf.array[EOS_ABUF_IDX_MASK(_eos_i2s_mic_buf.idx_r + i, _eos_i2s_mic_buf.size)]; } clear_csr(mstatus, MSTATUS_MIE); - i2s_mic_buf.idx_r += _ssize; + _eos_i2s_mic_buf.idx_r += _ssize; set_csr(mstatus, MSTATUS_MIE); return _ssize; } int eos_i2s_mic_pop8(uint8_t *sample) { - int ret; - clear_csr(mstatus, MSTATUS_MIE); - ret = _abuf_pop8(&i2s_mic_buf, sample); + int ret = _abuf_pop8(&_eos_i2s_mic_buf, sample); set_csr(mstatus, MSTATUS_MIE); - return ret; } int eos_i2s_mic_pop16(uint16_t *sample) { - int ret; - clear_csr(mstatus, MSTATUS_MIE); - ret = _abuf_pop16(&i2s_mic_buf, sample); + int ret = _abuf_pop16(&_eos_i2s_mic_buf, sample); set_csr(mstatus, MSTATUS_MIE); - return ret; } @@ -394,13 +314,14 @@ void eos_i2s_mic_vol_set(int vol) { i2s_mic_volume = vol; clear_csr(mstatus, MSTATUS_MIE); - i2s_cmp_set(); + _mic_vol_set(vol); + _spk_vol_set(i2s_spk_volume); set_csr(mstatus, MSTATUS_MIE); } void eos_i2s_spk_init(uint8_t *spk_arr, uint16_t spk_arr_size) { clear_csr(mstatus, MSTATUS_MIE); - _abuf_init(&i2s_spk_buf, spk_arr, spk_arr_size); + _abuf_init(&_eos_i2s_spk_buf, spk_arr, spk_arr_size); set_csr(mstatus, MSTATUS_MIE); } @@ -412,13 +333,13 @@ void eos_i2s_spk_set_handler(eos_i2s_handler_t wm_handler) { void eos_i2s_spk_set_wm(uint16_t wm) { clear_csr(mstatus, MSTATUS_MIE); - _eos_i2s_drvr[I2S_SPK_WM] = wm; + _eos_i2s_spk_wm = wm; set_csr(mstatus, MSTATUS_MIE); } uint16_t eos_i2s_spk_len(void) { clear_csr(mstatus, MSTATUS_MIE); - uint16_t ret = _abuf_len(&i2s_spk_buf); + uint16_t ret = _abuf_len(&_eos_i2s_spk_buf); set_csr(mstatus, MSTATUS_MIE); return ret; } @@ -428,37 +349,31 @@ uint16_t eos_i2s_spk_write(uint8_t *sample, uint16_t ssize) { uint16_t _ssize = 0; clear_csr(mstatus, MSTATUS_MIE); - _ssize = MIN(ssize, i2s_spk_buf.size - _abuf_len(&i2s_spk_buf)); + _ssize = MIN(ssize, _eos_i2s_spk_buf.size - _abuf_len(&_eos_i2s_spk_buf)); set_csr(mstatus, MSTATUS_MIE); for (i=0; i<_ssize; i++) { - i2s_spk_buf.array[EOS_ABUF_IDX_MASK(i2s_spk_buf.idx_w + i, i2s_spk_buf.size)] = sample[i]; + _eos_i2s_spk_buf.array[EOS_ABUF_IDX_MASK(_eos_i2s_spk_buf.idx_w + i, _eos_i2s_spk_buf.size)] = sample[i]; } clear_csr(mstatus, MSTATUS_MIE); - i2s_spk_buf.idx_w += _ssize; + _eos_i2s_spk_buf.idx_w += _ssize; set_csr(mstatus, MSTATUS_MIE); return _ssize; } int eos_i2s_spk_push8(uint8_t sample) { - int ret; - clear_csr(mstatus, MSTATUS_MIE); - ret = _abuf_push8(&i2s_spk_buf, sample); + int ret = _abuf_push8(&_eos_i2s_spk_buf, sample); set_csr(mstatus, MSTATUS_MIE); - return ret; } int eos_i2s_spk_push16(uint16_t sample) { - int ret; - clear_csr(mstatus, MSTATUS_MIE); - ret = _abuf_push16(&i2s_spk_buf, sample); + int ret = _abuf_push16(&_eos_i2s_spk_buf, sample); set_csr(mstatus, MSTATUS_MIE); - return ret; } @@ -472,6 +387,6 @@ void eos_i2s_spk_vol_set(int vol) { i2s_spk_volume = vol; clear_csr(mstatus, MSTATUS_MIE); - i2s_cmp_set(); + _spk_vol_set(vol); set_csr(mstatus, MSTATUS_MIE); } diff --git a/fw/fe310/eos/i2s.h b/fw/fe310/eos/soc/i2s.h index 81b4ade..f53e183 100644 --- a/fw/fe310/eos/i2s.h +++ b/fw/fe310/eos/soc/i2s.h @@ -13,11 +13,9 @@ typedef void (*eos_i2s_handler_t) (unsigned char); int eos_i2s_init(uint8_t wakeup_cause); void eos_i2s_init_mux(void); -void eos_i2s_start(uint32_t sample_rate); +void eos_i2s_start(uint32_t sample_rate, unsigned char fmt); void eos_i2s_stop(void); int eos_i2s_running(void); -void eos_i2s_set_fmt(unsigned char fmt); -void eos_i2s_set_mode(unsigned char mode); void eos_i2s_mic_init(uint8_t *mic_arr, uint16_t mic_arr_size); void eos_i2s_mic_set_handler(eos_i2s_handler_t wm_handler); void eos_i2s_mic_set_wm(uint16_t wm); diff --git a/fw/fe310/eos/i2s_def.h b/fw/fe310/eos/soc/i2s_def.h index 3529be1..3529be1 100644 --- a/fw/fe310/eos/i2s_def.h +++ b/fw/fe310/eos/soc/i2s_def.h diff --git a/fw/fe310/eos/i2s_priv.h b/fw/fe310/eos/soc/i2s_priv.h index 25237c3..25237c3 100644 --- a/fw/fe310/eos/i2s_priv.h +++ b/fw/fe310/eos/soc/i2s_priv.h diff --git a/fw/fe310/eos/interrupt.c b/fw/fe310/eos/soc/interrupt.c index dab6fab..dab6fab 100644 --- a/fw/fe310/eos/interrupt.c +++ b/fw/fe310/eos/soc/interrupt.c diff --git a/fw/fe310/eos/interrupt.h b/fw/fe310/eos/soc/interrupt.h index a239934..c6252b5 100644 --- a/fw/fe310/eos/interrupt.h +++ b/fw/fe310/eos/soc/interrupt.h @@ -1,6 +1,6 @@ #include <stdint.h> -#include "irq_def.h" +#include "../irq_def.h" typedef void (*eos_intr_handler_t) (void); @@ -10,4 +10,4 @@ void eos_intr_set_handler(uint8_t int_num, eos_intr_handler_t handler); void eos_intr_set_priority(uint8_t int_num, uint8_t priority); void eos_intr_enable(uint8_t int_num); void eos_intr_disable(uint8_t int_num); -void eos_intr_mask(uint8_t priority);
\ No newline at end of file +void eos_intr_mask(uint8_t priority); diff --git a/fw/fe310/eos/soc/pwr.c b/fw/fe310/eos/soc/pwr.c new file mode 100644 index 0000000..a2adfd4 --- /dev/null +++ b/fw/fe310/eos/soc/pwr.c @@ -0,0 +1,70 @@ +#include <stdlib.h> +#include <stdint.h> + +#include "encoding.h" +#include "platform.h" + +#include "eos.h" +#include "timer.h" +#include "dev/net.h" + +#include "pwr.h" + +#define PWR_RTC_SCALE 15 +#define PWR_RTC_SFREQ (EOS_TIMER_RTC_FREQ >> PWR_RTC_SCALE) + +int eos_pwr_init(uint8_t wakeup_cause) { + AON_REG(AON_PMUKEY) = 0x51F15E; + AON_REG(AON_PMUIE) = 0x5; + + AON_REG(AON_RTCCMP) = 0xFFFFFFFF; + AON_REG(AON_RTCCFG) = PWR_RTC_SCALE; + AON_REG(AON_RTCHI) = 0; + AON_REG(AON_RTCLO) = 0; + + return EOS_OK; +} + +uint8_t eos_pwr_wakeup_cause(void) { + return AON_REG(AON_PMUCAUSE) & 0xff; +} + +uint8_t eos_pwr_reset_cause(void) { + return (AON_REG(AON_PMUCAUSE) >> 8) & 0xff; +} + +int eos_pwr_sleep(void) { + int rv; + + rv = eos_net_sleep(1000); + if (rv) return rv; + + AON_REG(AON_PMUKEY) = 0x51F15E; + AON_REG(AON_PMUSLEEP) = 1; + + return EOS_OK; +} + +void eos_pwr_wake_at(uint32_t msec) { + uint32_t pmuie; + + AON_REG(AON_RTCCFG) |= AON_RTCCFG_ENALWAYS; + AON_REG(AON_RTCCMP) = msec * PWR_RTC_SFREQ / 1000; + + pmuie = AON_REG(AON_PMUIE) | 0x2; + AON_REG(AON_PMUKEY) = 0x51F15E; + AON_REG(AON_PMUIE) = pmuie; +} + +void eos_pwr_wake_disable(void) { + uint32_t pmuie; + + AON_REG(AON_RTCCMP) = 0xFFFFFFFF; + AON_REG(AON_RTCCFG) &= ~AON_RTCCFG_ENALWAYS; + AON_REG(AON_RTCHI) = 0; + AON_REG(AON_RTCLO) = 0; + + pmuie = AON_REG(AON_PMUIE) & ~0x2; + AON_REG(AON_PMUKEY) = 0x51F15E; + AON_REG(AON_PMUIE) = pmuie; +} diff --git a/fw/fe310/eos/pwr.h b/fw/fe310/eos/soc/pwr.h index 264436b..1a0d17a 100644 --- a/fw/fe310/eos/pwr.h +++ b/fw/fe310/eos/soc/pwr.h @@ -1,9 +1,4 @@ #include <stdint.h> -#include "event.h" - -#define EOS_PWR_MTYPE_BUTTON 1 - -#define EOS_PWR_MAX_MTYPE 2 #define EOS_PWR_WAKE_RST 0 #define EOS_PWR_WAKE_RTC 1 @@ -19,7 +14,3 @@ uint8_t eos_pwr_reset_cause(void); int eos_pwr_sleep(void); void eos_pwr_wake_at(uint32_t msec); void eos_pwr_wake_disable(void); - -void eos_pwr_netinit(void); -void eos_pwr_set_handler(unsigned char mtype, eos_evt_handler_t handler); -eos_evt_handler_t eos_pwr_get_handler(unsigned char mtype);
\ No newline at end of file diff --git a/fw/fe310/eos/spi.c b/fw/fe310/eos/soc/spi.c index 05c9448..2c36109 100644 --- a/fw/fe310/eos/spi.c +++ b/fw/fe310/eos/soc/spi.c @@ -21,6 +21,8 @@ #define SPI_FLAG_XCHG 0x10 +#define SPI_CSID_NONE 1 + #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) #define MAX(X, Y) (((X) > (Y)) ? (X) : (Y)) @@ -52,19 +54,7 @@ int eos_spi_init(uint8_t wakeup_cause) { evt_handler[i] = eos_evtq_bad_handler; } eos_evtq_set_handler(EOS_EVT_SPI, spi_handle_evt); - eos_intr_set(INT_SPI1_BASE, IRQ_PRIORITY_SPI_XCHG, NULL); - - GPIO_REG(GPIO_OUTPUT_VAL) &= ~(1 << IOF_SPI1_SCK); - GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << IOF_SPI1_MOSI); - - GPIO_REG(GPIO_INPUT_EN) &= ~(1 << IOF_SPI1_SCK); - GPIO_REG(GPIO_OUTPUT_EN) |= (1 << IOF_SPI1_SCK); - - GPIO_REG(GPIO_INPUT_EN) &= ~(1 << IOF_SPI1_MOSI); - GPIO_REG(GPIO_OUTPUT_EN) |= (1 << IOF_SPI1_MOSI); - - GPIO_REG(GPIO_INPUT_EN) |= (1 << IOF_SPI1_MISO); - GPIO_REG(GPIO_OUTPUT_EN) &= ~(1 << IOF_SPI1_MISO); + eos_intr_set_priority(INT_SPI1_BASE, IRQ_PRIORITY_SPI_XCHG); SPI1_REG(SPI_REG_SCKMODE) = SPI_MODE0; SPI1_REG(SPI_REG_FMT) = SPI_FMT_PROTO(SPI_PROTO_S) | @@ -72,25 +62,34 @@ int eos_spi_init(uint8_t wakeup_cause) { SPI_FMT_DIR(SPI_DIR_RX) | SPI_FMT_LEN(8); - GPIO_REG(GPIO_IOF_SEL) &= ~SPI_IOF_MASK; - GPIO_REG(GPIO_IOF_EN) |= SPI_IOF_MASK; + /* for spi 9bit protocol */ + GPIO_REG(GPIO_OUTPUT_EN) |= (1 << IOF_SPI1_SCK); + GPIO_REG(GPIO_OUTPUT_EN) |= (1 << IOF_SPI1_MOSI); + GPIO_REG(GPIO_INPUT_EN) |= (1 << IOF_SPI1_MISO); + + eos_spi_enable(); // There is no way here to change the CS polarity. // SPI1_REG(SPI_REG_CSDEF) = 0xFFFF; return EOS_OK; } -void eos_spi_start(uint16_t div, uint8_t csid, uint8_t cspin, unsigned char evt) { +void eos_spi_configure(uint16_t div, int8_t csid, int8_t cspin, unsigned char evt) { spi_state_flags = 0; spi_evt = evt; SPI1_REG(SPI_REG_SCKDIV) = div; - SPI1_REG(SPI_REG_CSID) = csid; - if (csid != SPI_CSID_NONE) { + if (csid != -1) { + SPI1_REG(SPI_REG_CSID) = csid; SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_AUTO; } else { spi_cspin = cspin; + SPI1_REG(SPI_REG_CSID) = SPI_CSID_NONE; SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_OFF; } +} + +void eos_spi_start(uint16_t div, int8_t csid, int8_t cspin, unsigned char evt) { + eos_spi_configure(div, csid, cspin, evt); eos_intr_set_handler(INT_SPI1_BASE, eos_spi_handle_xchg); } @@ -100,6 +99,19 @@ void eos_spi_stop(void) { spi_evt = 0; } +void eos_spi_enable(void) { + eos_intr_enable(INT_SPI1_BASE); + + GPIO_REG(GPIO_IOF_SEL) &= ~SPI_IOF_MASK; + GPIO_REG(GPIO_IOF_EN) |= SPI_IOF_MASK; +} + +void eos_spi_disable(void) { + GPIO_REG(GPIO_IOF_EN) &= ~SPI_IOF_MASK; + + eos_intr_disable(INT_SPI1_BASE); +} + void eos_spi_set_handler(unsigned char evt, eos_evt_handler_t handler) { if (handler == NULL) handler = eos_evtq_bad_handler; if (evt && (evt <= EOS_SPI_MAX_EVT)) evt_handler[evt - 1] = handler; diff --git a/fw/fe310/eos/spi.h b/fw/fe310/eos/soc/spi.h index a23a235..0c2de4b 100644 --- a/fw/fe310/eos/spi.h +++ b/fw/fe310/eos/soc/spi.h @@ -1,5 +1,5 @@ #include <stdint.h> -#include "event.h" +#include "../event.h" #define EOS_SPI_FLAG_TX 0x01 #define EOS_SPI_FLAG_MORE 0x02 @@ -11,8 +11,12 @@ #define EOS_SPI_MAX_EVT 2 int eos_spi_init(uint8_t wakeup_cause); -void eos_spi_start(uint16_t div, uint8_t csid, uint8_t cspin, unsigned char evt); +void eos_spi_configure(uint16_t div, int8_t csid, int8_t cspin, unsigned char evt); +void eos_spi_start(uint16_t div, int8_t csid, int8_t cspin, unsigned char evt); void eos_spi_stop(void); +void eos_spi_enable(void); +void eos_spi_disable(void); + void eos_spi_set_handler(unsigned char evt, eos_evt_handler_t handler); void _eos_spi_xchg_init(unsigned char *buffer, uint16_t len, uint8_t flags); diff --git a/fw/fe310/eos/spi_priv.h b/fw/fe310/eos/soc/spi_priv.h index 72c2dae..17081a3 100644 --- a/fw/fe310/eos/spi_priv.h +++ b/fw/fe310/eos/soc/spi_priv.h @@ -1,8 +1,5 @@ #include <stdint.h> -#define SPI_CSID_NONE 1 -#define SPI_CSPIN_NONE 0xff - /* DO NOT TOUCH THEESE */ #define SPI_SIZE_CHUNK 4 #define SPI_SIZE_WM 2 diff --git a/fw/fe310/eos/timer.c b/fw/fe310/eos/soc/timer.c index 5ae58da..91861a3 100644 --- a/fw/fe310/eos/timer.c +++ b/fw/fe310/eos/soc/timer.c @@ -32,7 +32,7 @@ void _eos_timer_handle(void) { uint64_t now = *mtime; uint64_t next = 0; - for (i = 0; i <= EOS_TIMER_MAX_ETYPE; i++) { + for (i=0; i<=EOS_TIMER_MAX_ETYPE; i++) { if (timer_next[i] && (timer_next[i] <= now)) { timer_next[i] = 0; if (i == 0) { @@ -88,7 +88,7 @@ uint32_t eos_timer_get(unsigned char evt) { return ret; } -void eos_timer_set(uint32_t msec, unsigned char evt) { +void eos_timer_set(unsigned char evt, uint32_t msec) { int i; volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME); uint64_t *mtimecmp = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIMECMP); @@ -97,7 +97,7 @@ void eos_timer_set(uint32_t msec, unsigned char evt) { if (*mtimecmp != 0) clear_csr(mie, MIP_MTIP); timer_next[evt] = tick; - for (i = 0; i <= EOS_TIMER_MAX_ETYPE; i++) { + for (i=0; i<=EOS_TIMER_MAX_ETYPE; i++) { next = next && timer_next[i] ? MIN(next, timer_next[i]) : (next ? next : timer_next[i]); } *mtimecmp = next; @@ -112,7 +112,7 @@ void eos_timer_clear(unsigned char evt) { if (*mtimecmp != 0) clear_csr(mie, MIP_MTIP); if (timer_next[evt]) { timer_next[evt] = 0; - for (i = 0; i <= EOS_TIMER_MAX_ETYPE; i++) { + for (i=0; i<=EOS_TIMER_MAX_ETYPE; i++) { next = next && timer_next[i] ? MIN(next, timer_next[i]) : (next ? next : timer_next[i]); } *mtimecmp = next; @@ -120,12 +120,11 @@ void eos_timer_clear(unsigned char evt) { if (*mtimecmp != 0) set_csr(mie, MIP_MTIP); } - void eos_time_sleep(uint32_t msec) { volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME); - uint64_t now_ms = *mtime * 1000 / EOS_TIMER_RTC_FREQ; + uint64_t mtime0 = *mtime; - while (*mtime * 1000 / EOS_TIMER_RTC_FREQ < now_ms + msec); + while ((*mtime - mtime0) < (msec * EOS_TIMER_RTC_FREQ / 1000 + 1)); } uint64_t eos_time_get_tick(void) { @@ -133,6 +132,6 @@ uint64_t eos_time_get_tick(void) { return *mtime; } -uint32_t eos_time_since(uint32_t start) { - return (eos_time_get_tick() - start) * 1000 / EOS_TIMER_RTC_FREQ; - } +uint32_t eos_time_delta_ms(uint32_t tick) { + return ((uint32_t)eos_time_get_tick() - tick) * 1000 / EOS_TIMER_RTC_FREQ; +} diff --git a/fw/fe310/eos/timer.h b/fw/fe310/eos/soc/timer.h index e479483..0309454 100644 --- a/fw/fe310/eos/timer.h +++ b/fw/fe310/eos/soc/timer.h @@ -6,7 +6,7 @@ #define EOS_TIMER_MAX_ETYPE 4 -#define EOS_TIMER_NONE 0xffffffff +#define EOS_TIMER_NONE -1 #define EOS_TIMER_RTC_FREQ 32768 typedef void (*eos_timer_handler_t) (unsigned char); @@ -15,9 +15,9 @@ int eos_timer_init(uint8_t wakeup_cause); void eos_timer_set_handler(unsigned char evt, eos_timer_handler_t handler); uint32_t eos_timer_get(unsigned char evt); -void eos_timer_set(uint32_t msec, unsigned char evt); +void eos_timer_set(unsigned char evt, uint32_t msec); void eos_timer_clear(unsigned char evt); void eos_time_sleep(uint32_t msec); uint64_t eos_time_get_tick(void); -uint32_t eos_time_since(uint32_t start); +uint32_t eos_time_delta_ms(uint32_t tick); diff --git a/fw/fe310/eos/trap_entry.S b/fw/fe310/eos/soc/trap_entry.S index fb2b121..96024cb 100644 --- a/fw/fe310/eos/trap_entry.S +++ b/fw/fe310/eos/soc/trap_entry.S @@ -25,18 +25,6 @@ #define INT_PWM1_BASE 44 #define INT_PWM2_BASE 48 -#define I2S_MIC_BUF (0*4) -#define I2S_SPK_BUF (1*4) -#define I2S_FMT (2*4) -#define I2S_MODE (3*4) -#define I2S_MIC_WM (4*4) -#define I2S_SPK_WM (5*4) -#define I2S_MIC_EVT (6*4) -#define I2S_SPK_EVT (7*4) -#define I2S_MIC_CMP2 (8*4) -#define I2S_MIC_CMP3 (9*4) -#define I2S_SAMPLE (10*4) - #include "board.h" #include "irq_def.h" #include "evt_def.h" @@ -49,7 +37,7 @@ .global eos_trap_entry eos_trap_entry: - addi sp, sp, -12*REGBYTES + addi sp, sp, -8*REGBYTES STORE x8, 0*REGBYTES(sp) STORE x9, 1*REGBYTES(sp) STORE x18, 2*REGBYTES(sp) @@ -58,10 +46,6 @@ eos_trap_entry: STORE x21, 5*REGBYTES(sp) STORE x22, 6*REGBYTES(sp) STORE x23, 7*REGBYTES(sp) - STORE x24, 8*REGBYTES(sp) # format: 0 - PCM16; 1 - ALAW - STORE x25, 9*REGBYTES(sp) # mode: 0 - stereo; 1 - mono - STORE x26, 10*REGBYTES(sp) # channel: 0 - left; 1 - right - STORE x27, 11*REGBYTES(sp) # _eos_event_q addr csrr x8, mcause li x18, MCAUSE_EXT @@ -100,68 +84,34 @@ evtq_push: jalr x0, x21 i2s_handle_sd: - li x8, I2S_CTRL_ADDR_WS_SPK - lw x18, PWM_COUNT(x8) - lw x19, PWM_CMP3(x8) - # exit if too early - bltu x18, x19, i2s_sd_exit - - la x27, _eos_i2s_drvr - - # move CMPs for next channel and store channel bit to x26 - lw x20, I2S_MIC_CMP2(x27) - lw x21, I2S_MIC_CMP3(x27) # 16-bit period - - add x23, x19, x20 - add x24, x23, x21 - slli x20, x21, 1 # 32-bit period - slli x21, x20, 1 # 64-bit period - bltu x24, x21, 0f - neg x21, x21 - add x23, x23, x21 - add x24, x24, x21 -0: - li x26, 0 - bltu x23, x20, 0f - li x26, 1 -0: - bltu x19, x20, 0f - neg x20, x20 + li x18, I2S_CTRL_ADDR_WS_SPK + lw x8, PWM_COUNT(x18) + lw x9, PWM_CMP3(x18) + bltu x8, x9, i2s_handle_sd_exit + + # disable sd irq li x18, PLIC_PRIORITY sw x0, 4*I2S_IRQ_SD_ID(x18) -0: - add x19, x19, x20 - li x9, I2S_CTRL_ADDR_WS_MIC - sw x19, PWM_CMP3(x8) - sw x23, PWM_CMP2(x9) - sw x24, PWM_CMP3(x9) - - lw x24, I2S_FMT(x27) - lw x25, I2S_MODE(x27) + la x9, _eos_i2s_fmt + lw x23, 0(x9) i2s_abuf_pop: - and x8, x25, x26 - beqz x8, 0f - - lw x8, I2S_SAMPLE(x27) - j i2s_sd_xchg -0: # pop from spk buf -> x8 - lw x9, I2S_SPK_BUF(x27) - beqz x9, i2s_sd_xchg + mv x8, x0 + la x9, _eos_i2s_spk_buf lhu x18, I2S_ABUF_OFF_IDXR(x9) lhu x19, I2S_ABUF_OFF_IDXW(x9) lhu x20, I2S_ABUF_OFF_SIZE(x9) - beq x18, x19, 2f + beq x18, x19, i2s_handle_sd_xchg addi x20, x20, -1 and x20, x20, x18 lw x21, I2S_ABUF_OFF_ARRAY(x9) add x21, x21, x20 - beqz x24, 0f + beqz x23, 0f lbu x8, 0(x21) addi x18, x18, 1 j 1f @@ -174,18 +124,20 @@ i2s_abuf_pop: 1: sh x18, I2S_ABUF_OFF_IDXR(x9) -2: li x21, 0xffff sub x18, x19, x18 and x18, x18, x21 # check for push to event queue - lw x9, I2S_SPK_WM(x27) - bgtu x18, x9, i2s_decode + la x9, _eos_i2s_spk_wm + lw x20, 0(x9) + beqz x20, i2s_decode + bgtu x18, x20, i2s_decode - lw x9, I2S_SPK_EVT(x27) - beqz x9, i2s_decode - sw x0, I2S_SPK_EVT(x27) + la x9, _eos_i2s_spk_evt_enable + lw x18, 0(x9) + beqz x18, i2s_decode + sw x0, 0(x9) # push to event queue jal x22, evtq_push @@ -194,7 +146,7 @@ i2s_abuf_pop: sb x18, MSGQ_ITEM_OFF_TYPE(x21) i2s_decode: - beqz x24, 3f + beqz x23, i2s_handle_sd_xchg # aLaw decode -> x8 xori x8, x8, 0x55 andi x9, x8, 0x80 @@ -229,13 +181,10 @@ i2s_decode: slli x8, x8, 1 ori x8, x8, 1 2: - beqz x9, 3f + beqz x9, i2s_handle_sd_xchg mul x8, x8, x9 -3: - beqz x25, i2s_sd_xchg - sw x8, I2S_SAMPLE(x27) -i2s_sd_xchg: +i2s_handle_sd_xchg: # read/write shift reg: x8 -> sr -> x8 li x18, GPIO_CTRL_ADDR li x19, (0x1 << I2S_PIN_SD_IN) @@ -285,15 +234,17 @@ i2s_sd_xchg: xor x22, x22, x21 sw x22, GPIO_OUTPUT_VAL(x18) - addi x23, x23, -1 - bnez x23, 0b - # idle li x9, I2S_IDLE_CYCLES 1: addi x9, x9, -1 bnez x9, 1b + addi x23, x23, -1 + beqz x23, 2f + j 0b + +2: # 74HC595 ck low (I2S_PIN_CK_SR high) xor x22, x22, x21 sw x22, GPIO_OUTPUT_VAL(x18) @@ -305,8 +256,11 @@ i2s_sd_xchg: slli x8, x8, 16 srai x8, x8, 16 + la x9, _eos_i2s_fmt + lw x23, 0(x9) + i2s_encode: - beqz x24, i2s_abuf_push + beqz x23, i2s_abuf_push # aLaw encode -> x8 li x18, 0x800 li x19, 7 @@ -338,12 +292,8 @@ i2s_encode: andi x8, x8, 0xff i2s_abuf_push: - # check channel - # bnez x26, i2s_sd_exit - # push to mic buf - lw x9, I2S_MIC_BUF(x27) - beqz x9, i2s_sd_exit + la x9, _eos_i2s_mic_buf lhu x18, I2S_ABUF_OFF_IDXR(x9) lhu x19, I2S_ABUF_OFF_IDXW(x9) lhu x20, I2S_ABUF_OFF_SIZE(x9) @@ -351,13 +301,13 @@ i2s_abuf_push: sub x18, x19, x18 and x18, x18, x21 - beq x18, x20, 2f + beq x18, x20, i2s_handle_sd_exit addi x20, x20, -1 and x20, x20, x19 lw x21, I2S_ABUF_OFF_ARRAY(x9) add x21, x21, x20 - beqz x24, 0f + beqz x23, 0f sb x8, 0(x21) addi x19, x19, 1 addi x18, x18, 1 @@ -371,22 +321,24 @@ i2s_abuf_push: 1: sh x19, I2S_ABUF_OFF_IDXW(x9) -2: # check for push to event queue - lw x9, I2S_MIC_WM(x27) - bltu x18, x9, i2s_sd_exit + la x9, _eos_i2s_mic_wm + lw x20, 0(x9) + beqz x20, i2s_handle_sd_exit + bltu x18, x20, i2s_handle_sd_exit - lw x9, I2S_MIC_EVT(x27) - beqz x9, i2s_sd_exit - sw x0, I2S_MIC_EVT(x27) + la x9, _eos_i2s_mic_evt_enable + lw x18, 0(x9) + beqz x18, i2s_handle_sd_exit + sw x0, 0(x9) # push to event queue jal x22, evtq_push - beqz x21, i2s_sd_exit + beqz x21, i2s_handle_sd_exit li x18, (EOS_EVT_I2S | EOS_I2S_ETYPE_MIC) sb x18, MSGQ_ITEM_OFF_TYPE(x21) -i2s_sd_exit: +i2s_handle_sd_exit: # complete li x18, I2S_IRQ_SD_ID li x19, PLIC_CLAIM @@ -443,6 +395,16 @@ _eos_i2s_start_pwm: ret +.global _eos_flash_set +_eos_flash_set: + li a3, SPI0_CTRL_ADDR + sw x0, SPI_REG_FCTRL(a3) + sw a0, SPI_REG_SCKDIV(a3) + sw a1, SPI_REG_FFMT(a3) + li a0, 1 + sw a0, SPI_REG_FCTRL(a3) + ret + trap_exit_data: # Remain in M-mode after mret li x18, MSTATUS_MPP @@ -456,11 +418,7 @@ trap_exit_data: LOAD x21, 5*REGBYTES(sp) LOAD x22, 6*REGBYTES(sp) LOAD x23, 7*REGBYTES(sp) - LOAD x24, 8*REGBYTES(sp) - LOAD x25, 9*REGBYTES(sp) - LOAD x26, 10*REGBYTES(sp) - LOAD x27, 11*REGBYTES(sp) - addi sp, sp, 12*REGBYTES + addi sp, sp, 8*REGBYTES mret @@ -473,7 +431,7 @@ handle_intr: .align 4 trap_entry_text: - addi sp, sp, -20*REGBYTES + addi sp, sp, -24*REGBYTES STORE x1, 0*REGBYTES(sp) STORE x2, 1*REGBYTES(sp) @@ -490,10 +448,14 @@ trap_entry_text: STORE x15, 12*REGBYTES(sp) STORE x16, 13*REGBYTES(sp) STORE x17, 14*REGBYTES(sp) - STORE x28, 15*REGBYTES(sp) - STORE x29, 16*REGBYTES(sp) - STORE x30, 17*REGBYTES(sp) - STORE x31, 18*REGBYTES(sp) + STORE x24, 15*REGBYTES(sp) + STORE x25, 16*REGBYTES(sp) + STORE x26, 17*REGBYTES(sp) + STORE x27, 18*REGBYTES(sp) + STORE x28, 19*REGBYTES(sp) + STORE x29, 20*REGBYTES(sp) + STORE x30, 21*REGBYTES(sp) + STORE x31, 22*REGBYTES(sp) li x18, MCAUSE_TIMER beq x8, x18, handle_timer @@ -511,6 +473,7 @@ handle_ext: call eos_intr_handle li x18, PLIC_CLAIM sw a0, 0(x18) + j trap_exit_text trap_exit_text: # Remain in M-mode after mret @@ -532,23 +495,23 @@ trap_exit_text: LOAD x15, 12*REGBYTES(sp) LOAD x16, 13*REGBYTES(sp) LOAD x17, 14*REGBYTES(sp) - LOAD x28, 15*REGBYTES(sp) - LOAD x29, 16*REGBYTES(sp) - LOAD x30, 17*REGBYTES(sp) - LOAD x31, 18*REGBYTES(sp) - - LOAD x8, 20*REGBYTES(sp) - LOAD x9, 21*REGBYTES(sp) - LOAD x18, 22*REGBYTES(sp) - LOAD x19, 23*REGBYTES(sp) - LOAD x20, 24*REGBYTES(sp) - LOAD x21, 25*REGBYTES(sp) - LOAD x22, 26*REGBYTES(sp) - LOAD x23, 27*REGBYTES(sp) - LOAD x24, 28*REGBYTES(sp) - LOAD x25, 29*REGBYTES(sp) - LOAD x26, 30*REGBYTES(sp) - LOAD x27, 31*REGBYTES(sp) + LOAD x24, 15*REGBYTES(sp) + LOAD x25, 16*REGBYTES(sp) + LOAD x26, 17*REGBYTES(sp) + LOAD x27, 18*REGBYTES(sp) + LOAD x28, 19*REGBYTES(sp) + LOAD x29, 20*REGBYTES(sp) + LOAD x30, 21*REGBYTES(sp) + LOAD x31, 22*REGBYTES(sp) + + LOAD x8, 24*REGBYTES(sp) + LOAD x9, 25*REGBYTES(sp) + LOAD x18, 26*REGBYTES(sp) + LOAD x19, 27*REGBYTES(sp) + LOAD x20, 28*REGBYTES(sp) + LOAD x21, 29*REGBYTES(sp) + LOAD x22, 30*REGBYTES(sp) + LOAD x23, 31*REGBYTES(sp) addi sp, sp, 32*REGBYTES mret diff --git a/fw/fe310/eos/uart.c b/fw/fe310/eos/soc/uart.c index 30f76d9..589832a 100644 --- a/fw/fe310/eos/uart.c +++ b/fw/fe310/eos/soc/uart.c @@ -9,6 +9,7 @@ #include "eos.h" #include "interrupt.h" #include "event.h" +#include "i2s.h" #include "uart.h" @@ -44,22 +45,30 @@ int eos_uart_init(uint8_t wakeup_cause) { eos_evtq_set_handler(EOS_EVT_UART, uart_handle_evt); eos_intr_set(INT_UART0_BASE, IRQ_PRIORITY_UART, uart_handle_intr); - UART0_REG(UART_REG_TXCTRL) |= UART_TXEN; - UART0_REG(UART_REG_RXCTRL) |= UART_RXEN; - eos_uart_speed(EOS_UART_SPEED); - eos_uart_start(); + + eos_uart_enable(); return EOS_OK; } -void eos_uart_start(void) { +void eos_uart_enable(void) { + UART0_REG(UART_REG_TXCTRL) |= UART_TXEN; + UART0_REG(UART_REG_RXCTRL) |= UART_RXEN; + GPIO_REG(GPIO_IOF_SEL) &= ~IOF0_UART0_MASK; GPIO_REG(GPIO_IOF_EN) |= IOF0_UART0_MASK; } -void eos_uart_stop(void) { +void eos_uart_disable(void) { GPIO_REG(GPIO_IOF_EN) &= ~IOF0_UART0_MASK; + + UART0_REG(UART_REG_TXCTRL) &= ~UART_TXEN; + UART0_REG(UART_REG_RXCTRL) &= ~UART_RXEN; +} + +int eos_uart_enabled(void) { + return !!(GPIO_REG(GPIO_IOF_EN) & IOF0_UART0_MASK); } void eos_uart_speed(uint32_t baud_rate) { @@ -90,8 +99,8 @@ void eos_uart_rxwm_clear(void) { UART0_REG(UART_REG_IE) &= ~UART_IP_RXWM; } -int eos_uart_putc(int c, char b) { - if (b) { +int eos_uart_putc(int c, int block) { + if (block) { while (UART0_REG(UART_REG_TXFIFO) & 0x80000000); UART0_REG(UART_REG_TXFIFO) = c & 0xff; } else { @@ -101,14 +110,14 @@ int eos_uart_putc(int c, char b) { return EOS_OK; } -int eos_uart_getc(char b) { +int eos_uart_getc(int block) { volatile uint32_t r; - if (b) { + if (block) { while ((r = UART0_REG(UART_REG_RXFIFO)) & 0x80000000); } else { r = UART0_REG(UART_REG_RXFIFO); if (r & 0x80000000) return EOS_ERR_EMPTY; } return r & 0xff; -}
\ No newline at end of file +} diff --git a/fw/fe310/eos/uart.h b/fw/fe310/eos/soc/uart.h index 94999e6..caaf6c6 100644 --- a/fw/fe310/eos/uart.h +++ b/fw/fe310/eos/soc/uart.h @@ -10,8 +10,9 @@ typedef void (*eos_uart_handler_t) (unsigned char); int eos_uart_init(uint8_t wakeup_cause); -void eos_uart_start(void); -void eos_uart_stop(void); +void eos_uart_enable(void); +void eos_uart_disable(void); +int eos_uart_enabled(void); void eos_uart_speed(uint32_t baud_rate); void eos_uart_set_handler(unsigned char type, eos_uart_handler_t handler); @@ -20,5 +21,5 @@ void eos_uart_txwm_set(uint8_t wm); void eos_uart_txwm_clear(void); void eos_uart_rxwm_set(uint8_t wm); void eos_uart_rxwm_clear(void); -int eos_uart_putc(int c, char b); -int eos_uart_getc(char b);
\ No newline at end of file +int eos_uart_putc(int c, int block); +int eos_uart_getc(int block);
\ No newline at end of file diff --git a/fw/fe310/eos/wifi.c b/fw/fe310/eos/wifi.c deleted file mode 100644 index 0663582..0000000 --- a/fw/fe310/eos/wifi.c +++ /dev/null @@ -1,106 +0,0 @@ -#include <stdlib.h> -#include <stdint.h> -#include <string.h> - -#include "eos.h" -#include "event.h" -#include "net.h" - -#include "wifi.h" - -static eos_evt_handler_t evt_handler[EOS_WIFI_MAX_MTYPE]; - -static void wifi_handle_msg(unsigned char type, unsigned char *buffer, uint16_t len) { - unsigned char mtype; - - if ((buffer == NULL) || (len < 1)) { - eos_net_bad_handler(type, buffer, len); - return; - } - - mtype = buffer[0]; - if ((mtype < EOS_WIFI_MAX_MTYPE) && evt_handler[mtype]) { - evt_handler[mtype](mtype, buffer, len); - } else { - eos_net_bad_handler(type, buffer, len); - } -} - -void eos_wifi_netinit(void) { - int i; - - for (i=0; i<EOS_WIFI_MAX_MTYPE; i++) { - evt_handler[i] = NULL; - } - eos_net_set_handler(EOS_NET_MTYPE_WIFI, wifi_handle_msg); -} - -void eos_wifi_set_handler(unsigned char mtype, eos_evt_handler_t handler) { - if (mtype < EOS_WIFI_MAX_MTYPE) evt_handler[mtype] = handler; -} - -eos_evt_handler_t eos_wifi_get_handler(unsigned char mtype) { - if (mtype < EOS_WIFI_MAX_MTYPE) return evt_handler[mtype]; - return NULL; -} - -int eos_wifi_scan(unsigned char *buffer) { - int async; - - async = 0; - if (buffer == NULL) { - buffer = eos_net_alloc(); - async = 1; - } - buffer[0] = EOS_WIFI_MTYPE_SCAN; - return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1); -} - -int eos_wifi_auth(const char *ssid, const char *pass, unsigned char *buffer) { - unsigned char *buf; - size_t ssid_len, pass_len; - int async; - - async = 0; - if (buffer == NULL) { - buffer = eos_net_alloc(); - async = 1; - } - ssid_len = strlen(ssid) + 1; - pass_len = strlen(pass) + 1; - if ((1 + ssid_len + pass_len) > EOS_NET_MTU) return EOS_ERR_SIZE; - - buf = buffer; - buf[0] = EOS_WIFI_MTYPE_AUTH; - buf++; - strcpy(buf, ssid); - buf += ssid_len; - strcpy(buf, pass); - buf += pass_len; - - return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1 + ssid_len + pass_len, async, 1); -} - -int eos_wifi_connect(unsigned char *buffer) { - int async; - - async = 0; - if (buffer == NULL) { - buffer = eos_net_alloc(); - async = 1; - } - buffer[0] = EOS_WIFI_MTYPE_CONNECT; - return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1); -} - -int eos_wifi_disconnect(unsigned char *buffer) { - int async; - - async = 0; - if (buffer == NULL) { - buffer = eos_net_alloc(); - async = 1; - } - buffer[0] = EOS_WIFI_MTYPE_DISCONNECT; - return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1); -} diff --git a/fw/fe310/eos/wifi.h b/fw/fe310/eos/wifi.h deleted file mode 100644 index 4a49518..0000000 --- a/fw/fe310/eos/wifi.h +++ /dev/null @@ -1,18 +0,0 @@ -#include <stdint.h> -#include "event.h" - -#define EOS_WIFI_MTYPE_SCAN 1 -#define EOS_WIFI_MTYPE_AUTH 2 -#define EOS_WIFI_MTYPE_CONNECT 3 -#define EOS_WIFI_MTYPE_DISCONNECT 4 - -#define EOS_WIFI_MAX_MTYPE 5 - -void eos_wifi_netinit(void); -void eos_wifi_set_handler(unsigned char mtype, eos_evt_handler_t handler); -eos_evt_handler_t eos_wifi_get_handler(unsigned char mtype); - -int eos_wifi_scan(unsigned char *buffer); -int eos_wifi_auth(const char *ssid, const char *pass, unsigned char *buffer); -int eos_wifi_connect(unsigned char *buffer); -int eos_wifi_disconnect(unsigned char *buffer); diff --git a/fw/fe310/test/Makefile b/fw/fe310/test/Makefile deleted file mode 100644 index dde518c..0000000 --- a/fw/fe310/test/Makefile +++ /dev/null @@ -1,30 +0,0 @@ -include ../common.mk -DEPS = main.o mem.o cell_dev.o cell_pdp.o phone.o modem.o wifi.o cam.o fs.o test.o -LIBS_ECP = - -# LIBS_ECP += -lecp -lecpcr -lecptr -lecptm -lecpdir -lecpvconn -# DEPS += ecp.o ecp_init.o audio.o - -CFLAGS += -I$(bsp_dir)/include -I$(bsp_dir)/drivers -I$(ext_dir)/crypto -I$(ext_dir)/fsfat -I$(ecp_dir)/src -I$(ecp_dir)/src/platform/fe310 -I.. -LDFLAGS = app/*.o $(CFLAGS) -L.. -L$(ecp_dir)/build-fe310 -Wl,--gc-sections -nostartfiles -nostdlib -Wl,--start-group -lc -lm -lgcc -leos $(LIBS_ECP) -Wl,--end-group -T../bsp/default.lds -TARGET = phone - - -all: $(TARGET) - -app_: - (cd app && $(MAKE)) || exit; - -%.o: %.c - $(CC) $(CFLAGS) -c $< - -$(TARGET): app_ $(DEPS) - $(CC) $(DEPS) $(LDFLAGS) -o $@ - -clean: - (cd app && $(MAKE) clean) || exit; - rm -f *.o *.a $(TARGET) - -upload: $(TARGET) - ../bsp/upload --elf ./$(TARGET) --openocd $(RISCV_OPENOCD_HOME)/bin/openocd --gdb $(RISCV_HOME)/bin/riscv64-unknown-elf-gdb --openocd-config ../bsp/openocd.cfg - diff --git a/fw/fe310/test/app/app_root.c b/fw/fe310/test/app/app_root.c index 7dda3c7..e6de2d1 100644 --- a/fw/fe310/test/app/app_root.c +++ b/fw/fe310/test/app/app_root.c @@ -16,7 +16,7 @@ #include "app_root.h" #define KBD_X 0 -#define KBD_Y 575 +#define KBD_Y 629 #define KBD_W 480 #define KBD_H 225 @@ -84,5 +84,5 @@ void app_root_init(eve_view_constructor_t home_page, int b) { eve_spi_stop(); - eos_net_acquire_for_evt(EOS_EVT_UI | EVE_ETYPE_INTR, 1); + eos_net_acquire_for_evt(EOS_EVT_EVE | EVE_ETYPE_INTR, 1); } diff --git a/fw/fe310/test/app/app_root.h b/fw/fe310/test/app/app_root.h index b085344..35f889f 100644 --- a/fw/fe310/test/app/app_root.h +++ b/fw/fe310/test/app/app_root.h @@ -1,7 +1,7 @@ #include <stdint.h> #define APP_SCREEN_W 480 -#define APP_SCREEN_H 800 +#define APP_SCREEN_H 854 #define APP_STATUS_H 60 #define APP_FONT_HANDLE 31 diff --git a/fw/fe310/test/fs.c b/fw/fe310/test/fs.c index 222833b..1dce3af 100644 --- a/fw/fe310/test/fs.c +++ b/fw/fe310/test/fs.c @@ -100,5 +100,30 @@ void app_fs_init(void) { rv = f_mount(&fs, "", 1); printf("f_mount:%d\n", rv); } + + if (rv == FR_OK) { + FIL f; + UINT w; + char *msg = "PERA JE CAR!"; + + rv = f_open(&f, TEXT_FN, FA_READ); + printf("f_open:%d\n", rv); + if (!rv) { + UINT r; + char _msg[TEXT_SIZE]; + + memset(_msg, 0, sizeof(_msg)); + rv = f_read(&f, _msg, sizeof(_msg), &r); + printf("f_read:%d %u %s\n", rv, r, _msg); + f_close(&f); + } else { + rv = f_open(&f, TEXT_FN, FA_WRITE | FA_CREATE_ALWAYS); + printf("f_open:%d\n", rv); + rv = f_write(&f, msg, strlen(msg), &w); + printf("f_write:%d\n", rv); + } + f_close(&f); + + } eos_spi_deselect(); }
\ No newline at end of file diff --git a/fw/fe310/test/main.c b/fw/fe310/test/main.c index ec57b99..2fc1a04 100644 --- a/fw/fe310/test/main.c +++ b/fw/fe310/test/main.c @@ -30,6 +30,8 @@ #include "fs.h" #include "test.h" +const uint32_t _eos_touch_matrix[6] = {0xf7ac,0x440,0x3e704,0xfffff718,0x108a3,0xfff76d42}; + void app_home_page(EVEWindow *window, EVEViewStack *stack) { EVEWidgetSpec spec[] = { /* @@ -63,13 +65,13 @@ void app_home_page(EVEWindow *window, EVEViewStack *stack) { .widget.spec.page.title = "Camera", .widget.spec.page.constructor = app_cam }, + */ { .widget.type = EVE_WIDGET_TYPE_PAGE, .widget.g.w = APP_SCREEN_W, .widget.spec.page.title = "File system", .widget.spec.page.constructor = app_fs }, - */ { .widget.type = EVE_WIDGET_TYPE_PAGE, .widget.g.w = APP_SCREEN_W, @@ -78,22 +80,26 @@ void app_home_page(EVEWindow *window, EVEViewStack *stack) { }, }; - EVEForm *form = eve_form_create(window, stack, spec, 1, NULL, NULL, NULL); + EVEForm *form = eve_form_create(window, stack, spec, 2, NULL, NULL, NULL); } void print_mem(void); int main() { eos_init(); + // eos_run_once(); + printf("FREQ:%lu\n", PRCI_get_cpu_freq()); printf("\nREADY.\n"); - app_root_init(app_home_page, 0x20); - // app_phone_init(); - // app_wifi_init(); - // app_cell_dev_init(); - // app_cell_pdp_init(); - // app_fs_init(); + app_root_init(app_home_page, 0x10); + app_phone_init(); + app_wifi_init(); + app_cell_dev_init(); + app_cell_pdp_init(); + app_fs_init(); + // audio_start(); + // app_ecp_init(); eos_evtq_loop(); } diff --git a/hw/footprints.pretty/Hirose_FH12-40S-0.5SH_1x40-1MP_P0.50mm_Horizontal.kicad_mod b/hw/footprints.pretty/Hirose_FH12-40S-0.5SH_1x40-1MP_P0.50mm_Horizontal.kicad_mod deleted file mode 100644 index 269dfef..0000000 --- a/hw/footprints.pretty/Hirose_FH12-40S-0.5SH_1x40-1MP_P0.50mm_Horizontal.kicad_mod +++ /dev/null @@ -1,89 +0,0 @@ -(module Hirose_FH12-40S-0.5SH_1x40-1MP_P0.50mm_Horizontal (layer F.Cu) (tedit 5AEE0F8A) - (descr "Molex FH12, FFC/FPC connector, FH12-40S-0.5SH, 40 Pins per row (https://www.hirose.com/product/en/products/FH12/FH12-24S-0.5SH(55)/), generated with kicad-footprint-generator") - (tags "connector Hirose top entry") - (attr smd) - (fp_text reference REF** (at 0 -3.7) (layer F.SilkS) - (effects (font (size 1 1) (thickness 0.15))) - ) - (fp_text value Hirose_FH12-40S-0.5SH_1x40-1MP_P0.50mm_Horizontal (at 0 5.6) (layer F.Fab) - (effects (font (size 1 1) (thickness 0.15))) - ) - (fp_line (start 0 -1.2) (end -11.55 -1.2) (layer F.Fab) (width 0.1)) - (fp_line (start -11.55 -1.2) (end -11.55 3.4) (layer F.Fab) (width 0.1)) - (fp_line (start -11.55 3.4) (end -10.95 3.4) (layer F.Fab) (width 0.1)) - (fp_line (start -10.95 3.4) (end -10.95 3.7) (layer F.Fab) (width 0.1)) - (fp_line (start -10.95 3.7) (end -11.45 3.7) (layer F.Fab) (width 0.1)) - (fp_line (start -11.45 3.7) (end -11.45 4.4) (layer F.Fab) (width 0.1)) - (fp_line (start -11.45 4.4) (end 0 4.4) (layer F.Fab) (width 0.1)) - (fp_line (start 0 -1.2) (end 11.55 -1.2) (layer F.Fab) (width 0.1)) - (fp_line (start 11.55 -1.2) (end 11.55 3.4) (layer F.Fab) (width 0.1)) - (fp_line (start 11.55 3.4) (end 10.95 3.4) (layer F.Fab) (width 0.1)) - (fp_line (start 10.95 3.4) (end 10.95 3.7) (layer F.Fab) (width 0.1)) - (fp_line (start 10.95 3.7) (end 11.45 3.7) (layer F.Fab) (width 0.1)) - (fp_line (start 11.45 3.7) (end 11.45 4.4) (layer F.Fab) (width 0.1)) - (fp_line (start 11.45 4.4) (end 0 4.4) (layer F.Fab) (width 0.1)) - (fp_line (start -10.16 -1.3) (end -11.65 -1.3) (layer F.SilkS) (width 0.12)) - (fp_line (start -11.65 -1.3) (end -11.65 0.04) (layer F.SilkS) (width 0.12)) - (fp_line (start 10.16 -1.3) (end 11.65 -1.3) (layer F.SilkS) (width 0.12)) - (fp_line (start 11.65 -1.3) (end 11.65 0.04) (layer F.SilkS) (width 0.12)) - (fp_line (start -11.65 2.76) (end -11.65 4.5) (layer F.SilkS) (width 0.12)) - (fp_line (start -11.65 4.5) (end 11.65 4.5) (layer F.SilkS) (width 0.12)) - (fp_line (start 11.65 4.5) (end 11.65 2.76) (layer F.SilkS) (width 0.12)) - (fp_line (start -10.16 -1.3) (end -10.16 -2.5) (layer F.SilkS) (width 0.12)) - (fp_line (start -10.25 -1.2) (end -9.75 -0.492893) (layer F.Fab) (width 0.1)) - (fp_line (start -9.75 -0.492893) (end -9.25 -1.2) (layer F.Fab) (width 0.1)) - (fp_line (start -13.05 -3) (end -13.05 4.9) (layer F.CrtYd) (width 0.05)) - (fp_line (start -13.05 4.9) (end 13.05 4.9) (layer F.CrtYd) (width 0.05)) - (fp_line (start 13.05 4.9) (end 13.05 -3) (layer F.CrtYd) (width 0.05)) - (fp_line (start 13.05 -3) (end -13.05 -3) (layer F.CrtYd) (width 0.05)) - (fp_text user %R (at 0 3.7) (layer F.Fab) - (effects (font (size 1 1) (thickness 0.15))) - ) - (pad MP smd rect (at 11.65 1.4) (size 1.8 2.2) (layers F.Cu F.Paste F.Mask)) - (pad MP smd rect (at -11.65 1.4) (size 1.8 2.2) (layers F.Cu F.Paste F.Mask)) - (pad 1 smd rect (at -9.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 2 smd rect (at -9.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 3 smd rect (at -8.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 4 smd rect (at -8.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 5 smd rect (at -7.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 6 smd rect (at -7.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 7 smd rect (at -6.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 8 smd rect (at -6.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 9 smd rect (at -5.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 10 smd rect (at -5.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 11 smd rect (at -4.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 12 smd rect (at -4.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 13 smd rect (at -3.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 14 smd rect (at -3.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 15 smd rect (at -2.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 16 smd rect (at -2.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 17 smd rect (at -1.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 18 smd rect (at -1.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 19 smd rect (at -0.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 20 smd rect (at -0.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 21 smd rect (at 0.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 22 smd rect (at 0.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 23 smd rect (at 1.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 24 smd rect (at 1.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 25 smd rect (at 2.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 26 smd rect (at 2.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 27 smd rect (at 3.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 28 smd rect (at 3.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 29 smd rect (at 4.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 30 smd rect (at 4.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 31 smd rect (at 5.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 32 smd rect (at 5.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 33 smd rect (at 6.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 34 smd rect (at 6.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 35 smd rect (at 7.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 36 smd rect (at 7.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 37 smd rect (at 8.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 38 smd rect (at 8.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 39 smd rect (at 9.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 40 smd rect (at 9.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (model ${KISYS3DMOD}/Connector_FFC-FPC.3dshapes/Hirose_FH12-40S-0.5SH_1x40-1MP_P0.50mm_Horizontal.wrl - (at (xyz 0 0 0)) - (scale (xyz 1 1 1)) - (rotate (xyz 0 0 0)) - ) -) diff --git a/hw/footprints.pretty/Hirose_FH12-40S-0.5SH_1x40-1MP_P0.50mm_Horizontal_Bottom.kicad_mod b/hw/footprints.pretty/Hirose_FH12-40S-0.5SH_1x40-1MP_P0.50mm_Horizontal_Bottom.kicad_mod deleted file mode 100644 index 8fb2633..0000000 --- a/hw/footprints.pretty/Hirose_FH12-40S-0.5SH_1x40-1MP_P0.50mm_Horizontal_Bottom.kicad_mod +++ /dev/null @@ -1,89 +0,0 @@ -(module Hirose_FH12-40S-0.5SH_1x40-1MP_P0.50mm_Horizontal_Bottom (layer F.Cu) (tedit 5C813A4B) - (descr "Molex FH12, FFC/FPC connector, FH12-40S-0.5SH, 40 Pins per row (https://www.hirose.com/product/en/products/FH12/FH12-24S-0.5SH(55)/), generated with kicad-footprint-generator") - (tags "connector Hirose bottom entry") - (attr smd) - (fp_text reference REF** (at 0 -3.7) (layer F.SilkS) - (effects (font (size 1 1) (thickness 0.15))) - ) - (fp_text value Hirose_FH12-40S-0.5SH_1x40-1MP_P0.50mm_Horizontal_Bottom (at 0 5.6) (layer F.Fab) - (effects (font (size 1 1) (thickness 0.15))) - ) - (fp_line (start 0 -1.2) (end -11.55 -1.2) (layer F.Fab) (width 0.1)) - (fp_line (start -11.55 -1.2) (end -11.55 3.4) (layer F.Fab) (width 0.1)) - (fp_line (start -11.55 3.4) (end -10.95 3.4) (layer F.Fab) (width 0.1)) - (fp_line (start -10.95 3.4) (end -10.95 3.7) (layer F.Fab) (width 0.1)) - (fp_line (start -10.95 3.7) (end -11.45 3.7) (layer F.Fab) (width 0.1)) - (fp_line (start -11.45 3.7) (end -11.45 4.4) (layer F.Fab) (width 0.1)) - (fp_line (start -11.45 4.4) (end 0 4.4) (layer F.Fab) (width 0.1)) - (fp_line (start 0 -1.2) (end 11.55 -1.2) (layer F.Fab) (width 0.1)) - (fp_line (start 11.55 -1.2) (end 11.55 3.4) (layer F.Fab) (width 0.1)) - (fp_line (start 11.55 3.4) (end 10.95 3.4) (layer F.Fab) (width 0.1)) - (fp_line (start 10.95 3.4) (end 10.95 3.7) (layer F.Fab) (width 0.1)) - (fp_line (start 10.95 3.7) (end 11.45 3.7) (layer F.Fab) (width 0.1)) - (fp_line (start 11.45 3.7) (end 11.45 4.4) (layer F.Fab) (width 0.1)) - (fp_line (start 11.45 4.4) (end 0 4.4) (layer F.Fab) (width 0.1)) - (fp_line (start -10.16 -1.3) (end -11.65 -1.3) (layer F.SilkS) (width 0.12)) - (fp_line (start -11.65 -1.3) (end -11.65 0.04) (layer F.SilkS) (width 0.12)) - (fp_line (start 10.16 -1.3) (end 11.65 -1.3) (layer F.SilkS) (width 0.12)) - (fp_line (start 11.65 -1.3) (end 11.65 0.04) (layer F.SilkS) (width 0.12)) - (fp_line (start -11.65 2.76) (end -11.65 4.5) (layer F.SilkS) (width 0.12)) - (fp_line (start -11.65 4.5) (end 11.65 4.5) (layer F.SilkS) (width 0.12)) - (fp_line (start 11.65 4.5) (end 11.65 2.76) (layer F.SilkS) (width 0.12)) - (fp_line (start -10.16 -1.3) (end -10.16 -2.5) (layer F.SilkS) (width 0.12)) - (fp_line (start -10.25 -1.2) (end -9.75 -0.492893) (layer F.Fab) (width 0.1)) - (fp_line (start -9.75 -0.492893) (end -9.25 -1.2) (layer F.Fab) (width 0.1)) - (fp_line (start -13.05 -3) (end -13.05 4.9) (layer F.CrtYd) (width 0.05)) - (fp_line (start -13.05 4.9) (end 13.05 4.9) (layer F.CrtYd) (width 0.05)) - (fp_line (start 13.05 4.9) (end 13.05 -3) (layer F.CrtYd) (width 0.05)) - (fp_line (start 13.05 -3) (end -13.05 -3) (layer F.CrtYd) (width 0.05)) - (fp_text user %R (at 0 3.7) (layer F.Fab) - (effects (font (size 1 1) (thickness 0.15))) - ) - (pad MP smd rect (at 11.65 1.4) (size 1.8 2.2) (layers F.Cu F.Paste F.Mask)) - (pad MP smd rect (at -11.65 1.4) (size 1.8 2.2) (layers F.Cu F.Paste F.Mask)) - (pad 40 smd rect (at -9.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 39 smd rect (at -9.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 38 smd rect (at -8.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 37 smd rect (at -8.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 36 smd rect (at -7.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 35 smd rect (at -7.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 34 smd rect (at -6.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 33 smd rect (at -6.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 32 smd rect (at -5.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 31 smd rect (at -5.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 30 smd rect (at -4.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 29 smd rect (at -4.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 28 smd rect (at -3.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 27 smd rect (at -3.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 26 smd rect (at -2.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 25 smd rect (at -2.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 24 smd rect (at -1.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 23 smd rect (at -1.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 22 smd rect (at -0.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 21 smd rect (at -0.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 20 smd rect (at 0.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 19 smd rect (at 0.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 18 smd rect (at 1.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 17 smd rect (at 1.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 16 smd rect (at 2.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 15 smd rect (at 2.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 14 smd rect (at 3.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 13 smd rect (at 3.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 12 smd rect (at 4.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 11 smd rect (at 4.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 10 smd rect (at 5.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 9 smd rect (at 5.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 8 smd rect (at 6.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 7 smd rect (at 6.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 6 smd rect (at 7.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 5 smd rect (at 7.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 4 smd rect (at 8.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 3 smd rect (at 8.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 2 smd rect (at 9.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 1 smd rect (at 9.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (model ${KISYS3DMOD}/Connector_FFC-FPC.3dshapes/Hirose_FH12-40S-0.5SH_1x40-1MP_P0.50mm_Horizontal.wrl - (at (xyz 0 0 0)) - (scale (xyz 1 1 1)) - (rotate (xyz 0 0 0)) - ) -) diff --git a/hw/footprints.pretty/Hirose_FH12-45S-0.5SH_1x45-1MP_P0.50mm_Horizontal.kicad_mod b/hw/footprints.pretty/Hirose_FH12-45S-0.5SH_1x45-1MP_P0.50mm_Horizontal.kicad_mod new file mode 100644 index 0000000..9ba12c2 --- /dev/null +++ b/hw/footprints.pretty/Hirose_FH12-45S-0.5SH_1x45-1MP_P0.50mm_Horizontal.kicad_mod @@ -0,0 +1,94 @@ +(module Hirose_FH12-45S-0.5SH_1x45-1MP_P0.50mm_Horizontal (layer F.Cu) (tedit 6282DA52) + (descr "Hirose FH12, FFC/FPC connector, FH12-45S-0.5SH, 45 Pins per row (https://www.hirose.com/product/en/products/FH12/FH12-24S-0.5SH(55)/), generated with kicad-footprint-generator") + (tags "connector Hirose FH12 horizontal") + (attr smd) + (fp_text reference REF** (at 0 -3.7) (layer F.SilkS) + (effects (font (size 1 1) (thickness 0.15))) + ) + (fp_text value Hirose_FH12-45S-0.5SH_1x45-1MP_P0.50mm_Horizontal (at 0 5.6) (layer F.Fab) + (effects (font (size 1 1) (thickness 0.15))) + ) + (fp_line (start 0 -1.2) (end -12.8 -1.2) (layer F.Fab) (width 0.1)) + (fp_line (start -12.8 -1.2) (end -12.8 3.4) (layer F.Fab) (width 0.1)) + (fp_line (start -12.8 3.4) (end -12.2 3.4) (layer F.Fab) (width 0.1)) + (fp_line (start -12.2 3.4) (end -12.2 3.7) (layer F.Fab) (width 0.1)) + (fp_line (start -12.2 3.7) (end -12.7 3.7) (layer F.Fab) (width 0.1)) + (fp_line (start -12.7 3.7) (end -12.7 4.4) (layer F.Fab) (width 0.1)) + (fp_line (start -12.7 4.4) (end 0 4.4) (layer F.Fab) (width 0.1)) + (fp_line (start 0 -1.2) (end 12.8 -1.2) (layer F.Fab) (width 0.1)) + (fp_line (start 12.8 -1.2) (end 12.8 3.4) (layer F.Fab) (width 0.1)) + (fp_line (start 12.8 3.4) (end 12.2 3.4) (layer F.Fab) (width 0.1)) + (fp_line (start 12.2 3.4) (end 12.2 3.7) (layer F.Fab) (width 0.1)) + (fp_line (start 12.2 3.7) (end 12.7 3.7) (layer F.Fab) (width 0.1)) + (fp_line (start 12.7 3.7) (end 12.7 4.4) (layer F.Fab) (width 0.1)) + (fp_line (start 12.7 4.4) (end 0 4.4) (layer F.Fab) (width 0.1)) + (fp_line (start -11.41 -1.3) (end -12.9 -1.3) (layer F.SilkS) (width 0.12)) + (fp_line (start -12.9 -1.3) (end -12.9 0.04) (layer F.SilkS) (width 0.12)) + (fp_line (start 11.41 -1.3) (end 12.9 -1.3) (layer F.SilkS) (width 0.12)) + (fp_line (start 12.9 -1.3) (end 12.9 0.04) (layer F.SilkS) (width 0.12)) + (fp_line (start -12.9 2.76) (end -12.9 4.5) (layer F.SilkS) (width 0.12)) + (fp_line (start -12.9 4.5) (end 12.9 4.5) (layer F.SilkS) (width 0.12)) + (fp_line (start 12.9 4.5) (end 12.9 2.76) (layer F.SilkS) (width 0.12)) + (fp_line (start -11.41 -1.3) (end -11.41 -2.5) (layer F.SilkS) (width 0.12)) + (fp_line (start -11.5 -1.2) (end -11 -0.492893) (layer F.Fab) (width 0.1)) + (fp_line (start -11 -0.492893) (end -10.5 -1.2) (layer F.Fab) (width 0.1)) + (fp_line (start -14.3 -3) (end -14.3 4.9) (layer F.CrtYd) (width 0.05)) + (fp_line (start -14.3 4.9) (end 14.3 4.9) (layer F.CrtYd) (width 0.05)) + (fp_line (start 14.3 4.9) (end 14.3 -3) (layer F.CrtYd) (width 0.05)) + (fp_line (start 14.3 -3) (end -14.3 -3) (layer F.CrtYd) (width 0.05)) + (fp_text user %R (at 0 3.7) (layer F.Fab) + (effects (font (size 1 1) (thickness 0.15))) + ) + (pad MP smd rect (at 12.6 1.4) (size 2.4 2.2) (layers F.Cu F.Paste F.Mask)) + (pad MP smd rect (at -12.6 1.4) (size 2.4 2.2) (layers F.Cu F.Paste F.Mask)) + (pad 1 smd rect (at -11 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 2 smd rect (at -10.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 3 smd rect (at -10 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 4 smd rect (at -9.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 5 smd rect (at -9 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 6 smd rect (at -8.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 7 smd rect (at -8 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 8 smd rect (at -7.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 9 smd rect (at -7 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 10 smd rect (at -6.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 11 smd rect (at -6 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 12 smd rect (at -5.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 13 smd rect (at -5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 14 smd rect (at -4.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 15 smd rect (at -4 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 16 smd rect (at -3.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 17 smd rect (at -3 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 18 smd rect (at -2.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 19 smd rect (at -2 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 20 smd rect (at -1.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 21 smd rect (at -1 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 22 smd rect (at -0.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 23 smd rect (at 0 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 24 smd rect (at 0.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 25 smd rect (at 1 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 26 smd rect (at 1.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 27 smd rect (at 2 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 28 smd rect (at 2.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 29 smd rect (at 3 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 30 smd rect (at 3.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 31 smd rect (at 4 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 32 smd rect (at 4.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 33 smd rect (at 5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 34 smd rect (at 5.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 35 smd rect (at 6 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 36 smd rect (at 6.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 37 smd rect (at 7 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 38 smd rect (at 7.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 39 smd rect (at 8 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 40 smd rect (at 8.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 41 smd rect (at 9 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 42 smd rect (at 9.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 43 smd rect (at 10 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 44 smd rect (at 10.5 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 45 smd rect (at 11 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (model ${KISYS3DMOD}/Connector_FFC-FPC.3dshapes/Hirose_FH12-45S-0.5SH_1x45-1MP_P0.50mm_Horizontal.wrl + (at (xyz 0 0 0)) + (scale (xyz 1 1 1)) + (rotate (xyz 0 0 0)) + ) +) diff --git a/hw/footprints.pretty/Hirose_FH12-6S-0.5SH_1x06-1MP_P0.50mm_Horizontal.kicad_mod b/hw/footprints.pretty/Hirose_FH12-6S-0.5SH_1x06-1MP_P0.50mm_Horizontal.kicad_mod deleted file mode 100644 index ef8ef9a..0000000 --- a/hw/footprints.pretty/Hirose_FH12-6S-0.5SH_1x06-1MP_P0.50mm_Horizontal.kicad_mod +++ /dev/null @@ -1,55 +0,0 @@ -(module Hirose_FH12-6S-0.5SH_1x06-1MP_P0.50mm_Horizontal (layer F.Cu) (tedit 5AEE0F8A) - (descr "Molex FH12, FFC/FPC connector, FH12-6S-0.5SH, 6 Pins per row (https://www.hirose.com/product/en/products/FH12/FH12-24S-0.5SH(55)/), generated with kicad-footprint-generator") - (tags "connector Hirose top entry") - (attr smd) - (fp_text reference REF** (at 0 -3.7) (layer F.SilkS) - (effects (font (size 1 1) (thickness 0.15))) - ) - (fp_text value Hirose_FH12-6S-0.5SH_1x06-1MP_P0.50mm_Horizontal (at 0 5.6) (layer F.Fab) - (effects (font (size 1 1) (thickness 0.15))) - ) - (fp_line (start 0 -1.2) (end -3.05 -1.2) (layer F.Fab) (width 0.1)) - (fp_line (start -3.05 -1.2) (end -3.05 3.4) (layer F.Fab) (width 0.1)) - (fp_line (start -3.05 3.4) (end -2.45 3.4) (layer F.Fab) (width 0.1)) - (fp_line (start -2.45 3.4) (end -2.45 3.7) (layer F.Fab) (width 0.1)) - (fp_line (start -2.45 3.7) (end -2.95 3.7) (layer F.Fab) (width 0.1)) - (fp_line (start -2.95 3.7) (end -2.95 4.4) (layer F.Fab) (width 0.1)) - (fp_line (start -2.95 4.4) (end 0 4.4) (layer F.Fab) (width 0.1)) - (fp_line (start 0 -1.2) (end 3.05 -1.2) (layer F.Fab) (width 0.1)) - (fp_line (start 3.05 -1.2) (end 3.05 3.4) (layer F.Fab) (width 0.1)) - (fp_line (start 3.05 3.4) (end 2.45 3.4) (layer F.Fab) (width 0.1)) - (fp_line (start 2.45 3.4) (end 2.45 3.7) (layer F.Fab) (width 0.1)) - (fp_line (start 2.45 3.7) (end 2.95 3.7) (layer F.Fab) (width 0.1)) - (fp_line (start 2.95 3.7) (end 2.95 4.4) (layer F.Fab) (width 0.1)) - (fp_line (start 2.95 4.4) (end 0 4.4) (layer F.Fab) (width 0.1)) - (fp_line (start -1.66 -1.3) (end -3.15 -1.3) (layer F.SilkS) (width 0.12)) - (fp_line (start -3.15 -1.3) (end -3.15 0.04) (layer F.SilkS) (width 0.12)) - (fp_line (start 1.66 -1.3) (end 3.15 -1.3) (layer F.SilkS) (width 0.12)) - (fp_line (start 3.15 -1.3) (end 3.15 0.04) (layer F.SilkS) (width 0.12)) - (fp_line (start -3.15 2.76) (end -3.15 4.5) (layer F.SilkS) (width 0.12)) - (fp_line (start -3.15 4.5) (end 3.15 4.5) (layer F.SilkS) (width 0.12)) - (fp_line (start 3.15 4.5) (end 3.15 2.76) (layer F.SilkS) (width 0.12)) - (fp_line (start -1.66 -1.3) (end -1.66 -2.5) (layer F.SilkS) (width 0.12)) - (fp_line (start -1.75 -1.2) (end -1.25 -0.492893) (layer F.Fab) (width 0.1)) - (fp_line (start -1.25 -0.492893) (end -0.75 -1.2) (layer F.Fab) (width 0.1)) - (fp_line (start -4.55 -3) (end -4.55 4.9) (layer F.CrtYd) (width 0.05)) - (fp_line (start -4.55 4.9) (end 4.55 4.9) (layer F.CrtYd) (width 0.05)) - (fp_line (start 4.55 4.9) (end 4.55 -3) (layer F.CrtYd) (width 0.05)) - (fp_line (start 4.55 -3) (end -4.55 -3) (layer F.CrtYd) (width 0.05)) - (fp_text user %R (at 0 3.7) (layer F.Fab) - (effects (font (size 1 1) (thickness 0.15))) - ) - (pad MP smd rect (at 3.15 1.4) (size 1.8 2.2) (layers F.Cu F.Paste F.Mask)) - (pad MP smd rect (at -3.15 1.4) (size 1.8 2.2) (layers F.Cu F.Paste F.Mask)) - (pad 1 smd rect (at -1.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 2 smd rect (at -0.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 3 smd rect (at -0.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 4 smd rect (at 0.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 5 smd rect (at 0.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 6 smd rect (at 1.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (model ${KISYS3DMOD}/Connector_FFC-FPC.3dshapes/Hirose_FH12-6S-0.5SH_1x06-1MP_P0.50mm_Horizontal.wrl - (at (xyz 0 0 0)) - (scale (xyz 1 1 1)) - (rotate (xyz 0 0 0)) - ) -) diff --git a/hw/footprints.pretty/Hirose_FH12-6S-0.5SH_1x06-1MP_P0.50mm_Horizontal_Bottom.kicad_mod b/hw/footprints.pretty/Hirose_FH12-6S-0.5SH_1x06-1MP_P0.50mm_Horizontal_Bottom.kicad_mod deleted file mode 100644 index 3fe4990..0000000 --- a/hw/footprints.pretty/Hirose_FH12-6S-0.5SH_1x06-1MP_P0.50mm_Horizontal_Bottom.kicad_mod +++ /dev/null @@ -1,55 +0,0 @@ -(module Hirose_FH12-6S-0.5SH_1x06-1MP_P0.50mm_Horizontal_Bottom (layer F.Cu) (tedit 5C813AE2) - (descr "Molex FH12, FFC/FPC connector, FH12-6S-0.5SH, 6 Pins per row (https://www.hirose.com/product/en/products/FH12/FH12-24S-0.5SH(55)/), generated with kicad-footprint-generator") - (tags "connector Hirose bottom entry") - (attr smd) - (fp_text reference REF** (at 0 -3.7) (layer F.SilkS) - (effects (font (size 1 1) (thickness 0.15))) - ) - (fp_text value Hirose_FH12-6S-0.5SH_1x06-1MP_P0.50mm_Horizontal_Bottom (at 0 5.6) (layer F.Fab) - (effects (font (size 1 1) (thickness 0.15))) - ) - (fp_line (start 0 -1.2) (end -3.05 -1.2) (layer F.Fab) (width 0.1)) - (fp_line (start -3.05 -1.2) (end -3.05 3.4) (layer F.Fab) (width 0.1)) - (fp_line (start -3.05 3.4) (end -2.45 3.4) (layer F.Fab) (width 0.1)) - (fp_line (start -2.45 3.4) (end -2.45 3.7) (layer F.Fab) (width 0.1)) - (fp_line (start -2.45 3.7) (end -2.95 3.7) (layer F.Fab) (width 0.1)) - (fp_line (start -2.95 3.7) (end -2.95 4.4) (layer F.Fab) (width 0.1)) - (fp_line (start -2.95 4.4) (end 0 4.4) (layer F.Fab) (width 0.1)) - (fp_line (start 0 -1.2) (end 3.05 -1.2) (layer F.Fab) (width 0.1)) - (fp_line (start 3.05 -1.2) (end 3.05 3.4) (layer F.Fab) (width 0.1)) - (fp_line (start 3.05 3.4) (end 2.45 3.4) (layer F.Fab) (width 0.1)) - (fp_line (start 2.45 3.4) (end 2.45 3.7) (layer F.Fab) (width 0.1)) - (fp_line (start 2.45 3.7) (end 2.95 3.7) (layer F.Fab) (width 0.1)) - (fp_line (start 2.95 3.7) (end 2.95 4.4) (layer F.Fab) (width 0.1)) - (fp_line (start 2.95 4.4) (end 0 4.4) (layer F.Fab) (width 0.1)) - (fp_line (start -1.66 -1.3) (end -3.15 -1.3) (layer F.SilkS) (width 0.12)) - (fp_line (start -3.15 -1.3) (end -3.15 0.04) (layer F.SilkS) (width 0.12)) - (fp_line (start 1.66 -1.3) (end 3.15 -1.3) (layer F.SilkS) (width 0.12)) - (fp_line (start 3.15 -1.3) (end 3.15 0.04) (layer F.SilkS) (width 0.12)) - (fp_line (start -3.15 2.76) (end -3.15 4.5) (layer F.SilkS) (width 0.12)) - (fp_line (start -3.15 4.5) (end 3.15 4.5) (layer F.SilkS) (width 0.12)) - (fp_line (start 3.15 4.5) (end 3.15 2.76) (layer F.SilkS) (width 0.12)) - (fp_line (start -1.66 -1.3) (end -1.66 -2.5) (layer F.SilkS) (width 0.12)) - (fp_line (start -1.75 -1.2) (end -1.25 -0.492893) (layer F.Fab) (width 0.1)) - (fp_line (start -1.25 -0.492893) (end -0.75 -1.2) (layer F.Fab) (width 0.1)) - (fp_line (start -4.55 -3) (end -4.55 4.9) (layer F.CrtYd) (width 0.05)) - (fp_line (start -4.55 4.9) (end 4.55 4.9) (layer F.CrtYd) (width 0.05)) - (fp_line (start 4.55 4.9) (end 4.55 -3) (layer F.CrtYd) (width 0.05)) - (fp_line (start 4.55 -3) (end -4.55 -3) (layer F.CrtYd) (width 0.05)) - (fp_text user %R (at 0 3.7) (layer F.Fab) - (effects (font (size 1 1) (thickness 0.15))) - ) - (pad MP smd rect (at 3.15 1.4) (size 1.8 2.2) (layers F.Cu F.Paste F.Mask)) - (pad MP smd rect (at -3.15 1.4) (size 1.8 2.2) (layers F.Cu F.Paste F.Mask)) - (pad 6 smd rect (at -1.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 5 smd rect (at -0.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 4 smd rect (at -0.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 3 smd rect (at 0.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 2 smd rect (at 0.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (pad 1 smd rect (at 1.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) - (model ${KISYS3DMOD}/Connector_FFC-FPC.3dshapes/Hirose_FH12-6S-0.5SH_1x06-1MP_P0.50mm_Horizontal.wrl - (at (xyz 0 0 0)) - (scale (xyz 1 1 1)) - (rotate (xyz 0 0 0)) - ) -) diff --git a/hw/footprints.pretty/Hirose_FH12-8S-0.5SH_1x08-1MP_P0.50mm_Horizontal.kicad_mod b/hw/footprints.pretty/Hirose_FH12-8S-0.5SH_1x08-1MP_P0.50mm_Horizontal.kicad_mod new file mode 100644 index 0000000..c49268d --- /dev/null +++ b/hw/footprints.pretty/Hirose_FH12-8S-0.5SH_1x08-1MP_P0.50mm_Horizontal.kicad_mod @@ -0,0 +1,57 @@ +(module Hirose_FH12-8S-0.5SH_1x08-1MP_P0.50mm_Horizontal (layer F.Cu) (tedit 6282D8E1) + (descr "Hirose FH12, FFC/FPC connector, FH12-8S-0.5SH, 8 Pins per row (https://www.hirose.com/product/en/products/FH12/FH12-24S-0.5SH(55)/), generated with kicad-footprint-generator") + (tags "connector Hirose FH12 horizontal") + (attr smd) + (fp_text reference REF** (at 0 -3.7) (layer F.SilkS) + (effects (font (size 1 1) (thickness 0.15))) + ) + (fp_text value Hirose_FH12-8S-0.5SH_1x08-1MP_P0.50mm_Horizontal (at 0 5.6) (layer F.Fab) + (effects (font (size 1 1) (thickness 0.15))) + ) + (fp_line (start 0 -1.2) (end -3.55 -1.2) (layer F.Fab) (width 0.1)) + (fp_line (start -3.55 -1.2) (end -3.55 3.4) (layer F.Fab) (width 0.1)) + (fp_line (start -3.55 3.4) (end -2.95 3.4) (layer F.Fab) (width 0.1)) + (fp_line (start -2.95 3.4) (end -2.95 3.7) (layer F.Fab) (width 0.1)) + (fp_line (start -2.95 3.7) (end -3.45 3.7) (layer F.Fab) (width 0.1)) + (fp_line (start -3.45 3.7) (end -3.45 4.4) (layer F.Fab) (width 0.1)) + (fp_line (start -3.45 4.4) (end 0 4.4) (layer F.Fab) (width 0.1)) + (fp_line (start 0 -1.2) (end 3.55 -1.2) (layer F.Fab) (width 0.1)) + (fp_line (start 3.55 -1.2) (end 3.55 3.4) (layer F.Fab) (width 0.1)) + (fp_line (start 3.55 3.4) (end 2.95 3.4) (layer F.Fab) (width 0.1)) + (fp_line (start 2.95 3.4) (end 2.95 3.7) (layer F.Fab) (width 0.1)) + (fp_line (start 2.95 3.7) (end 3.45 3.7) (layer F.Fab) (width 0.1)) + (fp_line (start 3.45 3.7) (end 3.45 4.4) (layer F.Fab) (width 0.1)) + (fp_line (start 3.45 4.4) (end 0 4.4) (layer F.Fab) (width 0.1)) + (fp_line (start -2.16 -1.3) (end -3.65 -1.3) (layer F.SilkS) (width 0.12)) + (fp_line (start -3.65 -1.3) (end -3.65 0.04) (layer F.SilkS) (width 0.12)) + (fp_line (start 2.16 -1.3) (end 3.65 -1.3) (layer F.SilkS) (width 0.12)) + (fp_line (start 3.65 -1.3) (end 3.65 0.04) (layer F.SilkS) (width 0.12)) + (fp_line (start -3.65 2.76) (end -3.65 4.5) (layer F.SilkS) (width 0.12)) + (fp_line (start -3.65 4.5) (end 3.65 4.5) (layer F.SilkS) (width 0.12)) + (fp_line (start 3.65 4.5) (end 3.65 2.76) (layer F.SilkS) (width 0.12)) + (fp_line (start -2.16 -1.3) (end -2.16 -2.5) (layer F.SilkS) (width 0.12)) + (fp_line (start -2.25 -1.2) (end -1.75 -0.492893) (layer F.Fab) (width 0.1)) + (fp_line (start -1.75 -0.492893) (end -1.25 -1.2) (layer F.Fab) (width 0.1)) + (fp_line (start -5.05 -3) (end -5.05 4.9) (layer F.CrtYd) (width 0.05)) + (fp_line (start -5.05 4.9) (end 5.05 4.9) (layer F.CrtYd) (width 0.05)) + (fp_line (start 5.05 4.9) (end 5.05 -3) (layer F.CrtYd) (width 0.05)) + (fp_line (start 5.05 -3) (end -5.05 -3) (layer F.CrtYd) (width 0.05)) + (fp_text user %R (at 0 3.7) (layer F.Fab) + (effects (font (size 1 1) (thickness 0.15))) + ) + (pad MP smd rect (at 3.35 1.4) (size 2.4 2.2) (layers F.Cu F.Paste F.Mask)) + (pad MP smd rect (at -3.35 1.4) (size 2.4 2.2) (layers F.Cu F.Paste F.Mask)) + (pad 1 smd rect (at -1.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 2 smd rect (at -1.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 3 smd rect (at -0.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 4 smd rect (at -0.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 5 smd rect (at 0.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 6 smd rect (at 0.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 7 smd rect (at 1.25 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (pad 8 smd rect (at 1.75 -1.85) (size 0.3 1.3) (layers F.Cu F.Paste F.Mask)) + (model ${KISYS3DMOD}/Connector_FFC-FPC.3dshapes/Hirose_FH12-8S-0.5SH_1x08-1MP_P0.50mm_Horizontal.wrl + (at (xyz 0 0 0)) + (scale (xyz 1 1 1)) + (rotate (xyz 0 0 0)) + ) +) diff --git a/hw/footprints.pretty/SiT1533.kicad_mod b/hw/footprints.pretty/SiT1533.kicad_mod index a5ef984..8303d2f 100644 --- a/hw/footprints.pretty/SiT1533.kicad_mod +++ b/hw/footprints.pretty/SiT1533.kicad_mod @@ -1,4 +1,4 @@ -(module SiT1533 (layer F.Cu) (tedit 60C95F79) +(module SiT1533 (layer F.Cu) (tedit 62EA42B1) (solder_mask_margin -0.001) (fp_text reference REF** (at 0 -1.65) (layer F.SilkS) (effects (font (size 1 1) (thickness 0.15))) @@ -6,18 +6,18 @@ (fp_text value SiT1533 (at 0 1.85) (layer F.Fab) (effects (font (size 1 1) (thickness 0.15))) ) - (fp_line (start 1.1 -0.6) (end 1.1 0.6) (layer F.SilkS) (width 0.1)) - (fp_line (start -1.1 -0.6) (end -1.1 0) (layer F.SilkS) (width 0.1)) - (fp_line (start -1 -0.6) (end 1 -0.6) (layer F.Fab) (width 0.1)) - (fp_line (start 1 -0.6) (end 1 0.6) (layer F.Fab) (width 0.1)) - (fp_line (start 1 0.6) (end -1 0.6) (layer F.Fab) (width 0.1)) + (fp_line (start 1.25 -1.05) (end -1.25 -1.05) (layer F.CrtYd) (width 0.05)) + (fp_line (start 1.25 1.05) (end 1.25 -1.05) (layer F.CrtYd) (width 0.05)) + (fp_line (start -1.25 1.05) (end 1.25 1.05) (layer F.CrtYd) (width 0.05)) + (fp_line (start -1.25 -1.05) (end -1.25 1.05) (layer F.CrtYd) (width 0.05)) (fp_line (start -1 0.6) (end -1 -0.6) (layer F.Fab) (width 0.1)) - (fp_line (start -1.3 -0.9) (end -1.3 0.9) (layer F.CrtYd) (width 0.05)) - (fp_line (start -1.3 0.9) (end 1.3 0.9) (layer F.CrtYd) (width 0.05)) - (fp_line (start 1.3 0.9) (end 1.3 -0.9) (layer F.CrtYd) (width 0.05)) - (fp_line (start 1.3 -0.9) (end -1.3 -0.9) (layer F.CrtYd) (width 0.05)) - (pad 1 smd rect (at -0.8 0) (size 0.4 1.4) (layers F.Cu F.Paste F.Mask)) - (pad 3 smd rect (at 0.8 0) (size 0.4 1.4) (layers F.Cu F.Paste F.Mask)) - (pad 2 smd rect (at -0.1 0.45) (size 0.3 0.5) (layers F.Cu F.Paste F.Mask)) - (pad 4 smd rect (at 0.1 -0.45) (size 0.3 0.5) (layers F.Cu F.Paste F.Mask)) + (fp_line (start 1 0.6) (end -1 0.6) (layer F.Fab) (width 0.1)) + (fp_line (start 1 -0.6) (end 1 0.6) (layer F.Fab) (width 0.1)) + (fp_line (start -1 -0.6) (end 1 -0.6) (layer F.Fab) (width 0.1)) + (fp_line (start -1.25 -0.6) (end -1.25 0) (layer F.SilkS) (width 0.1)) + (fp_line (start 1.25 -0.6) (end 1.25 0.6) (layer F.SilkS) (width 0.1)) + (pad 1 smd rect (at -0.8 0) (size 0.4 1.6) (layers F.Cu F.Paste F.Mask)) + (pad 3 smd rect (at 0.8 0) (size 0.4 1.6) (layers F.Cu F.Paste F.Mask)) + (pad 2 smd rect (at -0.1 0.5) (size 0.3 0.6) (layers F.Cu F.Paste F.Mask)) + (pad 4 smd rect (at 0.1 -0.5) (size 0.3 0.6) (layers F.Cu F.Paste F.Mask)) ) diff --git a/hw/footprints.pretty/TPS630250.kicad_mod b/hw/footprints.pretty/TPS630250.kicad_mod index 04ab7e4..fa94273 100644 --- a/hw/footprints.pretty/TPS630250.kicad_mod +++ b/hw/footprints.pretty/TPS630250.kicad_mod @@ -1,26 +1,26 @@ -(module TPS630250 (layer F.Cu) (tedit 60C962A2) +(module TPS630250 (layer F.Cu) (tedit 62EA46A1) (fp_text reference REF** (at 0 -2.45) (layer F.SilkS) (effects (font (size 1 1) (thickness 0.15))) ) (fp_text value TPS630250 (at 0 2.55) (layer F.Fab) (effects (font (size 1 1) (thickness 0.15))) ) - (fp_line (start -1.5 -1.25) (end -1.5 1.25) (layer F.Fab) (width 0.1)) - (fp_line (start -1.5 1.25) (end 1.5 1.25) (layer F.Fab) (width 0.1)) - (fp_line (start 1.5 1.25) (end 1.5 -0.25) (layer F.Fab) (width 0.1)) - (fp_line (start 0.5 -1.25) (end -1.5 -1.25) (layer F.Fab) (width 0.1)) - (fp_line (start -1.95 -1.7) (end 1.95 -1.7) (layer F.CrtYd) (width 0.05)) - (fp_line (start 1.95 -1.7) (end 1.95 1.7) (layer F.CrtYd) (width 0.05)) - (fp_line (start 1.95 1.7) (end -1.95 1.7) (layer F.CrtYd) (width 0.05)) - (fp_line (start -1.95 1.7) (end -1.95 -1.7) (layer F.CrtYd) (width 0.05)) - (fp_line (start -1.6 -1.35) (end -1.1 -1.35) (layer F.SilkS) (width 0.12)) - (fp_line (start -1.6 1.35) (end -1.1 1.35) (layer F.SilkS) (width 0.12)) - (fp_line (start 1.6 1.35) (end 1.1 1.35) (layer F.SilkS) (width 0.12)) - (fp_circle (center 1.65 -1.4) (end 1.9 -1.4) (layer F.SilkS) (width 0.12)) - (fp_line (start 0.5 -1.25) (end 1.5 -0.25) (layer F.Fab) (width 0.1)) - (fp_line (start 1.6 1.35) (end 1.6 1.15) (layer F.SilkS) (width 0.12)) - (fp_line (start -1.6 1.35) (end -1.6 1.15) (layer F.SilkS) (width 0.12)) (fp_line (start -1.6 -1.15) (end -1.6 -1.35) (layer F.SilkS) (width 0.12)) + (fp_line (start -1.6 1.35) (end -1.6 1.15) (layer F.SilkS) (width 0.12)) + (fp_line (start 1.6 1.35) (end 1.6 1.15) (layer F.SilkS) (width 0.12)) + (fp_line (start 0.5 -1.25) (end 1.5 -0.25) (layer F.Fab) (width 0.1)) + (fp_circle (center 1.95 -1.7) (end 2.2 -1.7) (layer F.SilkS) (width 0.12)) + (fp_line (start 1.6 1.35) (end 1.1 1.35) (layer F.SilkS) (width 0.12)) + (fp_line (start -1.6 1.35) (end -1.1 1.35) (layer F.SilkS) (width 0.12)) + (fp_line (start -1.6 -1.35) (end -1.1 -1.35) (layer F.SilkS) (width 0.12)) + (fp_line (start -1.95 1.7) (end -1.95 -1.7) (layer F.CrtYd) (width 0.05)) + (fp_line (start 1.95 1.7) (end -1.95 1.7) (layer F.CrtYd) (width 0.05)) + (fp_line (start 1.95 -1.7) (end 1.95 1.7) (layer F.CrtYd) (width 0.05)) + (fp_line (start -1.95 -1.7) (end 1.95 -1.7) (layer F.CrtYd) (width 0.05)) + (fp_line (start 0.5 -1.25) (end -1.5 -1.25) (layer F.Fab) (width 0.1)) + (fp_line (start 1.5 1.25) (end 1.5 -0.25) (layer F.Fab) (width 0.1)) + (fp_line (start -1.5 1.25) (end 1.5 1.25) (layer F.Fab) (width 0.1)) + (fp_line (start -1.5 -1.25) (end -1.5 1.25) (layer F.Fab) (width 0.1)) (pad 1 smd rect (at 0.49 -0.555) (size 0.22 1.79) (layers F.Cu F.Paste F.Mask)) (pad 2 smd rect (at 0 -0.555) (size 0.24 1.79) (layers F.Cu F.Paste F.Mask)) (pad 3 smd rect (at -0.49 -0.555) (size 0.22 1.79) (layers F.Cu F.Paste F.Mask)) diff --git a/hw/footprints.pretty/UQFN-10_1.4x1.8mm_P0.4mm.kicad_mod b/hw/footprints.pretty/UQFN-10_1.4x1.8mm_P0.4mm.kicad_mod index 02fda24..076bddb 100644 --- a/hw/footprints.pretty/UQFN-10_1.4x1.8mm_P0.4mm.kicad_mod +++ b/hw/footprints.pretty/UQFN-10_1.4x1.8mm_P0.4mm.kicad_mod @@ -1,13 +1,13 @@ -(module UQFN-10_1.4x1.8mm_P0.4mm (layer F.Cu) (tedit 60C93A2E) +(module UQFN-10_1.4x1.8mm_P0.4mm (layer F.Cu) (tedit 62EA465C) (tags UQFN-10_1.4x1.8mm_P0.4mm) (attr smd) - (fp_text reference REF** (at 0 -2) (layer F.SilkS) + (fp_text reference REF** (at 0 -2.35) (layer F.SilkS) (effects (font (size 1 1) (thickness 0.15))) ) (fp_text value UQFN-10_1.4x1.8mm_P0.4mm (at 0 2.05) (layer F.Fab) (effects (font (size 1 1) (thickness 0.15))) ) - (fp_line (start -0.76 0.96) (end -0.76 0.5) (layer F.SilkS) (width 0.12)) + (fp_line (start -0.8 0.96) (end -0.8 0.5) (layer F.SilkS) (width 0.12)) (fp_line (start -0.2 -0.9) (end -0.7 -0.4) (layer F.Fab) (width 0.1)) (fp_line (start -0.2 -0.9) (end 0.7 -0.9) (layer F.Fab) (width 0.1)) (fp_line (start -0.7 -0.4) (end -0.7 0.9) (layer F.Fab) (width 0.1)) @@ -17,12 +17,12 @@ (fp_line (start 1.1 -1.3) (end -1.1 -1.3) (layer F.CrtYd) (width 0.05)) (fp_line (start -1.1 1.3) (end 1.1 1.3) (layer F.CrtYd) (width 0.05)) (fp_line (start -1.1 1.3) (end -1.1 -1.3) (layer F.CrtYd) (width 0.05)) - (fp_line (start 0.76 -0.5) (end 0.76 -0.96) (layer F.SilkS) (width 0.12)) - (fp_line (start 0.76 0.96) (end 0.76 0.5) (layer F.SilkS) (width 0.12)) + (fp_line (start 0.8 -0.5) (end 0.8 -0.96) (layer F.SilkS) (width 0.12)) + (fp_line (start 0.8 0.96) (end 0.8 0.5) (layer F.SilkS) (width 0.12)) + (fp_circle (center -1.1 -1.3) (end -0.85 -1.3) (layer F.SilkS) (width 0.1)) (fp_text user %R (at 0 0) (layer F.Fab) (effects (font (size 0.5 0.5) (thickness 0.05))) ) - (fp_circle (center -1.1 -1) (end -0.85 -1) (layer F.SilkS) (width 0.1)) (pad 10 smd rect (at -0.4 -0.8) (size 0.2 0.6) (layers F.Cu F.Paste F.Mask)) (pad 1 smd rect (at -0.55 -0.2) (size 0.7 0.2) (layers F.Cu F.Paste F.Mask)) (pad 4 smd rect (at 0 0.8) (size 0.2 0.6) (layers F.Cu F.Paste F.Mask)) diff --git a/hw/footprints.pretty/UQFN-16_1.8x2.6mm_P0.4mm.kicad_mod b/hw/footprints.pretty/UQFN-16_1.8x2.6mm_P0.4mm.kicad_mod index 6c91cf4..1bf18d7 100644 --- a/hw/footprints.pretty/UQFN-16_1.8x2.6mm_P0.4mm.kicad_mod +++ b/hw/footprints.pretty/UQFN-16_1.8x2.6mm_P0.4mm.kicad_mod @@ -1,4 +1,4 @@ -(module UQFN-16_1.8x2.6mm_P0.4mm (layer F.Cu) (tedit 5FE4B4AE) +(module UQFN-16_1.8x2.6mm_P0.4mm (layer F.Cu) (tedit 62EA463B) (fp_text reference REF** (at 0 -3) (layer F.SilkS) (effects (font (size 1 1) (thickness 0.15))) ) @@ -17,7 +17,7 @@ (fp_line (start -1 0.9) (end -1 1.4) (layer F.SilkS) (width 0.12)) (fp_line (start 1 1.4) (end 1 0.9) (layer F.SilkS) (width 0.12)) (fp_line (start 1 -1.4) (end 1 -0.9) (layer F.SilkS) (width 0.12)) - (fp_circle (center -1.1 -1.5) (end -0.85 -1.5) (layer F.SilkS) (width 0.1)) + (fp_circle (center -1.35 -1.75) (end -1.1 -1.75) (layer F.SilkS) (width 0.1)) (pad 1 smd roundrect (at -0.75 -0.6) (size 0.7 0.2) (layers F.Cu F.Paste F.Mask) (roundrect_rratio 0.25)) (pad 2 smd roundrect (at -0.8 -0.2) (size 0.6 0.2) (layers F.Cu F.Paste F.Mask) (roundrect_rratio 0.25)) (pad 3 smd roundrect (at -0.8 0.2) (size 0.6 0.2) (layers F.Cu F.Paste F.Mask) (roundrect_rratio 0.25)) |