void incoming_connection(io_handle_t *server) { char buf[30]; io_handle_t *client = (io_handle_t*)calloc(1, sizeof(io_handle_t)); RET_IF_ERR(io_tcp_accept(server, client, buf, 30)); printf("new connection from %s\n", buf); printf("handle count: %i\n", loop.handle_count); RET_IF_ERR(io_read_start(client, echo_alloc, echo_read)); }
//**************************************************************************** // //! Parses the readreg command parameters and invokes the I2C APIs //! i2c readreg 0x<dev_addr> 0x<reg_offset> <rdlen> //! //! \param pcInpString pointer to the readreg command parameters //! //! This function //! 1. Parses the readreg command parameters. //! 2. Invokes the corresponding I2C APIs //! //! \return 0: Success, < 0: Failure. // //**************************************************************************** int ProcessReadRegCommand(char *pcInpString) { unsigned char ucDevAddr, ucRegOffset, ucRdLen; unsigned char aucRdDataBuf[256]; char *pcErrPtr; // // Get the device address // pcInpString = strtok(NULL, " "); RETERR_IF_TRUE(pcInpString == NULL); ucDevAddr = (unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16); // // Get the register offset address // pcInpString = strtok(NULL, " "); RETERR_IF_TRUE(pcInpString == NULL); ucRegOffset = (unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16); // // Get the length of data to be read // pcInpString = strtok(NULL, " "); RETERR_IF_TRUE(pcInpString == NULL); ucRdLen = (unsigned char)strtoul(pcInpString, &pcErrPtr, 10); //RETERR_IF_TRUE(ucLen > sizeof(aucDataBuf)); // // Write the register address to be read from. // Stop bit implicitly assumed to be 0. // RET_IF_ERR(I2C_IF_Write(ucDevAddr,&ucRegOffset,1,0)); // // Read the specified length of data // RET_IF_ERR(I2C_IF_Read(ucDevAddr, &aucRdDataBuf[0], ucRdLen)); UART_PRINT("I2C Read From address complete\n\r"); // // Display the buffer over UART on successful readreg // DisplayBuffer(aucRdDataBuf, ucRdLen); return SUCCESS; }
//**************************************************************************** // //! Get the raw accelerometer data register readings //! //! \param psAccX pointer to the raw AccX store //! \param psAccY pointer to the raw AccY store //! \param psAccZ pointer to the raw AccZ store //! //! This function //! 1. Reads the data registers over I2C. //! 2. Returns the accelerometer readings //! //! \return 0: Success, < 0: Failure. // //**************************************************************************** int BMA222ReadNew(signed char *pcAccX, signed char *pcAccY, signed char *pcAccZ) { char cAccX[6]; // // Read the acclerometer output registers LSB and MSB // RET_IF_ERR(BlockRead(BMA222_ACC_DATA_X_NEW, (unsigned char *)cAccX,6)); // // Check whether new Sensor Data is available // if((cAccX[0] & 0x1) && (cAccX[2] & 0x1) && (cAccX[4] & 0x1)) { *pcAccX = cAccX[1]; *pcAccY = cAccX[3]; *pcAccZ = cAccX[5]; return SUCCESS; } //New Sensor Data Not Available return FAILURE; }
//**************************************************************************** // //! Parses the writereg command parameters and invokes the I2C APIs //! i2c writereg 0x<dev_addr> 0x<reg_offset> <wrlen> <0x<byte0> [0x<byte1> ... ]> //! //! \param pcInpString pointer to the readreg command parameters //! //! This function //! 1. Parses the writereg command parameters. //! 2. Invokes the corresponding I2C APIs //! //! \return 0: Success, < 0: Failure. // //**************************************************************************** int ProcessWriteRegCommand(char *pcInpString) { unsigned char ucDevAddr, ucRegOffset, ucWrLen; unsigned char aucDataBuf[256]; char *pcErrPtr; int iLoopCnt = 0; // // Get the device address // pcInpString = strtok(NULL, " "); RETERR_IF_TRUE(pcInpString == NULL); ucDevAddr = (unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16); // // Get the register offset to be written // pcInpString = strtok(NULL, " "); RETERR_IF_TRUE(pcInpString == NULL); ucRegOffset = (unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16); aucDataBuf[iLoopCnt] = ucRegOffset; iLoopCnt++; // // Get the length of data to be written // pcInpString = strtok(NULL, " "); RETERR_IF_TRUE(pcInpString == NULL); ucWrLen = (unsigned char)strtoul(pcInpString, &pcErrPtr, 10); //RETERR_IF_TRUE(ucWrLen > sizeof(aucDataBuf)); // // Get the bytes to be written // for(; iLoopCnt < ucWrLen + 1; iLoopCnt++) { // // Store the data to be written // pcInpString = strtok(NULL, " "); RETERR_IF_TRUE(pcInpString == NULL); aucDataBuf[iLoopCnt] = (unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16); } // // Write the data values. // RET_IF_ERR(I2C_IF_Write(ucDevAddr,&aucDataBuf[0],ucWrLen+1,1)); UART_PRINT("I2C Write To address complete\n\r"); return SUCCESS; }
void file_changed(io_handle_t *handle, const char *path, int events) { if(strncmp(path, ttyACM, 6) != 0) return; if((events & IN_CREATE) && serl.fd == -1) { strcpy(addr, "/dev/"); strcat(addr, path); printf("arduino plugged in (%s).\n", path); RET_IF_ERR(io_serial(&loop, &serl, addr, B115200, SERIAL_8N1)); DIE_IF_ERR(io_publish_start(&serl, &channel, data_published)); } }
//**************************************************************************** // //! Initialize the BMA222 accelerometer device with defaults //! //! \param None //! //! This function //! 1. Reads the CHIP ID. //! //! \return 0: Success, < 0: Failure. // //**************************************************************************** int BMA222Open() { unsigned char ucRegVal; // // Read the CHIP ID NUM // RET_IF_ERR(GetRegisterValue(BMA222_CHID_ID_NUM, &ucRegVal)); DBG_PRINT("CHIP ID: 0x%x\n\r", ucRegVal); return SUCCESS; }
//**************************************************************************** // //! Get the accelerometer data readings //! //! \param pfAccX pointer to the AccX store //! \param pfAccY pointer to the AccY store //! \param pfAccZ pointer to the AccZ store //! //! This function //! 1. Reads the data registers over I2C. //! 2. Applies the range conversion to the raw values //! //! \return 0: Success, < 0: Failure. // //**************************************************************************** int BMA222Read(signed char *pcAccX, signed char *pcAccY, signed char *pcAccZ) { char cAccX = 0; char cAccY = 0; char cAccZ = 0; // // Read the acclerometer output registers LSB and MSB // RET_IF_ERR(BlockRead(BMA222_ACC_DATA_X, (unsigned char *)&cAccX, sizeof(cAccX))); RET_IF_ERR(BlockRead(BMA222_ACC_DATA_Y, (unsigned char *)&cAccY, sizeof(cAccY))); RET_IF_ERR(BlockRead(BMA222_ACC_DATA_Z, (unsigned char *)&cAccZ, sizeof(cAccZ))); *pcAccX = cAccX; *pcAccY = cAccY; *pcAccZ = cAccZ; return SUCCESS; }
void new_rfcm_conn(io_handle_t *server) { if(rfcm.fd != -1) io_rfcomm_close(&rfcm); RET_IF_ERR(io_rfcomm_accept(server, &rfcm, addr, 30)); printf("rfcm connected: %s\n", addr); RET_IF_ERR(io_read_start(&rfcm, rfcm_alloc, rfcm_read)); }
void new_tcp3_conn(io_handle_t *server) { if(tcp3.fd != -1) io_tcp_close(&tcp3); RET_IF_ERR(io_tcp_accept(server, &tcp3, addr, 30)); printf("tcp3 connected: %s\n", addr); RET_IF_ERR(io_read_start(&tcp3, tcp3_alloc, tcp3_read)); }
void new_tcp2_conn(io_handle_t *server) { if(tcp2.fd != -1) io_tcp_close(&tcp2); RET_IF_ERR(io_tcp_accept(server, &tcp2, addr, 30)); printf("tcp2 connected: %s\n", addr); RET_IF_ERR(io_splice_start(&tcp2, &rfcm, tcp2_spliced)); }
void new_tcp1_conn(io_handle_t *server) { if(tcp1.fd != -1) io_tcp_close(&tcp1); RET_IF_ERR(io_tcp_accept(server, &tcp1, addr, 30)); printf("tcp1 connected: %s\n", addr); RET_IF_ERR(io_splice_start(&tcp1, &serl, tcp1_spliced)); }