Skip to content

Commit

Permalink
TuyaMCU signal query packet check
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Nov 1, 2023
1 parent a809a21 commit b7e6659
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
37 changes: 28 additions & 9 deletions src/driver/drv_tuyaMCU.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ TuyaMCU version 0, aka low power protocol, documented here:
#define TUYA_CMD_SET_RSSI 0x24
#define TUYA_CMD_NETWORK_STATUS 0x2B

#define TUYA_V0_CMD_PRODUCTINFORMATION 0x01
#define TUYA_V0_CMD_NETWEORKSTATUS 0x02
#define TUYA_V0_CMD_RESETWIFI 0x03
#define TUYA_V0_CMD_RESETWIFI_AND_SEL_CONF 0x04
#define TUYA_V0_CMD_REALTIMESTATUS 0x05
#define TUYA_V0_CMD_OBTAINLOCALTIME 0x06
#define TUYA_V0_CMD_RECORDSTATUS 0x08
#define TUYA_V0_CMD_OBTAINDPCACHE 0x10
#define TUYA_V0_CMD_QUERYSIGNALSTRENGTH 0x0B

#define TUYA_NETWORK_STATUS_AP_MODE 0x01
#define TUYA_NETWORK_STATUS_NOT_CONNECTED 0x02
#define TUYA_NETWORK_STATUS_CONNECTED_TO_ROUTER 0x03
Expand Down Expand Up @@ -119,6 +129,8 @@ const char* TuyaMCU_GetCommandTypeLabel(int t) {
return "NetworkStatus";
if (t == TUYA_CMD_SET_RSSI)
return "SetRSSI";
if (t == TUYA_V0_CMD_QUERYSIGNALSTRENGTH)
return "QuerySignalStrngth";
return "Unknown";
}
typedef struct rtcc_s {
Expand Down Expand Up @@ -681,6 +693,12 @@ commandResult_t Cmd_TuyaMCU_Send_RSSI(const void* context, const char* cmd, cons
TuyaMCU_Send_RSSI(toSend);
return CMD_RES_OK;
}
void TuyaMCU_Send_SignalStrength(byte state, byte strength) {
byte payload_buffer[2];
payload_buffer[0] = state;
payload_buffer[1] = strength;
TuyaMCU_SendCommandWithData(TUYA_V0_CMD_QUERYSIGNALSTRENGTH, payload_buffer, sizeof(payload_buffer));
}
void TuyaMCU_Send_SetTime(struct tm* pTime, bool bSensorMode) {
byte payload_buffer[8];

Expand Down Expand Up @@ -1433,15 +1451,6 @@ void TuyaMCU_ResetWiFi() {
g_openAP = 1;
}
}
#define TUYA_V0_CMD_PRODUCTINFORMATION 0x01
#define TUYA_V0_CMD_NETWEORKSTATUS 0x02
#define TUYA_V0_CMD_RESETWIFI 0x03
#define TUYA_V0_CMD_RESETWIFI_AND_SEL_CONF 0x04
#define TUYA_V0_CMD_REALTIMESTATUS 0x05
#define TUYA_V0_CMD_OBTAINLOCALTIME 0x06
#define TUYA_V0_CMD_RECORDSTATUS 0x08
#define TUYA_V0_CMD_OBTAINDPCACHE 0x10

void TuyaMCU_V0_SendDPCacheReply() {
#if 0
// send empty?
Expand Down Expand Up @@ -1631,6 +1640,16 @@ void TuyaMCU_ProcessIncoming(const byte* data, int len) {
}
else {

}
break;
case TUYA_V0_CMD_QUERYSIGNALSTRENGTH:
if (version == 0) {
addLogAdv(LOG_INFO, LOG_FEATURE_TUYAMCU, "TuyaMCU_ProcessIncoming: received TUYA_V0_CMD_QUERYSIGNALSTRENGTH, so sending back signal");

TuyaMCU_Send_SignalStrength(1,80);
}
else {

}
break;
case TUYA_V0_CMD_OBTAINLOCALTIME:
Expand Down
30 changes: 30 additions & 0 deletions src/selftest/selftest_tuyaMCU_batteryPowered.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,34 @@ void Test_TuyaMCU_BatteryPowered_DPcacheFeature2() {

SELFTEST_ASSERT_HAS_SENT_UART_STRING("55 AA 00 10 00 02 0100 12");
}
void Test_TuyaMCU_BatteryPowered_QuerySignalStrength() {
// reset whole device
SIM_ClearOBK(0);
SIM_UART_InitReceiveRingBuffer(1024);

CMD_ExecuteCommand("startDriver TuyaMCU", 0);
CMD_ExecuteCommand("startDriver tmSensor", 0);

// nothing is sent by OBK at that point
SELFTEST_ASSERT_HAS_UART_EMPTY();
/*
Pressed: 1. Send query product information
(simulated delays are disabled)
Sent: 55 AA 00 01 00 00 00
Sent: Ver=0, Cmd=QueryInfo, Len=0, CHECKSUM OK
*/
Sim_RunSeconds(3.0f, false);
SELFTEST_ASSERT_HAS_SENT_UART_STRING("55 AA 00 01 00 00 00");
// nothing is sent by OBK at that point
SELFTEST_ASSERT_HAS_UART_EMPTY();

CMD_ExecuteCommand("uartFakeHex 55 AA 00 0B 00 00 0A", 0);
Sim_RunSeconds(0.1f, false);

SELFTEST_ASSERT_HAS_SENT_UART_STRING("55 AA 00 0B 00 02 01 50 5D");
// nothing is sent by OBK at that point
SELFTEST_ASSERT_HAS_UART_EMPTY();
}
void Test_TuyaMCU_BatteryPowered() {
Test_TuyaMCU_BatteryPowered_Style2();
Test_TuyaMCU_BatteryPowered_Style1();
Expand All @@ -336,6 +364,8 @@ void Test_TuyaMCU_BatteryPowered() {
Test_TuyaMCU_BatteryPowered_DPcacheFeature2();
Test_TuyaMCU_BatteryPowered_DPcacheFeature3();
Test_TuyaMCU_BatteryPowered_DPcacheFeature4();

Test_TuyaMCU_BatteryPowered_QuerySignalStrength();
}

#endif
Expand Down

0 comments on commit b7e6659

Please sign in to comment.