📄 mib.c
字号:
/*************************************************************************
mib.c
These are the routines to handle the retrieving and setting the various
MIB variables. The routines handle both the CM-1 and CM-2. Refer to
the respective documentation to understand the differences therein.
Copyright (C) 2001-2004 by Cirrus Logic Inc. All Rights Reserved
*************************************************************************/
#include <stdio.h>
#include <intrpt.h>
#include <8051.h>
#include <string.h>
#include "serial.h"
#include "cnmutil.h"
#include "command.h"
#include "test.h"
#include "error.h"
#include "command.h"
#include "hostport.h"
#include "mib.h"
#include "mystrings.h"
#include "cnmibvar.h"
extern unsigned char gWHICH_MODULE; //defines whether CM1 or CM2 is present
extern near unsigned short gMAC_Source[ 3 ];
extern unsigned char get_display_string( unsigned long address_ptr,
char * display_string_ptr ) {
//gets a DisplayString as defined by the CobraNet Datasheet
unsigned char i,j,k;
unsigned char err = cERR_NO_ERROR;
unsigned long temp_long;
char * temp_char_ptr;
unsigned char str_length;
temp_long = Host_Peek( address_ptr , &err );
str_length = ( unsigned char ) ( temp_long & 0xFF );
temp_char_ptr = ( char * ) &temp_long;
k = 0;
for ( i = 1; k < str_length; i++ ) {
temp_long = Host_Peek( ( long ) ( address_ptr + i ), &err );
for ( j = 0; j < ( 2 + gWHICH_MODULE ); j++ )
display_string_ptr[ k++ ] = temp_char_ptr[ j + 2 - gWHICH_MODULE ];
}
display_string_ptr[ str_length ] = cASCII_NULL;
return( err );
}
extern unsigned char put_display_string( unsigned long address_ptr,
char * display_string_ptr ) {
//puts a DisplayString as defined by the CobraNet Datasheet
unsigned char i,j,k;
unsigned char err = cERR_NO_ERROR;
unsigned long temp_long;
char * temp_char_ptr;
temp_char_ptr = ( char * ) &temp_long;
k = 0;
i = 1;
while ( display_string_ptr[ k ] ) {
temp_long = 0;
for ( j = 0; j < ( 2 + gWHICH_MODULE ); j++ )
if ( display_string_ptr[ k ] )
temp_char_ptr[ j + 2 - gWHICH_MODULE ] = display_string_ptr[ k++ ];
err = Host_Poke( address_ptr + i++, temp_long );
}
temp_long = k;
err = Host_Poke( address_ptr, temp_long ); //put the length
return( err );
}
unsigned char get_object_ID_string( unsigned long address_ptr,
char * string_ptr ) {
//gets an object ID as defined by the CobraNet Datasheet
unsigned char i,k,j;
unsigned char err = cERR_NO_ERROR;
unsigned long temp_long;
unsigned char str_length;
unsigned char * temp_char;
*string_ptr = cASCII_NULL; //initialize string
temp_long = Host_Peek( address_ptr , &err );
str_length = ( unsigned char ) ( temp_long & 0xFF );
i = 0;
k = 1;
while ( i < str_length ) {
temp_long = Host_Peek( ( long ) ( address_ptr + k++ ), &err );
temp_char = ( ( unsigned char * ) &temp_long ) + 2 - gWHICH_MODULE;
j = 0;
while ( ( i < str_length) && ( j < ( 2 + gWHICH_MODULE ) ) ) {
strcat( string_ptr, byte2str( *temp_char++ ) );
i++; j++;
}
}
return( err );
}
extern unsigned short get_Integer16( unsigned long address_ptr ) {
unsigned char err = cERR_NO_ERROR;
unsigned long temp_long;
temp_long = Host_Peek( address_ptr , &err );
if ( gWHICH_MODULE == cHOST_CM1 )
temp_long = temp_long >> 8;
return ( ( unsigned short ) temp_long );
}
extern void set_Integer16( unsigned long address_ptr,
unsigned short the_short ) {
unsigned long temp_long;
unsigned char err = cERR_NO_ERROR;
temp_long = ( long ) the_short;
if ( gWHICH_MODULE == cHOST_CM1 )
temp_long = temp_long << 8;
err = Host_Poke( address_ptr, temp_long );
}
extern void get_Integer32_Str( unsigned long address_ptr,
char * string_ptr ) {
//returns ASCII string for 32-bit integer.
unsigned char err = cERR_NO_ERROR;
unsigned long temp_long;
temp_long = Host_Peek( address_ptr , &err );
if ( gWHICH_MODULE == cHOST_CM1 ) {
temp_long = temp_long >> 8;
strcpy( string_ptr, long2str( temp_long, cNO_LEADING_ZEROS,
cFOUR_BYTES ) );
temp_long = Host_Peek( address_ptr + 1 , &err );
temp_long = temp_long >> 8;
strcat( string_ptr, long2str( temp_long, cLEADING_ZEROS,
cFOUR_BYTES ) );
}
else
strcpy( string_ptr, long2str( temp_long, cNO_LEADING_ZEROS,
cEIGHT_BYTES ) );
}
extern void put_Integer32_Str( unsigned long address_ptr,
char * string_ptr ) {
//returns ASCII string for 32-bit integer.
unsigned char err = cERR_NO_ERROR;
unsigned long temp_long = 0;
unsigned char temp_char;
unsigned char char_count = 0;
unsigned char str_len;
str_len = strlen( string_ptr );
if ( gWHICH_MODULE == cHOST_CM1 ) {
if ( str_len > 4 )
while( ( char_count < ( str_len - 4 ) ) ) {
temp_long = temp_long << 4;
temp_long += ASCII2char ( *string_ptr++ );
char_count++;
}
temp_long = temp_long << 8;
err = Host_Poke( address_ptr, temp_long );
temp_long = 0;
while( temp_char = *string_ptr++ ) {
temp_long = temp_long << 4;
temp_long += ASCII2char ( temp_char );
}
temp_long = temp_long << 8;
err = Host_Poke( address_ptr + 1, temp_long );
}
else {
while( temp_char = *string_ptr++ ) {
temp_long = temp_long << 4;
temp_long += ASCII2char ( temp_char );
}
err = Host_Poke( address_ptr, temp_long );
}
}
extern void get_Integer48_Str( unsigned long address_ptr,
char * string_ptr ) {
//returns ASCII string for 48-bit integer.
unsigned char err = cERR_NO_ERROR;
strcpy( string_ptr, long2str( Host_Peek( address_ptr, &err ),
cNO_LEADING_ZEROS, cSIX_BYTES +
( gWHICH_MODULE & 2 ) ) );
strcat( string_ptr, long2str( Host_Peek( address_ptr + 1, &err ),
cLEADING_ZEROS, cSIX_BYTES +
( gWHICH_MODULE & 2 ) ) );
}
extern void put_Integer48_Str( unsigned long address_ptr,
char * string_ptr ) {
//returns ASCII string for 48-bit integer.
unsigned char err = cERR_NO_ERROR;
unsigned long temp_long = 0;
unsigned char temp_char;
unsigned char char_count = 0;
unsigned char str_len;
str_len = strlen( string_ptr );
if ( str_len > 6 )
while( ( temp_char = *string_ptr++ ) &&
( char_count < ( str_len - 6 ) ) ) {
temp_long = temp_long << 4;
temp_long += ASCII2char ( temp_char );
char_count++;
}
err = Host_Poke( address_ptr, temp_long );
temp_long = 0;
while( temp_char = *string_ptr++ ) {
temp_long = temp_long << 4;
temp_long += ASCII2char ( temp_char );
}
err = Host_Poke( address_ptr + 1, temp_long );
}
extern void get_TimeTicks_Str( unsigned long address_ptr,
char * string_ptr ) {
//returns ASCII string for a TimeTicks type variable
unsigned long temp_long1;
unsigned long temp_long2;
unsigned long temp_long3;
unsigned char byte_len = cSIX_BYTES;
unsigned char err = cERR_NO_ERROR;
do {
temp_long1 = Host_Peek( address_ptr, &err );
temp_long2 = Host_Peek( address_ptr + 1, &err );
temp_long3 = Host_Peek( address_ptr, &err );
}
while ( temp_long1 != temp_long3 );
if ( gWHICH_MODULE == cHOST_CM2 )
byte_len = cEIGHT_BYTES;
strcpy( string_ptr, long2str( temp_long1,
cNO_LEADING_ZEROS, byte_len ) );
strcat( string_ptr, long2str( temp_long2,
cLEADING_ZEROS, byte_len ) );
}
extern void get_MAC_Address( void ) {
//stores the MAC address in a global variable
unsigned char i;
unsigned long temp_long;
unsigned char err = cERR_NO_ERROR;
if ( gWHICH_MODULE == cHOST_CM2 ) {
temp_long = Host_Peek( sysMACAddress_ptr, &err );
gMAC_Source[0] = swapBytes( *( ( short * ) &temp_long ) );
gMAC_Source[1] = swapBytes( ( short ) temp_long );
temp_long = Host_Peek( sysMACAddress_ptr + 1, &err );
gMAC_Source[2] = swapBytes( *( ( short * ) &temp_long ) );
}
else {
for ( i = 0; i < 3; i++ ) {
temp_long = Host_Peek( sysMACAddress_ptr + i, &err );
gMAC_Source[i] = swapBytes( ( short )( temp_long >> 8 ) );
}
}
}
extern void get_MAC_Address_Str( unsigned long address_ptr,
char * string_ptr ) {
//returns a MAC address
unsigned char i;
unsigned char err = cERR_NO_ERROR;
char * temp_char_p;
if ( gWHICH_MODULE == cHOST_CM2 ) {
temp_char_p = long2str( Host_Peek( address_ptr, &err ),
cLEADING_ZEROS, cEIGHT_BYTES );
*string_ptr++ = temp_char_p[ 2 ];
*string_ptr++ = temp_char_p[ 3 ];
*string_ptr++ = temp_char_p[ 0 ];
*string_ptr++ = temp_char_p[ 1 ];
*string_ptr++ = temp_char_p[ 6 ];
*string_ptr++ = temp_char_p[ 7 ];
*string_ptr++ = temp_char_p[ 4 ];
*string_ptr++ = temp_char_p[ 5 ];
temp_char_p = long2str( Host_Peek( address_ptr + 1, &err ),
cLEADING_ZEROS, cEIGHT_BYTES );
*string_ptr++ = temp_char_p[ 2 ];
*string_ptr++ = temp_char_p[ 3 ];
*string_ptr++ = temp_char_p[ 0 ];
*string_ptr++ = temp_char_p[ 1 ];
}
else {
for ( i = 0; i < 3; i++ ) {
temp_char_p = long2str( Host_Peek( address_ptr + i, &err ),
cLEADING_ZEROS, cSIX_BYTES );
*string_ptr++ = temp_char_p[ 2 ];
*string_ptr++ = temp_char_p[ 3 ];
*string_ptr++ = temp_char_p[ 0 ];
*string_ptr++ = temp_char_p[ 1 ];
}
}
*string_ptr = cASCII_NULL;
}
extern void put_MAC_Address_Str( unsigned long address_ptr,
char * string_ptr ) {
//puts a MAC address type into CM memory
unsigned char i, j;
unsigned char err = cERR_NO_ERROR;
char * a_char_ptr;
unsigned long temp_long;
char * a_temp_ptr;
a_temp_ptr = ( char * ) &temp_long;
a_char_ptr = string_ptr + strlen( string_ptr ) - 1; //point to last char
if ( gWHICH_MODULE == cHOST_CM2 ) {
temp_long = 0;
for ( j = 0; j < 2; j++ ) {
if ( *a_char_ptr != 'x' ) {
a_temp_ptr[ j ] = ASCII2char( *a_char_ptr-- );
if ( *a_char_ptr != 'x' )
a_temp_ptr[ j ] += ASCII2char( *a_char_ptr-- ) << 4;
}
}
err = Host_Poke( address_ptr + 1, temp_long );
temp_long = 0;
for ( i = 0; i < 2; i++ )
for ( j = 2; j < 4; j++ ) {
if ( *a_char_ptr != 'x' ) {
a_temp_ptr[ j - ( 2 * i ) ] = ASCII2char( *a_char_ptr-- );
if ( *a_char_ptr != 'x' )
a_temp_ptr[ j - ( 2 * i ) ] += ASCII2char( *a_char_ptr-- ) << 4;
}
}
err = Host_Poke( address_ptr, temp_long );
}
else {
for ( i = 0; i < 3; i++ ) {
temp_long = 0;
for ( j = 1; j < 3; j++ ) {
if ( *a_char_ptr != 'x' ) {
a_temp_ptr[ j ] = ASCII2char( *a_char_ptr-- );
if ( *a_char_ptr != 'x' )
a_temp_ptr[ j ] += ASCII2char( *a_char_ptr-- ) << 4;
}
}
err = Host_Poke( address_ptr + 2 - i, temp_long );
}
}
}
extern void get_IP_Address_Str( unsigned long address_ptr,
char * string_ptr ) {
//returns an IP address, formatted 0.0.0.0
unsigned long temp_long;
unsigned char err = cERR_NO_ERROR;
unsigned char * temp_ptr;
temp_ptr = ( unsigned char * ) &temp_long;
temp_long = Host_Peek( address_ptr, &err );
if ( gWHICH_MODULE == cHOST_CM2 ) {
strcpy( string_ptr, long2ascii( ( long )temp_ptr[ 1 ] ) );
strCodecat( string_ptr, str_Period );
strcat( string_ptr,long2ascii( ( long )temp_ptr[ 0 ] ) );
strCodecat( string_ptr, str_Period );
strcat( string_ptr, long2ascii( ( long )temp_ptr[ 3 ] ) );
strCodecat( string_ptr, str_Period );
strcat( string_ptr,long2ascii( ( long )temp_ptr[ 2 ] ) );
}
else {
strcpy( string_ptr, long2ascii( ( long )temp_ptr[ 2 ] ) );
strCodecat( string_ptr, str_Period );
strcat( string_ptr,long2ascii( ( long )temp_ptr[ 1 ] ) );
strCodecat( string_ptr, str_Period );
temp_long = Host_Peek( address_ptr + 1, &err );
strcat( string_ptr, long2ascii( ( long )temp_ptr[ 2 ] ) );
strCodecat( string_ptr, str_Period );
strcat( string_ptr,long2ascii( ( long )temp_ptr[ 1 ] ) );
}
}
extern void put_IP_Address_Str( unsigned long address_ptr,
char * string_ptr ) {
//puts an IP address, receives a valid string
unsigned long temp_long;
unsigned char err = cERR_NO_ERROR;
unsigned char str_ptr[ 4 ]; //for the longest string
unsigned char * temp_ptr;
unsigned char i, j, k;
unsigned char err;
temp_ptr = ( unsigned char * ) &temp_long;
if ( gWHICH_MODULE == cHOST_CM2 ) {
temp_long = 0;
j = 0;
for ( i = 0; i < 2; i++ ) {
k = 0;
while ( ( *string_ptr != '.' ) && ( *string_ptr ) )
str_ptr[ k++ ] = *string_ptr++;
str_ptr[ k ] = cASCII_NULL;
string_ptr++;
temp_ptr[ i + j + 1 ] = ( char )( alpha2long( ( char * ) str_ptr ) );
k = 0;
while ( ( *string_ptr != '.' ) && ( *string_ptr ) )
str_ptr[ k++ ] = *string_ptr++;
str_ptr[ k ] = cASCII_NULL;
string_ptr++;
temp_ptr[ i + j ] = ( char )( alpha2long( ( char * ) str_ptr ) );
j = 1;
}
err = Host_Poke( address_ptr, temp_long );
}
else {
for ( j = 0; j < 2; j++ ) {
temp_long = 0;
for ( i = 2; i > 0; i-- ) {
k = 0;
while ( ( *string_ptr != '.' ) && ( *string_ptr ) )
str_ptr[ k++ ] = *string_ptr++;
str_ptr[ k ] = cASCII_NULL;
string_ptr++;
temp_ptr[ i ] = ( char )( alpha2long( ( char * ) str_ptr ) );
}
err = Host_Poke( address_ptr + j, temp_long );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -