Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Eddie robots allowed on one network #5

Merged
merged 12 commits into from
Mar 5, 2015
Merged
Prev Previous commit
Next Next commit
Added ability to set a custom name to be returned with a DISCOVER pac…
…ket.

Will need a proper location to store said settings file.
r3n33 committed Feb 27, 2015
commit 8ce1cd235796ae3a7089fa862d77c69d0846ac0f
Binary file modified src/EddieBalance
Binary file not shown.
130 changes: 83 additions & 47 deletions src/identity.h
Original file line number Diff line number Diff line change
@@ -1,52 +1,88 @@
#ifndef IDENTITY_H
#define IDENTITY_H

#include <stdio.h>

int thisEddieSerial;

void initIdentity();
unsigned short checksum( const char * key, int len );

void initIdentity()
{
char responseBuff[256] = {0};
FILE * pipe = popen( "ls /dev/disk/by-uuid\n", "r" );
if (pipe == NULL )
{
printf("Eddie::initIdentity: Invoking command failed.\r\n");
}
while( fgets( responseBuff + strlen(responseBuff), sizeof(responseBuff) - strlen(responseBuff), pipe ) != NULL );
pclose( pipe );

thisEddieSerial = checksum( responseBuff, strlen( responseBuff ) );

//DEBUG: printf("Eddie Identity Found: %04x\r\n", thisEddieSerial);
}

unsigned short checksum( const char * key, int len )
{
unsigned short crc = 0xFFFF;
int i, j;

for( i=0; i<len; ++i )
{
char data = key[i];
crc = crc ^ ( data << 8 );
for ( j=0; j < 8; ++j )
{
if ( ( crc & 0x8000 ) != 0 )
{
crc = ( crc << 1 ) ^ 0x1021;
}
else
{
crc <<= 1;
}
}
}
return crc;
}

#include <stdio.h>

char thisEddieName[64];

int initName();
void setName( char * p_name );
void initIdentity();
unsigned short checksum( const char * key, int len );

int initName()
{
char responseBuff[64] = {0};
int pipe = open( "/home/root/EddieBalance/src/settingsTest", O_RDONLY );
if (pipe == -1 )
{
printf("Eddie::initName: Open file for reading failed.\r\n");
return 0;
}
if ( read( pipe, thisEddieName, sizeof(thisEddieName) ) )
{
//DEBUG: printf( "Eddie::initName: set name to: %s\r\n", thisEddieName );
return 1;
}
return 0;
}

void setName( char * p_name )
{
int pipe = open( "/home/root/EddieBalance/src/settingsTest", O_WRONLY | O_CREAT );
if (pipe == -1 )
{
printf("Eddie::setName: Open file for writing failed.\r\n");
return;
}
if ( write( pipe, p_name, sizeof(thisEddieName) ) )
{
strcpy( thisEddieName, p_name );
}
}

void initIdentity()
{
if ( !initName() ) //If initName fails no name has been saved. Generate a unique ID to append to default name.
{
int thisEddieID;
char responseBuff[256] = {0};
FILE * pipe = popen( "ls /dev/disk/by-uuid\n", "r" );
if (pipe == NULL )
{
printf("Eddie::initIdentity: Invoking command failed.\r\n");
}
while( fgets( responseBuff + strlen(responseBuff), sizeof(responseBuff) - strlen(responseBuff), pipe ) != NULL );
pclose( pipe );

thisEddieID = checksum( responseBuff, strlen( responseBuff ) );
sprintf( thisEddieName, "EddieBalance[%04x]", thisEddieID );
}
}

unsigned short checksum( const char * key, int len )
{
unsigned short crc = 0xFFFF;
int i, j;

for( i=0; i<len; ++i )
{
char data = key[i];
crc = crc ^ ( data << 8 );
for ( j=0; j < 8; ++j )
{
if ( ( crc & 0x8000 ) != 0 )
{
crc = ( crc << 1 ) ^ 0x1021;
}
else
{
crc <<= 1;
}
}
}
return crc;
}


#endif //--IDENTITY_H
15 changes: 10 additions & 5 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -9,11 +9,11 @@
the GPL2 ("Copyleft").
*/

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <math.h>
#include <signal.h>
#include <stdarg.h> //print function
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

#include "mraa.h"
@@ -109,7 +109,12 @@ void UDP_Control_Handler( char * p_udpin )

if ( !memcmp( p_udpin, "DISCOVER", 8 ) )
{
print( "EddiePlusRobotHere[%04x]", thisEddieSerial );
print( thisEddieName );
}
else if ( strncmp( p_udpin, "SETNAME", 7 ) == 0 )
{
setName( &p_udpin[7] );
print( "My name is: %s", thisEddieName );
}
}

@@ -431,7 +436,7 @@ initIdentity();

print( "Eddie is cleaning up...\r\n" );

CloseEncoder();
CloseEncoder();

pthread_join(udplistenerThread, NULL);
print( "UDP Thread Joined..\r\n" );