This repository has been archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
108 lines (89 loc) · 2.96 KB
/
Main.cpp
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
//=============================================================================
//File Name: Main.cpp
//Description: Accepts commands that manipulate the FRC robot code module
//Author: FRC Team 3512, Spartatroniks
//=============================================================================
/* Supports the following commands via Telnet:
* 1. reboot (reboots the cRIO)
* 2. reload (unloads /c/ni-rt/system/FRC_UserProgram.out then reloads it)
* 3. listTasks (lists all task IDs and their corresponding names for the tasks
* which are running on the system)
*/
// Port on which ALF waits for commands
const int gPortNumber = 3512;
#include <cstring>
// For STATUS typedef
#include <types/vxTypesOld.h>
#include <signal.h>
#include <arch/ppc/coprocPpc.h>
#include <sockLib.h>
#include <in.h>
#define socklen_t int
#include "Commands.h"
// Load on startup
int alf_entrypoint();
int alf_main();
const int32_t alf_entrystatus = alf_entrypoint();
// Process input from socket
int processCommands( int sockfd ) {
char buf[1024] = {};
char *cbuf;
// Receive string from client
for ( cbuf = buf ; *(cbuf-1) != '\n' ; cbuf++ ) {
if( recv( sockfd , cbuf , 1 , 0 ) != 1 ) {
return -1;
}
}
// Add the null terminator
*(cbuf-2) = '\0';
// Match the string with valid commands
if( std::strcmp( buf , "reboot" ) == 0 ) {
alf_reboot();
}
else if ( std::strcmp( buf , "reload" ) == 0 ) {
alf_reload( "FRC_UserProgram.out" );
}
else if ( std::strcmp( buf , "save" ) == 0 ) {
alf_copy( "/ni-rt/system/FRC_UserProgram.out" , "/ni-rt/system/FRC_SafeProgram.out" );
}
else if ( std::strcmp( buf , "fallback" ) == 0 ) {
alf_reload( "FRC_SafeProgram.out" );
}
else if ( std::strcmp( buf , "listTasks" ) == 0 ) {
alf_listTasks();
}
return 0;
}
int alf_entrypoint() {
char strTaskName[] = "ALF";
taskSpawn( strTaskName, // Task name
101, // Priority
VX_FP_TASK, // Task option flag
0xFFFF, // Stack size
(FUNCPTR)alf_main, // Entry point
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ); // Unused task args
return 0;
}
int alf_main() {
int sockfd, nsockfd;
int portno;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
sockfd = socket( AF_INET , SOCK_STREAM , 0 );
bzero( (char*)&serv_addr , sizeof(serv_addr));
portno = gPortNumber;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
bind( sockfd , (struct sockaddr*)&serv_addr , sizeof(serv_addr) );
listen( sockfd , 5 );
clilen = sizeof(cli_addr);
signal( SIGPIPE , SIG_IGN );
while( 1 ) {
// Accept connections from elsewhere to receive commands
nsockfd = accept( sockfd , (struct sockaddr*)&cli_addr , &clilen );
// Process input from socket
while ( processCommands( nsockfd ) == 0 );
}
return 0;
}