Skip to content

Commit

Permalink
Merge branch 'post-sequentially'
Browse files Browse the repository at this point in the history
  • Loading branch information
mash committed Aug 31, 2014
2 parents 0641425 + d38410a commit 424fa95
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
5 changes: 3 additions & 2 deletions firmware/src/IRKit/GSwifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ void GSwifi::loop() {

GSLOG_PRINT("!E4 "); GSLOG_PRINTLN(i);

// request timeout means GS is in trouble,
// request (from us) timeout means GS is in trouble,
// and it's easy to just rescue ourselves to reset
wifi_hardware_reset();
return;
Expand Down Expand Up @@ -633,6 +633,7 @@ inline void GSwifi::setCidIsRequest(int8_t cid, bool is_request) {
}
}

// request against us
inline bool GSwifi::cidIsRequest(int8_t cid) {
return cid_bitmap_ & _BV(cid);
}
Expand Down Expand Up @@ -746,7 +747,7 @@ void GSwifi::parseLine () {
int8_t cid = x2i(buf[10]); // 2nd cid = HTTP client cid
ASSERT((0 <= cid) && (cid <= 16));

setCidIsRequest(cid, true); // request against us
setCidIsRequest(cid, true);
content_lengths_[ cid ] = 0;

// don't close other connections,
Expand Down
8 changes: 5 additions & 3 deletions firmware/src/IRKit/IRKit.ino
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ void setup() {

pinMode(LDO33_ENABLE, OUTPUT);
wifi_hardware_reset();
irkit_http_init();

// add your own code here!!
}
Expand Down Expand Up @@ -181,6 +180,7 @@ void process_commands() {
irkit_httpclient_post_keys();
break;
case COMMAND_SETUP:
irkit_http_init();
gs.setup( &on_disconnect, &on_reset );

// vv continues
Expand Down Expand Up @@ -224,8 +224,10 @@ void on_ir_receive() {
IRLOG_PRINTLN("!E31");
return;
}
color.setLedColor( 0, 0, 1, true, 1 ); // received: blue blink for 1sec
irkit_httpclient_post_messages();
int8_t cid = irkit_httpclient_post_messages();
if (cid >= 0) {
color.setLedColor( 0, 0, 1, true, 1 ); // received: blue blink for 1sec
}
}
}

Expand Down
19 changes: 18 additions & 1 deletion firmware/src/IRKit/IRKitHTTPHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ static volatile uint8_t polling_timer = TIMER_OFF;
static uint32_t newest_message_id = 0; // on memory only should be fine
static int8_t post_keys_cid;
static int8_t polling_cid = CID_UNDEFINED; // GET /m continues forever
static bool is_posting_message = false;

#define POST_DOOR_BODY_LENGTH 61
#define POST_KEYS_BODY_LENGTH 42
Expand Down Expand Up @@ -235,6 +236,8 @@ static int8_t on_post_messages_response(int8_t cid, uint16_t status_code, GSwifi
gs.bufferClear();
}

is_posting_message = false;

if (state != GSwifi::GSREQUESTSTATE_RECEIVED) {
return 0;
}
Expand Down Expand Up @@ -396,7 +399,7 @@ int8_t irkit_httpclient_get_messages() {
return gs.get(path, &on_get_messages_response, 50);
}

int8_t irkit_httpclient_post_messages() {
int8_t irkit_httpclient_post_messages_() {
// post body is IR data, move devicekey parameter to query, for implementation simplicity
// /p?devicekey=C7363FDA0F06406AB11C29BA41272AE3&freq=38
char path[54];
Expand All @@ -415,6 +418,19 @@ int8_t irkit_httpclient_post_messages() {
return cid;
}

// stack memory to initiate a HTTP request is not small;
// do POST /p sequentially
int8_t irkit_httpclient_post_messages() {
if (is_posting_message) {
return -1;
}
int8_t cid = irkit_httpclient_post_messages_();
if (cid >= 0) {
is_posting_message = true; // this function is not called inside ISR; safe to set here
}
return cid;
}

int8_t irkit_httpclient_post_keys() {
// devicekey=[0-9A-F]{32}
char body[POST_KEYS_BODY_LENGTH+1];
Expand Down Expand Up @@ -454,6 +470,7 @@ void irkit_http_init() {
irkit_httpserver_register_handler();

polling_cid = CID_UNDEFINED;
is_posting_message = false;
}

void irkit_http_on_timer() {
Expand Down

0 comments on commit 424fa95

Please sign in to comment.