bool wait_uplink_buffer( int port, int sec100, byte *buffer ) { int count=0; CALL_MSG("wait_uplink_buffer()", ++pclta_call_level ); // initialize timeout timer setup_timeout( sec100 ); // wait for uplink buffer, or timeout while( !(inb_p(status_reg(port)) & ULREADY) && !pclta_timeout ) { count++; } // uplink buffer ready, kill timeout timer del_timer( &pclta_timer_list ); // unsuccessful if timed out if( pclta_timeout ) { RETURN_MSG("wait_uplink_buffer() TIMEOUT", pclta_call_level-- ); return false; } // otherwise, ready to send packet to device uplink_packet( port, buffer ); DEBUG_MSG("Count = %d",count); RETURN_MSG("wait_uplink_buffer()", pclta_call_level-- ); return true; }
static void fish_applet_update_speed (FishApplet *fish, gdouble speed) { if (speed <= 0) speed = FISH_SPEED_DEFAULT; fish->speed = speed; setup_timeout (fish); }
/* Callback from the alarm timeout */ static gboolean alarm_ready_cb (gpointer data) { time_t now; if (!alarms) { g_warning ("Alarm triggered, but no alarm present\n"); return FALSE; } timeout_id = 0; now = time (NULL); g_message ("Alarm callback!"); while (alarms) { AlarmRecord *notify_id, *ar; AlarmRecord ar_copy; ar = alarms->data; if (ar->trigger > now) break; g_message ("Process alarm with trigger %lu", ar->trigger); notify_id = ar; ar_copy = *ar; ar = &ar_copy; pop_alarm (); /* This will free the original AlarmRecord; that's why we copy it */ (* ar->alarm_fn) (notify_id, ar->trigger, ar->data); if (ar->destroy_notify_fn) (* ar->destroy_notify_fn) (notify_id, ar->data); } /* We need this check because one of the alarm_fn above may have * re-entered and added an alarm of its own, so the timer will * already be set up. */ if (alarms) setup_timeout (); return FALSE; }
bool wait_reset( unsigned int port, int sec100 ) { CALL_MSG("wait_reset()", ++pclta_call_level ); setup_timeout( sec100 ); while( ( inb_p(status_reg(port)) & RESET ) && !pclta_timeout ); del_timer( &pclta_timer_list ); if( pclta_timeout ) { RETURN_MSG("wait_reset() false", pclta_call_level-- ); return false; } RETURN_MSG("wait_reset() true", pclta_call_level-- ); return true; }
static int NetWrite(void *context, const byte* buf, int buf_len, int timeout_ms) { SocketContext *sock = (SocketContext*)context; int rc; SOERROR_T so_error = 0; #ifndef WOLFMQTT_NO_TIMEOUT struct timeval tv; #endif if (context == NULL || buf == NULL || buf_len <= 0) { return MQTT_CODE_ERROR_BAD_ARG; } #ifndef WOLFMQTT_NO_TIMEOUT /* Setup timeout */ setup_timeout(&tv, timeout_ms); setsockopt(sock->fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv)); #endif rc = (int)SOCK_SEND(sock->fd, buf, buf_len, 0); if (rc == -1) { /* Get error */ socklen_t len = sizeof(so_error); getsockopt(sock->fd, SOL_SOCKET, SO_ERROR, &so_error, &len); if (so_error == 0) { rc = 0; /* Handle signal */ } else { #ifdef WOLFMQTT_NONBLOCK if (so_error == EWOULDBLOCK || so_error == EAGAIN) { return MQTT_CODE_CONTINUE; } #endif rc = MQTT_CODE_ERROR_NETWORK; PRINTF("NetWrite: Error %d", so_error); } } (void)timeout_ms; return rc; }
/* Adds an alarm to the queue and sets up the timer */ static void queue_alarm (AlarmRecord *ar) { GList *old_head; /* Track the current head of the list in case there are changes */ old_head = alarms; /* Insert the new alarm in order if the alarm's trigger time is after the current time */ alarms = g_list_insert_sorted (alarms, ar, compare_alarm_by_time); /* If there first item on the list didn't change, the time out is fine */ if (old_head == alarms) return; /* Set the timer for removal upon activation */ setup_timeout (); }
bool write_byte_timeout( int port, byte data_byte, int sec100 ) { CALL_MSG("write_byte_timeout()", ++pclta_call_level ); setup_timeout( sec100 ); while( ( inb_p( status_reg(port) ) & _READY ) && ( ! pclta_timeout ) ); if( ! pclta_timeout ) outb_p( data_byte, data_reg(port) ); del_timer( &pclta_timer_list ); if( pclta_timeout ) { RETURN_MSG( "write_byte_timeout() false", pclta_call_level-- ); return false; } RETURN_MSG( "write_byte_timeout() true", pclta_call_level-- ); return true; }
void select_loop(int server_socket) { int highest_fd; fd_set sockets; struct timeval timeout; ; setup_timeout(&timeout); FD_ZERO(&sockets); FD_SET(server_socket, &sockets); highest_fd = server_socket; while (true) { fd_set read_set = sockets; switch (select(highest_fd + 1, &read_set, NULL, NULL, &timeout)) { case ERROR: throw("Error on select"); break; case 0: die("Timeout on select\n"); break; } _accept_select_connections(&read_set, server_socket, &sockets, &highest_fd); _handle_select_requests(&read_set, &sockets, highest_fd, server_socket); } }
static int NetRead(void *context, byte* buf, int buf_len, int timeout_ms) { SocketContext *sock = (SocketContext*)context; int rc = -1, timeout = 0; SOERROR_T so_error = 0; #if !defined(WOLFMQTT_NO_TIMEOUT) && !defined(WOLFMQTT_NONBLOCK) fd_set recvfds; fd_set errfds; struct timeval tv; #endif if (context == NULL || buf == NULL || buf_len <= 0) { return MQTT_CODE_ERROR_BAD_ARG; } sock->bytes = 0; #if !defined(WOLFMQTT_NO_TIMEOUT) && !defined(WOLFMQTT_NONBLOCK) /* Setup timeout and FD's */ setup_timeout(&tv, timeout_ms); FD_ZERO(&recvfds); FD_SET(sock->fd, &recvfds); FD_ZERO(&errfds); FD_SET(sock->fd, &errfds); #ifdef WOLFMQTT_ENABLE_STDIN_CAP FD_SET(STDIN, &recvfds); #endif #else (void)timeout_ms; #endif /* !WOLFMQTT_NO_TIMEOUT && !WOLFMQTT_NONBLOCK */ /* Loop until buf_len has been read, error or timeout */ while (sock->bytes < buf_len) { #if !defined(WOLFMQTT_NO_TIMEOUT) && !defined(WOLFMQTT_NONBLOCK) /* Wait for rx data to be available */ rc = select((int)SELECT_FD(sock->fd), &recvfds, NULL, &errfds, &tv); if (rc > 0) { /* Check if rx or error */ if (FD_ISSET(sock->fd, &recvfds)) { #endif /* !WOLFMQTT_NO_TIMEOUT && !WOLFMQTT_NONBLOCK */ /* Try and read number of buf_len provided, minus what's already been read */ rc = (int)SOCK_RECV(sock->fd, &buf[sock->bytes], buf_len - sock->bytes, 0); if (rc <= 0) { rc = -1; goto exit; /* Error */ } else { sock->bytes += rc; /* Data */ } #if !defined(WOLFMQTT_NO_TIMEOUT) && !defined(WOLFMQTT_NONBLOCK) } #ifdef WOLFMQTT_ENABLE_STDIN_CAP else if (FD_ISSET(STDIN, &recvfds)) { return MQTT_CODE_STDIN_WAKE; } #endif if (FD_ISSET(sock->fd, &errfds)) { rc = -1; break; } } else { timeout = 1; break; /* timeout or signal */ } #else /* non-blocking should always exit loop */ break; #endif /* !WOLFMQTT_NO_TIMEOUT && !WOLFMQTT_NONBLOCK */ } /* while */ exit: if (rc == 0 && timeout) { rc = MQTT_CODE_ERROR_TIMEOUT; } else if (rc < 0) { /* Get error */ socklen_t len = sizeof(so_error); getsockopt(sock->fd, SOL_SOCKET, SO_ERROR, &so_error, &len); if (so_error == 0) { rc = 0; /* Handle signal */ } else { #ifdef WOLFMQTT_NONBLOCK if (so_error == EWOULDBLOCK || so_error == EAGAIN) { return MQTT_CODE_CONTINUE; } #endif rc = MQTT_CODE_ERROR_NETWORK; PRINTF("NetRead: Error %d", so_error); } } else { rc = sock->bytes; } sock->bytes = 0; return rc; }
static int NetConnect(void *context, const char* host, word16 port, int timeout_ms) { SocketContext *sock = (SocketContext*)context; int type = SOCK_STREAM; int rc = -1; SOERROR_T so_error = 0; struct addrinfo *result = NULL; struct addrinfo hints; #if defined(MICROCHIP_MPLAB_HARMONY) struct hostent *hostInfo; #endif /* Get address information for host and locate IPv4 */ switch(sock->stat) { case SOCK_BEGIN: { XMEMSET(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; XMEMSET(&sock->addr, 0, sizeof(sock->addr)); sock->addr.sin_family = AF_INET; #if defined(MICROCHIP_MPLAB_HARMONY) hostInfo = gethostbyname((char *)host); if (hostInfo != NULL) { sock->addr.sin_port = port; /* htons(port); */ sock->addr.sin_family = AF_INET; XMEMCPY(&sock->addr.sin_addr.S_un, *(hostInfo->h_addr_list), sizeof(IPV4_ADDR)); } else { return MQTT_CODE_CONTINUE; } #else rc = getaddrinfo(host, NULL, &hints, &result); if (rc >= 0 && result != NULL) { struct addrinfo* res = result; /* prefer ip4 addresses */ while (res) { if (res->ai_family == AF_INET) { result = res; break; } res = res->ai_next; } if (result->ai_family == AF_INET) { sock->addr.sin_port = htons(port); sock->addr.sin_family = AF_INET; sock->addr.sin_addr = ((struct sockaddr_in*)(result->ai_addr))->sin_addr; } else { rc = -1; } freeaddrinfo(result); } if (rc != 0) goto exit; #endif /* MICROCHIP_MPLAB_HARMONY */ /* Default to error */ rc = -1; /* Create socket */ sock->fd = socket(sock->addr.sin_family, type, 0); if (sock->fd == SOCKET_INVALID) goto exit; sock->stat = SOCK_CONN; FALL_THROUGH; } case SOCK_CONN: { #ifndef WOLFMQTT_NO_TIMEOUT fd_set fdset; struct timeval tv; /* Setup timeout and FD's */ setup_timeout(&tv, timeout_ms); FD_ZERO(&fdset); FD_SET(sock->fd, &fdset); #endif /* !WOLFMQTT_NO_TIMEOUT */ #if !defined(WOLFMQTT_NO_TIMEOUT) && defined(WOLFMQTT_NONBLOCK) /* Set socket as non-blocking */ tcp_set_nonblocking(&sock->fd); #endif /* Start connect */ rc = connect(sock->fd, (struct sockaddr*)&sock->addr, sizeof(sock->addr)); #if defined(MICROCHIP_MPLAB_HARMONY) if (rc) #else #ifndef WOLFMQTT_NO_TIMEOUT /* Wait for connect */ if (rc < 0 || select((int)SELECT_FD(sock->fd), NULL, &fdset, NULL, &tv) > 0) #else if (rc < 0) #endif /* !WOLFMQTT_NO_TIMEOUT */ #endif /* MICROCHIP_MPLAB_HARMONY */ { /* Check for error */ #if defined(MICROCHIP_MPLAB_HARMONY) if (errno == EINPROGRESS) { rc = MQTT_CODE_CONTINUE; } #else socklen_t len = sizeof(so_error); getsockopt(sock->fd, SOL_SOCKET, SO_ERROR, &so_error, &len); if (so_error == 0) { rc = 0; /* Success */ } #if !defined(WOLFMQTT_NO_TIMEOUT) && defined(WOLFMQTT_NONBLOCK) else if (so_error == EINPROGRESS) { rc = MQTT_CODE_CONTINUE; } #endif #endif /* MICROCHIP_MPLAB_HARMONY */ } break; } default: rc = -1; } /* switch */ (void)timeout_ms; exit: /* Show error */ if (rc != 0) { PRINTF("NetConnect: Rc=%d, SoErr=%d", rc, so_error); } return rc; }
bool probe_hardware( int minor ) { int erc, trans; byte rx_buffer[256]; int port = base_address[minor]; int irq = pclta_interrupt[minor]; struct pclta_device * device; if( !port || !irq ) return 0; VERBOSE_MSG( "probing minor = %d, I/O = %#x-%#x, interrupt request = %d ... ", minor, port, port+3, irq ); if( 0 > check_region( port, 4 ) ) { VERBOSE_MSG( "error(%d) PCLTA_IOPERM.\n", PCLTA_IOPERM ); return false; } init_hw( port, 0x00, CLK_10 ); // initialize device hardware, no IRQ if( ! wait_reset( port, 500 ) ) { VERBOSE_MSG( "error(%d) PCLTA_LRESET.\n", PCLTA_LRESET ); return false; } // waiting to leave reset state setup_timeout( 200 ); while( !pclta_timeout ); // Why wait? Because, when PCLTA gets out of RESET state, it sets // status register BEFORE preparing uplink reset. So, I read the // packet of length 0 and get an error. Stupid! // wait for reset command response if( ! wait_uplink_buffer( port, 500, rx_buffer ) ) { VERBOSE_MSG( "error(%d) PCLTA_RESP. \n", PCLTA_RESP ); return false; } // make sure the reset was successful if( rx_buffer[0] != 1 || rx_buffer[1] != niRESET ) { VERBOSE_MSG( "error(%d) PCLTA_RESP. Reset not successfull. \n", PCLTA_RESP ); return false; } // set up internal interrupt register write_byte_wait( port, CMD_XFER ); write_byte_wait( port, 2 ); write_byte_wait( port, niIRQENA ); write_byte_wait( port, IRQPLRTY | RESET_ENABLE | DLREADY_ENABLE | DLPBA_ENABLE | DLBA_ENABLE | ULREADY_ENABLE ); // write a command to get transciever status write_byte_wait( port, CMD_XFER ); write_byte_wait( port, 1 ); write_byte_wait( port, niSSTATUS ); // wait for response if( ! wait_uplink_buffer( port, 500, rx_buffer ) ) { VERBOSE_MSG( "error(%d) PCLTA_STAT.\n", PCLTA_STAT ); return false; } // get transceiver type trans = rx_buffer[2]>>3; if( ! *pclta_transceiver[trans] ) { VERBOSE_MSG( "error(%d) PCLTA_TSCV.\n", PCLTA_TSCV ); return false; // transciever not supported } // register character device if( 0 > (pclta_major = register_chrdev( 0, "pclta", &pclta_fops )) ) { VERBOSE_MSG( "error(%d) PCLTA_MAJOR.\n", PCLTA_MAJOR ); return false; } // request interrupt permission OLD LOCATION // if( 0 > ( erc = request_irq( irq, pclta_interrupt, 0, "pclta", NULL ) ) ) { SLOW INT // if( 0 > ( erc = request_irq( irq, pclta_interrupt,SA_INTERRUPT, "pclta", device ) ) ) { // VERBOSE_MSG( "error(%d) PCLTA_IRQPERM.\n", PCLTA_IRQPERM ); // return false; // } // create device descriptor device = create_device( port, irq, pclta_clock[rx_buffer[2]>>3] ); if( device == NULL ) { VERBOSE_MSG( "error(%d) PCLTA_CDEV.\n", PCLTA_CDEV ); return false; } // request interrupt permission NEW LOCATION SO WE HAVE DEVICE STRUCT // if( 0 > ( erc = request_irq( irq, pclta_interrupt, 0, "pclta", NULL ) ) ) { SLOW INT if( 0 > ( erc = request_irq( irq, pclta_interrupt_handler,0, "pclta", device) ) ) { VERBOSE_MSG( "error(%d) PCLTA_IRQPERM.\n", PCLTA_IRQPERM ); return false; } // set up pointers to device descriptor pclta_device_table[minor] = device; pclta_interrupt_table[minor] = device; // remember minor number device->minor = minor; // claim I/O space request_region( port, 4, "pclta" ); // start up the interupts removed 07/07 by karl. // init_hw( port,irq_mask[irq], CLK_10 ); // initialize device hardware, no IRQ DEBUG_MSG( "Transceiver type: %s", pclta_transceiver[trans] ); MESSAGE("Device pclta%d installed.", minor ); return true; }