File: interface.c

package info (click to toggle)
jpnevulator 2.3.6-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 300 kB
  • sloc: ansic: 1,584; makefile: 95
file content (118 lines) | stat: -rw-r--r-- 4,590 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* jpnevulator - serial reader/writer
 * Copyright (C) 2006-2020 Freddy Spierenburg
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "options.h"
#include "jpnevulator.h"
#include "interface.h"
#include "list.h"

void interfaceInitialize(void) {
	listInitialize(&_jpnevulatorOptions.interface);
}

static int cmpr(void *p,void *q) {
	return(!strcasecmp(((struct interface *)p)->name,(char *)q));
}

enum interfaceRtrn interfaceAdd(char *name,int (*interfaceOpen)(char *,int),int (*interfaceControlGet)(int,char *),void (*interfaceControlWrite)(FILE *,int),void (*interfaceClose)(int)) {
	struct interface *interface;
	int charactersPrinted;
	char error[1024];
	char *alias;
	/* Was an interface name given? */
	if(name==NULL) {
		/* Nope. Since the interface name is optional for certain types of interfaces, make sure
		 * we have some sort of empty value to work with. */
		name="";
	} else {
		/* Is this interface already present in our list of interfaces? */
		if(listSearch(&_jpnevulatorOptions.interface,cmpr,(void *)name)!=NULL) {
			return(interfaceRtrnDouble);
		}
	}
	interface=(struct interface *)malloc(sizeof(struct interface));
	if(interface==NULL) {
		return(interfaceRtrnMemory);
	}
	/* Did the user give us an alias for the interface name? */
	if((alias=strstr(name,_jpnevulatorOptions.aliasSeparator))!=NULL) {
		/* Yes, so split the alias from the name. */
		*alias='\0';
		/* Advance the alias to the real beginning of it. */
		alias+=strlen(_jpnevulatorOptions.aliasSeparator);
		/* And put it in our alias reference. */
		charactersPrinted=sprintf(interface->alias,"%.*s",(int)sizeof(interface->alias)-1,alias);
		if(charactersPrinted!=strlen(alias)) {
			fprintf(stderr,"%s: interface alias %s truncated to %d chars -> %s\n",PROGRAM_NAME,alias,charactersPrinted,interface->alias);
		}
	} else {
		/* No, empty the alias. */
		strcpy(interface->alias,"");
	}
	charactersPrinted=sprintf(interface->name,"%.*s",(int)sizeof(interface->name)-1,name);
	if(charactersPrinted!=strlen(name)) {
		fprintf(stderr,"%s: interface %s truncated to %d chars -> %s\n",PROGRAM_NAME,name,charactersPrinted,interface->name);
	}
	interface->fd=interfaceOpen(interface->name,sizeof(interface->name)-1);
	if(interface->fd==-1) {
		snprintf(error,sizeof(error)-1,"%s: Unable to open interface %s",PROGRAM_NAME,interface->name);
		perror(error);
		return(interfaceRtrnOpen);
	}
	/* Initialize the byte count. We have not received/send any bytes yet. */
	interface->byteCount=0UL;
	/* Put the control call-back in place and get the current state of the control bits if needed. */
	interface->controlGet=interfaceControlGet;
	interface->controlWrite=interfaceControlWrite;
	if(boolIsSet(_jpnevulatorOptions.control)) {
		interface->control=interfaceControlGet(interface->fd,interface->name);
	}
	/* Add our interface to the list of interfaces. */
	if(listAppend(&_jpnevulatorOptions.interface,(void *)interface)!=listRtrnOk) {
		fprintf(stderr,"%s: unable to add interface %s to the list of interfaces!\n",PROGRAM_NAME,interface->name);
		return(interfaceRtrnList);
	}
	/* Store a reference to the function to close this interface. */
	interface->close=interfaceClose;
	/* Advance to this next position. so all interfaces will be inline with the order the user gave us. */
	listNext(&_jpnevulatorOptions.interface);
	return(interfaceRtrnOk);
}

int interfaceControlGet(struct interface *interface) {
	return(interface->controlGet(interface->fd,interface->name));
}

void interfaceControlWrite(struct interface *interface,FILE *output,int control) {
	interface->controlWrite(output,control);
}

static void garbageCollect(void *data) {
	struct interface *interface;
	interface=(struct interface *)data;
	interface->close(interface->fd);
	free(interface);
}

void interfaceDestroy(void) {
	listDestroy(&_jpnevulatorOptions.interface,garbageCollect);
}