void handle_error(request_t *q, char *name, int errno) { char buf[MAXLINE], body[MAXBUF],errnum[MAXLINE],msg[MAXLINE],title[MAXLINE]; sprintf(body, "<html><title>XMJ error</title>\r\n"); switch(errno) { case NO501: strcpy(errnum,"501"); strcpy(msg,"We doesnt implement the method"); strcpy(title,"Not Implemented"); break; case NO403: strcpy(errnum,"403"); strcpy(msg,"The read/run operation is denied"); strcpy(title,"Forbidden"); break; case NO404: strcpy(errnum,"404"); strcpy(msg,"Counldnt find the resource file"); strcpy(title,"Not found"); break; } sprintf(body,"%s%s: %s\r\n",body,errnum,title); sprintf(body,"%s<p>%s : %s</p>\r\n",body, msg, name); sprintf(buf, "%s %s %s\r\n",q->ver,errnum,title); io_writen(q->io, buf, strlen(buf)); sprintf(buf, "Content-type: text/html\r\n"); io_writen(q->io, buf, strlen(buf)); sprintf(buf, "Content-length: %d\r\n\r\n",(int)strlen(body)); io_writen(q->io, buf, strlen(buf)); io_writen(q->io, body, strlen(body)); }
int Camera_SendCmd(byte *cmd, int len) { int fd; int err = 0; byte cmd_stop[7] = {0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01}; pthread_mutex_lock(&rs485_mutex); av_rs485_used = 1; fd = uart_open_dev(UART_PORT_RS485); if (fd == -1) { logcat("serial port open error: %s\n", strerror(errno)); pthread_mutex_unlock(&rs485_mutex); return -1; } uart_set_speed(fd, UART_RS485_SPEDD); if(uart_set_parity(fd, 8, 1, 'N') == -1) { logcat ("Set Parity Error\n"); pthread_mutex_unlock(&rs485_mutex); return -1; } system("echo 1 >/sys/devices/platform/gpio-power.0/rs485_direction"); cmd[len - 1] = checksum((cmd + 1), 5); err = io_writen(fd, cmd, len); if (err > 0) logcat("RS485: Send Command Sucess.\n"); else { logcat("write error, ret = %d\n", err); pthread_mutex_unlock(&rs485_mutex); return -1; } usleep(ACTION_INTERVAL); /* Stop Cmd */ cmd_stop[6] = checksum((cmd_stop + 1), 5); err = io_writen(fd, cmd_stop, 7); if (err == 7) logcat("RS485: Send Stop Command Sucess.\n"); else { logcat("write error, ret = %d\n", err); pthread_mutex_unlock(&rs485_mutex); return -1; } #ifdef _DEBUG print_message(cmd, len); #endif pthread_mutex_unlock(&rs485_mutex); return 0; }
/** * Send a command to gdb. * * @param tgdb * An instance of tgdb * * @param request * The command request to issue the command for */ static void tgdb_run_request(struct tgdb *tgdb, struct tgdb_request *request) { std::string command; if (request->header == TGDB_REQUEST_CONSOLE_COMMAND || request->header == TGDB_REQUEST_COMPLETE || request->header == TGDB_REQUEST_DEBUGGER_COMMAND) { tgdb->make_console_ready_callback = true; } tgdb->is_gdb_ready_for_next_command = 0; tgdb_get_gdb_command(tgdb, request, command); /* Add a newline to the end of the command if it doesn't exist */ if (*command.rbegin() != '\n') { command.push_back('\n'); } /* Send what we're doing to log file */ char *str = sys_quote_nonprintables(command.c_str(), -1); clog_debug(CLOG_GDBIO, "%s", str); sbfree(str); /* A command for the debugger */ commands_set_current_request_type(tgdb->c, request->header); io_writen(tgdb->debugger_stdin, command.c_str(), command.size()); // Alert the GUI a command was sent tgdb->callbacks.request_sent_callback( tgdb->callbacks.context, request, command); tgdb_request_destroy(request); }
/** * Will send a command to the debugger immediatly. No queueing will be done * at this point. * * \param tgdb * The TGDB context to use. * * \param command * The command to run. * * NOTE: This function assummes valid commands are being sent to it. * Error checking should be done before inserting into queue. */ int Ctgdb::Deliver_command(struct tgdb_command *command) { IS_SUBSYSTEM_READY_FOR_NEXT_COMMAND = 0; /* Here is where the command is actually given to the debugger. * Before this is done, if the command is a GUI command, we save it, * so that later, it can be printed to the client. Its for debugging * purposes only, or for people who want to know the commands there * debugger is being given. */ if (command->command_choice == TGDB_CUSTOME_COMMAND_FRONT_END) { last_gui_command = cgdb_strdup (command->tgdb_command_data); m_target = (ITarget*) command->target; } else { m_target = NULL; } /* A command for the debugger */ if (tgdb_client_prepare_for_command (tcc, command) == -1) return -1; /* A regular command from the client */ io_debug_write_fmt ("<%s>", command->tgdb_command_data); io_writen (debugger_stdin, command->tgdb_command_data, strlen (command->tgdb_command_data)); /* Uncomment this if you wish to see all of the commands, that are * passed to GDB. */ #if 0 { char *s = strdup (client_command->tgdb_client_command_data); int length = strlen (s); s[length - 1] = '\0'; fprintf (stderr, "[%s]\n", s); s[length - 1] = ' '; free (s); s = NULL; } #endif return 0; }
void send_and_recv(int fd) { io_t io_buf; char buf[BUF_LEN]; // store input and response data io_initbuf(&io_buf, fd); printf("please input data to send:\n"); // fgets is terminated until EOF(Ctrl-D) while(fgets(buf, sizeof(buf), stdin) != NULL) { // send input data to server via fd if (io_writen(fd, buf, strlen(buf)) > 0) { // read a line from response data in io_buf io_readlineb(&io_buf, buf, BUF_LEN); fputs(buf, stdout); // printf("client receive data: %s\n", (char *)&buf); } else { perror("write fail: "); return; } } }
static int zigbee_send_cmd(int fd, byte *cmd) { int err = 0; if (cmd == NULL) return -1; cmd[6] = sum_check(cmd, 6); #ifdef _DEBUG logcat("Zigbee Send Command: "); debug_out(cmd, 7); #endif err = io_writen(fd, cmd, 7); if (err > 0) logcat("uart write %d bytes sucess.\n", err); else { logcat("write error, ret = %d\n", err); return -1; } return 0; }
io_http_res* http_get(const char* url, int timeout) { io_http_req req; io_http_res* res = malloc(sizeof(io_http_res)); parse_url(url, &req); printf("http.host = %s, http.port = %d, http.path = %s, http.query = %s\n", req.host, req.port, req.path, req.query_string); int sockfd = io_tcp_connect(req.host, req.port); if (sockfd < 0) return NULL; // request buff char buf[MAX_IO_BUFFER]; // poll struct pollfd pfds[1]; int maxpfds = 1, nready = 0, n; pfds[0].fd = sockfd; pfds[0].events = POLLWRNORM; for (;;) { bzero(buf, sizeof(buf)); nready = poll(pfds, maxpfds, timeout); if (nready < 0) { fprintf(stderr, "%s:%d http poll error\n", __FILE__, __LINE__); close(sockfd); return NULL; } else if (nready == 0) { fprintf(stderr, "%s:%d http poll timeout\n", __FILE__, __LINE__); close(sockfd); return NULL; } if (pfds[0].revents & POLLWRNORM) { // write // printf("sizeof char[] = %ld\n", sizeof(buf)); if (req.query_string[0] == '\0') snprintf(buf, sizeof(buf), "GET %s HTTP/1.1\r\n", req.path); else snprintf(buf, sizeof(buf), "GET %s?%s HTTP/1.1\r\n", req.path, req.query_string); snprintf(buf + strlen(buf), sizeof(buf), "host: %s\r\n\r\n", req.host); printf("request:\n%s\n", buf); if (io_writen(sockfd, buf, strlen(buf)) < 0) { fprintf(stderr, "%s:%d http send request error: %s\n", __FILE__, __LINE__, buf); close(sockfd); return NULL; } pfds[0].events = POLLRDNORM; } if (pfds[0].revents & POLLRDNORM) { // read if (io_readline(sockfd, buf, sizeof(buf)) < 0) { fprintf(stdout, "%s:%d http recv message error\n", __FILE__, __LINE__); close(sockfd); return NULL; } printf("response status line:\n%s\n", buf); sscanf(buf, "%s %d %s", res->version, &res->status_code, res->status_message); // io_readn(sockfd, buf, 1024); // printf("%s\n", buf); if (res->status_code == 200) { res->content_length = 0; while (buf[0] != '\r' && buf[1] != '\n') { io_readline(sockfd, buf, sizeof(buf)); fprintf(stdout, "%s\n", buf); // Content-Length if (14 == start_with(buf, "Content-Length")) { res->content_length = atoi(buf + 15); res->content = malloc(res->content_length); } } if (res->content_length <= 0) { res->content_length = 8192; res->content = malloc(res->content_length); } n = io_readn(sockfd, res->content, res->content_length); res->content[n] = '\0'; close(sockfd); return res; } break; } } close(sockfd); return NULL; }