Beispiel #1
0
/**
  Performs an HTTP POST operation to the path at the address / port specified.
  A buffer with the data to be POSTed is passed in, and up to maxresponse bytes of the response
  from the server are written back into the same buffer.
  @param hostname The name of the host to connect to.
  @param path The path on the server to post to.
  @param port The port to connect on - standard http port is 80
  @param data The buffer to write from, and then read the response back into
  @param data_length The number of bytes to write from \b data
  @param maxresponse How many bytes of the response to read back into \b data
  @param headers (optional) An array of strings to be sent as headers - last element in the array must be 0.
  @return The number of bytes written, or -1 on failure.

  \b Example
  \code
  // we'll post a test message to www.makingthings.com/post/path
  int bufLength = 100;
  char myBuffer[bufLength];
  int datalength = siprintf(myBuffer, "A test message to post"); // load the buffer with some data to send
  webclientPost("www.makingthings.com", "/post/path", 80, myBuffer, datalength, bufLength, 0);
  \endcode
*/
int webclientPost(const char* hostname, const char* path, int port, char* data, int data_length, int maxresponse, const char* headers[])
{
    int s = tcpOpen(networkGetHostByName(hostname), port);
    if (s > -1) {
        int len = sniprintf(webclientBuf, WEBCLIENT_BUFFER_SIZE,
                            "POST %s HTTP/1.1\r\nContent-Length: %d\r\n%s%s%s",
                            path, data_length,
                            (hostname != NULL) ? "Host: " : "",
                            (hostname != NULL) ? hostname : "",
                            (hostname != NULL) ? "\r\n" : "");
        tcpWrite(s, webclientBuf, len);

        if (headers != NULL) {
            for ( ; *headers != 0; headers++) {
                tcpWrite(s, *headers, strlen(*headers));
                tcpWrite(s, "\r\n", 2);
            }
        }
        tcpWrite(s, "\r\n", 2); // all done with headers

        // send the body...just check our last write here...
        if (tcpWrite(s, data, data_length) <= 0) {
            tcpClose(s);
            return -1;
        }

        // read back the response
        len = webclientReadResponse(s, data, maxresponse);
        tcpClose(s);
        return len;
    }
    return -1;
}
Beispiel #2
0
/**
  Performs an HTTP GET operation to the path at the address / port specified.

  The data returned from the web server (up to maxresponse bytes) is written into the given buffer.

  @param hostname The name of the host to connect to.
  @param path The path on the server to connect to.
  @param port The port to connect on - standard http port is 80
  @param response The buffer read the response back into.
  @param maxresponse An integer specifying the size of the response buffer.
  @param headers (optional) An array of strings to be sent as headers - last element in the array must be 0.
  @return the number of bytes received, or < 0 on error.

  \b Example
  \code
  #define BUF_LENGTH 100
  char myBuffer[BUF_LENGTH];
  int justGot = webclientGet("www.makingthings.com", "/test/path", 80, myBuffer, BUF_LENGTH, 0);
  \endcode
  Now we should have the results of the HTTP GET from \b www.makingthings.com/test/path in \b myBuffer.
*/
int webclientGet(const char* hostname, const char* path, int port, char* response, int maxresponse, const char* headers[])
{
    int s = tcpOpen(networkGetHostByName(hostname), port);
    if (s > -1) {
        // construct the GET request
        int len = sniprintf(webclientBuf, WEBCLIENT_BUFFER_SIZE, "GET %s HTTP/1.1\r\n%s%s%s",
                            path,
                            (hostname != NULL) ? "Host: " : "",
                            (hostname != NULL) ? hostname : "",
                            (hostname != NULL) ? "\r\n" : ""  );
        tcpWrite(s, webclientBuf, len);

        if (headers != NULL) {
            for ( ; *headers != 0; headers++) {
                tcpWrite(s, *headers, strlen(*headers));
                tcpWrite(s, "\r\n", 2);
            }
        }

        if (tcpWrite(s, "\r\n", 2 ) < 0) { // all done with headers...just check our last write here...
            tcpClose(s);
            return -1;
        }

        // read the data into the given buffer until there's none left, or the passed in buffer is full
        len = webclientReadResponse(s, response, maxresponse);
        tcpClose(s);
        return len;
    }
    return -1;
}
Beispiel #3
0
int process_clsr(){
	int ret;
	int free_no;
	char buff[1024];
	//T_MESSAGE_QUEUE_FMT mbuff;
	T_CLIENT_REQUEST req;

	//受信処理
	if ( tcpSelect(l_server_fd,0,G_ConfTbl->select_timeout) != 1 ){ return 0; }
	l_client_fd=tcpAccept(l_server_fd);
	logPrint(DEBUG_DEBUG,"tcpAccept(%d) => %d",l_server_fd,l_client_fd);
	if ( l_client_fd == 0 ){ return 0; }

	//クライアント要求をタスクに割り振る
	free_no=cacheGetFreeTaskNo();
	if ( free_no < 0 ){
		logPrint(DEBUG_WARNING,"task is busy (no slot)");
		tcpClose(l_client_fd);
		return 0;
	}
	sprintf(l_sock_file,"%s/ud%03d.sock",G_TempDir,free_no);
	l_gate_fd=udConnect(l_sock_file);
	ret=udSend(l_gate_fd, l_client_fd, &req, sizeof(req),G_ConfTbl->send_timeout);
	logPrint(DEBUG_DEBUG,"udSend(%d,%d,*,%d,%d) => %d",
				l_gate_fd, l_client_fd,sizeof(req),G_ConfTbl->send_timeout,ret);
	tcpClose(l_client_fd);
	return 0;
}
Beispiel #4
0
boolean GPRSbee::httpPostTextFile(char *serverName, char *serverPort, char *serverURL, char *fileContent, byte maxNumConnectAttempts) {

  // returns true if the status line of the response contains the http code : 200 

  boolean requestSuccess = false; 
  
  long fileContentLength = 0;
  while(fileContent[fileContentLength] != '\0') fileContentLength++;
  

  boolean tcpConnectSuccess = tcpConnect(serverName, serverPort, maxNumConnectAttempts);
  
  if(tcpConnectSuccess) {
  
  
    char httpResponseStatusLineBuffer[60];
    
    char formFieldName[] = HTTP_POST_FILE_DEFAULT_FORM_FIELD_NAME;
    
     
    requestAT(F("AT+CIPSEND"), 2, AT_CIPSEND_RESP_TIMOUT_IN_MS);
    
    echoHttpRequestInitHeaders(serverName, serverURL, "POST");
    
    echoHttpPostFileRequestAdditionalHeadersPart1(fileContentLength, formFieldName);
    
    serialConnection.print((char) 26);
    
    
    delay(300);
    
      
    requestAT(F("AT+CIPSEND"), 2, AT_CIPSEND_RESP_TIMOUT_IN_MS);
    
    serialConnection.print(fileContent);
    
    serialConnection.print((char) 26);
    
    
    delay(300);
    
    
    requestAT(F("AT+CIPSEND"), 2, AT_CIPSEND_RESP_TIMOUT_IN_MS);
    
    echoHttpPostFileRequestAdditionalHeadersPart2();  
   
    serialConnection.print((char) 26);
    

    retrieveHttpResponseStatusLine(httpResponseStatusLineBuffer, sizeof(httpResponseStatusLineBuffer), HTTP_RESP_TIMOUT_IN_MS);
  
    if(strstr(httpResponseStatusLineBuffer, "200") != NULL) requestSuccess = true; 
  
    tcpClose();

  }
  
  return requestSuccess;

}
Beispiel #5
0
boolean GPRSbee::httpGet(char *serverName, char *serverPort, char *serverURL, byte maxNumConnectAttempts) {

  // returns true if the status line of the response contains the http code : 200 

  boolean requestSuccess = false; 

  boolean tcpConnectSuccess = tcpConnect(serverName, serverPort, maxNumConnectAttempts);
  
  if(tcpConnectSuccess) {
  
    char httpResponseStatusLineBuffer[60];
    
    requestAT(F("AT+CIPSEND"), 2, AT_CIPSEND_RESP_TIMOUT_IN_MS);

    echoHttpRequestInitHeaders(serverName, serverURL, "GET");
  
    serialConnection.print('\n');
  
    serialConnection.print((char) 26);
    
    retrieveHttpResponseStatusLine(httpResponseStatusLineBuffer, sizeof(httpResponseStatusLineBuffer), HTTP_RESP_TIMOUT_IN_MS);
  
    if(strstr(httpResponseStatusLineBuffer, "200") != NULL) requestSuccess = true; 
  
    tcpClose();
  
  }
  
  return requestSuccess;

}
Beispiel #6
0
boolean GPRSbee::httpPostEncodedData(char *serverName, char *serverPort, char *serverURL, char *encodedData, byte maxNumConnectAttempts) {
  
  // encodedData example : "A=1&B=2&C=3" (URL encoded data)
  // returns true if the status line of the response contains the http code : 200 
  
  boolean requestSuccess = false; 
  
  long encodedDataLength = 0;
  while(encodedData[encodedDataLength] != '\0') encodedDataLength++;

  boolean tcpConnectSuccess = tcpConnect(serverName, serverPort, maxNumConnectAttempts);
  
  if(tcpConnectSuccess) {
  
    char httpResponseStatusLineBuffer[60];

    requestAT(F("AT+CIPSEND"), 2, AT_CIPSEND_RESP_TIMOUT_IN_MS);

    echoHttpRequestInitHeaders(serverName, serverURL, "POST");
    
    echoHttpPostURLEncodedRequestAdditionalHeaders(encodedDataLength);
    
    serialConnection.print(encodedData);
    
    serialConnection.print((char) 26);
    
    retrieveHttpResponseStatusLine(httpResponseStatusLineBuffer, sizeof(httpResponseStatusLineBuffer), HTTP_RESP_TIMOUT_IN_MS);
  
    if(strstr(httpResponseStatusLineBuffer, "200") != NULL) requestSuccess = true; 
  
    tcpClose();
    

  }
  
  return requestSuccess;
  
}
Beispiel #7
0
void WSocket::udpClose(SOCKET s)
{
  tcpClose(s);
}
Beispiel #8
0
int process_clsr_fin(){
	int ret;
	ret=tcpClose(l_server_fd);
	logPrint(DEBUG_DEBUG,"tcpClose(%d) => %d",l_server_fd, ret);
	return 0;
}
Beispiel #9
0
void EtherShield::ES_tcpClose( uint8_t *buf,uint16_t plen ) {
	tcpClose(buf, plen );
}
Beispiel #10
0
//............................................................................
int main(int argc, char *argv[]) {
    int optChar;
    char comPort[FNAME_SIZE];
    char inpFileName[FNAME_SIZE];
    char outFileName[FNAME_SIZE];
    char savFileName[FNAME_SIZE];
    char matFileName[FNAME_SIZE];
    TargetLink link = NO_LINK;
                                              // default configuration options
    strcpy(inpFileName, "qs.bin");
    strcpy(comPort, "COM1");
    int tcpPort          = 6601;
    int baudRate         = 38400;
    uint8_t quiet        = 0;
    uint8_t tstampSize   = 4;
    uint8_t objPtrSize   = 4;
    uint8_t funPtrSize   = 4;
    uint8_t sigSize      = 1;
    uint8_t evtSize      = 2;
    uint8_t queueCtrSize = 1;
    uint8_t poolCtrSize  = 2;
    uint8_t poolBlkSize  = 2;
    uint8_t tevtCtrSize  = 2;
    outFileName[0]       = '\0';                  // Output file not specified
    savFileName[0]       = '\0';                    // Save file not specified
    matFileName[0]       = '\0';                  // Matlab file not specified

    time_t now = time(NULL);
    printf("QSPY host application %s\n"
           "Copyright (c) Quantum Leaps, LLC.\n"
           "%s\n", QSPY_VER, ctime(&now));

    static char const help[] =
        "Syntax is: qspy [options]          * = default\n\n"
        "OPTION                    DEFAULT  COMMENT\n"
        "----------------------------------------------------------------\n"
        "-h                                 help (this message)\n"
        "-q                                 quiet mode (no stdout output)\n"
        "-o<File_name>                      produce output to a file\n"
        "-s<File_name>                      save the binary data to a file\n"
        "-m<File_name>                      produce a Matlab file\n"
        "-c<COM_port>  *           COM1     com port input\n"
        "-b<Baud_rate>             38400    baud rate selection\n"
        "-f<File_name>             qs.bin   file input\n"
        "-t                                 TCP/IP input\n"
        "-p<TCP_Port_number>       6601     TCP/IP server port\n"
        "-T<tstamp_size>           4        QS timestamp size (bytes)\n"
        "-O<pointer_size>          4        object pointer size (bytes)\n"
        "-F<pointer_size>          4        function pointer size (bytes)\n"
        "-S<signal_size>           1        signal size (bytes)\n"
        "-E<event_size>            2        event size size (bytes)\n"
        "-Q<queue_counter_size>    1        queue counter size (bytes)\n"
        "-P<pool_counter_size>     2        pool counter size (bytes)\n"
        "-B<pool_blocksize_size>   2        pool blocksize size (bytes)\n"
        "-C<QTimeEvt_counter_size> 2        QTimeEvt counter size\n";

    while ((optChar = getopt(argc, argv,
                             "hqo:s:m:c:b:tp:f:T:O:F:S:E:Q:P:B:C:")) != -1)
    {
        switch (optChar) {
            case 'q': {                                          // quiet mode
                quiet = 1;
                break;
            }
            case 'o': {                                         // file output
                strncpy(outFileName, optarg, sizeof(outFileName));
                printf("-o %s\n", outFileName);
                break;
            }
            case 's': {                          // save binary data to a file
                strncpy(savFileName, optarg, sizeof(savFileName));
                printf("-s %s\n", savFileName);
                break;
            }
            case 'm': {                                  // Matlab file output
                strncpy(matFileName, optarg, sizeof(matFileName));
                printf("-m %s\n", matFileName);
                break;
            }
            case 'c': {                                            // COM port
                if ((link != NO_LINK) && (link != SERIAL_LINK)) {
                    printf("The -c option is incompatible with -p/-f\n");
                    return -1;                                      // failure
                }
                strncpy(comPort, optarg, sizeof(comPort));
                printf("-c %s\n", comPort);
                link = SERIAL_LINK;
                break;
            }
            case 'b': {                                           // baud rate
                if ((link != NO_LINK) && (link != SERIAL_LINK)) {
                    printf("The -b option is incompatible with -p/-f\n");
                    return -1;                                      // failure
                }
                if (sscanf(optarg, "%d", &baudRate) != 1) {
                    printf("incorrect baud rate: %s\n", optarg);
                    return -1;                                      // failure
                }
                printf("-b %d\n", baudRate);
                link = SERIAL_LINK;
                break;
            }
            case 'f': {                                          // File input
                if (link != NO_LINK) {
                    printf("The -f option is incompatible with -c/-b/-p\n");
                    return -1;                                      // failure
                }
                strncpy(inpFileName, optarg, sizeof(inpFileName));
                printf("-f %s\n", inpFileName);
                link = FILE_LINK;
                break;
            }
            case 't': {                                        // TCP/IP input
                if ((link != NO_LINK) && (link != TCP_LINK)) {
                    printf("The -t option is incompatible with -c/-b/-f\n");
                    return -1;
                }
                printf("-t\n");
                link = TCP_LINK;
                break;
            }
            case 'p': {                                         // TCP/IP port
                if ((link != NO_LINK) && (link != TCP_LINK)) {
                    printf("The -p option is incompatible with -c/-b/-f\n");
                    return -1;
                }
                tcpPort = (int)strtoul(optarg, NULL, 10);
                printf("-p %d\n", tcpPort);
                link = TCP_LINK;
                break;
            }
            case 'T': {                                      // timestamp size
                tstampSize = (uint8_t)strtoul(optarg, 0, 10);
                break;
            }
            case 'F': {                               // function pointer size
                funPtrSize = (uint8_t)strtoul(optarg, 0, 10);
                break;
            }
            case 'O': {                                 // object pointer size
                objPtrSize = (uint8_t)strtoul(optarg, 0, 10);
                break;
            }
            case 'S': {                                         // signal size
                sigSize = (uint8_t)strtoul(optarg, 0, 10);
                break;
            }
            case 'E': {                                         // event size
                evtSize = (uint8_t)strtoul(optarg, 0, 10);
                break;
            }
            case 'Q': {                                  // Queue counter size
                queueCtrSize = (uint8_t)strtoul(optarg, 0, 10);
                break;
            }
            case 'P': {                            // Memory-pool counter size
                poolCtrSize = (uint8_t)strtoul(optarg, 0, 10);
                break;
            }
            case 'B': {                          // Memory-pool blocksize size
                poolBlkSize = (uint8_t)strtoul(optarg, 0, 10);
                break;
            }
            case 'C': {                             // Time event counter size
                tevtCtrSize = (uint8_t)strtoul(optarg, 0, 10);
                break;
            }
            case 'h':                                                  // help
            default: {                                       // unknown option
                printf(help);
                return -1;                                     // error return
            }
        }
    }
    if (argc != optind) {
        printf(help);
        return -1;
    }

    // configure the Quantum Spy
    FILE *outFile = (outFileName[0] != '\0'
                     ? fopen(outFileName, "w")
                     : (FILE *)0);
    FILE *matFile = (matFileName[0] != '\0'
                     ? fopen(matFileName, "w")
                     : (FILE*)0);
    FILE *savFile = (savFileName[0] != '\0'
                     ? fopen(savFileName, "wb")     // open for writing binary
                     : (FILE*)0);
    qsConfig(quiet,
             objPtrSize,
             funPtrSize,
             tstampSize,
             sigSize,
             evtSize,
             queueCtrSize,
             poolCtrSize,
             poolBlkSize,
             tevtCtrSize,
             outFile,
             matFile);

    static unsigned char buf[BUF_SIZE];
    int n;

    switch (link) {
        case NO_LINK:                            // intentionally fall through
        case SERIAL_LINK: {           // input trace data from the serial port
            if (!comOpen(comPort, baudRate)) {
                return -1;
            }
            else {
                printf("\nSerial port %s opened, hit any key to quit...\n\n",
                       comPort);
            }
            while ((n = comRead(buf, sizeof(buf))) != -1) {
                if (n > 0) {
                    qsParse(buf, n);
                    if (savFile != (FILE *)0) {
                        fwrite(buf, 1, n, savFile);
                    }
                }
            }
            comClose();
            break;
        }
        case FILE_LINK: {                      // input trace data from a file
            FILE *f = fopen(inpFileName, "rb");     // open for reading binary
            if (f == (FILE *)0) {
                printf("file %s not found\n", inpFileName);
                return -1;
            }
            do {
                n = fread(buf, 1, sizeof(buf), f);
                qsParse(buf, n);
            } while (n == sizeof(buf));

            fclose(f);
            break;
        }
        case TCP_LINK: {                 // input trace data from the TCP port
            if (!tcpOpen(tcpPort)) {
                return -1;
            }
            else {
                printf("\nTCP/IP port %d opened, "
                       "hit any key to quit...\n"
                       "(the target must be stopped)\n",
                       tcpPort);
            }
            while ((n = tcpRead(buf, sizeof(buf))) != -1) {
                if (n > 0) {
                    qsParse(buf, n);
                    if (savFile != (FILE *)0) {
                        fwrite(buf, 1, n, savFile);
                    }
                }
            }
            tcpClose();
            break;
        }
    }
    if (savFile != (FILE *)0) {
        fclose(savFile);
    }
    if (matFile != (FILE *)0) {
        fclose(matFile);
    }

    printf("\nDone.\n");
    return 0;                                                       // success
}