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; }
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 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; }