Rick McConney
/
stripReader
Proximity strip reader
Fork of StarterKit by
Diff: main.cpp
- Revision:
- 40:45e4636d20cc
- Parent:
- 39:3bbb3dbb531b
- Child:
- 41:85a736a9b900
--- a/main.cpp Tue Sep 27 15:45:21 2016 +0000 +++ b/main.cpp Thu Sep 29 14:19:53 2016 +0000 @@ -45,7 +45,7 @@ int mdm_dbgmask = MDM_DBG_OFF; Serial pc(USBTX, USBRX); -SerialBuffered mdm(PTD3, PTD2, 128); +SerialBuffered mdm(PTD3, PTD2, 512); DigitalOut led_green(LED_GREEN); DigitalOut led_red(LED_RED); DigitalOut led_blue(LED_BLUE); @@ -123,7 +123,7 @@ break; } } - wait_ms(1); + //wait_ms(1); } buff[len] = (char)NULL; @@ -324,6 +324,65 @@ int mdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len) { static char cmd_buf[3200]; // Need enough room for the WNC sockreads (over 3000 chars) size_t n = strlen(cmd); + + + if (cmd && n > 0) { + if (mdm_dbgmask & MDM_DBG_AT_CMDS) { + printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd); + } + while (n--) { + // pc.printf("%c",*cmd); + mdm.putc(*cmd++); + + //wait_ms(1); + + }; + mdm.putc('\r'); + + // wait_ms(1); + mdm.putc('\n'); + // wait_ms(1); + } + + if (rsp_list) { + rsp->erase(); // Clean up from prior cmd response + *len = 0; + Timer timer; + timer.start(); + while (timer.read_ms() < timeout_ms) { + int lenCmd = mdm_getline(cmd_buf, sizeof(cmd_buf), timeout_ms - timer.read_ms()); + + if (lenCmd == 0) + continue; + + if (lenCmd < 0) + return MDM_ERR_TIMEOUT; + else { + *len += lenCmd; + *rsp += cmd_buf; + } + + if (mdm_dbgmask & MDM_DBG_AT_CMDS) { + printf(MAG "ATRSP: " DEF "<-- " CYN "%s" DEF "\n", cmd_buf); + } + + int rsp_idx = 0; + while (rsp_list[rsp_idx]) { + if (strcasecmp(cmd_buf, rsp_list[rsp_idx]) == 0) { + return rsp_idx; + } + rsp_idx++; + } + } + return MDM_ERR_TIMEOUT; + } + pc.printf("D %s",rsp); + return MDM_OK; +} + +int oldmdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len) { + static char cmd_buf[3200]; // Need enough room for the WNC sockreads (over 3000 chars) + size_t n = strlen(cmd); if (cmd && n > 0) { if (mdm_dbgmask & MDM_DBG_AT_CMDS) { printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd); @@ -459,12 +518,12 @@ //******************************************************************************************************************************************** //* Process JSON response messages //******************************************************************************************************************************************** -bool extract_JSON(char* search_field, char* found_string) +bool extract_reply(char* search_field, char* found_string) { char* beginquote; char* endquote; - beginquote = strchr(search_field, '{'); //start of JSON - endquote = strchr(search_field, '}'); //end of JSON + beginquote = strstr(search_field, "\r\n\r\n"); //start of data + endquote = strchr(search_field, '\0'); if (beginquote != 0) { uint16_t ifoundlen; @@ -475,20 +534,11 @@ found_string[ifoundlen] = 0; //null terminate return true; } - else - { - endquote = strchr(search_field, '\0'); //end of string... sometimes the end bracket is missing - ifoundlen = (uint16_t) (endquote - beginquote) + 1; - strncpy(found_string, beginquote, ifoundlen ); - found_string[ifoundlen] = 0; //null terminate - return false; - } } - else - { - return false; - } -} //extract_JSON + + return false; + +} //extract_reply void setPowerSave(bool on) { @@ -505,28 +555,7 @@ } } -void parse_JSON_PSM(char* json_string) -{ - char* beginquote; - char token[] = "\"PSM\":\""; - beginquote = strstr(json_string, token ); - if ((beginquote != 0)) - { - char mode = beginquote[strlen(token)]; - printf(GRN "PSM Found : %c" DEF "\r\n", mode); - switch(mode) - { - case 'T': - { setPowerSave(true); - break; - } - case 'F': - { setPowerSave(false); - break; - } - } - } -} + int secToTau(int time) @@ -616,7 +645,47 @@ send_wnc_cmd(cmd_str.c_str(), &pRespStr, WNC_TIMEOUT_MS); } +void checkSocket() +{ + string * pRespStr; + send_wnc_cmd("AT+CREG?", &pRespStr, WNC_TIMEOUT_MS); + pc.printf(pRespStr->c_str()); + +} + +void parse_reply(char* reply) +{ + char *tokens[5]; + int index = 0; + tokens[index++] = strtok(reply," "); + while( index < 5 ) + { + char* token = strtok(NULL, " "); + if(token == NULL) break; + tokens[index++] = token; + } + if(strcmp("PSM",tokens[1]) == 0) + { + + int t3412 = atoi(tokens[3]); + int t3324 = atoi(tokens[4]); + pc.printf("t3412 %d t3324 %d\r\n",t3412,t3324); + setTauTimer(t3412); + setActivityTimer(t3324); + + if(strcmp("true",tokens[2]) == 0) + { + pc.printf("PSM ON\r\n"); + setPowerSave(true); + } + else + { + pc.printf("PSM OFF\r\n"); + setPowerSave(false); + } + } +} int main() { @@ -659,6 +728,7 @@ //Software init software_init_mdm(); + // Resolve URL to IP address to connect to resolve_mdm(); @@ -670,6 +740,7 @@ setTauTimer(12*60*60); setActivityTimer(20); + setPowerSave(false); proximityStrip.init(); @@ -699,7 +770,9 @@ GenerateModemString(&modem_string[0]); printf(BLU "Sending to modem : %s" DEF "\r\n", modem_string); wakeModem(); + checkSocket(); sockwrite_mdm(modem_string); + wakeModem(); sockread_mdm(&MySocketData, 1024, 20); // If any non-zero response from server, make it GREEN one-time @@ -711,11 +784,11 @@ //only copy on sucessful send printf(BLU "Read back : %s" DEF "\r\n", &MySocketData[0]); - char myJsonResponse[512]; - if (extract_JSON(&MySocketData[0], &myJsonResponse[0])) + char reply[512]; + if (extract_reply(&MySocketData[0], &reply[0])) { - printf(GRN "JSON : %s" DEF "\r\n", &myJsonResponse[0]); - parse_JSON_PSM(&myJsonResponse[0]); + printf(GRN "JSON : %s" DEF "\r\n", &reply[0]); + parse_reply(&reply[0]); } SetLedColor(0); // off }