/* * This function does the same as vstrpack from utils.[ch] but on a * Buffer instead of a char *. */ Buffer *bufVaPack(Buffer *buf, va_list ap) { if (buf->data == NULL) bufInit(buf); while (1) { va_list ap_copy; int required, available = buf->max_len - buf->act_len; va_copy(ap_copy, ap); required = vstrpack(buf->data + buf->act_len, available, ap_copy); va_end(ap_copy); if (available >= required + 1) { /* Room for "required" plus a null-byte? */ buf->act_len += required; /* Yes! Update actual length... */ buf->data[buf->act_len] = '\0'; /* ... and add the null-byte. */ break; } /* Not enough room: increase max_len until it fits. */ while (buf->max_len < buf->act_len + required + 1) { buf->max_len *= 2; } /* Then realloc and try again. */ buf->data = realloc(buf->data, buf->max_len); } return buf; }
// получает поле с размером данных из канала связи. Возвращает размер данных или 0xFFFFFFFF в случае ошибки static uint32 ReceiveLengthField(int Handle, StreamMethod Method, byte *LengthField, byte *LengthFieldSize, uint32 Timeout) { uint32 Size = 0xFFFFFFFF; *LengthFieldSize = 0; byte b; if (recvByte(Handle, Method, &b, Timeout) > 0) { LengthField[0] = b; if (b < 0x80) { Size = b; *LengthFieldSize = 1; } else { byte datasize = b & 0x0F; uint32 data = 0; // datasize не больше 4 байт tBuffer buf; bufInit(&buf, (byte *)&data, datasize); if (recvBuffer(Handle, Method, buf.ptr, buf.dim, Timeout) > 0) { memcpy(LengthField + 1, &data, datasize); if (IsLittleEndian()) memrev(&data, datasize); Size = data; *LengthFieldSize = datasize + 1; } } } return Size; }
/* * This function does the same as strunpack from utils.[ch] but on a * Buffer instead of a char *. */ Buffer *bufVaUnpack(Buffer *buf, va_list ap) { if (buf->data == NULL) bufInit(buf); vstrunpack(buf->data, buf->act_len, ap); return buf; }
/************************************************************************************ * @fn halUartInit * * @brief Initalise UART. Supported baudrates are: 38400, 57600 and 115200 * * @param uint8 baudrate * uint8 options - this parameter is ignored * * @return none */ void halUartInit(uint8 baudrate, uint8 options) { uint32 baud; // Initialize the buffers bufInit(&rbRxBuf); bufInit(&rbTxBuf); switch (baudrate) { case HAL_UART_BAUDRATE_38400: baud= 38400; break; case HAL_UART_BAUDRATE_57600: baud= 57600; break; default: baud= 115200; break; } usbUartInit(baud); }
void zb_sniffer_init() { ZB_MEMSET((void*)&g_izb, 0, sizeof(zb_intr_globals_t)); zb_8051_init_timer(); init_zu2400(); bufInit(&rbTxBuf); #ifndef ZB_SNIFFER_USB_TRACE zb_init_8051_serial(); #else usbUartInit(UART_BAUDRATE_115200); ZB_SNIFFER_TURN_ON_LED(); #endif ZB_ENABLE_ALL_INTER(); }
static void ExecuteCommandAndAnswer(int Handle) { uint32 Time = time(0); byte Result = 0; //int ret = pthread_mutex_lock(&DevMutex); switch (Request.Command) { case dcZero: printf("Zero\n"); Result = devSendMessage(0x56, NULL, 0) >= 0 ? crSuccess : crSendCommandError; break; case dcReset: printf("Reset\n"); Result = devSendMessage(0x42, NULL, 0) >= 0 ? crSuccess : crSendCommandError; break; case dcSCol: { char sData[strlen(Request.CommandData)+1]; strcpy(sData, Request.CommandData); printf("SCol: '%s'\n", sData); Result = devSendMessage(0x3B, (byte *)Request.CommandData, strlen(Request.CommandData)) >= 0 ? crSuccess : crSendCommandError; //PrintHEX(Request.CommandData, strlen(Request.CommandData)); break; } default: Result = crUnknownCommand; break; } //ret = pthread_mutex_unlock(&DevMutex); byte data[1+1+1 + 1+1+1 + 1+1+1 + 1+1+4 + 1+2+255]; // PackageID, Command, CommandResult, CommandTime, CommandData tBuffer buf; bufInit(&buf, data, sizeof(data)); bufAppTLVByte(&buf, sctPackageID, scpCommand); bufAppTLVByte(&buf, sctCommand, Request.Command); bufAppTLVByte(&buf, sctCommandResult, Result); bufAppTLVUInt32(&buf, sctCommandTime, Time); int lenCmdData = strlen(Request.CommandData); if (lenCmdData > 0) bufAppTLV(&buf, sctCommandData, lenCmdData, (byte *)Request.CommandData); tcpSendMessage(Handle, buf.ptr, buf.pos, SendTimeoutMSec); }
static void AnswerDRMData(int Handle) { byte data[sizeof(DRMData)*2]; tBuffer buf; bufInit(&buf, data, sizeof(data)); pthread_mutex_lock(&SyncMutex); //printf("TCP after lock\n"); bufAppTLVByte(&buf, sctPackageID, scpDRMData); /*if (DRMData.dgyroReceived)*/ bufAppTLV(&buf, sctDGYRO, sizeof(DRMData.dgyro), (byte *)&(DRMData.dgyro)); /*if (DRMData.dstatReceived)*/ bufAppTLV(&buf, sctDSTAT, sizeof(DRMData.dstat), (byte *)&(DRMData.dstat)); /*if (DRMData.deposnReceived)*/ bufAppTLV(&buf, sctDEPOSN, sizeof(DRMData.deposn), (byte *)&(DRMData.deposn)); /*if (DRMData.dorientReceived)*/ bufAppTLV(&buf, sctDORIENT, sizeof(DRMData.dorient), (byte *)&(DRMData.dorient)); /*if (DRMData.dgpvtReceived)*/ bufAppTLV(&buf, sctDGPVT, sizeof(DRMData.dgpvt), (byte *)&(DRMData.dgpvt)); //printf("TCP before unlock\n"); pthread_mutex_unlock(&SyncMutex); tcpSendMessage(Handle, buf.ptr, buf.pos, SendTimeoutMSec); }
/** @brief Send message to the server (PC or Retail system) @param size - return size of data @param decrypt - if TRUE, then decrypt received data by session key @param control - return control character @return TRUE if OK, or error code */ Bool tcpSendMessage(int Handle, byte *Data, uint32 DataSize, uint32 TimeoutMSec) { StreamMethod Method = smRecvSend; int ret; Bool result = FALSE; uint32 size = DataSize; uint32 AlignedSize = GetAlignedSize(size); byte LengthField[MAXLENGTHFIELDSIZE]; byte LengthFieldSize; tlvMakeLengthField(size, LengthField, &LengthFieldSize); byte MaxAttempsCount = 5; byte ack; tBuffer sendbuf; byte DataToSend[AlignedSize + MAXLENGTHFIELDSIZE + 1 + MAXCRCBUFFERSIZE]; // + max data size buffer, ETX, max CRC buffer bufInit(&sendbuf, DataToSend, sizeof(DataToSend)); // init buffer for writing (and clear of DataToSend) bufApp(&sendbuf, LengthField, LengthFieldSize); memcpy(DataToSend + sendbuf.pos, Data, DataSize); // просто копирование, без шифрования sendbuf.pos += AlignedSize; // DataToSend уже лежит в буфере на отправку bufAppByte(&sendbuf, pccETX); // append ETX to buffer byte CRCBuffer[MAXCRCBUFFERSIZE]; byte CRCBufferSize = crcMakeField(sendbuf.ptr, (word)sendbuf.pos, CRCBuffer); bufApp(&sendbuf, CRCBuffer, CRCBufferSize); /* printf("CRC Buffer: "); PrintHEX(CRCBuffer, CRCBufferSize);*/ byte AttempsCount = 1; lblSendAgain: ret = sendByte(Handle, Method, pccSTX); // send control char "start of packet" if (ret > 0) { ret = sendBuffer(Handle, Method, bufPtr(&sendbuf), bufLen(&sendbuf)); // send packet if (ret == bufLen(&sendbuf)) { // and wait for acknowledgment lblWaitAskAgain: ack = 0; ret = recvByte(Handle, Method, &ack, TimeoutMSec); if (ret > 0) { switch (ack) { case pccACK: // good result = TRUE; break; case pccBEL: // please, wait sendByte(Handle, Method, pccBEL); goto lblWaitAskAgain; case pccNAK: // send packet again if (AttempsCount <= MaxAttempsCount) { AttempsCount++; goto lblSendAgain; } else result = FALSE; break; default: // unknown state result = FALSE; break; } } else result = FALSE; } else result = FALSE; } else result = FALSE; //ptarr = memFree(ptarr); return result; }
/*********************************************************************************** * @fn bufFlush * * @brief Flush the buffer. * * @param pBuf- pointer to the buffer */ void bufFlush(ringBuf_t *pBuf) { bufInit(pBuf, pBuf->pData, pBuf->len); }