summaryrefslogtreecommitdiffstats
path: root/src/linuxaldl.c
blob: 8ffc4be468b66e1b64ac4e4dfddaca0f36683294 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*(C) copyright 2008, Steven Snyder, All Rights Reserved

  Steven T. Snyder, <stsnyder@ucla.edu> http://www.steventsnyder.com

  LICENSING INFORMATION:
  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 <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/signal.h>
#include <fcntl.h>
#include <popt.h> // for command line parsing
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h> // for memcpy
#include <errno.h>
#include "linuxaldl.h"
#include "sts_serial.h"
#include "linuxaldl_definitions.h"
#include "linuxaldl_gui.h"

// global variables
// =================================================

linuxaldl_settings aldl_settings = { 
	.aldl_definition_table = aldl_definition_table,
	.scan_interval = 150,
	.scan_timeout = 100,
};

// ============================================================================
//
//					linuxaldl
//  					 main ()
//
// ============================================================================

int main(int argc, const char* argv[]){
	int res; // temporary storage for function results

	int guimode = 0;

	// ========================================================================
	// 			COMMAND LINE OPTION PARSING 
	// ========================================================================

	poptContext  popt_aldl;

	// popt option table
	// -----------------
	struct poptOption aldl_opt_table[] = {
		{ "serial",'\0',
		  POPT_ARG_STRING | POPT_ARGFLAG_ONEDASH,&aldl_settings.aldlportname,0,
		  "Serial port the aldl interface is on",
		  "/dev/ttyUSB0" },
		{ "mask",'\0',
		  POPT_ARG_STRING | POPT_ARGFLAG_ONEDASH,&aldl_settings.aldldefname,0,
		  "ALDL code definition to use",
		  "DF"},
		POPT_AUTOHELP
		{ NULL, 0, 0, NULL, 0, 0, NULL}
	};

	// popt context
	popt_aldl = poptGetContext(NULL, argc, argv,aldl_opt_table,0);
	poptSetOtherOptionHelp(popt_aldl,"[logfile.log]\nTo use GUI: linuxaldl [-serial=/dev/ttyUSB0]");

	if (argc < 2) { poptPrintUsage(popt_aldl,stderr,0); return 1; }

	res = poptGetNextOpt(popt_aldl); // parse the command line arguments

	// if no serial port selected, print usage instructions and exit
	if (aldl_settings.aldlportname == NULL) {
		poptPrintUsage(popt_aldl,stderr,0);
		return 1; 
	}

	aldl_settings.logfilename = (char*)poptGetArg(popt_aldl);
	// if no log file specified or no definition file specified
	if (aldl_settings.logfilename == NULL || 
	    poptPeekArg(popt_aldl) != NULL || 
	    aldl_settings.aldldefname == NULL) {
		guimode = 1;
	} else { 
		// In command line mode, choose definition using the -mask=DEF argument
		// get the aldl definition address
		aldl_settings.definition = aldl_get_definition(NULL, aldl_settings.aldldefname);

		// if no definition by that name exists, exit
		if (aldl_settings.definition == NULL) {
			fprintf(stderr,"Error: No definition with mask \"%s\" found.",
				aldl_settings.aldldefname);
			fprintf(stderr,"Consult the documentation.\n");							
			fprintf(stderr," Note: definition names are case sensitive.\n");
			return -1;	
		} else {
			fprintf(stderr, "Definition \"%s\" selected.\n", aldl_settings.definition->name);

		}
	}
	poptFreeContext(popt_aldl); // free the popt context

	// ========================================================================
	// 							CONNECT / VERIFY ALDL INTERFACE 
	// ========================================================================

	// Establish a connection to the aldl
	// ===================================
	printf("Trying to connect to ALDL interface on %s...\n", aldl_settings.aldlportname);

	// connect to the device and set the serial port settings
	aldl_settings.faldl = serial_connect(aldl_settings.aldlportname,
					     O_RDWR | O_NOCTTY | O_NONBLOCK,
					     aldl_settings.definition->basic_baudrate);
	if (aldl_settings.faldl == -1) {
		fprintf(stderr," Couldn't open to %s\n",aldl_settings.aldlportname);
		return -1;
	}

	// set the custom baud rate:
	// the ALDL interface operates at 8192. it would be preferable to get as close to this
	// baud rate as possible. if it cannot be set; it should still work at the standard
	// baud rate of 9600; the framing errors aren't excessive enough to cause problems,
	// and will be caught by bad checksums.
	if (set_custom_baud_rate(aldl_settings.faldl, aldl_settings.definition->ideal_baudrate)!=0) {
		fprintf(stderr," Couldn't set baud rate to %d. Using standard rate (%d).\n",
			aldl_settings.definition->ideal_baudrate,
			aldl_settings.definition->basic_baudrate);
		fprintf(stderr," There may be framing errors.\n");
	}
	
	// verify the aldl
	if (verifyaldl()<0) {
		fprintf(stderr," ALDL verification failure. No response from ECM.\n");
		tcflush(aldl_settings.faldl, TCIOFLUSH);
		close(aldl_settings.faldl);
		return -1;
	}

	// GUI mode if no .log file specified at the command line 
	if (guimode) {
		// GUI mode
		linuxaldl_gui(argc, (char**)argv);
	} else {
		// Open the .log file for writing, create if it doesnt exist
		// ------------------------------------------------------------
		aldl_settings.flogfile = open(aldl_settings.logfilename,O_RDWR | O_CREAT, 0666);
		if (aldl_settings.flogfile == -1) {
			fprintf(stderr,"Unable to open/create %s for writing.\n",aldl_settings.logfilename);
			return 0;	}
		printf("Opened %s for writing.\n",aldl_settings.logfilename);
		// Read from the aldl
		// ------------------
		res = aldl_scan_and_log(aldl_settings.flogfile);
		if (res==-1)
			fprintf(stderr,"Error: Fatal read error occured. log file may be corrupted.\n");
		else
			printf("Received %d bytes from device and wrote to file: %s.\n", res,aldl_settings.logfilename);
	
		close(aldl_settings.flogfile);
	}


	// ========================================================================
	// 				CLEANUP (FLUSH SERIAL LINE, CLOSE PORT) 
	// ========================================================================

	// discard any unwritten data
	tcflush(aldl_settings.faldl, TCIOFLUSH);

	// close the port
	close(aldl_settings.faldl);
	printf("Connection closed.\n"); 
	return 0;
}



