Skip to content

Commit

Permalink
Merge branch 'master' of github.com:FujiNetWIFI/fujinet-platformio
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffpiep committed Oct 6, 2023
2 parents bb78cfe + 606f8b0 commit baf1ca3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 28 deletions.
19 changes: 12 additions & 7 deletions lib/bus/adamnet/adamnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static void adamnet_reset_intr_task(void *arg)
}

b->reset();
vTaskDelay(1);
vTaskDelay(1/portTICK_PERIOD_MS);
}
}

Expand Down Expand Up @@ -160,27 +160,32 @@ void virtualDevice::reset()
Debug_printf("No Reset implemented for device %u\n", _devnum);
}

void virtualDevice::adamnet_response_ack()
void virtualDevice::adamnet_response_ack(bool doNotWaitForIdle)
{
int64_t t = esp_timer_get_time() - AdamNet.start_time;

if (t < 300)
if (!doNotWaitForIdle)
{
AdamNet.wait_for_idle();
adamnet_send(0x90 | _devnum);
}
else

if (t < 300)
{
adamnet_send(0x90 | _devnum);
}
}

void virtualDevice::adamnet_response_nack()
void virtualDevice::adamnet_response_nack(bool doNotWaitForIdle)
{
int64_t t = esp_timer_get_time() - AdamNet.start_time;

if (t < 300)
if (!doNotWaitForIdle)
{
AdamNet.wait_for_idle();
}

if (t < 300)
{
adamnet_send(0xC0 | _devnum);
}
}
Expand Down
6 changes: 4 additions & 2 deletions lib/bus/adamnet/adamnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,15 @@ class virtualDevice

/**
* @brief acknowledge, but not if cmd took too long.
* @param doNotWaitForIdle do not wait for idle if true.
*/
virtual void adamnet_response_ack();
virtual void adamnet_response_ack(bool doNotWaitForIdle=false);

/**
* @brief non-acknowledge, but not if cmd took too long
* param doNotWaitForIdle do not wait for idle if true.
*/
virtual void adamnet_response_nack();
virtual void adamnet_response_nack(bool doNotWaitForIdle=false);

