void rfSendCommad(uint8_t Command, uint16_t Address, uint8_t *Data, uint8_t Length, uint32_t Salt) { if (Length > 21) { dxprintf("Data can't be length than 20 bytes"); } uint8_t DeviceAddress[5] = {0x00}; nRF24_GetDeviceFullAddress(Address, DeviceAddress); uint8_t *pBuf = (uint8_t *)pvPortMalloc(Length + 7 + 4); pBuf[0] = 0x00; //Header, default 0x00 memcpy(&pBuf[1], nRF24_HUB_addr, 5); pBuf[6] = Command; //Cmd memcpy(&pBuf[7], Data, Length); uint32_t crcRes = rfCalcCRC32(&pBuf[0], Length + 7); *((uint32_t *)(pBuf + 7 + Length)) = crcRes; dxprintf("TX:\n\n"); for (int i = 0; i < Length + 7 + 4; i++) { dxprintf("%x ", pBuf[i]); } dxprintf("\n\n"); nRF24_TXPacket(&hspi2, DeviceAddress, pBuf, Length + 7); vPortFree(pBuf); }
dLink rfCreateDevice(void) { dxprintf("rfCreateDevice\n"); dLink newDevice = (dLink)pvPortMalloc(sizeof(xDevice)); if (newDevice == NULL) { dxprintf("Error: can't allocate memory!\n"); return NULL; } dxprintf("New device on: %p!\n", newDevice); newDevice->Next = NULL; newDevice->Prev = NULL; newDevice->Type = 0x00; newDevice->Config = 0x00; newDevice->Salt = rfGetSalt(); if (rfDevices == NULL) { // If list is empty rfDevices = newDevice; rfDevices->Address = rfDeviceAddrCounter; } else { // If list is not empty, find last elem, and add to it. dLink Cur = rfDevices; rfDeviceAddrCounter = Cur->Address; // TODO: Change the code below to get find Address more accurate while (Cur->Next != NULL) { Cur = Cur->Next; if (rfDeviceAddrCounter < Cur->Address) { rfDeviceAddrCounter = Cur->Address; } }; newDevice->Address = ++rfDeviceAddrCounter; newDevice->Prev = Cur; Cur->Next = newDevice; } dxprintf("rf: %p, ND: %p\n", rfDevices, newDevice); return newDevice; }
void pkt_dprint_cdata(int i, dbuf_t * d) { dxprintf(d, " %d:%s do_ssl:%d,ssl:%d,listen:%d,lport:%-5d fd:%-5d revents:%x\n", i, cdata[i].is_udp ? "udp" : "tcp", cdata[i].do_ssl, cdata[i].is_ssl, cdata[i].listener, cdata[i].listen_port, ufds[i].fd, ufds[i].revents); }
void rfSaveDevices(void) { //0x4002 3C00 - 0x4002 3FFF // Unlock flash to erase and write if (rfDevices == NULL) { QueueResponse((char *)"Nothing to store\n"); return; } taskENTER_CRITICAL(); FLASH->KEYR = 0x45670123; FLASH->KEYR = 0xCDEF89AB; while (FLASH->SR & FLASH_SR_BSY) { }; //Wait untill memory ready for erase FLASH->CR |= FLASH_CR_SER; //Erase one sector FLASH->CR |= (FLASH_CR_SNB_0 | FLASH_CR_SNB_1 | FLASH_CR_SNB_2); //Erase sector 7 FLASH->CR |= FLASH_CR_STRT; while (FLASH->SR & FLASH_SR_BSY) { }; //Wait untill memory ready FLASH->CR &= ~FLASH_CR_SER; FLASH->CR |= FLASH_PSIZE_WORD; FLASH->CR |= FLASH_CR_PG; //Allow flash programming while (FLASH->SR & FLASH_SR_BSY) { }; dLink Cur = rfDevices; uint32_t Address = 0x08060000; while (Cur != NULL) { if (Cur->Type == NULL) { Cur = Cur->Next; continue; } *(__IO uint32_t *)Address = ((uint32_t)Cur->Address) | ((uint32_t)Cur->Type << 16) | ((uint32_t)Cur->Config << 24); Address += 4; *(__IO uint32_t *)Address = (uint32_t)Cur->Salt; Address += 4; Cur = Cur->Next; }; while (FLASH->SR & FLASH_SR_BSY) { }; dxprintf("SR: %d\n", FLASH->SR); FLASH->CR &= ~(FLASH_CR_PG); taskEXIT_CRITICAL(); }
/** * \brief Add text response into output queue * * \param [in] Response Text of response * \return void */ void QueueResponse(char *Response) { uint8_t Length = strlen(Response); uint8_t *Buf = (uint8_t *)pvPortMalloc(Length); if (Buf == NULL) { dxprintf("Error: can't allocate memory!\n"); } else { memcpy(Buf, Response, Length); xCmdResponse qCmdResponse; qCmdResponse.cLength = Length; qCmdResponse.cData = Buf; if (xQueueSend(U2TxQueueHandle, &(qCmdResponse), portMAX_DELAY) != pdPASS) { // Error, the queue if full!!! // TODO: handle this error dxprintf("Error to add to U2TxQueue!\n"); } } }
/** * \brief Parse user entered command and process it * * \param [in] Data User entered command with parameters * \param [in] Length Length of command * \return void * */ void ProcessATCommand(char *Data, uint8_t Length) { char * Parameters = strchr(Data, ':'); uint8_t ParametersPos = 0x00; if (Parameters == NULL) { dxprintf("Len: %d, no params\n", Length); } else { ParametersPos = Parameters - Data; dxprintf("Len: %d, Params: %d\n", Length, ParametersPos); } int CommandProcessed = 0; /** * CMD: PING - ping myself */ if (strncmp(&Data[3], (char *)"PING", 4) == 0) { CmdPING(); CommandProcessed = 1; } /** * CMD: FIND_NEW_DEVICE or FIND - find new device. Device should be able to accept 'discover' command. */ if (strncmp(&Data[3], (char *)"FIND_NEW_DEVICE", 15) == 0 || strncmp(&Data[3], (char *)"FIND", 4) == 0) { CmdFindNewDevice(); CommandProcessed = 1; } /** * CMD: CHECK_TX_STATUS - Check current transmitter status */ if (strncmp(&Data[3], (char *)"CHECK_TX_STATUS", 15) == 0) { QueueResponse((char *)"<CHECK_TX_STATUS\n"); CmdCheckTxRxStatus(CHIP_Tx); CommandProcessed = 1; } /** * CMD: CHECK_RX_STATUS - Check current receiver status */ if (strncmp(&Data[3], (char *)"CHECK_RX_STATUS", 15) == 0) { QueueResponse((char *)"<CHECK_RX_STATUS\n"); CmdCheckTxRxStatus(CHIP_Rx); CommandProcessed = 1; } /** * CMD: HANDLE_TX_STATUS - Check and handle current transmitter status */ if (strncmp(&Data[3], (char *)"HANDLE_TX_STATUS", 16) == 0) { QueueResponse((char *)"<HANDLE_TX_STATUS\n"); CmdHandleTxRxStatus(CHIP_Tx); CommandProcessed = 1; } /** * CMD: HANDLE_RX_STATUS - Check and handle current receiver status */ if (strncmp(&Data[3], (char *)"HANDLE_RX_STATUS", 16) == 0) { QueueResponse((char *)"<HANDLE_RX_STATUS\n"); CmdHandleTxRxStatus(CHIP_Rx); CommandProcessed = 1; } /** * CMD: ADD_DEVICE - Add new device into device list. This command doesn't send any data to device. */ if (strncmp(&Data[3], (char *)"ADD_DEVICE", 10) == 0) { QueueResponse((char *)"<ADD_DEVICE\n"); rfCreateDevice(); CommandProcessed = 1; } /** * CMD: LIST_DEVICES - print loaded devices */ if (strncmp(&Data[3], (char *)"LIST_DEVICES", 12) == 0) { QueueResponse((char *)"<LIST_DEVICES\n"); rfListDevices(); CommandProcessed = 1; } /** * CMD: SAVE_DEVICES - save current devices */ if (strncmp(&Data[3], (char *)"SAVE_DEVICES", 12) == 0) { QueueResponse((char *)"<SAVE_DEVICES\n"); rfSaveDevices(); QueueResponse((char *)"<DONE\n"); CommandProcessed = 1; } /** * CMD: LOAD_DEVICES - load saved devices */ if (strncmp(&Data[3], (char *)"LOAD_DEVICES", 12) == 0) { QueueResponse((char *)"<LOAD_DEVICES\n"); rfLoadDevices(); rfListDevices(); QueueResponse((char *)"<DONE\n"); CommandProcessed = 1; } /** * CMD: PREPARE_TEST_DEVICES - generate list of dummy devices */ if (strncmp(&Data[3], (char *)"PREPARE_TEST_DEVICES", 20) == 0) { QueueResponse((char *)"<PREPARE_TEST_DEVICES\n"); rfPrepareTestDevices(); QueueResponse((char *)"<DONE\n"); CommandProcessed = 1; } /** * CMD: PING_DEVICE - ping one device */ if (strncmp(&Data[3], (char *)"PING_DEVICE", 11) == 0) { QueueResponse((char *)"***PING_DEVICE NOT IMPLEMENTED!!!***"); CommandProcessed = 1; } /** * CMD: PING_ALL_DEVICES - ping all loaded devices */ if (strncmp(&Data[3], (char *)"PING_ALL_DEVICES", 16) == 0) { QueueResponse((char *)"<PING_ALL_DEVICES\n"); rfPingAllDevices(); QueueResponse((char *)"<DONE\n"); CommandProcessed = 1; } /** * CMD: SELF_TEST - run self tests */ if (strncmp(&Data[3], (char *)"SELF_TEST", 9) == 0) { QueueResponse((char *)"***SELF_TEST NOT IMPLEMENTED!!!***"); CommandProcessed = 1; } /** * CMD: SEND_DATA - send data to given device */ if (strncmp(&Data[3], (char *)"SEND_DATA", 9) == 0) { QueueResponse((char *)"<SEND_DATA\n"); char Parameters[Length - ParametersPos + 1]; int DevAddress = 0x00; uint8_t ArgumentsLength = Length - ParametersPos - 6; char Arguments[ArgumentsLength]; strlcpy(Parameters, &Data[ParametersPos + 1], Length - ParametersPos); if (sscanf(Parameters, "%4x:%s", &DevAddress, Arguments) > 0) { dLink Device = rfGetDevice(DevAddress); if (Device == NULL) { QueueResponse((char *)">ERROR: Device with given address not linked!\n\n"); } else if (Device->Type == 0x00) { QueueResponse((char *)">ERROR: Device type not valid!\n\n"); } else { rfSendData(rfCMD_W_DATA, Device, Arguments); } } else { QueueResponse((char *)">ERROR: Device address not set!\n\n"); } CommandProcessed = 1; } if (CommandProcessed == 0) { QueueResponse((char *)">ERROR: Unknown command!\n\n"); } }