// ============================================================
//
// 			linuxaldl general function definitions 
//
// ============================================================
// (mostly used for the command line interface but also main())

// wake up / verify the aldl
// This function should check that the aldl interface is working by listening
// for ECM chatter, or some other operation to determine whether there is
// a line connected to the serial port.
int verifyaldl()
{
	//XXX NOT IMPLEMENTED
	return 0;
}


int aldl_scan_and_log(int fd)
{
	//XXX NOT IMPLEMENTED
	return 0;
}

// sends an artibtrary aldl message contained in the buffer msg_buf.
// the checksum must be set in the buffer by the caller.
// the following macros can be used as arguments:
//		_ALDL_MESSAGE_MODE8
//		_ALDL_MESSAGE_MODE9
// which use the mode 8 and mode 9 message definitions from the
// current aldl definition.
// returns 0 on success.
int send_aldl_message(char* msg_buf, unsigned int size)
{
	int res;

#ifdef _LINUXALDL_DEBUG
	printf("Sending sequence: ");
	fprinthex(stdout,msg_buf,size);
	printf("\n");
#endif

	res = write(aldl_settings.faldl,&msg_buf,size);
	if (res <= 0)
		return -1;
	else if ((unsigned)res<size)
		return -1;
	tcdrain(aldl_settings.faldl);

	return res;
}

