-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuser_linux.c
883 lines (777 loc) · 28.9 KB
/
user_linux.c
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
// SPDX-License-Identifier: MIT
/**************************************************************************************
*
* Copyright (c) 2024, Hilscher Gesellschaft fuer Systemautomation mbH. All Rights Reserved.
*
* Description: Linux specific implementation for finding firmware files, etc.
*
* Changes:
*
* Version Date Author Description
* ----------------------------------------------------------------------------------
* 1 02.01.24 SD changed licensing terms
*
**************************************************************************************/
#include "cifXToolkit.h"
#include <sys/types.h>
#include <sys/time.h>
#include <dirent.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <ctype.h>
#include "WarmstartFile.h"
#include "cifxlinux_internal.h"
char* g_szDriverBaseDir = NULL; /*!< Global base path to driver/cifX configuration data */
static const char* DEVICE_CONF_ALIAS_KEY = "alias=";
static const char* DEVICE_CONF_IRQ_KEY = "irq=";
static const char* DEVICE_CONF_IRQPRIO_KEY = "irqprio=";
static const char* DEVICE_CONF_IRQSCHED_KEY = "irqsched=";
#ifdef CIFX_TOOLKIT_DMA
static const char* DEVICE_CONF_DMA = "dma=";
#endif
#ifdef CIFXETHERNET
static const char* DEVICE_CONF_ETH = "eth=";
#endif
uint8_t severity_mapping[] = {
0xff, /* not used */
1, /* TRACE_LEVEL_DEBUG */
2, /* TRACE_LEVEL_INFO */
0xff, /* not used */
4, /* TRACE_LEVEL_WARNING */
0xff, /* not used */
0xff, /* not used */
0xff, /* not used */
8, /* TRACE_LEVEL_ERROR */
};
/*****************************************************************************/
/*! Print a trace message from cifX toolkit
* \param ptDevInstance Device instance the trace is coming from
* \param ulTraceLevel see TRACE_LVL_XXX defines
* \param szFormat printf style format string
* \param ... printf arguments */
/*****************************************************************************/
void USER_Trace(PDEVICEINSTANCE ptDevInstance, uint32_t ulTraceLevel, const char* szFormat, ...)
{
PCIFX_DEVICE_INTERNAL_T internaldev = (PCIFX_DEVICE_INTERNAL_T)ptDevInstance->pvOSDependent;
va_list vaList;
struct timeval time;
struct tm *local_tm;
gettimeofday(&time, NULL);
local_tm = localtime(&time.tv_sec);
va_start(vaList, szFormat);
if(NULL != internaldev->log_file)
{
fprintf(internaldev->log_file,
"<%u> %.2d.%.2d.%.4d %.2d:%.2d:%.2d.%.3ld.%.3ld: ",
severity_mapping[ulTraceLevel],
local_tm->tm_mday, local_tm->tm_mon + 1, local_tm->tm_year + 1900,
local_tm->tm_hour, local_tm->tm_min, local_tm->tm_sec,
time.tv_usec / 1000, time.tv_usec % 1000);
/* log file is given, so add this trace to our logfile */
vfprintf(internaldev->log_file, szFormat, vaList);
fprintf(internaldev->log_file, "\n");
fflush(internaldev->log_file);
} else
{
printf("<%u> %.2d.%.2d.%.4d %.2d:%.2d:%.2d.%.3ld.%3ld: ",
severity_mapping[ulTraceLevel],
local_tm->tm_mday, local_tm->tm_mon + 1, local_tm->tm_year + 1900,
local_tm->tm_hour, local_tm->tm_min, local_tm->tm_sec,
time.tv_usec / 1000, time.tv_usec % 1000);
/* No logfile, so print to console */
vprintf(szFormat, vaList);
printf("\n");
}
UNREFERENCED_PARAMETER(ulTraceLevel);
}
#define PARSER_BUFFER_SIZE 1024
char* ToLowerCase( char* str, int len) {
int i = 0;
if (str != NULL) {
while ((str[i] != 0) && (i<len)) {
str[i] = tolower(str[i]);
i++;
}
}
return str;
}
/*****************************************************************************/
/*! Reads a value from an config file (ini-file style)
* \param szFile Path to the ini file
* \param szKey Key to search for (including trailing '=')
* \param szValue Pointer to returned value (needs to be free'd by caller)
* \return !=0 on success */
/*****************************************************************************/
static int GetDeviceConfigString(const char* szFile, const char* szKey, char** szValue)
{
int ret = 0;
FILE* fd = fopen(szFile, "r");
if(NULL != fd)
{
/* File is open */
char* buffer = malloc(PARSER_BUFFER_SIZE);
/* Read file line by line */
while(NULL != fgets(buffer, PARSER_BUFFER_SIZE, fd))
{
char* key;
/* '#' marks a comment line in the device.conf file */
if(buffer[0] == '#')
continue;
/* Search for key in the input buffer */
key = strstr(ToLowerCase(buffer, strlen(szKey)), szKey);
if(NULL != key)
{
/* We've found the key */
int allocsize = strlen(key + strlen(szKey)) + 1;
int valuelen;
char* tempstring = (char*)malloc(allocsize);
strcpy(tempstring, key + strlen(szKey));
valuelen = strlen(tempstring);
/* strip all trailing whitespaces */
while( (tempstring[valuelen - 1] == '\n') ||
(tempstring[valuelen - 1] == '\r') ||
(tempstring[valuelen - 1] == ' ') )
{
tempstring[valuelen - 1] = '\0';
--valuelen;
}
*szValue = tempstring;
ret = 1;
break;
}
}
free(buffer);
fclose(fd);
}
return ret;
}
int path_exists(char* szPath)
{
struct stat s;
return stat(szPath, &s);
}
/*****************************************************************************/
/*! Internal helper function returning the path to a channel directory
* on the given device (e.g. /opt/cifx/deviceconfig/1250100/20004/channel0/)
* \param szPath Pointer to returned path
* \param iPathLen Length of the buffer passed in szPath
* \param ptDevInfo Device information (DeviceNr, SerialNr, ChannelNr)*/
/*****************************************************************************/
static void GetChannelDir(char* szPath, size_t iPathLen,PCIFX_DEVICE_INFORMATION ptDevInfo)
{
uint32_t ulSlotNr = ptDevInfo->ptDeviceInstance->ulSlotNumber;
/* if the rotary switch is set != 0 */
if (ulSlotNr)
{
snprintf(szPath, iPathLen,
"%s/deviceconfig/Slot_%d/channel%d/",
g_szDriverBaseDir,
(unsigned int)ulSlotNr,
(unsigned int)ptDevInfo->ulChannel);
if (path_exists(szPath)==0)
goto exit;
}
snprintf(szPath, iPathLen,
"%s/deviceconfig/%d/%d/channel%d/",
g_szDriverBaseDir,
(unsigned int)ptDevInfo->ulDeviceNumber,
(unsigned int)ptDevInfo->ulSerialNumber,
(unsigned int)ptDevInfo->ulChannel);
if (path_exists(szPath)==0)
goto exit;
snprintf(szPath, iPathLen,
"%s/deviceconfig/%s/channel%d/",
g_szDriverBaseDir,
ptDevInfo->ptDeviceInstance->szName,
(unsigned int)ptDevInfo->ulChannel);
if (path_exists(szPath)==0)
goto exit;
snprintf(szPath, iPathLen,
"%s/deviceconfig/FW/channel%d/",
g_szDriverBaseDir,
(unsigned int)ptDevInfo->ulChannel);
exit:
return;
}
/*****************************************************************************/
/*! Internal helper function returning the path to a channel directory
* on the given device (e.g. /opt/cifx/deviceconfig/1250100/20004/)
* \param szPath Pointer to returned path
* \param iPathLen Length of the buffer passed in szPath
* \param ptDevInfo Device information (DeviceNr, SerialNr, ChannelNr)*/
/*****************************************************************************/
static void GetDeviceDir(char* szPath, size_t iPathLen,PCIFX_DEVICE_INFORMATION ptDevInfo)
{
uint32_t ulSlotNr = ptDevInfo->ptDeviceInstance->ulSlotNumber;
/* if the rotary switch is set != 0 */
if (ulSlotNr)
{
snprintf(szPath, iPathLen,
"%s/deviceconfig/Slot_%d/",
g_szDriverBaseDir,
(unsigned int)ulSlotNr);
if (path_exists(szPath)==0)
goto exit;
}
snprintf(szPath, iPathLen,
"%s/deviceconfig/%d/%d/",
g_szDriverBaseDir,
(unsigned int)ptDevInfo->ulDeviceNumber,
(unsigned int)ptDevInfo->ulSerialNumber);
if (path_exists(szPath)==0)
goto exit;
snprintf(szPath, iPathLen,
"%s/deviceconfig/%s/",
g_szDriverBaseDir,
ptDevInfo->ptDeviceInstance->szName);
if (path_exists(szPath)==0)
goto exit;
snprintf(szPath, iPathLen,
"%s/deviceconfig/FW/",
g_szDriverBaseDir);
exit:
return;
}
/*****************************************************************************/
/*! Returns the number of firmware files to be downloaded on the given
* device/channel
* \param ptDevInfo Device information (DeviceNr, SerialNr, ChannelNr)
* \return Number of files (used for USER_GetFirmwareFile() */
/*****************************************************************************/
uint32_t USER_GetFirmwareFileCount(PCIFX_DEVICE_INFORMATION ptDevInfo)
{
char szPath[CIFX_MAX_FILE_NAME_LENGTH];
unsigned long ulRet = 0;
DIR* dir;
GetChannelDir(szPath, sizeof(szPath), ptDevInfo);
if(NULL != (dir = opendir(szPath)))
{
struct dirent* dirent;
while(NULL != (dirent = readdir(dir)))
{
char* szExt = strstr(dirent->d_name, ".");
if(NULL != szExt)
{
if( (0 == strncasecmp(szExt, HIL_FILE_EXTENSION_FIRMWARE, 4)) ||
(0 == strncasecmp(szExt, HIL_FILE_EXTENSION_OPTION, 4)) )
{
++ulRet;
}
}
}
closedir(dir);
}
return ulRet;
}
/*****************************************************************************/
/*! Returns filename of the file to be downloaded on the given device/channel
* \param ptDevInfo Device information (DeviceNr, SerialNr, ChannelNr)
* \param ulIdx Number of the file (0..USER_GetFirmwareFileCount)
* \param ptFileInfo Full and short filename of the file
* \return !=0 on success */
/*****************************************************************************/
int USER_GetFirmwareFile(PCIFX_DEVICE_INFORMATION ptDevInfo, uint32_t ulIdx, PCIFX_FILE_INFORMATION ptFileInfo)
{
char szPath[CIFX_MAX_FILE_NAME_LENGTH];
int ret = 0;
DIR* dir;
GetChannelDir(szPath, sizeof(szPath), ptDevInfo);
if(NULL != (dir = opendir(szPath)))
{
struct dirent* dirent;
unsigned long ulFile = 0;
while(NULL != (dirent = readdir(dir)))
{
char* szExt = strstr(dirent->d_name, ".");
if(NULL != szExt)
{
if( (0 == strncasecmp(szExt, HIL_FILE_EXTENSION_FIRMWARE, 4)) ||
(0 == strncasecmp(szExt, HIL_FILE_EXTENSION_OPTION, 4)) )
{
if(ulFile++ == ulIdx)
{
snprintf(ptFileInfo->szFullFileName, sizeof(ptFileInfo->szFullFileName),
"%s/%s", szPath, dirent->d_name);
strncpy(ptFileInfo->szShortFileName, dirent->d_name,
sizeof(ptFileInfo->szShortFileName));
ret = 1;
break;
}
}
}
}
closedir(dir);
}
return ret;
}
/*****************************************************************************/
/*! Returns the number of configuration files to be downloaded on the given
* device/channel
* \param ptDevInfo Device information (DeviceNr, SerialNr, ChannelNr)
* \return Number of files (used for USER_GetConfigurationFile() */
/*****************************************************************************/
uint32_t USER_GetConfigurationFileCount(PCIFX_DEVICE_INFORMATION ptDevInfo)
{
char szPath[CIFX_MAX_FILE_NAME_LENGTH];
unsigned long ulRet = 0;
DIR* dir;
GetChannelDir(szPath, sizeof(szPath), ptDevInfo);
if(NULL != (dir = opendir(szPath)))
{
struct dirent* dirent;
while(NULL != (dirent = readdir(dir)))
{
char* szExt = strstr(dirent->d_name, ".");
if(NULL != szExt)
{
if(0 == strncasecmp(szExt, HIL_FILE_EXTENSION_DATABASE, 4))
{
++ulRet;
}
}
}
closedir(dir);
}
return ulRet;
}
/*****************************************************************************/
/*! Returns filename of the file to be downloaded on the given device/channel
* \param ptDevInfo Device information (DeviceNr, SerialNr, ChannelNr)
* \param ulIdx Number of the file (0..USER_GetConfigurationFileCount)
* \param ptFileInfo Full and short filename of the file
* \return !=0 on success */
/*****************************************************************************/
int USER_GetConfigurationFile(PCIFX_DEVICE_INFORMATION ptDevInfo, uint32_t ulIdx, PCIFX_FILE_INFORMATION ptFileInfo)
{
char szPath[CIFX_MAX_FILE_NAME_LENGTH];
int ret = 0;
DIR* dir;
GetChannelDir(szPath, sizeof(szPath), ptDevInfo);
if(NULL != (dir = opendir(szPath)))
{
struct dirent* dirent;
unsigned long ulFile = 0;
while(NULL != (dirent = readdir(dir)))
{
char* szExt = strstr(dirent->d_name, ".");
if(NULL != szExt)
{
if(0 == strncasecmp(szExt, HIL_FILE_EXTENSION_DATABASE, 4))
{
if(ulFile++ == ulIdx)
{
snprintf(ptFileInfo->szFullFileName, sizeof(ptFileInfo->szFullFileName),
"%s/%s", szPath, dirent->d_name);
strncpy(ptFileInfo->szShortFileName, dirent->d_name,
sizeof(ptFileInfo->szShortFileName));
ret = 1;
break;
}
}
}
}
closedir(dir);
}
return ret;
}
/*****************************************************************************/
/*! Get warmstart parameters (for slave devices to be configured at startup)
* This is usually done by the user application, and not the toolkit
* \param ptDevInfo Device information (DeviceNr, SerialNr, ChannelNr)
* \param ptPacket Packet to send after starting the firmware
* \return !=0 if a packet is available */
/*****************************************************************************/
int USER_GetWarmstartParameters(PCIFX_DEVICE_INFORMATION ptDevInfo, CIFX_PACKET* ptPacket)
{
int ret = 0;
char szFile[CIFX_MAX_FILE_NAME_LENGTH];
void* pvFile;
uint32_t ulFileLen;
GetChannelDir(szFile, sizeof(szFile), ptDevInfo);
strcat(szFile, "warmstart.dat");
pvFile = OS_FileOpen(szFile, &ulFileLen);
if ( (pvFile == NULL) ||
(ulFileLen < sizeof(CIFX_WS_FILEHEADER)) )
{
if (errno != ENOENT) /* do not print an error if file does not exists */
{
if(g_ulTraceLevel & TRACE_LEVEL_ERROR)
{
USER_Trace(ptDevInfo->ptDeviceInstance,
TRACE_LEVEL_ERROR,
"Error opening warm start file! %s", strerror(errno));
}
}
} else
{
CIFX_WS_FILEHEADER tHeader = {0};
/* Read file header */
if (OS_FileRead( pvFile, 0, sizeof(tHeader), &tHeader) != sizeof(tHeader))
{
if(g_ulTraceLevel & TRACE_LEVEL_ERROR)
{
USER_Trace(ptDevInfo->ptDeviceInstance,
TRACE_LEVEL_ERROR,
"Error reading from warm start file!");
}
} else if( tHeader.ulCookie != CIFX_WS_WARMSTART_FILE_COOKIE)
{
if(g_ulTraceLevel & TRACE_LEVEL_ERROR)
{
USER_Trace(ptDevInfo->ptDeviceInstance,
TRACE_LEVEL_ERROR,
"Invalid warm start file cookie!");
}
} else if( tHeader.ulDataLen > sizeof(*ptPacket))
{
if(g_ulTraceLevel & TRACE_LEVEL_ERROR)
{
USER_Trace(ptDevInfo->ptDeviceInstance,
TRACE_LEVEL_ERROR,
"Invalid warm start file length!");
}
} else
{
/* Read file data */
if (OS_FileRead( pvFile, sizeof(tHeader), tHeader.ulDataLen, (void*)ptPacket) != tHeader.ulDataLen)
{
if(g_ulTraceLevel & TRACE_LEVEL_ERROR)
{
USER_Trace(ptDevInfo->ptDeviceInstance,
TRACE_LEVEL_ERROR,
"Error reading user data from warm start file!");
}
} else
{
ret = 1;
}
}
if( NULL != pvFile)
OS_FileClose(pvFile);
}
return ret;
}
/*****************************************************************************/
/*! Retrieve the full and short filename of a bootloader file to download to
* the given device
* \param ptDevInstance Device Instance containing all device data
* \param ptFileInfo Pointer to returned file information */
/*****************************************************************************/
void USER_GetBootloaderFile(PDEVICEINSTANCE ptDevInstance, PCIFX_FILE_INFORMATION ptFileInfo)
{
switch(ptDevInstance->eChipType)
{
case eCHIP_TYPE_NETX500:
case eCHIP_TYPE_NETX100:
{
FILE* fd;
snprintf(ptFileInfo->szFullFileName, sizeof(ptFileInfo->szFullFileName),
"%s/%s",
g_szDriverBaseDir, "NETX100-BSL.bin");
strncpy(ptFileInfo->szShortFileName,
"NETX100-BSL.bin", sizeof(ptFileInfo->szShortFileName));
if(NULL != (fd = fopen(ptFileInfo->szFullFileName, "r")))
{
/* New bootloader found */
fclose(fd);
} else
{
/* new bootloader not found. Assume user still has the "old" cifX bootloader */
snprintf(ptFileInfo->szFullFileName, sizeof(ptFileInfo->szFullFileName),
"%s/%s",
g_szDriverBaseDir, "NXCIF50-RTE.bin");
strncpy(ptFileInfo->szShortFileName,
"NXCIF50-RTE.bin", sizeof(ptFileInfo->szShortFileName));
}
}
break;
case eCHIP_TYPE_NETX52:
snprintf(ptFileInfo->szFullFileName, sizeof(ptFileInfo->szFullFileName),
"%s/%s",
g_szDriverBaseDir, "NETX52-BSL.bin");
strncpy(ptFileInfo->szShortFileName,
"NETX52-BSL.bin", sizeof(ptFileInfo->szShortFileName));
break;
case eCHIP_TYPE_NETX51:
snprintf(ptFileInfo->szFullFileName, sizeof(ptFileInfo->szFullFileName),
"%s/%s",
g_szDriverBaseDir, "NETX51-BSL.bin");
strncpy(ptFileInfo->szShortFileName,
"NETX51-BSL.bin", sizeof(ptFileInfo->szShortFileName));
break;
case eCHIP_TYPE_NETX50:
/* new bootloader for netX50 will default to NETX50-BSL.bin (which is the default toolkit delivery) */
snprintf(ptFileInfo->szFullFileName, sizeof(ptFileInfo->szFullFileName),
"%s/%s",
g_szDriverBaseDir, "NETX50-BSL.bin");
strncpy(ptFileInfo->szShortFileName,
"NETX50-BSL.bin", sizeof(ptFileInfo->szShortFileName));
break;
default:
/* This should never happen */
break;
}
}
/*****************************************************************************/
/*! Retrieve the alias name of a card. This name can be alternatively used
* by a application in a call to xChannelOpen. This name must be unique
* \param ptDevInfo Device information (DeviceNr, SerialNr)
* \param ulMaxLen Maximum Length of alias
* \param szAlias Pointer to returned alias name (empty = no alias) */
/*****************************************************************************/
void USER_GetAliasName(PCIFX_DEVICE_INFORMATION ptDevInfo, uint32_t ulMaxLen, char* szAlias)
{
char szFile[CIFX_MAX_FILE_NAME_LENGTH];
char* szTempAlias = NULL;
GetDeviceDir(szFile, sizeof(szFile), ptDevInfo);
strcat(szFile, "/device.conf");
/* Read alias from file */
if(GetDeviceConfigString(szFile, DEVICE_CONF_ALIAS_KEY, &szTempAlias))
{
strncpy(szAlias, szTempAlias, ulMaxLen);
free(szTempAlias);
}
}
/*****************************************************************************/
/*! Returns OS file information for the given device/channel and Idx
* passed as argument.
* \param ptDevInfo DeviceInfo including channel number, for which the
* firmware file should be delivered
* \param ptFileInfo Short and full file name of the firmware. Used in
* calls to OS_OpenFile()
* \return != 0 on success */
/*****************************************************************************/
int USER_GetOSFile(PCIFX_DEVICE_INFORMATION ptDevInfo, PCIFX_FILE_INFORMATION ptFileInfo)
{
char szPath[CIFX_MAX_FILE_NAME_LENGTH];
int ret = 0;
DIR* dir;
GetDeviceDir(szPath, sizeof(szPath), ptDevInfo);
if(NULL != (dir = opendir(szPath)))
{
struct dirent* dirent;
while(NULL != (dirent = readdir(dir)))
{
char* szExt = strstr(dirent->d_name, ".");
if(NULL != szExt)
{
if(0 == strncasecmp(szExt, HIL_FILE_EXTENSION_FIRMWARE, 4))
{
snprintf(ptFileInfo->szFullFileName, sizeof(ptFileInfo->szFullFileName),
"%s/%s", szPath, dirent->d_name);
strncpy(ptFileInfo->szShortFileName, dirent->d_name,
sizeof(ptFileInfo->szShortFileName));
ret = 1;
break;
}
}
}
closedir(dir);
}
return ret;
}
/*****************************************************************************/
/*! Check if the interrupts are to be enabled on this device
* \param ptDevInstance Device Instance containing all device data
* \return !=0 to enable IRQs */
/*****************************************************************************/
int USER_GetInterruptEnable(PCIFX_DEVICE_INFORMATION ptDevInfo)
{
char szFile[CIFX_MAX_FILE_NAME_LENGTH];
char* szTempIrq = NULL;
int ret = 0;
GetDeviceDir(szFile, sizeof(szFile), ptDevInfo);
strcat(szFile, "/device.conf");
/* Read IRQ enable from file */
if(GetDeviceConfigString(szFile, DEVICE_CONF_IRQ_KEY, &szTempIrq))
{
if(0 == strcasecmp("yes", szTempIrq))
{
PCIFX_DEVICE_INTERNAL_T internaldev = (PCIFX_DEVICE_INTERNAL_T)ptDevInfo->ptDeviceInstance->pvOSDependent;
/* Check if we have a uio handle. If not, we cannot handle IRQs */
if(-1 == internaldev->userdevice->uio_fd)
{
if(g_ulTraceLevel & TRACE_LEVEL_ERROR)
{
USER_Trace(ptDevInfo->ptDeviceInstance,
TRACE_LEVEL_ERROR,
"Trying to activate IRQ's on a device, that does not have a uio connection. Fallback to polling mode!");
}
} else
{
char* szTempData = NULL;
uint32_t ulEnableInt = 0;
/* NOTE: We don't want do enable interrupts here! We just want to check if the device provides interrupt support! */
/* ret = ENOSYS => !to be backwards compatible! - verification not provided but device supports interrupt */
/* ret = EIO => interrupt not supported, the device did not register for any interrupt */
/* NOTE: For SPIDEV devices, the IRQ trigger file is opened read only! In this case the uio_num is -1. */
if ((internaldev->userdevice->uio_num >= 0) && ((ret=write(internaldev->userdevice->uio_fd, (void*)&ulEnableInt, sizeof(uint32_t))) < 0) && (errno != ENOSYS))
{
ret = 0;
if(g_ulTraceLevel & TRACE_LEVEL_ERROR)
{
USER_Trace(ptDevInfo->ptDeviceInstance,
TRACE_LEVEL_ERROR,
"Error enabling interrupts (%s), fallback to polling mode!",
strerror(errno));
if (errno == EIO)
{
if(g_ulTraceLevel & TRACE_LEVEL_ERROR)
{
USER_Trace(ptDevInfo->ptDeviceInstance,
TRACE_LEVEL_ERROR,
"No irq for device registered!");
}
}
}
} else
{
/* Check for custom priority and set them in CIFX_DEVICE_INTERNAL_T to use them in OS_EnableInterrupts() */
if(GetDeviceConfigString(szFile, DEVICE_CONF_IRQPRIO_KEY, &szTempData))
{
internaldev->set_irq_prio = 1;
internaldev->irq_prio = atoi(szTempData);
if(g_ulTraceLevel & TRACE_LEVEL_INFO)
{
USER_Trace(ptDevInfo->ptDeviceInstance,
TRACE_LEVEL_INFO,
"Using custom IRQ thread priority (%d).",
internaldev->irq_prio);
}
free(szTempData);
} else
{
internaldev->set_irq_prio = 0;
}
/* Check for custom scheduling policy */
if(GetDeviceConfigString(szFile, DEVICE_CONF_IRQSCHED_KEY, &szTempData))
{
internaldev->set_irq_scheduler_algo = 1;
if(strcasecmp("fifo", szTempData) == 0)
{
/* SCHED_FIFO */
internaldev->irq_scheduler_algo = SCHED_FIFO;
if(g_ulTraceLevel & TRACE_LEVEL_INFO)
{
USER_Trace(ptDevInfo->ptDeviceInstance,
TRACE_LEVEL_INFO,
"Using custom IRQ thread scheduling algorithm (SCHED_FIFO).");
}
} else if(strcasecmp("rr", szTempData) == 0)
{
/* SCHED_RR */
internaldev->irq_scheduler_algo = SCHED_RR;
if(g_ulTraceLevel & TRACE_LEVEL_INFO)
{
USER_Trace(ptDevInfo->ptDeviceInstance,
TRACE_LEVEL_INFO,
"Using custom IRQ thread scheduling algorithm (SCHED_RR).");
}
} else
{
internaldev->set_irq_scheduler_algo = 0;
if(g_ulTraceLevel & TRACE_LEVEL_INFO)
{
USER_Trace(ptDevInfo->ptDeviceInstance,
TRACE_LEVEL_INFO,
"Trying to override IRQ thread scheduling policy, but unknown policy was given (%s)", szTempData);
}
}
free(szTempData);
} else
{
internaldev->set_irq_scheduler_algo = 0;
}
ret = 1;
}
}
}
free(szTempIrq);
}
if(g_ulTraceLevel & TRACE_LEVEL_INFO)
{
USER_Trace( ptDevInfo->ptDeviceInstance, TRACE_LEVEL_INFO, "%s", (ret)?"IRQ-Mode enabled!":"Polling Mode enabled!");
}
return ret;
}
#ifdef CIFX_TOOLKIT_DMA
/*****************************************************************************/
/*! Check if the DMA mode is enabled
* \param ptDevInstance Device Instance containing all device data
* \return CIFX_TOOLKIT_DMA_MODE_E
*/
/*****************************************************************************/
int USER_GetDMAMode(PCIFX_DEVICE_INFORMATION ptDevInfo)
{
char szFile[CIFX_MAX_FILE_NAME_LENGTH];
char* szTempDMA = NULL;
GetDeviceDir(szFile, sizeof(szFile), ptDevInfo);
strcat(szFile, "/device.conf");
if(GetDeviceConfigString(szFile, DEVICE_CONF_DMA, &szTempDMA))
{
if(0 == strcasecmp("yes", szTempDMA))
{
if(g_ulTraceLevel & TRACE_LEVEL_INFO)
{
USER_Trace(ptDevInfo->ptDeviceInstance, 0, "DMA mode enabled!");
}
return eDMA_MODE_ON;
}
free(szTempDMA);
}
if(g_ulTraceLevel & TRACE_LEVEL_INFO)
{
USER_Trace(ptDevInfo->ptDeviceInstance, 0, "No DMA support!");
}
return eDMA_MODE_OFF;
}
#endif
#ifdef CIFXETHERNET
/*****************************************************************************/
/*! Check if the ethernet support is enabled
* \param ptDevInstance Device Instance containing all device data
* \return CIFX_TOOLKIT_DMA_MODE_E
*/
/*****************************************************************************/
int USER_GetEthernet(PCIFX_DEVICE_INFORMATION ptDevInfo)
{
char szFile[CIFX_MAX_FILE_NAME_LENGTH];
char* szTempEth = NULL;
int ret = 0;
GetDeviceDir(szFile, sizeof(szFile), ptDevInfo);
strcat(szFile, "/device.conf");
if(GetDeviceConfigString(szFile, DEVICE_CONF_ETH, &szTempEth))
{
if(0 == strcasecmp("yes", szTempEth))
{
if(g_ulTraceLevel & TRACE_LEVEL_INFO)
{
USER_Trace(ptDevInfo->ptDeviceInstance, 0, "Ethernet support enabled!");
}
ret = 1;
}
free(szTempEth);
} else
{
if(g_ulTraceLevel & TRACE_LEVEL_INFO)
{
USER_Trace(ptDevInfo->ptDeviceInstance, 0, "No ethernet support!");
}
}
return ret;
}
#endif
/*****************************************************************************/
/*! Get actual I/O buffer caching mode for the given device
* \param ptDevInfo Device Information
* \return eCACHED_MODE_ON to enable caching */
/*****************************************************************************/
int USER_GetCachedIOBufferMode(PCIFX_DEVICE_INFORMATION ptDevInfo)
{
return eCACHED_MODE_OFF;
}