Esempio n. 1
0
char putStream(char* streamName, char* json)
{

    int json_length;
    char http_cmd[512];
    char buffer[1024];
    int ret;

    sock.connect(APIHOST, 80);

    json_length = strlen(json);

    sprintf(http_cmd, "PUT /v1/feeds/%s/streams/%s HTTP/1.0\r\nX-M2X-KEY: %s\r\nContent-length: %d\r\nContent-type: application/json\r\n\r\n%s\r\n", FEEDID, streamName, APIKEY, json_length, json);
    //pc.printf(http_cmd);

    sock.send_all(http_cmd, sizeof(http_cmd)-1);

    ret = sock.receive(buffer, sizeof(buffer)-1);
    if (ret <= 0)
        return(0);
    buffer[ret] = '\0';
    if (strncmp(buffer, "HTTP/", 5) == 0) {
        ret = atoi(&buffer[9]);
    }
    //pc.printf("Received %d chars from server:\r\n%s\r\n", ret, buffer);

    strcpy(json, buffer);
    sock.close();

    return ret;
}
Esempio n. 2
0
char getLocation(char* json)
{
    char http_cmd[512];
    char buffer[1024];
    int ret;

    sock.connect(APIHOST, APIPORT);

    sprintf(http_cmd, "GET /v1/feeds/%s/location HTTP/1.0\r\nX-M2X-KEY: %s\r\n\r\n", FEEDID, APIKEY);
    //pc.printf(http_cmd);

    sock.send_all(http_cmd, sizeof(http_cmd)-1);

    ret = sock.receive(buffer, sizeof(buffer)-1);
    if (ret <= 0)
        return(0);
    buffer[ret] = '\0';
    if (strncmp(buffer, "HTTP/", 5) == 0) {
        ret = atoi(&buffer[9]);
    }

    //pc.printf("Received %d chars from server:\r\n%s\r\n", ret, buffer);

    strcpy(json, buffer);
    sock.close();

    return ret;

}
void
tcpClient_main(intptr_t exinf) {

	EthernetInterface network;
	TCPSocketConnection socket;

	/* syslogの設定 */
    SVC_PERROR(syslog_msk_log(LOG_UPTO(LOG_INFO), LOG_UPTO(LOG_EMERG)));

	syslog(LOG_NOTICE, "tcpClient:");
    syslog(LOG_NOTICE, "Sample program starts (exinf = %d).", (int_t) exinf);
    syslog(LOG_NOTICE, "LOG_NOTICE: Network Setting up...");

#if (USE_DHCP == 1)
    if (network.init() != 0) {                             //for DHCP Server
#else
    if (network.init(IP_ADDRESS, SUBNET_MASK, DEFAULT_GATEWAY) != 0) { //for Static IP Address (IPAddress, NetMasks, Gateway)
#endif
		syslog(LOG_NOTICE, "Network Initialize Error");
        return;
    }
    syslog(LOG_NOTICE, "Network was initialized successfully");
    while (network.connect(5000) != 0) {
        syslog(LOG_NOTICE, "LOG_NOTICE: Network Connect Error");
    }

    syslog(LOG_NOTICE, "MAC Address is %s", network.getMACAddress());
    syslog(LOG_NOTICE, "IP Address is %s", network.getIPAddress());
    syslog(LOG_NOTICE, "NetMask is %s", network.getNetworkMask());
    syslog(LOG_NOTICE, "Gateway Address is %s", network.getGateway());
    syslog(LOG_NOTICE, "Network Setup OK...");
	
    while (socket.connect(SERVER, HTTP_PORT) < 0) {
        syslog(LOG_EMERG, "Unable to connect to (%s) on port (%d)", SERVER, HTTP_PORT);
        wait(1.0);
    }
    ClientGreet(&socket);
    socket.close();
    syslog(LOG_NOTICE, "program end");
}

// set mac address
void mbed_mac_address(char *mac) {
	// PEACH1
    mac[0] = 0x00;
    mac[1] = 0x02;
    mac[2] = 0xF7;
    mac[3] = 0xF0;
    mac[4] = 0x00;
    mac[5] = 0x00;
}
int TCPSocketServer::accept(TCPSocketConnection& connection) {
    if (_sock_fd < 0) {
        return -1;
    }

    if (!_blocking) {
        TimeInterval timeout(_timeout);
        if (wait_readable(timeout) != 0) {
            return -1;
        }
    }

    connection.reset_address();
    socklen_t newSockRemoteHostLen = sizeof(connection._remote_host);
    int fd = _cc3000_module->_socket.accept(_sock_fd, (sockaddr *) &connection._remote_host, &newSockRemoteHostLen);
    if (fd < 0) {
        return -1;
    }
    /* s_addr is returned in the little endian */
    connection._remote_host.sin_addr.s_addr = htonl(connection._remote_host.sin_addr.s_addr);
    connection._sock_fd = fd;
    connection._is_connected = true;

    return 0;
}
Esempio n. 5
0
int main() {
    char buffer[BUFFER_SIZE] = {0};
    char out_buffer[BUFFER_SIZE] = {0};
    s_ip_address ip_addr = {0, 0, 0, 0};
    int port = 0;

    printf("MBED: TCPCllient waiting for server IP and port...\r\n");
    scanf("%d.%d.%d.%d:%d", &ip_addr.ip_1, &ip_addr.ip_2, &ip_addr.ip_3, &ip_addr.ip_4, &port);
    printf("MBED: Address received: %d.%d.%d.%d:%d\r\n", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4, port);

    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();

    printf("MBED: TCPClient IP Address is %s\r\n", eth.getIPAddress());
    sprintf(buffer, "%d.%d.%d.%d", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4);

    TCPSocketConnection socket;
    while (socket.connect(buffer, port) < 0) {
        printf("MBED: TCPCllient unable to connect to %s:%d\r\n", buffer, port);
        wait(1);
    }

    // Test loop for multiple client connections
    bool result = true;
    int count_error = 0;
    for (int i = 0; i < MAX_ECHO_LOOPS; i++) {
        std::generate(out_buffer, out_buffer + BUFFER_SIZE, char_rand);
        socket.send_all(out_buffer, BUFFER_SIZE);

        int n = socket.receive(buffer, BUFFER_SIZE);
        if (n > 0)
        {
            bool echoed = memcmp(out_buffer, buffer, BUFFER_SIZE) == 0;
            result = result && echoed;
            if (echoed == false) {
                count_error++;  // Count error messages
            }
        }
    }

    printf("MBED: Loop messages passed: %d / %d\r\n", MAX_ECHO_LOOPS - count_error, MAX_ECHO_LOOPS);

    if (notify_completion_str(result, buffer)) {
        socket.send_all(buffer, strlen(buffer));
    }
    socket.close();
    eth.disconnect();
    notify_completion(result);
    return 0;
}
Esempio n. 6
0
int TCPSocketServer::accept(TCPSocketConnection& connection)
{
    if (_sock_fd < 0) {
        return -1;
    }
    Timer t;
    t.reset();
    t.start();
    while(1) {
        if (t.read_ms() > _timeout && _blocking == false) {
            return -1;
        }
        if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == SOCK_ESTABLISHED) {
            break;
        }
    }
    uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR);
    char host[16];
    snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
    uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT);
    // change this server socket to connection socket.
    connection._sock_fd = _sock_fd;
    connection._is_connected = true;
    connection.set_address(host, port);

    // and then, for the next connection, server socket should be assigned new one.
    _sock_fd = -1; // want to assign new available _sock_fd.
    if(bind(listen_port) < 0) {
        // modified by Patrick Pollet
        error("No more socket for listening, bind error");
        return -1;
    } else {
        //return -1;
        if(listen(1) < 0) {
            // modified by Patrick Pollet
            error("No more socket for listening, listen error");
            return -1;
        }
    }

    return 0;
}
Esempio n. 7
0
int main(void)
{
    EthernetInterface eth;

    eth.init(); //Use DHCP
    eth.connect();
    printf("Server IP Address is %s:%d\n", eth.getIPAddress(), ECHO_SERVER_PORT);

    Thread UdpServerTask(udp_server_task, NULL, osPriorityNormal, DEFAULT_STACK_SIZE * 2.25);
    Thread UdpClientTask(udp_client_task, NULL, osPriorityNormal, DEFAULT_STACK_SIZE * 2.25);

    // Control TCP server to get mbed statistics
    {
        const int TELNET_SERVER_PORT = 23;
        const int BUFFER_SIZE = 256;
        TCPSocketServer server;
        server.bind(TELNET_SERVER_PORT);
        server.listen();

        while (true) {
            printf("Wait for new connection...\n");
            TCPSocketConnection client;
            server.accept(client);
            client.set_blocking(false, 1500); // Timeout after (1.5)s
            printf("Connection from: %s\n", client.get_address());

            char buffer[BUFFER_SIZE] = { 0 };
            while (true) {
                int n = client.receive(buffer, sizeof(buffer));
                //if (n <= 0) break;
                if (n > 0) {
                    printf("Recv %d chars\r\n", n);
                    const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE - 1 : n;
                    buffer[buffer_string_end_index] = '\0';
                    // client.send_all(buffer, strlen(buffer));
                    if (strncmp(buffer, "stat", 4) == 0) {
                        sprintf(buffer, "received_packets %d\nforwarded_packets %d\nmax_queue_len %d",
                                received_packets, forwarded_packets, max_queue_len);
                        client.send_all(buffer, strlen(buffer));
                        // printf("%s", buffer);
                    }
                }
                //if (n <= 0) break;
            }
            client.close();
        }
    }
}