// requests a mode1 message from the ECM using the currently loaded
// aldl definition. 
// returns the number of bytes received, if the message was complete.
//  0 is no response/timeout/partial message
// -1 is returned if the checksum is bad.
// XXX this is likely to fail if mode 8 is not set, because the call to read
// will in all probability return part of a normal mode message. a correct
// implementation should wait for a mode 1 message header in the response,
// so that mode 8 isn't even required.
int get_mode1_message(char* inbuffer, unsigned int size)
{
	int res;
	char outbuffer[__MAX_REQUEST_SIZE]; // max request size defined in linuxaldl_definitions.h

	aldl_definition* def = aldl_settings.definition;
	unsigned int mode1_len = def->mode1_response_length;

	if (size < mode1_len) {
		printf("Read buffer must be at least %d bytes.\n",mode1_len);
		return -1;
	}

	/* Enter Mode1 if required */
	if (def->mode1_request_length) {
		// put the mode 1 request message and checksum in the output buffer
		memcpy(outbuffer, def->mode1_request, def->mode1_request_length-1);
		outbuffer[def->mode1_request_length-1] = get_checksum(outbuffer, def->mode1_request_length-1);
		
		// form the response message start sequence
		char seq[] = { def->mode1_request[0], 0x52+def->mode1_response_length, 0x01};
		
		// flush the serial receive buffer
		tcflush(aldl_settings.faldl,TCIFLUSH);
		
		// write the request to the serial interface
		write(aldl_settings.faldl,&outbuffer,def->mode1_request_length); 
		// wait for the bytes to be written
		tcdrain(aldl_settings.faldl); 

		// wait for response from ECM
		// read sequence, 50msec timeout
		res=read_sequence(aldl_settings.faldl, inbuffer, mode1_len,
				  seq, 3, 0, 
				  aldl_settings.scan_timeout*1000);
	} else {
		// XXX what sequence to match for 160baud rate?
	}

	if (res<0) {
		fprintf(stderr,"Error receiving mode1 message: %s\n",strerror(errno));
		return -1;
	}
	if ((unsigned)res<mode1_len) {
		
#ifdef _LINUXALDL_DEBUG
		fprintf(stderr,"MODE1 timeout occured. (Received %d/%d bytes)\n",res,mode1_len);
#endif
		return 0;
	}

	char checksum = get_checksum(inbuffer,mode1_len-1);
	if (inbuffer[mode1_len-1]!=checksum) {
		fprintf(stderr,"MODE 1 bad checksum.\n");
		return -1;
	}

	return res;
}

// reads up to len bytes into inbuffer from the interface.
// listens for a maximum of timeout seconds.
// returns -1 on failure, 0 on timeout with no bytes received,
// and otherwise returns the number of bytes received 
int aldl_listen_raw(char* inbuffer, unsigned int len, int timeout)
{
	int res;
	res = readwithtimeout(aldl_settings.faldl,inbuffer,len,timeout);
	return res;
}

// calculates the single-byte checksum, summing from the start of buffer
// through len bytes. the checksum is calculated by adding each byte
// together and ignoring overflow, then taking the two's complement and adding 1
char get_checksum(char* buffer, unsigned int len)
{
	char acc = 0x00;

	unsigned int i;
	for (i=0; i<len; i++) {
		//printf("%d,",buffer[i]);
		//if (!(i%16)) printf("\n");
		acc+=buffer[i];
	}
	
	acc=0xFF-acc;
	acc+=0x01;
	//printf("Checksum: %d\n",acc);

	return acc;
}

aldl_definition* aldl_get_definition(const char* defname, const char *defmask)
{
	int index = 0;
	aldl_definition* result = aldl_definition_table[index];

	while (result) {
		if (defmask && !strcmp(defmask,result->mask))
			break;
		if (defname && !strcmp(defname,result->name))
			break;
		result = aldl_definition_table[++index];
	} 
	return result;
}


