mirror of
https://github.com/eddyem/eddys_snippets.git
synced 2025-12-06 02:35:12 +03:00
add serialsockCANmanage
This commit is contained in:
parent
830716fd1c
commit
7ecdf689d3
45
serialsockCANmanage/Makefile
Normal file
45
serialsockCANmanage/Makefile
Normal file
@ -0,0 +1,45 @@
|
||||
# run `make DEF=...` to add extra defines
|
||||
PROGRAM := SEWcontrol
|
||||
LDFLAGS := -fdata-sections -ffunction-sections -Wl,--gc-sections -Wl,--discard-all
|
||||
LDFLAGS += -lusefull_macros -lm -L/usr/local/lib/
|
||||
SRCS := $(wildcard *.c)
|
||||
DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111
|
||||
OBJDIR := mk
|
||||
CFLAGS += -O3 -Wno-trampolines -std=gnu99
|
||||
OBJS := $(addprefix $(OBJDIR)/, $(SRCS:%.c=%.o))
|
||||
DEPS := $(OBJS:.o=.d)
|
||||
CC = gcc
|
||||
#CXX = g++
|
||||
|
||||
|
||||
all : $(PROGRAM)
|
||||
|
||||
debug: CFLAGS += -DEBUG -Werror -Wall -Wextra
|
||||
debug: all
|
||||
|
||||
$(OBJS): $(OBJDIR)
|
||||
|
||||
$(PROGRAM) : $(OBJS)
|
||||
@echo -e "\t\tLD $(PROGRAM)"
|
||||
$(CC) $(OBJS) $(LDFLAGS) -o $(PROGRAM)
|
||||
|
||||
$(OBJDIR):
|
||||
mkdir $(OBJDIR)
|
||||
|
||||
ifneq ($(MAKECMDGOALS),clean)
|
||||
-include $(DEPS)
|
||||
endif
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
@echo -e "\t\tCC $<"
|
||||
$(CC) $< -MD -c $(CFLAGS) $(DEFINES) -o $@
|
||||
|
||||
clean:
|
||||
@echo -e "\t\tCLEAN"
|
||||
@rm -f $(OBJS) $(DEPS)
|
||||
@rmdir $(OBJDIR) 2>/dev/null || true
|
||||
|
||||
xclean: clean
|
||||
@rm -f $(PROGRAM)
|
||||
|
||||
.PHONY: clean xclean
|
||||
3
serialsockCANmanage/Readme
Normal file
3
serialsockCANmanage/Readme
Normal file
@ -0,0 +1,3 @@
|
||||
Simplest SEW motor controllers manage using serialsock and CAN-USB
|
||||
|
||||
|
||||
1
serialsockCANmanage/SEWmanage.cflags
Normal file
1
serialsockCANmanage/SEWmanage.cflags
Normal file
@ -0,0 +1 @@
|
||||
-std=c17
|
||||
6
serialsockCANmanage/SEWmanage.config
Normal file
6
serialsockCANmanage/SEWmanage.config
Normal file
@ -0,0 +1,6 @@
|
||||
// Add predefined macros for your project here. For example:
|
||||
// #define THE_ANSWER 42
|
||||
#define EBUG
|
||||
#define _GNU_SOURCE
|
||||
#define _XOPEN_SOURCE=1111
|
||||
|
||||
1
serialsockCANmanage/SEWmanage.creator
Normal file
1
serialsockCANmanage/SEWmanage.creator
Normal file
@ -0,0 +1 @@
|
||||
[General]
|
||||
162
serialsockCANmanage/SEWmanage.creator.user
Normal file
162
serialsockCANmanage/SEWmanage.creator.user
Normal file
@ -0,0 +1,162 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE QtCreatorProject>
|
||||
<!-- Written by QtCreator 4.15.1, 2022-01-24T14:33:13. -->
|
||||
<qtcreator>
|
||||
<data>
|
||||
<variable>EnvironmentId</variable>
|
||||
<value type="QByteArray">{cf63021e-ef53-49b0-b03b-2f2570cdf3b6}</value>
|
||||
</data>
|
||||
<data>
|
||||
<variable>ProjectExplorer.Project.ActiveTarget</variable>
|
||||
<value type="int">0</value>
|
||||
</data>
|
||||
<data>
|
||||
<variable>ProjectExplorer.Project.EditorSettings</variable>
|
||||
<valuemap type="QVariantMap">
|
||||
<value type="bool" key="EditorConfiguration.AutoIndent">true</value>
|
||||
<value type="bool" key="EditorConfiguration.AutoSpacesForTabs">false</value>
|
||||
<value type="bool" key="EditorConfiguration.CamelCaseNavigation">true</value>
|
||||
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.0">
|
||||
<value type="QString" key="language">Cpp</value>
|
||||
<valuemap type="QVariantMap" key="value">
|
||||
<value type="QByteArray" key="CurrentPreferences">CppGlobal</value>
|
||||
</valuemap>
|
||||
</valuemap>
|
||||
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.1">
|
||||
<value type="QString" key="language">QmlJS</value>
|
||||
<valuemap type="QVariantMap" key="value">
|
||||
<value type="QByteArray" key="CurrentPreferences">QmlJSGlobal</value>
|
||||
</valuemap>
|
||||
</valuemap>
|
||||
<value type="int" key="EditorConfiguration.CodeStyle.Count">2</value>
|
||||
<value type="QByteArray" key="EditorConfiguration.Codec">KOI8-R</value>
|
||||
<value type="bool" key="EditorConfiguration.ConstrainTooltips">false</value>
|
||||
<value type="int" key="EditorConfiguration.IndentSize">4</value>
|
||||
<value type="bool" key="EditorConfiguration.KeyboardTooltips">false</value>
|
||||
<value type="int" key="EditorConfiguration.MarginColumn">80</value>
|
||||
<value type="bool" key="EditorConfiguration.MouseHiding">true</value>
|
||||
<value type="bool" key="EditorConfiguration.MouseNavigation">true</value>
|
||||
<value type="int" key="EditorConfiguration.PaddingMode">1</value>
|
||||
<value type="bool" key="EditorConfiguration.PreferSingleLineComments">false</value>
|
||||
<value type="bool" key="EditorConfiguration.ScrollWheelZooming">false</value>
|
||||
<value type="bool" key="EditorConfiguration.ShowMargin">false</value>
|
||||
<value type="int" key="EditorConfiguration.SmartBackspaceBehavior">1</value>
|
||||
<value type="bool" key="EditorConfiguration.SmartSelectionChanging">true</value>
|
||||
<value type="bool" key="EditorConfiguration.SpacesForTabs">true</value>
|
||||
<value type="int" key="EditorConfiguration.TabKeyBehavior">0</value>
|
||||
<value type="int" key="EditorConfiguration.TabSize">8</value>
|
||||
<value type="bool" key="EditorConfiguration.UseGlobal">true</value>
|
||||
<value type="bool" key="EditorConfiguration.UseIndenter">false</value>
|
||||
<value type="int" key="EditorConfiguration.Utf8BomBehavior">2</value>
|
||||
<value type="bool" key="EditorConfiguration.addFinalNewLine">true</value>
|
||||
<value type="bool" key="EditorConfiguration.cleanIndentation">true</value>
|
||||
<value type="bool" key="EditorConfiguration.cleanWhitespace">true</value>
|
||||
<value type="QString" key="EditorConfiguration.ignoreFileTypes">*.md, *.MD, Makefile</value>
|
||||
<value type="bool" key="EditorConfiguration.inEntireDocument">true</value>
|
||||
<value type="bool" key="EditorConfiguration.skipTrailingWhitespace">true</value>
|
||||
</valuemap>
|
||||
</data>
|
||||
<data>
|
||||
<variable>ProjectExplorer.Project.PluginSettings</variable>
|
||||
<valuemap type="QVariantMap">
|
||||
<valuemap type="QVariantMap" key="ClangTools">
|
||||
<value type="bool" key="ClangTools.AnalyzeOpenFiles">true</value>
|
||||
<value type="bool" key="ClangTools.BuildBeforeAnalysis">true</value>
|
||||
<value type="QString" key="ClangTools.DiagnosticConfig">Builtin.DefaultTidyAndClazy</value>
|
||||
<value type="int" key="ClangTools.ParallelJobs">4</value>
|
||||
<valuelist type="QVariantList" key="ClangTools.SelectedDirs"/>
|
||||
<valuelist type="QVariantList" key="ClangTools.SelectedFiles"/>
|
||||
<valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/>
|
||||
<value type="bool" key="ClangTools.UseGlobalSettings">true</value>
|
||||
</valuemap>
|
||||
<valuemap type="QVariantMap" key="CppEditor.QuickFix">
|
||||
<value type="bool" key="UseGlobalSettings">true</value>
|
||||
</valuemap>
|
||||
</valuemap>
|
||||
</data>
|
||||
<data>
|
||||
<variable>ProjectExplorer.Project.Target.0</variable>
|
||||
<valuemap type="QVariantMap">
|
||||
<value type="QString" key="DeviceType">Desktop</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{91347f2c-5221-46a7-80b1-0a054ca02f79}</value>
|
||||
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
|
||||
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
|
||||
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
|
||||
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/home/eddy/Docs/SAO/ELECTRONICS/CAN_controller/Socket_CANserver</value>
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
|
||||
<valuelist type="QVariantList" key="GenericProjectManager.GenericMakeStep.BuildTargets">
|
||||
<value type="QString">all</value>
|
||||
</valuelist>
|
||||
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericMakeStep</value>
|
||||
</valuemap>
|
||||
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Build</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
|
||||
</valuemap>
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
|
||||
<valuelist type="QVariantList" key="GenericProjectManager.GenericMakeStep.BuildTargets">
|
||||
<value type="QString">clean</value>
|
||||
</valuelist>
|
||||
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericMakeStep</value>
|
||||
</valuemap>
|
||||
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Clean</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Clean</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
|
||||
</valuemap>
|
||||
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
|
||||
<value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
|
||||
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.CustomParsers"/>
|
||||
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Default</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericBuildConfiguration</value>
|
||||
</valuemap>
|
||||
<value type="int" key="ProjectExplorer.Target.BuildConfigurationCount">1</value>
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.Target.DeployConfiguration.0">
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
|
||||
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">0</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Deploy</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Deploy</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Deploy</value>
|
||||
</valuemap>
|
||||
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">1</value>
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.DeployConfiguration.CustomData"/>
|
||||
<value type="bool" key="ProjectExplorer.DeployConfiguration.CustomDataEnabled">false</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.DefaultDeployConfiguration</value>
|
||||
</valuemap>
|
||||
<value type="int" key="ProjectExplorer.Target.DeployConfigurationCount">1</value>
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.Target.RunConfiguration.0">
|
||||
<valuelist type="QVariantList" key="CustomOutputParsers"/>
|
||||
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
|
||||
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value>
|
||||
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value>
|
||||
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
|
||||
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
|
||||
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
|
||||
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
|
||||
</valuemap>
|
||||
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
|
||||
</valuemap>
|
||||
</data>
|
||||
<data>
|
||||
<variable>ProjectExplorer.Project.TargetCount</variable>
|
||||
<value type="int">1</value>
|
||||
</data>
|
||||
<data>
|
||||
<variable>ProjectExplorer.Project.Updater.FileVersion</variable>
|
||||
<value type="int">22</value>
|
||||
</data>
|
||||
<data>
|
||||
<variable>Version</variable>
|
||||
<value type="int">22</value>
|
||||
</data>
|
||||
</qtcreator>
|
||||
1
serialsockCANmanage/SEWmanage.cxxflags
Normal file
1
serialsockCANmanage/SEWmanage.cxxflags
Normal file
@ -0,0 +1 @@
|
||||
-std=c++17
|
||||
5
serialsockCANmanage/SEWmanage.files
Normal file
5
serialsockCANmanage/SEWmanage.files
Normal file
@ -0,0 +1,5 @@
|
||||
cmdlnopts.c
|
||||
cmdlnopts.h
|
||||
main.c
|
||||
sersock.c
|
||||
sersock.h
|
||||
1
serialsockCANmanage/SEWmanage.includes
Normal file
1
serialsockCANmanage/SEWmanage.includes
Normal file
@ -0,0 +1 @@
|
||||
.
|
||||
83
serialsockCANmanage/cmdlnopts.c
Normal file
83
serialsockCANmanage/cmdlnopts.c
Normal file
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* This file is part of the SEWmanage project.
|
||||
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "cmdlnopts.h"
|
||||
#include "usefull_macros.h"
|
||||
|
||||
// default PID filename:
|
||||
#define DEFAULT_SOCKPATH "\0canbus"
|
||||
|
||||
static int help;
|
||||
static glob_pars G = {
|
||||
.path = DEFAULT_SOCKPATH,
|
||||
};
|
||||
glob_pars *GP = &G;
|
||||
|
||||
/**
|
||||
* Define command line options by filling structure:
|
||||
* name has_arg flag val type argptr help
|
||||
*/
|
||||
static myoption cmdlnopts[] = {
|
||||
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")},
|
||||
{"logfile", NEED_ARG, NULL, 'l', arg_string, APTR(&G.logfile), _("file to save logs (default: none)")},
|
||||
{"sockpath",NEED_ARG, NULL, 'f', arg_string, APTR(&G.path), _("socket path (start from \\0 for no files)")},
|
||||
{"verbose", NO_ARGS, NULL, 'v', arg_none, APTR(&G.verbose), _("increase log verbose level (default: LOG_WARN) and messages (default: none)")},
|
||||
{"setspeed",NEED_ARG, NULL, 's', arg_int, APTR(&G.setspeed), _("set motor speed")},
|
||||
{"stop", NO_ARGS, NULL, '0', arg_int, APTR(&G.stop), _("stop motor")},
|
||||
end_option
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse command line options and return dynamically allocated structure
|
||||
* to global parameters
|
||||
* @param argc - copy of argc from main
|
||||
* @param argv - copy of argv from main
|
||||
* @return allocated structure with global parameters
|
||||
*/
|
||||
void parse_args(int argc, char **argv){
|
||||
int i;
|
||||
size_t hlen = 1024;
|
||||
char helpstring[1024], *hptr = helpstring;
|
||||
snprintf(hptr, hlen, "Usage: %%s [args]\n\n\tWhere args are:\n");
|
||||
// format of help: "Usage: progname [args]\n"
|
||||
change_helpstring(helpstring);
|
||||
// parse arguments
|
||||
parseargs(&argc, &argv, cmdlnopts);
|
||||
for(i = 0; i < argc; i++)
|
||||
printf("Ignore parameter\t%s\n", argv[i]);
|
||||
if(help) showhelp(-1, cmdlnopts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief verbose - print additional messages depending of G.verbose (add '\n' at end)
|
||||
* @param levl - message level
|
||||
* @param fmt - message
|
||||
*/
|
||||
void verbose(int levl, const char *fmt, ...){
|
||||
va_list ar;
|
||||
if(levl > G.verbose) return;
|
||||
//printf("%s: ", __progname);
|
||||
va_start(ar, fmt);
|
||||
vprintf(fmt, ar);
|
||||
va_end(ar);
|
||||
printf("\n");
|
||||
}
|
||||
39
serialsockCANmanage/cmdlnopts.h
Normal file
39
serialsockCANmanage/cmdlnopts.h
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* This file is part of the SEWmanage project.
|
||||
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#ifndef CMDLNOPTS_H__
|
||||
#define CMDLNOPTS_H__
|
||||
|
||||
/*
|
||||
* here are some typedef's for global data
|
||||
*/
|
||||
typedef struct{
|
||||
char *logfile; // logging to this file
|
||||
char *path; // path to socket file
|
||||
int stop; // stop motor
|
||||
int setspeed; // set this speed (rev/min)
|
||||
int verbose; // verbose level: for messages & logging
|
||||
} glob_pars;
|
||||
|
||||
extern glob_pars *GP;
|
||||
|
||||
void parse_args(int argc, char **argv);
|
||||
void verbose(int levl, const char *fmt, ...);
|
||||
|
||||
#endif // CMDLNOPTS_H__
|
||||
93
serialsockCANmanage/main.c
Normal file
93
serialsockCANmanage/main.c
Normal file
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* This file is part of the SEWmanage project.
|
||||
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/wait.h>
|
||||
#include <usefull_macros.h>
|
||||
|
||||
#include "cmdlnopts.h"
|
||||
#include "sersock.h"
|
||||
|
||||
static pid_t childpid = 0;
|
||||
|
||||
void signals(int sig){
|
||||
if(childpid){ // slave process
|
||||
DBG("Child killed with sig=%d", sig);
|
||||
LOGWARN("Child killed with sig=%d", sig);
|
||||
exit(sig);
|
||||
}
|
||||
if(sig){
|
||||
DBG("Exit with signal %d", sig);
|
||||
signal(sig, SIG_IGN);
|
||||
LOGERR("Exit with signal %d", sig);
|
||||
}else LOGERR("Exit");
|
||||
exit(sig);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv){
|
||||
initial_setup();
|
||||
parse_args(argc, argv);
|
||||
if(GP->logfile){
|
||||
int lvl = LOGLEVEL_WARN + GP->verbose;
|
||||
DBG("level = %d", lvl);
|
||||
if(lvl > LOGLEVEL_ANY) lvl = LOGLEVEL_ANY;
|
||||
verbose(1, "Log file %s @ level %d\n", GP->logfile, lvl);
|
||||
OPENLOG(GP->logfile, lvl, 1);
|
||||
if(!globlog) WARNX("Can't create log file");
|
||||
}
|
||||
// signal reactions:
|
||||
signal(SIGTERM, signals); // kill (-15) - quit
|
||||
signal(SIGHUP, SIG_IGN); // hup - ignore
|
||||
signal(SIGINT, signals); // ctrl+C - quit
|
||||
signal(SIGQUIT, signals); // ctrl+\ - quit
|
||||
signal(SIGTSTP, SIG_IGN); // ignore ctrl+Z
|
||||
DBG("start");
|
||||
LOGMSG("Started");
|
||||
/*
|
||||
#ifndef EBUG
|
||||
unsigned int pause = 5;
|
||||
while(1){
|
||||
childpid = fork();
|
||||
if(childpid){ // master
|
||||
double t0 = dtime();
|
||||
LOGMSG("Created child with pid %d", childpid);
|
||||
wait(NULL);
|
||||
LOGWARN("Child %d died", childpid);
|
||||
if(dtime() - t0 < 1.) pause += 5;
|
||||
else pause = 1;
|
||||
if(pause > 900) pause = 900;
|
||||
sleep(pause); // wait a little before respawn
|
||||
}else{ // slave
|
||||
prctl(PR_SET_PDEATHSIG, SIGTERM); // send SIGTERM to child when parent dies
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
CANcmd mesg = CMD_GETSPEED;
|
||||
if(GP->setspeed){
|
||||
if(GP->stop) ERRX("Commands 'stop' and 'set speed' can't be together");
|
||||
if(GP->setspeed < -0xffff/5 || GP->setspeed > 0xffff/5) ERRX("Parameter 'set speed' should be |int16_t| < %d", 0xffff/5);
|
||||
mesg = CMD_SETSPEED;
|
||||
}else if(GP->stop) mesg = CMD_STOP;
|
||||
signals(start_socket(GP->path, mesg, GP->setspeed));
|
||||
return 0;
|
||||
}
|
||||
137
serialsockCANmanage/sersock.c
Normal file
137
serialsockCANmanage/sersock.c
Normal file
@ -0,0 +1,137 @@
|
||||
/*
|
||||
* This file is part of the canserver project.
|
||||
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <netdb.h>
|
||||
#include <poll.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/un.h> // unix socket
|
||||
#include <usefull_macros.h>
|
||||
|
||||
#include "cmdlnopts.h"
|
||||
#include "sersock.h"
|
||||
|
||||
// canopen parameters idx: get speed and get current
|
||||
#define PARIDX_GETSPEED (8318)
|
||||
#define PARIDX_GETCURRENT (8326)
|
||||
|
||||
/**
|
||||
* check data from fd
|
||||
* @param fd - file descriptor
|
||||
* @return 0 in case of timeout, 1 in case of fd have data, -1 if error
|
||||
*/
|
||||
static int canberead(int fd){
|
||||
fd_set fds;
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = 0;
|
||||
timeout.tv_usec = 100;
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(fd, &fds);
|
||||
do{
|
||||
int rc = select(fd+1, &fds, NULL, NULL, &timeout);
|
||||
if(rc < 0){
|
||||
if(errno != EINTR){
|
||||
LOGWARN("select()");
|
||||
WARN("select()");
|
||||
return -1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}while(1);
|
||||
if(FD_ISSET(fd, &fds)){
|
||||
DBG("FD_ISSET");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int CANsend(int sock, char *cmd){
|
||||
ssize_t L = strlen(cmd);
|
||||
if(send(sock, cmd, L, 0) != L){
|
||||
WARN("send");
|
||||
LOGERR("send");
|
||||
return FALSE;
|
||||
}
|
||||
cmd[L-1] = 0;
|
||||
DBG("SEND: %s", cmd);
|
||||
LOGMSG("SEND: %s", cmd);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int start_socket(char *path, CANcmd mesg, int par){
|
||||
FNAME();
|
||||
int sock = -1;
|
||||
struct sockaddr_un saddr = {0};
|
||||
saddr.sun_family = AF_UNIX;
|
||||
strncpy(saddr.sun_path, path, 106); // if sun_path[0] == 0 we don't create a file
|
||||
if((sock = socket(AF_UNIX, SOCK_SEQPACKET, 0)) < 0){ // or SOCK_STREAM?
|
||||
LOGERR("socket()");
|
||||
ERR("socket()");
|
||||
}
|
||||
if(connect(sock, &saddr, sizeof(saddr)) == -1){
|
||||
LOGERR("connect()");
|
||||
ERR("connect()");
|
||||
}
|
||||
int Bufsiz = BUFLEN;
|
||||
char *recvBuff = MALLOC(char, Bufsiz);
|
||||
char cmd[BUFLEN];
|
||||
int setcmd = FALSE;
|
||||
int16_t spd;
|
||||
switch(mesg){
|
||||
case CMD_SETSPEED:
|
||||
spd = (int16_t) (par*5);
|
||||
sprintf(cmd, "s 6 0 6 %d %d 0 0\n", (spd>>8), (spd&0xff));
|
||||
setcmd = TRUE;
|
||||
break;
|
||||
case CMD_STOP:
|
||||
sprintf(cmd, "s 6 0 2 0 0 0 0\n");
|
||||
setcmd = TRUE;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if(setcmd && !CANsend(sock, cmd)) return 1;
|
||||
for(int i = 0; i < NMOTORS; ++i){
|
||||
uint16_t id = 512 + ((i+1)<<3) + 3, cmdidx = PARIDX_GETSPEED;
|
||||
//uint8_t CANbuf[8] = {0};
|
||||
sprintf(cmd, "s %u 0x31 0 %d %d 0 0 0 0\n", id, (cmdidx>>8), (cmdidx&0xff));
|
||||
if(!CANsend(sock, cmd)) continue;
|
||||
double T0 = dtime();
|
||||
while(dtime() - T0 < TIMEOUT){
|
||||
if(1 != canberead(sock)) continue;
|
||||
int n = read(sock, recvBuff, Bufsiz-1);
|
||||
DBG("Got %d", n);
|
||||
if(n == 0){
|
||||
WARNX("Server disconnected");
|
||||
signals(1);
|
||||
}
|
||||
recvBuff[n] = 0;
|
||||
printf("%s", recvBuff);
|
||||
if(recvBuff[n-1] == '\n') recvBuff[n-1] = 0;
|
||||
LOGMSG("RECEIVE: %s", recvBuff);
|
||||
break;
|
||||
}
|
||||
}
|
||||
close(sock);
|
||||
signals(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
37
serialsockCANmanage/sersock.h
Normal file
37
serialsockCANmanage/sersock.h
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* This file is part of the SEWmanage project.
|
||||
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#ifndef SERSOCK_H__
|
||||
#define SERSOCK_H__
|
||||
|
||||
#define BUFLEN (1024)
|
||||
// timeout for answer from socket
|
||||
#define TIMEOUT (5.)
|
||||
// motors amount
|
||||
#define NMOTORS (3)
|
||||
|
||||
typedef enum{
|
||||
CMD_GETSPEED, // just get speed
|
||||
CMD_SETSPEED, // set given speed
|
||||
CMD_STOP, // stop motor
|
||||
} CANcmd;
|
||||
|
||||
int start_socket(char *path, CANcmd cmd, int par);
|
||||
|
||||
#endif // SERSOCK_H__
|
||||
Loading…
x
Reference in New Issue
Block a user