|
| 1 | +#include <dos.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include <time.h> |
| 5 | + |
| 6 | +#include "trace.h" |
| 7 | +#include "utils.h" |
| 8 | +#include "packet.h" |
| 9 | +#include "arp.h" |
| 10 | +#include "udp.h" |
| 11 | +#include "dns.h" |
| 12 | + |
| 13 | + |
| 14 | +static void parseArgs( int argc, char *argv[] ); |
| 15 | +static void shutdown( int rc ); |
| 16 | + |
| 17 | + |
| 18 | +static char ServerAddrName[] = "tree.tannerstokes.com"; |
| 19 | +uint16_t treePort = 8733; |
| 20 | + |
| 21 | +uint8_t command_length = 0; |
| 22 | +uint8_t command_data[255]; |
| 23 | + |
| 24 | +// Check this flag once in a while to see if the user wants out. |
| 25 | +volatile uint8_t CtrlBreakDetected = 0; |
| 26 | + |
| 27 | + void __interrupt __far ctrlBreakHandler( ) { |
| 28 | + CtrlBreakDetected = 1; |
| 29 | + } |
| 30 | + |
| 31 | + void __interrupt __far ctrlCHandler( ) { |
| 32 | + // Do Nothing - Ctrl-C is a legal character |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | +int main( int argc, char *argv[] ) { |
| 37 | + |
| 38 | + parseArgs( argc, argv ); |
| 39 | + |
| 40 | + if ( Utils::parseEnv( ) != 0 ) { |
| 41 | + exit(-1); |
| 42 | + } |
| 43 | + |
| 44 | + if ( Utils::initStack( 0, 0, ctrlBreakHandler, ctrlCHandler ) ) { |
| 45 | + fprintf( stderr, "\nFailed to initialize TCP/IP - exiting\n" ); |
| 46 | + exit(-1); |
| 47 | + } |
| 48 | + |
| 49 | + IpAddr_t ipAddr; |
| 50 | + int8_t rc = Dns::resolve( ServerAddrName, ipAddr, 1 ); |
| 51 | + |
| 52 | + while ( 1 ) { |
| 53 | + if ( CtrlBreakDetected ) { |
| 54 | + break; |
| 55 | + } |
| 56 | + |
| 57 | + if ( biosIsKeyReady( ) ) { |
| 58 | + char c = biosKeyRead( ); |
| 59 | + if ( (c == 27) || (c == 3) ) { |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + PACKET_PROCESS_SINGLE; |
| 65 | + Arp::driveArp( ); |
| 66 | + Dns::drivePendingQuery( ); |
| 67 | + |
| 68 | + if ( !Dns::isQueryPending( ) ) { |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + rc = Dns::resolve( ServerAddrName, ipAddr, 0 ); |
| 74 | + if ( rc != 0 ) { |
| 75 | + printf( "Failed to resolve DNS.\n"); |
| 76 | + shutdown ( -1 ); |
| 77 | + return 0; |
| 78 | + } |
| 79 | + |
| 80 | + if ( command_length == 0 ) { |
| 81 | + printf( "Command length not set!\n" ); |
| 82 | + shutdown ( -1 ); |
| 83 | + return 0; |
| 84 | + } |
| 85 | + |
| 86 | + while( 1 ) { |
| 87 | + if ( CtrlBreakDetected ) { |
| 88 | + break; |
| 89 | + } |
| 90 | + |
| 91 | + if ( biosIsKeyReady( ) ) { |
| 92 | + char c = biosKeyRead( ); |
| 93 | + if ( (c == 27) || (c == 3) ) { |
| 94 | + break; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + PACKET_PROCESS_SINGLE; |
| 99 | + Arp::driveArp( ); |
| 100 | + rc = Udp::sendUdp( ipAddr, treePort, treePort, command_length, command_data, 0 ); |
| 101 | + if ( rc < 1 ) { |
| 102 | + break; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if ( rc == 0 ) { |
| 107 | + printf( "Command sent to tree!\n" ); |
| 108 | + } else { |
| 109 | + printf( "There was a problem sending the packet. Return code: %d\n", rc ); |
| 110 | + } |
| 111 | + |
| 112 | + shutdown( 0 ); |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +static void shutdown( int rc ) { |
| 117 | + Utils::endStack( ); |
| 118 | + Utils::dumpStats( stderr ); |
| 119 | + exit( rc ); |
| 120 | +} |
| 121 | + |
| 122 | +char *HelpText[] = { |
| 123 | + "\nxmastree <command> [options]\n\n", |
| 124 | + "Available commands:\n", |
| 125 | + " brightness [0-255] Set the tree's brightness\n", |
| 126 | + " fill [0-255][0-255][0-255] Fill all LEDs with a single RGB color\n", |
| 127 | + " rainbow [0-1] Show a rainbow sequence that can repeat\n", |
| 128 | + " off Turn the tree off\n", |
| 129 | + " help Show this help\n", |
| 130 | + NULL |
| 131 | +}; |
| 132 | + |
| 133 | + |
| 134 | +void usage( void ) { |
| 135 | + uint8_t i=0; |
| 136 | + while ( HelpText[i] != NULL ) { |
| 137 | + fprintf( stderr, HelpText[i] ); |
| 138 | + i++; |
| 139 | + } |
| 140 | + exit( 1 ); |
| 141 | +} |
| 142 | + |
| 143 | +void parseArgs( int argc, char *argv[] ) { |
| 144 | + if ( argc < 2 ) { |
| 145 | + usage ( ); |
| 146 | + } |
| 147 | + |
| 148 | + if ( stricmp( argv[1], "help" ) == 0 ) { |
| 149 | + usage( ); |
| 150 | + } |
| 151 | + else if ( stricmp( argv[1], "brightness" ) == 0 ) { |
| 152 | + if ( argc != 3 ) { |
| 153 | + usage ( ); |
| 154 | + } |
| 155 | + command_length = 2; |
| 156 | + command_data[0] = 1; |
| 157 | + command_data[1] = atoi( argv[2] ); |
| 158 | + } |
| 159 | + else if ( stricmp( argv[1], "fill" ) == 0 ) { |
| 160 | + if ( argc != 5 ) { |
| 161 | + usage ( ); |
| 162 | + } |
| 163 | + command_length = 4; |
| 164 | + command_data[0] = 3; |
| 165 | + command_data[1] = atoi( argv[2] ); |
| 166 | + command_data[2] = atoi( argv[3] ); |
| 167 | + command_data[3] = atoi( argv[4] ); |
| 168 | + } |
| 169 | + else if ( stricmp( argv[1], "rainbow" ) == 0 ) { |
| 170 | + if ( argc != 3 ) { |
| 171 | + usage ( ); |
| 172 | + } |
| 173 | + command_length = 2; |
| 174 | + command_data[0] = 5; |
| 175 | + command_data[1] = atoi( argv[2] ); |
| 176 | + } |
| 177 | + else if ( stricmp( argv[1], "off" ) == 0 ) { |
| 178 | + if ( argc != 2 ) { |
| 179 | + usage ( ); |
| 180 | + } |
| 181 | + command_length = 1; |
| 182 | + command_data[0] = 0; |
| 183 | + } |
| 184 | + else { |
| 185 | + fprintf( stderr, "Unknown option %s\n", argv[1] ); |
| 186 | + usage( ); |
| 187 | + } |
| 188 | + |
| 189 | +} |
0 commit comments