Skip to content
Snippets Groups Projects
Commit 5848897f authored by clohr's avatar clohr
Browse files

Outils de demo des lib xaal_aux et xaal_jc

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/C/branches/version-0.7@2281 b32b6428-25c9-4566-ad07-03861ab6144f
parent db465f9c
Branches
No related tags found
No related merge requests found
......@@ -3,3 +3,5 @@ libxaal_aux.so
libxaal_aux.o
dummyLamp_aux
dummyLamp_aux.o
xaal_aux
xaal_aux.o
LIB = xaal_aux
DEMO = xaal_aux
DEMOS = dummyLamp_aux
INSTALL_DIR = /usr/local
......@@ -7,11 +9,11 @@ INSTALL_DIR = /usr/local
CFLAGS = -Wall -I. -g
LDFLAGS =
LOADLIBES = -L. -lxaal -lmnl -lcbor -luuid -lsodium -l$(LIB)
LOADLIBES = -L. -lxaal -lcbor -luuid -lsodium -l$(LIB) -lmnl
INSTALL= install -c
all: lib$(LIB).a lib$(LIB).so $(DEMOS)
all: lib$(LIB).a lib$(LIB).so $(DEMOS) $(DEMO)
lib$(LIB).a: lib$(LIB).o
$(AR) rc $@ $^
......@@ -25,7 +27,7 @@ clean:
-rm -f *.o *~
proper: clean
-rm -f lib$(LIB).a lib$(LIB).so $(DEMOS)
-rm -f lib$(LIB).a lib$(LIB).so $(DEMOS) $(DEMO)
install: lib$(LIB).a lib$(LIB).so $(LIB).h
$(INSTALL) -d $(INSTALL_DIR)
......@@ -43,7 +45,7 @@ test: all
@echo " " Then, eg.:
@/bin/echo -e "" $(foreach i,$(DEMOS)," ./$(i) -a 224.0.29.200 -p 1234 -s my_secret\n")
Makefile.dep: lib$(LIB).c $(DEMOS:=.c)
Makefile.dep: lib$(LIB).c $(DEMOS:=.c) $(DEMO).c
$(CC) $(CFLAGS) -MM $^ > $@
include Makefile.dep
......@@ -53,7 +55,7 @@ include Makefile.dep
.SUFFIXES: .a
.svnignore:
echo lib$(LIB).a lib$(LIB).so lib$(LIB).o $(DEMOS) $(DEMOS:=.o) | tr ' ' '\012' > $@
echo lib$(LIB).a lib$(LIB).so lib$(LIB).o $(DEMOS) $(DEMOS:=.o) $(DEMO) $(DEMO).o | tr ' ' '\012' > $@
svnignore: .svnignore
svn propset svn:ignore -F $< .
libxaal_aux.o: libxaal_aux.c xaal_aux.h
dummyLamp_aux.o: dummyLamp_aux.c xaal.h xaal_aux.h
xaal_aux.o: xaal_aux.c xaal_aux.h
......@@ -25,6 +25,7 @@
#include <time.h>
#include <arpa/inet.h>
#include <cbor.h>
#include <string.h>
#include <libmnl/libmnl.h>
#include <linux/if.h>
#include <linux/if_link.h>
......@@ -76,6 +77,22 @@ static int data_attr_cb(const struct nlattr *attr, void *data) {
}
char *print_mac(const unsigned char *mac, unsigned len) {
unsigned txt_sz = len * 3;
if (!txt_sz)
return strdup("");
char *txt = malloc(txt_sz);
for (unsigned i = 0; i < len; i++)
sprintf(txt+3*i, "%02x:", mac[i]);
txt[txt_sz-1] = '\0';
return txt;
}
static int data_cb(const struct nlmsghdr *nlh, void *data) {
cbor_item_t *links = data;
struct nlattr *tb[IFLA_MAX+1] = {};
......@@ -93,10 +110,12 @@ static int data_cb(const struct nlmsghdr *nlh, void *data) {
cbor_map_add(link, (struct cbor_pair){ cbor_move(cbor_build_string("protocol")),
cbor_move(cbor_build_string("Ethernet")) });
if (tb[IFLA_ADDRESS])
if (tb[IFLA_ADDRESS]) {
char *txt = print_mac(mnl_attr_get_payload(tb[IFLA_ADDRESS]), mnl_attr_get_payload_len(tb[IFLA_ADDRESS]));
cbor_map_add(link, (struct cbor_pair){ cbor_move(cbor_build_string("addr")),
cbor_move(cbor_build_bytestring(mnl_attr_get_payload(tb[IFLA_ADDRESS]),
mnl_attr_get_payload_len(tb[IFLA_ADDRESS]))) });
cbor_move(cbor_build_string(txt)) });
free(txt);
}
cbor_array_push(links, link);
}
......
/* libxaal_aux
* Auxiliary helpers for developing xAAL devices
*
* (c) 2019 Christophe Lohr <christophe.lohr@imt-atlantique.fr>
*
* 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 3 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 program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include <fcntl.h>
#include <cbor.h>
#include <xaal_aux.h>
typedef enum { none, eth } action_t;
void call_xAAL_aux_eth_addr(char *fname) {
cbor_item_t *chwId;
chwId = xAAL_aux_eth_addr();
size_t sz, len;
cbor_mutable_data buf;
len = cbor_serialize_alloc(chwId, &buf, &sz);
cbor_decref(&chwId);
int fd;
fd = creat(fname, (mode_t)0666);
if (fd == -1) {
perror(fname);
exit(EXIT_FAILURE);
}
if (write(fd, buf, len) == -1) {
perror(fname);
close(fd);
exit(EXIT_FAILURE);
} else
close(fd);
}
int main(int argc, char *argv[]) {
char *fname = NULL;
int opt;
bool arg_error = false;
action_t act = none;
while ((opt = getopt(argc, argv, "e:")) != -1) {
switch (opt) {
case 'e':
act = eth;
fname = optarg;
break;
default:
arg_error = true;
}
}
if (optind < argc) {
fprintf(stderr, "Unknown argument %s\n", argv[optind]);
arg_error = true;
}
switch (act) {
case (eth):
if (fname)
call_xAAL_aux_eth_addr(fname);
else
arg_error = true;
break;
default:
arg_error = true;
}
if ( arg_error ) {
fprintf(stderr, "Usage: %s [-e <file>]\n"
"Command line tool using libxaal_aux\n"
"Options:\n"
" -e <file> Call xAAL_aux_eth_addr() and write result in 'file'\n"
" ... nothing else for now\n", argv[0]);
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
libxaal_jc.a
libxaal_jc.so
libxaal_jc.o
xaal_jc
xaal_jc.o
......@@ -34,7 +34,7 @@ install: lib$(LIB).a lib$(LIB).so $(LIB).h
SHELL = /bin/bash
Makefile.dep: lib$(LIB).c $(TOOL)
Makefile.dep: lib$(LIB).c $(DEMO).c
$(CC) $(CFLAGS) -MM $^ > $@
include Makefile.dep
......@@ -44,7 +44,7 @@ include Makefile.dep
.SUFFIXES: .a
.svnignore:
echo lib$(LIB).a lib$(LIB).so lib$(LIB).o | tr ' ' '\012' > $@
echo lib$(LIB).a lib$(LIB).so lib$(LIB).o $(DEMO) $(DEMO).o | tr ' ' '\012' > $@
svnignore: .svnignore
svn propset svn:ignore -F $< .
libxaal_jc.o: libxaal_jc.c cencode.h cdecode.h xaal.h xaal_jc.h
xaal_jc.o: xaal_jc.c xaal_jc.h
../libxaal/libxaal.so
\ No newline at end of file
No preview for this file type
/* libxaal_jc
* Auxiliary json2cbor & cbor2json transcoders for xAAL
*
* (c) 2017 Christophe Lohr <christophe.lohr@imt-atlantique.fr>
*
* 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 3 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 program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <string.h>
#include "xaal_jc.h"
#include <cbor.h>
#include <json-c/json.h>
#include <xaal_jc.h>
void cbor_to_json(char *file_in, char *file_out) {
......@@ -117,7 +136,7 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Unknown argument %s\n", argv[optind]);
arg_error = true;
}
if (!file_in || !file_out || arg_error) {
if (arg_error || !file_in || !file_out || arg_error) {
fprintf(stderr, "Usage: %s [-1] -i <file_in> -o <file_out>\n"
"Do its best to convert a json file into a cbor file, according to xAAL rules.\n"
"Options:\n"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment