Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
363 johboh 1
#include <string.h>
2
 
3
#include "..\Include\StackTsk.h"
4
#include "..\Include\SGP.h"
5
#include "..\Include\TCP.h"
6
#include "..\Include\Helpers.h"
7
#include "..\Include\UART.h"
8
#include "..\Include\compiler.h"
9
 
10
#if defined(STACK_USE_SGP_SERVER)
11
 
12
#define MAX_SGP_CONNECTIONS 2
13
#define MAX_SGP_CMD_LEN 100
14
 
15
#if (MAC_TX_BUFFER_SIZE <= 130 || MAC_TX_BUFFER_SIZE > 1500 )
16
#error SGP : Invalid MAC_TX_BUFFER_SIZE value specified.
17
#endif
18
 
19
 
20
// SGP FSM states for each connection.
21
typedef enum _SM_SGP
22
{
23
    SM_SGP_IDLE = 0,
24
    SM_SGP_GET,
25
    SM_SGP_DISCONNECT,
26
    SM_SGP_DISCONNECT_WAIT,
27
    SM_SGP_DISCARD
28
} SM_SGP;
29
 
30
 
31
// SGP Connection Info - one for each connection.
32
typedef struct _SGP_INFO
33
{
34
    TCP_SOCKET socket;
35
    SM_SGP smSGP;
36
 
37
    BYTE bProcess;
38
 
39
} SGP_INFO;
40
typedef BYTE SGP_HANDLE;
41
 
42
 
43
static SGP_INFO HCB[MAX_SGP_CONNECTIONS];
44
 
45
static void SGPProcess(SGP_HANDLE h);
46
 
47
 
48
void SGPInit(void)
49
{
50
    BYTE i;
51
 
52
    for ( i = 0; i <  MAX_SGP_CONNECTIONS; i++ )
53
    {
54
        HCB[i].socket = TCPListen(SGP_PORT);
55
        HCB[i].smSGP = SM_SGP_IDLE;
56
    }
57
}
58
 
59
 
60
 
61
void SGPServer(void)
62
{
63
    BYTE conn;
64
 
65
    for ( conn = 0;  conn < MAX_SGP_CONNECTIONS; conn++ )
66
        SGPProcess(conn);
67
}
68
 
69
 
70
/*********************************************************************
71
 * Function:        static BOOL HTTPProcess(HTTP_HANDLE h)
72
 *
73
 * PreCondition:    HTTPInit() called.
74
 *
75
 * Input:           h   -   Index to the handle which needs to be
76
 *                          processed.
77
 *
78
 * Output:          Connection referred by 'h' is served.
79
 *
80
 * Side Effects:    None
81
 *
82
 * Overview:
83
 *
84
 * Note:            None.
85
 ********************************************************************/
86
static void SGPProcess(SGP_HANDLE h)
87
{
88
    BYTE sgpData[MAX_SGP_CMD_LEN+1];
89
    WORD sgpLength;
90
    BOOL lbContinue;
91
    SGP_INFO* ph;
92
    ROM char* romString;
93
    BYTE i;
94
 
95
    ph = &HCB[h];
96
 
97
    do
98
    {
99
        lbContinue = FALSE;
100
 
101
        // If during handling of HTTP socket, it gets disconnected,
102
        // forget about previous processing and return to idle state.
103
        if(!TCPIsConnected(ph->socket))
104
        {
105
            ph->smSGP = SM_SGP_IDLE;
106
            break;
107
        }
108
 
109
 
110
        switch(ph->smSGP)
111
        {
112
        case SM_SGP_IDLE:
113
            if(TCPIsGetReady(ph->socket))
114
            {
115
 
116
                lbContinue = TRUE;
117
 
118
                sgpLength = 0;
119
                while(sgpLength < MAX_SGP_CMD_LEN &&
120
                      TCPGet(ph->socket, &sgpData[sgpLength++]) );
121
                sgpData[sgpLength] = '\0';
122
                TCPDiscard(ph->socket);
123
 
124
 
125
                    ph->smSGP = SM_SGP_GET;
126
            }
127
            break;
128
 
129
        case SM_SGP_GET:
130
 
131
            if ( TCPIsPutReady(ph->socket) )
132
            {
133
 
134
                for(i=0;i<sgpLength;i++)
135
                    TCPPut(ph->socket, sgpData[i]);
136
 
137
                if (sgpData[0]=='1') LED1_IO=1;
138
                if (sgpData[0]=='2') LED1_IO=0;
139
 
140
                TCPFlush(ph->socket);
141
                ph->smSGP = SM_SGP_DISCONNECT;
142
            }
143
        break;
144
 
145
 
146
        case SM_SGP_DISCONNECT:
147
            if(TCPIsConnected(ph->socket))
148
            {
149
                if(TCPIsPutReady(ph->socket))
150
                {
151
                    TCPDisconnect(ph->socket);
152
 
153
                    // Switch to not-handled state.  This FSM has
154
                    // one common action that checks for disconnect
155
                    // condition and returns to Idle state.
156
                    ph->smSGP = SM_SGP_DISCONNECT_WAIT;
157
                }
158
            }
159
            break;
160
 
161
        }
162
    } while( lbContinue );
163
}
164
 
165
 
166
#endif //#if defined(STACK_USE_SGP_SERVER)