/**
* @brief acknowledge if device is ready, but not if cmd took too long.
Expand Down
77 changes: 58 additions & 19 deletions lib/device/adamnet/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ void adamNetwork::open(unsigned short s)

// Associate channel mode
json.setProtocol(protocol);

// Clear response
memset(response, 0, sizeof(response));
response_len = 0;
}

/**
Expand Down Expand Up @@ -208,6 +212,9 @@ void adamNetwork::close()
// Delete the protocol object
delete protocol;
protocol = nullptr;

memset(response, 0, sizeof(response));
response_len = 0;
}

/**
Expand Down Expand Up @@ -387,6 +394,9 @@ void adamNetwork::set_prefix(unsigned short s)
}

Debug_printf("Prefix now: %s\n", prefix.c_str());

response_len = 0;
memset(response, 0, sizeof(response));
}

/**
Expand Down Expand Up @@ -449,6 +459,9 @@ void adamNetwork::del(uint16_t s)
statusByte.bits.client_error = true;
return;
}

memset(response, 0, sizeof(response));
response_len = 0;
}

void adamNetwork::rename(uint16_t s)
Expand All @@ -472,6 +485,9 @@ void adamNetwork::rename(uint16_t s)
statusByte.bits.client_error = true;
return;
}

memset(response, 0, sizeof(response));
response_len = 0;
}

void adamNetwork::mkdir(uint16_t s)
Expand All @@ -495,6 +511,9 @@ void adamNetwork::mkdir(uint16_t s)
statusByte.bits.client_error = true;
return;
}

memset(response, 0, sizeof(response));
response_len = 0;
}

void adamNetwork::channel_mode()
Expand All @@ -521,25 +540,24 @@ void adamNetwork::channel_mode()
}

Debug_printf("adamNetwork::channel_mode(%u)\n", m);
AdamNet.start_time = esp_timer_get_time();
adamnet_response_ack();
memset(response, 0, sizeof(response));
response_len = 0;
}

void adamNetwork::json_query(unsigned short s)
{
uint8_t *c = (uint8_t *)malloc(s);
memset(response, 0, sizeof(response));
response_len = 0;

adamnet_recv_buffer(c, s);
adamnet_recv_buffer(response, s);
adamnet_recv(); // CK

AdamNet.start_time = esp_timer_get_time();
adamnet_response_ack();

json.setReadQuery(std::string((char *)c, s), cmdFrame.aux2);

Debug_printv("adamNetwork::json_query(%s)\n", c);
json.setReadQuery(std::string((char *)response, s), cmdFrame.aux2);

free(c);
Debug_printv("adamNetwork::json_query(%s)\n", response);
}

void adamNetwork::json_parse()
Expand All @@ -548,6 +566,8 @@ void adamNetwork::json_parse()
AdamNet.start_time = esp_timer_get_time();
adamnet_response_ack();
json.parse();
memset(response, 0, sizeof(response));
response_len = 0;
}

/**
Expand Down Expand Up @@ -621,11 +641,16 @@ void adamNetwork::adamnet_special_00(unsigned short s)
cmdFrame.aux1 = adamnet_recv();
cmdFrame.aux2 = adamnet_recv();

adamnet_recv(); // CK

AdamNet.start_time = esp_timer_get_time();
adamnet_response_ack();

protocol->special_00(&cmdFrame);
inq_dstats = 0xff;

response_len = 0;
memset(response, 0, sizeof(response));
}

/**
Expand All @@ -639,12 +664,17 @@ void adamNetwork::adamnet_special_40(unsigned short s)
cmdFrame.aux1 = adamnet_recv();
cmdFrame.aux2 = adamnet_recv();

adamnet_recv(); // CK

if (protocol->special_40(response, 1024, &cmdFrame) == false)
adamnet_response_ack();
else
adamnet_response_nack();

inq_dstats = 0xff;

response_len = 0;
memset(response, 0, sizeof(response));
}

/**
Expand All @@ -666,12 +696,17 @@ void adamNetwork::adamnet_special_80(unsigned short s)

Debug_printf("adamNetwork::adamnet_special_80() - %s\n", spData);

adamnet_recv(); // CK

// Do protocol action and return
if (protocol->special_80(spData, SPECIAL_BUFFER_SIZE, &cmdFrame) == false)
adamnet_response_ack();
else
adamnet_response_nack();
inq_dstats = 0xff;

memset(response, 0, sizeof(response));
response_len = 0;
}

void adamNetwork::adamnet_response_status()
Expand Down Expand Up @@ -724,7 +759,7 @@ void adamNetwork::adamnet_control_send()
case '0':
get_prefix();
break;
case 'E':
case 'E':
get_error();
break;
case 'O':
Expand Down Expand Up @@ -814,23 +849,30 @@ void adamNetwork::adamnet_control_receive_channel_json()
}
}

void adamNetwork::adamnet_control_receive_channel_protocol()
inline void adamNetwork::adamnet_control_receive_channel_protocol()
{
NetworkStatus ns;

AdamNet.start_time = esp_timer_get_time();

if ((protocol == nullptr) || (receiveBuffer == nullptr))
{
// adamnet_response_nack();
adamnet_response_nack(true);
return; // Punch out.
}

// Get status
protocol->status(&ns);

if (!ns.rxBytesWaiting)
{
AdamNet.start_time = esp_timer_get_time();
adamnet_response_nack(true);
return;
}
else
{
AdamNet.start_time = esp_timer_get_time();
adamnet_response_ack(true);
}

// Truncate bytes waiting to response size
ns.rxBytesWaiting = (ns.rxBytesWaiting > 1024) ? 1024 : ns.rxBytesWaiting;
Expand All @@ -840,6 +882,7 @@ void adamNetwork::adamnet_control_receive_channel_protocol()
{
statusByte.bits.client_error = true;
err = protocol->error;
adamnet_response_nack();
return;
}
else // everything ok
Expand All @@ -851,7 +894,7 @@ void adamNetwork::adamnet_control_receive_channel_protocol()
}
}

void adamNetwork::adamnet_control_receive()
inline void adamNetwork::adamnet_control_receive()
{
AdamNet.start_time = esp_timer_get_time();

Expand All @@ -861,10 +904,6 @@ void adamNetwork::adamnet_control_receive()
adamnet_response_ack();
return;
}
else if ((response_len == 0) && (channelMode == PROTOCOL)) // this is hacky as hell
{
adamnet_response_nack();
}

switch (channelMode)
{
Expand All @@ -890,7 +929,7 @@ void adamNetwork::adamnet_response_send()
}
else
adamnet_send(0xC0 | _devnum); // NAK!

memset(response, 0, response_len);
response_len = 0;
}
Expand Down

0 comments on commit baf1ca3

Please sign in to comment.