コード例 #1
0
ファイル: wifi.c プロジェクト: yallawalla/stm32
bool ProcessUserInput(void)
{
    char	tmp[100];

		if(DbgScan("%s", tmp)==1)
		switch(tmp[0]) {
		case 'q':
        return false;
    
		case '0':
        GetStatus(seqNo++);
        break;

    case '1':
        WifiScan(seqNo++);
        break;

    case '2':
        WifiDisconn(seqNo++);
        WifiJoin(seqNo++);
        SnicInit(seqNo++);
        SnicIPConfig(seqNo++);
        break;

    case '3':
        SnicInit(seqNo++);
        SnicGetDhcp(seqNo++);
        break;

    case '4':
        mysock = -1;
        tcpCreateSocket(0, 0xFF, 0xFF, seqNo++, SNIC_TCP_CREATE_SOCKET_REQ);
        if (mysock != -1) {
            if (getTCPinfo() == CMD_ERROR) {
                DbgPrint("Invalid Server\r\n");
                break;
            }
            // This connection can receive data upto 0x0400=1K bytes at a time.
            tcpConnectToServer(mysock, destIP, (unsigned short)destPort, 0x0400, 0x5, seqNo++);
        }
        break;

    case '5':
        if (setTCPinfo() == CMD_ERROR) {
            DbgPrint("Invalid Server to create\r\n");
            break;
        }
        mysock = -1;
        tcpCreateSocket(1, srcIP, (unsigned short)srcPort, seqNo++, SNIC_TCP_CREATE_SOCKET_REQ);
        if (mysock != -1) {
            // This connection can receive data upto TEST_BUFFERSIZE at a time.
            tcpCreateConnection(mysock, TEST_BUFFERSIZE, 0x5, seqNo++);
        }
        break;

    case '6': {
        char tempstr[2] = {0};
        int8u datamode;
        char sockstr[8];
        int32u sock;
        char teststr[128];
        int len;

        DbgPrint("Enter socket number to send from: \r\n");
        DbgScan("%s", sockstr);
        sock = strtol(sockstr, NULL, 0);

        DbgPrint("Content Option? (0: Default  1: User specific) \r\n");
        DbgScan("%s", tempstr);
        datamode = atoi(tempstr);

        if (datamode) {
            DbgPrint("Enter payload to send (up to 128 bytes): \r\n");
            DbgScan("%s", teststr);
            len = (int)strlen(teststr);
            sendFromSock(sock, (int8u*)teststr, len, 2, seqNo++);
        } else {
            sendFromSock(sock, TxBuf, TEST_BUFFERSIZE, 2, seqNo++);
            pktcnt = 0;
        }
        break;
    }

    case '7':
        SnicCleanup(seqNo++);
        WifiDisconn(seqNo++);
        break;

    case '8':
        ApOnOff(seqNo++);
        break;

    case '9': {//udp send
        int i;
        udpCreateSocket(0, 0, 0, seqNo++);
        if (mysock != -1) {
            if (getUDPinfo() == CMD_ERROR) {
                DbgPrint("Invalid Server\r\n");
                break;
            }
            DbgPrint("Send %d\r\n", UDP_NUM_PKT);
            for (i=0; i<UDP_NUM_PKT; i++) {
                int si = i % TEST_BUFFERSIZE + 1;
                SendSNIC(TxBuf, si);
                DbgPrint("%d %d\r\n", i, si);
            }

            closeSocket(mysock,seqNo++);
        }
        break;
    }

    case 'a': {//udp recv
        int16u port = 43211;
        int32u ip = 0xAC1F0001; // 172.31.0.1
        udpCreateSocket(1, ip, port, seqNo++);
        udpStartRecv(mysock, 2048, seqNo++);
        break;
    }

    case 'b':
        SnicCleanup(seqNo++);
        WifiOff(seqNo++);
        break;
    case 'c':
        WifiOn(seqNo++);
        break;

    case 'd':
        DbgPrint("Enter server name:  %s\r\n", domain);
        DbgScan("%s", tmp);
        DbgPrint("\r\n");
        if (strlen(tmp)) 
            strcpy(domain, tmp);
        sendHttpReqTest(domain, 0);
        break;

    case'e':
        DbgPrint("Enter server name: ([CR] to accept %s)\r\n", domain);
        DbgScan("%s", tmp);
        DbgPrint("\r\n");
        if (strlen(tmp)) 
        strcpy(domain, tmp);
        sendHttpPostDemo(domain);
        break;

    case 'f':
        DbgPrint("Make sure STA is connected to SN8200 soft AP.\r\n");
        strcpy(domain, "sn8200.com");
        DbgPrint("Enter server name: ([CR] to accept %s)\r\n", domain);
        DbgScan("%s", tmp);
        DbgPrint("\r\n");
        if (strlen(tmp)) 
            strcpy(domain, tmp);
        sendHttpJsonPostDemo(domain);
        break;
#if 1

    case 'g':
        strcpy(domain, "192.168.10.100");
        DbgPrint("Enter server name (or the peer testclient IP, peer testclient should start TCP server on port 80): ([CR] to accept %s)\r\n", domain);
        DbgScan("%s", tmp);
        DbgPrint("\r\n");
        if (strlen(tmp)) 
            strcpy(domain, tmp);
        sendHttpChunkReqTest(domain);
        break;
#endif

    case 'h':
        DbgPrint("Enter server name: ([CR] to accept %s)\r\n", domain);
        DbgScan("%s", tmp);
        DbgPrint("\r\n");
        if (strlen(tmp)) 
            strcpy(domain, tmp);
        sendHttpReqTest(domain, 1);
        break;

    case 'i':
        timeout1 = 5;
        mysock = -1;
        tcpCreateSocket(0, 0xFF, 0xFF, seqNo++, SNIC_TCP_CREATE_SIMPLE_TLS_SOCKET_REQ);  // use less memory in SN8200
        mdelay(500);
        if (mysock != -1) {
            strcpy(Portstr, "443");
 
					if (getTCPinfo() == CMD_ERROR) {
                DbgPrint("Invalid Server\r\n");
                break;
            }
					
            tcpConnectToServer(mysock, destIP, (unsigned short)destPort,0x0000,timeout1,seqNo++);
            while ((sockConnected == -1) && timeout1) {
                mdelay(500);
                timeout1--;
                if (sockClosed == mysock) {
                    sockClosed = -1;
                    break;
                }
            }

            if (sockConnected == mysock) {
                sendFromSock(mysock, (int8u*)GET_REQUEST, sizeof(GET_REQUEST)-1, 2, seqNo++);
                sockConnected = -1;
            }
            else DbgPrint("Connect failed.\r\n");
        }
        break;

    case 'j': //ssl server
        strcpy(Portstr, "443");
        if (setTCPinfo() == CMD_ERROR) {
            DbgPrint("Invalid Server to create\r\n");
            break;
        }
        mysock = -1;
        tcpCreateSocket(1, srcIP, (unsigned short)srcPort, seqNo++, SNIC_TCP_CREATE_ADV_TLS_SOCKET_REQ);
        if (mysock != -1) {
            // This connection can receive data upto TEST_BUFFERSIZE at a time. 
            tcpCreateConnection(mysock, TEST_BUFFERSIZE, 0x5, seqNo++);
        }
        break;

    case 'm':
        ShowMenu();
        break;

    default:
        break;
    }
		return true;

}
コード例 #2
0
ファイル: sn8200_api.c プロジェクト: hgk333/SmartBell
}void handleRxSNIC(uint8_t* buf, int len)
{    uint8_t subCmdId = buf[0];
    static int times = 0;
    static int isPrintable = 0;
    switch (subCmdId) {
    case SNIC_CLOSE_SOCKET_RSP: {
        if (SNIC_SUCCESS != buf[2]) 
            printf("Close socket failed\n\r");
        else {
            printf("Socket closed\n\r");
        }
    }
            break;
    case SNIC_IP_CONFIG_RSP: {
        IsSNICIPConfigResponsed = true;
        ipok = 0;
        if (SNIC_SUCCESS == buf[2]) {
            printf("IPConfig OK\n\r");
            ipok = 1;
        } else
            printf("IPConfig fail\n\r");
    }
    break;
    case SNIC_GET_DHCP_INFO_RSP: {
        IsSNICGetDHCPInfoResponsed = true;
        if (SNIC_SUCCESS == buf[2]) {
            printf("IP assigned as %i.%i.%i.%i \n\r", buf[9],buf[10],buf[11],buf[12]);
            //save IP
            memcpy(&selfIP, buf+9, 4);
        } else
            printf("IP not assigned\n\r");
    }
    break;
    case SNIC_TCP_CREATE_SOCKET_RSP:
    case SNIC_TCP_CREATE_ADV_TLS_SOCKET_RSP:
    case SNIC_TCP_CREATE_SIMPLE_TLS_SOCKET_RSP:
    case SNIC_UDP_CREATE_SOCKET_RSP: {
        IsCreateSocketResponsed = true;
        if (SNIC_SUCCESS == buf[2]) {
            mysock = buf[3];
            printf("Socket %d opened\n\r", mysock);
        } else
            printf("Socket creation failed\n\r");
    }
    break;
    case SNIC_TCP_CONNECT_TO_SERVER_RSP: {
        IsSNICTCPConnectToServerResponsed = true;
        if (SNIC_CONNECT_TO_SERVER_PENDING == buf[2] || SNIC_SUCCESS == buf[2])
            ;
        else
            printf("Unable to connect server\n\r");
    }
    break;
    
    case SNIC_TCP_CREATE_CONNECTION_RSP:
            {
                if (SNIC_SUCCESS != buf[2])
                    printf("Unable to create TCP server\n\r");
            }
    break;
    case SNIC_TCP_CONNECTION_STATUS_IND: {
        if (SNIC_CONNECTION_UP == buf[2]) {
            printf("Socket connection UP\n\r");
            sockConnected = buf[3];
        }
        else if (SNIC_CONNECTION_CLOSED == buf[2]) {
            printf("Socket %i closed\n\r", buf[3]);
            sockClosed = buf[3];
        }
    }
    break;
    case SNIC_SEND_RSP: {
        int32u sentsize;
        IsSNICSendFromSocketResponsed = true;
        if (SNIC_SUCCESS == buf[2]) {
            pktcnt ++;
            sentsize = ((int32u)(buf[3] << 8) | (int32u)buf[4]);
            printf("pkt %d, %d bytes sent \n\r", pktcnt, sentsize);
        }
    }
    break;
    case SNIC_CONNECTION_RECV_IND: {
        int i=0;
        int32u sentsize = ((int32u)(buf[3] << 8) | (int32u)buf[4]);
        int32u sock = (int32u)buf[2];
        printf("%d bytes received from socket %d \n\r", sentsize, sock);
        mdelay(10);
        if (strncmp((char*)buf+5, "GET /", 5) == 0 || strncmp((char*)buf+5, "POST /", 6) == 0) { // Receives a HTTP(S) get/post request.
            static int i=0;
            for (i=0; i<sentsize; i++) {
                printf("%c", buf[5+i]);
            }
            i = 0;
            sprintf(out, HTTP_RSP_STR, i++);
            // Send back something
            sendFromSock(sock, (int8u*)out, strlen(out), 2, seqNo++);
        
            // If it is a TLS server, it only accepts one connection, so
            // it is better for the host app to close it after some idle time for new connections.
            //	Sleep(100);
            //	closeSocket((int8u)sock,seqNo++);
        }
        else if (strncmp((char*)buf+5, "HTTP/1.", 7) == 0) { // Receives HTTP response, close socket. If the socket is not closed,
             for (i=0; i<sentsize; i++) {
                printf("%c", buf[5+i]);
            }                                              // it can be used for more data communication (using send from socket).
            closeSocket((int8u)sock,seqNo++); }
    }
    break;
    case SNIC_TCP_CLIENT_SOCKET_IND: {
        int8u listen_sock = buf[2];
        printf("Accepted connection from %i.%i.%i.%i\n\r", buf[4], buf[5], buf[6], buf[7]);
        printf("Connection socket: %d\n\r", buf[3]);
    }
    break;
    case SNIC_UDP_RECV_IND: {
        printf("%d %d\n\r", times++, htons(*((int16u*)&buf[9])));
    }
    break;
    case SNIC_UDP_SEND_FROM_SOCKET_RSP: {
        IsSNICUDPSendFromSocketResponsed = true;
        if (SNIC_SUCCESS != buf[2]) {
            printf("UDP Send bad\n\r");
        }
        sendUDPDone = 1;
        break;
    }
    case SNIC_HTTP_REQ|0x80:
    case SNIC_HTTPS_REQ|0x80:
    case SNIC_HTTP_MORE_REQ|0x80: {	
        char *contentT = "";
        unsigned short len = *((short*)&buf[4]);
        short contTLen = 0;
        unsigned short moreData = len & 0x8000;	
        char more[10] = {0};
        int8u seq = buf[1];
        short status = *((short*)&buf[2]);
                
        status = swap16(status);
        len = swap16(len);
        
        if (subCmdId == (SNIC_HTTP_MORE_REQ|0x80))
            strcpy(more, "more ");
        len &= 0x7fff;
        
        if (status >= 100) {
            contentT = (char*)&buf[6];;
            contTLen = strlen(contentT)+1;
        }
        
        if (status < 100) {
            printf("\nHTTP %sRSP code: %d, seq#: %d\n\r", more, status, seq);
            break;
        }
        printf("\nHTTP %sRSP code: %d, seq#: %d, Content Length: %d, Type: %s, More data: %s\n\r", more, status, seq, len, contentT, moreData?"yes":"no");
        isPrintable = 0;
        if (contTLen && (strstr(contentT, "text") || strstr(contentT, "xml") ||
            strstr(contentT, "javascript") || strstr(contentT, "html") ||
            strstr(contentT, "json"))) {
            isPrintable = 1;
            buf[6+contTLen+len] = 0;
            printf("Content: \n%s\n\r", buf+6+contTLen);
        }
    }
    break;
    case SNIC_HTTP_RSP_IND: {
                int8u seq = buf[1];
                unsigned short moreData;
                unsigned short len = *((short*)&buf[2]);
                len = swap16(len);
                moreData = len & 0x8000;
                len &= 0x7fff; 
                printf("\nHTTP RSP indication, seq#: %d, content length: %d, More data: %s\n\r", seq, len, moreData?"yes":"no");
                if (isPrintable) { 
                    buf[4+len] = 0;
                    printf("Content: \n%s\n\r", buf+4);
                }
	}
	break;
    default:
        break;
    }
    printf(".\n\r");
}void SendSNIC(unsigned char *buf, int size)