mirror of
https://github.com/zhaojh329/rtty.git
synced 2026-02-27 09:53:17 +08:00
protobuf: doing...
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
This commit is contained in:
+6
-9
@@ -1,12 +1,5 @@
|
||||
syntax = "proto3";
|
||||
|
||||
message rtty_message_data {
|
||||
int32 code = 1;
|
||||
bytes data = 2;
|
||||
string name = 3;
|
||||
uint32 size = 4;
|
||||
}
|
||||
|
||||
message rtty_message {
|
||||
enum Type {
|
||||
UNKNOWN = 0;
|
||||
@@ -20,7 +13,11 @@ message rtty_message {
|
||||
}
|
||||
|
||||
uint32 version = 1;
|
||||
uint32 type = 2;
|
||||
Type type = 2;
|
||||
string sid = 3;
|
||||
rtty_message_data data = 4;
|
||||
|
||||
int32 code = 4;
|
||||
bytes data = 5;
|
||||
string name = 6;
|
||||
uint32 size = 7;
|
||||
}
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ find_package(Libprotobuf-c REQUIRED)
|
||||
include_directories(${CMAKE_CURRENT_BINARY_DIR} ${LIBUWSC_INCLUDE_DIR} ${LIBUBOX_INCLUDE_DIR} ${LIBPROTOBUFC_INCLUDE_DIR})
|
||||
set(EXTRA_LIBS ${LIBUWSC_LIBRARY} ${LIBUBOX_LIBRARY} ${LIBPROTOBUFC_LIBRARY} blobmsg_json util crypt)
|
||||
|
||||
add_executable(rtty main.c utils.c protocol.c command.c rtty.pb-c.c)
|
||||
add_executable(rtty main.c utils.c command.c rtty.pb-c.c)
|
||||
target_link_libraries(rtty ${EXTRA_LIBS})
|
||||
|
||||
# configure a header file to pass some of the CMake settings to the source code
|
||||
|
||||
+117
-63
@@ -35,7 +35,6 @@
|
||||
|
||||
#include "config.h"
|
||||
#include "utils.h"
|
||||
#include "protocol.h"
|
||||
#include "command.h"
|
||||
#include "rtty.pb-c.h"
|
||||
|
||||
@@ -65,7 +64,6 @@ static char server_url[512];
|
||||
static bool auto_reconnect;
|
||||
static int ping_interval;
|
||||
static struct uloop_timeout reconnect_timer;
|
||||
static struct rtty_packet *pkt;
|
||||
static struct avl_tree tty_sessions;
|
||||
|
||||
/* For execute command */
|
||||
@@ -98,11 +96,6 @@ static void del_tty_session(struct tty_session *tty)
|
||||
close(tty->upfile.fd);
|
||||
ULOG_INFO("Del session:%s\n", tty->sid);
|
||||
free(tty);
|
||||
|
||||
if (avl_is_empty(&tty_sessions)) {
|
||||
rtty_packet_free(pkt);
|
||||
pkt = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static inline struct tty_session *find_tty_session(const char *sid)
|
||||
@@ -123,48 +116,70 @@ static void pty_read_cb(struct ustream *s, int bytes)
|
||||
{
|
||||
struct tty_session *tty = container_of(s, struct tty_session, sfd.stream);
|
||||
struct uwsc_client *cl = tty->cl;
|
||||
void *data;
|
||||
int len;
|
||||
RttyMessage msg = RTTY_MESSAGE__INIT;
|
||||
void *data, *buf;
|
||||
int len, msg_len;
|
||||
|
||||
data = ustream_get_read_buf(s, &len);
|
||||
if (!data || len == 0)
|
||||
return;
|
||||
|
||||
rtty_packet_init(pkt, RTTY_PACKET_TTY);
|
||||
rtty_attr_put_string(pkt, RTTY_ATTR_SID, tty->sid);
|
||||
rtty_attr_put(pkt, RTTY_ATTR_DATA, len, data);
|
||||
ustream_consume(s, len);
|
||||
msg.has_version = true;
|
||||
msg.version = 2;
|
||||
|
||||
cl->send(cl, pkt->data, pkt->len, WEBSOCKET_OP_BINARY);
|
||||
msg.has_type = true;
|
||||
msg.type = RTTY_MESSAGE__TYPE__TTY;
|
||||
|
||||
msg.sid = tty->sid;
|
||||
|
||||
msg.has_data = true;
|
||||
msg.data.data = data;
|
||||
msg.data.len = len;
|
||||
|
||||
msg_len = rtty_message__get_packed_size(&msg);
|
||||
buf = malloc(msg_len);
|
||||
rtty_message__pack(&msg, buf);
|
||||
|
||||
cl->send(cl, buf, msg_len, WEBSOCKET_OP_BINARY);
|
||||
|
||||
free(buf);
|
||||
|
||||
ustream_consume(s, len);
|
||||
}
|
||||
|
||||
static void pty_on_exit(struct uloop_process *p, int ret)
|
||||
{
|
||||
struct tty_session *tty = container_of(p, struct tty_session, up);
|
||||
struct uwsc_client *cl = tty->cl;
|
||||
RttyMessage msg = RTTY_MESSAGE__INIT;
|
||||
int len;
|
||||
void *out;
|
||||
|
||||
rtty_packet_init(pkt, RTTY_PACKET_LOGOUT);
|
||||
rtty_attr_put_string(pkt, RTTY_ATTR_SID, tty->sid);
|
||||
msg.has_version = true;
|
||||
msg.version = 2;
|
||||
|
||||
cl->send(cl, pkt->data, pkt->len, WEBSOCKET_OP_BINARY);
|
||||
msg.has_type = true;
|
||||
msg.type = RTTY_MESSAGE__TYPE__LOGOUT;
|
||||
|
||||
msg.sid = tty->sid;
|
||||
|
||||
len = rtty_message__get_packed_size(&msg);
|
||||
out = malloc(len);
|
||||
rtty_message__pack(&msg, out);
|
||||
|
||||
cl->send(cl, out, len, WEBSOCKET_OP_BINARY);
|
||||
|
||||
free(out);
|
||||
|
||||
del_tty_session(tty);
|
||||
}
|
||||
|
||||
static void new_tty_session(struct uwsc_client *cl, struct rtty_packet_info *pi)
|
||||
static void new_tty_session(struct uwsc_client *cl, RttyMessage *pi)
|
||||
{
|
||||
struct tty_session *s;
|
||||
int pty;
|
||||
pid_t pid;
|
||||
|
||||
if (!pkt) {
|
||||
pkt = rtty_packet_new(sizeof(struct rtty_packet) + 8192);
|
||||
if (!pkt) {
|
||||
ULOG_ERR("rtty_packet_new failed\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
s = calloc(1, sizeof(struct tty_session));
|
||||
if (!s)
|
||||
return;
|
||||
@@ -191,19 +206,21 @@ static void new_tty_session(struct uwsc_client *cl, struct rtty_packet_info *pi)
|
||||
ULOG_INFO("New session:%s\n", pi->sid);
|
||||
}
|
||||
|
||||
static void pty_write(struct rtty_packet_info *pi)
|
||||
static void pty_write(RttyMessage *pi)
|
||||
{
|
||||
struct tty_session *tty = find_tty_session(pi->sid);
|
||||
ProtobufCBinaryData *data = &pi->data;
|
||||
|
||||
if (tty && write(tty->pty, pi->data, pi->data_len) < 0) {
|
||||
if (tty && write(tty->pty, data->data, data->len) < 0) {
|
||||
ULOG_ERR("write to pty error:%s\n", strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_upfile(struct rtty_packet_info *pi)
|
||||
static void handle_upfile(RttyMessage *pi)
|
||||
{
|
||||
struct tty_session *tty = find_tty_session(pi->sid);
|
||||
struct upfile_info *upfile;
|
||||
ProtobufCBinaryData *data = &pi->data;
|
||||
|
||||
if (!tty)
|
||||
return;
|
||||
@@ -236,15 +253,15 @@ static void handle_upfile(struct rtty_packet_info *pi)
|
||||
}
|
||||
|
||||
if (upfile->fd > 0) {
|
||||
if (pi->code == 1 && pi->data && pi->data_len) {
|
||||
if (write(upfile->fd, pi->data, pi->data_len) < 0) {
|
||||
if (pi->code == 1 && data && data->len) {
|
||||
if (write(upfile->fd, data->data, data->len) < 0) {
|
||||
ULOG_ERR("upfile failed:%s\n", strerror(errno));
|
||||
close(upfile->fd);
|
||||
upfile->fd = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
upfile->uploaded += pi->data_len;
|
||||
upfile->uploaded += data->len;
|
||||
}
|
||||
|
||||
if (pi->code == 2 || upfile->uploaded == upfile->size) {
|
||||
@@ -263,6 +280,9 @@ static void send_filelist(struct uwsc_client *cl, const char *sid, const char *p
|
||||
void *tbl, *array;
|
||||
char *str;
|
||||
char buf[512];
|
||||
int len;
|
||||
void *out;
|
||||
RttyMessage msg = RTTY_MESSAGE__INIT;
|
||||
|
||||
dir = opendir(path);
|
||||
if (!dir)
|
||||
@@ -314,10 +334,25 @@ static void send_filelist(struct uwsc_client *cl, const char *sid, const char *p
|
||||
|
||||
str = blobmsg_format_json(b.head, true);
|
||||
|
||||
rtty_packet_init(pkt, RTTY_PACKET_DOWNFILE);
|
||||
rtty_attr_put_string(pkt, RTTY_ATTR_SID, sid);
|
||||
rtty_attr_put(pkt, RTTY_ATTR_DATA, strlen(str) - 2, str + 1);
|
||||
cl->send(cl, pkt->data, pkt->len, WEBSOCKET_OP_BINARY);
|
||||
msg.has_version = true;
|
||||
msg.version = 2;
|
||||
|
||||
msg.has_type = true;
|
||||
msg.type = RTTY_MESSAGE__TYPE__DOWNFILE;
|
||||
|
||||
msg.sid = (char *)sid;
|
||||
|
||||
msg.has_data = true;
|
||||
msg.data.data = (void *)(str + 1);
|
||||
msg.data.len = strlen(str) - 2;
|
||||
|
||||
len = rtty_message__get_packed_size(&msg);
|
||||
out = malloc(len);
|
||||
rtty_message__pack(&msg, out);
|
||||
|
||||
cl->send(cl, out, len, WEBSOCKET_OP_BINARY);
|
||||
|
||||
free(out);
|
||||
|
||||
free(str);
|
||||
blob_buf_free(&b);
|
||||
@@ -326,30 +361,52 @@ static void send_filelist(struct uwsc_client *cl, const char *sid, const char *p
|
||||
static void send_file_cb(struct uloop_timeout *timer)
|
||||
{
|
||||
struct tty_session *tty = container_of(timer, struct tty_session, timer);
|
||||
RttyMessage msg = RTTY_MESSAGE__INIT;
|
||||
|
||||
if (tty->downfile > 0) {
|
||||
int len;
|
||||
char buf[4096];
|
||||
void *buf2;
|
||||
|
||||
rtty_packet_init(pkt, RTTY_PACKET_DOWNFILE);
|
||||
rtty_attr_put_string(pkt, RTTY_ATTR_SID, tty->sid);
|
||||
msg.has_version = true;
|
||||
msg.version = 2;
|
||||
|
||||
msg.has_type = true;
|
||||
msg.type = RTTY_MESSAGE__TYPE__DOWNFILE;
|
||||
|
||||
msg.sid = tty->sid;
|
||||
|
||||
len = read(tty->downfile, buf, sizeof(buf));
|
||||
if (len > 0) {
|
||||
rtty_attr_put_u8(pkt, RTTY_ATTR_CODE, 1);
|
||||
rtty_attr_put(pkt, RTTY_ATTR_DATA, len, buf);
|
||||
msg.has_code = true;
|
||||
msg.code = 1;
|
||||
|
||||
msg.has_data = true;
|
||||
msg.data.data = (void *)buf;
|
||||
msg.data.len = len;
|
||||
|
||||
uloop_timeout_set(timer, 5);
|
||||
} else {
|
||||
close(tty->downfile);
|
||||
tty->downfile = 0;
|
||||
rtty_attr_put_u8(pkt, RTTY_ATTR_CODE, 2);
|
||||
|
||||
msg.has_code = true;
|
||||
msg.code = 2;
|
||||
|
||||
ULOG_INFO("Down file finish\n");
|
||||
}
|
||||
tty->cl->send(tty->cl, pkt->data, pkt->len, WEBSOCKET_OP_BINARY);
|
||||
|
||||
len = rtty_message__get_packed_size(&msg);
|
||||
buf2 = malloc(len);
|
||||
rtty_message__pack(&msg, buf2);
|
||||
|
||||
tty->cl->send(tty->cl, buf2, len, WEBSOCKET_OP_BINARY);
|
||||
|
||||
free(buf2);
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_downfile(struct uwsc_client *cl, struct rtty_packet_info *pi)
|
||||
static void handle_downfile(struct uwsc_client *cl, RttyMessage *pi)
|
||||
{
|
||||
struct stat st;
|
||||
const char *name = "/";
|
||||
@@ -416,7 +473,7 @@ static const struct blobmsg_policy cmd_policy[] = {
|
||||
|
||||
static void uwsc_onmessage(struct uwsc_client *cl, void *msg, uint64_t len, enum websocket_op op)
|
||||
{
|
||||
struct rtty_packet_info pi;
|
||||
RttyMessage *pkt;
|
||||
|
||||
if (op == WEBSOCKET_OP_TEXT) {
|
||||
static struct blob_buf b;
|
||||
@@ -448,34 +505,36 @@ static void uwsc_onmessage(struct uwsc_client *cl, void *msg, uint64_t len, enum
|
||||
return;
|
||||
}
|
||||
|
||||
rtty_packet_parse(msg, len, &pi);
|
||||
pkt = rtty_message__unpack(NULL, len, msg);
|
||||
|
||||
switch (pi.type) {
|
||||
case RTTY_PACKET_LOGIN:
|
||||
new_tty_session(cl, &pi);
|
||||
switch (pkt->type) {
|
||||
case RTTY_MESSAGE__TYPE__LOGIN:
|
||||
new_tty_session(cl, pkt);
|
||||
break;
|
||||
case RTTY_PACKET_LOGOUT: {
|
||||
del_tty_session_by_sid(pi.sid);
|
||||
case RTTY_MESSAGE__TYPE__LOGOUT: {
|
||||
del_tty_session_by_sid(pkt->sid);
|
||||
break;
|
||||
}
|
||||
case RTTY_PACKET_TTY:
|
||||
pty_write(&pi);
|
||||
case RTTY_MESSAGE__TYPE__TTY:
|
||||
pty_write(pkt);
|
||||
break;
|
||||
case RTTY_PACKET_ANNOUNCE:
|
||||
if (pi.code) {
|
||||
case RTTY_MESSAGE__TYPE__ANNOUNCE:
|
||||
if (pkt->code) {
|
||||
auto_reconnect = false;
|
||||
ULOG_ERR("register failed: ID conflicting\n");
|
||||
uloop_end();
|
||||
}
|
||||
case RTTY_PACKET_UPFILE:
|
||||
handle_upfile(&pi);
|
||||
case RTTY_MESSAGE__TYPE__UPFILE:
|
||||
handle_upfile(pkt);
|
||||
break;
|
||||
case RTTY_PACKET_DOWNFILE:
|
||||
handle_downfile(cl, &pi);
|
||||
case RTTY_MESSAGE__TYPE__DOWNFILE:
|
||||
handle_downfile(cl, pkt);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
rtty_message__free_unpacked(pkt, NULL);
|
||||
}
|
||||
|
||||
static void uwsc_onopen(struct uwsc_client *cl)
|
||||
@@ -496,11 +555,6 @@ static void uwsc_onclose(struct uwsc_client *cl)
|
||||
|
||||
ULOG_ERR("onclose\n");
|
||||
|
||||
if (pkt) {
|
||||
rtty_packet_free(pkt);
|
||||
pkt = NULL;
|
||||
}
|
||||
|
||||
avl_for_each_element_safe(&tty_sessions, tty, avl, tmp)
|
||||
del_tty_session(tty);
|
||||
|
||||
|
||||
-163
@@ -1,163 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Jianhui Zhao <jianhuizhao329@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
||||
* USA
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <libubox/ulog.h>
|
||||
#include <libubox/utils.h>
|
||||
|
||||
#include "protocol.h"
|
||||
|
||||
struct rtty_packet *rtty_packet_new(int size)
|
||||
{
|
||||
struct rtty_packet *pkt = calloc(1, sizeof(struct rtty_packet));
|
||||
if (!pkt) {
|
||||
ULOG_ERR("malloc failed:%s\n", strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pkt->data = malloc(size);
|
||||
if (!pkt->data) {
|
||||
free(pkt);
|
||||
ULOG_ERR("malloc failed:%s\n", strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pkt->size = size;
|
||||
|
||||
return pkt;
|
||||
}
|
||||
|
||||
int rtty_packet_grow(struct rtty_packet *pkt, int size)
|
||||
{
|
||||
uint8_t *data = realloc(pkt->data, pkt->size + size);
|
||||
if (!data) {
|
||||
ULOG_ERR("rtty_packet_grow failed:%s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
pkt->data = data;
|
||||
pkt->size += size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void rtty_packet_free(struct rtty_packet *pkt)
|
||||
{
|
||||
if (pkt) {
|
||||
if (pkt->data)
|
||||
free(pkt->data);
|
||||
free(pkt);
|
||||
}
|
||||
}
|
||||
|
||||
int rtty_packet_init(struct rtty_packet *pkt, enum rtty_packet_type type)
|
||||
{
|
||||
uint8_t *data = pkt->data;;
|
||||
|
||||
if (pkt->size < 2 || !data) {
|
||||
ULOG_ERR("rtty_packet_init failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
data[0] = RTTY_PROTOCOL_VERSION;
|
||||
data[1] = type;
|
||||
pkt->len = 2;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rtty_attr_put(struct rtty_packet *pkt, enum rtty_attr_type type, uint16_t len, const void *data)
|
||||
{
|
||||
uint8_t *p = pkt->data + pkt->len;
|
||||
|
||||
if (pkt->len + len + 3 > pkt->size) {
|
||||
if (rtty_packet_grow(pkt, len) < 0) {
|
||||
ULOG_ERR("rtty_packet_grow failed\n");
|
||||
return -1;
|
||||
}
|
||||
p = pkt->data + pkt->len;
|
||||
}
|
||||
|
||||
pkt->len += 3 + len;
|
||||
|
||||
p[0] = type;
|
||||
|
||||
memcpy(p + 3, data, len);
|
||||
|
||||
len = htons(len);
|
||||
memcpy(p + 1, &len, sizeof(len));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rtty_packet_parse(const uint8_t *data, int pktlen, struct rtty_packet_info *pi)
|
||||
{
|
||||
const uint8_t *p;
|
||||
|
||||
memset(pi, 0, sizeof(struct rtty_packet_info));
|
||||
|
||||
pi->version = data[0];
|
||||
pi->type = data[1];
|
||||
|
||||
p = data + 2;
|
||||
|
||||
while(p < data + pktlen) {
|
||||
uint8_t type;
|
||||
uint16_t len;
|
||||
|
||||
type = p[0];
|
||||
memcpy(&len, p + 1, 2);
|
||||
|
||||
len = ntohs(len);
|
||||
|
||||
p += 3;
|
||||
|
||||
/* Check if len is invalid */
|
||||
if (p + len > data + pktlen)
|
||||
break;
|
||||
|
||||
switch (type) {
|
||||
case RTTY_ATTR_SID:
|
||||
pi->sid = (const char *)p;
|
||||
break;
|
||||
case RTTY_ATTR_DATA:
|
||||
pi->data = p;
|
||||
pi->data_len = len;
|
||||
break;
|
||||
case RTTY_ATTR_CODE:
|
||||
pi->code = *p;
|
||||
break;
|
||||
case RTTY_ATTR_NAME:
|
||||
pi->name = (const char *)p;
|
||||
break;
|
||||
case RTTY_ATTR_SIZE:
|
||||
memcpy(&pi->size, p, 4);
|
||||
pi->size = ntohl(pi->size);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
p += len;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
-100
@@ -1,100 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Jianhui Zhao <jianhuizhao329@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
||||
* USA
|
||||
*/
|
||||
|
||||
#ifndef _H_PROTOCOL
|
||||
#define _H_PROTOCOL
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#define RTTY_PROTOCOL_VERSION 1
|
||||
|
||||
#define RTTY_CLIENT_TYPE_DEVICE 0
|
||||
#define RTTY_CLIENT_TYPE_BROWSE 1
|
||||
|
||||
/* rtty packet type */
|
||||
enum rtty_packet_type {
|
||||
RTTY_PACKET_LOGIN = 1,
|
||||
RTTY_PACKET_LOGINACK,
|
||||
RTTY_PACKET_LOGOUT,
|
||||
RTTY_PACKET_TTY,
|
||||
RTTY_PACKET_ANNOUNCE,
|
||||
RTTY_PACKET_UPFILE,
|
||||
RTTY_PACKET_DOWNFILE
|
||||
};
|
||||
|
||||
/* rtty attribute type */
|
||||
enum rtty_attr_type {
|
||||
RTTY_ATTR_SID = 1,
|
||||
RTTY_ATTR_CODE,
|
||||
RTTY_ATTR_DATA,
|
||||
RTTY_ATTR_NAME,
|
||||
RTTY_ATTR_SIZE
|
||||
};
|
||||
|
||||
struct rtty_packet {
|
||||
uint16_t size;
|
||||
uint32_t len;
|
||||
uint8_t *data;
|
||||
};
|
||||
|
||||
struct rtty_packet_info {
|
||||
uint8_t version;
|
||||
uint8_t type;
|
||||
uint8_t code;
|
||||
const char *sid;
|
||||
const uint8_t *data;
|
||||
uint16_t data_len;
|
||||
const char *name;
|
||||
uint32_t size;
|
||||
};
|
||||
|
||||
struct rtty_packet *rtty_packet_new(int size);
|
||||
void rtty_packet_free(struct rtty_packet *pkt);
|
||||
int rtty_packet_grow(struct rtty_packet *pkt, int size);
|
||||
int rtty_packet_init(struct rtty_packet *pkt, enum rtty_packet_type type);
|
||||
|
||||
int rtty_attr_put(struct rtty_packet *pkt, enum rtty_attr_type type, uint16_t len, const void *data);
|
||||
|
||||
static inline int rtty_attr_put_u8(struct rtty_packet *pkt, enum rtty_attr_type type, uint8_t val)
|
||||
{
|
||||
return rtty_attr_put(pkt, type, sizeof(uint8_t), &val);
|
||||
}
|
||||
|
||||
static inline int rtty_attr_put_u16(struct rtty_packet *pkt, enum rtty_attr_type type, uint16_t val)
|
||||
{
|
||||
val = htons(val);
|
||||
return rtty_attr_put(pkt, type, sizeof(uint16_t), &val);
|
||||
}
|
||||
|
||||
static inline int rtty_attr_put_u32(struct rtty_packet *pkt, enum rtty_attr_type type, uint32_t val)
|
||||
{
|
||||
val = htonl(val);
|
||||
return rtty_attr_put(pkt, type, sizeof(uint32_t), &val);
|
||||
}
|
||||
|
||||
static inline int rtty_attr_put_string(struct rtty_packet *pkt, enum rtty_attr_type type, const char *str)
|
||||
{
|
||||
return rtty_attr_put(pkt, type, strlen(str) + 1, str);
|
||||
}
|
||||
|
||||
int rtty_packet_parse(const uint8_t *data, int len, struct rtty_packet_info *pi);
|
||||
|
||||
#endif
|
||||
+49
-130
@@ -7,49 +7,6 @@
|
||||
#endif
|
||||
|
||||
#include "rtty.pb-c.h"
|
||||
void rtty_message_data__init
|
||||
(RttyMessageData *message)
|
||||
{
|
||||
static RttyMessageData init_value = RTTY_MESSAGE_DATA__INIT;
|
||||
*message = init_value;
|
||||
}
|
||||
size_t rtty_message_data__get_packed_size
|
||||
(const RttyMessageData *message)
|
||||
{
|
||||
assert(message->base.descriptor == &rtty_message_data__descriptor);
|
||||
return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message));
|
||||
}
|
||||
size_t rtty_message_data__pack
|
||||
(const RttyMessageData *message,
|
||||
uint8_t *out)
|
||||
{
|
||||
assert(message->base.descriptor == &rtty_message_data__descriptor);
|
||||
return protobuf_c_message_pack ((const ProtobufCMessage*)message, out);
|
||||
}
|
||||
size_t rtty_message_data__pack_to_buffer
|
||||
(const RttyMessageData *message,
|
||||
ProtobufCBuffer *buffer)
|
||||
{
|
||||
assert(message->base.descriptor == &rtty_message_data__descriptor);
|
||||
return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer);
|
||||
}
|
||||
RttyMessageData *
|
||||
rtty_message_data__unpack
|
||||
(ProtobufCAllocator *allocator,
|
||||
size_t len,
|
||||
const uint8_t *data)
|
||||
{
|
||||
return (RttyMessageData *)
|
||||
protobuf_c_message_unpack (&rtty_message_data__descriptor,
|
||||
allocator, len, data);
|
||||
}
|
||||
void rtty_message_data__free_unpacked
|
||||
(RttyMessageData *message,
|
||||
ProtobufCAllocator *allocator)
|
||||
{
|
||||
assert(message->base.descriptor == &rtty_message_data__descriptor);
|
||||
protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator);
|
||||
}
|
||||
void rtty_message__init
|
||||
(RttyMessage *message)
|
||||
{
|
||||
@@ -93,83 +50,6 @@ void rtty_message__free_unpacked
|
||||
assert(message->base.descriptor == &rtty_message__descriptor);
|
||||
protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator);
|
||||
}
|
||||
static const ProtobufCFieldDescriptor rtty_message_data__field_descriptors[4] =
|
||||
{
|
||||
{
|
||||
"code",
|
||||
1,
|
||||
PROTOBUF_C_LABEL_OPTIONAL,
|
||||
PROTOBUF_C_TYPE_INT32,
|
||||
offsetof(RttyMessageData, has_code),
|
||||
offsetof(RttyMessageData, code),
|
||||
NULL,
|
||||
NULL,
|
||||
0, /* flags */
|
||||
0,NULL,NULL /* reserved1,reserved2, etc */
|
||||
},
|
||||
{
|
||||
"data",
|
||||
2,
|
||||
PROTOBUF_C_LABEL_OPTIONAL,
|
||||
PROTOBUF_C_TYPE_BYTES,
|
||||
offsetof(RttyMessageData, has_data),
|
||||
offsetof(RttyMessageData, data),
|
||||
NULL,
|
||||
NULL,
|
||||
0, /* flags */
|
||||
0,NULL,NULL /* reserved1,reserved2, etc */
|
||||
},
|
||||
{
|
||||
"name",
|
||||
3,
|
||||
PROTOBUF_C_LABEL_OPTIONAL,
|
||||
PROTOBUF_C_TYPE_STRING,
|
||||
0, /* quantifier_offset */
|
||||
offsetof(RttyMessageData, name),
|
||||
NULL,
|
||||
NULL,
|
||||
0, /* flags */
|
||||
0,NULL,NULL /* reserved1,reserved2, etc */
|
||||
},
|
||||
{
|
||||
"size",
|
||||
4,
|
||||
PROTOBUF_C_LABEL_OPTIONAL,
|
||||
PROTOBUF_C_TYPE_UINT32,
|
||||
offsetof(RttyMessageData, has_size),
|
||||
offsetof(RttyMessageData, size),
|
||||
NULL,
|
||||
NULL,
|
||||
0, /* flags */
|
||||
0,NULL,NULL /* reserved1,reserved2, etc */
|
||||
},
|
||||
};
|
||||
static const unsigned rtty_message_data__field_indices_by_name[] = {
|
||||
0, /* field[0] = code */
|
||||
1, /* field[1] = data */
|
||||
2, /* field[2] = name */
|
||||
3, /* field[3] = size */
|
||||
};
|
||||
static const ProtobufCIntRange rtty_message_data__number_ranges[1 + 1] =
|
||||
{
|
||||
{ 1, 0 },
|
||||
{ 0, 4 }
|
||||
};
|
||||
const ProtobufCMessageDescriptor rtty_message_data__descriptor =
|
||||
{
|
||||
PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC,
|
||||
"rtty_message_data",
|
||||
"RttyMessageData",
|
||||
"RttyMessageData",
|
||||
"",
|
||||
sizeof(RttyMessageData),
|
||||
4,
|
||||
rtty_message_data__field_descriptors,
|
||||
rtty_message_data__field_indices_by_name,
|
||||
1, rtty_message_data__number_ranges,
|
||||
(ProtobufCMessageInit) rtty_message_data__init,
|
||||
NULL,NULL,NULL /* reserved[123] */
|
||||
};
|
||||
static const ProtobufCEnumValue rtty_message__type__enum_values_by_number[8] =
|
||||
{
|
||||
{ "UNKNOWN", "RTTY_MESSAGE__TYPE__UNKNOWN", 0 },
|
||||
@@ -210,7 +90,7 @@ const ProtobufCEnumDescriptor rtty_message__type__descriptor =
|
||||
rtty_message__type__value_ranges,
|
||||
NULL,NULL,NULL,NULL /* reserved[1234] */
|
||||
};
|
||||
static const ProtobufCFieldDescriptor rtty_message__field_descriptors[4] =
|
||||
static const ProtobufCFieldDescriptor rtty_message__field_descriptors[7] =
|
||||
{
|
||||
{
|
||||
"version",
|
||||
@@ -228,10 +108,10 @@ static const ProtobufCFieldDescriptor rtty_message__field_descriptors[4] =
|
||||
"type",
|
||||
2,
|
||||
PROTOBUF_C_LABEL_OPTIONAL,
|
||||
PROTOBUF_C_TYPE_UINT32,
|
||||
PROTOBUF_C_TYPE_ENUM,
|
||||
offsetof(RttyMessage, has_type),
|
||||
offsetof(RttyMessage, type),
|
||||
NULL,
|
||||
&rtty_message__type__descriptor,
|
||||
NULL,
|
||||
0, /* flags */
|
||||
0,NULL,NULL /* reserved1,reserved2, etc */
|
||||
@@ -249,28 +129,67 @@ static const ProtobufCFieldDescriptor rtty_message__field_descriptors[4] =
|
||||
0,NULL,NULL /* reserved1,reserved2, etc */
|
||||
},
|
||||
{
|
||||
"data",
|
||||
"code",
|
||||
4,
|
||||
PROTOBUF_C_LABEL_OPTIONAL,
|
||||
PROTOBUF_C_TYPE_MESSAGE,
|
||||
0, /* quantifier_offset */
|
||||
PROTOBUF_C_TYPE_INT32,
|
||||
offsetof(RttyMessage, has_code),
|
||||
offsetof(RttyMessage, code),
|
||||
NULL,
|
||||
NULL,
|
||||
0, /* flags */
|
||||
0,NULL,NULL /* reserved1,reserved2, etc */
|
||||
},
|
||||
{
|
||||
"data",
|
||||
5,
|
||||
PROTOBUF_C_LABEL_OPTIONAL,
|
||||
PROTOBUF_C_TYPE_BYTES,
|
||||
offsetof(RttyMessage, has_data),
|
||||
offsetof(RttyMessage, data),
|
||||
&rtty_message_data__descriptor,
|
||||
NULL,
|
||||
NULL,
|
||||
0, /* flags */
|
||||
0,NULL,NULL /* reserved1,reserved2, etc */
|
||||
},
|
||||
{
|
||||
"name",
|
||||
6,
|
||||
PROTOBUF_C_LABEL_OPTIONAL,
|
||||
PROTOBUF_C_TYPE_STRING,
|
||||
0, /* quantifier_offset */
|
||||
offsetof(RttyMessage, name),
|
||||
NULL,
|
||||
NULL,
|
||||
0, /* flags */
|
||||
0,NULL,NULL /* reserved1,reserved2, etc */
|
||||
},
|
||||
{
|
||||
"size",
|
||||
7,
|
||||
PROTOBUF_C_LABEL_OPTIONAL,
|
||||
PROTOBUF_C_TYPE_UINT32,
|
||||
offsetof(RttyMessage, has_size),
|
||||
offsetof(RttyMessage, size),
|
||||
NULL,
|
||||
NULL,
|
||||
0, /* flags */
|
||||
0,NULL,NULL /* reserved1,reserved2, etc */
|
||||
},
|
||||
};
|
||||
static const unsigned rtty_message__field_indices_by_name[] = {
|
||||
3, /* field[3] = data */
|
||||
3, /* field[3] = code */
|
||||
4, /* field[4] = data */
|
||||
5, /* field[5] = name */
|
||||
2, /* field[2] = sid */
|
||||
6, /* field[6] = size */
|
||||
1, /* field[1] = type */
|
||||
0, /* field[0] = version */
|
||||
};
|
||||
static const ProtobufCIntRange rtty_message__number_ranges[1 + 1] =
|
||||
{
|
||||
{ 1, 0 },
|
||||
{ 0, 4 }
|
||||
{ 0, 7 }
|
||||
};
|
||||
const ProtobufCMessageDescriptor rtty_message__descriptor =
|
||||
{
|
||||
@@ -280,7 +199,7 @@ const ProtobufCMessageDescriptor rtty_message__descriptor =
|
||||
"RttyMessage",
|
||||
"",
|
||||
sizeof(RttyMessage),
|
||||
4,
|
||||
7,
|
||||
rtty_message__field_descriptors,
|
||||
rtty_message__field_indices_by_name,
|
||||
1, rtty_message__number_ranges,
|
||||
|
||||
+7
-41
@@ -15,7 +15,6 @@ PROTOBUF_C__BEGIN_DECLS
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct _RttyMessageData RttyMessageData;
|
||||
typedef struct _RttyMessage RttyMessage;
|
||||
|
||||
|
||||
@@ -35,9 +34,14 @@ typedef enum _RttyMessage__Type {
|
||||
|
||||
/* --- messages --- */
|
||||
|
||||
struct _RttyMessageData
|
||||
struct _RttyMessage
|
||||
{
|
||||
ProtobufCMessage base;
|
||||
protobuf_c_boolean has_version;
|
||||
uint32_t version;
|
||||
protobuf_c_boolean has_type;
|
||||
RttyMessage__Type type;
|
||||
char *sid;
|
||||
protobuf_c_boolean has_code;
|
||||
int32_t code;
|
||||
protobuf_c_boolean has_data;
|
||||
@@ -46,45 +50,11 @@ struct _RttyMessageData
|
||||
protobuf_c_boolean has_size;
|
||||
uint32_t size;
|
||||
};
|
||||
#define RTTY_MESSAGE_DATA__INIT \
|
||||
{ PROTOBUF_C_MESSAGE_INIT (&rtty_message_data__descriptor) \
|
||||
, 0,0, 0,{0,NULL}, NULL, 0,0 }
|
||||
|
||||
|
||||
struct _RttyMessage
|
||||
{
|
||||
ProtobufCMessage base;
|
||||
protobuf_c_boolean has_version;
|
||||
uint32_t version;
|
||||
protobuf_c_boolean has_type;
|
||||
uint32_t type;
|
||||
char *sid;
|
||||
RttyMessageData *data;
|
||||
};
|
||||
#define RTTY_MESSAGE__INIT \
|
||||
{ PROTOBUF_C_MESSAGE_INIT (&rtty_message__descriptor) \
|
||||
, 0,0, 0,0, NULL, NULL }
|
||||
, 0,0, 0,0, NULL, 0,0, 0,{0,NULL}, NULL, 0,0 }
|
||||
|
||||
|
||||
/* RttyMessageData methods */
|
||||
void rtty_message_data__init
|
||||
(RttyMessageData *message);
|
||||
size_t rtty_message_data__get_packed_size
|
||||
(const RttyMessageData *message);
|
||||
size_t rtty_message_data__pack
|
||||
(const RttyMessageData *message,
|
||||
uint8_t *out);
|
||||
size_t rtty_message_data__pack_to_buffer
|
||||
(const RttyMessageData *message,
|
||||
ProtobufCBuffer *buffer);
|
||||
RttyMessageData *
|
||||
rtty_message_data__unpack
|
||||
(ProtobufCAllocator *allocator,
|
||||
size_t len,
|
||||
const uint8_t *data);
|
||||
void rtty_message_data__free_unpacked
|
||||
(RttyMessageData *message,
|
||||
ProtobufCAllocator *allocator);
|
||||
/* RttyMessage methods */
|
||||
void rtty_message__init
|
||||
(RttyMessage *message);
|
||||
@@ -106,9 +76,6 @@ void rtty_message__free_unpacked
|
||||
ProtobufCAllocator *allocator);
|
||||
/* --- per-message closures --- */
|
||||
|
||||
typedef void (*RttyMessageData_Closure)
|
||||
(const RttyMessageData *message,
|
||||
void *closure_data);
|
||||
typedef void (*RttyMessage_Closure)
|
||||
(const RttyMessage *message,
|
||||
void *closure_data);
|
||||
@@ -118,7 +85,6 @@ typedef void (*RttyMessage_Closure)
|
||||
|
||||
/* --- descriptors --- */
|
||||
|
||||
extern const ProtobufCMessageDescriptor rtty_message_data__descriptor;
|
||||
extern const ProtobufCMessageDescriptor rtty_message__descriptor;
|
||||
extern const ProtobufCEnumDescriptor rtty_message__type__descriptor;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user