Exemple #1
0
void MCP23S17::digitalWrite(uint8_t pin, bool value)
{
    if(value)
        write_addr(GPIO, read_addr(GPIO) | 1<<pin );
    else
        write_addr(GPIO, read_addr(GPIO) & ~(1<<pin) );
}
Exemple #2
0
void MCP23S17::pinMode(uint8_t pin, bool mode)
{
    if(mode == INPUT)
        write_addr(IODIR, read_addr(IODIR) | 1<<pin );
    else
        write_addr(IODIR, read_addr(IODIR) & ~(1<<pin) );
}
QVector<uint> DFInstanceLinux::enumerate_vector(const uint &addr) {
    QVector<uint> addrs;
    if (!addr)
        return addrs;

    attach();
    VIRTADDR start = read_addr(addr);
    VIRTADDR end = read_addr(addr + 4);
    int bytes = end - start;
    int entries = bytes / 4;
    TRACE << "enumerating vector at" << hex << addr << "START" << start
        << "END" << end << "UNVERIFIED ENTRIES" << dec << entries;
    VIRTADDR tmp_addr = 0;

    if (entries > 5000) {
        LOGW << "vector at" << hexify(addr) << "has over 5000 entries! (" <<
                entries << ")";
    }

#ifdef _DEBUG
    if (m_layout->is_complete()) {
        Q_ASSERT_X(start > 0, "enumerate_vector", "start pointer must be larger than 0");
        Q_ASSERT_X(end > 0, "enumerate_vector", "End must be larger than start!");
        Q_ASSERT_X(start % 4 == 0, "enumerate_vector", "Start must be divisible by 4");
        Q_ASSERT_X(end % 4 == 0, "enumerate_vector", "End must be divisible by 4");
        Q_ASSERT_X(end >= start, "enumerate_vector", "End must be >= start!");
        Q_ASSERT_X((end - start) % 4 == 0, "enumerate_vector", "end - start must be divisible by 4");
    } else {
        // when testing it's usually pretty bad to find a vector with more
        // than 5000 entries... so throw
        //Q_ASSERT_X(entries < 5000, "enumerate_vector", "more than 5000 entires");
    }
#endif
    QByteArray data(bytes, 0);
    int bytes_read = read_raw(start, bytes, data);
    if (bytes_read != bytes && m_layout->is_complete()) {
        LOGW << "Tried to read" << bytes << "bytes but only got"
                << bytes_read;
        detach();
        return addrs;
    }
    for(int i = 0; i < bytes; i += 4) {
        tmp_addr = decode_dword(data.mid(i, 4));
        addrs << tmp_addr;
    }
    detach();
    return addrs;
}
void Annual_Routines::load_grid()
{
	m_FlexGrid.put_TextMatrix(0,0,_T("Num"));
	m_FlexGrid.put_TextMatrix(0,1,_T("Full_Label"));
	m_FlexGrid.put_TextMatrix(0,2,_T("A/m"));
	m_FlexGrid.put_TextMatrix(0,3,_T("Value"));
//	m_FlexGrid.put_TextMatrix(0,4,"Label");

	m_FlexGrid.put_TextMatrix(1,0,_T("1"));
	m_FlexGrid.put_TextMatrix(2,0,_T("2"));
	m_FlexGrid.put_TextMatrix(3,0,_T("3"));
	m_FlexGrid.put_TextMatrix(4,0,_T("4"));
	m_FlexGrid.put_TextMatrix(5,0,_T("5"));
	m_FlexGrid.put_TextMatrix(6,0,_T("6"));
	m_FlexGrid.put_TextMatrix(7,0,_T("7"));
	m_FlexGrid.put_TextMatrix(8,0,_T("8"));
	m_FlexGrid.put_TextMatrix(9,0,_T("9"));
	m_FlexGrid.put_TextMatrix(10,0,_T("10"));
	m_FlexGrid.put_TextMatrix(11,0,_T("11"));
	m_FlexGrid.put_TextMatrix(12,0,_T("12"));
	m_FlexGrid.put_TextMatrix(13,0,_T("13"));
	m_FlexGrid.put_TextMatrix(14,0,_T("14"));
	m_FlexGrid.put_TextMatrix(15,0,_T("15"));
	m_FlexGrid.put_TextMatrix(16,0,_T("16"));

	m_FlexGrid.put_ColWidth(0,400);

	description2 temp_description;
	memset(&temp_description,0,sizeof(description2));
	for(unsigned char i=1;i<=GRID_ROW_NUMBER;i++)
	{//////////////////////////////////////////////////get information from network
		temp_description=read_addr(i-1);
		put_row_col(temp_description,i);
	}
}
QString DFInstanceLinux::read_string(const VIRTADDR &addr) {
    VIRTADDR buffer_addr = read_addr(addr);
    int upper_size = 256;
    QByteArray buf(upper_size, 0);
    read_raw(buffer_addr, upper_size, buf);

    buf.truncate(buf.indexOf(QChar('\0')));
    CP437Codec *c = new CP437Codec();
    return c->toUnicode(buf);
}
Exemple #6
0
MCP23S17::MCP23S17(uint8_t slave_select_pin, byte aaa_hw_addr)
{
    // Set the aaa hardware address for this chip by tying the
    // MCP23S17's pins (A0, A1, and A2) to either 5v or GND.
    setup_ss(slave_select_pin);

    // We enable HAEN on all connected devices before we can address them individually
    setup_device(0x00);
    write_addr(IOCON, read_addr(IOCON)|HAEN);

    // Remember the hardware address for this chip
    setup_device(aaa_hw_addr);
}
void Annual_Routines::write_addr(unsigned char addr2)
{
	CString str_temp;
	description2 temp_descri;
	memset(&temp_descri,0,sizeof(description2));
	str_temp=m_FlexGrid.get_TextMatrix(addr2,1);//1
	int i=0;
	for(i=1;i<=FULL_LABEL_NUMBER ;i++)
	{
		if(i<str_temp.GetLength()+1)
            temp_descri.full_label[i-1]=str_temp.GetAt(i-1);
		else
			temp_descri.full_label[i-1]=0;
	}
	temp_descri.full_label[FULL_LABEL_NUMBER-1]=0;

//	str_temp=m_FlexGrid.get_TextMatrix(addr2,4);//4
//	for(i=1;i<=LABEL_NUMBER;i++)
//	{
//		if(i<str_temp.GetLength()+1)
  //          temp_descri.label[i-1]=str_temp.GetAt(i-1);
	//	else
	//		temp_descri.label[i-1]=0;
//	}
	temp_descri.flag=0;
	str_temp=m_FlexGrid.get_TextMatrix(addr2,3);//3
	if(str_temp==_T("ON"))
		temp_descri.flag=temp_descri.flag | 64;
	str_temp=m_FlexGrid.get_TextMatrix(addr2,2);//2
	if(str_temp==_T("MAN"))
		temp_descri.flag=temp_descri.flag | 128;
	unsigned char *p;
	p=(unsigned char *) &temp_descri;
	if(m_strtype.CompareNoCase(_T("Lightingcontroller")) == 0)
		Write_Multi(254,p,5228+(addr2-1)*AR_DESCRIPTION_SIZE,AR_DESCRIPTION_SIZE);
	else
		Write_Multi(g_tstat_id,p,MODBUS_AR_DESCRIP_FIRST+(addr2-1)*AR_DESCRIPTION_SIZE,AR_DESCRIPTION_SIZE);
	NET_WORK_SLEEP_BETWEEN_WRITE_READ
	temp_descri=read_addr(addr2-1);
	put_row_col(temp_descri,addr2);
}
bool DFInstanceLinux::find_running_copy(bool connect_anyway) {
    // find PID of DF
    TRACE << "attempting to find running copy of DF by executable name";
    QProcess *proc = new QProcess(this);
    QStringList args;
    args << "dwarfort.exe"; // 0.31.04 and earlier
    args << "Dwarf_Fortress"; // 0.31.05+
    proc->start("pidof", args);
    proc->waitForFinished(1000);
    if (proc->exitCode() == 0) { //found it
        QByteArray out = proc->readAllStandardOutput();
        QStringList str_pids = QString(out).split(" ");
        str_pids.sort();
        if(str_pids.count() > 1){
            m_pid = QInputDialog::getItem(0, tr("Warning"),tr("Multiple Dwarf Fortress processes found, please choose the process to use."),str_pids,str_pids.count()-1,false).toInt();
        }else{
            m_pid = str_pids.at(0).toInt();
        }

        m_memory_file.setFileName(QString("/proc/%1/mem").arg(m_pid));

        TRACE << "USING PID:" << m_pid;
    } else {
        QMessageBox::warning(0, tr("Warning"),
                             tr("Unable to locate a running copy of Dwarf "
                                "Fortress, are you sure it's running?"));
        LOGW << "can't find running copy";
        m_is_ok = false;
        return m_is_ok;
    }

    m_inject_addr = unsigned(-1);
    m_alloc_start = 0;
    m_alloc_end = 0;

    map_virtual_memory();

    //qDebug() << "LOWEST ADDR:" << hex << lowest_addr;


    //DUMP LIST OF MEMORY RANGES
    /*
    QPair<uint, uint> tmp_pair;
    foreach(tmp_pair, m_regions) {
        LOGD << "RANGE start:" << hex << tmp_pair.first << "end:" << tmp_pair.second;
    }*/

    VIRTADDR m_base_addr = read_addr(m_lowest_address + 0x18);
    LOGD << "base_addr:" << m_base_addr << "HEX" << hex << m_base_addr;
    m_is_ok = m_base_addr > 0;

    uint checksum = calculate_checksum();
    LOGD << "DF's checksum is" << hexify(checksum);
    if (m_is_ok) {
        m_layout = get_memory_layout(hexify(checksum).toLower(), !connect_anyway);
    }

    //Get dwarf fortress directory
    m_df_dir = QDir(QFileInfo(QString("/proc/%1/cwd").arg(m_pid)).symLinkTarget());
    LOGI << "Dwarf fortress path:" << m_df_dir.absolutePath();

    return m_is_ok || connect_anyway;
}
Exemple #9
0
int MCP23S17::digitalRead(uint8_t pin)
{
    (int)(read_addr(GPIO) & 1<<pin);
}
Exemple #10
0
uint16_t MCP23S17::port()
{
    return read_addr(GPIO);
}
Exemple #11
0
void child_core(int fd)
{
    /* make child encryptor*/
    make_encryptor(&crypto, &chd_crypto, 0, NULL);


    uint8_t buf[BUF_SIZE];
    size_t remote_addr_len;
    struct sockaddr *remote_addr;
    if (read_addr(fd, buf, &remote_addr, &remote_addr_len) != 0) {
        clean_exit(errno, "get remote addr error", 1, fd);
    }

    int remote_fd;
    if ((remote_fd = socket(remote_addr->sa_family, SOCK_STREAM, 0)) < 0) {
        clean_exit(errno, "cannot open socket", 1, fd);
    }

    int optval = 1;
    if (setsockopt(remote_fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(optval)) == -1) {
        clean_exit(errno, "set socket option error", 1, fd);
    }
        
    if (connect(remote_fd, remote_addr, remote_addr_len) < 0 ) {
        clean_exit(errno, "cannnot connect remote", 1, fd);
    }
    free(remote_addr);
    remote_addr = NULL;
    remote_addr_len = 0;


    /* use poll */
    struct pollfd fds[2];
    fds[0].fd = fd;
    fds[1].fd = remote_fd;
    fds[0].events = POLLIN | POLLERR | POLLHUP;
    fds[1].events = POLLIN | POLLERR | POLLHUP;

    size_t read_size;
    int rc;
    while (poll(fds, 2, -1) > 0) {
        if (fds[0].revents & (POLLIN | POLLHUP)) {
            rc = crypt_recv(fd, buf, BUF_SIZE);
	    if (rc > 0) {
	        read_size = (size_t)rc;
		if (sendall(remote_fd, buf, read_size, 0) < read_size) break;
            }
            else if (rc == 0) {
                fds[0].fd = -1;
                shutdown(fd, SHUT_RD);
                shutdown(remote_fd, SHUT_WR);
            }
            else {
                break;
            }
        }
        
        if (fds[1].revents & (POLLIN | POLLHUP)) {
            rc = recv(remote_fd, buf, BUF_SIZE, 0);
            if (read_size > 0) {
	        read_size = (size_t)rc;
		if (crypt_sendall(fd, buf, read_size) < read_size) break;
            }
            else if (read_size == 0) {
                fds[1].fd = -1;
                shutdown(fd, SHUT_WR);
                shutdown(remote_fd, SHUT_RD);              
            }
            else {
                break;
            }
        }

        if (fds[0].fd == fds[1].fd) clean_exit(0, "close all sock", 0);
        if ((fds[0].revents & POLLERR) || (fds[1].revents & POLLERR)) break;
    }
    clean_exit(errno, "socket pair error exit", 0);
}