int log_putc(int c) { if(last_update.trigger_value == 0) last_update = register_timer(LOG_TIMEOUT); if(log_buf && buf_size) { log_buf[buf_ptr++] = c; if(buf_ptr >= buf_size) { if(log_fp && log_fp->fflush_cb) { log_fp->fflush_cb(log_fp); last_update = register_timer(LOG_TIMEOUT); } buf_ptr = 0; } return 0; } else if(log_fp) { // Disable output to the log for the write rpi_boot_output_state state = output_get_state(); output_disable_log(); // Write one character fwrite(&c, 1, 1, log_fp); // Restore saved output state output_restore_state(state); return 0; } return EOF; }
void xpl_send_sensor_basic_output(enum XPL_MSG_TYPE msg_type) { char result[4]; xpl_print_header(msg_type); printf("sensor.basic\n{\ndevice=output%i\n",xpl_output_id); strcpypgm2ram(result,output_get_state(xpl_output_id)); printf("type=output\ncurrent=%s\n}\n",result); return; }
static int log_fflush(FILE *fp) { // Flush the buffer if(fp && log_buf) { // Disable output to the log for the write rpi_boot_output_state state = output_get_state(); output_disable_log(); // Write the buffer fwrite(log_buf, 1, buf_ptr, fp); // Restore the state output_restore_state(state); } return 0; }
static int send_message_int(int cmd_id, void *send_buf, size_t send_buf_len, void *recv_buf, size_t recv_buf_len) { // Send a message (v2 messages only) #ifdef RASPBOOTIN_DEBUG printf("RASPBOOTIN: sending cmd %i\n"); int fail_loc = 0; #endif // Check capabilities if((cmd_id < 0) || (cmd_id > 31)) { printf("RASPBOOTIN: invalid cmd number %i\n"); return INVALID_CMD; } if(cmd_id == 3) { printf("RASPBOOTIN: cannot use send_message_int to send cmd 3\n"); return INVALID_CMD; } uint32_t msg_capabilities = (1 << cmd_id); if(!(client_capabilities & msg_capabilities)) { printf("RASPBOOTIN: client does not support cmd %i\n"); return UNSUPPORTED_CMD; } if(!(server_capabilities & msg_capabilities)) { printf("RASPBOOTIN: server does not support cmd %i\n"); return UNSUPPORTED_CMD; } // The crc of the request is calculated from 'options' uint32_t crc = crc32(send_buf, send_buf_len); // Disable debug output on the uart rpi_boot_output_state ostate = output_get_state(); output_disable_uart(); // Clear the receive buffer while(uart_getc_timeout(1000) != -1); // Send the message uart_putc('\003'); uart_putc('\003'); uart_putc(cmd_id); uint8_t *send_p = (uint8_t *)send_buf; while(send_buf_len--) uart_putc(*send_p++); uart_putc(BYTE(crc, 0)); uart_putc(BYTE(crc, 1)); uart_putc(BYTE(crc, 2)); uart_putc(BYTE(crc, 3)); // Wait for the response usleep(2000); // Begin reading response int r_buf = 0; int ret = 0; // Read magic number uint32_t magic = 0; CHECK(read_lsb32(&magic, UART_TIMEOUT), 0); if(magic != MAGIC) { #ifdef RASPBOOTIN_DEBUG printf("RASPBOOTIN: invalid magic received: %08x (expecting %08x)\n", magic, MAGIC); #endif ret = INVALID_MAGIC; goto cleanup; } // Read response length uint32_t resp_length = 0; CHECK(read_lsb32(&resp_length, UART_TIMEOUT), 1); // Read error_code uint32_t error_code = 0; CHECK(read_lsb32(&error_code, UART_TIMEOUT), 2); if(error_code != SUCCESS) { ret = error_code; #ifdef RASPBOOTIN_DEBUG fail_loc = 3; #endif goto cleanup; } // Read the data (maximum is whatever is greater - recv_buf_len // or resp_length) // Resp_length is length of the message minus magic and crc // therefore it includes the length of resp_length and error_code // which have already been read, therefore subtract 8 size_t data_to_read_to_buffer = (size_t)(resp_length - 8); size_t data_to_discard = 0; size_t data_to_pad = 0; if(data_to_read_to_buffer > recv_buf_len) { data_to_read_to_buffer = recv_buf_len; data_to_discard = data_to_read_to_buffer - recv_buf_len; } else if(recv_buf_len > data_to_read_to_buffer) data_to_pad = recv_buf_len - data_to_read_to_buffer; crc = crc32_start(); crc = crc32_append(crc, &magic, 4); crc = crc32_append(crc, &resp_length, 4); crc = crc32_append(crc, &error_code, 4); int data_read = 0; uint8_t *rptr = (uint8_t *)recv_buf; while(data_to_read_to_buffer--) { r_buf = uart_getc_timeout(UART_TIMEOUT); CHECK(r_buf, 4); crc = crc32_append(crc, &r_buf, 1); *rptr++ = r_buf; data_read++; } while(data_to_discard--) { r_buf = uart_getc_timeout(UART_TIMEOUT); CHECK(r_buf, 5); crc = crc32_append(crc, &r_buf, 1); } while(data_to_pad--) *rptr++ = 0; crc = crc32_finish(crc); // Read the response CRC uint32_t resp_crc = 0; CHECK(read_lsb32(&resp_crc, UART_TIMEOUT), 6); if(resp_crc != crc) { ret = CRC_ERROR; #ifdef RASPBOOTIN_DEBUG fail_loc = 7; #endif goto cleanup; } cleanup: if(ret == 0) ret = data_read; // Clear the uart buffer while(uart_getc_timeout(1000) != -1); // Re-enable uart debug output output_restore_state(ostate); #ifdef RASPBOOTIN_DEBUG printf("RASPBOOTIN: send_message_int, returning %i (fail_loc %i)\n", ret, fail_loc); if(ret == CRC_ERROR) { printf("RASPBOOTIN: CRC error: read CRC %08x, expected %08x, magic %08x, resp_length %08x, error_code %08x\n", resp_crc, crc, magic, resp_length, error_code); } #endif return ret; }