// updates data_set_floats and/or data_set_strings using the current data_set_raw bytes.
// if the flags argument is ALDL_UPDATE_STRINGS then only sets will be updated.
// if flags is ALDL_UPDATE_FLOATS then only floats will be updated, and the data_set_strings
// array will not be modified in any way.
// if flags is ALDL_UPDATE_FLOATS|ALDL_UPDATE_STRINGS, then both will be updated.
// if the aldl_settings.data_set_floats or aldl_settings.data_set_strings arrays have
// not yet been initialized (e.g. are NULL pointers) then they will not be modified.
void aldl_update_sets(int flags)
{
	unsigned int i=0;
	byte_def_t* defs = aldl_settings.definition->mode1_def;
	byte_def_t* cur_def;

	for (i = 0 ; defs[i].label ; i++) {
		cur_def = defs+i;

		switch (cur_def->operation) {
		case ALDL_OP_SCALAR: {
			float converted_val;
			char *new_data_string = NULL;

			if (cur_def->bits == 8) {
				converted_val = (((float)aldl_settings.data_set_raw[cur_def->byte_offset-1]) * cur_def->op_factor) + cur_def->op_offset;
			} else if (cur_def->bits == 16) {
				converted_val = ((float)aldl_settings.data_set_raw[cur_def->byte_offset-1]) * 256 + ((float)aldl_settings.data_set_raw[cur_def->byte_offset]);
				converted_val = (converted_val*cur_def->op_factor) + cur_def->op_offset;
			} else {
				// XXX log some sort of error for non 8/16bpp?
				continue;
			}
			if ((flags & ALDL_UPDATE_FLOATS) && aldl_settings.data_set_floats != NULL)
				aldl_settings.data_set_floats[i] = converted_val;
			
			// convert the result to a string
			if ((flags & ALDL_UPDATE_STRINGS) && aldl_settings.data_set_strings != NULL) {
				new_data_string=malloc(10); // allocate ten bytes for the string
				snprintf(new_data_string,10,"%.1f",converted_val); // convert the floating point value to a string
				// if there is currently a string registered, free it
				if (aldl_settings.data_set_strings[i] != NULL)
					free(aldl_settings.data_set_strings[i]);
				
				// register the new string in the table
				aldl_settings.data_set_strings[i] = new_data_string;
			}
			break;
		}
		case ALDL_OP_MAP: {
			float converted_val;
			char *new_data_string = NULL;

			if (cur_def->bits == 8) {
				converted_val = cur_def->map[(unsigned char)aldl_settings.data_set_raw[cur_def->byte_offset-1]];
			} else {
				// XXX log some sort of error for non-8bpp?
				continue;
			}
			if ((flags & ALDL_UPDATE_FLOATS) && aldl_settings.data_set_floats != NULL)
				aldl_settings.data_set_floats[i] = converted_val;
			
			// convert the result to a string
			if ((flags & ALDL_UPDATE_STRINGS) && aldl_settings.data_set_strings != NULL) {
				new_data_string=malloc(10); // allocate ten bytes for the string
				snprintf(new_data_string,10,"%.1f",converted_val); // convert the floating point value to a string
				// if there is currently a string registered, free it
				if (aldl_settings.data_set_strings[i] != NULL)
					free(aldl_settings.data_set_strings[i]);
				
				// register the new string in the table
				aldl_settings.data_set_strings[i] = new_data_string;
			}
			break;
		}
		case ALDL_OP_BIT:
			if (flags & ALDL_UPDATE_FLOATS) {
				aldl_settings.data_set_floats[i] = (aldl_settings.data_set_raw[cur_def->byte_offset-1] & cur_def->b_bit) ? 1 : 0;
			} 
			if (flags & ALDL_UPDATE_STRINGS) {
				// if there is currently a string registered, free it
				if (aldl_settings.data_set_strings[i] != NULL)
					free(aldl_settings.data_set_strings[i]);
				// register the new string in the table
				aldl_settings.data_set_strings[i] = strdup((aldl_settings.data_set_raw[cur_def->byte_offset-1] & cur_def->b_bit) ? cur_def->b_set : cur_def->b_unset);
			}

			break;
		case ALDL_OP_SEPERATOR:
			break; /* Deliberately ignore this */
		default:
			// XXX log some sort of error here?
			continue;
		}



		//fprintf(stderr,"Updating element %s\n",cur_def->label);
	}
}