int parse_uint16(uint16_t *x, char *payload) { int errcode; if (bug_on(!x)) return -err_internal; payload = strtok(payload, " ,"); if (!payload) return -err_parse_no_args; errcode = str_to_uint16(payload, x, 0); if (errcode < 0) return errcode; return 0; }
static void uart1_rx_irq(enum sys_message msg) { uint16_t u16; uint8_t p; uint8_t pga, ch, left = 0, right = 0; uint8_t *flash_addr = FLASH_ADDR; char *input; uint8_t *ptr; uint8_t allfine = 0; input = (char *)uart1_rx_buf; p = input[0]; if (p > 97) { p -= 32; } if ((p == 63) || (p == M_CMD_HELP)) { // [h?] display_help(); } else if (p == M_CMD_SHOW) { // [s]how settings show_settings(); uart1_tx_str("ok\r\n", 4); } else if (p == M_CMD_WRITE) { // [w]rite to flash if (str_to_uint16(input, &u16, 1, 1, 1, 3)) { if (u16 == 1) { flash_addr = SEGMENT_B; } else if (u16 == 2) { flash_addr = SEGMENT_C; } else if (u16 == 3) { flash_addr = SEGMENT_D; } flash_save(flash_addr, (void *)&s, sizeof(s)); uart1_tx_str("ok\r\n", 4); } } else if (p == M_CMD_READ) { // [r]ead from flash if (str_to_uint16(input, &u16, 1, 1, 1, 3)) { if (u16 == 1) { flash_addr = SEGMENT_B; } else if (u16 == 2) { flash_addr = SEGMENT_C; } else if (u16 == 3) { flash_addr = SEGMENT_D; } settings_init(flash_addr); uart1_tx_str("ok\r\n", 4); } } else if (p == M_CMD_MUTE) { // [m]ute pga if (str_to_uint16(input, &u16, 1, 1, 1, 6)) { pga = u16; pga_set_mute_st(pga, MUTE, 1); uart1_tx_str("ok\r\n", 4); } } else if (p == M_CMD_UNMUTE) { // [u]nmute pga if (str_to_uint16(input, &u16, 1, 1, 1, 6)) { pga = u16; pga_set_mute_st(pga, UNMUTE, 1); uart1_tx_str("ok\r\n", 4); } } else if (p == M_CMD_VOL) { // [v]olume set if (str_to_uint16(input, &u16, 1, 1, 1, 6)) { pga = u16; ch = input[2]; if (str_to_uint16(input, &u16, 3, strlen(input) - 3, 0, 255)) { ptr = (uint8_t *) & s.v1_r; if (ch == 98) { // 'b' - change both channels right = u16; left = u16; allfine = 1; } else if (ch == 114) { // 'r' - only change the right channel right = u16; left = *(ptr + (pga * 2 - 2) + 1); allfine = 1; } else if (ch == 108) { // 'l' - only change the left channel right = *(ptr + (pga * 2 - 2)); left = u16; allfine = 1; } if (allfine) { pga_set_volume(pga, right, left, 1, 1); uart1_tx_str("ok\r\n", 4); } } } } //snprintf(str_temp, TEMP_LEN, "D 0x%x 0x%x 0x%x\r\n", input[0], input[1], input[2]); //uart1_tx_str(str_temp, strlen(str_temp)); uart1_p = 0; uart1_rx_enable = 1; }
int main(int argc, const char* argv[]) { if (argc != 2) { printf("<port>\n"); exit(EXIT_FAILURE); } uint16_t PORT; if (!str_to_uint16(argv[1], &PORT)) { printf("invalid <port>\n"); exit(EXIT_FAILURE); } char* users[MAX_USERS]; // set up the user name trie ascii_trie* user_names = ascii_trie_init(); for (int i = 0; i < MAX_USERS; i++) { users[i] = NULL; } char buffer[BUFFER_SIZE]; int sockfd = socket(PF_INET, SOCK_STREAM, STANDARD_PROTOCOL); // socket() creates an endpoint for communication and returns a descriptor. struct sockaddr_in serv_addr; memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(PORT); // INADDR_ANY allows program to work without knowing the IP address of the machine it was running on, // or, in the case of a machine with multiple network interfaces, it allows the server // to receive packets destined to any of the interfaces. bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); // bind() assigns a name to an unnamed socket. When a socket is created with // socket(2) it exists in a name space (address family) but has no name // assigned. bind() requests that address be assigned to the socket. listen(sockfd, BACKLOG); // Creation of socket-based connections requires several operations. First, a // socket is created with socket(2). Next, a willingness to accept incoming // connections and a queue limit for incoming connections are specified with // listen(). Finally, the connections are accepted with accept(2). The // listen() call applies only to sockets of type SOCK_STREAM. // // The backlog parameter defines the maximum length for the queue of pending // connections. If a connection request arrives with the queue full, the // client may receive an error with an indication of ECONNREFUSED. Alterna- // tively, if the underlying protocol supports retransmission, the request may // be ignored so that retries may succeed. fd_set active_fd_set, read_fd_set; struct sockaddr_in clientname; FD_ZERO (&active_fd_set); FD_SET (sockfd, &active_fd_set); while (1) { memset(buffer, 0, BUFFER_SIZE); read_fd_set = active_fd_set; if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0) { perror ("select"); exit (EXIT_FAILURE); } for (int fd = 0; fd < FD_SETSIZE; ++fd) { if (FD_ISSET(fd, &read_fd_set)) { if (fd == sockfd) { // connection on server socket // we want to get the address of the client connecting // and add its fd to the set of connections printf("new client connectint...\n"); socklen_t size = sizeof (clientname); int new_connfd = accept(sockfd, (struct sockaddr*) &clientname, &size); // The argument socket is a socket that has been created with socket(2), bound // to an address with bind(2), and is listening for connections after a // listen(2). accept() extracts the first connection request on the queue of // pending connections, creates a new socket with the same properties of // socket, and allocates a new file descriptor for the socket. If no pending // connections are present on the queue, and the socket is not marked as non- // blocking, accept() blocks the caller until a connection is present. If the // socket is marked non-blocking and no pending connections are present on the // queue, accept() returns an error as described below. The accepted socket // may not be used to accept more connections. The original socket socket, // remains open. if (new_connfd < 0) { perror ("accept"); exit (EXIT_FAILURE); } assert(ntohs(clientname.sin_port) > 0); fprintf (stderr,"Server: connect from host %s, port %d.\n", inet_ntoa(clientname.sin_addr), ntohs(clientname.sin_port)); char* uname = "PLEASE PROVIDE A USER NAME...\n"; send(new_connfd, uname, strlen(uname), 0); FD_SET (new_connfd, &active_fd_set); } else { ssize_t nbytes; // return length if ((nbytes = read_from_connected_client(fd, buffer)) < 1) { memset(buffer, 0, BUFFER_SIZE); printf("user: %s left\n", users[fd]); ascii_trie_delete(users[fd], user_names); free(users[fd]); users[fd] = NULL; close(fd); FD_CLR(fd, &active_fd_set); } else { if (users[fd] == NULL) { // new user is not registered yet.. we assume that the buffer // contains the user name to use followed by a '\n' char name[nbytes]; memset(name, 0, nbytes); strncpy(name, buffer, nbytes-1); if (!is_valid_username(name)) { char* badname = "INVALID USERNAME...\n"; send(fd, badname, strlen(badname), 0); } else { int insert = ascii_trie_insert(name, fd, user_names); if (insert == 0) { char* exists = "USER NAME TAKEN...\n"; send(fd, exists, strlen(exists), 0); } else if (insert == 1) { users[fd] = malloc(sizeof(char)*nbytes); memset(users[fd], 0, nbytes); strncpy(users[fd], buffer, nbytes-1); } else { perror("trie insert"); } } } else { printf("user: %s sent a message\n", users[fd]); if (buffer[0] == '@') { char toname[10]; int i = 0; while ((i < 10) && (buffer[i+1] != 0x00)) { if (!isspace(buffer[i+1])) { toname[i] = buffer[i+1]; i++; } else { break; } } toname[i] = 0; int to_fd; ascii_trie* node = ascii_trie_lookup(toname, &to_fd, user_names); if (node != NULL) { send(to_fd, buffer, BUFFER_SIZE, 0); } } } memset(buffer, 0, BUFFER_SIZE); } } } } } return 0; }
int main(int argc, char * argv[]) { int argv_offset = 1; bool dumpraw = false; bool prontohex = false; // Check the invocation/calling usage. if (argc < 2 || argc > 4) { usage_error(argv[0]); return 1; } if (strncmp("-prontohex", argv[argv_offset], 10) == 0) { prontohex = true; argv_offset++; } if (strncmp("-raw", argv[argv_offset], 4) == 0) { dumpraw = true; argv_offset++; } if (argc - argv_offset != 1) { usage_error(argv[0]); return 1; } uint16_t gc_test[MAX_GC_CODE_LENGTH]; int index = 0; char *pch; char *saveptr1; char *sep = const_cast<char *>(","); int codebase = 10; if (prontohex) { sep = const_cast<char *>(" "); codebase = 16; } pch = strtok_r(argv[argv_offset], sep, &saveptr1); while (pch != NULL && index < MAX_GC_CODE_LENGTH) { str_to_uint16(pch, &gc_test[index], codebase); pch = strtok_r(NULL, sep, &saveptr1); index++; } IRsendTest irsend(4); IRrecv irrecv(4); irsend.begin(); irsend.reset(); if (prontohex) { irsend.sendPronto(gc_test, index); } else { irsend.sendGC(gc_test, index); } irsend.makeDecodeResult(); irrecv.decode(&irsend.capture); std::cout << "Code length " << index << std::endl << "Code type " << irsend.capture.decode_type << " (" << typeToString(irsend.capture.decode_type) << ")" << std::endl << "Code bits " << irsend.capture.bits << std::endl; if (hasACState(irsend.capture.decode_type)) { std::cout << "State value 0x"; for (uint16_t i = 0; i < irsend.capture.bits / 8; i++) printf("%02X", irsend.capture.state[i]); std::cout << std::endl; } else { std::cout << "Code value 0x" << std::hex << irsend.capture.value << std::endl << "Code address 0x" << std::hex << irsend.capture.address << std::endl << "Code command 0x" << std::hex << irsend.capture.command << std::endl; } if (dumpraw || irsend.capture.decode_type == UNKNOWN) irsend.dumpRawResult(); return 0; }