diff options
Diffstat (limited to 'Src/Network/base')
-rw-r--r-- | Src/Network/base/.svn/dir-prop-base | 14 | ||||
-rw-r--r-- | Src/Network/base/.svn/entries | 164 | ||||
-rw-r--r-- | Src/Network/base/.svn/prop-base/DriverConfiguration.c.svn-base | 5 | ||||
-rw-r--r-- | Src/Network/base/.svn/prop-base/DriverConfiguration.h.svn-base | 5 | ||||
-rw-r--r-- | Src/Network/base/.svn/text-base/Board.c.svn-base | 51 | ||||
-rw-r--r-- | Src/Network/base/.svn/text-base/Board.h.svn-base | 63 | ||||
-rw-r--r-- | Src/Network/base/.svn/text-base/DriverConfiguration.c.svn-base | 608 | ||||
-rw-r--r-- | Src/Network/base/.svn/text-base/DriverConfiguration.h.svn-base | 239 | ||||
-rw-r--r-- | Src/Network/base/Board.c | 51 | ||||
-rw-r--r-- | Src/Network/base/Board.h | 63 | ||||
-rw-r--r-- | Src/Network/base/DriverConfiguration.c | 608 | ||||
-rw-r--r-- | Src/Network/base/DriverConfiguration.h | 239 |
12 files changed, 2110 insertions, 0 deletions
diff --git a/Src/Network/base/.svn/dir-prop-base b/Src/Network/base/.svn/dir-prop-base new file mode 100644 index 0000000..c4fbe3b --- /dev/null +++ b/Src/Network/base/.svn/dir-prop-base @@ -0,0 +1,14 @@ +K 16 +bugtraq:logregex +V 67 +(?:[Bb]ugs?|[Ii]ssues?|[Rr]eports?)+\s+#(?:(?:\d+)[#,\.\s]*)+ +(\d+) +K 15 +bugtraq:message +V 21 +Mantis issue #%BUGID% +K 11 +bugtraq:url +V 33 +http://mantis/view.php?id=%BUGID% +END diff --git a/Src/Network/base/.svn/entries b/Src/Network/base/.svn/entries new file mode 100644 index 0000000..eba0b2c --- /dev/null +++ b/Src/Network/base/.svn/entries @@ -0,0 +1,164 @@ +10 + +dir +6611 +svn://kar-vm-svn01.mchp-main.com/svn/AppsTeam/Samples/iMX6+OS81118/Industrial/VideoOnDemand/tags/V3.0.4/source/Examples/NetworkManager/Server/Src/Network/base +svn://kar-vm-svn01.mchp-main.com/svn/AppsTeam + + + +2016-03-08T10:11:00.246092Z +5263 +tkummermehr +has-props + + + + + + + + + + + + + +c50209ca-6af6-4c1a-9d47-4b11eae79d1c + +Board.c +file + + + + +2016-11-29T13:32:01.206172Z +002b1b0937a1a1653ff724364be62050 +2016-03-08T10:11:00.246092Z +5263 +tkummermehr + + + + + + + + + + + + + + + + + + + + + +1685 + +DriverConfiguration.h +file + + + + +2016-11-29T13:32:01.206172Z +05273e72ca5b7af8c2f55a35fcdaa5a0 +2015-12-17T09:32:42.832988Z +5040 +ashvetsov +has-props + + + + + + + + + + + + + + + + + + + + +11839 + +Board.h +file + + + + +2016-11-29T13:32:01.206172Z +40cb461e8d0039b063b790d626d37f1a +2016-03-08T10:11:00.246092Z +5263 +tkummermehr + + + + + + + + + + + + + + + + + + + + + +1799 + +DriverConfiguration.c +file + + + + +2016-11-29T13:32:01.202172Z +b8e55c1b0e2437a8513afd32ae4f5446 +2015-12-17T09:32:42.832988Z +5040 +ashvetsov +has-props + + + + + + + + + + + + + + + + + + + + +18468 + diff --git a/Src/Network/base/.svn/prop-base/DriverConfiguration.c.svn-base b/Src/Network/base/.svn/prop-base/DriverConfiguration.c.svn-base new file mode 100644 index 0000000..3160658 --- /dev/null +++ b/Src/Network/base/.svn/prop-base/DriverConfiguration.c.svn-base @@ -0,0 +1,5 @@ +K 13 +svn:mergeinfo +V 0 + +END diff --git a/Src/Network/base/.svn/prop-base/DriverConfiguration.h.svn-base b/Src/Network/base/.svn/prop-base/DriverConfiguration.h.svn-base new file mode 100644 index 0000000..3160658 --- /dev/null +++ b/Src/Network/base/.svn/prop-base/DriverConfiguration.h.svn-base @@ -0,0 +1,5 @@ +K 13 +svn:mergeinfo +V 0 + +END diff --git a/Src/Network/base/.svn/text-base/Board.c.svn-base b/Src/Network/base/.svn/text-base/Board.c.svn-base new file mode 100644 index 0000000..50daf0c --- /dev/null +++ b/Src/Network/base/.svn/text-base/Board.c.svn-base @@ -0,0 +1,51 @@ +/* + * Video On Demand Samples + * + * Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG + * + * 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/>. + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +/*----------------------------------------------------------*/ +/*! \file */ +/*! \brief Base Board initialisation */ +/*----------------------------------------------------------*/ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include <time.h> +#include "Board.h" +#include "Console.h" + +uint32_t GetTickCount( void ) +{ + struct timespec currentTime; + if (clock_gettime(CLOCK_MONOTONIC_RAW, ¤tTime)) + { + ConsolePrintf( PRIO_ERROR, RED"GetTickCount failed!!"RESETCOLOR"\n" ); + return 0; + } + return ( currentTime.tv_sec * 1000 ) + ( currentTime.tv_nsec / 1000000 ); +} + +uint16_t GetTickCountWord() +{ + return ( uint16_t )( GetTickCount() & UINT16_MAX ); +} diff --git a/Src/Network/base/.svn/text-base/Board.h.svn-base b/Src/Network/base/.svn/text-base/Board.h.svn-base new file mode 100644 index 0000000..585a862 --- /dev/null +++ b/Src/Network/base/.svn/text-base/Board.h.svn-base @@ -0,0 +1,63 @@ +/* + * Video On Demand Samples + * + * Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG + * + * 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/>. + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +/*----------------------------------------------------------*/ +/*! \file + * \brief This component abstracts platform specific functionalities. + */ +/*----------------------------------------------------------*/ +#ifndef _BOARD_H_ +#define _BOARD_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include <stdint.h> + + /*----------------------------------------------------------*/ + /*! \brief reads the ms tick counter. + * + * \return value of the ms counter. + */ + /*----------------------------------------------------------*/ + uint32_t GetTickCount( void ); + + + + + /*----------------------------------------------------------*/ + /*! \brief reads the ms tick counter + * + * \return value of the ms counter as word + */ + /*----------------------------------------------------------*/ + uint16_t GetTickCountWord( void ); + + +#ifdef __cplusplus +} +#endif + +#endif // _BOARD_H_ diff --git a/Src/Network/base/.svn/text-base/DriverConfiguration.c.svn-base b/Src/Network/base/.svn/text-base/DriverConfiguration.c.svn-base new file mode 100644 index 0000000..7a98d25 --- /dev/null +++ b/Src/Network/base/.svn/text-base/DriverConfiguration.c.svn-base @@ -0,0 +1,608 @@ +/* + * Video On Demand Samples + * + * Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG + * + * 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/>. + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +#include <dirent.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> +#include <errno.h> +#include "DriverConfiguration.h" +#include "Console.h" + +static bool WriteCharactersToFile( const char *pFileName, const char *pString ) +{ + bool success = false; + FILE *fh = fopen( pFileName, "a" ); + if( NULL != fh ) + { + int result = fputs( pString, fh ); + if( result >= 0 ) + fputc( '\n', fh ); + if( result >= 0 ) + success = true; + fclose( fh ); + } + if( success ) + ConsolePrintf( PRIO_MEDIUM, "*** configured device: '%s = %s', success:%d\n", pFileName, pString, success ); + else + ConsolePrintf( PRIO_ERROR, RED"DriverConfiguration.WriteCharactersToFile failed for file '%s', errno:'%s'"RESETCOLOR"\n", + pFileName, GetErrnoString() ); + return success; +} + +static bool WriteIntegerToFile( const char *pFileName, int intValue ) +{ + char tempBuffer[16]; + snprintf( tempBuffer, sizeof( tempBuffer ), "%d", intValue ); + return WriteCharactersToFile( pFileName, tempBuffer ); +} + +static bool ReadFromFile( const char *pFileName, char *pString, uint16_t bufferLen ) +{ + bool success = false; + if( NULL == pString || 0 == bufferLen ) + return success; + FILE *fh = fopen( pFileName, "r" ); + if( NULL != fh ) + { + success = ( NULL != fgets( pString, bufferLen, fh ) ); + fclose( fh ); + } + if( !success ) + ConsolePrintf( PRIO_ERROR, RED"DriverConfiguration.ReadFromFile failed for file '%s', errno:'%s'"RESETCOLOR"\n", + pFileName, GetErrnoString() ); + return success; +} + +static bool ExistsDevice( const char *pDeviceName ) +{ + struct stat buffer; + return ( stat( pDeviceName, &buffer ) == 0 ); +} + +static bool WaitForDevice( const char *pDeviceName ) +{ + int timeout; + bool deviceExists = false; + for( timeout = 0; timeout < 40; timeout++ ) + { + deviceExists = ExistsDevice( pDeviceName ); + if( deviceExists ) + { + break; + } + else + { + usleep( 2500 ); + } + } + if( !deviceExists ) + ConsolePrintf( PRIO_ERROR, RED"Waiting for device '%s' to appear, timed out"RESETCOLOR"\n", + pDeviceName ); + return deviceExists; +} + +static bool GetDeviceDescription( uint8_t deviceInstance, char *deviceString, uint32_t stringLen ) +{ + bool descriptionFound = false; + char descriptionPath[64]; + char descriptionValue[32]; + + if( NULL == deviceString ) + return descriptionFound; + + snprintf( descriptionPath, sizeof( descriptionPath ), + "/sys/devices/virtual/most/mostcore/devices/mdev%d/description", deviceInstance ); + + if( !ExistsDevice( descriptionPath ) ) + return descriptionFound; + + descriptionFound = ReadFromFile( descriptionPath, descriptionValue, sizeof( descriptionValue ) ); + if( descriptionFound ) + { + strncpy( deviceString, descriptionValue, stringLen ); + } + return descriptionFound; +} + +static bool ExistsDeviceWithType( uint8_t deviceInstance, const char *deviceString, uint32_t stringLen ) +{ + bool deviceFound = false; + char interfacePath[64]; + char interfaceValue[32]; + + if( NULL == deviceString ) + return deviceFound; + + snprintf( interfacePath, sizeof( interfacePath ), "/sys/devices/virtual/most/mostcore/devices/mdev%d/interface", + deviceInstance ); + + if( !ExistsDevice( interfacePath ) ) + return deviceFound; + + deviceFound = ReadFromFile( interfacePath, interfaceValue, sizeof( interfaceValue ) ); + if( deviceFound ) + { + deviceFound = ( 0 == strncmp( interfaceValue, deviceString, stringLen ) ); + } + return deviceFound; +} + +static bool GetAlsaConfiguration( uint16_t subBufferSize, uint8_t *amountOfChannels, uint8_t *bitDepth ) +{ + if( NULL == amountOfChannels || NULL == bitDepth ) + { + ConsolePrintf( PRIO_ERROR, RED"GetAlsaConfiguration was called with invalid parameters"RESETCOLOR"\n" ); + return false; + } + switch( subBufferSize ) + { + case 2: + *amountOfChannels = 1; + *bitDepth = 16; + break; + case 4: + *amountOfChannels = 2; + *bitDepth = 16; + break; + case 6: + *amountOfChannels = 2; + *bitDepth = 24; + break; + case 8: + *amountOfChannels = 2; + *bitDepth = 32; + break; + case 12: + *amountOfChannels = 6; + *bitDepth = 16; + break; + case 18: + *amountOfChannels = 6; + *bitDepth = 24; + break; + case 16: + *amountOfChannels = 8; + *bitDepth = 16; + break; + case 24: + *amountOfChannels = 8; + *bitDepth = 24; + break; + default: + ConsolePrintf( PRIO_ERROR, RED"Configure ALSA device was called"\ + " with unknown sub-buffer size: %d"RESETCOLOR"\n", subBufferSize ); + return false; + } + return true; +} + +bool ExistsUsbDeviceInstance( uint8_t deviceInstance ) +{ + uint32_t i; + bool found = false; + uint8_t curDevInst = 0; + char lastDescr[64]; + lastDescr[0] = '\0'; + for( i = 0; i < 12; i++ ) + { + uint32_t curDescrLen; + char curDescr[64]; + if( !ExistsDeviceWithType( i, "usb", 3 ) ) + continue; + + if( !GetDeviceDescription( i, curDescr, sizeof( curDescr ) ) ) + continue; + + curDescrLen = strnlen( curDescr, sizeof( curDescr ) ); + if( curDescrLen <= 2 ) + continue; + + //Cut away the last two characters, as they are different for the 3 interfaces of INIC + curDescrLen -= 2; + + if( 0 == strncmp( curDescr, lastDescr, curDescrLen ) ) + continue; + + strncpy( lastDescr, curDescr, curDescrLen ); + lastDescr[curDescrLen] = '\0'; + + if( curDevInst++ != deviceInstance ) + continue; + + found = true; + break; + } + return found; +} + +bool ExistsMlbDeviceInstance( uint8_t deviceInstance ) +{ + return ExistsDeviceWithType( deviceInstance, "mlb", 3 ); +} + +bool ExistsI2CDeviceInstance( uint8_t deviceInstance ) +{ + return ExistsDeviceWithType( deviceInstance, "i2c", 3 ); +} + +bool GetUsbDeviceNames( uint8_t deviceInstance, uint8_t endpointAddress, char *deviceName, uint16_t deviceNameLength, + char *systemName, uint16_t systemNameLength, char *linkName, uint16_t linkNameLength ) +{ + uint32_t i; + bool systemFound = false; + uint8_t curDevInst = 0; + char lastDescr[64]; + lastDescr[0] = '\0'; + char endpointBuffer[16]; + char systemSourceDir[64]; + DIR *d; + struct dirent *dir; + + if( NULL == deviceName || NULL == systemName || NULL == linkName ) + return false; + + deviceName[0] = '\0'; + systemName[0] = '\0'; + + for( i = 0; !systemFound && i < 12; i++ ) + { + uint32_t curDescrLen; + char curDescr[64]; + if( !ExistsDeviceWithType( i, "usb", 3 ) ) + continue; + + if( !GetDeviceDescription( i, curDescr, sizeof( curDescr ) ) ) + continue; + + curDescrLen = strnlen( curDescr, sizeof( curDescr ) ); + if( curDescrLen <= 2 ) + continue; + + //Cut away the last two characters, as they are different for the 3 interfaces of INIC + curDescrLen -= 2; + + if( ( '\0' != lastDescr[0] ) + && ( 0 != strncmp( curDescr, lastDescr, curDescrLen ) ) ) + { + ++curDevInst; + } + + strncpy( lastDescr, curDescr, curDescrLen ); + lastDescr[curDescrLen] = '\0'; + + if( curDevInst < deviceInstance ) + continue; + else if( curDevInst > deviceInstance ) + break; + + snprintf( endpointBuffer, sizeof( endpointBuffer ), "ep%02x", endpointAddress ); + snprintf( systemSourceDir, sizeof( systemSourceDir ), "/sys/devices/virtual/most/mostcore/devices/mdev%d", i ); + d = opendir( systemSourceDir ); + if( d ) + { + while( ( dir = readdir( d ) ) != NULL ) + { + if( strstr( dir->d_name, endpointBuffer ) ) + { + snprintf( systemName, systemNameLength, "%s/%s", systemSourceDir, dir->d_name ); + snprintf( deviceName, deviceNameLength, "/dev/mdev%d-%s", deviceInstance, dir->d_name ); + snprintf( linkName, linkNameLength, "mdev%d:%s:mdev%d-%s", i, dir->d_name, deviceInstance, + dir->d_name ); + systemFound = true; + break; + } + } + closedir( d ); + } + } + return systemFound; +} + +bool GetMlbDeviceNames( uint8_t deviceInstance, uint8_t mlbChannelAddress, char *deviceName, uint16_t deviceNameLength, + char *systemName, uint16_t systemNameLength, char *linkName, uint16_t linkNameLength ) +{ + bool systemFound = false; + char channelBuffer[16]; + char systemSourceDir[64]; + DIR *d; + struct dirent *dir; + + if( NULL == deviceName || NULL == systemName || NULL == linkName ) + return false; + + deviceName[0] = '\0'; + systemName[0] = '\0'; + + snprintf( channelBuffer, sizeof( channelBuffer ), "ca%d", mlbChannelAddress ); + snprintf( systemSourceDir, sizeof( systemSourceDir ), "/sys/devices/virtual/most/mostcore/devices/mdev%d", + deviceInstance ); + d = opendir( systemSourceDir ); + if( d ) + { + while( ( dir = readdir( d ) ) != NULL ) + { + if( strstr( dir->d_name, channelBuffer ) ) + { + snprintf( systemName, systemNameLength, "%s/%s", systemSourceDir, dir->d_name ); + snprintf( deviceName, deviceNameLength, "/dev/mdev%d-%s", deviceInstance, dir->d_name ); + snprintf( linkName, linkNameLength, "mdev%d:%s:mdev%d-%s", deviceInstance, dir->d_name, deviceInstance, + dir->d_name ); + systemFound = true; + break; + } + } + closedir( d ); + } + return systemFound; +} + +bool GetI2CDeviceNames( uint8_t deviceInstance, bool isTx, char *deviceName, uint16_t deviceNameLength, + char *systemName, uint16_t systemNameLength, char *linkName, uint16_t linkNameLength ) +{ + bool systemFound = false; + char systemSourceDir[64]; + DIR *d; + struct dirent *dir; + + if( NULL == deviceName || NULL == systemName || NULL == linkName ) + return false; + + deviceName[0] = '\0'; + systemName[0] = '\0'; + + snprintf( systemSourceDir, sizeof( systemSourceDir ), "/sys/devices/virtual/most/mostcore/devices/mdev%d", + deviceInstance ); + d = opendir( systemSourceDir ); + if( d ) + { + while( ( dir = readdir( d ) ) != NULL ) + { + if( strstr( dir->d_name, ( isTx ? "tx" : "rx" ) ) ) + { + snprintf( systemName, systemNameLength, "%s/%s", systemSourceDir, dir->d_name ); + snprintf( deviceName, deviceNameLength, "/dev/mdev%d-%s", deviceInstance, dir->d_name ); + snprintf( linkName, linkNameLength, "mdev%d:%s:mdev%d-%s", deviceInstance, dir->d_name, deviceInstance, + dir->d_name ); + systemFound = true; + break; + } + } + closedir( d ); + } + return systemFound; +} + +bool CloseMostChannel( const char *device ) +{ + return true; +} + +bool ConfigureMostChannel( const char *device, EPDataType_t mostType, EPDirection_t direction, uint32_t numBuf, + uint32_t bufSize ) +{ + static const char *controlType = "control"; + static const char *asyncType = "async"; + static const char *syncType = "sync"; + static const char *isocType = "isoc_avp"; + static const char *rxDirection = "dir_rx"; + static const char *txDirection = "dir_tx"; + bool success = true; + char tempBuffer[128]; + const char *typeString = NULL; + const char *directionString = NULL; + + switch( mostType ) + { + case EP_Control: + typeString = controlType; + break; + case EP_Asynchron: + typeString = asyncType; + break; + case EP_Synchron: + typeString = syncType; + break; + case EP_Isochron: + typeString = isocType; + break; + default: + return false; + } + + switch( direction ) + { + case EPDIR_IN: + directionString = txDirection; + break; + case EPDIR_OUT: + directionString = rxDirection; + break; + default: + return false; + } + + snprintf( tempBuffer, sizeof( tempBuffer ), "%s/set_datatype", device ); /// Setting data type + success = WriteCharactersToFile( tempBuffer, typeString ); + if( success ) + { + snprintf( tempBuffer, sizeof( tempBuffer ), "%s/set_direction", device ); /// Setting direction + success = WriteCharactersToFile( tempBuffer, directionString ); + } + if( success ) + { + snprintf( tempBuffer, sizeof( tempBuffer ), "%s/set_number_of_buffers", device ); /// Setting amount of buffers + success = WriteIntegerToFile( tempBuffer, numBuf ); + } + if( success ) + { + snprintf( tempBuffer, sizeof( tempBuffer ), "%s/set_buffer_size", device ); /// Setting amount of buffers + success = WriteIntegerToFile( tempBuffer, bufSize ); + } + return success; +} + +bool ConfigureIsocChannel( const char *device, uint32_t subbufferSize, uint32_t packetsPerTransaction ) +{ + char tempBuffer[128]; + bool success; + snprintf( tempBuffer, sizeof( tempBuffer ), "%s/set_subbuffer_size", device ); /// Setting the subbuffer size in bytes + success = WriteIntegerToFile( tempBuffer, subbufferSize ); + + snprintf( tempBuffer, sizeof( tempBuffer ), "%s/set_packets_per_xact", device ); /// Setting the packets per transaction + success = WriteIntegerToFile( tempBuffer, ( packetsPerTransaction & 0xFF ) ); + return success; +} + +bool ConfigureSyncChannel( const char *device, uint32_t syncBlockWidth, uint32_t amounOfSyncFrames ) +{ + char tempBuffer[128]; + bool success; + snprintf( tempBuffer, sizeof( tempBuffer ), "%s/set_packets_per_xact", device ); /// Setting the amount of frames in one USB frame + success = WriteIntegerToFile( tempBuffer, amounOfSyncFrames ); + + snprintf( tempBuffer, sizeof( tempBuffer ), "%s/set_subbuffer_size", device ); /// Setting the blockwidth of a single frame + success = WriteIntegerToFile( tempBuffer, syncBlockWidth ); + return success; +} + +bool LinkToCharacterDevice( const char *linkName, const char *deviceName ) +{ + bool success = + WriteCharactersToFile( "/sys/devices/virtual/most/mostcore/aims/cdev/add_link", linkName ); + if( success ) + { + success = WaitForDevice( deviceName ); + } + if( success ) + { + chmod( deviceName, ( S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH ) ); + } + return success; +} + +bool LinkToAudioDevice( const char *linkName, uint16_t subBufferSize ) +{ + char tempBuffer[128]; + uint8_t amountOfChannels; + uint8_t bitDepth; + if( !GetAlsaConfiguration( subBufferSize, &amountOfChannels, &bitDepth ) ) + return false; + + snprintf( tempBuffer, sizeof( tempBuffer ), "%s.%dx%d", linkName, amountOfChannels, bitDepth ); /// Add the channel information behind the Link Name + + //Be compatible to all versions of MOST Linux Driver. The name was changed of Sound AIM. + bool success = + WriteCharactersToFile( "/sys/devices/virtual/most/mostcore/aims/sound/add_link", tempBuffer ); + if( !success ) + success = + WriteCharactersToFile( "/sys/devices/virtual/most/mostcore/aims/audio/add_link", tempBuffer ); + return success; +} + +bool LinkToVideoForLinuxDevice( const char *linkName ) +{ + bool success = + WriteCharactersToFile( "/sys/devices/virtual/most/mostcore/aims/v4l/add_link", linkName ); + return success; +} + +const char *GetErrnoString() +{ + switch( errno ) + { + case 0: + return "Nothing stored in errno"; + case 1: + return "Operation not permitted"; + case 2: + return "No such file or directory"; + case 3: + return "No such process"; + case 4: + return "Interrupted system call"; + case 5: + return "I/O error"; + case 6: + return "No such device or address"; + case 7: + return "Argument list too long"; + case 8: + return "Exec format error"; + case 9: + return "Bad file number"; + case 10: + return "No child processes"; + case 11: + return "Try again"; + case 12: + return "Out of memory"; + case 13: + return "Permission denied"; + case 14: + return "Bad address"; + case 15: + return "Block device required"; + case 16: + return "Device or resource busy"; + case 17: + return "File exists"; + case 18: + return "Cross-device link"; + case 19: + return "No such device"; + case 20: + return "Not a directory"; + case 21: + return "Is a directory"; + case 22: + return "Invalid argument"; + case 23: + return "File table overflow"; + case 24: + return "Too many open files"; + case 25: + return "Not a typewriter"; + case 26: + return "Text file busy"; + case 27: + return "File too large"; + case 28: + return "No space left on device"; + case 29: + return "Illegal seek"; + case 30: + return "Read-only file system"; + case 31: + return "Too many links"; + case 32: + return "Broken pipe"; + case 33: + return "Math argument out of domain of func"; + case 34: + return "Math result not representable"; + default: + break; + } + return "Unknown"; +} diff --git a/Src/Network/base/.svn/text-base/DriverConfiguration.h.svn-base b/Src/Network/base/.svn/text-base/DriverConfiguration.h.svn-base new file mode 100644 index 0000000..b2fd44a --- /dev/null +++ b/Src/Network/base/.svn/text-base/DriverConfiguration.h.svn-base @@ -0,0 +1,239 @@ +/* + * Video On Demand Samples + * + * Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG + * + * 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/>. + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +/*----------------------------------------------------------*/ +/*! \file + * \brief This component provides helper tools to deal with USB endpoints. + */ +/*----------------------------------------------------------*/ +#ifndef DRIVERCONFIGURATION_H +#define DRIVERCONFIGURATION_H + +#include <stdint.h> +#include <stdbool.h> +#include "Types.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + /*----------------------------------------------------------*/ + /*! \brief Checks if the given USB device instance exists. + * + * \param deviceInstance - The device instance of the USB INIC, starting at 0 for the first instance. + * + * \return true: Device exists. + * \return false: Device does not exist. + */ + /*----------------------------------------------------------*/ + bool ExistsUsbDeviceInstance( uint8_t deviceInstance ); + + /*----------------------------------------------------------*/ + /*! \brief Checks if the given MLB device instance exists. + * + * \param deviceInstance - The device instance of the MLB INIC, starting at 0 for the first instance. + * + * \return true: Device exists. + * \return false: Device does not exist. + */ + /*----------------------------------------------------------*/ + bool ExistsMlbDeviceInstance( uint8_t deviceInstance ); + + /*----------------------------------------------------------*/ + /*! \brief Checks if the given I2C device instance exists. + * + * \param deviceInstance - The device instance of the I2C INIC, starting at 0 for the first instance. + * + * \return true: Device exists. + * \return false: Device does not exist. + */ + /*----------------------------------------------------------*/ + bool ExistsI2CDeviceInstance( uint8_t deviceInstance ); + + /*----------------------------------------------------------*/ + /*! \brief Retreives the Linux character device names for the given USB device instance and the endpoint address. + * + * \note If the given buffer is too small to hold the whole path, the string is truncated and not useable! + * + * \param deviceInstance - The device instance of the USB INIC, starting at 0 for the first instance. + * \param endpointAddress - The USB endpoint address, RX endpoints starting with upper bit set (0x8X). + * \param deviceName - [in,out] Given buffer will be filled with device name (starting with /dev/). + * \param deviceNameLength - The length of the given deviceName buffer in bytes. + * \param systemName - [in,out] Given buffer will be filled with system name (starting with /sys/). + * \param systemNameLength - The length of the given systemName buffer in bytes. + * \param linkName - [in,out] Given buffer will be filled with the string needed to register this device to an AIM + * \param linkNameLength - The length of the given linkName buffer in bytes. + * + * \return true: Operation was successful. + * \return false: Operation failed, device or endpoint does not exist. + */ + /*----------------------------------------------------------*/ + bool GetUsbDeviceNames( uint8_t deviceInstance, uint8_t endpointAddress, char *deviceName, + uint16_t deviceNameLength, char *systemName, uint16_t systemNameLength, char *linkName, + uint16_t linkNameLength ); + + + /*----------------------------------------------------------*/ + /*! \brief Retreives the Linux character device names for the given MLB device instance and the endpoint address. + * + * \note If the given buffer is too small to hold the whole path, the string is truncated and not useable! + * + * \param deviceInstance - The device instance of the USB INIC, starting at 0 for the first instance. + * \param mlbChannelAddress - The MLB channel address + * \param deviceName - [in,out] Given buffer will be filled with device name (starting with /dev/). + * \param deviceNameLength - The length of the given deviceName buffer in bytes. + * \param systemName - [in,out] Given buffer will be filled with system name (starting with /sys/). + * \param systemNameLength - The length of the given systemName buffer in bytes. + * \param linkName - [in,out] Given buffer will be filled with the string needed to register this device to an AIM + * \param linkNameLength - The length of the given linkName buffer in bytes. + * + * \return true: Operation was successful. + * \return false: Operation failed, device or endpoint does not exist. + */ + /*----------------------------------------------------------*/ + bool GetMlbDeviceNames( uint8_t deviceInstance, uint8_t mlbChannelAddress, char *deviceName, + uint16_t deviceNameLength, char *systemName, uint16_t systemNameLength, char *linkName, + uint16_t linkNameLength ); + + + /*----------------------------------------------------------*/ + /*! \brief Retreives the Linux character device names for the given I2C device instance and the endpoint address. + * + * \note If the given buffer is too small to hold the whole path, the string is truncated and not useable! + * + * \param deviceInstance - The device instance of the I2C INIC, starting at 0 for the first instance. + * \param isTx - true, if the names for transmission path should be retrieved, otherwise for the reception path. + * \param deviceName - [in,out] Given buffer will be filled with device name (starting with /dev/). + * \param deviceNameLength - The length of the given deviceName buffer in bytes. + * \param systemName - [in,out] Given buffer will be filled with system name (starting with /sys/). + * \param systemNameLength - The length of the given systemName buffer in bytes. + * \param linkName - [in,out] Given buffer will be filled with the string needed to register this device to an AIM + * \param linkNameLength - The length of the given linkName buffer in bytes. + * + * \return true: Operation was successful. + * \return false: Operation failed, device or endpoint does not exist. + */ + /*----------------------------------------------------------*/ + bool GetI2CDeviceNames( uint8_t deviceInstance, bool isTx, char *deviceName, uint16_t deviceNameLength, + char *systemName, uint16_t systemNameLength, char *linkName, uint16_t linkNameLength ); + + /*----------------------------------------------------------*/ + /*! \brief Closes the MOST channel. + * + * \param device - The system path to configure the MOST device (starting with "/sys/"). + * + * \return True if no error + */ + /*----------------------------------------------------------*/ + bool CloseMostChannel( const char *device ); + + /*----------------------------------------------------------*/ + /*! \brief Configures the MOST channel. + * + * \param device - The system path to configure the MOST device (starting with "/sys/"). + * \param mostType - The MOST data type (control, asynchron, synchron, isochron). + * \param direction - The direction of the stream (directionRX, directionTX). + * \param numBuf - The amount of buffers. + * \param bufSize - The size of each buffer in bytes. + * + * \return True if no error + */ + /*----------------------------------------------------------*/ + bool ConfigureMostChannel( const char *device, EPDataType_t mostType, EPDirection_t direction, uint32_t numBuf, + uint32_t bufSize ); + + /*----------------------------------------------------------*/ + /*! \brief Configures the isochronous channel of a device. + * \note This function has to be called before ConfigureMostChannel! + * + * \param device - The system path to configure the MOST device (starting with "/sys/"). + * \param subbufferSize - The amount of bytes for a single subbuffer (eg. 188 or 196 bytes). + * \param packetsPerTransaction - The amount of ischronous packets per transaction. + * + * \return True if no error + */ + /*----------------------------------------------------------*/ + bool ConfigureIsocChannel( const char *device, uint32_t subbufferSize, uint32_t packetsPerTransaction ); + + /*----------------------------------------------------------*/ + /*! \brief Configures the Synchronous channel of a device. + * \note This function has to be called before ConfigureMostChannel! + * + * \param device - The system path to configure the MOST device (starting with "/sys/"). + * \param syncBlockWidth - The bandwidth of a single synchronous frame. Ignored if not sync channel. + * \param amounOfSyncFrames - The amount of synchronous frames per USB packet. Ignored if not sync channel. + * + * \return True if no error + */ + /*----------------------------------------------------------*/ + bool ConfigureSyncChannel( const char *device, uint32_t syncBlockWidth, uint32_t amounOfSyncFrames ); + + + /*----------------------------------------------------------*/ + /*! \brief Creates a character device for the given device's link name. + * + * \param linkName - [in] Link name, which is needed to register this device to an AIM. + * \param deviceName - [in,out] Device name (starting with /dev/). + * + * \return True if no error + */ + /*----------------------------------------------------------*/ + bool LinkToCharacterDevice( const char *linkName, const char *deviceName ); + + + /*----------------------------------------------------------*/ + /*! \brief Creates a audio device (ALSA) for the given device's link name. + * + * \param linkName - [in] Link name, which is needed to register this device to an AIM. + * \param subBufferSize - [in] The amount of bytes for all channels of this connection on the MOST. + * + * \return True if no error + */ + /*----------------------------------------------------------*/ + bool LinkToAudioDevice( const char *linkName, uint16_t subBufferSize ); + + /*----------------------------------------------------------*/ + /*! \brief Creates a Video for Linux device (V4L) for the given device's link name. + * + * \param linkName - [in] Link name, which is needed to register this device to an AIM. + * + * \return True if no error + */ + /*----------------------------------------------------------*/ + bool LinkToVideoForLinuxDevice( const char *linkName ); + + + /*----------------------------------------------------------*/ + /*! \brief Parsed the global errno variable and generate a human readable error description out of it. + * + * \return Zero terminated error description string. + */ + /*----------------------------------------------------------*/ + const char *GetErrnoString(); + +#ifdef __cplusplus +} +#endif + +#endif //DRIVERCONFIGURATION_H diff --git a/Src/Network/base/Board.c b/Src/Network/base/Board.c new file mode 100644 index 0000000..50daf0c --- /dev/null +++ b/Src/Network/base/Board.c @@ -0,0 +1,51 @@ +/* + * Video On Demand Samples + * + * Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG + * + * 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/>. + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +/*----------------------------------------------------------*/ +/*! \file */ +/*! \brief Base Board initialisation */ +/*----------------------------------------------------------*/ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include <time.h> +#include "Board.h" +#include "Console.h" + +uint32_t GetTickCount( void ) +{ + struct timespec currentTime; + if (clock_gettime(CLOCK_MONOTONIC_RAW, ¤tTime)) + { + ConsolePrintf( PRIO_ERROR, RED"GetTickCount failed!!"RESETCOLOR"\n" ); + return 0; + } + return ( currentTime.tv_sec * 1000 ) + ( currentTime.tv_nsec / 1000000 ); +} + +uint16_t GetTickCountWord() +{ + return ( uint16_t )( GetTickCount() & UINT16_MAX ); +} diff --git a/Src/Network/base/Board.h b/Src/Network/base/Board.h new file mode 100644 index 0000000..585a862 --- /dev/null +++ b/Src/Network/base/Board.h @@ -0,0 +1,63 @@ +/* + * Video On Demand Samples + * + * Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG + * + * 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/>. + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +/*----------------------------------------------------------*/ +/*! \file + * \brief This component abstracts platform specific functionalities. + */ +/*----------------------------------------------------------*/ +#ifndef _BOARD_H_ +#define _BOARD_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include <stdint.h> + + /*----------------------------------------------------------*/ + /*! \brief reads the ms tick counter. + * + * \return value of the ms counter. + */ + /*----------------------------------------------------------*/ + uint32_t GetTickCount( void ); + + + + + /*----------------------------------------------------------*/ + /*! \brief reads the ms tick counter + * + * \return value of the ms counter as word + */ + /*----------------------------------------------------------*/ + uint16_t GetTickCountWord( void ); + + +#ifdef __cplusplus +} +#endif + +#endif // _BOARD_H_ diff --git a/Src/Network/base/DriverConfiguration.c b/Src/Network/base/DriverConfiguration.c new file mode 100644 index 0000000..7a98d25 --- /dev/null +++ b/Src/Network/base/DriverConfiguration.c @@ -0,0 +1,608 @@ +/* + * Video On Demand Samples + * + * Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG + * + * 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/>. + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +#include <dirent.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> +#include <errno.h> +#include "DriverConfiguration.h" +#include "Console.h" + +static bool WriteCharactersToFile( const char *pFileName, const char *pString ) +{ + bool success = false; + FILE *fh = fopen( pFileName, "a" ); + if( NULL != fh ) + { + int result = fputs( pString, fh ); + if( result >= 0 ) + fputc( '\n', fh ); + if( result >= 0 ) + success = true; + fclose( fh ); + } + if( success ) + ConsolePrintf( PRIO_MEDIUM, "*** configured device: '%s = %s', success:%d\n", pFileName, pString, success ); + else + ConsolePrintf( PRIO_ERROR, RED"DriverConfiguration.WriteCharactersToFile failed for file '%s', errno:'%s'"RESETCOLOR"\n", + pFileName, GetErrnoString() ); + return success; +} + +static bool WriteIntegerToFile( const char *pFileName, int intValue ) +{ + char tempBuffer[16]; + snprintf( tempBuffer, sizeof( tempBuffer ), "%d", intValue ); + return WriteCharactersToFile( pFileName, tempBuffer ); +} + +static bool ReadFromFile( const char *pFileName, char *pString, uint16_t bufferLen ) +{ + bool success = false; + if( NULL == pString || 0 == bufferLen ) + return success; + FILE *fh = fopen( pFileName, "r" ); + if( NULL != fh ) + { + success = ( NULL != fgets( pString, bufferLen, fh ) ); + fclose( fh ); + } + if( !success ) + ConsolePrintf( PRIO_ERROR, RED"DriverConfiguration.ReadFromFile failed for file '%s', errno:'%s'"RESETCOLOR"\n", + pFileName, GetErrnoString() ); + return success; +} + +static bool ExistsDevice( const char *pDeviceName ) +{ + struct stat buffer; + return ( stat( pDeviceName, &buffer ) == 0 ); +} + +static bool WaitForDevice( const char *pDeviceName ) +{ + int timeout; + bool deviceExists = false; + for( timeout = 0; timeout < 40; timeout++ ) + { + deviceExists = ExistsDevice( pDeviceName ); + if( deviceExists ) + { + break; + } + else + { + usleep( 2500 ); + } + } + if( !deviceExists ) + ConsolePrintf( PRIO_ERROR, RED"Waiting for device '%s' to appear, timed out"RESETCOLOR"\n", + pDeviceName ); + return deviceExists; +} + +static bool GetDeviceDescription( uint8_t deviceInstance, char *deviceString, uint32_t stringLen ) +{ + bool descriptionFound = false; + char descriptionPath[64]; + char descriptionValue[32]; + + if( NULL == deviceString ) + return descriptionFound; + + snprintf( descriptionPath, sizeof( descriptionPath ), + "/sys/devices/virtual/most/mostcore/devices/mdev%d/description", deviceInstance ); + + if( !ExistsDevice( descriptionPath ) ) + return descriptionFound; + + descriptionFound = ReadFromFile( descriptionPath, descriptionValue, sizeof( descriptionValue ) ); + if( descriptionFound ) + { + strncpy( deviceString, descriptionValue, stringLen ); + } + return descriptionFound; +} + +static bool ExistsDeviceWithType( uint8_t deviceInstance, const char *deviceString, uint32_t stringLen ) +{ + bool deviceFound = false; + char interfacePath[64]; + char interfaceValue[32]; + + if( NULL == deviceString ) + return deviceFound; + + snprintf( interfacePath, sizeof( interfacePath ), "/sys/devices/virtual/most/mostcore/devices/mdev%d/interface", + deviceInstance ); + + if( !ExistsDevice( interfacePath ) ) + return deviceFound; + + deviceFound = ReadFromFile( interfacePath, interfaceValue, sizeof( interfaceValue ) ); + if( deviceFound ) + { + deviceFound = ( 0 == strncmp( interfaceValue, deviceString, stringLen ) ); + } + return deviceFound; +} + +static bool GetAlsaConfiguration( uint16_t subBufferSize, uint8_t *amountOfChannels, uint8_t *bitDepth ) +{ + if( NULL == amountOfChannels || NULL == bitDepth ) + { + ConsolePrintf( PRIO_ERROR, RED"GetAlsaConfiguration was called with invalid parameters"RESETCOLOR"\n" ); + return false; + } + switch( subBufferSize ) + { + case 2: + *amountOfChannels = 1; + *bitDepth = 16; + break; + case 4: + *amountOfChannels = 2; + *bitDepth = 16; + break; + case 6: + *amountOfChannels = 2; + *bitDepth = 24; + break; + case 8: + *amountOfChannels = 2; + *bitDepth = 32; + break; + case 12: + *amountOfChannels = 6; + *bitDepth = 16; + break; + case 18: + *amountOfChannels = 6; + *bitDepth = 24; + break; + case 16: + *amountOfChannels = 8; + *bitDepth = 16; + break; + case 24: + *amountOfChannels = 8; + *bitDepth = 24; + break; + default: + ConsolePrintf( PRIO_ERROR, RED"Configure ALSA device was called"\ + " with unknown sub-buffer size: %d"RESETCOLOR"\n", subBufferSize ); + return false; + } + return true; +} + +bool ExistsUsbDeviceInstance( uint8_t deviceInstance ) +{ + uint32_t i; + bool found = false; + uint8_t curDevInst = 0; + char lastDescr[64]; + lastDescr[0] = '\0'; + for( i = 0; i < 12; i++ ) + { + uint32_t curDescrLen; + char curDescr[64]; + if( !ExistsDeviceWithType( i, "usb", 3 ) ) + continue; + + if( !GetDeviceDescription( i, curDescr, sizeof( curDescr ) ) ) + continue; + + curDescrLen = strnlen( curDescr, sizeof( curDescr ) ); + if( curDescrLen <= 2 ) + continue; + + //Cut away the last two characters, as they are different for the 3 interfaces of INIC + curDescrLen -= 2; + + if( 0 == strncmp( curDescr, lastDescr, curDescrLen ) ) + continue; + + strncpy( lastDescr, curDescr, curDescrLen ); + lastDescr[curDescrLen] = '\0'; + + if( curDevInst++ != deviceInstance ) + continue; + + found = true; + break; + } + return found; +} + +bool ExistsMlbDeviceInstance( uint8_t deviceInstance ) +{ + return ExistsDeviceWithType( deviceInstance, "mlb", 3 ); +} + +bool ExistsI2CDeviceInstance( uint8_t deviceInstance ) +{ + return ExistsDeviceWithType( deviceInstance, "i2c", 3 ); +} + +bool GetUsbDeviceNames( uint8_t deviceInstance, uint8_t endpointAddress, char *deviceName, uint16_t deviceNameLength, + char *systemName, uint16_t systemNameLength, char *linkName, uint16_t linkNameLength ) +{ + uint32_t i; + bool systemFound = false; + uint8_t curDevInst = 0; + char lastDescr[64]; + lastDescr[0] = '\0'; + char endpointBuffer[16]; + char systemSourceDir[64]; + DIR *d; + struct dirent *dir; + + if( NULL == deviceName || NULL == systemName || NULL == linkName ) + return false; + + deviceName[0] = '\0'; + systemName[0] = '\0'; + + for( i = 0; !systemFound && i < 12; i++ ) + { + uint32_t curDescrLen; + char curDescr[64]; + if( !ExistsDeviceWithType( i, "usb", 3 ) ) + continue; + + if( !GetDeviceDescription( i, curDescr, sizeof( curDescr ) ) ) + continue; + + curDescrLen = strnlen( curDescr, sizeof( curDescr ) ); + if( curDescrLen <= 2 ) + continue; + + //Cut away the last two characters, as they are different for the 3 interfaces of INIC + curDescrLen -= 2; + + if( ( '\0' != lastDescr[0] ) + && ( 0 != strncmp( curDescr, lastDescr, curDescrLen ) ) ) + { + ++curDevInst; + } + + strncpy( lastDescr, curDescr, curDescrLen ); + lastDescr[curDescrLen] = '\0'; + + if( curDevInst < deviceInstance ) + continue; + else if( curDevInst > deviceInstance ) + break; + + snprintf( endpointBuffer, sizeof( endpointBuffer ), "ep%02x", endpointAddress ); + snprintf( systemSourceDir, sizeof( systemSourceDir ), "/sys/devices/virtual/most/mostcore/devices/mdev%d", i ); + d = opendir( systemSourceDir ); + if( d ) + { + while( ( dir = readdir( d ) ) != NULL ) + { + if( strstr( dir->d_name, endpointBuffer ) ) + { + snprintf( systemName, systemNameLength, "%s/%s", systemSourceDir, dir->d_name ); + snprintf( deviceName, deviceNameLength, "/dev/mdev%d-%s", deviceInstance, dir->d_name ); + snprintf( linkName, linkNameLength, "mdev%d:%s:mdev%d-%s", i, dir->d_name, deviceInstance, + dir->d_name ); + systemFound = true; + break; + } + } + closedir( d ); + } + } + return systemFound; +} + +bool GetMlbDeviceNames( uint8_t deviceInstance, uint8_t mlbChannelAddress, char *deviceName, uint16_t deviceNameLength, + char *systemName, uint16_t systemNameLength, char *linkName, uint16_t linkNameLength ) +{ + bool systemFound = false; + char channelBuffer[16]; + char systemSourceDir[64]; + DIR *d; + struct dirent *dir; + + if( NULL == deviceName || NULL == systemName || NULL == linkName ) + return false; + + deviceName[0] = '\0'; + systemName[0] = '\0'; + + snprintf( channelBuffer, sizeof( channelBuffer ), "ca%d", mlbChannelAddress ); + snprintf( systemSourceDir, sizeof( systemSourceDir ), "/sys/devices/virtual/most/mostcore/devices/mdev%d", + deviceInstance ); + d = opendir( systemSourceDir ); + if( d ) + { + while( ( dir = readdir( d ) ) != NULL ) + { + if( strstr( dir->d_name, channelBuffer ) ) + { + snprintf( systemName, systemNameLength, "%s/%s", systemSourceDir, dir->d_name ); + snprintf( deviceName, deviceNameLength, "/dev/mdev%d-%s", deviceInstance, dir->d_name ); + snprintf( linkName, linkNameLength, "mdev%d:%s:mdev%d-%s", deviceInstance, dir->d_name, deviceInstance, + dir->d_name ); + systemFound = true; + break; + } + } + closedir( d ); + } + return systemFound; +} + +bool GetI2CDeviceNames( uint8_t deviceInstance, bool isTx, char *deviceName, uint16_t deviceNameLength, + char *systemName, uint16_t systemNameLength, char *linkName, uint16_t linkNameLength ) +{ + bool systemFound = false; + char systemSourceDir[64]; + DIR *d; + struct dirent *dir; + + if( NULL == deviceName || NULL == systemName || NULL == linkName ) + return false; + + deviceName[0] = '\0'; + systemName[0] = '\0'; + + snprintf( systemSourceDir, sizeof( systemSourceDir ), "/sys/devices/virtual/most/mostcore/devices/mdev%d", + deviceInstance ); + d = opendir( systemSourceDir ); + if( d ) + { + while( ( dir = readdir( d ) ) != NULL ) + { + if( strstr( dir->d_name, ( isTx ? "tx" : "rx" ) ) ) + { + snprintf( systemName, systemNameLength, "%s/%s", systemSourceDir, dir->d_name ); + snprintf( deviceName, deviceNameLength, "/dev/mdev%d-%s", deviceInstance, dir->d_name ); + snprintf( linkName, linkNameLength, "mdev%d:%s:mdev%d-%s", deviceInstance, dir->d_name, deviceInstance, + dir->d_name ); + systemFound = true; + break; + } + } + closedir( d ); + } + return systemFound; +} + +bool CloseMostChannel( const char *device ) +{ + return true; +} + +bool ConfigureMostChannel( const char *device, EPDataType_t mostType, EPDirection_t direction, uint32_t numBuf, + uint32_t bufSize ) +{ + static const char *controlType = "control"; + static const char *asyncType = "async"; + static const char *syncType = "sync"; + static const char *isocType = "isoc_avp"; + static const char *rxDirection = "dir_rx"; + static const char *txDirection = "dir_tx"; + bool success = true; + char tempBuffer[128]; + const char *typeString = NULL; + const char *directionString = NULL; + + switch( mostType ) + { + case EP_Control: + typeString = controlType; + break; + case EP_Asynchron: + typeString = asyncType; + break; + case EP_Synchron: + typeString = syncType; + break; + case EP_Isochron: + typeString = isocType; + break; + default: + return false; + } + + switch( direction ) + { + case EPDIR_IN: + directionString = txDirection; + break; + case EPDIR_OUT: + directionString = rxDirection; + break; + default: + return false; + } + + snprintf( tempBuffer, sizeof( tempBuffer ), "%s/set_datatype", device ); /// Setting data type + success = WriteCharactersToFile( tempBuffer, typeString ); + if( success ) + { + snprintf( tempBuffer, sizeof( tempBuffer ), "%s/set_direction", device ); /// Setting direction + success = WriteCharactersToFile( tempBuffer, directionString ); + } + if( success ) + { + snprintf( tempBuffer, sizeof( tempBuffer ), "%s/set_number_of_buffers", device ); /// Setting amount of buffers + success = WriteIntegerToFile( tempBuffer, numBuf ); + } + if( success ) + { + snprintf( tempBuffer, sizeof( tempBuffer ), "%s/set_buffer_size", device ); /// Setting amount of buffers + success = WriteIntegerToFile( tempBuffer, bufSize ); + } + return success; +} + +bool ConfigureIsocChannel( const char *device, uint32_t subbufferSize, uint32_t packetsPerTransaction ) +{ + char tempBuffer[128]; + bool success; + snprintf( tempBuffer, sizeof( tempBuffer ), "%s/set_subbuffer_size", device ); /// Setting the subbuffer size in bytes + success = WriteIntegerToFile( tempBuffer, subbufferSize ); + + snprintf( tempBuffer, sizeof( tempBuffer ), "%s/set_packets_per_xact", device ); /// Setting the packets per transaction + success = WriteIntegerToFile( tempBuffer, ( packetsPerTransaction & 0xFF ) ); + return success; +} + +bool ConfigureSyncChannel( const char *device, uint32_t syncBlockWidth, uint32_t amounOfSyncFrames ) +{ + char tempBuffer[128]; + bool success; + snprintf( tempBuffer, sizeof( tempBuffer ), "%s/set_packets_per_xact", device ); /// Setting the amount of frames in one USB frame + success = WriteIntegerToFile( tempBuffer, amounOfSyncFrames ); + + snprintf( tempBuffer, sizeof( tempBuffer ), "%s/set_subbuffer_size", device ); /// Setting the blockwidth of a single frame + success = WriteIntegerToFile( tempBuffer, syncBlockWidth ); + return success; +} + +bool LinkToCharacterDevice( const char *linkName, const char *deviceName ) +{ + bool success = + WriteCharactersToFile( "/sys/devices/virtual/most/mostcore/aims/cdev/add_link", linkName ); + if( success ) + { + success = WaitForDevice( deviceName ); + } + if( success ) + { + chmod( deviceName, ( S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH ) ); + } + return success; +} + +bool LinkToAudioDevice( const char *linkName, uint16_t subBufferSize ) +{ + char tempBuffer[128]; + uint8_t amountOfChannels; + uint8_t bitDepth; + if( !GetAlsaConfiguration( subBufferSize, &amountOfChannels, &bitDepth ) ) + return false; + + snprintf( tempBuffer, sizeof( tempBuffer ), "%s.%dx%d", linkName, amountOfChannels, bitDepth ); /// Add the channel information behind the Link Name + + //Be compatible to all versions of MOST Linux Driver. The name was changed of Sound AIM. + bool success = + WriteCharactersToFile( "/sys/devices/virtual/most/mostcore/aims/sound/add_link", tempBuffer ); + if( !success ) + success = + WriteCharactersToFile( "/sys/devices/virtual/most/mostcore/aims/audio/add_link", tempBuffer ); + return success; +} + +bool LinkToVideoForLinuxDevice( const char *linkName ) +{ + bool success = + WriteCharactersToFile( "/sys/devices/virtual/most/mostcore/aims/v4l/add_link", linkName ); + return success; +} + +const char *GetErrnoString() +{ + switch( errno ) + { + case 0: + return "Nothing stored in errno"; + case 1: + return "Operation not permitted"; + case 2: + return "No such file or directory"; + case 3: + return "No such process"; + case 4: + return "Interrupted system call"; + case 5: + return "I/O error"; + case 6: + return "No such device or address"; + case 7: + return "Argument list too long"; + case 8: + return "Exec format error"; + case 9: + return "Bad file number"; + case 10: + return "No child processes"; + case 11: + return "Try again"; + case 12: + return "Out of memory"; + case 13: + return "Permission denied"; + case 14: + return "Bad address"; + case 15: + return "Block device required"; + case 16: + return "Device or resource busy"; + case 17: + return "File exists"; + case 18: + return "Cross-device link"; + case 19: + return "No such device"; + case 20: + return "Not a directory"; + case 21: + return "Is a directory"; + case 22: + return "Invalid argument"; + case 23: + return "File table overflow"; + case 24: + return "Too many open files"; + case 25: + return "Not a typewriter"; + case 26: + return "Text file busy"; + case 27: + return "File too large"; + case 28: + return "No space left on device"; + case 29: + return "Illegal seek"; + case 30: + return "Read-only file system"; + case 31: + return "Too many links"; + case 32: + return "Broken pipe"; + case 33: + return "Math argument out of domain of func"; + case 34: + return "Math result not representable"; + default: + break; + } + return "Unknown"; +} diff --git a/Src/Network/base/DriverConfiguration.h b/Src/Network/base/DriverConfiguration.h new file mode 100644 index 0000000..b2fd44a --- /dev/null +++ b/Src/Network/base/DriverConfiguration.h @@ -0,0 +1,239 @@ +/* + * Video On Demand Samples + * + * Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG + * + * 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/>. + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +/*----------------------------------------------------------*/ +/*! \file + * \brief This component provides helper tools to deal with USB endpoints. + */ +/*----------------------------------------------------------*/ +#ifndef DRIVERCONFIGURATION_H +#define DRIVERCONFIGURATION_H + +#include <stdint.h> +#include <stdbool.h> +#include "Types.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + /*----------------------------------------------------------*/ + /*! \brief Checks if the given USB device instance exists. + * + * \param deviceInstance - The device instance of the USB INIC, starting at 0 for the first instance. + * + * \return true: Device exists. + * \return false: Device does not exist. + */ + /*----------------------------------------------------------*/ + bool ExistsUsbDeviceInstance( uint8_t deviceInstance ); + + /*----------------------------------------------------------*/ + /*! \brief Checks if the given MLB device instance exists. + * + * \param deviceInstance - The device instance of the MLB INIC, starting at 0 for the first instance. + * + * \return true: Device exists. + * \return false: Device does not exist. + */ + /*----------------------------------------------------------*/ + bool ExistsMlbDeviceInstance( uint8_t deviceInstance ); + + /*----------------------------------------------------------*/ + /*! \brief Checks if the given I2C device instance exists. + * + * \param deviceInstance - The device instance of the I2C INIC, starting at 0 for the first instance. + * + * \return true: Device exists. + * \return false: Device does not exist. + */ + /*----------------------------------------------------------*/ + bool ExistsI2CDeviceInstance( uint8_t deviceInstance ); + + /*----------------------------------------------------------*/ + /*! \brief Retreives the Linux character device names for the given USB device instance and the endpoint address. + * + * \note If the given buffer is too small to hold the whole path, the string is truncated and not useable! + * + * \param deviceInstance - The device instance of the USB INIC, starting at 0 for the first instance. + * \param endpointAddress - The USB endpoint address, RX endpoints starting with upper bit set (0x8X). + * \param deviceName - [in,out] Given buffer will be filled with device name (starting with /dev/). + * \param deviceNameLength - The length of the given deviceName buffer in bytes. + * \param systemName - [in,out] Given buffer will be filled with system name (starting with /sys/). + * \param systemNameLength - The length of the given systemName buffer in bytes. + * \param linkName - [in,out] Given buffer will be filled with the string needed to register this device to an AIM + * \param linkNameLength - The length of the given linkName buffer in bytes. + * + * \return true: Operation was successful. + * \return false: Operation failed, device or endpoint does not exist. + */ + /*----------------------------------------------------------*/ + bool GetUsbDeviceNames( uint8_t deviceInstance, uint8_t endpointAddress, char *deviceName, + uint16_t deviceNameLength, char *systemName, uint16_t systemNameLength, char *linkName, + uint16_t linkNameLength ); + + + /*----------------------------------------------------------*/ + /*! \brief Retreives the Linux character device names for the given MLB device instance and the endpoint address. + * + * \note If the given buffer is too small to hold the whole path, the string is truncated and not useable! + * + * \param deviceInstance - The device instance of the USB INIC, starting at 0 for the first instance. + * \param mlbChannelAddress - The MLB channel address + * \param deviceName - [in,out] Given buffer will be filled with device name (starting with /dev/). + * \param deviceNameLength - The length of the given deviceName buffer in bytes. + * \param systemName - [in,out] Given buffer will be filled with system name (starting with /sys/). + * \param systemNameLength - The length of the given systemName buffer in bytes. + * \param linkName - [in,out] Given buffer will be filled with the string needed to register this device to an AIM + * \param linkNameLength - The length of the given linkName buffer in bytes. + * + * \return true: Operation was successful. + * \return false: Operation failed, device or endpoint does not exist. + */ + /*----------------------------------------------------------*/ + bool GetMlbDeviceNames( uint8_t deviceInstance, uint8_t mlbChannelAddress, char *deviceName, + uint16_t deviceNameLength, char *systemName, uint16_t systemNameLength, char *linkName, + uint16_t linkNameLength ); + + + /*----------------------------------------------------------*/ + /*! \brief Retreives the Linux character device names for the given I2C device instance and the endpoint address. + * + * \note If the given buffer is too small to hold the whole path, the string is truncated and not useable! + * + * \param deviceInstance - The device instance of the I2C INIC, starting at 0 for the first instance. + * \param isTx - true, if the names for transmission path should be retrieved, otherwise for the reception path. + * \param deviceName - [in,out] Given buffer will be filled with device name (starting with /dev/). + * \param deviceNameLength - The length of the given deviceName buffer in bytes. + * \param systemName - [in,out] Given buffer will be filled with system name (starting with /sys/). + * \param systemNameLength - The length of the given systemName buffer in bytes. + * \param linkName - [in,out] Given buffer will be filled with the string needed to register this device to an AIM + * \param linkNameLength - The length of the given linkName buffer in bytes. + * + * \return true: Operation was successful. + * \return false: Operation failed, device or endpoint does not exist. + */ + /*----------------------------------------------------------*/ + bool GetI2CDeviceNames( uint8_t deviceInstance, bool isTx, char *deviceName, uint16_t deviceNameLength, + char *systemName, uint16_t systemNameLength, char *linkName, uint16_t linkNameLength ); + + /*----------------------------------------------------------*/ + /*! \brief Closes the MOST channel. + * + * \param device - The system path to configure the MOST device (starting with "/sys/"). + * + * \return True if no error + */ + /*----------------------------------------------------------*/ + bool CloseMostChannel( const char *device ); + + /*----------------------------------------------------------*/ + /*! \brief Configures the MOST channel. + * + * \param device - The system path to configure the MOST device (starting with "/sys/"). + * \param mostType - The MOST data type (control, asynchron, synchron, isochron). + * \param direction - The direction of the stream (directionRX, directionTX). + * \param numBuf - The amount of buffers. + * \param bufSize - The size of each buffer in bytes. + * + * \return True if no error + */ + /*----------------------------------------------------------*/ + bool ConfigureMostChannel( const char *device, EPDataType_t mostType, EPDirection_t direction, uint32_t numBuf, + uint32_t bufSize ); + + /*----------------------------------------------------------*/ + /*! \brief Configures the isochronous channel of a device. + * \note This function has to be called before ConfigureMostChannel! + * + * \param device - The system path to configure the MOST device (starting with "/sys/"). + * \param subbufferSize - The amount of bytes for a single subbuffer (eg. 188 or 196 bytes). + * \param packetsPerTransaction - The amount of ischronous packets per transaction. + * + * \return True if no error + */ + /*----------------------------------------------------------*/ + bool ConfigureIsocChannel( const char *device, uint32_t subbufferSize, uint32_t packetsPerTransaction ); + + /*----------------------------------------------------------*/ + /*! \brief Configures the Synchronous channel of a device. + * \note This function has to be called before ConfigureMostChannel! + * + * \param device - The system path to configure the MOST device (starting with "/sys/"). + * \param syncBlockWidth - The bandwidth of a single synchronous frame. Ignored if not sync channel. + * \param amounOfSyncFrames - The amount of synchronous frames per USB packet. Ignored if not sync channel. + * + * \return True if no error + */ + /*----------------------------------------------------------*/ + bool ConfigureSyncChannel( const char *device, uint32_t syncBlockWidth, uint32_t amounOfSyncFrames ); + + + /*----------------------------------------------------------*/ + /*! \brief Creates a character device for the given device's link name. + * + * \param linkName - [in] Link name, which is needed to register this device to an AIM. + * \param deviceName - [in,out] Device name (starting with /dev/). + * + * \return True if no error + */ + /*----------------------------------------------------------*/ + bool LinkToCharacterDevice( const char *linkName, const char *deviceName ); + + + /*----------------------------------------------------------*/ + /*! \brief Creates a audio device (ALSA) for the given device's link name. + * + * \param linkName - [in] Link name, which is needed to register this device to an AIM. + * \param subBufferSize - [in] The amount of bytes for all channels of this connection on the MOST. + * + * \return True if no error + */ + /*----------------------------------------------------------*/ + bool LinkToAudioDevice( const char *linkName, uint16_t subBufferSize ); + + /*----------------------------------------------------------*/ + /*! \brief Creates a Video for Linux device (V4L) for the given device's link name. + * + * \param linkName - [in] Link name, which is needed to register this device to an AIM. + * + * \return True if no error + */ + /*----------------------------------------------------------*/ + bool LinkToVideoForLinuxDevice( const char *linkName ); + + + /*----------------------------------------------------------*/ + /*! \brief Parsed the global errno variable and generate a human readable error description out of it. + * + * \return Zero terminated error description string. + */ + /*----------------------------------------------------------*/ + const char *GetErrnoString(); + +#ifdef __cplusplus +} +#endif + +#endif //DRIVERCONFIGURATION_H |