Commit 0ff0d009 authored by Isabella Skořepová's avatar Isabella Skořepová
Browse files

Squashed 'dserial-cli/' content from commit 2810ad7

git-subtree-dir: dserial-cli
git-subtree-split: 2810ad791282dd53571c5ad4ff72584a7f038cd5
parents
build
.vala-aux
This diff is collapsed.
# --------------------------------------------------
# Vala Makefile
# --------------------------------------------------
#
# This Makefile provides a starting point for building
# and testing Vala projects on Linux.
#
# There are two types of projects supported: executable
# and shared library. Basic installation/uninstallation
# support is provided as well.
#
# To build your project, first fill in some details below.
# At a minimum you need to specify a name for your
# project's target (the executable or library to be
# produced). You may also configure what packages are
# used to build it, how the files in your project's
# root are organized, and of course, whether it is an
# executable or library.
#
# There are three important subdirectories that this
# Makefile uses. The names of these can be configured
# further down, if you so desire.
#
# src/
# all *.vala source files. The `find' command
# is used to locate them, so feel free to create
# as many subdirectories here as you wish
#
# data/
# any additional data files for your project. When
# installed, the contents of this folder are copied
# directly to $(PREFIX)/share, so you should make
# sure that the contents mirror where you want them
# to be installed. For example, to create a launcher
# for your program, you could put the .desktop file
# in data/applications so that it would be installed
# to $(PREFIX)/share/applications.
# NOTE: this directory is only used for executable
# projects, and is set to read-only as long as the
# project is installed. To regain write access, you
# must first uninstall your project from the
# filesystem.
#
# build/
# where compiled results are placed, including
# intermediate .c and .o files as well as the final
# executable or library
#
# To build your project, make sure your desired source
# files are placed in src/, then run make.
#
# The following targets are supported:
#
# all/target: compiles the source and generates the
# final result in build/ (default)
# compile: compiles vala down to its C source, but
# doesn't generate the final result
# clean: cleans all compilation results for a fresh build
# install: installs this program/library
# uninstall: uninstalls this program/library
#
# To install the project, run `make install' with root
# privileges. The default prefix is /usr/local, which can
# be changed by specifying a new PREFIX variable on the
# command-line, e.g. `make install PREFIX=/usr'. Likewise,
# it can be uninstalled using `make uninstall` as root,
# using the same PREFIX variable as when it was installed.
#
# WHAT THIS MAKEFILE DOES NOT DO:
#
# - dependency/version checking
# - program configuration prior to running make
# - internationalization
# - anything besides compile vala and cp/rm files
#
# Enjoy.
#
# --------------------------------------------------
# BEGIN PROJECT CONFIGURATION
# --------------------------------------------------
# The name of the final result.
TARGET := dserial
# A space-separated list of packages required by your
# project. Names must be compatible with pkg-config, e.g.
# gtk+-3.0
PKGS :=
# Configure the project's directories.
SRC_DIR := $(DIRECTORY)src/
BUILD_DIR := $(DIRECTORY)build/
DATA_DIR := $(DIRECTORY)data/
# This is a hidden folder, simply used to help parallel builds
VALA_AUX := $(DIRECTORY).vala-aux/
# Set the vala compiler.
VALAC := valac
# Add any additional flags to pass to the vala compiler.
VALAFLAGS := --pkg readline --pkg gio-2.0 -g
# Set the C compiler.
CC := gcc
# Add any additional flags to pass to the C compiler.
CFLAGS := -O -w -lreadline -lgio-2.0 -g
# Add any additional flags to pass to the compiler during
# the linking phase.
OFLAGS :=
# --------------------------------------------------
# END PROJECT CONFIGURATION
# --------------------------------------------------
# Get pkg-config cflags and libs
PKG_CFLAGS := $(shell pkg-config --cflags gobject-2.0 $(PKGS))
PKG_LIBS := $(shell pkg-config --libs gobject-2.0 $(PKGS))
# Locate necessary files
VALA_FILES := $(shell find $(SRC_DIR) -type f -name "*.vala")
C_FILES = $(subst $(SRC_DIR),$(BUILD_DIR)c/,$(VALA_FILES:.vala=.c))
O_FILES = $(subst $(SRC_DIR),$(BUILD_DIR)o/,$(VALA_FILES:.vala=.o))
# Set stamp and deps information
# This part is needed in order to avoid re-building everything each time
BASENAMES = $(subst $(SRC_DIR),,$(subst $(SRC_DIR)/,,$(basename $(VALA_FILES))))
VALA_FASTVAPI_FILES = $(foreach f, $(BASENAMES), $(VALA_AUX)$(f).vapi)
VALA_FASTVAPI_STAMPS = $(foreach f, $(BASENAMES), $(VALA_AUX)$(f).vapi.stamp)
VALA_DEPS = $(foreach f, $(BASENAMES), $(VALA_AUX)$(f).dep)
# Default installation prefix
PREFIX = /usr/local
BUILDMSG := Generating executable...
.PRECIOUS: $(VALA_AUX)%.vapi.stamp $(VALA_AUX)%.dep
# Fast option
ifneq ($(INCREMENTAL), true)
$(BUILD_DIR)$(TARGET): src/*.vala src/commands/*.vala Makefile
@echo ' $(BUILDMSG) ';mkdir -p $(BUILD_DIR) && $(VALAC) $(wildcard src/*.vala) $(wildcard src/*/*.vala) -X -w -o $(BUILD_DIR)$(TARGET) --pkg gio-2.0 --pkg readline --pkg posix -X -lreadline -g
@echo " Done"
# At first slower but then faster option, sometimes buggy
else
all: $(BUILD_DIR)$(TARGET)
$(VALA_FASTVAPI_FILES): ;
$(VALA_AUX)%.vapi.stamp: $(SRC_DIR)%.vala | $(VALA_AUX)
@echo ' GEN '$(@:$(VALA_AUX)%.stamp=%); mkdir -p $(dir $@) && $(VALAC) --fast-vapi=$(@:.stamp=) $(@:$(VALA_AUX)%.vapi.stamp=$(SRC_DIR)%.vala) && touch $@
$(VALA_AUX)%.dep: $(SRC_DIR)%.vala | $(VALA_FASTVAPI_STAMPS)
@echo ' VALAC -> dep '$(patsubst $(VALA_AUX)%.dep,%.vala,$(subst //,/,$@)); $(VALAC) -C --deps=$@ -b $(SRC_DIR) -d $(BUILD_DIR)c $(VALAFLAGS) $(addprefix --use-fast-vapi=,$(subst $(patsubst $(VALA_AUX)%.dep,%.vapi,$(subst //,/,$@)),, $(VALA_FASTVAPI_FILES))) $(addprefix --pkg=,$(PKGS)) $<
$(VALA_AUX):
@mkdir -p $(VALA_AUX)
$(BUILD_DIR)c/%.c : | $(VALA_AUX)%.dep ;
@echo ' VALAC -> c '$(@:$(BUILD_DIR)c/%=%);$(VALAC) -C -b $(SRC_DIR) -d $(BUILD_DIR)c $(VALAFLAGS) $(addprefix --use-fast-vapi=,$(subst $(VALA_AUX)$(@:$(BUILD_DIR)c/%.c=%.vapi),, $(VALA_FASTVAPI_FILES))) $(addprefix --pkg=,$(PKGS)) $(@:$(BUILD_DIR)c/%.c=$(SRC_DIR)%.vala)
$(BUILD_DIR)o/%.o: $(BUILD_DIR)c/%.c
@echo ' CC '$(<:$(BUILD_DIR)c/%=%); mkdir -p $(dir $@); $(CC) -c $(CFLAGS) $(PKG_CFLAGS) -o $@ $(@:$(BUILD_DIR)o/%.o=$(BUILD_DIR)c/%.c)
$(BUILD_DIR)$(TARGET): $(O_FILES) $(C_FILES)
@echo ' $(BUILDMSG) ';$(CC) -o $(BUILD_DIR)$(TARGET) $(OFLAGS) $(O_FILES) $(PKG_LIBS) $(CFLAGS)
@echo ' Done.'
endif
run: $(BUILD_DIR)$(TARGET)
./$(BUILD_DIR)$(TARGET) interactive
clean:
@echo ' Cleaning up...'; $(RM) -rf $(BUILD_DIR) $(VALA_AUX)
@echo ' Done.'
install:
@echo ' Installing to $(PREFIX)...' && cp $(BUILD_DIR)$(TARGET) $(PREFIX)/bin/$(TARGET)
@echo ' Done.'
uninstall:
@echo ' Removing $(PREFIX)/bin/$(TARGET)...'; $(RM) $(PREFIX)/bin/$(TARGET)
@echo ' Done.'
.PHONY: all clean $(BUILD_DIR)$(TARGET) install install-EXEC install-LIB uninstall uninstall-EXEC uninstall-LIB
include $(wildcard $(VALA_AUX)/*.dep)
DSerial-CLI
===========
DSerial-CLI is command line interface to DSerial D-Bus server. It allows changing port settings
(baudrate, flowcontrol, data/stop bits). Features port autodetection.
Allows writing data in various bases (ASCII, decimal, octal, hexadecimal and binary). It also allows
reading in multiple bases simultaneously (see help read). Also has interactive regime to save you
from repetitive writing.
## How to install
Currently only from source
dependencies (for fedora): readline-devel vala
Build and install with
make
sudo make install
Run
dserial
## Features (in no particular order)
- Port autodetection
- Command-line autocompletion
- All basic serial port settings (baud, databits, stopbits, parity, flow control)
- Interactive console (so you dont have to write dserial and port name all the time)
- Advanced options for displaying recieved messages
## Quickstart
dserial interactive
$ help
$ bind /dev/ttyUSB0
$ write ahoj
$ read
## License
This part of DSerial project is release under the terms GPL3 license or later
as released by Free Software Foundation
(c) Jakub Skořepa 2015
#!/usr/bin/bash
/*
* Copyright 2015 Jakub Skořepa <jakub@skorepa.info>
*
* This file is part of dserial
*
* dserial is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* dserial 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with dserial.
* If not, see <http://www.gnu.org/licenses/>.
*
*/
_dserial()
{
local cur cmd
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if [ $COMP_CWORD -ge 2 ]
then
case "${COMP_WORDS[1]}" in
interactive|list)
return 0
;;
help)
if [ $COMP_CWORD -eq 2 ]
then COMPREPLY=($(compgen -W "$(dserial help topics)" -- "${cur}"))
fi
return 0
;;
add)
if [ $COMP_CWORD -eq 2 ]
then
COMPREPLY=( $(compgen -f "${cur}") )
compopt -o filenames
fi
return 0
;;
info)
if [ $COMP_CWORD -eq 2 ]
then COMPREPLY=($(compgen -W "$(dserial list)" -- "${cur}"))
fi
return 0
;;
get)
if [ $COMP_CWORD -eq 2 ]
then COMPREPLY=($(compgen -W "$(dserial list)" -- "${cur}"))
elif [ $COMP_CWORD -eq 3 ]
then COMPREPLY=($(compgen -W "$(dserial help readoptions)" -- "${cur}"))
fi
return 0
;;
set)
if [ $COMP_CWORD -eq 2 ]
then COMPREPLY=($(compgen -W "$(dserial list)" -- "${cur}"))
elif [ $COMP_CWORD -eq 3 ]
then COMPREPLY=($(compgen -W "$(dserial help options)" -- "${cur}"))
elif [ $COMP_CWORD -eq 4 ]
then COMPREPLY=($(compgen -W "$(dserial values ${COMP_WORDS[3]} ${COMP_WORDS[2]})" -- "${cur}"))
fi
return 0
;;
values)
if [ $COMP_CWORD -eq 2 ] ;then
COMPREPLY=($(compgen -W "$(dserial help options)" -- "${cur}"))
elif [ $COMP_CWORD -eq 3 ] ;then
if [ "${COMP_WORDS[2]}" == "baudrate" -o "${COMP_WORDS[2]}" == "databits" -o "${COMP_WORDS[2]}" == "stopbits" ]
then COMPREPLY=($(compgen -W "$(dserial list)" -- "${cur}"))
fi
fi
return 0
;;
open|close|write|destroy)
if [ $COMP_CWORD -eq 2 ]
then COMPREPLY=($(compgen -W "$(dserial list)" -- "${cur}"))
fi
return 0
;;
read)
if [ $COMP_CWORD -eq 2 ]
then COMPREPLY=($(compgen -W "$(dserial list)" -- "${cur}"))
elif [ $COMP_CWORD -eq 3 ]
then COMPREPLY=($(for p in X x c s d b
do
if [[ ${cur} != *"$p"* ]]
then printf "%s%s\n" "${cur}" "$p";
fi
done
))
fi
return 0
;;
*)
return 0
;;
esac
fi
COMPREPLY=($(compgen -W "$(dserial help commands)" -- "${cur}"))
return 0
}
complete -F _dserial dserial
/*
* Copyright 2015 Jakub Skořepa <jakub@skorepa.info>
*
* This file is part of dserial
*
* dserial is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* dserial 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with dserial.
* If not, see <http://www.gnu.org/licenses/>.
*
*/
public class UInt16 {
public uint16 v;
public UInt16(uint16 v) {
this.v = v;
}
}
public class BigInt { // base 256
public Array<UInt16> number;
public BigInt() {
number = new Array<UInt16>();
number.append_val(new UInt16(0));
}
public void carry(uint n) {
for(uint i = n; i < number.length; i++) {
if(number.data[i].v >= 256) {
if(i == number.length-1) {
number.append_val(new UInt16(0));
}
number.data[i+1].v += (number.data[i].v&0xff00)>>8;
number.data[i].v &= 0xff;
} else {
return;
}
}
}
public void copy_from(BigInt n) {
number = new Array<UInt16>();
for(int i = 0; i < n.number.length; i++) {
number.append_val(new UInt16(n.number.data[i].v));
}
}
public void add(BigInt n) {
for(int i = 0; i < n.number.length - number.length; i++) {
number.append_val(new UInt16(0));
}
for(int i = 0; i < n.number.length; i++) {
number.data[i].v += n.number.data[i].v;
carry(i);
}
}
public void add_uint8(uint8 n) {
number.data[0].v += n;
carry(0);
}
public void hex_print() {
for(uint i = number.length; i>0; i--) {
if(number.data[i-1].v<16) stdout.printf("0");
stdout.printf("%X ",number.data[i-1].v);
}
}
public void times_uint8(uint8 n) {
for(uint i = number.length; i > 0; i--) {
number.data[i-1].v *= n;
carry(i-1);
}
}
}
/*
* Copyright 2015 Jakub Skořepa <jakub@skorepa.info>
*
* This file is part of dserial
*
* dserial is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* dserial 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with dserial.
* If not, see <http://www.gnu.org/licenses/>.
*
*/
public class AddCommand : Object, Command
{
public int execute(string[] args, ApplicationState state)
{
if(args.length == 0) {
return error(Err.TYPE_NOT_SPECIFIED);
}
if(args.length == 1) {
return error(Err.NAME_NOT_SPECIFIED);
}
if(args[0] == "fake") {
Serial.create_fake_port(args[0]);
stdout.printf("Warning: this action is not implemented in DSerial server\n");
} else if(args[0] == "file") {
Serial.create_file_port(args[0]);
stdout.printf("Warning: this action is not implemented in DSerial server\n");
} else {
return error(Err.WRONG_TYPE);
}
return 0;
}
public string name()
{
return "add";
}
public string[]? args(string[] args, ApplicationState state)
{
if(args.length == 0)
return {"fake", "file"};
else if(args.length == 1)
return null; // => complete filename
else
return {}; // => complete nothing
}
}
/*
* Copyright 2015 Jakub Skořepa <jakub@skorepa.info>
*
* This file is part of dserial
*
* dserial is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* dserial 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with dserial.
* If not, see <http://www.gnu.org/licenses/>.
*
*/
public class AutodetectCommand : Object, Command
{
public int execute(string[] args, ApplicationState state)
{
Serial.autodetect();
return 0;
}
public string name()
{
return "autodetect";
}
public string[]? args(string[] args, ApplicationState state)
{
return {}; // => nothing
}
}
/*
* Copyright 2015 Jakub Skořepa <jakub@skorepa.info>
*
* This file is part of dserial
*
* dserial is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* dserial 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with dserial.
* If not, see <http://www.gnu.org/licenses/>.
*
*/
public class BindCommand : Object, Command
{
public int execute(string[] args, ApplicationState state)
{
if(args.length == 0) {
return error(Err.PORT_NOT_SPECIFIED);
}
string? o = Serial.object_name(args[0]);
if(o == null) return error(Err.NONEXISTENT_PORT);
state.bound_port = new Serial.SerialClient(o);
return 0;
}
public string name()
{
return "bind";
}
public string[]? args(string[] args, ApplicationState state)
{
if(args.length == 0)
return Serial.get_ports();
else
return {};
}
}
/*
* Copyright 2015 Jakub Skořepa <jakub@skorepa.info>
*
* This file is part of dserial
*
* dserial is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* dserial 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with dserial.
* If not, see <http://www.gnu.org/licenses/>.
*
*/
public class CloseCommand : Object, Command
{
public int execute(string[] args, ApplicationState state)
{
Serial.SerialClient p;
if(state.bound_port == null){
if(args.length == 0) {
return error(Err.PORT_NOT_SPECIFIED);
}
string? o = Serial.object_name(args[0]);
if(o == null) return error(Err.NONEXISTENT_PORT);